Compare commits
13
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e57d97b0ab | ||
|
|
cb0cb433ac | ||
|
|
fed01f5910 | ||
|
|
4a81685066 | ||
|
|
556fbe9530 | ||
|
|
1ae6fe6ebe | ||
|
|
192e28795a | ||
|
|
ccd1b917d0 | ||
|
|
9f6f4c9ada | ||
|
|
036a57d907 | ||
|
|
266ec6a049 | ||
|
|
abb2e161af | ||
|
|
03ac91bf65 |
@@ -14,6 +14,3 @@ _vivado/
|
|||||||
*.pb
|
*.pb
|
||||||
*.hex.lst
|
*.hex.lst
|
||||||
.Xil/
|
.Xil/
|
||||||
|
|
||||||
# saolei 的构建产物(tools/build.ps1 一键重建,不入库)
|
|
||||||
saolei/build/
|
|
||||||
@@ -10,28 +10,20 @@
|
|||||||
|
|
||||||
```text
|
```text
|
||||||
risc-v/
|
risc-v/
|
||||||
├─ README.md 本文档
|
|
||||||
├─ pipelined-cpu/ Vivado 工程目录(也是源文件所在地)
|
├─ pipelined-cpu/ Vivado 工程目录(也是源文件所在地)
|
||||||
│ ├─ create_project.tcl 一键重建工程的 Tcl 脚本
|
│ ├─ create_project.tcl 一键重建工程的 Tcl 脚本
|
||||||
│ ├─ srcs/rtl/*.v 处理器与外设 RTL(17 个文件,cpu21_riscv_redirect_int_bpb.v 为处理器顶层)
|
│ ├─ srcs/rtl/*.v 处理器 RTL(13 个文件,cpu21_riscv_redirect_int_bpb.v 为顶层)
|
||||||
│ │ 外设:cpu21_mmio.v(MMIO 中枢)、cpu21_vga.v(显示)、
|
|
||||||
│ │ cpu21_seg7.v(数码管)、cpu21_pointer.v(按键当鼠标)、
|
|
||||||
│ │ cpu21_ps2_mouse.v(可选 PS/2 鼠标)
|
|
||||||
│ ├─ srcs/board/*.v|*.vh 板级顶层(时钟分频 + 片内 ROM/RAM + 引脚)、片内 ROM 初始化
|
│ ├─ srcs/board/*.v|*.vh 板级顶层(时钟分频 + 片内 ROM/RAM + 引脚)、片内 ROM 初始化
|
||||||
│ ├─ srcs/constrs/*.xdc 引脚与分频时钟约束(+ Nexys4DDR 母版)
|
│ ├─ srcs/constrs/*.xdc 引脚与分频时钟约束(+ Nexys4DDR 母版)
|
||||||
│ ├─ srcs/tb/*.v 三个测试平台
|
│ ├─ srcs/tb/*.v 三个测试平台
|
||||||
│ ├─ srcs/rom/*.hex 测试平台加载的 ROM 镜像
|
│ ├─ srcs/rom/*.hex 测试平台加载的 ROM 镜像
|
||||||
│ ├─ tools/ rv_asm.py(迷你汇编器)、hex2vh.py(hex → ROM 初始化)、run_regress.ps1(一键回归)
|
│ ├─ tools/ rv_asm.py(迷你汇编器)、hex2vh.py(hex → ROM 初始化)、run_regress.ps1(一键回归)
|
||||||
│ └─ programs/ 汇编源程序、期望值、课程原始资产(RARS)
|
│ ├─ programs/ 汇编源程序、期望值、课程原始资产(RARS)
|
||||||
└─ saolei/ 纯 C 扫雷(MMIO 显示 + 中断读鼠标),见下文
|
│ └─ pipelined-cpu.xpr(+*.runs/…) 本机工程与生成物(不入库,可删)
|
||||||
├─ sw/ C 源码 + 链接脚本(三个固定中断入口)
|
├─ cpu21_riscv_redirect_int_bpb说明.md 本文档
|
||||||
└─ tools/ build.ps1(C → ROM)、check_isa.py(指令白名单校验)
|
└─ _tmp/、_vivado/ 本地临时目录(不入库)
|
||||||
```
|
```
|
||||||
|
|
||||||
**仓库里不放任何 Vivado 生成物**(`.xpr`、`.runs`、`.sim`、`.cache`、`.hw`、
|
|
||||||
`.ip_user_files`、`.Xil`、`vivado.jou/log` 本地已清理干净):需要工程时用
|
|
||||||
`source create_project.tcl` 一键重建,这样仓库始终是最清爽的源文件状态。
|
|
||||||
|
|
||||||
约定:git 只跟踪 `pipelined-cpu/` 下的源文件(`srcs/`、`tools/`、`programs/`、
|
约定:git 只跟踪 `pipelined-cpu/` 下的源文件(`srcs/`、`tools/`、`programs/`、
|
||||||
`create_project.tcl`),Vivado 生成物(`*.xpr`、`*.runs`、`*.sim`、`*.cache`…)由
|
`create_project.tcl`),Vivado 生成物(`*.xpr`、`*.runs`、`*.sim`、`*.cache`…)由
|
||||||
`.gitignore` 排除。`srcs/rtl/` 只放可综合的处理器源码;板级顶层与约束在 `srcs/board/`、
|
`.gitignore` 排除。`srcs/rtl/` 只放可综合的处理器源码;板级顶层与约束在 `srcs/board/`、
|
||||||
@@ -80,22 +72,13 @@ risc-v/
|
|||||||
|
|
||||||
## MMIO 与外设
|
## MMIO 与外设
|
||||||
|
|
||||||
外设寄存器映射在数据存储器地址空间,由 `pipelined-cpu/srcs/rtl/cpu21_mmio.v` 在 MEM 级译码。
|
外设寄存器映射在数据存储器地址空间,由 `pipelined-cpu/srcs/rtl/cpu21_mmio.v` 在 MEM 级译码,
|
||||||
这些地址都在 4 KB 数据 RAM(`0x0000`–`0x0FFC`)之外,因此不会与 RAM 冲突。
|
顶层参数 `LED_ADDR`(默认 `0x0000_1000`)指定 LED 数据寄存器的地址。
|
||||||
每行的字节偏移都按**字**对齐译码(同一 32 位寄存器内任意字节偏移都命中)。
|
该地址位于 4 KB 数据 RAM(`0x0000`–`0x0FFC`)之外,因此不会与 RAM 冲突。
|
||||||
|
|
||||||
| 地址 | 外设 | 写 | 读 |
|
| 地址 | 外设 | 写 | 读 |
|
||||||
| ---------------------- | -------------- | ---------------------------------------------------------------------------- | -------------------------- |
|
| ------------- | -------------- | ---------------------------------------------------------------------------- | -------------------------- |
|
||||||
| `0x0000_1000` | LED 数据寄存器 | `sw` 写全字;`sb` 按 `data_wstrb_o` 只改对应字节通道,`led_valid_o` 拉高一拍 | `lw` 返回 LED 寄存器当前值 |
|
| `0x0000_1000` | LED 数据寄存器 | `sw` 写全字;`sb` 按 `data_wstrb_o` 只改对应字节通道,`led_valid_o` 拉高一拍 | `lw` 返回 LED 寄存器当前值 |
|
||||||
| `0x0000_1004` | 七段数码管 | `sw`/`sb` 写 8 位十六进制数(低 4 位 = 最右边那个数码管) | `lw` 读回 |
|
|
||||||
| `0x0000_1100`+ | 鼠标寄存器组 | STATUS / DATA / ACK / CTRL / INPUT / TIME / FRAME / EVCNT / LEVEL / MAGIC | 同左(DATA 读无副作用) |
|
|
||||||
| `0x0000_1140`+ | 显示控制器 | CTRL(光标可见、背景色)/ CURSOR(列、行) | 同左(另含 MAGIC 自检值) |
|
|
||||||
| `0x0000_2000`–`0x27FF` | 显存 | 40 列 × 30 行、每格 1 字节(字形 + 颜色),写端口接到板级顶层的双口 RAM | `lw` 组合读回 |
|
|
||||||
|
|
||||||
其中 `LEVEL`(`0x1120`)是一个**软件中断**寄存器:写 1 会拉起 `irq_i[1]`,用来让程序
|
|
||||||
"跳到关卡入口";`MAGIC`(`0xCAFE0001`/`0xCAFE0002`)可以读来自检 MMIO 是否活着。
|
|
||||||
鼠标事件 FIFO(深度 16)配合 `mouse_irq_o` 提供"事件到达即中断",`isr_body` 读空 FIFO 后
|
|
||||||
中断线自动回 0,因此不会丢事件。
|
|
||||||
|
|
||||||
地址译码按**字**对齐:同一 32 位寄存器内的任意字节偏移(如 `0x1001`)都算命中,
|
地址译码按**字**对齐:同一 32 位寄存器内的任意字节偏移(如 `0x1001`)都算命中,
|
||||||
具体修改哪几个字节由 `data_wstrb_o` 决定。`data_addr_o` 在命中 MMIO 时不会
|
具体修改哪几个字节由 `data_wstrb_o` 决定。`data_addr_o` 在命中 MMIO 时不会
|
||||||
@@ -154,13 +137,8 @@ risc-v/
|
|||||||
| `pipelined-cpu/srcs/rtl/cpu21_riscv_hazard_unit.v` | `cpu21_riscv_hazard_unit` | load-use 冒险、停顿(stall)与冲刷(flush)判定 |
|
| `pipelined-cpu/srcs/rtl/cpu21_riscv_hazard_unit.v` | `cpu21_riscv_hazard_unit` | load-use 冒险、停顿(stall)与冲刷(flush)判定 |
|
||||||
| `pipelined-cpu/srcs/rtl/cpu21_riscv_irq_ctrl.v` | `cpu21_riscv_irq_ctrl` | 中断采样与优先级、`ustatus`/`uepc` CSR、嵌套返回栈 |
|
| `pipelined-cpu/srcs/rtl/cpu21_riscv_irq_ctrl.v` | `cpu21_riscv_irq_ctrl` | 中断采样与优先级、`ustatus`/`uepc` CSR、嵌套返回栈 |
|
||||||
| `pipelined-cpu/srcs/rtl/cpu21_riscv_perf_counters.v` | `cpu21_riscv_perf_counters` | 周期/停顿/气泡/分支/预测成功率等统计计数器 |
|
| `pipelined-cpu/srcs/rtl/cpu21_riscv_perf_counters.v` | `cpu21_riscv_perf_counters` | 周期/停顿/气泡/分支/预测成功率等统计计数器 |
|
||||||
| `pipelined-cpu/srcs/rtl/cpu21_mmio.v` | `cpu21_mmio` | MMIO 中枢:LED、七段数码管、鼠标寄存器组 + 事件 FIFO + 中断、显示控制器、显存窗口 |
|
| `pipelined-cpu/srcs/rtl/cpu21_mmio.v` | `cpu21_mmio` | MMIO 外设:LED 数据寄存器(可写、可读回,支持字节写) |
|
||||||
| `pipelined-cpu/srcs/rtl/cpu21_vga.v` | `cpu21_vga` | 640x480@60 显示控制器:按"格子"扫描,8x8 字模放大 2 倍填满 16x16 的格子 |
|
| `pipelined-cpu/srcs/board/cpu21_board_top.v` | `cpu21_board_top` | 板级顶层:时钟分频、复位同步、片内 ROM/RAM、引脚外设连接 |
|
||||||
| `pipelined-cpu/srcs/rtl/cpu21_seg7.v` | `cpu21_seg7` | 板载 8 位数码管动态扫描驱动(共阳极,位选/段选低有效) |
|
|
||||||
| `pipelined-cpu/srcs/rtl/cpu21_pointer.v` | `cpu21_pointer` | 把 5 向按键变成"鼠标"事件(去抖 + 事件排队,单位 1/8 格) |
|
|
||||||
| `pipelined-cpu/srcs/rtl/cpu21_ps2_mouse.v` | `cpu21_ps2_mouse` | 可选的真鼠标:PS/2 接收(11 位帧 + 奇校验 + 3 字节包解析),只实现接收 |
|
|
||||||
| `pipelined-cpu/srcs/board/cpu21_board_top.v` | `cpu21_board_top` | 板级顶层:时钟分频、复位同步、片内 ROM/RAM、显存双口 RAM、VGA/数码管/鼠标/中断连线 |
|
|
||||||
| `pipelined-cpu/srcs/board/cpu21_saolei_rom.vh` | — | 扫雷程序的 ROM 初始化(由 `saolei/tools/build.ps1` 生成) |
|
|
||||||
| `pipelined-cpu/srcs/constrs/cpu21_board_top.xdc` | — | Nexys4DDR 引脚约束 + 分频时钟(`create_generated_clock`) |
|
| `pipelined-cpu/srcs/constrs/cpu21_board_top.xdc` | — | Nexys4DDR 引脚约束 + 分频时钟(`create_generated_clock`) |
|
||||||
| `pipelined-cpu/srcs/board/cpu21_board_rom.vh` | — | 片内 ROM 内容(生成文件,`rom[i] = 32'h…;`) |
|
| `pipelined-cpu/srcs/board/cpu21_board_rom.vh` | — | 片内 ROM 内容(生成文件,`rom[i] = 32'h…;`) |
|
||||||
| `pipelined-cpu/srcs/tb/tb_board_top.v` | `tb_board_top` | 板级顶层的 RTL 仿真(观察 LED 跑马灯与写入指示) |
|
| `pipelined-cpu/srcs/tb/tb_board_top.v` | `tb_board_top` | 板级顶层的 RTL 仿真(观察 LED 跑马灯与写入指示) |
|
||||||
@@ -179,8 +157,7 @@ risc-v/
|
|||||||
BPB/中断观测值)。如果直接把它设为实现顶层,这些端口会全部变成引脚,导致
|
BPB/中断观测值)。如果直接把它设为实现顶层,这些端口会全部变成引脚,导致
|
||||||
`[Place 30-415] IO Placement failed due to overutilization`(占用 775 个 IO,
|
`[Place 30-415] IO Placement failed due to overutilization`(占用 775 个 IO,
|
||||||
而 `xc7a100tcsg324-1` 只有 210 个)。因此上板时用 `cpu21_board_top` 作为顶层,
|
而 `xc7a100tcsg324-1` 只有 210 个)。因此上板时用 `cpu21_board_top` 作为顶层,
|
||||||
只引出板上真正需要的那些引脚(LED、按键、拨码、VGA、8 位数码管、可选的 PS/2),
|
只引出板上真正需要的 23 个引脚,其余调试端口不连接。
|
||||||
其余调试端口不连接。定义宏 `SAOLEI_ROM` 时还会把扫雷程序固化进 ROM。
|
|
||||||
|
|
||||||
### 引脚分配(`cpu21_board_top.xdc`)
|
### 引脚分配(`cpu21_board_top.xdc`)
|
||||||
|
|
||||||
@@ -300,22 +277,6 @@ CPU 时钟 6.25 MHz → 50 MHz(快 8 倍),并且乘除法不再位于关
|
|||||||
`.bit`。复位后跑马灯立即从 `0001` 开始逐位左移(演示程序不执行 `ecall`,所以不用
|
`.bit`。复位后跑马灯立即从 `0001` 开始逐位左移(演示程序不执行 `ecall`,所以不用
|
||||||
按 `go`;`go` 只对执行了 `ecall` 停机、等待继续运行的程序有意义),LED16_B 每次写入时点亮。
|
按 `go`;`go` 只对执行了 `ecall` 停机、等待继续运行的程序有意义),LED16_B 每次写入时点亮。
|
||||||
|
|
||||||
## 扫雷程序(`saolei/`)
|
|
||||||
|
|
||||||
`saolei/` 是一个**纯 C 写的扫雷**,用 RISC-V 工具链编译成机器码后固化进片内 ROM 运行,
|
|
||||||
反过来验证这套 MMIO/中断/显示/鼠标外设。完整说明见 `saolei/README.md`,要点:
|
|
||||||
|
|
||||||
- **三段中断入口**(地址由 `saolei/sw/linker.ld` 定死,与顶层 `IRQ1/2/3_VECTOR` 成对):
|
|
||||||
IRQ1=`0x100` 关卡(游戏)程序、IRQ2=`0x200` 鼠标读取、IRQ3=`0x300` 备用;
|
|
||||||
- **两层结构**:背景 `main()` 画开始界面并一直运行 → 点"开始"写 `LEVEL` 寄存器拉起软件中断
|
|
||||||
→ CPU 跳进 `0x100` 的关卡程序跑完整局 → 点左上角黄格 `uret` 回到背景被打断的那条指令;
|
|
||||||
- **纯 C**,只有三处必要的内联汇编:设 `sp`、三个中断入口的现场保护、`uret`;
|
|
||||||
- **指令子集适配**:没有 `auipc`/`lb`/`lbu`/有符号 `div`,所以程序只按字节写显存、
|
|
||||||
索引全用移位、且不含 `.data`/`.rodata`(链接脚本用 `ASSERT` 兜住);
|
|
||||||
- **构建**:`saolei/tools/build.ps1`(C → ELF → `.hex` + `cpu21_saolei_rom.vh`),
|
|
||||||
并用 `check_isa.py` 逐条反汇编检查有没有用到未实现的指令。实测 842 条指令全部合法;
|
|
||||||
- **上板切换**:Vivado 里定义宏 `SAOLEI_ROM` 就加载扫雷程序,不定义仍是下面的跑马灯。
|
|
||||||
|
|
||||||
## 回归测试
|
## 回归测试
|
||||||
|
|
||||||
工程里带了一个 xsim 回归脚本,在 `pipelined-cpu/` 目录下运行:
|
工程里带了一个 xsim 回归脚本,在 `pipelined-cpu/` 目录下运行:
|
||||||
@@ -338,7 +299,7 @@ powershell -ExecutionPolicy Bypass -File tools\run_regress.ps1
|
|||||||
期望结果(2026-09,多周期乘除法 + `CLK_DIV=2`):
|
期望结果(2026-09,多周期乘除法 + `CLK_DIV=2`):
|
||||||
|
|
||||||
| 测试 | 期望 |
|
| 测试 | 期望 |
|
||||||
| -------------------------------------------------- | ---------------------------------------------------------------------------- |
|
| ---- | ---- |
|
||||||
| muldiv_test / isa_test / lui_test / mmio_test | 13 / 24 / 4 / 5 个 LED 值全部匹配 |
|
| muldiv_test / isa_test / lui_test / mmio_test | 13 / 24 / 4 / 5 个 LED 值全部匹配 |
|
||||||
| tb_no_intr(benchmark,5560 周期) | `stalls=1528 bubbles=1830 cond_taken=639 uncond=71 pred_ok=687 pred_fail=80` |
|
| tb_no_intr(benchmark,5560 周期) | `stalls=1528 bubbles=1830 cond_taken=639 uncond=71 pred_ok=687 pred_fail=80` |
|
||||||
| tb_cpu21_riscv_redirect_int_bpb(中断,6003 周期) | `stalls=963 bubbles=1205 cond_taken=547 uncond=28 pred_ok=557 pred_fail=87` |
|
| tb_cpu21_riscv_redirect_int_bpb(中断,6003 周期) | `stalls=963 bubbles=1205 cond_taken=547 uncond=28 pred_ok=557 pred_fail=87` |
|
||||||
|
|||||||
@@ -64,16 +64,6 @@ add_files -norecurse -fileset sources_1 [list \
|
|||||||
set_property file_type {Verilog Header} \
|
set_property file_type {Verilog Header} \
|
||||||
[get_files "$origin_dir/srcs/board/cpu21_board_rom.vh"]
|
[get_files "$origin_dir/srcs/board/cpu21_board_rom.vh"]
|
||||||
|
|
||||||
# 扫雷程序的 ROM 初始化(由 saolei/tools/build.ps1 生成,可能还不存在)。
|
|
||||||
# 综合/仿真时定义宏 SAOLEI_ROM 就会用它替换上面的跑马灯 ROM。
|
|
||||||
set saolei_rom "$origin_dir/srcs/board/cpu21_saolei_rom.vh"
|
|
||||||
if {[file exists $saolei_rom]} {
|
|
||||||
add_files -norecurse -fileset sources_1 [list $saolei_rom]
|
|
||||||
set_property file_type {Verilog Header} [get_files $saolei_rom]
|
|
||||||
} else {
|
|
||||||
puts "NOTE: 还没有 $saolei_rom —— 需要先跑 saolei/tools/build.ps1 生成"
|
|
||||||
}
|
|
||||||
|
|
||||||
# 约束(cpu21_board_top.xdc 是引脚与分频时钟约束)
|
# 约束(cpu21_board_top.xdc 是引脚与分频时钟约束)
|
||||||
add_files -norecurse -fileset constrs_1 [glob "$origin_dir/srcs/constrs/*.xdc"]
|
add_files -norecurse -fileset constrs_1 [glob "$origin_dir/srcs/constrs/*.xdc"]
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
module cpu21_board_top #(
|
module cpu21_board_top #(
|
||||||
parameter [31:0] LED_ADDR = 32'h0000_1000,
|
parameter [31:0] LED_ADDR = 32'h0000_1000,
|
||||||
parameter ROM_WORDS = 8192,
|
parameter ROM_WORDS = 1024,
|
||||||
parameter RAM_WORDS = 1024,
|
parameter RAM_WORDS = 1024,
|
||||||
parameter [31:0] USTATUS_INIT = 32'h0000_0001,
|
parameter [31:0] USTATUS_INIT = 32'h0000_0001,
|
||||||
// 板载 100 MHz 的分频比(1 = 直用 100 MHz,2 = 50 MHz,…)。
|
// 板载 100 MHz 的分频比(1 = 直用 100 MHz,2 = 50 MHz,…)。
|
||||||
@@ -37,35 +37,15 @@ module cpu21_board_top #(
|
|||||||
// 7~8 ns 都是布线);
|
// 7~8 ns 都是布线);
|
||||||
// CLK_DIV=2( 50 MHz):WNS ≈ +8 ns,余量充足。
|
// CLK_DIV=2( 50 MHz):WNS ≈ +8 ns,余量充足。
|
||||||
// 因此默认取 2,既比原来的 16 分频快 8 倍,又能保证时序收敛。
|
// 因此默认取 2,既比原来的 16 分频快 8 倍,又能保证时序收敛。
|
||||||
parameter integer CLK_DIV = 2,
|
parameter integer CLK_DIV = 2
|
||||||
// 鼠标事件源:0 = 板上 5 向按键(免外设,默认);1 = PS/2 鼠标(Pmod JA)
|
) (
|
||||||
parameter integer USE_PS2_MOUSE = 0
|
|
||||||
) (
|
|
||||||
input wire clk100, // E3
|
input wire clk100, // E3
|
||||||
input wire rst_n, // C12:CPU_RESETN,低有效
|
input wire rst_n, // C12:CPU_RESETN,低有效
|
||||||
input wire go, // N17:BTNC(同时用作鼠标左键)
|
input wire go, // N17:BTNC
|
||||||
input wire [ 2:0] irq_sw, // SW[2:0]:原来的 3 路中断源(保持兼容)
|
input wire [ 2:0] irq_sw, // SW[2:0]
|
||||||
input wire btnu, // M18:光标上
|
|
||||||
input wire btnd, // P18:光标下
|
|
||||||
input wire btnl, // P17:光标左
|
|
||||||
input wire btnr, // M17:光标右
|
|
||||||
input wire sw_flag, // R15:SW[3],插旗模式(软件读 INPUT 寄存器)
|
|
||||||
output wire [15:0] led, // LED[15:0]
|
output wire [15:0] led, // LED[15:0]
|
||||||
output wire led_we, // LED16_B:MMIO 写入指示
|
output wire led_we // LED16_B:MMIO 写入指示
|
||||||
// 七段数码管(共阳极:位选/段选都是低有效)
|
);
|
||||||
output wire [ 7:0] an,
|
|
||||||
output wire [ 6:0] seg,
|
|
||||||
output wire dp,
|
|
||||||
// VGA
|
|
||||||
output wire [ 3:0] vga_r,
|
|
||||||
output wire [ 3:0] vga_g,
|
|
||||||
output wire [ 3:0] vga_b,
|
|
||||||
output wire vga_hs,
|
|
||||||
output wire vga_vs,
|
|
||||||
// PS/2(只有 USE_PS2_MOUSE=1 时才用)
|
|
||||||
input wire ps2_clk,
|
|
||||||
input wire ps2_data
|
|
||||||
);
|
|
||||||
localparam ROM_AW = $clog2(ROM_WORDS);
|
localparam ROM_AW = $clog2(ROM_WORDS);
|
||||||
localparam RAM_AW = $clog2(RAM_WORDS);
|
localparam RAM_AW = $clog2(RAM_WORDS);
|
||||||
|
|
||||||
@@ -87,12 +67,10 @@ module cpu21_board_top #(
|
|||||||
if (arst) begin
|
if (arst) begin
|
||||||
clk_cnt <= 16'd0;
|
clk_cnt <= 16'd0;
|
||||||
clk_div <= 1'b0;
|
clk_div <= 1'b0;
|
||||||
end
|
end else if (clk_cnt == (CLK_DIV / 2 - 1)) begin
|
||||||
else if (clk_cnt == (CLK_DIV / 2 - 1)) begin
|
|
||||||
clk_cnt <= 16'd0;
|
clk_cnt <= 16'd0;
|
||||||
clk_div <= ~clk_div;
|
clk_div <= ~clk_div;
|
||||||
end
|
end else begin
|
||||||
else begin
|
|
||||||
clk_cnt <= clk_cnt + 16'd1;
|
clk_cnt <= clk_cnt + 16'd1;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@@ -105,10 +83,8 @@ module cpu21_board_top #(
|
|||||||
reg [2:0] rst_sync_q;
|
reg [2:0] rst_sync_q;
|
||||||
|
|
||||||
always @(posedge clk_cpu or posedge arst) begin
|
always @(posedge clk_cpu or posedge arst) begin
|
||||||
if (arst)
|
if (arst) rst_sync_q <= 3'b111;
|
||||||
rst_sync_q <= 3'b111;
|
else rst_sync_q <= {rst_sync_q[1:0], 1'b0};
|
||||||
else
|
|
||||||
rst_sync_q <= {rst_sync_q[1:0], 1'b0};
|
|
||||||
end
|
end
|
||||||
|
|
||||||
wire reset = rst_sync_q[2];
|
wire reset = rst_sync_q[2];
|
||||||
@@ -121,86 +97,61 @@ module cpu21_board_top #(
|
|||||||
wire data_we;
|
wire data_we;
|
||||||
wire [31:0] led_data;
|
wire [31:0] led_data;
|
||||||
wire led_valid;
|
wire led_valid;
|
||||||
// 扫雷扩展外设的连线(先声明,例化在下面)
|
|
||||||
wire [31:0] disp_ctrl, disp_cursor; // 显示控制器(CPU 内部 MMIO 驱动)
|
// ---------------------------------------------------------------------------
|
||||||
wire [31:0] seg_data; // 数码管:8 位十六进制
|
// 指令 ROM:组合读
|
||||||
wire seg_valid;
|
// ---------------------------------------------------------------------------
|
||||||
wire fb_we; // 显存写端口
|
reg [31:0] rom [0:ROM_WORDS-1];
|
||||||
wire [ 9:0] fb_waddr;
|
integer i;
|
||||||
wire [31:0] fb_wdata;
|
|
||||||
wire [ 3:0] fb_wstrb;
|
initial begin
|
||||||
wire [ 9:0] fb_raddr; // 显存读端口(VGA)
|
for (i = 0; i < ROM_WORDS; i = i + 1) rom[i] = 32'h0000_0013; // NOP 填充
|
||||||
wire mouse_irq, level_irq; // 鼠标事件中断 / 软件中断(关卡入口)
|
`ifdef BOARD_ROM_FAST
|
||||||
wire frame_tick;
|
|
||||||
wire [15:0] sw_vec = {12'b0, sw_flag, irq_sw};
|
|
||||||
// 中断连线:SW0 与鼠标共用一个入口,SW1 与"关卡入口"共用一个入口。
|
|
||||||
wire [ 2:0] irq_vec = {irq_sw[2], (level_irq | irq_sw[1]), (mouse_irq | irq_sw[0])};
|
|
||||||
for (i = 0; i < ROM_WORDS; i = i + 1)
|
|
||||||
rom[i] = 32'h0000_0013; // NOP 填充
|
|
||||||
`ifdef SAOLEI_ROM
|
|
||||||
// 扫雷程序(由 saolei/tools/build.ps1 生成的 cpu21_saolei_rom.vh)。
|
|
||||||
// 综合/仿真时定义宏 SAOLEI_ROM 即可切换到这个程序;不定义就是下面的跑马灯。
|
|
||||||
`include "cpu21_saolei_rom.vh"
|
|
||||||
`elsif BOARD_ROM_FAST
|
|
||||||
// 仿真专用:把演示程序的延时改小的版本(综合流程不会用到)
|
// 仿真专用:把演示程序的延时改小的版本(综合流程不会用到)
|
||||||
`include "cpu21_board_rom_fast.vh"
|
`include "cpu21_board_rom_fast.vh"
|
||||||
`else
|
`else
|
||||||
`include "cpu21_board_rom.vh"
|
`include "cpu21_board_rom.vh"
|
||||||
`endif
|
`endif
|
||||||
|
|
||||||
end
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 数据 RAM:字节写使能 + 组合读
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [31:0] ram[0:RAM_WORDS-1];
|
|
||||||
integer j;
|
|
||||||
|
|
||||||
initial begin
|
|
||||||
for (j = 0; j < RAM_WORDS; j = j + 1)
|
|
||||||
ram[j] = 32'b0;
|
|
||||||
end
|
|
||||||
|
|
||||||
wire [ROM_AW+1:2] rom_index = instr_addr[ROM_AW+1:2];
|
|
||||||
wire [RAM_AW+1:2] ram_index = data_addr[RAM_AW+1:2];
|
|
||||||
wire ram_sel = (data_addr[31:RAM_AW+2] == 0);
|
|
||||||
|
|
||||||
wire [31:0] instr_word = rom[rom_index];
|
|
||||||
wire [31:0] data_rdata = ram_sel ? ram[ram_index] : 32'b0;
|
|
||||||
|
|
||||||
always @(posedge clk_cpu) begin
|
|
||||||
if (ram_sel && data_we) begin
|
|
||||||
if (data_wstrb[0])
|
|
||||||
ram[ram_index][7:0] <= data_wdata[7:0];
|
|
||||||
if (data_wstrb[1])
|
|
||||||
ram[ram_index][15:8] <= data_wdata[15:8];
|
|
||||||
if (data_wstrb[2])
|
|
||||||
ram[ram_index][23:16] <= data_wdata[23:16];
|
|
||||||
if (data_wstrb[3])
|
|
||||||
ram[ram_index][31:24] <= data_wdata[31:24];
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// 处理器:只连接上板需要的端口,调试端口留空(不产生 I/O)
|
// 数据 RAM:字节写使能 + 组合读
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
cpu21_riscv_redirect_int_bpb #(
|
reg [31:0] ram[0:RAM_WORDS-1];
|
||||||
|
integer j;
|
||||||
|
|
||||||
|
initial begin
|
||||||
|
for (j = 0; j < RAM_WORDS; j = j + 1) ram[j] = 32'b0;
|
||||||
|
end
|
||||||
|
|
||||||
|
wire [ROM_AW+1:2] rom_index = instr_addr[ROM_AW+1:2];
|
||||||
|
wire [RAM_AW+1:2] ram_index = data_addr[RAM_AW+1:2];
|
||||||
|
wire ram_sel = (data_addr[31:RAM_AW+2] == 0);
|
||||||
|
|
||||||
|
wire [31:0] instr_word = rom[rom_index];
|
||||||
|
wire [31:0] data_rdata = ram_sel ? ram[ram_index] : 32'b0;
|
||||||
|
|
||||||
|
always @(posedge clk_cpu) begin
|
||||||
|
if (ram_sel && data_we) begin
|
||||||
|
if (data_wstrb[0]) ram[ram_index][7:0] <= data_wdata[7:0];
|
||||||
|
if (data_wstrb[1]) ram[ram_index][15:8] <= data_wdata[15:8];
|
||||||
|
if (data_wstrb[2]) ram[ram_index][23:16] <= data_wdata[23:16];
|
||||||
|
if (data_wstrb[3]) ram[ram_index][31:24] <= data_wdata[31:24];
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
// 处理器:只连接上板需要的端口,调试端口留空(不产生 I/O)
|
||||||
|
// ---------------------------------------------------------------------------
|
||||||
|
cpu21_riscv_redirect_int_bpb #(
|
||||||
.LED_ADDR (LED_ADDR),
|
.LED_ADDR (LED_ADDR),
|
||||||
.USTATUS_INIT(USTATUS_INIT),
|
.USTATUS_INIT(USTATUS_INIT)
|
||||||
// 三个中断入口:必须与 sw/linker.ld 里
|
|
||||||
// .text.isr1/2/3 的地址一致
|
|
||||||
// IRQ1(0x100) = 关卡(游戏)入口
|
|
||||||
// IRQ2(0x200) = 鼠标读取
|
|
||||||
// IRQ3(0x300) = 备用
|
|
||||||
.IRQ1_VECTOR (32'h0000_0100),
|
|
||||||
.IRQ2_VECTOR (32'h0000_0200),
|
|
||||||
.IRQ3_VECTOR (32'h0000_0300)
|
|
||||||
) u_cpu (
|
) u_cpu (
|
||||||
.clk (clk_cpu),
|
.clk (clk_cpu),
|
||||||
.reset (reset),
|
.reset (reset),
|
||||||
.instr_i (instr_word),
|
.instr_i (instr_word),
|
||||||
.data_rdata_i(data_rdata),
|
.data_rdata_i(data_rdata),
|
||||||
.irq_i (irq_vec),
|
.irq_i (irq_sw),
|
||||||
.go_i (go),
|
.go_i (go),
|
||||||
|
|
||||||
.instr_addr_o(instr_addr),
|
.instr_addr_o(instr_addr),
|
||||||
@@ -210,156 +161,23 @@ cpu21_riscv_redirect_int_bpb #(
|
|||||||
.data_we_o (data_we),
|
.data_we_o (data_we),
|
||||||
|
|
||||||
.led_data_o (led_data),
|
.led_data_o (led_data),
|
||||||
.led_valid_o(led_valid),
|
.led_valid_o(led_valid)
|
||||||
|
|
||||||
// 扫雷扩展外设
|
|
||||||
.seg_data_o (seg_data),
|
|
||||||
.seg_valid_o (seg_valid),
|
|
||||||
.fb_we_o (fb_we),
|
|
||||||
.fb_waddr_o (fb_waddr),
|
|
||||||
.fb_wdata_o (fb_wdata),
|
|
||||||
.fb_wstrb_o (fb_wstrb),
|
|
||||||
.fb_rdata_i (fb_rdata),
|
|
||||||
.disp_ctrl_o (disp_ctrl),
|
|
||||||
.disp_cursor_o (disp_cursor),
|
|
||||||
.mouse_ev_valid_i(ev_valid),
|
|
||||||
.mouse_dx_i (ev_dx),
|
|
||||||
.mouse_dy_i (ev_dy),
|
|
||||||
.mouse_btn_i (ev_btn),
|
|
||||||
.sw_i (sw_vec),
|
|
||||||
.frame_tick_i (frame_tick),
|
|
||||||
.mouse_irq_o (mouse_irq),
|
|
||||||
.level_irq_o (level_irq)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
// 板载 LED:显示 MMIO 写入值的低 16 位;写入指示拉长便于观察
|
// 板载 LED:显示 MMIO 写入值的低 16 位;写入指示拉长便于观察
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
assign led = led_data[15:0];
|
assign led = led_data[15:0];
|
||||||
|
|
||||||
reg [23:0] led_we_cnt;
|
reg [23:0] led_we_cnt;
|
||||||
|
|
||||||
always @(posedge clk_cpu or posedge arst) begin
|
always @(posedge clk_cpu or posedge arst) begin
|
||||||
if (arst)
|
if (arst) led_we_cnt <= 24'd0;
|
||||||
led_we_cnt <= 24'd0;
|
else if (led_valid) led_we_cnt <= 24'hFF_FFFF; // 约 0.34 s @50 MHz
|
||||||
else if (led_valid)
|
else if (led_we_cnt != 24'd0) led_we_cnt <= led_we_cnt - 24'd1;
|
||||||
led_we_cnt <= 24'hFF_FFFF; // 约 0.34 s @50 MHz
|
|
||||||
else if (led_we_cnt != 24'd0)
|
|
||||||
led_we_cnt <= led_we_cnt - 24'd1;
|
|
||||||
end
|
|
||||||
|
|
||||||
assign led_we = (led_we_cnt != 24'd0);
|
|
||||||
|
|
||||||
// ===========================================================================
|
|
||||||
// 扫雷外设:显存 + VGA + 七段数码管 + 鼠标事件源 + 中断连线
|
|
||||||
//
|
|
||||||
// 显存是一块 1200 字节的双口 RAM(40x30 格,每格 1 字节):CPU 通过 MMIO 的
|
|
||||||
// fb_* 写端口写入,VGA 用另一个读端口组合读出。两者同处 clk_cpu 域(VGA 的
|
|
||||||
// 25 MHz 像素节拍由使能信号产生,不是独立时钟),因此没有跨时钟域问题。
|
|
||||||
// ===========================================================================
|
|
||||||
localparam integer FB_WORDS = 300; // 300 字 = 1200 字节
|
|
||||||
|
|
||||||
|
|
||||||
// ---- 显存 ----
|
|
||||||
reg [31:0] fb [0:FB_WORDS-1];
|
|
||||||
integer k;
|
|
||||||
|
|
||||||
initial begin
|
|
||||||
for (k = 0; k < FB_WORDS; k = k + 1)
|
|
||||||
fb[k] = 32'b0;
|
|
||||||
end
|
|
||||||
|
|
||||||
always @(posedge clk_cpu) begin
|
|
||||||
if (fb_we) begin
|
|
||||||
if (fb_wstrb[0])
|
|
||||||
fb[fb_waddr][7:0] <= fb_wdata[7:0];
|
|
||||||
if (fb_wstrb[1])
|
|
||||||
fb[fb_waddr][15:8] <= fb_wdata[15:8];
|
|
||||||
if (fb_wstrb[2])
|
|
||||||
fb[fb_waddr][23:16] <= fb_wdata[23:16];
|
|
||||||
if (fb_wstrb[3])
|
|
||||||
fb[fb_waddr][31:24] <= fb_wdata[31:24];
|
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
wire [31:0] fb_rdata = fb[fb_raddr];
|
|
||||||
|
|
||||||
// ---- VGA 像素节拍:clk_cpu 二分频 => 25 MHz ----
|
|
||||||
reg px_q;
|
|
||||||
always @(posedge clk_cpu or posedge arst) begin
|
|
||||||
if (arst)
|
|
||||||
px_q <= 1'b0;
|
|
||||||
else
|
|
||||||
px_q <= ~px_q;
|
|
||||||
end
|
|
||||||
wire px_tick = ~px_q;
|
|
||||||
|
|
||||||
cpu21_vga u_vga (
|
|
||||||
.clk (clk_cpu),
|
|
||||||
.reset (reset),
|
|
||||||
.tick (px_tick),
|
|
||||||
.fb_raddr_o (fb_raddr),
|
|
||||||
.fb_word_i (fb_rdata),
|
|
||||||
.ctrl_i (disp_ctrl),
|
|
||||||
.cursor_i (disp_cursor),
|
|
||||||
.vga_r (vga_r),
|
|
||||||
.vga_g (vga_g),
|
|
||||||
.vga_b (vga_b),
|
|
||||||
Tcl 一键重建
|
|
||||||
ga_hs (vga_hs),
|
|
||||||
.vga_vs (vga_vs),
|
|
||||||
.frame_tick_o(frame_tick)
|
|
||||||
);
|
|
||||||
|
|
||||||
// ---- 七段数码管(显示项目程序写的 8 位十六进制数)----
|
|
||||||
cpu21_seg7 u_seg7 (
|
|
||||||
.clk (clk_cpu),
|
|
||||||
.reset (reset),
|
|
||||||
.seg_data_i(seg_data),
|
|
||||||
.an_en_i (8'hFF),
|
|
||||||
.an_o (an),
|
|
||||||
.seg_o (seg),
|
|
||||||
.dp_o (dp)
|
|
||||||
);
|
|
||||||
|
|
||||||
// ---- 鼠标事件源 ----
|
|
||||||
wire ev_valid;
|
|
||||||
wire [7:0] ev_dx, ev_dy;
|
|
||||||
wire [4:0] ev_btn;
|
|
||||||
wire [4:0] btn_vec = {go, btnu, btnd, btnl, btnr}; // {BTNC, BTNU, BTND, BTNL, BTNR}
|
|
||||||
|
|
||||||
generate
|
|
||||||
if (USE_PS2_MOUSE != 0) begin : g_ps2
|
|
||||||
cpu21_ps2_mouse u_ps2 (
|
|
||||||
.clk (clk_cpu),
|
|
||||||
.reset (reset),
|
|
||||||
.ps2_clk_i (ps2_clk),
|
|
||||||
.ps2_clk_oe_o (),
|
|
||||||
.ps2_data_i (ps2_data),
|
|
||||||
.ps2_data_oe_o(),
|
|
||||||
.ev_valid_o (ev_valid),
|
|
||||||
.ev_dx_o (ev_dx),
|
|
||||||
.ev_dy_o (ev_dy),
|
|
||||||
.ev_btn_o (ev_btn),
|
|
||||||
.rx_count_o ()
|
|
||||||
)
|
|
||||||
;
|
|
||||||
end
|
|
||||||
else begin : g_btn
|
|
||||||
cpu21_pointer #(
|
|
||||||
.SAMPLE_DIV(50000)
|
|
||||||
) u_ptr (
|
|
||||||
.clk (clk_cpu),
|
|
||||||
.reset (reset),
|
|
||||||
.btn_i (btn_vec),
|
|
||||||
.ev_valid_o(ev_valid),
|
|
||||||
.ev_dx_o (ev_dx),
|
|
||||||
.ev_dy_o (ev_dy),
|
|
||||||
.ev_btn_o (ev_btn)
|
|
||||||
);
|
|
||||||
end
|
|
||||||
endgenerate
|
|
||||||
|
|
||||||
|
assign led_we = (led_we_cnt != 24'd0);
|
||||||
endmodule
|
endmodule
|
||||||
|
|
||||||
`default_nettype wire
|
`default_nettype wire
|
||||||
|
|||||||
File diff suppressed because it is too large
Load Diff
@@ -45,52 +45,3 @@ set_property -dict { PACKAGE_PIN R12 IOSTANDARD LVCMOS33 } [get_ports { led_we
|
|||||||
|
|
||||||
## 上板调试时可把 MMIO 写入的 LED 寄存器也引到 LED16_R/G,
|
## 上板调试时可把 MMIO 写入的 LED 寄存器也引到 LED16_R/G,
|
||||||
## 但默认只用一个引脚,避免与 led_we 争用。
|
## 但默认只用一个引脚,避免与 led_we 争用。
|
||||||
|
|
||||||
## ======================= 扫雷扩展外设 =======================
|
|
||||||
|
|
||||||
## 5 向按键:BTNC 已作 go(同时也当鼠标左键),另外 4 个当光标方向
|
|
||||||
set_property -dict { PACKAGE_PIN M18 IOSTANDARD LVCMOS33 } [get_ports { btnu }]
|
|
||||||
set_property -dict { PACKAGE_PIN P18 IOSTANDARD LVCMOS33 } [get_ports { btnd }]
|
|
||||||
set_property -dict { PACKAGE_PIN P17 IOSTANDARD LVCMOS33 } [get_ports { btnl }]
|
|
||||||
set_property -dict { PACKAGE_PIN M17 IOSTANDARD LVCMOS33 } [get_ports { btnr }]
|
|
||||||
|
|
||||||
## SW[3]:插旗模式(1 = 点击时插旗/取消旗)
|
|
||||||
set_property -dict { PACKAGE_PIN R15 IOSTANDARD LVCMOS33 } [get_ports { sw_flag }]
|
|
||||||
|
|
||||||
## 七段数码管(共阳极:位选/段选都是低有效)
|
|
||||||
set_property -dict { PACKAGE_PIN T10 IOSTANDARD LVCMOS33 } [get_ports { seg[0] }]
|
|
||||||
set_property -dict { PACKAGE_PIN R10 IOSTANDARD LVCMOS33 } [get_ports { seg[1] }]
|
|
||||||
set_property -dict { PACKAGE_PIN K16 IOSTANDARD LVCMOS33 } [get_ports { seg[2] }]
|
|
||||||
set_property -dict { PACKAGE_PIN K13 IOSTANDARD LVCMOS33 } [get_ports { seg[3] }]
|
|
||||||
set_property -dict { PACKAGE_PIN P15 IOSTANDARD LVCMOS33 } [get_ports { seg[4] }]
|
|
||||||
set_property -dict { PACKAGE_PIN T11 IOSTANDARD LVCMOS33 } [get_ports { seg[5] }]
|
|
||||||
set_property -dict { PACKAGE_PIN L18 IOSTANDARD LVCMOS33 } [get_ports { seg[6] }]
|
|
||||||
set_property -dict { PACKAGE_PIN H15 IOSTANDARD LVCMOS33 } [get_ports { dp }]
|
|
||||||
set_property -dict { PACKAGE_PIN J17 IOSTANDARD LVCMOS33 } [get_ports { an[0] }]
|
|
||||||
set_property -dict { PACKAGE_PIN J18 IOSTANDARD LVCMOS33 } [get_ports { an[1] }]
|
|
||||||
set_property -dict { PACKAGE_PIN T9 IOSTANDARD LVCMOS33 } [get_ports { an[2] }]
|
|
||||||
set_property -dict { PACKAGE_PIN J14 IOSTANDARD LVCMOS33 } [get_ports { an[3] }]
|
|
||||||
set_property -dict { PACKAGE_PIN P14 IOSTANDARD LVCMOS33 } [get_ports { an[4] }]
|
|
||||||
set_property -dict { PACKAGE_PIN T14 IOSTANDARD LVCMOS33 } [get_ports { an[5] }]
|
|
||||||
set_property -dict { PACKAGE_PIN K2 IOSTANDARD LVCMOS33 } [get_ports { an[6] }]
|
|
||||||
set_property -dict { PACKAGE_PIN U13 IOSTANDARD LVCMOS33 } [get_ports { an[7] }]
|
|
||||||
|
|
||||||
## VGA(640x480@60,25 MHz 像素节拍由 CPU 时钟使能产生)
|
|
||||||
set_property -dict { PACKAGE_PIN A3 IOSTANDARD LVCMOS33 } [get_ports { vga_r[0] }]
|
|
||||||
set_property -dict { PACKAGE_PIN B4 IOSTANDARD LVCMOS33 } [get_ports { vga_r[1] }]
|
|
||||||
set_property -dict { PACKAGE_PIN C5 IOSTANDARD LVCMOS33 } [get_ports { vga_r[2] }]
|
|
||||||
set_property -dict { PACKAGE_PIN A4 IOSTANDARD LVCMOS33 } [get_ports { vga_r[3] }]
|
|
||||||
set_property -dict { PACKAGE_PIN C6 IOSTANDARD LVCMOS33 } [get_ports { vga_g[0] }]
|
|
||||||
set_property -dict { PACKAGE_PIN A5 IOSTANDARD LVCMOS33 } [get_ports { vga_g[1] }]
|
|
||||||
set_property -dict { PACKAGE_PIN B6 IOSTANDARD LVCMOS33 } [get_ports { vga_g[2] }]
|
|
||||||
set_property -dict { PACKAGE_PIN A6 IOSTANDARD LVCMOS33 } [get_ports { vga_g[3] }]
|
|
||||||
set_property -dict { PACKAGE_PIN B7 IOSTANDARD LVCMOS33 } [get_ports { vga_b[0] }]
|
|
||||||
set_property -dict { PACKAGE_PIN C7 IOSTANDARD LVCMOS33 } [get_ports { vga_b[1] }]
|
|
||||||
set_property -dict { PACKAGE_PIN D7 IOSTANDARD LVCMOS33 } [get_ports { vga_b[2] }]
|
|
||||||
set_property -dict { PACKAGE_PIN D8 IOSTANDARD LVCMOS33 } [get_ports { vga_b[3] }]
|
|
||||||
set_property -dict { PACKAGE_PIN B11 IOSTANDARD LVCMOS33 } [get_ports { vga_hs }]
|
|
||||||
set_property -dict { PACKAGE_PIN B12 IOSTANDARD LVCMOS33 } [get_ports { vga_vs }]
|
|
||||||
|
|
||||||
## PS/2 鼠标(可选:Pmod JA 插 Pmod PS2,顶层 USE_PS2_MOUSE=1)
|
|
||||||
set_property -dict { PACKAGE_PIN C17 IOSTANDARD LVCMOS33 PULLUP true } [get_ports { ps2_clk }]
|
|
||||||
set_property -dict { PACKAGE_PIN D18 IOSTANDARD LVCMOS33 PULLUP true } [get_ports { ps2_data }]
|
|
||||||
|
|||||||
@@ -4,47 +4,19 @@
|
|||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// MMIO:把外设寄存器映射到数据存储器地址空间。
|
// MMIO:把外设寄存器映射到数据存储器地址空间。
|
||||||
//
|
//
|
||||||
// 地址映射(字节地址,都是"字对齐"译码:同一 32 位寄存器内的任意字节偏移都命中,
|
// 目前实现 LED 数据寄存器(LED_ADDR 默认 0x0000_1000,位于 4 KB 数据 RAM 之外):
|
||||||
// 具体改哪几个字节由 wstrb_i 决定,与原来 LED 的语义一致):
|
// * 写:sw / sb 命中该地址(按字对齐,即同一 32 位寄存器内的任意字节偏移
|
||||||
|
// 都算命中)时更新 LED 寄存器,led_valid_o 拉高一拍;sb 依据 wstrb_i 只
|
||||||
|
// 修改对应的字节通道(与 RAM 的字节写语义一致)。
|
||||||
|
// * 读:lw 命中该地址时 rdata_hit_o 拉高,顶层据此选择本模块的 rdata_o
|
||||||
|
// 而不是外部 data_rdata_i,从而支持把 LED 值读回。
|
||||||
//
|
//
|
||||||
// 0x0000_1000 LED 数据寄存器(**保持原有语义不变**,led_valid_o 拉高一拍)
|
// 以后要增加外设(拨码开关、UART 等)时只需在这里扩展地址译码,
|
||||||
// 0x0000_1004 七段数码管:8 位十六进制数(每 4 位一个数码管,低 4 位 = 最右)
|
// 处理器核心与流水线时序不需要改动。
|
||||||
// 0x0000_1100 + 4*k 鼠标寄存器组(见下)
|
|
||||||
// 0x0000_1140 + 4*k 显示控制器寄存器(见下)
|
|
||||||
// 0x0000_2000 - 0x27FF 显存窗口:40x30 格、每格 1 字节(只用到前 1200 字节)
|
|
||||||
//
|
|
||||||
// 鼠标寄存器(基址 0x1100):
|
|
||||||
// +0x00 STATUS RO [0] FIFO 非空 [5:1] 项数 [6] 中断使能 [7] FIFO 满
|
|
||||||
// +0x04 DATA RO [7:0] dx [15:8] dy [20:16] 按键(读无副作用)
|
|
||||||
// +0x08 ACK WO 写任意值 -> 弹出 FIFO 一项
|
|
||||||
// +0x0C CTRL RW [0] 中断使能(复位默认 1)
|
|
||||||
// +0x10 INPUT RO [15:0] 拨码开关 [20:16] 按键(原样)
|
|
||||||
// +0x14 TIME RO 自由运行计数器(随机种子)
|
|
||||||
// +0x18 FRAME RO VGA 帧计数
|
|
||||||
// +0x1C EVCNT RO 累计鼠标事件数
|
|
||||||
// +0x20 LEVEL RW [0] 写 1 -> 拉起 level_irq_o(软件中断,用作"关卡入口");
|
|
||||||
// 关卡程序返回前要写 0 清掉,否则下次没有上升沿
|
|
||||||
// +0x24 MAGIC RO 0xCAFE0002(自检用:读得到说明 MMIO 活着)
|
|
||||||
//
|
|
||||||
// 显示控制器寄存器(基址 0x1140):
|
|
||||||
// +0x00 CTRL RW [0] 光标可见 [6:4] 背景色(3 位调色板)
|
|
||||||
// +0x04 CURSOR RW [5:0] 列(0..39) [13:8] 行(0..29)
|
|
||||||
// +0x08 MAGIC RO 0xCAFE0001
|
|
||||||
//
|
|
||||||
// 显存本体不在本模块里:写操作通过 fb_* 端口直接送到板级顶层的双口 RAM(VGA 读
|
|
||||||
// 同一块 RAM),读操作把 fb_rdata_i 组合回读数据,保持"组合读"的时序不变。
|
|
||||||
//
|
|
||||||
// 两个中断源:
|
|
||||||
// mouse_irq_o = 中断使能 && 事件 FIFO 非空(接 irq_i[0])
|
|
||||||
// level_irq_o = LEVEL 寄存器(软件中断,接 irq_i[1],用来"进入关卡")
|
|
||||||
//
|
|
||||||
// 以后要增加外设(UART、SD 卡等)时只需在这里扩展地址译码。
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
module cpu21_mmio #(
|
module cpu21_mmio #(
|
||||||
parameter [31:0] LED_ADDR = 32'h0000_1000,
|
parameter [31:0] LED_ADDR = 32'h0000_1000
|
||||||
parameter integer FB_WORDS = 300,
|
) (
|
||||||
parameter integer FIFO_DEPTH = 16
|
|
||||||
) (
|
|
||||||
input wire clk,
|
input wire clk,
|
||||||
input wire reset,
|
input wire reset,
|
||||||
// MEM 级的访存请求(来自 EX/MEM 流水寄存器)
|
// MEM 级的访存请求(来自 EX/MEM 流水寄存器)
|
||||||
@@ -57,254 +29,34 @@ module cpu21_mmio #(
|
|||||||
// MMIO 读回
|
// MMIO 读回
|
||||||
output wire [31:0] rdata_o,
|
output wire [31:0] rdata_o,
|
||||||
output wire rdata_hit_o,
|
output wire rdata_hit_o,
|
||||||
// LED 外设(原样保留)
|
// LED 外设
|
||||||
output reg [31:0] led_data_o,
|
output reg [31:0] led_data_o,
|
||||||
output reg led_valid_o,
|
output reg led_valid_o
|
||||||
// 七段数码管:8 位十六进制数
|
);
|
||||||
output reg [31:0] seg_data_o,
|
// 地址译码按“字”对齐:同一 32 位寄存器内的任意字节偏移都算命中,
|
||||||
output reg seg_valid_o,
|
// 具体修改哪几个字节由 wstrb_i 决定(sw 全字、sb 单字节)。
|
||||||
// 显存写端口(板级顶层的双口 RAM)
|
wire led_wr = mem_valid_i && mem_write_i && (addr_i[31:2] == LED_ADDR[31:2]);
|
||||||
output wire fb_we_o,
|
wire led_rd = mem_valid_i && mem_read_i && (addr_i[31:2] == LED_ADDR[31:2]);
|
||||||
output wire [ 9:0] fb_waddr_o,
|
|
||||||
output wire [31:0] fb_wdata_o,
|
|
||||||
output wire [ 3:0] fb_wstrb_o,
|
|
||||||
input wire [31:0] fb_rdata_i,
|
|
||||||
// 显示控制器寄存器(给 VGA 用)
|
|
||||||
output wire [31:0] disp_ctrl_o,
|
|
||||||
output wire [31:0] disp_cursor_o,
|
|
||||||
// 鼠标事件源(来自板级顶层:按键去抖 / PS/2 解析)
|
|
||||||
input wire mouse_ev_valid_i,
|
|
||||||
input wire [ 7:0] mouse_dx_i,
|
|
||||||
input wire [ 7:0] mouse_dy_i,
|
|
||||||
input wire [ 4:0] mouse_btn_i,
|
|
||||||
input wire [15:0] sw_i,
|
|
||||||
input wire frame_tick_i,
|
|
||||||
// 中断
|
|
||||||
output wire mouse_irq_o,
|
|
||||||
output wire level_irq_o
|
|
||||||
);
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 地址译码(都按"字"对齐:同一 32 位寄存器内的任意字节偏移都算命中)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
localparam [31:0] SEG_ADDR = 32'h0000_1004;
|
|
||||||
localparam [31:0] MOUSE_BASE = 32'h0000_1100;
|
|
||||||
localparam [31:0] DISP_BASE = 32'h0000_1140;
|
|
||||||
localparam integer FIFO_AW = 4;
|
|
||||||
localparam [FIFO_AW:0] FIFO_FULL_V = FIFO_DEPTH[FIFO_AW:0];
|
|
||||||
|
|
||||||
wire sel_led = (addr_i[31:2] == LED_ADDR[31:2]);
|
assign rdata_hit_o = led_rd;
|
||||||
wire sel_seg = (addr_i[31:2] == SEG_ADDR[31:2]);
|
assign rdata_o = led_data_o;
|
||||||
wire sel_mouse = (addr_i[31:8] == MOUSE_BASE[31:8]) && (addr_i[7:6] == 2'b00);
|
|
||||||
wire sel_disp = (addr_i[31:8] == DISP_BASE[31:8]) && (addr_i[7:6] == 2'b01);
|
|
||||||
// 显存窗口 0x2000 - 0x27FF(addr[12]=1 且 addr[11]=0)
|
|
||||||
wire sel_fb = (addr_i[31:13] == 19'd0) && addr_i[12] && !addr_i[11];
|
|
||||||
|
|
||||||
wire [3:0] midx = addr_i[5:2];
|
|
||||||
wire [3:0] didx = addr_i[5:2];
|
|
||||||
wire [9:0] fb_widx = addr_i[10:2];
|
|
||||||
wire fb_ok = (fb_widx < FB_WORDS);
|
|
||||||
|
|
||||||
wire led_wr = mem_valid_i && mem_write_i && sel_led;
|
|
||||||
wire led_rd = mem_valid_i && mem_read_i && sel_led;
|
|
||||||
wire seg_wr = mem_valid_i && mem_write_i && sel_seg;
|
|
||||||
wire seg_rd = mem_valid_i && mem_read_i && sel_seg;
|
|
||||||
wire mo_wr = mem_valid_i && mem_write_i && sel_mouse;
|
|
||||||
wire mo_rd = mem_valid_i && mem_read_i && sel_mouse;
|
|
||||||
wire dp_wr = mem_valid_i && mem_write_i && sel_disp;
|
|
||||||
wire dp_rd = mem_valid_i && mem_read_i && sel_disp;
|
|
||||||
wire fb_wr = mem_valid_i && mem_write_i && sel_fb && fb_ok;
|
|
||||||
wire fb_rd = mem_valid_i && mem_read_i && sel_fb && fb_ok;
|
|
||||||
|
|
||||||
assign fb_we_o = fb_wr;
|
|
||||||
assign fb_waddr_o = fb_widx;
|
|
||||||
assign fb_wdata_o = wdata_i;
|
|
||||||
assign fb_wstrb_o = wstrb_i;
|
|
||||||
|
|
||||||
assign rdata_hit_o = led_rd | seg_rd | mo_rd | dp_rd | fb_rd;
|
|
||||||
|
|
||||||
// 字节写合并:sw 改 4 字节、sb 只改 1 字节(与 RAM 的字节写语义一致)
|
|
||||||
function [31:0] merge_bytes;
|
|
||||||
input [31:0] new_v;
|
|
||||||
input [31:0] old_v;
|
|
||||||
input [ 3:0] strb;
|
|
||||||
begin
|
|
||||||
merge_bytes[7:0] = strb[0] ? new_v[7:0] : old_v[7:0];
|
|
||||||
merge_bytes[15:8] = strb[1] ? new_v[15:8] : old_v[15:8];
|
|
||||||
merge_bytes[23:16] = strb[2] ? new_v[23:16] : old_v[23:16];
|
|
||||||
merge_bytes[31:24] = strb[3] ? new_v[31:24] : old_v[31:24];
|
|
||||||
end
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// LED:保持原有语义(led_valid_o 只拉高一拍)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
always @(posedge clk or posedge reset) begin
|
always @(posedge clk or posedge reset) begin
|
||||||
if (reset) begin
|
if (reset) begin
|
||||||
led_data_o <= 32'b0;
|
led_data_o <= 32'b0;
|
||||||
led_valid_o <= 1'b0;
|
led_valid_o <= 1'b0;
|
||||||
end
|
end else begin
|
||||||
else begin
|
// led_valid_o 只拉高一拍,作为“本周期有 LED 写入”的指示。
|
||||||
led_valid_o <= 1'b0;
|
led_valid_o <= 1'b0;
|
||||||
if (led_wr) begin
|
if (led_wr) begin
|
||||||
led_data_o <= merge_bytes(wdata_i, led_data_o, wstrb_i);
|
if (wstrb_i[0]) led_data_o[7:0] <= wdata_i[7:0];
|
||||||
|
if (wstrb_i[1]) led_data_o[15:8] <= wdata_i[15:8];
|
||||||
|
if (wstrb_i[2]) led_data_o[23:16] <= wdata_i[23:16];
|
||||||
|
if (wstrb_i[3]) led_data_o[31:24] <= wdata_i[31:24];
|
||||||
led_valid_o <= 1'b1;
|
led_valid_o <= 1'b1;
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 七段数码管:8 位十六进制数(低 4 位 = 最右边那个数码管)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset) begin
|
|
||||||
seg_data_o <= 32'b0;
|
|
||||||
seg_valid_o <= 1'b0;
|
|
||||||
end
|
|
||||||
else begin
|
|
||||||
seg_valid_o <= 1'b0;
|
|
||||||
if (seg_wr) begin
|
|
||||||
seg_data_o <= merge_bytes(wdata_i, seg_data_o, wstrb_i);
|
|
||||||
seg_valid_o <= 1'b1;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 显示控制器寄存器(显存本体在板级顶层)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [31:0] disp_ctrl_q, disp_cursor_q;
|
|
||||||
|
|
||||||
assign disp_ctrl_o = disp_ctrl_q;
|
|
||||||
assign disp_cursor_o = disp_cursor_q;
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 鼠标:事件 FIFO + 寄存器 + 中断
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [31:0] fifo [0:FIFO_DEPTH-1];
|
|
||||||
reg [FIFO_AW-1:0] wr_ptr_q, rd_ptr_q;
|
|
||||||
reg [FIFO_AW:0] cnt_q;
|
|
||||||
reg [31:0] mctrl_q, time_q, frame_q, evcnt_q;
|
|
||||||
reg level_q;
|
|
||||||
|
|
||||||
wire fifo_empty = (cnt_q == {FIFO_AW+1{1'b0}});
|
|
||||||
wire fifo_full = (cnt_q == FIFO_FULL_V);
|
|
||||||
wire [31:0] fifo_head = fifo[rd_ptr_q];
|
|
||||||
wire irq_en = mctrl_q[0];
|
|
||||||
|
|
||||||
assign mouse_irq_o = irq_en && !fifo_empty;
|
|
||||||
assign level_irq_o = level_q;
|
|
||||||
|
|
||||||
wire push = mouse_ev_valid_i && !fifo_full;
|
|
||||||
wire pop = mo_wr && (midx == 4'd2) && !fifo_empty;
|
|
||||||
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset) begin
|
|
||||||
mctrl_q <= 32'h0000_0001; // 鼠标中断默认打开
|
|
||||||
time_q <= 32'd0;
|
|
||||||
frame_q <= 32'd0;
|
|
||||||
evcnt_q <= 32'd0;
|
|
||||||
level_q <= 1'b0;
|
|
||||||
wr_ptr_q <= {FIFO_AW{1'b0}};
|
|
||||||
rd_ptr_q <= {FIFO_AW{1'b0}};
|
|
||||||
cnt_q <= {(FIFO_AW+1){1'b0}};
|
|
||||||
disp_ctrl_q <= 32'h0000_0001; // 光标可见,背景色 0
|
|
||||||
disp_cursor_q <= 32'h0000_0000;
|
|
||||||
end
|
|
||||||
else begin
|
|
||||||
time_q <= time_q + 32'd1;
|
|
||||||
if (frame_tick_i)
|
|
||||||
frame_q <= frame_q + 32'd1;
|
|
||||||
|
|
||||||
if (push) begin
|
|
||||||
fifo[wr_ptr_q] <= {11'b0, mouse_btn_i, mouse_dy_i, mouse_dx_i};
|
|
||||||
wr_ptr_q <= wr_ptr_q + 1'b1;
|
|
||||||
evcnt_q <= evcnt_q + 32'd1;
|
|
||||||
end
|
|
||||||
if (pop)
|
|
||||||
rd_ptr_q <= rd_ptr_q + 1'b1;
|
|
||||||
cnt_q <= cnt_q + (push ? 1'b1 : 1'b0) - (pop ? 1'b1 : 1'b0);
|
|
||||||
|
|
||||||
// 鼠标寄存器:CTRL(3) / LEVEL(8)
|
|
||||||
if (mo_wr) begin
|
|
||||||
case (midx)
|
|
||||||
4'd3:
|
|
||||||
mctrl_q <= merge_bytes(wdata_i, mctrl_q, wstrb_i);
|
|
||||||
4'd8:
|
|
||||||
level_q <= wdata_i[0];
|
|
||||||
default:
|
|
||||||
;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
|
|
||||||
// 显示控制器:CTRL(0) / CURSOR(1)
|
|
||||||
if (dp_wr) begin
|
|
||||||
case (didx)
|
|
||||||
4'd0:
|
|
||||||
disp_ctrl_q <= merge_bytes(wdata_i, disp_ctrl_q, wstrb_i);
|
|
||||||
4'd1:
|
|
||||||
disp_cursor_q <= merge_bytes(wdata_i, disp_cursor_q, wstrb_i);
|
|
||||||
default:
|
|
||||||
;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 读数据(组合,与"指令存储器组合读/数据存储器组合读"的时序一致)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [31:0] rdata_mux;
|
|
||||||
|
|
||||||
always @* begin
|
|
||||||
rdata_mux = 32'b0;
|
|
||||||
if (led_rd) begin
|
|
||||||
rdata_mux = led_data_o;
|
|
||||||
end
|
|
||||||
else if (seg_rd) begin
|
|
||||||
rdata_mux = seg_data_o;
|
|
||||||
end
|
|
||||||
else if (fb_rd) begin
|
|
||||||
rdata_mux = fb_rdata_i;
|
|
||||||
end
|
|
||||||
else if (mo_rd) begin
|
|
||||||
case (midx)
|
|
||||||
4'd0:
|
|
||||||
rdata_mux = {24'b0, fifo_full, irq_en, cnt_q[4:0], !fifo_empty};
|
|
||||||
4'd1:
|
|
||||||
rdata_mux = fifo_head;
|
|
||||||
4'd3:
|
|
||||||
rdata_mux = mctrl_q;
|
|
||||||
4'd4:
|
|
||||||
rdata_mux = {11'b0, mouse_btn_i, sw_i};
|
|
||||||
4'd5:
|
|
||||||
rdata_mux = time_q;
|
|
||||||
4'd6:
|
|
||||||
rdata_mux = frame_q;
|
|
||||||
4'd7:
|
|
||||||
rdata_mux = evcnt_q;
|
|
||||||
4'd8:
|
|
||||||
rdata_mux = {31'b0, level_q};
|
|
||||||
4'd9:
|
|
||||||
rdata_mux = 32'hCAFE_0002;
|
|
||||||
default:
|
|
||||||
rdata_mux = 32'b0;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
else if (dp_rd) begin
|
|
||||||
case (didx)
|
|
||||||
4'd0:
|
|
||||||
rdata_mux = disp_ctrl_q;
|
|
||||||
4'd1:
|
|
||||||
rdata_mux = disp_cursor_q;
|
|
||||||
4'd2:
|
|
||||||
rdata_mux = 32'hCAFE_0001;
|
|
||||||
default:
|
|
||||||
rdata_mux = 32'b0;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
assign rdata_o = rdata_mux;
|
|
||||||
endmodule
|
endmodule
|
||||||
|
|
||||||
`default_nettype wire
|
`default_nettype wire
|
||||||
|
|||||||
@@ -1,106 +0,0 @@
|
|||||||
`timescale 1ns / 1ps
|
|
||||||
`default_nettype none
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// cpu21_pointer —— 把板上的 5 向按键变成"鼠标"事件源(板级顶层用它给 MMIO 送事件)。
|
|
||||||
//
|
|
||||||
// 事件单位:1/8 格。按键一次 = 8 个单位 = 光标移动一格,这样软件端用
|
|
||||||
// 纯整数加减(不需要任何除法)就能把累积量换算成格,配合 PS/2 鼠标时
|
|
||||||
// 手感也一致(PS/2 的原始计数直接按 1/8 格处理)。
|
|
||||||
//
|
|
||||||
// BTNU -> dy = -8 BTND -> dy = +8
|
|
||||||
// BTNL -> dx = -8 BTNR -> dx = +8
|
|
||||||
// BTNC -> 左键按下(btn[0] = 1,dx = dy = 0)
|
|
||||||
//
|
|
||||||
// 去抖方式:用 ~1 ms 的采样节拍(50 MHz 下计数 50000)采样两级同步后的按键,
|
|
||||||
// 与上一次的稳定值比较,只在"按下"的边沿产生事件。一次采样可能同时检测到
|
|
||||||
// 多个按下,此时用 press_pend_q 排队,由优先级选择器逐个上报(每个时钟周期
|
|
||||||
// 最多一个事件),因此不会丢按键。
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
module cpu21_pointer #(
|
|
||||||
// 采样节拍 = clk / (SAMPLE_DIV),默认 50 MHz / 50000 = 1 kHz
|
|
||||||
parameter integer SAMPLE_DIV = 50000
|
|
||||||
) (
|
|
||||||
input wire clk,
|
|
||||||
input wire reset,
|
|
||||||
input wire [4:0] btn_i, // {BTNC, BTNU, BTND, BTNL, BTNR}
|
|
||||||
output wire ev_valid_o, // 单周期脉冲
|
|
||||||
output wire [7:0] ev_dx_o, // 有符号,1/8 格
|
|
||||||
output wire [7:0] ev_dy_o,
|
|
||||||
output wire [4:0] ev_btn_o
|
|
||||||
);
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 1 ms 采样节拍
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [15:0] div_q;
|
|
||||||
wire sample = (div_q == (SAMPLE_DIV - 1));
|
|
||||||
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset)
|
|
||||||
div_q <= 16'd0;
|
|
||||||
else if (sample)
|
|
||||||
div_q <= 16'd0;
|
|
||||||
else
|
|
||||||
div_q <= div_q + 16'd1;
|
|
||||||
end
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 两级同步 + 采样保持(采样率远低于按键抖动频率,等效去抖)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [4:0] btn_sync1_q, btn_sync2_q, btn_stable_q;
|
|
||||||
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset) begin
|
|
||||||
btn_sync1_q <= 5'b0;
|
|
||||||
btn_sync2_q <= 5'b0;
|
|
||||||
btn_stable_q <= 5'b0;
|
|
||||||
end
|
|
||||||
else begin
|
|
||||||
btn_sync1_q <= btn_i;
|
|
||||||
btn_sync2_q <= btn_sync1_q;
|
|
||||||
if (sample)
|
|
||||||
btn_stable_q <= btn_sync2_q;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
// 只在按下的边沿产生新事件
|
|
||||||
wire [4:0] press_new = sample ? (btn_sync2_q & ~btn_stable_q) : 5'b0;
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 待上报队列 + 优先级选择(每周期最多上报一个)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [4:0] press_pend_q;
|
|
||||||
wire [4:0] pend_next = press_pend_q | press_new;
|
|
||||||
|
|
||||||
reg [4:0] sel;
|
|
||||||
always @* begin
|
|
||||||
sel = 5'b0;
|
|
||||||
if (pend_next[4])
|
|
||||||
sel = 5'b10000; // BTNC:点击
|
|
||||||
else if (pend_next[3])
|
|
||||||
sel = 5'b01000; // BTNU:上
|
|
||||||
else if (pend_next[2])
|
|
||||||
sel = 5'b00010; // BTND:下
|
|
||||||
else if (pend_next[1])
|
|
||||||
sel = 5'b00001; // BTNL:左
|
|
||||||
else if (pend_next[0])
|
|
||||||
sel = 5'b00100; // BTNR:右
|
|
||||||
end
|
|
||||||
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset)
|
|
||||||
press_pend_q <= 5'b0;
|
|
||||||
else
|
|
||||||
press_pend_q <= pend_next & ~sel;
|
|
||||||
end
|
|
||||||
|
|
||||||
assign ev_valid_o = |pend_next;
|
|
||||||
assign ev_dx_o = sel[1] ? 8'd248 : (sel[0] ? 8'd8 : 8'd0); // 248 = -8 的补码
|
|
||||||
assign ev_dy_o = sel[3] ? 8'd248 : (sel[2] ? 8'd8 : 8'd0);
|
|
||||||
assign ev_btn_o = {4'b0, sel[4]};
|
|
||||||
|
|
||||||
endmodule
|
|
||||||
|
|
||||||
`default_nettype wire
|
|
||||||
Tcl 一键重建
|
|
||||||
@@ -1,145 +0,0 @@
|
|||||||
`timescale 1ns / 1ps
|
|
||||||
`default_nettype none
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// cpu21_ps2_mouse —— 可选的真鼠标输入:PS/2 鼠标(Digilent Pmod PS2 插在 JA 上)。
|
|
||||||
//
|
|
||||||
// JA[1] = PS/2 DATA,JA[2] = PS/2 CLK(两条线都需要上拉,见 XDC 里的 PULLUP)。
|
|
||||||
//
|
|
||||||
// 注意:本模块没有在真实硬件上验证过(作者手头没有 PS/2 鼠标),如果你不用真
|
|
||||||
// 鼠标,请让顶层用 cpu21_pointer(5 向按键)作为事件源即可,C 程序完全不用改。
|
|
||||||
// 板级顶层 cpu21_board_top 用参数 USE_PS2_MOUSE 选择这两者。
|
|
||||||
//
|
|
||||||
// 实现内容:
|
|
||||||
// 1) 接收:在 ps2_clk 的下降沿采样 ps2_data,收满 11 位(起始 0 + 8 数据
|
|
||||||
// LSB 先 + 奇校验 + 停止 1)后校验并拼成 3 字节一包;
|
|
||||||
// 2) 解析:byte0 = 标志(bit0 左键、bit1 右键、bit3 固定 1),byte1 = dx,
|
|
||||||
// byte2 = dy,bit7 为符号位;
|
|
||||||
// 3) 上报:当 dx/dy 非 0 或按键状态变化时产生一个事件(单位与
|
|
||||||
// cpu21_pointer 相同,即原始计数 = 1/8 格);
|
|
||||||
// 4) 本模块只做"接收",不实现主机→鼠标的命令发送(0xF4 使能上报)。多数
|
|
||||||
// PS/2 鼠标上电后就会主动上报,这类鼠标可以直接用;如果你的鼠标上电毫无
|
|
||||||
// 反应,说明它需要主机先发 0xF4 —— 那部分需要自行补上,或者直接用默认的
|
|
||||||
// 5 向按键(cpu21_pointer),C 程序完全不用改。
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
module cpu21_ps2_mouse (
|
|
||||||
input wire clk,
|
|
||||||
input wire reset,
|
|
||||||
// PS/2 双向线(开漏)
|
|
||||||
input wire ps2_clk_i,
|
|
||||||
output wire ps2_clk_oe_o,
|
|
||||||
input wire ps2_data_i,
|
|
||||||
output wire ps2_data_oe_o,
|
|
||||||
// 事件上报
|
|
||||||
output reg ev_valid_o,
|
|
||||||
output reg [7:0] ev_dx_o,
|
|
||||||
output reg [7:0] ev_dy_o,
|
|
||||||
output reg [4:0] ev_btn_o,
|
|
||||||
// 调试:收到的字节数(可选观察)
|
|
||||||
output reg [15:0] rx_count_o
|
|
||||||
);
|
|
||||||
|
|
||||||
// 开漏:oe = 1 时把线拉低,否则高阻(外部上拉)
|
|
||||||
assign ps2_clk_oe_o = 1'b0; // 本设计只接收,不主动拉低时钟(除了初始化)
|
|
||||||
assign ps2_data_oe_o = init_oe;
|
|
||||||
。本模块只接收,两条线都不驱动。
|
|
||||||
assign ps2_clk_oe_o = 1'b0;
|
|
||||||
assign ps2_data_oe_o = 1'b02_data_s1, ps2_data_s2;
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset) begin
|
|
||||||
ps2_clk_s1 <= 1'b1;
|
|
||||||
ps2_clk_s2 <= 1'b1;
|
|
||||||
ps2_data_s1 <= 1'b1;
|
|
||||||
ps2_data_s2 <= 1'b1;
|
|
||||||
end
|
|
||||||
else begin
|
|
||||||
ps2_clk_s1 <= ps2_clk_i;
|
|
||||||
ps2_clk_s2 <= ps2_clk_s1;
|
|
||||||
ps2_data_s1 <= ps2_data_i;
|
|
||||||
ps2_data_s2 <= ps2_data_s1;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
wire clk_fall = ~ps2_clk_s1 & ps2_clk_s2; // 同步后的下降沿
|
|
||||||
wire clk_rise = ps2_clk_s1 & ~ps2_clk_s2;
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 接收移位:11 位一帧,bit0 = 起始位,bit1..8 = 数据,bit9 = 校验,bit10 = 停止
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [3:0] bit_cnt_q;
|
|
||||||
reg [10:0] frame_q;
|
|
||||||
reg frame_ok_q;
|
|
||||||
reg [23:0] pkt_q; // 3 字节包
|
|
||||||
reg [1:0] pkt_cnt_q;
|
|
||||||
|
|
||||||
reg [7:0] byte_cnt_q; // 一包内的字节序号(0..2)
|
|
||||||
reg [7:0] dx_q, dy_q;
|
|
||||||
reg [4:0] btn_q; // {4'b0, middle, right, left} -> 只取低 3 位使用
|
|
||||||
|
|
||||||
// 校验:11 位中 1 的个数应为奇数(奇校验)
|
|
||||||
function automatic odd_parity_ok(input [10:0] f);
|
|
||||||
odd_parity_ok = ^{f[9:1]}; // data + parity 共 9 位,1 的个数应为偶数
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset) begin
|
|
||||||
bit_cnt_q <= 4'd0;
|
|
||||||
frame_q <= 11'd0;
|
|
||||||
frame_ok_q <= 1'b0;
|
|
||||||
byte_cnt_q <= 8'd0;
|
|
||||||
pkt_q <= 24'd0;
|
|
||||||
rx_count_o <= 16'd0;
|
|
||||||
ev_valid_o <= 1'b0;
|
|
||||||
end
|
|
||||||
else begin
|
|
||||||
ev_valid_o <= 1'b0;
|
|
||||||
|
|
||||||
if (clk_fall) begin
|
|
||||||
if (bit_cnt_q == 4'd0 && ps2_data_s2 == 1'b0) begin
|
|
||||||
// 起始位有效,开始收
|
|
||||||
frame_q <= {ps2_data_s2, frame_q[10:1]};
|
|
||||||
bit_cnt_q <= 4'd1;
|
|
||||||
end
|
|
||||||
else if (bit_cnt_q != 4'd0) begin
|
|
||||||
frame_q <= {ps2_data_s2, frame_q[10:1]};
|
|
||||||
if (bit_cnt_q == 4'd10) begin
|
|
||||||
bit_cnt_q <= 4'd0;
|
|
||||||
// f = {停止, 校验, 8 数据, 起始},这里只关心数据与校验
|
|
||||||
if (odd_parity_ok(frame_q) && (frame_q[10] == 1'b1)) begin
|
|
||||||
rx_count_o <= rx_count_o + 16'd1;
|
|
||||||
case (byte_cnt_q)
|
|
||||||
8'd0: begin
|
|
||||||
// 标志字节:bit3 必须为 1(否则是鼠标发来的其他响应,丢弃)
|
|
||||||
if (frame_q[8:1][3] == 1'b1) begin
|
|
||||||
pkt_q[7:0] <= frame_q[8:1];
|
|
||||||
byte_cnt_q <= 8'd1;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
8'd1: begin
|
|
||||||
pkt_q[15:8] <= frame_q[8:1]; // dx
|
|
||||||
byte_cnt_q <= 8'd2;
|
|
||||||
end
|
|
||||||
8'd2: begin
|
|
||||||
byte_cnt_q <= 8'd0;
|
|
||||||
ev_dx_o <= frame_q[8:1]; // dy
|
|
||||||
ev_dy_o <= pkt_q[15:8]; // 注意:byte1 是 dx
|
|
||||||
ev_btn_o <= {2'b0, pkt_q[2], pkt_q[1]}; // {middle, right, left}
|
|
||||||
ev_valid_o <= 1'b1;
|
|
||||||
end
|
|
||||||
default:
|
|
||||||
byte_cnt_q <= 8'd0;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
end
|
|
||||||
else begin
|
|
||||||
bit_cnt_q <= bit_cnt_q + 4'd1;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
endmodule
|
|
||||||
|
|
||||||
`default_nettype wire
|
|
||||||
Tcl 一键重建
|
|
||||||
@@ -49,7 +49,7 @@ module cpu21_riscv_redirect_int_bpb #(
|
|||||||
parameter [31:0] LED_ADDR = 32'h0000_1000,
|
parameter [31:0] LED_ADDR = 32'h0000_1000,
|
||||||
// ustatus 复位值。详见 cpu21_riscv_irq_ctrl 中的说明。
|
// ustatus 复位值。详见 cpu21_riscv_irq_ctrl 中的说明。
|
||||||
parameter [31:0] USTATUS_INIT = 32'h0000_0001
|
parameter [31:0] USTATUS_INIT = 32'h0000_0001
|
||||||
) (
|
) (
|
||||||
input wire clk,
|
input wire clk,
|
||||||
input wire reset,
|
input wire reset,
|
||||||
input wire [31:0] instr_i,
|
input wire [31:0] instr_i,
|
||||||
@@ -65,24 +65,6 @@ module cpu21_riscv_redirect_int_bpb #(
|
|||||||
|
|
||||||
output wire [31:0] led_data_o,
|
output wire [31:0] led_data_o,
|
||||||
output wire led_valid_o,
|
output wire led_valid_o,
|
||||||
// ---- 扫雷扩展外设(由 cpu21_mmio 驱动;板级顶层接 VGA/数码管/鼠标)----
|
|
||||||
output wire [31:0] seg_data_o, // 七段数码管:8 位十六进制数
|
|
||||||
output wire seg_valid_o,
|
|
||||||
output wire fb_we_o, // 显存写端口(板级顶层的双口 RAM)
|
|
||||||
output wire [ 9:0] fb_waddr_o, // 显存字地址(0..299)
|
|
||||||
output wire [31:0] fb_wdata_o,
|
|
||||||
output wire [ 3:0] fb_wstrb_o,
|
|
||||||
input wire [31:0] fb_rdata_i, // 显存读回(组合)
|
|
||||||
output wire [31:0] disp_ctrl_o, // 显示控制器寄存器(给 VGA)
|
|
||||||
output wire [31:0] disp_cursor_o,
|
|
||||||
input wire mouse_ev_valid_i, // 鼠标事件源(板级顶层:按键去抖 / PS/2)
|
|
||||||
input wire [ 7:0] mouse_dx_i, // 单位 1/8 格,有符号
|
|
||||||
input wire [ 7:0] mouse_dy_i,
|
|
||||||
input wire [ 4:0] mouse_btn_i, // {BTNC,BTNU,BTND,BTNL,BTNR}
|
|
||||||
input wire [15:0] sw_i, // 板载拨码(软件读 INPUT 寄存器)
|
|
||||||
input wire frame_tick_i, // VGA 每帧一个脉冲
|
|
||||||
output wire mouse_irq_o, // 鼠标事件中断(板级顶层接 irq_i[0])
|
|
||||||
output wire level_irq_o, // 软件中断:关卡入口(接 irq_i[1])
|
|
||||||
output wire [31:0] if_pc_o,
|
output wire [31:0] if_pc_o,
|
||||||
output wire [31:0] id_pc_o,
|
output wire [31:0] id_pc_o,
|
||||||
output wire [31:0] ex_pc_o,
|
output wire [31:0] ex_pc_o,
|
||||||
@@ -114,7 +96,7 @@ module cpu21_riscv_redirect_int_bpb #(
|
|||||||
output wire [15:0] unconditional_branch_count_o,
|
output wire [15:0] unconditional_branch_count_o,
|
||||||
output wire [15:0] prediction_success_count_o,
|
output wire [15:0] prediction_success_count_o,
|
||||||
output wire [15:0] prediction_failure_count_o
|
output wire [15:0] prediction_failure_count_o
|
||||||
);
|
);
|
||||||
// 顶层只用到少量常量,完整译码表见 cpu21_riscv_decoder.v。
|
// 顶层只用到少量常量,完整译码表见 cpu21_riscv_decoder.v。
|
||||||
localparam OP_BRANCH = 5'h18;
|
localparam OP_BRANCH = 5'h18;
|
||||||
localparam ALU_ADD = 4'd5;
|
localparam ALU_ADD = 4'd5;
|
||||||
@@ -491,30 +473,8 @@ module cpu21_riscv_redirect_int_bpb #(
|
|||||||
.wstrb_i (exmem_wstrb_q),
|
.wstrb_i (exmem_wstrb_q),
|
||||||
.rdata_o (mmio_rdata),
|
.rdata_o (mmio_rdata),
|
||||||
.rdata_hit_o(mmio_rdata_hit),
|
.rdata_hit_o(mmio_rdata_hit),
|
||||||
// LED(原样)
|
|
||||||
.led_data_o (led_data_o),
|
.led_data_o (led_data_o),
|
||||||
.led_valid_o(led_valid_o),
|
.led_valid_o(led_valid_o)
|
||||||
// 七段数码管
|
|
||||||
.seg_data_o (seg_data_o),
|
|
||||||
.seg_valid_o(seg_valid_o),
|
|
||||||
// 显存(本体在板级顶层)
|
|
||||||
.fb_we_o (fb_we_o),
|
|
||||||
.fb_waddr_o (fb_waddr_o),
|
|
||||||
.fb_wdata_o (fb_wdata_o),
|
|
||||||
.fb_wstrb_o (fb_wstrb_o),
|
|
||||||
.fb_rdata_i (fb_rdata_i),
|
|
||||||
// 显示控制器
|
|
||||||
.disp_ctrl_o (disp_ctrl_o),
|
|
||||||
.disp_cursor_o(disp_cursor_o),
|
|
||||||
// 鼠标事件源与中断
|
|
||||||
.mouse_ev_valid_i(mouse_ev_valid_i),
|
|
||||||
.mouse_dx_i (mouse_dx_i),
|
|
||||||
.mouse_dy_i (mouse_dy_i),
|
|
||||||
.mouse_btn_i (mouse_btn_i),
|
|
||||||
.sw_i (sw_i),
|
|
||||||
.frame_tick_i (frame_tick_i),
|
|
||||||
.mouse_irq_o (mouse_irq_o),
|
|
||||||
.level_irq_o (level_irq_o)
|
|
||||||
);
|
);
|
||||||
|
|
||||||
// 访存读数据:命中 MMIO 时用外设数据,否则用外部存储器数据。
|
// 访存读数据:命中 MMIO 时用外设数据,否则用外部存储器数据。
|
||||||
@@ -712,12 +672,9 @@ module cpu21_riscv_redirect_int_bpb #(
|
|||||||
memwb_wb_sel_q <= 3'b0;
|
memwb_wb_sel_q <= 3'b0;
|
||||||
|
|
||||||
halted_q <= 1'b0;
|
halted_q <= 1'b0;
|
||||||
end
|
end else begin
|
||||||
else begin
|
if (go_i) halted_q <= 1'b0;
|
||||||
if (go_i)
|
if (ex_halt_event) halted_q <= 1'b1;
|
||||||
halted_q <= 1'b0;
|
|
||||||
if (ex_halt_event)
|
|
||||||
halted_q <= 1'b1;
|
|
||||||
|
|
||||||
// MEM/WB 流水寄存器捕获。
|
// MEM/WB 流水寄存器捕获。
|
||||||
memwb_valid_q <= exmem_valid_q;
|
memwb_valid_q <= exmem_valid_q;
|
||||||
@@ -748,8 +705,7 @@ module cpu21_riscv_redirect_int_bpb #(
|
|||||||
exmem_mem_write_q <= 1'b0;
|
exmem_mem_write_q <= 1'b0;
|
||||||
exmem_wstrb_q <= 4'b0;
|
exmem_wstrb_q <= 4'b0;
|
||||||
exmem_wb_sel_q <= 3'b0;
|
exmem_wb_sel_q <= 3'b0;
|
||||||
end
|
end else begin
|
||||||
else begin
|
|
||||||
exmem_valid_q <= idex_valid_q;
|
exmem_valid_q <= idex_valid_q;
|
||||||
exmem_pc_q <= idex_pc_q;
|
exmem_pc_q <= idex_pc_q;
|
||||||
exmem_ir_q <= idex_ir_q;
|
exmem_ir_q <= idex_ir_q;
|
||||||
@@ -812,13 +768,11 @@ module cpu21_riscv_redirect_int_bpb #(
|
|||||||
|
|
||||||
if (halted_q || ex_md_stall) begin
|
if (halted_q || ex_md_stall) begin
|
||||||
// 停机 / 乘除法迭代:valid 保持不变(指令继续占用 EX)。
|
// 停机 / 乘除法迭代:valid 保持不变(指令继续占用 EX)。
|
||||||
end
|
end else if (pipeline_stall || flush_younger) begin
|
||||||
else if (pipeline_stall || flush_younger) begin
|
|
||||||
// load-use 停顿:load 已经进 EX/MEM,这里给 EX 装一个气泡(payload 保持
|
// load-use 停顿:load 已经进 EX/MEM,这里给 EX 装一个气泡(payload 保持
|
||||||
// 原值,valid=0 时无害);冲刷同理。
|
// 原值,valid=0 时无害);冲刷同理。
|
||||||
idex_valid_q <= 1'b0;
|
idex_valid_q <= 1'b0;
|
||||||
end
|
end else begin
|
||||||
else begin
|
|
||||||
idex_valid_q <= ifid_valid_q;
|
idex_valid_q <= ifid_valid_q;
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -827,38 +781,32 @@ module cpu21_riscv_redirect_int_bpb #(
|
|||||||
ifid_valid_q <= 1'b0;
|
ifid_valid_q <= 1'b0;
|
||||||
ifid_pred_taken_q <= 1'b0;
|
ifid_pred_taken_q <= 1'b0;
|
||||||
ifid_pred_target_q <= 32'b0;
|
ifid_pred_target_q <= 32'b0;
|
||||||
end
|
end else if (ex_uret_redirect) begin
|
||||||
else if (ex_uret_redirect) begin
|
|
||||||
pc_q <= ex_return_pc;
|
pc_q <= ex_return_pc;
|
||||||
ifid_valid_q <= 1'b0;
|
ifid_valid_q <= 1'b0;
|
||||||
ifid_pred_taken_q <= 1'b0;
|
ifid_pred_taken_q <= 1'b0;
|
||||||
ifid_pred_target_q <= 32'b0;
|
ifid_pred_target_q <= 32'b0;
|
||||||
end
|
end else if (ex_halt_event) begin
|
||||||
else if (ex_halt_event) begin
|
|
||||||
pc_q <= idex_pc_q + 32'd4;
|
pc_q <= idex_pc_q + 32'd4;
|
||||||
ifid_valid_q <= 1'b0;
|
ifid_valid_q <= 1'b0;
|
||||||
ifid_pred_taken_q <= 1'b0;
|
ifid_pred_taken_q <= 1'b0;
|
||||||
ifid_pred_target_q <= 32'b0;
|
ifid_pred_target_q <= 32'b0;
|
||||||
end
|
end else if (ex_redirect_valid) begin
|
||||||
else if (ex_redirect_valid) begin
|
|
||||||
pc_q <= ex_redirect_pc;
|
pc_q <= ex_redirect_pc;
|
||||||
ifid_valid_q <= 1'b0;
|
ifid_valid_q <= 1'b0;
|
||||||
ifid_pred_taken_q <= 1'b0;
|
ifid_pred_taken_q <= 1'b0;
|
||||||
ifid_pred_target_q <= 32'b0;
|
ifid_pred_target_q <= 32'b0;
|
||||||
end
|
end else if (pipeline_stall || ex_md_stall) begin
|
||||||
else if (pipeline_stall || ex_md_stall) begin
|
|
||||||
pc_q <= pc_q;
|
pc_q <= pc_q;
|
||||||
ifid_valid_q <= ifid_valid_q;
|
ifid_valid_q <= ifid_valid_q;
|
||||||
ifid_pc_q <= ifid_pc_q;
|
ifid_pc_q <= ifid_pc_q;
|
||||||
ifid_ir_q <= ifid_ir_q;
|
ifid_ir_q <= ifid_ir_q;
|
||||||
ifid_pred_taken_q <= ifid_pred_taken_q;
|
ifid_pred_taken_q <= ifid_pred_taken_q;
|
||||||
ifid_pred_target_q <= ifid_pred_target_q;
|
ifid_pred_target_q <= ifid_pred_target_q;
|
||||||
end
|
end else if (halted_q) begin
|
||||||
else if (halted_q) begin
|
|
||||||
pc_q <= pc_q;
|
pc_q <= pc_q;
|
||||||
ifid_valid_q <= 1'b0;
|
ifid_valid_q <= 1'b0;
|
||||||
end
|
end else begin
|
||||||
else begin
|
|
||||||
pc_q <= fetch_next_pc;
|
pc_q <= fetch_next_pc;
|
||||||
ifid_valid_q <= 1'b1;
|
ifid_valid_q <= 1'b1;
|
||||||
ifid_pc_q <= pc_q;
|
ifid_pc_q <= pc_q;
|
||||||
|
|||||||
@@ -1,129 +0,0 @@
|
|||||||
`timescale 1ns / 1ps
|
|
||||||
`default_nettype none
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// cpu21_seg7 —— Nexys4DDR 板载 8 位数码管的动态扫描驱动。
|
|
||||||
//
|
|
||||||
// 输入:seg_data_i 每 4 位一个十六进制数字(低 4 位 = 最右边那个数码管),
|
|
||||||
// an_en_i 每一位对应一个数码管的使能(1 = 显示,0 = 熄灭)。
|
|
||||||
// 输出:an_o[7:0] 位选(低有效)、seg_o[6:0] 段选 a..g(低有效)、dp_o 小数点。
|
|
||||||
//
|
|
||||||
// 扫描频率:CLK_DIV 分频后约 1 kHz 切换一位(8 位 => 每位数码管 125 Hz 刷新)。
|
|
||||||
// 板载数码管是共阳极:位选拉低选中,段选拉低点亮。
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
module cpu21_seg7 #(
|
|
||||||
parameter integer REFRESH_DIV = 50000 // 50 MHz / 50000 = 1 kHz
|
|
||||||
) (
|
|
||||||
input wire clk,
|
|
||||||
input wire reset,
|
|
||||||
input wire [31:0] seg_data_i, // 8 个十六进制数字
|
|
||||||
input wire [ 7:0] an_en_i, // 每位是否显示
|
|
||||||
output reg [ 7:0] an_o, // 位选,低有效
|
|
||||||
output reg [ 6:0] seg_o, // 段选 a..g,低有效
|
|
||||||
output reg dp_o // 小数点,低有效
|
|
||||||
);
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 刷新计数与当前位
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [15:0] div_q;
|
|
||||||
reg [2:0] digit_q;
|
|
||||||
wire tick = (div_q == (REFRESH_DIV - 1));
|
|
||||||
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset) begin
|
|
||||||
div_q <= 16'd0;
|
|
||||||
digit_q <= 3'd0;
|
|
||||||
end
|
|
||||||
else if (tick) begin
|
|
||||||
div_q <= 16'd0;
|
|
||||||
digit_q <= digit_q + 3'd1;
|
|
||||||
end
|
|
||||||
else begin
|
|
||||||
div_q <= div_q + 16'd1;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
// 当前要显示的十六进制数字
|
|
||||||
reg [3:0] nib;
|
|
||||||
always @* begin
|
|
||||||
case (digit_q)
|
|
||||||
3'd0:
|
|
||||||
nib = seg_data_i[3:0];
|
|
||||||
3'd1:
|
|
||||||
nib = seg_data_i[7:4];
|
|
||||||
3'd2:
|
|
||||||
nib = seg_data_i[11:8];
|
|
||||||
3'd3:
|
|
||||||
nib = seg_data_i[15:12];
|
|
||||||
3'd4:
|
|
||||||
nib = seg_data_i[19:16];
|
|
||||||
3'd5:
|
|
||||||
nib = seg_data_i[23:20];
|
|
||||||
3'd6:
|
|
||||||
nib = seg_data_i[27:24];
|
|
||||||
default:
|
|
||||||
nib = seg_data_i[31:28];
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 十六进制 -> 七段码(bit0=a ... bit6=g,1 表示该段点亮)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [6:0] seg_on;
|
|
||||||
always @* begin
|
|
||||||
case (nib)
|
|
||||||
4'h0:
|
|
||||||
seg_on = 7'b0111111;
|
|
||||||
4'h1:
|
|
||||||
seg_on = 7'b0000110;
|
|
||||||
4'h2:
|
|
||||||
seg_on = 7'b1011011;
|
|
||||||
4'h3:
|
|
||||||
seg_on = 7'b1001111;
|
|
||||||
4'h4:
|
|
||||||
seg_on = 7'b1100110;
|
|
||||||
4'h5:
|
|
||||||
seg_on = 7'b1101101;
|
|
||||||
4'h6:
|
|
||||||
seg_on = 7'b1111101;
|
|
||||||
4'h7:
|
|
||||||
seg_on = 7'b0000111;
|
|
||||||
4'h8:
|
|
||||||
seg_on = 7'b1111111;
|
|
||||||
4'h9:
|
|
||||||
seg_on = 7'b1101111;
|
|
||||||
4'hA:
|
|
||||||
seg_on = 7'b1110111;
|
|
||||||
4'hB:
|
|
||||||
seg_on = 7'b1111100;
|
|
||||||
4'hC:
|
|
||||||
seg_on = 7'b0111001;
|
|
||||||
4'hD:
|
|
||||||
seg_on = 7'b1011110;
|
|
||||||
4'hE:
|
|
||||||
seg_on = 7'b1111001;
|
|
||||||
default:
|
|
||||||
seg_on = 7'b1110001; // F
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 位选与段选输出(共阳极 => 低有效)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset) begin
|
|
||||||
an_o <= 8'hFF;
|
|
||||||
seg_o <= 7'h7F;
|
|
||||||
dp_o <= 1'b1;
|
|
||||||
end
|
|
||||||
else if (tick) begin
|
|
||||||
an_o <= ~(8'b0000_0001 << digit_q) | ~an_en_i;
|
|
||||||
seg_o <= ~seg_on;
|
|
||||||
dp_o <= 1'b1; // 不用小数点
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
endmodule
|
|
||||||
|
|
||||||
`default_nettype wire
|
|
||||||
@@ -1,242 +0,0 @@
|
|||||||
`timescale 1ns / 1ps
|
|
||||||
`default_nettype none
|
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
// cpu21_vga —— 640x480@60 的"格子"显示控制器(显存端口接板级顶层的双口 RAM)。
|
|
||||||
//
|
|
||||||
// CPU 不直接操作像素:它只通过 MMIO 往显示缓冲区里写"每格 1 字节"(字形 + 颜色),
|
|
||||||
// 本模块按像素扫描把格子展开成画面(8x8 字模放大 2 倍填满 16x16 的格子)。
|
|
||||||
// 这样一格只要 1 次字节写,刷屏开销极小。
|
|
||||||
//
|
|
||||||
// 网格:40 列 x 30 行,每格 16x16 像素 = 640x480。
|
|
||||||
//
|
|
||||||
// 时钟:本模块工作在 CPU 时钟(clk)下,用 tick(每 2 个 clk 一个使能)产生
|
|
||||||
// 25 MHz 的像素节拍。**不引入第二个时钟域**,因此显示缓冲区的读(本模块)
|
|
||||||
// 与写(MMIO,同一时钟)之间没有跨时钟域问题。
|
|
||||||
//
|
|
||||||
// 光标:由 CURSOR 寄存器的 (x,y) 决定,在对应格子上画一个白色闪动的方框
|
|
||||||
// (CTRL[0] 控制显示与否)。缓冲区字节的 bit7 在本模块中不使用(保留)。
|
|
||||||
// -----------------------------------------------------------------------------
|
|
||||||
module cpu21_vga #(
|
|
||||||
parameter integer H_VISIBLE = 640,
|
|
||||||
parameter integer H_FP = 16,
|
|
||||||
parameter integer H_SYNC = 96,
|
|
||||||
parameter integer H_BP = 48,
|
|
||||||
parameter integer V_VISIBLE = 480,
|
|
||||||
parameter integer V_FP = 10,
|
|
||||||
parameter integer V_SYNC = 2,
|
|
||||||
parameter integer V_BP = 33,
|
|
||||||
parameter integer COLS = 40,
|
|
||||||
parameter integer ROWS = 30
|
|
||||||
) (
|
|
||||||
input wire clk,
|
|
||||||
input wire reset,
|
|
||||||
input wire tick, // 25 MHz 像素使能(每 2 个 clk 一拍)
|
|
||||||
// 显示缓冲区读端口
|
|
||||||
output wire [ 9:0] fb_raddr_o,
|
|
||||||
input wire [31:0] fb_word_i,
|
|
||||||
// 控制
|
|
||||||
input wire [31:0] ctrl_i,
|
|
||||||
input wire [31:0] cursor_i,
|
|
||||||
// VGA 输出
|
|
||||||
output reg [ 3:0] vga_r,
|
|
||||||
output reg [ 3:0] vga_g,
|
|
||||||
output reg [ 3:0] vga_b,
|
|
||||||
output reg vga_hs,
|
|
||||||
output reg vga_vs,
|
|
||||||
output reg frame_tick_o
|
|
||||||
);
|
|
||||||
|
|
||||||
localparam integer H_TOTAL = H_VISIBLE + H_FP + H_SYNC + H_BP; // 800
|
|
||||||
localparam integer V_TOTAL = V_VISIBLE + V_FP + V_SYNC + V_BP; // 525
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 扫描计数
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [9:0] hx, vy;
|
|
||||||
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset) begin
|
|
||||||
hx <= 10'd0;
|
|
||||||
vy <= 10'd0;
|
|
||||||
end
|
|
||||||
else if (tick) begin
|
|
||||||
if (hx == H_TOTAL - 1) begin
|
|
||||||
hx <= 10'd0;
|
|
||||||
vy <= (vy == V_TOTAL - 1) ? 10'd0 : (vy + 10'd1);
|
|
||||||
end
|
|
||||||
else begin
|
|
||||||
hx <= hx + 10'd1;
|
|
||||||
end
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
wire de = (hx < H_VISIBLE) && (vy < V_VISIBLE);
|
|
||||||
|
|
||||||
// 同步信号在 tick 上寄存输出
|
|
||||||
wire hs_n = ~((hx >= (H_VISIBLE + H_FP)) && (hx < (H_VISIBLE + H_FP + H_SYNC)));
|
|
||||||
wire vs_n = ~((vy >= (V_VISIBLE + V_FP)) && (vy < (V_VISIBLE + V_FP + V_SYNC)));
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 格子索引与缓冲区读出
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
wire [5:0] ccol = hx[9:4]; // 0..39
|
|
||||||
wire [4:0] crow = vy[8:4]; // 0..29
|
|
||||||
wire [10:0] cidx = (crow << 5) + (crow << 3) + ccol; // row*40 + col
|
|
||||||
|
|
||||||
assign fb_raddr_o = cidx[10:2]; // 字地址(1 字 = 4 格)
|
|
||||||
|
|
||||||
wire [1:0] lane = cidx[1:0];
|
|
||||||
wire [31:0] cell_word = fb_word_i;
|
|
||||||
wire [7:0] cell_byte = cell_word >> {lane, 3'b000};
|
|
||||||
|
|
||||||
wire [3:0] glyph = cell_byte[3:0];
|
|
||||||
wire [2:0] gcolor = cell_byte[6:4];
|
|
||||||
// 全局背景色(字模点为 0 时显示的颜色)
|
|
||||||
wire [2:0] bgcolor = ctrl_i[7:4];
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 8x8 字模(bit7 = 最左像素),放大 2 倍填满 16x16 的格子
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
wire [2:0] fx = hx[3:1];
|
|
||||||
wire [2:0] fy = vy[3:1];
|
|
||||||
|
|
||||||
function [7:0] font_row;
|
|
||||||
input [3:0] g;
|
|
||||||
input [2:0] r;
|
|
||||||
begin
|
|
||||||
case (g)
|
|
||||||
4'd0:
|
|
||||||
font_row = 8'h00; // 空白
|
|
||||||
4'd1:
|
|
||||||
font_row = (r == 3'd0) ? 8'h18 : (r == 3'd1) ? 8'h38 : (r == 3'd2) ? 8'h18 :
|
|
||||||
(r == 3'd3) ? 8'h18 : (r == 3'd4) ? 8'h18 : (r == 3'd5) ? 8'h18 :
|
|
||||||
(r == 3'd6) ? 8'h7E : 8'h00; // 1
|
|
||||||
4'd2:
|
|
||||||
font_row = (r == 3'd0) ? 8'h3C : (r == 3'd1) ? 8'h66 : (r == 3'd2) ? 8'h06 :
|
|
||||||
(r == 3'd3) ? 8'h0C : (r == 3'd4) ? 8'h18 : (r == 3'd5) ? 8'h30 :
|
|
||||||
(r == 3'd6) ? 8'h7E : 8'h00; // 2
|
|
||||||
4'd3:
|
|
||||||
font_row = (r == 3'd0) ? 8'h3C : (r == 3'd1) ? 8'h66 : (r == 3'd2) ? 8'h06 :
|
|
||||||
(r == 3'd3) ? 8'h1C : (r == 3'd4) ? 8'h06 : (r == 3'd5) ? 8'h66 :
|
|
||||||
(r == 3'd6) ? 8'h3C : 8'h00; // 3
|
|
||||||
4'd4:
|
|
||||||
font_row = (r == 3'd0) ? 8'h0C : (r == 3'd1) ? 8'h1C : (r == 3'd2) ? 8'h3C :
|
|
||||||
(r == 3'd3) ? 8'h6C : (r == 3'd4) ? 8'h7E : (r == 3'd5) ? 8'h0C :
|
|
||||||
(r == 3'd6) ? 8'h0C : 8'h00; // 4
|
|
||||||
4'd5:
|
|
||||||
font_row = (r == 3'd0) ? 8'h7E : (r == 3'd1) ? 8'h60 : (r == 3'd2) ? 8'h7C :
|
|
||||||
(r == 3'd3) ? 8'h06 : (r == 3'd4) ? 8'h06 : (r == 3'd5) ? 8'h66 :
|
|
||||||
(r == 3'd6) ? 8'h3C : 8'h00; // 5
|
|
||||||
4'd6:
|
|
||||||
font_row = (r == 3'd0) ? 8'h1C : (r == 3'd1) ? 8'h30 : (r == 3'd2) ? 8'h60 :
|
|
||||||
(r == 3'd3) ? 8'h7C : (r == 3'd4) ? 8'h66 : (r == 3'd5) ? 8'h66 :
|
|
||||||
(r == 3'd6) ? 8'h3C : 8'h00; // 6
|
|
||||||
4'd7:
|
|
||||||
font_row = (r == 3'd0) ? 8'h7E : (r == 3'd1) ? 8'h66 : (r == 3'd2) ? 8'h0C :
|
|
||||||
(r == 3'd3) ? 8'h18 : (r == 3'd4) ? 8'h18 : (r == 3'd5) ? 8'h18 :
|
|
||||||
(r == 3'd6) ? 8'h18 : 8'h00; // 7
|
|
||||||
4'd8:
|
|
||||||
font_row = (r == 3'd0) ? 8'h3C : (r == 3'd1) ? 8'h66 : (r == 3'd2) ? 8'h66 :
|
|
||||||
(r == 3'd3) ? 8'h3C : (r == 3'd4) ? 8'h66 : (r == 3'd5) ? 8'h66 :
|
|
||||||
(r == 3'd6) ? 8'h3C : 8'h00; // 8
|
|
||||||
4'd9:
|
|
||||||
font_row = (r == 3'd0) ? 8'hFF : (r == 3'd1) ? 8'h81 : (r == 3'd2) ? 8'hBD :
|
|
||||||
(r == 3'd3) ? 8'hA5 : (r == 3'd4) ? 8'hA5 : (r == 3'd5) ? 8'hBD :
|
|
||||||
(r == 3'd6) ? 8'h81 : 8'hFF; // 未揭开的方块
|
|
||||||
4'd10:
|
|
||||||
font_row = (r == 3'd0) ? 8'h40 : (r == 3'd1) ? 8'h70 : (r == 3'd2) ? 8'h78 :
|
|
||||||
(r == 3'd3) ? 8'h70 : (r == 3'd4) ? 8'h40 : (r == 3'd5) ? 8'h40 :
|
|
||||||
(r == 3'd6) ? 8'h40 : 8'h00; // 旗
|
|
||||||
4'd11:
|
|
||||||
font_row = (r == 3'd0) ? 8'h3C : (r == 3'd1) ? 8'h7E : (r == 3'd2) ? 8'hFF :
|
|
||||||
(r == 3'd3) ? 8'hFF : (r == 3'd4) ? 8'hFF : (r == 3'd5) ? 8'hFF :
|
|
||||||
(r == 3'd6) ? 8'h7E : 8'h3C; // 雷
|
|
||||||
4'd12:
|
|
||||||
font_row = (r == 3'd0) ? 8'h81 : (r == 3'd1) ? 8'h42 : (r == 3'd2) ? 8'h24 :
|
|
||||||
(r == 3'd3) ? 8'h18 : (r == 3'd4) ? 8'h18 : (r == 3'd5) ? 8'h24 :
|
|
||||||
(r == 3'd6) ? 8'h42 : 8'h81; // 爆炸
|
|
||||||
4'd13:
|
|
||||||
font_row = (r == 3'd0) ? 8'h3C : (r == 3'd1) ? 8'h66 : (r == 3'd2) ? 8'h06 :
|
|
||||||
(r == 3'd3) ? 8'h0C : (r == 3'd4) ? 8'h18 : (r == 3'd5) ? 8'h00 :
|
|
||||||
(r == 3'd6) ? 8'h18 : 8'h00; // 问号
|
|
||||||
4'd14:
|
|
||||||
font_row = (r == 3'd3) ? 8'hFF : (r == 3'd4) ? 8'hFF : 8'h00; // 横线
|
|
||||||
4'd15:
|
|
||||||
font_row = 8'hFF; // 实心
|
|
||||||
default:
|
|
||||||
font_row = 8'h00;
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
wire [7:0] frow = font_row(glyph, fy);
|
|
||||||
wire glyph_px = frow[3'd7 - fx]; // 1 = 画字模点
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 光标框(闪动)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
reg [24:0] blink_q;
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset)
|
|
||||||
blink_q <= 25'd0;
|
|
||||||
else if (tick)
|
|
||||||
blink_q <= blink_q + 25'd1;
|
|
||||||
end
|
|
||||||
wire blink = blink_q[24];
|
|
||||||
|
|
||||||
wire cursor_here = (cursor_i[4:0] == ccol) && (cursor_i[12:8] == crow);
|
|
||||||
wire on_border = (hx[3:0] == 4'd0) || (hx[3:0] == 4'd15) ||
|
|
||||||
(vy[3:0] == 4'd0) || (vy[3:0] == 4'd15);
|
|
||||||
wire cursor_box = cursor_here && blink && ctrl_i[0] && on_border;
|
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
// 颜色(3 位调色板 -> 4:4:4 RGB)
|
|
||||||
// ---------------------------------------------------------------------------
|
|
||||||
function [11:0] pal;
|
|
||||||
input [2:0] c;
|
|
||||||
begin
|
|
||||||
case (c)
|
|
||||||
3'd0:
|
|
||||||
pal = {4'h0, 4'h0, 4'h0}; // 黑
|
|
||||||
3'd1:
|
|
||||||
pal = {4'h0, 4'h0, 4'hA}; // 深蓝
|
|
||||||
3'd2:
|
|
||||||
pal = {4'h0, 4'hA, 4'h0}; // 绿
|
|
||||||
3'd3:
|
|
||||||
pal = {4'h0, 4'hA, 4'hA}; // 青
|
|
||||||
3'd4:
|
|
||||||
pal = {4'hA, 4'h0, 4'h0}; // 红
|
|
||||||
3'd5:
|
|
||||||
pal = {4'hA, 4'h0, 4'hA}; // 洋红
|
|
||||||
3'd6:
|
|
||||||
pal = {4'hA, 4'hA, 4'h0}; // 黄
|
|
||||||
default:
|
|
||||||
pal = {4'hA, 4'hA, 4'hA}; // 浅灰
|
|
||||||
endcase
|
|
||||||
end
|
|
||||||
endfunction
|
|
||||||
|
|
||||||
wire [2:0] px_color = glyph_px ? gcolor : bgcolor;
|
|
||||||
wire [2:0] px_final = cursor_box ? 3'd7 : px_color;
|
|
||||||
wire [11:0] px_rgb = de ? pal(px_final) : 12'h000;
|
|
||||||
|
|
||||||
always @(posedge clk or posedge reset) begin
|
|
||||||
if (reset) begin
|
|
||||||
{vga_r, vga_g, vga_b} <= 12'h000;
|
|
||||||
vga_hs <= 1'b1;
|
|
||||||
vga_vs <= 1'b1;
|
|
||||||
frame_tick_o <= 1'b0;
|
|
||||||
end
|
|
||||||
else if (tick) begin
|
|
||||||
{vga_r, vga_g, vga_b} <= px_rgb;
|
|
||||||
vga_hs <= hs_n;
|
|
||||||
vga_vs <= vs_n;
|
|
||||||
frame_tick_o <= (hx == H_TOTAL - 1) && (vy == V_TOTAL - 1);
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
endmodule
|
|
||||||
|
|
||||||
`default_nettype wire
|
|
||||||
Tcl 一键重建
|
|
||||||
@@ -30,24 +30,7 @@ module tb_board_top;
|
|||||||
.go (go),
|
.go (go),
|
||||||
.irq_sw(irq_sw),
|
.irq_sw(irq_sw),
|
||||||
.led (led),
|
.led (led),
|
||||||
.led_we(led_we),
|
.led_we(led_we)
|
||||||
// 扫雷扩展端口:本测试平台不复位测试这些功能,输入显式接 0/1,
|
|
||||||
// 避免悬空(z)传到中断控制器里影响跑马灯的比对结果。
|
|
||||||
.btnu (1'b0),
|
|
||||||
.btnd (1'b0),
|
|
||||||
.btnl (1'b0),
|
|
||||||
.btnr (1'b0),
|
|
||||||
.sw_flag (1'b0),
|
|
||||||
.ps2_clk (1'b1),
|
|
||||||
.ps2_data(1'b1),
|
|
||||||
.an (),
|
|
||||||
.seg (),
|
|
||||||
.dp (),
|
|
||||||
.vga_r (),
|
|
||||||
.vga_g (),
|
|
||||||
.vga_b (),
|
|
||||||
.vga_hs (),
|
|
||||||
.vga_vs ()
|
|
||||||
);
|
);
|
||||||
|
|
||||||
integer changes = 0;
|
integer changes = 0;
|
||||||
|
|||||||
@@ -1,184 +0,0 @@
|
|||||||
# 扫雷(RISC-V / cpu21)— 纯 C + MMIO 显示 + 中断读鼠标
|
|
||||||
|
|
||||||
给 `pipelined-cpu` 里那颗 CPU(`cpu21_riscv_redirect_int_bpb`)写的扫雷。
|
|
||||||
**游戏逻辑和三个中断入口都由纯 C 写成**,用 RISC-V 工具链编译成机器码后固化进片内 ROM 运行。
|
|
||||||
|
|
||||||
```
|
|
||||||
saolei/
|
|
||||||
├─ sw/
|
|
||||||
│ ├─ mmio.h 全部 MMIO 地址与格子编码(换外设地址只改这一个文件)
|
|
||||||
│ ├─ saolei.c 纯 C 扫雷:背景界面(main) + 关卡(level_main) + 棋盘与绘制
|
|
||||||
│ ├─ isr.c **中断读鼠标的代码**:三个固定地址入口 + 纯 C 服务主体 isr_body
|
|
||||||
│ ├─ start.c C 启动(设 sp、清 .bss、进 main)——只有这一处必需的内联汇编
|
|
||||||
│ └─ linker.ld ROM/RAM 布局:复位 0x0、中断入口 0x100/0x200/0x300、栈顶 0x0FFC
|
|
||||||
└─ tools/
|
|
||||||
├─ build.ps1 一键构建:C → .o → ELF → .hex + cpu21_saolei_rom.vh
|
|
||||||
├─ check_isa.py 逐条反汇编检查:有没有用到这颗核没实现的指令
|
|
||||||
└─ elf2hex.py 裸二进制 → $readmemh hex + ROM 初始化头文件
|
|
||||||
```
|
|
||||||
|
|
||||||
配套硬件在 Vivado 工程里(不在本目录):`srcs/rtl/cpu21_mmio.v`(外设中枢)、
|
|
||||||
`cpu21_vga.v`(显示)、`cpu21_seg7.v`(数码管)、`cpu21_pointer.v`(按键当鼠标)、
|
|
||||||
`cpu21_ps2_mouse.v`(可选真鼠标)、`srcs/board/cpu21_board_top.v`(上板顶层)。
|
|
||||||
|
|
||||||
---
|
|
||||||
|
|
||||||
## 1. 程序结构:背景"伪操作系统" + 关卡 + 鼠标,正好占三个中断
|
|
||||||
|
|
||||||
CPU 复位后从 `0x0` 开始跑 `main()`(**背景程序**,从头到尾一直运行),它画开始界面
|
|
||||||
并等鼠标。整套交互是这样串起来的:
|
|
||||||
|
|
||||||
```mermaid
|
|
||||||
sequenceDiagram
|
|
||||||
participant BG as 背景 main()(0x0 起,一直跑)
|
|
||||||
participant LV as 关卡 level_main()(IRQ1 = 0x100)
|
|
||||||
participant MS as 鼠标 ISR(IRQ2 = 0x200)
|
|
||||||
BG->>BG: 画开始界面(标题 + 绿色"开始"按钮 + 光标框)
|
|
||||||
BG->>BG: 点中"开始"按钮 → 写 LEVEL 寄存器 = 1
|
|
||||||
Note over BG: 硬件拉起 irq_i[1],CPU 跳到 0x100
|
|
||||||
BG->>LV: isr1_entry:保存现场 + 打开 MIE → level_main()
|
|
||||||
LV->>LV: 初始化关卡(布雷、画盘面)
|
|
||||||
MS-->>LV: 鼠标事件中断(IRQ2 优先级更高,嵌套进来)
|
|
||||||
LV->>LV: 轮询事件队列:移动光标 / 揭开 / 插旗
|
|
||||||
LV->>BG: 点左上角黄格 = 返回 → 清 LEVEL + uret
|
|
||||||
Note over BG: 回到被打断的那条指令,继续等下一次"开始"
|
|
||||||
```
|
|
||||||
|
|
||||||
| 入口 | 地址 | 作用 | 对应 RTL 参数 |
|
|
||||||
| ---- | ------- | ----------------------------------- | ------------- |
|
|
||||||
| 复位 | `0x000` | `_start`:设栈、进 `main` | `RESET_PC` |
|
|
||||||
| IRQ1 | `0x100` | **关卡(游戏)程序** `level_main()` | `IRQ1_VECTOR` |
|
|
||||||
| IRQ2 | `0x200` | **鼠标读取** `isr_body()` | `IRQ2_VECTOR` |
|
|
||||||
| IRQ3 | `0x300` | 备用(只 `uret`) | `IRQ3_VECTOR` |
|
|
||||||
|
|
||||||
**地址是链接脚本定死的**(`linker.ld` 里 `.text.isr1 0x100 : {...}`),所以先定地址、
|
|
||||||
再设 RTL 参数,两边成对;不存在"编完才知道地址"的问题。优先级 IRQ3 > IRQ2 > IRQ1,
|
|
||||||
所以"关卡跑在 IRQ1、鼠标跑在 IRQ2"天然允许鼠标嵌套打断关卡。
|
|
||||||
|
|
||||||
中断硬件行为(见 `cpu21_riscv_irq_ctrl.v`):`irq_i` 上升沿置挂起位;响应时硬件自动清
|
|
||||||
挂起位、清 `ustatus[0]`(MIE)、把返回地址压进内部 EPC 栈;返回用 **`uret`**;**硬件不保存
|
|
||||||
任何通用寄存器**。因此:
|
|
||||||
|
|
||||||
- `isr1_entry`(游戏体量大会调函数)保存**全部调用者保存寄存器**,并用
|
|
||||||
`csrrsi x0,0x004,1` 打开 MIE 让鼠标中断能嵌套;
|
|
||||||
- `isr2_entry` 只存 `ra`,C 主体 `isr_body` 用 `-ffixed-x1,x5..x7,x10..x17,x28..x31`
|
|
||||||
编译,逼 GCC 只用 `s0-s11`(callee-saved,函数自己会存/恢复)和 `sp` —— 这样纯 C
|
|
||||||
主体不会破坏被打断的代码;
|
|
||||||
- 事件流:鼠标/按键 → RTL 事件 FIFO(深 16)→ 中断 → `isr_body` 搬进软件环形队列 →
|
|
||||||
主循环 `handle_event()` 处理。FIFO 读空后中断线自动回 0,下一个事件重新产生上升沿,
|
|
||||||
所以不丢事件;主循环只能轮询(这颗核没有 `wfi`)。
|
|
||||||
|
|
||||||
## 2. 针对这颗 CPU 的硬约束(改 C 代码时别踩)
|
|
||||||
|
|
||||||
`cpu21_riscv_decoder.v` 实现的是 RV32I 的一个子集:
|
|
||||||
|
|
||||||
| 类别 | 有 | **没有** |
|
|
||||||
| ------ | -------------------------------------------- | ---------------------------------- |
|
|
||||||
| R 型 | add sub sll slt sltu xor srl sra or and | — |
|
|
||||||
| M 扩展 | mul mulh mulhsu mulhu **divu remu** | **div / rem(有符号)** |
|
|
||||||
| I 型 | addi slti sltiu xori ori andi slli srli srai | — |
|
|
||||||
| 访存 | **lw / sw / sb** | **lb / lh / lbu / lhu / sh** |
|
|
||||||
| 高位 | lui | **auipc** |
|
|
||||||
| 控制流 | 6 条分支 + jal + jalr | — |
|
|
||||||
| 系统 | ecall uret csrrw csrrsi csrrci | mret / sret / fence / ebreak / wfi |
|
|
||||||
|
|
||||||
对应做法:不按字节*读*内存(**写**显存用 `sb` 可以)、不做有符号除法、索引运算全用
|
|
||||||
2 的幂 + 移位/掩码、**不能有 `.data`/`.rodata`**(数据空间读不到 ROM,所以没有带初值的
|
|
||||||
全局变量、没有字符串/常量表);字模放在 RTL 里,不在软件里。
|
|
||||||
|
|
||||||
## 3. 构建(已实测通过)
|
|
||||||
|
|
||||||
工具链:xPack `riscv-none-elf-gcc`(本机装在
|
|
||||||
`C:\Users\29653\tools\riscv\xpack-riscv-none-elf-gcc-15.2.0-1\bin`,已加入用户 PATH)。
|
|
||||||
|
|
||||||
```powershell
|
|
||||||
cd <仓库>\saolei
|
|
||||||
powershell -ExecutionPolicy Bypass -File tools\build.ps1
|
|
||||||
# 若没配 PATH:-Prefix 'C:\Users\29653\tools\riscv\xpack-riscv-none-elf-gcc-15.2.0-1\bin\riscv-none-elf-'
|
|
||||||
```
|
|
||||||
|
|
||||||
| 步骤 | 说明 |
|
|
||||||
| ------------ | -------------------------------------------------------------------------------------------- |
|
|
||||||
| 编译 | `start.c`/`saolei.c` 正常编;`isr.c` 额外加 `-ffixed-x...` |
|
|
||||||
| 链接 | `-T sw/linker.ld`;脚本里的 `ASSERT` 会拦住 `.data`/`.rodata` |
|
|
||||||
| **反汇编** | `build/saolei.lst` ← 这就是"编译出来的汇编",可以直接读 |
|
|
||||||
| **ISA 检查** | `tools/check_isa.py` 逐条比对指令白名单,出现 `auipc`/`lb`/`lbu`/`div`… 直接报错并给修复提示 |
|
|
||||||
| 转格式 | `build/saolei.hex`(`$readmemh`)+ `pipelined-cpu/srcs/board/cpu21_saolei_rom.vh` |
|
|
||||||
|
|
||||||
当前实测结果:`text=3368 B, data=0, bss=1436 B`,**842 条指令全部通过 ISA 检查**,
|
|
||||||
入口地址 `_start=0x0`、`isr1_entry=0x100`、`isr2_entry=0x200`、`isr3_entry=0x300`。
|
|
||||||
|
|
||||||
编译参数里有三处是这颗核/工具链的坑,已经写在脚本注释里:
|
|
||||||
|
|
||||||
1. `-march=rv32im_zicsr` —— GCC 15 起 `rv32im` 不含 Zicsr,`csrrsi` 会报
|
|
||||||
`unrecognized opcode ... extension 'zicsr' required`;
|
|
||||||
2. **不要禁链接器松弛**(别加 `-mno-relax`/`-Wl,--no-relax`)—— gcc 的函数调用默认是
|
|
||||||
`auipc ra,%pcrel_hi` + `jalr`,要靠链接器松弛压成一条 `jal`;禁掉后 35 条 `auipc`
|
|
||||||
会留在代码里(这颗核没有)。用 `-msmall-data-limit=0` 保证松弛不会引入 gp 相对寻址;
|
|
||||||
3. 中断入口地址必须写在**段声明**上(`.text.isr1 0x100 : {...} > ROM`)——
|
|
||||||
只写 `. = 0x100;` 会被 `ld` 忽略(实测 isr1 掉到了 0x14)。
|
|
||||||
|
|
||||||
## 4. MMIO 地址表(与 `cpu21_mmio.v` 的译码一致)
|
|
||||||
|
|
||||||
| 地址 | 外设 |
|
|
||||||
| -------------------- | ------------------------------------------------------------------------------- |
|
|
||||||
| `0x0000_1000` | LED 数据寄存器(低 16 位点亮板载 LED) |
|
|
||||||
| `0x0000_1004` | 七段数码管:8 位十六进制数(低 4 位 = 最右) |
|
|
||||||
| `0x0000_1100+` | 鼠标:STATUS / DATA / ACK / CTRL / INPUT / TIME / FRAME / EVCNT / LEVEL / MAGIC |
|
|
||||||
| `0x0000_1140+` | 显示控制器:CTRL(光标可见、背景色)/ CURSOR(列、行)/ MAGIC |
|
|
||||||
| `0x0000_2000-0x27FF` | 显存:40 列 × 30 行、每格 1 字节(只用到前 1200 字节) |
|
|
||||||
|
|
||||||
`LEVEL` 是**软件中断**寄存器:写 1 拉起 `irq_i[1]`(关卡入口),关卡返回前写 0 清掉,
|
|
||||||
否则下次点"开始"没有新的上升沿。`MAGIC`(`0xCAFE0001/2`)可以读来自检 MMIO 是否活着。
|
|
||||||
|
|
||||||
格子编码 1 字节 = `[3:0] 字形 [6:4] 颜色 [7] 保留`;字形 0 空白、1..8 数字、9 未揭开、
|
|
||||||
10 旗、11 雷、12 爆炸、13 问号、14 横线、15 实心;颜色是 3 位调色板。
|
|
||||||
|
|
||||||
## 5. 怎么跑起来
|
|
||||||
|
|
||||||
**仿真**(用你现成的测试平台):
|
|
||||||
|
|
||||||
```powershell
|
|
||||||
copy build\saolei.hex <仓库>\pipelined-cpu\srcs\rom\saolei.hex
|
|
||||||
# 把 srcs/tb/tb_no_intr.v 顶部的 ROM_FILE 改成 "saolei.hex"
|
|
||||||
```
|
|
||||||
|
|
||||||
**上板**:在 Vivado 里给综合/仿真**定义宏 `SAOLEI_ROM`**(Settings → Verilog Macros),
|
|
||||||
`cpu21_board_top.v` 就会加载 `cpu21_saolei_rom.vh`;不定义仍是原来的跑马灯程序。
|
|
||||||
接 VGA 显示器,按 `CPU_RESETN` 复位 → 出现开始界面(标题 + 绿色按钮 + 闪动光标框)。
|
|
||||||
|
|
||||||
**操作**(默认用板上 5 向按键):
|
|
||||||
|
|
||||||
| 操作 | 按键 |
|
|
||||||
| ------------------------------------ | --------------------------------------- |
|
|
||||||
| 移动光标 | `BTNU` / `BTND` / `BTNL` / `BTNR` |
|
|
||||||
| 左键(揭开;在开始界面上 = 开始) | `BTNC` |
|
|
||||||
| 插旗模式(拨上去后左键 = 插/取消旗) | `SW0` 或 `SW3` |
|
|
||||||
| 返回上一层(关卡内) | 把光标移到盘面**左上角黄格**再按 `BTNC` |
|
|
||||||
|
|
||||||
**鼠标**:默认按键;想用真鼠标就把顶层参数 `USE_PS2_MOUSE` 设为 1(需要 Digilent
|
|
||||||
Pmod PS2 插在 JA + 一个 PS/2 鼠标)。`cpu21_ps2_mouse.v` 只实现了**接收**(发 0xF4
|
|
||||||
初始化要靠鼠标产生的时钟脉冲,不是固定延时能糊的,所以没写假的);多数鼠标上电即上报,
|
|
||||||
需要 0xF4 的那种得自己补。USB 鼠标在这个核上不现实(协议栈放不下)。C 程序两种方式
|
|
||||||
完全不用改。
|
|
||||||
|
|
||||||
## 6. 验证状态(说实话)
|
|
||||||
|
|
||||||
| 部分 | 状态 |
|
|
||||||
| ------------------------------------------------------------------------------------ | ----------------------------------------------------------------- |
|
|
||||||
| C 工具链链路 | **已实测**:编译/链接/ISA 检查/hex 生成/入口地址逐条核对 全部通过 |
|
|
||||||
| `saolei.c` / `isr.c` / `start.c` / `linker.ld` | 逻辑对着 RTL 写;**未在仿真器/硬件上跑过** |
|
|
||||||
| `cpu21_mmio.v` 扩展、`cpu21_vga.v`、`cpu21_seg7.v`、`cpu21_pointer.v`、板级顶层、XDC | **未综合、未上板** |
|
|
||||||
|
|
||||||
建议验证顺序:① 跑 `pipelined-cpu/tools/run_regress.ps1` 确认原来的跑马灯/中断/指令
|
|
||||||
测试仍全 PASS;② 定义 `SAOLEI_ROM` 跑行为仿真,看有没有往 `0x2000` 起的显存写字节、
|
|
||||||
按键后有没有跳进 `0x200`;③ 再上板。
|
|
||||||
|
|
||||||
## 7. 已知可改进项
|
|
||||||
|
|
||||||
- **屏幕上没有字母**:字模是 4 位字形(0-15),所以"开始"是绿色按钮条 + 光标框而不是
|
|
||||||
"START" 字样。要显示文字就把格子编码扩成 5 位字形(`mmio.h` 的 `CELL()` 与
|
|
||||||
`cpu21_vga.v` 的字模表各改一处,颜色降到 3 位)。
|
|
||||||
- 棋盘固定 16×16、40 雷(16 是 2 的幂,索引全用移位,一次除法都不用);要改大小就改
|
|
||||||
`saolei.c` 顶部的 `BW/BH/NMINES`。
|
|
||||||
- `run_regress.ps1` 里还没有扫雷的自动测试项。
|
|
||||||
-129
@@ -1,129 +0,0 @@
|
|||||||
/* -----------------------------------------------------------------------------
|
|
||||||
* isr.c —— 中断服务程序:从 MMIO 读鼠标事件(**这就是"中断读鼠标操作的指令"**)
|
|
||||||
*
|
|
||||||
* 这颗核的中断硬件行为(见 srcs/rtl/cpu21_riscv_irq_ctrl.v):
|
|
||||||
* * irq_i 上升沿 -> 挂起位置 1;响应中断时硬件自动清挂起位、清 ustatus[0](MIE)、
|
|
||||||
* 把返回地址压进内部 EPC 栈;
|
|
||||||
* * 从 uepc 返回用 `uret`(编码 0x00200073,不在标准 C 里,只能内联汇编);
|
|
||||||
* * **硬件不保存任何通用寄存器**,所以软件必须自己保证不破坏被打断的代码。
|
|
||||||
*
|
|
||||||
* 因此这里分了三个固定地址的入口 + 一个纯 C 主体:
|
|
||||||
* 1) isr1_entry(.text.isr1 = 0x100):**关卡(游戏)入口**,调 level_main()。
|
|
||||||
* 游戏是一整段会调用别的函数的 C 程序,所以入口把**所有调用者保存寄存器**
|
|
||||||
* (ra、t0-t6、a0-a7)都存起来,并用 csrrsi 打开 MIE 让鼠标中断能嵌套;
|
|
||||||
* 2) isr2_entry(.text.isr2 = 0x200):**鼠标读取**,只存 ra 后调下面的 isr_body;
|
|
||||||
* 3) isr3_entry(.text.isr3 = 0x300):备用,直接 uret;
|
|
||||||
* 4) isr_body:**纯 C**,本文件用 -ffixed-x1,x5..x7,x10..x17,x28..x31 编译,
|
|
||||||
* 逼 GCC 只用 s0-s11(callee-saved,函数自己会存/恢复)和 sp,于是被打断的
|
|
||||||
* 代码的调用者保存寄存器绝对不会被破坏 —— 这样主体就是安全的纯 C 叶子函数。
|
|
||||||
*
|
|
||||||
* 主体要做的事:把硬件事件 FIFO(深度 16)里的项全部搬到软件环形队列,
|
|
||||||
* 主循环再从队列里取。FIFO 读空后中断线自动回 0,下一个事件会重新产生上升沿,
|
|
||||||
* 所以不会丢事件。
|
|
||||||
* ---------------------------------------------------------------------------*/
|
|
||||||
#include "mmio.h"
|
|
||||||
|
|
||||||
/* ---- 与 saolei.c 共用的软件事件队列(定义在这里,声明在 mmio.h) ---- */
|
|
||||||
volatile unsigned g_evq[EVQ_SIZE];
|
|
||||||
volatile unsigned g_evq_head; /* 主循环消费 */
|
|
||||||
volatile unsigned g_evq_tail; /* 中断生产 */
|
|
||||||
volatile unsigned g_evq_lost; /* 队列满丢弃计数(调试) */
|
|
||||||
|
|
||||||
/* 中断主体:纯 C 叶子函数(不调用任何函数),只使用 s 寄存器和 sp。
|
|
||||||
* 编译本文件时必须加 -ffixed-x1 -ffixed-x5..x7 -ffixed-x10..x17 -ffixed-x28..x31
|
|
||||||
* (见 tools/build.ps1),否则它会用到调用者保存寄存器从而破坏被打断的代码。 */
|
|
||||||
__attribute__((used, noinline)) void isr_body(void);
|
|
||||||
|
|
||||||
void isr_body(void)
|
|
||||||
{
|
|
||||||
unsigned ev, next;
|
|
||||||
|
|
||||||
/* 硬件 FIFO 非空就一直搬(STATUS[0] = 非空) */
|
|
||||||
while ((REG_R(REG_STATUS) & 1u) != 0u)
|
|
||||||
{
|
|
||||||
ev = REG_R(REG_MDATA); /* lw:{btn[20:16], dy[15:8], dx[7:0]} */
|
|
||||||
next = (g_evq_tail + 1u) & EVQ_MASK;
|
|
||||||
if (next != g_evq_head)
|
|
||||||
{
|
|
||||||
g_evq[g_evq_tail] = ev;
|
|
||||||
g_evq_tail = next;
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
g_evq_lost = g_evq_lost + 1u; /* 软件队列满:丢弃并计数 */
|
|
||||||
}
|
|
||||||
REG_W(REG_MACK) = 1u; /* sw:弹出硬件 FIFO 一项 */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ---------------------------------------------------------------------------
|
|
||||||
* 三个中断入口,分别在链接脚本固定住的 0x100 / 0x200 / 0x300。
|
|
||||||
*
|
|
||||||
* IRQ1 = 0x100:**关卡(游戏)入口**。游戏是一整段会调用别的函数的 C 程序,
|
|
||||||
* 所以入口必须把**所有调用者保存寄存器**(ra、t0-t6、a0-a7)都存起来,
|
|
||||||
* 否则返回时会把被打断的背景程序弄坏。进入中断时硬件已把 MIE 清 0,这里再
|
|
||||||
* 重新打开,鼠标中断(IRQ2 优先级更高)才能嵌套进来;uret 时硬件会从内部
|
|
||||||
* 栈里恢复原来的 MIE。
|
|
||||||
* IRQ2 = 0x200:**鼠标读取**。C 主体用 -ffixed 编译(只用 s 寄存器和 sp),
|
|
||||||
* 所以入口只要保存 ra。
|
|
||||||
* IRQ3 = 0x300:备用(保留给拨码开关等外部源)。
|
|
||||||
* ------------------------------------------------------------------------- */
|
|
||||||
extern void level_main(void);
|
|
||||||
|
|
||||||
__attribute__((naked, section(".text.isr1"), used, noinline)) void isr1_entry(void)
|
|
||||||
{
|
|
||||||
__asm__ volatile(
|
|
||||||
"addi sp, sp, -64\n\t"
|
|
||||||
"sw ra, 0(sp)\n\t"
|
|
||||||
"sw t0, 4(sp)\n\t"
|
|
||||||
"sw t1, 8(sp)\n\t"
|
|
||||||
"sw t2, 12(sp)\n\t"
|
|
||||||
"sw t3, 16(sp)\n\t"
|
|
||||||
"sw t4, 20(sp)\n\t"
|
|
||||||
"sw t5, 24(sp)\n\t"
|
|
||||||
"sw t6, 28(sp)\n\t"
|
|
||||||
"sw a0, 32(sp)\n\t"
|
|
||||||
"sw a1, 36(sp)\n\t"
|
|
||||||
"sw a2, 40(sp)\n\t"
|
|
||||||
"sw a3, 44(sp)\n\t"
|
|
||||||
"sw a4, 48(sp)\n\t"
|
|
||||||
"sw a5, 52(sp)\n\t"
|
|
||||||
"sw a6, 56(sp)\n\t"
|
|
||||||
"sw a7, 60(sp)\n\t"
|
|
||||||
"csrrsi x0, 0x004, 1\n\t" /* ustatus[0]=MIE:允许鼠标中断嵌套 */
|
|
||||||
"jal ra, level_main\n\t"
|
|
||||||
"lw a7, 60(sp)\n\t"
|
|
||||||
"lw a6, 56(sp)\n\t"
|
|
||||||
"lw a5, 52(sp)\n\t"
|
|
||||||
"lw a4, 48(sp)\n\t"
|
|
||||||
"lw a3, 44(sp)\n\t"
|
|
||||||
"lw a2, 40(sp)\n\t"
|
|
||||||
"lw a1, 36(sp)\n\t"
|
|
||||||
"lw a0, 32(sp)\n\t"
|
|
||||||
"lw t6, 28(sp)\n\t"
|
|
||||||
"lw t5, 24(sp)\n\t"
|
|
||||||
"lw t4, 20(sp)\n\t"
|
|
||||||
"lw t3, 16(sp)\n\t"
|
|
||||||
"lw t2, 12(sp)\n\t"
|
|
||||||
"lw t1, 8(sp)\n\t"
|
|
||||||
"lw t0, 4(sp)\n\t"
|
|
||||||
"lw ra, 0(sp)\n\t"
|
|
||||||
"addi sp, sp, 64\n\t"
|
|
||||||
"uret\n\t");
|
|
||||||
}
|
|
||||||
|
|
||||||
__attribute__((naked, section(".text.isr2"), used, noinline)) void isr2_entry(void)
|
|
||||||
{
|
|
||||||
__asm__ volatile(
|
|
||||||
"addi sp, sp, -8\n\t" /* 保存被打断代码的 ra(jal 会覆盖它) */
|
|
||||||
"sw ra, 0(sp)\n\t"
|
|
||||||
"jal ra, isr_body\n\t" /* 调纯 C 主体(它自己保存 s 寄存器) */
|
|
||||||
"lw ra, 0(sp)\n\t"
|
|
||||||
"addi sp, sp, 8\n\t"
|
|
||||||
"uret\n\t"); /* 从 uepc 返回:这颗核的中断返回指令 */
|
|
||||||
}
|
|
||||||
|
|
||||||
__attribute__((naked, section(".text.isr3"), used, noinline)) void isr3_entry(void)
|
|
||||||
{
|
|
||||||
__asm__ volatile("uret\n\t");
|
|
||||||
}
|
|
||||||
@@ -1,99 +0,0 @@
|
|||||||
/* -----------------------------------------------------------------------------
|
|
||||||
* linker.ld —— 扫雷程序的内存布局
|
|
||||||
*
|
|
||||||
* 这颗 CPU 是"哈佛"结构:指令从 ROM 取(instr_addr_o),数据访问 RAM
|
|
||||||
* (data_addr_o),两个地址空间都从 0 开始,所以这里用两个 MEMORY 区域,
|
|
||||||
* 都在 0 处开头。
|
|
||||||
*
|
|
||||||
* ROM 0x0000_0000 32 KB(= cpu21_board_top 的 ROM_WORDS = 8192 字)
|
|
||||||
* +0x0000 .text.init 复位入口 _start
|
|
||||||
* +0x0100 .text.isr1 中断入口 isr1_entry(关卡/游戏,**必须等于 IRQ1_VECTOR**)
|
|
||||||
* +0x0200 .text.isr2 中断入口 isr2_entry(鼠标读取,必须等于 IRQ2_VECTOR)
|
|
||||||
* +0x0300 .text.isr3 中断入口 isr3_entry(备用,必须等于 IRQ3_VECTOR)
|
|
||||||
* +0x0400 .text 其余代码(紧跟 isr3 之后)
|
|
||||||
*
|
|
||||||
* RAM 0x0000_0000 4 KB(= saolei_board_top 的 RAM_WORDS = 1024 字)
|
|
||||||
* +0x0000 .bss 全局变量(全部零初始化,不需要 ROM 里的初值)
|
|
||||||
* +0x0FFC __stack_top 栈顶(向下增长)
|
|
||||||
*
|
|
||||||
* 两个断言很关键(都是这颗 CPU 的硬限制):
|
|
||||||
* 1) 不能有 .data —— 数据空间读不到 ROM,没有地方放"带初值的变量",
|
|
||||||
* 所有全局变量必须零初始化;
|
|
||||||
* 2) 不能有 .rodata —— 同理,程序里不能出现字符串/常量表
|
|
||||||
* (字模在 RTL 里,不在软件里)。
|
|
||||||
* ---------------------------------------------------------------------------*/
|
|
||||||
|
|
||||||
ENTRY(_start)
|
|
||||||
|
|
||||||
MEMORY
|
|
||||||
{
|
|
||||||
ROM (rx) : ORIGIN = 0x00000000, LENGTH = 32K
|
|
||||||
RAM (rwx) : ORIGIN = 0x00000000, LENGTH = 4K
|
|
||||||
}
|
|
||||||
|
|
||||||
SECTIONS
|
|
||||||
{
|
|
||||||
/* ------------------------- 数据段(RAM) ------------------------- */
|
|
||||||
. = 0x00000000;
|
|
||||||
|
|
||||||
.bss (NOLOAD) :
|
|
||||||
{
|
|
||||||
__bss_start = .;
|
|
||||||
*(.sbss)
|
|
||||||
*(.sbss.*)
|
|
||||||
*(.bss)
|
|
||||||
*(.bss.*)
|
|
||||||
*(COMMON)
|
|
||||||
. = ALIGN(4);
|
|
||||||
__bss_end = .;
|
|
||||||
} > RAM
|
|
||||||
|
|
||||||
/* 有初值的变量:不允许(见文件头的说明) */
|
|
||||||
.data : { *(.data) *(.data.*) *(.sdata) *(.sdata.*) } > RAM
|
|
||||||
|
|
||||||
/* 只读数据:不允许(没有 auipc / 数据空间读不到 ROM) */
|
|
||||||
.rodata : { *(.rodata) *(.rodata.*) } > RAM
|
|
||||||
|
|
||||||
ASSERT(SIZEOF(.data) == 0, "错误:程序里出现了带初值的全局变量(.data),这颗核没有地方存放它")
|
|
||||||
ASSERT(SIZEOF(.rodata) == 0, "错误:程序里出现了字符串或常量表(.rodata),这颗核读不到 ROM 里的数据")
|
|
||||||
|
|
||||||
/* ------------------------- 代码段(ROM) -------------------------
|
|
||||||
* 三个中断入口的地址**必须写在段声明里**(.text.isr1 0x100 : ...):
|
|
||||||
* 只写 ". = 0x100;" 这种位置计数器赋值,在段带 "> ROM" 时会被 ld 忽略,
|
|
||||||
* 它会按区域顺序把段紧挨着排下去(实测 isr1 掉到了 0x14)。
|
|
||||||
* 这三个地址要和顶层 cpu21_board_top 的 IRQ1/2/3_VECTOR 参数一致。 */
|
|
||||||
.text.init 0x00000000 :
|
|
||||||
{
|
|
||||||
KEEP(*(.text.init))
|
|
||||||
} > ROM
|
|
||||||
|
|
||||||
.text.isr1 0x00000100 : /* IRQ1:关卡(游戏)入口 */
|
|
||||||
{
|
|
||||||
KEEP(*(.text.isr1))
|
|
||||||
} > ROM
|
|
||||||
|
|
||||||
.text.isr2 0x00000200 : /* IRQ2:鼠标读取 */
|
|
||||||
{
|
|
||||||
KEEP(*(.text.isr2))
|
|
||||||
} > ROM
|
|
||||||
|
|
||||||
.text.isr3 0x00000300 : /* IRQ3:备用 */
|
|
||||||
{
|
|
||||||
KEEP(*(.text.isr3))
|
|
||||||
} > ROM
|
|
||||||
|
|
||||||
.text :
|
|
||||||
{
|
|
||||||
*(.text)
|
|
||||||
*(.text.*)
|
|
||||||
. = ALIGN(4);
|
|
||||||
} > ROM
|
|
||||||
|
|
||||||
/* ------------------------- 符号 ------------------------- */
|
|
||||||
__stack_top = ORIGIN(RAM) + LENGTH(RAM) - 4; /* 0x0FFC */
|
|
||||||
__isr_vector = 0x00000100;
|
|
||||||
|
|
||||||
/* .bss 不能长到撞到栈 */
|
|
||||||
ASSERT(__bss_end < (__stack_top - 0x200),
|
|
||||||
"错误:.bss 太大,会覆盖栈;请缩小棋盘或加大 RAM_WORDS")
|
|
||||||
}
|
|
||||||
@@ -1,108 +0,0 @@
|
|||||||
/* -----------------------------------------------------------------------------
|
|
||||||
* mmio.h —— 扫雷程序的全部 MMIO 地址与编码定义(**只改这一个文件就能换地址**)
|
|
||||||
*
|
|
||||||
* 约定:本文件里的所有地址都是"数据空间"的字节地址,和 CPU 的 data_addr_o 一致。
|
|
||||||
* 写用 *(volatile unsigned *)addr(sw 全字写)或 *(volatile unsigned char *)addr
|
|
||||||
* (sb 字节写,显存必须用这个)。
|
|
||||||
*
|
|
||||||
* -----------------------------------------------------------------------------
|
|
||||||
* 地址表(与 pipelined-cpu/srcs/rtl/cpu21_mmio.v 里的译码完全一致):
|
|
||||||
*
|
|
||||||
* 0x0000_0000 - 0x0000_0FFF 数据 RAM(4 KB,栈 + 全局变量)
|
|
||||||
* 0x0000_1000 LED 数据寄存器(低 16 位点亮 LED)
|
|
||||||
* 0x0000_1004 七段数码管:8 位十六进制(低 4 位 = 最右)
|
|
||||||
* 0x0000_1100 + 0x00 STATUS [0] 事件FIFO非空 [5:1] 项数 [6] 中断使能 [7] 满
|
|
||||||
* 0x0000_1100 + 0x04 DATA [7:0] dx [15:8] dy [20:16] 按键(读无副作用)
|
|
||||||
* 0x0000_1100 + 0x08 ACK 写任意值 -> 弹出一次事件
|
|
||||||
* 0x0000_1100 + 0x0C CTRL [0] 鼠标中断使能
|
|
||||||
* 0x0000_1100 + 0x10 INPUT [15:0] 拨码开关 [20:16] 按键
|
|
||||||
* 0x0000_1100 + 0x14 TIME 自由计数器(随机种子)
|
|
||||||
* 0x0000_1100 + 0x18 FRAME VGA 帧计数(闪烁用)
|
|
||||||
* 0x0000_1100 + 0x1C EVENTCNT 累计事件数
|
|
||||||
* 0x0000_1100 + 0x20 LEVEL 写 1 -> 触发"关卡入口"软件中断(IRQ2)
|
|
||||||
* 0x0000_1100 + 0x24 MAGIC 0xCAFE0002(自检:读得到说明 MMIO 活着)
|
|
||||||
* 0x0000_1140 + 0x00 DISP_CTRL [0] 光标可见 [6:4] 背景色
|
|
||||||
* 0x0000_1140 + 0x04 DISP_CURSOR [5:0] 列 [13:8] 行
|
|
||||||
* 0x0000_1140 + 0x08 DISP_MAGIC 0xCAFE0001
|
|
||||||
* 0x0000_2000 - 0x0000_27FF 显存:40 列 x 30 行,每格 1 字节,共 1200 字节
|
|
||||||
*
|
|
||||||
* 鼠标事件单位:1/8 格(按键一次 = 8 = 移动一格;PS/2 鼠标用原始计数)。
|
|
||||||
* 左键点击 -> 事件的 btn 位 0 置 1;右键(PS/2)-> 位 1。
|
|
||||||
*
|
|
||||||
* ⚠ 如果你的外设地址和上表不同:只改下面这几个宏,.c 文件不用动。
|
|
||||||
* ⚠ 三个中断入口必须与 linker.ld 里 .text.isr1/2/3 的地址一致:
|
|
||||||
* IRQ1(0x100) = 关卡(游戏)入口,IRQ2(0x200) = 鼠标读取,IRQ3(0x300) = 备用。
|
|
||||||
* 顶层 cpu21_board_top 的 IRQ1/2/3_VECTOR 参数也要跟着改(默认就是这个布局)。
|
|
||||||
* ---------------------------------------------------------------------------*/
|
|
||||||
#ifndef SAOLEI_MMIO_H
|
|
||||||
#define SAOLEI_MMIO_H
|
|
||||||
|
|
||||||
/* ---- LED / 七段数码管 ---- */
|
|
||||||
#define LED_ADDR 0x00001000u
|
|
||||||
#define REG_SEG 0x00001004u
|
|
||||||
|
|
||||||
/* ---- 显存 ---- */
|
|
||||||
#define FB_BASE 0x00002000u /* 显存起始(字节地址) */
|
|
||||||
#define FB_PITCH 40u /* 每行 40 格 */
|
|
||||||
#define FB_ROWS 30u /* 30 行 */
|
|
||||||
#define GRID_COLS 40u
|
|
||||||
#define GRID_ROWS 30u
|
|
||||||
|
|
||||||
/* ---- 鼠标寄存器组 ---- */
|
|
||||||
#define REG_STATUS 0x00001100u
|
|
||||||
#define REG_MDATA 0x00001104u
|
|
||||||
#define REG_MACK 0x00001108u
|
|
||||||
#define REG_MCTRL 0x0000110Cu
|
|
||||||
#define REG_INPUT 0x00001110u
|
|
||||||
#define REG_TIME 0x00001114u
|
|
||||||
#define REG_FRAME 0x00001118u
|
|
||||||
#define REG_EVCNT 0x0000111Cu
|
|
||||||
#define REG_LEVEL 0x00001120u
|
|
||||||
#define REG_MMAGIC 0x00001124u
|
|
||||||
|
|
||||||
/* ---- 显示控制器寄存器 ---- */
|
|
||||||
#define REG_CTRL 0x00001140u
|
|
||||||
#define REG_CURSOR 0x00001144u
|
|
||||||
#define REG_DMAGIC 0x00001148u
|
|
||||||
|
|
||||||
/* ---- 访问宏 ---- */
|
|
||||||
#define REG_R(a) (*(volatile unsigned *)((unsigned)(a)))
|
|
||||||
#define REG_W(a) (*(volatile unsigned *)((unsigned)(a)))
|
|
||||||
|
|
||||||
/* 显存按字节写(只能 sb,不能 lb —— 这颗核没有 lb/lbu) */
|
|
||||||
static inline void fb_write(unsigned idx, unsigned val)
|
|
||||||
{
|
|
||||||
*(volatile unsigned char *)(FB_BASE + idx) = (unsigned char)(val & 0xFFu);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ---- 格子编码:1 字节 = [3:0] 字形 [6:4] 颜色 [7] 保留 ---- */
|
|
||||||
#define G_BLANK 0u /* 空 */
|
|
||||||
#define G_N1 1u /* 数字 1..8 = 1..8 */
|
|
||||||
#define G_COVER 9u /* 未揭开 */
|
|
||||||
#define G_FLAG 10u /* 旗 */
|
|
||||||
#define G_MINE 11u /* 雷 */
|
|
||||||
#define G_BOOM 12u /* 爆炸 */
|
|
||||||
#define G_QUEST 13u /* 问号 */
|
|
||||||
#define G_BAR 14u /* 横线 */
|
|
||||||
#define G_SOLID 15u /* 实心 */
|
|
||||||
|
|
||||||
#define C_BLACK 0u
|
|
||||||
#define C_BLUE 1u
|
|
||||||
#define C_GREEN 2u
|
|
||||||
#define C_CYAN 3u
|
|
||||||
#define C_RED 4u
|
|
||||||
#define C_MAGENTA 5u
|
|
||||||
#define C_YELLOW 6u
|
|
||||||
#define C_GRAY 7u
|
|
||||||
|
|
||||||
#define CELL(g, c) ((unsigned)((g) | ((c) << 4)))
|
|
||||||
|
|
||||||
/* ---- 软件事件环形队列:中断服务程序(isr.c)生产,主循环(saolei.c)消费 ---- */
|
|
||||||
#define EVQ_SIZE 64u
|
|
||||||
#define EVQ_MASK (EVQ_SIZE - 1u)
|
|
||||||
extern volatile unsigned g_evq[EVQ_SIZE];
|
|
||||||
extern volatile unsigned g_evq_head; /* 主循环消费 */
|
|
||||||
extern volatile unsigned g_evq_tail; /* 中断生产 */
|
|
||||||
extern volatile unsigned g_evq_lost; /* 队列满丢弃计数(调试用) */
|
|
||||||
|
|
||||||
#endif /* SAOLEI_MMIO_H */
|
|
||||||
@@ -1,635 +0,0 @@
|
|||||||
/* -----------------------------------------------------------------------------
|
|
||||||
* saolei.c —— 纯 C 扫雷(RV32IM 子集:只用这颗核实现了的指令)
|
|
||||||
*
|
|
||||||
* 显示:把每个格子写成显存里的 1 个字节(MMIO 0x2000 起)
|
|
||||||
* 输入:鼠标事件(中断驱动)——中断里搬到软件队列,主循环在下面处理
|
|
||||||
*
|
|
||||||
* 针对这颗 CPU 的硬约束(很重要,改代码时别踩):
|
|
||||||
* 1) 存储只有 lw / sw / sb,**没有 lb/lh/lbu/lhu** ——
|
|
||||||
* 所以只按字节"写"显存,从不按字节"读";所有状态都用 32 位字保存。
|
|
||||||
* 2) **没有 auipc** —— 编译时必须 -fno-pic -mcmodel=medlow,并且不能出现
|
|
||||||
* 只读数据(.rodata),所以本文件没有任何字符串/常量表(字模在 RTL 里)。
|
|
||||||
* 3) 乘除只有 mul/mulh/mulhsu/mulhu 和**无符号**的 divu/remu ——
|
|
||||||
* 所以本文件所有索引运算都用"2 的幂 + 移位/掩码",一次除法都不用。
|
|
||||||
* 4) 没有 fence/ebreak 等;中断返回是 uret(在 isr.c 里)。
|
|
||||||
*
|
|
||||||
* 棋盘:16 x 16,40 颗雷(16 是 2 的幂 => 索引全用移位,颜色/字形查表也不用除法)
|
|
||||||
* 网格:40 列 x 30 行,棋盘居中放在 (行 7..22, 列 12..27)
|
|
||||||
* ---------------------------------------------------------------------------*/
|
|
||||||
#include "mmio.h"
|
|
||||||
|
|
||||||
#define BW 16u /* 棋盘宽(格) */
|
|
||||||
#define BH 16u /* 棋盘高(格) */
|
|
||||||
#define NCELLS (BW * BH) /* 256 */
|
|
||||||
#define NMINES 40u
|
|
||||||
#define BM_WORDS (NCELLS / 32u) /* 8 */
|
|
||||||
|
|
||||||
#define BOARD_C0 12u /* 棋盘左上角在网格里的列 */
|
|
||||||
#define BOARD_R0 7u /* 棋盘左上角在网格里的行 */
|
|
||||||
|
|
||||||
/* 游戏状态 */
|
|
||||||
#define ST_PLAY 0u
|
|
||||||
#define ST_WIN 1u
|
|
||||||
#define ST_LOSE 2u
|
|
||||||
|
|
||||||
/* ---- 棋盘状态:3 个位图,每个 8 个字(256 位) ---- */
|
|
||||||
static unsigned mine_bm[BM_WORDS];
|
|
||||||
static unsigned open_bm[BM_WORDS];
|
|
||||||
static unsigned flag_bm[BM_WORDS];
|
|
||||||
|
|
||||||
/* 其它全局状态(全部进 .bss,靠 start.c 清零) */
|
|
||||||
static unsigned opened_cnt;
|
|
||||||
static unsigned flags_cnt;
|
|
||||||
static unsigned g_state;
|
|
||||||
static unsigned g_first_done;
|
|
||||||
static unsigned rnd_state;
|
|
||||||
static unsigned cur_r, cur_c; /* 光标所在的网格格 */
|
|
||||||
static int acc_x, acc_y; /* 指针累积量(1/8 格,有符号) */
|
|
||||||
static unsigned bfs_q[NCELLS]; /* 揭开用的队列 */
|
|
||||||
|
|
||||||
/* ============================ 小工具 ============================ */
|
|
||||||
|
|
||||||
/* 位图取位/置位/清位:i = r*16 + c,全部用移位和掩码 */
|
|
||||||
static unsigned bget(const unsigned *bm, unsigned i)
|
|
||||||
{
|
|
||||||
return (bm[i >> 5] >> (i & 31u)) & 1u;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void bset(unsigned *bm, unsigned i)
|
|
||||||
{
|
|
||||||
bm[i >> 5] |= (1u << (i & 31u));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void bclr(unsigned *bm, unsigned i)
|
|
||||||
{
|
|
||||||
bm[i >> 5] &= ~(1u << (i & 31u));
|
|
||||||
}
|
|
||||||
|
|
||||||
static void draw_cell(unsigned gr, unsigned gc, unsigned glyph, unsigned color)
|
|
||||||
{
|
|
||||||
fb_write(gr * GRID_COLS + gc, CELL(glyph, color));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 把棋盘格 (r,c) 画出来 */
|
|
||||||
static void draw_tile(unsigned r, unsigned c, unsigned glyph, unsigned color)
|
|
||||||
{
|
|
||||||
draw_cell(BOARD_R0 + r, BOARD_C0 + c, glyph, color);
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned num_color(unsigned n)
|
|
||||||
{
|
|
||||||
if (n == 1u)
|
|
||||||
return C_BLUE;
|
|
||||||
if (n == 2u)
|
|
||||||
return C_GREEN;
|
|
||||||
if (n == 3u)
|
|
||||||
return C_RED;
|
|
||||||
if (n == 4u)
|
|
||||||
return C_MAGENTA;
|
|
||||||
if (n == 5u)
|
|
||||||
return C_YELLOW;
|
|
||||||
if (n == 6u)
|
|
||||||
return C_CYAN;
|
|
||||||
return C_GRAY;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned in_board(void)
|
|
||||||
{
|
|
||||||
if (cur_r < BOARD_R0)
|
|
||||||
return 0u;
|
|
||||||
if (cur_r >= (BOARD_R0 + BH))
|
|
||||||
return 0u;
|
|
||||||
if (cur_c < BOARD_C0)
|
|
||||||
return 0u;
|
|
||||||
if (cur_c >= (BOARD_C0 + BW))
|
|
||||||
return 0u;
|
|
||||||
return 1u;
|
|
||||||
}
|
|
||||||
|
|
||||||
static unsigned cur_index(void)
|
|
||||||
{
|
|
||||||
return ((cur_r - BOARD_R0) << 4) + (cur_c - BOARD_C0); /* *BW(=16) 用移位 */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* LCG 随机数:只用 mul/add/移位 */
|
|
||||||
static unsigned rnd_next(void)
|
|
||||||
{
|
|
||||||
rnd_state = rnd_state * 1103515245u + 12345u;
|
|
||||||
return rnd_state >> 16;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ============================ 画面 ============================ */
|
|
||||||
|
|
||||||
static void clear_screen(void)
|
|
||||||
{
|
|
||||||
unsigned i;
|
|
||||||
for (i = 0u; i < (GRID_COLS * GRID_ROWS); i = i + 1u)
|
|
||||||
{
|
|
||||||
fb_write(i, 0u);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void draw_frame(void)
|
|
||||||
{
|
|
||||||
unsigned gc, gr;
|
|
||||||
for (gc = (BOARD_C0 - 1u); gc <= (BOARD_C0 + BW); gc = gc + 1u)
|
|
||||||
{
|
|
||||||
draw_cell(BOARD_R0 - 1u, gc, G_SOLID, C_CYAN);
|
|
||||||
draw_cell(BOARD_R0 + BH, gc, G_SOLID, C_CYAN);
|
|
||||||
}
|
|
||||||
for (gr = (BOARD_R0 - 1u); gr <= (BOARD_R0 + BH); gr = gr + 1u)
|
|
||||||
{
|
|
||||||
draw_cell(gr, BOARD_C0 - 1u, G_SOLID, C_CYAN);
|
|
||||||
draw_cell(gr, BOARD_C0 + BW, G_SOLID, C_CYAN);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void draw_all_covered(void)
|
|
||||||
{
|
|
||||||
unsigned r, c;
|
|
||||||
for (r = 0u; r < BH; r = r + 1u)
|
|
||||||
{
|
|
||||||
for (c = 0u; c < BW; c = c + 1u)
|
|
||||||
{
|
|
||||||
draw_tile(r, c, G_COVER, C_GRAY);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void update_cursor_reg(void)
|
|
||||||
{
|
|
||||||
REG_W(REG_CURSOR) = (cur_c & 31u) | ((cur_r & 31u) << 8);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void set_bg(unsigned color)
|
|
||||||
{
|
|
||||||
REG_W(REG_CTRL) = (color << 4) | 1u; /* bit0 = 光标显示 */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ============================ 游戏逻辑 ============================ */
|
|
||||||
|
|
||||||
static unsigned count_adj(unsigned i)
|
|
||||||
{
|
|
||||||
unsigned r = i >> 4, c = i & 15u, n = 0u, rr, cc;
|
|
||||||
unsigned r0 = (r == 0u) ? 0u : (r - 1u);
|
|
||||||
unsigned r1 = (r + 1u >= BH) ? (BH - 1u) : (r + 1u);
|
|
||||||
unsigned c0 = (c == 0u) ? 0u : (c - 1u);
|
|
||||||
unsigned c1 = (c + 1u >= BW) ? (BW - 1u) : (c + 1u);
|
|
||||||
|
|
||||||
for (rr = r0; rr <= r1; rr = rr + 1u)
|
|
||||||
{
|
|
||||||
for (cc = c0; cc <= c1; cc = cc + 1u)
|
|
||||||
{
|
|
||||||
if ((rr == r) && (cc == c))
|
|
||||||
continue;
|
|
||||||
n = n + bget(mine_bm, (rr << 4) + cc);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return n;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void lose(unsigned hit)
|
|
||||||
{
|
|
||||||
unsigned k;
|
|
||||||
for (k = 0u; k < NCELLS; k = k + 1u)
|
|
||||||
{
|
|
||||||
if (bget(mine_bm, k))
|
|
||||||
{
|
|
||||||
if (k == hit)
|
|
||||||
{
|
|
||||||
draw_tile(k >> 4, k & 15u, G_BOOM, C_RED);
|
|
||||||
}
|
|
||||||
else if (!bget(flag_bm, k))
|
|
||||||
{
|
|
||||||
draw_tile(k >> 4, k & 15u, G_MINE, C_RED);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
REG_W(LED_ADDR) = 0xDEADu;
|
|
||||||
g_state = ST_LOSE;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void win(void)
|
|
||||||
{
|
|
||||||
unsigned k;
|
|
||||||
for (k = 0u; k < NCELLS; k = k + 1u)
|
|
||||||
{
|
|
||||||
if (bget(mine_bm, k))
|
|
||||||
{
|
|
||||||
draw_tile(k >> 4, k & 15u, G_FLAG, C_YELLOW);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
REG_W(LED_ADDR) = 0xF00Du;
|
|
||||||
g_state = ST_WIN;
|
|
||||||
}
|
|
||||||
|
|
||||||
static void reveal_at(unsigned i)
|
|
||||||
{
|
|
||||||
unsigned qh = 0u, qt = 0u, cur, n, r, c, rr, cc, ni;
|
|
||||||
unsigned r0, r1, c0, c1;
|
|
||||||
|
|
||||||
if (bget(open_bm, i) || bget(flag_bm, i))
|
|
||||||
return;
|
|
||||||
if (bget(mine_bm, i))
|
|
||||||
{
|
|
||||||
lose(i);
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
bfs_q[qt] = i;
|
|
||||||
qt = qt + 1u;
|
|
||||||
|
|
||||||
while (qh < qt)
|
|
||||||
{
|
|
||||||
cur = bfs_q[qh];
|
|
||||||
qh = qh + 1u;
|
|
||||||
if (bget(open_bm, cur))
|
|
||||||
continue;
|
|
||||||
|
|
||||||
bset(open_bm, cur);
|
|
||||||
opened_cnt = opened_cnt + 1u;
|
|
||||||
|
|
||||||
n = count_adj(cur);
|
|
||||||
draw_tile(cur >> 4, cur & 15u, n, num_color(n));
|
|
||||||
|
|
||||||
if (n != 0u)
|
|
||||||
continue;
|
|
||||||
|
|
||||||
/* 空白格:把周围 8 格入队(队列有界,满了就丢,不会越界) */
|
|
||||||
r = cur >> 4;
|
|
||||||
c = cur & 15u;
|
|
||||||
r0 = (r == 0u) ? 0u : (r - 1u);
|
|
||||||
r1 = (r + 1u >= BH) ? (BH - 1u) : (r + 1u);
|
|
||||||
c0 = (c == 0u) ? 0u : (c - 1u);
|
|
||||||
c1 = (c + 1u >= BW) ? (BW - 1u) : (c + 1u);
|
|
||||||
for (rr = r0; rr <= r1; rr = rr + 1u)
|
|
||||||
{
|
|
||||||
for (cc = c0; cc <= c1; cc = cc + 1u)
|
|
||||||
{
|
|
||||||
if ((rr == r) && (cc == c))
|
|
||||||
continue;
|
|
||||||
ni = (rr << 4) + cc;
|
|
||||||
if (qt < NCELLS)
|
|
||||||
{
|
|
||||||
bfs_q[qt] = ni;
|
|
||||||
qt = qt + 1u;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (opened_cnt == (NCELLS - NMINES))
|
|
||||||
win();
|
|
||||||
}
|
|
||||||
|
|
||||||
static void place_mines(unsigned safe)
|
|
||||||
{
|
|
||||||
unsigned placed = 0u, i, r, c;
|
|
||||||
unsigned sr = safe >> 4;
|
|
||||||
unsigned sc = safe & 15u;
|
|
||||||
|
|
||||||
while (placed < NMINES)
|
|
||||||
{
|
|
||||||
i = rnd_next() & 255u; /* NCELLS = 256,正好 2 的幂,不需要取模 */
|
|
||||||
r = i >> 4;
|
|
||||||
c = i & 15u;
|
|
||||||
/* 首点及其 8 邻居不放雷,保证第一次点击一定能展开 */
|
|
||||||
if ((r + 1u >= sr) && (r <= (sr + 1u)) &&
|
|
||||||
(c + 1u >= sc) && (c <= (sc + 1u)))
|
|
||||||
continue;
|
|
||||||
if (bget(mine_bm, i))
|
|
||||||
continue;
|
|
||||||
bset(mine_bm, i);
|
|
||||||
placed = placed + 1u;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static void click_here(void)
|
|
||||||
{
|
|
||||||
unsigned i;
|
|
||||||
|
|
||||||
if (!in_board())
|
|
||||||
return;
|
|
||||||
i = cur_index();
|
|
||||||
if (bget(flag_bm, i))
|
|
||||||
return;
|
|
||||||
|
|
||||||
if (!g_first_done)
|
|
||||||
{
|
|
||||||
place_mines(i);
|
|
||||||
g_first_done = 1u;
|
|
||||||
}
|
|
||||||
reveal_at(i);
|
|
||||||
}
|
|
||||||
|
|
||||||
static void toggle_flag(void)
|
|
||||||
{
|
|
||||||
unsigned i, r, c;
|
|
||||||
|
|
||||||
if (!in_board())
|
|
||||||
return;
|
|
||||||
i = cur_index();
|
|
||||||
if (bget(open_bm, i))
|
|
||||||
return;
|
|
||||||
|
|
||||||
r = cur_r - BOARD_R0;
|
|
||||||
c = cur_c - BOARD_C0;
|
|
||||||
|
|
||||||
if (bget(flag_bm, i))
|
|
||||||
{
|
|
||||||
bclr(flag_bm, i);
|
|
||||||
flags_cnt = flags_cnt - 1u;
|
|
||||||
draw_tile(r, c, G_COVER, C_GRAY);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
bset(flag_bm, i);
|
|
||||||
flags_cnt = flags_cnt + 1u;
|
|
||||||
draw_tile(r, c, G_FLAG, C_YELLOW);
|
|
||||||
}
|
|
||||||
REG_W(LED_ADDR) = NMINES - flags_cnt;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ============================ 鼠标事件 ============================ */
|
|
||||||
|
|
||||||
/* 关卡内:光标允许落在盘面外那一圈边框上(左上角那格是"返回") */
|
|
||||||
static void move_cursor(int dr, int dc)
|
|
||||||
{
|
|
||||||
int nr = (int)cur_r + dr;
|
|
||||||
int nc = (int)cur_c + dc;
|
|
||||||
|
|
||||||
if (nr < (int)(BOARD_R0 - 1u))
|
|
||||||
nr = (int)(BOARD_R0 - 1u);
|
|
||||||
if (nr > (int)(BOARD_R0 + BH))
|
|
||||||
nr = (int)(BOARD_R0 + BH);
|
|
||||||
if (nc < (int)(BOARD_C0 - 1u))
|
|
||||||
nc = (int)(BOARD_C0 - 1u);
|
|
||||||
if (nc > (int)(BOARD_C0 + BW))
|
|
||||||
nc = (int)(BOARD_C0 + BW);
|
|
||||||
|
|
||||||
cur_r = (unsigned)nr;
|
|
||||||
cur_c = (unsigned)nc;
|
|
||||||
update_cursor_reg();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ======================= 运行环境状态(背景 / 关卡) =======================
|
|
||||||
* 程序分成两层,正好对应"伪操作系统 + 关卡"的设想:
|
|
||||||
* 背景程序 main() 从头跑到尾:画开始界面、等鼠标;
|
|
||||||
* 关卡程序 level_main() 跑在 IRQ1(0x100) 的中断服务程序里:点"开始"进入,
|
|
||||||
* 点"返回"就 uret 回到背景被打断的那条指令。
|
|
||||||
* ========================================================================= */
|
|
||||||
static unsigned g_in_level; /* 0 = 背景界面,1 = 关卡内 */
|
|
||||||
static unsigned g_exit; /* 关卡内请求返回 */
|
|
||||||
|
|
||||||
/* 背景界面上"开始"按钮的位置(行 20,列 17..22) */
|
|
||||||
#define START_R 20u
|
|
||||||
#define START_C0 17u
|
|
||||||
#define START_C1 22u
|
|
||||||
|
|
||||||
/* 背景用:光标可以在整个画面上移动 */
|
|
||||||
static void move_cursor_screen(int dr, int dc)
|
|
||||||
{
|
|
||||||
int nr = (int)cur_r + dr;
|
|
||||||
int nc = (int)cur_c + dc;
|
|
||||||
|
|
||||||
if (nr < 0)
|
|
||||||
nr = 0;
|
|
||||||
if (nr > (int)(GRID_ROWS - 1u))
|
|
||||||
nr = (int)(GRID_ROWS - 1u);
|
|
||||||
if (nc < 0)
|
|
||||||
nc = 0;
|
|
||||||
if (nc > (int)(GRID_COLS - 1u))
|
|
||||||
nc = (int)(GRID_COLS - 1u);
|
|
||||||
|
|
||||||
cur_r = (unsigned)nr;
|
|
||||||
cur_c = (unsigned)nc;
|
|
||||||
update_cursor_reg();
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 处理一个鼠标事件:ev = {btn[20:16], dy[15:8], dx[7:0]} */
|
|
||||||
static void handle_event(unsigned ev)
|
|
||||||
{
|
|
||||||
unsigned ux = ev & 0xFFu;
|
|
||||||
unsigned uy = (ev >> 8) & 0xFFu;
|
|
||||||
unsigned btn = (ev >> 16) & 31u;
|
|
||||||
int dx = (ux & 0x80u) ? ((int)ux - 256) : (int)ux;
|
|
||||||
int dy = (uy & 0x80u) ? ((int)uy - 256) : (int)uy;
|
|
||||||
unsigned want_flag = (REG_R(REG_INPUT) & 9u) != 0u; /* SW0 或 SW3 = 插旗模式 */
|
|
||||||
|
|
||||||
if ((btn & 1u) != 0u)
|
|
||||||
{ /* 左键 */
|
|
||||||
if (g_in_level == 0u)
|
|
||||||
{
|
|
||||||
/* 背景:点到"开始"按钮 -> 拉起软件中断,跳到 IRQ1 里的关卡程序 */
|
|
||||||
if ((cur_r == START_R) && (cur_c >= START_C0) && (cur_c <= START_C1))
|
|
||||||
{
|
|
||||||
g_in_level = 1u; /* 关卡程序返回时自己清 0 */
|
|
||||||
REG_W(REG_LEVEL) = 1u;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if ((cur_r == (BOARD_R0 - 1u)) && (cur_c == (BOARD_C0 - 1u)))
|
|
||||||
{
|
|
||||||
g_exit = 1u; /* 关卡:点左上角黄格 = 返回上一层 */
|
|
||||||
}
|
|
||||||
else if (want_flag != 0u)
|
|
||||||
{
|
|
||||||
toggle_flag();
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
click_here();
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if ((btn & 2u) != 0u)
|
|
||||||
{ /* 右键(PS/2 鼠标有;按键没有) */
|
|
||||||
if (g_in_level != 0u)
|
|
||||||
toggle_flag();
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 移动:单位是 1/8 格,累计到 8 就动一格(纯加减比较,没有除法) */
|
|
||||||
acc_x += dx;
|
|
||||||
acc_y += dy;
|
|
||||||
while (acc_x >= 8)
|
|
||||||
{
|
|
||||||
if (g_in_level != 0u)
|
|
||||||
move_cursor(0, 1);
|
|
||||||
else
|
|
||||||
move_cursor_screen(0, 1);
|
|
||||||
acc_x -= 8;
|
|
||||||
}
|
|
||||||
while (acc_x <= -8)
|
|
||||||
{
|
|
||||||
if (g_in_level != 0u)
|
|
||||||
move_cursor(0, -1);
|
|
||||||
else
|
|
||||||
move_cursor_screen(0, -1);
|
|
||||||
acc_x += 8;
|
|
||||||
}
|
|
||||||
while (acc_y >= 8)
|
|
||||||
{
|
|
||||||
if (g_in_level != 0u)
|
|
||||||
move_cursor(1, 0);
|
|
||||||
else
|
|
||||||
move_cursor_screen(1, 0);
|
|
||||||
acc_y -= 8;
|
|
||||||
}
|
|
||||||
while (acc_y <= -8)
|
|
||||||
{
|
|
||||||
if (g_in_level != 0u)
|
|
||||||
move_cursor(-1, 0);
|
|
||||||
else
|
|
||||||
move_cursor_screen(-1, 0);
|
|
||||||
acc_y += 8;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ============================ 背景程序(伪操作系统)============================
|
|
||||||
* 从头跑到尾:画开始界面 -> 等鼠标 -> 点到"开始"按钮就拉起软件中断。
|
|
||||||
* ========================================================================== */
|
|
||||||
static void draw_start_screen(void)
|
|
||||||
{
|
|
||||||
unsigned i;
|
|
||||||
|
|
||||||
clear_screen();
|
|
||||||
|
|
||||||
/* 标题区:两排雷图案,纯图形(字模里没有字母;要显示文字就得扩字模,见 README) */
|
|
||||||
for (i = 0u; i < 6u; i = i + 1u)
|
|
||||||
{
|
|
||||||
draw_cell(6u, 15u + i * 2u, G_MINE, C_RED);
|
|
||||||
draw_cell(8u, 15u + i * 2u, G_MINE, C_RED);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* "开始"按钮:绿色实心条,上下各一条黄线框住它 */
|
|
||||||
for (i = START_C0; i <= START_C1; i = i + 1u)
|
|
||||||
{
|
|
||||||
draw_cell(START_R - 1u, i, G_BAR, C_YELLOW);
|
|
||||||
draw_cell(START_R, i, G_SOLID, C_GREEN);
|
|
||||||
draw_cell(START_R + 1u, i, G_BAR, C_YELLOW);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 下方提示条 */
|
|
||||||
for (i = 12u; i <= 27u; i = i + 1u)
|
|
||||||
{
|
|
||||||
draw_cell(24u, i, G_BAR, C_CYAN);
|
|
||||||
}
|
|
||||||
|
|
||||||
set_bg(C_BLACK);
|
|
||||||
cur_r = START_R;
|
|
||||||
cur_c = (START_C0 + START_C1) / 2u;
|
|
||||||
update_cursor_reg();
|
|
||||||
|
|
||||||
REG_W(LED_ADDR) = 0x0001u; /* LED = 背景状态码 */
|
|
||||||
REG_W(REG_SEG) = 0u;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ============================ 关卡(游戏)程序 ============================
|
|
||||||
* **运行在 IRQ1 的中断服务程序里**(入口 isr1_entry,固定地址 0x100):
|
|
||||||
* 背景 main() 点"开始" -> 写 LEVEL 寄存器 -> 硬件拉起 irq_i[1]
|
|
||||||
* -> CPU 跳到 0x100 -> isr1_entry 保存现场、打开 MIE、调用本函数
|
|
||||||
* -> 这里跑完整一局(鼠标中断 0x200 优先级更高,会嵌套进来把事件放进队列,
|
|
||||||
* 所以本循环只要轮询队列)
|
|
||||||
* -> 点左上角黄格 = 返回:清 LEVEL、return -> isr1_entry 恢复现场并 uret
|
|
||||||
* -> 回到背景程序被打断的那条指令,继续等下一次"开始"。
|
|
||||||
* ========================================================================== */
|
|
||||||
#define RET_R (BOARD_R0 - 1u)
|
|
||||||
#define RET_C (BOARD_C0 - 1u)
|
|
||||||
|
|
||||||
void level_main(void)
|
|
||||||
{
|
|
||||||
unsigned r;
|
|
||||||
|
|
||||||
/* ---- 关卡初始化 ---- */
|
|
||||||
for (r = 0u; r < BM_WORDS; r = r + 1u)
|
|
||||||
{
|
|
||||||
mine_bm[r] = 0u;
|
|
||||||
open_bm[r] = 0u;
|
|
||||||
flag_bm[r] = 0u;
|
|
||||||
}
|
|
||||||
opened_cnt = 0u;
|
|
||||||
flags_cnt = 0u;
|
|
||||||
g_first_done = 0u;
|
|
||||||
g_state = ST_PLAY;
|
|
||||||
g_exit = 0u;
|
|
||||||
acc_x = 0;
|
|
||||||
acc_y = 0;
|
|
||||||
cur_r = BOARD_R0 + 8u;
|
|
||||||
cur_c = BOARD_C0 + 8u;
|
|
||||||
|
|
||||||
clear_screen();
|
|
||||||
draw_frame();
|
|
||||||
draw_all_covered();
|
|
||||||
draw_cell(RET_R, RET_C, G_BAR, C_YELLOW); /* 左上角黄格 = 返回 */
|
|
||||||
set_bg(C_BLUE);
|
|
||||||
update_cursor_reg();
|
|
||||||
REG_W(LED_ADDR) = NMINES;
|
|
||||||
REG_W(REG_SEG) = NMINES; /* 数码管显示剩余雷数 */
|
|
||||||
|
|
||||||
/* ---- 关卡主循环 ---- */
|
|
||||||
while (g_exit == 0u)
|
|
||||||
{
|
|
||||||
if (g_evq_head != g_evq_tail)
|
|
||||||
{
|
|
||||||
unsigned ev = g_evq[g_evq_head];
|
|
||||||
g_evq_head = (g_evq_head + 1u) & EVQ_MASK;
|
|
||||||
if (g_state == ST_PLAY)
|
|
||||||
{
|
|
||||||
handle_event(ev);
|
|
||||||
}
|
|
||||||
else if ((ev >> 16) & 1u)
|
|
||||||
{
|
|
||||||
g_exit = 1u; /* 分出胜负后任意点击 = 返回 */
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (g_state != ST_PLAY)
|
|
||||||
{
|
|
||||||
/* 分出胜负后让背景闪动(用 VGA 帧计数做时间基准,不需要定时器) */
|
|
||||||
static unsigned phase;
|
|
||||||
if ((REG_R(REG_FRAME) & 31u) == 0u)
|
|
||||||
{
|
|
||||||
phase = phase ^ 1u;
|
|
||||||
if (phase != 0u)
|
|
||||||
{
|
|
||||||
set_bg((g_state == ST_WIN) ? C_GREEN : C_RED);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
set_bg(C_BLUE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
REG_W(REG_LEVEL) = 0u; /* 清软件中断:否则下次点"开始"没有新的上升沿 */
|
|
||||||
g_in_level = 0u; /* 回到背景状态 */
|
|
||||||
draw_start_screen(); /* 恢复开始界面 */
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ============================ 背景主程序 ============================ */
|
|
||||||
|
|
||||||
extern void crt_zero_bss(void);
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
crt_zero_bss(); /* 清 .bss(上电 RAM 本来就是 0,保险起见) */
|
|
||||||
|
|
||||||
rnd_state = REG_R(REG_TIME) | 1u; /* 用自由计数器当随机种子 */
|
|
||||||
REG_W(REG_MCTRL) = 1u; /* 打开鼠标中断(IRQ2,入口 0x200) */
|
|
||||||
REG_W(REG_LEVEL) = 0u; /* 清关卡软中断 */
|
|
||||||
|
|
||||||
g_in_level = 0u;
|
|
||||||
draw_start_screen();
|
|
||||||
|
|
||||||
/* 背景循环:只服务鼠标事件;点"开始"后会被 IRQ1 里的关卡程序"接管",
|
|
||||||
* 关卡返回时又回到这个循环里继续。 */
|
|
||||||
for (;;)
|
|
||||||
{
|
|
||||||
if (g_evq_head != g_evq_tail)
|
|
||||||
{
|
|
||||||
unsigned ev = g_evq[g_evq_head];
|
|
||||||
g_evq_head = (g_evq_head + 1u) & EVQ_MASK;
|
|
||||||
handle_event(ev);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,35 +0,0 @@
|
|||||||
/* -----------------------------------------------------------------------------
|
|
||||||
* start.c —— C 运行时启动:设置栈指针、把控制权交给 main。
|
|
||||||
*
|
|
||||||
* 这是全工程唯一非写不可的内联汇编(C 语言无法表达"设置 sp")。
|
|
||||||
* 复位时 PC = 0,所以 _start 必须落在 .text.init(见 linker.ld)。
|
|
||||||
*
|
|
||||||
* 注意用的是 lui/addi 绝对寻址 + jalr,**不是** auipc —— 这颗核没有 auipc。
|
|
||||||
* ---------------------------------------------------------------------------*/
|
|
||||||
extern unsigned __stack_top[];
|
|
||||||
|
|
||||||
int main(void);
|
|
||||||
|
|
||||||
__attribute__((naked, section(".text.init"), used, noreturn)) void _start(void)
|
|
||||||
{
|
|
||||||
__asm__ volatile(
|
|
||||||
"lui sp, %hi(__stack_top)\n\t"
|
|
||||||
"addi sp, sp, %lo(__stack_top)\n\t"
|
|
||||||
"lui t0, %hi(main)\n\t"
|
|
||||||
"addi t0, t0, %lo(main)\n\t"
|
|
||||||
"jalr x0, t0, 0\n\t");
|
|
||||||
}
|
|
||||||
|
|
||||||
/* 清 .bss(纯 C)。上电时 FPGA 里的 RAM 本来就是 0,这一步只是保险。 */
|
|
||||||
extern unsigned __bss_start[];
|
|
||||||
extern unsigned __bss_end[];
|
|
||||||
|
|
||||||
void crt_zero_bss(void)
|
|
||||||
{
|
|
||||||
unsigned *p = __bss_start;
|
|
||||||
while (p < __bss_end)
|
|
||||||
{
|
|
||||||
*p = 0u;
|
|
||||||
p = p + 1u;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,174 +0,0 @@
|
|||||||
# =============================================================================
|
|
||||||
# build.ps1 —— 扫雷程序一键构建:C -> 汇编/目标文件 -> ELF -> .hex + .vh
|
|
||||||
#
|
|
||||||
# 用法(在 saolei\ 目录下):
|
|
||||||
# powershell -ExecutionPolicy Bypass -File tools\build.ps1
|
|
||||||
# powershell -ExecutionPolicy Bypass -File tools\build.ps1 -Prefix riscv64-unknown-elf-
|
|
||||||
#
|
|
||||||
# 产物(都在 saolei\build\ 下):
|
|
||||||
# saolei.lst objdump -d 反汇编清单(这就是"编译出来的汇编",可以直接看)
|
|
||||||
# saolei.elf 可执行文件
|
|
||||||
# saolei.bin 裸二进制
|
|
||||||
# saolei.hex $readmemh 格式:拷到 pipelined-cpu\srcs\rom\saolei.hex 就能
|
|
||||||
# 用现成的测试平台(tb_no_intr 等)仿真
|
|
||||||
# saolei.map 链接 map(看段地址和大小)
|
|
||||||
# 并且会生成 pipelined-cpu\srcs\board\cpu21_saolei_rom.vh —— 供 cpu21_board_top.v 用
|
|
||||||
# `include 固化进 ROM(在 Vivado 里定义宏 SAOLEI_ROM 即可切换到扫雷程序)
|
|
||||||
#
|
|
||||||
# 构建流程里有三道针对这颗 CPU 的关卡:
|
|
||||||
# 1) -fno-pic -mcmodel=medlow -mno-relax:避免 auipc / 链接器松弛
|
|
||||||
# 2) 链接脚本里的 ASSERT:禁止 .data / .rodata(这颗核读不到 ROM 里的数据)
|
|
||||||
# 3) tools\check_isa.py:逐条检查反汇编,凡是用到未实现指令(auipc/lb/lbu/div...)
|
|
||||||
# 直接报错并给出修复提示
|
|
||||||
# =============================================================================
|
|
||||||
param(
|
|
||||||
[string]$Prefix = "",
|
|
||||||
[int]$RomWords = 8192,
|
|
||||||
[switch]$SkipIsaCheck
|
|
||||||
)
|
|
||||||
|
|
||||||
$ErrorActionPreference = 'Stop'
|
|
||||||
|
|
||||||
$root = Split-Path -Parent $PSScriptRoot
|
|
||||||
$sw = Join-Path $root 'sw'
|
|
||||||
$build = Join-Path $root 'build'
|
|
||||||
New-Item -ItemType Directory -Force $build | Out-Null
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 工具链探测
|
|
||||||
if (-not $Prefix) {
|
|
||||||
foreach ($cand in 'riscv-none-elf-', 'riscv64-unknown-elf-', 'riscv32-unknown-elf-',
|
|
||||||
'riscv-none-embed-', 'riscv64-linux-gnu-') {
|
|
||||||
if (Get-Command ($cand + 'gcc') -ErrorAction SilentlyContinue) { $Prefix = $cand; break }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (-not $Prefix) {
|
|
||||||
Write-Output "ERROR: 找不到 RISC-V 交叉编译器(riscv*-gcc)。"
|
|
||||||
Write-Output ""
|
|
||||||
Write-Output "装一个即可(推荐 xPack 的 riscv-none-elf-gcc,解压就用,不用管理员权限):"
|
|
||||||
Write-Output " 1) 打开 https://github.com/xpack-dev-tools/riscv-none-elf-gcc-xpack/releases"
|
|
||||||
Write-Output " 2) 下载 win32-x64 的 .zip(例如 xpack-riscv-none-elf-gcc-14.2.0-3-win32-x64.zip)"
|
|
||||||
Write-Output " 3) 解压到例如 C:\tools\riscv-none-elf-gcc"
|
|
||||||
Write-Output " 4) 把 C:\tools\riscv-none-elf-gcc\bin 加进 PATH(或者直接运行本脚本时):"
|
|
||||||
Write-Output " powershell -ExecutionPolicy Bypass -File tools\build.ps1 -Prefix 'C:\tools\riscv-none-elf-gcc\bin\riscv-none-elf-'"
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
|
|
||||||
$GCC = $Prefix + 'gcc'
|
|
||||||
$OBJDUMP = $Prefix + 'objdump'
|
|
||||||
$OBJCOPY = $Prefix + 'objcopy'
|
|
||||||
$SIZE = $Prefix + 'size'
|
|
||||||
|
|
||||||
Write-Output "工具链前缀: $Prefix"
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 编译参数
|
|
||||||
# -march=rv32im_zicsr : RV32I + M 扩展 + Zicsr(CSR 指令:csrrw/csrrsi/csrrci)。
|
|
||||||
# GCC 15 起不再默认给 rv32im 开 zicsr,中断入口里的
|
|
||||||
# csrrsi 必须显式打开这个扩展才认。
|
|
||||||
# -mcmodel=medlow -fno-pic : 用 lui/addi 绝对寻址,不产生 auipc
|
|
||||||
# **必须放开链接器松弛**(不要 -mno-relax):gcc 的函数调用默认是
|
|
||||||
# auipc ra, %pcrel_hi(f) + jalr ra, ra, %pcrel_lo(f)
|
|
||||||
# 要靠链接器松弛把它变成一条 jal,否则 auipc 会留在最终代码里(这颗核没有)。
|
|
||||||
# 松弛只会在两种情况下改变指令:把 pcrel 调用变成 jal,或在使用 gp 相对寻址时
|
|
||||||
# 变成 gp 相对访问 —— 后者已被 -msmall-data-limit=0 从根上禁止。
|
|
||||||
# -msmall-data-limit=0 : 不用 gp 相对寻址
|
|
||||||
# -fno-builtin -nostdlib -ffreestanding : 不要 libc(没有环境,也没有 printf)
|
|
||||||
# -fno-jump-tables : 不生成跳转表(跳转表会落在 .rodata,这颗核读不到)
|
|
||||||
$ccCommon = @(
|
|
||||||
'-march=rv32im_zicsr', '-mabi=ilp32',
|
|
||||||
'-mcmodel=medlow',
|
|
||||||
'-fno-pic', '-fno-PIC', '-fno-pie',
|
|
||||||
'-msmall-data-limit=0', '-fno-common',
|
|
||||||
'-fno-builtin', '-fno-stack-protector', '-fno-jump-tables',
|
|
||||||
'-ffreestanding', '-nostdlib',
|
|
||||||
'-ffunction-sections', '-fdata-sections',
|
|
||||||
'-Os', '-Wall', '-Wextra'
|
|
||||||
)
|
|
||||||
|
|
||||||
# 中断主体(isr.c)必须只使用 callee-saved 寄存器,否则会破坏被打断的代码。
|
|
||||||
# 下面把所有"调用者保存"的通用寄存器都固定住,GCC 就只能用 s0-s11 和 sp。
|
|
||||||
$fixedRegs = @()
|
|
||||||
foreach ($r in 1, 3, 4, 5, 6, 7, 10, 11, 12, 13, 14, 15, 16, 17, 28, 29, 30, 31) {
|
|
||||||
$fixedRegs += ('-ffixed-x' + $r)
|
|
||||||
}
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 编译
|
|
||||||
$objs = @()
|
|
||||||
|
|
||||||
# 跑外部命令并在非 0 退出码时立刻停下(native 命令失败不会抛异常,必须查 $LASTEXITCODE)
|
|
||||||
function Invoke-Tool {
|
|
||||||
param([string]$Exe, [string[]]$ArgList, [string]$What)
|
|
||||||
& $Exe @ArgList
|
|
||||||
if ($LASTEXITCODE -ne 0) {
|
|
||||||
Write-Output ("ERROR: 失败 -> " + $What)
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Write-Output "编译 start.c ..."
|
|
||||||
Invoke-Tool -Exe $GCC -ArgList ($ccCommon + @('-c', (Join-Path $sw 'start.c'), '-o', (Join-Path $build 'start.o'))) -What 'start.c'
|
|
||||||
$objs += (Join-Path $build 'start.o')
|
|
||||||
|
|
||||||
Write-Output "编译 saolei.c ..."
|
|
||||||
Invoke-Tool -Exe $GCC -ArgList ($ccCommon + @('-c', (Join-Path $sw 'saolei.c'), '-o', (Join-Path $build 'saolei.o'))) -What 'saolei.c'
|
|
||||||
$objs += (Join-Path $build 'saolei.o')
|
|
||||||
|
|
||||||
Write-Output "编译 isr.c(额外加 -ffixed 约束)..."
|
|
||||||
Invoke-Tool -Exe $GCC -ArgList ($ccCommon + $fixedRegs + @('-c', (Join-Path $sw 'isr.c'), '-o', (Join-Path $build 'isr.o'))) -What 'isr.c'
|
|
||||||
$objs += (Join-Path $build 'isr.o')
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 链接
|
|
||||||
$elf = Join-Path $build 'saolei.elf'
|
|
||||||
$map = Join-Path $build 'saolei.map'
|
|
||||||
$ld = Join-Path $sw 'linker.ld'
|
|
||||||
|
|
||||||
Write-Output "链接 ..."
|
|
||||||
# 注意:PowerShell 会把未加引号的 "-Wl,--no-relax" 当成参数对解析(逗号引起),
|
|
||||||
# 所以这里统一用参数数组,所有选项都带引号。
|
|
||||||
$linkArgs = @(
|
|
||||||
'-march=rv32im_zicsr', '-mabi=ilp32', '-nostdlib', '-nostartfiles',
|
|
||||||
"-Wl,-Map=$map",
|
|
||||||
'-T', $ld, '-o', $elf
|
|
||||||
) + $objs
|
|
||||||
Invoke-Tool -Exe $GCC -ArgList $linkArgs -What '链接(-T sw/linker.ld)'
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 反汇编 + ISA 检查
|
|
||||||
$lst = Join-Path $build 'saolei.lst'
|
|
||||||
& $OBJDUMP -d -M no-aliases $elf | Out-File -Encoding utf8 $lst
|
|
||||||
Write-Output "反汇编清单: $lst"
|
|
||||||
|
|
||||||
$isaOk = $true
|
|
||||||
if (-not $SkipIsaCheck) {
|
|
||||||
Write-Output ""
|
|
||||||
& py (Join-Path $PSScriptRoot 'check_isa.py') $lst
|
|
||||||
if ($LASTEXITCODE -ne 0) { $isaOk = $false }
|
|
||||||
}
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 转 hex / vh
|
|
||||||
$bin = Join-Path $build 'saolei.bin'
|
|
||||||
$hex = Join-Path $build 'saolei.hex'
|
|
||||||
$vh = Join-Path $root '..\pipelined-cpu\srcs\board\cpu21_saolei_rom.vh'
|
|
||||||
|
|
||||||
& $OBJCOPY -O binary $elf $bin
|
|
||||||
& py (Join-Path $PSScriptRoot 'elf2hex.py') $bin $hex $RomWords $vh
|
|
||||||
|
|
||||||
# ---------------------------------------------------------------- 汇总
|
|
||||||
Write-Output ""
|
|
||||||
Write-Output "==== 段大小 ===="
|
|
||||||
& $SIZE $elf
|
|
||||||
|
|
||||||
$isrLine = Select-String -Path $lst -Pattern '^[0-9a-f]+ <(isr1_entry|isr2_entry|isr3_entry|_start)>:' |
|
|
||||||
Select-Object -ExpandProperty Line
|
|
||||||
Write-Output "==== 入口地址 ===="
|
|
||||||
foreach ($l in $isrLine) { Write-Output (" " + $l) }
|
|
||||||
Write-Output " 三个中断入口必须和顶层 cpu21_board_top 的参数一致:"
|
|
||||||
Write-Output " linker.ld 的 .text.isr1/2/3 = IRQ1/2/3_VECTOR = 0x100/0x200/0x300"
|
|
||||||
|
|
||||||
Write-Output ""
|
|
||||||
if ($isaOk) {
|
|
||||||
Write-Output "构建完成:$hex 与 $vh 已生成。"
|
|
||||||
Write-Output " 仿真:把 saolei.hex 拷到 pipelined-cpu\srcs\rom\saolei.hex,用 tb_no_intr 加载"
|
|
||||||
Write-Output " 上板:cpu21_saolei_rom.vh 已生成到 srcs/board/,Vivado 里定义宏 SAOLEI_ROM 后综合"
|
|
||||||
} else {
|
|
||||||
Write-Output "构建完成,但 ISA 检查没通过(见上面的清单),这个 hex 放到 CPU 上会跑飞。"
|
|
||||||
exit 1
|
|
||||||
}
|
|
||||||
@@ -1,113 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""check_isa.py —— 校验反汇编清单里没有这颗 CPU 未实现的指令。
|
|
||||||
|
|
||||||
用法: python tools/check_isa.py <objdump输出.txt>
|
|
||||||
|
|
||||||
为什么需要它:本 CPU(srcs/rtl/cpu21_riscv_decoder.v)只实现了 RV32I 的一个
|
|
||||||
子集,最容易踩的三个坑是:
|
|
||||||
* 没有 auipc -> 编译器一旦生成它,整段代码就废了(多为 PIC/rodata 引起)
|
|
||||||
* 没有 lb/lh/lbu/lhu -> 只要碰 char/short 类型的读就会出现
|
|
||||||
* 没有有符号 div/rem(只有 divu/remu) -> 有符号除法/取余会被静默算错
|
|
||||||
|
|
||||||
所以这里用"白名单"方式检查:出现名单外的助记符就报错并给出修复提示。
|
|
||||||
退出码 = 违规条数(>255 时截断),0 表示通过。
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
# 这颗 CPU 真正实现了的指令(见 cpu21_riscv_decoder.v)
|
|
||||||
ALLOWED = {
|
|
||||||
# R 型
|
|
||||||
"add", "sub", "sll", "slt", "sltu", "xor", "srl", "sra", "or", "and",
|
|
||||||
# M 扩展(只有这几个)
|
|
||||||
"mul", "mulh", "mulhsu", "mulhu", "divu", "remu",
|
|
||||||
# I 型
|
|
||||||
"addi", "slti", "sltiu", "xori", "ori", "andi", "slli", "srli", "srai",
|
|
||||||
# 访存:只有 lw / sw / sb(没有 lb/lh/lbu/lhu/sh)
|
|
||||||
"lw", "sw", "sb",
|
|
||||||
# 控制流
|
|
||||||
"beq", "bne", "blt", "bge", "bltu", "bgeu", "jal", "jalr",
|
|
||||||
# 高位立即数
|
|
||||||
"lui",
|
|
||||||
# 系统
|
|
||||||
"ecall", "uret", "csrrw", "csrrsi", "csrrci",
|
|
||||||
}
|
|
||||||
|
|
||||||
# objdump 有时仍打印伪指令;这些展开后都是上面支持的指令
|
|
||||||
ALIAS_OK = {
|
|
||||||
"nop", "mv", "li", "j", "jr", "ret", "neg", "not",
|
|
||||||
"beqz", "bnez", "blez", "bgez", "bltz", "bgtz",
|
|
||||||
"seqz", "snez", "sltz", "sgtz",
|
|
||||||
}
|
|
||||||
|
|
||||||
# 违规时给出针对性提示
|
|
||||||
HINTS = {
|
|
||||||
"auipc": "出现了 auipc:说明编译带了 PIC/PIE 或访问了只读数据。"
|
|
||||||
"请用 -fno-pic -mcmodel=medlow,并去掉字符串/常量表(.rodata)",
|
|
||||||
"lb": "没有 lb:不要用 char/short 类型读内存,改用 32 位字",
|
|
||||||
"lh": "没有 lh:同上",
|
|
||||||
"lbu": "没有 lbu:不要用 unsigned char/short 类型读内存(写显存可以用 sb,但别读回来)",
|
|
||||||
"lhu": "没有 lhu:同上",
|
|
||||||
"sh": "没有 sh:只用 sw / sb",
|
|
||||||
"div": "没有有符号 div:改用 unsigned 运算(unsigned 除法会编成 divu)",
|
|
||||||
"rem": "没有有符号 rem:改用 unsigned 运算(unsigned 取余会编成 remu)",
|
|
||||||
"divw": "不支持 64 位运算",
|
|
||||||
"remw": "不支持 64 位运算",
|
|
||||||
"call": "call 会展开成 auipc+jalr:请直接用普通函数调用(jal)",
|
|
||||||
"tail": "tail 会展开成 auipc:请用普通跳转",
|
|
||||||
"fence": "没有 fence/fence.i",
|
|
||||||
"ebreak": "没有 ebreak",
|
|
||||||
"mret": "这颗核的中断返回是 uret,不是 mret",
|
|
||||||
"sret": "这颗核的中断返回是 uret",
|
|
||||||
"wfi": "没有 wfi:主循环只能用轮询",
|
|
||||||
"unimp": "出现了 unimp(非法指令/内联汇编占位)",
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
if len(sys.argv) < 2:
|
|
||||||
print("用法: python tools/check_isa.py <objdump输出.txt>")
|
|
||||||
return 2
|
|
||||||
|
|
||||||
path = sys.argv[1]
|
|
||||||
total = 0
|
|
||||||
violations = []
|
|
||||||
|
|
||||||
with open(path, encoding="utf-8", errors="replace") as f:
|
|
||||||
for line in f:
|
|
||||||
if "\t" not in line:
|
|
||||||
continue
|
|
||||||
parts = line.split("\t")
|
|
||||||
if len(parts) < 3 or not parts[0].strip().endswith(":"):
|
|
||||||
continue
|
|
||||||
mnemonic = parts[2].strip().split()[0]
|
|
||||||
if not mnemonic:
|
|
||||||
continue
|
|
||||||
total += 1
|
|
||||||
if (mnemonic not in ALLOWED) and (mnemonic not in ALIAS_OK):
|
|
||||||
violations.append((parts[0].strip(), mnemonic, line.rstrip()))
|
|
||||||
|
|
||||||
print("检查了 %d 条指令。" % total)
|
|
||||||
|
|
||||||
if not violations:
|
|
||||||
print("ISA 检查通过:全部指令都在本 CPU 的实现范围内。")
|
|
||||||
return 0
|
|
||||||
|
|
||||||
print("")
|
|
||||||
print("!! ISA 检查失败:有 %d 条指令这颗 CPU 没有实现:" % len(violations))
|
|
||||||
seen = set()
|
|
||||||
for addr, mnemonic, raw in violations[:40]:
|
|
||||||
print(" %s %s" % (addr, raw.strip()))
|
|
||||||
seen.add(mnemonic)
|
|
||||||
if len(violations) > 40:
|
|
||||||
print(" ...(只列出前 40 条)")
|
|
||||||
|
|
||||||
print("")
|
|
||||||
print("修复提示:")
|
|
||||||
for m in sorted(seen):
|
|
||||||
print(" * %s: %s" % (m, HINTS.get(m, "不在本 CPU 的指令白名单里")))
|
|
||||||
return min(len(violations), 255)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
sys.exit(main())
|
|
||||||
@@ -1,66 +0,0 @@
|
|||||||
#!/usr/bin/env python3
|
|
||||||
"""elf2hex.py —— 把 objcopy 出来的裸二进制转成 ROM 需要的两份文件。
|
|
||||||
|
|
||||||
用法: python tools/elf2hex.py <input.bin> <output.hex> [ROM_WORDS] [output.vh]
|
|
||||||
|
|
||||||
产出:
|
|
||||||
1) <output.hex> 每行一个 32 位字($readmemh 格式,供 srcs/rom/ 下的
|
|
||||||
测试平台加载,例如拷成 srcs/rom/saolei.hex)
|
|
||||||
2) <output.vh>(可选) rom[i] = 32'h...; 语句,供
|
|
||||||
pipelined-cpu/srcs/board/cpu21_board_top.v 里 `include
|
|
||||||
固化进片内 ROM(未用到的地址填 NOP 0x00000013)
|
|
||||||
(未用到的地址填 NOP 0x00000013)
|
|
||||||
|
|
||||||
字序:输入的 .bin 是按字节小端排列的机器码,这里按小端拼成 32 位字。
|
|
||||||
"""
|
|
||||||
|
|
||||||
import sys
|
|
||||||
|
|
||||||
NOP = 0x00000013
|
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
if len(sys.argv) < 3:
|
|
||||||
print("用法: python tools/elf2hex.py <input.bin> <output.hex> [ROM_WORDS] [output.vh]")
|
|
||||||
return 2
|
|
||||||
|
|
||||||
src = sys.argv[1]
|
|
||||||
dst = sys.argv[2]
|
|
||||||
words_total = int(sys.argv[3]) if len(sys.argv) > 3 else 8192
|
|
||||||
vh_path = sys.argv[4] if len(sys.argv) > 4 else None
|
|
||||||
|
|
||||||
with open(src, "rb") as f:
|
|
||||||
data = f.read()
|
|
||||||
|
|
||||||
if len(data) % 4 != 0:
|
|
||||||
print("警告:二进制长度 %d 不是 4 的倍数,尾部会被补 0" % len(data))
|
|
||||||
data += b"\x00" * (4 - len(data) % 4)
|
|
||||||
|
|
||||||
words = []
|
|
||||||
for i in range(0, len(data), 4):
|
|
||||||
words.append(int.from_bytes(data[i:i + 4], "little"))
|
|
||||||
|
|
||||||
if len(words) > words_total:
|
|
||||||
print("错误:程序 %d 个字,超过 ROM 容量 %d 个字(改大 ROM_WORDS 或精简程序)"
|
|
||||||
% (len(words), words_total))
|
|
||||||
return 1
|
|
||||||
|
|
||||||
with open(dst, "w", encoding="ascii") as f:
|
|
||||||
for w in words:
|
|
||||||
f.write("%08X\n" % w)
|
|
||||||
|
|
||||||
if vh_path:
|
|
||||||
with open(vh_path, "w", encoding="utf-8") as f:
|
|
||||||
f.write(" // 由 tools/elf2hex.py 生成,请勿手工编辑\n")
|
|
||||||
f.write(" // 源文件:%s(%d 个字,其余填 NOP)\n" % (src, len(words)))
|
|
||||||
for addr in range(words_total):
|
|
||||||
w = words[addr] if addr < len(words) else NOP
|
|
||||||
f.write(" rom[%d] = 32'h%08X;\n" % (addr, w))
|
|
||||||
|
|
||||||
print("已生成 %s(%d 个字)%s" % (dst, len(words),
|
|
||||||
("、%s(%d 个字,NOP 填充)" % (vh_path, words_total)) if vh_path else ""))
|
|
||||||
return 0
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
|
||||||
sys.exit(main())
|
|
||||||
Reference in New Issue
Block a user