"""Offline Computer Organization lab for Chapter 02 (why-computer-only-knows-0-and-1)."""
import json
VERSION = "CPython 3.12.14 offline computer organization simulation verifier"
def classify_voltage_3v3(v: float) -> str:
if 0.0 <= v <= 1.5:
return "LOGIC_0"
if 1.8 <= v <= 3.3:
return "LOGIC_1"
return "FORBIDDEN"
def case_voltage_noise_margin():
samples = {
"0.0V": classify_voltage_3v3(0.0),
"0.8V": classify_voltage_3v3(0.8),
"1.5V": classify_voltage_3v3(1.5),
"1.65V": classify_voltage_3v3(1.65),
"1.8V": classify_voltage_3v3(1.8),
"3.3V": classify_voltage_3v3(3.3),
}
# 10-level decimal encoding over 3.3V has step 0.33V (half-step margin 0.165V)
dec_step = round(3.3 / 10, 2)
# 0.66V (digit 2) drifted by +0.20V -> 0.86V, closer to digit 3 (0.99V) than digit 2 (0.66V)
drifted = round(0.66 + 0.20, 2)
nearest_digit = round(drifted / dec_step)
# In binary, 0.2V drifted by +0.20V -> 0.4V still LOGIC_0
actual = {
"samples": samples,
"forbidden_band_volts": round(1.8 - 1.5, 2),
"decimal_step_volts": dec_step,
"decimal_digit2_after_0.20v_noise": nearest_digit,
"binary_0_after_0.20v_noise": classify_voltage_3v3(0.2 + 0.20),
}
expected = {
"samples": {
"0.0V": "LOGIC_0",
"0.8V": "LOGIC_0",
"1.5V": "LOGIC_0",
"1.65V": "FORBIDDEN",
"1.8V": "LOGIC_1",
"3.3V": "LOGIC_1",
},
"forbidden_band_volts": 0.3,
"decimal_step_volts": 0.33,
"decimal_digit2_after_0.20v_noise": 3,
"binary_0_after_0.20v_noise": "LOGIC_0",
}
return {
"name": "voltage_noise_margin",
"summary": "校验 3.3V 数字电路的逻辑 0/禁止区/逻辑 1 电压区间及二进制相比十进制的抗干扰容差优势",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_byte_bulb_weighted_sum():
def bits_to_dec(bits: list[int]) -> int:
return sum(bit * (2 ** (7 - i)) for i, bit in enumerate(bits))
def dec_to_bits(n: int) -> list[int]:
return [(n >> (7 - i)) & 1 for i in range(8)]
presets = [0, 42, 65, 97, 114, 255]
mapping = {}
for n in presets:
bits = dec_to_bits(n)
mapping[str(n)] = {"bits": "".join(str(b) for b in bits), "restored": bits_to_dec(bits)}
actual = {
"byte_distinct_values": 2 ** 8,
"weights_msb_to_lsb": [2 ** i for i in range(7, -1, -1)],
"presets": mapping,
}
expected = {
"byte_distinct_values": 256,
"weights_msb_to_lsb": [128, 64, 32, 16, 8, 4, 2, 1],
"presets": {
"0": {"bits": "00000000", "restored": 0},
"42": {"bits": "00101010", "restored": 42},
"65": {"bits": "01000001", "restored": 65},
"97": {"bits": "01100001", "restored": 97},
"114": {"bits": "01110010", "restored": 114},
"255": {"bits": "11111111", "restored": 255},
},
}
return {
"name": "byte_bulb_weighted_sum",
"summary": "仿真 8 位字节灯泡按权展开(128..1),验证 0、42、65('A')、97('a')、114('r') 与 255 的双向映射",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_byte_width_capacities():
actual = {
"1_byte_max": (1 << 8) - 1,
"2_byte_max": (1 << 16) - 1,
"4_byte_max": (1 << 32) - 1,
"8_byte_max": (1 << 64) - 1,
}
expected = {
"1_byte_max": 255,
"2_byte_max": 65535,
"4_byte_max": 4294967295,
"8_byte_max": 18446744073709551615,
}
return {
"name": "byte_width_capacities",
"summary": "校验 1/2/4/8 字节(8/16/32/64 bit)无符号整数的最大可表示数值",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def case_bitwise_ops_and_binary_basics():
a, b = 0b0011, 0b0101
sample_word = "BYTES"
encoded = [(ch, ord(ch), format(ord(ch), "08b")) for ch in sample_word]
actual = {
"and": bin(a & b),
"or": bin(a | b),
"shl": bin(a << 1),
"shr": bin(b >> 1),
"parse_01000001": int("01000001", 2),
"parse_01100001": int("01100001", 2),
"word_bits": encoded,
}
expected = {
"and": "0b1",
"or": "0b111",
"shl": "0b110",
"shr": "0b10",
"parse_01000001": 65,
"parse_01100001": 97,
"word_bits": [
("B", 66, "01000010"),
("Y", 89, "01011001"),
("T", 84, "01010100"),
("E", 69, "01000101"),
("S", 83, "01010011"),
],
}
return {
"name": "bitwise_ops_and_binary_basics",
"summary": "验证按位与、按位或、左右移位、二进制字符串解析及多字符 8 位二进制编码",
"expected": expected,
"actual": actual,
"passed": actual == expected,
}
def main():
cases = [
case_voltage_noise_margin(),
case_byte_bulb_weighted_sum(),
case_byte_width_capacities(),
case_bitwise_ops_and_binary_basics(),
]
print(json.dumps({"version": VERSION, "cases": cases}, ensure_ascii=False, indent=2))
if __name__ == "__main__":
main()
本例需在 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。
入口文件:projects/computer-organization-why-computer-only-knows-0-and-1-lab/explore-why-computer-only-knows-0-and-1.py
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-why-computer-only-knows-0-and-1.py,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、CPython 3.12.14 离线计算机组成原理数值计算、逻辑门电路与体系结构仿真验证器(未在真实浏览器中执行来源交互脚本,也未连接外部网络)。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a2638.zip
课程关联工程包(登记关联22章) · 含当前章节或实例 · mbskycom202612a2616.zip