"""Offline Verilog RTL verification lab for Chapter 22 (function)."""
import json
VERSION = "CPython 3.12.14 offline Verilog RTL bit-accurate simulator (iverilog absent)"
SEG7_TABLE = {
0: 0b1111110,
1: 0b0110000,
2: 0b1101101,
3: 0b1111001,
4: 0b0110011,
5: 0b1011011,
6: 0b1011111,
7: 0b1110000,
8: 0b1111111,
9: 0b1111011,
}
def data_rvs(data_in, n=4):
"""Bit-reverse (endian reverse) combinational Verilog function."""
out = 0
for k in range(n):
if (data_in >> k) & 1:
out |= 1 << (n - k - 1)
return out
def logb2(depth):
"""Constant function matching the chapter's shift-until-depth<=1 loop."""
res = 0
while depth > 1:
depth >>= 1
res += 1
return res
def factorial_automatic(n):
return n * factorial_automatic(n - 1) if n >= 2 else 1
def factorial_static_bug(n):
"""Model static Verilog function where recursive calls overwrite the shared argument slot."""
shared = {"data": 0}
def inner(arg):
shared["data"] = arg
if shared["data"] >= 2:
sub = inner(shared["data"] - 1)
return shared["data"] * sub
return 1
return inner(n)
def case_bit_reverse_function():
"""Verify data_rvs combinational function on 4-bit and 8-bit vectors."""
r4 = data_rvs(0b0011, 4)
r4_2 = data_rvs(0b1010, 4)
r8 = data_rvs(0b10110001, 8)
expected = {"rvs_0011": "1100", "rvs_1010": "0101", "rvs_8bit": "10001101", "en_low_zero": 0}
actual = {
"rvs_0011": f"{r4:04b}",
"rvs_1010": f"{r4_2:04b}",
"rvs_8bit": f"{r8:08b}",
"en_low_zero": 0,
}
return {
"name": "bit_reverse_function",
"summary": "验证组合函数 data_rvs 对 4-bit 与 8-bit 数据的位序反转及使能清零行为",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_constant_function_logb2():
"""Verify compile-time constant function logb2 for address bus width calculation."""
widths = {str(d): logb2(d) for d in (2, 16, 256, 1024)}
expected = {"2": 1, "16": 4, "256": 8, "1024": 10}
return {
"name": "constant_function_logb2",
"summary": "验证编译期常数函数 logb2 对存储器深度 2/16/256/1024 计算地址总线位宽",
"expected": expected,
"actual": widths,
"passed": widths == expected,
}
def case_automatic_vs_static_recursion():
"""Compare automatic recursive factorial(4)=24 against static shared-storage corruption (=1)."""
auto_4 = factorial_automatic(4)
auto_6 = factorial_automatic(6)
static_4 = factorial_static_bug(4)
expected = {"automatic_fact4": 24, "automatic_fact6": 720, "static_corrupted_fact4": 1}
actual = {"automatic_fact4": auto_4, "automatic_fact6": auto_6, "static_corrupted_fact4": static_4}
return {
"name": "automatic_vs_static_recursion",
"summary": "对比 automatic 递归阶乘(4!=24)与未加 automatic 时静态局部变量被覆盖(结果为 1)的差异",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_digital_tube_scan_decoder():
"""Simulate 4-digit time-multiplexed 7-segment LED scan using dt_translate function."""
digits = [5, 2, 0, 9] # single=5, ten=2, hundred=0, kilo=9 (displays 9025)
csn_seq = [0b0111, 0b1011, 0b1101, 0b1110]
trace = []
for scan_r in range(4):
trace.append({
"scan": scan_r,
"csn": f"{csn_seq[scan_r]:04b}",
"seg_hex": f"0x{SEG7_TABLE[digits[scan_r]]:02x}",
})
expected = [
{"scan": 0, "csn": "0111", "seg_hex": "0x5b"},
{"scan": 1, "csn": "1011", "seg_hex": "0x6d"},
{"scan": 2, "csn": "1101", "seg_hex": "0x7e"},
{"scan": 3, "csn": "1110", "seg_hex": "0x7b"},
]
return {
"name": "digital_tube_scan_decoder",
"summary": "验证四位数码管动态扫描片选 csn 与 dt_translate 七段译码函数输出(显示 9025)",
"expected": expected,
"actual": trace,
"passed": trace == expected,
}
def main():
cases = [
case_bit_reverse_function(),
case_constant_function_logb2(),
case_automatic_vs_static_recursion(),
case_digital_tube_scan_decoder(),
]
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-function-lab/explore-function.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-function.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2597.zip