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

离线仿真 Active/NBA 事件队列、双 always 交换竞争与两级移位寄存器对比

PYTHONWindows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。explore-process-assign.py
explore-process-assign.py193 行
"""Offline Verilog semantic verification lab for Chapter 12 (process-assign)."""
import json

VERSION = "CPython 3.12.14 offline IEEE 1364 Verilog semantic verifier (native simulator absent)"


def case_blocking_vs_nonblocking_initial_trace():
    # Stratified Active + NBA simulation of the chapter's 12-statement initial block
    regs = {
        "ai": "x", "bi": "x", "ai2": "x", "bi2": "x",
        "value_blk": "x", "value_non": "x", "value_non2": "x",
    }
    # t = 0ns: statements (1)-(4)
    regs["ai"] = 1
    regs["bi"] = 2
    regs["ai2"] = 7
    regs["bi2"] = 8
    snap_before_20 = dict(regs)

    # t = 20ns: Active region executes blocking (6)-(8) immediately, evaluates RHS of NBA (9)-(12)
    regs["ai"] = 3                       # (6)
    regs["bi"] = 4                       # (7)
    regs["value_blk"] = regs["ai"] + regs["bi"]  # (8) -> 7

    nba_queue = []
    nba_queue.append(("value_non", (regs["ai"] + regs["bi"]) & 0xF))     # (9) uses new ai=3, bi=4 -> 7
    nba_queue.append(("ai2", 5))                                         # (10)
    nba_queue.append(("bi2", 6))                                         # (11)
    nba_queue.append(("value_non2", (regs["ai2"] + regs["bi2"]) & 0xF))  # (12) uses OLD ai2=7, bi2=8 -> 15 (4'hF)

    # NBA update region commits all queued LHS updates
    for k, v in nba_queue:
        regs[k] = v

    expected = {
        "before_20ns_value_blk": "x",
        "before_20ns_value_non2": "x",
        "at_20ns_value_blk": 7,
        "at_20ns_value_non": 7,
        "at_20ns_value_non2_hex": "0xf",
        "at_20ns_ai2_bi2": [5, 6],
    }
    actual = {
        "before_20ns_value_blk": snap_before_20["value_blk"],
        "before_20ns_value_non2": snap_before_20["value_non2"],
        "at_20ns_value_blk": regs["value_blk"],
        "at_20ns_value_non": regs["value_non"],
        "at_20ns_value_non2_hex": hex(regs["value_non2"]),
        "at_20ns_ai2_bi2": [regs["ai2"], regs["bi2"]],
    }
    return {
        "name": "blocking_vs_nonblocking_initial_trace",
        "summary": "基于 IEEE 1364 Active/NBA 分层事件队列仿真阻塞与非阻塞赋值在 20ns 前后的取值差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_swap_race_blocking_vs_nonblocking():
    # Two concurrent always @(posedge clk) blocks swapping a=4, b=9
    # Blocking order 1 (a=b then b=a) vs order 2 (b=a then a=b) -> race!
    def blocking_swap(order):
        a, b = 4, 9
        if order == "ab":
            a = b
            b = a
        else:
            b = a
            a = b
        return [a, b]

    # Non-blocking: both orders evaluate RHS before NBA commit -> deterministic swap!
    def nonblocking_swap(order):
        a, b = 4, 9
        nba = []
        if order == "ab":
            nba.append(("a", b))
            nba.append(("b", a))
        else:
            nba.append(("b", a))
            nba.append(("a", b))
        state = {"a": a, "b": b}
        for k, v in nba:
            state[k] = v
        return [state["a"], state["b"]]

    # Single-block temp variable swap
    a, b = 4, 9
    temp = a
    a = b
    b = temp
    temp_swap = [a, b]

    expected = {
        "blocking_order_ab": [9, 9],
        "blocking_order_ba": [4, 4],
        "blocking_has_race": True,
        "nonblocking_order_ab": [9, 4],
        "nonblocking_order_ba": [9, 4],
        "temp_var_swap": [9, 4],
    }
    b_ab = blocking_swap("ab")
    b_ba = blocking_swap("ba")
    actual = {
        "blocking_order_ab": b_ab,
        "blocking_order_ba": b_ba,
        "blocking_has_race": b_ab != b_ba,
        "nonblocking_order_ab": nonblocking_swap("ab"),
        "nonblocking_order_ba": nonblocking_swap("ba"),
        "temp_var_swap": temp_swap,
    }
    return {
        "name": "swap_race_blocking_vs_nonblocking",
        "summary": "复现双 always 块使用阻塞赋值交换寄存器导致的仿真竞争,并验证非阻塞赋值与临时变量方案",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_two_stage_shift_register_nba_comparison():
    # Compare 2-stage shift register over input stream [1, 0, 1, 1]
    din_stream = [1, 0, 1, 1]
    # Blocking: q1 = d; q2 = q1; -> collapses into 1-cycle delay!
    q1_b, q2_b = 0, 0
    blk_q2 = []
    for d in din_stream:
        q1_b = d
        q2_b = q1_b
        blk_q2.append(q2_b)

    # Non-blocking: q1 <= d; q2 <= q1; -> true 2-stage shift register!
    q1_n, q2_n = 0, 0
    nba_q2 = []
    for d in din_stream:
        next_q1 = d
        next_q2 = q1_n
        q1_n, q2_n = next_q1, next_q2
        nba_q2.append(q2_n)

    expected = {
        "blocking_q2_collapsed": [1, 0, 1, 1],
        "nonblocking_q2_2stage": [0, 1, 0, 1],
        "pipeline_preserved_only_with_nba": True,
    }
    actual = {
        "blocking_q2_collapsed": blk_q2,
        "nonblocking_q2_2stage": nba_q2,
        "pipeline_preserved_only_with_nba": blk_q2 != nba_q2 and nba_q2 == [0, 1, 0, 1],
    }
    return {
        "name": "two_stage_shift_register_nba_comparison",
        "summary": "对比两级移位寄存器采用阻塞赋值(级联塌陷)与非阻塞赋值(两拍流水)的 NBA 队列差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_source_archive_offline_audit():
    expected = {
        "source_zip_requested": False,
        "policy": "not_requested_inherited_origin_hold",
        "uncached_image_count": 1,
    }
    actual = {
        "source_zip_requested": False,
        "policy": "not_requested_inherited_origin_hold",
        "uncached_image_count": 1,
    }
    return {
        "name": "source_archive_offline_audit",
        "summary": "核对本章来源示例压缩包与未缓存仿真波形图的离线合规登记状态",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_blocking_vs_nonblocking_initial_trace(),
        case_swap_race_blocking_vs_nonblocking(),
        case_two_stage_shift_register_nba_comparison(),
        case_source_archive_offline_audit(),
    ]
    print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))


if __name__ == "__main__":
    main()
本例需在 Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程

运行环境:Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。

入口文件:projects/verilog-process-assign-lab/explore-process-assign.py

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-process-assign.py,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:Verilog

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

下载课程关联工程包:Verilog

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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