#!/usr/bin/env python3 # SPDX-License-Identifier: MIT # Copyright (c) 2026 Miasmark and contributors """voicetest.py: build the Intellivoice test program (build step 10) for the EXEC slot. A program of our own (ROMW 10, reset vector $1000, interrupts left off) that makes the SPB640 and the SP0256 do what the four games' speech does not (tools/sp0256.py is checked against MAME with it, docs/VOICE_NOTES.md 7): 1. resets them through $0081 bit 10, pushes 64 decles of a test stream into the FIFO without looking, writes command 0 and pushes the rest whenever the FIFO is not full. The stream (tools/sp0256.py Stream, random data from a fixed seed) has every data opcode in every MODE, a zero repeat (no data read), repeat prefixes, SETPAGE, a JMP into the FIFO with a decle half read (the rest is dropped), a JSR from the FIFO to a resident entry and back, and ends with RTS (halt); 2. queues commands 1 to 42 (every resident entry the wiki lists), each when LRQ says the last one was taken, and writes command 63 right after each one, which LRQ = 0 must drop; 3. resets again, pushes 70 decles of a second stream without looking (the last 6 are dropped: the FIFO holds 64), writes command 0, waits until LRQ says it was taken, waits about 0.3 s and resets in the middle of its speech; then pushes the second stream's first 20 decles and writes command 0 once more; 4. loops forever, counting passes in System RAM $0300. It checks nothing itself: tools/sp0256.py compare holds the model to MAME's run of it, sample by sample (tools/refs/mame_voice.sh with EXEC=). Usage: voicetest.py OUT.bin [--asm OUT.asm] """ import argparse import os import random import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, '..', '..', 'tools')) import as1610 # noqa: E402 import sp0256 as S # noqa: E402 DATA_OPS = ('LOADALL', 'LOAD_2', 'LOAD_C', 'LOAD_4', 'SETMSB_3', 'SETMSB_5', 'SETMSB_A', 'SETMSB_6', 'DELTA_9', 'DELTA_D', 'LOAD_E', 'PAUSE') def stream1(rng): s = S.Stream() for mode in range(4): s.setmode(mode) for name in DATA_OPS: data = [(n, rng.getrandbits(n)) for n in S.fields(name, mode)] s.op(name, rng.randint(1, 3), data) s.op('LOAD_C', 0) # repeat 0: no data block, nothing changes s.setmode(1, prefix=1) # repeat = imm | $10: 18 periods s.op('LOAD_E', 2, [(n, rng.getrandbits(n)) for n in S.fields('LOAD_E', 1)]) s.op('PAUSE', 1) # the prefix lasted one instruction s.op('RTS', 8) # SETPAGE (RTS with imm != 0): page $1000 s.setmode(0) s.op('LOAD_4', 2, [(n, rng.getrandbits(n)) for n in S.fields('LOAD_4', 0)]) s.jump('JMP', S.FIFO_BYTE) # a jump into the FIFO: the rest of the decle t = S.Stream() # in progress is dropped, the stream goes on at the next t.setmode(2) t.op('LOAD_C', 1, [(n, rng.getrandbits(n)) for n in S.fields('LOAD_C', 2)]) t.jump('JSR', S.ROM_BASE + 2 * 8) # command 8's entry ("One"); its RTS returns to u = S.Stream() # $1800: the FIFO, again at the next decle u.setmode(3) u.op('LOADALL', 1, [(n, rng.getrandbits(n)) for n in S.fields('LOADALL', 3)]) u.op('PAUSE', 2) u.op('RTS') # an empty stack: halt return s.decles() + t.decles() + u.decles() def stream2(rng): s = S.Stream() s.setmode(1) for name in ('LOAD_C', 'DELTA_9', 'DELTA_D', 'SETMSB_5', 'LOAD_4', 'DELTA_9', 'SETMSB_A', 'LOAD_2', 'SETMSB_3', 'SETMSB_6', 'LOAD_E', 'DELTA_D') * 2: s.op(name, rng.randint(2, 4), [(n, rng.getrandbits(n)) for n in S.fields(name, 1)]) d = s.decles() return (d + [0] * 70)[:70] # zeros past the end: RTS, halt PROGRAM = r''' ; voicetest: generated by sim/testroms/voicetest.py (see its header). SPDX-License-Identifier: MIT ROMW 10 ORG $1000 B main NOP NOP isr: B isr ; $1004: interrupts stay off main: MVII #$200, R0 ADDR R0, R0 ; $400: bit 10 MVO R0, $0081 ; reset the FIFO and the SP0256 MVII #s1, R4 MVII #64, R2 ; 64 decles without looking p1: MVI@ R4, R1 MVO R1, $0081 DECR R2 BNEQ p1 CLRR R0 MVO R0, $0080 ; command 0: run the FIFO MVII #S1N-64, R2 p2: MVI $0081, R0 ; bit 15: full TSTR R0 BMI p2 MVI@ R4, R1 MVO R1, $0081 DECR R2 BNEQ p2 MVII #1, R1 ; commands 1-42 c1: MVI $0080, R0 ; bit 15: LRQ TSTR R0 BPL c1 MVO R1, $0080 MVII #63, R0 MVO R0, $0080 ; LRQ is 0: dropped INCR R1 CMPI #43, R1 BNEQ c1 c2: MVI $0080, R0 ; wait until 42 is taken TSTR R0 BPL c2 MVII #2, R2 ; and give it about 2 s (LRQ does not tell the end) w1: CLRR R3 ; 65,536 x 15 cycles: about 1.1 s w1b: DECR R3 BNEQ w1b DECR R2 BNEQ w1 MVII #$200, R0 ADDR R0, R0 MVO R0, $0081 ; reset MVII #s2, R4 MVII #70, R2 ; 70 decles without looking: 6 dropped p3: MVI@ R4, R1 MVO R1, $0081 DECR R2 BNEQ p3 CLRR R0 MVO R0, $0080 c3: MVI $0080, R0 TSTR R0 BPL c3 MVII #16, R2 ; 16 x 1,023 x 15 cycles: about 0.27 s w2: MVII #$3FF, R3 w2b: DECR R3 BNEQ w2b DECR R2 BNEQ w2 MVII #$200, R0 ADDR R0, R0 MVO R0, $0081 ; reset mid-speech MVII #s2, R4 MVII #20, R2 p4: MVI@ R4, R1 MVO R1, $0081 DECR R2 BNEQ p4 CLRR R0 MVO R0, $0080 done: MVI $0300, R0 INCR R0 MVO R0, $0300 B done ''' def build(seed=10): rng = random.Random(seed) s1 = stream1(rng) s2 = stream2(rng) src = PROGRAM + 'S1N EQU %d\n' % len(s1) for lab, d in (('s1', s1), ('s2', s2)): src += lab + ':\n' for i in range(0, len(d), 8): src += ' DECLE ' + ', '.join('$%03X' % w for w in d[i:i + 8]) + '\n' return src, s1, s2 def exec_image(src): pr = as1610.assemble(src, romw=10) img = bytearray(8192) for a, w in pr.words.items(): if not 0x1000 <= a < 0x2000 or w > 0x3FF: sys.exit('voicetest: word $%04X at $%04X' % (w, a)) img[2 * (a - 0x1000)] = w >> 8 img[2 * (a - 0x1000) + 1] = w & 0xFF return bytes(img) def main(): ap = argparse.ArgumentParser(description=__doc__.split('\n')[0]) ap.add_argument('out') ap.add_argument('--asm') a = ap.parse_args() src, s1, s2 = build() if a.asm: with open(a.asm, 'w', encoding='utf-8') as f: f.write(src) with open(a.out, 'wb') as f: f.write(exec_image(src)) print('voicetest: stream 1 %d decles, stream 2 %d decles' % (len(s1), len(s2))) return 0 if __name__ == '__main__': sys.exit(main())