蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

离线验证冗余项消冒险、异步毛刺打拍抑制、格雷码单比特翻转与编码规范

PYTHONWindows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。explore-competition-hazard.py
explore-competition-hazard.py148 行
"""Offline Verilog RTL verification lab for Chapter 25 (competition-hazard)."""
import json

VERSION = "CPython 3.12.14 offline Verilog RTL bit-accurate simulator (iverilog absent)"


def eval_with_skew(expr_fn, a_transitions, b, c):
    """Evaluate expression across a skewed transition of A and A_bar to detect static-1/0 glitch."""
    # Suppose A transitions 0->1 while A_bar still lags at 1->0 (or 1->0 while A_bar stays 0->1)
    outputs = []
    for a_val, a_bar_val in a_transitions:
        outputs.append(expr_fn(a_val, a_bar_val, b, c))
    return outputs


def case_algebraic_and_redundant_term_elimination():
    """Verify static-1 hazard in Y = A'B' + AC at B=0, C=1 and elimination by consensus term B'C."""
    # Transition A: 1->0 with inverter delay -> (A=1,A'=0) -> transient (A=0,A'=0) -> (A=0,A'=1)
    skew_seq = [(1, 0), (0, 0), (0, 1)]
    y_hazard = eval_with_skew(lambda a, ab, b, c: int((ab and (not b)) or (a and c)), skew_seq, b=0, c=1)
    y_safe = eval_with_skew(
        lambda a, ab, b, c: int((ab and (not b)) or (a and c) or ((not b) and c)), skew_seq, b=0, c=1
    )
    # Also check F = A & A' static-0 hazard during A: 0->1 -> (0,1) -> (1,1) -> (1,0)
    f_and_glitch = eval_with_skew(lambda a, ab, b, c: int(a and ab), [(0, 1), (1, 1), (1, 0)], 0, 0)
    expected = {
        "y_without_consensus": [1, 0, 1],
        "y_with_consensus_BbarC": [1, 1, 1],
        "f_A_and_Abar_glitch": [0, 1, 0],
    }
    actual = {
        "y_without_consensus": y_hazard,
        "y_with_consensus_BbarC": y_safe,
        "f_A_and_Abar_glitch": f_and_glitch,
    }
    return {
        "name": "algebraic_and_redundant_term_elimination",
        "summary": "验证逻辑表达式 Y=A'B'+AC 与 F=A&A' 在反相器延时下的毛刺及增加冗余项 B'C 消除冒险",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_async_glitch_vs_sync_register_clap():
    """Simulate competition_hazard vs clap_delay on t=19ns en=1 and t=20ns din_rvs=0."""
    # Timeline t = 0..35ns, clk toggles every 5ns starting low (posedge at 10, 20, 30)
    # Unregistered condition = en & din_rvs: at t=19ns en=1, din_rvs=1 -> glitch=1 for 1ns [19, 20)
    raw_cond_at_19ns = 1 & 1
    raw_cond_at_20ns = 1 & 0
    # With 1-stage input register (clap_delay) sampling at posedge 10, 20, 30:
    # At posedge 10: en=0, din_rvs=1 -> en_r=0, din_rvs_r=1 -> cond=0
    # At posedge 20 (after din_rvs->0 at 20ns, or even before if setup missed): synchronized outputs change on edge only
    sync_samples = []
    for t_edge, en_val, din_val in [(10, 0, 1), (20, 1, 0), (30, 1, 0)]:
        sync_samples.append((t_edge, en_val & din_val))
    expected = {
        "raw_glitch_at_19ns": 1,
        "raw_settled_at_20ns": 0,
        "sync_condition_all_zero": True,
    }
    actual = {
        "raw_glitch_at_19ns": raw_cond_at_19ns,
        "raw_settled_at_20ns": raw_cond_at_20ns,
        "sync_condition_all_zero": all(v == 0 for _, v in sync_samples),
    }
    return {
        "name": "async_glitch_vs_sync_register_clap",
        "summary": "仿真异步信号 en/din_rvs 在 t=19ns 产生的 1ns 毛刺脉冲与时钟打拍同步后的毛刺抑制效果",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_binary_vs_gray_counter_transitions():
    """Compare multi-bit binary transition (5->6 passing 7) against 1-bit Gray code transitions."""
    # Binary 5 (0101) -> 6 (0110) flips 2 bits; transient intermediate 7 (0111) triggers cout==(counter==7)
    bin_hamming_5_to_6 = bin(0b0101 ^ 0b0110).count("1")
    gray_seq = [n ^ (n >> 1) for n in range(16)]
    gray_hamming_all_one = all(
        bin(gray_seq[i] ^ gray_seq[(i + 1) % 16]).count("1") == 1 for i in range(16)
    )
    expected = {"binary_5_to_6_bit_flips": 2, "transient_7_glitch_possible": True, "gray_4bit_all_hamming_1": True}
    actual = {
        "binary_5_to_6_bit_flips": bin_hamming_5_to_6,
        "transient_7_glitch_possible": bin_hamming_5_to_6 > 1,
        "gray_4bit_all_hamming_1": gray_hamming_all_one,
    }
    return {
        "name": "binary_vs_gray_counter_transitions",
        "summary": "验证 4-bit 二进制计数器 5->6 两位同时翻转可经 7 产生假脉冲,而格雷码相邻仅 1-bit 翻转",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_blocking_vs_nonblocking_coding_rules():
    """Verify the 6 Verilog coding rules: swap with NBA vs BA, and combinational chain BA vs NBA."""
    # Rule 1: sequential swap (a=1, b=2)
    a_ba, b_ba = 1, 2
    a_ba = b_ba
    b_ba = a_ba  # clobbered -> (2, 2)
    a_nba, b_nba = 1, 2
    a_nba, b_nba = b_nba, a_nba  # swapped -> (2, 1)

    # Rule 2: combinational C=A&B; F=C&D; when A=1,B=1,D=1 and old C=0
    old_c = 0
    # NBA in comb uses old C
    f_nba_wrong = old_c & 1
    # BA in comb uses updated C
    c_ba = 1 & 1
    f_ba_right = c_ba & 1
    expected = {
        "seq_ba_swap_broken": [2, 2],
        "seq_nba_swap_ok": [2, 1],
        "comb_nba_stale_F": 0,
        "comb_ba_fresh_F": 1,
    }
    actual = {
        "seq_ba_swap_broken": [a_ba, b_ba],
        "seq_nba_swap_ok": [a_nba, b_nba],
        "comb_nba_stale_F": f_nba_wrong,
        "comb_ba_fresh_F": f_ba_right,
    }
    return {
        "name": "blocking_vs_nonblocking_coding_rules",
        "summary": "验证时序块用非阻塞赋值、组合块用阻塞赋值以及混用/错用引发的仿真与综合偏差",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_algebraic_and_redundant_term_elimination(),
        case_async_glitch_vs_sync_register_clap(),
        case_binary_vs_gray_counter_transitions(),
        case_blocking_vs_nonblocking_coding_rules(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。

入口文件:projects/verilog-competition-hazard-lab/explore-competition-hazard.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-competition-hazard.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Verilog

课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip

下载课程关联工程包:Verilog

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2584.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习