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,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
|
||||
Reference in new issue
Block a user