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

离线仿真并行 initial 激励时间线、100MHz always 时钟发生器与嵌套约束

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

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


def case_parallel_initial_blocks_timeline():
    # Two independent initial blocks starting at t=0
    def run_initial(steps):
        t = 0
        trace = []
        for dt, val in steps:
            t += dt
            trace.append([t, val])
        return trace

    ai_trace = run_initial([(0, 0), (25, 1), (35, 0), (40, 1), (10, 0)])
    bi_trace = run_initial([(0, 1), (70, 0), (20, 1)])
    # Third initial block watchdog stops simulation at $time >= 1000
    t_watch = 0
    while t_watch < 1000:
        t_watch += 100

    expected = {
        "ai_trace": [[0, 0], [25, 1], [60, 0], [100, 1], [110, 0]],
        "bi_trace": [[0, 1], [70, 0], [90, 1]],
        "both_start_at_0": True,
        "watchdog_finish_ns": 1000,
    }
    actual = {
        "ai_trace": ai_trace,
        "bi_trace": bi_trace,
        "both_start_at_0": ai_trace[0][0] == 0 and bi_trace[0][0] == 0,
        "watchdog_finish_ns": t_watch,
    }
    return {
        "name": "parallel_initial_blocks_timeline",
        "summary": "仿真两个并行 initial 块从 0 时刻并发启动且块内按顺序执行的信号时间线",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_always_100mhz_clock_generator():
    clk_freq_mhz = 100
    clk_cycle_ns = int(1e9 / (clk_freq_mhz * 1e6))  # 10 ns
    half_cycle_ns = clk_cycle_ns // 2               # 5 ns

    # initial clk = 0; always #5 clk = ~clk;
    clk = 0
    edges = [(0, clk)]
    t = 0
    while t < 30:
        t += half_cycle_ns
        clk = 1 - clk
        edges.append((t, clk))

    # always #10 if ($time >= 1000) $finish;
    stop_t = 0
    while True:
        stop_t += 10
        if stop_t >= 1000:
            break

    expected = {
        "clk_cycle_ns": 10,
        "half_cycle_ns": 5,
        "first_30ns_edges": [[0, 0], [5, 1], [10, 0], [15, 1], [20, 0], [25, 1], [30, 0]],
        "computed_freq_mhz": 100.0,
        "stop_threshold_ns": 1000,
    }
    actual = {
        "clk_cycle_ns": clk_cycle_ns,
        "half_cycle_ns": half_cycle_ns,
        "first_30ns_edges": [list(e) for e in edges],
        "computed_freq_mhz": 1000.0 / clk_cycle_ns,
        "stop_threshold_ns": stop_t,
    }
    return {
        "name": "always_100mhz_clock_generator",
        "summary": "仿真 100MHz(周期 10ns、半周期 5ns)always 时钟发生器及仿真终止条件",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def case_nesting_and_synthesizability_rules():
    def check_structure(outer, inner):
        # initial and always cannot be nested inside each other
        if outer in ("initial", "always") and inner in ("initial", "always"):
            return False
        return True

    expected = {
        "initial_in_always_illegal": True,
        "always_in_initial_illegal": True,
        "begin_end_in_initial_legal": True,
        "initial_executes_once": True,
        "always_executes_repeatedly": True,
    }
    actual = {
        "initial_in_always_illegal": not check_structure("always", "initial"),
        "always_in_initial_illegal": not check_structure("initial", "always"),
        "begin_end_in_initial_legal": check_structure("initial", "begin"),
        "initial_executes_once": True,
        "always_executes_repeatedly": True,
    }
    return {
        "name": "nesting_and_synthesizability_rules",
        "summary": "校验 initial/always 不可互相嵌套、模块内并行执行及单次与循环执行语义",
        "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": 2,
    }
    actual = {
        "source_zip_requested": False,
        "policy": "not_requested_inherited_origin_hold",
        "uncached_image_count": 2,
    }
    return {
        "name": "source_archive_offline_audit",
        "summary": "核对本章来源示例压缩包与 2 张未缓存仿真波形图的离线合规登记状态",
        "expected": expected,
        "actual": actual,
        "passed": actual == expected,
    }


def main():
    cases = [
        case_parallel_initial_blocks_timeline(),
        case_always_100mhz_clock_generator(),
        case_nesting_and_synthesizability_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-process-structure-lab/explore-process-structure.py

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

下载课程关联工程包:Verilog

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

下载课程关联工程包:Verilog

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

搜索教程、工具与资讯

热门搜索

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

欢迎登录蓝色天空网

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