"""Offline Verilog RTL verification lab for Chapter 35 (numerical-conversion)."""
import json
VERSION = "CPython 3.12.14 offline Verilog RTL bit-accurate simulator (iverilog absent)"
def dec_to_twos_invert(val, dw=4):
if val >= 0:
return val & ((1 << dw) - 1)
mag = (-val) & ((1 << (dw - 1)) - 1)
low = ((~mag) + 1) & ((1 << (dw - 1)) - 1)
return (1 << (dw - 1)) | low
def dec_to_twos_mod(val, dw=4):
return (val + (1 << dw)) & ((1 << dw) - 1) if val < 0 else val
def twos_to_dec_invert(bits, dw=4):
mask = (1 << dw) - 1
if (bits >> (dw - 1)) & 1:
return -(((~bits) + 1) & mask)
return bits
def twos_to_dec_mod(bits, dw=4):
return bits - (1 << dw) if ((bits >> (dw - 1)) & 1) else bits
def case_signed_dec_twos_complement_roundtrip():
"""Verify both two's-complement conversion methods (invert+1 & modular +2^DW) across all 4-bit signed values."""
neg6_inv = dec_to_twos_invert(-6, 4)
neg6_mod = dec_to_twos_mod(-6, 4)
back_inv = twos_to_dec_invert(0b1010, 4)
back_mod = twos_to_dec_mod(0b1010, 4)
all_16_ok = all(
dec_to_twos_invert(v, 4) == dec_to_twos_mod(v, 4)
and twos_to_dec_invert(dec_to_twos_mod(v, 4), 4) == v
and twos_to_dec_mod(dec_to_twos_mod(v, 4), 4) == v
for v in range(-8, 8)
)
expected = {
"neg6_bin_invert": "1010",
"neg6_bin_mod": "1010",
"back_from_1010_inv": -6,
"back_from_1010_mod": -6,
"all_16_4bit_roundtrip_ok": True,
}
actual = {
"neg6_bin_invert": f"{neg6_inv:04b}",
"neg6_bin_mod": f"{neg6_mod:04b}",
"back_from_1010_inv": back_inv,
"back_from_1010_mod": back_mod,
"all_16_4bit_roundtrip_ok": all_16_ok,
}
return {
"name": "signed_dec_twos_complement_roundtrip",
"summary": "验证十进制有符号数与 4-bit 补码双向转换(取反加一法与模 2^DW 法)全量 16 值一致",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_abs_value_formulas():
"""Compare chapter's biased abs ((sign?~x:x)+1) vs exact two's complement abs (sign?(~x+1):x)."""
def chapter_abs(bits, dw=4):
mask = (1 << dw) - 1
sign = (bits >> (dw - 1)) & 1
return (((~bits) & mask if sign else bits) + 1) & mask
def exact_abs(bits, dw=4):
mask = (1 << dw) - 1
sign = (bits >> (dw - 1)) & 1
return (((~bits) + 1) & mask) if sign else bits
expected = {
"neg6_chapter_abs": 6,
"pos2_chapter_abs_plus1": 3,
"pos2_exact_abs": 2,
"neg8_min_overflow_bits": 8,
}
actual = {
"neg6_chapter_abs": chapter_abs(0b1010, 4),
"pos2_chapter_abs_plus1": chapter_abs(0b0010, 4),
"pos2_exact_abs": exact_abs(0b0010, 4),
"neg8_min_overflow_bits": exact_abs(0b1000, 4),
}
return {
"name": "abs_value_formulas",
"summary": "对比统一加一绝对值写法(正数偏移+1)与标准补码绝对值写法及最小负数 -8 边界特性",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_signed_to_offset_unsigned():
"""Verify MSB flip {!dbin[DW-1], dbin[DW-2:0]} shifts [-8..+7] to [0..15] preserving differences."""
def to_offset_unsigned(bits, dw=4):
return bits ^ (1 << (dw - 1))
# -6 (1010) -> 0010 (2); +2 (0010) -> 1010 (10); difference 2 - (-6) = 8 == 10 - 2
u_neg6 = to_offset_unsigned(0b1010, 4)
u_pos2 = to_offset_unsigned(0b0010, 4)
order_preserved = all(
to_offset_unsigned(dec_to_twos_mod(v, 4), 4) == v + 8 for v in range(-8, 8)
)
expected = {
"neg6_to_unsigned": 2,
"pos2_to_unsigned": 10,
"diff_preserved": 8,
"monotonic_shift_plus8": True,
}
actual = {
"neg6_to_unsigned": u_neg6,
"pos2_to_unsigned": u_pos2,
"diff_preserved": u_pos2 - u_neg6,
"monotonic_shift_plus8": order_preserved,
}
return {
"name": "signed_to_offset_unsigned",
"summary": "验证最高位取反将有符号补码 [-8,7] 平移至无符号 [0,15] 且保持线性差值不变",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_sign_extension_4_to_8_bits():
"""Verify sign extension {{W{dbin[DW-1]}}, dbin} preserves numeric value for both negative and positive."""
def sign_extend(bits, src_w=4, ext_w=4):
sign = (bits >> (src_w - 1)) & 1
high = ((1 << ext_w) - 1) if sign else 0
return (high << src_w) | (bits & ((1 << src_w) - 1))
neg6_ext = sign_extend(0b1010, 4, 4) # 8'b11111010 = -6
pos5_ext = sign_extend(0b0101, 4, 4) # 8'b00000101 = +5
all_preserved = all(
twos_to_dec_mod(sign_extend(dec_to_twos_mod(v, 4), 4, 4), 8) == v for v in range(-8, 8)
)
expected = {
"neg6_8bit_bin": "11111010",
"neg6_8bit_dec": -6,
"pos5_8bit_bin": "00000101",
"all_16_sign_ext_ok": True,
}
actual = {
"neg6_8bit_bin": f"{neg6_ext:08b}",
"neg6_8bit_dec": twos_to_dec_mod(neg6_ext, 8),
"pos5_8bit_bin": f"{pos5_ext:08b}",
"all_16_sign_ext_ok": all_preserved,
}
return {
"name": "sign_extension_4_to_8_bits",
"summary": "验证有符号数 4-bit 至 8-bit 符号位扩展(-6 -> 8'b11111010)对全部 16 个值保持数值不变",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_signed_dec_twos_complement_roundtrip(),
case_abs_value_formulas(),
case_signed_to_offset_unsigned(),
case_sign_extension_4_to_8_bits(),
]
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-numerical-conversion-lab/explore-numerical-conversion.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-numerical-conversion.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线 Verilog RTL 位精确数字算法与周期仿真模型(本机未安装 iverilog/vvp/verilator/vsim/vivado)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联36章) · 含当前章节或实例 · mbskycom202612a2580.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2605.zip