1280x1024 display: 80x32 big font or 132x64 (DECCOLM)

The display runs at 1280x1024 60 Hz from a second MMCM (108 MHz pixel
clock), one pixel per clock through a four-stage pipeline. 80 columns
show 80x32 cells of 16x32 pixels (the 8x16 font doubled); CSI ?3 h
switches to 132x64 cells of 8x16 pixels, centred, and CSI ?3 l back,
both clearing the screen and margins as a VT100 does. The 320x240
graphics layer is drawn 4x4 in the top 960 lines, so 80-column row r
still meets graphics line 8(r-1).

The terminal engine has 6-bit rows and 8-bit columns, a 132-cell row
stride in both layouts (so the copy engine is unchanged), 132 tab stops
and 3-digit cursor reports. The character RAM holds two 132x64 pages
(normal and alternate) and is read on the pixel clock.

BASIC's LOCATE takes rows up to 64 and columns up to 132. terminfo has
basys3 (80x32) and basys3-w (132x64). The testbench reads the 132-cell
layout, checks 1280x1024 sync timing, captures 1280x1024 frames and
tests DECCOLM both ways.

Timing met (0.044 ns at 90 MHz CPU, 108 MHz pixel); 46 of 50 BRAMs.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-28 12:24:21 -08:00
1 parent 634e314010
commit 88b5942da5
12 files changed
+344 -210

No files matched your search

+14 -4
View File
@@ -3,8 +3,10 @@
Turns a Digilent Basys 3 (Artix-7 XC7A35T) into a stand-alone VT220-compatible
serial terminal with ANSI colour:
- **Screen**: VGA port, 640x480 @ 60 Hz, 80 columns x 30 rows, 8x16 CP437 font,
16 colours, underline, blink, reverse video, optional smooth scrolling.
- **Screen**: VGA port, 1280x1024 @ 60 Hz. 80 columns x 32 rows in a 16x32
font (the 8x16 CP437 font doubled), or 132 columns x 64 rows in the 8x16 font
after `ESC[?3h` (DECCOLM). 16 colours, underline, blink, reverse video,
optional smooth scrolling.
- **Keyboard**: a USB keyboard in the **USB HID host** port (J2). The board's
PIC24 presents it to the FPGA as a PS/2 keyboard.
- **Serial line**: the micro-USB **PROG/UART** port (the FTDI chip), 8N1. Bytes
@@ -45,6 +47,12 @@ printf '[Service]\nExecStart=\nExecStart=-/usr/sbin/agetty -L 115200 %%I basys3\
sudo systemctl daemon-reload && sudo systemctl restart serial-getty@ttyUSB1.service
```
For 132 columns, switch the terminal and tell the PC its size:
```bash
printf '\033[?3h'; export TERM=basys3-w; stty rows 64 cols 132
```
The getty sets `TERM=basys3` for the login session. Programs find the entry in
`~/.terminfo` once you're logged in. Without the entry, `TERM=vt220` works but
has no colour.
@@ -130,7 +138,9 @@ For the fastest terminal:
approximated to the 16 colours.
- **Modes**: `DECCKM` (cursor keys), `DECKPAM`/`DECKPNM` (keypad), `LNM`,
`DECTCEM` (cursor on/off), `DECSCLM` (smooth scroll), alternate screen
(`CSI ?47 h`/`l`, a second page; graphics are hidden while it is shown).
(`CSI ?47 h`/`l`, a second page; graphics are hidden while it is shown),
`DECCOLM` (`CSI ?3 h`/`l`: 132x64 / 80x32, clearing the screen as a VT100
does).
- **Replies to the host**:
- `DA` gets `ESC[?62;22c` (a VT220 with colour)
- Secondary `DA` gets `ESC[>1;10;0c`
@@ -459,5 +469,5 @@ which Vivado needs on newer distros.
| `rtl/uart.v` | UART receiver and transmitter |
| `rtl/fifo.v`, `rtl/sevenseg.v` | Helpers |
| `font/font.mem` | 256x16 font ROM, made by `tools/psf2mem.py` from the Linux console fonts |
| `terminfo/basys3.ti` | terminfo entry: VT220 plus 16 colours, 30 lines |
| `terminfo/basys3.ti` | terminfo entries: `basys3` (VT220, 16 colours, 80x32) and `basys3-w` (132x64) |
| `constraints/basys3.xdc` | Pin assignments |
BIN
View File
Binary file not shown.
+1 -1
View File
@@ -1551,7 +1551,7 @@ static void graphics(u8 t)
break;
case T_LOCATE: /* LOCATE row, column (from 1) */
int_args(a, 2, 2);
if (a[0] < 1 || a[0] > 30 || a[1] < 1 || a[1] > 80) error(E_FUNC);
if (a[0] < 1 || a[0] > 64 || a[1] < 1 || a[1] > 132) error(E_FUNC);
out_c(27); out_c('['); esc_num(a[0]); out_c(';'); esc_num(a[1]); out_c('H');
col = a[1] - 1;
break;
+2 -2
View File
@@ -17162,7 +17162,7 @@ de
38
21
3e
1e
40
b9
3e
00
@@ -17187,7 +17187,7 @@ de
38
08
3e
50
84
b9
3e
00
+2 -1
View File
@@ -98,7 +98,8 @@ set_false_path -from [get_ports {QspiDB[*]}]
# The Z-80 clock is only crossed through synchronisers and toggle handshakes.
set_clock_groups -asynchronous -group [get_clocks sys_clk_pin] \
-group [get_clocks -of_objects [get_pins cpu_mmcm/CLKOUT0]]
-group [get_clocks -of_objects [get_pins cpu_mmcm/CLKOUT0]] \
-group [get_clocks -of_objects [get_pins pix_mmcm/CLKOUT0]]
set_false_path -to [get_ports {led[*] seg[*] dp an[*] RsTx}]
## Configuration
+8 -5
View File
@@ -381,9 +381,10 @@ It is mixed with the 80×30 text:
Drawing in colour 0 erases.
- **Clipping:** anything that falls off the edge is simply not drawn, so a
circle may hang off the screen.
- **Text cells:** a text character covers 4×8 graphics pixels. The character at
row *r*, column *c* (as used by `LOCATE`) starts at
*X* = 4(*c*−1), *Y* = 8(*r*−1).
- **Text cells:** in 80 columns a text character covers 4×8 graphics pixels.
The character at row *r*, column *c* (as used by `LOCATE`) starts at
*X* = 4(*c*−1), *Y* = 8(*r*−1). The graphics cover rows 1–30; rows 31 and
32 are text only.
### Colours
@@ -449,11 +450,13 @@ the numbers (see section 9).
## 8. The text screen
The text screen is 80 columns by 30 rows. Two statements place and colour text:
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:
| Statement | Does |
|---|---|
| `LOCATE R, C` | Moves the cursor to row *R* (1–30), column *C* (1–80) |
| `LOCATE R, C` | Moves the cursor to row *R* (1–32, or 1–64), column *C* (1–80, or 1–132) |
| `INK F [, B]` | Sets the text colour *F* and, optionally, background *B*, using the colour numbers above |
```
Binary file not shown.
+47 -15
View File
@@ -237,7 +237,7 @@ module basys3_terminal #(
// 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 80x30 ",
"\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"};
@@ -264,6 +264,27 @@ module basys3_terminal #(
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};
@@ -311,7 +332,7 @@ module basys3_terminal #(
.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(z80_seg), .halted(), .brk_key(brk_s[2] ^ brk_s[1]),
.vclk(clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix),
.vclk(pix_clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix),
.fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1])
);
@@ -379,16 +400,24 @@ module basys3_terminal #(
);
// ------------------------------------------------------------- terminal
wire [11:0] t_addr;
wire alt_scr;
wire [13:0] t_addr;
wire alt_scr, wide;
wire t_we;
wire [17:0] t_wdata, t_rdata;
wire [4:0] cur_row, stop, sbot, sm_off;
wire [6:0] cur_col;
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;
wire frame_tick, pix_frame;
// 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),
@@ -399,25 +428,28 @@ module basys3_terminal #(
.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)
.dec_leds(dec_leds), .bell(bell), .alt_scr(alt_scr), .wide(wide)
);
wire [11:0] v_addr;
wire [13:0] v_addr;
wire [17:0] v_data;
// the alternate screen (the Z-80 monitor uses it) has no graphics layer
// 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 Z-80 monitor uses it) has no graphics layer.
char_ram screen (
.clk(clk),
.a_addr({alt_scr, t_addr}), .a_we(t_we), .a_wdata(t_wdata), .a_rdata(t_rdata),
.b_addr({alt_scr, v_addr}), .b_rdata(v_data)
.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(clk), .rst(rst), .cur_row(cur_row), .cur_col(cur_col), .cur_vis(cur_vis),
.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),
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync),
.frame_tick(frame_tick)
.frame_tick(pix_frame)
);
// ----------------------------------------------------- status displays
+117 -97
View File
@@ -1,7 +1,9 @@
`timescale 1ns / 1ps
// VT220 terminal engine with ANSI colour. Consumes received bytes and keeps
// the 80x30 new_cell buffer (row-major, address = row*80 + col) up to date.
// 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.
@@ -10,7 +12,8 @@
// 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) DECSCLM(?4) DECOM(?6) DECAWM(?7) DECTCEM(?25)
// 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.
@@ -31,16 +34,16 @@ module term_ctrl (
input smooth_fast,
input frame_tick, // start of vertical blanking
// character RAM port
output reg [11:0] addr,
output reg [13:0] addr,
output reg we,
output reg [17:0] wdata,
input [17:0] rdata,
// to the display
output reg [4:0] row,
output reg [6:0] col,
output reg [5:0] row,
output reg [7:0] col,
output reg cur_vis,
output reg [4:0] stop, // scroll region, inclusive
output reg [4:0] sbot,
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,
@@ -54,11 +57,16 @@ module term_ctrl (
output reg lnm,
output reg [3:0] dec_leds,
output reg bell,
output reg alt_scr // showing and writing the alternate screen
output reg alt_scr, // showing and writing the alternate screen
output reg wide // 132 columns (DECCOLM)
);
localparam [4:0] LAST_ROW = 5'd29;
localparam [6:0] LAST_COL = 7'd79;
localparam [11:0] CELLS = 12'd2400;
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,
@@ -77,11 +85,12 @@ module term_ctrl (
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 [79:0] tabs;
reg [131:0] tabs;
reg colm_clr; // DECCOLM: clear the screen when idle
// saved cursor (DECSC)
reg [4:0] sv_row;
reg [6:0] sv_col;
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;
@@ -94,12 +103,12 @@ module term_ctrl (
// scroll request
reg sc_dir;
reg [4:0] sc_t, sc_b;
reg [5:0] sc_t, sc_b;
reg [7:0] sc_n;
reg [5:0] sc_h, sc_k; // region height, lines to move
reg [6:0] sc_h, sc_k; // region height, lines to move
// memory copy / fill engine
reg [11:0] c_src, c_dst, c_cnt, f_addr, f_cnt;
reg [13:0] c_src, c_dst, c_cnt, f_addr, f_cnt;
reg c_dir;
reg [17:0] f_cell;
@@ -119,12 +128,12 @@ module term_ctrl (
integer i;
// ------------------------------------------------------------ helpers
function [11:0] rc(input [4:0] r, input [6:0] cc);
rc = {1'b0, r, 6'b0} + {3'b0, r, 4'b0} + {5'b0, cc};
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 [11:0] rows80(input [5:0] n);
rows80 = {n, 6'b0} + {2'b0, n, 4'b0};
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);
@@ -187,20 +196,20 @@ module term_ctrl (
end
endfunction
function [6:0] next_tab(input [79:0] t, input [6:0] cc);
function [7:0] next_tab(input [131:0] t, input [7:0] cc, input [7:0] last);
integer k;
begin
next_tab = LAST_COL;
for (k = 79; k >= 0; k = k - 1)
if (k > cc && t[k]) next_tab = k;
next_tab = last;
for (k = 131; k >= 0; k = k - 1)
if (k > cc && k < last && t[k]) next_tab = k;
end
endfunction
function [6:0] prev_tab(input [79:0] t, input [6:0] cc);
function [7:0] prev_tab(input [131:0] t, input [7:0] cc);
integer k;
begin
prev_tab = 7'd0;
for (k = 0; k < 80; k = k + 1)
prev_tab = 8'd0;
for (k = 0; k < 132; k = k + 1)
if (k < cc && t[k]) prev_tab = k;
end
endfunction
@@ -210,12 +219,12 @@ module term_ctrl (
wire [7:0] p0 = par[0];
wire [7:0] p1 = par[1];
reg [7:0] n8; // count parameter (default 1), registered
reg [6:0] nc; // ... clamped to the rest of the line
wire [6:0] room = 7'd80 - col; // cells from cursor to end of line
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[6:0];
nc <= n8_c > room ? room : n8_c;
end
wire [3:0] fgb = bold ? (fg | 4'd8) : fg;
@@ -229,29 +238,29 @@ module term_ctrl (
wire smooth = sclm | smooth_sw;
wire [4:0] sm_step = smooth_fast ? 5'd4 : 5'd2;
wire [4:0] up_lim = row >= stop ? stop : 5'd0;
wire [4:0] dn_lim = row <= sbot ? sbot : LAST_ROW;
wire [4:0] row_up = (row - up_lim > n8) ? row - n8 : up_lim;
wire [4:0] row_dn = (dn_lim - row > n8) ? row + n8 : dn_lim;
wire [6:0] col_rt = (LAST_COL - col > n8) ? col + n8 : LAST_COL;
wire [6:0] col_lt = (col > n8) ? col - n8 : 7'd0;
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 : 5'd0) + r_rel;
wire [4:0] r_max = om ? sbot : LAST_ROW;
wire [4:0] row_p0 = r_abs > r_max ? r_max : r_abs[4:0];
wire [6:0] col_p0 = p0 == 0 ? 7'd0 : p0 > 80 ? LAST_COL : p0 - 1'b1;
wire [6:0] col_p1 = p1 == 0 ? 7'd0 : p1 > 80 ? LAST_COL : p1 - 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 [11:0] here = rc(row, col);
wire [4:0] rep_row = (om ? row - stop : row) + 1'b1;
wire [6:0] rep_col = col + 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 [11:0] src, input [11:0] dst, input [11:0] ccnt, input dir,
input [11:0] fa, input [11:0] fcnt, input [17:0] fcell, input [3:0] rs);
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;
@@ -260,14 +269,14 @@ module term_ctrl (
end
endtask
task fill(input [11:0] fa, input [11:0] fcnt);
op(12'd0, 12'd0, 12'd0, 1'b0, fa, fcnt, BLANK, S_IDLE);
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 [4:0] t, input [4:0] b, input [7:0] n, input [3:0] rs);
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;
@@ -322,7 +331,7 @@ module term_ctrl (
awm <= 1'b1;
cur_vis <= 1'b1;
alt_scr <= 1'b0;
stop <= 5'd0;
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;
@@ -334,13 +343,14 @@ module term_ctrl (
begin
soft_reset;
{row, col} <= 0;
{sclm, lnm} <= 2'b0;
{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 < 80; i = i + 1) tabs[i] <= i != 0 && i % 8 == 0;
fill(12'd0, CELLS);
for (i = 0; i < 132; i = i + 1) tabs[i] <= i != 0 && i % 8 == 0;
fill(14'd0, ALL);
end
endtask
@@ -352,15 +362,20 @@ module term_ctrl (
end
endtask
function [7:0] tens(input [6:0] v);
tens = "0" + v / 7'd10;
function [7:0] tens(input [7:0] v);
tens = "0" + (v / 8'd10) % 8'd10;
endfunction
function [7:0] ones(input [6:0] v);
ones = "0" + v % 7'd10;
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
@@ -414,8 +429,13 @@ module term_ctrl (
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 : 5'd0; col <= 7'd0; pend <= 1'b0; end
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;
@@ -447,9 +467,9 @@ module term_ctrl (
cursor_app <= 1'b0;
end else case (st)
S_IDLE:
if (clear_req) begin
{row, col, pend} <= 0;
fill(12'd0, CELLS);
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;
@@ -459,16 +479,16 @@ module term_ctrl (
// ---- 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[5:0];
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), rows80(sc_h - sc_k), 1'b0,
rc(sc_b - sc_k + 1'b1, 0), rows80(sc_k), BLANK, ret_st);
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, rows80(sc_h - sc_k), 1'b1,
rc(sc_t, 0), rows80(sc_k), BLANK, ret_st);
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:
@@ -521,7 +541,7 @@ module term_ctrl (
end
S_TAB: begin
col <= tdir ? prev_tab(tabs, col) : next_tab(tabs, col);
col <= tdir ? prev_tab(tabs, col) : next_tab(tabs, col, LAST_COL);
rep <= rep - 1'b1;
if (rep == 1) st <= S_IDLE;
end
@@ -569,13 +589,13 @@ module term_ctrl (
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); 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 <= 7'd0;
if (lf_cr || lnm) col <= 8'd0;
line_feed(S_IDLE);
end
8'h0D: begin pend <= 1'b0; col <= 7'd0; end
8'h0D: begin pend <= 1'b0; col <= 8'd0; end
8'h0E: gl <= 1'b1;
8'h0F: gl <= 1'b0;
default: ;
@@ -586,12 +606,12 @@ module term_ctrl (
P_NORM:
if (pend && awm) begin
pend <= 1'b0;
col <= 7'd0;
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), {5'd0, LAST_COL - col}, 1'b1,
12'd0, 12'd0, BLANK, S_PUT);
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;
@@ -610,7 +630,7 @@ module term_ctrl (
cint <= 8'd0;
end
"D": begin pend <= 1'b0; line_feed(S_IDLE); end
"E": begin pend <= 1'b0; col <= 7'd0; 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
@@ -641,7 +661,7 @@ module term_ctrl (
"#": if (c == "8") begin // DECALN: fill the screen with E
{row, col, pend, stop} <= 0;
sbot <= LAST_ROW;
op(12'd0, 12'd0, 12'd0, 1'b0, 12'd0, CELLS, {2'b00, 4'd0, def_fg, "E"}, S_IDLE);
op(14'd0, 14'd0, 14'd0, 1'b0, 14'd0, CELLS, {2'b00, 4'd0, def_fg, "E"}, S_IDLE);
end
default: ;
endcase
@@ -684,14 +704,14 @@ module term_ctrl (
"J": begin
pend <= 1'b0;
if (p0 == 0) fill(here, CELLS - here);
else if (p0 == 1) fill(12'd0, here + 1'b1);
else if (p0 == 2) fill(12'd0, CELLS);
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), 12'd80);
else if (p0 == 2) fill(rc(row, 0), STRIDE);
end
"n": if (p0 == 6) cursor_report;
default: ;
@@ -701,8 +721,8 @@ module term_ctrl (
"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 <= 7'd0; end
"F": begin pend <= 1'b0; row <= row_up; col <= 7'd0; 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
@@ -711,47 +731,47 @@ module term_ctrl (
"J": begin
pend <= 1'b0;
if (p0 == 0) fill(here, CELLS - here);
else if (p0 == 1) fill(12'd0, here + 1'b1);
else if (p0 == 2 || p0 == 3) fill(12'd0, CELLS);
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), 12'd80);
else if (p0 == 2) fill(rc(row, 0), STRIDE);
end
"L": if (row >= stop && row <= sbot) begin
pend <= 1'b0; col <= 7'd0;
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 <= 7'd0;
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), room - nc, 1'b1,
here, nc, BLANK, S_IDLE);
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, room - nc, 1'b0,
rc(row, 7'd80 - nc), nc, BLANK, S_IDLE);
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, nc); 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 <= 80'd0;
"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 > 30 ? 8'd30 : p1)) begin
stop <= p0 == 0 ? 5'd0 : p0 - 1'b1;
sbot <= p1 == 0 || p1 > 30 ? LAST_ROW : p1 - 1'b1;
row <= om ? (p0 == 0 ? 5'd0 : p0 - 1'b1) : 5'd0;
col <= 7'd0;
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
+102 -66
View File
@@ -1,29 +1,33 @@
`timescale 1ns / 1ps
// 640x480 @ 60 Hz VGA text display: 80x30 cells of 8x16 pixels, 16 colours,
// underline, blink, block cursor and smooth-scroll offset for the scroll region.
// A 320x240 graphics layer (each pixel 2x2) shows through wherever the text is
// background; graphics colour 0 is transparent.
// Runs on the 100 MHz clock with a 25 MHz pixel enable. After each pixel tick:
// clock 1 scanline values registered (row base address, glyph line)
// clock 2 character RAM read
// clock 3 font ROM read
// clock 4 pixel out (next tick)
// 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.
// 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.
// 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,
input clk, // pixel clock, 108 MHz
input rst,
input [4:0] cur_row,
input [6:0] cur_col,
input wide, // 132-column layout
input [5:0] cur_row,
input [7:0] cur_col,
input cur_vis,
input [4:0] stop, // scroll region (rows, inclusive)
input [4:0] sbot,
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, // pixels the region has moved
output [11:0] ram_addr,
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,
@@ -34,22 +38,19 @@ module text_display #(
output reg vsync,
output reg frame_tick // one clock at the start of vertical blanking
);
localparam H_VIS = 640, H_FP = 16, H_SYNC = 96, H_TOTAL = 800;
localparam V_VIS = 480, V_FP = 10, V_SYNC = 2, V_TOTAL = 525;
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 [1:0] div = 2'd0;
reg [9:0] h, v;
reg [10:0] h, v;
reg [5:0] frame = 6'd0;
wire tick = div == 2'd3;
always @(posedge clk) begin
div <= rst ? 2'd0 : div + 1'b1;
frame_tick <= tick && h == 0 && v == V_VIS;
frame_tick <= h == 0 && v == V_VIS;
if (rst) begin
h <= 0;
v <= 0;
end else if (tick) begin
if (h == H_TOTAL - 1) begin
end else if (h == H_TOTAL - 1) begin
h <= 0;
if (v == V_TOTAL - 1) begin
v <= 0;
@@ -59,32 +60,65 @@ module text_display #(
end else
h <= h + 1'b1;
end
end
// which buffer row and glyph line this scanline shows
wire in_region = sm_on && v < V_VIS && v[8:4] >= stop && v[8:4] <= sbot;
wire [9:0] ys = !in_region ? v : sm_dir ? v - sm_off : v + sm_off;
wire gap = in_region && (sm_dir ? v < {stop, 4'b0} + sm_off : ys[9:4] > sbot);
// ---- 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 [11:0] row_base;
reg [4:0] srow;
reg [3:0] line;
reg blank_line;
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;
reg [16:0] a_gfx;
always @(posedge clk) begin
srow <= ys[8:4];
row_base <= {1'b0, ys[8:4], 6'b0} + {3'b0, ys[8:4], 4'b0};
line <= ys[3:0];
blank_line <= gap;
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_gfx <= {v[9:2], 8'd0} + {2'd0, v[9:2], 6'd0} + {8'd0, h[10:2]};
end
wire [6:0] tcol = h[9:3];
assign ram_addr = row_base + {5'b0, tcol};
assign gfx_addr = {v[8:1], 8'd0} + {2'd0, v[8:1], 6'd0} + {8'd0, h[9:1]};
// ---- clock 1: character RAM and graphics reads
assign ram_addr = a_base + {6'd0, a_col};
assign gfx_addr = a_gfx;
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;
always @(posedge clk)
{b_line, b_bit, b_blank, b_text, b_vis, b_cur, b_hs, b_vs, b_gon} <=
{a_line, a_bit, a_blank, a_text, a_vis, a_cur, a_hs, a_vs, a_gon};
// ---- clock 2: font ROM
reg [7:0] font [0:4095];
reg [7:0] glyph;
initial $readmemh(FONT_FILE, font);
always @(posedge clk) glyph <= font[{ram_data[7:0], line}];
reg [7:0] glyph;
reg [17:0] c_cell;
reg [3:0] c_gfx, 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 ? gfx_pix : 4'd0;
{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)
@@ -99,42 +133,44 @@ module text_display #(
endcase
endfunction
wire visible = h < H_VIS && v < V_VIS;
wire cursor = cur_vis && frame[5] && tcol == cur_col && srow == cur_row;
wire ink = (glyph[~h[2:0]] | (ram_data[16] && line == 4'd15)) && !(ram_data[17] && frame[5]);
wire fg = !blank_line && (ink ^ cursor);
wire [3:0] idx = fg ? ram_data[11:8] : gfx_pix != 4'd0 ? gfx_pix :
blank_line ? 4'd0 : ram_data[15:12];
// ---- 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_gfx != 4'd0 ? c_gfx :
c_blank || !c_text ? 4'd0 : c_cell[15:12];
always @(posedge clk) begin
if (tick) begin
{red, green, blue} <= visible ? palette(idx) : 12'h000;
hsync <= !(h >= H_VIS + H_FP && h < H_VIS + H_FP + H_SYNC);
vsync <= !(v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC);
end
{red, green, blue} <= c_vis ? palette(idx) : 12'h000;
hsync <= c_hs;
vsync <= c_vs;
end
endmodule
// Two 2400-cell character buffers (the normal and the alternate screen; the
// top address bit picks one): read/write port for the terminal engine,
// read-only port for video.
// 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 [12:0] a_addr,
input a_alt,
input [13:0] a_addr,
input a_we,
input [17:0] a_wdata,
output reg [17:0] a_rdata,
input [12:0] b_addr,
input vclk,
input b_alt,
input [13:0] b_addr,
output reg [17:0] b_rdata
);
(* ram_style = "block" *) reg [17:0] mem [0:8191];
localparam PAGE = 132 * 64;
(* ram_style = "block" *) reg [17:0] mem [0:2 * PAGE - 1];
integer i;
initial for (i = 0; i < 8192; i = i + 1) mem[i] = {10'h00A, 8'h20};
initial for (i = 0; i < 2 * PAGE; i = i + 1) mem[i] = {10'h00A, 8'h20};
wire [14:0] aa = {1'b0, a_addr} + (a_alt ? PAGE : 0);
wire [14:0] ba = {1'b0, b_addr} + (b_alt ? PAGE : 0);
always @(posedge clk) begin
if (a_we) mem[a_addr] <= a_wdata;
a_rdata <= mem[a_addr];
if (a_we) mem[aa] <= a_wdata;
a_rdata <= mem[aa];
end
always @(posedge clk) b_rdata <= mem[b_addr];
always @(posedge vclk) b_rdata <= mem[ba];
endmodule
+45 -17
View File
@@ -102,7 +102,7 @@ module tb_terminal;
function [7:0] cellraw(input integer rr, input integer cc);
reg [17:0] v;
begin
v = dut.screen.mem[(dut.alt_scr ? 4096 : 0) + rr * 80 + cc]; // the page on show
v = dut.screen.mem[(dut.alt_scr ? 8448 : 0) + rr * 132 + cc]; // the page on show
cellraw = v[7:0];
end
endfunction
@@ -193,15 +193,17 @@ module tb_terminal;
screen_pc = {cellraw(2, 4), cellraw(2, 5), cellraw(2, 6), cellraw(2, 7)};
endfunction
// capture one frame
reg [11:0] fb [0:640*480-1];
reg cap = 0, smp = 0;
reg [9:0] hh, vv;
always @(posedge clk) if (dut.video.tick) begin hh <= dut.video.h; vv <= dut.video.v; smp <= cap; end
always @(negedge clk) if (smp && hh < 640 && vv < 480) begin
fb[vv * 640 + hh] = {r, g, b};
smp = 0;
// capture one frame (1280x1024; the colour on the pins belongs to the
// position the display's counters had four clocks earlier)
reg [11:0] fb [0:1280*1024-1];
reg cap = 0;
reg [21:0] pd1, pd2, pd3, pd4;
always @(posedge dut.pix_clk) begin
pd1 <= {dut.video.h, dut.video.v};
pd2 <= pd1; pd3 <= pd2; pd4 <= pd3;
end
always @(negedge dut.pix_clk)
if (cap && pd4[21:11] < 1280 && pd4[10:0] < 1024) fb[pd4[10:0] * 1280 + pd4[21:11]] = {r, g, b};
task capture_frame(input [8*16-1:0] name);
integer fd, i;
@@ -209,24 +211,29 @@ module tb_terminal;
@(posedge vs);
cap = 1;
@(negedge vs);
@(posedge vs);
cap = 0;
fd = $fopen(name, "w");
$fwrite(fd, "P3\n640 480\n15\n");
for (i = 0; i < 640 * 480; i = i + 1)
$fwrite(fd, "P3\n1280 1024\n15\n");
for (i = 0; i < 1280 * 1024; i = i + 1)
$fwrite(fd, "%0d %0d %0d\n", fb[i][11:8], fb[i][7:4], fb[i][3:0]);
$fclose(fd);
$display("wrote %0s", name);
end
endtask
// check VGA timing on the outputs
// check VGA timing on the outputs: 1688 x 1066 pixel clocks (10 ns here)
realtime t_hs = 0, t_vs = 0, t_cmd;
always @(negedge hs) begin
if (t_hs != 0 && ($realtime - t_hs) != 32000) $display("HSYNC period %0t", $realtime - t_hs);
integer hs_bad = 0, vs_bad = 0;
always @(posedge hs) begin
if (t_hs != 0 && ($realtime - t_hs) != 16880) hs_bad = hs_bad + 1;
t_hs = $realtime;
end
always @(negedge vs) begin
if (t_vs != 0 && ($realtime - t_vs) != 16800000) $display("VSYNC period %0t", $realtime - t_vs);
always @(posedge vs) begin
if (t_vs != 0 && ($realtime - t_vs) != 16880 * 1066) begin
vs_bad = vs_bad + 1;
$display("VSYNC period %0t", $realtime - t_vs);
end
t_vs = $realtime;
end
@@ -291,13 +298,34 @@ module tb_terminal;
// ---- smooth scroll inside the region
uart_str("\033[18;1H\033[?4h\015\012r7");
capture_frame("frame_2.ppm"); // mid-scroll
#40_000_000;
#100_000_000;
expect_row(14, "r4");
expect_row(17, "r7");
expect_row(18, "below region");
uart_str("\033[?4l\033[r");
dump_screen;
// ---- 132 columns (DECCOLM): 132x64, cleared, then back to 80x32
uart_str("\033[?3h");
#1_000_000;
uart_str("132 columns\033[64;124Hcorner!\033[10;120H\033[6n");
expect_tx({8'h1B, "[10;120R"}, 9, "cursor report past column 99");
#200_000;
expect_row(0, "132 columns");
if ({cellraw(63, 123), cellraw(63, 129)} == {"c", "!"} && dut.wide)
$display("PASS 132x64: last row and column");
else begin $display("FAIL 132x64 corner: %c%c", cellraw(63, 123), cellraw(63, 129)); errors = errors + 1; end
capture_frame("frame_4.ppm");
uart_str("\033[?3l");
#1_000_000;
uart_str("\033[32;75Hend");
#200_000;
if (!dut.wide && cellraw(31, 74) == "e" && cellraw(0, 0) == " ")
$display("PASS back to 80x32, cleared");
else begin $display("FAIL back to 80x32"); errors = errors + 1; end
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
// ---- Z-80 BASIC as the console, input pasted over the UART
uart_str("\033[2J\033[H");
#100_000;
+6 -2
View File
@@ -1,8 +1,8 @@
# terminfo for the Basys 3 FPGA terminal: a VT220 with 16 ANSI colours,
# underline, blink, 30 lines.
# underline, blink, 80x32 (basys3) or 132x64 after ESC[?3h (basys3-w).
# tic -x -o ~/.terminfo terminfo/basys3.ti then export TERM=basys3
basys3|Basys 3 FPGA VT220 terminal with ANSI colour,
lines#30, cols#80,
lines#32, cols#80,
colors#16, pairs#256,
op=\E[39;49m,
setaf=\E[%?%p1%{8}%<%t3%p1%d%e9%p1%{8}%-%d%;m,
@@ -18,3 +18,7 @@ basys3|Basys 3 FPGA VT220 terminal with ANSI colour,
hpa=\E[%i%p1%dG, vpa=\E[%i%p1%dd,
cbt=\E[Z,
use=vt220,
basys3-w|Basys 3 FPGA terminal in 132-column mode,
lines#64, cols#132,
is2=\E[?3h, rs2=\E[?3h,
use=basys3,