#!/usr/bin/env python3 """Verify the actual pixel divider's generated-clock edges and reject a defect.""" import argparse import hashlib import json import os from pathlib import Path import re import subprocess ROOT = Path(__file__).resolve().parents[1] 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() out = a.output.resolve() if not out.is_relative_to(ROOT / 'build') or out.exists(): p.error('Use a fresh build/ directory') out.mkdir(parents=True) source = a.project / 'rtl/Mister_Video.v' sdc = a.project / 'external-io.sdc' text = source.read_text() divider = re.search(r'always @ \(posedge MASTER_CLK\)\s*begin\s*reg\s*\[1:0\]\s*div;.*?\nend', text, re.S) assert divider is not None assert '-edges {3 7 11}' in sdc.read_text() block = divider[0].replace('begin', 'begin : divider', 1) 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 '' bench = '''`timescale 1ns/1ps module tb; reg clk=0; always #5 clk=~clk; wire pix; generator dut(clk,pix); integer cycles=0, rises=0, last_rise=0, last_fall=0; reg previous=0; always @(posedge clk) cycles=cycles+1; // Observe the registered final value after all nonblocking assignments settle. // The original RTL assigns default low then conditionally high in one process. always @(negedge clk) begin if(pix && !previous) begin if(rises==0 && cycles!=2) $fatal(1,"Pixel first edge %0d",cycles); if(rises>0 && cycles-last_rise!=4) $fatal(1,"Pixel period %0d",cycles-last_rise); if(rises>0 && cycles-last_fall!=2) $fatal(1,"Pixel low width"); last_rise=cycles;rises=rises+1; end if(!pix && previous && rises>0) begin if(cycles-last_rise!=2) $fatal(1,"Pixel high width"); last_fall=cycles; end previous=pix; end initial begin dut.divider.div=0; repeat(4096) @(negedge clk); if(rises!=1024) $fatal(1,"Pixel count"); $display("PASS pixel edges {3 7 11}: 1024 periods, divide four, duty half"); $finish; end endmodule ''' (out / 'tb.sv').write_text(bench) cmd = [str(a.suite / 'bin' / ('iverilog' + ext)), '-g2012', '-s', 'tb', '-o', str(out / 'run.vvp'), str(out / 'tb.sv'), str(out / 'generator.sv')] for negative in (False, True): tag = 'negative' if negative else 'positive' logic = block.replace("(div == 2'd2)", "(div == 2'd3)") if negative else block (out / 'generator.sv').write_text('module generator(input MASTER_CLK,output reg PIX_CLK);\n' + logic + '\nendmodule\n') with (out / f'{tag}-compile.log').open('w') as log: subprocess.run(cmd, env=env, stdout=log, stderr=subprocess.STDOUT, check=True) with (out / f'{tag}.log').open('w') as log: r = subprocess.run([str(a.suite / 'bin' / ('vvp' + ext)), str(out / 'run.vvp')], env=env, stdout=log, stderr=subprocess.STDOUT, timeout=30) output = (out / f'{tag}.log').read_text() print(output, end='') if (not negative and (r.returncode or 'PASS pixel edges' not in output)) or (negative and (not r.returncode or 'Pixel' not in output)): raise RuntimeError('Unexpected divider result: ' + tag) (out / 'result.json').write_text(json.dumps({'passed': True, 'wrong_duty_mutant_rejected': True, 'generated_edges': [3, 7, 11], 'periods': 1024, 'scope': 'extracted actual divider; FPGA zero power-up counter assumption; not pixel rendering', 'source_sha256': {str(path.relative_to(a.project)): hashlib.sha256(path.read_bytes()).hexdigest() for path in (source, sdc)}}, indent=2) + '\n') if __name__ == '__main__': main()