Files
CoCo3_Plus_MiSTer/scripts/test_joystick_button_routing.py
T

119 lines
4.9 KiB
Python

#!/usr/bin/env python3
"""Simulate actual MiSTer pad routing and keyboard rows against CoCo 3 PIA wiring.
Copyright (c) 2026 Brian K. Holman. Original contribution.
Hardware oracle: Tandy 26-3334 service manual, printed p9: PA0 right1,
PA1 left1, PA2 right2, PA3 left2. No FPGA deployment or game execution.
"""
import argparse
import hashlib
import json
import os
from pathlib import Path
import re
import subprocess
ROOT = Path(__file__).resolve().parents[1]
BENCH = r"""
`timescale 1ns/1ps
module joystick_button_tb;
reg clk_sys=0;
always #5 clk_sys=~clk_sys;
reg [31:0] joy1=0,joy2=0;
reg [63:0] status=0;
reg [72:0] KEY=0;
reg [7:0] KEY_COLUMN=8'hff;
reg ps2_button=1,SWAP_M_J=0;
wire [3:0] rows;
actual_buttons dut(clk_sys,joy1,joy2,status,KEY,KEY_COLUMN,ps2_button,SWAP_M_J,rows);
reg [3:0] want,pressed;
reg [31:0] left_pad,right_pad;
integer sw,b,mouse,cols,keys,r,c,cases=0;
initial begin
for(sw=0;sw<2;sw=sw+1)
for(b=0;b<16;b=b+1) begin
@(posedge clk_sys);
status[6]=sw;joy1=0;joy2=0;
joy1[4]=b[0];joy1[5]=b[1];joy2[4]=b[2];joy2[5]=b[3];
@(negedge clk_sys);#1;
left_pad=sw?joy2:joy1;right_pad=sw?joy1:joy2;
for(mouse=0;mouse<4;mouse=mouse+1)
for(cols=0;cols<256;cols=cols+1)
for(keys=0;keys<16;keys=keys+1) begin
ps2_button=mouse[0];SWAP_M_J=mouse[1];KEY_COLUMN=cols;KEY=0;
for(r=0;r<4;r=r+1)
for(c=0;c<8;c=c+1) KEY[r*8+c]=keys[r];
// Independent service-manual PA bit order, plus existing mouse1.
pressed[0]=right_pad[4] | (!ps2_button & SWAP_M_J);
pressed[1]=left_pad[4];pressed[2]=right_pad[5];pressed[3]=left_pad[5];
want=~pressed;
for(r=0;r<4;r=r+1)
for(c=0;c<8;c=c+1)
if(!KEY_COLUMN[c] && KEY[r*8+c]) want[r]=0;
#1;
if(rows!==want)
$fatal(1,"PIA routing mismatch swap=%0d buttons=%h mouse=%0d columns=%h keys=%h got=%h expected=%h",sw,b,mouse,KEY_COLUMN,keys,rows,want);
cases=cases+1;
end
end
$display("PASS %0d PIA comparisons: 16 button combinations, 2 port swaps, 4 mouse states, 256 column masks, 16 keyboard-row patterns",cases);
$finish;
end
endmodule
"""
def digest(path):
return hashlib.sha256(path.read_bytes()).hexdigest()
def main():
p = argparse.ArgumentParser(description=__doc__)
p.add_argument('project', type=Path)
p.add_argument('output', type=Path)
p.add_argument('--suite', type=Path, required=True)
a = p.parse_args()
a.suite = a.suite.resolve()
output = a.output.resolve()
if not output.is_relative_to(ROOT / 'build') or output.exists():
p.error('Use a fresh directory under this repository build/')
top_path = a.project / 'CoCo3.sv'
core_path = a.project / 'rtl/coco3fpga.sv'
top = top_path.read_text()
core = core_path.read_text()
pad_state = top[top.index('reg [31:0] coco_joy1'):top.index('reg [15:0] coco_ajoy1')]
binding = re.findall(r'\.P_SWITCH\(([^\n]+)\),', top)
if len(binding) != 1:
raise ValueError('Require one literal P_SWITCH binding')
rows = core[core.index('assign KEYBOARD_IN[0]'):core.index('assign KEYBOARD_IN[4]')]
output.mkdir(parents=True)
actual = output / 'actual-buttons.sv'
actual.write_text('module actual_buttons(input clk_sys,input [31:0] joy1,joy2,'
'input [63:0] status,input [72:0] KEY,input [7:0] KEY_COLUMN,'
'input ps2_button,SWAP_M_J,output [3:0] rows);\n'
'wire [3:0] P_SWITCH;wire [7:0] KEYBOARD_IN;\n' + pad_state +
'\nassign P_SWITCH=' + binding[0] + ';\n' + rows +
'\nassign rows=KEYBOARD_IN[3:0];endmodule\n', encoding='utf-8')
bench = output / 'test.sv'
bench.write_text(BENCH, encoding='utf-8')
env = os.environ.copy()
env['PATH'] = os.pathsep.join([str(a.suite / 'bin'), str(a.suite / 'lib'), env['PATH']])
ext = '.exe' if os.name == 'nt' else ''
with (output / 'compile.log').open('w') as log:
subprocess.run([str(a.suite / 'bin' / ('iverilog' + ext)), '-g2012',
'-s', 'joystick_button_tb', '-o', str(output / 'test.vvp'), str(actual), str(bench)],
env=env, stdout=log, stderr=subprocess.STDOUT, check=True)
with (output / 'test.log').open('w') as log:
result = subprocess.run([str(a.suite / 'bin' / ('vvp' + ext)), str(output / 'test.vvp')],
env=env, stdout=log, stderr=subprocess.STDOUT)
record = dict(passed=result.returncode == 0, exit_code=result.returncode,
hardware_tested=False, scope='Extracted actual routing and keyboard rows, service-manual oracle',
sha256={str(x): digest(x) for x in (top_path, core_path, Path(__file__), actual, bench, output / 'test.log')})
(output / 'test-result.json').write_text(json.dumps(record, indent=2) + '\n', encoding='utf-8')
print((output / 'test.log').read_text())
raise SystemExit(result.returncode)
if __name__ == '__main__':
main()