Add a 640x480 two-colour graphics mode; start in white

The graphics memory doubles as 640x480 at a bit a pixel (port 38), four
pixels to an entry, plotted by read-modify-write, in two colours from
port 39 (0 transparent). BASIC gets SCREEN 0 / SCREEN 1 [,set[,clear]];
a plain fill (as every game does) returns to 320x240 and BASIC follows.
The text now starts in white (SW5:4 = 00; 10 is green). The README is
rewritten to stand on its own.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-12-01 06:06:30 -08:00
1 parent 71fd403294
commit bda9bb67b6
13 files changed
+2985 -2680

No files matched your search

+8 -5
View File
@@ -8,7 +8,7 @@
// 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
// SW5:4 default text colour: 00 white, 01 yellow, 10 green, 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)
@@ -94,9 +94,9 @@ module basys3_terminal #(
reg [3:0] def_fg;
always @(posedge clk)
case (sw_r[5:4])
2'b00: def_fg <= 4'd10; // bright green
2'b00: def_fg <= 4'd15; // white
2'b01: def_fg <= 4'd11; // yellow
2'b10: def_fg <= 4'd7; // white
2'b10: def_fg <= 4'd10; // bright green
2'b11: def_fg <= 4'd14; // bright cyan
endcase
@@ -332,7 +332,8 @@ module basys3_terminal #(
wire sp_go, sp_buf, sp_on, pix_frame;
wire [8:0] scroll_x;
wire [7:0] scroll_y;
wire g_border;
wire g_border, g_hires;
wire [7:0] g_cols;
wire [7:0] sp_line;
wire [8:0] sp_x;
wire [3:0] sp_pix;
@@ -352,6 +353,7 @@ module basys3_terminal #(
.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),
.g_hires(g_hires), .g_cols(g_cols),
.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]),
@@ -473,8 +475,9 @@ module basys3_terminal #(
.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),
.ram_addr(v_addr), .ram_data(v_data), .gfx_addr(gfx_addr), .gfx_pix(gfx_pix),
.scroll_x(scroll_x), .scroll_y(scroll_y), .g_border(g_border),
.g_hires(g_hires), .g_cols(g_cols), .g_show(!alt_scr),
.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),
+49 -17
View File
@@ -24,15 +24,21 @@
// 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
// 30 out graphics X bits 7:0 31 out X bits 9:8, Y bit 8 (bit 2)
// 32 out Y bits 7:0
// 33 out plot colour at (X,Y), then X+1 in colour at (X,Y)
// (in 640x480 mode a pixel is 0 or 1: any non-zero colour sets it)
// 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)
// (also turns the sprites off, the scroll back to 0 and the
// 320x240 mode back on, 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)
// 38 i/o bit 0 = 640x480 in two colours (the same memory, a bit a pixel;
// no scrolling or border) instead of 320x240 in 16
// 39 out 640x480 colours: bits 3:0 for a set pixel, 7:4 for a clear one
// (0 = transparent, the text's background showing through)
// 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,
@@ -89,6 +95,8 @@ module rv32_system #(
output reg [8:0] scroll_x,
output reg [7:0] scroll_y,
output reg g_border,
output reg g_hires, // 640x480, 1 bit a pixel
output reg [7:0] g_cols, // its colours: {clear, set}
// sprites, on the video clock (see sprites.v)
input sp_go,
input [7:0] sp_line,
@@ -254,7 +262,10 @@ module rv32_system #(
// ------------------------------------------------------------ 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).
// RAMs (one array would be built as 96K deep, 12 of them). In 640x480 mode
// each 4-bit entry holds four pixels side by side (bit 0 the leftmost):
// entry y*160 + x/4. A plot there reads the entry and writes it back the
// next clock with one bit changed.
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];
@@ -264,18 +275,31 @@ module rv32_system #(
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 [9:0] gx;
reg [8: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 [16:0] lxy = {gy[7:0], 8'd0} + {2'd0, gy[7:0], 6'd0} + {7'd0, gx}; // y*320 + x
wire [16:0] hxy = {gy, 7'd0} + {2'd0, gy, 5'd0} + {9'd0, gx[9:2]}; // y*160 + x/4
wire plot = io_wr && io_addr == 8'h33 &&
(g_hires ? gx < 10'd640 && gy < 9'd480 : gx < 10'd320 && gy < 9'd240);
reg rmw; // 640x480 plot: write the entry read last clock
reg [16:0] rmw_a;
reg [1:0] rmw_bit;
reg rmw_v;
wire [3:0] gfx_q = gfx_q_sel ? gfx_q_hi : gfx_q_lo;
wire [3:0] rmw_wd = (gfx_q & ~(4'd1 << rmw_bit)) | ({3'd0, rmw_v} << rmw_bit);
wire [16:0] g_addr = fill_busy ? fill_a : rmw ? rmw_a : g_hires ? hxy : lxy;
wire g_we = fill_busy || (plot && !g_hires) || rmw;
wire [3:0] g_wd = fill_busy ? fill_c : rmw ? rmw_wd : io_wd[3:0];
always @(posedge clk) begin
rmw <= plot && g_hires && !fill_busy;
rmw_a <= hxy;
rmw_bit <= gx[1:0];
rmw_v <= io_wd != 8'd0;
end
always @(posedge clk) begin
if (g_we && !g_addr[16]) gfx_lo[g_addr[15:0]] <= g_wd;
@@ -303,21 +327,28 @@ module rv32_system #(
always @(posedge clk)
if (rst) begin
fill_busy <= 1'b0;
gx <= 9'd0; gy <= 8'd0;
gx <= 10'd0; gy <= 9'd0;
scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0;
g_hires <= 1'b0; g_cols <= 8'h0F;
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'h31: begin gx[9:8] <= io_wd[1:0]; gy[8] <= io_wd[2]; end
8'h32: gy[7:0] <= 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
fill_busy <= 1'b1; fill_a <= 17'd0;
// in 640x480 mode a colour fills all four pixels of each entry
fill_c <= g_hires && io_wd[7] ? {4{io_wd[3:0] != 4'd0}} : io_wd[3:0];
if (!io_wd[7]) begin
scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0; g_hires <= 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;
8'h38: g_hires <= io_wd[0];
8'h39: g_cols <= io_wd;
default: ;
endcase
if (fill_busy) begin
@@ -439,8 +470,9 @@ module rv32_system #(
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'h33: io_din = g_hires ? {7'd0, gfx_q[gx[1:0]]} : {4'd0, gfx_q};
8'h34: io_din = {7'd0, fill_busy};
8'h38: io_din = {7'd0, g_hires};
8'h40: io_din = fl_ctl_q;
8'h41: io_din = fl_dat_q;
8'h60: io_din = {5'd0, why};
+18 -5
View File
@@ -12,6 +12,9 @@
// 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.
// With g_hires the same memory is 640x480 at a bit a pixel (each pixel 2x2,
// four pixels to an entry, bit 0 the leftmost), in two colours from g_cols
// ({clear, set}; 0 is transparent); it doesn't scroll or hide in a border.
// One pixel per clock, pipelined:
// clock 0 position -> row, column, glyph line (registered)
// clock 1 character RAM and graphics reads
@@ -38,6 +41,9 @@ module text_display #(
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
input g_hires, // 640x480, 1 bit a pixel
input [7:0] g_cols, // its colours: {clear, set}
input g_show, // show the graphics layer at all
// sprites: the line to prepare next, and the pixel on screen
output reg sp_go,
output reg [7:0] sp_line,
@@ -114,6 +120,8 @@ module text_display #(
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 [1:0] a_gbit;
wire [8:0] hrow = v[9:1]; // 640x480 pixel row
reg [8:0] a_spx;
reg a_spbuf;
always @(posedge clk) begin
@@ -128,8 +136,10 @@ module text_display #(
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_ghide <= !g_hires && g_border && (gcol_s < 9'd4 || gcol_s >= 9'd316 || grow_s < 8'd2 || grow_s >= 8'd238);
a_gfx <= g_hires ? {hrow, 7'd0} + {2'd0, hrow, 5'd0} + {9'd0, h[10:3]}
: {1'b0, g_row, 8'd0} + {3'd0, g_row, 6'd0} + {8'd0, g_col};
a_gbit <= h[2:1];
a_spx <= h[10:2];
a_spbuf <= v[2];
end
@@ -141,6 +151,8 @@ module text_display #(
assign sp_buf = a_spbuf;
assign sp_on = a_gon;
reg [3:0] b_line;
reg [1:0] b_gbit;
always @(posedge clk) b_gbit <= a_gbit;
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)
@@ -158,7 +170,8 @@ module text_display #(
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_gfx <= !g_show || !b_gon || b_ghide ? 4'd0 :
!g_hires ? gfx_pix : gfx_pix[b_gbit] ? g_cols[3:0] : g_cols[7:4];
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};
@@ -212,8 +225,8 @@ module char_ram (
(* 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};
for (i = 0; i < 16384; i = i + 1) lo[i] = {10'h00F, 8'h20};
for (i = 0; i < 2 * PAGE - 16384; i = i + 1) hi[i] = {10'h00F, 8'h20};
end
wire [14:0] aa = {1'b0, a_addr} + (a_alt ? PAGE : 0);