# SPDX-License-Identifier: MIT """Instruction semantics against notaz's svpdoc.txt and MAME 0.285's ssp1601.cpp.""" from ssp1601_python import SSP1601, disassemble from ssp1601_python.cpu import FLAG_N, FLAG_Z A, X, Y, ST, STACK, PC, P = 3, 1, 2, 4, 5, 6, 7 class Machine: """A program in word memory and a recorder of the external bus.""" def __init__(self, *words: int) -> None: self.program = {0x400 + i: w for i, w in enumerate(words)} self.ext_log: list[tuple] = [] self.ext_values = dict.fromkeys(range(8), 0) self.cpu = SSP1601(self.read, self.ext_read, self.ext_write) def read(self, address: int) -> int: return self.program.get(address, 0) def ext_read(self, n: int) -> int: self.ext_log.append(("r", n)) return self.ext_values[n] def ext_write(self, n: int, value: int) -> None: self.ext_log.append(("w", n, value)) def run(self, steps: int = 1) -> SSP1601: for _ in range(steps): self.cpu.step() return self.cpu def ld(d: int, s: int) -> int: return (d << 4) | s def test_reset_state_and_the_null_register(): m = Machine(ld(X, 0)) # ld X, - (the "-" register reads $FFFF) cpu = m.run() assert cpu.pc == 0x401 and cpu.x == 0xFFFF def test_ldi_loads_a_word_and_takes_two_cycles(): m = Machine(0x0800 | ld(X, 0), 0x1234) assert m.cpu.step() == 2 and m.cpu.x == 0x1234 and m.cpu.pc == 0x402 def test_a_is_its_high_word_and_al_its_low(): m = Machine(0x0800 | ld(A, 0), 0x1234, 0x0800 | ld(15, 0), 0xABCD, ld(X, A), ld(Y, 15)) cpu = m.run(4) assert cpu.a == 0x1234ABCD assert cpu.x == 0x1234 and cpu.y == 0xABCD # AL drives the external bus, read or written (EXT7) assert ("w", 7, 0xABCD) in m.ext_log and ("r", 7) in m.ext_log def test_p_is_twice_the_signed_product_and_a_loads_all_of_it(): m = Machine(0x0800 | ld(X, 0), 0xFFFE, 0x0800 | ld(Y, 0), 3, ld(A, P), ld(Y, P)) cpu = m.run(5) assert cpu.a == (-2 * 3 * 2) & 0xFFFFFFFF # ld A, P moves all 32 bits assert cpu.y == 0xFFFF # ld Y, P is its high word def test_alu_operates_on_the_full_accumulator_and_sets_z_and_n(): # ldi A, $8000 ; subi A, $8000 -> 0 (Z) ; subi 1 -> negative (N) m = Machine(0x0800 | ld(A, 0), 0x8000, 0x2800, 0x8000, 0x3800 | 1) cpu = m.run(2) assert cpu.a == 0 and cpu.st & FLAG_Z and not cpu.st & FLAG_N m.cpu.pc = 0x404 m.cpu.step() assert m.cpu.a == 0xFFFF0000 and m.cpu.st & FLAG_N and not m.cpu.st & FLAG_Z def test_cmp_sets_flags_without_changing_a(): m = Machine(0x0800 | ld(A, 0), 5, 0x6800, 5) cpu = m.run(2) assert cpu.a == 5 << 16 and cpu.st & FLAG_Z def test_logical_ops_and_register_forms(): m = Machine(0x0800 | ld(A, 0), 0xFF00, 0x0800 | ld(X, 0), 0x0FF0, 0xA000 | X, 0xC800, 0x0001) cpu = m.run(3) assert cpu.a >> 16 == 0x0F00 # and A, X m.run(1) assert m.cpu.a >> 16 == 0x0F01 # ori A, 1 def test_p_and_a_as_32_bit_operands(): m = Machine(0x0800 | ld(X, 0), 2, 0x0800 | ld(Y, 0), 3, 0x8000 | P, 0x8000 | A) cpu = m.run(3) assert cpu.a == 12 # add A, P (P = 2*2*3, a whole 32 bits) m.run(1) assert m.cpu.a == 24 # add A, A doubles it def test_mod_shift_negate_abs_and_its_condition(): m = Machine(0x0800 | ld(A, 0), 0x8000, 0x9000 | 2, 0x9000 | 3, 0x9000 | 6, 0x9000 | 7) cpu = m.run(2) assert cpu.a == 0xC0000000 # arithmetic shift right m.run(1) assert m.cpu.a == 0x80000000 # shift left m.run(1) assert m.cpu.a == 0x80000000 and m.cpu.st & FLAG_N # neg of the most negative number m.run(1) assert m.cpu.a == 0x80000000 # abs of it too # a false condition does nothing: z=1 needs Z set n = Machine(0x0800 | ld(A, 0), 1, 0x9000 | 0x0150 | 3) n.run(3) assert n.cpu.a == 1 << 16 def test_call_bra_and_ret(): # call $410 ; (at $410) ret ; bra z=1 not taken ; bra always $420 m = Machine(0x4800, 0x410, 0x4D50, 0x999, 0x4C00, 0x420) m.program[0x410] = ld(PC, STACK) # ret cpu = m.run(1) assert cpu.pc == 0x410 and cpu.stack[0] == 0x402 and cpu.sp == 1 cpu = m.run(1) assert cpu.pc == 0x402 and cpu.sp == 0 m.run(1) assert m.cpu.pc == 0x404 # bra z=1 (f=1) with Z clear: not taken, the address skipped m.run(1) assert m.cpu.pc == 0x420 def test_stack_holds_six_and_wraps(): program = [] for value in range(1, 8): program += [0x0800 | ld(STACK, 0), value] m = Machine(*program) m.run(7) assert m.cpu.sp == 1 and m.cpu.stack[0] == 7 # the seventh push wrapped def test_pointer_modifiers_and_the_fixed_r3(): m = Machine(0x1800 | 5, 0x0400 | ld(0, 0) | 0x0000) # ldi r0, 5 m.cpu.ram[5] = 0x1111 m.cpu.ram[6] = 0x2222 m.cpu.r[0] = 5 # ld X, (r0+) then ld Y, (r0) m.program = {0x400: 0x0200 | ld(X, 0) | 0x0C, 0x401: 0x0200 | ld(Y, 0)} m.cpu.step() m.cpu.step() assert m.cpu.x == 0x1111 and m.cpu.y == 0x2222 and m.cpu.r[0] == 6 # r3|01 is RAM0[1], whatever r3 holds m.cpu.ram[1] = 0xBEEF m.program[0x402] = 0x0200 | ld(X, 0) | 3 | 0x04 m.cpu.step() assert m.cpu.x == 0xBEEF def test_rpl_makes_the_read_modifiers_circular_but_not_the_write_ones(): m = Machine(0x0200 | ld(X, 0) | 0x0C, 0x0400 | ld(X, 0) | 0x0C) m.cpu.st = 2 # modulo 4 m.cpu.r[0] = 3 m.cpu.step() assert m.cpu.r[0] == 0 # 3 -> 0 inside the 4-word ring m.cpu.r[0] = 3 m.cpu.step() assert m.cpu.r[0] == 4 # a write does not wrap (MAME) def test_mld_mpya_mpys_load_x_and_y_and_accumulate_the_old_product(): # mld: A = 0, Z set, X from RAM0[r0], Y from RAM1[r4] m = Machine(0xB700, 0x9700, 0x3700) m.cpu.ram[0] = 3 m.cpu.ram[0x100] = 4 m.cpu.a = 99 cpu = m.run(1) assert cpu.a == 0 and cpu.st & FLAG_Z and cpu.x == 3 and cpu.y == 4 m.cpu.ram[0] = 5 m.cpu.ram[0x100] = 6 cpu = m.run(1) # mpya: A += old P (3*4*2), then X, Y = 5, 6 assert cpu.a == 24 and cpu.x == 5 and cpu.y == 6 m.run(1) # mpys: A -= P (5*6*2) assert m.cpu.a == (24 - 60) & 0xFFFFFFFF def test_double_indirect_reads_program_memory_and_bumps_the_ram_word(): m = Machine(0x0A00 | ld(X, 0)) m.cpu.r[0] = 2 m.cpu.ram[2] = 0x500 m.program[0x500] = 0xCAFE cycles = m.cpu.step() assert m.cpu.x == 0xCAFE and m.cpu.ram[2] == 0x501 and cycles == 3 def test_ld_d_a_indirect_reads_program_at_the_accumulators_high_word(): m = Machine(0x0800 | ld(A, 0), 0x0500, 0x4A00 | ld(X, 0)) m.program[0x500] = 0x7777 m.run(2) assert m.cpu.x == 0x7777 def test_ldi_ri_uses_the_low_byte_and_ld_ri_ld_d_ri_move_pointers(): m = Machine(0x1A00 | 0x7F, 0x1400 | ld(0, 0) | 0, 0x1200 | ld(Y, 0) | 2) m.cpu.step() assert m.cpu.r[2] == 0x7F m.cpu.step() # ld r0, - ($FFFF -> $FF) assert m.cpu.r[0] == 0xFF m.cpu.step() assert m.cpu.y == 0x7F def test_ld_adr_a_and_ld_a_adr_address_both_banks(): m = Machine(0x0800 | ld(A, 0), 0x1234, 0x0E00 | 0x105, 0x0600 | 0x105) m.run(2) assert m.cpu.ram[0x105] == 0x1234 m.cpu.a = 0 m.cpu.step() assert m.cpu.a == 0x12340000 # high word only, flags untouched def test_writing_pc_costs_a_cycle_more(): m = Machine(0x0800 | ld(PC, 0), 0x0410) assert m.cpu.step() == 3 and m.cpu.pc == 0x410 def test_unhandled_ops_are_recorded_and_cost_a_cycle(): m = Machine(0x9410) # the flag opcodes MAME's disassembler names are not implemented assert m.cpu.step() == 1 and m.cpu.unhandled == [(0x400, 0x9410)] def test_disassembler_names_the_notaz_forms(): program = {0x400: 0x0800 | ld(X, 0), 0x401: 0x1234, 0x402: 0x4800, 0x403: 0x410, 0x404: 0xB700} read = lambda a: program.get(a, 0) # noqa: E731 assert disassemble(read, 0x400) == ("ld X, 1234", 2) assert disassemble(read, 0x402) == ("call always, 410", 2) assert disassemble(read, 0x404)[0] == "mld (r4), (r0), 1"