# SPDX-License-Identifier: MIT """Rung 3: time against BK2, the only timing judge (manufacturer tier; timing.md). **The six worked examples** (BK2 2-7-2..2-7-5, read from the page images): in the cache at CLSR = 1, each line's cycles. These are the decisive vectors; the core meets all six, line by line (the emulators meet 2 of 6: C13). **Chapter 9's "Machine Cycles" tables** (ROM / RAM / cache execution time). The fetch-only cells are 10.7 MHz figures (a ROM byte 3 cycles, a cache byte 1: U5), so the core is measured at CLSR = 0. The tables are not internally consistent with one per-access model (and the RAM-read cells disagree with example 5's LM = 4 fetches + 10 at 21.4 MHz), so every cell is pinned: ``MATCH`` cells equal BK2; ``DIFFER`` cells hold BK2's value and the core's, and are listed in claims.md. A change to the model shows up here as a moved cell. """ from __future__ import annotations import pytest from gsu_asm import ( CMODE, FMULT, GETB, GETBH, GETC, LMULT, LOOP, NOP, PLOT, RAMB, ROMB, RPIX, SBK, STOP, adc, add, branch, from_, ibt, inc, iwt, jmp, ldb, ldw, link, ljmp, lm, lms, move, mult, sm, sms, stb, stw, to, umult, ) from timing_probe import core_with, measure from gsu_python import GSU # ----- the six worked examples ------------------------------------------------------------ EXAMPLES = { # Example 1: MOVE R14,R1 starts the ROM read; GETB waits for it (5 = 1 + 4). 1: [(move(14, 1), 2), (GETB, 5), (to(1), 1), (from_(2), 1), (add(3), 1), (to(4), 1), (from_(5), 1), (add(6), 1), (add(8), 1)], # Example 2: six instructions between MOVE and GETB hide the read. 2: [(move(14, 1), 2), (to(1), 1), (from_(2), 1), (add(3), 1), (to(4), 1), (from_(5), 1), (add(6), 1), (GETB, 1), (add(8), 1)], # Example 3: the second STW waits for the first word's 10 cycles. 3: [(from_(8), 1), (stw(10), 1), (stw(11), 10), (to(1), 1), (from_(2), 1), (add(3), 1), (from_(5), 1), (add(6), 1)], # Example 4: three instructions in between: 7. 4: [(from_(8), 1), (stw(10), 1), (to(1), 1), (from_(2), 1), (add(3), 1), (stw(11), 7), (from_(5), 1), (add(6), 1)], # Example 5: LM is 4 fetches + a 10-cycle word read; SM is buffered (4 fetches). 5: [(lm(0, 0x1234), 14), (inc(0), 1), (sm(0x1234, 0), 4)], # Example 6: SBK instead of SM. 6: [(lm(0, 0x1234), 14), (inc(0), 1), (SBK, 1)], } # fmt: skip TOTALS = {1: 14, 2: 10, 3: 17, 4: 14, 5: 19, 6: 16} def prefix_count(code: list[int]) -> int: """Steps in one line of an example: its ALT/WITH prefixes plus the opcode.""" count = 1 for op in code: if op not in (0x3D, 0x3E, 0x3F) and not 0x20 <= op <= 0x2F: break count += 1 return count @pytest.mark.parametrize("number", sorted(EXAMPLES)) def test_bk2_worked_example(number): lines = EXAMPLES[number] code = [byte for part, _ in lines for byte in part] g = core_with(code, "cache", clsr=1, cfgr=0x80) g.r[1] = 0x8000 # R14 <- R1: a ROM address g.r[10], g.r[11] = 0x0100, 0x0200 g.step() # the start's pipeline NOP got = [] for part, _ in lines: got.append(sum(g.step() for _ in range(prefix_count(part)))) assert got == [cycles for _, cycles in lines] assert sum(got) == TOTALS[number] # ----- chapter 9 ------------------------------------------------------------------------- #: name -> (code, steps, CFGR, BK2's (ROM, RAM, cache) best case in 10.7 MHz cycles). TABLE = { "NOP (9.68)": (NOP, 1, 0x80, (3, 3, 1)), "STOP (9.84)": (STOP, 1, 0x80, (3, 3, 1)), "ADD Rn (9.6)": (add(1), 1, 0x80, (3, 3, 1)), "ADC Rn (9.4)": (adc(1), 2, 0x80, (6, 6, 2)), "INC Rn (9.42)": (inc(1), 1, 0x80, (3, 3, 1)), "IBT (9.41)": (ibt(1, 5), 1, 0x80, (6, 6, 2)), "IWT (9.43)": (iwt(1, 5), 1, 0x80, (9, 9, 3)), "MOVE (9.57)": (move(1, 2), 2, 0x80, (6, 6, 2)), "BRA (9.24)": (branch("BRA", 0) + NOP, 1, 0x80, (6, 6, 2)), "JMP (9.44)": (jmp(8), 1, 0x80, (3, 3, 1)), "LJMP (9.49)": (ljmp(8), 2, 0x80, (6, 6, 2)), "LOOP (9.54)": (LOOP, 1, 0x80, (3, 3, 1)), "LINK (9.48)": (link(1), 1, 0x80, (3, 3, 1)), "CMODE (9.28)": (CMODE, 2, 0x80, (6, 6, 2)), "ROMB (9.75)": (ROMB, 2, 0x80, (6, 6, 2)), "RAMB (9.73)": (RAMB, 2, 0x80, (6, 6, 2)), "GETB (9.35)": (GETB, 1, 0x80, (3, 3, 1)), "GETBH (9.36)": (GETBH, 2, 0x80, (6, 6, 2)), "GETC (9.39)": (GETC, 1, 0x80, (3, 3, 1)), "PLOT (9.72)": (PLOT, 1, 0x80, (3, 3, 1)), "STW (9.85)": (stw(1), 1, 0x80, (3, 7, 1)), "STB (9.83)": (stb(1), 2, 0x80, (6, 8, 2)), "SM (9.81)": (sm(0x100, 1), 2, 0x80, (12, 16, 4)), "SMS (9.82)": (sms(0x100, 1), 2, 0x80, (9, 13, 3)), "SBK (9.79)": (SBK, 1, 0x80, (3, 7, 1)), "MULT MS0=1 (9.66)": (mult(1), 1, 0xA0, (3, 3, 1)), "MULT MS0=0 (9.66)": (mult(1), 1, 0x80, (5, 5, 2)), "UMULT MS0=0 (9.90)": (umult(1), 2, 0x80, (8, 8, 3)), "FMULT MS0=1 (9.33)": (FMULT, 1, 0xA0, (7, 7, 4)), "FMULT MS0=0 (9.33)": (FMULT, 1, 0x80, (11, 11, 8)), "LMULT MS0=1 (9.52)": (LMULT, 2, 0xA0, (10, 10, 5)), "LMULT MS0=0 (9.52)": (LMULT, 2, 0x80, (14, 14, 9)), "LDW (9.46)": (ldw(1), 1, 0x80, (10, 12, 7)), "LDB (9.45)": (ldb(1), 2, 0x80, (11, 13, 6)), "LM (9.50)": (lm(1, 0x100), 2, 0x80, (20, 21, 11)), "LMS (9.51)": (lms(1, 0x100), 2, 0x80, (17, 17, 10)), "RPIX (9.77)": (RPIX, 2, 0x80, (24, 24, 20)), } #: Cells where the core's model gives another figure: name -> the core's (ROM, RAM, cache), #: None where it equals BK2. Patterns (claims.md): the multiplies take one cycle more from #: ROM/RAM than from the cache in BK2's tables; the RAM reads take 1-4 more (and LM's would #: then not fit example 5); a store's RAM-column wait is charged to the next instruction's #: fetch here (BK2 7.2.2); RPIX's 20-cycle floor has no per-access explanation. DIFFER = { "STW (9.85)": (3, 3, 1), "STB (9.83)": (6, 6, 2), "SM (9.81)": (12, 12, 4), "SMS (9.82)": (9, 9, 3), "SBK (9.79)": (3, 3, 1), "MULT MS0=0 (9.66)": (4, 4, 2), "UMULT MS0=0 (9.90)": (7, 7, 3), "FMULT MS0=1 (9.33)": (6, 6, 4), "FMULT MS0=0 (9.33)": (10, 10, 8), "LMULT MS0=1 (9.52)": (9, 9, 5), "LMULT MS0=0 (9.52)": (13, 13, 9), "LDW (9.46)": (9, 9, 7), "LDB (9.45)": (9, 9, 5), "LM (9.50)": (18, 18, 10), "LMS (9.51)": (15, 15, 9), "RPIX (9.77)": (12, 12, 8), } def cells(name: str) -> tuple[int, int, int]: code, steps, cfgr, _ = TABLE[name] return tuple(measure(code, where, steps=steps, cfgr=cfgr) for where in ("rom", "ram", "cache")) @pytest.mark.parametrize("name", sorted(TABLE)) def test_chapter9_cell(name): expected = DIFFER.get(name, TABLE[name][3]) assert cells(name) == expected def test_cell_counts(): """The record the claims cite: of 37 entries x 3 columns, how many cells equal BK2.""" total = 3 * len(TABLE) same = sum( ours == bk2 for name in TABLE for ours, bk2 in zip(DIFFER.get(name, TABLE[name][3]), TABLE[name][3], strict=True) ) assert (same, total) == (80, 111) def test_wait_ranges_upper_ends(): """Range ends the core produces (BK2's in brackets): GETB right after an R14 write (cache 1-6: here 3 at CLSR = 0, 5 at CLSR = 1, the 21.4 MHz 5-cycle read: U5); a store right after a word store (cache 1-6: here 6, BK2's upper end exactly).""" after_r14 = iwt(14, 0x8000) + GETB assert measure(after_r14, "cache", steps=1, skip=1, settle=False) == 3 assert measure(after_r14, "cache", steps=1, skip=1, settle=False, clsr=1) == 5 after_store = stw(1) + stw(2) assert measure(after_store, "cache", steps=1, skip=1, settle=False) == 6 assert measure(after_store, "cache", steps=1, skip=1, settle=False, clsr=1) == 10 def test_c13_switches_give_the_emulators_totals(): """C13: with free RAM reads and a one-byte write buffer (ares) the six examples take 14, 10, 22, 19, 14, 11 cycles, the figures timing.md reads from their sources.""" class Emulators(GSU): RAM_READS_CHARGED = False WORD_WRITE_BUFFER = False totals = [] for number in sorted(EXAMPLES): lines = EXAMPLES[number] g = core_with([b for part, _ in lines for b in part], "cache", clsr=1, cfgr=0x80, cls=Emulators) # fmt: skip g.r[1], g.r[10], g.r[11] = 0x8000, 0x0100, 0x0200 g.step() totals.append(sum(g.step() for part, _ in lines for _ in range(prefix_count(part)))) assert totals == [14, 10, 22, 19, 14, 11]