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

+139 -45
View File
@@ -1,22 +1,109 @@
# Basys 3 RISC-V Computer # Basys 3 RISC-V Computer
The [Basys 3 Z-80 computer](https://git.barant.com/barant/Basys3Terminal) with A complete home computer on a Digilent Basys 3 FPGA board, built around a
the Z-80 replaced by a RISC-V processor written from scratch in Verilog. The RISC-V processor written from scratch in Verilog:
rest is the same machine: a VT220 terminal with ANSI colour on 1280×1024 VGA,
320×240 graphics in 16 colours with hardware scrolling and 32 sprites, a
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 - **RV32IMF processor at 90 MHz**: integer, multiply/divide and IEEE 754
to 80×32 in a font twice the size and `ESC [?3h` back (for a PC using the single-precision floating point in hardware, with traps, interrupts, hardware
terminal, `TERM=basys3-w` and `stty rows 64 cols 132`, from breakpoints and single step.
`terminfo/basys3.ti`). - **VT220 terminal** with ANSI colour on 1280×1024 VGA: 132 × 64 text, or 80 × 32
in a double-size font.
- **Graphics** over the text: 320 × 240 in 16 colours with hardware scrolling,
or 640 × 480 in two colours, plus 32 hardware sprites.
- **Sound**: a three-voice chip with noise and envelopes (PmodAMP2 on JA).
- **USB keyboard** on the board's USB host port, with a keys-held port for
games.
- **Files** saved in the board's configuration flash.
- **Software**: Dartmouth-style BASIC with graphics, sound and files, a
machine-code monitor with a RISC-V disassembler and assembler, and five
original games.
Flip **SW15** up and the terminal is the computer's console. BASIC and the The [BASIC manual](docs/basic-manual.md) ([PDF](docs/basic-manual.pdf))
monitor are described in [docs/basic-manual.md](docs/basic-manual.md); the describes the computer from the user's side: BASIC, graphics, sprites, sound,
terminal itself (SW15 down) works exactly as in the Z-80 project, whose README files, the monitor and the games.
covers it.
## Getting started
Download `basys3_terminal.bit` (or `basys3_terminal.mcs` to keep it in the
flash) from the [latest release](https://git.barant.com/barant/Basys3RiscV/releases),
or build it yourself (below).
1. Plug a VGA monitor, a USB keyboard, and optionally a PmodAMP2 into JA.
2. Load the bitstream with Vivado's Hardware Manager, or `scripts/build.sh
program` / `flash` from a checkout.
3. Flip **SW15 up**. The screen shows BASIC's banner and `READY`.
With SW15 down the board is a plain VT220 terminal on its USB serial port, for
a PC. The computer keeps running underneath, waiting at its next character of
output, so flipping back and forth loses nothing.
The games come in the release as `*.ihx` (the program) and `*.bas` (the
loader). From a checkout of this repository, on a PC connected to the board's
serial port:
```bash
tools/send-hex.py keep.ihx --save KEEPGAME --baud 3000000
```
```bash
tools/send-bas.py keep.bas --baud 3000000
```
then `SAVE "KEEP"` in BASIC, and `RUN "KEEP"` from then on.
## Controls
| Control | Function |
|---|---|
| SW1:0 | Serial speed: `00` 115200, `01` 9600, `10` 921600, `11` 3000000 |
| SW2 | Local echo |
| SW3 | Received LF also does CR |
| SW5:4 | Text colour at start-up: `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 `ESC[?4h`) |
| SW9 | Fast smooth scrolling: 15 lines a second instead of 7.5 |
| SW14 | Computer mode: link the PC's serial port too (PC input is typed into BASIC, BASIC's output is copied to the PC) |
| SW15 | Computer mode: the terminal is the computer's console |
| BTNC | Reset everything |
| BTNU | Clear the screen |
| BTND | Restart the computer only (or Ctrl-Alt-Del in computer mode) |
| Pause key | Stop the program in the machine-code monitor |
| LED7:0 | Last byte received |
| LED11:8 | VT220 keyboard LEDs L4..L1 (`ESC[n q`) |
| LED12–15 | Bell, Caps Lock, serial activity, keyboard activity |
| 7-segment | In computer mode, whatever a program writes to ports 33/32 (`OUT 32,n`); otherwise the last scan code and the last byte sent |
## The terminal
The terminal understands what a VT220 does, plus ANSI colour:
- Cursor movement, save and restore, `IND`/`NEL`/`RI`; insert and delete of
characters and lines, erase, scroll, insert mode.
- Scroll regions, origin mode, deferred auto-wrap, tab stops.
- DEC special graphics (drawn with the CP437 line characters).
- Bold, underline, blink, reverse, concealed; 16 foreground and background
colours (256-colour and RGB codes are mapped to the nearest).
- Cursor and keypad application modes, cursor on/off, smooth scrolling, the
alternate screen, `DECCOLM` (`ESC[?3h` 132 × 64, `ESC[?3l` 80 × 32).
- Replies to `DA`, secondary `DA` and `DSR`/`CPR`; `RIS`, `DECSTR`,
`DECALN`.
- XON/XOFF flow control, so a PC can send at full speed.
For a Linux login on the board, install the terminfo entry and run a getty
on the board's port (the second of its two, usually `/dev/ttyUSB1`):
```bash
tic -x -o ~/.terminfo terminfo/basys3.ti
```
```bash
sudo /usr/sbin/agetty -L 115200 ttyUSB1 basys3-w
```
`basys3-w` is the 132 × 64 entry (`stty rows 64 cols 132`); `basys3` is
80 × 32. Set SW6 up so Backspace sends DEL. Text is drawn as CP437 bytes, with
no UTF-8 decoding, so use `LANG=C` for clean line drawing.
## The processor (`rtl/rv32_cpu.v`) ## The processor (`rtl/rv32_cpu.v`)
@@ -33,9 +120,8 @@ It is a multi-cycle design with the next instruction fetched while the current
one decodes: 2 clocks for most instructions, 3 for taken branches, jumps, loads one decodes: 2 clocks for most instructions, 3 for taken branches, jumps, loads
and stores, 4 for multiplies, 35 for divides; BASIC averages about 2.5. and stores, 4 for multiplies, 35 for divides; BASIC averages about 2.5.
Floating-point add, subtract, multiply and the fused multiply-adds take 12 Floating-point add, subtract, multiply and the fused multiply-adds take 12
clocks, divide and square root 36. It runs at 90 MHz (the Z-80's clock) and clocks, divide and square root 36. It runs at 90 MHz and takes about 5,300
takes about 5,300 LUTs (3,000 of them the FPU), 1,900 flip-flops and 6 DSP LUTs (3,000 of them the FPU), 1,900 flip-flops and 6 DSP slices.
slices.
## The machine (`rtl/rv32_system.v`) ## The machine (`rtl/rv32_system.v`)
@@ -45,34 +131,42 @@ slices.
| 0002_0000–0002_7FFF | the monitor's ROM (32 KB of block RAM); the NMI enters at 20000h, traps at 20004h | | 0002_0000–0002_7FFF | the monitor's ROM (32 KB of block RAM); the NMI enters at 20000h, traps at 20004h |
| 0002_8000–0002_9FFF | the monitor's RAM (8 KB of distributed RAM, data only) | | 0002_8000–0002_9FFF | the monitor's RAM (8 KB of distributed RAM, data only) |
| FFFF_FE00–FFFF_FEFF | 32-bit registers: milliseconds, microseconds, the timer interrupt's compare, breakpoints | | FFFF_FE00–FFFF_FEFF | 32-bit registers: milliseconds, microseconds, the timer interrupt's compare, breakpoints |
| FFFF_FF00–FFFF_FFFF | the Z-80 machine's I/O ports: port *N* at FFFF_FF00 + *N*, one instruction from `x0` (`sb a0,-255(zero)` is `OUT (1),A`) | | FFFF_FF00–FFFF_FFFF | byte-wide I/O ports: port *N* at FFFF_FF00 + *N*, one instruction from `x0` (`sb a0,-255(zero)` writes port 1) |
The peripherals are the Z-80 machine's, unchanged in function: `term_ctrl.v`, The ports (listed at the top of `rv32_system.v` and in the manual) cover the
`text_display.v`, `sprites.v`, `sound.v`, `flash_spi.v`, `uart.v`, console, the millisecond counter, the 7-segment display, graphics, sprites,
`ps2_keyboard.v`, `sevenseg.v`. To make room in block RAM for the wider sound, the keys held down and the flash. The peripherals are `term_ctrl.v`
processor's memory, the text screen and the graphics framebuffer are each (the VT220), `text_display.v` (video), `sprites.v`, `sound.v`,
split into a power-of-two array and a small one (Vivado builds an array of `flash_spi.v`, `uart.v`, `ps2_keyboard.v` and `sevenseg.v`.
16,896 or 76,800 entries as 32K or 96K deep); that saves 10.5 block RAMs and
changes nothing else.
Resources at 90 MHz: see `build/utilization.rpt` and `build/timing.rpt` after a The graphics memory is 76,800 four-bit entries: 320 × 240 pixels in 16
build. colours, or, with port 38 set, 640 × 480 pixels at one bit each (four to an
entry; a plot reads the entry and writes it back with one bit changed). To
fit the block RAM, the text screen and the graphics are each split into a
power-of-two array and a small one, because Vivado builds an array of 16,896
or 76,800 entries as 32K or 96K deep. That saves 10.5 block RAMs. The design
uses 49.5 of the XC7A35T's 50.
## Software ## Software
All in C (plus a little assembly), compiled with GCC for `rv32im`: All in C (plus a little assembly), compiled with GCC:
- `basic/` — BASIC, ported from the Z-80 version: the same language, the same - `basic/` — BASIC, with single-precision arithmetic on the FPU (`rv32imf`,
manual. `lib/` has the few C library functions it needs (small string `ilp32f`), correctly rounded. `lib/` has the few C library functions it
functions, `setjmp`, and single-precision `sinf` … `sqrtf`), since newlib's needs (small string functions, `setjmp`, and `sinf` … `sqrtf`), since
bring in its stdio and malloc. BASIC and the games are compiled for the FPU newlib's bring in its stdio and malloc.
(`rv32imf`, `ilp32f`); its arithmetic is IEEE single precision, correctly - `monitor/` — the machine-code monitor: memory, registers (integer and
rounded (the Z-80's SDCC library wasn't, so the last digit of a result can floating point), step, breakpoints, a RISC-V disassembler and a two-pass
differ). assembler, Intel HEX in and out.
- `monitor/` — the machine-code monitor, with a RISC-V disassembler and a
two-pass assembler in place of the Z-80 ones.
- `games/` — The Iron Keep, Bastion Raid, Star Warden, Star Swarm and Crystal - `games/` — The Iron Keep, Bastion Raid, Star Warden, Star Swarm and Crystal
Cavern, recompiled; they load at A200h (`CALL 41472`). Cavern; they load at A200h (`CALL 41472`).
| BASIC on the board | |
|---|---|
| `Y = SIN(X)` | 37 µs |
| `Y = EXP(X)` | 45 µs |
| `X = I * 1.5` | 26 µs |
| An empty `FOR` loop, each time round | 8.3 µs |
## Building ## Building
@@ -91,19 +185,15 @@ scripts/build.sh
`scripts/build.sh program` loads the bitstream over JTAG; `flash` writes it to `scripts/build.sh program` loads the bitstream over JTAG; `flash` writes it to
the configuration flash. the configuration flash.
Games go onto the board as in the Z-80 project: `tools/send-hex.py
games/keep/keep.ihx --save KEEPGAME --baud 3000000`, then `tools/send-bas.py
games/keep/keep.bas` and `SAVE "KEEP"` in BASIC.
## Tests ## Tests
| | | | | |
|---|---| |---|---|
| `scripts/fputest.sh` | `rtl/rv32_fpu.v` against `tools/f32.py`, an exact model of RISC-V's single precision (itself checked against the PC's FPU), on random vectors: every F instruction, every rounding mode, special values, subnormals and near-cancellations | | `scripts/fputest.sh` | `rtl/rv32_fpu.v` against `tools/f32.py`, an exact model of RISC-V's single precision (itself checked against the PC's FPU), on random vectors: every F instruction, every rounding mode, special values, subnormals and near-cancellations |
| `scripts/cputest.sh` | the official [riscv-tests](https://github.com/riscv-software-src/riscv-tests) (`rv32ui`, `rv32um`, `rv32uf`, `rv32mi`, fetched into `third_party/`) and `tests/` (interrupts, the NMI and single step) on `rv32_cpu.v` in xsim, with random wait states on the data bus. Two riscv-tests don't apply: `ma_data` expects misaligned loads and stores to work (here they trap, as the spec allows) and `pmpaddr` needs PMP. | | `scripts/cputest.sh` | the official [riscv-tests](https://github.com/riscv-software-src/riscv-tests) (`rv32ui`, `rv32um`, `rv32uf`, `rv32mi`, fetched into `third_party/`) and `tests/` (interrupts, the NMI and single step) on `rv32_cpu.v` in xsim, with random wait states on the data bus. Two riscv-tests don't apply: `ma_data` expects misaligned loads and stores to work (here they trap, as the spec allows) and `pmpaddr` needs PMP. |
| `scripts/sim.sh` | the whole design in xsim: the terminal, BASIC, graphics, sprites, scrolling, flash files, the monitor (step, breakpoint, Pause, memory files, the assembler, Intel HEX), 3 Mbaud. `scripts/sim.sh quick` skips the terminal and the monitor. | | `scripts/sim.sh` | the whole design in xsim: the terminal, BASIC, both graphics modes, sprites, scrolling, flash files, the monitor (step, breakpoint, Pause, memory files, the assembler, Intel HEX), 3 Mbaud. `scripts/sim.sh quick` skips the terminal and the monitor; `wide` checks the 132 × 64 start-up. |
| `monitor/test-disasm.py`, `test-asm.py` | the disassembler against GNU objdump on thousands of random instructions, and the assembler against GNU as | | `monitor/test-disasm.py`, `test-asm.py` | the disassembler against GNU objdump on thousands of random instructions, and the assembler against GNU as |
| `basic/test-*.py`, `games/*/test-*.py` | BASIC's line editor, RENUM and string arrays, and each game, on `tools/rv32emu.py` (a Python RV32IMF emulator of the machine; `tools/boardemu.py` models the game hardware). They need `pyte` and Pillow. | | `basic/test-*.py`, `games/*/test-*.py` | BASIC's line editor, RENUM, string arrays and graphics modes, and each game, on `tools/rv32emu.py` (a Python RV32IMF emulator of the machine; `tools/boardemu.py` models the game hardware). They need `pyte` and Pillow. |
| `bench/` | the processor alone with 64 KB of RAM, for timing | | `bench/` | the processor alone with 64 KB of RAM, for timing |
## Toolchain ## Toolchain
@@ -112,3 +202,7 @@ games/keep/keep.bas` and `SAVE "KEEP"` in BASIC.
- RISC-V GCC: Vivado ships one for MicroBlaze V (`~/vivado/*/gnu/riscv/lin/bin/riscv64-unknown-elf-gcc`, - RISC-V GCC: Vivado ships one for MicroBlaze V (`~/vivado/*/gnu/riscv/lin/bin/riscv64-unknown-elf-gcc`,
GCC 13 with an `rv32im/ilp32` multilib), which `scripts/rv_env.sh` finds GCC 13 with an `rv32im/ilp32` multilib), which `scripts/rv_env.sh` finds
if there is no `riscv64-unknown-elf-gcc` on the PATH. `RV_PREFIX` overrides. if there is no `riscv64-unknown-elf-gcc` on the PATH. `RV_PREFIX` overrides.
## License
MIT; see [LICENSE](LICENSE).
BIN
View File
Binary file not shown.
+43 -20
View File
@@ -9,8 +9,8 @@
* statement typed without a line number runs at once. Ctrl-C stops a program. * statement typed without a line number runs at once. Ctrl-C stops a program.
* *
* Graphics: 320x240 in 16 colours over the text, origin top left, colour 0 * Graphics: 320x240 in 16 colours over the text, origin top left, colour 0
* transparent. CLS COLOR PLOT LINE RECT FILL CIRCLE and POINT(x,y); LOCATE and * transparent, or (SCREEN 1) 640x480 in two. CLS COLOR PLOT LINE RECT FILL
* INK place and colour text. * CIRCLE and POINT(x,y); LOCATE and INK place and colour text.
* *
* Files: SAVE "NAME", LOAD "NAME", RUN "NAME", DIR, DEL "NAME" keep programs * Files: SAVE "NAME", LOAD "NAME", RUN "NAME", DIR, DEL "NAME" keep programs
* in the spare part of the configuration flash (see the file system below). * in the spare part of the configuration flash (see the file system below).
@@ -53,10 +53,15 @@ static u8 scr_width(void) { return con_stat & 4 ? 132 : 80; }
#define gy_p IO8(0x32) #define gy_p IO8(0x32)
#define gpix IO8(0x33) /* write plots and moves right; read gives the colour */ #define gpix IO8(0x33) /* write plots and moves right; read gives the colour */
#define gfill IO8(0x34) #define gfill IO8(0x34)
static void g_at(u16 x, u8 y) { gx_lo = x; gx_hi = x >> 8; gy_p = y; } #define gmode IO8(0x38) /* bit 0: 640x480 in two colours (a game's fill ends it) */
#define gcols IO8(0x39) /* its colours: set pixels 3:0, clear ones 7:4 */
static void g_at(u16 x, u16 y) { gx_lo = x; gx_hi = x >> 8 | (y >> 8) << 2; gy_p = y; }
static void g_put(u8 c) { gpix = c; } static void g_put(u8 c) { gpix = c; }
static u8 g_get(void) { return gpix; } static u8 g_get(void) { return gpix; }
static void g_clear(u8 c) { gfill = c; while (gfill & 1); } /* clear to colour 0, sprites off, scroll to 0; the fill also ends 640x480 mode */
static void g_clear(u8 hi) { gfill = 0; while (gfill & 1); gmode = hi; }
static void g_colours(u8 set, u8 clr) { gcols = set | clr << 4; }
static u8 g_hires(void) { return gmode & 1; }
/* SPI flash: port 40h opens/ends a command (bit 1 reads back "refused"), /* SPI flash: port 40h opens/ends a command (bit 1 reads back "refused"),
41h shifts a byte. The hardware refuses to write below the file area. */ 41h shifts a byte. The hardware refuses to write below the file area. */
@@ -118,13 +123,14 @@ static void io_out(u8 p, u8 v) { (void)p; (void)v; }
static void close_all(void); static void close_all(void);
static void bye(void) { close_all(); exit(0); } static void bye(void) { close_all(); exit(0); }
static u8 scr_width(void) { return 80; } static u8 scr_width(void) { return 80; }
static u8 gmem[240][320]; static u8 gmem[480][640];
static u16 hgx; static u16 hgx, hgy, hgw = 320, hgh = 240;
static u8 hgy; static void g_at(u16 x, u16 y) { hgx = x; hgy = y; }
static void g_at(u16 x, u8 y) { hgx = x; hgy = y; } static void g_put(u8 c) { if (hgx < hgw && hgy < hgh) gmem[hgy][hgx] = hgw > 320 ? c != 0 : c; hgx++; }
static void g_put(u8 c) { if (hgx < 320 && hgy < 240) gmem[hgy][hgx] = c; hgx++; } static u8 g_get(void) { return hgx < hgw && hgy < hgh ? gmem[hgy][hgx] : 0; }
static u8 g_get(void) { return hgx < 320 && hgy < 240 ? gmem[hgy][hgx] : 0; } static void g_clear(u8 hi) { memset(gmem, 0, sizeof gmem); hgw = hi ? 640 : 320; hgh = hi ? 480 : 240; }
static void g_clear(u8 c) { memset(gmem, c, sizeof gmem); } static void g_colours(u8 set, u8 clr) { (void)set; (void)clr; }
static u8 g_hires(void) { return hgw > 320; }
/* flash file area kept in basic-flash.img */ /* flash file area kept in basic-flash.img */
#define HOST_BASE 0x240000UL #define HOST_BASE 0x240000UL
@@ -174,12 +180,17 @@ static struct chan ch[NCHAN];
static struct chan *chan_arg(u8 mode); static struct chan *chan_arg(u8 mode);
static u8 chan_line(struct chan *c); static u8 chan_line(struct chan *c);
static u8 ghi; /* 640x480 (SCREEN 1) */
static u16 gw = 320, gh = 240; /* the graphics' size */
/* follow the hardware's mode: a game run with CALL may have ended SCREEN 1 */
static void g_sync(void) { ghi = g_hires(); gw = ghi ? 640 : 320; gh = ghi ? 480 : 240; }
/* ----------------------------------------------------------------- tokens */ /* ----------------------------------------------------------------- tokens */
enum { enum {
T_LET = 0x80, T_PRINT, T_INPUT, T_IF, T_THEN, T_GOTO, T_GOSUB, T_RETURN, T_FOR, T_LET = 0x80, T_PRINT, T_INPUT, T_IF, T_THEN, T_GOTO, T_GOSUB, T_RETURN, T_FOR,
T_TO, T_STEP, T_NEXT, T_DIM, T_DEF, T_END, T_STOP, T_REM, T_READ, T_DATA, T_TO, T_STEP, T_NEXT, T_DIM, T_DEF, T_END, T_STOP, T_REM, T_READ, T_DATA,
T_RESTORE, T_ON, T_RANDOMIZE, T_POKE, T_OUT, T_RUN, T_LIST, T_NEW, T_SCRATCH, T_RESTORE, T_ON, T_RANDOMIZE, T_POKE, T_OUT, T_RUN, T_LIST, T_NEW, T_SCRATCH,
T_BYE, T_CLS, T_COLOR, T_PLOT, T_LINE, T_RECT, T_FILL, T_CIRCLE, T_LOCATE, T_INK, T_BYE, T_CLS, T_COLOR, T_PLOT, T_LINE, T_RECT, T_FILL, T_CIRCLE, T_LOCATE, T_INK, T_SCREEN,
T_SAVE, T_LOAD, T_DIR, T_DEL, T_DATE, T_OPEN, T_CLOSE, T_MONITOR, T_CALL, T_BSAVE, T_BLOAD, T_OUTPUT, T_APPEND, T_AND, T_OR, T_NOT, T_SIN, T_COS, T_TAN, T_ATN, T_EXP, T_LOG, T_ABS, T_SAVE, T_LOAD, T_DIR, T_DEL, T_DATE, T_OPEN, T_CLOSE, T_MONITOR, T_CALL, T_BSAVE, T_BLOAD, T_OUTPUT, T_APPEND, T_AND, T_OR, T_NOT, T_SIN, T_COS, T_TAN, T_ATN, T_EXP, T_LOG, T_ABS,
T_SQR, T_INT, T_RND, T_SGN, T_FN, T_TAB, T_PEEK, T_INP, T_LEN, T_VAL, T_ASC, T_EOF, T_SQR, T_INT, T_RND, T_SGN, T_FN, T_TAB, T_PEEK, T_INP, T_LEN, T_VAL, T_ASC, T_EOF,
T_CHRS, T_STRS, T_LEFTS, T_RIGHTS, T_MIDS, T_DATES, T_TIM, T_POINT, T_LE, T_GE, T_NE, T_RENUM, T_LAST T_CHRS, T_STRS, T_LEFTS, T_RIGHTS, T_MIDS, T_DATES, T_TIM, T_POINT, T_LE, T_GE, T_NE, T_RENUM, T_LAST
@@ -189,7 +200,7 @@ static const char *const kw[] = {
"LET", "PRINT", "INPUT", "IF", "THEN", "GOTO", "GOSUB", "RETURN", "FOR", "LET", "PRINT", "INPUT", "IF", "THEN", "GOTO", "GOSUB", "RETURN", "FOR",
"TO", "STEP", "NEXT", "DIM", "DEF", "END", "STOP", "REM", "READ", "DATA", "TO", "STEP", "NEXT", "DIM", "DEF", "END", "STOP", "REM", "READ", "DATA",
"RESTORE", "ON", "RANDOMIZE", "POKE", "OUT", "RUN", "LIST", "NEW", "SCRATCH", "RESTORE", "ON", "RANDOMIZE", "POKE", "OUT", "RUN", "LIST", "NEW", "SCRATCH",
"BYE", "CLS", "COLOR", "PLOT", "LINE", "RECT", "FILL", "CIRCLE", "LOCATE", "INK", "BYE", "CLS", "COLOR", "PLOT", "LINE", "RECT", "FILL", "CIRCLE", "LOCATE", "INK", "SCREEN",
"SAVE", "LOAD", "DIR", "DEL", "DATE", "OPEN", "CLOSE", "MONITOR", "CALL", "BSAVE", "BLOAD", "OUTPUT", "APPEND", "AND", "OR", "NOT", "SIN", "COS", "TAN", "ATN", "EXP", "LOG", "ABS", "SAVE", "LOAD", "DIR", "DEL", "DATE", "OPEN", "CLOSE", "MONITOR", "CALL", "BSAVE", "BLOAD", "OUTPUT", "APPEND", "AND", "OR", "NOT", "SIN", "COS", "TAN", "ATN", "EXP", "LOG", "ABS",
"SQR", "INT", "RND", "SGN", "FN", "TAB", "PEEK", "INP", "LEN", "VAL", "ASC", "EOF", "SQR", "INT", "RND", "SGN", "FN", "TAB", "PEEK", "INP", "LEN", "VAL", "ASC", "EOF",
"CHR$", "STR$", "LEFT$", "RIGHT$", "MID$", "DATE$", "TIM", "POINT", "<=", ">=", "<>", "RENUM" "CHR$", "STR$", "LEFT$", "RIGHT$", "MID$", "DATE$", "TIM", "POINT", "<=", ">=", "<>", "RENUM"
@@ -763,7 +774,8 @@ static float func(u8 t)
expect(','); expect(',');
y = to_int(expr()); y = to_int(expr());
expect(')'); expect(')');
if ((u16)x >= 320 || (u16)y >= 240) return 0; g_sync();
if ((u16)x >= gw || (u16)y >= gh) return 0;
g_at(x, y); g_at(x, y);
return g_get(); return g_get();
} }
@@ -1570,16 +1582,16 @@ static void hline(i16 x1, i16 x2, i16 y, u8 c)
{ {
i16 t; i16 t;
if (x1 > x2) { t = x1; x1 = x2; x2 = t; } if (x1 > x2) { t = x1; x1 = x2; x2 = t; }
if ((u16)y >= 240 || x2 < 0 || x1 > 319) return; if ((u16)y >= gh || x2 < 0 || x1 >= (i16)gw) return;
if (x1 < 0) x1 = 0; if (x1 < 0) x1 = 0;
if (x2 > 319) x2 = 319; if (x2 >= (i16)gw) x2 = gw - 1;
g_at(x1, y); g_at(x1, y);
for (; x1 <= x2; x1++) g_put(c); for (; x1 <= x2; x1++) g_put(c);
} }
static void pset(i16 x, i16 y, u8 c) static void pset(i16 x, i16 y, u8 c)
{ {
if ((u16)x < 320 && (u16)y < 240) { g_at(x, y); g_put(c); } if ((u16)x < gw && (u16)y < gh) { g_at(x, y); g_put(c); }
} }
static void line(i16 x1, i16 y1, i16 x2, i16 y2, u8 c) static void line(i16 x1, i16 y1, i16 x2, i16 y2, u8 c)
@@ -1623,12 +1635,23 @@ static void graphics(u8 t)
{ {
i16 a[5]; i16 a[5];
u8 n, c; u8 n, c;
g_sync();
switch (t) { switch (t) {
case T_CLS: case T_CLS:
out_c(27); out_c('['); out_c('2'); out_c('J'); out_c(27); out_c('['); out_c('2'); out_c('J');
out_c(27); out_c('['); out_c('H'); out_c(27); out_c('['); out_c('H');
col = 0; col = 0;
g_clear(0); g_clear(ghi);
break;
case T_SCREEN: /* SCREEN 0 | SCREEN 1 [, set [, clear]] */
n = int_args(a, 1, 3);
if ((u16)a[0] > 1 || (n > 1 && (u16)a[1] > 15) || (n > 2 && (u16)a[2] > 15))
error(E_FUNC);
ghi = a[0];
gw = ghi ? 640 : 320;
gh = ghi ? 480 : 240;
g_colours(n > 1 ? a[1] : 15, n > 2 ? a[2] : 0);
g_clear(ghi);
break; break;
case T_COLOR: case T_COLOR:
int_args(a, 1, 1); int_args(a, 1, 1);
@@ -1658,7 +1681,7 @@ static void graphics(u8 t)
y = a[1] < a[3] ? a[1] : a[3]; y = a[1] < a[3] ? a[1] : a[3];
y2 = a[1] < a[3] ? a[3] : a[1]; y2 = a[1] < a[3] ? a[3] : a[1];
if (y < 0) y = 0; if (y < 0) y = 0;
if (y2 > 239) y2 = 239; if (y2 >= (i16)gh) y2 = gh - 1;
for (; y <= y2; y++) hline(a[0], a[2], y, c); for (; y <= y2; y++) hline(a[0], a[2], y, c);
break; break;
} }
@@ -2311,7 +2334,7 @@ static void statement(void)
graphics(t); graphics(t);
break; break;
case T_CLS: case T_COLOR: case T_PLOT: case T_RECT: case T_CLS: case T_COLOR: case T_PLOT: case T_RECT:
case T_FILL: case T_CIRCLE: case T_LOCATE: case T_INK: case T_FILL: case T_CIRCLE: case T_LOCATE: case T_INK: case T_SCREEN:
graphics(t); graphics(t);
break; break;
default: default:
+2572 -2572
View File
File diff suppressed because it is too large. Load diff
+57
View File
@@ -0,0 +1,57 @@
#!/usr/bin/env python3
"""BASIC's graphics modes in tools/rv32emu.py (tools/boardemu.py models the
graphics): SCREEN 1 (640x480 in two colours) and back to SCREEN 0.
python3 test-screen.py
Needs pyte.
"""
import importlib.util
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
spec = importlib.util.spec_from_file_location("test_edit", os.path.join(HERE, "test-edit.py"))
te = importlib.util.module_from_spec(spec)
spec.loader.exec_module(te)
def main():
b = te.Basic()
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def last():
rows = [l.rstrip() for l in b.screen_text().display if l.strip()]
return rows[-2] if rows and rows[-1] == "READY" else rows[-1:]
CR = b"\r"
b.type(b"")
b.type(b"SCREEN 1,14,1" + CR)
check("SCREEN 1 turns on 640x480 with its colours", b.g_hires == 1 and b.g_cols == 0x1E)
b.type(b"LINE 0,0,639,479:CIRCLE 320,240,200:PLOT 600,470,0:PRINT POINT(639,479);POINT(320,40);POINT(1,0);POINT(700,10)" + CR)
check("POINT reads pixels past 320x240", last() == " 1 1 0 0")
h = b.hgfx
check("the line reaches the corner", h[0] == 1 and h[479 * 640 + 639] == 1 and h[240 * 640 + 320] == 1)
check("the circle is in the 640x480 plane", h[240 * 640 + 520] == 1 and h[440 * 640 + 320] == 1)
check("the 320x240 plane is untouched", not any(b.gfx))
b.type(b"FILL 600,460,639,479:PLOT 639,479,0:PRINT POINT(600,460);POINT(639,479)" + CR)
check("FILL and erasing with colour 0", last() == " 1 0")
b.type(b"CLS:PRINT POINT(320,40)" + CR)
check("CLS clears but stays in 640x480", b.g_hires == 1 and not any(b.hgfx) and last() == " 0")
b.type(b"SCREEN 0:PLOT 319,239,9:PRINT POINT(319,239);POINT(320,40)" + CR)
check("SCREEN 0 goes back to 320x240 in 16 colours", b.g_hires == 0 and last() == " 9 0")
b.type(b"SCREEN 2" + CR)
check("SCREEN 2 is an error", "?" in "".join(b.screen_text().display) or "ERROR" in "".join(b.screen_text().display))
# a game's fill ends 640x480 mode; BASIC follows the hardware
b.type(b"SCREEN 1:OUT 52,0:PLOT 319,239,5:PRINT POINT(319,239)" + CR)
check("after a plain fill BASIC is back in 320x240", b.g_hires == 0 and last() == " 5")
print("%d error(s)" % errors)
sys.exit(1 if errors else 0)
main()
+41 -10
View File
@@ -367,8 +367,8 @@ machine-code monitor (section 10). See section 9 for memory and ports.
### Graphics and text placement ### Graphics and text placement
`CLS`, `COLOR`, `PLOT`, `LINE`, `RECT`, `FILL`, `CIRCLE`, `LOCATE` and `INK` `CLS`, `SCREEN`, `COLOR`, `PLOT`, `LINE`, `RECT`, `FILL`, `CIRCLE`, `LOCATE`
are described in sections 7 and 8. and `INK` are described in sections 7 and 8.
## 6. Functions ## 6. Functions
@@ -409,7 +409,7 @@ A whole number from 1 to 6: `1 + INT(RND * 6)`.
| `TIM` | Seconds since the computer was reset, to the millisecond | | `TIM` | Seconds since the computer was reset, to the millisecond |
| `DATE$` | The date and time as `"2026-11-28 09:30:15"`, or `""` if not set (section 11) | | `DATE$` | The date and time as `"2026-11-28 09:30:15"`, or `""` if not set (section 11) |
| `EOF(N)` | -1 (true) when file *N* has been read to the end, else 0 (section 11) | | `EOF(N)` | -1 (true) when file *N* has been read to the end, else 0 (section 11) |
| `POINT(X, Y)` | Colour of the graphics pixel at *X, Y* (0 off screen) | | `POINT(X, Y)` | Colour of the graphics pixel at *X, Y* (0 off screen; 0 or 1 in `SCREEN 1`) |
| `PEEK(A)` | Byte at memory address *A* | | `PEEK(A)` | Byte at memory address *A* |
| `INP(P)` | Byte from I/O port *P* | | `INP(P)` | Byte from I/O port *P* |
@@ -422,11 +422,12 @@ A whole number from 1 to 6: `1 + INT(RND * 6)`.
## 7. Graphics ## 7. Graphics
The screen has a graphics layer **320 pixels wide and 240 high in 16 colours**. The screen has a graphics layer **320 pixels wide and 240 high in 16 colours**,
It is mixed with the 80×30 text: or, after `SCREEN 1`, **640 by 480 in two** (see below). It is mixed with the
text:
- **Coordinates:** *X* runs from 0 at the left to 319, and *Y* from 0 at the - **Coordinates:** *X* runs from 0 at the left to 319, and *Y* from 0 at the
top to 239. Each graphics pixel is a 2×2 block on the monitor. top to 239. Each graphics pixel is a 4×4 block on the monitor.
- **Mixing with text:** text characters are drawn on top of the graphics. The - **Mixing with text:** text characters are drawn on top of the graphics. The
graphics show through wherever the text screen is blank. graphics show through wherever the text screen is blank.
- **Colour 0** is "no graphics": it shows the text background, normally black. - **Colour 0** is "no graphics": it shows the text background, normally black.
@@ -478,6 +479,34 @@ by `COLOR` is used, initially 15 (white).
70 RECT 20, 120, 300, 230, 7 70 RECT 20, 120, 300, 230, 7
``` ```
### 640 × 480 in two colours
The same graphics memory holds 640 × 480 pixels at one bit each, twice the
detail across and down in two colours:
| Statement | |
|---|---|
| `SCREEN 1 [, S [, C]]` | 640 × 480: *X* 0–639, *Y* 0–479, each pixel a 2×2 block on the monitor. Set pixels show in colour *S* (15, white, if left out), clear ones in *C* (0, transparent, if left out) |
| `SCREEN 0` | Back to 320 × 240 in 16 colours (the mode at power-up) |
Both clear the graphics. In `SCREEN 1` every drawing statement sets pixels
in any colour but 0 and clears them in colour 0, so `COLOR`, `PLOT`, `LINE`,
`RECT`, `FILL` and `CIRCLE` work unchanged; `POINT` gives 1 or 0. `CLS` clears
the graphics and stays in the mode. With a clear colour other than 0 the whole
picture area is that colour, under the text.
```
10 SCREEN 1, 10
20 FOR A = 0 TO 6.28 STEP .02
30 LINE 320, 240, 320 + 300*COS(A), 240 + 230*SIN(3*A)
40 NEXT A
```
The mode doesn't scroll (the scroll registers and the border are ignored).
Sprites still work, on the 320 × 240 grid, and a sprite counts as touching
the graphics where a set pixel is within the 4 × 1 group of pixels under it. A
game started with `CALL` goes back to 320 × 240; BASIC notices.
### Animation ### Animation
Erase something by drawing it again in colour 0, then draw it in its new Erase something by drawing it again in colour 0, then draw it in its new
@@ -662,10 +691,12 @@ compare value, 10h and 14h the monitor's breakpoint addresses.
| 16–19 | the millisecond counter, low byte first (reading 16 freezes the other three) | — | | 16–19 | the millisecond counter, low byte first (reading 16 freezes the other three) | — |
| 32 | — | right two digits of the 7-segment display (hex) | | 32 | — | right two digits of the 7-segment display (hex) |
| 33 | — | left two digits of the 7-segment display | | 33 | — | left two digits of the 7-segment display |
| 48, 49, 50 | — | graphics X (low byte, high bit), Y | | 48, 49, 50 | — | graphics X low byte; X bits 8–9 (1, 2) and Y bit 8 (4); Y low byte |
| 51 | colour at X, Y | plot a pixel at X, Y and move right | | 51 | colour at X, Y (0 or 1 in 640 × 480) | plot a pixel at X, Y and move right (in 640 × 480 any colour but 0 sets it) |
| 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour; also hides the sprites and resets the scroll, unless 128 is added | | 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour; also hides the sprites, resets the scroll and goes back to 320 × 240, unless 128 is added |
| 53, 54, 55 | — | graphics scroll and border (section 7) | | 53, 54, 55 | — | graphics scroll and border (section 7) |
| 56 | bit 0: 640 × 480 mode | 1: 640 × 480 in two colours, 0: 320 × 240 in 16 |
| 57 | — | 640 × 480 colours: set pixels' colour + 16 × clear pixels' colour |
| 64 | bit 0: flash byte in progress; bit 1: last flash command refused | 1 selects the flash and starts a command, 0 ends it | | 64 | bit 0: flash byte in progress; bit 1: last flash command refused | 1 selects the flash and starts a command, 0 ends it |
| 65 | the byte the flash sent back during the last `OUT 65` | a byte to the flash | | 65 | the byte the flash sent back during the last `OUT 65` | a byte to the flash |
| 96 | why the monitor was entered (used by the monitor) | single step (used by the monitor) | | 96 | why the monitor was entered (used by the monitor) | single step (used by the monitor) |
@@ -1407,7 +1438,7 @@ while keeping the cave connected, mirrored and free of dead ends
| Arrays | A–Z, 1 or 2 subscripts, up to 6000 elements each | | Arrays | A–Z, 1 or 2 subscripts, up to 6000 elements each |
| User functions | FNA–FNZ, one argument each | | User functions | FNA–FNZ, one argument each |
| FOR nesting / GOSUB nesting | 16 / 32 | | FOR nesting / GOSUB nesting | 16 / 32 |
| Graphics | 320 × 240, 16 colours | | Graphics | 320 × 240 in 16 colours, or 640 × 480 in 2 |
| Sprites | 32 on screen, 64 shapes, 16 × 16 pixels each | | Sprites | 32 on screen, 64 shapes, 16 × 16 pixels each |
| Text | 80 × 32, or 132 × 64 | | Text | 80 × 32, or 132 × 64 |
| Files in flash | 64, 1,784 KB in all, 32 KB each | | Files in flash | 64, 1,784 KB in all, 32 KB each |
Binary file not shown.
+8 -5
View File
@@ -8,7 +8,7 @@
// SW1:0 baud rate: 00 115200, 01 9600, 10 921600, 11 3000000 (8N1) // SW1:0 baud rate: 00 115200, 01 9600, 10 921600, 11 3000000 (8N1)
// SW2 local echo // SW2 local echo
// SW3 received LF also does CR // 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) // SW6 Backspace sends DEL (7F) instead of BS (08)
// SW7 Enter sends CR LF instead of CR // SW7 Enter sends CR LF instead of CR
// SW8 smooth scrolling (also turned on by ESC [ ? 4 h) // SW8 smooth scrolling (also turned on by ESC [ ? 4 h)
@@ -94,9 +94,9 @@ module basys3_terminal #(
reg [3:0] def_fg; reg [3:0] def_fg;
always @(posedge clk) always @(posedge clk)
case (sw_r[5:4]) 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'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 2'b11: def_fg <= 4'd14; // bright cyan
endcase endcase
@@ -332,7 +332,8 @@ module basys3_terminal #(
wire sp_go, sp_buf, sp_on, pix_frame; wire sp_go, sp_buf, sp_on, pix_frame;
wire [8:0] scroll_x; wire [8:0] scroll_x;
wire [7:0] scroll_y; wire [7:0] scroll_y;
wire g_border; wire g_border, g_hires;
wire [7:0] g_cols;
wire [7:0] sp_line; wire [7:0] sp_line;
wire [8:0] sp_x; wire [8:0] sp_x;
wire [3:0] sp_pix; 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]), .seg_val(rv_seg), .halted(), .brk_key(brk_s[2] ^ brk_s[1]),
.vclk(pix_clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix), .vclk(pix_clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix),
.scroll_x(scroll_x), .scroll_y(scroll_y), .g_border(g_border), .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_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), .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]), .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), .clk(pix_clk), .rst(pix_rst_s[1]), .wide(wide),
.cur_row(cur_row), .cur_col(cur_col), .cur_vis(cur_vis), .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), .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), .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_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), .sp_pix(alt_scr ? 4'd0 : sp_pix),
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync), .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 // 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 // 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 // 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) // 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 // 34 out fill the whole graphics screen in bit 0 = fill busy
// (also turns the sprites off and the scroll back to 0, unless // (also turns the sprites off, the scroll back to 0 and the
// bit 7 is set) // 320x240 mode back on, unless bit 7 is set)
// 35 out scroll: buffer X shown at the left, bits 7:0 // 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 // 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 // columns each side, 2 rows top and bottom) to draw in unseen
// 37 out scroll: buffer Y shown at the top (0-239) // 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) // 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 // 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, // 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 [8:0] scroll_x,
output reg [7:0] scroll_y, output reg [7:0] scroll_y,
output reg g_border, 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) // sprites, on the video clock (see sprites.v)
input sp_go, input sp_go,
input [7:0] sp_line, input [7:0] sp_line,
@@ -254,7 +262,10 @@ module rv32_system #(
// ------------------------------------------------------------ graphics // ------------------------------------------------------------ graphics
// 76800 pixels: a 64K array and an 11264 one, so they take about 9.5 block // 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; localparam GFX_PIXELS = 320 * 240;
(* ram_style = "block" *) reg [3:0] gfx_lo [0:65535]; (* ram_style = "block" *) reg [3:0] gfx_lo [0:65535];
(* ram_style = "block" *) reg [3:0] gfx_hi [0:GFX_PIXELS - 65536 - 1]; (* 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; for (gi = 0; gi < GFX_PIXELS - 65536; gi = gi + 1) gfx_hi[gi] = 4'd0;
end end
reg [8:0] gx; reg [9:0] gx;
reg [7:0] gy; reg [8:0] gy;
reg [3:0] gfx_q_lo, gfx_q_hi, fill_c; reg [3:0] gfx_q_lo, gfx_q_hi, fill_c;
reg gfx_q_sel; reg gfx_q_sel;
reg [16:0] fill_a; reg [16:0] fill_a;
reg fill_busy; reg fill_busy;
wire [16:0] gxy = {gy, 8'd0} + {2'd0, gy, 6'd0} + {8'd0, gx}; wire [16:0] lxy = {gy[7:0], 8'd0} + {2'd0, gy[7:0], 6'd0} + {7'd0, gx}; // y*320 + x
wire plot = io_wr && io_addr == 8'h33 && gx < 9'd320 && gy < 8'd240; wire [16:0] hxy = {gy, 7'd0} + {2'd0, gy, 5'd0} + {9'd0, gx[9:2]}; // y*160 + x/4
wire [16:0] g_addr = fill_busy ? fill_a : gxy; wire plot = io_wr && io_addr == 8'h33 &&
wire g_we = fill_busy || plot; (g_hires ? gx < 10'd640 && gy < 9'd480 : gx < 10'd320 && gy < 9'd240);
wire [3:0] g_wd = fill_busy ? fill_c : io_wd[3:0]; 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] 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 always @(posedge clk) begin
if (g_we && !g_addr[16]) gfx_lo[g_addr[15:0]] <= g_wd; if (g_we && !g_addr[16]) gfx_lo[g_addr[15:0]] <= g_wd;
@@ -303,21 +327,28 @@ module rv32_system #(
always @(posedge clk) always @(posedge clk)
if (rst) begin if (rst) begin
fill_busy <= 1'b0; 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; scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0;
g_hires <= 1'b0; g_cols <= 8'h0F;
end else begin end else begin
if (io_wr) case (io_addr) if (io_wr) case (io_addr)
8'h30: gx[7:0] <= io_wd; 8'h30: gx[7:0] <= io_wd;
8'h31: gx[8] <= io_wd[0]; 8'h31: begin gx[9:8] <= io_wd[1:0]; gy[8] <= io_wd[2]; end
8'h32: gy <= io_wd; 8'h32: gy[7:0] <= io_wd;
8'h33: gx <= gx + 1'b1; 8'h33: gx <= gx + 1'b1;
8'h34: begin 8'h34: begin
fill_busy <= 1'b1; fill_a <= 17'd0; fill_c <= io_wd[3:0]; fill_busy <= 1'b1; fill_a <= 17'd0;
if (!io_wd[7]) begin scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0; end // 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 end
8'h35: scroll_x[7:0] <= io_wd; 8'h35: scroll_x[7:0] <= io_wd;
8'h36: begin scroll_x[8] <= io_wd[0]; g_border <= io_wd[1]; end 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'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: ; default: ;
endcase endcase
if (fill_busy) begin if (fill_busy) begin
@@ -439,8 +470,9 @@ module rv32_system #(
8'h11: io_din = ms_latch[15:8]; 8'h11: io_din = ms_latch[15:8];
8'h12: io_din = ms_latch[23:16]; 8'h12: io_din = ms_latch[23:16];
8'h13: io_din = ms_latch[31:24]; 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'h34: io_din = {7'd0, fill_busy};
8'h38: io_din = {7'd0, g_hires};
8'h40: io_din = fl_ctl_q; 8'h40: io_din = fl_ctl_q;
8'h41: io_din = fl_dat_q; 8'h41: io_din = fl_dat_q;
8'h60: io_din = {5'd0, why}; 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 // 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 // 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. // 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: // One pixel per clock, pipelined:
// clock 0 position -> row, column, glyph line (registered) // clock 0 position -> row, column, glyph line (registered)
// clock 1 character RAM and graphics reads // 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 [8:0] scroll_x, // graphics pixel shown at the top left
input [7:0] scroll_y, input [7:0] scroll_y,
input g_border, // hide 4 graphics columns each side, 2 rows top and bottom 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 // sprites: the line to prepare next, and the pixel on screen
output reg sp_go, output reg sp_go,
output reg [7:0] sp_line, 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 [8:0] gcol_s = h[10:2]; // graphics pixel on the screen, unscrolled
wire [7:0] grow_s = v[9:2]; wire [7:0] grow_s = v[9:2];
reg [16:0] a_gfx; 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 [8:0] a_spx;
reg a_spbuf; reg a_spbuf;
always @(posedge clk) begin 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_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_vs <= v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC;
a_gon <= v < 960; 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_ghide <= !g_hires && 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_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_spx <= h[10:2];
a_spbuf <= v[2]; a_spbuf <= v[2];
end end
@@ -141,6 +151,8 @@ module text_display #(
assign sp_buf = a_spbuf; assign sp_buf = a_spbuf;
assign sp_on = a_gon; assign sp_on = a_gon;
reg [3:0] b_line; reg [3:0] b_line;
reg [1:0] b_gbit;
always @(posedge clk) b_gbit <= a_gbit;
reg [2:0] b_bit; reg [2:0] b_bit;
reg b_blank, b_text, b_vis, b_cur, b_hs, b_vs, b_gon, b_ghide; reg b_blank, b_text, b_vis, b_cur, b_hs, b_vs, b_gon, b_ghide;
always @(posedge clk) always @(posedge clk)
@@ -158,7 +170,8 @@ module text_display #(
always @(posedge clk) begin always @(posedge clk) begin
glyph <= font[{ram_data[7:0], b_line}]; glyph <= font[{ram_data[7:0], b_line}];
c_cell <= ram_data; 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_spr <= sp_pix;
{c_line, c_bit, c_blank, c_text, c_vis, c_cur, c_hs, c_vs} <= {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}; {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]; (* ram_style = "block" *) reg [17:0] hi [0:2 * PAGE - 16384 - 1];
integer i; integer i;
initial begin initial begin
for (i = 0; i < 16384; i = i + 1) lo[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'h00A, 8'h20}; for (i = 0; i < 2 * PAGE - 16384; i = i + 1) hi[i] = {10'h00F, 8'h20};
end end
wire [14:0] aa = {1'b0, a_addr} + (a_alt ? PAGE : 0); wire [14:0] aa = {1'b0, a_addr} + (a_alt ? PAGE : 0);
+29
View File
@@ -496,6 +496,35 @@ module tb_terminal;
errors = errors + 1; errors = errors + 1;
end end
// ---- 640x480 in two colours: four pixels an entry, a plot changes one
// bit (500, 501 and 503 set, then 500 cleared), pixel (x, y) is screen
// pixel (2x, 2y), set pixels colour 14 and clear ones colour 1
uart_str({"CLS:SCREEN 1,14,1:LINE 0,0,639,479:PLOT 500,333:PLOT 501,333:",
"PLOT 503,333:PLOT 500,333,0:PRINT POINT(639,479);POINT(501,333);POINT(502,333)\015"});
wait_row(0, " 1 1 0");
#5_000_000;
capture_frame("frame_8.ppm");
if (dut.sys.g_hires && dut.sys.gfx(333 * 160 + 125) == 4'b1010 &&
dut.sys.gfx(0) == 4'b0001 && dut.sys.gfx(479 * 160 + 159) == 4'b1000 &&
fb[666*1280 + 1002] == 12'h5FF && fb[667*1280 + 1003] == 12'h5FF &&
fb[666*1280 + 1000] == 12'hA00 && fb[666*1280 + 1004] == 12'hA00 &&
fb[958*1280 + 1278] == 12'h5FF && fb[1000*1280 + 600] == 12'h000)
$display("PASS 640x480 graphics");
else begin
$display("FAIL 640x480 graphics: %b %b %b %h %h %h %h %h %h", dut.sys.gfx(333 * 160 + 125),
dut.sys.gfx(0), dut.sys.gfx(479 * 160 + 159), fb[666*1280 + 1002],
fb[667*1280 + 1003], fb[666*1280 + 1000], fb[666*1280 + 1004],
fb[958*1280 + 1278], fb[1000*1280 + 600]);
errors = errors + 1;
end
uart_str("SCREEN 0\015");
#5_000_000;
if (!dut.sys.g_hires) $display("PASS SCREEN 0");
else begin
$display("FAIL SCREEN 0 left 640x480 on");
errors = errors + 1;
end
// ---- files in the flash, and the bitstream protection // ---- files in the flash, and the bitstream protection
uart_str({"CLS:DATE \"2026-11-28 09:30\":NEW\01510 PRINT \"SAVED\";6*7\015SAVE \"TEST1\"\015NEW\015", uart_str({"CLS:DATE \"2026-11-28 09:30\":NEW\01510 PRINT \"SAVED\";6*7\015SAVE \"TEST1\"\015NEW\015",
"LOAD \"TEST1\"\015RUN\015DIR\015"}); "LOAD \"TEST1\"\015RUN\015DIR\015"});
+22 -6
View File
@@ -47,6 +47,8 @@ class Board(RV32):
self.frame_at = -1000 self.frame_at = -1000
self.joy = 0 self.joy = 0
self.gfx = bytearray(320 * 240) self.gfx = bytearray(320 * 240)
self.hgfx = bytearray(640 * 480) # 640x480 mode (port 38): 0 or 1 a pixel
self.g_cols = 0x0F
self.gxv = self.gyv = 0 self.gxv = self.gyv = 0
self.spr = [[0, 0, 0, 0] for _ in range(32)] # x, y, shape, ctl self.spr = [[0, 0, 0, 0] for _ in range(32)] # x, y, shape, ctl
self.sel = 0 self.sel = 0
@@ -70,24 +72,37 @@ class Board(RV32):
if p == 0x88: if p == 0x88:
return self.joy return self.joy
if p == 0x33: if p == 0x33:
if self.g_hires:
return self.hgfx[self.gyv * 640 + self.gxv] if self.gxv < 640 and self.gyv < 480 else 0
return self.gfx[self.gyv * 320 + self.gxv] if self.gxv < 320 and self.gyv < 240 else 0 return self.gfx[self.gyv * 320 + self.gxv] if self.gxv < 320 and self.gyv < 240 else 0
if p == 0x34: if p == 0x34:
return 0 return 0
if p == 0x38:
return self.g_hires
return 0xFF return 0xFF
def io_out(self, port, v): def io_out(self, port, v):
p = port & 0xFF p = port & 0xFF
if p == 1: if p == 1:
self.text.append(v) self.text.append(v)
elif p == 0x30: self.gxv = (self.gxv & 0x100) | v elif p == 0x30: self.gxv = (self.gxv & 0x300) | v
elif p == 0x31: self.gxv = (self.gxv & 0xFF) | (v & 1) << 8 elif p == 0x31:
elif p == 0x32: self.gyv = v self.gxv = (self.gxv & 0xFF) | (v & 3) << 8
self.gyv = (self.gyv & 0xFF) | (v & 4) << 6
elif p == 0x32: self.gyv = (self.gyv & 0x100) | v
elif p == 0x33: elif p == 0x33:
if self.gxv < 320 and self.gyv < 240: if self.g_hires:
if self.gxv < 640 and self.gyv < 480:
self.hgfx[self.gyv * 640 + self.gxv] = int(v != 0)
elif self.gxv < 320 and self.gyv < 240:
self.gfx[self.gyv * 320 + self.gxv] = v & 15 self.gfx[self.gyv * 320 + self.gxv] = v & 15
self.gxv = (self.gxv + 1) & 0x1FF self.gxv = (self.gxv + 1) & 0x3FF
elif p == 0x34: elif p == 0x34:
self.gfx[:] = bytes([v & 15]) * (320 * 240) if self.g_hires and v & 0x80:
self.hgfx[:] = bytes([int(v & 15 != 0)]) * (640 * 480)
else:
self.gfx[:] = bytes([v & 15]) * (320 * 240)
self.hgfx[:] = bytes(640 * 480)
if not v & 0x80: if not v & 0x80:
for s in self.spr: s[3] &= ~1 for s in self.spr: s[3] &= ~1
self.scroll_x = self.scroll_y = 0 self.scroll_x = self.scroll_y = 0
@@ -97,6 +112,7 @@ class Board(RV32):
self.scroll_x = (self.scroll_x & 0xFF) | (v & 1) << 8 self.scroll_x = (self.scroll_x & 0xFF) | (v & 1) << 8
self.border = bool(v & 2) self.border = bool(v & 2)
elif p == 0x37: self.scroll_y = v - 240 if v >= 240 else v elif p == 0x37: self.scroll_y = v - 240 if v >= 240 else v
elif p == 0x39: self.g_cols = v
elif p == 0x70: self.sel = v & 31 elif p == 0x70: self.sel = v & 31
elif p == 0x71: self.xst = (self.xst & 0x100) | v elif p == 0x71: self.xst = (self.xst & 0x100) | v
elif p == 0x72: self.xst = (self.xst & 0xFF) | (v & 1) << 8 elif p == 0x72: self.xst = (self.xst & 0xFF) | (v & 1) << 8
+7
View File
@@ -66,6 +66,7 @@ class RV32:
self.why = 0 self.why = 0
self.bp = [0, 0] self.bp = [0, 0]
self.bp_on = 0 self.bp_on = 0
self.g_hires = 0 # port 38: 640x480 graphics mode
@property @property
def sp(self): def sp(self):
@@ -137,6 +138,8 @@ class RV32:
return self.why return self.why
if port == 0x34: if port == 0x34:
return 0 # graphics fill: done at once return 0 # graphics fill: done at once
if port == 0x38:
return self.g_hires
return 0xFF return 0xFF
def io_out(self, port, v): def io_out(self, port, v):
@@ -152,6 +155,10 @@ class RV32:
self.nmi_pending, self.why_r = True, 2 self.nmi_pending, self.why_r = True, 2
elif port == 0x67: elif port == 0x67:
self.bp_on = v & 3 self.bp_on = v & 3
elif port == 0x38:
self.g_hires = v & 1
elif port == 0x34 and not v & 0x80:
self.g_hires = 0
self.io_out(port, v) self.io_out(port, v)
def millis(self): def millis(self):