further digging and basic emulator
This commit is contained in:
76
tests/test_emulator.py
Normal file
76
tests/test_emulator.py
Normal file
@@ -0,0 +1,76 @@
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
from h8536.emulator import (
|
||||
HEARTBEAT_FRAME,
|
||||
ON_CHIP_RAM_START,
|
||||
REGISTER_FIELD_START,
|
||||
SCI1_SCR,
|
||||
SCI1_SSR,
|
||||
SCI1_TDR,
|
||||
SCI_SCR_TE,
|
||||
SCI_SSR_TDRE,
|
||||
H8536Emulator,
|
||||
MemoryMap,
|
||||
SCI1,
|
||||
discover_rom_path,
|
||||
load_rom,
|
||||
)
|
||||
|
||||
|
||||
class EmulatorHarnessTest(unittest.TestCase):
|
||||
def test_memory_map_routes_rom_ram_register_and_external(self):
|
||||
memory = MemoryMap(bytes([0x12, 0x34, 0x56, 0x78]))
|
||||
|
||||
self.assertEqual(memory.read8(0x0001), 0x34)
|
||||
memory.write8(ON_CHIP_RAM_START, 0xA5)
|
||||
self.assertEqual(memory.read8(ON_CHIP_RAM_START), 0xA5)
|
||||
memory.write8(REGISTER_FIELD_START, 0x5A)
|
||||
self.assertEqual(memory.read8(REGISTER_FIELD_START), 0x5A)
|
||||
memory.write8(0xF000, 0x11)
|
||||
self.assertEqual(memory.read8(0xF000), 0x11)
|
||||
|
||||
def test_sci_transmit_capture_requires_enabled_transmitter(self):
|
||||
sci = SCI1()
|
||||
sci.write(SCI1_TDR, 0x33)
|
||||
self.assertEqual(sci.tx_bytes, [])
|
||||
self.assertFalse(sci.tx_events[-1].emitted)
|
||||
|
||||
sci.write(SCI1_SCR, sci.scr | SCI_SCR_TE)
|
||||
for byte in HEARTBEAT_FRAME:
|
||||
sci.write(SCI1_TDR, byte)
|
||||
|
||||
self.assertEqual(bytes(sci.tx_bytes), HEARTBEAT_FRAME)
|
||||
self.assertEqual(sci.tx_frames, [HEARTBEAT_FRAME])
|
||||
self.assertTrue(sci.saw_heartbeat())
|
||||
self.assertTrue(sci.read(SCI1_SSR) & SCI_SSR_TDRE)
|
||||
|
||||
def test_vector_decoding_uses_minimum_mode_reset_word(self):
|
||||
rom = bytearray(0x1004)
|
||||
rom[0:2] = b"\x10\x00"
|
||||
rom[0x1000] = 0x00
|
||||
|
||||
emulator = H8536Emulator(bytes(rom))
|
||||
|
||||
self.assertEqual(emulator.reset_vector(), 0x1000)
|
||||
self.assertEqual(emulator.cpu.pc, 0x1000)
|
||||
self.assertEqual(emulator.vectors[0x0000][1], 0x1000)
|
||||
|
||||
def test_harness_instantiates_on_repo_artifacts(self):
|
||||
root = Path(__file__).resolve().parents[1]
|
||||
rom_path = discover_rom_path(root)
|
||||
self.assertIsNotNone(rom_path)
|
||||
rom_bytes, loaded_path = load_rom(root=root)
|
||||
|
||||
emulator = H8536Emulator(rom_bytes)
|
||||
report = emulator.run(max_steps=4)
|
||||
|
||||
self.assertEqual(loaded_path, rom_path)
|
||||
self.assertEqual(emulator.reset_vector(), 0x1000)
|
||||
self.assertGreaterEqual(len(rom_bytes), 0x1002)
|
||||
self.assertEqual(report.steps, 4)
|
||||
self.assertFalse(report.heartbeat_seen)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
Reference in New Issue
Block a user