# SPDX-License-Identifier: MIT """Random single instructions through ssp1601-python, for tests/vectors.rs. Each line is a state, the next two words of program, the values the external registers give, and what the reference did: the state after, the cycles, and the external bus. Program memory reads anywhere else give address ^ $5A5A, so a double-indirect read shows where it went. Run with the reference on PYTHONPATH: PYTHONPATH=../ssp1601-python/src python3 scripts/make_vectors.py > tests/vectors.txt """ import random import sys from ssp1601_python import SSP1601 CASES = 60000 # Every index of the instruction map. Uniform random words land on an unmapped index more # than half the time and hardly ever on a conditional form that is taken, which let a # wrong arithmetic shift through: so most cases aim at the map, each index alike. MAPPED = [0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x09, 0x0A, 0x0C, 0x0D, 0x0E, 0x0F, 0x24, 0x25, 0x26, 0x48, 0x1B, 0x4B, 0x5B] # fmt: skip for kind in (1, 3, 4, 5, 6, 7): MAPPED += [(kind << 4) | low for low in (0x00, 0x01, 0x03, 0x04, 0x05, 0x09, 0x0C)] CONDITIONAL = (0x24, 0x26, 0x48) def opcode(rng): if rng.random() < 0.1: return rng.randrange(0x10000) index = rng.choice(MAPPED) low = rng.randrange(0x200) if index in CONDITIONAL and rng.random() < 0.9: low = (low & ~0xF0) | rng.choice((0x00, 0x50, 0x70)) # a condition the core models return (index << 9) | low def state(cpu): words = [cpu.x, cpu.y, cpu.a, cpu.st, cpu.pc, cpu.sp, *cpu.stack, *cpu.r] return " ".join(f"{word:X}" for word in words) def main(): rng = random.Random(0x1601) out = sys.stdout for _ in range(CASES): pc = rng.randrange(0x10000) program = {pc: opcode(rng), (pc + 1) & 0xFFFF: rng.randrange(0x10000)} ext = [rng.randrange(0x10000) for _ in range(8)] bus = [] def read(address): return program.get(address, address ^ 0x5A5A) def ext_read(n): bus.append(f"r{n}") return ext[n] def ext_write(n, value): bus.append(f"w{n}={value:X}") cpu = SSP1601(read, ext_read, ext_write) cpu.x = rng.randrange(0x10000) cpu.y = rng.randrange(0x10000) cpu.a = rng.randrange(0x100000000) # ST: mostly the low bits and flags anything, but RPL small often enough to matter. cpu.st = rng.randrange(0x10000) cpu.pc = pc cpu.sp = rng.randrange(7) cpu.stack = [rng.randrange(0x10000) for _ in range(6)] cpu.r = [rng.randrange(0x100) for _ in range(8)] # RAM is a function of the address plus a per-case salt, so a line stays short. salt = rng.randrange(0x10000) cpu.ram = [(i * 0x9E37 ^ salt) & 0xFFFF for i in range(0x200)] before = state(cpu) words = f"{program[pc]:X} {program[(pc + 1) & 0xFFFF]:X}" cycles = cpu.step() ram = 0 for word in cpu.ram: ram = (ram * 31 + word) & 0xFFFFFFFF fields = [before, words, f"{salt:X}", " ".join(f"{v:X}" for v in ext), state(cpu), f"{cycles:X}", f"{ram:X}", ",".join(bus) or "-", f"{len(cpu.unhandled):X}"] # fmt: skip out.write("|".join(fields) + "\n") if __name__ == "__main__": main()