#!/usr/bin/env python3 """Compare the exact keyboard row against upstream with the stated control delay.""" import argparse import hashlib import json import os from pathlib import Path import subprocess from source_snapshot import original_sources ROOT=Path(__file__).resolve().parents[1] def main(): parser=argparse.ArgumentParser(description=__doc__) parser.add_argument('project',type=Path) parser.add_argument('output',type=Path) parser.add_argument('--suite',type=Path,required=True) args=parser.parse_args();output=args.output.resolve() if not output.is_relative_to((ROOT/'build').resolve()) or output.exists():parser.error('Use a new build/ output') output.mkdir(parents=True) original=original_sources('coco3-mister')/'rtl/coco3fpga.sv';candidate=args.project/'rtl/coco3fpga.sv' def row(text,start):return text[text.index(start):text.index('assign KEYBOARD_IN[7]')] reference=row(original.read_text(),'assign KEYBOARD_IN[6] =') actual=row(candidate.read_text(),'// Research: phase-match the remaining CPU keyboard control flags.') header='input [3:0] controls,input [72:0] KEY,input [7:0] KEY_COLUMN,output y);\nwire EE_N=controls[3],MUGS=controls[2],SHIFT=controls[1],SHIFT_OVERRIDE=controls[0];\nwire [7:0] KEYBOARD_IN;wire [7:0] BUTTON_N={7\'d0,EE_N};\n' source=output/'actual-keyboard.sv' source.write_text('module actual_keyboard_controls(input clk_sys,'+header+actual+'\nassign y=KEYBOARD_IN[6];endmodule\nmodule reference_keyboard_controls('+header+reference+'\nassign y=KEYBOARD_IN[6];endmodule\n',encoding='utf-8') bench=ROOT/'tests/keyboard_control_phase_tb.sv' env=os.environ.copy();env['PATH']=os.pathsep.join([str(args.suite/'bin'),str(args.suite/'lib'),env['PATH']]) ext='.exe' if os.name=='nt' else '' with (output/'compile.log').open('w') as log: subprocess.run([str(args.suite/'bin'/('iverilog'+ext)),'-g2012','-s','keyboard_control_phase_tb','-o',str(output/'test.vvp'),str(bench),str(source)],env=env,stdout=log,stderr=subprocess.STDOUT,check=True) with (output/'test.log').open('w') as log: subprocess.run([str(args.suite/'bin'/('vvp'+ext)),str(output/'test.vvp')],env=env,stdout=log,stderr=subprocess.STDOUT,check=True) print((output/'test.log').read_text()) (output/'test-result.json').write_text(json.dumps({'passed':True,'hardware_tested':False,'sha256':{str(p):hashlib.sha256(p.read_bytes()).hexdigest() for p in [original,candidate,bench,source,Path(__file__),output/'test.log']}},indent=2)+'\n',encoding='utf-8') if __name__=='__main__':main()