"""Offline Verilog semantic verification lab for Chapter 14 (statements-block)."""
import json
VERSION = "CPython 3.12.14 offline IEEE 1364 Verilog semantic verifier (native simulator absent)"
def case_begin_end_vs_fork_join_timestamps():
# (1) begin...end: delays accumulate sequentially
seq_t = 0
seq_t += 5
ai_sequen_t = (seq_t, 5)
seq_t += 5
bi_sequen_t = (seq_t, 8)
# (2) fork...join (blocking): delays relative to fork entry time (t=0)
fork_entry = 0
ai_paral_t = (fork_entry + 5, 5)
bi_paral_t = (fork_entry + 5, 8)
# (3) fork...join (non-blocking): also relative to fork entry time (t=0)
ai_nonblk_t = (fork_entry + 5, 5)
bi_nonblk_t = (fork_entry + 5, 8)
expected = {
"seq_ai": [5, 5],
"seq_bi": [10, 8],
"fork_ai": [5, 5],
"fork_bi": [5, 8],
"fork_nba_matches_fork_blk": True,
}
actual = {
"seq_ai": list(ai_sequen_t),
"seq_bi": list(bi_sequen_t),
"fork_ai": list(ai_paral_t),
"fork_bi": list(bi_paral_t),
"fork_nba_matches_fork_blk": (ai_nonblk_t == ai_paral_t and bi_nonblk_t == bi_paral_t),
}
return {
"name": "begin_end_vs_fork_join_timestamps",
"summary": "对比顺序块 begin...end 累加时延与并行块 fork...join 相对于入块时刻的并发时间戳",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_nested_seq_fork_join_barrier():
# initial begin ai_sequen2=5 (@0); fork #10 ai_paral2=5; #15 bi_paral2=8; join #20 bi_sequen2=8; end
t = 0
events = {"ai_sequen2": (t, 5)}
fork_start = t
branch1_end = fork_start + 10
branch2_end = fork_start + 15
events["ai_paral2"] = (branch1_end, 5)
events["bi_paral2"] = (branch2_end, 8)
# join barrier waits for the slowest branch
t = max(branch1_end, branch2_end)
t += 20
events["bi_sequen2"] = (t, 8)
expected = {
"ai_sequen2": [0, 5],
"ai_paral2": [10, 5],
"bi_paral2": [15, 8],
"fork_join_exit_ns": 15,
"bi_sequen2": [35, 8],
}
actual = {
"ai_sequen2": list(events["ai_sequen2"]),
"ai_paral2": list(events["ai_paral2"]),
"bi_paral2": list(events["bi_paral2"]),
"fork_join_exit_ns": max(branch1_end, branch2_end),
"bi_sequen2": list(events["bi_sequen2"]),
}
return {
"name": "nested_seq_fork_join_barrier",
"summary": "验证顺序块嵌套 fork...join 时 join 同步屏障等待最慢分支(15ns)后再推进后续 #20(35ns)",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_named_block_hierarchy_and_disable():
# Named block hierarchical variable access: test.clk_cnt.i increments by 10 every 10ns
scope = {"test.clk_cnt.i": 0}
stop_flag = 0
stop_time = None
for t in range(10, 120, 10):
scope["test.clk_cnt.i"] += 10
if scope["test.clk_cnt.i"] == 100 and stop_flag == 0:
stop_flag = 1
stop_time = t
# Named block disable: loop stops external clk_gen and current cnt_loop once i_d >= 50
i_d = 0
clk_gen_active = True
clk_toggles = 0
t_sim = 0
while i_d <= 100:
t_sim += 10
if clk_gen_active:
clk_toggles += 1
if i_d >= 50:
# disable clk_proc.clk_gen; disable cnt_loop;
clk_gen_active = False
break
i_d += 10
# disable inside always/forever only exits current iteration (continue-like)
always_iters_completed = 0
always_skipped_iter2 = False
for it in range(1, 5):
if it == 2:
always_skipped_iter2 = True
continue # simulates disable of inner named block inside always
always_iters_completed += 1
expected = {
"hier_stop_time_ns": 100,
"stop_flag": 1,
"disabled_i_d": 50,
"disable_time_ns": 60,
"clk_gen_stopped": True,
"always_disable_acts_like_continue": True,
}
actual = {
"hier_stop_time_ns": stop_time,
"stop_flag": stop_flag,
"disabled_i_d": i_d,
"disable_time_ns": t_sim,
"clk_gen_stopped": not clk_gen_active,
"always_disable_acts_like_continue": always_skipped_iter2 and always_iters_completed == 3,
}
return {
"name": "named_block_hierarchy_and_disable",
"summary": "验证命名块局部变量层次名引用、disable 终止本块与外部时钟块及在 always 块中类似 continue 的语义",
"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": 4,
}
actual = {
"source_zip_requested": False,
"policy": "not_requested_inherited_origin_hold",
"uncached_image_count": 4,
}
return {
"name": "source_archive_offline_audit",
"summary": "核对本章来源示例压缩包与 4 张未缓存仿真波形图的离线合规登记状态",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_begin_end_vs_fork_join_timestamps(),
case_nested_seq_fork_join_barrier(),
case_named_block_hierarchy_and_disable(),
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-statements-block-lab/explore-statements-block.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-statements-block.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2610.zip