# SPDX-License-Identifier: MIT """Rung 1: OP/RT fields other than the ALU: P-SELECT, SRC, DST, DPL, DPHM, RPDCR, RT, LD, the multiplier, SGN, @KLR/@KLM, SR's write mask, and the contested orders (C3, C4, C14, C15, C16, U2). Expected values from the DS pages named; contested points name their entry. """ from __future__ import annotations import pytest from words import core, jp, ld, op from upd7725_python import FLAG_C, FLAG_S1, UPD7725, UPD96050_MODEL ADD, SHL2 = 5, 13 (SRC_TRB, SRC_A, SRC_B, SRC_TR, SRC_DP, SRC_RP, SRC_RO, SRC_SGN) = range(8) (SRC_DR, SRC_DRNF, SRC_SR, SRC_SIM, SRC_SIL, SRC_K, SRC_L, SRC_MEM) = range(8, 16) (DST_NON, DST_A, DST_B, DST_TR, DST_DP, DST_RP, DST_DR, DST_SR) = range(8) (DST_SOL, DST_SOM, DST_K, DST_KLR, DST_KLM, DST_L, DST_TRB, DST_MEM) = range(8, 16) def test_every_source_reaches_the_bus(): """DS p.21: each SRC moved to TR.""" expected = { SRC_TRB: 0x1111, # "NON" outputs TRB (note 1; U15) SRC_A: 0x2222, SRC_B: 0x3333, SRC_TR: 0x4444, SRC_DP: 0x0042, SRC_RP: 0x0005, SRC_RO: 0xBEEF, # ROM[RP] SRC_SGN: 0x8000, # SA1 = 0 (DS (16)) SRC_DR: 0x5555, SRC_DRNF: 0x5555, SRC_SR: 0x0400, SRC_SIM: 0x6666, SRC_SIL: 0x6666, # C4: no reversal by default SRC_K: 0x7777, SRC_L: 0x8888, SRC_MEM: 0x9999, # RAM[DP] } for source, value in expected.items(): dsp = core( op(src=source, dst=DST_A if source == SRC_TR else DST_TR), data_rom=[0] * 5 + [0xBEEF], trb=0x1111, a=0x2222, b=0x3333, tr=0x4444, dp=0x42, rp=5, dr=0x5555, sr=0x0400, si=0x6666, k=0x7777, l=0x8888, ) # fmt: skip dsp.ram[0x42] = 0x9999 dsp.step() assert (dsp.a if source == SRC_TR else dsp.tr) == value, source def test_rqm_set_by_dr_not_drnf(): """U7, DS SRC note 2: MOV x,DR sets RQM; DRNF does not.""" dsp = core(op(src=SRC_DR, dst=DST_NON)) dsp.step() assert dsp.sr & 0x8000 dsp = core(op(src=SRC_DRNF, dst=DST_A)) dsp.step() assert not dsp.sr & 0x8000 def test_every_destination(): """DS p.22: each DST from TR = 0x1234.""" for destination, check in { DST_A: lambda d: d.a, DST_B: lambda d: d.b, DST_TR: lambda d: d.tr, DST_DP: lambda d: d.dp, DST_RP: lambda d: d.rp, DST_DR: lambda d: d.dr, DST_SOL: lambda d: d.so, DST_SOM: lambda d: d.so, DST_K: lambda d: d.k, DST_L: lambda d: d.l, DST_TRB: lambda d: d.trb, DST_MEM: lambda d: d.ram[0x10], }.items(): dsp = core(op(src=SRC_A, dst=destination), a=0x1234, dp=0x10) dsp.step() expected = {DST_DP: 0x34, DST_RP: 0x234}.get(destination, 0x1234) # widths (U9) assert check(dsp) == expected, destination dsp = core(op(src=SRC_A, dst=DST_DR), a=1) dsp.step() assert dsp.sr & 0x8000 # @DR sets RQM (U7) def test_klr_and_klm(): """U5, DS DST notes 3-4: @KLR: K = bus, L = ROM[RP]; @KLM: L = bus, K = RAM[DP | 40h].""" dsp = core(op(src=SRC_A, dst=DST_KLR), data_rom=[0, 0, 0xABCD], a=0x1234, rp=2) dsp.step() assert (dsp.k, dsp.l) == (0x1234, 0xABCD) dsp = core(op(src=SRC_A, dst=DST_KLM), a=0x1234, dp=0x85) dsp.ram[0xC5] = 0x4321 dsp.step() assert (dsp.k, dsp.l) == (0x4321, 0x1234) def test_c15_klm_from_l_reads_first(): """C15: MOV @KLM,L (prohibited, used twice by the DSP-1): SRC is read before the move, so L keeps its old value and K comes from RAM.""" dsp = core(op(src=SRC_L, dst=DST_KLM), l=0x5555, dp=0x01) dsp.ram[0x41] = 0x7777 dsp.step() assert (dsp.k, dsp.l) == (0x7777, 0x5555) def test_sr_write_mask(): """U8: a program write keeps RQM, DRS and bits 6-2 (mask 907Ch).""" dsp = core(ld(0xFFFF, DST_SR), sr=0x9000) dsp.step() assert dsp.sr == 0x9000 | (0xFFFF & ~0x907C) dsp = core(ld(0x0000, DST_SR), sr=0x9000 | 0x6F83) dsp.step() assert dsp.sr == 0x9000 assert (dsp.p0, dsp.p1) == (0, 0) dsp = core(ld(0x0003, DST_SR)) dsp.step() assert (dsp.p0, dsp.p1) == (1, 1) # U17 def test_sgn_reads_flag_a_s1(): """U4, DS (16): 8000h if SA1 = 0, 7FFFh if SA1 = 1, whatever ASL selects.""" dsp = core(op(src=SRC_SGN, dst=DST_TR), flaga=FLAG_S1) dsp.step() assert dsp.tr == 0x7FFF dsp = core(op(src=SRC_SGN, dst=DST_TR), flagb=FLAG_S1) dsp.step() assert dsp.tr == 0x8000 @pytest.mark.parametrize( ("dpl", "dp", "expected"), [(1, 0x3F, 0x30), (1, 0x35, 0x36), (2, 0x30, 0x3F), (3, 0x3A, 0x30), (0, 0x3A, 0x3A)], ) def test_dpl_wraps_in_the_low_nibble(dpl, dp, expected): """U6, DS (5) and p.19: DPINC/DPDEC/DPCLR on DP bits 3-0, no carry into DPH.""" dsp = core(op(dpl=dpl), dp=dp) dsp.step() assert dsp.dp == expected def test_dphm_xors_bits_7_4(): """DS p.20: DPH XOR M; with DPINC both apply.""" dsp = core(op(dpl=1, dphm=0x5), dp=0x3F) dsp.step() assert dsp.dp == 0x60 def test_c16_dpl_on_the_upd96050(): """C16: by default DPL keeps DP bits 10-8 on the uPD96050; the emulators clear them.""" dsp = core(op(dpl=1), dp=0x523, model=UPD96050_MODEL) dsp.step() assert dsp.dp == 0x524 class Emulators(UPD7725): DPL_CLEARS_DP_BITS_10_8 = True dsp = core(op(dpl=1), dp=0x523, model=UPD96050_MODEL, cls=Emulators) dsp.step() assert dsp.dp == 0x024 dsp = core(op(dphm=1), dp=0x523, model=UPD96050_MODEL, cls=Emulators) dsp.step() assert dsp.dp == 0x533 # DPNOP with M1: only the XOR def test_rpdec_wraps(): """U9: RPDEC from 0 gives 3FFh (7FFh on the uPD96050).""" dsp = core(op(rpdec=1), rp=0) dsp.step() assert dsp.rp == 0x3FF dsp = core(op(rpdec=1), rp=0, model=UPD96050_MODEL) dsp.step() assert dsp.rp == 0x7FF def test_move_to_pointer_cancels_its_change(): """U2: a move to DP cancels DPL/DPHM; a move to RP cancels RPDEC.""" dsp = core(op(src=SRC_A, dst=DST_DP, dpl=1, dphm=3), a=0x20) dsp.step() assert dsp.dp == 0x20 dsp = core(op(src=SRC_A, dst=DST_RP, rpdec=1), a=0x20) dsp.step() assert dsp.rp == 0x20 def test_pointers_change_after_the_move(): """U1, DB p.9: the move and P use the old DP/RP; the new ones are valid next.""" dsp = core(op(alu=ADD, p=0, src=SRC_MEM, dst=DST_TR, dpl=1), dp=0, a=1) dsp.ram[0] = 0x10 dsp.ram[1] = 0x99 dsp.step() assert (dsp.tr, dsp.a, dsp.dp) == (0x10, 0x11, 1) dsp = core(op(src=SRC_RO, dst=DST_TR, rpdec=1), data_rom=[0, 0x55, 0x66], rp=2) dsp.step() assert (dsp.tr, dsp.rp) == (0x66, 1) def test_p_select(): """DS p.18: P = RAM[DP], IDB, M, N.""" for p, expected in ((0, 0x10), (1, 0x20), (2, 0x30), (3, 0x40)): dsp = core(op(alu=ADD, p=p, src=SRC_TR), dp=3, tr=0x20, m=0x30, n=0x40) dsp.ram[3] = 0x10 dsp.step() assert dsp.a == expected def test_c14_alu_reads_ram_before_mem_write(): """C14: P = RAM with DST = @MEM (prohibited, used): the ALU sees the old RAM word.""" dsp = core(op(alu=ADD, p=0, src=SRC_TR, dst=DST_MEM), tr=0x100, a=1, dp=2) dsp.ram[2] = 5 dsp.step() assert (dsp.a, dsp.ram[2]) == (6, 0x100) def test_c3_move_overwrites_the_alu_result(): """C3 default: flags from the ALU, the accumulator from the move (the DSP-4's ``SHL2 A`` + ``MOV @A, MEM`` clears CA).""" dsp = core(op(alu=SHL2, src=SRC_MEM, dst=DST_A), a=0xFFFF, flaga=FLAG_C, dp=0) dsp.ram[0] = 0x1234 dsp.step() assert dsp.a == 0x1234 assert not dsp.flaga & FLAG_C class DataBook(UPD7725): ALU_NOP_WHEN_MOVE_TO_ACC = True dsp = core(op(alu=SHL2, src=SRC_MEM, dst=DST_A), a=0xFFFF, flaga=FLAG_C, cls=DataBook) dsp.ram[0] = 0x1234 dsp.step() assert dsp.a == 0x1234 and dsp.flaga == FLAG_C def test_c4_serial_bit_order(): """C4 default: @SOL stores the bus value; MAME's reversal behind the switch.""" dsp = core(ld(0x0001, DST_SOL)) dsp.step() assert dsp.so == 0x0001 class Mame(UPD7725): SERIAL_BIT_REVERSE = True dsp = core(ld(0x0001, DST_SOL), op(src=SRC_SIL, dst=DST_A), cls=Mame, si=0x0003) dsp.run(2) assert dsp.so == 0x8000 and dsp.a == 0xC000 def test_multiplier(): """U3, DS (9)-(10), p.26: M/N hold K x L from the next instruction; M = sign + upper 15 bits, N = lower 15 bits shifted left.""" dsp = core(ld(0x4000, DST_K), ld(0x0003, DST_L), op()) dsp.step() assert (dsp.m, dsp.n) == (0, 0) # L still 0 dsp.step() assert (dsp.m, dsp.n) == (0x0001, 0x8000) # 4000h x 3 = 0C000h dsp = core(op(), k=0xFFFF, l=0x0002) # -1 x 2 = -2 dsp.step() assert (dsp.m, dsp.n) == (0xFFFF, 0xFFFC) dsp = core(op(), k=0x8000, l=0x8000) # U3 overflow: the emulators' 40000000h dsp.step() assert (dsp.m, dsp.n) == (0x8000, 0x0000) def test_rt_returns_last(): """DB p.9: an RT's operation runs, then the return.""" dsp = core(jp(0x140, na=2), op(), op(src=SRC_A, dst=DST_TR, rt=True), a=7) dsp.run(2) assert dsp.pc == 1 and dsp.tr == 7 and dsp.sp == 0 def test_ld_has_no_pointer_changes(): """DS p.24: LD is DST <- ID only (bits 13-8 of an LD are immediate bits).""" dsp = core(ld(0xFFFF, DST_TR), dp=0x12, rp=0x34) dsp.step() assert (dsp.tr, dsp.dp, dsp.rp) == (0xFFFF, 0x12, 0x34)