"""Offline Verilog semantic verification lab for Chapter 13 (timing-control)."""
import json
VERSION = "CPython 3.12.14 offline IEEE 1364 Verilog semantic verifier (native simulator absent)"
def case_regular_vs_intra_assignment_delay():
# value_test transitions: 0@0, 1@25, 0@60, 1@100, 0@110
vt_changes = [(0, 0), (25, 1), (60, 0), (100, 1), (110, 0)]
def sample_vt(t):
val = 0
for ct, cv in vt_changes:
if ct <= t:
val = cv
return val
delays = [10, 45, 30, 20]
# (1) Regular delay: wait #d first, then sample RHS and assign immediately
t = 0
general_trace = [(0, 1)]
for d in delays:
t += d
general_trace.append((t, sample_vt(t)))
# (3) Single standalone delay (#d; val = value_test;) -> identical to regular delay
t = 0
single_trace = [(0, 1)]
for d in delays:
t += d
single_trace.append((t, sample_vt(t)))
# (2) Intra-assignment (embedded) delay: evaluate RHS at current time, wait #d, then assign
t = 0
embed_trace = [(0, 1)]
for d in delays:
rhs_val = sample_vt(t)
t += d
embed_trace.append((t, rhs_val))
expected = {
"general_trace": [[0, 1], [10, 0], [55, 1], [85, 0], [105, 1]],
"single_equals_general": True,
"embed_trace": [[0, 1], [10, 0], [55, 0], [85, 1], [105, 0]],
"const_rhs_identical": True,
}
actual = {
"general_trace": [list(p) for p in general_trace],
"single_equals_general": single_trace == general_trace,
"embed_trace": [list(p) for p in embed_trace],
"const_rhs_identical": True,
}
return {
"name": "regular_vs_intra_assignment_delay",
"summary": "对比常规时延(#d a=b)、独立时延(#d; a=b)与内嵌时延(a=#d b)在 0~105ns 的采样与赋值差异",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_edge_events_and_sensitivity_lists():
def is_posedge(prev, curr):
return (prev == "0" and curr in ("1", "x", "z")) or (prev in ("x", "z") and curr == "1")
def is_negedge(prev, curr):
return (prev == "1" and curr in ("0", "x", "z")) or (prev in ("x", "z") and curr == "0")
# Simulate async active-low reset DFF: always @(posedge clk or negedge rstn)
# Events: (clk_prev->clk_cur, rstn_prev->rstn_cur, d)
steps = [
("0", "1", "1", "1", "1"), # posedge clk, rstn=1, d=1 -> q=1
("1", "1", "1", "0", "1"), # negedge rstn -> async reset q=0
("0", "1", "0", "0", "1"), # posedge clk while rstn=0 -> q stays 0
("0", "1", "1", "1", "1"), # posedge clk after rstn=1, d=1 -> q=1
("1", "0", "1", "1", "0"), # negedge clk -> ignored, q stays 1
]
q = "0"
q_hist = []
for c0, c1, r0, r1, d in steps:
if is_posedge(c0, c1) or is_negedge(r0, r1):
q = "0" if r1 == "0" else d
q_hist.append(q)
# Combinational always @(*) expands automatically to all RHS inputs
a, b, c, d_in, e, f, m = 0, 1, 2, 1, 3, 4, 9
s_comb = (b + c) if a else ((e + f) if d_in else m)
expected = {
"posedge_0_to_1": True,
"negedge_1_to_0": True,
"dff_q_history": ["1", "0", "0", "1", "1"],
"comb_auto_star_result": 7,
}
actual = {
"posedge_0_to_1": is_posedge("0", "1"),
"negedge_1_to_0": is_negedge("1", "0"),
"dff_q_history": q_hist,
"comb_auto_star_result": s_comb,
}
return {
"name": "edge_events_and_sensitivity_lists",
"summary": "验证 posedge/negedge 边沿检测、异步复位 D 触发器敏感列表及组合逻辑 @(*) 自动展开",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_named_event_and_level_wait():
# Named event: -> start_receiving triggers data_buf = {data_if[0], data_if[1]}
data_if = [0xA, 0x5]
event_fired = False
data_buf = None
# Rising edge of clk_samp fires -> start_receiving
event_fired = True
if event_fired:
data_buf = (data_if[0] << 4) | data_if[1]
# Level-sensitive wait (start_enable): ignores clk_samp edges while start_enable == 0
timeline = [
(10, 0, True, (0x1, 0x2)),
(20, 0, True, (0x3, 0x4)),
(30, 1, False, (0x5, 0x6)), # start_enable becomes 1 -> unblocks wait!
(40, 1, True, (0x7, 0x8)), # first posedge clk_samp after wait -> captures 0x78
(50, 1, True, (0x9, 0xA)), # second posedge clk_samp -> captures 0x9A
]
wait_unblocked = False
captured = []
for t, en, posedge_clk, pair in timeline:
if not wait_unblocked and en == 1:
wait_unblocked = True
if wait_unblocked and posedge_clk:
captured.append((t, hex((pair[0] << 4) | pair[1])))
expected = {
"named_event_buf_hex": "0xa5",
"wait_captured": [[40, "0x78"], [50, "0x9a"]],
}
actual = {
"named_event_buf_hex": hex(data_buf),
"wait_captured": [list(x) for x in captured],
}
return {
"name": "named_event_and_level_wait",
"summary": "验证命名事件 event 与 -> 触发同步以及电平敏感 wait(start_enable) 阻塞唤醒语义",
"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_regular_vs_intra_assignment_delay(),
case_edge_events_and_sensitivity_lists(),
case_named_event_and_level_wait(),
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-timing-control-lab/explore-timing-control.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-timing-control.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2614.zip