1) cpu21_pointer.v: sel 的位映射写错了(pend[2]->bit1, pend[1]->bit0, pend[0]->bit2), 而输出读的是 sel[1]=-dx / sel[2]=+dy / sel[3]=-dy / sel[0]=+dx。 后果:方向全乱(按下变左移、按右变下移),更致命的是清除逻辑 press_pend_q <= pend_next & ~sel 清不掉被选中的那一位 -> ev_valid_o 永久为 1 -> 鼠标中断线一直挂着 -> CPU 只收到一个上升沿、之后再无事件。改成一一对应。 2) cpu21_board_top.v: 鼠标中断与 LEVEL 软件中断的接线正好对调 (mouse 接 irq_vec[0]=IRQ1=0x100=关卡入口, level 接 irq_vec[1]=IRQ2=0x200=鼠标)。 后果:第一个鼠标事件把关卡叫起来画棋盘, 而鼠标 ISR 永不执行 -> FIFO 不弹空。 按 linker.ld/mmio.h 约定交换;拨码开关 SW[2:0] 仍按位 OR 进来。 3) cpu21_mmio.v: 同步中断线注释。 仿真验证(tb_saolei + fb2png): 事件队列正常出入(h/t 同步增长)、光标方向正确、 点击 START 进关卡、关卡内点击揭开格子(fb_we +90)、数字场 1/2/3 与大片 BFS 展开均正确
110 lines
4.4 KiB
Verilog
110 lines
4.4 KiB
Verilog
`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;
|
||
// 注意:sel 的每一位必须和下面的输出一致,也必须和 pend_next 一一对应!
|
||
// sel[4]=点击 sel[3]=上(-dy) sel[2]=下(+dy) sel[1]=左(-dx) sel[0]=右(+dx)
|
||
// 而且 press_pend_q <= pend_next & ~sel 要求 sel 的位数与 pend_next 同域,
|
||
// 否则被选中的那一位清不掉、ev_valid_o 会永久为 1(中断线一直挂着)。
|
||
if (pend_next[4])
|
||
sel = 5'b10000; // BTNC:点击
|
||
else if (pend_next[3])
|
||
sel = 5'b01000; // BTNU:上
|
||
else if (pend_next[2])
|
||
sel = 5'b00100; // BTND:下
|
||
else if (pend_next[1])
|
||
sel = 5'b00010; // BTNL:左
|
||
else if (pend_next[0])
|
||
sel = 5'b00001; // 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
|