Start the screen at 132x64

The terminal comes up (and resets with ESC c) in DECCOLM's 132x64 mode;
term_ctrl's WIDE_BOOT parameter selects 80x32 instead. Port 0 bit 2 tells
software which width is showing: BASIC's line editor wraps at it, and
the games, which play at 80 columns, put 132 back when they end.

scripts/sim.sh wide checks the start-up; the other checks still start
at 80x32. Manual, README and terminfo updated. send-hex.py is executable.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-30 19:34:12 -08:00
1 parent 5930b958b4
commit 19278d1d74
23 files changed
+4183 -4102

No files matched your search

+5
View File
@@ -8,6 +8,11 @@ three-voice sound chip (PmodAMP2 on JA), the keyboard on the USB host port
(with a keys-held port for games), files in the configuration flash,
Dartmouth-style BASIC, a machine-code monitor and five games.
The screen starts at 132 columns by 64 rows (8×16 font); `ESC [?3l` switches
to 80×32 in a font twice the size and `ESC [?3h` back (for a PC using the
terminal, `TERM=basys3-w` and `stty rows 64 cols 132`, from
`terminfo/basys3.ti`).
Flip **SW15** up and the terminal is the computer's console. BASIC and the
monitor are described in [docs/basic-manual.md](docs/basic-manual.md); the
terminal itself (SW15 down) works exactly as in the Z-80 project, whose README
BIN
View File
Binary file not shown.
+11 -8
View File
@@ -33,7 +33,7 @@ typedef uint32_t u32;
/* ------------------------------------------------------------- platform */
#ifdef __riscv
#include "basys3.h"
#define con_stat IO8(0x00) /* bit 0: key waiting, bit 1: can send */
#define con_stat IO8(0x00) /* bit 0: key waiting, bit 1: can send, bit 2: 132 columns */
#define con_data IO8(0x01)
static void out_c(u8 c) { while (!(con_stat & 2)); con_data = c; }
@@ -46,6 +46,7 @@ static void poke(u32 a, u8 v) { *(volatile u8 *)a = v; }
static u8 io_in(u8 p) { return IO8(p); }
static void io_out(u8 p, u8 v) { IO8(p) = v; }
static void bye(void) { }
static u8 scr_width(void) { return con_stat & 4 ? 132 : 80; }
#define gx_lo IO8(0x30)
#define gx_hi IO8(0x31)
@@ -116,6 +117,7 @@ static u8 io_in(u8 p) { (void)p; return 0xFF; }
static void io_out(u8 p, u8 v) { (void)p; (void)v; }
static void close_all(void);
static void bye(void) { close_all(); exit(0); }
static u8 scr_width(void) { return 80; }
static u8 gmem[240][320];
static u16 hgx;
static u8 hgy;
@@ -1037,10 +1039,10 @@ static void check_break(void)
At the end of a line that is only a line number, Right copies that program
line one character at a time and End copies the rest of it; Delete there
skips a character of it. The screen is kept up to date with cursor moves
only, so a line may wrap: ed_sc is the column it starts in, ED_W the
screen width, ed_row the screen row (from the line's first) the
only, so a line may wrap: ed_sc is the column it starts in, ed_w the
screen width (80 or 132, read when the line starts), ed_row the screen row (from the line's first) the
terminal's cursor is on and ed_at the place it is at. */
#define ED_W 80
static u8 ed_w = 80;
static void esc_num(u8 n);
static u8 ed_sc, ed_row, ed_at, ed_pend, ed_n, ed_cur;
static u8 *ed_src; /* the program line being copied, or 0 */
@@ -1055,7 +1057,7 @@ static void ed_csi(u8 n, char f)
static void ed_go(u8 i)
{
u16 p = ed_sc + i;
u8 r = p / ED_W, c = p % ED_W;
u8 r = p / ed_w, c = p % ed_w;
if (i == ed_at && !ed_pend) return;
out_c('\r');
if (r < ed_row) ed_csi(ed_row - r, 'A');
@@ -1073,9 +1075,9 @@ static void ed_show(u8 i, u8 gone)
for (; k < ed_n; k++) out_c(linebuf[k]);
for (; gone; gone--, k++) out_c(' ');
if (k > i) {
ed_row = (ed_sc + k - 1) / ED_W;
ed_row = (ed_sc + k - 1) / ed_w;
ed_at = k;
ed_pend = (ed_sc + k) % ED_W == 0;
ed_pend = (ed_sc + k) % ed_w == 0;
}
ed_go(ed_cur);
}
@@ -1133,7 +1135,8 @@ static void ed_del(void)
static u8 read_line(void)
{
u8 c, k;
ed_sc = col % ED_W;
ed_w = scr_width();
ed_sc = col % ed_w;
ed_row = ed_at = ed_pend = ed_n = ed_cur = 0;
ed_src = 0;
for (;;) {
+780 -780
View File
File diff suppressed because it is too large. Load diff
+6 -4
View File
@@ -596,9 +596,11 @@ the numbers (see section 9).
## 8. The text screen
The text screen is 80 columns by 32 rows; `PRINT CHR$(27);"[?3h"` switches to
132 columns by 64 rows in a smaller font (and clears it), `CHR$(27);"[?3l"`
back. Two statements place and colour text:
The text screen starts at 132 columns by 64 rows in the 8×16 font.
`PRINT CHR$(27);"[?3l"` switches to 80 columns by 32 rows in a font twice the
size (and clears it), `CHR$(27);"[?3h"` back to 132; `INP(0) AND 4` is 4 at
132 columns. The line editor follows the change, and the games play at 80
columns and put the screen back as they found it. Two statements place and colour text:
| Statement | Does |
|---|---|
@@ -654,7 +656,7 @@ compare value, 10h and 14h the monitor's breakpoint addresses.
| Port | `INP` | `OUT` |
|---|---|---|
| 0 | bit 0: a key is waiting; bit 1: the screen can take a character | — |
| 0 | bit 0: a key is waiting; bit 1: the screen can take a character; bit 2: the screen is 132 columns wide | — |
| 1 | the waiting key | a character to the screen |
| 16–19 | the millisecond counter, low byte first (reading 16 freezes the other three) | — |
| 32 | — | right two digits of the 7-segment display (hex) |
Binary file not shown.
+3 -1
View File
@@ -637,6 +637,7 @@ int main(void)
{
rnd_state = 0x1980 ^ frame_ctr;
last_frame = frame_ctr;
u8 was_wide = con_stat & 4; /* 132 columns before the game? */
puts_("\033[?3l\033[?25l");
load_shapes();
quit = 0;
@@ -645,6 +646,7 @@ int main(void)
play();
}
clear_all();
puts_("\033[0m\033[2J\033[H\033[?25h");
puts_(was_wide ? "\033[0m\033[?3h\033[?25h" /* back to 132 columns (this clears) */
: "\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+590 -587
View File
File diff suppressed because it is too large. Load diff
+3 -1
View File
@@ -632,6 +632,7 @@ int main(void)
{
rnd_state = 0xACE1 ^ frame_ctr;
last_frame = frame_ctr;
u8 was_wide = con_stat & 4; /* 132 columns before the game? */
puts_("\033[?3l\033[?25l"); /* 80 columns (this clears), no cursor */
load_shapes();
quit = 0;
@@ -642,6 +643,7 @@ int main(void)
if (!end_screen()) break;
}
clear_gfx();
puts_("\033[0m\033[2J\033[H\033[?25h");
puts_(was_wide ? "\033[0m\033[?3h\033[?25h" /* back to 132 columns (this clears) */
: "\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+812 -809
View File
File diff suppressed because it is too large. Load diff
+3 -1
View File
@@ -683,6 +683,7 @@ int main(void)
{
rnd_state = 0xBEEF ^ frame_ctr;
last_frame = frame_ctr;
u8 was_wide = con_stat & 4; /* 132 columns before the game? */
puts_("\033[?3l\033[?25l");
load_shapes();
quit = 0;
@@ -691,6 +692,7 @@ int main(void)
play();
}
clear_all();
puts_("\033[0m\033[2J\033[H\033[?25h");
puts_(was_wide ? "\033[0m\033[?3h\033[?25h" /* back to 132 columns (this clears) */
: "\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+597 -594
View File
File diff suppressed because it is too large. Load diff
+3 -1
View File
@@ -641,6 +641,7 @@ int main(void)
u8 i;
rnd_state = 0x1979 ^ frame_ctr;
last_frame = frame_ctr;
u8 was_wide = con_stat & 4; /* 132 columns before the game? */
puts_("\033[?3l\033[?25l");
load_shapes();
for (i = 0; i < NST; i++) { st_x[i] = rnd() % 304; st_y[i] = rnd() % 240; st_v[i] = 1 + (i & 1); }
@@ -650,6 +651,7 @@ int main(void)
play();
}
clear_all();
puts_("\033[0m\033[2J\033[H\033[?25h");
puts_(was_wide ? "\033[0m\033[?3h\033[?25h" /* back to 132 columns (this clears) */
: "\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+550 -548
View File
File diff suppressed because it is too large. Load diff
+3 -1
View File
@@ -803,6 +803,7 @@ int main(void)
{
rnd_state = 0x2084 ^ frame_ctr;
last_frame = frame_ctr;
u8 was_wide = con_stat & 4; /* 132 columns before the game? */
puts_("\033[?3l\033[?25l");
load_shapes();
quit = 0;
@@ -811,6 +812,7 @@ int main(void)
play();
}
clear_all();
puts_("\033[0m\033[2J\033[H\033[?25h");
puts_(was_wide ? "\033[0m\033[?3h\033[?25h" /* back to 132 columns (this clears) */
: "\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+757 -755
View File
File diff suppressed because it is too large. Load diff
+5 -3
View File
@@ -30,6 +30,7 @@ module basys3_terminal #(
parameter MEM_FILE = "basic.mem",
parameter MEM_HI_FILE = "basic-hi.mem",
parameter MON_FILE = "monitor.mem",
parameter WIDE_BOOT = 1, // the screen starts at 132x64 (0: 80x32)
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,
@@ -343,10 +344,11 @@ module basys3_terminal #(
(* ASYNC_REG = "TRUE" *) reg [2:0] brk_s = 3'd0;
always @(posedge cpu_clk) brk_s <= {brk_s[1:0], brk_t};
wire wide; // DECCOLM from the terminal (term_ctrl below)
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),
.tx_req(ztx_req), .tx_data(ztx_data), .tx_ack(ztx_ack), .wide(wide),
.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),
@@ -425,7 +427,7 @@ module basys3_terminal #(
// ------------------------------------------------------------- terminal
wire [13:0] t_addr;
wire alt_scr, wide;
wire alt_scr;
wire t_we;
wire [17:0] t_wdata, t_rdata;
wire [5:0] cur_row, stop, sbot;
@@ -443,7 +445,7 @@ module basys3_terminal #(
always @(posedge clk) frame_s <= {frame_s[1:0], frame_t};
assign frame_tick = frame_s[2] ^ frame_s[1];
term_ctrl term (
term_ctrl #(.WIDE_BOOT(WIDE_BOOT)) 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),
+5 -1
View File
@@ -78,6 +78,7 @@ module rv32_system #(
output reg tx_req,
output reg [7:0] tx_data,
input tx_ack,
input wide, // the screen is 132x64 (terminal clock: synchronised here)
output reg [15:0] seg_val,
output halted,
input brk_key, // pulse: the Pause key, enter the monitor
@@ -427,9 +428,12 @@ module rv32_system #(
end
end
(* ASYNC_REG = "TRUE" *) reg [1:0] wide_s = 2'b00;
always @(posedge clk) wide_s <= {wide_s[0], wide};
// -------------------------------------------------------- port reads
always @* case (io_addr)
8'h00: io_din = {6'd0, tx_ready, rx_avail};
8'h00: io_din = {5'd0, wide_s[1], 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 -4
View File
@@ -1,7 +1,8 @@
`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 character buffer up to date: 132x64 (DECCOLM, CSI ?3h; the start-up
// mode unless WIDE_BOOT = 0) or 80x32 (CSI ?3l).
// 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.
//
@@ -21,7 +22,9 @@
// 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 (
module term_ctrl #(
parameter WIDE_BOOT = 1 // start (and ESC c) in 132x64; 0 for 80x32
) (
input clk,
input rst,
input [7:0] in_byte,
@@ -343,8 +346,9 @@ module term_ctrl (
begin
soft_reset;
{row, col} <= 0;
{sclm, lnm, wide, colm_clr} <= 4'b0;
sbot <= 6'd31;
{sclm, lnm, colm_clr} <= 3'b0;
wide <= WIDE_BOOT != 0;
sbot <= WIDE_BOOT != 0 ? 6'd63 : 6'd31;
dec_leds <= 4'd0;
sm_on <= 1'b0;
sm_off <= 5'd0;
+3 -1
View File
@@ -1,12 +1,14 @@
#!/bin/bash
# Run the whole-design testbench in Vivado's simulator; output lands in sim/work.
# scripts/sim.sh [quick] quick: only the computer, up to the monitor
# scripts/sim.sh [quick|wide] quick: only the computer, up to the monitor
# wide: the 132x64 start-up (the checks otherwise start at 80x32)
set -e
cd "$(dirname "$0")/.."
ROOT=$(pwd)
source scripts/vivado_env.sh
DEFS=""
[ "$1" = quick ] && DEFS="-d SKIP_TERMINAL -d STOP_BEFORE_MONITOR"
[ "$1" = wide ] && DEFS="-d SKIP_TERMINAL -d WIDE_BOOT"
MON=$ROOT/monitor/monitor.mem
[ -e "$MON" ] || { mkdir -p sim/work; MON=$ROOT/sim/work/empty.mem; : > "$MON"; }
mkdir -p sim/work
+36 -1
View File
@@ -17,7 +17,12 @@ module tb_terminal;
wire qcs;
wire [3:0] qdb;
basys3_terminal #(.FONT_FILE(`FONT_FILE), .MEM_FILE(`MEM_FILE), .MEM_HI_FILE(`MEM_HI_FILE), .MON_FILE(`MON_FILE)) dut (
`ifdef WIDE_BOOT
localparam WIDE = 1; // scripts/sim.sh wide: the board as built, 132x64
`else
localparam WIDE = 0; // the other checks were written for an 80x32 start
`endif
basys3_terminal #(.WIDE_BOOT(WIDE), .FONT_FILE(`FONT_FILE), .MEM_FILE(`MEM_FILE), .MEM_HI_FILE(`MEM_HI_FILE), .MON_FILE(`MON_FILE)) dut (
.clk(clk), .btnC(btnC), .btnU(btnU), .btnD(btnD), .sw(sw), .led(led), .seg(), .dp(), .an(),
.vgaRed(r), .vgaGreen(g), .vgaBlue(b), .Hsync(hs), .Vsync(vs),
.RsRx(rsrx), .RsTx(rstx), .PS2Clk(ps2c), .PS2Data(ps2d),
@@ -28,6 +33,7 @@ module tb_terminal;
real BIT_NS = 1_000_000_000.0 / 115200; // changed for the 3 Mbaud test
integer errors = 0;
integer wk;
task uart_send(input [7:0] d);
integer i;
@@ -346,6 +352,35 @@ module tb_terminal;
if (hs_bad == 0 && vs_bad == 0) $display("PASS 1280x1024 sync timing");
else begin $display("FAIL sync timing: %0d hsync, %0d vsync periods off", hs_bad, vs_bad); errors = errors + 1; end
`endif
`ifdef WIDE_BOOT
// ---- start-up in 132x64: the screen, port 0 bit 2, BASIC's line editor
uart_str("\033[2J\033[H");
#100_000;
sw[15] = 1; sw[14] = 1;
#3_000_000;
if (dut.wide) $display("PASS the screen starts at 132x64");
else begin $display("FAIL the screen starts at 80x32"); errors = errors + 1; end
uart_str("PRINT INP(0) AND 4\015");
#2_000_000;
expect_row(1, "DARTMOUTH-STYLE BASIC");
expect_row(5, " 4");
// a 107-character line stays on one row; three Lefts and a Y land
// in that row only if the editor knows the screen is 132 wide
uart_str("10 REM ");
for (wk = 0; wk < 100; wk = wk + 1) uart_send("X");
uart_str("\033[D\033[D\033[DY");
#2_000_000;
if (cellraw(8, 106) == "X" && cellraw(8, 104) == "Y" && cellraw(9, 0) == " ")
$display("PASS the line editor works 132 wide");
else begin
$display("FAIL the line editor at 132 wide: row 8 col 104 '%c' col 106 '%c', row 9 col 0 '%c'",
cellraw(8, 104), cellraw(8, 106), cellraw(9, 0));
errors = errors + 1;
end
if (errors == 0) $display("ALL TESTS PASSED");
else $display("%0d TESTS FAILED", errors);
$finish;
`endif
// ---- BASIC as the console, input pasted over the UART
uart_str("\033[2J\033[H");
+3 -2
View File
@@ -1,6 +1,7 @@
# terminfo for the Basys 3 FPGA terminal: a VT220 with 16 ANSI colours,
# underline, blink, 80x32 (basys3) or 132x64 after ESC[?3h (basys3-w).
# tic -x -o ~/.terminfo terminfo/basys3.ti then export TERM=basys3
# underline, blink. The RISC-V board starts at 132x64 (basys3-w); after
# ESC[?3l it is 80x32 (basys3).
# tic -x -o ~/.terminfo terminfo/basys3.ti then export TERM=basys3-w
basys3|Basys 3 FPGA VT220 terminal with ANSI colour,
lines#32, cols#80,
colors#16, pairs#256,
Regular → Executable
View File
File mode changed.