Basys 3 RISC-V computer
The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and traps, interrupts, two hardware triggers and a resumable NMI for the monitor. Passes riscv-tests rv32ui/um/mi. rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor ROM. BASIC, the five games and the monitor (with an RV32 disassembler, assembler, breakpoints and single step) are ported to RISC-V GCC. Python emulator and tests in tools/; full-system simulation in sim/. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
5930b958b4
103 files changed
+55677
No files matched your search
@@ -0,0 +1,502 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// Basys 3 VT220 terminal with ANSI colour, and a RISC-V computer running BASIC.
|
||||
// SW15 down: keyboard (USB HID port) -> UART TX (USB-UART port),
|
||||
// UART RX -> 80x30 colour text on VGA
|
||||
// SW15 up: the terminal is the computer's console
|
||||
//
|
||||
// SW1:0 baud rate: 00 115200, 01 9600, 10 921600, 11 3000000 (8N1)
|
||||
// SW2 local echo
|
||||
// SW3 received LF also does CR
|
||||
// SW5:4 default text colour: 00 green, 01 yellow, 10 white, 11 cyan
|
||||
// SW6 Backspace sends DEL (7F) instead of BS (08)
|
||||
// SW7 Enter sends CR LF instead of CR
|
||||
// SW8 smooth scrolling (also turned on by ESC [ ? 4 h)
|
||||
// SW9 fast smooth scrolling
|
||||
// SW14 in computer mode, link the PC's UART too: bytes from the PC also go to the
|
||||
// computer, and the computer's output is copied to the PC
|
||||
// SW15 computer mode
|
||||
// BTNC reset BTNU clear screen BTND reset the computer only
|
||||
// LED7:0 last byte received LED11:8 VT220 LEDs L4..L1 (DECLL)
|
||||
// LED12 bell LED13 Caps Lock LED14 UART RX activity LED15 keyboard activity
|
||||
// 7-seg last scan code (left), last byte sent (right); in computer mode, ports 21h/20h
|
||||
//
|
||||
// The terminal sends XOFF when its input buffer passes 256 bytes and XON when
|
||||
// it drains below 32, so smooth scrolling can hold the host back. In computer mode
|
||||
// it does the same for the computer's input buffer, for text pasted over the UART.
|
||||
|
||||
module basys3_terminal #(
|
||||
parameter FONT_FILE = "font.mem",
|
||||
parameter MEM_FILE = "basic.mem",
|
||||
parameter MEM_HI_FILE = "basic-hi.mem",
|
||||
parameter MON_FILE = "monitor.mem",
|
||||
parameter CLK_HZ = 100_000_000,
|
||||
parameter real CPU_MULT = 9.0, // CPU clock = 100 MHz * CPU_MULT / CPU_DIV
|
||||
parameter real CPU_DIV = 10.0,
|
||||
parameter CPU_HZ = 90_000_000,
|
||||
parameter ROM_TOP = 32'h0000_A000 // BASIC's code is below; writes there are ignored
|
||||
) (
|
||||
input clk,
|
||||
input btnC,
|
||||
input btnU,
|
||||
input btnD,
|
||||
input [15:0] sw,
|
||||
output [15:0] led,
|
||||
output [6:0] seg,
|
||||
output dp,
|
||||
output [3:0] an,
|
||||
output [3:0] vgaRed,
|
||||
output [3:0] vgaGreen,
|
||||
output [3:0] vgaBlue,
|
||||
output Hsync,
|
||||
output Vsync,
|
||||
input RsRx,
|
||||
output RsTx,
|
||||
input PS2Clk,
|
||||
input PS2Data,
|
||||
output QspiCSn,
|
||||
inout [3:0] QspiDB,
|
||||
output [3:0] JA // PmodAMP2: audio in, gain, -, shutdown (low)
|
||||
);
|
||||
localparam [7:0] XON = 8'h11, XOFF = 8'h13;
|
||||
|
||||
// ---------------------------------------------------------------- reset
|
||||
reg [1:0] btn_sync;
|
||||
reg [15:0] por = 16'd0;
|
||||
reg rst = 1'b1;
|
||||
always @(posedge clk) begin
|
||||
btn_sync <= {btn_sync[0], btnC};
|
||||
if (btn_sync[1]) por <= 16'd0;
|
||||
else if (!por[15]) por <= por + 1'b1;
|
||||
rst <= !por[15];
|
||||
end
|
||||
|
||||
reg [15:0] sw_r;
|
||||
reg [1:0] clr_sync, zrst_sync;
|
||||
always @(posedge clk) begin
|
||||
sw_r <= sw;
|
||||
clr_sync <= {clr_sync[0], btnU};
|
||||
zrst_sync <= {zrst_sync[0], btnD};
|
||||
end
|
||||
wire rv_mode = sw_r[15];
|
||||
wire rv_link = sw_r[14];
|
||||
|
||||
reg [13:0] baud_div;
|
||||
always @(posedge clk)
|
||||
case (sw_r[1:0])
|
||||
2'b00: baud_div <= CLK_HZ / 115200;
|
||||
2'b01: baud_div <= CLK_HZ / 9600;
|
||||
2'b10: baud_div <= CLK_HZ / 921600; // 108: 0.5% fast
|
||||
2'b11: baud_div <= CLK_HZ / 3000000; // 33: 1% fast
|
||||
endcase
|
||||
|
||||
reg [3:0] def_fg;
|
||||
always @(posedge clk)
|
||||
case (sw_r[5:4])
|
||||
2'b00: def_fg <= 4'd10; // bright green
|
||||
2'b01: def_fg <= 4'd11; // yellow
|
||||
2'b10: def_fg <= 4'd7; // white
|
||||
2'b11: def_fg <= 4'd14; // bright cyan
|
||||
endcase
|
||||
|
||||
// ------------------------------------------------------------- keyboard
|
||||
wire [7:0] scan;
|
||||
wire scan_valid;
|
||||
wire [7:0] key;
|
||||
wire key_valid;
|
||||
wire caps;
|
||||
wire cursor_app, keypad_app, lnm;
|
||||
|
||||
ps2_rx kbd_rx (
|
||||
.clk(clk), .rst(rst), .ps2_clk(PS2Clk), .ps2_data(PS2Data),
|
||||
.code(scan), .valid(scan_valid)
|
||||
);
|
||||
|
||||
wire cad;
|
||||
ps2_keymap kbd_map (
|
||||
.clk(clk), .rst(rst), .code(scan), .code_valid(scan_valid),
|
||||
.bs_del(sw_r[6]), .enter_crlf(sw_r[7] | lnm),
|
||||
.cursor_app(cursor_app), .keypad_app(keypad_app),
|
||||
.ascii(key), .ascii_valid(key_valid), .caps(caps), .cad(cad)
|
||||
);
|
||||
|
||||
// keys held down, for the computer's game ports (nothing is held outside computer mode)
|
||||
wire [255:0] keys_down;
|
||||
ps2_keystate kbd_state (
|
||||
.clk(clk), .rst(rst | ~rv_mode), .code(scan), .code_valid(scan_valid), .down(keys_down)
|
||||
);
|
||||
|
||||
// Ctrl-Alt-Del in computer mode restarts the computer, like BTND (held 1 us)
|
||||
reg [6:0] cad_t = 7'd0;
|
||||
always @(posedge clk) cad_t <= cad && rv_mode ? 7'd100 : cad_t - (cad_t != 0);
|
||||
|
||||
// ----------------------------------------------------------- host input
|
||||
// Keyboard bytes and terminal replies go to the host: the PC's UART, or in
|
||||
// computer mode the computer's input FIFO. The keyboard/reply choice sticks until
|
||||
// that queue is empty, so multi-byte sequences are never interleaved.
|
||||
// Flow control bytes for the PC go first.
|
||||
wire [7:0] kq, rq;
|
||||
wire kq_empty, rq_empty, rq_full;
|
||||
wire [7:0] resp_byte;
|
||||
wire resp_wr;
|
||||
wire tx_busy;
|
||||
reg tx_start, kq_rd, rq_rd, from_resp;
|
||||
reg [7:0] tx_byte;
|
||||
reg flow_pend;
|
||||
reg [7:0] flow_byte;
|
||||
|
||||
fifo #(.W(8), .A(4)) key_fifo (
|
||||
.clk(clk), .rst(rst), .din(key), .wr(key_valid),
|
||||
.rd(kq_rd), .dout(kq), .empty(kq_empty), .full(), .count()
|
||||
);
|
||||
|
||||
fifo #(.W(8), .A(4)) resp_fifo (
|
||||
.clk(clk), .rst(rst), .din(resp_byte), .wr(resp_wr),
|
||||
.rd(rq_rd), .dout(rq), .empty(rq_empty), .full(rq_full), .count()
|
||||
);
|
||||
|
||||
wire [7:0] h_byte = from_resp ? rq : kq;
|
||||
wire h_avail = from_resp ? !rq_empty : !kq_empty;
|
||||
wire pop_pend = kq_rd | rq_rd; // last byte taken pops this clock
|
||||
|
||||
// computer input FIFO: keyboard/replies, and the PC's UART when linked
|
||||
wire [7:0] rx_byte;
|
||||
wire rx_valid;
|
||||
wire [7:0] zin;
|
||||
wire zin_empty, zin_full;
|
||||
wire [10:0] zin_count;
|
||||
reg zin_rd;
|
||||
wire rx_to_z = rv_mode && rv_link && rx_valid;
|
||||
wire take_z = rv_mode && h_avail && !zin_full && !rx_to_z && !pop_pend;
|
||||
|
||||
fifo #(.W(8), .A(10)) rv_in_fifo (
|
||||
.clk(clk), .rst(rst), .din(rx_to_z ? rx_byte : h_byte), .wr(rx_to_z || take_z),
|
||||
.rd(zin_rd), .dout(zin), .empty(zin_empty), .full(zin_full), .count(zin_count)
|
||||
);
|
||||
|
||||
// computer output copied to the PC when linked
|
||||
wire [7:0] mq;
|
||||
wire mq_empty;
|
||||
wire [10:0] mq_count;
|
||||
reg mq_rd;
|
||||
wire rv_out; // a computer byte is going to the screen
|
||||
wire [7:0] rv_out_byte;
|
||||
|
||||
fifo #(.W(8), .A(10)) mirror_fifo (
|
||||
.clk(clk), .rst(rst), .din(rv_out_byte), .wr(rv_out && rv_link),
|
||||
.rd(mq_rd), .dout(mq), .empty(mq_empty), .full(), .count(mq_count)
|
||||
);
|
||||
|
||||
wire [10:0] tq_count;
|
||||
wire [10:0] flow_count = rv_mode ? zin_count : tq_count;
|
||||
reg xoff_sent;
|
||||
|
||||
always @(posedge clk) begin
|
||||
tx_start <= 1'b0;
|
||||
kq_rd <= 1'b0;
|
||||
rq_rd <= 1'b0;
|
||||
mq_rd <= 1'b0;
|
||||
if (rst) begin
|
||||
from_resp <= 1'b0;
|
||||
flow_pend <= 1'b0;
|
||||
xoff_sent <= 1'b0;
|
||||
end else begin
|
||||
if (!flow_pend && !xoff_sent && flow_count >= 256) begin
|
||||
flow_pend <= 1'b1; flow_byte <= XOFF; xoff_sent <= 1'b1;
|
||||
end else if (!flow_pend && xoff_sent && flow_count < 32) begin
|
||||
flow_pend <= 1'b1; flow_byte <= XON; xoff_sent <= 1'b0;
|
||||
end
|
||||
|
||||
if (take_z) begin
|
||||
kq_rd <= !from_resp;
|
||||
rq_rd <= from_resp;
|
||||
end else if (!h_avail)
|
||||
from_resp <= !from_resp;
|
||||
|
||||
if (!tx_busy && !tx_start) begin
|
||||
if (flow_pend) begin
|
||||
tx_byte <= flow_byte;
|
||||
tx_start <= 1'b1;
|
||||
flow_pend <= 1'b0;
|
||||
end else if (!rv_mode && h_avail && !pop_pend) begin
|
||||
tx_byte <= h_byte;
|
||||
tx_start <= 1'b1;
|
||||
kq_rd <= !from_resp;
|
||||
rq_rd <= from_resp;
|
||||
end else if (rv_mode && !mq_empty && !mq_rd) begin
|
||||
tx_byte <= mq;
|
||||
tx_start <= 1'b1;
|
||||
mq_rd <= 1'b1;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
uart_tx utx (
|
||||
.clk(clk), .rst(rst), .div(baud_div), .data(tx_byte),
|
||||
.start(tx_start), .tx(RsTx), .busy(tx_busy)
|
||||
);
|
||||
|
||||
// -------------------------------------------------------------- receive
|
||||
uart_rx urx (
|
||||
.clk(clk), .rst(rst), .div(baud_div), .rx(RsRx),
|
||||
.data(rx_byte), .valid(rx_valid)
|
||||
);
|
||||
|
||||
// Start-up banner, fed in like received text. Bytes are read from the
|
||||
// high end of the string; the unused high bytes are zero and skipped.
|
||||
localparam [8*200-1:0] BANNER = {
|
||||
"\033[1mBasys 3 VT220 Terminal\033[0m 80x32 ",
|
||||
"\033[31mc\033[33mo\033[32ml\033[36mo\033[34mu\033[35mr\033[0m ",
|
||||
"\033[4munderline\033[0m\015\012",
|
||||
"UART RX -> screen, keyboard -> UART TX TERM=basys3 or vt220\015\012\012"};
|
||||
// (not shown in computer mode, where BASIC prints its own)
|
||||
reg [7:0] ban_i;
|
||||
wire [7:0] ban_c = BANNER[ban_i*8 +: 8];
|
||||
|
||||
// ---------------------------------------------------------------- RISC-V
|
||||
wire cpu_clk, locked;
|
||||
`ifdef SIMULATION
|
||||
assign cpu_clk = clk;
|
||||
assign locked = 1'b1;
|
||||
`else
|
||||
wire cpu_fb, cpu_clk_mmcm;
|
||||
MMCME2_BASE #(
|
||||
.CLKIN1_PERIOD(10.0), .DIVCLK_DIVIDE(1),
|
||||
.CLKFBOUT_MULT_F(CPU_MULT), .CLKOUT0_DIVIDE_F(CPU_DIV)
|
||||
) cpu_mmcm (
|
||||
.CLKIN1(clk), .CLKFBIN(cpu_fb), .CLKFBOUT(cpu_fb), .CLKFBOUTB(),
|
||||
.CLKOUT0(cpu_clk_mmcm), .CLKOUT0B(), .CLKOUT1(), .CLKOUT1B(), .CLKOUT2(),
|
||||
.CLKOUT2B(), .CLKOUT3(), .CLKOUT3B(), .CLKOUT4(), .CLKOUT5(), .CLKOUT6(),
|
||||
.LOCKED(locked), .PWRDWN(1'b0), .RST(1'b0)
|
||||
);
|
||||
BUFG cpu_bufg (.I(cpu_clk_mmcm), .O(cpu_clk));
|
||||
`endif
|
||||
|
||||
// ------------------------------------------------- pixel clock, 108 MHz
|
||||
// 1280x1024 at 60 Hz: 100 MHz x 10.125 / 9.375
|
||||
wire pix_clk;
|
||||
`ifdef SIMULATION
|
||||
assign pix_clk = clk;
|
||||
`else
|
||||
wire pix_fb, pix_clk_mmcm;
|
||||
MMCME2_BASE #(
|
||||
.CLKIN1_PERIOD(10.0), .DIVCLK_DIVIDE(1),
|
||||
.CLKFBOUT_MULT_F(10.125), .CLKOUT0_DIVIDE_F(9.375)
|
||||
) pix_mmcm (
|
||||
.CLKIN1(clk), .CLKFBIN(pix_fb), .CLKFBOUT(pix_fb), .CLKFBOUTB(),
|
||||
.CLKOUT0(pix_clk_mmcm), .CLKOUT0B(), .CLKOUT1(), .CLKOUT1B(), .CLKOUT2(),
|
||||
.CLKOUT2B(), .CLKOUT3(), .CLKOUT3B(), .CLKOUT4(), .CLKOUT5(), .CLKOUT6(),
|
||||
.LOCKED(), .PWRDWN(1'b0), .RST(1'b0)
|
||||
);
|
||||
BUFG pix_bufg (.I(pix_clk_mmcm), .O(pix_clk));
|
||||
`endif
|
||||
(* ASYNC_REG = "TRUE" *) reg [1:0] pix_rst_s;
|
||||
always @(posedge pix_clk) pix_rst_s <= {pix_rst_s[0], rst};
|
||||
|
||||
(* ASYNC_REG = "TRUE" *) reg [2:0] cpu_rst_s;
|
||||
always @(posedge cpu_clk) cpu_rst_s <= {cpu_rst_s[1:0], rst | zrst_sync[1] | (cad_t != 0) | !locked};
|
||||
|
||||
// computer input: one byte at a time from rv_in_fifo
|
||||
reg zrx_req, zrx_pend;
|
||||
reg [7:0] zrx_data;
|
||||
wire zrx_ack;
|
||||
(* ASYNC_REG = "TRUE" *) reg [1:0] zrx_ack_s;
|
||||
always @(posedge clk) begin
|
||||
zin_rd <= 1'b0;
|
||||
zrx_ack_s <= {zrx_ack_s[0], zrx_ack};
|
||||
if (rst)
|
||||
zrx_pend <= 1'b0;
|
||||
else if (!zrx_pend && !zin_empty && !zin_rd) begin
|
||||
zrx_data <= zin;
|
||||
zin_rd <= 1'b1;
|
||||
zrx_req <= ~zrx_req;
|
||||
zrx_pend <= 1'b1;
|
||||
end else if (zrx_pend && zrx_ack_s[1] == zrx_req)
|
||||
zrx_pend <= 1'b0;
|
||||
end
|
||||
initial zrx_req = 1'b0;
|
||||
|
||||
// computer output
|
||||
wire ztx_req;
|
||||
wire [7:0] ztx_data;
|
||||
reg ztx_ack = 1'b0;
|
||||
(* ASYNC_REG = "TRUE" *) reg [1:0] ztx_req_s;
|
||||
always @(posedge clk) ztx_req_s <= {ztx_req_s[0], ztx_req};
|
||||
|
||||
wire [15:0] rv_seg;
|
||||
wire fl_sck;
|
||||
wire [16:0] gfx_addr;
|
||||
wire [3:0] gfx_pix;
|
||||
wire sp_go, sp_buf, sp_on, pix_frame;
|
||||
wire [8:0] scroll_x;
|
||||
wire [7:0] scroll_y;
|
||||
wire g_border;
|
||||
wire [7:0] sp_line;
|
||||
wire [8:0] sp_x;
|
||||
wire [3:0] sp_pix;
|
||||
|
||||
// The Pause key (its scan codes start E1, used by nothing else) enters the
|
||||
// monitor: a toggle crosses to the CPU clock.
|
||||
reg brk_t = 1'b0;
|
||||
always @(posedge clk) if (scan_valid && scan == 8'hE1 && rv_mode) brk_t <= ~brk_t;
|
||||
(* ASYNC_REG = "TRUE" *) reg [2:0] brk_s = 3'd0;
|
||||
always @(posedge cpu_clk) brk_s <= {brk_s[1:0], brk_t};
|
||||
|
||||
rv32_system #(.MEM_FILE(MEM_FILE), .MEM_HI_FILE(MEM_HI_FILE), .MON_FILE(MON_FILE), .CPU_HZ(CPU_HZ), .ROM_TOP(ROM_TOP)) sys (
|
||||
.clk(cpu_clk), .rst(cpu_rst_s[2]),
|
||||
.rx_req(zrx_req), .rx_data(zrx_data), .rx_ack(zrx_ack),
|
||||
.tx_req(ztx_req), .tx_data(ztx_data), .tx_ack(ztx_ack),
|
||||
.seg_val(rv_seg), .halted(), .brk_key(brk_s[2] ^ brk_s[1]),
|
||||
.vclk(pix_clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix),
|
||||
.scroll_x(scroll_x), .scroll_y(scroll_y), .g_border(g_border),
|
||||
.sp_go(sp_go), .sp_line(sp_line), .sp_buf(sp_buf), .sp_x(sp_x), .sp_on(sp_on),
|
||||
.sp_pix(sp_pix), .sp_ftick(pix_frame), .keys(keys_down),
|
||||
.fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1]),
|
||||
.audio(JA[0])
|
||||
);
|
||||
|
||||
// PmodAMP2 on JA's top row: gain high is 6 dB (low would be 12 dB), and
|
||||
// the amplifier is on unless its shutdown pin is low.
|
||||
assign JA[3:1] = 3'b101;
|
||||
|
||||
// The flash clock is the configuration clock pin, reached through STARTUPE2.
|
||||
// DQ2/DQ3 (write protect, hold) stay high.
|
||||
assign QspiDB[1] = 1'bz;
|
||||
assign QspiDB[3:2] = 2'b11;
|
||||
`ifndef SIMULATION
|
||||
STARTUPE2 #(.PROG_USR("FALSE"), .SIM_CCLK_FREQ(0.0)) startup (
|
||||
.CFGCLK(), .CFGMCLK(), .EOS(), .PREQ(),
|
||||
.CLK(1'b0), .GSR(1'b0), .GTS(1'b0), .KEYCLEARB(1'b1), .PACK(1'b0),
|
||||
.USRCCLKO(fl_sck), .USRCCLKTS(1'b0), .USRDONEO(1'b1), .USRDONETS(1'b1)
|
||||
);
|
||||
`endif
|
||||
|
||||
// terminal input: computer output or received bytes, local echo, banner
|
||||
reg echo_pend;
|
||||
reg [7:0] echo_byte;
|
||||
reg [7:0] tin;
|
||||
reg tin_wr;
|
||||
wire tin_full;
|
||||
|
||||
assign rv_out = rv_mode && ztx_req_s[1] != ztx_ack && !tin_wr && tq_count < 1000 &&
|
||||
(!rv_link || mq_count < 1000);
|
||||
assign rv_out_byte = ztx_data;
|
||||
|
||||
always @(posedge clk) begin
|
||||
tin_wr <= 1'b0;
|
||||
if (kq_rd && sw_r[2] && !rv_mode) begin
|
||||
echo_pend <= 1'b1;
|
||||
echo_byte <= kq;
|
||||
end
|
||||
if (rst) begin
|
||||
echo_pend <= 1'b0;
|
||||
ban_i <= rv_mode ? 8'hFF : 8'd199;
|
||||
end else if (rv_mode) begin
|
||||
if (rv_out) begin
|
||||
tin <= ztx_data;
|
||||
tin_wr <= 1'b1;
|
||||
ztx_ack <= ~ztx_ack;
|
||||
end
|
||||
end else if (rx_valid) begin
|
||||
tin <= rx_byte;
|
||||
tin_wr <= 1'b1;
|
||||
end else if (echo_pend && !tin_full) begin
|
||||
tin <= echo_byte;
|
||||
tin_wr <= 1'b1;
|
||||
echo_pend <= kq_rd && sw_r[2] && !rv_mode;
|
||||
end else if (ban_i != 8'hFF && !tin_full && !tin_wr) begin
|
||||
ban_i <= ban_i - 1'b1;
|
||||
if (ban_c != 0) begin
|
||||
tin <= ban_c;
|
||||
tin_wr <= 1'b1;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
wire [7:0] tq;
|
||||
wire tq_empty;
|
||||
wire tq_rd;
|
||||
|
||||
fifo #(.W(8), .A(10)) term_fifo (
|
||||
.clk(clk), .rst(rst), .din(tin), .wr(tin_wr),
|
||||
.rd(tq_rd), .dout(tq), .empty(tq_empty), .full(tin_full), .count(tq_count)
|
||||
);
|
||||
|
||||
// ------------------------------------------------------------- terminal
|
||||
wire [13:0] t_addr;
|
||||
wire alt_scr, wide;
|
||||
wire t_we;
|
||||
wire [17:0] t_wdata, t_rdata;
|
||||
wire [5:0] cur_row, stop, sbot;
|
||||
wire [4:0] sm_off;
|
||||
wire [7:0] cur_col;
|
||||
wire cur_vis, sm_on, sm_dir;
|
||||
wire [3:0] dec_leds;
|
||||
wire bell;
|
||||
wire frame_tick;
|
||||
|
||||
// start of vertical blanking, from the pixel clock to this one
|
||||
reg frame_t = 1'b0;
|
||||
always @(posedge pix_clk) if (pix_frame) frame_t <= ~frame_t;
|
||||
(* ASYNC_REG = "TRUE" *) reg [2:0] frame_s = 3'd0;
|
||||
always @(posedge clk) frame_s <= {frame_s[1:0], frame_t};
|
||||
assign frame_tick = frame_s[2] ^ frame_s[1];
|
||||
|
||||
term_ctrl term (
|
||||
.clk(clk), .rst(rst), .in_byte(tq), .in_empty(tq_empty), .in_rd(tq_rd),
|
||||
.lf_cr(sw_r[3]), .clear_req(clr_sync[1]), .def_fg(def_fg),
|
||||
.smooth_sw(sw_r[8]), .smooth_fast(sw_r[9]), .frame_tick(frame_tick),
|
||||
.addr(t_addr), .we(t_we), .wdata(t_wdata), .rdata(t_rdata),
|
||||
.row(cur_row), .col(cur_col), .cur_vis(cur_vis),
|
||||
.stop(stop), .sbot(sbot), .sm_on(sm_on), .sm_dir(sm_dir), .sm_off(sm_off),
|
||||
.resp_byte(resp_byte), .resp_wr(resp_wr), .resp_full(rq_full),
|
||||
.cursor_app(cursor_app), .keypad_app(keypad_app), .lnm(lnm),
|
||||
.dec_leds(dec_leds), .bell(bell), .alt_scr(alt_scr), .wide(wide)
|
||||
);
|
||||
|
||||
wire [13:0] v_addr;
|
||||
wire [17:0] v_data;
|
||||
|
||||
// The display reads the terminal's cursor and modes across clocks: they
|
||||
// change rarely and a stale value lasts one pixel.
|
||||
// The alternate screen (the monitor uses it) has no graphics layer.
|
||||
char_ram screen (
|
||||
.clk(clk), .a_alt(alt_scr),
|
||||
.a_addr(t_addr), .a_we(t_we), .a_wdata(t_wdata), .a_rdata(t_rdata),
|
||||
.vclk(pix_clk), .b_alt(alt_scr), .b_addr(v_addr), .b_rdata(v_data)
|
||||
);
|
||||
|
||||
text_display #(.FONT_FILE(FONT_FILE)) video (
|
||||
.clk(pix_clk), .rst(pix_rst_s[1]), .wide(wide),
|
||||
.cur_row(cur_row), .cur_col(cur_col), .cur_vis(cur_vis),
|
||||
.stop(stop), .sbot(sbot), .sm_on(sm_on), .sm_dir(sm_dir), .sm_off(sm_off),
|
||||
.ram_addr(v_addr), .ram_data(v_data), .gfx_addr(gfx_addr), .gfx_pix(alt_scr ? 4'd0 : gfx_pix),
|
||||
.scroll_x(scroll_x), .scroll_y(scroll_y), .g_border(g_border),
|
||||
.sp_go(sp_go), .sp_line(sp_line), .sp_buf(sp_buf), .sp_x(sp_x), .sp_on(sp_on),
|
||||
.sp_pix(alt_scr ? 4'd0 : sp_pix),
|
||||
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync),
|
||||
.frame_tick(pix_frame)
|
||||
);
|
||||
|
||||
// ----------------------------------------------------- status displays
|
||||
reg [7:0] last_rx;
|
||||
reg [7:0] last_scan;
|
||||
reg [7:0] last_tx;
|
||||
reg [22:0] rx_led, kb_led, bell_led; // ~84 ms pulse stretchers
|
||||
|
||||
always @(posedge clk) begin
|
||||
if (rv_mode ? tin_wr : rx_valid) last_rx <= rv_mode ? tin : rx_byte;
|
||||
if (scan_valid) last_scan <= scan;
|
||||
if (tx_start) last_tx <= tx_byte;
|
||||
rx_led <= (rv_mode ? tin_wr : rx_valid) ? 23'h7FFFFF : rx_led - (rx_led != 0);
|
||||
kb_led <= scan_valid ? 23'h7FFFFF : kb_led - (kb_led != 0);
|
||||
bell_led <= bell ? 23'h7FFFFF : bell_led - (bell_led != 0);
|
||||
end
|
||||
|
||||
assign led = {kb_led != 0, rx_led != 0, caps, bell_led != 0, dec_leds[3:0], last_rx};
|
||||
assign dp = 1'b1;
|
||||
|
||||
sevenseg digits (.clk(clk), .value(rv_mode ? rv_seg : {last_scan, last_tx}),
|
||||
.seg(seg), .an(an));
|
||||
endmodule
|
||||
+38
@@ -0,0 +1,38 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// Small synchronous first-word-fall-through FIFO (dout is valid while !empty).
|
||||
module fifo #(
|
||||
parameter W = 8,
|
||||
parameter A = 4 // depth = 2**A
|
||||
) (
|
||||
input clk,
|
||||
input rst,
|
||||
input [W-1:0] din,
|
||||
input wr,
|
||||
input rd,
|
||||
output [W-1:0] dout,
|
||||
output empty,
|
||||
output full,
|
||||
output [A:0] count
|
||||
);
|
||||
reg [W-1:0] mem [0:(1<<A)-1];
|
||||
reg [A:0] wp, rp;
|
||||
|
||||
assign empty = wp == rp;
|
||||
assign full = wp[A] != rp[A] && wp[A-1:0] == rp[A-1:0];
|
||||
assign dout = mem[rp[A-1:0]];
|
||||
assign count = wp - rp;
|
||||
|
||||
always @(posedge clk)
|
||||
if (wr && !full) mem[wp[A-1:0]] <= din;
|
||||
|
||||
always @(posedge clk) begin
|
||||
if (rst) begin
|
||||
wp <= 0;
|
||||
rp <= 0;
|
||||
end else begin
|
||||
if (wr && !full) wp <= wp + 1'b1;
|
||||
if (rd && !empty) rp <= rp + 1'b1;
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
+100
@@ -0,0 +1,100 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// SPI port to the configuration flash for the Z-80, with write protection for
|
||||
// the bitstream.
|
||||
//
|
||||
// port 40h out: bit 0 = 1 opens a command, 0 ends it (chip select high)
|
||||
// in: bit 0 = byte in progress, bit 1 = command refused
|
||||
// port 41h out: send a byte (the reply byte is read back from 41h)
|
||||
//
|
||||
// The first byte of a command is checked before the chip is selected. Only
|
||||
// read (03), read status (05), write enable/disable (06/04), read ID (9F), page
|
||||
// program (02) and 4 KB sector erase (20) are allowed. Program and erase are
|
||||
// abandoned after the opcode, before any address byte reaches the flash,
|
||||
// unless the address is at or above PROTECT (a 64 KB boundary), so the
|
||||
// bitstream below it can never be changed by the Z-80.
|
||||
//
|
||||
// SCK is clk/4, SPI mode 0.
|
||||
module flash_spi #(
|
||||
parameter [7:0] PROTECT_HI = 8'h24 // first writable address = PROTECT_HI << 16
|
||||
) (
|
||||
input clk,
|
||||
input rst,
|
||||
input wr_ctl, // port 40h written
|
||||
input wr_dat, // port 41h written
|
||||
input [7:0] wdata,
|
||||
output [7:0] ctl_q, // port 40h read value
|
||||
output [7:0] dat_q, // port 41h read value
|
||||
output reg cs_n,
|
||||
output reg sck,
|
||||
output mosi,
|
||||
input miso
|
||||
);
|
||||
reg [7:0] sh;
|
||||
reg [3:0] nbits;
|
||||
reg [1:0] ph;
|
||||
reg busy, rbit;
|
||||
reg open_, refused, first, guarded;
|
||||
reg [1:0] nbytes;
|
||||
|
||||
assign mosi = sh[7];
|
||||
assign ctl_q = {6'd0, refused, busy};
|
||||
assign dat_q = sh;
|
||||
|
||||
(* IOB = "TRUE" *) reg miso_r;
|
||||
always @(posedge clk) miso_r <= miso;
|
||||
|
||||
task start(input [7:0] b, input [3:0] n);
|
||||
begin sh <= b; nbits <= n; ph <= 2'd0; busy <= 1'b1; end
|
||||
endtask
|
||||
|
||||
always @(posedge clk)
|
||||
if (rst) begin
|
||||
cs_n <= 1'b1; sck <= 1'b0; open_ <= 1'b0; refused <= 1'b0;
|
||||
start(8'hFF, 4'd8); // a few clocks so STARTUPE2 hands over CCLK
|
||||
end else begin
|
||||
if (busy) begin
|
||||
ph <= ph + 1'b1;
|
||||
case (ph)
|
||||
2'd1: sck <= 1'b1;
|
||||
2'd2: rbit <= miso_r;
|
||||
2'd3: begin
|
||||
sck <= 1'b0;
|
||||
sh <= {sh[6:0], rbit};
|
||||
nbits <= nbits - 1'b1;
|
||||
if (nbits == 4'd1) busy <= 1'b0;
|
||||
end
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
|
||||
if (wr_ctl && !busy) begin
|
||||
open_ <= wdata[0];
|
||||
first <= 1'b1;
|
||||
nbytes <= 2'd0;
|
||||
refused <= 1'b0;
|
||||
if (!wdata[0]) cs_n <= 1'b1;
|
||||
end else if (wr_dat && !busy && open_) begin
|
||||
if (nbytes != 2'd3) nbytes <= nbytes + 1'b1;
|
||||
if (refused)
|
||||
sh <= 8'hFF;
|
||||
else if (first) begin
|
||||
first <= 1'b0;
|
||||
guarded <= wdata == 8'h02 || wdata == 8'h20;
|
||||
if (wdata == 8'h03 || wdata == 8'h05 || wdata == 8'h06 || wdata == 8'h04 ||
|
||||
wdata == 8'h9F || wdata == 8'h02 || wdata == 8'h20) begin
|
||||
cs_n <= 1'b0;
|
||||
start(wdata, 4'd8);
|
||||
end else begin
|
||||
refused <= 1'b1;
|
||||
sh <= 8'hFF;
|
||||
end
|
||||
end else if (nbytes == 2'd1 && guarded && wdata < PROTECT_HI) begin
|
||||
refused <= 1'b1; // incomplete command: the flash ignores it
|
||||
cs_n <= 1'b1;
|
||||
sh <= 8'hFF;
|
||||
end else
|
||||
start(wdata, 4'd8);
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
@@ -0,0 +1,268 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// USB keyboard input. The Basys 3's PIC24 USB host turns a USB keyboard into a
|
||||
// PS/2 device (scan code set 2) on PS2Clk/PS2Data; this decodes that to ASCII.
|
||||
|
||||
module ps2_rx (
|
||||
input clk,
|
||||
input rst,
|
||||
input ps2_clk,
|
||||
input ps2_data,
|
||||
output reg [7:0] code,
|
||||
output reg valid
|
||||
);
|
||||
reg [2:0] cs = 3'b111, ds = 3'b111;
|
||||
reg [5:0] fcnt;
|
||||
reg fclk = 1'b1, fclk_d = 1'b1;
|
||||
reg [10:0] sh;
|
||||
reg [3:0] n;
|
||||
reg [17:0] idle;
|
||||
|
||||
always @(posedge clk) begin
|
||||
cs <= {cs[1:0], ps2_clk};
|
||||
ds <= {ds[1:0], ps2_data};
|
||||
// the clock only changes after 64 stable samples, which filters glitches
|
||||
if (cs[2] == fclk)
|
||||
fcnt <= 6'd0;
|
||||
else begin
|
||||
fcnt <= fcnt + 1'b1;
|
||||
if (&fcnt) fclk <= cs[2];
|
||||
end
|
||||
fclk_d <= fclk;
|
||||
end
|
||||
|
||||
wire fall = fclk_d & ~fclk;
|
||||
wire [10:0] frame = {ds[2], sh[10:1]};
|
||||
|
||||
always @(posedge clk) begin
|
||||
valid <= 1'b0;
|
||||
if (rst) begin
|
||||
n <= 4'd0;
|
||||
idle <= 18'd0;
|
||||
end else if (fall) begin
|
||||
sh <= frame;
|
||||
idle <= 18'd0;
|
||||
if (n == 4'd10) begin
|
||||
n <= 4'd0;
|
||||
// start 0, stop 1, odd parity over data + parity bit
|
||||
if (!frame[0] && frame[10] && ^frame[9:1]) begin
|
||||
code <= frame[8:1];
|
||||
valid <= 1'b1;
|
||||
end
|
||||
end else
|
||||
n <= n + 1'b1;
|
||||
end else if (n != 0) begin
|
||||
idle <= idle + 1'b1;
|
||||
if (&idle) n <= 4'd0; // ~2.6 ms without a clock: resync
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
|
||||
// Turns scan codes into ASCII bytes and VT220 key sequences:
|
||||
// arrows ESC [ A..D, or ESC O A..D in cursor-key application mode (DECCKM)
|
||||
// Home End ESC [ 1 ~ ESC [ 4 ~ (VT220 Find / Select)
|
||||
// Ins Del PgUp PgDn ESC [ 2 ~ 3 ~ 5 ~ 6 ~
|
||||
// F1-F4 ESC O P..S F5-F12 ESC [ 15 17 18 19 20 21 23 24 ~
|
||||
// keypad digits, or ESC O p..y etc. in keypad application mode (DECKPAM)
|
||||
// Alt+key ESC prefix
|
||||
module ps2_keymap (
|
||||
input clk,
|
||||
input rst,
|
||||
input [7:0] code,
|
||||
input code_valid,
|
||||
input bs_del, // Backspace sends DEL (7F) instead of BS (08)
|
||||
input enter_crlf, // Enter sends CR LF instead of CR
|
||||
input cursor_app, // DECCKM
|
||||
input keypad_app, // DECKPAM
|
||||
output reg [7:0] ascii,
|
||||
output reg ascii_valid,
|
||||
output reg caps,
|
||||
output reg cad // Ctrl-Alt-Del was pressed (one clock)
|
||||
);
|
||||
localparam [7:0] ESC = 8'h1B;
|
||||
|
||||
reg e0, f0;
|
||||
reg [2:0] skip;
|
||||
reg lsh, rsh, lct, rct, alt;
|
||||
reg [47:0] sq;
|
||||
reg [2:0] cnt;
|
||||
|
||||
// {unshifted, shifted}
|
||||
function [15:0] keymap(input [7:0] c);
|
||||
case (c)
|
||||
8'h0E: keymap = "`~"; 8'h16: keymap = "1!"; 8'h1E: keymap = "2@";
|
||||
8'h26: keymap = "3#"; 8'h25: keymap = "4$"; 8'h2E: keymap = "5%";
|
||||
8'h36: keymap = "6^"; 8'h3D: keymap = "7&"; 8'h3E: keymap = "8*";
|
||||
8'h46: keymap = "9("; 8'h45: keymap = "0)"; 8'h4E: keymap = "-_";
|
||||
8'h55: keymap = "=+"; 8'h0D: keymap = {8'h09, 8'h09};
|
||||
8'h15: keymap = "qQ"; 8'h1D: keymap = "wW"; 8'h24: keymap = "eE";
|
||||
8'h2D: keymap = "rR"; 8'h2C: keymap = "tT"; 8'h35: keymap = "yY";
|
||||
8'h3C: keymap = "uU"; 8'h43: keymap = "iI"; 8'h44: keymap = "oO";
|
||||
8'h4D: keymap = "pP"; 8'h54: keymap = "[{"; 8'h5B: keymap = "]}";
|
||||
8'h5D: keymap = "\\|";
|
||||
8'h1C: keymap = "aA"; 8'h1B: keymap = "sS"; 8'h23: keymap = "dD";
|
||||
8'h2B: keymap = "fF"; 8'h34: keymap = "gG"; 8'h33: keymap = "hH";
|
||||
8'h3B: keymap = "jJ"; 8'h42: keymap = "kK"; 8'h4B: keymap = "lL";
|
||||
8'h4C: keymap = ";:"; 8'h52: keymap = "'\"";
|
||||
8'h1A: keymap = "zZ"; 8'h22: keymap = "xX"; 8'h21: keymap = "cC";
|
||||
8'h2A: keymap = "vV"; 8'h32: keymap = "bB"; 8'h31: keymap = "nN";
|
||||
8'h3A: keymap = "mM"; 8'h41: keymap = ",<"; 8'h49: keymap = ".>";
|
||||
8'h4A: keymap = "/?"; 8'h29: keymap = " "; 8'h76: keymap = {ESC, ESC};
|
||||
// keypad, as if Num Lock were on
|
||||
8'h70: keymap = "00"; 8'h69: keymap = "11"; 8'h72: keymap = "22";
|
||||
8'h7A: keymap = "33"; 8'h6B: keymap = "44"; 8'h73: keymap = "55";
|
||||
8'h74: keymap = "66"; 8'h6C: keymap = "77"; 8'h75: keymap = "88";
|
||||
8'h7D: keymap = "99"; 8'h71: keymap = ".."; 8'h7C: keymap = "**";
|
||||
8'h7B: keymap = "--"; 8'h79: keymap = "++";
|
||||
default: keymap = 16'h0000;
|
||||
endcase
|
||||
endfunction
|
||||
|
||||
// final byte of ESC O x for the keypad in application mode (0 = not a keypad key)
|
||||
function [7:0] kp_app(input [7:0] c);
|
||||
case (c)
|
||||
8'h70: kp_app = "p"; 8'h69: kp_app = "q"; 8'h72: kp_app = "r";
|
||||
8'h7A: kp_app = "s"; 8'h6B: kp_app = "t"; 8'h73: kp_app = "u";
|
||||
8'h74: kp_app = "v"; 8'h6C: kp_app = "w"; 8'h75: kp_app = "x";
|
||||
8'h7D: kp_app = "y"; 8'h71: kp_app = "n"; 8'h7B: kp_app = "m";
|
||||
8'h79: kp_app = "l"; 8'h7C: kp_app = "j";
|
||||
default: kp_app = 8'h00;
|
||||
endcase
|
||||
endfunction
|
||||
|
||||
wire shift = lsh | rsh;
|
||||
wire ctrl = lct | rct;
|
||||
wire [15:0] km = keymap(code);
|
||||
wire letter = km[15:8] >= "a" && km[15:8] <= "z";
|
||||
wire [7:0] ch = (letter ? shift ^ caps : shift) ? km[7:0] : km[15:8];
|
||||
wire [7:0] ckey = cursor_app ? "O" : "[";
|
||||
wire [7:0] kpa = kp_app(code);
|
||||
|
||||
// bytes a key press produces, first byte in k_seq[47:40]
|
||||
reg [2:0] k_len;
|
||||
reg [47:0] k_seq;
|
||||
always @* begin
|
||||
k_len = 3'd0;
|
||||
k_seq = 48'd0;
|
||||
if (e0) case (code)
|
||||
8'h75: {k_len, k_seq} = {3'd3, ESC, ckey, "A", 24'd0}; // up
|
||||
8'h72: {k_len, k_seq} = {3'd3, ESC, ckey, "B", 24'd0}; // down
|
||||
8'h74: {k_len, k_seq} = {3'd3, ESC, ckey, "C", 24'd0}; // right
|
||||
8'h6B: {k_len, k_seq} = {3'd3, ESC, ckey, "D", 24'd0}; // left
|
||||
8'h6C: {k_len, k_seq} = {3'd4, ESC, "[1~", 16'd0}; // home (Find)
|
||||
8'h70: {k_len, k_seq} = {3'd4, ESC, "[2~", 16'd0}; // insert
|
||||
8'h71: {k_len, k_seq} = {3'd4, ESC, "[3~", 16'd0}; // delete (Remove)
|
||||
8'h69: {k_len, k_seq} = {3'd4, ESC, "[4~", 16'd0}; // end (Select)
|
||||
8'h7D: {k_len, k_seq} = {3'd4, ESC, "[5~", 16'd0}; // page up
|
||||
8'h7A: {k_len, k_seq} = {3'd4, ESC, "[6~", 16'd0}; // page down
|
||||
8'h4A: {k_len, k_seq} = keypad_app ? {3'd3, ESC, "Oo", 24'd0} : {3'd1, "/", 40'd0};
|
||||
8'h5A: {k_len, k_seq} = keypad_app ? {3'd3, ESC, "OM", 24'd0} :
|
||||
enter_crlf ? {3'd2, 8'h0D, 8'h0A, 32'd0} : {3'd1, 8'h0D, 40'd0};
|
||||
default: ;
|
||||
endcase
|
||||
else case (code)
|
||||
8'h66: {k_len, k_seq} = {3'd1, bs_del ? 8'h7F : 8'h08, 40'd0};
|
||||
8'h5A: {k_len, k_seq} = enter_crlf ? {3'd2, 8'h0D, 8'h0A, 32'd0} : {3'd1, 8'h0D, 40'd0};
|
||||
8'h05: {k_len, k_seq} = {3'd3, ESC, "OP", 24'd0}; // F1
|
||||
8'h06: {k_len, k_seq} = {3'd3, ESC, "OQ", 24'd0}; // F2
|
||||
8'h04: {k_len, k_seq} = {3'd3, ESC, "OR", 24'd0}; // F3
|
||||
8'h0C: {k_len, k_seq} = {3'd3, ESC, "OS", 24'd0}; // F4
|
||||
8'h03: {k_len, k_seq} = {3'd5, ESC, "[15~", 8'd0}; // F5
|
||||
8'h0B: {k_len, k_seq} = {3'd5, ESC, "[17~", 8'd0}; // F6
|
||||
8'h83: {k_len, k_seq} = {3'd5, ESC, "[18~", 8'd0}; // F7
|
||||
8'h0A: {k_len, k_seq} = {3'd5, ESC, "[19~", 8'd0}; // F8
|
||||
8'h01: {k_len, k_seq} = {3'd5, ESC, "[20~", 8'd0}; // F9
|
||||
8'h09: {k_len, k_seq} = {3'd5, ESC, "[21~", 8'd0}; // F10
|
||||
8'h78: {k_len, k_seq} = {3'd5, ESC, "[23~", 8'd0}; // F11
|
||||
8'h07: {k_len, k_seq} = {3'd5, ESC, "[24~", 8'd0}; // F12
|
||||
default:
|
||||
if (keypad_app && kpa != 0)
|
||||
{k_len, k_seq} = {3'd3, ESC, "O", kpa, 24'd0};
|
||||
else if (ch != 0) begin
|
||||
k_len = 3'd1;
|
||||
if (!ctrl) k_seq[47:40] = ch;
|
||||
else if (ch == " ") k_seq[47:40] = 8'h00;
|
||||
else if (ch >= 8'h40) k_seq[47:40] = ch & 8'h1F; // ^A..^Z, ^[ ^\ ^] ^^ ^_
|
||||
else k_seq[47:40] = ch;
|
||||
end
|
||||
endcase
|
||||
end
|
||||
|
||||
always @(posedge clk) begin
|
||||
ascii_valid <= 1'b0;
|
||||
cad <= 1'b0;
|
||||
if (rst) begin
|
||||
{e0, f0, lsh, rsh, lct, rct, alt, caps} <= 8'd0;
|
||||
skip <= 3'd0;
|
||||
cnt <= 3'd0;
|
||||
end else begin
|
||||
if (cnt != 0) begin
|
||||
ascii <= sq[47:40];
|
||||
ascii_valid <= 1'b1;
|
||||
sq <= sq << 8;
|
||||
cnt <= cnt - 1'b1;
|
||||
end
|
||||
if (code_valid) begin
|
||||
if (skip != 0)
|
||||
skip <= skip - 1'b1;
|
||||
else if (code == 8'hE1)
|
||||
skip <= 3'd7; // Pause: E1 14 77 E1 F0 14 F0 77
|
||||
else if (code == 8'hE0)
|
||||
e0 <= 1'b1;
|
||||
else if (code == 8'hF0)
|
||||
f0 <= 1'b1;
|
||||
else begin
|
||||
e0 <= 1'b0;
|
||||
f0 <= 1'b0;
|
||||
cad <= !f0 && code == 8'h71 && (lct || rct) && alt; // Delete or keypad .
|
||||
if (code == 8'h12 && !e0) lsh <= !f0; // E0 12 / E0 59 are fake shifts
|
||||
else if (code == 8'h59 && !e0) rsh <= !f0;
|
||||
else if (code == 8'h14) begin
|
||||
if (e0) rct <= !f0; else lct <= !f0;
|
||||
end else if (code == 8'h11)
|
||||
alt <= !f0;
|
||||
else if (code == 8'h58 && !e0) begin
|
||||
if (!f0) caps <= !caps;
|
||||
end else if (!f0 && k_len != 0) begin
|
||||
if (alt && k_len != 3'd6) begin
|
||||
sq <= {ESC, k_seq[47:8]};
|
||||
cnt <= k_len + 1'b1;
|
||||
end else begin
|
||||
sq <= k_seq;
|
||||
cnt <= k_len;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
|
||||
// Which keys are held down, for games: one bit per set-2 scan code, 128 plain
|
||||
// codes ({0, code}) and 128 after the E0 prefix ({1, code}). The Pause key's
|
||||
// E1 sequence is skipped.
|
||||
module ps2_keystate (
|
||||
input clk,
|
||||
input rst,
|
||||
input [7:0] code,
|
||||
input code_valid,
|
||||
output reg [255:0] down
|
||||
);
|
||||
reg ext, brk, pause;
|
||||
reg [2:0] skip;
|
||||
always @(posedge clk)
|
||||
if (rst) begin
|
||||
down <= 256'd0;
|
||||
ext <= 1'b0; brk <= 1'b0; skip <= 3'd0;
|
||||
end else if (code_valid) begin
|
||||
if (skip != 3'd0) skip <= skip - 1'b1; // rest of E1 14 77 E1 F0 14 F0 77
|
||||
else if (code == 8'hE1) skip <= 3'd7;
|
||||
else if (code == 8'hE0) ext <= 1'b1;
|
||||
else if (code == 8'hF0) brk <= 1'b1;
|
||||
else begin
|
||||
if (!code[7]) down[{ext, code[6:0]}] <= !brk;
|
||||
ext <= 1'b0;
|
||||
brk <= 1'b0;
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
+551
@@ -0,0 +1,551 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// RV32IM processor, machine mode only.
|
||||
//
|
||||
// A multi-cycle core with one instruction in flight. Each instruction is
|
||||
// decoded in one cycle (D) and executed in the next (E); the decode cycle
|
||||
// fetches the following word, so straight-line code takes 2 clocks per
|
||||
// instruction. Taken branches and jumps take 3, loads and stores 3 (more if
|
||||
// the bus waits), multiplies 4 and divides 35.
|
||||
//
|
||||
// Instruction port: synchronous read. i_rdata is the word at the i_addr given
|
||||
// in the last cycle with i_en high, and must hold while i_en is low.
|
||||
//
|
||||
// Data port: d_req is high for one cycle with d_addr, d_we, d_wstrb and
|
||||
// d_wdata; the bus answers with d_ack (and d_rdata for a read) one or more
|
||||
// cycles later. d_addr is word-aligned for words, halfword-aligned for
|
||||
// halfwords (misaligned accesses trap); d_rdata is the whole word.
|
||||
// d_addr and d_wdata come early in the cycle (an adder from registers), but
|
||||
// d_req comes late (after the alignment and breakpoint checks): register it,
|
||||
// and the store, before it reaches block RAM write enables. A read with no
|
||||
// request is harmless for RAM, so RAM can be read from d_addr every cycle.
|
||||
//
|
||||
// Exceptions that depend on the branch comparison or the load/store address
|
||||
// (a taken branch to a misaligned target, a misaligned load/store, a load/store
|
||||
// breakpoint) are raised one cycle late, from registers, to keep the long
|
||||
// paths out of the trap logic; the instruction has no effect either way.
|
||||
//
|
||||
// Speed: 90 MHz with 1.5 ns to spare on the XC7A35T-1 (bench/), about 2100
|
||||
// LUTs, 900 flip-flops and 4 DSP slices.
|
||||
//
|
||||
// Implemented: RV32I, M, Zicsr, Zicntr (cycle, time = cycle, instret), Zifencei,
|
||||
// mret and wfi (a no-op). Machine-mode CSRs: mstatus (MIE, MPIE; MPP reads 3),
|
||||
// misa, mie, mip, mtvec (direct mode), mscratch, mepc, mcause, mtval, mcycle,
|
||||
// minstret, mhartid etc. (read 0), and two Sdtrig match triggers (tselect,
|
||||
// tdata1, tdata2, tinfo, tcontrol) for execute, load and store address
|
||||
// breakpoints. Triggers fire while tcontrol.MTE is set; a trap clears it
|
||||
// (saving it in MPTE) and mret restores it.
|
||||
//
|
||||
// Resumable NMI (after the Smrnmi extension, for a debug monitor): while
|
||||
// mnstatus.NMIE is set, `nmi` (checked as each instruction starts; it may come
|
||||
// straight from comparators on dbg_pc) or `nmi_step` (after one instruction
|
||||
// has completed with NMIE set) saves the pc in mnepc, clears NMIE and jumps to
|
||||
// NMI_VEC; mnret goes back and sets NMIE again. nmi_taken pulses as it is
|
||||
// taken. Unlike Smrnmi, NMIE resets to 1, software may clear it, and traps
|
||||
// still go to mtvec while it is clear. CSRs: mnscratch, mnepc, mncause, mnstatus.
|
||||
//
|
||||
// Interrupts: mip.MSIP (irq_soft), MTIP (irq_timer), MEIP (irq_ext) and local
|
||||
// interrupts 16-31 (irq_local), all level-sensitive. Priority: external,
|
||||
// software, timer, then local 16 upward.
|
||||
module rv32_cpu #(
|
||||
parameter [31:0] RESET_PC = 32'h0000_0000,
|
||||
parameter [31:0] NMI_VEC = 32'h0000_0000
|
||||
) (
|
||||
input clk,
|
||||
input rst, // synchronous
|
||||
output i_en,
|
||||
output [31:0] i_addr,
|
||||
input [31:0] i_rdata,
|
||||
output d_req,
|
||||
output d_we,
|
||||
output [3:0] d_wstrb,
|
||||
output [31:0] d_addr,
|
||||
output [31:0] d_wdata,
|
||||
input [31:0] d_rdata,
|
||||
input d_ack,
|
||||
input irq_soft,
|
||||
input irq_timer,
|
||||
input irq_ext,
|
||||
input [15:0] irq_local,
|
||||
input nmi,
|
||||
input nmi_step,
|
||||
output reg nmi_taken,
|
||||
output [31:0] dbg_pc, // address of the instruction being run
|
||||
output dbg_decode, // high in an instruction's first cycle
|
||||
output reg retire // pulse: an instruction completed
|
||||
);
|
||||
localparam S_IF = 3'd0, S_D = 3'd1, S_E = 3'd2, S_MEM = 3'd3,
|
||||
S_MUL = 3'd4, S_DIV = 3'd5, S_DIVF = 3'd6, S_XT = 3'd7;
|
||||
reg [2:0] state;
|
||||
reg [31:0] pc;
|
||||
|
||||
assign i_en = state == S_IF || state == S_D;
|
||||
assign i_addr = state == S_D ? pc + 32'd4 : pc;
|
||||
assign dbg_pc = pc;
|
||||
assign dbg_decode = state == S_D;
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Registers: distributed RAM, two asynchronous read ports.
|
||||
reg [31:0] rf [0:31];
|
||||
integer k;
|
||||
initial for (k = 0; k < 32; k = k + 1) rf[k] = 32'd0;
|
||||
reg rf_we;
|
||||
reg [4:0] rf_wa;
|
||||
reg [31:0] rf_wd;
|
||||
always @(posedge clk) if (rf_we && rf_wa != 5'd0) rf[rf_wa] <= rf_wd;
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Decode (state D): inst is the fetched word.
|
||||
wire [31:0] inst = i_rdata;
|
||||
wire [6:0] opc = inst[6:0];
|
||||
wire [2:0] f3 = inst[14:12];
|
||||
wire [6:0] f7 = inst[31:25];
|
||||
wire [4:0] rs1 = inst[19:15], rs2 = inst[24:20];
|
||||
wire [31:0] rv1 = rs1 == 5'd0 ? 32'd0 : rf[rs1];
|
||||
wire [31:0] rv2 = rs2 == 5'd0 ? 32'd0 : rf[rs2];
|
||||
|
||||
wire [31:0] imm_i = {{20{inst[31]}}, inst[31:20]};
|
||||
wire [31:0] imm_s = {{20{inst[31]}}, inst[31:25], inst[11:7]};
|
||||
wire [31:0] imm_b = {{20{inst[31]}}, inst[7], inst[30:25], inst[11:8], 1'b0};
|
||||
wire [31:0] imm_u = {inst[31:12], 12'd0};
|
||||
wire [31:0] imm_j = {{12{inst[31]}}, inst[19:12], inst[20], inst[30:21], 1'b0};
|
||||
|
||||
wire is_lui = opc == 7'b0110111;
|
||||
wire is_auipc = opc == 7'b0010111;
|
||||
wire is_jal = opc == 7'b1101111;
|
||||
wire is_jalr = opc == 7'b1100111 && f3 == 3'd0;
|
||||
wire is_br = opc == 7'b1100011 && f3[2:1] != 2'b01;
|
||||
wire is_ld = opc == 7'b0000011 && (f3 == 3'd0 || f3 == 3'd1 || f3 == 3'd2 ||
|
||||
f3 == 3'd4 || f3 == 3'd5);
|
||||
wire is_st = opc == 7'b0100011 && f3[2] == 1'b0 && f3[1:0] != 2'b11;
|
||||
wire is_opi = opc == 7'b0010011 &&
|
||||
(f3 == 3'd1 ? f7 == 7'd0 :
|
||||
f3 == 3'd5 ? (f7 == 7'd0 || f7 == 7'b0100000) : 1'b1);
|
||||
wire is_op = opc == 7'b0110011 &&
|
||||
(f7 == 7'd0 || (f7 == 7'b0100000 && (f3 == 3'd0 || f3 == 3'd5)));
|
||||
wire is_mdv = opc == 7'b0110011 && f7 == 7'b0000001;
|
||||
wire is_fence = opc == 7'b0001111 && f3 == 3'd0;
|
||||
wire is_fencei= opc == 7'b0001111 && f3 == 3'd1;
|
||||
wire is_sys = opc == 7'b1110011;
|
||||
wire is_csr = is_sys && f3[1:0] != 2'b00;
|
||||
wire is_ecall = inst == 32'h0000_0073;
|
||||
wire is_ebrk = inst == 32'h0010_0073;
|
||||
wire is_mret = inst == 32'h3020_0073;
|
||||
wire is_wfi = inst == 32'h1050_0073;
|
||||
wire is_mnret = inst == 32'h7020_0073;
|
||||
wire legal = is_lui | is_auipc | is_jal | is_jalr | is_br | is_ld | is_st |
|
||||
is_opi | is_op | is_mdv | is_fence | is_fencei | is_csr |
|
||||
is_ecall | is_ebrk | is_mret | is_wfi | is_mnret;
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Execute-stage registers, loaded at the end of D.
|
||||
reg [31:0] ir; // the instruction
|
||||
reg [31:0] a, b; // ALU operands
|
||||
reg [31:0] sv; // store data / rs2
|
||||
reg [31:0] tgt; // branch or jal target
|
||||
reg [31:0] pc4;
|
||||
reg e_tmis; // branch / jal target is misaligned
|
||||
reg x_trap; // the deferred trap, below
|
||||
reg [31:0] x_cause, x_val;
|
||||
reg e_alu, e_sub, e_jal, e_jalr, e_br, e_ld, e_st, e_mul, e_div,
|
||||
e_csr, e_ecall, e_ebrk, e_mret, e_mnret, e_fencei, e_ill;
|
||||
wire [2:0] e_f3 = ir[14:12];
|
||||
wire [4:0] e_rd = ir[11:7];
|
||||
reg [2:0] alu_fn;
|
||||
reg alu_alt; // sra rather than srl
|
||||
|
||||
// multiplier (DSP), fed straight from decode
|
||||
reg signed [32:0] m_a, m_b;
|
||||
reg signed [65:0] m_p, m_p2;
|
||||
reg m_hi, m_cnt;
|
||||
always @(posedge clk) begin
|
||||
m_p <= m_a * m_b;
|
||||
m_p2 <= m_p;
|
||||
end
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Execute (state E)
|
||||
wire [31:0] sum = a + b;
|
||||
wire eq = a == b;
|
||||
wire lt = $signed(a) < $signed(b);
|
||||
wire ltu = a < b;
|
||||
wire [31:0] sra_out = $signed(a) >>> b[4:0];
|
||||
reg [31:0] alu_out;
|
||||
always @* begin
|
||||
case (alu_fn)
|
||||
3'd0: alu_out = e_sub ? a - b : sum;
|
||||
3'd1: alu_out = a << b[4:0];
|
||||
3'd2: alu_out = {31'd0, lt};
|
||||
3'd3: alu_out = {31'd0, ltu};
|
||||
3'd4: alu_out = a ^ b;
|
||||
3'd5: alu_out = alu_alt ? sra_out : a >> b[4:0];
|
||||
3'd6: alu_out = a | b;
|
||||
default: alu_out = a & b;
|
||||
endcase
|
||||
end
|
||||
reg taken;
|
||||
always @* begin
|
||||
case (e_f3)
|
||||
3'd0: taken = eq;
|
||||
3'd1: taken = !eq;
|
||||
3'd4: taken = lt;
|
||||
3'd5: taken = !lt;
|
||||
3'd6: taken = ltu;
|
||||
default: taken = !ltu;
|
||||
endcase
|
||||
end
|
||||
wire [31:0] jtarget = e_jalr ? {sum[31:1], 1'b0} : tgt;
|
||||
wire jump = e_jal | e_jalr | (e_br & taken);
|
||||
|
||||
// loads and stores
|
||||
wire [1:0] sz = e_f3[1:0];
|
||||
wire [1:0] lo = a[1:0] + b[1:0];
|
||||
wire mis = (sz == 2'd1 && lo[0]) || (sz == 2'd2 && lo != 2'd0);
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// CSRs
|
||||
reg mie_b, mpie_b; // mstatus
|
||||
reg [31:0] mie_r, mtvec, mscratch, mepc, mcause, mtval;
|
||||
reg [63:0] mcycle, minstret;
|
||||
reg tsel, mte, mpte;
|
||||
reg nmie, stepped;
|
||||
reg [31:0] mnscratch, mnepc, mncause;
|
||||
reg [1:0] t_m, t_x, t_s, t_l;
|
||||
reg [31:0] t_a0, t_a1;
|
||||
reg [31:0] irq_r;
|
||||
localparam [31:0] MIE_MASK = 32'hFFFF_0888;
|
||||
|
||||
wire [11:0] csr_a = ir[31:20];
|
||||
reg [31:0] csr_rd;
|
||||
reg csr_ok;
|
||||
always @* begin
|
||||
csr_ok = 1'b1;
|
||||
case (csr_a)
|
||||
12'h300: csr_rd = {19'd0, 2'b11, 3'd0, mpie_b, 3'd0, mie_b, 3'd0};
|
||||
12'h301: csr_rd = 32'h4000_1100; // RV32 I M
|
||||
12'h304: csr_rd = mie_r;
|
||||
12'h305: csr_rd = mtvec;
|
||||
12'h310: csr_rd = 32'd0; // mstatush
|
||||
12'h340: csr_rd = mscratch;
|
||||
12'h341: csr_rd = mepc;
|
||||
12'h342: csr_rd = mcause;
|
||||
12'h343: csr_rd = mtval;
|
||||
12'h344: csr_rd = irq_r;
|
||||
12'h740: csr_rd = mnscratch;
|
||||
12'h741: csr_rd = mnepc;
|
||||
12'h742: csr_rd = mncause;
|
||||
12'h744: csr_rd = {19'd0, 2'b11, 7'd0, nmie, 3'd0};
|
||||
12'h7A0: csr_rd = {31'd0, tsel};
|
||||
12'h7A1: csr_rd = {4'd2, 21'd0, t_m[tsel], 3'd0, t_x[tsel], t_s[tsel], t_l[tsel]};
|
||||
12'h7A2: csr_rd = tsel ? t_a1 : t_a0;
|
||||
12'h7A4: csr_rd = 32'h0000_0004; // tinfo: match triggers
|
||||
12'h7A5: csr_rd = {24'd0, mpte, 3'd0, mte, 3'd0};
|
||||
12'hB00, 12'hC00, 12'hC01: csr_rd = mcycle[31:0];
|
||||
12'hB80, 12'hC80, 12'hC81: csr_rd = mcycle[63:32];
|
||||
12'hB02, 12'hC02: csr_rd = minstret[31:0];
|
||||
12'hB82, 12'hC82: csr_rd = minstret[63:32];
|
||||
12'hF11, 12'hF12, 12'hF13, 12'hF14, 12'hF15: csr_rd = 32'd0;
|
||||
default: begin csr_rd = 32'd0; csr_ok = 1'b0; end
|
||||
endcase
|
||||
end
|
||||
wire csr_imm = e_f3[2];
|
||||
wire [31:0] csr_src = csr_imm ? {27'd0, ir[19:15]} : a;
|
||||
wire csr_wr = e_f3[1:0] == 2'b01 || ir[19:15] != 5'd0;
|
||||
wire [31:0] csr_wv = e_f3[1:0] == 2'b01 ? csr_src :
|
||||
e_f3[1:0] == 2'b10 ? csr_rd | csr_src : csr_rd & ~csr_src;
|
||||
wire csr_bad = !csr_ok || (csr_wr && csr_a[11:10] == 2'b11);
|
||||
wire csr_go = state == S_E && e_csr && !csr_bad && csr_wr;
|
||||
|
||||
// triggers
|
||||
wire fire_x = mte && ((t_m[0] && t_x[0] && pc == t_a0) || (t_m[1] && t_x[1] && pc == t_a1));
|
||||
// a + b == k without waiting for the carries: bit i of the sum matches k
|
||||
// exactly when the carry into it is x[i] = a^b^k, and that carry is the
|
||||
// majority of a, b and x one bit down (and 0 into bit 0).
|
||||
function addeq(input [31:0] a, input [31:0] b, input [31:0] k);
|
||||
reg [31:0] x, c;
|
||||
begin
|
||||
x = a ^ b ^ k;
|
||||
c = (a & b) | (a & x) | (b & x);
|
||||
addeq = !x[0] && x[31:1] == c[30:0];
|
||||
end
|
||||
endfunction
|
||||
wire hit0 = addeq(a, b, t_a0), hit1 = addeq(a, b, t_a1);
|
||||
wire fire_l = mte && ((t_m[0] && t_l[0] && hit0) || (t_m[1] && t_l[1] && hit1));
|
||||
wire fire_s = mte && ((t_m[0] && t_s[0] && hit0) || (t_m[1] && t_s[1] && hit1));
|
||||
|
||||
// interrupts
|
||||
wire [31:0] pend = irq_r & mie_r;
|
||||
wire irq_take = mie_b && pend != 32'd0;
|
||||
reg [4:0] irq_code;
|
||||
integer j;
|
||||
always @* begin
|
||||
irq_code = 5'd16;
|
||||
for (j = 31; j >= 16; j = j - 1) if (pend[j]) irq_code = j[4:0];
|
||||
if (pend[7]) irq_code = 5'd7;
|
||||
if (pend[3]) irq_code = 5'd3;
|
||||
if (pend[11]) irq_code = 5'd11;
|
||||
end
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Data bus
|
||||
wire ls_bad = (e_ld && fire_l) || (e_st && fire_s) || mis;
|
||||
wire mem_go = state == S_E && (e_ld || e_st) && !ls_bad;
|
||||
assign d_req = mem_go;
|
||||
assign d_we = e_st;
|
||||
assign d_addr = sum;
|
||||
assign d_wstrb = sz == 2'd0 ? 4'b0001 << sum[1:0] :
|
||||
sz == 2'd1 ? (sum[1] ? 4'b1100 : 4'b0011) : 4'b1111;
|
||||
assign d_wdata = sz == 2'd0 ? {4{sv[7:0]}} : sz == 2'd1 ? {2{sv[15:0]}} : sv;
|
||||
reg [1:0] la; // address bits of the load
|
||||
wire [31:0] lw_sh = d_rdata >> {la, 3'b000};
|
||||
wire [31:0] ld_val = e_f3 == 3'd0 ? {{24{lw_sh[7]}}, lw_sh[7:0]} :
|
||||
e_f3 == 3'd1 ? {{16{lw_sh[15]}}, lw_sh[15:0]} :
|
||||
e_f3 == 3'd4 ? {24'd0, lw_sh[7:0]} :
|
||||
e_f3 == 3'd5 ? {16'd0, lw_sh[15:0]} : d_rdata;
|
||||
|
||||
// divider
|
||||
reg [31:0] dq, dr, dd, da;
|
||||
reg [5:0] dcnt;
|
||||
reg dneg_q, dneg_r, dzero;
|
||||
wire [32:0] dtmp = {dr, dq[31]};
|
||||
wire [33:0] dsub = {1'b0, dtmp} - {2'b0, dd};
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Traps
|
||||
wire nmi_go = state == S_D && nmie && (nmi || (nmi_step && stepped));
|
||||
reg trap;
|
||||
reg [31:0] t_cause, t_val;
|
||||
always @* begin
|
||||
trap = 1'b0; t_cause = 32'd0; t_val = 32'd0;
|
||||
if (state == S_D) begin
|
||||
if (nmi_go) begin
|
||||
trap = 1'b1; // to NMI_VEC; see below
|
||||
end else if (irq_take) begin
|
||||
trap = 1'b1; t_cause = {1'b1, 26'd0, irq_code};
|
||||
end else if (fire_x) begin
|
||||
trap = 1'b1; t_cause = 32'd3; t_val = pc;
|
||||
end
|
||||
end else if (state == S_XT) begin
|
||||
// Traps that depend on the branch comparison or the load/store
|
||||
// address are taken a cycle late, from registers, to keep those
|
||||
// long paths out of the trap logic: a taken branch to a misaligned
|
||||
// target, a load/store breakpoint, a misaligned load/store.
|
||||
if (x_trap) begin
|
||||
trap = 1'b1; t_cause = x_cause; t_val = x_val;
|
||||
end
|
||||
end else if (state == S_E) begin
|
||||
if (e_ill || (e_csr && csr_bad)) begin
|
||||
trap = 1'b1; t_cause = 32'd2; t_val = ir;
|
||||
end else if ((e_jal && e_tmis) || (e_jalr && lo[1])) begin
|
||||
trap = 1'b1; t_cause = 32'd0; t_val = jtarget;
|
||||
end else if (e_ecall) begin
|
||||
trap = 1'b1; t_cause = 32'd11;
|
||||
end else if (e_ebrk) begin
|
||||
trap = 1'b1; t_cause = 32'd3; t_val = pc;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Register write-back
|
||||
always @* begin
|
||||
rf_we = 1'b0; rf_wa = e_rd; rf_wd = alu_out;
|
||||
case (state)
|
||||
S_E: begin
|
||||
rf_we = !trap && (e_alu || e_jal || e_jalr || e_csr);
|
||||
rf_wd = e_csr ? csr_rd : (e_jal || e_jalr) ? pc4 : alu_out;
|
||||
end
|
||||
S_MEM: begin rf_we = d_ack && e_ld; rf_wd = ld_val; end
|
||||
S_MUL: begin rf_we = m_cnt; rf_wd = m_hi ? m_p2[63:32] : m_p2[31:0]; end
|
||||
S_DIVF: begin
|
||||
rf_we = 1'b1;
|
||||
if (e_f3[1]) // rem, remu
|
||||
rf_wd = dzero ? da : dneg_r ? -dr : dr;
|
||||
else
|
||||
rf_wd = dzero ? 32'hFFFF_FFFF : dneg_q ? -dq : dq;
|
||||
end
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
|
||||
// an instruction completes (counted by instret unless it writes instret)
|
||||
reg count_i;
|
||||
wire done = (state == S_E && !trap && !e_ld && !e_st && !e_mul && !e_div) ||
|
||||
(state == S_MEM && d_ack) || (state == S_MUL && m_cnt) ||
|
||||
state == S_DIVF || (state == S_XT && !x_trap);
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
always @(posedge clk) begin
|
||||
irq_r <= {irq_local, 4'd0, irq_ext, 3'd0, irq_timer, 3'd0, irq_soft, 3'd0};
|
||||
retire <= done;
|
||||
// a write to either half replaces the count for this cycle
|
||||
if (!(csr_go && (csr_a == 12'hB00 || csr_a == 12'hB80))) mcycle <= mcycle + 64'd1;
|
||||
// instret counts a cycle late (instructions take at least two, so the
|
||||
// next one still reads the right count)
|
||||
count_i <= done && !(csr_go && (csr_a == 12'hB02 || csr_a == 12'hB82));
|
||||
if (count_i) minstret <= minstret + 64'd1;
|
||||
|
||||
if (csr_go) begin
|
||||
case (csr_a)
|
||||
12'h300: begin mie_b <= csr_wv[3]; mpie_b <= csr_wv[7]; end
|
||||
12'h304: mie_r <= csr_wv & MIE_MASK;
|
||||
12'h305: mtvec <= {csr_wv[31:2], 2'b00};
|
||||
12'h340: mscratch <= csr_wv;
|
||||
12'h341: mepc <= {csr_wv[31:2], 2'b00};
|
||||
12'h342: mcause <= csr_wv;
|
||||
12'h343: mtval <= csr_wv;
|
||||
12'h740: mnscratch <= csr_wv;
|
||||
12'h741: mnepc <= {csr_wv[31:2], 2'b00};
|
||||
12'h742: mncause <= csr_wv;
|
||||
12'h744: nmie <= csr_wv[3];
|
||||
12'h7A0: if (csr_wv[31:1] == 31'd0) tsel <= csr_wv[0];
|
||||
12'h7A1: begin
|
||||
t_m[tsel] <= csr_wv[6]; t_x[tsel] <= csr_wv[2];
|
||||
t_s[tsel] <= csr_wv[1]; t_l[tsel] <= csr_wv[0];
|
||||
end
|
||||
12'h7A2: if (tsel) t_a1 <= csr_wv; else t_a0 <= csr_wv;
|
||||
12'h7A5: begin mte <= csr_wv[3]; mpte <= csr_wv[7]; end
|
||||
12'hB00: mcycle[31:0] <= csr_wv;
|
||||
12'hB80: mcycle[63:32] <= csr_wv;
|
||||
12'hB02: minstret[31:0] <= csr_wv;
|
||||
12'hB82: minstret[63:32] <= csr_wv;
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
|
||||
case (state)
|
||||
S_IF: state <= S_D;
|
||||
|
||||
S_D: begin
|
||||
ir <= inst;
|
||||
pc4 <= pc + 32'd4;
|
||||
tgt <= pc + (is_jal ? imm_j : imm_b);
|
||||
a <= is_lui ? 32'd0 : (is_auipc || is_jal) ? pc : rv1;
|
||||
b <= (is_lui || is_auipc) ? imm_u : is_st ? imm_s :
|
||||
(is_op || is_br || is_mdv) ? rv2 : imm_i;
|
||||
sv <= rv2;
|
||||
alu_fn <= (is_lui || is_auipc) ? 3'd0 : f3;
|
||||
alu_alt <= inst[30];
|
||||
e_alu <= is_lui | is_auipc | is_opi | is_op;
|
||||
e_sub <= is_op & inst[30];
|
||||
e_jal <= is_jal;
|
||||
e_jalr <= is_jalr;
|
||||
e_br <= is_br;
|
||||
e_tmis <= is_jal ? inst[21] : inst[8];
|
||||
e_ld <= is_ld;
|
||||
e_st <= is_st;
|
||||
e_mul <= is_mdv & !f3[2];
|
||||
e_div <= is_mdv & f3[2];
|
||||
e_csr <= is_csr;
|
||||
e_ecall <= is_ecall;
|
||||
e_ebrk <= is_ebrk;
|
||||
e_mret <= is_mret;
|
||||
e_mnret <= is_mnret;
|
||||
e_fencei<= is_fencei;
|
||||
e_ill <= !legal;
|
||||
m_a <= {f3[1:0] != 2'b11 && rv1[31], rv1}; // mulhu: unsigned
|
||||
m_b <= {f3[1:0] == 2'b01 && rv2[31], rv2}; // mulh only: signed
|
||||
m_hi <= f3[1:0] != 2'b00;
|
||||
if (!trap) state <= S_E;
|
||||
end
|
||||
|
||||
S_E: begin
|
||||
// set up the multi-cycle units (unused if this traps)
|
||||
la <= lo;
|
||||
x_trap <= e_br ? taken : 1'b1;
|
||||
x_cause <= e_br ? 32'd0 : (fire_l || fire_s) ? 32'd3 : e_st ? 32'd6 : 32'd4;
|
||||
x_val <= e_br ? tgt : sum;
|
||||
m_cnt <= 1'b0;
|
||||
dneg_q <= !e_f3[0] && (a[31] ^ b[31]) && b != 32'd0;
|
||||
dneg_r <= !e_f3[0] && a[31];
|
||||
dzero <= b == 32'd0;
|
||||
da <= a;
|
||||
dq <= (!e_f3[0] && a[31]) ? -a : a;
|
||||
dd <= (!e_f3[0] && b[31]) ? -b : b;
|
||||
dr <= 32'd0;
|
||||
dcnt <= 6'd0;
|
||||
if (!trap) begin
|
||||
if (e_ld || e_st)
|
||||
state <= ls_bad ? S_XT : S_MEM;
|
||||
else if (e_mul)
|
||||
state <= S_MUL;
|
||||
else if (e_div)
|
||||
state <= S_DIV;
|
||||
else if (e_br && e_tmis)
|
||||
state <= S_XT;
|
||||
else if (e_mret) begin
|
||||
pc <= mepc; mie_b <= mpie_b; mpie_b <= 1'b1; mte <= mpte;
|
||||
state <= S_IF;
|
||||
end else if (e_mnret) begin
|
||||
pc <= mnepc; nmie <= 1'b1;
|
||||
state <= S_IF;
|
||||
end else if (jump) begin
|
||||
pc <= jtarget;
|
||||
state <= S_IF;
|
||||
end else if (e_fencei) begin
|
||||
pc <= pc4;
|
||||
state <= S_IF;
|
||||
end else begin
|
||||
pc <= pc4;
|
||||
state <= S_D;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
S_MEM: if (d_ack) begin pc <= pc4; state <= S_D; end
|
||||
|
||||
S_MUL: begin
|
||||
m_cnt <= 1'b1;
|
||||
if (m_cnt) begin pc <= pc4; state <= S_D; end
|
||||
end
|
||||
|
||||
S_DIV: begin
|
||||
if (!dsub[33]) begin dr <= dsub[31:0]; dq <= {dq[30:0], 1'b1}; end
|
||||
else begin dr <= dtmp[31:0]; dq <= {dq[30:0], 1'b0}; end
|
||||
dcnt <= dcnt + 6'd1;
|
||||
if (dcnt == 6'd31) state <= S_DIVF;
|
||||
end
|
||||
|
||||
S_DIVF: begin pc <= pc4; state <= S_D; end
|
||||
|
||||
S_XT: begin pc <= pc4; state <= S_D; end // not taken (else trap)
|
||||
|
||||
endcase
|
||||
|
||||
nmi_taken <= 1'b0;
|
||||
stepped <= nmie && (stepped || done); // only counts outside the monitor
|
||||
if (nmi_go) begin
|
||||
mnepc <= pc;
|
||||
mncause <= 32'h8000_0000;
|
||||
nmie <= 1'b0;
|
||||
stepped <= 1'b0;
|
||||
nmi_taken <= 1'b1;
|
||||
pc <= NMI_VEC;
|
||||
state <= S_IF;
|
||||
end else if (trap) begin
|
||||
mepc <= pc;
|
||||
mcause <= t_cause;
|
||||
mtval <= t_val;
|
||||
mpie_b <= mie_b;
|
||||
mie_b <= 1'b0;
|
||||
mpte <= mte;
|
||||
mte <= 1'b0;
|
||||
pc <= mtvec;
|
||||
state <= S_IF;
|
||||
end
|
||||
|
||||
if (rst) begin
|
||||
state <= S_IF;
|
||||
pc <= RESET_PC;
|
||||
mie_b <= 1'b0; mpie_b <= 1'b0;
|
||||
mie_r <= 32'd0;
|
||||
mtvec <= RESET_PC;
|
||||
mcause <= 32'd0;
|
||||
tsel <= 1'b0; mte <= 1'b0; mpte <= 1'b0;
|
||||
t_m <= 2'b00; t_x <= 2'b00; t_s <= 2'b00; t_l <= 2'b00;
|
||||
mcycle <= 64'd0; minstret <= 64'd0;
|
||||
retire <= 1'b0; count_i <= 1'b0;
|
||||
nmie <= 1'b1; stepped <= 1'b0; nmi_taken <= 1'b0;
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
@@ -0,0 +1,478 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// RISC-V computer: rv32_cpu, 80 KB of block RAM preloaded with BASIC, the
|
||||
// monitor's 32 KB, and the Z-80 computer's I/O ports as memory-mapped
|
||||
// registers. Runs on its own clock; the console crosses to the terminal's
|
||||
// clock with toggle handshakes (one byte in flight each way).
|
||||
//
|
||||
// Memory map:
|
||||
// 0000_0000-0001_3FFF RAM, 80 KB (BASIC). Writes below ROM_TOP are ignored
|
||||
// so a crashed program cannot damage BASIC's code.
|
||||
// 0002_0000-0002_7FFF the monitor's ROM (writes ignored). The NMI enters at
|
||||
// 0002_0000.
|
||||
// 0002_8000-0002_9FFF the monitor's RAM, for data only (it is distributed
|
||||
// RAM, answering a cycle later than block RAM; code
|
||||
// can't run from it).
|
||||
// FFFF_FE00-FFFF_FEFF 32-bit registers (below)
|
||||
// FFFF_FF00-FFFF_FFFF I/O port N at FFFF_FF00 + N, a byte each, the same
|
||||
// ports as the Z-80 machine. Being within 2 KB of the top
|
||||
// of memory, a port is one instruction from x0:
|
||||
// lbu a0, -256+N(zero) / sb a0, -256+N(zero).
|
||||
//
|
||||
// I/O ports (FFFF_FF00 + N):
|
||||
// 00 in bit 0 = key waiting, bit 1 = console can take a byte
|
||||
// 01 in key (clears bit 0) out byte to the terminal
|
||||
// 10 in millisecond counter bits 7:0; reading it latches bits 31:8 for 11-13
|
||||
// 20 out 7-segment display, right two digits 21 out left two digits
|
||||
// 30 out graphics X bits 7:0 31 out X bit 8 32 out Y
|
||||
// 33 out plot colour at (X,Y), then X+1 in colour at (X,Y)
|
||||
// 34 out fill the whole graphics screen in bit 0 = fill busy
|
||||
// (also turns the sprites off and the scroll back to 0, unless
|
||||
// bit 7 is set)
|
||||
// 35 out scroll: buffer X shown at the left, bits 7:0
|
||||
// 36 out bit 0 = scroll X bit 8; bit 1 = hide the graphics in a border (4
|
||||
// columns each side, 2 rows top and bottom) to draw in unseen
|
||||
// 37 out scroll: buffer Y shown at the top (0-239)
|
||||
// 40 SPI flash control, 41 SPI flash data (see flash_spi.v)
|
||||
// 60 out bit 1 = stop again (NMI) after one instruction of the program
|
||||
// in why the last NMI came: 1 Pause key, 2 port 61, 3 step,
|
||||
// 4 breakpoint 0, 5 breakpoint 1
|
||||
// 61 out enter the monitor (NMI)
|
||||
// 67 out bit 0 = breakpoint 0 on, bit 1 = breakpoint 1 on
|
||||
// 70-78, 80-87 sprites (see sprites.v)
|
||||
// 88 in keys held: bit 0 up (W, cursor or keypad 8), 1 down (S, 2), 2 left
|
||||
// (A, 4), 3 right (D, 6), 4 fire (Space or Ctrl), 5 Enter, 6 Shift,
|
||||
// 7 Esc
|
||||
// 89 out pick a scan code (set 2; add 128 for an E0 code)
|
||||
// in 1 if that key is held, else 0
|
||||
// 90-9D sound (see sound.v)
|
||||
// The Z-80's debug-read ports (62-64) are gone: the monitor runs in the same
|
||||
// address space as the program. Its breakpoint addresses are 32 bits, below.
|
||||
//
|
||||
// 32-bit registers (FFFF_FE00 + N):
|
||||
// 00 in milliseconds since reset
|
||||
// 04 in microseconds since reset (wraps every 71 minutes)
|
||||
// 08 timer compare: the timer interrupt (mip.MTIP) is pending while the
|
||||
// microsecond counter is at or past it (by less than 2^31)
|
||||
// 10 breakpoint 0 address 14 breakpoint 1 address
|
||||
//
|
||||
// The monitor: an NMI (the Pause key, port 61, a breakpoint or a single step)
|
||||
// saves the program's pc in mnepc and jumps to the monitor ROM, which leaves
|
||||
// with mnret. NMIs wait while the program counter is in the monitor's ROM.
|
||||
// Breakpoints compare the address of each instruction as it starts, so the
|
||||
// instruction at a breakpoint has not run.
|
||||
module rv32_system #(
|
||||
parameter MEM_FILE = "basic.mem", // RAM 0000-FFFF
|
||||
parameter MEM_HI_FILE = "basic-hi.mem", // RAM 10000-13FFF
|
||||
parameter MON_FILE = "monitor.mem",
|
||||
parameter CPU_HZ = 90_000_000,
|
||||
parameter ROM_TOP = 32'h0000_A000
|
||||
) (
|
||||
input clk, // CPU clock
|
||||
input rst, // synchronous to clk
|
||||
// keyboard side: a byte is offered when rx_req != rx_ack
|
||||
input rx_req,
|
||||
input [7:0] rx_data,
|
||||
output reg rx_ack,
|
||||
// screen side: a byte is offered when tx_req != tx_ack
|
||||
output reg tx_req,
|
||||
output reg [7:0] tx_data,
|
||||
input tx_ack,
|
||||
output reg [15:0] seg_val,
|
||||
output halted,
|
||||
input brk_key, // pulse: the Pause key, enter the monitor
|
||||
// graphics layer read port, on the video clock
|
||||
input vclk,
|
||||
input [16:0] gfx_vaddr,
|
||||
output reg [3:0] gfx_vpix, // (combinational from registers)
|
||||
output reg [8:0] scroll_x,
|
||||
output reg [7:0] scroll_y,
|
||||
output reg g_border,
|
||||
// sprites, on the video clock (see sprites.v)
|
||||
input sp_go,
|
||||
input [7:0] sp_line,
|
||||
input sp_buf,
|
||||
input [8:0] sp_x,
|
||||
input sp_on,
|
||||
output [3:0] sp_pix,
|
||||
input sp_ftick,
|
||||
// keys held down (one bit per scan code, from the keyboard's clock)
|
||||
input [255:0] keys,
|
||||
// configuration flash (SPI)
|
||||
output fl_cs_n,
|
||||
output fl_sck,
|
||||
output fl_mosi,
|
||||
input fl_miso,
|
||||
// sound, for a PmodAMP2
|
||||
output audio
|
||||
);
|
||||
localparam [31:0] MON_BASE = 32'h0002_0000;
|
||||
assign halted = 1'b0;
|
||||
|
||||
// ------------------------------------------------------------------ CPU
|
||||
wire i_en, d_req, d_we, dbg_decode, retire, nmi_taken;
|
||||
wire [31:0] i_addr, d_addr, d_wdata, dbg_pc;
|
||||
wire [3:0] d_wstrb;
|
||||
reg [31:0] i_rdata, d_rdata;
|
||||
reg d_ack;
|
||||
wire nmi, timer_irq;
|
||||
reg step_arm;
|
||||
|
||||
rv32_cpu #(.RESET_PC(32'h0), .NMI_VEC(MON_BASE)) cpu (
|
||||
.clk(clk), .rst(rst),
|
||||
.i_en(i_en), .i_addr(i_addr), .i_rdata(i_rdata),
|
||||
.d_req(d_req), .d_we(d_we), .d_wstrb(d_wstrb), .d_addr(d_addr),
|
||||
.d_wdata(d_wdata), .d_rdata(d_rdata), .d_ack(d_ack),
|
||||
.irq_soft(1'b0), .irq_timer(timer_irq), .irq_ext(1'b0), .irq_local(16'd0),
|
||||
.nmi(nmi), .nmi_step(step_arm), .nmi_taken(nmi_taken),
|
||||
.dbg_pc(dbg_pc), .dbg_decode(dbg_decode), .retire(retire)
|
||||
);
|
||||
|
||||
// ------------------------------------------------------------- memories
|
||||
// Each block RAM: instruction fetch on port A; loads and stores on port B,
|
||||
// where a store is written the cycle after the request (its address
|
||||
// replacing the core's for that cycle).
|
||||
// RAM is two arrays (64 KB and 16 KB) so that it takes 20 block RAMs, not 32.
|
||||
wire d_main = d_addr[31:17] == 15'd0 && (!d_addr[16] || d_addr[15:14] == 2'd0);
|
||||
wire d_mon = d_addr[31:15] == MON_BASE[31:15];
|
||||
wire d_mram = d_addr[31:13] == 19'h14; // 0002_8000-0002_9FFF
|
||||
wire d_io = d_addr[31:12] == 20'hFFFFF;
|
||||
integer b;
|
||||
|
||||
(* ram_style = "block" *) reg [31:0] ram0 [0:16383]; // 0000_0000-0000_FFFF
|
||||
(* ram_style = "block" *) reg [31:0] ram1 [0:4095]; // 0001_0000-0001_3FFF
|
||||
initial $readmemh(MEM_FILE, ram0);
|
||||
initial $readmemh(MEM_HI_FILE, ram1);
|
||||
reg [31:0] ram0_iq, ram1_iq, ram0_dq, ram1_dq, w_d;
|
||||
reg [13:0] w_a;
|
||||
reg [3:0] w_s;
|
||||
reg w_r0, w_r1, d_hi;
|
||||
wire [13:0] r0_b = w_r0 ? w_a : d_addr[15:2];
|
||||
wire [11:0] r1_b = w_r1 ? w_a[11:0] : d_addr[13:2];
|
||||
always @(posedge clk) if (i_en) ram0_iq <= ram0[i_addr[15:2]];
|
||||
always @(posedge clk) if (i_en) ram1_iq <= ram1[i_addr[13:2]];
|
||||
always @(posedge clk) begin
|
||||
if (w_r0)
|
||||
for (b = 0; b < 4; b = b + 1)
|
||||
if (w_s[b]) ram0[r0_b][8*b +: 8] <= w_d[8*b +: 8];
|
||||
ram0_dq <= ram0[r0_b];
|
||||
end
|
||||
always @(posedge clk) begin
|
||||
if (w_r1)
|
||||
for (b = 0; b < 4; b = b + 1)
|
||||
if (w_s[b]) ram1[r1_b][8*b +: 8] <= w_d[8*b +: 8];
|
||||
ram1_dq <= ram1[r1_b];
|
||||
end
|
||||
|
||||
(* ram_style = "block" *) reg [31:0] mrom [0:8191];
|
||||
initial $readmemh(MON_FILE, mrom);
|
||||
reg [31:0] mon_iq, mon_dq;
|
||||
always @(posedge clk) if (i_en) mon_iq <= mrom[i_addr[14:2]];
|
||||
always @(posedge clk) mon_dq <= mrom[d_addr[14:2]];
|
||||
|
||||
// the monitor's RAM: an access is registered, then the distributed RAM is
|
||||
// read (or written) in the next cycle
|
||||
(* ram_style = "distributed" *) reg [31:0] mram [0:2047];
|
||||
reg [10:0] mr_a;
|
||||
reg mr_go, mr_we;
|
||||
reg [31:0] mr_q;
|
||||
always @(posedge clk) begin
|
||||
if (mr_go && mr_we)
|
||||
for (b = 0; b < 4; b = b + 1)
|
||||
if (w_s[b]) mram[mr_a][8*b +: 8] <= w_d[8*b +: 8];
|
||||
mr_q <= mram[mr_a];
|
||||
end
|
||||
|
||||
// instruction fetch: anything outside the two memories reads as 0 (illegal)
|
||||
reg [1:0] i_sel;
|
||||
always @(posedge clk)
|
||||
if (i_en) i_sel <= i_addr[31:16] == 16'h0000 ? 2'd0 :
|
||||
i_addr[31:14] == 18'h4 ? 2'd1 :
|
||||
i_addr[31:15] == MON_BASE[31:15] ? 2'd2 : 2'd3;
|
||||
always @* case (i_sel)
|
||||
2'd0: i_rdata = ram0_iq;
|
||||
2'd1: i_rdata = ram1_iq;
|
||||
2'd2: i_rdata = mon_iq;
|
||||
default: i_rdata = 32'd0;
|
||||
endcase
|
||||
|
||||
// ----------------------------------------------------------- data bus
|
||||
reg [2:0] d_sel; // 0 nothing, 1 RAM, 2 monitor ROM, 3 I/O, 4 monitor RAM
|
||||
reg io_go, io_we;
|
||||
reg [11:0] io_a;
|
||||
reg [31:0] io_ww; // the store data as it came
|
||||
reg [7:0] io_wd; // the byte for a port
|
||||
reg [31:0] io_q;
|
||||
always @(posedge clk) begin
|
||||
w_r0 <= d_req && d_we && d_main && !d_addr[16] && d_addr >= ROM_TOP;
|
||||
w_r1 <= d_req && d_we && d_main && d_addr[16] && d_addr >= ROM_TOP;
|
||||
mr_go <= d_req && d_mram;
|
||||
mr_we <= d_we;
|
||||
mr_a <= d_addr[12:2];
|
||||
w_a <= d_addr[15:2];
|
||||
w_d <= d_wdata;
|
||||
w_s <= d_wstrb;
|
||||
io_go <= d_req && d_io;
|
||||
io_we <= d_we;
|
||||
io_a <= d_addr[11:0];
|
||||
io_ww <= d_wdata;
|
||||
io_wd <= d_wdata >> {d_addr[1:0], 3'b000};
|
||||
if (d_req) begin
|
||||
d_sel <= d_main ? 3'd1 : d_mon ? 3'd2 : d_io ? 3'd3 : d_mram ? 3'd4 : 3'd0;
|
||||
d_hi <= d_addr[16];
|
||||
end
|
||||
// block RAM answers the next cycle, I/O and the monitor's RAM the one after
|
||||
d_ack <= (d_req && !d_io && !d_mram) || io_go || mr_go;
|
||||
if (rst) begin io_go <= 1'b0; d_ack <= 1'b0; w_r0 <= 1'b0; w_r1 <= 1'b0; mr_go <= 1'b0; end
|
||||
end
|
||||
always @* case (d_sel)
|
||||
3'd1: d_rdata = d_hi ? ram1_dq : ram0_dq;
|
||||
3'd2: d_rdata = mon_dq;
|
||||
3'd3: d_rdata = io_q;
|
||||
3'd4: d_rdata = mr_q;
|
||||
default: d_rdata = 32'd0;
|
||||
endcase
|
||||
|
||||
// the port strobes, a cycle after the request
|
||||
wire io_bytes = io_a[11:8] == 4'hF;
|
||||
wire io_wr = io_go && io_we && io_bytes;
|
||||
wire io_rd = io_go && !io_we && io_bytes;
|
||||
wire [7:0] io_addr = io_a[7:0];
|
||||
wire wr_word = io_go && io_we && io_a[11:8] == 4'hE;
|
||||
reg [7:0] io_din;
|
||||
reg [31:0] word_q;
|
||||
|
||||
// ------------------------------------------------------------- console
|
||||
(* ASYNC_REG = "TRUE" *) reg [1:0] rx_req_s, tx_ack_s;
|
||||
always @(posedge clk) begin
|
||||
rx_req_s <= {rx_req_s[0], rx_req};
|
||||
tx_ack_s <= {tx_ack_s[0], tx_ack};
|
||||
end
|
||||
wire rx_avail = rx_req_s[1] != rx_ack;
|
||||
wire tx_ready = tx_ack_s[1] == tx_req;
|
||||
|
||||
// ------------------------------------------------------------ graphics
|
||||
// 76800 pixels: a 64K array and an 11264 one, so they take about 9.5 block
|
||||
// RAMs (one array would be built as 96K deep, 12 of them).
|
||||
localparam GFX_PIXELS = 320 * 240;
|
||||
(* ram_style = "block" *) reg [3:0] gfx_lo [0:65535];
|
||||
(* ram_style = "block" *) reg [3:0] gfx_hi [0:GFX_PIXELS - 65536 - 1];
|
||||
integer gi;
|
||||
initial begin
|
||||
for (gi = 0; gi < 65536; gi = gi + 1) gfx_lo[gi] = 4'd0;
|
||||
for (gi = 0; gi < GFX_PIXELS - 65536; gi = gi + 1) gfx_hi[gi] = 4'd0;
|
||||
end
|
||||
|
||||
reg [8:0] gx;
|
||||
reg [7:0] gy;
|
||||
reg [3:0] gfx_q_lo, gfx_q_hi, fill_c;
|
||||
reg gfx_q_sel;
|
||||
reg [16:0] fill_a;
|
||||
reg fill_busy;
|
||||
wire [16:0] gxy = {gy, 8'd0} + {2'd0, gy, 6'd0} + {8'd0, gx};
|
||||
wire plot = io_wr && io_addr == 8'h33 && gx < 9'd320 && gy < 8'd240;
|
||||
wire [16:0] g_addr = fill_busy ? fill_a : gxy;
|
||||
wire g_we = fill_busy || plot;
|
||||
wire [3:0] g_wd = fill_busy ? fill_c : io_wd[3:0];
|
||||
wire [3:0] gfx_q = gfx_q_sel ? gfx_q_hi : gfx_q_lo;
|
||||
|
||||
always @(posedge clk) begin
|
||||
if (g_we && !g_addr[16]) gfx_lo[g_addr[15:0]] <= g_wd;
|
||||
gfx_q_lo <= gfx_lo[g_addr[15:0]];
|
||||
end
|
||||
always @(posedge clk) begin
|
||||
if (g_we && g_addr[16]) gfx_hi[g_addr[13:0]] <= g_wd;
|
||||
gfx_q_hi <= gfx_hi[g_addr[13:0]];
|
||||
gfx_q_sel <= g_addr[16];
|
||||
end
|
||||
reg [3:0] gv_lo, gv_hi;
|
||||
reg gv_sel;
|
||||
always @(posedge vclk) begin
|
||||
gv_lo <= gfx_lo[gfx_vaddr[15:0]];
|
||||
gv_hi <= gfx_hi[gfx_vaddr[13:0]];
|
||||
gv_sel <= gfx_vaddr[16];
|
||||
end
|
||||
always @* gfx_vpix = gv_sel ? gv_hi : gv_lo;
|
||||
|
||||
// whole-array view for testbenches
|
||||
function [3:0] gfx(input [16:0] n);
|
||||
gfx = n[16] ? gfx_hi[n[13:0]] : gfx_lo[n[15:0]];
|
||||
endfunction
|
||||
|
||||
always @(posedge clk)
|
||||
if (rst) begin
|
||||
fill_busy <= 1'b0;
|
||||
gx <= 9'd0; gy <= 8'd0;
|
||||
scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0;
|
||||
end else begin
|
||||
if (io_wr) case (io_addr)
|
||||
8'h30: gx[7:0] <= io_wd;
|
||||
8'h31: gx[8] <= io_wd[0];
|
||||
8'h32: gy <= io_wd;
|
||||
8'h33: gx <= gx + 1'b1;
|
||||
8'h34: begin
|
||||
fill_busy <= 1'b1; fill_a <= 17'd0; fill_c <= io_wd[3:0];
|
||||
if (!io_wd[7]) begin scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0; end
|
||||
end
|
||||
8'h35: scroll_x[7:0] <= io_wd;
|
||||
8'h36: begin scroll_x[8] <= io_wd[0]; g_border <= io_wd[1]; end
|
||||
8'h37: scroll_y <= io_wd >= 8'd240 ? io_wd - 8'd240 : io_wd;
|
||||
default: ;
|
||||
endcase
|
||||
if (fill_busy) begin
|
||||
fill_a <= fill_a + 1'b1;
|
||||
if (fill_a == GFX_PIXELS - 1) fill_busy <= 1'b0;
|
||||
end
|
||||
end
|
||||
|
||||
// ------------------------------------------------------------- sprites
|
||||
wire [7:0] spr_q;
|
||||
sprites spr (
|
||||
.clk(clk), .rst(rst),
|
||||
.io_wr(io_wr), .io_port(io_addr), .io_wd(io_wd), .io_q(spr_q),
|
||||
.vclk(vclk), .go(sp_go), .line(sp_line), .rd_buf(sp_buf), .rd_x(sp_x),
|
||||
.rd_on(sp_on), .pix(sp_pix), .gfx_pix(gfx_vpix), .ftick(sp_ftick)
|
||||
);
|
||||
|
||||
// ---------------------------------------------------------------- sound
|
||||
wire [7:0] snd_q;
|
||||
sound #(.CPU_HZ(CPU_HZ)) snd (
|
||||
.clk(clk), .rst(rst), .hush(io_wr && io_addr == 8'h34 && !io_wd[7]),
|
||||
.io_wr(io_wr), .io_port(io_addr), .io_wd(io_wd), .io_q(snd_q), .audio(audio)
|
||||
);
|
||||
|
||||
// ---------------------------------------------------------- held keys
|
||||
// The keyboard's clock writes these; a bit that changes while it is read
|
||||
// just reads as before or after.
|
||||
reg [7:0] key_sel = 8'd0;
|
||||
always @(posedge clk) if (io_wr && io_addr == 8'h89) key_sel <= io_wd;
|
||||
wire [7:0] joy = {
|
||||
keys[8'h76], // Esc
|
||||
keys[8'h12] | keys[8'h59], // Shift
|
||||
keys[8'h5A] | keys[8'hDA], // Enter
|
||||
keys[8'h29] | keys[8'h14] | keys[8'h94], // Space, Ctrl
|
||||
keys[8'h23] | keys[8'hF4] | keys[8'h74], // D, right, keypad 6
|
||||
keys[8'h1C] | keys[8'hEB] | keys[8'h6B], // A, left, keypad 4
|
||||
keys[8'h1B] | keys[8'hF2] | keys[8'h72], // S, down, keypad 2
|
||||
keys[8'h1D] | keys[8'hF5] | keys[8'h75] // W, up, keypad 8
|
||||
};
|
||||
|
||||
// --------------------------------------------------------------- flash
|
||||
wire [7:0] fl_ctl_q, fl_dat_q;
|
||||
flash_spi flash (
|
||||
.clk(clk), .rst(rst),
|
||||
.wr_ctl(io_wr && io_addr == 8'h40), .wr_dat(io_wr && io_addr == 8'h41),
|
||||
.wdata(io_wd), .ctl_q(fl_ctl_q), .dat_q(fl_dat_q),
|
||||
.cs_n(fl_cs_n), .sck(fl_sck), .mosi(fl_mosi), .miso(fl_miso)
|
||||
);
|
||||
|
||||
// ------------------------------------------------- milli/microseconds
|
||||
localparam MS_DIV = CPU_HZ / 1000;
|
||||
localparam US_DIV = CPU_HZ / 1000000;
|
||||
reg [31:0] ms, ms_latch, us, tcmp;
|
||||
reg [17:0] ms_div;
|
||||
reg [7:0] us_div;
|
||||
always @(posedge clk)
|
||||
if (rst) begin
|
||||
ms <= 0; ms_div <= 0; us <= 0; us_div <= 0; tcmp <= 32'h8000_0000;
|
||||
end else begin
|
||||
if (ms_div == MS_DIV - 1) begin ms <= ms + 1'b1; ms_div <= 0; end
|
||||
else ms_div <= ms_div + 1'b1;
|
||||
if (us_div == US_DIV - 1) begin us <= us + 1'b1; us_div <= 0; end
|
||||
else us_div <= us_div + 1'b1;
|
||||
if (wr_word && io_a[7:0] == 8'h08) tcmp <= io_ww;
|
||||
end
|
||||
wire [31:0] t_left = us - tcmp;
|
||||
reg timer_r;
|
||||
always @(posedge clk) timer_r <= !t_left[31];
|
||||
assign timer_irq = timer_r;
|
||||
|
||||
// ------------------------------------------------------------- monitor
|
||||
// An NMI request waits (in nmi_r) until the core takes it; breakpoint hits
|
||||
// go straight to the core as their instruction starts.
|
||||
reg [31:0] bp0, bp1;
|
||||
reg [1:0] bp_on, hit_d;
|
||||
reg [2:0] why, why_r;
|
||||
reg nmi_r, in_mon;
|
||||
wire hit0 = bp_on[0] && dbg_decode && dbg_pc == bp0;
|
||||
wire hit1 = bp_on[1] && dbg_decode && dbg_pc == bp1;
|
||||
assign nmi = nmi_r || hit0 || hit1;
|
||||
|
||||
always @(posedge clk) begin
|
||||
in_mon <= dbg_pc[31:15] == MON_BASE[31:15];
|
||||
hit_d <= {hit1, hit0};
|
||||
if (rst) begin
|
||||
nmi_r <= 1'b0; step_arm <= 1'b0; bp_on <= 2'b00; why <= 3'd0;
|
||||
bp0 <= 32'd0; bp1 <= 32'd0;
|
||||
end else begin
|
||||
if (io_wr) case (io_addr)
|
||||
8'h60: step_arm <= io_wd[1];
|
||||
8'h67: bp_on <= io_wd[1:0];
|
||||
default: ;
|
||||
endcase
|
||||
if (wr_word && io_a[7:0] == 8'h10) bp0 <= io_ww;
|
||||
if (wr_word && io_a[7:0] == 8'h14) bp1 <= io_ww;
|
||||
if (!nmi_r && !in_mon) begin
|
||||
if (brk_key) begin
|
||||
nmi_r <= 1'b1; why_r <= 3'd1;
|
||||
end else if (io_wr && io_addr == 8'h61) begin
|
||||
nmi_r <= 1'b1; why_r <= 3'd2;
|
||||
end
|
||||
end
|
||||
if (nmi_taken) begin
|
||||
why <= nmi_r ? why_r : hit_d[0] ? 3'd4 : hit_d[1] ? 3'd5 : 3'd3;
|
||||
nmi_r <= 1'b0;
|
||||
step_arm <= 1'b0;
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
// -------------------------------------------------------- port reads
|
||||
always @* case (io_addr)
|
||||
8'h00: io_din = {6'd0, tx_ready, rx_avail};
|
||||
8'h01: io_din = rx_data;
|
||||
8'h10: io_din = ms[7:0]; // sampled on the same edge that fills ms_latch
|
||||
8'h11: io_din = ms_latch[15:8];
|
||||
8'h12: io_din = ms_latch[23:16];
|
||||
8'h13: io_din = ms_latch[31:24];
|
||||
8'h33: io_din = {4'd0, gfx_q};
|
||||
8'h34: io_din = {7'd0, fill_busy};
|
||||
8'h40: io_din = fl_ctl_q;
|
||||
8'h41: io_din = fl_dat_q;
|
||||
8'h60: io_din = {5'd0, why};
|
||||
8'h78, 8'h80, 8'h81, 8'h82, 8'h83, 8'h84, 8'h85, 8'h86, 8'h87:
|
||||
io_din = spr_q;
|
||||
8'h88: io_din = joy;
|
||||
8'h89: io_din = {7'd0, keys[key_sel]};
|
||||
8'h98, 8'h99, 8'h9A:
|
||||
io_din = snd_q;
|
||||
default: io_din = 8'hFF;
|
||||
endcase
|
||||
always @* case (io_a[7:0])
|
||||
8'h00: word_q = ms;
|
||||
8'h04: word_q = us;
|
||||
8'h08: word_q = tcmp;
|
||||
8'h10: word_q = bp0;
|
||||
8'h14: word_q = bp1;
|
||||
default: word_q = 32'd0;
|
||||
endcase
|
||||
|
||||
always @(posedge clk) begin
|
||||
if (io_go) io_q <= io_bytes ? {24'd0, io_din} << {io_a[1:0], 3'b000} : word_q;
|
||||
if (rst) begin
|
||||
rx_ack <= rx_req_s[1];
|
||||
tx_req <= tx_ack_s[1];
|
||||
seg_val <= 16'h0032;
|
||||
ms_latch <= 32'd0;
|
||||
end else begin
|
||||
if (io_rd && io_addr == 8'h01 && rx_avail) rx_ack <= ~rx_ack;
|
||||
if (io_rd && io_addr == 8'h10) ms_latch <= ms;
|
||||
if (io_wr && io_addr == 8'h01 && tx_ready) begin
|
||||
tx_data <= io_wd;
|
||||
tx_req <= ~tx_req;
|
||||
end
|
||||
if (io_wr && io_addr == 8'h20) seg_val[7:0] <= io_wd;
|
||||
if (io_wr && io_addr == 8'h21) seg_val[15:8] <= io_wd;
|
||||
end
|
||||
end
|
||||
endmodule
|
||||
@@ -0,0 +1,29 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// Four-digit hex display, digit 3 (left) = value[15:12]. Segments active low.
|
||||
module sevenseg (
|
||||
input clk,
|
||||
input [15:0] value,
|
||||
output reg [6:0] seg,
|
||||
output reg [3:0] an
|
||||
);
|
||||
reg [17:0] cnt;
|
||||
always @(posedge clk) cnt <= cnt + 1'b1;
|
||||
|
||||
wire [1:0] d = cnt[17:16];
|
||||
wire [3:0] x = value[d*4 +: 4];
|
||||
|
||||
always @(posedge clk) begin
|
||||
an <= ~(4'b0001 << d);
|
||||
case (x) // gfedcba
|
||||
4'h0: seg <= 7'b1000000; 4'h1: seg <= 7'b1111001;
|
||||
4'h2: seg <= 7'b0100100; 4'h3: seg <= 7'b0110000;
|
||||
4'h4: seg <= 7'b0011001; 4'h5: seg <= 7'b0010010;
|
||||
4'h6: seg <= 7'b0000010; 4'h7: seg <= 7'b1111000;
|
||||
4'h8: seg <= 7'b0000000; 4'h9: seg <= 7'b0010000;
|
||||
4'hA: seg <= 7'b0001000; 4'hB: seg <= 7'b0000011;
|
||||
4'hC: seg <= 7'b1000110; 4'hD: seg <= 7'b0100001;
|
||||
4'hE: seg <= 7'b0000110; 4'hF: seg <= 7'b0001110;
|
||||
endcase
|
||||
end
|
||||
endmodule
|
||||
+173
@@ -0,0 +1,173 @@
|
||||
`timescale 1ns / 1ps
|
||||
// A small sound chip for the Z-80, in the style of the 8-bit era's
|
||||
// programmable sound generators, played through a Digilent PmodAMP2.
|
||||
//
|
||||
// Three square-wave voices and a noise source. Each voice plays its tone,
|
||||
// the noise, or both (the mixer), at one of 16 volumes on a log scale (about
|
||||
// 3 dB a step). A volume write starts an envelope: an optional rise from 0 at
|
||||
// the attack rate, then an optional fall to 0 at the decay rate, so a game
|
||||
// can fire off a shot or an explosion with a few OUTs and leave it.
|
||||
//
|
||||
// 90h/91h voice 0 pitch, low byte / high 4 bits frequency = 125000 / pitch Hz
|
||||
// 92h/93h voice 1 pitch (pitch 0 counts as 1)
|
||||
// 94h/95h voice 2 pitch
|
||||
// 96h noise pitch, 0-31: higher is lower and rougher
|
||||
// 97h mixer: bits 0-2 tone on for voices 0-2, bits 3-5 noise on for them
|
||||
// 98h-9Ah voice 0-2 volume 0-15, which (re)starts its envelope; reading
|
||||
// gives the volume it is at now, so 0 means the sound has ended
|
||||
// 9Bh-9Dh voice 0-2 envelope: high nibble attack, low nibble decay, each
|
||||
// n x 4 ms a volume step (0 = jump straight up / hold)
|
||||
//
|
||||
// A voice with neither its tone nor the noise on outputs a steady level, as
|
||||
// on the old chips. `hush` (CLS) and reset silence everything.
|
||||
//
|
||||
// The three voices are added up and turned into a single bit by a first-order
|
||||
// sigma-delta modulator at the full clock rate; the PmodAMP2 filters it and
|
||||
// drives headphones or a speaker.
|
||||
module sound #(
|
||||
parameter CPU_HZ = 90_000_000
|
||||
) (
|
||||
input clk,
|
||||
input rst,
|
||||
input hush,
|
||||
input io_wr,
|
||||
input [7:0] io_port,
|
||||
input [7:0] io_wd,
|
||||
output reg [7:0] io_q, // for reads of 98h-9Ah
|
||||
output reg audio = 1'b0
|
||||
);
|
||||
localparam TICK_DIV = CPU_HZ / 250_000; // tone counters run at 250 kHz
|
||||
localparam MS4_DIV = CPU_HZ / 250; // envelope steps in 4 ms units
|
||||
|
||||
reg [11:0] pitch [0:2];
|
||||
reg [4:0] npitch;
|
||||
reg [5:0] mixer;
|
||||
reg [3:0] vol [0:2]; // the volume asked for
|
||||
reg [3:0] cur [0:2]; // the volume now
|
||||
reg [3:0] att [0:2], dec [0:2];
|
||||
reg rising [0:2];
|
||||
|
||||
// ---------------------------------------------------------------- ticks
|
||||
reg [9:0] tdiv = 10'd0;
|
||||
reg tick = 1'b0;
|
||||
reg [19:0] ediv = 20'd0;
|
||||
reg etick = 1'b0;
|
||||
always @(posedge clk) begin
|
||||
tick <= tdiv == 0;
|
||||
tdiv <= tdiv == 0 ? TICK_DIV - 1 : tdiv - 1'b1;
|
||||
etick <= ediv == 0;
|
||||
ediv <= ediv == 0 ? MS4_DIV - 1 : ediv - 1'b1;
|
||||
end
|
||||
|
||||
// --------------------------------------------------------------- voices
|
||||
reg [11:0] tcnt [0:2];
|
||||
reg [2:0] tone = 3'd0;
|
||||
reg [4:0] ncnt = 5'd0;
|
||||
reg [16:0] lfsr = 17'd1;
|
||||
reg [3:0] ecnt [0:2];
|
||||
integer i;
|
||||
initial for (i = 0; i < 3; i = i + 1) begin
|
||||
tcnt[i] = 12'd0; ecnt[i] = 4'd0; cur[i] = 4'd0; vol[i] = 4'd0;
|
||||
end
|
||||
|
||||
always @(posedge clk) begin
|
||||
if (rst || hush) begin
|
||||
for (i = 0; i < 3; i = i + 1) begin
|
||||
vol[i] <= 4'd0; cur[i] <= 4'd0; rising[i] <= 1'b0;
|
||||
if (rst) begin pitch[i] <= 12'd0; att[i] <= 4'd0; dec[i] <= 4'd0; end
|
||||
end
|
||||
mixer <= 6'd0;
|
||||
if (rst) begin npitch <= 5'd0; lfsr <= 17'd1; tone <= 3'd0; end
|
||||
end else begin
|
||||
if (io_wr) case (io_port)
|
||||
8'h90: pitch[0][7:0] <= io_wd;
|
||||
8'h91: pitch[0][11:8] <= io_wd[3:0];
|
||||
8'h92: pitch[1][7:0] <= io_wd;
|
||||
8'h93: pitch[1][11:8] <= io_wd[3:0];
|
||||
8'h94: pitch[2][7:0] <= io_wd;
|
||||
8'h95: pitch[2][11:8] <= io_wd[3:0];
|
||||
8'h96: npitch <= io_wd[4:0];
|
||||
8'h97: mixer <= io_wd[5:0];
|
||||
8'h9B: {att[0], dec[0]} <= io_wd;
|
||||
8'h9C: {att[1], dec[1]} <= io_wd;
|
||||
8'h9D: {att[2], dec[2]} <= io_wd;
|
||||
default: ;
|
||||
endcase
|
||||
|
||||
// tones: each output flips every `pitch` ticks
|
||||
if (tick) begin
|
||||
for (i = 0; i < 3; i = i + 1)
|
||||
if (tcnt[i] <= 12'd1) begin
|
||||
tcnt[i] <= pitch[i];
|
||||
tone[i] <= ~tone[i];
|
||||
end else
|
||||
tcnt[i] <= tcnt[i] - 1'b1;
|
||||
if (ncnt == 0) begin
|
||||
ncnt <= npitch;
|
||||
lfsr <= {lfsr[0] ^ lfsr[3], lfsr[16:1]};
|
||||
end else
|
||||
ncnt <= ncnt - 1'b1;
|
||||
end
|
||||
|
||||
// envelopes
|
||||
for (i = 0; i < 3; i = i + 1) begin
|
||||
if (io_wr && io_port == 8'h98 + i) begin
|
||||
vol[i] <= io_wd[3:0];
|
||||
ecnt[i] <= 4'd0;
|
||||
if (att[i] != 0 && io_wd[3:0] != 0) begin
|
||||
cur[i] <= 4'd0; rising[i] <= 1'b1;
|
||||
end else begin
|
||||
cur[i] <= io_wd[3:0]; rising[i] <= 1'b0;
|
||||
end
|
||||
end else if (etick) begin
|
||||
if (rising[i]) begin
|
||||
if (ecnt[i] + 1'b1 >= att[i]) begin
|
||||
ecnt[i] <= 4'd0;
|
||||
cur[i] <= cur[i] + 1'b1;
|
||||
if (cur[i] + 1'b1 >= vol[i]) rising[i] <= 1'b0;
|
||||
end else
|
||||
ecnt[i] <= ecnt[i] + 1'b1;
|
||||
end else if (dec[i] != 0 && cur[i] != 0) begin
|
||||
if (ecnt[i] + 1'b1 >= dec[i]) begin
|
||||
ecnt[i] <= 4'd0;
|
||||
cur[i] <= cur[i] - 1'b1;
|
||||
end else
|
||||
ecnt[i] <= ecnt[i] + 1'b1;
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
always @* case (io_port)
|
||||
8'h98: io_q = {4'd0, cur[0]};
|
||||
8'h99: io_q = {4'd0, cur[1]};
|
||||
8'h9A: io_q = {4'd0, cur[2]};
|
||||
default: io_q = 8'hFF;
|
||||
endcase
|
||||
|
||||
// ---------------------------------------------------------------- mixing
|
||||
function [7:0] level(input [3:0] v); // about 3 dB a step
|
||||
case (v)
|
||||
4'd0: level = 8'd0; 4'd1: level = 8'd2; 4'd2: level = 8'd3; 4'd3: level = 8'd4;
|
||||
4'd4: level = 8'd6; 4'd5: level = 8'd8; 4'd6: level = 8'd11; 4'd7: level = 8'd16;
|
||||
4'd8: level = 8'd23; 4'd9: level = 8'd32; 4'd10: level = 8'd45; 4'd11: level = 8'd64;
|
||||
4'd12: level = 8'd90; 4'd13: level = 8'd128; 4'd14: level = 8'd181; default: level = 8'd255;
|
||||
endcase
|
||||
endfunction
|
||||
|
||||
wire [2:0] on;
|
||||
genvar g;
|
||||
generate for (g = 0; g < 3; g = g + 1) begin : mix
|
||||
assign on[g] = (tone[g] | ~mixer[g]) & (lfsr[0] | ~mixer[g + 3]);
|
||||
end endgenerate
|
||||
|
||||
reg [9:0] sum = 10'd0;
|
||||
reg [10:0] acc = 11'd0;
|
||||
always @(posedge clk) begin
|
||||
sum <= (on[0] ? level(cur[0]) : 10'd0) + (on[1] ? level(cur[1]) : 10'd0) +
|
||||
(on[2] ? level(cur[2]) : 10'd0);
|
||||
acc <= rst ? 11'd0 : {1'b0, acc[9:0]} + {1'b0, sum}; // carry out = the output bit
|
||||
audio <= acc[10];
|
||||
end
|
||||
endmodule
|
||||
+237
@@ -0,0 +1,237 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// Hardware sprites over the 320x240 graphics layer.
|
||||
//
|
||||
// 32 sprites of 16x16 graphics pixels in the 16 colours (0 = transparent),
|
||||
// drawn from 64 shapes. Sprite 0 is on top of sprite 1 and so on; all sprites
|
||||
// are over the graphics and under the text. Each sprite has a position, a
|
||||
// shape, X and Y flip and an on/off bit.
|
||||
//
|
||||
// Z-80 ports (low address byte):
|
||||
// 70 out select a sprite, 0-31
|
||||
// 71 out X bits 7:0 72 out X bit 8
|
||||
// 73 out Y; moves the selected sprite to the X and Y given (both at once)
|
||||
// 74 out shape, 0-63
|
||||
// 75 out bit 0 = on, bit 1 = flip left-right, bit 2 = flip upside down
|
||||
// 76 out start loading shape n (0-63) at its top left
|
||||
// 77 out two pixels of the shape, left one in bits 7:4, then move on;
|
||||
// 128 bytes make a shape, a row of 16 pixels at a time
|
||||
// 78 in frame counter, +1 at the start of each vertical blank
|
||||
// 80-83 in sprites 0-7, 8-15, 16-23, 24-31 that touched another sprite
|
||||
// in the last frame
|
||||
// 84-87 in sprites that touched a graphics pixel (not colour 0)
|
||||
// 34 out (the graphics fill) also turns every sprite off unless bit 7 is set
|
||||
//
|
||||
// X and Y are the top-left pixel and wrap at 512 and 256, so a sprite can go
|
||||
// partly off the left or top edge: X = 500 is 12 pixels left of the screen.
|
||||
//
|
||||
// Drawing: the video asks for each graphics line one line ahead (go, line).
|
||||
// The engine clears that line's buffer, then walks the sprites from 31 down
|
||||
// to 0, writing each visible pixel, so lower numbers end up on top. A pixel
|
||||
// written over another sprite's records a collision for both. The video reads
|
||||
// the other buffer, one pixel per clock. A line takes at most about 900 of the
|
||||
// 6,752 pixel clocks that a graphics line lasts.
|
||||
module sprites (
|
||||
// Z-80 side
|
||||
input clk,
|
||||
input rst,
|
||||
input io_wr,
|
||||
input [7:0] io_port,
|
||||
input [7:0] io_wd,
|
||||
output reg [7:0] io_q,
|
||||
// video side (pixel clock)
|
||||
input vclk,
|
||||
input go, // prepare graphics line `line`
|
||||
input [7:0] line,
|
||||
input rd_buf, // buffer of the line on screen
|
||||
input [8:0] rd_x, // graphics x on screen, registered
|
||||
input rd_on, // inside the graphics area
|
||||
output reg [3:0] pix, // sprite colour there, a clock later
|
||||
input [3:0] gfx_pix, // graphics colour there, same clock as pix
|
||||
input ftick // start of vertical blank
|
||||
);
|
||||
integer i;
|
||||
|
||||
// ------------------------------------------------------------ Z-80 side
|
||||
reg [4:0] sel = 5'd0;
|
||||
reg [8:0] x_stage = 9'd0;
|
||||
reg [31:0] en = 32'd0;
|
||||
reg [12:0] pat_a = 13'd0;
|
||||
(* ram_style = "distributed" *) reg [16:0] pos [0:31]; // {y, x}
|
||||
(* ram_style = "distributed" *) reg [7:0] shp [0:31]; // {flip y, flip x, shape}
|
||||
(* ram_style = "block" *) reg [7:0] pat [0:8191]; // shape, row, pixel pair
|
||||
initial begin
|
||||
for (i = 0; i < 32; i = i + 1) begin pos[i] = 17'd0; shp[i] = 8'd0; end
|
||||
for (i = 0; i < 8192; i = i + 1) pat[i] = 8'd0;
|
||||
end
|
||||
|
||||
wire [7:0] shp_sel = shp[sel];
|
||||
always @(posedge clk) begin
|
||||
if (io_wr) case (io_port)
|
||||
8'h73: pos[sel] <= {io_wd, x_stage};
|
||||
8'h74: shp[sel] <= {shp_sel[7:6], io_wd[5:0]};
|
||||
8'h75: shp[sel] <= {io_wd[2:1], shp_sel[5:0]};
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
always @(posedge clk) if (io_wr && io_port == 8'h77) pat[pat_a] <= io_wd;
|
||||
|
||||
always @(posedge clk)
|
||||
if (rst) begin
|
||||
en <= 32'd0;
|
||||
sel <= 5'd0;
|
||||
x_stage <= 9'd0;
|
||||
pat_a <= 13'd0;
|
||||
end else if (io_wr) case (io_port)
|
||||
8'h34: if (!io_wd[7]) en <= 32'd0;
|
||||
8'h70: sel <= io_wd[4:0];
|
||||
8'h71: x_stage[7:0] <= io_wd;
|
||||
8'h72: x_stage[8] <= io_wd[0];
|
||||
8'h75: en[sel] <= io_wd[0];
|
||||
8'h76: pat_a <= {io_wd[5:0], 7'd0};
|
||||
8'h77: pat_a <= pat_a + 1'b1;
|
||||
default: ;
|
||||
endcase
|
||||
|
||||
// ----------------------------------------------------------- line fill
|
||||
// line buffers: {buffer, x} -> {colour, sprite}
|
||||
(* ram_style = "distributed" *) reg [8:0] lb [0:1023];
|
||||
initial for (i = 0; i < 1024; i = i + 1) lb[i] = 9'd0;
|
||||
|
||||
localparam S_IDLE = 2'd0, S_CLEAR = 2'd1, S_SPR = 2'd2;
|
||||
reg [1:0] st = S_IDLE;
|
||||
reg [8:0] cx;
|
||||
reg wb;
|
||||
reg [7:0] ln;
|
||||
reg [4:0] s;
|
||||
reg drawing = 1'b0;
|
||||
reg [3:0] col;
|
||||
reg [8:0] sx;
|
||||
reg [3:0] srow;
|
||||
reg sfx;
|
||||
reg [5:0] sshape;
|
||||
|
||||
// the sprite being considered
|
||||
wire [16:0] s_pos = pos[s];
|
||||
wire [7:0] s_shp = shp[s];
|
||||
wire [7:0] s_row = ln - s_pos[16:9];
|
||||
wire s_vis = en[s] && s_row < 8'd16;
|
||||
|
||||
// pixel pipeline: address, pattern read, write
|
||||
reg [12:0] pat_ra;
|
||||
reg [7:0] pat_q;
|
||||
reg p0_v = 1'b0, p1_v = 1'b0, p0_n, p1_n;
|
||||
reg [8:0] p0_x, p1_x;
|
||||
reg [4:0] p0_s, p1_s;
|
||||
wire [3:0] cc = sfx ? ~col : col;
|
||||
|
||||
always @(posedge vclk) pat_q <= pat[pat_ra];
|
||||
|
||||
always @(posedge vclk) begin
|
||||
p0_v <= 1'b0;
|
||||
if (go) begin
|
||||
st <= S_CLEAR;
|
||||
cx <= 9'd0;
|
||||
wb <= line[0];
|
||||
ln <= line;
|
||||
drawing <= 1'b0;
|
||||
end else case (st)
|
||||
S_CLEAR: begin
|
||||
cx <= cx + 1'b1;
|
||||
if (cx == 9'd319) begin
|
||||
st <= S_SPR;
|
||||
s <= 5'd31;
|
||||
end
|
||||
end
|
||||
S_SPR:
|
||||
if (!drawing) begin
|
||||
if (s_vis) begin
|
||||
drawing <= 1'b1;
|
||||
col <= 4'd0;
|
||||
sx <= s_pos[8:0];
|
||||
srow <= s_shp[7] ? ~s_row[3:0] : s_row[3:0];
|
||||
sfx <= s_shp[6];
|
||||
sshape <= s_shp[5:0];
|
||||
end else if (s == 5'd0)
|
||||
st <= S_IDLE;
|
||||
else
|
||||
s <= s - 1'b1;
|
||||
end else begin
|
||||
pat_ra <= {sshape, srow, cc[3:1]};
|
||||
p0_n <= cc[0];
|
||||
p0_x <= sx + col;
|
||||
p0_s <= s;
|
||||
p0_v <= 1'b1;
|
||||
col <= col + 1'b1;
|
||||
if (col == 4'd15) begin
|
||||
drawing <= 1'b0;
|
||||
if (s == 5'd0) st <= S_IDLE;
|
||||
else s <= s - 1'b1;
|
||||
end
|
||||
end
|
||||
default: ;
|
||||
endcase
|
||||
{p1_v, p1_n, p1_x, p1_s} <= {p0_v, p0_n, p0_x, p0_s};
|
||||
end
|
||||
|
||||
wire [3:0] w_col = p1_n ? pat_q[3:0] : pat_q[7:4];
|
||||
wire w_spr = p1_v && w_col != 4'd0 && p1_x < 9'd320;
|
||||
wire w_clr = st == S_CLEAR;
|
||||
wire [9:0] wa = {wb, w_clr ? cx : p1_x};
|
||||
wire [8:0] old = lb[wa];
|
||||
always @(posedge vclk)
|
||||
if (w_clr || w_spr) lb[wa] <= w_clr ? 9'd0 : {w_col, p1_s};
|
||||
|
||||
// ----------------------------------------------------------- video read
|
||||
wire [8:0] rd = lb[{rd_buf, rd_x}];
|
||||
reg [4:0] pix_id;
|
||||
always @(posedge vclk) begin
|
||||
pix <= rd_on ? rd[8:5] : 4'd0;
|
||||
pix_id <= rd[4:0];
|
||||
end
|
||||
|
||||
// ----------------------------------------------------------- collisions
|
||||
reg [31:0] ss_acc = 32'd0, bg_acc = 32'd0, ss_frame = 32'd0, bg_frame = 32'd0;
|
||||
reg ft = 1'b0;
|
||||
always @(posedge vclk)
|
||||
if (ftick) begin
|
||||
ss_frame <= ss_acc;
|
||||
bg_frame <= bg_acc;
|
||||
ss_acc <= 32'd0;
|
||||
bg_acc <= 32'd0;
|
||||
ft <= ~ft;
|
||||
end else begin
|
||||
if (pix != 4'd0 && gfx_pix != 4'd0) bg_acc[pix_id] <= 1'b1;
|
||||
if (w_spr && old[8:5] != 4'd0) begin
|
||||
ss_acc[p1_s] <= 1'b1;
|
||||
ss_acc[old[4:0]] <= 1'b1;
|
||||
end
|
||||
end
|
||||
|
||||
// A frame's results are steady for a whole frame after ft flips, so the
|
||||
// Z-80 side copies them once it has seen the flip.
|
||||
(* ASYNC_REG = "TRUE" *) reg [2:0] ft_s = 3'd0;
|
||||
reg [7:0] frames = 8'd0;
|
||||
reg [31:0] ss_cpu = 32'd0, bg_cpu = 32'd0;
|
||||
always @(posedge clk) begin
|
||||
ft_s <= {ft_s[1:0], ft};
|
||||
if (ft_s[2] != ft_s[1]) begin
|
||||
frames <= frames + 1'b1;
|
||||
ss_cpu <= ss_frame;
|
||||
bg_cpu <= bg_frame;
|
||||
end
|
||||
end
|
||||
|
||||
always @* case (io_port)
|
||||
8'h78: io_q = frames;
|
||||
8'h80: io_q = ss_cpu[7:0];
|
||||
8'h81: io_q = ss_cpu[15:8];
|
||||
8'h82: io_q = ss_cpu[23:16];
|
||||
8'h83: io_q = ss_cpu[31:24];
|
||||
8'h84: io_q = bg_cpu[7:0];
|
||||
8'h85: io_q = bg_cpu[15:8];
|
||||
8'h86: io_q = bg_cpu[23:16];
|
||||
8'h87: io_q = bg_cpu[31:24];
|
||||
default: io_q = 8'hFF;
|
||||
endcase
|
||||
endmodule
|
||||
+789
@@ -0,0 +1,789 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// VT220 terminal engine with ANSI colour. Consumes received bytes and keeps
|
||||
// the character buffer up to date: 80x32, or 132x64 after DECCOLM (CSI ?3h).
|
||||
// The buffer is row-major with a 132-cell stride in both (address = row*132 +
|
||||
// col), so scrolling copies whole 132-cell rows either way.
|
||||
//
|
||||
// Cell format: {blink, underline, bg[3:0], fg[3:0], CP437 code}. Reverse,
|
||||
// bold and concealed text are resolved into the colours when a new_cell is written.
|
||||
//
|
||||
// Supported (beyond C0 controls):
|
||||
// ESC 7 8 D E H M Z c = > ESC ( ) <0|A|B> ESC # 8
|
||||
// CSI A B C D E F G H I J K L M P S T X Z @ ` a d e f g h l m n q r s u c
|
||||
// CSI ? J K h l n CSI > c CSI ! p
|
||||
// Modes: IRM(4) LNM(20) DECCKM(?1) DECCOLM(?3) DECSCLM(?4) DECOM(?6) DECAWM(?7)
|
||||
// DECTCEM(?25)
|
||||
// alternate screen ?47 (parameters stop at 255, so not ?1047/?1049)
|
||||
// SGR 0 1 4 5 7 8 21-28 30-49 90-107, 38/48;5;n and 38/48;2;r;g;b (approximated)
|
||||
// DECLL (CSI n q) drives four LEDs.
|
||||
// OSC, DCS, APC, PM and SOS strings (e.g. titles, OSC 3008) are discarded.
|
||||
// Scrolling copies rows in memory. With smooth scroll on, a line feed at the
|
||||
// bottom margin first slides the region up a few pixels per frame.
|
||||
|
||||
module term_ctrl (
|
||||
input clk,
|
||||
input rst,
|
||||
input [7:0] in_byte,
|
||||
input in_empty,
|
||||
output reg in_rd,
|
||||
input lf_cr, // LF also returns the carriage
|
||||
input clear_req,
|
||||
input [3:0] def_fg, // default foreground colour index
|
||||
input smooth_sw, // force smooth scrolling on
|
||||
input smooth_fast,
|
||||
input frame_tick, // start of vertical blanking
|
||||
// character RAM port
|
||||
output reg [13:0] addr,
|
||||
output reg we,
|
||||
output reg [17:0] wdata,
|
||||
input [17:0] rdata,
|
||||
// to the display
|
||||
output reg [5:0] row,
|
||||
output reg [7:0] col,
|
||||
output reg cur_vis,
|
||||
output reg [5:0] stop, // scroll region, inclusive
|
||||
output reg [5:0] sbot,
|
||||
output reg sm_on,
|
||||
output reg sm_dir, // 0 = scrolling up, 1 = down
|
||||
output reg [4:0] sm_off,
|
||||
// replies to the host (DA, DSR)
|
||||
output reg [7:0] resp_byte,
|
||||
output reg resp_wr,
|
||||
input resp_full,
|
||||
// modes the keyboard needs
|
||||
output reg cursor_app,
|
||||
output reg keypad_app,
|
||||
output reg lnm,
|
||||
output reg [3:0] dec_leds,
|
||||
output reg bell,
|
||||
output reg alt_scr, // showing and writing the alternate screen
|
||||
output reg wide // 132 columns (DECCOLM)
|
||||
);
|
||||
localparam [13:0] STRIDE = 14'd132;
|
||||
localparam [13:0] ALL = 14'd8448; // 132 x 64: clears either layout
|
||||
wire [5:0] LAST_ROW = wide ? 6'd63 : 6'd31;
|
||||
wire [7:0] LAST_COL = wide ? 8'd131 : 8'd79;
|
||||
wire [7:0] NCOLS = wide ? 8'd132 : 8'd80;
|
||||
wire [6:0] NROWS = wide ? 7'd64 : 7'd32;
|
||||
wire [13:0] CELLS = wide ? 14'd8448 : 14'd4224;
|
||||
|
||||
localparam [3:0] S_IDLE = 4'd0, S_DISP = 4'd1, S_FILL = 4'd2, S_CRD = 4'd3,
|
||||
S_CWAIT = 4'd4, S_CWR = 4'd5, S_PLOOP = 4'd6, S_RESP = 4'd7,
|
||||
S_PUT = 4'd8, S_TAB = 4'd9, S_SMOOTH = 4'd10, S_SMEND = 4'd11,
|
||||
S_OP = 4'd12, S_EXEC = 4'd13, S_SCR1 = 4'd14, S_SCR2 = 4'd15;
|
||||
localparam [2:0] P_NORM = 3'd0, P_ESC = 3'd1, P_ESCI = 3'd2, P_CSI = 3'd3, P_STR = 3'd4;
|
||||
localparam [1:0] K_SGR = 2'd0, K_SET = 2'd1, K_RST = 2'd2, K_LL = 2'd3;
|
||||
|
||||
reg [3:0] st, ret_st, sm_ret;
|
||||
reg [2:0] ps;
|
||||
reg [7:0] c;
|
||||
|
||||
// modes and attributes
|
||||
reg pend, awm, irm, om, sclm;
|
||||
reg bold, ul, blink, rev, conceal;
|
||||
reg [3:0] fg, bg;
|
||||
reg [1:0] g0, g1; // 0 ASCII, 1 DEC special graphics, 2 UK
|
||||
reg gl; // 0 = G0 active, 1 = G1 (SO)
|
||||
reg [131:0] tabs;
|
||||
reg colm_clr; // DECCOLM: clear the screen when idle
|
||||
|
||||
// saved cursor (DECSC)
|
||||
reg [5:0] sv_row;
|
||||
reg [7:0] sv_col;
|
||||
reg sv_pend, sv_om, sv_gl, sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal;
|
||||
reg [3:0] sv_fg, sv_bg;
|
||||
reg [1:0] sv_g0, sv_g1;
|
||||
|
||||
// parser
|
||||
reg [7:0] par [0:15];
|
||||
reg [3:0] pidx;
|
||||
reg priv, gtm, ign;
|
||||
reg [7:0] cint, inter;
|
||||
|
||||
// scroll request
|
||||
reg sc_dir;
|
||||
reg [5:0] sc_t, sc_b;
|
||||
reg [7:0] sc_n;
|
||||
reg [6:0] sc_h, sc_k; // region height, lines to move
|
||||
|
||||
// memory copy / fill engine
|
||||
reg [13:0] c_src, c_dst, c_cnt, f_addr, f_cnt;
|
||||
reg c_dir;
|
||||
reg [17:0] f_cell;
|
||||
|
||||
// parameter loop (SGR, SM/RM, DECLL)
|
||||
reg [1:0] pk;
|
||||
reg [3:0] pi;
|
||||
reg [1:0] ext; // 38/48 extended colour state
|
||||
reg ext_bg;
|
||||
reg [1:0] ext_n;
|
||||
reg [7:0] ext_r, ext_g;
|
||||
|
||||
reg [95:0] rbuf;
|
||||
reg [3:0] rlen;
|
||||
reg [7:0] rep;
|
||||
reg tdir;
|
||||
|
||||
integer i;
|
||||
|
||||
// ------------------------------------------------------------ helpers
|
||||
function [13:0] rc(input [5:0] r, input [7:0] cc);
|
||||
rc = {1'b0, r, 7'b0} + {6'b0, r, 2'b0} + {6'b0, cc};
|
||||
endfunction
|
||||
|
||||
function [13:0] rows(input [6:0] n); // n whole rows of cells
|
||||
rows = {n, 7'b0} + {5'b0, n, 2'b0};
|
||||
endfunction
|
||||
|
||||
function [7:0] acc(input [7:0] p, input [7:0] d);
|
||||
reg [11:0] v;
|
||||
begin
|
||||
v = p * 10 + (d - "0");
|
||||
acc = v > 255 ? 8'd255 : v[7:0];
|
||||
end
|
||||
endfunction
|
||||
|
||||
// DEC special graphics to CP437
|
||||
function [7:0] gfx(input [7:0] ch);
|
||||
case (ch)
|
||||
8'h5F: gfx = 8'h20; 8'h60: gfx = 8'h04; 8'h61: gfx = 8'hB1;
|
||||
8'h66: gfx = 8'hF8; 8'h67: gfx = 8'hF1; 8'h6A: gfx = 8'hD9;
|
||||
8'h6B: gfx = 8'hBF; 8'h6C: gfx = 8'hDA; 8'h6D: gfx = 8'hC0;
|
||||
8'h6E: gfx = 8'hC5; 8'h6F: gfx = 8'hC4; 8'h70: gfx = 8'hC4;
|
||||
8'h71: gfx = 8'hC4; 8'h72: gfx = 8'hC4; 8'h73: gfx = 8'hC4;
|
||||
8'h74: gfx = 8'hC3; 8'h75: gfx = 8'hB4; 8'h76: gfx = 8'hC1;
|
||||
8'h77: gfx = 8'hC2; 8'h78: gfx = 8'hB3; 8'h79: gfx = 8'hF3;
|
||||
8'h7A: gfx = 8'hF2; 8'h7B: gfx = 8'hE3; 8'h7C: gfx = 8'hD8;
|
||||
8'h7D: gfx = 8'h9C; 8'h7E: gfx = 8'hFA;
|
||||
default: gfx = 8'hFE; // control-code pictures
|
||||
endcase
|
||||
endfunction
|
||||
|
||||
function [1:0] charset(input [7:0] ch);
|
||||
charset = ch == "0" ? 2'd1 : ch == "A" ? 2'd2 : 2'd0;
|
||||
endfunction
|
||||
|
||||
// colour index from levels: bit0 red, bit1 green, bit2 blue, bit3 bright
|
||||
function [3:0] approx(input hr, input hg, input hb, input bright, input dim);
|
||||
approx = (hr | hg | hb) ? {bright, hb, hg, hr} : (dim ? 4'd8 : 4'd0);
|
||||
endfunction
|
||||
|
||||
function [3:0] map256(input [7:0] n);
|
||||
reg [7:0] k;
|
||||
reg [2:0] r, g, b, mx;
|
||||
begin
|
||||
if (n < 16)
|
||||
map256 = n[3:0];
|
||||
else if (n >= 232)
|
||||
map256 = n < 238 ? 4'd0 : n < 244 ? 4'd8 : n < 250 ? 4'd7 : 4'd15;
|
||||
else begin
|
||||
k = n - 8'd16;
|
||||
r = k / 36;
|
||||
g = (k / 6) % 6;
|
||||
b = k % 6;
|
||||
mx = r > g ? (r > b ? r : b) : (g > b ? g : b);
|
||||
map256 = approx(r >= 3, g >= 3, b >= 3, mx >= 4, mx >= 2);
|
||||
end
|
||||
end
|
||||
endfunction
|
||||
|
||||
function [3:0] rgb16(input [7:0] r, input [7:0] g, input [7:0] b);
|
||||
reg [7:0] mx;
|
||||
begin
|
||||
mx = r > g ? (r > b ? r : b) : (g > b ? g : b);
|
||||
rgb16 = approx(r[7], g[7], b[7], mx >= 192, mx >= 64);
|
||||
end
|
||||
endfunction
|
||||
|
||||
function [7:0] next_tab(input [131:0] t, input [7:0] cc, input [7:0] last);
|
||||
integer k;
|
||||
begin
|
||||
next_tab = last;
|
||||
for (k = 131; k >= 0; k = k - 1)
|
||||
if (k > cc && k < last && t[k]) next_tab = k;
|
||||
end
|
||||
endfunction
|
||||
|
||||
function [7:0] prev_tab(input [131:0] t, input [7:0] cc);
|
||||
integer k;
|
||||
begin
|
||||
prev_tab = 8'd0;
|
||||
for (k = 0; k < 132; k = k + 1)
|
||||
if (k < cc && t[k]) prev_tab = k;
|
||||
end
|
||||
endfunction
|
||||
|
||||
// --------------------------------------------------- derived values
|
||||
wire [17:0] BLANK = {2'b00, 4'd0, def_fg, 8'h20};
|
||||
wire [7:0] p0 = par[0];
|
||||
wire [7:0] p1 = par[1];
|
||||
reg [7:0] n8; // count parameter (default 1), registered
|
||||
reg [7:0] nc; // ... clamped to the rest of the line
|
||||
wire [7:0] room = NCOLS - col; // cells from cursor to end of line
|
||||
wire [7:0] n8_c = p0 == 0 ? 8'd1 : p0;
|
||||
always @(posedge clk) begin
|
||||
n8 <= n8_c;
|
||||
nc <= n8_c > room ? room : n8_c;
|
||||
end
|
||||
|
||||
wire [3:0] fgb = bold ? (fg | 4'd8) : fg;
|
||||
wire [3:0] dfg = rev ? bg : fgb;
|
||||
wire [3:0] dbg = rev ? fgb : bg;
|
||||
wire [1:0] cs = gl ? g1 : g0;
|
||||
wire [7:0] glyph = (cs == 2'd1 && c >= 8'h5F && c <= 8'h7E) ? gfx(c) :
|
||||
(cs == 2'd2 && c == "#") ? 8'h9C : c;
|
||||
wire [17:0] new_cell = {blink, ul, dbg, conceal ? dbg : dfg, glyph};
|
||||
|
||||
wire smooth = sclm | smooth_sw;
|
||||
wire [4:0] sm_step = smooth_fast ? 5'd4 : 5'd2;
|
||||
|
||||
wire [5:0] up_lim = row >= stop ? stop : 6'd0;
|
||||
wire [5:0] dn_lim = row <= sbot ? sbot : LAST_ROW;
|
||||
wire [5:0] row_up = (row - up_lim > n8) ? row - n8 : up_lim;
|
||||
wire [5:0] row_dn = (dn_lim - row > n8) ? row + n8 : dn_lim;
|
||||
wire [7:0] col_rt = (LAST_COL - col > n8) ? col + n8 : LAST_COL;
|
||||
wire [7:0] col_lt = (col > n8) ? col - n8 : 8'd0;
|
||||
|
||||
wire [7:0] r_rel = p0 == 0 ? 8'd0 : p0 - 1'b1;
|
||||
wire [8:0] r_abs = (om ? stop : 6'd0) + r_rel;
|
||||
wire [5:0] r_max = om ? sbot : LAST_ROW;
|
||||
wire [5:0] row_p0 = r_abs > r_max ? r_max : r_abs[5:0];
|
||||
wire [7:0] col_p0 = p0 == 0 ? 8'd0 : p0 > NCOLS ? LAST_COL : p0 - 1'b1;
|
||||
wire [7:0] col_p1 = p1 == 0 ? 8'd0 : p1 > NCOLS ? LAST_COL : p1 - 1'b1;
|
||||
|
||||
wire [13:0] here = rc(row, col);
|
||||
wire [6:0] rep_row = (om ? row - stop : row) + 1'b1;
|
||||
wire [7:0] rep_col = col + 1'b1;
|
||||
reg [7:0] pv; // parameter being processed, registered
|
||||
reg pv_ok;
|
||||
|
||||
// ------------------------------------------------------------ tasks
|
||||
task op(input [13:0] src, input [13:0] dst, input [13:0] ccnt, input dir,
|
||||
input [13:0] fa, input [13:0] fcnt, input [17:0] fcell, input [3:0] rs);
|
||||
begin
|
||||
c_src <= src; c_dst <= dst; c_cnt <= ccnt; c_dir <= dir;
|
||||
f_addr <= fa; f_cnt <= fcnt; f_cell <= fcell;
|
||||
ret_st <= rs;
|
||||
st <= S_OP; // decided next clock, off the critical path
|
||||
end
|
||||
endtask
|
||||
|
||||
task fill(input [13:0] fa, input [13:0] fcnt);
|
||||
op(14'd0, 14'd0, 14'd0, 1'b0, fa, fcnt, BLANK, S_IDLE);
|
||||
endtask
|
||||
|
||||
// scroll rows t..b up (dir 0) or down (dir 1) by n lines
|
||||
// scroll rows t..b up (dir 0) or down (dir 1) by n lines; the copy and fill
|
||||
// ranges are worked out over the next two clocks (S_SCR1, S_SCR2)
|
||||
task scroll(input dir, input [5:0] t, input [5:0] b, input [7:0] n, input [3:0] rs);
|
||||
begin
|
||||
sc_dir <= dir;
|
||||
sc_t <= t;
|
||||
sc_b <= b;
|
||||
sc_n <= n;
|
||||
ret_st <= rs;
|
||||
st <= S_SCR1;
|
||||
end
|
||||
endtask
|
||||
|
||||
task line_feed(input [3:0] rs);
|
||||
begin
|
||||
if (row == sbot) begin
|
||||
if (smooth) begin
|
||||
sm_on <= 1'b1; sm_dir <= 1'b0; sm_off <= 5'd0; sm_ret <= rs;
|
||||
st <= S_SMOOTH;
|
||||
end else
|
||||
scroll(1'b0, stop, sbot, 8'd1, rs);
|
||||
end else begin
|
||||
if (row != LAST_ROW) row <= row + 1'b1;
|
||||
st <= rs;
|
||||
end
|
||||
end
|
||||
endtask
|
||||
|
||||
task reverse_index;
|
||||
begin
|
||||
if (row == stop) begin
|
||||
if (smooth) begin
|
||||
sm_on <= 1'b1; sm_dir <= 1'b1; sm_off <= 5'd0; sm_ret <= S_IDLE;
|
||||
st <= S_SMOOTH;
|
||||
end else
|
||||
scroll(1'b1, stop, sbot, 8'd1, S_IDLE);
|
||||
end else if (row != 0)
|
||||
row <= row - 1'b1;
|
||||
end
|
||||
endtask
|
||||
|
||||
task reset_attrs;
|
||||
begin
|
||||
{bold, ul, blink, rev, conceal} <= 5'b0;
|
||||
fg <= def_fg;
|
||||
bg <= 4'd0;
|
||||
end
|
||||
endtask
|
||||
|
||||
task soft_reset;
|
||||
begin
|
||||
reset_attrs;
|
||||
{pend, irm, om, cursor_app, keypad_app, gl} <= 6'b0;
|
||||
{g0, g1} <= 4'b0;
|
||||
awm <= 1'b1;
|
||||
cur_vis <= 1'b1;
|
||||
alt_scr <= 1'b0;
|
||||
stop <= 6'd0;
|
||||
sbot <= LAST_ROW;
|
||||
{sv_row, sv_col, sv_pend, sv_om, sv_gl, sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal} <= 0;
|
||||
{sv_g0, sv_g1, sv_bg} <= 0;
|
||||
sv_fg <= def_fg;
|
||||
end
|
||||
endtask
|
||||
|
||||
task full_reset;
|
||||
begin
|
||||
soft_reset;
|
||||
{row, col} <= 0;
|
||||
{sclm, lnm, wide, colm_clr} <= 4'b0;
|
||||
sbot <= 6'd31;
|
||||
dec_leds <= 4'd0;
|
||||
sm_on <= 1'b0;
|
||||
sm_off <= 5'd0;
|
||||
ps <= P_NORM;
|
||||
for (i = 0; i < 132; i = i + 1) tabs[i] <= i != 0 && i % 8 == 0;
|
||||
fill(14'd0, ALL);
|
||||
end
|
||||
endtask
|
||||
|
||||
task respond(input [95:0] s, input [3:0] n);
|
||||
begin
|
||||
rbuf <= s;
|
||||
rlen <= n;
|
||||
st <= S_RESP;
|
||||
end
|
||||
endtask
|
||||
|
||||
function [7:0] tens(input [7:0] v);
|
||||
tens = "0" + (v / 8'd10) % 8'd10;
|
||||
endfunction
|
||||
|
||||
function [7:0] ones(input [7:0] v);
|
||||
ones = "0" + v % 8'd10;
|
||||
endfunction
|
||||
|
||||
// ESC [ rr ; cc R, or ESC [ rr ; 1cc R past column 99
|
||||
task cursor_report;
|
||||
if (rep_col >= 100)
|
||||
respond({8'h1B, "[", tens(rep_row), ones(rep_row), ";", "1",
|
||||
tens(rep_col), ones(rep_col), "R", 24'd0}, 4'd9);
|
||||
else
|
||||
respond({8'h1B, "[", tens(rep_row), ones(rep_row), ";",
|
||||
tens(rep_col), ones(rep_col), "R", 32'd0}, 4'd8);
|
||||
endtask
|
||||
|
||||
// one SGR parameter
|
||||
task sgr(input [7:0] v);
|
||||
begin
|
||||
case (ext)
|
||||
2'd0:
|
||||
if (v == 0) reset_attrs;
|
||||
else if (v == 1) bold <= 1'b1;
|
||||
else if (v == 4 || v == 21) ul <= 1'b1;
|
||||
else if (v == 5 || v == 6) blink <= 1'b1;
|
||||
else if (v == 7) rev <= 1'b1;
|
||||
else if (v == 8) conceal <= 1'b1;
|
||||
else if (v == 22) bold <= 1'b0;
|
||||
else if (v == 24) ul <= 1'b0;
|
||||
else if (v == 25) blink <= 1'b0;
|
||||
else if (v == 27) rev <= 1'b0;
|
||||
else if (v == 28) conceal <= 1'b0;
|
||||
else if (v >= 30 && v <= 37) fg <= v - 8'd30;
|
||||
else if (v == 38 || v == 48) begin ext <= 2'd1; ext_bg <= v == 48; end
|
||||
else if (v == 39) fg <= def_fg;
|
||||
else if (v >= 40 && v <= 47) bg <= v - 8'd40;
|
||||
else if (v == 49) bg <= 4'd0;
|
||||
else if (v >= 90 && v <= 97) fg <= v - 8'd82;
|
||||
else if (v >= 100 && v <= 107) bg <= v - 8'd92;
|
||||
2'd1: // after 38/48: 5 = indexed, 2 = RGB
|
||||
if (v == 5) ext <= 2'd2;
|
||||
else if (v == 2) begin ext <= 2'd3; ext_n <= 2'd0; end
|
||||
else ext <= 2'd0;
|
||||
2'd2: begin
|
||||
if (ext_bg) bg <= map256(v); else fg <= map256(v);
|
||||
ext <= 2'd0;
|
||||
end
|
||||
2'd3: begin
|
||||
ext_n <= ext_n + 1'b1;
|
||||
if (ext_n == 0) ext_r <= v;
|
||||
else if (ext_n == 1) ext_g <= v;
|
||||
else begin
|
||||
if (ext_bg) bg <= rgb16(ext_r, ext_g, v); else fg <= rgb16(ext_r, ext_g, v);
|
||||
ext <= 2'd0;
|
||||
end
|
||||
end
|
||||
endcase
|
||||
end
|
||||
endtask
|
||||
|
||||
// one SM/RM parameter
|
||||
task set_mode(input [7:0] v, input on);
|
||||
begin
|
||||
if (priv) begin
|
||||
if (v == 1) cursor_app <= on;
|
||||
if (v == 3) begin // DECCOLM: 80x32 / 132x64, cleared
|
||||
wide <= on; colm_clr <= 1'b1;
|
||||
{row, col, pend, om, stop} <= 0;
|
||||
sbot <= on ? 6'd63 : 6'd31;
|
||||
end
|
||||
if (v == 4) sclm <= on;
|
||||
if (v == 6) begin om <= on; row <= on ? stop : 6'd0; col <= 8'd0; pend <= 1'b0; end
|
||||
if (v == 7) awm <= on;
|
||||
if (v == 25) cur_vis <= on;
|
||||
if (v == 47) alt_scr <= on;
|
||||
end else begin
|
||||
if (v == 4) irm <= on;
|
||||
if (v == 20) lnm <= on;
|
||||
end
|
||||
end
|
||||
endtask
|
||||
|
||||
task plist(input [1:0] kind);
|
||||
begin
|
||||
pk <= kind;
|
||||
pi <= 4'd0;
|
||||
ext <= 2'd0;
|
||||
pv_ok <= 1'b0;
|
||||
st <= S_PLOOP;
|
||||
end
|
||||
endtask
|
||||
|
||||
// ------------------------------------------------------------ main
|
||||
always @(posedge clk) begin
|
||||
in_rd <= 1'b0;
|
||||
we <= 1'b0;
|
||||
bell <= 1'b0;
|
||||
resp_wr <= 1'b0;
|
||||
if (rst) begin
|
||||
full_reset;
|
||||
cursor_app <= 1'b0;
|
||||
end else case (st)
|
||||
S_IDLE:
|
||||
if (clear_req || colm_clr) begin
|
||||
{row, col, pend, colm_clr} <= 0;
|
||||
fill(14'd0, ALL);
|
||||
end else if (!in_empty && !in_rd) begin
|
||||
c <= in_byte;
|
||||
in_rd <= 1'b1;
|
||||
st <= S_DISP;
|
||||
end
|
||||
|
||||
// ---- scroll: size the move, then set up the copy and fill
|
||||
S_SCR1: begin
|
||||
sc_h <= sc_b - sc_t + 1'b1;
|
||||
sc_k <= sc_n > sc_b - sc_t ? sc_b - sc_t + 1'b1 : sc_n[6:0];
|
||||
st <= S_SCR2;
|
||||
end
|
||||
S_SCR2:
|
||||
if (!sc_dir)
|
||||
op(rc(sc_t + sc_k, 0), rc(sc_t, 0), rows(sc_h - sc_k), 1'b0,
|
||||
rc(sc_b - sc_k + 1'b1, 0), rows(sc_k), BLANK, ret_st);
|
||||
else
|
||||
op(rc(sc_b - sc_k + 1'b1, 0) - 1'b1, rc(sc_b + 1'b1, 0) - 1'b1, rows(sc_h - sc_k), 1'b1,
|
||||
rc(sc_t, 0), rows(sc_k), BLANK, ret_st);
|
||||
|
||||
// ---- copy c_cnt cells, then fill f_cnt cells, then go to ret_st
|
||||
S_OP:
|
||||
st <= c_cnt != 0 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st;
|
||||
S_CRD: begin
|
||||
addr <= c_src;
|
||||
st <= S_CWAIT;
|
||||
end
|
||||
S_CWAIT:
|
||||
st <= S_CWR;
|
||||
S_CWR: begin
|
||||
addr <= c_dst;
|
||||
wdata <= rdata;
|
||||
we <= 1'b1;
|
||||
c_src <= c_dir ? c_src - 1'b1 : c_src + 1'b1;
|
||||
c_dst <= c_dir ? c_dst - 1'b1 : c_dst + 1'b1;
|
||||
c_cnt <= c_cnt - 1'b1;
|
||||
st <= c_cnt != 1 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st;
|
||||
end
|
||||
S_FILL: begin
|
||||
addr <= f_addr;
|
||||
wdata <= f_cell;
|
||||
we <= 1'b1;
|
||||
f_addr <= f_addr + 1'b1;
|
||||
f_cnt <= f_cnt - 1'b1;
|
||||
if (f_cnt == 1) st <= ret_st;
|
||||
end
|
||||
|
||||
S_SMOOTH:
|
||||
// the region slides during active video; the copy runs in blanking
|
||||
if (frame_tick) begin
|
||||
if (sm_off + sm_step >= 16)
|
||||
scroll(sm_dir, stop, sbot, 8'd1, S_SMEND);
|
||||
else
|
||||
sm_off <= sm_off + sm_step;
|
||||
end
|
||||
S_SMEND: begin
|
||||
sm_on <= 1'b0;
|
||||
sm_off <= 5'd0;
|
||||
st <= sm_ret;
|
||||
end
|
||||
|
||||
S_RESP:
|
||||
if (!resp_full) begin
|
||||
resp_byte <= rbuf[95:88];
|
||||
resp_wr <= 1'b1;
|
||||
rbuf <= rbuf << 8;
|
||||
rlen <= rlen - 1'b1;
|
||||
if (rlen == 1) st <= S_IDLE;
|
||||
end
|
||||
|
||||
S_TAB: begin
|
||||
col <= tdir ? prev_tab(tabs, col) : next_tab(tabs, col, LAST_COL);
|
||||
rep <= rep - 1'b1;
|
||||
if (rep == 1) st <= S_IDLE;
|
||||
end
|
||||
|
||||
S_PLOOP: if (!pv_ok) begin
|
||||
// fetch the parameter one clock ahead, then act on it
|
||||
pv <= par[pi];
|
||||
pv_ok <= 1'b1;
|
||||
end else begin
|
||||
pv_ok <= 1'b0;
|
||||
case (pk)
|
||||
K_SGR: sgr(pv);
|
||||
K_SET: set_mode(pv, 1'b1);
|
||||
K_RST: set_mode(pv, 1'b0);
|
||||
K_LL:
|
||||
if (pv == 0) dec_leds <= 4'd0;
|
||||
else if (pv >= 1 && pv <= 4) dec_leds[pv - 1] <= 1'b1;
|
||||
else if (pv >= 21 && pv <= 24) dec_leds[pv - 21] <= 1'b0;
|
||||
endcase
|
||||
pi <= pi + 1'b1;
|
||||
if (pi == pidx) st <= S_IDLE;
|
||||
end
|
||||
|
||||
S_PUT: begin // write the character at the cursor
|
||||
we <= 1'b1;
|
||||
addr <= here;
|
||||
wdata <= new_cell;
|
||||
if (col == LAST_COL) pend <= awm;
|
||||
else col <= col + 1'b1;
|
||||
st <= S_IDLE;
|
||||
end
|
||||
|
||||
S_DISP: begin
|
||||
st <= S_IDLE;
|
||||
if (ps == P_STR) begin
|
||||
// inside OSC/DCS/APC/PM/SOS: discard until BEL or ESC \ (ST)
|
||||
if (c == 8'h07 || c == 8'h18 || c == 8'h1A) ps <= P_NORM;
|
||||
else if (c == 8'h1B) ps <= P_ESC;
|
||||
end else if (c == 8'h1B) begin
|
||||
ps <= P_ESC;
|
||||
end else if (c == 8'h18 || c == 8'h1A) begin
|
||||
ps <= P_NORM; // CAN / SUB cancel a sequence
|
||||
end else if (c < 8'h20) begin
|
||||
// C0 controls act immediately, even inside an escape sequence
|
||||
case (c)
|
||||
8'h07: bell <= 1'b1;
|
||||
8'h08: begin pend <= 1'b0; if (col != 0) col <= col - 1'b1; end
|
||||
8'h09: begin pend <= 1'b0; col <= next_tab(tabs, col, LAST_COL); end
|
||||
8'h0A, 8'h0B, 8'h0C: begin
|
||||
pend <= 1'b0;
|
||||
if (lf_cr || lnm) col <= 8'd0;
|
||||
line_feed(S_IDLE);
|
||||
end
|
||||
8'h0D: begin pend <= 1'b0; col <= 8'd0; end
|
||||
8'h0E: gl <= 1'b1;
|
||||
8'h0F: gl <= 1'b0;
|
||||
default: ;
|
||||
endcase
|
||||
end else if (c == 8'h7F) begin
|
||||
// DEL is ignored
|
||||
end else case (ps)
|
||||
P_NORM:
|
||||
if (pend && awm) begin
|
||||
pend <= 1'b0;
|
||||
col <= 8'd0;
|
||||
line_feed(S_DISP); // wrap, then print this byte
|
||||
end else if (irm && col != LAST_COL)
|
||||
// insert mode: shift the rest of the line right first
|
||||
op(rc(row, LAST_COL - 1'b1), rc(row, LAST_COL), {6'd0, LAST_COL - col}, 1'b1,
|
||||
14'd0, 14'd0, BLANK, S_PUT);
|
||||
else
|
||||
st <= S_PUT;
|
||||
|
||||
P_ESC: begin
|
||||
ps <= P_NORM;
|
||||
if (c >= 8'h20 && c <= 8'h2F) begin
|
||||
inter <= c;
|
||||
ps <= P_ESCI;
|
||||
end else case (c)
|
||||
"]", "P", "_", "^", "X":
|
||||
ps <= P_STR; // string sequences are ignored
|
||||
"[": begin
|
||||
ps <= P_CSI;
|
||||
for (i = 0; i < 16; i = i + 1) par[i] <= 8'd0;
|
||||
{pidx, priv, gtm, ign} <= 0;
|
||||
cint <= 8'd0;
|
||||
end
|
||||
"D": begin pend <= 1'b0; line_feed(S_IDLE); end
|
||||
"E": begin pend <= 1'b0; col <= 8'd0; line_feed(S_IDLE); end
|
||||
"M": begin pend <= 1'b0; reverse_index; end
|
||||
"H": tabs[col] <= 1'b1;
|
||||
"7": begin
|
||||
{sv_row, sv_col, sv_pend, sv_om, sv_gl} <= {row, col, pend, om, gl};
|
||||
{sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal} <= {bold, ul, blink, rev, conceal};
|
||||
{sv_fg, sv_bg, sv_g0, sv_g1} <= {fg, bg, g0, g1};
|
||||
end
|
||||
"8": begin
|
||||
{row, col, pend, om, gl} <= {sv_row, sv_col, sv_pend, sv_om, sv_gl};
|
||||
{bold, ul, blink, rev, conceal} <= {sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal};
|
||||
{fg, bg, g0, g1} <= {sv_fg, sv_bg, sv_g0, sv_g1};
|
||||
end
|
||||
"=": keypad_app <= 1'b1;
|
||||
">": keypad_app <= 1'b0;
|
||||
"Z": respond({8'h1B, "[?62;22c", 24'd0}, 4'd9);
|
||||
"c": begin full_reset; cursor_app <= 1'b0; end
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
|
||||
P_ESCI: begin
|
||||
ps <= P_NORM;
|
||||
if (c >= 8'h20 && c <= 8'h2F)
|
||||
ps <= P_ESCI; // further intermediates
|
||||
else case (inter)
|
||||
"(": g0 <= charset(c);
|
||||
")": g1 <= charset(c);
|
||||
"#": if (c == "8") begin // DECALN: fill the screen with E
|
||||
{row, col, pend, stop} <= 0;
|
||||
sbot <= LAST_ROW;
|
||||
op(14'd0, 14'd0, 14'd0, 1'b0, 14'd0, CELLS, {2'b00, 4'd0, def_fg, "E"}, S_IDLE);
|
||||
end
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
|
||||
P_CSI:
|
||||
if (c >= "0" && c <= "9")
|
||||
par[pidx] <= acc(par[pidx], c);
|
||||
else if (c == ";" || c == ":") begin
|
||||
if (pidx != 4'd15) pidx <= pidx + 1'b1;
|
||||
end else if (c == "?")
|
||||
priv <= 1'b1;
|
||||
else if (c == ">")
|
||||
gtm <= 1'b1;
|
||||
else if (c == "<" || c == "=")
|
||||
ign <= 1'b1;
|
||||
else if (c >= 8'h20 && c <= 8'h2F)
|
||||
cint <= c;
|
||||
else if (c >= 8'h40 && c <= 8'h7E) begin
|
||||
ps <= P_NORM;
|
||||
st <= S_EXEC; // run it next clock, off the parser's path
|
||||
end
|
||||
default: ps <= P_NORM;
|
||||
endcase
|
||||
end
|
||||
|
||||
// ---- CSI final byte (in c): parameters and cursor are unchanged
|
||||
S_EXEC: begin
|
||||
st <= S_IDLE;
|
||||
if (ign) begin
|
||||
end else if (cint == "!") begin
|
||||
if (c == "p") soft_reset;
|
||||
end else if (cint != 0) begin
|
||||
end else if (gtm) begin
|
||||
if (c == "c") respond({8'h1B, "[>1;10;0c", 16'd0}, 4'd10);
|
||||
end else if (priv) begin
|
||||
case (c)
|
||||
"h": plist(K_SET);
|
||||
"l": plist(K_RST);
|
||||
"J": begin
|
||||
pend <= 1'b0;
|
||||
if (p0 == 0) fill(here, CELLS - here);
|
||||
else if (p0 == 1) fill(14'd0, here + 1'b1);
|
||||
else if (p0 == 2) fill(14'd0, CELLS);
|
||||
end
|
||||
"K": begin
|
||||
pend <= 1'b0;
|
||||
if (p0 == 0) fill(here, room);
|
||||
else if (p0 == 1) fill(rc(row, 0), col + 1'b1);
|
||||
else if (p0 == 2) fill(rc(row, 0), STRIDE);
|
||||
end
|
||||
"n": if (p0 == 6) cursor_report;
|
||||
default: ;
|
||||
endcase
|
||||
end else case (c)
|
||||
"A": begin pend <= 1'b0; row <= row_up; end
|
||||
"B", "e": begin pend <= 1'b0; row <= row_dn; end
|
||||
"C", "a": begin pend <= 1'b0; col <= col_rt; end
|
||||
"D": begin pend <= 1'b0; col <= col_lt; end
|
||||
"E": begin pend <= 1'b0; row <= row_dn; col <= 8'd0; end
|
||||
"F": begin pend <= 1'b0; row <= row_up; col <= 8'd0; end
|
||||
"G", "`": begin pend <= 1'b0; col <= col_p0; end
|
||||
"d": begin pend <= 1'b0; row <= row_p0; end
|
||||
"H", "f": begin pend <= 1'b0; row <= row_p0; col <= col_p1; end
|
||||
"I": begin pend <= 1'b0; rep <= n8; tdir <= 1'b0; st <= S_TAB; end
|
||||
"Z": begin pend <= 1'b0; rep <= n8; tdir <= 1'b1; st <= S_TAB; end
|
||||
"J": begin
|
||||
pend <= 1'b0;
|
||||
if (p0 == 0) fill(here, CELLS - here);
|
||||
else if (p0 == 1) fill(14'd0, here + 1'b1);
|
||||
else if (p0 == 2 || p0 == 3) fill(14'd0, CELLS);
|
||||
end
|
||||
"K": begin
|
||||
pend <= 1'b0;
|
||||
if (p0 == 0) fill(here, room);
|
||||
else if (p0 == 1) fill(rc(row, 0), col + 1'b1);
|
||||
else if (p0 == 2) fill(rc(row, 0), STRIDE);
|
||||
end
|
||||
"L": if (row >= stop && row <= sbot) begin
|
||||
pend <= 1'b0; col <= 8'd0;
|
||||
scroll(1'b1, row, sbot, n8, S_IDLE);
|
||||
end
|
||||
"M": if (row >= stop && row <= sbot) begin
|
||||
pend <= 1'b0; col <= 8'd0;
|
||||
scroll(1'b0, row, sbot, n8, S_IDLE);
|
||||
end
|
||||
"@": begin
|
||||
pend <= 1'b0;
|
||||
op(rc(row, LAST_COL - nc), rc(row, LAST_COL), {6'd0, room - nc}, 1'b1,
|
||||
here, {6'd0, nc}, BLANK, S_IDLE);
|
||||
end
|
||||
"P": begin
|
||||
pend <= 1'b0;
|
||||
op(rc(row, col + nc), here, {6'd0, room - nc}, 1'b0,
|
||||
rc(row, NCOLS - nc), {6'd0, nc}, BLANK, S_IDLE);
|
||||
end
|
||||
"X": begin pend <= 1'b0; fill(here, {6'd0, nc}); end
|
||||
"S": scroll(1'b0, stop, sbot, n8, S_IDLE);
|
||||
"T": scroll(1'b1, stop, sbot, n8, S_IDLE);
|
||||
"g": if (p0 == 0) tabs[col] <= 1'b0; else if (p0 == 3) tabs <= 132'd0;
|
||||
"h": plist(K_SET);
|
||||
"l": plist(K_RST);
|
||||
"m": plist(K_SGR);
|
||||
"q": plist(K_LL);
|
||||
"r": begin
|
||||
if ((p0 == 0 ? 8'd1 : p0) < (p1 == 0 || p1 > NROWS ? {1'b0, NROWS} : p1)) begin
|
||||
stop <= p0 == 0 ? 6'd0 : p0 - 1'b1;
|
||||
sbot <= p1 == 0 || p1 > NROWS ? LAST_ROW : p1 - 1'b1;
|
||||
row <= om ? (p0 == 0 ? 6'd0 : p0 - 1'b1) : 6'd0;
|
||||
col <= 8'd0;
|
||||
pend <= 1'b0;
|
||||
end
|
||||
end
|
||||
"s": {sv_row, sv_col, sv_pend} <= {row, col, pend};
|
||||
"u": {row, col, pend} <= {sv_row, sv_col, sv_pend};
|
||||
"n": if (p0 == 5) respond({8'h1B, "[0n", 64'd0}, 4'd4);
|
||||
else if (p0 == 6) cursor_report;
|
||||
"c": if (p0 == 0) respond({8'h1B, "[?62;22c", 24'd0}, 4'd9);
|
||||
default: ;
|
||||
endcase
|
||||
end
|
||||
default: st <= S_IDLE;
|
||||
endcase
|
||||
end
|
||||
endmodule
|
||||
@@ -0,0 +1,245 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// 1280x1024 @ 60 Hz VGA text display (108 MHz pixel clock), two layouts:
|
||||
// 80 columns: 80x32 cells of 16x32 pixels (the 8x16 font doubled)
|
||||
// 132 columns: 132x64 cells of 8x16 pixels, centred (112-pixel side borders)
|
||||
// 16 colours, underline, blink, block cursor and smooth-scroll offset for the
|
||||
// scroll region. The character buffer has a 132-cell stride in both layouts.
|
||||
// Hardware sprites (sprites.v) sit between the graphics and the text.
|
||||
// A 320x240 graphics layer (each pixel 4x4, the top 960 lines, so 80-column
|
||||
// text row r still starts at graphics line 8(r-1)) shows through wherever the
|
||||
// text is background; colour 0 is transparent. It scrolls: the picture starts
|
||||
// at (scroll_x, scroll_y) of the buffer and wraps round at 320 and 240. With
|
||||
// g_border the graphics are hidden in a border (4 columns each side, 2 rows
|
||||
// top and bottom), where a scrolling program can draw what comes next unseen.
|
||||
// One pixel per clock, pipelined:
|
||||
// clock 0 position -> row, column, glyph line (registered)
|
||||
// clock 1 character RAM and graphics reads
|
||||
// clock 2 font ROM read
|
||||
// clock 3 colour out
|
||||
module text_display #(
|
||||
parameter FONT_FILE = "font.mem"
|
||||
) (
|
||||
input clk, // pixel clock, 108 MHz
|
||||
input rst,
|
||||
input wide, // 132-column layout
|
||||
input [5:0] cur_row,
|
||||
input [7:0] cur_col,
|
||||
input cur_vis,
|
||||
input [5:0] stop, // scroll region (rows, inclusive)
|
||||
input [5:0] sbot,
|
||||
input sm_on, // smooth scroll in progress
|
||||
input sm_dir, // 0 = up, 1 = down
|
||||
input [4:0] sm_off, // font lines the region has moved
|
||||
output [13:0] ram_addr, // row * 132 + column
|
||||
input [17:0] ram_data, // {blink, underline, bg, fg, code}
|
||||
output [16:0] gfx_addr, // graphics pixel, read a clock later
|
||||
input [3:0] gfx_pix,
|
||||
input [8:0] scroll_x, // graphics pixel shown at the top left
|
||||
input [7:0] scroll_y,
|
||||
input g_border, // hide 4 graphics columns each side, 2 rows top and bottom
|
||||
// sprites: the line to prepare next, and the pixel on screen
|
||||
output reg sp_go,
|
||||
output reg [7:0] sp_line,
|
||||
output sp_buf,
|
||||
output [8:0] sp_x,
|
||||
output sp_on,
|
||||
input [3:0] sp_pix, // read a clock after sp_x, like gfx_pix
|
||||
output reg [3:0] red,
|
||||
output reg [3:0] green,
|
||||
output reg [3:0] blue,
|
||||
output reg hsync,
|
||||
output reg vsync,
|
||||
output reg frame_tick // one clock at the start of vertical blanking
|
||||
);
|
||||
localparam H_VIS = 1280, H_FP = 48, H_SYNC = 112, H_TOTAL = 1688;
|
||||
localparam V_VIS = 1024, V_FP = 1, V_SYNC = 3, V_TOTAL = 1066;
|
||||
localparam WIDE_X0 = 112; // 132 x 8 = 1056 pixels, centred
|
||||
|
||||
reg [10:0] h, v;
|
||||
reg [5:0] frame = 6'd0;
|
||||
|
||||
always @(posedge clk) begin
|
||||
frame_tick <= h == 0 && v == V_VIS;
|
||||
// each graphics line is 4 scan lines; ask for the next one as it starts
|
||||
sp_go <= h == 0 && v[1:0] == 2'd0 && (v < 11'd956 || v == 11'd1064);
|
||||
sp_line <= v == 11'd1064 ? 8'd0 : v[9:2] + 1'b1;
|
||||
if (rst) begin
|
||||
h <= 0;
|
||||
v <= 0;
|
||||
end else if (h == H_TOTAL - 1) begin
|
||||
h <= 0;
|
||||
if (v == V_TOTAL - 1) begin
|
||||
v <= 0;
|
||||
frame <= frame + 1'b1;
|
||||
end else
|
||||
v <= v + 1'b1;
|
||||
end else
|
||||
h <= h + 1'b1;
|
||||
end
|
||||
|
||||
// graphics pixel for this h, v with the scroll added: counters set from
|
||||
// the scroll registers at the end of each line and each frame
|
||||
reg [8:0] g_col = 9'd0;
|
||||
reg [7:0] g_row = 8'd0;
|
||||
reg [1:0] g_sub = 2'd0;
|
||||
always @(posedge clk)
|
||||
if (h == H_TOTAL - 1) begin
|
||||
g_col <= scroll_x;
|
||||
g_sub <= 2'd0;
|
||||
if (v == V_TOTAL - 1) g_row <= scroll_y;
|
||||
else if (v[1:0] == 2'd3) g_row <= g_row == 8'd239 ? 8'd0 : g_row + 1'b1;
|
||||
end else begin
|
||||
g_sub <= g_sub + 1'b1;
|
||||
if (g_sub == 2'd3) g_col <= g_col >= 9'd319 ? 9'd0 : g_col + 1'b1;
|
||||
end
|
||||
|
||||
// ---- clock 0: where this pixel is in the text, in font units
|
||||
// yl counts font lines (a 16-line font: 32 pixels a row in 80 columns)
|
||||
wire [9:0] yl = wide ? v[9:0] : {1'b0, v[9:1]};
|
||||
wire [10:0] xw = h - WIDE_X0;
|
||||
wire [10:0] xl = wide ? xw : {1'b0, h[10:1]}; // font pixels across
|
||||
wire text_on = wide ? h >= WIDE_X0 && h < WIDE_X0 + 1056 : 1'b1;
|
||||
wire in_region = sm_on && v < V_VIS && yl[9:4] >= stop && yl[9:4] <= sbot;
|
||||
wire [9:0] ys = !in_region ? yl : sm_dir ? yl - sm_off : yl + sm_off;
|
||||
wire gap = in_region && (sm_dir ? yl < {stop, 4'b0} + sm_off : ys[9:4] > sbot);
|
||||
wire [7:0] tcol = xl[10:3];
|
||||
wire [5:0] trow = ys[9:4];
|
||||
|
||||
reg [13:0] a_base; // row * 132
|
||||
reg [7:0] a_col;
|
||||
reg [3:0] a_line;
|
||||
reg [2:0] a_bit;
|
||||
reg a_blank, a_text, a_vis, a_cur, a_hs, a_vs, a_gon, a_ghide;
|
||||
wire [8:0] gcol_s = h[10:2]; // graphics pixel on the screen, unscrolled
|
||||
wire [7:0] grow_s = v[9:2];
|
||||
reg [16:0] a_gfx;
|
||||
reg [8:0] a_spx;
|
||||
reg a_spbuf;
|
||||
always @(posedge clk) begin
|
||||
a_base <= {trow, 7'd0} + {6'd0, trow, 2'd0};
|
||||
a_col <= tcol;
|
||||
a_line <= ys[3:0];
|
||||
a_bit <= xl[2:0];
|
||||
a_blank <= gap;
|
||||
a_text <= text_on;
|
||||
a_vis <= h < H_VIS && v < V_VIS;
|
||||
a_cur <= cur_vis && frame[5] && tcol == cur_col && ys[9:4] == cur_row && !gap;
|
||||
a_hs <= h >= H_VIS + H_FP && h < H_VIS + H_FP + H_SYNC; // positive sync
|
||||
a_vs <= v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC;
|
||||
a_gon <= v < 960;
|
||||
a_ghide <= g_border && (gcol_s < 9'd4 || gcol_s >= 9'd316 || grow_s < 8'd2 || grow_s >= 8'd238);
|
||||
a_gfx <= {1'b0, g_row, 8'd0} + {3'd0, g_row, 6'd0} + {8'd0, g_col};
|
||||
a_spx <= h[10:2];
|
||||
a_spbuf <= v[2];
|
||||
end
|
||||
|
||||
// ---- clock 1: character RAM and graphics reads
|
||||
assign ram_addr = a_base + {6'd0, a_col};
|
||||
assign gfx_addr = a_gfx;
|
||||
assign sp_x = a_spx;
|
||||
assign sp_buf = a_spbuf;
|
||||
assign sp_on = a_gon;
|
||||
reg [3:0] b_line;
|
||||
reg [2:0] b_bit;
|
||||
reg b_blank, b_text, b_vis, b_cur, b_hs, b_vs, b_gon, b_ghide;
|
||||
always @(posedge clk)
|
||||
{b_line, b_bit, b_blank, b_text, b_vis, b_cur, b_hs, b_vs, b_gon, b_ghide} <=
|
||||
{a_line, a_bit, a_blank, a_text, a_vis, a_cur, a_hs, a_vs, a_gon, a_ghide};
|
||||
|
||||
// ---- clock 2: font ROM
|
||||
reg [7:0] font [0:4095];
|
||||
initial $readmemh(FONT_FILE, font);
|
||||
reg [7:0] glyph;
|
||||
reg [17:0] c_cell;
|
||||
reg [3:0] c_gfx, c_spr, c_line;
|
||||
reg [2:0] c_bit;
|
||||
reg c_blank, c_text, c_vis, c_cur, c_hs, c_vs;
|
||||
always @(posedge clk) begin
|
||||
glyph <= font[{ram_data[7:0], b_line}];
|
||||
c_cell <= ram_data;
|
||||
c_gfx <= b_gon && !b_ghide ? gfx_pix : 4'd0;
|
||||
c_spr <= sp_pix;
|
||||
{c_line, c_bit, c_blank, c_text, c_vis, c_cur, c_hs, c_vs} <=
|
||||
{b_line, b_bit, b_blank, b_text, b_vis, b_cur, b_hs, b_vs};
|
||||
end
|
||||
|
||||
function [11:0] palette(input [3:0] n);
|
||||
case (n)
|
||||
4'd0: palette = 12'h000; 4'd1: palette = 12'hA00;
|
||||
4'd2: palette = 12'h0A0; 4'd3: palette = 12'hA50;
|
||||
4'd4: palette = 12'h00A; 4'd5: palette = 12'hA0A;
|
||||
4'd6: palette = 12'h0AA; 4'd7: palette = 12'hAAA;
|
||||
4'd8: palette = 12'h555; 4'd9: palette = 12'hF55;
|
||||
4'd10: palette = 12'h5F5; 4'd11: palette = 12'hFF5;
|
||||
4'd12: palette = 12'h55F; 4'd13: palette = 12'hF5F;
|
||||
4'd14: palette = 12'h5FF; 4'd15: palette = 12'hFFF;
|
||||
endcase
|
||||
endfunction
|
||||
|
||||
// ---- clock 3: colour
|
||||
wire ink = (glyph[~c_bit] | (c_cell[16] && c_line == 4'd15)) && !(c_cell[17] && frame[5]);
|
||||
wire fg = c_text && !c_blank && (ink ^ c_cur);
|
||||
wire [3:0] idx = fg ? c_cell[11:8] : c_spr != 4'd0 ? c_spr : c_gfx != 4'd0 ? c_gfx :
|
||||
c_blank || !c_text ? 4'd0 : c_cell[15:12];
|
||||
always @(posedge clk) begin
|
||||
{red, green, blue} <= c_vis ? palette(idx) : 12'h000;
|
||||
hsync <= c_hs;
|
||||
vsync <= c_vs;
|
||||
end
|
||||
endmodule
|
||||
|
||||
// Two character buffers of 132x64 cells (the normal and the alternate screen,
|
||||
// picked by a_alt/b_alt): read/write port for the terminal engine, read-only
|
||||
// port for video on the pixel clock.
|
||||
module char_ram (
|
||||
input clk,
|
||||
input a_alt,
|
||||
input [13:0] a_addr,
|
||||
input a_we,
|
||||
input [17:0] a_wdata,
|
||||
output [17:0] a_rdata,
|
||||
input vclk,
|
||||
input b_alt,
|
||||
input [13:0] b_addr,
|
||||
output [17:0] b_rdata
|
||||
);
|
||||
// Two pages of 132 x 64 cells: 16896, just past 16K. Kept as a 16K array
|
||||
// and a 512-entry one, so it takes about 9.5 block RAMs rather than 18
|
||||
// (one array of 16896 would be built as 32K deep).
|
||||
localparam PAGE = 132 * 64;
|
||||
(* ram_style = "block" *) reg [17:0] lo [0:16383];
|
||||
(* ram_style = "block" *) reg [17:0] hi [0:2 * PAGE - 16384 - 1];
|
||||
integer i;
|
||||
initial begin
|
||||
for (i = 0; i < 16384; i = i + 1) lo[i] = {10'h00A, 8'h20};
|
||||
for (i = 0; i < 2 * PAGE - 16384; i = i + 1) hi[i] = {10'h00A, 8'h20};
|
||||
end
|
||||
|
||||
wire [14:0] aa = {1'b0, a_addr} + (a_alt ? PAGE : 0);
|
||||
wire [14:0] ba = {1'b0, b_addr} + (b_alt ? PAGE : 0);
|
||||
reg [17:0] a_lo, a_hi, b_lo, b_hi;
|
||||
reg a_sel, b_sel;
|
||||
always @(posedge clk) begin
|
||||
if (a_we && !aa[14]) lo[aa[13:0]] <= a_wdata;
|
||||
a_lo <= lo[aa[13:0]];
|
||||
end
|
||||
always @(posedge clk) begin
|
||||
if (a_we && aa[14]) hi[aa[8:0]] <= a_wdata;
|
||||
a_hi <= hi[aa[8:0]];
|
||||
a_sel <= aa[14];
|
||||
end
|
||||
assign a_rdata = a_sel ? a_hi : a_lo;
|
||||
|
||||
always @(posedge vclk) begin
|
||||
b_lo <= lo[ba[13:0]];
|
||||
b_hi <= hi[ba[8:0]];
|
||||
b_sel <= ba[14];
|
||||
end
|
||||
assign b_rdata = b_sel ? b_hi : b_lo;
|
||||
|
||||
// whole-array view for testbenches
|
||||
function [17:0] cell_at(input [14:0] n);
|
||||
cell_at = n[14] ? hi[n[8:0]] : lo[n[13:0]];
|
||||
endfunction
|
||||
endmodule
|
||||
+106
@@ -0,0 +1,106 @@
|
||||
`timescale 1ns / 1ps
|
||||
|
||||
// 8N1 UART receiver and transmitter. div = clock frequency / baud rate.
|
||||
|
||||
module uart_rx (
|
||||
input clk,
|
||||
input rst,
|
||||
input [13:0] div,
|
||||
input rx,
|
||||
output reg [7:0] data,
|
||||
output reg valid
|
||||
);
|
||||
localparam IDLE = 3'd0, START = 3'd1, DATA = 3'd2, STOP = 3'd3, BREAK = 3'd4;
|
||||
|
||||
reg [2:0] sync = 3'b111;
|
||||
reg [2:0] st;
|
||||
reg [13:0] cnt;
|
||||
reg [2:0] bitn;
|
||||
reg [7:0] sh;
|
||||
wire rxs = sync[2];
|
||||
|
||||
always @(posedge clk) sync <= {sync[1:0], rx};
|
||||
|
||||
always @(posedge clk) begin
|
||||
valid <= 1'b0;
|
||||
if (rst) begin
|
||||
st <= IDLE;
|
||||
end else case (st)
|
||||
IDLE:
|
||||
if (!rxs) begin
|
||||
st <= START;
|
||||
cnt <= {1'b0, div[13:1]}; // sample mid-bit
|
||||
end
|
||||
START:
|
||||
if (cnt == 0) begin
|
||||
if (!rxs) begin
|
||||
st <= DATA;
|
||||
cnt <= div - 1'b1;
|
||||
bitn <= 3'd0;
|
||||
end else
|
||||
st <= IDLE; // glitch, not a start bit
|
||||
end else
|
||||
cnt <= cnt - 1'b1;
|
||||
DATA:
|
||||
if (cnt == 0) begin
|
||||
sh <= {rxs, sh[7:1]};
|
||||
cnt <= div - 1'b1;
|
||||
bitn <= bitn + 1'b1;
|
||||
if (bitn == 3'd7) st <= STOP;
|
||||
end else
|
||||
cnt <= cnt - 1'b1;
|
||||
STOP:
|
||||
if (cnt == 0) begin
|
||||
if (rxs) begin
|
||||
data <= sh;
|
||||
valid <= 1'b1;
|
||||
st <= IDLE;
|
||||
end else
|
||||
st <= BREAK; // bad stop bit: drop it
|
||||
end else
|
||||
cnt <= cnt - 1'b1;
|
||||
BREAK:
|
||||
if (rxs) st <= IDLE; // wait for the line to go idle
|
||||
default:
|
||||
st <= IDLE;
|
||||
endcase
|
||||
end
|
||||
endmodule
|
||||
|
||||
module uart_tx (
|
||||
input clk,
|
||||
input rst,
|
||||
input [13:0] div,
|
||||
input [7:0] data,
|
||||
input start,
|
||||
output reg tx = 1'b1,
|
||||
output reg busy
|
||||
);
|
||||
reg [9:0] sh;
|
||||
reg [3:0] n;
|
||||
reg [13:0] cnt;
|
||||
|
||||
always @(posedge clk) begin
|
||||
if (rst) begin
|
||||
tx <= 1'b1;
|
||||
busy <= 1'b0;
|
||||
end else if (!busy) begin
|
||||
if (start) begin
|
||||
sh <= {1'b1, data, 1'b0};
|
||||
n <= 4'd0;
|
||||
cnt <= 14'd0;
|
||||
busy <= 1'b1;
|
||||
end
|
||||
end else if (cnt == 0) begin
|
||||
if (n == 4'd10)
|
||||
busy <= 1'b0;
|
||||
else begin
|
||||
tx <= sh[0];
|
||||
sh <= {1'b1, sh[9:1]};
|
||||
n <= n + 1'b1;
|
||||
cnt <= div - 1'b1;
|
||||
end
|
||||
end else
|
||||
cnt <= cnt - 1'b1;
|
||||
end
|
||||
endmodule
|
||||
Reference in new issue
Block a user