"""Offline Verilog semantic verification lab for Chapter 03 (design-method)."""
import json
VERSION = "CPython 3.12.14 offline IEEE 1364 Verilog semantic verifier (native simulator absent)"
def case_top_down_hierarchy_decomposition():
tree = {
"display_top": ["timing_ctrl", "frame_buffer", "pixel_pipeline"],
"timing_ctrl": ["clk_div", "sync_gen"],
"frame_buffer": ["sram_ctrl"],
"pixel_pipeline": ["color_lut", "gamma_corr"],
"clk_div": [],
"sync_gen": [],
"sram_ctrl": [],
"color_lut": [],
"gamma_corr": [],
}
leaves = sorted(k for k, children in tree.items() if not children)
visited = set()
def walk(node):
if node in visited:
return False
visited.add(node)
return all(walk(ch) for ch in tree[node])
acyclic = walk("display_top")
expected = {
"acyclic_dag": True,
"total_modules": 9,
"leaf_count": 5,
"leaves": ["clk_div", "color_lut", "gamma_corr", "sram_ctrl", "sync_gen"],
}
actual = {
"acyclic_dag": acyclic and len(visited) == len(tree),
"total_modules": len(tree),
"leaf_count": len(leaves),
"leaves": leaves,
}
return {
"name": "top_down_hierarchy_decomposition",
"summary": "验证自上而下(Top-Down)模块层次分解的无环树结构与底层功能块划分",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_rtl_to_silicon_flow_stages():
stages = [
"requirements_analysis",
"functional_partition",
"rtl_verilog_coding",
"functional_pre_simulation",
"logic_synthesis",
"place_and_route",
"timing_post_simulation",
"fpga_download_or_asic_tapeout",
]
timing_fail_target = "rtl_verilog_coding"
expected = {
"stage_count": 8,
"pre_sim_before_synthesis": True,
"post_sim_after_pnr": True,
"timing_violation_reentry": "rtl_verilog_coding",
}
actual = {
"stage_count": len(stages),
"pre_sim_before_synthesis": stages.index("functional_pre_simulation") < stages.index("logic_synthesis"),
"post_sim_after_pnr": stages.index("timing_post_simulation") > stages.index("place_and_route"),
"timing_violation_reentry": timing_fail_target,
}
return {
"name": "rtl_to_silicon_flow_stages",
"summary": "校验需求分析、功能划分、RTL 编写、前仿真、综合、布局布线、后仿真到下载/流片 8 阶段顺序",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_synthesizable_vs_nonsynthesizable_subset():
constructs = {
"assign_comb": True,
"always_posedge_ff": True,
"always_star_comb": True,
"case_mux": True,
"delay_hash_10": False,
"initial_test_stimulus": False,
"system_finish": False,
"force_release_debug": False,
}
synth = sorted(k for k, v in constructs.items() if v)
nonsynth = sorted(k for k, v in constructs.items() if not v)
expected = {
"synthesizable_count": 4,
"nonsynthesizable_count": 4,
"delay_is_synthesizable": False,
}
actual = {
"synthesizable_count": len(synth),
"nonsynthesizable_count": len(nonsynth),
"delay_is_synthesizable": constructs["delay_hash_10"],
}
return {
"name": "synthesizable_vs_nonsynthesizable_subset",
"summary": "区分可综合 RTL 语句子集与不可综合的时延/仿真激励语句",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_pre_sim_vs_post_sim_timing_model():
# Pre-sim: zero delay -> always passes functional check
# Post-sim: T_clk must satisfy T_cq + T_comb + T_route + T_setup <= T_clk
t_cq_ns = 1.2
t_comb_ns = 4.5
t_route_ns = 2.8
t_setup_ns = 0.7
required_period_ns = round(t_cq_ns + t_comb_ns + t_route_ns + t_setup_ns, 2)
pre_sim_ok_at_8ns = True # zero-delay functional sim ignores physical timing
post_sim_ok_at_8ns = 8.0 >= required_period_ns
post_sim_ok_at_10ns = 10.0 >= required_period_ns
expected = {
"required_period_ns": 9.2,
"pre_sim_8ns_pass": True,
"post_sim_8ns_pass": False,
"post_sim_10ns_pass": True,
}
actual = {
"required_period_ns": required_period_ns,
"pre_sim_8ns_pass": pre_sim_ok_at_8ns,
"post_sim_8ns_pass": post_sim_ok_at_8ns,
"post_sim_10ns_pass": post_sim_ok_at_10ns,
}
return {
"name": "pre_sim_vs_post_sim_timing_model",
"summary": "对比零延迟功能前仿真与含单元/布线时延的时序后仿真在 8ns 与 10ns 时钟周期下的建立时间判定",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_top_down_hierarchy_decomposition(),
case_rtl_to_silicon_flow_stages(),
case_synthesizable_vs_nonsynthesizable_subset(),
case_pre_sim_vs_post_sim_timing_model(),
]
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-design-method-lab/explore-design-method.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-design-method.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2591.zip
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip