#!/usr/bin/env python3 """Test exact consumer reset chains and disk-buffer pipeline cadence contracts.""" import argparse import hashlib import json import os from pathlib import Path import subprocess 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 output directory inside build/') output.mkdir(parents=True) core=(args.project/'rtl/coco3fpga.sv').read_text() start=core.index('(* preserve *) reg [1:0] reset_main_release') end=core.index('reg\t\t[6:0]\tCPU_RESET_SM;',start) block=core[start:end] for name in ['RESET_N','RESET_POS_N','RESET_FAST_N']: block=block.replace('wire '+name+' =','assign '+name+' =') stages='module actual_reset_domains(input clk_sys,CLK_114,RESET_RAW_N,output RESET_N,RESET_POS_N,RESET_FAST_N);\n'+block+'\nendmodule\n' fdc=(args.project/'rtl/fdc.sv').read_text() start=fdc.index('(* preserve *) reg [1:0] wd_reset_release') end=fdc.index('always@',start) stages+='module actual_wd_release(input CLK,RESET_N,output WD_RESET_N);\n'+fdc[start:end]+'\nassign WD_RESET_N=wd_reset_release[1];\nendmodule\n' if '// Research: matching fast-clock' in core: start=core.index('// Research: matching fast-clock') end=core.index('localparam Sz_512K',start) block=core[start:end].replace('reg data_ready_phase;','').replace('reg [15:0] hold_data_phase;','') stages+='module actual_completion(input CLK_114,clear_data_ready,sdram_cpu_ready,input [15:0] sdram_dout,output reg data_ready_phase,output reg [15:0] hold_data_phase);\nreg data_ready;reg [15:0] hold_data;\n'+block+'\nendmodule\n' sdc=(args.project/'rtl/sdc.sv').read_text() wd=(args.project/'rtl/wd1793.sv').read_text() if '(* preserve *) reg [7:0] cpu_buffer_pipe;' in sdc: start=sdc.index('(* preserve *) reg [7:0] cpu_buffer_pipe;') end=sdc.index('sdc_dpram hd_buff',start) stages+='module actual_sdc_pipeline(input CLK,input [7:0] hd_buff_data_raw,output [7:0] hd_buff_data);\n'+sdc[start:end]+'\nendmodule\n' else: assert '.q_b(hd_buff_data)' in sdc and 'always_ff@(negedge clock)' in sdc stages+='module actual_sdc_pipeline(input CLK,input [7:0] hd_buff_data_raw,output [7:0] hd_buff_data); assign hd_buff_data=hd_buff_data_raw; endmodule\n' if '(* preserve *) reg [7:0] cpu_buffer_pipe;' in wd: start=wd.index('(* preserve *) reg [7:0] cpu_buffer_pipe;') end=wd.index('always @*',start) stages+='module actual_wd_pipeline(input clk_sys,RWMODE,input [7:0] buff_dout,buff_din,output [7:0] q);\n'+wd[start:end]+'\nassign q=cpu_buffer_pipe;\nendmodule\n' else: assert 'buff_rd ? (RWMODE ? buff_dout : buff_din) :' in wd stages+='module actual_wd_pipeline(input clk_sys,RWMODE,input [7:0] buff_dout,buff_din,output [7:0] q); assign q=RWMODE?buff_dout:buff_din; endmodule\n' # Verify the actual wrapper inversion, not just the leaf module clock name. for name,count in [('fdc.sv',4),('sdc_top.sv',2)]: if (args.project/'rtl'/name).read_text().count('.clk_sys(~CLK)')!=count:raise RuntimeError('WD clock binding changed') stages+=sdc[sdc.index('module sdc_dpram'):]+'\n'+wd[wd.index('module wd1793_dpram'):] source=output/'actual-domains-buffers.sv';source.write_text(stages,encoding='utf-8') 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 '';results={} cases=[('domains','reset_domains_tb',False),('buffers','disk_buffer_pipeline_tb',False),('buffers-negative','disk_buffer_pipeline_tb',True)] if 'module actual_completion' in stages:cases.append(('completion','completion_phase_tb',False)) for name,top,negative in cases: bench=ROOT/'tests'/(top+'.sv') with (output/(name+'-compile.log')).open('w') as log: subprocess.run([str(args.suite/'bin'/('iverilog'+ext)),'-g2012',*(['-DNEGATIVE_CONSECUTIVE'] if negative else []),'-s',top,'-o',str(output/(name+'.vvp')),str(bench),str(source)],env=env,stdout=log,stderr=subprocess.STDOUT,check=True) log=output/(name+'.log') with log.open('w') as file:run=subprocess.run([str(args.suite/'bin'/('vvp'+ext)),str(output/(name+'.vvp'))],env=env,stdout=file,stderr=subprocess.STDOUT) text=log.read_text();print(text,flush=True) if negative: if run.returncode==0 or not any(message in text for message in ['CPU observed stale disk buffer','unsupported consecutive CPU disk read']):raise RuntimeError('Unsupported consecutive-enable negative control missing') print('PASS negative control: CPU buffer read phases require the proven nonconsecutive enable contract') elif run.returncode:raise RuntimeError('Regression failed: '+name) results[name]={'exit':run.returncode,'sha256':{str(p):hashlib.sha256(p.read_bytes()).hexdigest() for p in [bench,source,log]}} results['hardware_tested']=False (output/'test-result.json').write_text(json.dumps(results,indent=2)+'\n',encoding='utf-8') if __name__=='__main__':main()