From 1e7acc018eb9cce19f2f99ee53d7a7e1d214514d Mon Sep 17 00:00:00 2001 From: dsh Date: Fri, 20 Nov 2026 16:35:02 +0800 Subject: [PATCH] hardware: the 74LS machine gets a period bill of materials, and a mechanics plan Two things: the TTL build is now the main target (Tang Nano looks too modern), and the packer learned the format that makes the TTL build simple. Format: r8forge_tape_pack --eprom writes one byte per instruction with the opcode in the low three bits. Then ip is the EPROM address and D2..D0 are the opcode -- no shift register, no 16 bit window, no decoder. It wastes five bits per instruction, and a 27C512 is exactly 64K, which is the historical instruction budget: the 8 bit calculator (51378) and the flagship (43676) both fit. Both formats live in the same .r8t trailer now (format byte 1 = packed, 2 = one byte per instruction), decode dispatches on it, and the test runs both and requires the same output and step count. Carrier: 74LS DIPs on a green board, 27C512 in a socket with its quartz window, 6116 SRAM, 74LS299 for the rotate, 6850 ACIA for serial, 74LS47 and four seven segment displays on the panel, 1 MHz can oscillator. About 31 to 40 chips, 400 to 700 CNY, a few weeks. The FPGA stays as a bench-only stepping stone. hardware/mechanics.md: where mechanics actually pays. The program must stay electronic (130 metres of paper tape for the calculator), and 512 relays for the data ring are out, but the pointer is worth it -- > and < are 71 to 73 percent of all steps, so a 64 position ratchet or uniselector makes the machine physically walk the ring. Also listed: an electromagnetic step counter (10 to 20 Hz, so only in single step mode, with a clutch for free running), a paper tape punch or plotter for output, a tape/card reader or plain keypad for input, and a hand crank as the clock. The rule that keeps this safe: mechanics only ever sit on sensor and actuator pins and never in the logic path, and the handshake just stops the sequencer while something moves -- since nothing in the ISA can observe time, slower is not different. A relay-only machine is priced honestly too: 60 to 120 relays, 10 to 50 Hz, hello in 10 to 40 seconds, the calculator in four hours, so that one is an exhibit. Also fixed: the synthesised macro fallback file name now carries a per-run token. When the system temp directory is not writable every r8asm falls back to the same working directory, so parallel ctest runs could write and import the same file -- which showed up as a random one-test failure in 0.39 seconds and nothing else. --- docs/hardware.md | 82 +++++++++++++++++------------ hardware/mechanics.md | 114 ++++++++++++++++++++++++++++++++++++++++ include/r8forge/tape.hh | 8 ++- src/r8asm.cc | 17 +++++- src/tape.cc | 35 ++++++++++-- tests/tape_pack.cc | 10 ++-- tests/tape_pack_test.cc | 19 +++++++ 7 files changed, 241 insertions(+), 44 deletions(-) create mode 100644 hardware/mechanics.md diff --git a/docs/hardware.md b/docs/hardware.md index 5dc25c8..8cadd14 100644 --- a/docs/hardware.md +++ b/docs/hardware.md @@ -132,56 +132,68 @@ rnz, 最低位 == 1: ip-- ; 深度=0 ## 5. 载体三档 -### A. FPGA(建议先做这一档) +**取向:越复古越好** —— 面板、器件、封装都要 1980 年代那味,所以 **B(74LS + EPROM + 数码管 +面板)是主目标**;A 只当"先把逻辑跑通"的跳板(Tang Nano 那种指甲盖板子长得太现代,真要 +做板就用 74LS 分立件 + 绿色 PCB + 大号丝印)。 + +### B'. 分立 74LS 机器(主目标,博物馆档) + +按 **8 位 / 64 格**、**一条指令一个字节**(低 3 位是操作码)算 —— 这个格式让线路简单一大截: +`ip` 直接当 EPROM 地址,取出的字节 `D2..D0` 就是操作码,**不需要移位寄存器、不需要 16 位 +窗口、不需要查表**。代价是每条指令浪费 5 bit,而 **27C512 正好 64K 字节**,正好等于项目 +历史上那条 `INS_SIZE = 65536` 的预算 —— 8 位计算器(51 378 条)和全家桶(43 676 条)都装得下 ✓。 + +| 部件 | 选型 | 片数 | 年代感 | +|---|---|---|---| +| 程序 ROM | **27C512**(64K×8,UV 擦除窗口)一条指令一字节 | 1 | 石英窗 + 紫外线擦除,最有味 | +| `ip` 计数器 | 74LS161 ×4(16 位,接 EPROM 的 A0..A15) | 4 | | +| 数据 RAM | **6116**(2K×8)—— 只用 64 字节 | 1 | 1980 年代的 SRAM | +| 数据寄存器 + 右旋 | **74LS299**(8 位通用移位寄存器,右旋 / 保持) | 1 | | +| `dp` 计数器 | 74LS161 ×2(6 位,回绕免费) | 2 | | +| 循环栈 | 74LS670(4×4 寄存器堆)×2 + 74LS161 当栈指针(8 深够用) | 3 | | +| 控制/微码 | 28C64 微码 ROM ×1 + 74LS161 微序列计数器 + 74LS138 译码 | 3 | 微码控制,8086 同代做法 | +| 串口 | **6850 ACIA** + 74LS4060(分频出 9600 baud) | 2 | 8080 时代的经典 ACIA | +| 面板 | 74LS47 ×4(七段译码)+ 4 位数码管 + LED(指令/`ip`/`dp`)+ 拨动开关 + 大红按钮 | 6~8 | 前面板就是"机器"的脸 | +| 胶合 | 74LS00/04/08/32/244/245 | 6~9 | | +| 时钟/复位 | 1 MHz 罐头晶振(或 74LS04 + 2 MHz 晶体 ÷2)+ 74LS14 + RC | 2 | | +| **合计** | | **~31~40 片**,¥400~700 | 5 V 2 A,绿色 PCB,全 DIP 插座 | + +速度:1~2 MHz、约 4 拍/指令 → 0.25~0.5 M 指令/秒;8 位计算器一次会话(166 k 步)0.4~0.7 s ✓。 +工作量:几周(面包板先通,再画 PCB;面板可以最后做)。 + +**面板**(这才是"8086 那种"的来源):4 位数码管显示当前格(十六进制),一排 8 个 LED 显示 +当前指令,一排 16 个 LED 显示 `ip` 低 16 位(或 4 位数码管显示 `ip`),一排 6 个 LED 显示 +`dp`,外加 **RUN / STEP / RESET** 三个带灯按钮和一个 8 位拨码当"输入字节"。外壳用 +铝型材/亚克力开窗都行,丝印上写满信号名。 + +### A. FPGA(跳板,不是终态) | 项 | 选择 | |---|---| -| 板子 | Tang Nano 9K(GW1NR-9):≈58 KB BSRAM、HDMI、板载 USB 串口,¥100 上下 | -| 能跑 | 8 位全家桶(16.4 KB)✓、8 位计算器(19.3 KB)✓;11/16/32 位纸带只当对照(37 KB / — / ✗) | -| 工具 | Gowin IDE 或开源的 yosys + nextpnr-gowin(后者更符合这个项目的脾气) | -| I/O | UART 115200 进/出,HDMI 画 64 格环 + `dp`/`ip`/当前格 | -| 工作量 | 300~500 行 Verilog + testbench,一个周末 | -| 速度 | 50 MHz / 4 拍 ≈ 12 M 指令/秒(比软件快一个量级,面板上能看到"瞬间算完") | +| 板子 | 任意(Tang Nano 9K 一类,¥100 上下)——只用来先把逻辑跑通,外形不讲究 | +| 能跑 | 8 位全家桶(16.4 KB)✓、8 位计算器(19.3 KB)✓ | +| 工具 | Gowin IDE 或 yosys + nextpnr-gowin | +| 速度 | 50 MHz / 2~4 拍 ≈ 10 M 指令/秒 | +| 价值 | `hardware/rot8.v` 已经在它上面验证过逻辑;烧上去只是看灯,最终机器是 B' | -### B. 分立 TTL("真机器",博物馆档) - -按 **8 位**算,片数比原来那版少一截(没有 11 位那种 8+3 的拼法,也没有位宽开关): - -| 部件 | 选型 | 片数 | -|---|---|---| -| 程序 ROM | 29C010 / 39SF010(128 KB flash,用低 3 bit;8 位全家桶只要 16.4 KB) | 1 | -| `ip` 计数器 | 74HC161 ×4(16 位够装 51 378 条;做 20 位就 ×5) | 4~5 | -| 数据 RAM | 6116(2K×8)—— 只用 64 字节,一片能用 32 台 | 1 | -| 数据寄存器/右旋 | 74HC299(8 位通用移位寄存器,右旋 + 保持一片搞定) | 1 | -| `dp` 计数器 | 74HC161 ×2(6 位,回绕免费,不用比较器) | 2 | -| 括号扫描 | 74HC191 ×2(上下计数)+ 比较器,或塞进 CPLD | 3~4 | -| 控制/微码 | GAL22V10 ×2 或 ATF1502 CPLD(一片搞定解码 + 微序列) | 1~3 | -| UART | 6850 ACIA 或 ATmega/CH340 桥 | 1~2 | -| 时钟/复位/面板 | 555 或晶振、74HC14、LED/数码管 | 5~8 | -| **合计** | | **~20~28 片**,¥250~500 | - -速度:1~2 MHz、4 拍/指令 → 0.25~0.5 M 指令/秒,8 位计算器一次会话(166 k 步)0.4~0.7 s ✓。 -工作量:几周(面包板先通,再画 PCB)。 - -### C. 机电(艺术品档) +### C. 机电(艺术品档,另外的野心) * **数据环做转鼓**:**8 条磁道 × 64 格**、一个读头 + 一个写头(写头在下游**一格**处)。 `>`/`<` 的"访问"就是等下一个格脉冲;`+`/`-` 在读到写头之间的那一格时间里改好写回, 所以每条指令 ≈ 一格时间。3000 rpm、64 格/圈 → 312 µs/格 → 166 k 步 ≈ **52 s**(环小了每圈格子少,鼓要转快点才追得上:10000 rpm → 16 s)。 代价是机加工(或找一个旧磁鼓/软盘改)。 * **程序做纸带**:3 bit 数据 + 1 孔同步,2.54 mm 孔距。`hello`(727 条)≈ 1.8 m ✓; - 计算器(73 624 条)≈ **187 m** ✗ —— 所以纸带只适合演示级小程序,大纸带还是进 EEPROM。 - 折中:**卡带**(FSK 1200~9600 baud)当"装载介质",23 KB 在 20~160 s 内装进 RAM。 + 计算器(51 378 条)≈ **130 m** ✗ —— 所以纸带只适合演示级小程序,大纸带还是进 EPROM。 * 也可以只做"面板 + 真 I/O"的半机电:MCU 跑 ISA,面板是真按键/LED/数码管。 -| | A FPGA | B TTL | C 机电 | +| | A FPGA 跳板 | **B' 分立 74LS** | C 机电 | |---|---|---|---| | 上手时间 | 一个周末 | 几周 | 几个月起 | -| 成本 | ¥100~300 | ¥250~500 | ¥1000+ + 机加工 | -| 速度 | 12 M 指令/秒 | 0.25~0.5 M | 40~45 k | +| 成本 | ¥100~300 | ¥400~700 | ¥1000+ + 机加工 | +| 速度 | ~10 M 指令/秒 | 0.25~0.5 M | 40~45 k | | 能跑 8 位全家桶/计算器 | ✓ | ✓ | ✓(慢,但够:一次会话 ~20 s) | -| 教学/观赏性 | 中 | **高** | **最高** | -| 可改 ISA/调试 | 改一行重烧 | 换 ROM | 换凸轮/继电器 | +| 复古程度 | ✗ | **✓✓**(DIP、UV EPROM、数码管、拨码) | ✓✓✓ | +| 可改 ISA/调试 | 改一行重烧 | 换 EPROM(擦 20 分钟,仪式感) | 换凸轮/继电器 | 建议:**A 先做**(一周内能跑真纸带),B 作为"正式机器"的目标(A 的 RTL 状态机原样翻成 微码表,接口和时序都不变),C 当展示件。8 位让 B 的片数掉到 20~28 片、C 的鼓只要 8 条磁道。 diff --git a/hardware/mechanics.md b/hardware/mechanics.md new file mode 100644 index 0000000..61b5bc7 --- /dev/null +++ b/hardware/mechanics.md @@ -0,0 +1,114 @@ +# 多加机械:哪些地方能真的动起来 + +软件已经证明"语义里看不见时间",硬件 RTL 也对上了,所以现在可以放心把**机械件当传感器/ +执行器**加进去 —— 它们慢、有惯量、会磨损,但**不参与逻辑**,所以不会改变机器语义。 +这条是这份文档能成立的前提。 + +## 1. 先划清界限:什么是机械做不到的 + +| 部分 | 能机械吗 | 为什么 | +|---|---|---| +| **程序纸带**(51 378 条) | ✗ | 2.54 mm 孔距下 130 m;0.2 mm 光学带也要 10 m,而且读头精度不现实。**必须电子**(27C512 EPROM) | +| 数据环(64 格 × 8 bit) | 勉强 | 512 个继电器/磁芯 ✗ 不可能手做;**留电子**(6116) | +| **`dp` 指针** | ✓✓ | 64 个位置、走一步退一步 —— 电话交换机的**步进器(uniselector)** 就是为这个生的 | +| **步数** | ✓ | 电磁计数器,每步"咔"一声 | +| **输入 / 输出** | ✓✓ | 纸带/卡片读入、纸带打孔/绘图仪输出、真键盘 | +| **时钟** | ✓ | 手摇曲柄、凸轮节拍器 | +| 逻辑(译码/微序列) | ✓ 但慢 | 继电器能跑,但 ~10 Hz:`hello`(417 步)42 s ✓,计算器(166 k 步)4.6 小时 ✗ | + +**结论**:机械应该加在 **指针、计数、I/O、时钟** 上,而不是程序存储和逻辑上。 + +## 2. 机械件的清单(按"每块钱换来的手感"排序) + +### 2.1 `dp` = 64 位棘轮步进器 ★★★ + +这是最值钱的一项:`>`/`<` 占全部步数的 **71~73%**(`tape_run --profile` 实测),也就是说这 +台机器**大部分时间在移动指针**。把它做成机械的,机器就真的在"咔哒咔哒"地走格子。 + +| 方案 | 件 | 价格 | 说明 | +|---|---|---|---| +| **电话步进器(uniselector)** | 二手拆机 | ¥50~200 | 64 个位置、棘轮 + 电磁驱动、自带**归位触点**(home)✓;步进器按脉冲前进,反向用一个"倒转"线圈或第二组电刷 | +| **步进电机 + 棘轮/日内瓦机构** | 28BYJ-48 + 3D 打印件 | ¥30 | 便宜、好控制(ULN2003 四相),反向容易;位置用**光电码盘**读回 | +| **旋转电磁铁 + 棘爪** | 旋转电磁铁 | ¥40 | 单步式,最像原件,但反向要另设计 | +| **机械计数轮 + 光电索引** | 旧电表/里程表 | ¥20 | 只能显示,不能驱动 ✗ 当"读数窗"用 | + +位置读回:步进器用**触点组**(64 位格雷码盘,6 位),或光电码盘 + 比较器;读回值就是 +`dp` 的 6 位地址,送进 6116 ✓。**home 触点**顺便做复位 ✓。 + +### 2.2 步数 = 电磁计数器 ★★ + +五位电磁计数器(¥20~40,每来一个脉冲加一,带手动清零)。**但它的动作频率上限只有 10~20 Hz** +✗:连续跑 166 k 步会变成一堆废铁。所以: + +* **单步/演示档**:每按一次 STEP,计数器"咔"一下,`ip`、`dp`、当前格全在面板上 ✓; +* **连续档**:计数器**脱开**(机械上用一个离合器或干脆断开驱动线圈),改看电子计数 ✓。 + +这个"两档"设计本身就是老机器的味道(计算器/电表都这么干)。 + +### 2.3 输出 = 纸带打孔机 / 绘图仪 ★★★ + +每条 `.` 打一行孔(8 位),结果就是一条可以贴墙的纸带 ✓。 + +| 方案 | 做法 | 难度 | +|---|---|---| +| **纸带打孔** | 8 个微型电磁铁 + 冲模 + 步进送带(1 mm 进给) | 中高(要机加工冲模) | +| **绘图仪** | 旧 X-Y 绘图仪(¥200~500)或两台步进电机 + 笔架,把数字"画"出来 | 中(代码量在 MCU 里) | +| **电传打字机** | ASR-33 之类(已很贵) | 低(接口就是 20 mA 电流环) | + +绘图仪那条最有性价比:字符集只要 0~9 和几个符号,画出来比打孔好看得多。 + +### 2.4 输入 = 卡片/纸带读入器 ★★ + +| 方案 | 做法 | 难度 | +|---|---|---| +| **80 列卡片** | 光电对管阵列(80 路太多,读 12 行就够)+ 步进送卡 | 中高 | +| **纸带读入** | 8 路红外对管 + 步进送带(和打孔机共用机构)✓ | 中 | +| **真键盘** | 1970 年代键盘 + 二极管编码矩阵 | 低(最省事) | +| **拨码开关** | 8 位拨码 + "送一个字符"按钮 | 极低(先做这个) | + +### 2.5 时钟 = 手摇 / 凸轮 ★★ + +* **手摇曲柄**:摇一圈 = 一个时钟脉冲 = 一条指令。面板上能直接"手算"一条 ROT-8 程序 ✓ + 最有仪式感,也最适合讲解。 +* **凸轮节拍器**:小电机 + 凸轮 + 微动开关,1~10 Hz 的"慢动作"档。 +* 连续档仍用晶振(1 MHz 罐头晶振)。 + +## 3. 接线原则(保证机械不破坏语义) + +1. 机械件只挂在**传感器的输出**和**执行器的输入**上:`dp` 步进器 ↔ 6 位地址、计数器 ↔ + 时钟脉冲、打孔机 ↔ `OUT` 脉冲、读入器 ↔ `INP` 数据。**逻辑永远在 74LS/EPROM 里跑**。 +2. 机械动作**慢于**逻辑时,用**握手**(一个"到位"触点):`>` 发出一路脉冲后等回位, + 状态机在这段时间**不前进** —— 因为语义看不见时间,这只是变慢,不是变语义 ✓。 +3. 掉电时机械件会停在某处:`dp` 步进器有 home 触点,复位时归零 ✓;数据在 6116 里会丢 ✗ —— + 加一个**手动"清内存"**按钮,和当年的小计算机一样。 +4. 全部 5 V,电磁铁/步进电机用 ULN2003 或继电器隔离,**别把反向电动势灌进 TTL** ✓。 + +## 4. 分阶段(机械版) + +| | 做什么 | 验收 | 工作量 | +|---|---|---|---| +| N0 | 6132 电子版跑通(现在的 RTL / 面包板) | 已有的 `rtl_sim` 那一套 | 已完成(仿真) | +| N1 | **机械 `dp`**:步进电机 + 码盘,替换 6 位计数器 | 单步跑 `hello`,指针一步一步走,OUT 不变 | 一个周末 | +| N2 | **单步档 + 电磁步数计数器** | 每步"咔"一声,计数与 `steps` 一致 | 一天 | +| N3 | **纸带打孔 / 绘图仪输出** | `hello` 打出可读的纸带/纸上字 | 一周 | +| N4 | **纸带/卡片输入** | 从纸带喂 `12 34 + .`,计算器照常算 | 一周 | +| N5 | **手摇曲柄时钟** | 摇一圈走一条指令 | 一天 | + +## 5. 继电器整机(另一条路,只适合展览) + +如果想让**逻辑本身**也是机械的:8 条指令的译码 + 64 格存储 + 循环栈,约 **60~120 个继电器** +(每个 8~12 mA,全部吸合约 1 A @ 5 V ✗ 要用 12 V 版本省电)。速度 10~50 Hz: + +* `hello`(417 步)≈ **10~40 s** ✓ 完全能看; +* `full_add`(95 步)≈ 2~10 s ✓; +* 计算器 ✗ 4 小时以上,只适合"让它跑着,明天来看"。 + +所以继电器机的定位是**展品**:面板上写着"这就是 ROT-8,它的逻辑在继电器里",跑 `hello` +和 `full_add`,旁边放一张说明为什么算不了计算器(程序太长、步数太多)。 + +## 6. 下一步 + +* **N1(机械 dp)** 是性价比最高的一步,而且它不依赖别的机械件 —— 先把步进电机 + 码盘 + 接上去,看指针走格子。 +* 需要的话我可以先写一份 **N1 的接线表**(步进电机四相 → ULN2003 → 74LS 计数器/比较器 → + 6116 地址;码盘 6 位 → 74LS244 缓冲 → 地址总线),以及 MCU/CPLD 中间那一小层的固件思路。 diff --git a/include/r8forge/tape.hh b/include/r8forge/tape.hh index 7d7756d..1f1fb0c 100644 --- a/include/r8forge/tape.hh +++ b/include/r8forge/tape.hh @@ -66,10 +66,16 @@ struct tape_image { std::vector code; size_t width = 0; size_t ring = 0; + unsigned format = 1; // 1: 3 bits per instruction, 2: one byte per instruction }; +// format 1 packs three bits per instruction (dense: a block RAM or a bit sliced +// ROM); format 2 stores one byte per instruction with the opcode in the low three +// bits, which is what a plain EPROM wants: ip is the address and D2..D0 are the +// opcode, so the fetch needs no shift register and no decoder. std::vector tape_image_encode(const std::vector &code, - size_t width, size_t ring); + size_t width, size_t ring, + unsigned format = 1); // True when the bytes end with the trailer (the loader uses this to tell an // image from a text tape without trusting the file name). bool tape_is_image(const std::vector &bytes); diff --git a/src/r8asm.cc b/src/r8asm.cc index 8d832fb..a479e47 100644 --- a/src/r8asm.cc +++ b/src/r8asm.cc @@ -1,4 +1,5 @@ #include "r8forge/r8asm.hh" +#include #include #include #include @@ -629,10 +630,22 @@ r8asm::ast_node r8asm::parse(const token_stream &stream) { if (!ec) dirs.push_back(work_dir); + // The file name carries a per-run token: when the system temp directory + // is not writable every process falls back to the *same* working + // directory, and two runs in parallel (ctest -j) would write and import + // the same file -- one of them then assembles the other's body, or + // imports a half written one. That is a race that shows up as a + // random, fast, one-test failure and nothing else. A clock tick is + // enough of a token here and needs no platform include. + const std::string unique = + name + "." + + std::to_string(std::chrono::steady_clock::now() + .time_since_epoch() + .count()) + + ".synth.r8asm"; std::string path; for (size_t d = 0; d < dirs.size(); d++) { - const std::filesystem::path candidate = - dirs[d] / (name + ".synth.r8asm"); + const std::filesystem::path candidate = dirs[d] / unique; std::ofstream file(candidate, std::ios::binary); if (!file) continue; diff --git a/src/tape.cc b/src/tape.cc index 80b58d9..03f8203 100644 --- a/src/tape.cc +++ b/src/tape.cc @@ -133,18 +133,30 @@ uint32_t get_u32(const std::vector &v, size_t at) { } constexpr size_t kTrailer = 16; +constexpr unsigned kFormatPacked = 1; // 3 bits per instruction +constexpr unsigned kFormatEprom = 2; // one byte per instruction, low 3 bits } // namespace std::vector tape_image_encode(const std::vector &code, - size_t width, size_t ring) { - std::vector image = tape_pack(code); + size_t width, size_t ring, + unsigned format) { + std::vector image; + if (format == kFormatEprom) { + // One byte per instruction: what a plain EPROM wants, because `ip` is then + // the address and D2..D0 are the opcode -- no shift register, no two byte + // window, no decoder. It wastes five bits per instruction, and a 27C512 + // happens to be exactly 64K, which is the historical instruction budget. + image.assign(code.begin(), code.end()); + } else { + image = tape_pack(code); + } const size_t at = image.size(); image.resize(at + kTrailer, 0); put_u32(image, at + 0, uint32_t(code.size())); image[at + 4] = uint8_t(width); image[at + 5] = uint8_t(ring > 255 ? 0 : ring); - image[at + 6] = 1; // format version + image[at + 6] = uint8_t(format == kFormatEprom ? kFormatEprom : kFormatPacked); std::memcpy(&image[at + 8], "R8T1", 4); const std::vector body(image.begin(), image.begin() + std::ptrdiff_t(at)); put_u32(image, at + 12, crc32(body)); @@ -169,6 +181,7 @@ bool tape_image_decode(const std::vector &bytes, tape_image &img, const size_t length = get_u32(bytes, body); const unsigned width = bytes[body + 4]; const unsigned ring = bytes[body + 5]; + const unsigned format = bytes[body + 6]; if (width < 1 || width > 32) { error = "image says " + std::to_string(width) + " bits per cell"; return false; @@ -181,6 +194,22 @@ bool tape_image_decode(const std::vector &bytes, tape_image &img, } img.width = width; img.ring = ring ? size_t(ring) : kDefaultMemoryCells; + img.format = format; + if (format == kFormatEprom) { + if (payload.size() < length) { + error = "eprom image holds " + std::to_string(payload.size()) + + " bytes, too few for " + std::to_string(length) + " instructions"; + return false; + } + img.code.assign(payload.begin(), payload.begin() + std::ptrdiff_t(length)); + for (size_t i = 0; i < img.code.size(); i++) + if (img.code[i] > uint8_t(opcode::inp)) { + error = "eprom instruction " + std::to_string(i) + " is 0x" + + std::to_string(int(img.code[i])) + ", not an opcode"; + return false; + } + return true; + } return tape_unpack(payload, length, img.code, error); } diff --git a/tests/tape_pack.cc b/tests/tape_pack.cc index 24d3cbe..ef0b525 100644 --- a/tests/tape_pack.cc +++ b/tests/tape_pack.cc @@ -1,6 +1,6 @@ // r8forge_tape_pack -- turn a .rot8 text tape into the image a machine reads. // -// r8forge_tape_pack -o out.r8t [--hex out.hex] [--verify] +// r8forge_tape_pack -o out.r8t [--hex out.hex] [--eprom] [--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 @@ -14,7 +14,7 @@ // `--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. // -// `--verify` reads the file back and compares it, so a wrong bit order or a bad +// `--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 // crc cannot slip through. #include "r8forge/geometry.hh" #include "r8forge/tape.hh" @@ -50,12 +50,15 @@ int main(int argc, char **argv) { std::string out_path; std::string hex_path; bool verify = false; + bool eprom = false; // one byte per instruction: the 27C512 format 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 == "--eprom") { + eprom = true; } else if (a == "--verify") { verify = true; } else { @@ -84,7 +87,8 @@ int main(int argc, char **argv) { } const std::vector image = - tape_image_encode(code, kDefaultCellSize, kDefaultMemoryCells); + tape_image_encode(code, kDefaultCellSize, kDefaultMemoryCells, + eprom ? 2u : 1u); const size_t body_bytes = image.size() - 16; { std::ofstream out(out_path, std::ios::binary); diff --git a/tests/tape_pack_test.cc b/tests/tape_pack_test.cc index 38e45e0..3f1d530 100644 --- a/tests/tape_pack_test.cc +++ b/tests/tape_pack_test.cc @@ -229,6 +229,25 @@ void check_broken_tapes() { else if (decoded.code != tape || decoded.width != 8 || decoded.ring != 64) fail("image round trip lost something"); + // The EPROM format: one byte per instruction, low three bits. It runs the + // same, it is bigger, and a byte with the high bits set must be refused. + const std::vector eprom = tape_image_encode(tape, 8, 64, 2); + if (eprom.size() != tape.size() + 16) + fail("eprom image is not one byte per instruction plus the trailer"); + tape_image nvram; + if (!tape_image_decode(eprom, nvram, error)) + fail("decode eprom image: " + error); + else if (nvram.code != tape || nvram.format != 2) + fail("eprom image round trip lost something"); + const machine_result from_packed = tape_run(tape, {}, opt); + const machine_result from_eprom = tape_run(nvram.code, {}, opt); + if (from_packed.out != from_eprom.out || from_packed.steps != from_eprom.steps) + fail("the two image formats do not run the same"); + std::vector bad_eprom = eprom; + bad_eprom[0] = 0x55; // high bits set: not an opcode + if (tape_image_decode(bad_eprom, nvram, error) || error.empty()) + fail("an eprom byte that is not an opcode should be refused"); + std::vector flipped = image; flipped[0] ^= 0x01; if (tape_image_decode(flipped, decoded, error) || error.empty())