#!/usr/bin/env python3 """Check actual MCP I2C master against fast-mode slots and finite release delay.""" 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 new build/ directory') out.mkdir(parents=True) source = a.project / 'sys/i2c.v' wrapper = a.project / 'sys/mcp23009.sv' m = re.search(r'i2c\s*#\(50_000_000,\s*([\d_]+)\)', wrapper.read_text()) assert m native_hz = int(m[1].replace('_', '')) 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 '' results = [] for hz in sorted({native_hz, 250000, 500000}): # Four independent pin-edge extrema for the accepted profile; # the ideal legacy waveform is the negative frequency/low-time control. corners = range(16) if hz == 250000 else (0, 1) for corner in corners: scl_fall = 300 if corner & 1 else 0 scl_release = 300 if corner & 2 else 0 sda_fall = 300 if corner & 4 else 0 sda_release = 300 if corner & 8 else 0 tag = f'{hz}-{corner}' text = source.read_text() # Icarus requires declarations before their use in delayed drivers; # Quartus accepts the original declaration order. No logic changes. declarations = [] for name in ('SCLK', 'SDO'): d = re.search(r'^reg\s+(?:\[[^\]]+\]\s*)?' + name + r';', text, re.M) assert d declarations.append(d[0]) text = text.replace(d[0], '', 1) text = text.replace('assign I2C_SCL =', '\n'.join(declarations) + '\nassign I2C_SCL =', 1) text = text.replace('assign I2C_SCL =', f'assign #(0,{scl_fall},{scl_release}) I2C_SCL =') text = text.replace('assign I2C_SDA =', f'assign #(0,{sda_fall},{sda_release}) I2C_SDA =') (out / 'i2c.v').write_text(text) cmd = [str(a.suite / 'bin' / ('iverilog' + ext)), '-g2012', f'-DBUS_HZ={hz}', f'-DSDA_FALL_NS={sda_fall}', f'-DSDA_RELEASE_NS={sda_release}', '-s', 'external_i2c_tb', '-o', str(out / 'run.vvp'), str(ROOT / 'tests/external_i2c_tb.sv'), str(out / 'i2c.v')] 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=60) text = (out / f'{tag}.log').read_text() print(text, end='') if hz == 500000: assert r.returncode and ('I2C low below 1300ns' in text or 'I2C fast-mode period below 2500ns' in text) else: assert not r.returncode and 'PASS I2C fast-mode slots' in text results.append({'bus_hz': hz, 'scl_fall_ns': scl_fall, 'scl_release_ns': scl_release, 'sda_fall_ns': sda_fall, 'sda_release_ns': sda_release, 'exit_code': r.returncode, 'log_sha256': hashlib.sha256((out / f'{tag}.log').read_bytes()).hexdigest()}) (out / 'result.json').write_text(json.dumps({'native_bus_hz': native_hz, 'fast_profile_passed': native_hz == 250000, 'scope': 'actual master, NACK/released FF and maximum-900ns ACK/slave-data cases; independent 0/300ns pin-edge extrema, no physical slave/RC measurement', 'source_sha256': {str(f.relative_to(a.project)): hashlib.sha256(f.read_bytes()).hexdigest() for f in (source, wrapper)}, 'results': results}, indent=2) + '\n') if __name__ == '__main__': main()