#!/usr/bin/env python3 """Test new request boundary with actual SDRAM controller and complete boot logic.""" import argparse import hashlib import json import os from pathlib import Path import re import subprocess from source_snapshot import original_sources ROOT=Path(__file__).resolve().parents[1] def boot_declarations(text): constants=re.findall(r'^localparam\s+inst_\w+\s*=[^;]+;',text,re.M) for declaration in constants: text=text.replace(declaration,'',1) position=re.search(r'localparam\s+EE_PROG',text).start() return text[:position]+'\n'.join(constants)+'\n'+text[position:] 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) # Icarus does not accept procedural assignment to Quartus's inout reg. # Model that exact bidirectional driver with an internal reg + continuous wire. controller=args.project/'rtl/sdram/sdram2.sv' text=controller.read_text() text=text.replace('inout reg [15:0] SDRAM_DQ','inout wire [15:0] SDRAM_DQ',1) text=text.replace('assign SDRAM_nCS = 0;', 'reg [15:0] dq_drive;\nassign SDRAM_DQ = dq_drive;\nassign SDRAM_nCS = 0;',1) text=text.replace('SDRAM_DQ <=','dq_drive <=') # Icarus requires these part-select/continuous-assignment operands declared # before use. Move declarations only, retaining width and all behavior. declarations=[] for name in ['command','dq_reg']: declaration=re.search(r'^reg\s+\[[^\]]+\]\s+'+name+r';',text,re.M).group(0) text=text.replace(declaration,'',1) declarations.append(declaration) text=text.replace('reg [15:0] dq_drive;', '\n'.join(declarations)+'\nreg [15:0] dq_drive;',1) # The unused board clock-output DDIO primitive has no effect on controller # state or this bench's constant read bus; model its configured inversion. text=text[:text.index('\naltddio_out')]+ '\nassign SDRAM_CLK = ~clk;\nendmodule\n' normalized=output/'sdram-icarus.sv' normalized.write_text(text,encoding='utf-8') original=original_sources('coco3-mister')/'rtl/EE_Cold_Bt.v' reference=output/'boot-reference.v' reference.write_text(boot_declarations(original.read_text()).replace('module EE_Cold_Bt','module EE_Cold_Bt_reference',1),encoding='utf-8') boot_candidate=output/'boot-candidate.v' boot_candidate.write_text(boot_declarations((args.project/'rtl/EE_Cold_Bt.v').read_text()),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={} for name,files in [('bridge_protocol_tb',[ROOT/'tests/bridge_protocol_tb.sv',args.project/'rtl/cpu_sdram_bridge.sv']),('cpu_sdram_bridge_tb',[ROOT/'tests/cpu_sdram_bridge_tb.sv',args.project/'rtl/cpu_sdram_bridge.sv',normalized]),('boot_control_tb',[ROOT/'tests/boot_control_tb.sv',boot_candidate,reference])]: with (output/(name+'-compile.log')).open('w') as file: subprocess.run([str(args.suite/'bin'/('iverilog'+ext)),'-g2012','-s',name,'-o',str(output/(name+'.vvp')),*map(str,files)],env=env,stdout=file,stderr=subprocess.STDOUT,check=True) log=output/(name+'.log') with log.open('w') as file: subprocess.run([str(args.suite/'bin'/('vvp'+ext)),str(output/(name+'.vvp'))],env=env,stdout=file,stderr=subprocess.STDOUT,check=True) print(log.read_text(),flush=True) results[name]={'sha256':{str(p):hashlib.sha256(p.read_bytes()).hexdigest() for p in [*files,log]}} results['hardware_tested']=False results['sdram_model_limit']='Actual RTL controller; inout-reg driver adaptation and unused clock-output DDIO model; constant read bus, no physical SDRAM array/timing model' (output/'test-result.json').write_text(json.dumps(results,indent=2)+'\n',encoding='utf-8') if __name__=='__main__':main()