#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Write tests/vectors/units.txt: the 32X's own parts driven call by call on the reference. Rung 2, unit-level lockstep (tests/units.rs replays the file on this board): a seeded stream of calls straight into the device, no 68000 or VDP program behind them -- - the 68000's and the Z80's accesses to every 32X range (the registers, the VDP and the palette with their waits, the frame buffer and its overwrite image, the windows, /TIME, the vector ROM and the HINT vector), at rising master clocks; - the SH-2 side's bus cycles as a chip makes them (the callbacks), on every region, with the BSC as the chips' reset leaves it: arbitration, the device's WAIT, the frame buffer's write FIFO, the cartridge against the 68000, SDRAM, the registers (VDP, PWM, the DREQ FIFO, the interrupt clears); - socket 6's line records (synthetic: the H40 and H32 tables, every V), socket 7's lines (random pixels and YS), socket 8's samples; - catch-ups, which step the two SH-2s through our own boot programs (a few instructions each: loops over SDRAM, the registers and the frame buffer). After each call the answer, and a CRC of the device's whole state as the lockstep trace writes it (``s32x_trace.device_lines``); every 64th call the state itself. No byte of Sega's boot ROMs or of any game is used: the boot ROMs are written here. $SEGA32X_PY scripts/make_unit_vectors.py > tests/vectors/units.txt (with the reference's ``src`` on PYTHONPATH, as scripts/lockstep.py runs it). """ from __future__ import annotations import random import struct import sys import zlib from megadrive_python.cartridge import Cartridge from megadrive_python.machine import MACHINES from megadrive_python.sockets import M68K, Z80, PictureLine, VideoLine from megadrive_python.system import System from megadrive_python.vdp import H32, H40 from sega32x_python.bootrom import BootROMs from sega32x_python.slot import NotModelled, Sega32X sys.path.insert(0, str(__import__("pathlib").Path(__file__).resolve().parent)) import s32x_trace # noqa: E402 SEED = 32 CALLS = 6000 def sh2_program(slave: bool) -> bytes: """A boot ROM of our own: vectors 0/1 (PC $00000100, SP $06040000), then a loop that reads and writes SDRAM (cache-through), a communication port and (with FM) the frame buffer.""" rom = bytearray(2048 if not slave else 1024) struct.pack_into(">II", rom, 0, 0x00000100, 0x06040000) code = [ 0xD002, # 100 MOV.L @(2,PC),R0 R0 = $26001000 (SDRAM, cache-through) 0xD103, # 102 MOV.L @(3,PC),R1 R1 = $20004020 (COMM0) 0xD203, # 104 MOV.L @(3,PC),R2 R2 = $24000000 (the frame buffer, through) 0xA007, # 106 BRA $118 0x0009, # 108 NOP 0x0009, # 10A NOP 0x2600, 0x1000, # 10C .long $26001000 0x2000, 0x4020, # 110 .long $20004020 0x2400, 0x0000, # 114 .long $24000000 0x6301, # 118 MOV.W @R0,R3 0x7301, # 11A ADD #1,R3 0x2031, # 11C MOV.W R3,@R0 0x6411, # 11E MOV.W @R1,R4 0x0009 if slave else 0x2141, # 120 MOV.W R4,@R1 (the master only) 0x2231, # 122 MOV.W R3,@R2 0x7202, # 124 ADD #2,R2 0xAFF7, # 126 BRA $118 0x0009, # 128 NOP ] for i, word in enumerate(code): struct.pack_into(">H", rom, 0x100 + 2 * i, word) return bytes(rom) def picture(seed: int, width: int) -> tuple[list[int], bytes]: """A socket-7 line's pixels and YS from a seed (the same formula in tests/units.rs).""" rgb = [(seed * 2654435761 + i * 40503) & 0xFFFFFF for i in range(width)] ys = bytes(((seed >> (i % 30)) ^ i) & 1 for i in range(width)) return rgb, ys def boot_roms() -> BootROMs: m68k = bytearray(256) struct.pack_into(">II", m68k, 0, 0x00FF0000, 0x00880200) return BootROMs(bytes(m68k), sh2_program(False), sh2_program(True)) def cartridge_image() -> bytes: image = bytearray(0x80000) for i in range(0, len(image), 2): struct.pack_into(">H", image, i, (i * 7 + 3) & 0xFFFF) image[0x100:0x110] = b"SEGA 32X " struct.pack_into(">II", image, 0, 0x00FF0000, 0x00000200) struct.pack_into(">H", image, 0x200, 0x60FE) # BRA.S * return bytes(image) def line_record(rng: random.Random, start: int, v: int, h40: bool, pal: bool, late: int) -> VideoLine: mode = H40 if h40 else H32 return VideoLine( start=start, length=mode.length, v=v, hmode=mode.name, hblank_set=start + mode.offset_of[mode.hblank_set], hblank_clear=start + mode.offset_of[mode.hblank_clear], vblank=False, vsync=False, interlace=False, odd=False, pal=pal, pixels=mode.starts, published=start + late, ) def main() -> int: rng = random.Random(SEED) roms = boot_roms() cartridge = Cartridge(cartridge_image()) device = Sega32X(cartridge, boot_roms=roms) system = System(MACHINES["genesis"], None, slot=device) # type: ignore[arg-type] board = system.board out = sys.stdout w = out.write w("# sega32x-rust unit vectors 1 (scripts/make_unit_vectors.py; seed %d)\n" % SEED) for name, rom in (("m68k", roms.m68k), ("master", roms.master), ("slave", roms.slave)): w(f"B {name} {rom.hex()}\n") mclk = 1000 line_start = 0 v = 0 h40 = True pal = False regs68 = [0xA15100 + 2 * i for i in range(0x20)] + [0xA15180 + 2 * i for i in range(8)] regs68 += [0xA15200 + 2 * rng.randrange(256) for _ in range(16)] + [0xA13000 + 2 * i for i in (0x76, 0x77, 0x78)] sh2_regs = [0x20004000 + 2 * i for i in range(0x20)] + [0x20004100 + 2 * i for i in range(8)] sh2_regs += [0x20004200 + 2 * rng.randrange(256) for _ in range(8)] def digest(full: bool) -> None: text = s32x_trace.device_lines(device, None) w(f"D {zlib.crc32(text.encode()) & 0xFFFFFFFF:08X}\n") if full: for line in text.splitlines(): w(f"S {line}\n") def stop(fn): # type: ignore[no-untyped-def] try: return fn() except Exception as error: # Lockup, BootROMError, NotModelled return f"stop {type(error).__name__}" for call in range(CALLS): kind = rng.random() mclk += rng.choice((7, 28, 56, 300, 1500, 3420)) while line_start + (H40.length if h40 else H32.length) <= mclk - 4000: length = H40.length if h40 else H32.length line_start += length v = (v + 1) % (313 if pal else 262) if rng.random() < 0.01: h40 = not h40 late = rng.choice((0, 0, 0, 200, 1700)) line = line_record(rng, line_start, v, h40, pal, late) w(f"L {line_start} {v} {int(h40)} {int(pal)} {late} {line.hblank_set} {line.hblank_clear}\n") device.video_line(line) if kind < 0.03: # The line table and the registers the picture is drawn by: a row's word address # (now and then on an $FF boundary: the shift's exception), the mode, PRI and the # shift, the swap. choice = rng.random() if choice < 0.6: address = 0x840000 + 2 * rng.randrange(240) value = (rng.randrange(0x100) << 8) | (0xFF if rng.random() < 0.5 else rng.randrange(0x100)) elif choice < 0.8: address, value = 0xA15180, rng.choice((0x01, 0x81, 0x02, 0x03, 0x41)) elif choice < 0.9: address, value = 0xA15182, rng.randrange(2) else: address, value = 0xA1518A, rng.randrange(2) w(f"O WW {address:X} {value:X} {mclk}\n") result = stop(lambda: device.write_word(address, value, mclk)) elif kind < 0.30: address = rng.choice(regs68 + [0x840000 + 2 * rng.randrange(0x10000), 0x860000 + 2 * rng.randrange(0x10000), 0x880000 + 2 * rng.randrange(0x40000), 0x900000 + 2 * rng.randrange(0x40000), 0x000000 + 2 * rng.randrange(0x80), 0x000070, 0x000100 + 2 * rng.randrange(100)]) # fmt: skip if rng.random() < 0.5: if address == 0xA15100: value = rng.choice((0x0003, 0x0003, 0x8003, 0x0001, 0x0000)) elif address == 0xA15106: value = rng.choice((0, 4, 1)) else: value = rng.randrange(0x10000) if rng.random() < 0.7: w(f"O WW {address:X} {value:X} {mclk}\n") result = stop(lambda: device.write_word(address, value, mclk)) else: byte_address = address | rng.randrange(2) master = M68K if rng.random() < 0.8 else Z80 w(f"O WB {byte_address:X} {value & 0xFF:X} {mclk} {master}\n") result = stop(lambda: device.write_byte(byte_address, value & 0xFF, mclk, master)) elif rng.random() < 0.7: w(f"O RW {address:X} {mclk}\n") result = stop(lambda: device.read_word(address, mclk)) else: byte_address = address | rng.randrange(2) master = M68K if rng.random() < 0.8 else Z80 w(f"O RB {byte_address:X} {mclk} {master}\n") result = stop(lambda: device.read_byte(byte_address, mclk, master)) elif kind < 0.60 and device.sh2.running: index = rng.randrange(2) chip = device.sh2.chips[index] region = rng.random() if region < 0.35: address = rng.choice(sh2_regs) elif region < 0.55: address = 0x04000000 | rng.choice((0, 0x20000)) | (2 * rng.randrange(0x10000)) elif region < 0.7: address = 0x06000000 | (4 * rng.randrange(0x10000)) elif region < 0.85: address = 0x02000000 | (2 * rng.randrange(0x40000)) elif region < 0.95: address = 2 * rng.randrange(0x200) else: address = 0x08000000 | 2 * rng.randrange(0x100) width = rng.choice((8, 16, 16, 32)) if width == 32: address &= ~3 elif width == 8: address |= rng.randrange(2) kind_bits = rng.choice((0, 0, 1, 8, 16, 2 | 4, 2)) # A cycle on the time line just after the chip's own place (its clock moves on # as the core would move it between cycles). chip.clock += rng.choice((0, 1, 2, 3, 12)) chip.wait = 0 if rng.random() < 0.5: value = rng.randrange(1 << width) w(f"O SW {index} {address:X} {width} {value:X} {kind_bits} {chip.clock}\n") result = stop(lambda: chip.write(address, width, value, kind_bits)) result = f"{result} wait={chip.wait}" else: w(f"O SR {index} {address:X} {width} {kind_bits} {chip.clock}\n") result = stop(lambda: chip.read(address, width, kind_bits)) result = f"{result} wait={chip.wait}" chip.wait = 0 elif kind < 0.70: w(f"O C {mclk}\n") result = stop(lambda: device.catch_up(mclk)) elif kind < 0.78: w(f"O P {mclk}\n") samples = device.pwm.take_samples(mclk) data = struct.pack(f"<{2 * len(samples)}d", *[x for s in samples for x in s]) result = f"{len(samples)}:{zlib.crc32(data) & 0xFFFFFFFF:08X} rate={struct.pack('