# SPDX-License-Identifier: MIT """Rung 1: the GSU's map, the ROM buffer, RAM loads and stores (BK2 3.2, 7.1-7.3; U7, U13, U17, U29, U32).""" from __future__ import annotations from conftest import execute, make_gsu, run_to_stop, start from gsu_asm import ( GETB, GETBH, GETBL, GETBS, ROMB, SBK, STOP, ibt, inc, iwt, ldb, ldw, lm, lms, program, sm, sms, stb, stw, to, ) from gsu_python import GSU2 def gsu_with_rom(code, data: dict[int, int]): """``data``: ROM offset -> byte.""" g = make_gsu(code) rom = bytearray(g.rom) for offset, value in data.items(): rom[offset] = value return type(g)(bytes(rom), g.ram, model=GSU2) def test_map_lorom_hirom_and_mirror(): """U13: 00-3F:8000-FFFF LoROM, 00-3F:0000-7FFF the same halves, 40-5F linear.""" g = gsu_with_rom([0], {0x8123: 0x5A}) # LoROM 01:8123 assert g._read(0x01, 0x8123) == 0x5A assert g._read(0x01, 0x0123) == 0x5A # the mirror assert g._read(0x40, 0x8123) == 0x5A # linear: 40:8123 = offset 8123h assert g._read(0x65, 0x0000) == 0 # outside the map def test_getb_family(): """BK2 9.35-9.38: GETB zero-extends, GETBH/GETBL keep the other byte, GETBS sign-extends. R14 written by IWT starts the buffer read at the end of the IWT.""" code = program(iwt(14, 0x9000), GETB, to(1), GETBH, to(2), GETBL, to(3), GETBS, STOP) g = gsu_with_rom(code, {0x1000: 0x85}) g.r[0] = 0x1234 start(g) run_to_stop(g) assert g.r[0] == 0x0085 # GETB into R0 (Sreg for the rest stays R0 = 0085h) assert g.r[1] == 0x8585 assert g.r[2] == 0x0085 assert g.r[3] == 0xFF85 def test_romb_waits_then_gets_old_bank(): """U7: ROMB waits for the pending read, so a GETB after it, with no new R14 write, still returns [old ROMBR:R14] (fullsnes "Other Caches").""" code = program(iwt(14, 0x9000), ibt(0, 1), ROMB, GETB, STOP) g = gsu_with_rom(code, {0x1000: 0x11, 0x9000: 0x22}) # 00:9000 and 01:9000 start(g) run_to_stop(g) assert g.r[0] == 0x11 assert g.rombr == 1 code = program(iwt(14, 0x9000), ibt(0, 1), ROMB, inc(14), GETB, STOP) g = gsu_with_rom(code, {0x9001: 0x33}) start(g) run_to_stop(g) assert g.r[0] == 0x33 # after a new R14 write, the new bank def test_ldw_stw_and_odd_addresses(): """U17 (BK2 9.85): a word at an odd address: low byte there, high byte at address - 1.""" g = execute(program(stw(1), STOP), r0=0xBEEF, r1=0x0101) assert g.ram[0x0101] == 0xEF assert g.ram[0x0100] == 0xBE g3 = make_gsu(program(ldw(1), to(2), ldw(3), STOP)) g3.ram[0x0200:0x0202] = bytes([0x34, 0x12]) g3.r[1], g3.r[3] = 0x0200, 0x0201 start(g3) run_to_stop(g3) assert g3.r[0] == 0x1234 assert g3.r[2] == 0x3412 def test_bytes(): g = make_gsu(program(stb(1), ldb(2), STOP)) g.r[0], g.r[1], g.r[2] = 0xBEEF, 0x0010, 0x0020 g.ram[0x20] = 0x99 start(g) run_to_stop(g) assert g.ram[0x10] == 0xEF assert g.ram[0x11] == 0 assert g.r[0] == 0x0099 # LDB zero-extends def test_lm_sm_lms_sms(): """U32: LM/SM take a 16-bit address, LMS/SMS the operand x 2.""" g = make_gsu(program(lm(3, 0x1234), sm(0x2000, 3), lms(4, 0x01FE), sms(0x0010, 4), STOP)) g.ram[0x1234:0x1236] = bytes([0xCD, 0xAB]) g.ram[0x01FE:0x0200] = bytes([0x21, 0x43]) start(g) run_to_stop(g) assert g.r[3] == 0xABCD assert bytes(g.ram[0x2000:0x2002]) == bytes([0xCD, 0xAB]) assert g.r[4] == 0x4321 assert bytes(g.ram[0x0010:0x0012]) == bytes([0x21, 0x43]) def test_sbk_writes_back_to_the_last_address(): """BK2 example 6, U29: LM, INC, SBK writes back to 1234h.""" g = make_gsu(program(lm(0, 0x1234), inc(0), SBK, STOP)) g.ram[0x1234:0x1236] = bytes([0xFF, 0x00]) start(g) run_to_stop(g) assert bytes(g.ram[0x1234:0x1236]) == bytes([0x00, 0x01]) assert g.ramaddr == 0x1234 def test_ramb_selects_bank_71(): """RAMBR selects 70h/71h for data (BK2 9.73).""" from gsu_asm import RAMB g = make_gsu(program(ibt(0, 1), RAMB, iwt(0, 0x5555), stw(1), STOP), ram_size=0x20000) g.r[1] = 0x0040 start(g) run_to_stop(g) assert g.ram[0x10040] == 0x55 assert g.ram[0x40] == 0