feat(saolei): 显存扩成每格 2 字节 + 内置 A-Z 字模;开始界面加文字、难度选择和黑背景

显示(RTL)
- cpu21_vga.v:显存从"每格 1 字节"改成"每格 2 字节"(第 1 字节是 6 位字形、
  第 2 字节是 3 位颜色),字地址由 cidx[10:2] 改为 cidx[10:1](1 字 = 2 格);
  字模表由 4 位扩到 6 位(0..63),新增 A-Z 大写字母(16..41),原 0..15 的
  数字/雷/旗/问号/横线/实心字形不变
- cpu21_mmio.v:显存窗口 0x2000-0x27FF -> 0x2000-0x2FFF(300 -> 600 字),
  fb_widx 相应取 addr_i[11:2]
- cpu21_board_top.v:FB_WORDS 300 -> 600(1200 -> 2400 字节)

软件
- mmio.h:fb_write() -> fb_write_cell(idx, glyph, color)(一格写两个 sb);
  删掉 CELL(),新增 G_A..G_Z 字形常量
- saolei.c:draw_cell / clear_screen 改用 fb_write_cell;开始界面显示
  MINES / EASY / NORMAL / HARD / START 文字(START 直接写在绿色按钮条上),
  不再是"两排雷图案 + 纯色条"
- 难度选择:新增 g_difficulty / g_mines,三档 20 / 40 / 60 雷;place_mines、
  胜利用的 opened_cnt 比较、toggle_flag 的剩余雷数都改读 g_mines;点难度那一行
  即切换并重画菜单,点 START 才把雷数交给关卡
- 全部索引运算仍只用移位/掩码(棋盘固定 16x16),没有引入除法

配色(修"数字 1 看不见")
- 数字 1 用 C_BLUE、游戏背景原来也是 C_BLUE,调色板里是同一个深蓝,所以数字 1
  完全看不见;现在菜单与关卡背景统一改成 C_BLACK,胜负闪动仍是绿/红/黑,
  数字本身的配色不动

工具与文档
- build.ps1:Python 启动器兼容 —— 有 py 用 py,没有就回退到 python
  (本机只有 python,之前 check_isa.py / elf2hex.py 两处都会中断)
- saolei/README.md:显存表(0x2000-0x2FFF、每格 2 字节)、格子编码(6 位字形、
  16..41 是 A-Z)、难度说明同步更新
- README.md / cpu21_board_top.v:仅格式化工具重排(Markdown 表格对齐、
  if/else 合并成一行),无语义变化

验证:build.ps1 全流程通过(972 条指令 ISA 检查全过;text=3888 B / data=0 /
bss=1444 B;入口 _start=0x0、isr1/2/3=0x100/0x200/0x300);xvlog 不带宏与带
SAOLEI_ROM 两种配置编译均无错;tools/check_rtl_text.ps1 体检 OK。
未验证:未跑行为仿真、未上板。
This commit is contained in:
2026-09-23 19:23:45 +08:00
parent db0a174bb7
commit cfcf2f4923
9 changed files with 2010 additions and 1877 deletions
+35 -7
View File
@@ -24,7 +24,7 @@
* 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 字节
* 0x0000_2000 - 0x0000_2FFF 显存:40 列 x 30 行,每格 2 字节,共 2400 字节
*
* 鼠标事件单位:1/8 格(按键一次 = 8 = 移动一格;PS/2 鼠标用原始计数)。
* 左键点击 -> 事件的 btn 位 0 置 1;右键(PS/2)-> 位 1。
@@ -69,13 +69,15 @@
#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)
/* 显存按字节写(只能 sb,不能 lb —— 这颗核没有 lb/lbu)。每个格子占两个字节。 */
static inline void fb_write_cell(unsigned idx, unsigned glyph, unsigned color)
{
*(volatile unsigned char *)(FB_BASE + idx) = (unsigned char)(val & 0xFFu);
unsigned addr = FB_BASE + (idx << 1);
*(volatile unsigned char *)addr = (unsigned char)(glyph & 0x3Fu);
*(volatile unsigned char *)(addr + 1u) = (unsigned char)(color & 0x07u);
}
/* ---- 格子编码:1 字节 = [3:0] 字形 [6:4] 颜色 [7] 保留 ---- */
/* ---- 格子编码:两个字节 = 字形[5:0] + 颜色[2:0] ---- */
#define G_BLANK 0u /* 空 */
#define G_N1 1u /* 数字 1..8 = 1..8 */
#define G_COVER 9u /* 未揭开 */
@@ -86,6 +88,34 @@ static inline void fb_write(unsigned idx, unsigned val)
#define G_BAR 14u /* 横线 */
#define G_SOLID 15u /* 实心 */
/* 大写字母字形编号 16..41,供菜单文字使用。 */
#define G_A 16u
#define G_B 17u
#define G_C 18u
#define G_D 19u
#define G_E 20u
#define G_F 21u
#define G_G 22u
#define G_H 23u
#define G_I 24u
#define G_J 25u
#define G_K 26u
#define G_L 27u
#define G_M 28u
#define G_N 29u
#define G_O 30u
#define G_P 31u
#define G_Q 32u
#define G_R 33u
#define G_S 34u
#define G_T 35u
#define G_U 36u
#define G_V 37u
#define G_W 38u
#define G_X 39u
#define G_Y 40u
#define G_Z 41u
#define C_BLACK 0u
#define C_BLUE 1u
#define C_GREEN 2u
@@ -95,8 +125,6 @@ static inline void fb_write(unsigned idx, unsigned val)
#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)
+89 -23
View File
@@ -1,7 +1,7 @@
/* -----------------------------------------------------------------------------
* saolei.c —— 纯 C 扫雷(RV32IM 子集:只用这颗核实现了的指令)
*
* 显示:把每个格子写成显存里的 1 个字节(MMIO 0x2000 起)
* 显示:把每个格子写成显存里的 2 个字节(MMIO 0x2000 起)
* 输入:鼠标事件(中断驱动)——中断里搬到软件队列,主循环在下面处理
*
* 针对这颗 CPU 的硬约束(很重要,改代码时别踩):
@@ -13,15 +13,14 @@
* 所以本文件所有索引运算都用"2 的幂 + 移位/掩码",一次除法都不用。
* 4) 没有 fence/ebreak 等;中断返回是 uret(在 isr.c 里)。
*
* 棋盘:16 x 16,40 颗雷(16 是 2 的幂 => 索引全用移位,颜色/字形查表也不用除法)
* 棋盘:16 x 16,雷数可选 20/40/60(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 BW 16u /* 棋盘宽(格) */
#define BH 16u /* 棋盘高(格) */
#define NCELLS (BW * BH) /* 256 */
#define BM_WORDS (NCELLS / 32u) /* 8 */
#define BOARD_C0 12u /* 棋盘左上角在网格里的列 */
@@ -42,6 +41,8 @@ static unsigned opened_cnt;
static unsigned flags_cnt;
static unsigned g_state;
static unsigned g_first_done;
static unsigned g_mines;
static unsigned g_difficulty;
static unsigned rnd_state;
static unsigned cur_r, cur_c; /* 光标所在的网格格 */
static int acc_x, acc_y; /* 指针累积量(1/8 格,有符号) */
@@ -67,7 +68,7 @@ static void bclr(unsigned *bm, unsigned i)
static void draw_cell(unsigned gr, unsigned gc, unsigned glyph, unsigned color)
{
fb_write(gr * GRID_COLS + gc, CELL(glyph, color));
fb_write_cell(gr * GRID_COLS + gc, glyph, color);
}
/* 把棋盘格 (r,c) 画出来 */
@@ -118,6 +119,15 @@ static unsigned rnd_next(void)
return rnd_state >> 16;
}
static unsigned difficulty_mines(unsigned difficulty)
{
if (difficulty == 0u)
return 20u;
if (difficulty == 2u)
return 60u;
return 40u;
}
/* ============================ 画面 ============================ */
static void clear_screen(void)
@@ -125,7 +135,7 @@ static void clear_screen(void)
unsigned i;
for (i = 0u; i < (GRID_COLS * GRID_ROWS); i = i + 1u)
{
fb_write(i, 0u);
fb_write_cell(i, G_BLANK, C_BLACK);
}
}
@@ -278,7 +288,7 @@ static void reveal_at(unsigned i)
}
}
if (opened_cnt == (NCELLS - NMINES))
if (opened_cnt == (NCELLS - g_mines))
win();
}
@@ -288,7 +298,7 @@ static void place_mines(unsigned safe)
unsigned sr = safe >> 4;
unsigned sc = safe & 15u;
while (placed < NMINES)
while (placed < g_mines)
{
i = rnd_next() & 255u; /* NCELLS = 256,正好 2 的幂,不需要取模 */
r = i >> 4;
@@ -347,7 +357,7 @@ static void toggle_flag(void)
flags_cnt = flags_cnt + 1u;
draw_tile(r, c, G_FLAG, C_YELLOW);
}
REG_W(LED_ADDR) = NMINES - flags_cnt;
REG_W(LED_ADDR) = g_mines - flags_cnt;
}
/* ============================ 鼠标事件 ============================ */
@@ -385,6 +395,11 @@ static unsigned g_exit; /* 关卡内请求返回 */
#define START_R 20u
#define START_C0 17u
#define START_C1 22u
#define DIFF_EASY_R 14u
#define DIFF_NORMAL_R 16u
#define DIFF_HARD_R 18u
static void draw_start_screen(void);
/* 背景用:光标可以在整个画面上移动 */
static void move_cursor_screen(int dr, int dc)
@@ -420,10 +435,29 @@ static void handle_event(unsigned ev)
{ /* 左键 */
if (g_in_level == 0u)
{
if (cur_r == DIFF_EASY_R)
{
g_difficulty = 0u;
draw_start_screen();
return;
}
if (cur_r == DIFF_NORMAL_R)
{
g_difficulty = 1u;
draw_start_screen();
return;
}
if (cur_r == DIFF_HARD_R)
{
g_difficulty = 2u;
draw_start_screen();
return;
}
/* 背景:点到"开始"按钮 -> 拉起软件中断,跳到 IRQ1 里的关卡程序 */
if ((cur_r == START_R) && (cur_c >= START_C0) && (cur_c <= START_C1))
{
g_in_level = 1u; /* 关卡程序返回时自己清 0 */
g_mines = difficulty_mines(g_difficulty);
REG_W(REG_LEVEL) = 1u;
}
}
@@ -490,24 +524,55 @@ static void handle_event(unsigned ev)
* ========================================================================== */
static void draw_start_screen(void)
{
unsigned i;
unsigned i, selected_color;
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);
}
/* 标题和难度菜单使用 RTL 字模中的大写字母。 */
draw_cell(6u, 18u, G_M, C_RED);
draw_cell(6u, 19u, G_I, C_RED);
draw_cell(6u, 20u, G_N, C_RED);
draw_cell(6u, 21u, G_E, C_RED);
draw_cell(6u, 22u, G_S, C_RED);
/* "开始"按钮:绿色实心条,上下各一条黄线框住它 */
selected_color = C_YELLOW;
if (g_difficulty != 0u)
selected_color = C_GRAY;
draw_cell(DIFF_EASY_R, 18u, G_E, selected_color);
draw_cell(DIFF_EASY_R, 19u, G_A, selected_color);
draw_cell(DIFF_EASY_R, 20u, G_S, selected_color);
draw_cell(DIFF_EASY_R, 21u, G_Y, selected_color);
selected_color = C_YELLOW;
if (g_difficulty != 1u)
selected_color = C_GRAY;
draw_cell(DIFF_NORMAL_R, 16u, G_N, selected_color);
draw_cell(DIFF_NORMAL_R, 17u, G_O, selected_color);
draw_cell(DIFF_NORMAL_R, 18u, G_R, selected_color);
draw_cell(DIFF_NORMAL_R, 19u, G_M, selected_color);
draw_cell(DIFF_NORMAL_R, 20u, G_A, selected_color);
draw_cell(DIFF_NORMAL_R, 21u, G_L, selected_color);
selected_color = C_YELLOW;
if (g_difficulty != 2u)
selected_color = C_GRAY;
draw_cell(DIFF_HARD_R, 18u, G_H, selected_color);
draw_cell(DIFF_HARD_R, 19u, G_A, selected_color);
draw_cell(DIFF_HARD_R, 20u, G_R, selected_color);
draw_cell(DIFF_HARD_R, 21u, G_D, selected_color);
/* "START" 按钮:绿色实心条,上下各一条黄线框住它。 */
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);
}
draw_cell(START_R, 17u, G_S, C_BLACK);
draw_cell(START_R, 18u, G_T, C_BLACK);
draw_cell(START_R, 19u, G_A, C_BLACK);
draw_cell(START_R, 20u, G_R, C_BLACK);
draw_cell(START_R, 21u, G_T, C_BLACK);
/* 下方提示条 */
for (i = 12u; i <= 27u; i = i + 1u)
@@ -561,10 +626,10 @@ void level_main(void)
draw_frame();
draw_all_covered();
draw_cell(RET_R, RET_C, G_BAR, C_YELLOW); /* 左上角黄格 = 返回 */
set_bg(C_BLUE);
set_bg(C_BLACK);
update_cursor_reg();
REG_W(LED_ADDR) = NMINES;
REG_W(REG_SEG) = NMINES; /* 数码管显示剩余雷数 */
REG_W(LED_ADDR) = g_mines;
REG_W(REG_SEG) = g_mines; /* 数码管显示剩余雷数 */
/* ---- 关卡主循环 ---- */
while (g_exit == 0u)
@@ -595,7 +660,7 @@ void level_main(void)
}
else
{
set_bg(C_BLUE);
set_bg(C_BLACK);
}
}
}
@@ -615,6 +680,7 @@ int main(void)
crt_zero_bss(); /* 清 .bss(上电 RAM 本来就是 0,保险起见) */
rnd_state = REG_R(REG_TIME) | 1u; /* 用自由计数器当随机种子 */
g_difficulty = 1u; /* 默认普通:40 颗雷 */
REG_W(REG_MCTRL) = 1u; /* 打开鼠标中断(IRQ2,入口 0x200) */
REG_W(REG_LEVEL) = 0u; /* 清关卡软中断 */