rtl: 修 in_ready 单步漏洞、加 EPROM 取指模式、量出扫描占 76% 拍 #1
No files matched your search
@@ -42,6 +42,8 @@ RTL 已验证、面板已定稿、采购清单已下单(≈¥280)、面包板未
|
||||
| 面包板第一块 + 宿舍版全机 | [`docs/bringup.md`](docs/bringup.md) |
|
||||
| 成本 / 采购 / 链接 | [`docs/bom.md`](docs/bom.md)、[`docs/buy.md`](docs/buy.md)、[`docs/order.md`](docs/order.md)、[`docs/links.txt`](docs/links.txt) |
|
||||
| RTL 说明与对拍方法 | [`rtl/README.md`](rtl/README.md) |
|
||||
| 括号跳过吃掉多少拍(实测) | [`docs/bracket-skip.md`](docs/bracket-skip.md) |
|
||||
| 只有 iverilog 就能跑的探针 | `./tests/rtl_probe.sh in-ready` / `states <image.hex> <LEN> [input] [eprom]` |
|
||||
|
||||
## 约定
|
||||
|
||||
|
||||
@@ -12,6 +12,23 @@
|
||||
* **不要**因为"更完整"而加回已经删掉的东西(数字显示、步数计数器、点阵、机械指针),
|
||||
理由都在 `docs/panel.md`。
|
||||
|
||||
## 上一轮做完的(2026-11-26)
|
||||
|
||||
* **`in_ready` 修好了**:原来只在 `state == S_FETCH && op_now == OP_INP` 上判断,单步模式
|
||||
(`run=0`)停着时照样说"给我一个字节",生产者交出去的字节会丢、机器从此卡在 `,`。
|
||||
现在加上 `(run || step) && !halted`。回归:`tests/tb_in_ready.v`(自带纸带,不需要镜像)。
|
||||
* **栈满也停机**:`[` 压栈到顶从"静默回绕"改成和空栈弹出一样停机(`STACKW=4` = 16 层)。
|
||||
* **两种程序存储器都在 RTL 里了**:`rot8.v` 新增 `EPROM` 参数(1 = 一字节一指令,就是
|
||||
`docs/ttl-schematic.md` 焊的 27C512 方案),`tb_rot8.v` 用 `-DEPROM_MODE` 打开。
|
||||
三张纸带在两种格式下**步数、拍数、OUT 流全等**(hello 417 / calc 166 741 / test 100 142)。
|
||||
* **拍数第一次量出来了**(`tests/tb_state_mix.v` + `tests/rtl_probe.sh states`):
|
||||
`calc` 698 075 拍里 **531 332 拍是括号扫描(76%)**;`hello` 27%、`asm/test` 53%。
|
||||
分析、代价和三条可选方案写在 **`docs/bracket-skip.md`**。
|
||||
* **实测栈深**:`calc` 7 层、`asm/test` 4 层 → `docs/ttl-schematic.md` 里"两片 74LS670"
|
||||
只有 4 层会溢出,已按 §7 改成用 6116 高地址区当栈;顺手改了片数(31 片 + 晶振)。
|
||||
* **微码字宽定了**:字段数下来 18 根,一片 28C64(8 位)装不下 → **16 位、两片 28C64**,
|
||||
写 `fields.md` 之前先按这条来(见 `ttl-schematic.md` §8)。
|
||||
|
||||
## 上一轮做完的(2026-11-25)
|
||||
|
||||
* **仓库拆分**:物理机从 R8Forge 独立成这个仓库;R8Forge 那边 10/10 测试仍全绿(`57014cc`)。
|
||||
@@ -24,6 +41,13 @@
|
||||
|
||||
目标:把 `microcode/README.md` 里那三张表写出来,并且**在电脑上验证**,而不是等焊完发现错。
|
||||
|
||||
先决条件(2026-11-26 已定,别再改回去):
|
||||
|
||||
* **微码宽 16 位、两片 28C64**(字段数 18 > 8,见 `ttl-schematic.md` §8);
|
||||
* **取指口径是一字节一指令**(27C512,`ip` 直接当地址);RTL 的 `EPROM=1` 就是这个,
|
||||
写微码时照它的拍序走,不要照打包格式那套"两次字节读"(见 `rtl/README.md`「两种程序存储器」);
|
||||
* 支架栈用 6116 高地址区,深度不必按 16 算(实测最深 7 层),但接口上仍留 `sp` 归零/满停机。
|
||||
|
||||
1. `microcode/fields.md` —— 控制字段定义(每一位控制哪根线)
|
||||
2. `microcode/isa.md` —— 8 条指令 × 每拍的字段(插线时打印贴墙上)
|
||||
3. `microcode/gen.py` —— 由表生成 28C64/28C256 镜像
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
# 括号跳过到底吃掉多少拍
|
||||
|
||||
`rtl/README.md` 证明的是**语义**:RTL 的 OUT 流和步数与 `r8forge_tape_run --loop-stack --retest`
|
||||
逐字节相同。这份文档量的是**拍**——语义一样,硬件预算可以差好几倍。
|
||||
|
||||
工具:`tests/tb_state_mix.v`(`./tests/rtl_probe.sh states <image.hex> <LEN> [input] [eprom]`),
|
||||
它数三个状态各占多少周期:`S_FETCH`(取指+执行)、`S_SCAN`(`[` 最低位为 0 时的前向跳过)、
|
||||
`S_HALT`。8 位 / 64 格,Icarus Verilog 13,2026-11-26 实测。
|
||||
|
||||
## 数字
|
||||
|
||||
| 纸带 | 步数 | 总拍数 | S_SCAN | 扫描占比 | 拍/步 | 最大栈深 | 最大扫描深度 |
|
||||
|---|---|---|---|---|---|---|---|
|
||||
| `hello` | 417 | 579 | 160 | 27% | 1.38 | 0 | 1 |
|
||||
| `asm/test.r8asm` | 100 142 | 217 276 | 117 133 | 53% | 2.16 | 4 | 3 |
|
||||
| `calc`(`12 34 + .`) | 166 741 | 698 075 | **531 332** | **76%** | 4.18 | 7 | 7 |
|
||||
|
||||
`hello` 不走循环栈(栈深 0,只有 1 层跳过),所以接近 1 拍/步;`calc` 是真实字节级代码
|
||||
(`clb_add`/`clb_mul`/`clb_digits` 里成片的 `[-]`、`set_0`、条件块),几乎每条指令都在跳过
|
||||
某个"这一轮不执行"的括号块,于是 **3/4 的周期是在扫纸带,不是在算**。
|
||||
|
||||
## 为什么会这样
|
||||
|
||||
循环栈只优化了 `]` 那一侧(弹栈 O(1) 跳回 `[`)。`[` 那一侧分两路:
|
||||
|
||||
* 最低位 = 1 → 进栈,1 拍,和解释器一样;
|
||||
* 最低位 = 0 → **前向扫描到配对的 `]`**,每拍前进一条指令、不计步数。
|
||||
|
||||
字节级宏里第二种极多(每层条件都是一次跳过),跳过的长度又往往是几十上百条 —— 这就是
|
||||
76% 的来源。需求文档 §4 说"循环栈让 `]` 从 O(n) 变 O(1),所以主循环不吃亏"是对的,
|
||||
但**没算 `[` 的跳过**;1 MHz 下 `calc` 的 0.53 s 全花在这上面。
|
||||
|
||||
## 三条路(带代价)
|
||||
|
||||
1. **什么都不做**。1 MHz 下 `calc` 一次会话 ≈ 0.7 s(0.53 s 扫描 + 0.17 s 执行),
|
||||
`asm/test` ≈ 1.1 s(微码按 3 拍/指令还要再乘 ~1.5)。博物馆档的机器,人按一下按钮等
|
||||
一秒完全合理。**建议先走这条**:微码还没写,扫描这条路径在微码里就是"多几个拍",
|
||||
等真机手感不行再改。
|
||||
2. **配对表**:`tape_pack` 顺带生成"每个括号的配对地址"表,硬件在 `[`(最低位 0)时
|
||||
直接装 `ip`,一次访问,O(1)。代价是地址怎么找:
|
||||
* 按 `ip` 索引要 16 位/条的二表 → `calc` 51 378 条 × 2 B ≈ 100 KB,27C512 装不下;
|
||||
* 按"第几个括号"索引(硬件维护一个括号计数器,压栈时连计数器一起压):
|
||||
`calc` 有 3006 对括号,每条 2 B 地址 + 2 B 序号 ≈ 12 KB,和打包程序(19.3 KB)
|
||||
一起塞进 27C512 刚好;若用一字节一指令的镜像(51 KB)就得换 27C010(128 KB)。
|
||||
3. **把跳过压到每拍多条**:一字节一指令的镜像一次只有 3 位有用,剩下的 5 位可以放
|
||||
"这条到配对 `]` 的距离"(最大 31 条)——短跳过零额外周期,长跳过退化成扫描。
|
||||
不额外占 ROM,但只对 ≤31 条的跳过有效(实测多数正是这种)。
|
||||
|
||||
**如果要上 2 或 3,`tape_pack` 得先有对应格式**,那是 R8Forge 那边的活(`SPEC.md` 管格式),
|
||||
不是这台机器的 RTL 能单方面决定的。所以顺序仍然是:微码 → 真机 → 手感不行再考虑。
|
||||
|
||||
## 顺带量到的两条
|
||||
|
||||
* **栈深**:`calc` 峰值 7 层,`asm/test` 4 层,`hello` 0 层。`STACKW=4`(16 层)有两倍余量;
|
||||
`docs/ttl-schematic.md` 原来用两片 74LS670(只有 4 层)会溢出,已改为"栈放进 6116 高地址区"。
|
||||
* **`in_ready`**:`tests/tb_in_ready.v` 是这轮加的定向测试,抓到一个只在单步模式下发作的
|
||||
契约漏洞(`run=0` 时 `in_ready` 仍然拉高,生产者交出的字节会丢)。修在 `rtl/rot8.v`。
|
||||
+22
-4
@@ -36,7 +36,7 @@
|
||||
| U9 | **74LS86** | 1 | `-`(flb):只对 bit0 取反 |
|
||||
| U10–U11 | **74LS161 ×2** | 2 | `DP` 计数器(6 位;回绕免费) |
|
||||
| U12 | **74LS244** | 1 | 机械档时把编码器送进 `DP` 总线(跳线选源) |
|
||||
| U13–U14 | **74LS670 ×2** | 2 | 循环栈:16 深 × 8 位(两个 4×4 拼成 8 位,见 §7) |
|
||||
| U13–U14 | ~~74LS670 ×2~~ | 0 | 循环栈的早期方案,**已取消**:670 是 4 字 × 4 位,两片并联只有 **4 深 × 8 位**,而实测 `calc` 需要 7 层(`tests/tb_state_mix.v`)→ 按 §7 改用 6116 的高地址区当栈,这两片不装 |
|
||||
| U15 | **74LS161** | 1 | 栈指针(4 位,够 16 深) |
|
||||
| U16 | **28C64**(8K×8 EEPROM) | 1 | 微码:`A[?]` = 指令线 + 拍号,`D[7:0]` = 控制字段 |
|
||||
| U17 | **74LS161** | 1 | 微序列计数器(每拍 +1,指令结束复位) |
|
||||
@@ -51,7 +51,8 @@
|
||||
| U29 | **74LS14** | 1 | 时钟整形 + 按钮去抖 |
|
||||
| U30 | **1 MHz 罐头晶振** | 1 | 时钟源 |
|
||||
|
||||
总计 **约 30 片 + 1 片晶振**。
|
||||
总计 **31 片 + 1 片晶振**(逐项加:U1 一、U2–U5 四、U10–U11 二、U17/U15 各一、U22–U25 四、
|
||||
U27 胶合 3~4;不含已取消的 U13–U14)。`README.md` 里写的 34 是早先含 670 一对的数,以本表为准。
|
||||
|
||||
## 3. 时钟、复位、单步
|
||||
|
||||
@@ -71,8 +72,11 @@
|
||||
* `IP` 计数器:4 片 74LS161 级联成 16 位,`~PE` 接高(不加预置),`CET`/`CEP` 串联进位,
|
||||
每拍 `IP_CE` 有效时 +1。
|
||||
* `A[15:0]` 直接接 27C512 的地址脚;`D2..D0` → 74LS138 → 8 条指令线。
|
||||
* **一条指令一字节**:不需要移位寄存器、不需要 16 位窗口 —— 这正是 `tape_pack --eprom` 那个
|
||||
格式存在的理由。
|
||||
* **一条指令一字节**:不需要移位寄存器、不需要 16 位窗口 —— 这正是 `tape_pack --eprom` 那个格式存在的理由。
|
||||
这一侧现在**在 RTL 里有模型了**:`rot8.v` 的 `EPROM=1`(`tb_rot8.v` 用 `iverilog -DEPROM_MODE` 打开)
|
||||
把 `prog_data0[2:0]` 当操作码、`ip` 直接当地址,和打包格式跑同一批纸带、要求 OUT 流与步数逐字节相同
|
||||
(hello 417 / calc 166 741 / asm/test 100 142,两种格式全等,2026-11-26 实测)。
|
||||
换句话说:`rtl_sim.cmake` 那套对拍用的是**打包**镜像,和这块板子焊的格式不是同一份 —— 别再把两者混着说。
|
||||
* `IP` 的**终点比较**:`IP == LEN` 停机。LEN 用 4 片 74LS688(比较器)或者更省:让 ROM 的
|
||||
第一个**空操作码**当"停机"(把操作码 3 bit 全 0 定义成 HALT 会改语义 ✗)→ 改用**运行长度
|
||||
寄存器 + 比较器**;或者干脆用一个 16 位拨码开关当 LEN,4 片 74LS688 比较。
|
||||
@@ -127,6 +131,20 @@
|
||||
* 好处:改控制只换一片 28C64(而且它是 **EEPROM**,不用紫外线 ✗ 少点仪式感,但调试友好)。
|
||||
* 想更复古就把 28C64 换成 27C64(UV)—— 那就要擦片器,改一次 20 分钟。
|
||||
|
||||
**先数一遍字段再画板子(2026-11-26 的账)**:`microcode/README.md` 列的 13 个
|
||||
(`RAM_OE RAM_WE L299_LOAD ROR FLB PUSH POP IP_CE DP_UP DP_DN OUT_CLK AWAIT_RDRF AWAIT_TDRE`)
|
||||
再加上绕不开的 `L299_OE`(三态)、`IP_LOAD`(`]` 要把栈顶装回 `IP`)、`SCAN_EN`(进前向扫描)、
|
||||
`SEQ_RST`(一条指令走完复位拍计数器)、`HALT` —— 一共 **18 根**。28C64 是 8 位输出,
|
||||
**一片装不下**,写 `fields.md` 之前必须先二选一:
|
||||
|
||||
1. **两片 28C64 并联成 16 位**(+1 片,地址线共用,`~CE` 分开或共用都行)—— 最省事,
|
||||
微码表照 16 位写,`D15..D0` 就是 18 根里的 16 根,剩下两根用 74LS138 的空闲输出编。
|
||||
2. **把字段压到 8 位**:`DP_UP/DP_DN` 合成一根 + 一根方向;`AWAIT_RDRF/AWAIT_TDRE` 合成
|
||||
"等 ACIA 的哪一位"2 位;`ROR/FLB/L299_LOAD` 合成 299 的 `S1S0` 两码 + 一个"写";
|
||||
压完约 9~10 根 —— 还是超,**要么选 1,要么接受 10 位(两片并联同样跑不掉)**。
|
||||
|
||||
结论:**微码宽 16 位,两片 28C64**。这条定下来之前不要写 `isa.md`,否则表要返工。
|
||||
|
||||
## 9. 串口与面板
|
||||
|
||||
### 9.1 6850 ACIA 是什么、为什么有它
|
||||
|
||||
@@ -56,6 +56,19 @@ vvp rot8_tb.vvp +HEX=calc8.hex +LEN=51378 +INFILE=calc_in.txt
|
||||
build8/Release/r8forge_tape_run calc8.r8t --loop-stack --retest -i '12 34 + .'
|
||||
```
|
||||
|
||||
## 两条不依赖 R8Forge 的探针
|
||||
|
||||
`tests/rtl_probe.sh` 只需要 iverilog,喂现成的镜像即可(镜像怎么来见上一节):
|
||||
|
||||
```sh
|
||||
./tests/rtl_probe.sh in-ready # in_ready 契约,自带纸带
|
||||
./tests/rtl_probe.sh states calc.hex 51378 calc_in.txt # 打包格式
|
||||
./tests/rtl_probe.sh states calc_e.hex 51378 calc_in.txt eprom # 一字节一指令的镜像
|
||||
```
|
||||
|
||||
`states` 打印步数、总拍数、`S_FETCH`/`S_SCAN` 占比、拍/步、最大栈深。实测数字与结论见
|
||||
[`docs/bracket-skip.md`](../docs/bracket-skip.md):`calc` 有 **76% 的周期花在括号扫描上**。
|
||||
|
||||
## 两个坑(都踩过)
|
||||
|
||||
1. **`+HEX=` / `+INFILE=` 的路径必须是纯 ASCII**。Icarus 把 plusarg 原样传下去,路径里有
|
||||
@@ -64,6 +77,10 @@ build8/Release/r8forge_tape_run calc8.r8t --loop-stack --retest -i '12 34 + .'
|
||||
路径(`tests/rtl_sim.cmake` 就是这么做的),要么把文件拷到 ASCII 路径。
|
||||
2. **`in_ready` 不能加 `!in_valid`** 的限定。第一版写成 `in_ready = … && !in_valid`,
|
||||
于是生产者永远看不到"接受"的边沿、握手死锁 —— 只有 `hello` 这种不读输入的纸带能跑。
|
||||
3. **`in_ready` 必须算上 `run`/`step` 和 `halted`**(2026-11-26 补)。它原本只有
|
||||
`state == S_FETCH && op_now == OP_INP`:单步模式停着(`run=0`)时它照样拉高,生产者按
|
||||
契约交出一个字节、机器却没在跑,那个字节就没了,机器从此卡在 `,` 上等下一个
|
||||
(`steps` 一直是 0)。复现/回归:`tests/tb_in_ready.v`。
|
||||
|
||||
## RTL 里几个"故意这么写"的地方
|
||||
|
||||
@@ -78,6 +95,21 @@ build8/Release/r8forge_tape_run calc8.r8t --loop-stack --retest -i '12 34 + .'
|
||||
(`in_valid` + `in_data=0`)。
|
||||
5. **停机**:`ip >= prog_len` → `halted`,并且这一拍不再执行(第一版漏了判断,会从纸带外
|
||||
多取一条、步数多 1)。
|
||||
6. **栈满/空栈都停机**:`]` 弹空栈早就定义成停机;`[` 压栈时 `sp` 到顶(`STACKW=4` 即 16 层)
|
||||
原来是**静默回绕**,后面某次跳转会跳到别处而看起来像"程序坏了"。现在同样停机
|
||||
(`tests/tb_state_mix.v` 打印最大栈深,实测 `calc` 7 层、`asm/test` 4 层,余量足够)。
|
||||
|
||||
## 两种程序存储器(`EPROM` 参数)
|
||||
|
||||
`rot8.v` 的 `EPROM` 参数选程序存储器的形态,语义完全相同、OUT 流与步数必须逐字节一致:
|
||||
|
||||
| `EPROM` | 镜像 | 取指 | 板子上对应 |
|
||||
|---|---|---|---|
|
||||
| 0(默认) | `tape_pack --hex`(3 bit/条) | `ip*3` 定位,读 16 位窗口(`prog_addr0/1` 两次读) | 16 位宽 ROM / 两片 ROM / 移位寄存器 |
|
||||
| 1 | `tape_pack --eprom --hex`(1 字节/条) | `prog_addr0 = ip`,`prog_data0[2:0]` 即操作码 | **一片 27C512**,就是 `docs/ttl-schematic.md` 的方案 |
|
||||
|
||||
`tb_rot8.v` 用 `iverilog -DEPROM_MODE` 打开 EPROM 那一侧。2026-11-26 实测两种格式在
|
||||
hello(417 步)/ calc(166 741 步)/ asm/test(100 142 步)上**步数、拍数、OUT 流全部相同**。
|
||||
|
||||
## 还没做的
|
||||
|
||||
|
||||
+30
-9
@@ -9,6 +9,11 @@
|
||||
// bit instruction can straddle a byte boundary. A real board does this with
|
||||
// a 16 bit wide ROM, two ROMs, or a shift register; the interface is written
|
||||
// as two reads so all three are countable.
|
||||
// * EPROM=1 selects the *other* legal program memory: one instruction per byte,
|
||||
// opcode in D2..D0, `ip` straight into the address pins (what a single 27C512
|
||||
// does, see docs/ttl-schematic.md §4). Eight times the ROM, but no shift
|
||||
// register, no alignment, one read per fetch. Both formats run the same
|
||||
// tapes and must produce the same OUT stream and step count.
|
||||
// * the data ring is 64 cells, so `dp` is a 6 bit counter whose wrap is free.
|
||||
// * `,` blocks: `in_ready` goes high and the machine does not advance until
|
||||
// `in_valid` is seen. Reading zero has to be an explicit event (`in_valid`
|
||||
@@ -32,7 +37,8 @@ module rot8 #(
|
||||
parameter integer WIDTH = 8, // bits per data cell
|
||||
parameter integer RING = 64, // data cells (power of two: free wrap)
|
||||
parameter integer IPW = 16, // instruction pointer width
|
||||
parameter integer STACKW = 4 // loop stack depth: 2^STACKW entries
|
||||
parameter integer STACKW = 4, // loop stack depth: 2^STACKW entries
|
||||
parameter integer EPROM = 0 // 0: 3 bit packed stream, 1: one instruction per byte
|
||||
) (
|
||||
input wire clk,
|
||||
input wire rst_n,
|
||||
@@ -77,20 +83,28 @@ module rot8 #(
|
||||
reg [STACKW-1:0] sp;
|
||||
|
||||
// ---- fetch ----------------------------------------------------------------
|
||||
// instruction ip starts at bit 3*ip of the byte stream
|
||||
// packed: instruction ip starts at bit 3*ip of the byte stream, so a fetch has
|
||||
// to see two neighbouring bytes; eprom: ip *is* the address and the low three
|
||||
// data bits are the whole opcode.
|
||||
wire [IPW+1:0] bit_index = ip * 3;
|
||||
wire [IPW-1:0] byte_index = bit_index >> 3;
|
||||
wire [2:0] bit_offset = bit_index[2:0];
|
||||
wire [15:0] window = {prog_data1, prog_data0};
|
||||
wire [2:0] op_now = (window >> bit_offset) & 16'h0007;
|
||||
wire [2:0] op_packed = (window >> bit_offset) & 16'h0007;
|
||||
|
||||
assign prog_addr0 = byte_index;
|
||||
assign prog_addr1 = byte_index + 1'b1;
|
||||
wire [2:0] op_now = EPROM ? prog_data0[2:0] : op_packed;
|
||||
|
||||
assign prog_addr0 = EPROM ? ip : byte_index;
|
||||
assign prog_addr1 = EPROM ? ip : (byte_index + 1'b1);
|
||||
// in_ready means "the byte I am waiting for would be taken this cycle". It
|
||||
// must NOT be qualified with !in_valid: a producer then never sees the
|
||||
// acceptance edge and the handshake deadlocks (the first testbench did exactly
|
||||
// that and hello, which reads nothing, was the only tape that could run).
|
||||
assign in_ready = (state == S_FETCH) && (op_now == OP_INP);
|
||||
// ...and it is only true when a byte would really be consumed this cycle:
|
||||
// with run low (single step mode) or after a halt the machine is not going to
|
||||
// take it, and a producer that trusts a high in_ready drops its byte.
|
||||
assign in_ready = (state == S_FETCH) && (op_now == OP_INP)
|
||||
&& (run || step) && !halted;
|
||||
|
||||
// forward depth counter for the '[' skip
|
||||
reg [STACKW:0] scan_depth;
|
||||
@@ -145,9 +159,16 @@ module rot8 #(
|
||||
state <= S_SCAN;
|
||||
ip <= ip + 1'b1;
|
||||
end else begin
|
||||
// entering the loop: remember where it starts
|
||||
stack[sp] <= ip;
|
||||
sp <= sp + 1'b1;
|
||||
// entering the loop: remember where it starts. A full stack
|
||||
// is as malformed as an empty pop below -- halt instead of
|
||||
// wrapping sp and silently jumping somewhere else later.
|
||||
if (sp == {STACKW{1'b1}}) begin
|
||||
halted <= 1'b1;
|
||||
state <= S_HALT;
|
||||
end else begin
|
||||
stack[sp] <= ip;
|
||||
sp <= sp + 1'b1;
|
||||
end
|
||||
ip <= ip + 1'b1;
|
||||
end
|
||||
end
|
||||
|
||||
+10
-1
@@ -76,7 +76,16 @@ module tb_rot8;
|
||||
end
|
||||
endtask
|
||||
|
||||
rot8 #(.WIDTH(8), .RING(64), .IPW(MEMW), .STACKW(4)) dut (
|
||||
// Compile with -DEPROM_MODE to model the one-instruction-per-byte ROM
|
||||
// (27C512, see docs/ttl-schematic.md §4); the tape and the expected numbers
|
||||
// are the same either way, only the image file differs (--hex vs --eprom --hex).
|
||||
`ifdef EPROM_MODE
|
||||
localparam integer EPROM = 1;
|
||||
`else
|
||||
localparam integer EPROM = 0;
|
||||
`endif
|
||||
|
||||
rot8 #(.WIDTH(8), .RING(64), .IPW(MEMW), .STACKW(4), .EPROM(EPROM)) dut (
|
||||
.clk(clk), .rst_n(rst_n), .run(run), .step(step),
|
||||
.prog_addr0(prog_addr0), .prog_addr1(prog_addr1),
|
||||
.prog_data0(prog_data0), .prog_data1(prog_data1),
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/bin/sh
|
||||
# tests/rtl_probe.sh -- 不依赖 R8Forge 的两条探针。
|
||||
#
|
||||
# ./tests/rtl_probe.sh in-ready
|
||||
# 只跑 in_ready 的定向测试(自带纸带,不需要镜像)
|
||||
#
|
||||
# ./tests/rtl_probe.sh states <image.hex> <LEN> [input.txt] [eprom]
|
||||
# 跑一张镜像,打印步数/拍数/扫描占比/最大栈深
|
||||
#
|
||||
# 镜像来源(R8Forge,8 位 / 64 格的构建):
|
||||
# r8forge examples/calc.r8asm > calc.rot8
|
||||
# r8forge_tape_pack calc.rot8 -o calc.r8t --hex calc.hex --verify
|
||||
# r8forge_tape_pack calc.rot8 -o calc_e.r8t --eprom --hex calc_e.hex # 一字节一指令
|
||||
#
|
||||
# 注意:Icarus 把 plusarg 原样传给 $readmemh,路径里有非 ASCII 字符时内存会**静默留空**
|
||||
# (见 rtl/README.md 的坑 1)——所以这里一律先 cd 到镜像目录再用相对路径。
|
||||
set -e
|
||||
cd "$(dirname "$0")/.."
|
||||
|
||||
mode=${1:-in-ready}
|
||||
build=$(mktemp -d)
|
||||
trap 'rm -rf "$build"' EXIT
|
||||
|
||||
case "$mode" in
|
||||
in-ready)
|
||||
iverilog -g2012 -o "$build/tb.vvp" rtl/rot8.v tests/tb_in_ready.v
|
||||
exec vvp "$build/tb.vvp"
|
||||
;;
|
||||
states)
|
||||
hex=$2; len=$3; infile=$4; eprom=$5
|
||||
[ -n "$hex" ] && [ -n "$len" ] || { echo "usage: $0 states <image.hex> <LEN> [input.txt] [eprom]"; exit 2; }
|
||||
dir=$(cd "$(dirname "$hex")" && pwd); base=$(basename "$hex")
|
||||
extra=""
|
||||
[ -n "$infile" ] && extra="+INFILE=$(cd "$(dirname "$infile")" && pwd)/$(basename "$infile")"
|
||||
if [ "$eprom" = "eprom" ]; then
|
||||
iverilog -g2012 -DEPROM_MODE -o "$build/tb.vvp" rtl/rot8.v tests/tb_state_mix.v
|
||||
else
|
||||
iverilog -g2012 -o "$build/tb.vvp" rtl/rot8.v tests/tb_state_mix.v
|
||||
fi
|
||||
cd "$dir"
|
||||
exec vvp "$build/tb.vvp" "+HEX=$base" "+LEN=$len" $extra
|
||||
;;
|
||||
*)
|
||||
echo "usage: $0 in-ready | states <image.hex> <LEN> [input.txt] [eprom]"; exit 2
|
||||
;;
|
||||
esac
|
||||
@@ -0,0 +1,113 @@
|
||||
// tests/tb_in_ready.v -- the in_ready contract, as a directed test.
|
||||
//
|
||||
// in_ready means "a byte handed over this cycle would really be consumed".
|
||||
// Two historical bugs here, neither of which any tape found:
|
||||
// * qualified with !in_valid -> deadlock (only a tape that reads input hangs,
|
||||
// and it shows up as the cycle cap, not as a wrong answer; rtl/README.md 坑 2)
|
||||
// * not qualified with run/step -> in single-step mode the machine is stopped
|
||||
// but still says "send me a byte", so a producer that trusts it hands one
|
||||
// over and loses it; the machine then waits forever on ','
|
||||
//
|
||||
// Tape: instruction 0 = ',' (7), instruction 1 = '.' (6), packed into one byte
|
||||
// (0b001_110_111 = 0x37) -- no assembler, no ROM image needed.
|
||||
//
|
||||
// iverilog -g2012 -o tb.vvp rtl/rot8.v tests/tb_in_ready.v && vvp tb.vvp
|
||||
|
||||
`timescale 1ns/1ps
|
||||
`default_nettype none
|
||||
|
||||
module tb_in_ready;
|
||||
reg clk = 0, rst_n = 0, run = 0, step = 0;
|
||||
reg in_valid = 0;
|
||||
reg [7:0] in_data = 0;
|
||||
reg [15:0] prog_len = 2;
|
||||
|
||||
wire [15:0] prog_addr0, prog_addr1;
|
||||
reg [7:0] prog_data0, prog_data1;
|
||||
wire in_ready, out_valid, halted;
|
||||
wire [7:0] out_data;
|
||||
wire [15:0] ip;
|
||||
wire [5:0] dp;
|
||||
wire [31:0] steps;
|
||||
|
||||
reg [7:0] mem [0:63];
|
||||
integer errors = 0;
|
||||
|
||||
rot8 #(.WIDTH(8), .RING(64), .IPW(16), .STACKW(4)) dut (
|
||||
.clk(clk), .rst_n(rst_n), .run(run), .step(step),
|
||||
.prog_addr0(prog_addr0), .prog_addr1(prog_addr1),
|
||||
.prog_data0(prog_data0), .prog_data1(prog_data1), .prog_len(prog_len),
|
||||
.in_valid(in_valid), .in_data(in_data), .in_ready(in_ready),
|
||||
.out_valid(out_valid), .out_data(out_data),
|
||||
.ip(ip), .dp(dp), .steps(steps), .halted(halted));
|
||||
|
||||
always @(*) begin
|
||||
prog_data0 = mem[prog_addr0[5:0]];
|
||||
prog_data1 = mem[prog_addr1[5:0]];
|
||||
end
|
||||
|
||||
always #5 clk = ~clk;
|
||||
|
||||
task expect0(input exp, input got);
|
||||
begin
|
||||
if (exp !== got) begin
|
||||
$display(" expected %0b got %0b -> FAIL", exp, got);
|
||||
errors = errors + 1;
|
||||
end else
|
||||
$display(" got %0b -> ok", got);
|
||||
end
|
||||
endtask
|
||||
|
||||
initial begin
|
||||
mem[0] = 8'h37; // 指令 0 = ',' (7), 指令 1 = '.' (6)
|
||||
mem[1] = 8'h00;
|
||||
|
||||
rst_n = 0; #20 rst_n = 1; #20;
|
||||
|
||||
$display("1. 单步模式停着(run=0):in_ready 必须是低");
|
||||
run = 0; step = 0; #30;
|
||||
expect0(1'b0, in_ready);
|
||||
|
||||
$display("2. run=0 时给一拍 in_valid:不能出现\"假接受\"(in_ready 全程低)");
|
||||
in_valid = 1; in_data = 8'h41; #10;
|
||||
expect0(1'b0, in_ready);
|
||||
in_valid = 0; #20;
|
||||
if (dut.cells[0] !== 8'h00) begin
|
||||
$display(" cell0 = %02x -> FAIL(机器不该吃)", dut.cells[0]);
|
||||
errors = errors + 1;
|
||||
end else
|
||||
$display(" cell0 仍是 00 -> ok");
|
||||
|
||||
$display("3. 电平式 in_valid 挂在 run=0 上,随后放开 run:必须吃掉");
|
||||
in_valid = 1; in_data = 8'h43; #20;
|
||||
run = 1; #45;
|
||||
if (dut.cells[0] !== 8'h43) begin
|
||||
$display(" cell0 = %02x -> FAIL", dut.cells[0]);
|
||||
errors = errors + 1;
|
||||
end else
|
||||
$display(" cell0 = 43 -> ok");
|
||||
|
||||
$display("4. 复位后 run=1 停在 ',' 上:in_ready 必须为高,且交出的字节进格");
|
||||
rst_n = 0; #20 rst_n = 1; run = 1; in_valid = 0; #25;
|
||||
expect0(1'b1, in_ready);
|
||||
in_valid = 1; in_data = 8'h42; #10 in_valid = 0; #25;
|
||||
if (dut.cells[0] !== 8'h42) begin
|
||||
$display(" cell0 = %02x -> FAIL", dut.cells[0]);
|
||||
errors = errors + 1;
|
||||
end else
|
||||
$display(" cell0 = 42 -> ok");
|
||||
|
||||
$display("5. 停机后(ip >= prog_len):in_ready 必须是低");
|
||||
prog_len = 0; #40;
|
||||
if (!halted) begin
|
||||
$display(" 没有停机 -> FAIL"); errors = errors + 1;
|
||||
end
|
||||
expect0(1'b0, in_ready);
|
||||
|
||||
if (errors == 0) $display("ALL OK");
|
||||
else $display("%0d FAILURES", errors);
|
||||
$finish;
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
@@ -0,0 +1,107 @@
|
||||
// tests/tb_state_mix.v -- 跑一张纸带,数周期花在哪:取指 / 括号扫描 / 停机。
|
||||
//
|
||||
// 存在的理由:rtl/README.md 只说"和解释器步数一致",那对语义够用,但对**硬件预算**
|
||||
// 不够 —— 步数不等于拍数。实测(8 位 / 64 格,见 docs/bracket-skip.md):
|
||||
//
|
||||
// hello 417 步 / 579 拍 (扫描 27%) 最大栈深 0
|
||||
// test 100142 步 / (扫描 53%) 最大栈深 4
|
||||
// calc 166741 步 / 698075 拍 (扫描 76%) 最大栈深 7
|
||||
//
|
||||
// iverilog -g2012 -o tb_state_mix.vvp rtl/rot8.v tests/tb_state_mix.v
|
||||
// vvp tb_state_mix.vvp +HEX=hello.hex +LEN=577
|
||||
// vvp tb_state_mix.vvp +HEX=calc.hex +LEN=51378 +INFILE=calc_in.txt
|
||||
// vvp tb_state_mix.vvp +HEX=calc.hex +LEN=51378 +EPROM // 一字节一指令的镜像
|
||||
|
||||
`timescale 1ns/1ps
|
||||
`default_nettype none
|
||||
|
||||
module tb_state_mix;
|
||||
reg clk = 0, rst_n = 0, run = 1, step = 0;
|
||||
reg in_valid = 0;
|
||||
reg [7:0] in_data = 0;
|
||||
reg [15:0] length = 0;
|
||||
|
||||
wire [15:0] prog_addr0, prog_addr1;
|
||||
reg [7:0] prog_data0, prog_data1;
|
||||
wire in_ready, out_valid, halted;
|
||||
wire [7:0] out_data;
|
||||
wire [15:0] ip;
|
||||
wire [5:0] dp;
|
||||
wire [31:0] steps;
|
||||
|
||||
reg [7:0] mem [0:65535];
|
||||
reg [1023:0] hex_path, in_path;
|
||||
integer in_fd, next_char, i;
|
||||
integer cycles = 0, c_fetch = 0, c_scan = 0, c_halt = 0;
|
||||
integer n_out = 0, stalls = 0, max_sp = 0, max_scan = 0;
|
||||
reg in_eof = 0;
|
||||
|
||||
`ifdef EPROM_MODE
|
||||
localparam integer EPROM = 1;
|
||||
`else
|
||||
localparam integer EPROM = 0;
|
||||
`endif
|
||||
|
||||
rot8 #(.WIDTH(8), .RING(64), .IPW(16), .STACKW(4), .EPROM(EPROM)) dut (
|
||||
.clk(clk), .rst_n(rst_n), .run(run), .step(step),
|
||||
.prog_addr0(prog_addr0), .prog_addr1(prog_addr1),
|
||||
.prog_data0(prog_data0), .prog_data1(prog_data1), .prog_len(length),
|
||||
.in_valid(in_valid), .in_data(in_data), .in_ready(in_ready),
|
||||
.out_valid(out_valid), .out_data(out_data),
|
||||
.ip(ip), .dp(dp), .steps(steps), .halted(halted));
|
||||
|
||||
assign prog_data0 = mem[prog_addr0];
|
||||
assign prog_data1 = mem[prog_addr1];
|
||||
|
||||
always #5 clk = ~clk;
|
||||
|
||||
task take_char;
|
||||
begin
|
||||
next_char = $fgetc(in_fd);
|
||||
if (next_char < 0) begin
|
||||
in_eof <= 1'b1; in_data <= 8'd0;
|
||||
end else
|
||||
in_data <= next_char[7:0];
|
||||
end
|
||||
endtask
|
||||
|
||||
initial begin
|
||||
if (!$value$plusargs("HEX=%s", hex_path)) begin $display("need +HEX="); $finish; end
|
||||
if (!$value$plusargs("LEN=%d", length)) begin $display("need +LEN="); $finish; end
|
||||
if ($test$plusargs("EPROM")) begin
|
||||
// 一字节一指令的镜像里 ip 就是地址,长度也要按"字节数"给;这里不改指针,
|
||||
// 只提醒调用方用 --eprom --hex 生成的镜像与同样的 +LEN。
|
||||
end
|
||||
for (i = 0; i < 65536; i = i + 1) mem[i] = 8'h00;
|
||||
$readmemh(hex_path, mem);
|
||||
if ($value$plusargs("INFILE=%s", in_path)) in_fd = $fopen(in_path, "r");
|
||||
#20 rst_n = 1;
|
||||
if (in_fd != 0) begin in_valid = 1'b1; take_char; end
|
||||
while (!halted && cycles < 200000000) begin
|
||||
@(posedge clk);
|
||||
cycles = cycles + 1;
|
||||
case (dut.state)
|
||||
2'd0: c_fetch = c_fetch + 1;
|
||||
2'd1: c_scan = c_scan + 1;
|
||||
2'd2: c_halt = c_halt + 1;
|
||||
endcase
|
||||
if (dut.sp > max_sp) max_sp = dut.sp;
|
||||
if (dut.scan_depth > max_scan) max_scan = dut.scan_depth;
|
||||
if (in_ready && !in_valid) stalls = stalls + 1;
|
||||
if (in_ready && in_valid && in_fd != 0 && !in_eof) take_char;
|
||||
if (out_valid) n_out = n_out + 1;
|
||||
end
|
||||
$display("%s%s", EPROM ? "[EPROM 1 byte/instruction] " : "[packed 3 bit/instruction] ",
|
||||
halted ? "" : "NO HALT -- ");
|
||||
$display("steps=%0d cycles=%0d outs=%0d stalls=%0d", steps, cycles, n_out, stalls);
|
||||
if (cycles > 0)
|
||||
$display(" S_FETCH=%0d (%0d%%) S_SCAN=%0d (%0d%%) S_HALT=%0d",
|
||||
c_fetch, (100*c_fetch)/cycles, c_scan, (100*c_scan)/cycles, c_halt);
|
||||
if (steps > 0)
|
||||
$display(" cycles/step = %0d.%02d, max stack depth = %0d, max scan depth = %0d",
|
||||
cycles/steps, (100*(cycles%steps))/steps, max_sp, max_scan);
|
||||
$finish;
|
||||
end
|
||||
endmodule
|
||||
|
||||
`default_nettype wire
|
||||
Reference in new issue
Block a user