1
0

EMualtor adjustments

This commit is contained in:
Aiden
2026-05-25 20:42:45 +10:00
parent d2e7609bbf
commit 3ab79648ff
17 changed files with 3047 additions and 83 deletions

View File

@@ -0,0 +1,63 @@
import unittest
from h8536.emulator import H8536Emulator
def rom_with_reset(*, reset: int = 0x1000, size: int = 0x1100) -> bytearray:
rom = bytearray([0xFF] * size)
rom[0:2] = reset.to_bytes(2, "big")
return rom
class EmulatorAddressingTest(unittest.TestCase):
def test_txi_indexed_byte_load_uses_signed_word_displacement_and_full_index_register(self):
rom = rom_with_reset()
rom[0x1000:0x1004] = b"\xF0\xF8\x58\x80" # MOV:G.B @(-H'07A8,R0), R0
emulator = H8536Emulator(bytes(rom))
emulator.cpu.regs[0] = 5
emulator.memory.write8(0xF85D, 0xA6)
emulator.step()
self.assertEqual(emulator.cpu.regs[0], 0x00A6)
self.assertEqual(emulator.cpu.pc, 0x1004)
def test_txi_indexed_byte_load_preserves_destination_high_byte(self):
rom = rom_with_reset()
rom[0x1000:0x1004] = b"\xF0\xF8\x58\x80" # MOV:G.B @(-H'07A8,R0), R0
emulator = H8536Emulator(bytes(rom))
emulator.cpu.regs[0] = 0x0105
emulator.memory.write8(0xF95D, 0x4B)
emulator.step()
self.assertEqual(emulator.cpu.regs[0], 0x014B)
def test_extu_byte_clears_high_byte_before_txi_indexed_load(self):
rom = rom_with_reset()
rom[0x1000:0x1002] = b"\xA0\x12" # EXTU.B R0
rom[0x1002:0x1006] = b"\xF0\xF8\x58\x80" # MOV:G.B @(-H'07A8,R0), R0
emulator = H8536Emulator(bytes(rom))
emulator.cpu.regs[0] = 0x0105
emulator.memory.write8(0xF85D, 0xC7)
emulator.memory.write8(0xF95D, 0x99)
emulator.run(max_steps=2)
self.assertEqual(emulator.cpu.regs[0], 0x00C7)
self.assertEqual(emulator.cpu.pc, 0x1006)
def test_signed_byte_displacement_addresses_below_base(self):
rom = rom_with_reset()
rom[0x1000:0x1003] = b"\xE1\xFE\x81" # MOV:G.B @(H'-02,R1), R1
emulator = H8536Emulator(bytes(rom))
emulator.cpu.regs[1] = 0xFE90
emulator.memory.write8(0xFE8E, 0x37)
emulator.step()
self.assertEqual(emulator.cpu.regs[1], 0xFE37)
if __name__ == "__main__":
unittest.main()