diff --git a/hardware/bringup.md b/hardware/bringup.md new file mode 100644 index 0000000..1d838a2 --- /dev/null +++ b/hardware/bringup.md @@ -0,0 +1,141 @@ +# 面包板第一块:`ip` 计数 + 取指 + 数据灯 + +目标:**用最少的件看见机器在取指** —— 上电后,数据灯显示 ROM[0] 的字节,每来一个时钟地址 ++1、数据灯变成下一条指令。这一步通了,后面才谈数据通路。 + +预计一个下午。器件约 **¥120**。 + +## 1. 器件 + +| 件 | 数量 | 说明 | 约价 | +|---|---|---|---| +| **28C256**(32K×8 EEPROM,DIP-28) | 1 | **调试阶段用它**:5 V 就能烧,不用紫外线擦 ✗ 比 27C512 方便得多 | ¥12 | +| 74LS161 | 2 | `ip` 低 8 位 | ¥4 | +| 74LS14 | 1 | 复位去抖 + 慢速时钟(RC 振荡) | ¥2 | +| LED(红) | 8 | 数据灯 | ¥2 | +| 330 Ω | 8 | LED 限流 | ¥1 | +| 10 kΩ | 2 | 上拉 / RC | ¥0.5 | +| 100 nF | 2 | 去耦 / RC | ¥0.5 | +| 轻触按钮 | 2 | RESET、CLOCK(手动单步) | ¥2 | +| 面包板 830 孔 | 2 | 28 脚 DIP 要跨两块板(0.6 英寸宽) | ¥20 | +| 杜邦线 | 1 包 | | ¥8 | +| 5 V 2 A 适配器 | 1 | 或 USB 电源 | ¥25 | +| **合计** | | | **≈ ¥80~120** | + +> **为什么用 28C256 而不是 27C512**:两者都是 DIP-28、引脚兼容(A0..A14 / D0..D7 / ~CE / ~OE / +> ~WE),但 28C256 是 EEPROM,**CH341A 直接烧**;27C512 要紫外擦片器,改一次 20 分钟 —— 调试阶段 +> 会疯掉。等机器稳定了再换 27C512 走"仪式"。 + +## 2. 接线表 + +### 74LS161 #1(`ip[3:0]`) + +| 脚 | 名 | 接到 | +|---|---|---| +| 1 | `~MR` | RESET 网络(10 kΩ 上拉到 5 V + 按钮到 GND + 100 nF 到 GND) | +| 2 | `CP` | CLOCK(见 §3) | +| 3–6 | `A B C D` | **GND**(不用预置;悬空会被预置成随机值) | +| 7 | `CEP` | 5 V | +| 10 | `CET` | 5 V | +| 9 | `~PE` | 5 V | +| 11–14 | `QA QB QC QD` | `A0 A1 A2 A3`(到 ROM) | +| 15 | `TC` | 到 #2 的 `CET` | + +### 74LS161 #2(`ip[7:4]`) + +与 #1 相同,除了: + +| 脚 | 名 | 接到 | +|---|---|---| +| 10 | `CET` | #1 的 `TC`(15 脚) | +| 11–14 | `QA..QD` | `A4 A5 A6 A7` | + +### 28C256(ROM) + +| 脚 | 名 | 接到 | +|---|---|---| +| 1–10 | `A0..A9` | `A0..A7` 来自计数器,`A8 A9` → GND | +| 11–13 | `A10 A11 A12` | GND | +| 14 | `GND` | GND | +| 15–22 | `D0..D7` | 数据灯(见 §2.4) | +| 23–25 | `A13 A14 A15` | GND | +| 26 | `~CE` | GND(常选中) | +| 27 | `~OE` | GND | +| 28 | `VCC` | 5 V | +| 1 | `~WE` | **5 V**(只读!接低会改写片子) | + +> 引脚 1 在 28C256 上是 `~WE`(不是 A14),**别接错**:接低就成了写模式,上电瞬间会毁数据。 + +### 数据灯 + +`D0..D7` → 330 Ω → LED → GND,8 组。LED 长脚朝 ROM。 + +### 复位网络 + +``` +5 V ── 10 kΩ ──┬── ~MR(两片 161 的脚 1) + ├── 按钮 ── GND + └── 100 nF ── GND +``` + +## 3. 时钟:先手动,再自动 + +**调试时不要接 1 MHz** —— 灯会糊成一片,看不出东西。 + +1. **手动单步**:一个按钮从 5 V 到 `CP`,串 100 nF 到 GND(RC 去抖),每按一下走一条指令 ✓ +2. **自动慢速**:74LS14 的 RC 振荡,`R = 100 kΩ, C = 1 µF` → **约 5 Hz**,肉眼能数 ✓ +3. 想看"真实的机器"再换 1 MHz 罐头晶振(那时就该上示波器了)。 + +## 4. 烧片子 + +```sh +# 1) 汇编出一条纸带 +build/Release/r8forge examples/hello.r8asm > hello.rot8 + +# 2) 打成"一条指令一字节"的裸镜像(--raw 去掉 16 字节 trailer) +build/Release/r8forge_tape_pack hello.rot8 -o hello.bin --eprom --raw --verify +# hello.rot8: 727 instructions -> 727 bytes (100% of the text form), verified + +# 3) CH341A 烧进 28C256(minipro / 官方软件都行) +``` + +**注意**:镜像里第 n 个字节就是第 n 条指令,低 3 位是操作码、高 5 位是 0 —— 所以数据灯上 +只会看到 `00..07` 的字节,这是对的 ✓ + +## 5. 预期现象 + +| 操作 | 应该看到 | +|---|---| +| 上电 / 按 RESET | 数据灯 = ROM[0] 的字节(`hello` 是 `>` = 0x00 → **全灭** ✗ 不好看) | +| 每按一次 CLOCK | 地址 +1,数据灯变成下一条 | +| 按 RESET | 回到 ROM[0] | + +> **别用 `hello` 做第一次测试**:它开头是一串 `>`(0x00),灯全灭,看不出来 ✗。 +> 用一个手写的测试纸带,让前 16 条都是不同的操作码: + +``` +-+.>-+.<-->+<.[-] +``` + +(这样每个字节 0..7 都会出现,8 颗灯的模式一眼能分辨 ✓) + +## 6. 常见故障 + +| 现象 | 大概原因 | +|---|---| +| 数据灯全灭 | `~OE`/`~CE` 没接低;或 `~WE` 被接了低 | +| 数据灯全亮 | 数据线接反 / ROM 没插好 | +| 地址不跳 | `CP` 没接;或 `CEP`/`CET`/`~PE` 没拉高(74LS 输入悬空 = 高,但不可靠) | +| 地址乱跳 | `A B C D` 悬空,161 会预置成随机值 ✗ 必须接 GND | +| 按一下跳好几格 | 按钮没去抖(加 100 nF 或 74LS14) | +| 上电就乱 | 复位脉冲太短;把 100 nF 加大到 1 µF | + +## 7. 通了之后 + +1. 加 **6116 + 74LS299**:数据环 + 当前格寄存器(跑 `-` 再 `+`,看灯的位模式); +2. 加 **`dp` 计数器**:跑 `>` `<` 的纸带; +3. 加 **微码 + 拍计数器**:把前面几步串成一条指令流; +4. 最后才是栈、串口、面板。 + +**这一步的唯一目的:把"取指"这件事从抽象变具体。** 8 颗灯跟着纸带一格一格变,比看 500 行 +Verilog 有用得多。 diff --git a/hardware/bringup.png b/hardware/bringup.png new file mode 100644 index 0000000..55faaaa Binary files /dev/null and b/hardware/bringup.png differ diff --git a/hardware/bringup_sketch.py b/hardware/bringup_sketch.py new file mode 100644 index 0000000..b449eac --- /dev/null +++ b/hardware/bringup_sketch.py @@ -0,0 +1,104 @@ +"""Block diagram for stage 1: ip counter, ROM, data lamps. + +A breadboard top view looked like a drawer of parts; this says the same thing in +the form that is actually checkable: which block feeds which, and on which bus. +The pin table in bringup.md is the wiring; this is the shape of it. + + python hardware/bringup_sketch.py hardware/bringup.png +""" +import sys + +from PIL import Image, ImageDraw, ImageFont + +S = 7 +W, H = 200 * S, 96 * S + +BG = (247, 245, 239) +BOX = (255, 254, 250) +EDGE = (52, 48, 44) +TEXT = (40, 36, 32) +DIM = (120, 112, 100) +BUS = (44, 82, 150) +CTRL = (172, 48, 40) +DATA = (36, 116, 74) + + +def font(pt): + for path in (r"C:\Windows\Fonts\consola.ttf", r"C:\Windows\Fonts\lucon.ttf"): + try: + return ImageFont.truetype(path, int(pt * S)) + except OSError: + continue + return ImageFont.load_default() + + +def p(v): + return int(round(v * S)) + + +def box(d, x, y, w, h, title, sub, fill=BOX): + d.rounded_rectangle([p(x), p(y), p(x + w), p(y + h)], radius=p(1.4), + fill=fill, outline=EDGE, width=p(0.35)) + d.text((p(x + 3.0), p(y + 3.2)), title, font=font(2.0), fill=TEXT) + if sub: + for i, line in enumerate(sub.split("|")): + d.text((p(x + 3.0), p(y + 7.6 + i * 3.0)), line, font=font(1.35), + fill=DIM) + + +def bus(d, pts, label, colour, at=0.5, above=True): + d.line([(p(a), p(b)) for a, b in pts], fill=colour, width=p(0.8), joint="curve") + # arrow head on the last segment + (x0, y0), (x1, y1) = pts[-2], pts[-1] + d.polygon([(p(x1), p(y1)), (p(x1 - 2.0), p(y1 - 1.4)), + (p(x1 - 2.0), p(y1 + 1.4))] if abs(x1 - x0) > abs(y1 - y0) else + [(p(x1), p(y1)), (p(x1 - 1.4), p(y1 - 2.0)), (p(x1 + 1.4), p(y1 - 2.0))], + fill=colour) + # label along the first segment + seg = pts[0], pts[1] + lx = seg[0][0] + (seg[1][0] - seg[0][0]) * at + ly = seg[0][1] + (seg[1][1] - seg[0][1]) * at + tw = d.textlength(label, font=font(1.4)) / S + d.text((p(lx - tw / 2), p(ly - (3.4 if above else -1.4))), label, + font=font(1.4), fill=colour) + + +def main(): + out = sys.argv[1] if len(sys.argv) > 1 else "bringup.png" + img = Image.new("RGB", (W, H), BG) + d = ImageDraw.Draw(img) + + d.text((p(6), p(5)), "STAGE 1 · ip COUNTER → ROM → DATA LAMPS", font=font(2.6), + fill=TEXT) + d.text((p(6), p(10.6)), + "the whole point is to watch the address walk and the fetched byte change", + font=font(1.5), fill=DIM) + + # blocks + box(d, 22, 30, 34, 22, "74LS161", "ip[3:0]|TC → next CET", BOX) + box(d, 22, 62, 34, 22, "74LS161", "ip[7:4]", BOX) + box(d, 82, 38, 34, 30, "28C256", "A0..A7 in|D0..D7 out|low 3 bits = opcode", + (250, 250, 244)) + box(d, 144, 38, 34, 30, "8 LEDS", "330 Ω each|D0 left, D7 right", (250, 250, 244)) + + # buses + bus(d, [(56, 41), (70, 41), (70, 46), (82, 46)], "A0..A3", BUS, 0.45) + bus(d, [(56, 73), (70, 73), (70, 52), (82, 52)], "A4..A7", BUS, 0.45) + bus(d, [(39, 52), (39, 62)], "TC", CTRL, 0.5, above=False) + bus(d, [(116, 50), (144, 50)], "D0..D7", DATA, 0.5) + bus(d, [(6, 39), (22, 39)], "CLK", CTRL, 0.4) + bus(d, [(6, 73), (22, 73)], "RST", CTRL, 0.4) + + # power and notes + d.text((p(6), p(86)), "5 V / GND to every part · ~CE and ~OE tied low (always " + "selected) · ~WE tied HIGH (read only!)", font=font(1.4), fill=DIM) + d.text((p(6), p(90)), + "clock: a button for single step first, then 74LS14 RC at ~5 Hz, 1 MHz only " + "when it all works", font=font(1.4), fill=DIM) + + img.save(out) + print("wrote", out) + + +if __name__ == "__main__": + main() diff --git a/tests/tape_pack.cc b/tests/tape_pack.cc index ef0b525..98797d4 100644 --- a/tests/tape_pack.cc +++ b/tests/tape_pack.cc @@ -1,20 +1,22 @@ // r8forge_tape_pack -- turn a .rot8 text tape into the image a machine reads. -// -// r8forge_tape_pack -o out.r8t [--hex out.hex] [--eprom] [--verify] -// +// r8forge_tape_pack -o out.r8t [--hex out.hex] [--eprom] [--raw] [--verify] // The text tape is one character per instruction. The hardware stores three bits // per instruction (eight opcodes), so this packs them and appends a small // trailer; the format is defined once, in `r8forge/tape.hh`, so the runner and // the tests read the file exactly the way this writes it. -// // The trailer goes at the *end* on purpose: if the file is burnt into a ROM, // address 0 is instruction 0, so the program is executed in place and the // trailer sits past the halt point, never fetched. -// // `--hex` writes the body alone, one byte per line, for Verilog's $readmemh; the // comment on the first line carries the length and the geometry. +// `--eprom` writes one byte per instruction instead of three bits. That is the +// format a bare EPROM wants: `ip` is the address and D2..D0 are the opcode, so +// the fetch needs no shift register and no decoder, at the cost of five wasted +// bits per instruction -- and a 27C512 is exactly 64K, the historical budget. // -// `--eprom` writes one byte per instruction instead of three bits. That is the`n// format a bare EPROM wants: `ip` is the address and D2..D0 are the opcode, so`n// the fetch needs no shift register and no decoder, at the cost of five wasted`n// bits per instruction -- and a 27C512 is exactly 64K, the historical budget.`n//`n// `--verify` reads the file back and compares it, so a wrong bit order or a bad +// `--raw` drops the 16 byte trailer, which is what a ROM burner wants. +// +// `--verify` reads the file back and compares it, so a wrong bit order or a bad // crc cannot slip through. #include "r8forge/geometry.hh" #include "r8forge/tape.hh" @@ -51,12 +53,15 @@ int main(int argc, char **argv) { std::string hex_path; bool verify = false; bool eprom = false; // one byte per instruction: the 27C512 format + bool payload_only = false; // write the payload only: what a ROM burner wants for (int i = 2; i < argc; i++) { const std::string a = argv[i]; if ((a == "-o" || a == "--output") && i + 1 < argc) { out_path = argv[++i]; } else if (a == "--hex" && i + 1 < argc) { hex_path = argv[++i]; + } else if (a == "--raw") { + payload_only = true; } else if (a == "--eprom") { eprom = true; } else if (a == "--verify") { @@ -86,10 +91,15 @@ int main(int argc, char **argv) { return 1; } - const std::vector image = + std::vector image = tape_image_encode(code, kDefaultCellSize, kDefaultMemoryCells, eprom ? 2u : 1u); const size_t body_bytes = image.size() - 16; + // --raw drops the 16 byte trailer: a ROM burner takes the payload and nothing + // else, and the trailer would otherwise sit in the address space after the + // halt, which is harmless but ugly when reading the chip back. + if (payload_only) + image.resize(body_bytes); { std::ofstream out(out_path, std::ios::binary); if (!out) { @@ -130,19 +140,29 @@ int main(int argc, char **argv) { std::cerr << "verify: " << out_path << " does not read back byte for byte\n"; return 1; } - tape_image decoded; - if (!tape_image_decode(back, decoded, error)) { - std::cerr << "verify: " << error << "\n"; - return 1; - } - if (decoded.code != code) { - std::cerr << "verify: the image decodes to a different tape\n"; - return 1; - } - if (decoded.width != kDefaultCellSize || - decoded.ring != kDefaultMemoryCells) { - std::cerr << "verify: the trailer records the wrong geometry\n"; - return 1; + if (payload_only) { + // no trailer to decode in a raw payload: the byte comparison above is the + // whole check, and the length is checked against the body size + if (back.size() != body_bytes) { + std::cerr << "verify: raw payload is " << back.size() + << " bytes, expected " << body_bytes << "\n"; + return 1; + } + } else { + tape_image decoded; + if (!tape_image_decode(back, decoded, error)) { + std::cerr << "verify: " << error << "\n"; + return 1; + } + if (decoded.code != code) { + std::cerr << "verify: the image decodes to a different tape\n"; + return 1; + } + if (decoded.width != kDefaultCellSize || + decoded.ring != kDefaultMemoryCells) { + std::cerr << "verify: the trailer records the wrong geometry\n"; + return 1; + } } }