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

离线仿真 DFF assign/deassign 覆盖与 counter10 reg/wire force/release 释放差异

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

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


def case_dff_normal_vs_assign_deassign():
    # Compare standard async-reset DFF vs assign/deassign DFF (with proper @(rstn) sensitivity)
    # Events: (event_kind, rstn, d)
    events = [
        ("rstn_fall", 0, 1),  # reset asserted -> Q=0
        ("clk_rise", 0, 1),   # clock edge while reset active -> Q stays 0 (assign blocks clk!)
        ("rstn_rise", 1, 1),  # reset released -> deassign Q; Q holds 0 until next clk edge!
        ("clk_rise", 1, 1),   # clock edge after deassign -> Q=1
        ("clk_rise", 1, 0),   # clock edge -> Q=0
    ]

    # Standard DFF: always @(posedge clk or negedge rstn)
    q_norm = 1
    hist_norm = []
    for ev, rstn, d in events:
        if ev == "rstn_fall":
            q_norm = 0
        elif ev == "clk_rise":
            q_norm = 0 if rstn == 0 else d
        hist_norm.append(q_norm)

    # assign/deassign DFF with proper always @(rstn)
    q_ad = 1
    q_assigned = False
    hist_ad = []
    for ev, rstn, d in events:
        if ev in ("rstn_fall", "rstn_rise"):
            if rstn == 0:
                q_assigned = True
                q_ad = 0
            else:
                q_assigned = False  # deassign Q: retains 0 until next procedural assign
        elif ev == "clk_rise":
            if not q_assigned:
                q_ad = d
        hist_ad.append(q_ad)

    # Pitfall: if sensitivity is only @(negedge rstn), rstn_rise never triggers deassign!
    q_bug = 1
    bug_assigned = False
    hist_bug = []
    for ev, rstn, d in events:
        if ev == "rstn_fall":
            bug_assigned = True
            q_bug = 0
        elif ev == "clk_rise" and not bug_assigned:
            q_bug = d
        hist_bug.append(q_bug)

    expected = {
        "normal_dff_trace": [0, 0, 0, 1, 0],
        "assign_deassign_matches_normal": True,
        "negedge_only_pitfall_stuck_0": [0, 0, 0, 0, 0],
    }
    actual = {
        "normal_dff_trace": hist_norm,
        "assign_deassign_matches_normal": hist_ad == hist_norm,
        "negedge_only_pitfall_stuck_0": hist_bug,
    }
    return {
        "name": "dff_normal_vs_assign_deassign",
        "summary": "对比常规异步复位 D 触发器与 assign/deassign 改写行为,并揭示敏感列表仅写 negedge rstn 的陷阱",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_force_release_reg_vs_wire_counter10():
    # Cycle-accurate simulation of counter10 + chapter testbench:
    # clk period = 20ns (posedge at 10, 30, 50, 70, 90, 110, 130; negedge at 20, 40, 60, 80, 100, 120)
    # rstn=0 at 0ns, rstn=1 at 10ns (Wait: cnt_temp counts 1@10?, wait: if rstn=1 at 10ns after posedge or before:
    # In chapter: cnt_temp reaches 4 before 80ns negedge; at 80ns force cnt_temp=6, force cout=1;
    # at 120ns release cnt_temp, release cout -> cnt_temp stays 6 until 130ns posedge (-> 7),
    # while cout (wire driven by assign cout=(cnt_temp==9)) IMMEDIATELY drops to 0 at 120ns!)
    cnt_temp = 0
    cnt_forced = False
    cout_forced = False
    cout_force_val = 0

    def read_cout():
        return cout_force_val if cout_forced else (1 if cnt_temp == 9 else 0)

    snapshots = {}
    # Simulate from 10ns to 140ns in 10ns half-clock steps (odd*10 = posedge, even*10 = negedge)
    # Match chapter timing: at 10ns rstn=1 & first count 1, 30ns->2, 50ns->3, 70ns->4
    for t in range(10, 141, 10):
        is_posedge = (t % 20 == 10)
        is_negedge = (t % 20 == 0)
        if is_posedge:
            if not cnt_forced:
                cnt_temp = 0 if cnt_temp == 9 else cnt_temp + 1
        if is_negedge and t == 80:
            cnt_forced = True
            cnt_temp = 6
            cout_forced = True
            cout_force_val = 1
        if is_negedge and t == 120:
            cnt_forced = False   # reg retains 6!
            cout_forced = False  # wire reverts immediately to (cnt_temp == 9) -> 0!
        if t in (70, 80, 110, 120, 130):
            snapshots[f"t{t}"] = [cnt_temp, read_cout()]

    expected = {
        "t70_cnt4_cout0": [4, 0],
        "t80_forced_cnt6_cout1": [6, 1],
        "t110_still_forced_cnt6_cout1": [6, 1],
        "t120_released_reg_holds_6_wire_drops_0": [6, 0],
        "t130_next_posedge_cnt7_cout0": [7, 0],
    }
    actual = {
        "t70_cnt4_cout0": snapshots["t70"],
        "t80_forced_cnt6_cout1": snapshots["t80"],
        "t110_still_forced_cnt6_cout1": snapshots["t110"],
        "t120_released_reg_holds_6_wire_drops_0": snapshots["t120"],
        "t130_next_posedge_cnt7_cout0": snapshots["t130"],
    }
    return {
        "name": "force_release_reg_vs_wire_counter10",
        "summary": "仿真 counter10 在 80ns 强制赋值与 120ns release 时 reg 保持 6 至下一上升沿变 7、wire 立即恢复 0 的差异",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_procedural_continuous_assign_priority_rules():
    def is_legal_target(stmt, var_kind):
        if stmt in ("assign_proc", "deassign"):
            return var_kind == "reg"
        if stmt in ("force", "release"):
            return var_kind in ("reg", "wire")
        return False

    # Precedence: force > procedural assign > normal procedural / continuous driver
    priority = ["normal_assign", "procedural_assign", "force"]
    expected = {
        "assign_on_reg_legal": True,
        "assign_on_wire_illegal": True,
        "force_on_reg_and_wire_legal": True,
        "force_highest_priority": True,
    }
    actual = {
        "assign_on_reg_legal": is_legal_target("assign_proc", "reg"),
        "assign_on_wire_illegal": not is_legal_target("assign_proc", "wire"),
        "force_on_reg_and_wire_legal": is_legal_target("force", "reg") and is_legal_target("force", "wire"),
        "force_highest_priority": priority[-1] == "force",
    }
    return {
        "name": "procedural_continuous_assign_priority_rules",
        "summary": "校验 assign/deassign(仅 reg)与 force/release(reg 与 wire)的目标类型约束及驱动优先级",
        "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_dff_normal_vs_assign_deassign(),
        case_force_release_reg_vs_wire_counter10(),
        case_procedural_continuous_assign_priority_rules(),
        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-deassign-lab/explore-deassign.py

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

下载课程关联工程包:Verilog

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

下载课程关联工程包:Verilog

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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