#!/usr/bin/env python3 """Check the real platform serializer's timing slots; reject a same-edge defect.""" import argparse import hashlib import json import os from pathlib import Path 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 / 'sys/audio_out.v' i2s = a.project / 'sys/i2s.v' text = source.read_text() ce = text[text.index('localparam AUDIO_RATE'):text.index('\ni2s i2s')] ce = ce.replace('always @(posedge clk) begin', 'always @(posedge clk) begin : ce_phase', 1) at = ce.index('reg i2s_ce;') ce = ce[:at] + ce[at:].replace('always @(posedge clk) begin', 'always @(posedge clk) begin : divider_phase', 1) wrapper = ('module audio_pin_contract(input clk,reset,sample_rate,output bclk,lrclk,data);\n' 'localparam CLK_RATE=24576000;\n' + ce + '\n' 'i2s i2s(.reset(reset),.clk(clk),.ce(i2s_ce),.sclk(bclk),.lrclk(lrclk),.sdata(data),' '.left_chan(16\'ha55a),.right_chan(16\'h5aa5));\nendmodule\n') (out / 'wrapper.sv').write_text(wrapper) (out / 'i2s.v').write_bytes(i2s.read_bytes()) 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 '' cmd = [str(a.suite / 'bin' / ('iverilog' + ext)), '-g2012', '-s', 'external_audio_tb', '-o', str(out / 'run.vvp'), str(ROOT / 'tests/external_audio_tb.sv'), str(out / 'wrapper.sv'), str(out / 'i2s.v')] for negative in (False, True): tag = 'negative' if negative else 'positive' if negative: # Deliberately invert the forwarded sampling edge. mutant = i2s.read_text().replace('sclk <= msclk;', 'sclk <= ~msclk;') assert mutant != i2s.read_text() (out / 'i2s.v').write_text(mutant) 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: result = subprocess.run([str(a.suite / 'bin' / ('vvp' + ext)), str(out / 'run.vvp')], env=env, stdout=log, stderr=subprocess.STDOUT, timeout=60) log_text = (out / f'{tag}.log').read_text() print(log_text, end='') if (not negative and (result.returncode or 'PASS audio pin slots' not in log_text)) or ( negative and (not result.returncode or 'I2S receiver setup slot lost' not in log_text)): raise RuntimeError(f'Unexpected {tag} result') (out / 'i2s.v').write_bytes(i2s.read_bytes()) (out / 'result.json').write_text(json.dumps({'passed': True, 'wrong_edge_mutant_rejected': True, 'setup_min_fabric_cycles': 5, 'hold_min_fabric_cycles': 3, 'modes_hz': [48000, 96000], 'scope': 'real CE and I2S RTL; no analog edge/pull-up or external receiver model', 'source_sha256': {str(path.relative_to(a.project)): hashlib.sha256(path.read_bytes()).hexdigest() for path in (source, i2s)}}, indent=2) + '\n') if __name__ == '__main__': main()