"""Offline Verilog RTL verification lab for Chapter 26 (latch)."""
import json
VERSION = "CPython 3.12.14 offline Verilog RTL bit-accurate simulator (iverilog absent)"
def check_latch_inference(block_kind, branches_assigned, outputs, self_ref_outputs, sensitivity_complete=True):
"""Static RTL analyzer modeling synthesis latch inference rules."""
if block_kind == "sequential":
return {"inferred_latches": [], "storage_kind": "flip_flop"}
latches = set()
if not sensitivity_complete:
latches.update(outputs)
for out in outputs:
if out in self_ref_outputs:
latches.add(out)
if not all(out in b for b in branches_assigned):
latches.add(out)
return {
"inferred_latches": sorted(latches),
"storage_kind": "latch" if latches else "combinational",
}
def case_incomplete_if_comb_vs_seq():
"""Verify incomplete if infers Latch in combinational always@(*) but Flip-Flop in always@(posedge clk)."""
comb_no_else = check_latch_inference("combinational", [{"q"}, set()], ["q"], set())
comb_with_else = check_latch_inference("combinational", [{"q"}, {"q"}], ["q"], set())
seq_no_else = check_latch_inference("sequential", [{"q"}, set()], ["q"], set())
# Multi-signal partial assignment: if(en) q1=data1; else q2=data2; -> both q1 and q2 infer latches
comb_partial_two = check_latch_inference("combinational", [{"q1"}, {"q2"}], ["q1", "q2"], set())
comb_complete_two = check_latch_inference("combinational", [{"q1", "q2"}, {"q1", "q2"}], ["q1", "q2"], set())
expected = {
"comb_no_else": ["q"],
"comb_with_else": [],
"seq_no_else_kind": "flip_flop",
"comb_partial_two": ["q1", "q2"],
"comb_complete_two": [],
}
actual = {
"comb_no_else": comb_no_else["inferred_latches"],
"comb_with_else": comb_with_else["inferred_latches"],
"seq_no_else_kind": seq_no_else["storage_kind"],
"comb_partial_two": comb_partial_two["inferred_latches"],
"comb_complete_two": comb_complete_two["inferred_latches"],
}
return {
"name": "incomplete_if_comb_vs_seq",
"summary": "验证组合逻辑缺 else 或多信号漏赋值推断 Latch,补全/赋初值及同步时序块不产生 Latch",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_incomplete_case_vs_full_default():
"""Verify 2-bit sel case missing 2'b10/2'b11 infers Latch unless default or all 4 items are covered."""
case_missing = check_latch_inference("combinational", [{"q"}, {"q"}, set(), set()], ["q"], set())
case_default = check_latch_inference("combinational", [{"q"}, {"q"}, {"q"}, {"q"}], ["q"], set())
expected = {"missing_branches_latch": ["q"], "with_default_or_all4": []}
actual = {
"missing_branches_latch": case_missing["inferred_latches"],
"with_default_or_all4": case_default["inferred_latches"],
}
return {
"name": "incomplete_case_vs_full_default",
"summary": "验证 2-bit case 缺少 2'b10/2'b11 分支推断 Latch,添加 default 或穷举全部选项后消除",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_self_reference_and_sensitivity_latch():
"""Verify self-referencing combinational assignment/condition and incomplete sensitivity list infer Latch."""
self_if = check_latch_inference("combinational", [{"a"}, {"a"}], ["a"], {"a"})
self_case = check_latch_inference("combinational", [{"c"}, {"c"}, {"c"}], ["c"], {"c"})
broken_by_ff = check_latch_inference("combinational", [{"a"}, {"a"}], ["a"], set())
incomplete_sens = check_latch_inference(
"combinational", [{"q"}, {"q"}], ["q"], set(), sensitivity_complete=False
)
expected = {
"self_cond_a_latch": ["a"],
"self_assign_c_latch": ["c"],
"reg_delayed_a_r_no_latch": [],
"incomplete_sensitivity_latch": ["q"],
}
actual = {
"self_cond_a_latch": self_if["inferred_latches"],
"self_assign_c_latch": self_case["inferred_latches"],
"reg_delayed_a_r_no_latch": broken_by_ff["inferred_latches"],
"incomplete_sensitivity_latch": incomplete_sens["inferred_latches"],
}
return {
"name": "self_reference_and_sensitivity_latch",
"summary": "验证组合自引用(if(a&b) a=... / c=c)与不完整敏感列表推断 Latch,以及打拍断环消除效果",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_level_latch_vs_edge_ff_behavior():
"""Simulate transparent level-sensitive Latch vs rising-edge Flip-Flop on glitchy data input."""
# During en=1 (or clk=1), data toggles 0 -> 1 -> 0 -> 1; posedge sampled data=0
data_during_high = [0, 1, 0, 1]
latch_outputs = list(data_during_high) # transparent when en=1, passes all glitches
ff_outputs = [data_during_high[0]] * len(data_during_high) # holds value sampled at posedge
expected = {
"latch_transitions_while_en_high": 3,
"ff_transitions_while_clk_high": 0,
}
actual = {
"latch_transitions_while_en_high": sum(
1 for i in range(1, len(latch_outputs)) if latch_outputs[i] != latch_outputs[i - 1]
),
"ff_transitions_while_clk_high": sum(
1 for i in range(1, len(ff_outputs)) if ff_outputs[i] != ff_outputs[i - 1]
),
}
return {
"name": "level_latch_vs_edge_ff_behavior",
"summary": "对比电平透明锁存器在使能高电平期间透传输入毛刺与边沿触发器仅在时钟边沿采样的特性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_incomplete_if_comb_vs_seq(),
case_incomplete_case_vs_full_default(),
case_self_reference_and_sensitivity_latch(),
case_level_latch_vs_edge_ff_behavior(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。
入口文件:projects/verilog-latch-lab/explore-latch.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-latch.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2601.zip