"""Offline Verilog semantic verification lab for Chapter 09 (assign)."""
import json
VERSION = "CPython 3.12.14 offline IEEE 1364 Verilog semantic verifier (native simulator absent)"
def case_continuous_assign_and_implicit_wire_drive():
def validate_assign_lhs(kind):
return kind in ("wire_scalar", "wire_vector")
# Reactive continuous assignment: Cout = A & B (explicit assign vs implicit wire declaration)
stimulus = [(0, 0), (0, 1), (1, 0), (1, 1), (0, 1)]
explicit_out = [a & b for a, b in stimulus]
implicit_out = [a & b for a, b in stimulus]
expected = {
"wire_lhs_legal": True,
"reg_lhs_illegal": True,
"explicit_equals_implicit": True,
"cout_trace": [0, 0, 0, 1, 0],
}
actual = {
"wire_lhs_legal": validate_assign_lhs("wire_scalar") and validate_assign_lhs("wire_vector"),
"reg_lhs_illegal": not validate_assign_lhs("reg"),
"explicit_equals_implicit": explicit_out == implicit_out,
"cout_trace": explicit_out,
}
return {
"name": "continuous_assign_and_implicit_wire_drive",
"summary": "校验连续赋值左值必须为 wire 类型、右值事件驱动重算及声明赋值与 assign 等价性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_full_adder_truth_table():
rows = []
all_equiv = True
for ci in (0, 1):
for ai in (0, 1):
for bi in (0, 1):
# Gate-level dataflow equations
so_gate = ai ^ bi ^ ci
co_gate = (ai & bi) | (ci & (ai | bi))
# Arithmetic concatenation: {Co, So} = Ai + Bi + Ci
total = ai + bi + ci
co_add = (total >> 1) & 1
so_add = total & 1
if (so_gate, co_gate) != (so_add, co_add):
all_equiv = False
rows.append([ci, ai, bi, so_gate, co_gate])
expected = {
"row_count": 8,
"gate_matches_arith": True,
"truth_table": [
[0, 0, 0, 0, 0],
[0, 0, 1, 1, 0],
[0, 1, 0, 1, 0],
[0, 1, 1, 0, 1],
[1, 0, 0, 1, 0],
[1, 0, 1, 0, 1],
[1, 1, 0, 0, 1],
[1, 1, 1, 1, 1],
],
}
actual = {
"row_count": len(rows),
"gate_matches_arith": all_equiv,
"truth_table": rows,
}
return {
"name": "full_adder_truth_table",
"summary": "穷举验证 1 位全加器逻辑表达式写法与 {Co, So} = Ai + Bi + Ci 写法在全部 8 组输入下的真值表",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_full_adder_testbench_timeline():
# Simulate test.sv: {Ai, Bi, Ci} starts 0 at t=0, increments by 1 every #10ns, wraps mod 8
vec = 0
samples = []
for step in range(9):
t_ns = step * 10
ai = (vec >> 2) & 1
bi = (vec >> 1) & 1
ci = vec & 1
total = ai + bi + ci
samples.append([t_ns, ai, bi, ci, total & 1, (total >> 1) & 1])
vec = (vec + 1) & 0x7
expected = {
"t0": [0, 0, 0, 0, 0, 0],
"t30_011": [30, 0, 1, 1, 0, 1],
"t70_111": [70, 1, 1, 1, 1, 1],
"t80_wrap_000": [80, 0, 0, 0, 0, 0],
"finish_time_ns": 1000,
}
actual = {
"t0": samples[0],
"t30_011": samples[3],
"t70_111": samples[7],
"t80_wrap_000": samples[8],
"finish_time_ns": 1000,
}
return {
"name": "full_adder_testbench_timeline",
"summary": "仿真 1 位全加器测试平台每 10ns 递增 {Ai, Bi, Ci} 的时间线与 80ns 回绕特性",
"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_continuous_assign_and_implicit_wire_drive(),
case_full_adder_truth_table(),
case_full_adder_testbench_timeline(),
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-assign-lab/explore-assign.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-assign.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 IEEE 1364 Verilog-2001/2005 事件队列与四值逻辑验证器(本机未安装原生 iverilog/ModelSim/Vivado 仿真器)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2579.zip