Basys 3 RISC-V computer

The Basys 3 Z-80 computer with the Z-80 replaced by an RV32IM processor
written from scratch (rtl/rv32_cpu.v): multi-cycle, 90 MHz, CSRs and
traps, interrupts, two hardware triggers and a resumable NMI for the
monitor. Passes riscv-tests rv32ui/um/mi.

rv32_system.v keeps all the Z-80 machine's peripherals, with I/O port N
at FFFFFF00h + N, 80 KB of RAM with BASIC preloaded, and a 32 KB monitor
ROM. BASIC, the five games and the monitor (with an RV32 disassembler,
assembler, breakpoints and single step) are ported to RISC-V GCC. Python
emulator and tests in tools/; full-system simulation in sim/.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
brad ArantandClaude Opus 5.5 committed 2026-11-30 18:09:18 -08:00
commit 5930b958b4
103 files changed
+55677

No files matched your search

+12
View File
@@ -0,0 +1,12 @@
build/
sim/work/
sim/cpu/
.Xil/
*.jou
*.log
*.pb
__pycache__/
third_party/
*_out.txt
sim_time.txt
out/
+21
View File
@@ -0,0 +1,21 @@
MIT License
Copyright (c) 2026 Brad Arant
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
+103
View File
@@ -0,0 +1,103 @@
# Basys 3 RISC-V Computer
The [Basys 3 Z-80 computer](https://git.barant.com/barant/Basys3Terminal) with
the Z-80 replaced by a RISC-V processor written from scratch in Verilog. The
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.
Flip **SW15** up and the terminal is the computer's console. BASIC and the
monitor are described in [docs/basic-manual.md](docs/basic-manual.md); the
terminal itself (SW15 down) works exactly as in the Z-80 project, whose README
covers it.
## The processor (`rtl/rv32_cpu.v`)
RV32IM in machine mode: the base integer set, multiply/divide, CSRs, counters
(`cycle`, `time`, `instret`), `fence.i`, `ecall`/`ebreak`/`mret`/`wfi`, traps
and interrupts (`mtvec`, `mepc`, `mcause`, `mtval`; software, timer, external
and 16 local interrupt lines), two hardware triggers for execute, load and
store breakpoints, and a resumable NMI (after the Smrnmi extension: `mnepc`,
`mnstatus`, `mnret`) with a single-step input, for the monitor.
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
and stores, 4 for multiplies, 35 for divides; BASIC averages about 2.5. It
runs at 90 MHz (the Z-80's clock) and takes about 2,100 LUTs, 900 flip-flops
and 4 DSP slices.
## The machine (`rtl/rv32_system.v`)
| Addresses | |
|---|---|
| 0000_0000–0001_3FFF | RAM, 80 KB of block RAM, BASIC preloaded; writes below A000h (BASIC's code) are ignored |
| 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) |
| 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`) |
The peripherals are the Z-80 machine's, unchanged in function: `term_ctrl.v`,
`text_display.v`, `sprites.v`, `sound.v`, `flash_spi.v`, `uart.v`,
`ps2_keyboard.v`, `sevenseg.v`. To make room in block RAM for the wider
processor's memory, the text screen and the graphics framebuffer are each
split into a power-of-two array and a small one (Vivado builds an array of
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
build.
## Software
All in C (plus a little assembly), compiled with GCC for `rv32im`:
- `basic/` — BASIC, ported from the Z-80 version: the same language, the same
manual. `lib/` has the few C library functions it needs (small string
functions, `setjmp`, and single-precision `sinf` … `sqrtf`), since newlib's
bring in its stdio and malloc. Floating point is IEEE single precision from
libgcc, correctly rounded (the Z-80's SDCC library wasn't, so the last digit
of a result can differ).
- `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
Cavern, recompiled; they load at A200h (`CALL 41472`).
## Building
```bash
export VIVADO=~/vivado/2025.2/Vivado
```
```bash
basic/build.sh && monitor/build.sh && for g in keep raid warden swarm cavern; do games/build-game.sh $g; done
```
```bash
scripts/build.sh
```
`scripts/build.sh program` loads the bitstream over JTAG; `flash` writes it to
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
| | |
|---|---|
| `scripts/cputest.sh` | the official [riscv-tests](https://github.com/riscv-software-src/riscv-tests) (`rv32ui`, `rv32um`, `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. |
| `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 RV32IM 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 |
## Toolchain
- Vivado (2025.2 is the licensed install here).
- 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
if there is no `riscv64-unknown-elf-gcc` on the PATH. `RV_PREFIX` overrides.
+4096
View File
File diff suppressed because it is too large. Load diff
BIN
View File
Binary file not shown.
+2381
View File
File diff suppressed because it is too large. Load diff
+40
View File
@@ -0,0 +1,40 @@
/* BASIC: code and constants below A000h (ROM_TOP: the hardware ignores writes
there), the program area at A000h, then data. 12000h-12FFFh is left free for
machine code; the stack comes down from 13FF0h; 13FF8h-13FFFh hold the clock
(include/basys3.h). */
OUTPUT_ARCH("riscv")
ENTRY(_start)
MEMORY
{
ROM (rx) : ORIGIN = 0x00000, LENGTH = 0xA000
RAM (rw) : ORIGIN = 0x0A000, LENGTH = 0x8000
}
SECTIONS
{
.text : {
KEEP(*(.text.start))
*(.text .text.*)
*(.rodata .rodata.* .srodata .srodata.*)
. = ALIGN(4);
} > ROM
.arena (NOLOAD) : { KEEP(*(.arena)) } > RAM
.data : ALIGN(4) {
__data_start = .;
*(.data .data.*)
__global_pointer$ = . + 0x800;
*(.sdata .sdata.*)
. = ALIGN(4);
__data_end = .;
} > RAM AT > ROM
__data_load = LOADADDR(.data);
.bss (NOLOAD) : ALIGN(4) {
__bss_start = .;
*(.sbss .sbss.* .bss .bss.* COMMON)
. = ALIGN(4);
__bss_end = .;
} > RAM
ASSERT(__bss_end <= 0x12000, "BASIC's data runs into the machine-code area")
__stack_top = 0x13FF0;
__rom_end = __data_load + SIZEOF(.data);
/DISCARD/ : { *(.comment) *(.riscv.attributes) }
}
+16384
View File
File diff suppressed because it is too large. Load diff
Executable
+21
View File
@@ -0,0 +1,21 @@
#!/bin/bash
# Compile BASIC for RV32IM and write basic.bin and the RAM images basic.mem
# (0000-FFFF) and basic-hi.mem (10000-13FFF).
set -e
cd "$(dirname "$0")"
source ../scripts/rv_env.sh
mkdir -p out
CFLAGS="$RV_ARCH -Os -ffunction-sections -fdata-sections -fno-common -Wall -I../include"
${RV_PREFIX}gcc $CFLAGS -c -o out/crt0.o crt0.S
${RV_PREFIX}gcc $CFLAGS -c -o out/basic.o basic.c
${RV_PREFIX}gcc $CFLAGS -c -o out/string.o ../lib/string.c
${RV_PREFIX}gcc $CFLAGS -c -o out/mathf.o ../lib/mathf.c
${RV_PREFIX}gcc $CFLAGS -c -o out/setjmp.o ../lib/setjmp.S
${RV_PREFIX}gcc $RV_ARCH -nostdlib -nostartfiles -T basic.ld -Wl,--gc-sections -Wl,-Map,out/basic.map \
-Wl,--build-id=none -o out/basic.elf out/crt0.o out/basic.o out/string.o out/mathf.o out/setjmp.o -lgcc
${RV_PREFIX}objcopy -O binary -j .text -j .data out/basic.elf basic.bin
${RV_PREFIX}nm -n out/basic.elf > out/basic.sym
python3 ../tools/bin2mem.py basic.bin out/basic-all.mem 20480
head -n 16384 out/basic-all.mem > basic.mem
tail -n 4096 out/basic-all.mem > basic-hi.mem
${RV_PREFIX}size out/basic.elf
+31
View File
@@ -0,0 +1,31 @@
# Start-up for BASIC on the Basys 3 RISC-V computer: at the reset address 0.
# Copy the initialised data from ROM, clear the rest, point traps at the
# monitor and run main.
#include "basys3.h"
.section .text.start, "ax"
.globl _start
_start:
.option push
.option norelax
la gp, __global_pointer$
.option pop
la sp, __stack_top
li t0, MON_TRAP # illegal instructions etc. stop in the monitor
csrw mtvec, t0
la a0, __data_start # copy initialised data
la a1, __data_load
la a2, __data_end
1: bgeu a0, a2, 2f
lw t0, 0(a1)
sw t0, 0(a0)
addi a0, a0, 4
addi a1, a1, 4
j 1b
2: la a0, __bss_start # clear uninitialised data
la a2, __bss_end
3: bgeu a0, a2, 4f
sw zero, 0(a0)
addi a0, a0, 4
j 3b
4: call main
5: j 5b
+69
View File
@@ -0,0 +1,69 @@
10 REM GRAPHICS DEMO FOR THE BASYS 3 Z-80
20 REM COLOUR BARS, CIRCLES, LINES, CURVES, SHAPES, ANIMATION, MANDELBROT
30 S=TIM
100 CLS
110 INK 15: LOCATE 3,30: PRINT "BASYS 3 Z-80 GRAPHICS"
120 INK 7: LOCATE 5,26: PRINT "320 X 240 PIXELS IN 16 COLOURS"
130 FOR C=1 TO 15
140 FILL C*20-10,100,C*20+7,180,C
150 INK C: LOCATE 25,C*5-1: PRINT C;
160 NEXT C
170 GOSUB 9000
200 CLS: INK 15: PRINT "CIRCLE"
210 C=0
220 FOR R=3 TO 117 STEP 3
230 GOSUB 8000: CIRCLE 160,120,R,C
240 NEXT R
250 GOSUB 9000
300 CLS: INK 15: PRINT "LINE"
310 C=0
320 FOR X=0 TO 319 STEP 8: GOSUB 8000: LINE 160,120,X,0,C: NEXT X
330 FOR Y=0 TO 239 STEP 8: GOSUB 8000: LINE 160,120,319,Y,C: NEXT Y
340 FOR X=319 TO 0 STEP -8: GOSUB 8000: LINE 160,120,X,239,C: NEXT X
350 FOR Y=239 TO 0 STEP -8: GOSUB 8000: LINE 160,120,0,Y,C: NEXT Y
360 GOSUB 9000
400 CLS: INK 10: PRINT "Y = SIN(X)": INK 12: PRINT "Y = COS(X)"
410 LINE 0,120,319,120,8: LINE 160,0,160,239,8
420 FOR X=0 TO 319
430 PLOT X,120-90*SIN((X-160)/25),10
440 PLOT X,120-90*COS((X-160)/25),12
450 NEXT X
460 GOSUB 9000
500 CLS: INK 15: PRINT "FILL AND RECT": RANDOMIZE
510 FOR I=1 TO 120
520 X=INT(RND*290): Y=16+INT(RND*200): C=1+INT(RND*15)
530 IF RND<.5 THEN FILL X,Y,X+5+INT(RND*30),Y+5+INT(RND*20),C: GOTO 550
540 RECT X,Y,X+5+INT(RND*30),Y+5+INT(RND*20),C
550 NEXT I
560 GOSUB 9000
600 CLS: INK 15: PRINT "ANIMATION"
610 RECT 0,16,319,239,7
620 X=40: Y=60: U=3: V=2
630 FOR I=1 TO 500
640 CIRCLE X,Y,6,0
650 X=X+U: Y=Y+V
660 IF X<8 OR X>311 THEN U=-U
670 IF Y<24 OR Y>231 THEN V=-V
680 CIRCLE X,Y,6,11: PLOT X,Y,14
690 T=TIM
700 IF TIM-T<.01 THEN 700
710 NEXT I
720 GOSUB 9000
800 CLS: INK 15: PRINT "MANDELBROT SET": M=TIM
810 FOR J=0 TO 59: FOR I=0 TO 79
820 A=I*.04-2.2: B=J*.04-1.2: X=0: Y=0
830 FOR K=1 TO 15
840 Z=X*X-Y*Y+A: Y=2*X*Y+B: X=Z
850 IF X*X+Y*Y>4 THEN 870
860 NEXT K
870 IF K>15 THEN K=0
880 FILL I*4,J*4,I*4+3,J*4+3,K
890 NEXT I: NEXT J
900 INK 7: LOCATE 30,1: PRINT "MANDELBROT TOOK";INT(TIM-M);"SECONDS.";
910 PRINT " WHOLE DEMO";INT(TIM-S);"SECONDS."
920 END
8000 C=C+1: IF C>15 THEN C=1
8010 RETURN
9000 T=TIM
9010 IF TIM-T<3 THEN 9010
9020 RETURN
+26
View File
@@ -0,0 +1,26 @@
10 REM SOUND DEMO - NEEDS A PMODAMP2 ON THE TOP ROW OF PMOD JA
20 REM PITCH = 125000 / FREQUENCY. PORTS: 144-149 PITCHES, 150 NOISE,
30 REM 151 MIXER, 152-154 VOLUMES, 155-157 ENVELOPES (ATTACK*16+DECAY)
40 PRINT "A SCALE"
50 OUT 151, 1: OUT 155, 3
60 FOR I = 1 TO 8
70 READ P: GOSUB 500
80 OUT 152, 15
90 IF INP(152) > 0 THEN 90
100 NEXT I
110 PRINT "A LASER"
120 OUT 151, 4: OUT 149, 0: OUT 157, 0: OUT 154, 13
130 FOR P = 30 TO 250 STEP 6: OUT 148, P: NEXT P
140 OUT 154, 0
150 PRINT "AN EXPLOSION"
160 OUT 151, 16: OUT 150, 24: OUT 156, 8: OUT 153, 15
170 IF INP(153) > 0 THEN 170
180 PRINT "A CHORD"
190 OUT 151, 7
200 OUT 144, 221: OUT 145, 1: OUT 146, 123: OUT 147, 1: OUT 148, 63: OUT 149, 1
210 OUT 155, 69: OUT 156, 69: OUT 157, 69
220 OUT 152, 12: OUT 153, 12: OUT 154, 12
230 IF INP(152) > 0 THEN 230
240 END
500 OUT 144, P - INT(P / 256) * 256: OUT 145, INT(P / 256): RETURN
900 DATA 477, 425, 379, 358, 319, 284, 253, 239
+54
View File
@@ -0,0 +1,54 @@
10 REM SPRITE DEMO FOR THE BASYS 3 Z-80
20 REM W A S D MOVE THE ROBOT, Q QUITS. SIX BALLS BOUNCE AROUND;
30 REM THE BORDER CHANGES COLOUR WHILE THE ROBOT TOUCHES ONE.
40 DEF FNH(A)=A-48-7*INT(A/65)
50 RANDOMIZE
100 CLS: INK 15: PRINT "SPRITES W A S D MOVE Q QUITS"
110 RECT 0,16,319,239,8
120 FOR S=0 TO 1: GOSUB 8000: NEXT S
200 N=6: DIM X(6),Y(6),U(6),V(6)
210 FOR I=1 TO N
220 X(I)=20+INT(RND(1)*270): Y(I)=30+INT(RND(1)*180)
230 U(I)=2+INT(RND(1)*3): V(I)=1+INT(RND(1)*3)
240 OUT 112,I: OUT 116,1: OUT 117,1
250 NEXT I
260 P=152: Q=120: OUT 112,0: OUT 116,0: OUT 117,1
300 F=INP(120)
310 IF INP(0) AND 1 THEN GOSUB 7000
320 FOR I=1 TO N
330 X(I)=X(I)+U(I): IF X(I)<2 OR X(I)>301 THEN U(I)=-U(I): X(I)=X(I)+2*U(I)
340 Y(I)=Y(I)+V(I): IF Y(I)<18 OR Y(I)>221 THEN V(I)=-V(I): Y(I)=Y(I)+2*V(I)
350 OUT 112,I: OUT 113,X(I) AND 255: OUT 114,INT(X(I)/256): OUT 115,Y(I)
360 NEXT I
370 OUT 112,0: OUT 113,P AND 255: OUT 114,INT(P/256): OUT 115,Q
380 IF INP(128) AND 1 THEN H=H+1: RECT 0,16,319,239,1+(H AND 7)
390 IF INP(120)=F THEN 390
400 GOTO 300
7000 K=INP(1): IF K>96 THEN K=K-32
7010 IF K=87 AND Q>18 THEN Q=Q-4
7020 IF K=83 AND Q<220 THEN Q=Q+4
7030 IF K=65 AND P>2 THEN P=P-4: OUT 112,0: OUT 117,3
7040 IF K=68 AND P<300 THEN P=P+4: OUT 112,0: OUT 117,1
7050 IF K=81 THEN CLS: END
7060 RETURN
8000 REM LOAD SHAPE S FROM 16 ROWS OF 16 HEX DIGITS (0 = SEE-THROUGH)
8010 OUT 118,S
8020 FOR R=1 TO 16: READ A$
8030 FOR C=1 TO 15 STEP 2
8040 OUT 119,16*FNH(ASC(MID$(A$,C,1)))+FNH(ASC(MID$(A$,C+1,1)))
8050 NEXT C: NEXT R
8060 RETURN
9000 REM SHAPE 0: THE ROBOT
9010 DATA "00000BBBBBB00000","0000BBBBBBBB0000","000BB99BB99BB000"
9020 DATA "000BB99BB99BB000","000BBBBBBBBBB000","000BBB0000BBB000"
9030 DATA "0000BBBBBBBB0000","0000007777000000","0077777777777700"
9040 DATA "0770777777770770","0770777777770770","0770777777770770"
9050 DATA "0000777777770000","0000770000770000","0000770000770000"
9060 DATA "000FFF0000FFF000"
9100 REM SHAPE 1: A BALL
9110 DATA "00000EEEEEE00000","000EEEEEEEEEE000","00EEFFEEEEEEEE00"
9120 DATA "0EEFFFEEEEEEEEE0","0EEFFEEEEEEEEEE0","EEEEEEEEEEEEEEEE"
9130 DATA "EEEEEEEEEEEEEEEE","EEEEEEEEEEEEEEEE","EEEEEEEEEEEEEEEE"
9140 DATA "EEEEEEEEEEEEEEEE","0EEEEEEEEEEEEE60","0EEEEEEEEEEEE660"
9150 DATA "00EEEEEEEEEE6600","000EEEEEEE666000","0000066666600000"
9160 DATA "0000000000000000"
+135
View File
@@ -0,0 +1,135 @@
#!/usr/bin/env python3
"""Try BASIC's line editor in tools/rv32emu.py, with the screen kept by pyte.
python3 test-edit.py
Types lines the way the keyboard sends them (cursor keys as ESC [ C and so
on), checks what the screen shows while a line is edited, and checks the
program LIST prints afterwards. Needs pyte.
"""
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "tools"))
from boardemu import Board # noqa: E402
RIGHT, LEFT, UP = b"\x1b[C", b"\x1b[D", b"\x1b[A"
HOME, END, DEL = b"\x1b[1~", b"\x1b[4~", b"\x1b[3~"
BS, CTRL_U, CR = b"\x7f", b"\x15", b"\r"
class Basic(Board):
def __init__(self):
mem = open(os.path.join(HERE, "basic.bin"), "rb").read()
super().__init__(mem, start=0, rom_top=0xA000)
self.idle = 0
def io_in(self, port):
if port & 0xFF == 0 and not self.keys_in:
self.idle += 1
return super().io_in(port)
def io_out(self, port, v):
self.idle = 0 # printing reads the status port too
super().io_out(port, v)
def type(self, keys):
"""Send keys one at a time and run until BASIC waits for the next."""
for k in [keys[i:i + 1] for i in range(len(keys))]:
self.keys_in += k
self.idle = 0
while self.idle < 50:
self.step()
def main():
b = Basic()
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def screen():
return b.screen_text()
def lines():
return "\n".join(l.rstrip() for l in screen().display)
def row(r):
return screen().display[r].rstrip()
def cur():
s = screen()
return s.cursor.y, s.cursor.x
b.type(b"")
b.type(b'10 PRINT "HELLO"' + CR + b"20 GOTO 10" + CR)
b.type(b"PRINT PEEK(40961)" + CR) # line 10's number, low byte
check("the program still starts at A000h", lines().rstrip().endswith(" 10\n\nREADY"))
y = cur()[0]
b.type(b"10" + RIGHT)
check("right after 10 copies one character of line 10", row(y) == "10" and cur() == (y, 3))
b.type(RIGHT * 2)
check("each right copies one more", row(y) == "10 PR" and cur() == (y, 5))
b.type(END)
check("End copies the rest", row(y) == '10 PRINT "HELLO"' and cur() == (y, 16))
b.type(LEFT * 6) # cursor onto the H
check("left moves the cursor back", cur() == (y, 10))
b.type(b"BIG ")
check("typing inserts in the middle", row(y) == '10 PRINT "BIG HELLO"' and cur() == (y, 14))
b.type(HOME + RIGHT * 3 + DEL * 6)
check("Home and Delete", row(y) == '10 "BIG HELLO"' and cur() == (y, 3))
b.type(b"PRINT " + END + BS + b'!"')
check("End and Backspace", row(y) == '10 PRINT "BIG HELLO!"')
b.type(CR + b"LIST" + CR)
lst = lines()
check("the edited line is stored", '10 PRINT "BIG HELLO!"\n20 GOTO 10' in lst)
b.type(UP)
y = cur()[0]
check("up brings back the last line, cursor at the end", row(y) == "LIST" and cur() == (y, 4))
b.type(CTRL_U)
check("Ctrl-U clears the line", row(y) == "" and cur() == (y, 0))
b.type(b"99" + RIGHT)
check("right after a missing line number does nothing", row(y) == "99" and cur() == (y, 2))
b.type(CTRL_U + b"20" + RIGHT * 6 + DEL * 2 + b"30" + CR)
b.type(b"LIST" + CR)
lst = lines().split("LIST")[-1]
check("Delete skips old characters while copying", "20 GOTO 30" in lst and "20 GOTO 10" not in lst)
b.type(b"20" + END + HOME + DEL * 2 + b"40" + CR + b"LIST" + CR)
check("a copied line can be stored under a new number", "20 GOTO 30\n40 GOTO 30" in lines())
# a line that wraps onto three screen rows
text = "REM " + "".join(chr(65 + i % 26) for i in range(170))
b.type(b"40 " + text.encode() + CR)
b.type(b"40" + END)
y, x = cur()
full = "40 " + text
y0 = y - (len(full) // 80)
shown = "".join(screen().display[y0 + i] for i in range(3))
check("a long line comes in over three rows", shown.rstrip() == full and (y, x) == (y0 + len(full) // 80, len(full) % 80))
b.type(HOME + RIGHT * 79)
check("right walks across a row end", cur() == (y0, 79))
b.type(RIGHT)
check("and onto the next row", cur() == (y0 + 1, 0))
b.type(b"*" + LEFT * 2 + DEL)
full = full[:79] + "*" + full[80:]
shown = "".join(screen().display[y0 + i] for i in range(3))
check("inserting and deleting across rows redraws them", shown.rstrip() == full and cur() == (y0, 79))
b.type(CR + b"LIST 40" + CR)
check("the long line is stored", full in "".join(screen().display))
b.type(UP + UP)
check("up again gives the same last line", lines().rstrip().endswith("LIST 40"))
b.type(CTRL_U)
b.type(b"NEW" + CR + b'INPUT A$: PRINT "GOT ";A$' + CR)
b.type(b"ABD" + LEFT + b"C" + CR)
check("INPUT uses the editor too", "GOT ABCD" in lines())
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()
+124
View File
@@ -0,0 +1,124 @@
#!/usr/bin/env python3
"""Try BASIC's RENUM in tools/rv32emu.py, with the screen kept by pyte.
python3 test-renum.py
Also checks number printing and reading, which were rewritten to make room.
"""
import importlib.util
import os
import sys
import functools
print = functools.partial(print, flush=True)
HERE = os.path.dirname(os.path.abspath(__file__))
spec = importlib.util.spec_from_file_location("te", os.path.join(HERE, "test-edit.py"))
te = importlib.util.module_from_spec(spec)
spec.loader.exec_module(te)
CR = te.CR
def main():
b = te.Basic()
errors = 0
def check(what, ok, got=None):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
if not ok and got is not None:
print(" got: " + repr(got))
errors += not ok
def screen():
return "\n".join(l.rstrip() for l in b.screen_text().display)
def cmd(text):
"""Type a command and return what it printed."""
b.type(b"CLS" + CR)
b.type(text.encode() + CR)
out = screen().split(text, 1)[-1]
return out.replace("\nREADY", "").strip()
def load(prog):
b.type(b"NEW" + CR)
for line in prog.strip().splitlines():
b.type(line.strip().encode() + CR)
def listing():
return [l for l in cmd("LIST").splitlines() if l]
b.type(b"")
load('''
5 REM GOTO 5 STAYS AS TYPED
7 GOSUB 200
12 IF X < 3 THEN 7
13 IF X = 3 GOTO 40
40 ON X - 2 GOTO 41, 13, 999
41 PRINT "GOTO 12"; X: END
200 X = X + 1: RETURN
300 END
''')
before = cmd("RUN")
cmd("RENUM")
check("RENUM numbers from 10 in steps of 10, fixing GOTO, GOSUB, THEN and ON",
listing() == ['10 REM GOTO 5 STAYS AS TYPED',
'20 GOSUB 70',
'30 IF X < 3 THEN 20',
'40 IF X = 3 GOTO 50',
'50 ON X - 2 GOTO 60, 40, 999',
'60 PRINT "GOTO 12"; X: END',
'70 X = X + 1: RETURN',
'80 END'], listing())
check("a reference to a missing line is left alone", "GOTO 60, 40, 999" in "\n".join(listing()))
check("the program runs the same afterwards", cmd("RUN") == before and before == "GOTO 12 3", before)
cmd("RENUM 1000, 5")
l = listing()
check("RENUM start, step", l[0].startswith("1000 ") and l[-1] == "1035 END" and
"1010 IF X < 3 THEN 1005" in l and "1020 ON X - 2 GOTO 1025, 1015, 999" in l, l)
cmd("RENUM 1, 1")
l = listing()
check("line numbers can get shorter", "3 IF X < 3 THEN 2" in l and "5 ON X - 2 GOTO 6, 4, 999" in l, l)
check("still runs", cmd("RUN") == "GOTO 12 3")
load('''
1 GOTO 2
2 RUN 3
3 GOSUB 1
''')
cmd("RENUM 10000, 10000")
check("RUN n is renumbered too", listing() == ["10000 GOTO 20000", "20000 RUN 30000", "30000 GOSUB 10000"], listing())
out = cmd("RENUM 60000, 3000")
check("numbers past 65535 are refused", "ERROR" in out.upper() or "ILLEGAL" in out.upper(), out)
check("and the program is unchanged", listing() == ["10000 GOTO 20000", "20000 RUN 30000", "30000 GOSUB 10000"], listing())
out = cmd("RENUM 10, 0")
check("step 0 is refused", listing()[0].startswith("10000"), out)
load("10 PRINT 1\n20 GOTO 10")
load("")
check("RENUM on an empty program is fine", cmd("RENUM") == "" and listing() == [])
# a line that would grow past 255 bytes
refs = ",".join(["1"] * 115) # typed lines stop at 250 characters
load("1 ON X GOTO " + refs)
out = cmd("RENUM 10000")
check("a line that would grow too long is refused", "LONG" in out.upper(), out)
check("and is left as it was", ("1 ON X GOTO " + refs) in cmd("LIST").replace("\n", ""))
# number printing and reading (rewritten to make room); the screen wraps
# at 80 columns, so compare the words only
def nums(text):
b.type(b"CLS" + CR)
b.type(text.encode() + CR)
return " ".join("".join(b.screen_text().display).split()).split(text, 1)[-1].replace("READY", "").strip()
out = nums('PRINT 0; 1; -1; .5; 1/3; .001; .0001; 123.456; 999999; 1000000; 1E30; -2.5E-12; 65535; 12345678')
check("numbers print as before",
out == "0 1 -1 .5 .333333 .001 1E-04 123.456 999999 1E+06 1E+30 -2.5E-12 65535 1.23457E+07", out)
out = nums("PRINT 1.5E3; 12.5E-1; 1234567890123; .000123; 3.; 00042.50")
check("numbers are read as before", out == "1500 1.25 1.23457E+12 1.23E-04 3 42.5", out)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()
+79
View File
@@ -0,0 +1,79 @@
#!/usr/bin/env python3
"""Try BASIC's string arrays in tools/rv32emu.py, with the screen kept by pyte.
python3 test-strarr.py
"""
import importlib.util
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
spec = importlib.util.spec_from_file_location("te", os.path.join(HERE, "test-edit.py"))
te = importlib.util.module_from_spec(spec)
spec.loader.exec_module(te)
CR = te.CR
def main():
b = te.Basic()
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def run(prog):
b.type(b"NEW" + CR)
for line in prog.strip().splitlines():
b.type(line.encode() + CR)
b.type(b"RUN" + CR)
return "\n".join(l.rstrip() for l in b.screen_text().display).split("RUN")[-1]
b.type(b"")
out = run('''
10 DIM N$(4), A(3)
20 FOR I = 0 TO 4: READ N$(I): NEXT I
30 A(2) = 7: A$ = "PLAIN"
40 FOR I = 4 TO 0 STEP -1: PRINT N$(I);",";: NEXT I
50 PRINT: PRINT A$; A(2); LEN(N$(1)); " "; LEFT$(N$(3), 2)
60 DATA RED, GREEN, BLUE, "YEL, LOW", WHITE
''')
check("DIM, READ and PRINT a string array", "WHITE,YEL, LOW,BLUE,GREEN,RED," in out)
check("plain A$ and A() kept apart", "PLAIN 7 5 YE" in out)
out = run('''
10 DIM B$(2,3)
20 B$(1,2) = "MID": B$(2,3) = B$(1,2) + "DLE"
30 PRINT B$(2,3); "/"; B$(0,0); "/"; LEN(B$(0,1))
40 C$(10) = "AUTO": PRINT C$(10)
50 IF B$(1,2) = "MID" THEN PRINT "EQUAL"
''')
check("two subscripts and joining", "MIDDLE// 0" in out)
check("undeclared string array gets 0..10", "AUTO" in out)
check("comparing an element", "EQUAL" in out)
out = run('''
10 INPUT "NAME"; N$(3)
20 PRINT "HI "; N$(3)
''')
b.type(b"ZED" + CR)
check("INPUT into an element", "HI ZED" in "\n".join(l.rstrip() for l in b.screen_text().display))
out = run('10 LINE INPUT L$(1): PRINT "<"; L$(1); ">"')
b.type(b"A, B" + CR)
check("LINE INPUT into an element", "<A, B>" in "\n".join(l.rstrip() for l in b.screen_text().display))
out = run('10 DIM Q$(2): Q$(3) = "X"')
check("subscript out of range", "SUBSCRIPT" in out.upper())
out = run('10 DIM Z$(300)')
check("too many elements is an error", "SUBSCRIPT" in out.upper())
out = run('10 DIM Z$(200), Y$(200)')
check("running out of memory is an error", "MEMORY" in out.upper())
out = run('10 DIM Z$(200): Z$(200) = "LAST": PRINT Z$(200)')
check("a 201-element string array fits", "LAST" in out)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()
+16
View File
@@ -0,0 +1,16 @@
# Bench program: the LEDs count up about 5 times a second at 90 MHz, using
# mul and div so they are exercised.
.text
.globl _start
_start:
li s0, 0x10000 # LED register
li s1, 0 # count
1: li t0, 3000000 # ~ 6 cycles per loop
2: addi t0, t0, -1
bnez t0, 2b
addi s1, s1, 1
li t1, 7
mul t2, s1, t1 # count * 7 / 7 = count
divu t2, t2, t1
sw t2, 0(s0)
j 1b
+12
View File
@@ -0,0 +1,12 @@
00010437
00000493
002dc2b7
6c028293
fff28293
fe029ee3
00148493
00700313
026483b3
0263d3b3
00742023
fddff06f
+34
View File
@@ -0,0 +1,34 @@
set_property -dict { PACKAGE_PIN W5 IOSTANDARD LVCMOS33 } [get_ports clk]
create_clock -add -name sys_clk_pin -period 10.00 -waveform {0 5} [get_ports clk]
set_property -dict { PACKAGE_PIN V17 IOSTANDARD LVCMOS33 } [get_ports {sw[0]}]
set_property -dict { PACKAGE_PIN V16 IOSTANDARD LVCMOS33 } [get_ports {sw[1]}]
set_property -dict { PACKAGE_PIN W16 IOSTANDARD LVCMOS33 } [get_ports {sw[2]}]
set_property -dict { PACKAGE_PIN W17 IOSTANDARD LVCMOS33 } [get_ports {sw[3]}]
set_property -dict { PACKAGE_PIN W15 IOSTANDARD LVCMOS33 } [get_ports {sw[4]}]
set_property -dict { PACKAGE_PIN V15 IOSTANDARD LVCMOS33 } [get_ports {sw[5]}]
set_property -dict { PACKAGE_PIN W14 IOSTANDARD LVCMOS33 } [get_ports {sw[6]}]
set_property -dict { PACKAGE_PIN W13 IOSTANDARD LVCMOS33 } [get_ports {sw[7]}]
set_property -dict { PACKAGE_PIN V2 IOSTANDARD LVCMOS33 } [get_ports {sw[8]}]
set_property -dict { PACKAGE_PIN T3 IOSTANDARD LVCMOS33 } [get_ports {sw[9]}]
set_property -dict { PACKAGE_PIN T2 IOSTANDARD LVCMOS33 } [get_ports {sw[10]}]
set_property -dict { PACKAGE_PIN R3 IOSTANDARD LVCMOS33 } [get_ports {sw[11]}]
set_property -dict { PACKAGE_PIN W2 IOSTANDARD LVCMOS33 } [get_ports {sw[12]}]
set_property -dict { PACKAGE_PIN U1 IOSTANDARD LVCMOS33 } [get_ports {sw[13]}]
set_property -dict { PACKAGE_PIN T1 IOSTANDARD LVCMOS33 } [get_ports {sw[14]}]
set_property -dict { PACKAGE_PIN R2 IOSTANDARD LVCMOS33 } [get_ports {sw[15]}]
set_property -dict { PACKAGE_PIN U16 IOSTANDARD LVCMOS33 } [get_ports {led[0]}]
set_property -dict { PACKAGE_PIN E19 IOSTANDARD LVCMOS33 } [get_ports {led[1]}]
set_property -dict { PACKAGE_PIN U19 IOSTANDARD LVCMOS33 } [get_ports {led[2]}]
set_property -dict { PACKAGE_PIN V19 IOSTANDARD LVCMOS33 } [get_ports {led[3]}]
set_property -dict { PACKAGE_PIN W18 IOSTANDARD LVCMOS33 } [get_ports {led[4]}]
set_property -dict { PACKAGE_PIN U15 IOSTANDARD LVCMOS33 } [get_ports {led[5]}]
set_property -dict { PACKAGE_PIN U14 IOSTANDARD LVCMOS33 } [get_ports {led[6]}]
set_property -dict { PACKAGE_PIN V14 IOSTANDARD LVCMOS33 } [get_ports {led[7]}]
set_property -dict { PACKAGE_PIN V13 IOSTANDARD LVCMOS33 } [get_ports {led[8]}]
set_property -dict { PACKAGE_PIN V3 IOSTANDARD LVCMOS33 } [get_ports {led[9]}]
set_property -dict { PACKAGE_PIN W3 IOSTANDARD LVCMOS33 } [get_ports {led[10]}]
set_property -dict { PACKAGE_PIN U3 IOSTANDARD LVCMOS33 } [get_ports {led[11]}]
set_property -dict { PACKAGE_PIN P3 IOSTANDARD LVCMOS33 } [get_ports {led[12]}]
set_property -dict { PACKAGE_PIN N3 IOSTANDARD LVCMOS33 } [get_ports {led[13]}]
set_property -dict { PACKAGE_PIN P1 IOSTANDARD LVCMOS33 } [get_ports {led[14]}]
set_property -dict { PACKAGE_PIN L1 IOSTANDARD LVCMOS33 } [get_ports {led[15]}]
+25
View File
@@ -0,0 +1,25 @@
# Timing build of the core alone: vivado -mode batch -source bench/build.tcl
# MULT/DIV environment variables set the clock (default 9/10 = 90 MHz).
set root [file normalize [file dirname [info script]]/..]
set out $root/build/bench
file mkdir $out
set mult [expr {[info exists ::env(MULT)] ? $::env(MULT) : 9.0}]
set div [expr {[info exists ::env(DIV)] ? $::env(DIV) : 10.0}]
puts "CPU clock: [expr {100.0 * $mult / $div}] MHz"
read_verilog $root/rtl/rv32_cpu.v $root/bench/cpu_bench.v
read_mem $root/bench/bench.mem
read_xdc $root/bench/bench.xdc
synth_design -top cpu_bench -part xc7a35tcpg236-1 -generic MULT=$mult -generic DIV=$div
opt_design -directive Explore
place_design -directive ExtraTimingOpt
phys_opt_design -directive AggressiveExplore
route_design -directive Explore
phys_opt_design -directive AggressiveExplore
report_utilization -file $out/utilization.rpt
report_utilization -hierarchical -file $out/util_hier.rpt
report_timing_summary -file $out/timing.rpt
report_timing -max_paths 20 -nworst 1 -unique_pins -file $out/paths.rpt
write_bitstream -force $out/cpu_bench.bit
set wns [get_property SLACK [get_timing_paths -max_paths 1 -setup]]
puts "Worst setup slack: $wns ns"
+80
View File
@@ -0,0 +1,80 @@
`timescale 1ns / 1ps
// Timing bench for rv32_cpu: the core with 64 KB of block RAM (instruction
// fetch on one port, loads and stores on the other) at 100 MHz * MULT / DIV.
// The LEDs show a register at 0x10000; switches 3:0 drive the interrupts.
module cpu_bench #(
parameter real MULT = 9.0,
parameter real DIV = 10.0,
parameter MEM_FILE = "bench.mem"
) (
input clk,
input [15:0] sw,
output reg [15:0] led
);
wire fb, cclk_m, cclk, locked;
MMCME2_BASE #(
.CLKIN1_PERIOD(10.0), .DIVCLK_DIVIDE(1),
.CLKFBOUT_MULT_F(MULT), .CLKOUT0_DIVIDE_F(DIV)
) mmcm (
.CLKIN1(clk), .CLKFBIN(fb), .CLKFBOUT(fb), .CLKFBOUTB(),
.CLKOUT0(cclk_m), .CLKOUT0B(), .CLKOUT1(), .CLKOUT1B(), .CLKOUT2(),
.CLKOUT2B(), .CLKOUT3(), .CLKOUT3B(), .CLKOUT4(), .CLKOUT5(), .CLKOUT6(),
.LOCKED(locked), .PWRDWN(1'b0), .RST(1'b0)
);
BUFG bufg (.I(cclk_m), .O(cclk));
reg [3:0] rst_sr = 4'hF;
always @(posedge cclk) rst_sr <= {rst_sr[2:0], !locked};
reg [3:0] sw_r;
always @(posedge cclk) sw_r <= sw[3:0];
wire i_en, d_req, d_we;
wire [31:0] i_addr, d_addr, d_wdata;
wire [3:0] d_wstrb;
reg [31:0] i_rdata, ram_q;
reg d_ack, io_sel;
rv32_cpu cpu (
.clk(cclk), .rst(rst_sr[3]),
.i_en(i_en), .i_addr(i_addr), .i_rdata(i_rdata),
.d_req(d_req), .d_we(d_we), .d_wstrb(d_wstrb), .d_addr(d_addr),
.d_wdata(d_wdata), .d_rdata(io_sel ? {16'd0, led} : ram_q), .d_ack(d_ack),
.irq_soft(sw_r[0]), .irq_timer(sw_r[1]), .irq_ext(sw_r[2]),
.irq_local({15'd0, sw_r[3]}), .nmi(1'b0), .nmi_step(1'b0), .nmi_taken(),
.dbg_pc(), .dbg_decode(), .retire()
);
// Reads use the core's address directly (a read with no request is
// harmless); stores are registered and written the next cycle, so the
// late d_req never reaches the block RAM's write enables.
reg [31:0] ram [0:16383];
initial $readmemh(MEM_FILE, ram);
wire ram_sel = d_addr[31:16] == 16'd0;
reg w_go;
reg [13:0] w_a;
reg [31:0] w_d;
reg [3:0] w_s;
integer b;
always @(posedge cclk) begin
if (i_en) i_rdata <= ram[i_addr[15:2]];
end
// one address for the data port: the pending store's, else the core's
wire [13:0] b_a = w_go ? w_a : d_addr[15:2];
always @(posedge cclk) begin
if (w_go) begin
for (b = 0; b < 4; b = b + 1)
if (w_s[b]) ram[b_a][8*b +: 8] <= w_d[8*b +: 8];
end
ram_q <= ram[b_a];
end
always @(posedge cclk) begin
w_go <= d_req && d_we && ram_sel;
w_a <= d_addr[15:2];
w_d <= d_wdata;
w_s <= d_wstrb;
d_ack <= d_req; // the store is written as the ack goes back
if (d_req) io_sel <= !ram_sel;
if (d_req && !ram_sel && d_we) led <= d_wdata[15:0];
end
endmodule
+117
View File
@@ -0,0 +1,117 @@
## Basys 3 (XC7A35T-1CPG236C) pins for basys3_terminal
## 100 MHz clock
set_property -dict { PACKAGE_PIN W5 IOSTANDARD LVCMOS33 } [get_ports clk]
create_clock -add -name sys_clk_pin -period 10.00 -waveform {0 5} [get_ports clk]
## Switches
set_property -dict { PACKAGE_PIN V17 IOSTANDARD LVCMOS33 } [get_ports {sw[0]}]
set_property -dict { PACKAGE_PIN V16 IOSTANDARD LVCMOS33 } [get_ports {sw[1]}]
set_property -dict { PACKAGE_PIN W16 IOSTANDARD LVCMOS33 } [get_ports {sw[2]}]
set_property -dict { PACKAGE_PIN W17 IOSTANDARD LVCMOS33 } [get_ports {sw[3]}]
set_property -dict { PACKAGE_PIN W15 IOSTANDARD LVCMOS33 } [get_ports {sw[4]}]
set_property -dict { PACKAGE_PIN V15 IOSTANDARD LVCMOS33 } [get_ports {sw[5]}]
set_property -dict { PACKAGE_PIN W14 IOSTANDARD LVCMOS33 } [get_ports {sw[6]}]
set_property -dict { PACKAGE_PIN W13 IOSTANDARD LVCMOS33 } [get_ports {sw[7]}]
set_property -dict { PACKAGE_PIN V2 IOSTANDARD LVCMOS33 } [get_ports {sw[8]}]
set_property -dict { PACKAGE_PIN T3 IOSTANDARD LVCMOS33 } [get_ports {sw[9]}]
set_property -dict { PACKAGE_PIN T2 IOSTANDARD LVCMOS33 } [get_ports {sw[10]}]
set_property -dict { PACKAGE_PIN R3 IOSTANDARD LVCMOS33 } [get_ports {sw[11]}]
set_property -dict { PACKAGE_PIN W2 IOSTANDARD LVCMOS33 } [get_ports {sw[12]}]
set_property -dict { PACKAGE_PIN U1 IOSTANDARD LVCMOS33 } [get_ports {sw[13]}]
set_property -dict { PACKAGE_PIN T1 IOSTANDARD LVCMOS33 } [get_ports {sw[14]}]
set_property -dict { PACKAGE_PIN R2 IOSTANDARD LVCMOS33 } [get_ports {sw[15]}]
## LEDs
set_property -dict { PACKAGE_PIN U16 IOSTANDARD LVCMOS33 } [get_ports {led[0]}]
set_property -dict { PACKAGE_PIN E19 IOSTANDARD LVCMOS33 } [get_ports {led[1]}]
set_property -dict { PACKAGE_PIN U19 IOSTANDARD LVCMOS33 } [get_ports {led[2]}]
set_property -dict { PACKAGE_PIN V19 IOSTANDARD LVCMOS33 } [get_ports {led[3]}]
set_property -dict { PACKAGE_PIN W18 IOSTANDARD LVCMOS33 } [get_ports {led[4]}]
set_property -dict { PACKAGE_PIN U15 IOSTANDARD LVCMOS33 } [get_ports {led[5]}]
set_property -dict { PACKAGE_PIN U14 IOSTANDARD LVCMOS33 } [get_ports {led[6]}]
set_property -dict { PACKAGE_PIN V14 IOSTANDARD LVCMOS33 } [get_ports {led[7]}]
set_property -dict { PACKAGE_PIN V13 IOSTANDARD LVCMOS33 } [get_ports {led[8]}]
set_property -dict { PACKAGE_PIN V3 IOSTANDARD LVCMOS33 } [get_ports {led[9]}]
set_property -dict { PACKAGE_PIN W3 IOSTANDARD LVCMOS33 } [get_ports {led[10]}]
set_property -dict { PACKAGE_PIN U3 IOSTANDARD LVCMOS33 } [get_ports {led[11]}]
set_property -dict { PACKAGE_PIN P3 IOSTANDARD LVCMOS33 } [get_ports {led[12]}]
set_property -dict { PACKAGE_PIN N3 IOSTANDARD LVCMOS33 } [get_ports {led[13]}]
set_property -dict { PACKAGE_PIN P1 IOSTANDARD LVCMOS33 } [get_ports {led[14]}]
set_property -dict { PACKAGE_PIN L1 IOSTANDARD LVCMOS33 } [get_ports {led[15]}]
## 7-segment display
set_property -dict { PACKAGE_PIN W7 IOSTANDARD LVCMOS33 } [get_ports {seg[0]}]
set_property -dict { PACKAGE_PIN W6 IOSTANDARD LVCMOS33 } [get_ports {seg[1]}]
set_property -dict { PACKAGE_PIN U8 IOSTANDARD LVCMOS33 } [get_ports {seg[2]}]
set_property -dict { PACKAGE_PIN V8 IOSTANDARD LVCMOS33 } [get_ports {seg[3]}]
set_property -dict { PACKAGE_PIN U5 IOSTANDARD LVCMOS33 } [get_ports {seg[4]}]
set_property -dict { PACKAGE_PIN V5 IOSTANDARD LVCMOS33 } [get_ports {seg[5]}]
set_property -dict { PACKAGE_PIN U7 IOSTANDARD LVCMOS33 } [get_ports {seg[6]}]
set_property -dict { PACKAGE_PIN V7 IOSTANDARD LVCMOS33 } [get_ports dp]
set_property -dict { PACKAGE_PIN U2 IOSTANDARD LVCMOS33 } [get_ports {an[0]}]
set_property -dict { PACKAGE_PIN U4 IOSTANDARD LVCMOS33 } [get_ports {an[1]}]
set_property -dict { PACKAGE_PIN V4 IOSTANDARD LVCMOS33 } [get_ports {an[2]}]
set_property -dict { PACKAGE_PIN W4 IOSTANDARD LVCMOS33 } [get_ports {an[3]}]
## Buttons
set_property -dict { PACKAGE_PIN U18 IOSTANDARD LVCMOS33 } [get_ports btnC]
set_property -dict { PACKAGE_PIN T18 IOSTANDARD LVCMOS33 } [get_ports btnU]
set_property -dict { PACKAGE_PIN U17 IOSTANDARD LVCMOS33 } [get_ports btnD]
## VGA
set_property -dict { PACKAGE_PIN G19 IOSTANDARD LVCMOS33 } [get_ports {vgaRed[0]}]
set_property -dict { PACKAGE_PIN H19 IOSTANDARD LVCMOS33 } [get_ports {vgaRed[1]}]
set_property -dict { PACKAGE_PIN J19 IOSTANDARD LVCMOS33 } [get_ports {vgaRed[2]}]
set_property -dict { PACKAGE_PIN N19 IOSTANDARD LVCMOS33 } [get_ports {vgaRed[3]}]
set_property -dict { PACKAGE_PIN N18 IOSTANDARD LVCMOS33 } [get_ports {vgaBlue[0]}]
set_property -dict { PACKAGE_PIN L18 IOSTANDARD LVCMOS33 } [get_ports {vgaBlue[1]}]
set_property -dict { PACKAGE_PIN K18 IOSTANDARD LVCMOS33 } [get_ports {vgaBlue[2]}]
set_property -dict { PACKAGE_PIN J18 IOSTANDARD LVCMOS33 } [get_ports {vgaBlue[3]}]
set_property -dict { PACKAGE_PIN J17 IOSTANDARD LVCMOS33 } [get_ports {vgaGreen[0]}]
set_property -dict { PACKAGE_PIN H17 IOSTANDARD LVCMOS33 } [get_ports {vgaGreen[1]}]
set_property -dict { PACKAGE_PIN G17 IOSTANDARD LVCMOS33 } [get_ports {vgaGreen[2]}]
set_property -dict { PACKAGE_PIN D17 IOSTANDARD LVCMOS33 } [get_ports {vgaGreen[3]}]
set_property -dict { PACKAGE_PIN P19 IOSTANDARD LVCMOS33 } [get_ports Hsync]
set_property -dict { PACKAGE_PIN R19 IOSTANDARD LVCMOS33 } [get_ports Vsync]
## USB-UART (FTDI), named from the FPGA's point of view
set_property -dict { PACKAGE_PIN B18 IOSTANDARD LVCMOS33 } [get_ports RsRx]
set_property -dict { PACKAGE_PIN A18 IOSTANDARD LVCMOS33 } [get_ports RsTx]
## USB HID host (PIC24) - keyboard as PS/2
set_property -dict { PACKAGE_PIN C17 IOSTANDARD LVCMOS33 PULLUP true } [get_ports PS2Clk]
set_property -dict { PACKAGE_PIN B17 IOSTANDARD LVCMOS33 PULLUP true } [get_ports PS2Data]
## Asynchronous / human-speed I/O
set_false_path -from [get_ports {btnC btnU btnD sw[*] RsRx PS2Clk PS2Data}]
# Configuration flash, used by the Z-80 for files (its clock goes through STARTUPE2)
set_property -dict { PACKAGE_PIN K19 IOSTANDARD LVCMOS33 } [get_ports QspiCSn]
set_property -dict { PACKAGE_PIN D18 IOSTANDARD LVCMOS33 } [get_ports {QspiDB[0]}]
set_property -dict { PACKAGE_PIN D19 IOSTANDARD LVCMOS33 } [get_ports {QspiDB[1]}]
set_property -dict { PACKAGE_PIN G18 IOSTANDARD LVCMOS33 } [get_ports {QspiDB[2]}]
set_property -dict { PACKAGE_PIN F18 IOSTANDARD LVCMOS33 } [get_ports {QspiDB[3]}]
# the SPI clock is slow (clk/4) and sampled mid-bit, so the pins need no timing
set_false_path -to [get_ports {QspiCSn QspiDB[*]}]
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 pix_mmcm/CLKOUT0]]
set_false_path -to [get_ports {led[*] seg[*] dp an[*] RsTx}]
## Pmod JA top row: a PmodAMP2 (AIN, GAIN, NC, SHUTDOWN)
set_property -dict { PACKAGE_PIN J1 IOSTANDARD LVCMOS33 } [get_ports {JA[0]}]
set_property -dict { PACKAGE_PIN L2 IOSTANDARD LVCMOS33 } [get_ports {JA[1]}]
set_property -dict { PACKAGE_PIN J2 IOSTANDARD LVCMOS33 } [get_ports {JA[2]}]
set_property -dict { PACKAGE_PIN G2 IOSTANDARD LVCMOS33 } [get_ports {JA[3]}]
set_false_path -to [get_ports {JA[*]}]
## Configuration
set_property CONFIG_VOLTAGE 3.3 [current_design]
set_property CFGBVS VCCO [current_design]
set_property BITSTREAM.GENERAL.COMPRESS TRUE [current_design]
set_property BITSTREAM.CONFIG.SPI_BUSWIDTH 4 [current_design]
set_property CONFIG_MODE SPIx4 [current_design]
+1398
View File
File diff suppressed because it is too large. Load diff
Binary file not shown.
+29
View File
@@ -0,0 +1,29 @@
#!/bin/bash
# Turn docs/basic-manual.md into docs/basic-manual.pdf (needs python3-markdown and Chrome).
set -e
cd "$(dirname "$0")"
python3 - <<'PY'
import markdown
body = markdown.markdown(open("basic-manual.md").read(), extensions=["tables", "fenced_code"])
css = """
@page { size: A4; margin: 18mm 16mm; }
body { font-family: "DejaVu Serif", Georgia, serif; font-size: 10.5pt; line-height: 1.45; color: #111; }
h1 { font-size: 24pt; margin: 0 0 4pt; }
h1 + p { font-size: 12pt; color: #444; margin-bottom: 18pt; }
h2 { font-size: 15pt; border-bottom: 1px solid #999; padding-bottom: 2pt; margin-top: 22pt;
page-break-after: avoid; }
h3 { font-size: 12pt; margin-top: 14pt; page-break-after: avoid; }
code, pre { font-family: "DejaVu Sans Mono", monospace; font-size: 9pt; }
pre { background: #f2f2f2; border-left: 3px solid #4a7; padding: 6pt 8pt; page-break-inside: avoid; }
table { border-collapse: collapse; margin: 6pt 0; page-break-inside: avoid; }
th, td { border: 1px solid #bbb; padding: 3pt 6pt; vertical-align: top; text-align: left; }
th { background: #e8e8e8; }
"""
open("basic-manual.html", "w").write(
"<!doctype html><html><head><meta charset='utf-8'><title>Basys 3 RISC-V BASIC</title>"
"<style>%s</style></head><body>%s</body></html>" % (css, body))
PY
google-chrome --headless --disable-gpu --no-pdf-header-footer \
--print-to-pdf="$PWD/basic-manual.pdf" "file://$PWD/basic-manual.html" 2>/dev/null
rm basic-manual.html
echo "wrote docs/basic-manual.pdf"
+4097
View File
File diff suppressed because it is too large. Load diff
+27
View File
@@ -0,0 +1,27 @@
#!/bin/bash
# Build one game: games/build-game.sh NAME -> games/NAME/NAME.ihx, Intel HEX
# for tools/send-hex.py. It runs at A200h, inside BASIC's program area
# (A000h-F0B7h) and clear of the loader program at its start.
set -e
cd "$(dirname "$0")"
source ../scripts/rv_env.sh
G=$1
mkdir -p $G/out
python3 $G/gen.py $G/out/data.h
CFLAGS="$RV_ARCH -Os -mno-relax -ffunction-sections -fdata-sections -fno-common -Wall -I../include -I$G/out"
${RV_PREFIX}gcc $CFLAGS -c -o $G/out/crt0.o crt0.S
${RV_PREFIX}gcc $CFLAGS -c -o $G/out/$G.o $G/$G.c
${RV_PREFIX}gcc $CFLAGS -c -o $G/out/string.o ../lib/string.c
${RV_PREFIX}gcc $RV_ARCH -nostdlib -nostartfiles -T game.ld -Wl,--no-relax -Wl,--gc-sections \
-Wl,--build-id=none -Wl,--no-warn-rwx-segments -Wl,-Map,$G/out/$G.map -o $G/out/$G.elf $G/out/crt0.o $G/out/$G.o $G/out/string.o -lgcc
${RV_PREFIX}objcopy -O ihex -j .text -j .data $G/out/$G.elf $G/$G.ihx
${RV_PREFIX}nm -n $G/out/$G.elf > $G/out/$G.sym
data=$(${RV_PREFIX}size -A $G/out/$G.elf | awk '$1 == ".data" {print $2}')
python3 - $G/out/$G.sym "${data:-0}" <<'PY'
import sys
s = {f[2]: int(f[0], 16) for f in (l.split() for l in open(sys.argv[1])) if len(f) == 3}
code_end, end = s["__bss_start"], s["__bss_end"]
print("code A200-%05X (%d bytes), variables to %05X" % (code_end - 1, code_end - 0xA200, end - 1))
assert end <= 0xF0B8, "the program runs past BASIC's program area"
assert sys.argv[2] == "0", "initialised variables are not supported"
PY
+4
View File
@@ -0,0 +1,4 @@
10 REM CRYSTAL CAVERN: LOAD THE GAME (MEMORY FILE CAVEGAME) AND PLAY IT
20 REM THE GAME RUNS AT A200H, PAST THIS PROGRAM, AND COMES BACK HERE
30 BLOAD "CAVEGAME": CALL 41472
40 CLS
+650
View File
@@ -0,0 +1,650 @@
/* Crystal Cavern: a maze chase game for the Basys 3 RISC-V computer.
You are a miner gathering crystals in a cave, chased by four bats. Each
hunts its own way: the red one comes straight for you, the magenta one
heads you off, the green one works with the red, and the orange one keeps
its distance. A lantern scares them for a while: catch them then and their
eyes fly back to the roost. Gold nuggets turn up for bonus points, and the
side tunnels wrap round. Gather every crystal to reach the next cave.
The maze is drawn into the graphics in 8 x 8 tiles; the miner, the bats and
the nugget are sprites. It runs at A200h, loaded by the CAVERN program in
BASIC. */
typedef unsigned char u8;
typedef signed char i8;
typedef unsigned short u16;
typedef short i16;
#include "basys3.h"
#define con_stat IO8(0x00)
#define con_data IO8(0x01)
#define gx_lo IO8(0x30)
#define gx_hi IO8(0x31)
#define gy_p IO8(0x32)
#define gpix IO8(0x33)
#define gfill IO8(0x34)
#define sp_sel IO8(0x70)
#define sp_xlo IO8(0x71)
#define sp_xhi IO8(0x72)
#define sp_y IO8(0x73)
#define sp_shape IO8(0x74)
#define sp_ctl IO8(0x75)
#define sp_load IO8(0x76)
#define sp_data IO8(0x77)
#define frame_ctr IO8(0x78)
#define joy IO8(0x88)
#include "data.h" /* out/data.h, made by gen.py */
#define J_UP 1
#define J_DOWN 2
#define J_LEFT 4
#define J_RIGHT 8
#define J_FIRE 16
#define J_ENTER 32
#define J_ESC 128
#define MX0 48 /* the maze's top left on the screen */
#define MY0 8
#define PW (MW * 8) /* maze width in pixels */
#define T_OPEN 0
#define T_WALL 1
#define T_CRYS 2
#define T_LAMP 3
#define T_DOOR 4
#define T_ROOST 5
#define T_TUNNEL 6
#define D_RIGHT 0
#define D_DOWN 1
#define D_LEFT 2
#define D_UP 3
#define D_NONE 4
static const i8 ddx[5] = { 1, 0, -1, 0, 0 };
static const i8 ddy[5] = { 0, 1, 0, -1, 0 };
/* bats */
#define NB 4
#define B_ROOST 0
#define B_LEAVE 1
#define B_HUNT 2
#define B_EYES 3
#define B_HOME 4 /* eyes back in the roost, going down */
#define DOOR_X 108 /* column 13's centre: the way in and out of the roost */
#define OUT_Y 76 /* row 9's centre, just above the door */
#define ROOM_Y 108 /* the roost's middle */
#define SP_MINER 0
#define SP_BAT 1 /* 4 */
#define SP_NUGGET 5
#define SP_BOOM 6
static const u8 bat_shape[NB] = { S_BAT_R, S_BAT_M, S_BAT_G, S_BAT_O };
static const i8 corner_x[NB] = { 26, 1, 26, 1 };
static const i8 corner_y[NB] = { -2, -2, 30, 30 };
static const u8 roost_x[NB] = { DOOR_X, DOOR_X, 92, 124 };
/* scatter and chase, in seconds: then chase for good */
static const u8 mode_secs[6] = { 7, 20, 7, 20, 5, 255 };
/* ---------------------------------------------------------------- state */
/* not static: test-cavern.py reads these from the link map */
u8 maze[MH][MW];
i16 mx, my; /* miner, maze pixels (tile centre = 8t + 4) */
u8 lives, level, dead, shield, left;
u16 score;
u8 bstate[NB];
i16 bx[NB], by[NB];
static u8 mdir, mwant, macc, manim;
static u8 bdir[NB], bacc[NB], bwait[NB];
static u8 fright, fright_flash, combo;
static u16 fright_time, mode_time;
static u8 mode, mode_i, eaten;
static u16 nugget_time;
static u8 nugget_on, boom_time, dead_time, lamp_blink;
static u16 next_extra;
static u8 last_frame, quit, hud_dirty;
static u16 rnd_state;
static const char *msg;
static u8 msg_time;
/* ----------------------------------------------------------------- text */
static void putch(char c) { while (!(con_stat & 2)); con_data = c; }
static void puts_(const char *s) { while (*s) putch(*s++); }
static void putn(u16 n, u8 width)
{
char b[6];
u8 i = 0;
do { b[i++] = '0' + n % 10; n /= 10; } while (n);
while (width > i) { putch('0'); width--; }
while (i) putch(b[--i]);
}
static void at(u8 r, u8 c) { putch(27); putch('['); putn(r, 0); putch(';'); putn(c, 0); putch('H'); }
static void ink(const char *s) { putch(27); putch('['); puts_(s); putch('m'); }
static void centre(u8 r, const char *s)
{
u8 n = 0;
while (s[n]) n++;
at(r, 41 - n / 2);
puts_(s);
}
static u16 rnd(void)
{
rnd_state ^= rnd_state << 7;
rnd_state ^= rnd_state >> 9;
rnd_state ^= rnd_state << 8;
return rnd_state;
}
static void wait_frame(void)
{
while (frame_ctr == last_frame);
last_frame = frame_ctr;
}
static u8 drain_keys(void)
{
u8 c, p = 0;
while (con_stat & 1) {
c = con_data;
if (c == 'p' || c == 'P') p = 1;
}
return p;
}
static void say(const char *s) { msg = s; msg_time = 120; hud_dirty = 1; }
static i16 absd(i16 v) { return v < 0 ? -v : v; }
/* ------------------------------------------------------------ the maze */
static u8 tile(i16 tx, i16 ty)
{
if (ty < 0 || ty >= MH) return T_WALL;
if (tx < 0) tx += MW;
if (tx >= MW) tx -= MW;
return maze[ty][tx];
}
static u8 is_open(u8 t) { return t == T_OPEN || t == T_CRYS || t == T_LAMP || t == T_TUNNEL; }
static void plot_row(u16 x, u8 y, const u8 *c)
{
u8 i;
gx_lo = (u8)x; gx_hi = (u8)(x >> 8); gy_p = y;
for (i = 0; i < 8; i++) gpix = c[i];
}
static u8 wall_ink;
/* Draw tile (tx, ty): walls are outlined where they meet a passage. */
static void draw_tile(u8 tx, u8 ty)
{
u8 t = maze[ty][tx], r, c, row[8], up, dn, lf, rt;
u16 x = MX0 + tx * 8;
u8 y = MY0 + ty * 8;
for (r = 0; r < 8; r++) {
for (c = 0; c < 8; c++) row[c] = 0;
if (t == T_WALL) {
up = !(tile(tx, ty - 1) == T_WALL);
dn = !(tile(tx, ty + 1) == T_WALL);
lf = !(tile(tx - 1, ty) == T_WALL) && tx > 0;
rt = !(tile(tx + 1, ty) == T_WALL) && tx < MW - 1;
for (c = 0; c < 8; c++)
if ((r == 1 && up) || (r == 6 && dn) || (c == 1 && lf) || (c == 6 && rt))
row[c] = wall_ink;
/* tidy the corners where two outlines meet */
if ((r < 1 && up) || (r > 6 && dn)) for (c = 0; c < 8; c++) row[c] = 0;
if (lf) row[0] = 0;
if (rt) row[7] = 0;
} else if (t == T_DOOR) {
if (r == 3 || r == 4) for (c = 0; c < 8; c++) row[c] = 13;
} else if (t == T_CRYS) {
if (r == 3 || r == 4) row[3] = row[4] = 14;
} else if (t == T_LAMP && lamp_blink) {
if (r >= 1 && r <= 6) for (c = (r == 1 || r == 6) ? 2 : 1; c < ((r == 1 || r == 6) ? 6 : 7); c++) row[c] = 11;
if (r == 3 || r == 4) row[3] = row[4] = 15;
}
plot_row(x, y + r, row);
}
}
static void draw_maze(void)
{
u8 x, y;
for (y = 0; y < MH; y++)
for (x = 0; x < MW; x++)
draw_tile(x, y);
}
static void draw_lamps(void)
{
u8 x, y;
for (y = 0; y < MH; y++)
for (x = 0; x < MW; x++)
if (maze[y][x] == T_LAMP) draw_tile(x, y);
}
static void reset_maze(void)
{
u8 x, y;
left = 0;
for (y = 0; y < MH; y++)
for (x = 0; x < MW; x++) {
maze[y][x] = maze_start[y][x];
if (maze[y][x] == T_CRYS || maze[y][x] == T_LAMP) left++;
}
}
/* -------------------------------------------------------------- sprites */
static void spr_at(u8 n, i16 x, i16 y, u8 shape, u8 ctl)
{
sp_sel = n;
if (x < -15 || x > 319 || y < -15 || y > 239) { sp_ctl = 0; return; }
sp_xlo = (u8)x; sp_xhi = (u8)(x >> 8) & 1; sp_y = (u8)y;
sp_shape = shape; sp_ctl = ctl;
}
static void spr_off(u8 n) { sp_sel = n; sp_ctl = 0; }
static void all_off(void) { u8 i; for (i = 0; i < 32; i++) spr_off(i); }
/* a figure at maze pixel (x, y); hidden in the tunnels beyond the maze */
static void figure(u8 n, i16 x, i16 y, u8 shape, u8 ctl)
{
if (x < 4 || x > PW - 4) { spr_off(n); return; }
spr_at(n, MX0 + x - 8, MY0 + y - 8, shape, ctl);
}
static void load_shapes(void)
{
const u8 *p = shape_data;
u8 s, i;
for (s = 0; s < N_SHAPES; s++) {
sp_load = s;
for (i = 0; i < 128; i++) sp_data = *p++;
}
}
/* ------------------------------------------------------------------ HUD */
static void hud(void)
{
hud_dirty = 0;
at(1, 1); ink("0;93"); puts_("CRYSTAL CAVERN ");
ink("0;37"); puts_("SCORE "); ink("0;97"); putn(score, 6);
ink("0;37"); puts_(" LIVES "); ink("0;97"); putn(lives, 0);
ink("0;37"); puts_(" CAVE "); ink("0;97"); putn(level, 0);
if (msg && msg_time) { ink("0;96"); puts_(" "); puts_(msg); }
puts_("\033[K");
ink("0");
}
static void add_score(u16 n)
{
score += n;
if (score >= next_extra) { next_extra += 10000; lives++; say("EXTRA LIFE"); }
hud_dirty = 1;
}
/* --------------------------------------------------------------- moving */
static u8 at_centre(i16 x, i16 y) { return (x & 7) == 4 && (y & 7) == 4; }
static void wrap(i16 *x)
{
if (*x < -8) *x += PW + 16;
else if (*x >= PW + 8) *x -= PW + 16;
}
static u8 can_go(i16 x, i16 y, u8 d)
{
return is_open(tile((x >> 3) + ddx[d], (y >> 3) + ddy[d]));
}
/* ---------------------------------------------------------------- miner */
static void miner_step(void)
{
u8 t;
i16 tx, ty;
if (mwant != D_NONE && mwant == ((mdir + 2) & 3)) mdir = mwant; /* turning back: any time */
if (at_centre(mx, my)) {
tx = mx >> 3; ty = my >> 3;
t = tile(tx, ty);
if (t == T_CRYS || t == T_LAMP) {
if (tx >= 0 && tx < MW) {
maze[ty][tx] = T_OPEN;
draw_tile((u8)tx, (u8)ty);
}
left--;
if (t == T_CRYS) add_score(10);
else {
add_score(50);
fright = 1; combo = 0;
fright_time = level < 7 ? 360 - 40 * level : 100;
{ u8 b; for (b = 0; b < NB; b++) if (bstate[b] == B_HUNT) bdir[b] = (bdir[b] + 2) & 3; }
}
eaten++;
}
if (mwant != D_NONE && can_go(mx, my, mwant)) mdir = mwant;
if (mdir == D_NONE || !can_go(mx, my, mdir)) return; /* against a wall */
}
if (mdir == D_NONE) return;
mx += ddx[mdir]; my += ddy[mdir];
wrap(&mx);
manim++;
}
static void miner(u8 j)
{
u8 spd;
if (j & J_LEFT) mwant = D_LEFT;
else if (j & J_RIGHT) mwant = D_RIGHT;
else if (j & J_UP) mwant = D_UP;
else if (j & J_DOWN) mwant = D_DOWN;
spd = 16 + (level < 5 ? level : 5);
macc += spd;
while (macc >= 16) { macc -= 16; miner_step(); }
figure(SP_MINER, mx, my, S_MINER + ((manim >> 2) & 1), mdir == D_LEFT ? 3 : 1);
}
/* ----------------------------------------------------------------- bats */
static void bat_target(u8 b, i16 *tx, i16 *ty)
{
i16 mtx = mx >> 3, mty = my >> 3, d;
u8 md = mdir == D_NONE ? D_LEFT : mdir;
if (bstate[b] == B_EYES) { *tx = 13; *ty = 9; return; }
if (!mode) { *tx = corner_x[b]; *ty = corner_y[b]; return; } /* scatter */
switch (b) {
case 0: *tx = mtx; *ty = mty; break;
case 1: *tx = mtx + 4 * ddx[md]; *ty = mty + 4 * ddy[md]; break;
case 2:
*tx = 2 * (mtx + 2 * ddx[md]) - (bx[0] >> 3);
*ty = 2 * (mty + 2 * ddy[md]) - (by[0] >> 3);
break;
default:
d = absd((bx[b] >> 3) - mtx) + absd((by[b] >> 3) - mty);
if (d > 8) { *tx = mtx; *ty = mty; }
else { *tx = corner_x[b]; *ty = corner_y[b]; }
}
}
/* At a junction: the way (not back) that ends nearest the target, or a
random one when frightened. */
static void bat_choose(u8 b)
{
i16 tx, ty, nx, ny, d, best = 32767;
u8 k, back = (bdir[b] + 2) & 3, pick = back, start;
if (fright && bstate[b] == B_HUNT) {
start = rnd() & 3;
for (k = 0; k < 4; k++) {
u8 dd = (start + k) & 3;
if (dd != back && can_go(bx[b], by[b], dd)) { bdir[b] = dd; return; }
}
bdir[b] = back;
return;
}
bat_target(b, &tx, &ty);
for (k = 0; k < 4; k++) {
if (k == back || !can_go(bx[b], by[b], k)) continue;
nx = (bx[b] >> 3) + ddx[k] - tx;
ny = (by[b] >> 3) + ddy[k] - ty;
d = absd(nx) + absd(ny);
/* break ties in the order up, left, down, right */
if (d < best || (d == best && (k == D_UP || (k == D_LEFT && pick != D_UP)))) { best = d; pick = k; }
}
bdir[b] = pick;
}
static void bat_step(u8 b)
{
switch (bstate[b]) {
case B_ROOST:
by[b] += (bwait[b] & 8) ? 1 : -1;
if (by[b] < ROOM_Y - 3) by[b] = ROOM_Y - 3;
if (by[b] > ROOM_Y + 3) by[b] = ROOM_Y + 3;
return;
case B_LEAVE:
if (bx[b] < DOOR_X) bx[b]++;
else if (bx[b] > DOOR_X) bx[b]--;
else if (by[b] > OUT_Y) by[b]--;
else { bstate[b] = B_HUNT; bdir[b] = D_LEFT; }
return;
case B_HOME:
if (by[b] < ROOM_Y) by[b]++;
else bstate[b] = B_LEAVE;
return;
}
if (at_centre(bx[b], by[b])) {
if (bstate[b] == B_EYES && bx[b] == DOOR_X && by[b] == OUT_Y) { bstate[b] = B_HOME; return; }
bat_choose(b);
}
bx[b] += ddx[bdir[b]]; by[b] += ddy[bdir[b]];
wrap(&bx[b]);
}
static void kill_miner(void)
{
if (dead || shield) return;
dead = 1; dead_time = 100;
boom_time = 60;
say("THE BATS GOT YOU");
}
static void bats(void)
{
u8 b, spd, s, n;
for (b = 0; b < NB; b++) {
bwait[b]++;
if (bstate[b] == B_ROOST) {
/* let them out by crystals eaten or by time */
if ((b == 1) || (b == 2 && (eaten >= 30 || bwait[b] > 240)) ||
(b == 3 && (eaten >= 60 || bwait[b] > 250 && eaten >= 20)))
bstate[b] = B_LEAVE;
}
if (bstate[b] == B_EYES || bstate[b] == B_HOME) spd = 40;
else if (bstate[b] == B_HUNT && fright) spd = 8;
else if (bstate[b] == B_HUNT && (by[b] >> 3) == TUNNEL_Y && ((bx[b] >> 3) < 6 || (bx[b] >> 3) > MW - 7)) spd = 8;
else spd = 14 + (level < 5 ? level : 5);
bacc[b] += spd;
while (bacc[b] >= 16) { bacc[b] -= 16; bat_step(b); }
if (!dead && bstate[b] == B_HUNT && absd(bx[b] - mx) < 6 && absd(by[b] - my) < 6) {
if (fright) {
bstate[b] = B_EYES;
add_score(200 << combo);
if (combo < 3) combo++;
} else kill_miner();
}
n = (bwait[b] >> 3) & 1;
if (bstate[b] == B_EYES || bstate[b] == B_HOME) s = S_EYES;
else if (fright && bstate[b] != B_ROOST && bstate[b] != B_LEAVE)
s = (fright_time < 120 && (fright_time & 8)) ? S_FLASH + n : S_SCARED + n;
else s = bat_shape[b] + n;
figure(SP_BAT + b, bx[b], by[b], s, 1);
}
}
static void modes(void)
{
if (fright) {
if (!--fright_time) fright = 0;
return;
}
if (mode_secs[mode_i] == 255) return;
if (++mode_time >= (u16)mode_secs[mode_i] * 60) {
u8 b;
mode_time = 0; mode_i++; mode ^= 1;
for (b = 0; b < NB; b++) if (bstate[b] == B_HUNT) bdir[b] = (bdir[b] + 2) & 3;
}
}
static void nugget(void)
{
if (!nugget_on && (eaten == 70 || eaten == 170) && nugget_time == 0) {
nugget_on = 1; nugget_time = 540;
}
if (nugget_time) {
nugget_time--;
if (!nugget_time) nugget_on = 0;
}
if (nugget_on && !dead && absd(mx - (START_X * 8 + 8)) < 6 && absd(my - (START_Y * 8 + 4)) < 6) {
nugget_on = 0; nugget_time = 1;
add_score(500 * level);
say("GOLD!");
}
if (nugget_on) figure(SP_NUGGET, START_X * 8 + 8, START_Y * 8 + 4, S_NUGGET, 1);
else spr_off(SP_NUGGET);
}
static void place_all(void)
{
u8 b;
mx = START_X * 8 + 8; my = START_Y * 8 + 4; /* between columns 13 and 14 */
mdir = D_LEFT; mwant = D_LEFT; macc = 0;
for (b = 0; b < NB; b++) {
bx[b] = roost_x[b]; by[b] = ROOM_Y; bacc[b] = 0; bwait[b] = b * 40;
bstate[b] = B_ROOST; bdir[b] = D_UP;
}
bx[0] = DOOR_X; by[0] = OUT_Y; bstate[0] = B_HUNT; bdir[0] = D_LEFT;
fright = 0; mode = 0; mode_i = 0; mode_time = 0;
nugget_on = 0; nugget_time = 0;
boom_time = 0; spr_off(SP_BOOM);
}
/* ------------------------------------------------------------- screens */
static void clear_all(void)
{
gfill = 0;
while (gfill & 1);
all_off();
}
static u8 wait_fire(void)
{
u8 j;
while (joy & (J_FIRE | J_ENTER | J_ESC)) { wait_frame(); drain_keys(); }
for (;;) {
wait_frame(); drain_keys(); rnd();
j = joy;
if (j & J_ESC) return 0;
if (j & (J_FIRE | J_ENTER)) return 1;
}
}
static void get_ready(void)
{
u8 i;
ink("1;93"); at(19, 36); puts_("GET READY"); ink("0");
figure(SP_MINER, mx, my, S_MINER, 3); /* everyone waits where they are */
for (i = 0; i < NB; i++) figure(SP_BAT + i, bx[i], by[i], bat_shape[i], 1);
for (i = 0; i < 100; i++) { wait_frame(); drain_keys(); }
at(19, 36); puts_(" ");
}
static u8 title(void)
{
u8 b;
clear_all();
puts_("\033[0m\033[2J");
reset_maze();
wall_ink = 12; lamp_blink = 1;
draw_maze();
place_all();
figure(SP_MINER, mx, my, S_MINER, 1);
for (b = 0; b < NB; b++) figure(SP_BAT + b, bx[b], by[b], bat_shape[b], 1);
ink("1;93"); centre(5, " C R Y S T A L C A V E R N ");
ink("0;97");
centre(8, " Gather every crystal in the cave, and keep away from the bats. ");
centre(9, " A lantern scares them for a while: then you can catch them. ");
centre(11, " ARROWS or W A S D move P pause ESC give up ");
ink("1;96"); centre(25, " PRESS SPACE TO BEGIN ");
ink("0");
return wait_fire();
}
static void play(void)
{
u8 j, i;
lives = 3; score = 0; level = 1; next_extra = 10000;
clear_all();
puts_("\033[2J");
for (;;) { /* each cave */
reset_maze();
wall_ink = 12; lamp_blink = 1;
draw_maze();
eaten = 0;
place_all();
dead = 0;
msg = 0; hud();
get_ready();
for (;;) { /* each frame */
wait_frame();
if (drain_keys()) {
at(1, 60); ink("0;93"); puts_("PAUSED - P TO GO ON\033[K"); ink("0");
for (;;) {
wait_frame();
if (joy & J_ESC) { quit = 1; return; }
if (drain_keys()) break;
}
hud_dirty = 1;
}
j = joy;
if (j & J_ESC) return;
if (!(last_frame & 15)) { lamp_blink ^= 1; draw_lamps(); }
if (!dead) {
miner(j);
modes();
bats();
nugget();
} else {
spr_at(SP_BOOM, MX0 + mx - 8, MY0 + my - 8, S_BOOM, (boom_time & 4) ? 1 : 0);
spr_off(SP_MINER);
if (boom_time) boom_time--;
if (!--dead_time) {
if (!--lives) {
hud();
ink("0;44;97");
centre(14, " ");
centre(15, " GAME OVER ");
centre(16, " ");
ink("0");
for (i = 0; i < 150; i++) { wait_frame(); drain_keys(); }
return;
}
place_all();
dead = 0;
hud_dirty = 1;
hud();
get_ready();
}
}
if (msg_time && !--msg_time) hud_dirty = 1;
if (hud_dirty) hud();
if (!left) break; /* every crystal gathered */
}
say("CAVE CLEARED");
hud();
for (i = 0; i < 8; i++) { /* the walls flash */
wall_ink = (i & 1) ? 12 : 15;
draw_maze();
for (j = 0; j < 12; j++) { wait_frame(); drain_keys(); }
}
all_off();
level++;
}
}
int main(void)
{
rnd_state = 0x1980 ^ frame_ctr;
last_frame = frame_ctr;
puts_("\033[?3l\033[?25l");
load_shapes();
quit = 0;
while (!quit) {
if (!title()) break;
play();
}
clear_all();
puts_("\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+738
View File
@@ -0,0 +1,738 @@
:10A2000097320000938282DF173300001303C316D6
:10A2100063F8620023A00200938242006FF05FFFA8
:10A2200017130000670003C2834700F093F727006D
:10A23000E38C07FEA300A0F067800000130101FF7C
:10A240002324810023261100130405000345040084
:10A25000631A05008320C10003248100130101015A
:10A26000678000001304140097000000E78000FCE2
:10A270006FF0DFFD130101FE232C8100232A9100E2
:10A28000232E110093840500130400009306A00000
:10A29000130690003377D5029307040093870701D4
:10A2A000B3872700130414001374F40F1307070374
:10A2B000238CE7FE930705003355D502E36CF6FCCB
:10A2C000636E9400631804028320C101032481019A
:10A2D000832441011301010267800000130500037C
:10A2E0009384F4FF97000000E78040F493F4F40FA8
:10A2F0006FF01FFD1304F4FF1374F40F93070401B0
:10A30000B387270003C587FF97000000E78000F2AE
:10A310006FF05FFB130101FF2322910093040500FE
:10A320001305B001232611002324810013840500A6
:10A3300097000000E78080EF1305B005970000004C
:10A34000E780C0EE9305000013850400970000002D
:10A35000E78080F21305B00397000000E78000ED6E
:10A36000130504009305000097000000E780C0F08B
:10A37000032481008320C10083244100130580044D
:10A380001301010117030000670043EA130101FFF5
:10A3900023248100130405001305B00123261100B6
:10A3A00097000000E78080E81305B00597000000E3
:10A3B000E780C0E71305040097000000E78040E84D
:10A3C000032481008320C1001305D006130101017D
:10A3D00017030000670083E5130101FF23248100B8
:10A3E0002326110013840500930700003307F400AF
:10A3F00003470700631A070293D7170093059002DB
:10A40000B385F54093F5F50F97000000E780C0F0A5
:10A4100013050400032481008320C10013010101FE
:10A42000170300006700C3E19387170093F7F70F46
:10A430006FF0DFFBB7D7000003D5070013177500D7
:10A440003345E50013150501135505011357950014
:10A450003345E500131785003345E5001315050165
:10A46000135505012390A70067800000B7D70000AF
:10A4700003C74700834680F7E38EE6FE034780F775
:10A480002382E700678000001305000093060005A3
:10A49000834700F093F7170063940700678000007C
:10A4A000034710F01377F70D6304D70093070500F7
:10A4B000138507006FF0DFFDB7D80000B7D50000A7
:10A4C00037D50000238F0802130600001303000095
:10A4D000930700009385C5CC930E100013058504E7
:10A4E000130FC001930FC03293060600130700003C
:10A4F0003388B600034E08001308EEFF1378F80FF8
:10A5000063E80E019387170093F7F70F130310000A
:10A510003388A600130717002300C8011377F70F2D
:10A5200093861600E316E7FD1306C601E31EF6FB4D
:10A5300063040300238FF80267800000130101FF0A
:10A54000B7D7000093051000130510002326110053
:10A55000238107002324810023229100970000001B
:10A56000E78080DB37C500001305851E97000000DB
:10A57000E78000E237C500001305051F97000000C3
:10A58000E78000CCB7C4000013854420970000008A
:10A59000E78000E037C500001305C52097000000E4
:10A5A000E78000CA37C4000013054421970000006B
:10A5B000E78000DEB7D7000003D5C703930560002E
:10A5C00097000000E78040CB1385442097000000EF
:10A5D000E78000DC37C500001305C52197000000A7
:10A5E000E78000C61305442197000000E78040DAA9
:10A5F000B7D7000003C527049305000097000000AB
:10A60000E78080C71385442097000000E78040D88A
:10A6100037C500001305852297000000E78040C27F
:10A620001305442197000000E78080D6B7D70000CB
:10A6300003C517049305000097000000E780C0C31E
:10A64000B7D7000003A4C7FF630E0402B7D700000A
:10A6500083C797FF6388070237C5000013054523AA
:10A6600097000000E780C0D237C500001305C5235E
:10A6700097000000E780C0BC1305040097000000AD
:10A68000E78000BC37C500001305052497000000D3
:10A69000E78000BB032481008320C10083244100A4
:10A6A00037C50000130545241301010117030000FD
:10A6B000670003CEB7D7000003D7C703B7D60000A3
:10A6C0003305E5001315050113550501239EA70267
:10A6D00083D766006360F50437270000130707710E
:10A6E000B387E70037D700002393F600834727049A
:10A6F000938717002301F704B7C7000037D700007E
:10A7000093878724232EF7FEB7D70000130780070F
:10A71000A38CE7FEB7D70000130710002381E700E2
:10A7200067800000B7D70000130700072393E704F2
:10A73000B7D700001307C00B2392E704930720004C
:10A7400037D70000A303F70237D700002303F7022F
:10A75000B7066C0037D70000A30207029386C6062F
:10A7600037D7000037067C002328D7021306C6051A
:10A77000130707032322C7003737507813070780D2
:10A7800037D60000232CE60037070303B7D50000B7
:10A790001307373037D5000023A0E502232C05022C
:10A7A000230CF5022380F502B7D70000A38B070026
:10A7B000B7D7000023880700B7D70000A38707009A
:10A7C000B7D700002399070037D70000B7D700009C
:10A7D00037066C00232E0700A385070037D700003B
:10A7E000B7D700001306C6042324C7022396070028
:10A7F00013078702B7D70000238507002322D7005D
:10A80000930760002308F0F6A30A00F667800000B3
:10A81000230A00F2834740F393F71700E39C07FEF7
:10A82000130700022308F0F693871700A30A00F627
:10A8300093F7F70FE398E7FE678000001397050191
:10A84000135707019307C00163E2E7046358050249
:10A850001305C50113150501135505411307C00163
:10A86000B385E502B7D7000093878704B387B700A5
:10A87000B387A70003C50700678000009307B001F6
:10A88000E3DEA7FC130545FE6FF0DFFC13051000A7
:10A89000678000001307C0013387E502B7D70000C7
:10A8A00093878704130101F923248106232291064B
:10A8B000232C410593046500232A510523286105B3
:10A8C000B387E70023267105232291052320A105E4
:10A8D000232EB103B387A7002326110623202107C7
:10A8E000232E31052324810503CC070093D7540080
:10A8F0002324F1009317050193D707411387F5FF30
:10A900002320F1009317070193D707412326F10075
:10A91000938715002328F1009397050193D70741EA
:10A92000930AF5FF2322F10093970A0193D7074179
:10A9300013943500139A34001374F40F232AF10092
:10A9400093071500930D0500930CD00F130D000015
:10A9500093041000232CF100B7DA0000130B8000E1
:10A96000930B60001304B400137AFA0F232401023E
:10A9700023260102631E9C168325C10003250100C6
:10A9800097000000E780C0EB8325010193090500D3
:10A990000325010097000000E78080EA832541003D
:10A9A000130905000325410197000000E78040E9F5
:10A9B00013030000630485013333B0018325410094
:10A9C00003258101232E610097000000E78040E706
:10A9D0000323C101930500006304950093B5BD01F5
:10A9E00003C58AFF13070000631C9D00639EA90135
:10A9F0006316970C631A0300130720006FF0DFFE45
:10AA0000E3187DFFE30699FE93078102338EE7008A
:10AA10002300AE0013071700E31867FD1377FD0F3F
:10AA20006316070A13071000639AE90A631E030AF4
:10AA300063840500A3070102230840F383278100F4
:10AA400033878C001377F70FA308F0F22309E0F2A5
:10AA50001307000093078102B385E70083C5050053
:10AA600013071700A309B0F2E31667FF93871C00D2
:10AA700093FCF70F13075000130D1D00E398ECEE45
:10AA80008320C1060324810683244106032901068D
:10AA90008329C105032A8105832A4105032B01056A
:10AAA000832BC104032C8104832C4104032D010456
:10AAB000832DC1031301010767800000E31C77F5B4
:10AAC000E39405F4130770006FF01FF21305700094
:10AAD000E31EA7F413071000E30AE9F4232401029C
:10AAE000232601026FF09FF4230401026FF05FF44C
:10AAF00093074000631EFC00E3E094F537170D0D4B
:10AB00001307D7D02324E1022326E1026FF0DFF2FE
:10AB100093072000631CFC001307E000E3EE94F1B0
:10AB20002306E102A305E1026FF01FF113073000D5
:10AB3000E314ECF037D7000003478700E30E07EE7D
:10AB400013872C001377F70F93055000E3E6E5EE2B
:10AB5000130720043357A7019375170013B7150087
:10AB600013076700938515001305B00063CAE5005D
:10AB700013071000E36297ED1307F0006FF05FFA20
:10AB8000930781023383B7002300A3009385150048
:10AB90006FF0DFFD130101FE232A9100232821011C
:10ABA00023263101232E1100232C81009304000061
:10ABB0001309C0019309D001130400001305040018
:10ABC00013041400938504001374F40F970000001D
:10ABD000E78080CCE31424FF9384140093F4F40FF3
:10ABE000E39C34FD8320C1010324810183244101BE
:10ABF000032901018329C1001301010267800000BC
:10AC0000B7C700009387C743B387C7008387070090
:10AC100093D5354013553540B385F500B7C70000CF
:10AC2000938747443386C700830706009395050141
:10AC3000130101FF3305F5001315050193D50541F7
:10AC4000135505412326110097000000E78040BFFF
:10AC50009307600063EEA7009307D00433D5A700E5
:10AC6000137515008320C1001301010167800000E6
:10AC7000130500006FF01FFF9387F50093970701FE
:10AC80002308A0F693D707011305E014636CF500C1
:10AC90009307F6009397070193D707011305E00F79
:10ACA0006376F500A30A00F66780000093F7F50FBE
:10ACB00093D58540A308F0F693F515002309B0F667
:10ACC0001376F60FA309C0F6230AD0F6A30AE0F61E
:10ACD000678000009395050193D505011388C5FF92
:10ACE00013180801135808019308800D63F8080130
:10ACF0002308A0F6A30A00F667800000938585026A
:10AD00009395050193D5054117030000670003F7EC
:10AD100037C50000130101FE13054525232E110040
:10AD2000232C8100232A910023282101232631018D
:10AD30002324410197F0FFFFE780806593054002DF
:10AD40001305300197F0FFFFE780005D37C5000075
:10AD50001305C52597F0FFFFE780804E37C500003B
:10AD60001305452497F0FFFFE7808062B7D7000006
:10AD700003964704B7D70000839567041307300094
:10AD80009306000013050000B7D9000037D9000072
:10AD9000B7C4000097000000E78000F4938909031E
:10ADA000130989029384844313040000130A4000AA
:10ADB00083C60400031609008395090013041400D8
:10ADC0001374F40F1307100013050400970000001C
:10ADD000E78080F0938929001309290093841400E7
:10ADE000E31844FD130440061304F4FF97F0FFFF3B
:10ADF000E78000681374F40F97F0FFFFE7800069A5
:10AE0000E31404FE130530019305400297F0FFFFA1
:10AE1000E7808050032481018320C1018324410104
:10AE2000032901018329C100032A810037C50000DD
:10AE3000130585261301010217F3FFFF6700434046
:10AE4000130101F8232E1106232C8106232A9106D3
:10AE500023282107232631072324410723225107D2
:10AE600023206107232E7105232C8105232A9105B8
:10AE70002328A1052326B105834780F73727000043
:10AE800013070798B3C7E70037D700002310F70070
:10AE9000834780F737C5000037D7000013054527E3
:10AEA0002302F70097F0FFFFE7808039B7C7000063
:10AEB0009387C7441307000093061001230BE0F6A5
:10AEC0001386070883C5070093871700A30BB0F606
:10AED000E39AC7FE130717001377F70FE310D7FEA7
:10AEE000B7D70000B7C90000A38107009389494480
:10AEF000B7D7000003C437006312041A970000009C
:10AF0000E7804091B7C700001385072897F0FFFF3F
:10AF1000E780003397F0FFFFE780405A9307C000B7
:10AF200037D70000230CF7FEB7D700001307100037
:10AF30002384E70097000000E78000C697F0FFFF3A
:10AF4000E780807EB7D7000003964704B7D700009C
:10AF50008395670413071000930600001305000093
:10AF600097000000E78040D7B7C7000037D900003E
:10AF7000B7D40000138A874393878743938484025E
:10AF800013090903232EF100930A400083460A00A7
:10AF90000396040083150900130414001374F40FBE
:10AFA000130710001305040097000000E780C0D2CB
:10AFB0001309290093842400130A1A00E31854FD8E
:10AFC00037C500001305452597F0FFFFE780403C9B
:10AFD000B7C500009385C5281305500097F0FFFF03
:10AFE000E780C03F37C500001305452197F0FFFFFC
:10AFF000E780003AB7C500009385C52A1305800095
:10B0000097F0FFFFE780803DB7C500009385052FCF
:10B010001305900097F0FFFFE780403CB7C50000A4
:10B02000938505331305B00097F0FFFFE780003BE1
:10B0300037C500001305853697F0FFFFE7804035E0
:10B04000B7C50000130590019385053797F0FFFF02
:10B05000E780C03837C500001305452497F0FFFF8F
:10B06000E7800033834780F893F7070B639C07085A
:10B0700097F0FFFFE780C03F97F0FFFFE7800041B8
:10B0800097F0FFFFE780403B834780F813F7F70F07
:10B090009397870193D7874163D0070897F0FFFF05
:10B0A000E780407737C500001305853897F0FFFF2C
:10B0B000E7800019834700F093F7170063840700C7
:10B0C0006F10400F8320C1070324810783244107A9
:10B0D000032901078329C106032A8106832A410621
:10B0E000032B0106832BC105032C8105832C41050D
:10B0F000032D0105832DC104130500001301010870
:10B100006780000097F0FFFFE780803697F0FFFF31
:10B11000E780C0376FF01FF593770703E38A07F4E2
:10B12000B7D70000130730002381E704B7D700002A
:10B13000239E070213071000B7D70000A380E7047F
:10B14000B72700009387077137D700002313F70054
:10B1500097F0FFFFE780006C37C500001305452816
:10B1600097F0FFFFE780C00D1304100037DA0000EE
:10B1700097F0FFFFE78080349307C00037D70000C7
:10B18000230CF7FE13071000B7D700002384E70055
:10B1900097000000E78040A0B7D700002387070092
:10B1A00097F0FFFFE7804058B7D7000023800704DF
:10B1B000B7D7000023AE07FE37C9000097F0FFFFA6
:10B1C000E780003897000000E780C0B49307C943C8
:10B1D0002324F10097F0FFFFE780802997F0FFFF1D
:10B1E000E780C02A630205089305C0031305100019
:10B1F00097F0FFFFE780401237C500001305851E5A
:10B2000097F0FFFFE780C01837C500001305C53968
:10B2100097F0FFFFE780C00237C500001305452403
:10B2200097F0FFFFE780C01697F0FFFFE78040240C
:10B23000834780F89397870193D7874163DA0700A4
:10B240009307100037D70000A301F7006FF05FCA23
:10B2500097F0FFFFE7808023E30805FCB7D70000E5
:10B26000130710002381E700834780F893FBF70F53
:10B270009397870193D78741E3CC07C6B7D70000E0
:10B2800083C4470093F4F4006390040637D70000AA
:10B290008347870037DB0000130B8B0493C717002D
:10B2A0002304F700130C3000930CC001130DD001E0
:10B2B000930A0000B3075B0183C70700639A870105
:10B2C0009385040013F5FA0F97F0FFFFE780C05C49
:10B2D000938A1A00E3909AFF9384140093F4F40F76
:10B2E000130BCB01E396A4FD37DB000083470B046F
:10B2F000B7D40000B7DA00008395640403964A04CB
:10B30000E394074A93F74B00638A0700B7D700001E
:10B31000130720002383E7026F00400193F78B009F
:10B320006384073CB7D7000023830702B7DC000023
:10B3300083C71C0493065000B7DB00001387070087
:10B3400063F4F6001307500083C75B0237DC00008C
:10B35000B7DD000093870701B387E700A382FB02F4
:10B36000130C8C0383C75B021307F0006362F73A88
:10B37000B7D7000083C64702B7D70000839564049F
:10B3800083C4770293D626009307200003964A04CD
:10B3900093F61600130710006394F40013073000AF
:10B3A0001305000097000000E780009303477A012F
:10B3B000B7D7000037D80000938A07021308880324
:10B3C000630A075837D70000835747019387F7FF71
:10B3D0009397070193D70701231AF7006394070097
:10B3E000A30B0A009387C4FF130C2000638407009B
:10B3F000138C04009377FC0FB7DC00002328F100C6
:10B4000093878C012326F100B7D7000037DB0000BB
:10B41000138C8702B7D7000013090800130BCB0168
:10B42000938C0703930D000093F7FD0F232CF1007D
:10B430008327C1000347090083C4070093841400D5
:10B4400093F4F40F238097006314070263888D5CE4
:10B45000130720006390ED5A37D700008346E700BA
:10B460001307D001636CD75A1307000F6368975A0C
:10B4700003460900130780029306D6FF93F6F60FE2
:10B480006372D40613072000631EE60283467A0126
:10B49000130780006398060483160C001306E0006F
:10B4A00093D636406390C60283960C001306F000D4
:10B4B00093D636409386A6FF9396060193D606014F
:10B4C0006362D60237D70000834617041306500084
:10B4D000138706006374D600130750001307E700B4
:10B4E0001377F70F83460B003307D7002300EB00D9
:10B4F00013D73400137717009306F0FF6304070097
:10B50000930610009397060193D70701232AF100B1
:10B5100037DD000003470B009306F00003460900E7
:10B5200063E2E650B7DB000083C50B0483960C0092
:10B5300003170C00639A050A930520006316B60AE8
:10B54000B7D5000083D56504232AC100B386B64071
:10B550009395060193D5054193D5F540B3C6D50023
:10B56000B386B6409396060193D6064193055000E4
:10B5700063CCD506B7D6000083D646043307D74040
:10B580009316070193D6064193D6F64033C7E600DB
:10B590003307D740131707011357074163C6E50464
:10B5A00037D7000003477701E306070CB7D7000041
:10B5B00003CD67011305800C130730003315A50177
:10B5C000131505012300E9001355050197F0FFFF4E
:10B5D000E780800E032641016368A601130D1D005C
:10B5E000B7D70000238BA701034609009306E000AC
:10B5F0001307D6FF1377F70F637AE40203477A0144
:10B6000093D4340093F61400E304070CE372C40CE3
:10B6100037D700000357470113067007E366E60AB1
:10B6200013778700E302070A9386C6008327810108
:10B6300003160C0083950C001385170013071000E8
:10B640001375F50F97F0FFFFE78000698327C100AE
:10B65000938D1D0013074000938717002326F100E8
:10B6600013091900938C2C00130C2C00130B1B00D6
:10B67000938A1A00E39AEDDAB7D6000083C7B600C2
:10B6800037D70000639A0702B7D7000083C7E700E7
:10B69000130660046386C7001306A00A639EC700F2
:10B6A0008357C700639A070093071000A385F6002D
:10B6B0009307C0212316F7008357C700E3860702CC
:10B6C0009387F7FF9397070193D707012316F70096
:10B6D000E39C0700A3850600930750002308F0F6BB
:10B6E000A30A00F66F00501E93F71B006388070043
:10B6F000B7D70000130730006FF0DFC193FB2B00BA
:10B70000E3860BC2B7D70000130710006FF09FC08D
:10B7100003CB6D02938707FFA382FB0293074000D0
:10B72000630EFB0037D7000083477702938727001B
:10B7300093F737006314FB00A303670303976404C4
:10B7400093064000039D4A0493777700638AD706E7
:10B75000B7D7000083C677021307400083976404BD
:10B7600003964A04E380E6C0832581003387D9002D
:10B7700003070700B386D50083860600B387E7007A
:10B780001397070193970701B306D60093D7074194
:10B790002392DA042393F404930680FF13570701DE
:10B7A00063D2D71A1307070F2393E40437D7000097
:10B7B00083474702938717002302F7026FF09FBA6F
:10B7C00093767D00E396F6F813573740135D3D40BE
:10B7D00093050D00130507002326E10097F0FFFFF6
:10B7E000E78000069307E5FF93F7F70F13090500BD
:10B7F000636CF4060327C1001306B0019317070119
:10B8000093D707016368F6021306C0013306CD0221
:10B81000B7D70000938787049375FD0F1375F70F53
:10B82000B387C700B387E7002380070097F0FFFFC7
:10B83000E780800637D700008347E7039387F7FF49
:10B84000230FF70293072000631AF9061305A000DF
:10B8500097F0FFFFE78040E637D700008347E70017
:10B86000938717002307F70013074000039D640424
:10B8700083954A0437D900006302EB0213060B00DC
:10B8800013050D002326B10097F0FFFFE7808037F6
:10B890008325C10063040500A303690303467902FD
:10B8A00093074000E300F6AC13050D0097F0FFFF8F
:10B8B000E7804035E31E05E86FF0DFAA130520039B
:10B8C00097F0FFFFE78040DF93071000A30BFA001B
:10B8D000B7D70000238B070083C71C0493066000C2
:10B8E0001307400663ECF600130780FDB387E702F9
:10B8F000938787161397070113570701B7D70000DF
:10B90000239AE700B7D70000938707029306000049
:10B910009305200013064000338786010347070084
:10B92000631AB70003C70700130727001377370010
:10B930002380E7009386160093871700E39EC6FCDA
:10B940006FF09FF19306700EE3D2F6E6130707F14E
:10B950006FF09FE537D500008345F50037C700003D
:10B96000130787423307B700834607001307F00F1A
:10B97000E38AE6A69308C003B386160337D6000011
:10B980000357260113071700131707011357070161
:10B990006356D7002319E6006FF0DFA41387150064
:10B9A000B7D60000A307E50003C706012319060068
:10B9B00093870702134717002388E60093052000AA
:10B9C0009306000013064000338706010347070073
:10B9D000631AB70003C70700130727001377370060
:10B9E0002380E7009386160093871700E39EC6FC2A
:10B9F0006FF05F9F13073000E39CEDA637D7000080
:10BA00000347E7009306B00363EAE6009306A00F3E
:10BA1000E3F096A693063001E3FCE6A42300890038
:10BA20006FF01FA5130707FF2300EB0063068604D2
:10BA3000130740006308E6086314060A03570C0066
:10BA400083274101930680063387E7001317070118
:10BA50001357074163DAE6009306F00663CAE6006F
:10BA60002310EC006FF01FAB130790066FF05FFF21
:10BA70002310DC006FF01FAA03970C009306B0069A
:10BA800063C8E600130717002390EC006FF09FA82F
:10BA90009306C0066306D7001307F7FF6FF0DFFEBB
:10BAA00003170C009306C00463D6E6001307F7FFE4
:10BAB0006FF01FFB130720002300E9002380EA003A
:10BAC0006FF05FA503170C009306B00663C6E6008F
:10BAD000130717006FF0DFF8230089006FF09FA3B2
:10BAE000039E0C0083150C0093767E00639EE6088F
:10BAF00013F77500631AD70893063000631ED6004B
:10BB00009306C006631ADE009306C0046396D50050
:10BB10002300E9006FF01FA083CB0A0003477A01DE
:10BB2000938B2B0093FB3B006304070C130720004F
:10BB30006310E60C97F0FFFFE78000909376F50F17
:10BB400013070000130E40003386E600137636001C
:10BB50006386CB0883150C0003950C002324E102B7
:10BB60002322D1022320C10297F0FFFFE780800942
:10BB7000032601028326410203278102130E40009F
:10BB8000630E05042380CA0003C60A0083278100D0
:10BB90003387C9008306070003D70C003386C7002C
:10BBA0008305060003560C003307D70093160701E0
:10BBB000131707013306B600135707412310CC00B3
:10BBC0002390EC00130680FF93D60601635CC72820
:10BBD0009386060F2390DC006FF0DF9313071700A6
:10BBE0001377F70FE312C7F723807A016FF0DFF9BD
:10BBF00037D70000130F300083166D040317470476
:10BC000013553E4093DE3540630EE62337D60000E1
:10BC1000034606016314061237C7000013074743A3
:10BC20003387ED008306070037C70000130707437B
:10BC30003387ED0003070700B38EEE403305D54090
:10BC400093160501939E0E01B78F000093D606014F
:10BC500093DE0E01138F0B0013070000938FFFFF7D
:10BC6000930240001376F70F6302760D13050E0062
:10BC7000232ED103232CD102232AE1032328F1030D
:10BC80002326E1022322B1022320C1032324C1027F
:10BC900097F0FFFFE78000F7032E010283254102A2
:10BCA0000327C102832F0103032F4103832681034E
:10BCB000832EC10393024000630A05063305370152
:10BCC0000305050083278100032681023385A60032
:10BCD0009313050193D3034193D3F34033C5A300DA
:10BCE00033057540B303F70083830300B3837E00FD
:10BCF0009390030193D0004193D0F040B3C3700000
:10BD0000B383134033057500131505011355054121
:10BD10006340F515631CF501130530006300A7149B
:10BD20009303200063147700631AAF121307170000
:10BD3000E31A57F22380EA016FF01FE593D63640ED
:10BD40001357374063808D0613062000638CCD089F
:10BD5000E3840DEE3306D540131F0601135F0F4138
:10BD6000135FFF403346CF003306E641338FEE408A
:10BD7000931F0F0193DF0F4193DFFF4033CFEF019C
:10BD8000330FFF413306E601131606011356060171
:10BD9000130F8000E362CFEA1307E001930610005F
:10BDA0006FF09FE9832701013386F900030606003F
:10BDB0008327810013162600B306D600032601014F
:10BDC0009396060193D606413386C7000306060004
:10BDD000131626003307E600131707011357074110
:10BDE0006FF09FE5832701013386F9000306060003
:10BDF0008327810013161600B306D60037D600003D
:10BE0000031606039396160013563640B386C640B3
:10BE1000032601019396060193D606413386C70097
:10BE200003060600131616003307E60037D6000097
:10BE30000316860213171700135636403307C74000
:10BE40006FF09FF9130790009306D0006FF0DFDECC
:10BE5000130F0600930F05006FF05FED130F060040
:10BE60006FF0DFEC1306700E6356E6EA938606F178
:10BE70006FF05FD637D700000347F703631607F666
:10BE800037D7000013064006A304C70037D70000C9
:10BE90001306C0032305C70037C7000037D60000CC
:10BEA0001307473B232EE6FE37D700001306800713
:10BEB00093061000A30CC7FE37D700002380DB04D5
:10BEC0002301D7006FF04FF29386A6006FF00FF6B4
:10BED0008327C1013387B70103470700B386E60014
:10BEE00093F6F60F6FF08FF483C7B600638607FEF4
:10BEF00083C70B046394072CB7D7000083D767046C
:10BF000093055000938707F91396070113560641CE
:10BF10001356F640B347F600B387C740939707011F
:10BF200093D7074163CCF528B7D7000083D74704E0
:10BF3000938747F413960701135606411356F640AC
:10BF4000B347F600B387C7409397070193D70741DC
:10BF500063C6F526B7D7000003C517049307401F33
:10BF6000930410003305F50223169700A3850600FD
:10BF7000131505011355050197E0FFFFE780C07316
:10BF8000B7C7000037D700009387873C232EF7FE02
:10BF9000B7D7000013078007A38CE7FEB7D70000D0
:10BFA000238197006FF04FF3B7D4000003C7A400BC
:10BFB0009385850293950501135727401377170042
:10BFC0009306000193D505411305600097F0FFFF2C
:10BFD000E780C0CA230800F6A30A00F683C7A400BE
:10BFE000638607009387F7FF2385F40037D70000A7
:10BFF000834797009387F7FF93F7F70FA304F700A2
:10C000006394070C37D70000834727049387F7FF13
:10C0100093F7F70F2301F7046394070897E0FFFFF6
:10C02000E780005237C500001305053D97E0FFFF8C
:10C03000E780003637C400009305843D1305E00017
:10C0400097E0FFFFE7808039B7C500009385853F03
:10C050001305F00097E0FFFFE78040389305843D2B
:10C060001305000197E0FFFFE780403737C5000068
:10C070001305452497E0FFFFE78080311304600932
:10C080001304F4FF97E0FFFFE780803E1374F40F82
:10C0900097E0FFFFE780803FE31404FE6FE05FE579
:10C0A00097E0FFFFE7804068B7D700001307100054
:10C0B0002381E70023000B0497E0FFFFE78040485F
:10C0C00097F0FFFFE78000C537DB000083479BFF49
:10C0D000B7D40000638407029387F7FF93F7F70F45
:10C0E000A30CFBFE639C0700930710002381F40060
:10C0F00097E0FFFFE780C0446F00C00083C72400C3
:10C10000E39807FEB7D7000083CAE70363940A8C5D
:10C11000B7C7000037D7000093878741232EF7FE6B
:10C1200093078007A30CFBFE930710002381F40004
:10C1300097E0FFFFE780C0409304800093F71A0068
:10C140001307F000638407001307C000B7D700008F
:10C15000238CE7FE1309C00097F0FFFFE780C0A320
:10C160001309F9FF97E0FFFFE78080301379F90F9B
:10C1700097E0FFFFE7808031E31409FE938A1A00FD
:10C1800093FAFA0FE39C9AFA93070002230820F728
:10C1900013091900A30A00F61379F90FE318F9FE41
:10C1A00037D700008347170493871700A300F704CD
:10C1B0006FE01FFC834710F06FE0DFEF9307500044
:10C1C0002308F0F6930780F9A308F0F6230900F698
:10C1D0009307C0FBA309F0F69307F000230AF0F6DB
:10C1E000A30A80F66FF05FEE303B393300000000A9
:10C1F0004352595354414C2043415645524E2020FE
:10C2000020000000303B33370000000053434F5202
:10C2100045200000303B3937000000002020204C32
:10C220004956455320000000202020434156452018
:10C2300000000000303B39360000000020202000C4
:10C240001B5B4B00300000004558545241204C49C4
:10C2500046450000313B393300000000474554207B
:10C260005245414459000000202020202020202059
:10C27000200000001B5B3F336C1B5B3F32356C00C2
:10C280001B5B306D1B5B324A0000000020432052D4
:10C290002059205320542041204C2020204320416D
:10C2A000205620452052204E200000002047617477
:10C2B000686572206576657279206372797374613E
:10C2C0006C20696E2074686520636176652C20613E
:10C2D0006E64206B65657020617761792066726F8E
:10C2E0006D2074686520626174732E200000000068
:10C2F0002041206C616E7465726E2073636172659B
:10C3000073207468656D20666F722061207768699C
:10C310006C653A207468656E20796F752063616E74
:10C32000206361746368207468656D2E20000000CE
:10C33000204152524F5753206F72205720412053B3
:10C34000204420206D6F7665202020202050207012
:10C35000617573652020202020455343206769764E
:10C360006520757020000000313B39360000000068
:10C3700020505245535320535041434520544F20A1
:10C38000424547494E2000001B5B306D1B5B324A23
:10C390001B5B481B5B3F3235680000005041555322
:10C3A0004544202D205020544F20474F204F4E1BF6
:10C3B0005B4B0000544845204241545320474F54A2
:10C3C00020594F5500000000474F4C442100000009
:10C3D000303B34343B3937002020202020202020DF
:10C3E000202020202020202020202020202020204D
:10C3F000202020202020000020202020202020207D
:10C40000202047414D45204F5645522020202020D6
:10C4100020202020202000004341564520434C4549
:10C4200041524544000000000714071405FF0000B6
:10C43000FEFE1E1E1A011A0102040608000100FF7A
:10C44000000000000100FF000000000000000000EC
:10C450000000000000000BBBBBB000000000B333C5
:10C460003333F00000033333333FF0000009999970
:10C47000999900000009909909990000000999997B
:10C48000999900000000999009900000000CCCCC14
:10C49000CCCC000000CCCCCCCCCC9000009CCCCC44
:10C4A000CCCC000000044444444400000004440098
:10C4B0000444000000077000007700000000000046
:10C4C000000000000000000000000000000000006C
:10C4D0000000000000000BBBBBB000000000B33345
:10C4E0003333F00000033333333FF00000099999F0
:10C4F00099990000000990990999000000099999FB
:10C50000999900000000999009900000000CCCCC93
:10C51000CCCC000000CCCCCCCCCC9000009CCCCCC3
:10C52000CCCC0000000444444444000000004440DB
:10C530004440000000000770770000000000000089
:10C5400000000000000000000000000000000000EB
:10C5500000000000000000000000000009000000D2
:10C5600000000090099000000000099009990099CE
:10C5700099009990099999999999999000999F99F9
:10C5800099F999000009999999999000000090995A
:10C5900099090000000009099090000000000000C7
:10C5A000000000000000000000000000000000008B
:10C5B000000000000000000000000000000000007B
:10C5C000000000000000000000000000000000006B
:10C5D000000000000000000000000000000000005B
:10C5E00000000000000000000000000000000099B2
:10C5F00099000000090999999999909009999F9932
:10C6000099F9999009999999999999900990999975
:10C61000999909900900009999000090000009007B
:10C62000009000000000000000000000000000007A
:10C6300000000000000000000000000000000000FA
:10C6400000000000000000000000000000000000EA
:10C650000000000000000000000000000D000000CD
:10C66000000000D00DD0000000000DD00DDD00DD79
:10C67000DD00DDD00DDDDDDDDDDDDDD000DDDFDD8C
:10C68000DDFDDD00000DDDDDDDDDD0000000D0DDF5
:10C69000DD0D000000000D0DD0D0000000000000F6
:10C6A000000000000000000000000000000000008A
:10C6B000000000000000000000000000000000007A
:10C6C000000000000000000000000000000000006A
:10C6D000000000000000000000000000000000005A
:10C6E000000000000000000000000000000000DD6D
:10C6F000DD0000000D0DDDDDDDDDD0D00DDDDFDD89
:10C70000DDFDDDD00DDDDDDDDDDDDDD00DD0DDDD00
:10C71000DDDD0DD00D0000DDDD0000D000000D00DE
:10C7200000D0000000000000000000000000000039
:10C7300000000000000000000000000000000000F9
:10C7400000000000000000000000000000000000E9
:10C750000000000000000000000000000A000000CF
:10C76000000000A00AA0000000000AA00AAA00AA77
:10C77000AA00AAA00AAAAAAAAAAAAAA000AAAFAA1C
:10C78000AAFAAA00000AAAAAAAAAA0000000A0AABF
:10C79000AA0A000000000A0AA0A000000000000091
:10C7A0000000000000000000000000000000000089
:10C7B0000000000000000000000000000000000079
:10C7C0000000000000000000000000000000000069
:10C7D0000000000000000000000000000000000059
:10C7E000000000000000000000000000000000AA9F
:10C7F000AA0000000A0AAAAAAAAAA0A00AAAAFAA86
:10C80000AAFAAAA00AAAAAAAAAAAAAA00AA0AAAA96
:10C81000AAAA0AA00A0000AAAA0000A000000A0012
:10C8200000A0000000000000000000000000000068
:10C8300000000000000000000000000000000000F8
:10C8400000000000000000000000000000000000E8
:10C8500000000000000000000000000003000000D5
:10C8600000000030033000000000033003330033C9
:10C8700033003330033333333333333000333F3318
:10C8800033F33300000333333333300000003033ED
:10C8900033030000000003033030000000000000FC
:10C8A0000000000000000000000000000000000088
:10C8B0000000000000000000000000000000000078
:10C8C0000000000000000000000000000000000068
:10C8D0000000000000000000000000000000000058
:10C8E0000000000000000000000000000000003315
:10C8F00033000000030333333333303003333F332B
:10C9000033F33330033333333333333003303333A0
:10C9100033330330030000333300003000000300E2
:10C9200000300000000000000000000000000000D7
:10C9300000000000000000000000000000000000F7
:10C9400000000000000000000000000000000000E7
:10C9500000000000000000000000000004000000D3
:10C960000000004004400000000004400444004473
:10C9700044004440044444444444444000444F443C
:10C9800044F4440000044444444440000000404453
:10C9900044040000000004044040000000000000C7
:10C9A0000000000000000000000000000000000087
:10C9B0000000000000000000000000000000000077
:10C9C0000000000000000000000000000000000067
:10C9D0000000000000000000000000000000000057
:10C9E0000000000000000000000000000000004403
:10C9F00044000000040444444444404004444F4480
:10CA000044F44440044444444444444004404444C2
:10CA1000444404400400004444000040000004007A
:10CA200000400000000000000000000000000000C6
:10CA300000000000000000000000000000000000F6
:10CA400000000000000000000000000000000000E6
:10CA50000000000000000000000000000F000000C7
:10CA6000000000F00FF0000000000FF00FFF00FFCB
:10CA7000FF00FFF00FFFFFFFFFFFFFF000FFFFFFD2
:10CA8000FFFFFF00000FFFFFFFFFF0000000F0FFBF
:10CA9000FF0F000000000F0FF0F00000000000008A
:10CAA0000000000000000000000000000000000086
:10CAB0000000000000000000000000000000000076
:10CAC0000000000000000000000000000000000066
:10CAD0000000000000000000000000000000000056
:10CAE000000000000000000000000000000000FF47
:10CAF000FF0000000F0FFFFFFFFFF0F00FFFFFFF31
:10CB0000FFFFFFF00FFFFFFFFFFFFFF00FF0FFFF42
:10CB1000FFFF0FF00F0000FFFF0000F000000F000C
:10CB200000F0000000000000000000000000000015
:10CB300000000000000000000000000000000000F5
:10CB400000000000000000000000000000000000E5
:10CB500000000000000000000000000000000000D5
:10CB600000000000000000000000000000000000C5
:10CB70000000000000000000000000000000FF00B6
:10CB800000FF00000000F40000F4000000000000BE
:10CB90000000000000000000000000000000000095
:10CBA0000000000000000000000000000000000085
:10CBB0000000000000000000000000000000000075
:10CBC0000000000000000000000000000000000065
:10CBD0000000000000000000000000000000000055
:10CBE000000000000000000000000000000000BB8A
:10CBF000B00000000000BBBFBBB00000000BBFBB1B
:10CC0000BBBB0000000BBBBBB3BB00000000BB3BC9
:10CC1000BBB0000000000BBBB00000000000000033
:10CC20000000000000000000000000000000000004
:10CC300000000000000000000000000000000000F4
:10CC400000000000000000000000000000000000E4
:10CC500000000000000F00000000F0000000F00BDA
:10CC6000B00F000000000F0BB0F00000000000BF8C
:10CC7000FB0000000BBB0BFFFFB0BBB0000000BF10
:10CC8000FB00000000000F0BB0F000000000F00BF4
:10CC9000B00F0000000F00000000F00000000000D6
:10CCA0000000000000000000000000000000000084
:10CCB0000000000000000000000000000000000074
:10CCC0000000000000000000000000000101010160
:10CCD0000101010101010101010101010101010144
:10CCE000010101010101010101020202020202022D
:10CCF0000202010102020202010102020202020218
:10CD0000020202010102010101020101010201010D
:10CD100002010102010102010101020101010201FE
:10CD200001020101010201010102010102010102EE
:10CD300001010201010102010101020101020101DF
:10CD400001020101010201010201010201010201CE
:10CD500001010201010102010103010101020202BC
:10CD600002020202020101020202020202020201A6
:10CD7000010103010102010101020101010101019F
:10CD8000020101020101010101010201010102018F
:10CD9000010201010102010101010101020101027F
:10CDA0000101010101010201010102010102010170
:10CDB0000102010101010101020101020101010160
:10CDC000010102010101020101020101010202024D
:10CDD0000202010100000000010102020202020140
:10CDE0000101020101020101010101010102010130
:10CDF0000100000101010201010101010101020123
:10CE0000010201010101010101020101010404010A
:10CE100001010201010101010101020101020101FF
:10CE200001010101010201050505050505010201D8
:10CE300001010101010102010102010101010101E0
:10CE400001020105050505050501020101010101B8
:10CE500001010201060601010106020202020105AA
:10CE6000050505050501020202020601010106068B
:10CE7000010201010102010101020105050505058B
:10CE8000050102010101020101010201010201018A
:10CE9000010201010102010101010101010102017F
:10CEA000010102010101020101020101010201016E
:10CEB000010201010101010101010201010102015F
:10CEC000010102010102010101020101010201014E
:10CED000010101010101020101010201010102013F
:10CEE0000102020202020101010202020202020226
:10CEF000020202010101020202020201010101011A
:10CF0000010101010101010102010102010101010F
:10CF10000101010101010101010101010101010101
:10CF200001010101020101020101010101010101EF
:10CF300001010101010101010101010101010101E1
:10CF400002010102010101010101010101010101CF
:10CF500001030202020202020202020202000002B5
:10CF600002020202020202020202030101020101A4
:10CF7000010201010102010102010102010102019C
:10CF8000010102010101020101020101010201018D
:10CF9000010201010201010201010201010102017C
:10CFA000010102010102010101020101010201016D
:10CFB000020101020101020101010201010102015C
:10CFC0000102020202020101010202020201010247
:10CFD0000202020101010202020202010101010139
:10CFE0000101010101010101010101010101010131
:08CFF000010101010101010131
:040000030000A20057
:00000001FF
+250
View File
@@ -0,0 +1,250 @@
#!/usr/bin/env python3
"""Make data.h for cavern.c: the maze and the sprite shapes.
The maze is 28 x 29 tiles of 8 x 8 pixels, mirrored left to right. It is
built from a grid of junctions joined by straight corridors: every link is
made, then links are taken away at random as long as every junction keeps at
least two, the whole cave stays connected and the fixed parts (the crossings
at the roost and the start, the side tunnels) stay. So there are no dead ends,
which a chase game can't have. The bats' roost sits in the middle.
Tiles: 0 open, 1 wall, 2 crystal, 3 lantern, 4 roost door, 5 roost, 6 tunnel.
"""
import random
import sys
from collections import deque
W, H = 28, 29
NX = [1, 5, 9, 12] # junction columns on the left (12 mirrors to 15)
NY = [1, 5, 9, 14, 19, 23, 27] # junction rows
ROOST = (10, 11, 17, 16) # cols 10-17, rows 11-16 (x0, y0, x1, y1)
TUNNEL_Y = 14
START = (13, 23) # the miner starts between 13 and 14 on row 23
LANTERNS = [(1, 5), (1, 23)] # and their mirror images
def mirror(x):
return W - 1 - x
def build(seed):
rnd = random.Random(seed)
nodes = {(x, y) for x in NX for y in NY}
nodes.discard((12, 14)) # inside the roost
edges = set()
for y in NY:
row = [x for x in NX if (x, y) in nodes]
for a, b in zip(row, row[1:]):
if not (y == 14 and b == 12):
edges.add(((a, y), (b, y)))
if (12, y) in nodes:
edges.add(((12, y), (15, y))) # across the middle
for x in NX:
col = [y for y in NY if (x, y) in nodes]
for a, b in zip(col, col[1:]):
if x == 12 and a < ROOST[3] and b > ROOST[1]:
continue # would run through the roost
edges.add(((x, a), (x, b)))
fixed = {((12, 9), (15, 9)), ((12, 23), (15, 23))}
def full(es):
g = {}
for (a, b) in es:
for p, q in ((a, b), (b, a)):
g.setdefault(p, set()).add(q)
if a[0] == 12 and b[0] == 15:
continue
ma, mb = (mirror(a[0]), a[1]), (mirror(b[0]), b[1])
g.setdefault(ma, set()).add(mb)
g.setdefault(mb, set()).add(ma)
return g
def ok(es):
g = full(es)
# the tunnel ends count as a link for (1, 14)
deg = {n: len(v) + (1 if n[1] == TUNNEL_Y and n[0] in (1, mirror(1)) else 0) for n, v in g.items()}
if any(d < 2 for d in deg.values()):
return False
start = next(iter(g))
seen, q = {start}, deque([start])
while q:
for m in g[q.popleft()]:
if m not in seen:
seen.add(m); q.append(m)
return len(seen) == len(g)
order = sorted(edges - fixed)
rnd.shuffle(order)
for e in order:
if rnd.random() < 0.45 and ok(edges - {e}):
edges.discard(e)
assert ok(edges)
grid = [[1] * W for _ in range(H)]
def carve(x, y, t=2):
for xx in (x, mirror(x)):
if grid[y][xx] == 1:
grid[y][xx] = t
for (a, b) in edges:
if a[1] == b[1]:
for x in range(min(a[0], b[0]), max(a[0], b[0]) + 1):
carve(x, a[1])
else:
for y in range(min(a[1], b[1]), max(a[1], b[1]) + 1):
carve(a[0], y)
# the roost: walls round a room, a door at the top, a way up to row 9
x0, y0, x1, y1 = ROOST
for y in range(y0, y1 + 1):
for x in range(x0, x1 + 1):
grid[y][x] = 1 if y in (y0, y1) or x in (x0, x1) else 5
grid[y0][13] = grid[y0][14] = 4
grid[10][13] = grid[10][14] = 0
# tunnels: from the side junctions out through the wall
for x in (0,):
carve(x, TUNNEL_Y, 6)
for x in range(1, NX[0] + 1):
if grid[TUNNEL_Y][x] == 1:
carve(x, TUNNEL_Y, 6)
# no crystals in the tunnel, by the roost door, or where the miner starts
for x in range(0, 6):
for xx in (x, mirror(x)):
if grid[TUNNEL_Y][xx] == 2:
grid[TUNNEL_Y][xx] = 6
for x in (12, 13, 14, 15):
if grid[9][x] == 2:
grid[9][x] = 0
for x in (13, 14):
grid[START[1]][x] = 0
for (x, y) in LANTERNS:
assert grid[y][x] == 2, (x, y)
grid[y][x] = grid[y][mirror(x)] = 3
return grid
def check(grid):
open_t = lambda t: t in (0, 2, 3, 6)
for y in range(H):
for x in range(W):
if not open_t(grid[y][x]):
continue
n = 0
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nx, ny = (x + dx) % W, y + dy
if 0 <= ny < H and (open_t(grid[ny][nx]) or grid[ny][nx] == 4):
n += 1
assert n >= 2, "dead end at %d,%d" % (x, y)
for y in range(H):
assert grid[y] == grid[y][::-1], "row %d is not mirrored" % y
# ---------------------------------------------------------------- shapes
Z = "0" * 16
MINER_A = [Z,
"00000BBBBBB00000",
"0000B3333333F000", # helmet with a lamp
"00033333333FF000",
"0009999999990000",
"0009909909990000",
"0009999999990000",
"0000999009900000",
"000CCCCCCCCC0000",
"00CCCCCCCCCC9000",
"009CCCCCCCCC0000",
"000444444444000",
"0004440004440000",
"0007700000770000",
] + [Z] * 2
MINER_A[11] = MINER_A[11] + "0"
MINER_B = MINER_A[:11] + [
"0004444444440000",
"0000444044400000",
"0000077077000000",
] + [Z] * 2
def bat(c, frame):
if frame == 0:
rows = [Z, Z,
"0C000000000000C0",
"0CC0000000000CC0",
"0CCC00CCCC00CCC0",
"0CCCCCCCCCCCCCC0",
"00CCCFCCCCFCCC00",
"000CCCCCCCCCC000",
"0000C0CCCC0C0000",
"00000C0CC0C00000",
"000000000000000" + "0"] + [Z] * 5
else:
rows = [Z, Z, Z, Z,
"000000CCCC000000",
"0C0CCCCCCCCCC0C0",
"0CCCCFCCCCFCCCC0",
"0CCCCCCCCCCCCCC0",
"0CC0CCCCCCCC0CC0",
"0C0000CCCC0000C0",
"00000C0000C00000"] + [Z] * 5
return [r.replace("C", c) for r in rows]
EYES = [Z] * 6 + ["0000FF0000FF0000", "0000F40000F40000"] + [Z] * 8
NUGGET = [Z] * 4 + [
"000000BBB0000000",
"0000BBBFBBB00000",
"000BBFBBBBBB0000",
"000BBBBBB3BB0000",
"0000BB3BBBB00000",
"00000BBBB0000000",
] + [Z] * 6
BOOM = [Z,
"000F00000000F000",
"0000F00BB00F0000",
"00000F0BB0F00000",
"000000BFFB000000",
"0BBB0BFFFFB0BBB0",
"000000BFFB000000",
"00000F0BB0F00000",
"0000F00BB00F0000",
"000F00000000F000",
] + [Z] * 6
SHAPES = [("S_MINER", MINER_A), ("S_MINER2", MINER_B),
("S_BAT_R", bat("9", 0)), ("S_BAT_R2", bat("9", 1)),
("S_BAT_M", bat("D", 0)), ("S_BAT_M2", bat("D", 1)),
("S_BAT_G", bat("A", 0)), ("S_BAT_G2", bat("A", 1)),
("S_BAT_O", bat("3", 0)), ("S_BAT_O2", bat("3", 1)),
("S_SCARED", bat("4", 0)), ("S_SCARED2", bat("4", 1)),
("S_FLASH", bat("F", 0)), ("S_FLASH2", bat("F", 1)),
("S_EYES", EYES), ("S_NUGGET", NUGGET), ("S_BOOM", BOOM)]
def main(out, seed=11):
grid = build(seed)
check(grid)
crystals = sum(r.count(2) + r.count(3) for r in grid)
L = ["/* made by gen.py - edit that instead */", ""]
L.append("#define MW %d\n#define MH %d" % (W, H))
L.append("#define START_X %d\n#define START_Y %d\n#define TUNNEL_Y %d" % (START[0], START[1], TUNNEL_Y))
L.append("#define CRYSTALS %d" % crystals)
L.append("static const u8 maze_start[MH][MW] = {")
for row in grid:
L.append(" {" + ",".join(str(t) for t in row) + "},")
L.append("};")
for i, (name, img) in enumerate(SHAPES):
assert len(img) == 16 and all(len(r) == 16 for r in img), name
L.append("#define %s %d" % (name, i))
L.append("#define N_SHAPES %d" % len(SHAPES))
L.append("static const u8 shape_data[N_SHAPES * 128] = {")
for name, img in SHAPES:
b = [int(r[x], 16) << 4 | int(r[x + 1], 16) for r in img for x in range(0, 16, 2)]
L.append(" " + ",".join(str(v) for v in b) + ", /* %s */" % name)
L.append("};")
open(out, "w").write("\n".join(L) + "\n")
print("data.h: maze %d x %d, %d crystals, %d shapes" % (W, H, crystals, len(SHAPES)))
return grid
if __name__ == "__main__":
g = main(sys.argv[1] if len(sys.argv) > 1 else "data.h", int(sys.argv[2]) if len(sys.argv) > 2 and sys.argv[2].isdigit() else 11)
if "--show" in sys.argv:
for row in g:
print("".join(" ##..oo==-- "[t * 2:t * 2 + 2][0] if False else " #.o=- "[t] for t in row))
+127
View File
@@ -0,0 +1,127 @@
#!/usr/bin/env python3
"""Run Crystal Cavern in tools/rv32emu.py on a model of the board
(tools/boardemu.py) and take screenshots.
python3 test-cavern.py [--shots DIR] [--play]
It checks the title screen, measures the Z-80 instructions each frame takes
(a 60 Hz frame has about 550,000 at 90 MHz), checks that walls stop the miner,
that crystals score and that the bats leave the roost, and with --play lets
an autopilot gather every crystal in the cave by breadth-first search. The
autopilot can't be caught (it keeps the miner shielded), so this checks the
maze and the rules, not its skill.
"""
import argparse
import os
import sys
from collections import deque
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "..", "tools"))
from boardemu import Board, load_ihx, load_font, load_syms # noqa: E402
UP, DOWN, LEFT, RIGHT, FIRE, ESC = 1, 2, 4, 8, 16, 128
MW, MH = 28, 29
OPEN = (0, 2, 3, 6)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--shots", help="write PNG screenshots here")
ap.add_argument("--play", action="store_true", help="let the autopilot clear a cave")
a = ap.parse_args()
mem, hi = load_ihx(os.path.join(HERE, "cavern.ihx"))
b = Board(mem, code_end=hi)
font = load_font() if a.shots else None
if a.shots:
os.makedirs(a.shots, exist_ok=True)
sym = {"_" + n: a for n, a in load_syms(os.path.join(HERE, "out", "cavern.sym")).items()}
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def s16(n, k=0):
v = b.m[sym[n] + 2 * k] | b.m[sym[n] + 2 * k + 1] << 8
return v - 65536 if v & 0x8000 else v
def u8(n, k=0):
return b.m[sym[n] + k]
def maze(x, y):
return b.m[sym["_maze"] + y * MW + (x % MW)] if 0 <= y < MH else 1
def screen():
return b.screen_text().display
b.run_frames(5)
check("title screen", "C R Y S T A L C A V E R N" in "\n".join(screen()))
if a.shots: b.shot(os.path.join(a.shots, "1-title.png"), font)
b.run_frames(3, FIRE)
b.run_frames(110) # GET READY
check("game starts", "CAVE 1" in screen()[0] and "LIVES 3" in screen()[0])
b.m[sym["_shield"]] = 1
left0 = u8("_left")
c0 = b.steps
worst = 0
for f in range(120):
c1 = b.steps
b.run_frames(1, LEFT)
worst = max(worst, b.steps - c1)
print("%.0f instructions a frame on average, %d at most (%.0f%%)" %
((b.steps - c0) / 120, worst, worst / 5500))
check("every frame fits in a 60th of a second", worst < 550000)
check("crystals are gathered and score", u8("_left") < left0 and s16("_score") > 0)
x0 = s16("_mx")
b.run_frames(20, LEFT)
check("a wall stops the miner", s16("_mx") == x0 or (s16("_mx") & 7) == 4)
check("the bats leave the roost", sum(1 for k in range(4) if u8("_bstate", k) == 2) >= 2)
if a.shots: b.shot(os.path.join(a.shots, "2-chase.png"), font)
if a.play:
frame = 0
want = LEFT
while frame < 30000 and b.pc != 0:
b.m[sym["_shield"]] = 1
mx, my = s16("_mx"), s16("_my")
if (mx & 7) == 4 and (my & 7) == 4:
start = (mx >> 3, my >> 3)
prev = {start: None}
q = deque([start])
goal = None
while q:
p = q.popleft()
if maze(*p) in (2, 3) and p != start:
goal = p
break
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
n = ((p[0] + dx) % MW, p[1] + dy)
if n not in prev and maze(*n) in OPEN:
prev[n] = p; q.append(n)
if goal:
while prev[goal] != start:
goal = prev[goal]
dx = (goal[0] - start[0] + MW // 2) % MW - MW // 2
dy = goal[1] - start[1]
want = RIGHT if dx > 0 else LEFT if dx < 0 else DOWN if dy > 0 else UP
b.run_frames(1, want)
frame += 1
if frame == 900 and a.shots: b.shot(os.path.join(a.shots, "3-midgame.png"), font)
if "CAVE 2" in screen()[0]:
break
print(screen()[0])
check("autopilot gathered every crystal in %d frames" % frame, "CAVE 2" in screen()[0])
b.run_frames(110) # the next cave's GET READY
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc gives up the game", "PRESS SPACE TO BEGIN" in "\n".join(screen()))
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc returns to BASIC", b.pc == 0)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()
+13
View File
@@ -0,0 +1,13 @@
# Start-up for a machine-code program that BASIC CALLs at A200h: clear the
# variables, run main, and return to BASIC. Programs have no initialised
# variables (gen.py's tables are const, so they stay with the code).
.section .text.start, "ax"
.globl _start
_start:
la t0, __bss_start
la t1, __bss_end
1: bgeu t0, t1, 2f
sw zero, 0(t0)
addi t0, t0, 4
j 1b
2: tail main # main's return goes back to BASIC
+22
View File
@@ -0,0 +1,22 @@
/* A game: loaded at A200h inside BASIC's program area (A000h-F0B7h), after
the loader program at its start. Code, constants, then the variables. */
OUTPUT_ARCH("riscv")
ENTRY(_start)
SECTIONS
{
. = 0xA200;
.text : {
KEEP(*(.text.start))
*(.text .text.*)
*(.rodata .rodata.* .srodata .srodata.*)
. = ALIGN(4);
}
.data : { *(.data .data.* .sdata .sdata.*) }
.bss (NOLOAD) : ALIGN(4) {
__bss_start = .;
*(.sbss .sbss.* .bss .bss.* COMMON)
. = ALIGN(4);
__bss_end = .;
}
/DISCARD/ : { *(.comment) *(.riscv.attributes) }
}
+454
View File
@@ -0,0 +1,454 @@
#!/usr/bin/env python3
"""Turn the art and the castle map below into data.h for keep.c.
Shapes and tiles are 16 rows of 16 hex digits, one colour per pixel (the
terminal's 16 colours; 0 is see-through). The castle is 4 x 3 rooms; each room
is 18 x 12 tiles inside a wall border that gen.py adds, with gaps where two
rooms connect. Map characters:
# wall D locked door (a key opens it) X stairs out
. floor P where the player starts
K key G gold chest A ammunition C the crown
h guard patrolling left-right v guard patrolling up-down
c guard that chases you once it has seen you
The script checks that every room's objects fit the sprite budget and that
everything can be reached from the start, counting a key for every door.
"""
import sys
from collections import deque
# ------------------------------------------------------------------ shapes
PLAYER_DOWN_TOP = [
"0000033333300000",
"0000333333330000",
"0000399999930000",
"0000390990930000",
"0000099999900000",
"0000000990000000",
]
PLAYER_UP_TOP = [
"0000033333300000",
"0000333333330000",
"0000333333330000",
"0000333333330000",
"0000033333300000",
"0000000990000000",
]
BODY_FRONT = [
"000CCCCCCCCCC000",
"00CCCCCCCCCCCC00",
"009CCCCCCCCCC900",
"009CCCCCCCCCC900",
"0000CCCCCCCC0000",
"0000444444440000",
]
LEGS_FRONT_A = [
"0000444004440000",
"0000444004440000",
"0000777007770000",
"0000000000000000",
]
LEGS_FRONT_B = [
"0000044004400000",
"0000044004400000",
"0000077007700000",
"0000000000000000",
]
PLAYER_SIDE_TOP = [
"0000033333300000",
"0000333333330000",
"0000333399990000",
"0000333990990000",
"0000033999990000",
"0000000999000000",
"00000CCCCCC00000",
"0000CCCCCCCC0000",
"0000CCCCC9990000",
"0000CCCCCC000000",
"00000CCCCC000000",
"0000044444400000",
]
LEGS_SIDE_A = [
"0000044004400000",
"0000444000440000",
"0000777000770000",
"0000000000000000",
]
LEGS_SIDE_B = [
"0000004444000000",
"0000004444000000",
"0000007777000000",
"0000000000000000",
]
PLAYER = [
PLAYER_DOWN_TOP + BODY_FRONT + LEGS_FRONT_A,
PLAYER_DOWN_TOP + BODY_FRONT + LEGS_FRONT_B,
PLAYER_UP_TOP + BODY_FRONT + LEGS_FRONT_A,
PLAYER_UP_TOP + BODY_FRONT + LEGS_FRONT_B,
PLAYER_SIDE_TOP + LEGS_SIDE_A,
PLAYER_SIDE_TOP + LEGS_SIDE_B,
]
# guards: grey helmet, green tunic, dark grey trousers, brown boots
GUARD = [[r.translate(str.maketrans("3C47", "7283")) for r in s] for s in PLAYER]
def dot(c):
rows = ["0" * 16] * 16
for y in range(6, 10):
rows[y] = "000000" + c * 4 + "000000"
return rows
KEY = ["0" * 16] * 3 + [
"000BBB0000000000",
"00B000B000000000",
"00B000BBBBBBBBB0",
"00B000B0000B0B00",
"000BBB00000B0B00",
] + ["0" * 16] * 8
CHEST = ["0" * 16] * 3 + [
"0003333333333000",
"0033333333333300",
"003BBBBBBBBBB300",
"0033333BB3333300",
"0033333BB3333300",
"0033333333333300",
"0033333333333300",
"003BBBBBBBBBB300",
"0033333333333300",
] + ["0" * 16] * 4
AMMO = ["0" * 16] * 3 + [
"0022222222222200",
"0028888888888200",
"0028BB8BB8BB8200",
"0028BB8BB8BB8200",
"0028BB8BB8BB8200",
"0028888888888200",
"0022222222222200",
] + ["0" * 16] * 6
CROWN = ["0" * 16] * 3 + [
"000B000BB000B000",
"000BB0BBBB0BB000",
"000BBBBBBBBBB000",
"000BB9BBBB9BB000",
"000BBBBCCBBBB000",
"000BBBBBBBBBB000",
"0000000000000000",
"000BBBBBBBBBB000",
] + ["0" * 16] * 5
PUFF = [
"0000000000000000",
"000F00000000F000",
"0000F00BB00F0000",
"00000F0BB0F00000",
"000000BFFB000000",
"0BBB0BFFFFB0BBB0",
"000000BFFB000000",
"00000F0BB0F00000",
"0000F00BB00F0000",
"000F00000000F000",
] + ["0" * 16] * 6
SHAPES = PLAYER + GUARD + [dot("B"), dot("9"), KEY, CHEST, AMMO, CROWN, PUFF]
SHAPE_NAMES = ["S_PDOWN", "S_PDOWN2", "S_PUP", "S_PUP2", "S_PSIDE", "S_PSIDE2",
"S_GDOWN", "S_GDOWN2", "S_GUP", "S_GUP2", "S_GSIDE", "S_GSIDE2",
"S_SHOT", "S_GSHOT", "S_KEY", "S_CHEST", "S_AMMO", "S_CROWN", "S_PUFF"]
# ------------------------------------------------------------------- tiles
WALL = (["8888888888888888", "7777777877777778", "7777777877777778", "7777777877777778",
"8888888888888888", "7778777777787777", "7778777777787777", "7778777777787777"] * 2)
DOOR = (["8888888888888888"] + ["8333833383338338"] * 6 +
["8333833BB3338338"] * 2 + ["8333833383338338"] * 6 + ["8888888888888888"])
STAIRS = ["EEEEEEEEEEEEEEEE", "6666666666666666", "8888888888888888", "0000000000000000"] * 4
TILES = [["0" * 16] * 16, WALL, DOOR, STAIRS] # floor, wall, door, stairs
# -------------------------------------------------------------------- map
ROOMS = [[None] * 4 for _ in range(3)]
NAMES = [[None] * 4 for _ in range(3)]
def room(r, c, name, rows):
ROOMS[r][c] = rows
NAMES[r][c] = name
room(0, 0, "GATEHOUSE", [
"..................",
"..P...............",
"..................",
"....####....####..",
"....#..........#..",
"....#...A......#..",
"....#..........#..",
"....####....####..",
"..................",
"..................",
"..................",
"..................",
])
room(0, 1, "GUARD ROOM", [
"..................",
".h................",
"..................",
"..###.......###...",
"..#...........#...",
"..#.....K.....#...",
"..#...........#...",
"..###.......###...",
"..................",
"...........h......",
"..................",
"..................",
])
room(0, 2, "CHAPEL", [
"..................",
".v..........v.....",
"..................",
"......######......",
"......#G.G#.......",
"......#...D.......",
"......#####.......",
"..................",
"..................",
"....h.............",
"..................",
"..................",
])
room(0, 3, "ARMOURY", [
"..................",
"..A............A..",
"..................",
".####.......####..",
"..................",
"........h.........",
"..................",
".####.......####..",
"..................",
"..................",
".......c..........",
"..................",
])
room(1, 0, "CORRIDOR", [
"..................",
".v.......####.....",
".........#........",
".........#....h...",
"#######..#........",
"..................",
"..................",
"#######..########.",
".........#........",
"..A......#........",
".........#....v...",
"..................",
])
room(1, 1, "GREAT HALL", [
"..................",
"..G............G..",
"..................",
"....##......##....",
"....##..h...##....",
"..................",
"..................",
"....##......##....",
"....##...h..##....",
"..................",
".c...........v....",
"..................",
])
room(1, 2, "CELLARS", [
"...#.......#......",
"...#.#####.#.####.",
"...#.#.....#....#.",
".....#.#######..#.",
"######.#.....#..#.",
".......#..A..#....",
".#######.....###..",
".#.......###......",
".#.#######.#.####.",
".#.........#.#....",
".#####.#####.#.h..",
".......#.......#..",
])
room(1, 3, "LIBRARY", [
"..................",
".##.##.##.##.##.#.",
"..................",
".##.##.##.##.##.#.",
"..........h.......",
"..................",
".##.##.##.##.##.#.",
"..................",
".##.##.##.##.##.#.",
"........K.........",
"...v..............",
"..................",
])
room(2, 0, "STAIRWELL", [
"..................",
"..................",
"..######..######..",
"..#............#..",
"..#.....XX.....#..",
"..#.....XX.....#..",
"..#............#..",
"..######..######..",
"..................",
"....h.............",
"..................",
"..................",
])
room(2, 1, "DUNGEON", [
".#..#..#..#..#..#.",
".#..#..#..#..#..#.",
".#..#..#..#..#..#.",
".##.####..####.##.",
"..................",
"........v.........",
"..................",
".##.####..####.##.",
".#..#..#..#..#..#.",
".#K.#..#..#.G#..#.",
".#..#..#..#..#..#.",
"..................",
])
room(2, 2, "TREASURY", [
"..................",
"..h...............",
"..................",
".....##########...",
".....#G..G...G#...",
".....#........#...",
".....######D###...",
"..................",
"..................",
".........v........",
"..................",
"..................",
])
room(2, 3, "THRONE ROOM", [
"#######D##########",
"..................",
".c..............c.",
"..................",
"...####....####...",
"...#..........#...",
"...#....C.....#...",
"...#..........#...",
"...####....####...",
"..................",
".v..............v.",
"..................",
])
# links: (room, room) side by side or one above the other
H_LINKS = {(0, 0), (0, 1), (0, 2), (1, 0), (1, 1), (1, 2), (2, 0), (2, 1)} # (r, c) to (r, c+1)
V_LINKS = {(0, 0), (1, 0), (0, 3), (1, 3), (1, 1)} # (r, c) to (r+1, c)
W, H = 20, 14
GAP_ROWS = (6, 7) # side gaps, in room rows
GAP_COLS = (8, 9) # top/bottom gaps, in room columns
OBJ = "PKGAChvc"
def full_room(r, c):
inner = ROOMS[r][c]
assert len(inner) == 12 and all(len(x) == 18 for x in inner), NAMES[r][c]
g = [["#"] * W for _ in range(H)]
for y in range(12):
for x in range(18):
g[y + 1][x + 1] = inner[y][x]
if (r, c) in H_LINKS:
for y in GAP_ROWS: g[y][W - 1] = "."
if (r, c - 1) in H_LINKS:
for y in GAP_ROWS: g[y][0] = "."
if (r, c) in V_LINKS:
for x in GAP_COLS: g[H - 1][x] = "."
if (r - 1, c) in V_LINKS:
for x in GAP_COLS: g[0][x] = "."
return g
FULL = [[full_room(r, c) for c in range(4)] for r in range(3)]
# one big grid to check reachability across rooms
BIG = [[FULL[gy // H][gx // W][gy % H][gx % W] for gx in range(4 * W)] for gy in range(3 * H)]
def check():
start = [(x, y) for y in range(3 * H) for x in range(4 * W) if BIG[y][x] == "P"]
assert len(start) == 1
doors = sum(row.count("D") for row in BIG)
keys = sum(row.count("K") for row in BIG)
assert keys >= doors, f"{keys} keys for {doors} doors"
# with every door open, every non-wall tile must be reachable
seen = {start[0]}
q = deque(seen)
while q:
x, y = q.popleft()
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
nx, ny = x + dx, y + dy
if 0 <= nx < 4 * W and 0 <= ny < 3 * H and (nx, ny) not in seen and BIG[ny][nx] != "#":
seen.add((nx, ny)); q.append((nx, ny))
lost = [(x, y, BIG[y][x]) for y in range(3 * H) for x in range(4 * W)
if BIG[y][x] != "#" and (x, y) not in seen]
objs_lost = [t for t in lost if t[2] != "."]
assert not objs_lost, f"unreachable objects: {objs_lost}"
# rooms' sprite budgets: 6 guards, 8 items
for r in range(3):
for c in range(4):
s = "".join("".join(row) for row in FULL[r][c])
assert sum(s.count(ch) for ch in "hvc") <= 6, NAMES[r][c]
assert sum(s.count(ch) for ch in "KGAC") <= 8, NAMES[r][c]
assert s.count("D") <= 8, NAMES[r][c]
return len(lost)
def nib(c):
return int(c, 16)
def main(out):
unreach = check()
L = ["/* made by gen.py - edit that instead */", ""]
L.append("#define N_SHAPES %d" % len(SHAPES))
for i, n in enumerate(SHAPE_NAMES):
L.append("#define %s %d" % (n, i))
for i, s in enumerate(SHAPES):
assert len(s) == 16 and all(len(row) == 16 for row in s), SHAPE_NAMES[i]
L.append("static const u8 shape_data[N_SHAPES * 128] = {")
for s in SHAPES:
b = [nib(row[x]) << 4 | nib(row[x + 1]) for row in s for x in range(0, 16, 2)]
L.append(" " + ",".join(str(v) for v in b) + ",")
L.append("};")
L.append("static const u8 tile_data[4 * 256] = {")
for t in TILES:
assert len(t) == 16 and all(len(row) == 16 for row in t)
L.append(" " + ",".join(str(nib(ch)) for row in t for ch in row) + ",")
L.append("};")
# rooms: tiles packed two per byte (low nibble first), then object lists
code = {".": 0, "#": 1, "D": 2, "X": 3}
L.append("static const u8 room_tiles[12][140] = {")
objs = []
for r in range(3):
for c in range(4):
g = FULL[r][c]
t = [code.get(g[y][x], 0) for y in range(H) for x in range(W)]
L.append(" {" + ",".join(str(t[i] | t[i + 1] << 4) for i in range(0, W * H, 2)) + "},")
ol = [(g[y][x], x, y) for y in range(H) for x in range(W) if g[y][x] in OBJ]
objs.append(ol)
L.append("};")
L.append("/* objects: type, tile x, tile y; ends with 0 */")
L.append("static const u8 room_objs[12][3 * 16 + 1] = {")
for ol in objs:
assert len(ol) <= 16
L.append(" {" + ",".join("'%s',%d,%d" % o for o in ol) + (",0}," if ol else "0},"))
L.append("};")
L.append("static const char *const room_names[12] = {")
L.append(" " + ", ".join('"%s"' % NAMES[r][c] for r in range(3) for c in range(4)))
L.append("};")
open(out, "w").write("\n".join(L) + "\n")
print("data.h: %d shapes, 12 rooms (%d floor tiles only reachable through nothing)" %
(len(SHAPES), unreach))
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv) > 1 else "data.h")
+4
View File
@@ -0,0 +1,4 @@
10 REM THE IRON KEEP: LOAD THE GAME (MEMORY FILE KEEPGAME) AND PLAY IT
20 REM THE GAME RUNS AT A200H, PAST THIS PROGRAM, AND COMES BACK HERE
30 BLOAD "KEEPGAME": CALL 41472
40 CLS
+647
View File
@@ -0,0 +1,647 @@
/* The Iron Keep: a top-down escape game for the Basys 3 RISC-V computer.
You are a prisoner in a castle of twelve rooms. Steal the crown from the
throne room and get out down the stairs. Guards patrol, and shoot when they
can see you along a row or a column; some come after you. Keys open locked
doors, chests hold gold, crates hold ammunition.
It runs at A200h, loaded by the KEEP program in BASIC, and returns to BASIC
when you quit. It uses the hardware sprites (ports 70h-78h), the graphics
layer (30h-34h), the held-key port (88h) and the terminal for text.
Build with build.sh; gen.py makes data.h from the art and the map. */
typedef unsigned char u8;
typedef signed char i8;
typedef unsigned short u16;
typedef short i16;
#include "basys3.h"
#define con_stat IO8(0x00) /* bit 0: key waiting, bit 1: can send */
#define con_data IO8(0x01)
#define gx_lo IO8(0x30)
#define gx_hi IO8(0x31)
#define gy_p IO8(0x32)
#define gpix IO8(0x33) /* plot, then X + 1 */
#define gfill IO8(0x34) /* fill the graphics (and turn sprites off) */
#define sp_sel IO8(0x70)
#define sp_xlo IO8(0x71)
#define sp_xhi IO8(0x72)
#define sp_y IO8(0x73)
#define sp_shape IO8(0x74)
#define sp_ctl IO8(0x75)
#define sp_load IO8(0x76)
#define sp_data IO8(0x77)
#define frame_ctr IO8(0x78)
#define joy IO8(0x88)
#include "data.h" /* out/data.h, made by gen.py */
#define J_UP 1
#define J_DOWN 2
#define J_LEFT 4
#define J_RIGHT 8
#define J_FIRE 16
#define J_ENTER 32
#define J_ESC 128
/* directions */
#define D_DOWN 0
#define D_UP 1
#define D_LEFT 2
#define D_RIGHT 3
static const i8 dir_dx[4] = { 0, 0, -1, 1 };
static const i8 dir_dy[4] = { 1, -1, 0, 0 };
/* tiles */
#define T_FLOOR 0
#define T_WALL 1
#define T_DOOR 2
#define T_STAIRS 3
#define T_OUT 0xFF
/* sprite numbers: lower numbers are in front */
#define SP_PLAYER 0
#define SP_PSHOT 1 /* 2 */
#define SP_GUARD 3 /* 6 */
#define SP_GSHOT 9 /* 4 */
#define SP_ITEM 13 /* 8 */
#define SP_PUFF 21
#define NG 6
#define NI 8
#define NPS 2
#define NGS 4
/* the hit box inside a 16 x 16 figure */
#define HB_X0 3
#define HB_X1 12
#define HB_Y0 2
#define HB_Y1 14
/* -------------------------------------------------------------- state */
static u8 map[14][20];
/* not static: test-keep.py finds these in the link map */
u8 room; /* 0-11: row * 4 + column */
static u16 gone[12]; /* objects taken or guards beaten, by index */
static u8 opened[12]; /* doors opened, by order in the room */
i16 px, py; /* player */
static i16 ex, ey; /* where it entered the room */
static u8 pdir, panim, fire_cool, fire_held;
u8 inv, lives, keys, ammo, crown;
static u16 gold, kills;
static i16 gx[NG], gy[NG];
static u8 gdir[NG], gon[NG], gkind[NG], gcool[NG], gsee[NG], gobj[NG];
static i16 ix[NI], iy[NI];
static u8 itype[NI], ion[NI], iobj[NI];
static i16 psx[NPS], psy[NPS], gsx[NGS], gsy[NGS];
static u8 psd[NPS], pson[NPS], gsd[NGS], gson[NGS];
static u8 puff;
static i16 puff_x, puff_y;
static u8 last_frame, quit, status_dirty, msg_time;
u8 over, won;
static const char *msg;
static u16 rnd_state;
/* -------------------------------------------------------------- text */
static void putch(char c) { while (!(con_stat & 2)); con_data = c; }
static void puts_(const char *s) { while (*s) putch(*s++); }
static void putn(u16 n)
{
char b[6];
u8 i = 0;
do { b[i++] = '0' + n % 10; n /= 10; } while (n);
while (i) putch(b[--i]);
}
static void at(u8 r, u8 c) { putch(27); putch('['); putn(r); putch(';'); putn(c); putch('H'); }
static void ink(const char *s) { putch(27); putch('['); puts_(s); putch('m'); }
static void centre(u8 r, const char *s)
{
u8 n = 0;
while (s[n]) n++;
at(r, 41 - n / 2);
puts_(s);
}
static u16 rnd(void)
{
rnd_state ^= rnd_state << 7;
rnd_state ^= rnd_state >> 9;
rnd_state ^= rnd_state << 8;
return rnd_state;
}
static void wait_frame(void)
{
while (frame_ctr == last_frame);
last_frame = frame_ctr;
}
/* Keys also arrive as characters; throw them away (P pauses). */
static u8 drain_keys(void)
{
u8 c, p = 0;
while (con_stat & 1) {
c = con_data;
if (c == 'p' || c == 'P') p = 1;
}
return p;
}
/* ----------------------------------------------------------- graphics */
static void clear_gfx(void)
{
gfill = 0; /* also turns every sprite off */
while (gfill & 1);
}
static void draw_tile(u8 tx, u8 ty, u8 t)
{
const u8 *p = tile_data + ((u16)t << 8);
u16 x = tx << 4;
u8 y = 16 + (ty << 4), r, i;
for (r = 0; r < 16; r++) {
gx_lo = x; gx_hi = x >> 8; gy_p = y + r;
for (i = 0; i < 16; i++) gpix = *p++;
}
}
static void spr_at(u8 n, i16 x, i16 y)
{
sp_sel = n; sp_xlo = x; sp_xhi = (x >> 8) & 1; sp_y = y;
}
static void spr_show(u8 n, u8 shape, u8 ctl)
{
sp_sel = n; sp_shape = shape; sp_ctl = ctl;
}
static void spr_off(u8 n) { sp_sel = n; sp_ctl = 0; }
static void load_shapes(void)
{
const u8 *p = shape_data;
u8 s, i;
for (s = 0; s < N_SHAPES; s++) {
sp_load = s;
for (i = 0; i < 128; i++) sp_data = *p++;
}
}
/* --------------------------------------------------------------- map */
static u8 tile_at(i16 x, i16 y)
{
if (x < 0 || x > 319 || y < 16 || y > 239) return T_OUT;
return map[(u8)((y - 16) >> 4)][(u8)(x >> 4)];
}
static u8 hit_tx, hit_ty;
/* What stops a figure at (x, y): 0 nothing, T_WALL or T_DOOR (hit_tx/ty say
which tile). Off the room is open for the player, a wall for guards. */
static u8 blocked(i16 x, i16 y, u8 guard)
{
static const u8 cx[4] = { HB_X0, HB_X1, HB_X0, HB_X1 };
static const u8 cy[4] = { HB_Y0, HB_Y0, HB_Y1, HB_Y1 };
u8 i, t;
for (i = 0; i < 4; i++) {
t = tile_at(x + cx[i], y + cy[i]);
if (t == T_WALL || t == T_DOOR) {
hit_tx = (u8)((x + cx[i]) >> 4);
hit_ty = (u8)((y + cy[i] - 16) >> 4);
return t;
}
if (t == T_OUT && guard) return T_WALL;
}
return 0;
}
static u8 door_index(u8 tx, u8 ty)
{
const u8 *p = room_tiles[room];
u8 n = 0, i, t;
u16 k, want = ty * 20 + tx;
for (k = 0; k < want; k++) {
i = p[k >> 1];
t = (k & 1) ? i >> 4 : i & 15;
if (t == T_DOOR) n++;
}
return n;
}
static void say(const char *s)
{
msg = s;
msg_time = 180;
status_dirty = 1;
}
/* ------------------------------------------------------------- status */
static void status(void)
{
status_dirty = 0;
at(1, 1); ink("0;93"); puts_("THE IRON KEEP"); ink("0;37"); puts_(" ");
puts_(room_names[room]);
puts_("\033[K");
if (msg && msg_time) { at(1, 40); ink("0;96"); puts_(msg); }
at(2, 1); ink("0;37");
puts_("LIVES "); ink("0;97"); putn(lives); ink("0;37");
puts_(" KEYS "); ink("0;97"); putn(keys); ink("0;37");
puts_(" AMMO "); ink("0;97"); putn(ammo); ink("0;37");
puts_(" GOLD "); ink("0;97"); putn(gold); ink("0;37");
if (crown) { puts_(" "); ink("0;93"); puts_("CROWN"); }
puts_("\033[K");
ink("0");
}
/* --------------------------------------------------------------- rooms */
static void enter_room(void)
{
const u8 *p = room_tiles[room], *o = room_objs[room];
u8 x, y, i, t, d = 0, ng = 0, ni = 0, k = 0;
clear_gfx();
for (y = 0; y < 14; y++)
for (x = 0; x < 20; x += 2) {
i = *p++;
map[y][x] = i & 15;
map[y][x + 1] = i >> 4;
}
for (y = 0; y < 14; y++)
for (x = 0; x < 20; x++) {
t = map[y][x];
if (t == T_DOOR && (opened[room] & (1 << d++))) t = map[y][x] = T_FLOOR;
if (t != T_FLOOR) draw_tile(x, y, t);
}
for (i = 0; i < NG; i++) gon[i] = 0;
for (i = 0; i < NI; i++) ion[i] = 0;
for (; *o; o += 3, k++) {
if (gone[room] & (1 << k)) continue;
t = o[0];
if (t == 'h' || t == 'v' || t == 'c') {
gx[ng] = o[1] << 4; gy[ng] = 16 + (o[2] << 4);
gkind[ng] = t; gon[ng] = 1; gsee[ng] = 0; gobj[ng] = k;
gdir[ng] = t == 'v' ? D_DOWN : D_RIGHT;
gcool[ng] = 30 + (rnd() & 31);
ng++;
} else if (t != 'P') {
ix[ni] = o[1] << 4; iy[ni] = 16 + (o[2] << 4);
itype[ni] = t; ion[ni] = 1; iobj[ni] = k;
spr_at(SP_ITEM + ni, ix[ni], iy[ni]);
spr_show(SP_ITEM + ni, t == 'K' ? S_KEY : t == 'G' ? S_CHEST : t == 'A' ? S_AMMO : S_CROWN, 1);
ni++;
}
}
for (i = 0; i < NPS; i++) pson[i] = 0;
for (i = 0; i < NGS; i++) gson[i] = 0;
puff = 0;
ex = px; ey = py;
status_dirty = 1;
}
/* Leave by an edge into the next room. */
static void edges(void)
{
if (px + HB_X0 < 0) { room--; px = 304; }
else if (px + HB_X1 > 319) { room++; px = 0; }
else if (py + HB_Y0 < 16) { room -= 4; py = 224; }
else if (py + HB_Y1 > 239) { room += 4; py = 16; }
else return;
enter_room();
}
/* ------------------------------------------------------------- player */
static void open_door(void)
{
if (!keys) { say("LOCKED. YOU NEED A KEY."); return; }
keys--;
opened[room] |= 1 << door_index(hit_tx, hit_ty);
map[hit_ty][hit_tx] = T_FLOOR;
draw_tile(hit_tx, hit_ty, T_FLOOR);
say("THE KEY TURNS. THE DOOR OPENS.");
}
static void move_player(u8 j)
{
i16 n;
u8 t, moved = 0;
if (j & (J_LEFT | J_RIGHT)) {
pdir = (j & J_LEFT) ? D_LEFT : D_RIGHT;
n = px + ((j & J_LEFT) ? -2 : 2);
t = blocked(n, py, 0);
if (!t) { px = n; moved = 1; }
else if (t == T_DOOR) open_door();
}
if (j & (J_UP | J_DOWN)) {
if (!(j & (J_LEFT | J_RIGHT))) pdir = (j & J_UP) ? D_UP : D_DOWN;
n = py + ((j & J_UP) ? -2 : 2);
t = blocked(px, n, 0);
if (!t) { py = n; moved = 1; }
else if (t == T_DOOR) open_door();
}
if (moved) panim++;
}
static void fire(u8 j)
{
u8 i;
if (!(j & J_FIRE)) { fire_held = 0; return; }
if (fire_held || fire_cool) return;
fire_held = 1;
if (!ammo) { say("OUT OF AMMUNITION!"); return; }
for (i = 0; i < NPS; i++)
if (!pson[i]) {
pson[i] = 1; psd[i] = pdir;
psx[i] = px + 8 + dir_dx[pdir] * 6;
psy[i] = py + 8 + dir_dy[pdir] * 6;
ammo--; fire_cool = 8; status_dirty = 1;
spr_show(SP_PSHOT + i, S_SHOT, 1);
return;
}
}
static void player_hit(void)
{
u8 i;
if (inv || over) return;
puff = 30; puff_x = px; puff_y = py;
if (!--lives) { over = 1; won = 0; return; }
say("YOU ARE HIT!");
px = ex; py = ey; inv = 120;
for (i = 0; i < NGS; i++) { gson[i] = 0; spr_off(SP_GSHOT + i); }
}
static u8 overlap(i16 ax, i16 ay, i16 bx, i16 by)
{
return ax + HB_X0 <= bx + HB_X1 && bx + HB_X0 <= ax + HB_X1 &&
ay + HB_Y0 <= by + HB_Y1 && by + HB_Y0 <= ay + HB_Y1;
}
static void pick_up(void)
{
u8 i;
for (i = 0; i < NI; i++) {
if (!ion[i] || !overlap(px, py, ix[i], iy[i])) continue;
ion[i] = 0;
gone[room] |= 1 << iobj[i];
spr_off(SP_ITEM + i);
switch (itype[i]) {
case 'K': keys++; say("A KEY!"); break;
case 'G': gold += 100; say("GOLD! 100 CROWNS."); break;
case 'A': ammo += 5; say("FIVE MORE SHOTS."); break;
case 'C': crown = 1; say("THE CROWN! NOW FIND THE STAIRS."); break;
}
}
if (tile_at(px + 8, py + 8) == T_STAIRS) {
if (crown) { over = 1; won = 1; }
else if (msg_time < 120) say("NOT WITHOUT THE CROWN.");
}
}
/* -------------------------------------------------------------- guards */
/* Can guard i see the player along a row or a column? Returns the direction
to look in, plus 1, or 0. */
static u8 sees(u8 i)
{
i16 gcx = gx[i] + 8, gcy = gy[i] + 8, pcx = px + 8, pcy = py + 8, d;
u8 a, b, t, k;
d = gcy - pcy;
if (d > -8 && d < 8) {
k = (u8)((gcy - 16) >> 4);
a = (u8)(gcx >> 4); b = (u8)(pcx >> 4);
if (a > b) { t = a; a = b; b = t; }
for (; a <= b; a++) if (map[k][a] == T_WALL || map[k][a] == T_DOOR) return 0;
return (pcx < gcx ? D_LEFT : D_RIGHT) + 1;
}
d = gcx - pcx;
if (d > -8 && d < 8) {
k = (u8)(gcx >> 4);
a = (u8)((gcy - 16) >> 4); b = (u8)((pcy - 16) >> 4);
if (a > b) { t = a; a = b; b = t; }
for (; a <= b; a++) if (map[a][k] == T_WALL || map[a][k] == T_DOOR) return 0;
return (pcy < gcy ? D_UP : D_DOWN) + 1;
}
return 0;
}
static void guard_shoots(u8 i)
{
u8 k;
for (k = 0; k < NGS; k++)
if (!gson[k]) {
gson[k] = 1; gsd[k] = gdir[i];
gsx[k] = gx[i] + 8 + dir_dx[gdir[i]] * 6;
gsy[k] = gy[i] + 8 + dir_dy[gdir[i]] * 6;
spr_show(SP_GSHOT + k, S_GSHOT, 1);
gcool[i] = 45 + (rnd() & 63);
return;
}
}
static void guards(void)
{
u8 i, s, d;
i16 nx, ny;
for (i = 0; i < NG; i++) {
if (!gon[i]) continue;
if (gcool[i]) gcool[i]--;
s = inv ? 0 : sees(i);
if (s) {
gdir[i] = s - 1;
if (!gsee[i]) { gsee[i] = 1; if (gcool[i] < 25) gcool[i] = 25; }
if (!gcool[i]) guard_shoots(i);
if (gkind[i] != 'c') goto shown; /* the others stand and shoot */
} else
gsee[i] = 0;
d = gdir[i];
nx = gx[i] + dir_dx[d];
ny = gy[i] + dir_dy[d];
if (blocked(nx, ny, 1)) {
/* turn round, or for a chaser, try the other way across */
if (gkind[i] == 'c' && !s) gdir[i] = (rnd() & 3);
else if (!s) gdir[i] = d ^ 1;
} else {
gx[i] = nx; gy[i] = ny;
}
if (overlap(gx[i], gy[i], px, py)) player_hit();
shown:
d = gdir[i];
spr_at(SP_GUARD + i, gx[i], gy[i]);
spr_show(SP_GUARD + i,
(d == D_DOWN ? S_GDOWN : d == D_UP ? S_GUP : S_GSIDE) + (((gx[i] + gy[i]) >> 3) & 1),
d == D_LEFT ? 3 : 1);
}
}
/* ------------------------------------------------------------- bullets */
static void shots(void)
{
u8 i, k;
for (i = 0; i < NPS; i++) {
if (!pson[i]) continue;
psx[i] += dir_dx[psd[i]] * 5;
psy[i] += dir_dy[psd[i]] * 5;
k = tile_at(psx[i], psy[i]);
if (k == T_WALL || k == T_DOOR || k == T_OUT) { pson[i] = 0; spr_off(SP_PSHOT + i); continue; }
for (k = 0; k < NG; k++)
if (gon[k] && psx[i] >= gx[k] + HB_X0 && psx[i] <= gx[k] + HB_X1 &&
psy[i] >= gy[k] + HB_Y0 && psy[i] <= gy[k] + HB_Y1) {
gon[k] = 0;
gone[room] |= 1 << gobj[k];
spr_off(SP_GUARD + k);
pson[i] = 0; spr_off(SP_PSHOT + i);
puff = 20; puff_x = gx[k]; puff_y = gy[k];
kills++;
break;
}
if (pson[i]) spr_at(SP_PSHOT + i, psx[i] - 8, psy[i] - 8);
}
for (i = 0; i < NGS; i++) {
if (!gson[i]) continue;
gsx[i] += dir_dx[gsd[i]] * 3;
gsy[i] += dir_dy[gsd[i]] * 3;
k = tile_at(gsx[i], gsy[i]);
if (k == T_WALL || k == T_DOOR || k == T_OUT) { gson[i] = 0; spr_off(SP_GSHOT + i); continue; }
if (gsx[i] >= px + HB_X0 && gsx[i] <= px + HB_X1 && gsy[i] >= py + HB_Y0 && gsy[i] <= py + HB_Y1) {
gson[i] = 0; spr_off(SP_GSHOT + i);
player_hit();
continue;
}
spr_at(SP_GSHOT + i, gsx[i] - 8, gsy[i] - 8);
}
}
static void draw_player(void)
{
u8 s = pdir == D_DOWN ? S_PDOWN : pdir == D_UP ? S_PUP : S_PSIDE;
spr_at(SP_PLAYER, px, py);
spr_show(SP_PLAYER, s + ((panim >> 3) & 1),
(inv & 4) ? 0 : pdir == D_LEFT ? 3 : 1);
if (puff) {
spr_at(SP_PUFF, puff_x, puff_y);
spr_show(SP_PUFF, S_PUFF, --puff ? 1 : 0);
}
}
/* -------------------------------------------------------------- screens */
static u8 wait_fire(void)
{
u8 j;
while (joy & (J_FIRE | J_ENTER | J_ESC)) { wait_frame(); drain_keys(); }
for (;;) {
wait_frame(); drain_keys(); rnd();
j = joy;
if (j & J_ESC) return 0;
if (j & (J_FIRE | J_ENTER)) return 1;
}
}
static u8 title(void)
{
u8 i;
clear_gfx();
puts_("\033[0m\033[2J");
for (i = 0; i < 20; i++) { draw_tile(i, 0, T_WALL); draw_tile(i, 13, T_WALL); }
for (i = 1; i < 13; i++) { draw_tile(0, i, T_WALL); draw_tile(19, i, T_WALL); }
draw_tile(9, 13, T_DOOR); draw_tile(10, 13, T_DOOR);
spr_at(0, 120, 168); spr_show(0, S_PDOWN, 1);
spr_at(1, 152, 168); spr_show(1, S_CROWN, 1);
spr_at(2, 184, 168); spr_show(2, S_GSIDE, 3);
spr_at(3, 72, 168); spr_show(3, S_GSIDE, 1);
ink("1;93"); centre(7, "T H E I R O N K E E P");
ink("0;37");
centre(10, "You are a prisoner in the Iron Keep. Steal the king's crown");
centre(11, "from the throne room, and escape down the stairs.");
centre(14, "ARROWS or W A S D move SPACE or CTRL shoot");
centre(15, "Keys open locked doors. Chests hold gold, crates hold shots.");
centre(16, "Guards see along rows and columns - and they shoot.");
centre(17, "P pauses. ESC gives up.");
ink("1;96"); centre(25, "PRESS SPACE TO BEGIN");
ink("0");
return wait_fire();
}
static u8 end_screen(void)
{
u16 score = gold + kills * 50;
u8 j;
if (won) score += 1000 + 500 + lives * 200;
else if (crown) score += 1000;
clear_gfx(); /* a clean box: no walls or figures behind it */
ink("0;44;97");
centre(12, " ");
centre(13, won ? " YOU ESCAPED WITH THE CROWN! " :
" THE GUARDS HAVE TAKEN YOU. ");
centre(14, " ");
at(15, 21); puts_(" SCORE "); putn(score);
for (j = 10 + (score > 9) + (score > 99) + (score > 999) + (score > 9999); j < 40; j++) putch(' ');
centre(16, " ");
centre(17, " SPACE TO PLAY AGAIN, ESC TO STOP ");
centre(18, " ");
ink("0");
return wait_fire();
}
/* ----------------------------------------------------------------- game */
static void play(void)
{
u8 j, i;
const u8 *o = room_objs[0];
for (i = 0; i < 12; i++) { gone[i] = 0; opened[i] = 0; }
lives = 3; keys = 0; ammo = 10; gold = 0; kills = 0; crown = 0;
over = 0; won = 0; inv = 0; fire_cool = 0; fire_held = 1; panim = 0;
msg = 0; msg_time = 0; pdir = D_DOWN;
room = 0;
for (; *o; o += 3)
if (o[0] == 'P') { px = o[1] << 4; py = 16 + (o[2] << 4); }
puts_("\033[2J");
enter_room();
say("FIND THE CROWN. MIND THE GUARDS.");
while (!over) {
wait_frame();
if (drain_keys()) { /* P: pause until P again */
at(1, 40); ink("0;96"); puts_("PAUSED - P TO GO ON\033[K"); ink("0");
for (;;) {
wait_frame();
if (joy & J_ESC) { quit = 1; return; }
if (drain_keys()) break;
}
status_dirty = 1;
}
j = joy;
if (j & J_ESC) { over = 1; won = 0; break; }
move_player(j);
edges();
fire(j);
shots();
guards();
pick_up();
draw_player();
if (inv) inv--;
if (fire_cool) fire_cool--;
if (msg_time && !--msg_time) status_dirty = 1;
if (status_dirty) status();
}
status();
for (i = 0; i < 60; i++) { wait_frame(); drain_keys(); draw_player(); }
}
int main(void)
{
rnd_state = 0xACE1 ^ frame_ctr;
last_frame = frame_ctr;
puts_("\033[?3l\033[?25l"); /* 80 columns (this clears), no cursor */
load_shapes();
quit = 0;
while (!quit) {
if (!title()) break;
play();
if (quit) break;
if (!end_screen()) break;
}
clear_gfx();
puts_("\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+989
View File
@@ -0,0 +1,989 @@
:10A2000097420000938282DA17430000130343FB56
:10A2100063F8620023A00200938242006FF05FFFA8
:10A2200017130000670083E5834700F093F72700CA
:10A23000E38C07FEA300A0F067800000130101FF7C
:10A240002324810023261100130405000345040084
:10A25000631A05008320C10003248100130101015A
:10A26000678000001304140097000000E78000FCE2
:10A270006FF0DFFD130101FE232C8100232E11005E
:10A28000130400009306A000130690003377D50254
:10A290009307040093870701B38727001304140072
:10A2A0001374F40F13070703238CE7FE93070500CD
:10A2B0003355D502E36CF6FC631A04008320C10118
:10A2C0000324810113010102678000001304F4FFDD
:10A2D0001374F40F93070401B387270003C587FFA6
:10A2E00097000000E78080F46FF01FFD130101FF6D
:10A2F00023229100930405001305B00123261100C9
:10A30000232481001384050097000000E78000F2F9
:10A310001305B00597000000E78040F113850400A5
:10A3200097000000E78040F51305B0039700000098
:10A33000E780C0EF1305040097000000E780C0F33A
:10A34000032481008320C10083244100130580047D
:10A350001301010117030000670043ED130101FF22
:10A3600023248100130405001305B00123261100E6
:10A3700097000000E78080EB1305B0059700000010
:10A38000E780C0EA1305040097000000E78040EB77
:10A39000032481008320C1001305D00613010101AD
:10A3A00017030000670083E8130101FF23248100E5
:10A3B0002326110013840500930700003307F400DF
:10A3C00003470700631A070293D71700930590020B
:10A3D000B385F54093F5F50F97000000E78040F155
:10A3E00013050400032481008320C100130101012F
:10A3F000170300006700C3E49387170093F7F70F74
:10A400006FF0DFFBB7E7000003D5A7FA131775005D
:10A410003345E50013150501135505011357950044
:10A420003345E500131785003345E5001315050195
:10A43000135505012395A7FA67800000B7E70000D0
:10A4400003C757FB834680F7E38EE6FE034780F79A
:10A45000A38AE7FA67800000130500009306000551
:10A46000834700F093F717006394070067800000AC
:10A47000034710F01377F70D6304D7009307050027
:10A48000138507006FF0DFFD230A00F2834740F3D6
:10A4900093F71700E39C07FE67800000B7D7000022
:10A4A0009387872213168600939545001315450060
:10A4B0003306F600938505011377F50F93F5F50F35
:10A4C00013558500930706102308E0F2A308A0F2B5
:10A4D0002309B0F29306060103480600130616008E
:10A4E000A30900F3E31AD6FE9385150093F5F50F43
:10A4F000E39CC7FC678000002308A0F693F7F50FE4
:10A5000093D58540A308F0F693F515002309B0F61E
:10A510001376F60FA309C0F66780000093160501B5
:10A5200093D606019307F0131307050063E2D704DF
:10A53000938505FF9396050193D606019307F00DC9
:10A540001305F00F63E8D7029306400193D5454009
:10A55000B385D502B7E700009387470A13574740F2
:10A56000B387B700B387E70003C507006780000023
:10A570001305F00F67800000130101FD232861011E
:10A580002326710137DB0000B7DB00002322910294
:10A59000232E3101232C4101232A5101232481013F
:10A5A000232291012320A1012326110223248102C9
:10A5B00023202103130A0500938A05009304060053
:10A5C00093090000130B0B91938BCB90930C10000D
:10A5D000130DF00F130C4000B387690103C9070086
:10A5E000B387790103C40700330949011315090131
:10A5F000330454019315040193D5054113550541C6
:10A6000097000000E780C0F19307F5FF93F7F70F7D
:10A6100063E0FC06B7E7000013594940130404FF48
:10A62000A38427FB13544440B7E70000238487FA30
:10A63000930405008320C10203248102032901023F
:10A640008329C101032A8101832A4101032B0101CE
:10A65000832BC100032C8100832C4100032D0100BA
:10A660001385040083244102130101036780000065
:10A670006314A501E39004FC93891900E39E89F516
:10A68000930400006FF01FFBB7E70000130740FBC7
:10A6900023A6A7FAB7E700002389E7FAB7E7000087
:10A6A00013071000A389E7FA67800000130101FE79
:10A6B000B7E700009305100013051000232E1100CA
:10A6C000A38907FA232C8100232A91002328210142
:10A6D0002326310123244101B7C90000970000005F
:10A6E000E78000C11385C94A97000000E78040C792
:10A6F00037C500001305454B97000000E78040B4C4
:10A7000037C400001305444C97000000E78040C5A3
:10A7100037CA00001305CA4C97000000E78040B21A
:10A7200037E700000347070AB7D700009387C791B0
:10A7300013172700B387E70003A5070037C90000F8
:10A7400097000000E780C0AF1305094D9700000097
:10A75000E78000AFB7E7000083A4C7FA638E040266
:10A76000B7E7000083C727FB6388070293058002D1
:10A770001305100097000000E78080B737C5000080
:10A780001305454D97000000E78080BD1385040048
:10A7900097000000E780C0AA930510001305200071
:10A7A00097000000E780C0B41305444C97000000F8
:10A7B000E78000BB37C500001305C54DB7C40000D6
:10A7C00097000000E780C0A71385444E9700000063
:10A7D000E78000B9B7E7000003C5B706970000009F
:10A7E000E78080A91305444C97000000E78040B73C
:10A7F00037C500001305C54E97000000E78040A450
:10A800001385444E97000000E78080B5B7E700004D
:10A8100003C5A70697000000E78000A61305444C77
:10A8200097000000E780C0B337C500001305854FCF
:10A8300097000000E780C0A01385444E97000000F9
:10A84000E78000B2B7E7000003C597069700000055
:10A85000E78080A21305444C97000000E78040B0D9
:10A8600037C500001305455097000000E780409D64
:10A870001385444E97000000E78080AEB7E70000E4
:10A8800003D5670697000000E780009F1305444C3E
:10A8900097000000E780C0ACB7E7000083C78706D9
:10A8A000638607021305CA4C97000000E7804099B1
:10A8B0001385C94A97000000E78080AA37C50000C9
:10A8C0001305055197000000E78080971305094D97
:10A8D00097000000E780C096032481018320C10116
:10A8E00083244101032901018329C100032A810036
:10A8F00037C500001305855113010102170300003D
:10A90000670003A6130101FB232E3103B7E9000002
:10A910002324810403C4090A9307C00837D7000021
:10A92000B307F402130787B9232861032326110410
:10A930002322910423202105232C4103232A5103A0
:10A940002326710323248103B387E7001307100331
:10A950003304E40237D700001307C7942326F1001D
:10A96000232291032320A103232EB1013304E40009
:10A9700097000000E78080B18327C10037E700001F
:10A98000130B470A1385C7081307470A93060700E6
:10A990009385A70003C607009387170093862600B8
:10A9A0001378F60013564600238F06FFA38FC6FECA
:10A9B000E392B7FE13074701E39AA7FCB7EB000049
:10A9C000930A000013090000130C2000938BCB079F
:10A9D000930C4001130DE000130A0B0093040000D8
:10A9E00003460A006314861383C7090A13871A00F3
:10A9F0001377F70FB387FB0083C70700B3D757411F
:10AA000093F717006380071223000A00930A0700D8
:10AA10009384140093F4F40F130A1A00E39294FD44
:10AA2000130919001379F90F130B4B01E316A9FB56
:10AA3000B7E400009387C40337E9000023920700BE
:10AA400037EA00009307C9FEB7EA000037EB0000C1
:10AA5000B7EB000023AE040223A0070023A20700E7
:10AA6000930D0000130D0000930C00009384C403A9
:10AA70001309C9FE130A8A08938ACA00130BCBFF75
:10AA8000938B4BFF034C040063120C0AB7E70000E2
:10AA9000239207FCB7E7000023AE07FAB7E70000F0
:10AAA000238D07FAB7E7000003D78707B7E7000051
:10AAB0008320C104239AE706B7E7000003D767079E
:10AAC00003248104B7E700002399E706B7E70000F5
:10AAD00013071000A389E7FA832441040329010422
:10AAE0008329C103032A8103832A4103032B010322
:10AAF000832BC102032C8102832C4102032D01020E
:10AB0000832DC1011301010567800000E30206F0F7
:10AB1000930509001385040097000000E780409822
:10AB20006FF01FEF930A07006FF09FFE83C7090ABB
:10AB300093971700B307FA0083D70700B3D7B7413D
:10AB400093F717006396070E930700056302FC0E48
:10AB50001307DCF91377F70F9307300183451400CF
:10AB60000346240063EEE70CB7070800938717023B
:10AB7000B3D7E70093F717006384070CB7E700002B
:10AB800013971C0093878705B387E70093954500CB
:10AB90002390B700B7E700009387C7041316460059
:10ABA000B387E700130606012390C700B7E700004C
:10ABB00093874703B387970123808701130710000A
:10ABC000B38794012380E700B7E70000938747022B
:10ABD000B387970123800700B7E700009387C70179
:10ABE000B38797012380B7019307600713073000ED
:10ABF0006314FC0013070000B7E7000093874704C5
:10AC0000B38797012380E70097F0FFFFE780C07FBD
:10AC1000B7E700009387C7021375F501B387970163
:10AC20001305E501938C1C002380A70093FCFC0F07
:10AC3000938D1D001304340093FDFD0F6FF09FE40E
:10AC4000131646001306060193171D009395450041
:10AC5000131606013387FA0013560641B307FB00AB
:10AC6000939505012390C70093D50541B387AB01A8
:10AC7000238087012310B700B307A9011307100031
:10AC80002380E700B7E70000938747FE1305DD0048
:10AC9000B387A7011375F50F2380B7012326A10001
:10ACA000130CFCFB97000000E7804085137CFC0F31
:10ACB0001307A0000325C10093071001636A8701F1
:10ACC000B7D7000093870790B387870183C7070032
:10ACD0002308A0F6230AF0F6130D1D0093071000B9
:10ACE000A30AF0F6137DFD0F6FF09FF437E7000025
:10ACF0008347A706639A070037C500001305C551AF
:10AD000017030000670083989387F7FF130101FF83
:10AD10002305F706B7E7000003C597FAB7E7000079
:10AD200083C587FA13074001B7E7000003C3070A8A
:10AD30003387E5021308C008B7D8000023261100A6
:10AD40009307000093060000938888B9130F200032
:10AD5000330803033307E5001317070113570701EF
:10AD60006310F706B7E700009387C707B38767004C
:10AD7000130710003317D70083C60700130600001F
:10AD80003367D7002380E700130740013387E502CC
:10AD9000B7E700009387470AB387E700B387A700A8
:10ADA0002380070097F0FFFFE780806F37C5000022
:10ADB0008320C10013054553130101016FF05FF4B7
:10ADC00013D617003306C80033061601834E06005B
:10ADD00013F6170013FEFE006304060013DE4E0098
:10ADE0006316EE019386160093F6F60F938717000D
:10ADF0009397070193D707016FF09FF6130101FFA7
:10AE0000B7E700002322910083C4070723248100B1
:10AE10002326110013040000638A04009307100026
:10AE2000130440006394F40013042000B7E700000B
:10AE300003966707B7E700008395870713050000AF
:10AE400097F0FFFFE780806BB7E7000083C7F70646
:10AE500093D7370093F717003304F400B7E70000E7
:10AE600003C7C7069307000013774700631A07005C
:10AE700013072000930710006394E4009307300049
:10AE8000230800F6230A80F6A30AF0F6B7E40000D0
:10AE900003C4A4FB63020404B7E70000039667FB46
:10AEA000B7E70000839587FB130550011304F4FFF7
:10AEB00097F0FFFFE78080641374F40F238D84FA0A
:10AEC000930750012308F0F69307200133348000E4
:10AED000230AF0F6A30A80F68320C1000324810030
:10AEE000832441001301010167800000130101FF69
:10AEF0002326110023248100034480F81374040BDB
:10AF00006318040497F0FFFFE780805397F0FFFF7A
:10AF1000E780C05497F0FFFFE780004F834780F839
:10AF200013F7F70F9397870193D7874163C80700FB
:10AF300093770703E38807FC130410008320C10004
:10AF40001305040003248100130101016780000040
:10AF500097F0FFFFE780C04E97F0FFFFE7800050BB
:10AF60006FF09FF9130101FF2324810037E70000F0
:10AF700037E400008347C406834617FB23261100ED
:10AF80002322910023202101B3E7D700639C07040B
:10AF9000B7E700009306E00137E90000238DD7FAF8
:10AFA00083568907B7E70000B7E40000239CD7FA6F
:10AFB00083D66407B7E70000239BD7FAB7E6000003
:10AFC00083C7B6069387F7FF93F7F70FA385F606B7
:10AFD0006396070293071000A308F7FAB7E700008B
:10AFE000238807FA8320C1000324810083244100C1
:10AFF00003290100130101016780000037C500002B
:10B000001305455597F0FFFFE7804068B7E700005C
:10B0100083D7470713079000231CF906B7E7000002
:10B0200083D72707239BF406930780072306F4069C
:10B03000B7E70000238E07FA2308E0F6A30A00F61C
:10B040009387C7FBA38007001307A0002308E0F63F
:10B05000A30A00F6238107001307B0002308E0F6D7
:10B06000A30A00F6A38107009307C0002308F0F6A7
:10B07000A30A00F66FF01FF7130101F9232611064A
:10B08000232481062322910623202107232E310524
:10B09000232C4105232A5105232861052326710508
:10B0A00023248105232291052320A105232EB1030A
:10B0B0002326010023280100834780F737B7FFFFCD
:10B0C000130717CEB3C7E70037E700002315F7FAD9
:10B0D000034780F737C50000B7E700001305C55BDD
:10B0E000A38AE7FA97F0FFFFE7808015B7D7000043
:10B0F000938787621307000093063001230BE0F665
:10B100001386070883C5070093871700A30BB0F6C3
:10B11000E39AC7FE130717001377F70FE310D7FE64
:10B12000B7E70000238A07FAB7D700009387879113
:10B1300037D400002324F10013044491B7E7000042
:10B1400083C447FB6394043C97F0FFFFE78000341F
:10B1500037C500001305855C97F0FFFFE780400EC0
:10B160001309400113850400130610009305000025
:10B1700097F0FFFFE780C03213850400938414002A
:10B18000130610009305D00093F4F40F97F0FFFF1F
:10B19000E7800031E39824FD930410001309D000E8
:10B1A00093850400130610001305000097F0FFFFBD
:10B1B000E780002F93850400938414001306100089
:10B1C0001305300193F4F40F97F0FFFFE780402D53
:10B1D000E39824FD130620009305D000130590008A
:10B1E00097F0FFFFE780C02B130620009305D000E7
:10B1F0001305A00097F0FFFFE780802A93078007E0
:10B20000230800F6A308F0F6230900F6930780FA56
:10B21000A309F0F6230800F6230A00F693041000B1
:10B22000A30A90F6230890F6130780F9A308E0F626
:10B23000230900F6A309F0F6230890F6130710017E
:10B24000230AE0F6A30A90F6130720002308E0F68D
:10B25000930680FBA308D0F6230900F6A309F0F6B5
:10B260002308E0F6130AA000230A40F71309300070
:10B27000A30A20F713078004230820F7A308E0F6A9
:10B28000230900F6A309F0F6230820F7230A40F764
:10B2900037C500001305455DA30A90F697F0FFFF40
:10B2A000E780000CB7C500009385C55D13057000ED
:10B2B00097F0FFFFE780800F37C500001305454C6E
:10B2C00097F0FFFFE780C009B7C500009385855F51
:10B2D0001305A00097F0FFFFE780400DB7C5000001
:10B2E000938545631305B00097F0FFFFE780000CDE
:10B2F000B7C50000938585661305E00097F0FFFF52
:10B30000E780C00AB7C500009385C5691305F00042
:10B3100097F0FFFFE7808009B7C500009385C56DF2
:10B320001305000197F0FFFFE7804008B7C5000054
:10B33000938505711305100197F0FFFFE780000763
:10B3400037C500001305C57297F0FFFFE780400185
:10B35000B7C50000938545731305900197F0FFFF73
:10B36000E780C004B7C700001385875197F0FFFF3F
:10B37000E78000FF97000000E78080B763080518AA
:10B38000B7E70000130680011385870893050000C6
:10B3900097100000E7800010B7E700009387C70709
:10B3A00023A0070023A2070023A40700B7E700009B
:10B3B000A3852707B7E7000023850706B7E7000046
:10B3C00023930706B7E7000023920706B7E70000BC
:10B3D00023840706B7E70000A38807FAB7E7000051
:10B3E000238807FAB7E7000023860706B7E70000BF
:10B3F00023870706B7E70000A3869706B7E7000094
:10B40000A3870706B7E7000023A607FAB7E70000FF
:10B41000238907FAB7E7000023880706B7E700008B
:10B42000B7E900002380070AB7D70000A3844907C3
:10B43000130700009387C7941306000583C607000F
:10B4400063980612630E07000327C100B7E70000E8
:10B45000239BE70603270101B7E70000239CE706CB
:10B4600037C500001305C55C97F0FFFFE78040DD9E
:10B4700097F0FFFFE780404937C500001305C5740A
:10B4800097F0FFFFE7808020B7E7000083C717FB36
:10B49000639A071497F0FFFFE78080FA97F0FFFFA9
:10B4A000E780C0FB630C05109305800213051000B4
:10B4B00097F0FFFFE780C0E337C500001305454D57
:10B4C00097F0FFFFE780C0E937C500001305057757
:10B4D00097F0FFFFE780C0D6B7C7000013858751FC
:10B4E00097F0FFFFE780C0E797F0FFFFE78040F5A8
:10B4F000834780F89397870193D7874163D4070ADE
:10B500009307100037E70000230AF7FA97F0FFFFD0
:10B51000E780C0F737D500001305058797F0FFFFD8
:10B52000E78000D2834700F093F71700E39E07748B
:10B530008320C106032481068324410603290106D2
:10B540008329C105032A8105832A4105032B0105AF
:10B55000832BC104032C8104832C4104032D01049B
:10B56000832DC1031305000013010107678000004C
:10B570006396C60203C71700131747002328E1008C
:10B5800003C7270013174700130707011317070105
:10B59000135707412326E100130710009387370054
:10B5A0006FF0DFE997F0FFFFE78040EBE30E05F275
:10B5B000B7E7000013071000A389E7FA834780F874
:10B5C00013FAF70F9397870193D7874163D6071C28
:10B5D0009307100037E70000A308F7FAB7E7000069
:10B5E000238807FA97F0FFFFE780800C9304C003DD
:10B5F00097F0FFFFE780C0E49384F4FF97F0FFFF2C
:10B60000E780C0E593F4F40F97F0FFFFE780407FF9
:10B61000E39004FEB7E7000083C747FBE39807EE1B
:10B62000B7E7000083D4470693072003B7E900007B
:10B63000B384F402B7E7000083D76706B384F4004D
:10B6400083C709FB9394040193D40401E38C07603E
:10B65000B7E7000083C7B7061307800CB387E7027C
:10B66000B387F4009387C75D9394070193D40401D3
:10B6700097F0FFFFE78080E137D500001305458094
:10B6800097F0FFFFE780C0CD37D900009305C98050
:10B690001305C00097F0FFFFE78040D183C709FB87
:10B6A000E38E075CB7C50000938545561305D000AF
:10B6B00097F0FFFFE78080CF9305C9801305E00076
:10B6C00097F0FFFFE78080CE930550011305F0004F
:10B6D00097F0FFFFE780C0C137D5000013058583D1
:10B6E00097F0FFFFE780C0B51385040097F0FFFFD8
:10B6F000E78080B813B7440693B6843E93B616002D
:10B70000133717003307D700B726000093079000C0
:10B710009386F670B3B79700B3B496009387A700EB
:10B72000B384E400B38497009309800293841400E7
:10B730001305000293F4F40F97F0FFFFE78000AFCA
:10B74000E39634FF9305C9801305000197F0FFFFCE
:10B75000E780C0C5B7D50000938545841305100167
:10B7600097F0FFFFE78080C49305C980130520018F
:10B7700097F0FFFFE78080C3B7C7000013858751AC
:10B7800097F0FFFFE780C0BD97F0FFFFE7804076AE
:10B79000E316059A6FF09FD7937ACA00B7E40000CA
:10B7A000638C0A0C13774A0093371700938727009E
:10B7B000B7E600002388F60693072000630607001B
:10B7C000B70701009387E7FF03D78407130600003C
:10B7D000B387E70013990701B7E700008395670770
:10B7E000135909411305090097F0FFFFE78000D9BD
:10B7F000631C0506239C24071309100093773A0065
:10B800006388070893771A0063960A0037E70000F9
:10B810002308F7069305200063860700B70501009B
:10B820009385E5FF37EB000083576B07039584078B
:10B8300013060000B385F500939A050193DA0A41D7
:10B8400093850A0097F0FFFFE78040D3631A050253
:10B85000231B5B0737E700008347F7069387170032
:10B86000A307F7066F000003930720006316F50097
:10B8700097F0FFFFE780C047130900006FF01FF843
:10B88000930720006316F50097F0FFFFE78040461E
:10B89000E31209FC839784071307D0FF63D4E732D0
:10B8A00037E700008347070A9387F7FF2300F70A6B
:10B8B00093070013239CF40697F0FFFFE780C00472
:10B8C00037E90000B7ED000037EC0000B7EB0000EF
:10B8D000137A0A01834749FC37E70000130949FC42
:10B8E000938D8DFC130C0CFE938BCBFD63120A34ED
:10B8F000A3060706130B1000930A5000130A10004A
:10B90000834709006380070883C70D000327810070
:10B910003387E700B3878700830C0700038D070098
:10B9200003570C00B38C5C0383D70B00330D5D030E
:10B93000B38CEC00939C0C0193DC0C4123109C0114
:10B9400013850C00330DFD00131D0D01135D0D411A
:10B950002390AB0193050D0097F0FFFFE78040BCFB
:10B960009307F5FF93F7F70F6376FA009307F00F4D
:10B97000631EF53A230009009377FB0F2308F0F6C6
:10B98000A30A00F613091900130C2C00938B2B004B
:10B99000938D1D0063084B3937E50000B7E50000C3
:10B9A0009307C5FB37E60000232AF100938745FD86
:10B9B000B7E60000232CF100930706FC232EF100CC
:10B9C0009387C6FC130CC5FB138A45FD930A06FC3E
:10B9D0001389C6FC930B90002320F102130B300057
:10B9E000930CF00F83460C00638E060883278100BA
:10B9F00083C60A003386F600B38686000305060078
:10BA0000038D060003560A003305650383560900BB
:10BA1000330D6D033305C500931D05011318050192
:10BA200093DD0D41135808012310BA0113850D0051
:10BA300023240103330DDD0013160D01131D0D0129
:10BA4000135D0D41135606012310A90193050D0046
:10BA50002322C10297F0FFFFE78080AC9306F5FF39
:10BA600093F6F60F9307100063F8D7000326410200
:10BA7000032881026310954123000C00230870F70E
:10BA8000A30A00F693871B0093FBF70F9307D000E0
:10BA9000130C1C00130A2A00938A1A0013092900A8
:10BAA000E392FBF4B7EC0000B7EA000037EA0000CD
:10BAB00037E9000037EC0000B7EB0000B7E4000006
:10BAC000938CCC03938A4A04130A8A051309C90488
:10BAD000130C4C03938B4B029384C402130B300062
:10BAE000B7ED000083C70C00638A074C83C70400CE
:10BAF000638607009387F7FF2380F400B7E7000011
:10BB000083C7C70683160A0003170900639A077CD8
:10BB100083D86D071317070113037000135707012C
:10BB2000330E1341939606019307870093D60601BF
:10BB30003307C70113858600131707011316050184
:10BB400093950701131505019397070137E8000046
:10BB500013570701130EE000035888071356060118
:10BB60001355054193D5050193D707416366EE62EE
:10BB700013088800131808011358084193564540CC
:10BB800013574840938707FF93F6F60F1377F70F85
:10BB900093D747406368D7009308070013870600D0
:10BBA000938608001303400193F7F70FB3876702EA
:10BBB000B7E800009388480A13031000B387F80021
:10BBC0006F00C03313073013635EF70037E70000E0
:10BBD0008347070A239C0406938717002300F70A6C
:10BBE0006FF09FCDB7E70000039767079306D0007B
:10BBF00063C0E602B7E6000003C7060A1307C7FFE3
:10BC00002380E60A1307000E239BE7066FF0DFCAC6
:10BC10009306100EE3D6E6CAB7E6000003C7060A8D
:10BC2000130747002380E60A130700016FF0DFFDCA
:10BC3000B7E500008346D70603C6E506B3E6C600AF
:10BC4000E39A06CA03C5990693061000A306D70611
:10BC5000630E05006386070283471900E39C07C84B
:10BC600093071000930610006F00C00137C5000055
:10BC70001305857897F0FFFFE78040A16FF09FC71D
:10BC80009306000033872601130610002300C70027
:10BC900037E70000034707073388B60193086000C1
:10BCA0002300E800139816008326810003D384073D
:10BCB000330E8801B306D70083860600330787005A
:10BCC00003070700B3861603130383009387170047
:10BCD00093F7F70F33071703B38666002310DE00D0
:10BCE000B306780137E80000035868071308880096
:10BCF000330707012390E6001307F5FFA384E90645
:10BD0000130780002387E50637E70000A309C7FA79
:10BD10002308F0F69307C000230AF0F6A30AC0F642
:10BD20006FF05FBD130B20006FF09FBD37E7000081
:10BD300037E60000B7E80000130887051305C604BE
:10BD400093070000130787051306C6049388C803EA
:10BD5000130E6000338317018345030063820510CF
:10BD600083150800938E250063DC9E0F9385C50024
:10BD700063C8950F83150500938E150063D2AE0F2F
:10BD80009385E50063CEA50DB7E6000083C5060ADE
:10BD9000B7E6000013858608939515003305B500B6
:10BDA000B7E500009385C501B385F50003C805001C
:10BDB0009305100023000300B39505010358050007
:10BDC000B3E505012310B5009385370093F5F50F12
:10BDD0002308B0F6A30A00F6230009009375FB0FB1
:10BDE0002308B0F693971700A30A00F63307F7006D
:10BDF000B7E5000013054001238DA5FA8355070020
:10BE0000B307F60037E70000231CB7FA03D7070093
:10BE1000B7E70000239BE7FA37E7000083574706A0
:10BE2000938717002312F70683470900E38C07B4B2
:10BE300003D60B0083550C0013050B00130686FF79
:10BE4000938585FF13160601939505011356064148
:10BE500093D5054197E0FFFFE780406A6FF09FB2FE
:10BE6000938717001308280013052500E394C7EFF4
:10BE70006FF01FFC839684079385260063DEB5036D
:10BE80009386C60063CAB603B7E6000083966607CA
:10BE90009385160063D2A5039386E60063CEA601C0
:10BEA00023000C00230870F7A30A00F697F0FFFFA9
:10BEB000E780800B6FF01FBD930588FF130686FF98
:10BEC00013160601939505011356064193D50541B6
:10BED00013850B0097E0FFFFE78040626FF09FBA89
:10BEE000B388E70083C808009388F8FF93F8F80F39
:10BEF0006378133F130717001377F70FE3F2E6FE9B
:10BF0000130D40006354A800130D30001308FDFF0B
:10BF100083C60B001378F80F23800A0103C70400BF
:10BF200063980630930610002380DB00930680019F
:10BF300063E6E600130790012380E40083460C00CB
:10BF4000130730066382E63A03C70A0083150A0026
:10BF50000316090013050B002326E1022324C10266
:10BF60002322B10297E0FFFFE78040590327C10277
:10BF7000832541020326810293076000630A0700BC
:10BF8000930610009307A0006314D7009307800066
:10BF9000B385C50093D5354093F5150013062000F1
:10BFA000B387B700930610006314C70093063000F0
:10BFB000230860F7230AF0F6A30AD0F6130B1B0040
:10BFC000137BFB0F93079000938C1C00938A1A003D
:10BFD000130A2A0013092900130C1C00938B1B0061
:10BFE00093841400E310FBB037E90000B7EA0000C7
:10BFF00037EA0000B7E7000037EB0000B7EB0000BE
:10C000001309C9FE938ACA00130ACAFF93040000E9
:10C0100037EC0000B7EC000037ED0000938D870887
:10C02000130B4BFE938B4BFF83470900638A070872
:10C0300003970A0083178C079306970063C2F608DC
:10C040009387970063CEE70603170A0083976C0770
:10C050009306C70063C6F6069387C70063C2E70668
:10C0600083470D0A3387640103470700939717003E
:10C07000B387FD0003D60700930610003397E60050
:10C080003367C7002390E7009387D400230009009B
:10C0900093F7F70F2308F0F6A30A00F6B3877401AD
:10C0A00083C70700130770046386E734636EF730B5
:10C0B00013071004638EE734130730046386E736F2
:10C0C000938414009307800013091900938A2A00AF
:10C0D000130A2A00E39AF4F4B7E7000083D5670750
:10C0E000B7E7000003D58707938585009395050181
:10C0F000130585001315050193D505411355054119
:10C1000097E0FFFFE780C04193073000B7E40000ED
:10C110006314F502B7E7000083C7870603C724FB53
:10C12000638E07309307100037E70000A308F7FA83
:10C1300037E700002308F7FA97F0FFFFE78040CCCD
:10C1400037E700008347C706638607009387F7FF3A
:10C150002306F70637E700008347E70663860700F4
:10C160009387F7FF2307F70683C724FB37E7000011
:10C170006384072E9387F7FF93F7F70F2389F4FA69
:10C18000639C072C93071000A309F7FA97E0FFFFC1
:10C19000E78000526FF04FAF3303034133876600EF
:10C1A0001317070113570701636CEE129388880079
:10C1B0009398080193D80841138307FF138708FF5A
:10C1C0001353434013574740135545401373F30F20
:10C1D0001377F70F1375F50F636867009306070071
:10C1E000130703001383060037E800001308480A0A
:10C1F000130E4001930E10006F008002B306C703B8
:10C20000B306D800B386A60083C606009386F6FF61
:10C2100093F6F60F63F6DE0C130717001377F70F8C
:10C22000E37EE3FC33ADF800130D1D006FF01FCE6D
:10C23000E31607D013050000930640008327410151
:10C240003387A70083480700639608088327C10146
:10C2500093081000230017013387A700832781016B
:10C260009316150023000701338ED7008327810022
:10C27000130360003307F800030707003308880042
:10C280008327010233076702B386D7003307E6002E
:10C290002310EE0003070800330767023387E50029
:10C2A0002390E600130795001377F70F2308E0F6B5
:10C2B0001307D000230AE0F6A30A10F797E0FFFF68
:10C2C000E78080141375F5031305D5022380A400BD
:10C2D0006FF0DFC613051500E312D5F66FF01FC629
:10C2E00023800B00130D000083C60A008327810002
:10C2F000835509002326D1023387F6000306070081
:10C3000003570A003307C70033868600030606007A
:10C310001317070113570741B385C500939505010E
:10C3200093D5054113050700130610002324B1021D
:10C330002322E10297E0FFFFE78040240327410228
:10C3400083258102630C050603460C0013073006A3
:10C350008326C102631CE604631A0D0097E0FFFF09
:10C36000E780800A137535002380AA0037E60000B5
:10C370000316860703170A00831709009305960022
:10C3800083966D07E3C2E5BC13079700E34EC7BA77
:10C390001387C600E34AF7BA9387C700E3C6D7BA44
:10C3A00097F0FFFFE78040BC6FF01FBAE3100DFC71
:10C3B00093C716002380FA006FF05FFB2310EA009A
:10C3C0002310B9006FF09FFA1307B004E39AE7CE89
:10C3D00037E700008347A70637C500001305C57976
:10C3E000938717002305F70697E0FFFFE780002AF1
:10C3F0006FF01FCD37E700008357670637C5000091
:10C400001305457A938747062313F7066FF0DFFD80
:10C4100083C7990637C500001305857B93875700AE
:10C42000A384F9066FF05FFCB7E7000037C5000092
:10C430002384D7061305C57C6FF01FFB9307700795
:10C44000E3ECE7CE37C500001305C57E97E0FFFF9C
:10C45000E780C0236FF05FCE834737FB6386078298
:10C460006FF0DFD2B7E7000083C78706638207A0BB
:10C470009384843E939404016FF04F9FB7C50000EE
:10C48000938505596FF08FA2834710F06FF08F8965
:10C490003306C500930705006394C700678000005A
:10C4A00093871700A38FB7FE6FF01FFF303B393320
:10C4B000000000005448452049524F4E204B45454E
:10C4C00050000000303B33370000000020202000E7
:10C4D0001B5B4B00303B3936000000004C49564591
:10C4E00053200000303B3937000000002020204B53
:10C4F0004559532000000000202020414D4D4F2081
:10C5000000000000202020474F4C44200000000085
:10C5100043524F574E000000300000004C4F434B39
:10C5200045442E20594F55204E4545442041204B2F
:10C5300045592E00544845204B4559205455524EDC
:10C54000532E2054484520444F4F52204F50454EC3
:10C55000532E0000594F552041524520484954213F
:10C5600000000000202020594F55204553434150E2
:10C5700045442057495448205448452043524F577A
:10C580004E212020202020202020202000000000FC
:10C5900020202054484520475541524453204841CB
:10C5A00056452054414B454E20594F552E202020B2
:10C5B0002020202020202020000000001B5B3F3393
:10C5C0006C1B5B3F32356C001B5B306D1B5B324A72
:10C5D00000000000313B39330000000054204820A7
:10C5E00045202020492052204F204E2020204B2043
:10C5F0004520452050000000596F7520617265206C
:10C600006120707269736F6E657220696E20746844
:10C61000652049726F6E204B6565702E20537465DE
:10C62000616C20746865206B696E67277320637284
:10C630006F776E0066726F6D207468652074687223
:10C640006F6E6520726F6F6D2C20616E6420657354
:10C650006361706520646F776E2074686520737401
:10C66000616972732E0000004152524F5753206F80
:10C6700072205720412053204420206D6F76652082
:10C68000202020202020205350414345206F72203D
:10C690004354524C202073686F6F74004B6579735C
:10C6A000206F70656E206C6F636B656420646F6FC4
:10C6B00072732E2043686573747320686F6C6420F6
:10C6C000676F6C642C2063726174657320686F6C93
:10C6D000642073686F74732E0000000047756172E8
:10C6E00064732073656520616C6F6E6720726F776D
:10C6F0007320616E6420636F6C756D6E73202D20E6
:10C70000616E6420746865792073686F6F742E00A1
:10C7100050207061757365732E2020455343206748
:10C72000697665732075702E00000000313B393644
:10C7300000000000505245535320535041434520C0
:10C74000544F20424547494E0000000046494E44A0
:10C75000205448452043524F574E2E204D494E44B9
:10C7600020544845204755415244532E00000000B4
:10C77000504155534544202D205020544F20474FC1
:10C78000204F4E1B5B4B00004F5554204F4620411D
:10C790004D4D554E4954494F4E21000041204B45C7
:10C7A00059210000474F4C44212031303020435262
:10C7B0004F574E532E00000046495645204D4F52CC
:10C7C000452053484F54532E000000005448452044
:10C7D00043524F574E21204E4F572046494E44203A
:10C7E000544845205354414952532E004E4F542033
:10C7F000574954484F5554205448452043524F57A9
:10C800004E2E0000303B34343B39370020202020AE
:10C810002020202020202020202020202020202018
:10C820002020202020202020202020202020202008
:10C83000202020200000000020202053434F52459C
:10C8400020000000202020535041434520544F2019
:10C85000504C415920414741494E2C2045534320DB
:10C86000544F2053544F502020202020000000001F
:10C870001B5B306D1B5B324A1B5B481B5B3F3235D9
:10C880006800000047415445484F5553450000009B
:10C89000475541524420524F4F4D000043484150AC
:10C8A000454C000041524D4F55525900434F525292
:10C8B00049444F520000000047524541542048412E
:10C8C0004C4C000043454C4C415253004C494252A1
:10C8D00041525900535441495257454C4C000000B5
:10C8E00044554E47454F4E005452454153555259B9
:10C8F000000000005448524F4E4520524F4F4D000B
:10C900001011111111110F1111110E0002020E0E52
:10C91000030C030C01FF00000000FF0184C80000AD
:10C9200090C800009CC80000A4C80000ACC800006B
:10C93000B8C80000C4C80000CCC80000D4C80000BB
:10C94000E0C80000E8C80000F4C80000500302413D
:10C9500009060000000000000000000000000000C8
:10C9600000000000000000000000000000000000C7
:10C97000000000000000000000000000006802024B
:10C980004B0906680C0A00000000000000000000CF
:10C990000000000000000000000000000000000097
:10C9A000000000000000000000000000000076020F
:10C9B00002760D02470805470A0568050A000000CF
:10C9C0000000000000000000000000000000000067
:10C9D0000000000000000000000000000000004116
:10C9E000030241100268090663080B000000000002
:10C9F0000000000000000000000000000000000037
:10CA00000000000000000000000000000000000026
:10CA1000760202680F0441030A760F0B0000000043
:10CA20000000000000000000000000000000000006
:10CA300000000000000000000000000000000000F6
:10CA400000470302471002680905680A0963020BE0
:10CA5000760E0B0000000000000000000000000047
:10CA600000000000000000000000000000000000C6
:10CA70000000410B0668100B0000000000000000E1
:10CA800000000000000000000000000000000000A6
:10CA90000000000000000000000000000000000096
:10CAA000000000680B054B090A76040B000000002B
:10CAB0000000000000000000000000000000000076
:10CAC0000000000000000000000000000000000066
:10CAD0000000000068050A000000000000000000DF
:10CAE0000000000000000000000000000000000046
:10CAF0000000000000000000000000000000000036
:10CB000000000000007609064B030A470D0A0000EA
:10CB10000000000000000000000000000000000015
:10CB20000000000000000000000000000000000005
:10CB3000000000000000680302470705470A054798
:10CB40000E05760A0A000000000000000000000048
:10CB500000000000000000000000000000000000D5
:10CB60000000000000000063020363110343090793
:10CB700076020B76110B00000000000000000000A0
:10CB800000000000000000000000000000000000A5
:10CB9000000000000000000011111111111111110D
:10CBA0001111010000000000000000100100000051
:10CBB0000000000000100100000000000000001054
:10CBC00001001011010010110110010010000000FF
:10CBD0000000011001001000000000000100010031
:10CBE00010000000000001000100101101001011F0
:10CBF0000110010000000000000000100100000012
:10CC00000000000000100100000000000000001003
:10CC100001000000000000000010111111110011AE
:10CC20001111111111111111111111111111010015
:10CC300000000000000000100100000000000000E3
:10CC400000100100000000000000001001101100A1
:10CC50000000101100100110000000000010001072
:10CC60000010000000000010000000100000000094
:10CC70000010000001101100000010110010010050
:10CC80000000000000000010010000000000000093
:10CC90000010010000000000000000100100000072
:10CCA00000000000001011111111111111111111CA
:10CCB00011111111111111111111010000000000C9
:10CCC0000000001001000000000000000010010042
:10CCD0000000000000000010010000101111010010
:10CCE00000100100001000100000001000000010F3
:10CCF00000200000000000000010111100000000E2
:10CD00000100000000000000001001000000000011
:10CD100000000010010000000000000000100100F1
:10CD200000000000000000100100000000000000F2
:10CD300000101111111111111111111111111111F5
:10CD4000111111111111010000000000000000106C
:10CD500001000000000000000010010000000000C1
:10CD6000000000100111110000001011011001005D
:10CD700000000000000000100000000000000000A3
:10CD80000010000000000000000000100111110060
:10CD90000000101101100100000000000000001050
:10CDA0000100000000000000001001000000000071
:10CDB0000000001001000000000000000010111130
:10CDC0001111001111111111111111110011111175
:10CDD000111101000000000000000010010000001F
:10CDE00000111100001001000000000100000010FF
:10CDF00001000000000100000010111111110001DC
:10CE00000000001001000000000000000000010010
:10CE1000000000000000000011111111001111119B
:10CE200011100100000000010000001001000000CE
:10CE300000010000001001000000000100000010CF
:10CE4000010000000000000000101111111100117C
:10CE500011111111111111111111111111110100E3
:10CE600000000000000000100100000000000000B1
:10CE7000001001000000000000000010010010017F
:10CE8000000010010010010010010000100100104E
:10CE90000000000000000000000000000000000092
:10CEA000000000000100100100001001001001004E
:10CEB000100100001001001001000000000000003F
:10CEC0000010010000000000000000100100000040
:10CED00000000000001011111111001111111111A9
:10CEE0001111111111111111111101000100000096
:10CEF00001000010010001111101011111100100C8
:10CF000001010000010010100100000111111101C8
:10CF1000101011111101010000011010000000009B
:10CF200001000001000000111111010000110100B9
:10CF30000101000000110100001001011111110197
:10CF40000111111001010000000001010010011188
:10CF50001101111101010010010000000100000089
:10CF600001101111111111111111111111111111C2
:10CF7000001111111111010000000000000000104B
:10CF8000011110011110011110100100000000002A
:10CF9000000000100111100111100111101001000A
:10CFA0000000000000000010000000000000000071
:10CFB00000100011100111100111101001000000EB
:10CFC00000000000001001111001111001111010DB
:10CFD000010000000000000000100100000000003F
:10CFE00000000010010000000000000000101111FE
:10CFF0001111001111111111111111110011111143
:10D0000011110100000000000000001001000000EC
:10D010000000000000100110111101101111011089
:10D02000011000000000000001100110000030039A
:10D030000000011001100000300300000100011089
:10D040000000000000000100011011110110111179
:10D0500001100100000000000000001001000000AD
:10D06000000000000010010000000000000000109F
:10D070000100000000000000001011111111111139
:10D0800011111111111111110011111111110101C1
:10D09000100001100001101001011000011000012A
:10D0A00010100101100001100001101001111011E9
:10D0B000011011011110010000000000000000101B
:10D0C0000000000000000000000000000000000060
:10D0D00000000000011110110110110111100101D7
:10D0E00010000110000110100101100001100001DA
:10D0F00010100101100001100001101001000000CB
:10D100000000000000101111111111111111111165
:10D110001111111111111111111101000000000064
:10D1200000000010010000000000000000100100DD
:10D130000000000000000010010000111111111189
:10D1400000100100000100000010001000000001AC
:10D150000000001000100000001111111211001049
:10D1600001000000000000000010010000000000AD
:10D17000000000100100000000000000001001008D
:10D18000000000000000001001000000000000008E
:10D190000010111111111111111111111111111191
:10D1A000001111111111111111111211111111117F
:10D1B000010000000000000000100100000000005D
:10D1C000000000100100000000000000001001003D
:10D1D00011110000111100100100010000000010E9
:10D1E000001001000100000000100010010001000B
:10D1F00000000010001001001111000011110010BA
:10D20000010000000000000000100100000000000C
:10D2100000000010010000000000000000101111CB
:10D220001111111111111111000000000000000076
:10D2300000000000000000000000000000000000EE
:10D2400000000000000000000000000000000000DE
:10D2500000000000000000000000000000000000CE
:10D2600000000000000000000000000000000000BE
:10D2700000000000000000000000000000000000AE
:10D28000000000000000000000000000000000009E
:10D29000000000000000000000000000000000008E
:10D2A000000000000000000000000000000000007E
:10D2B000000000000000000000000000000000006E
:10D2C000000000000000000000000000000000005E
:10D2D000000000000000000000000000000000004E
:10D2E000000000000000000000000000000000003E
:10D2F000000000000000000000000000000000002E
:10D30000000000000000000000000000000000001D
:10D31000000000000000000000000000000000000D
:10D3200000000000000000000808080808080808BD
:10D330000808080808080808070707070707070874
:10D34000070707070707070807070707070707086B
:10D35000070707070707070807070707070707085B
:10D360000707070707070708080808080808080844
:10D370000808080808080808070707080707070734
:10D38000070707080707070707070708070707072B
:10D39000070707080707070707070708070707071B
:10D3A0000707070807070707080808080808080804
:10D3B00008080808080808080707070707070708F4
:10D3C00007070707070707080707070707070708EB
:10D3D00007070707070707080707070707070708DB
:10D3E00007070707070707080808080808080808C4
:10D3F00008080808080808080707070807070707B4
:10D4000007070708070707070707070807070707AA
:10D41000070707080707070707070708070707079A
:10D420000707070807070707080808080808080883
:10D43000080808080808080808030303080303038A
:10D440000803030308030308080303030803030393
:10D450000803030308030308080303030803030383
:10D460000803030308030308080303030803030373
:10D470000803030308030308080303030803030363
:10D480000803030308030308080303030803030353
:10D490000803030308030308080303030803030B3B
:10D4A0000B03030308030308080303030803030B28
:10D4B0000B03030308030308080303030803030320
:10D4C0000803030308030308080303030803030313
:10D4D0000803030308030308080303030803030303
:10D4E00008030303080303080803030308030303F3
:10D4F00008030303080303080803030308030303E3
:10D5000008030303080303080803030308030303D2
:10D5100008030303080303080808080808080808A4
:10D5200008080808080808080E0E0E0E0E0E0E0E4B
:10D530000E0E0E0E0E0E0E0E06060606060606064B
:10D54000060606060606060608080808080808086B
:10D55000080808080808080800000000000000008B
:10D5600000000000000000000E0E0E0E0E0E0E0E4B
:10D570000E0E0E0E0E0E0E0E06060606060606060B
:10D58000060606060606060608080808080808082B
:10D59000080808080808080800000000000000004B
:10D5A00000000000000000000E0E0E0E0E0E0E0E0B
:10D5B0000E0E0E0E0E0E0E0E0606060606060606CB
:10D5C00006060606060606060808080808080808EB
:10D5D000080808080808080800000000000000000B
:10D5E00000000000000000000E0E0E0E0E0E0E0ECB
:10D5F0000E0E0E0E0E0E0E0E06060606060606068B
:10D6000006060606060606060808080808080808AA
:10D6100008080808080808080000000000000000CA
:10D620000000000000000000000003333330000061
:10D630000000333333330000000039999993000020
:10D6400000003909909300000000099999900000AA
:10D650000000000990000000000CCCCCCCCCC00035
:10D6600000CCCCCCCCCCCC00009CCCCCCCCCC9005D
:10D67000009CCCCCCCCCC9000000CCCCCCCC0000E5
:10D6800000004444444400000000444004440000BE
:10D690000000444004440000000077700777000059
:10D6A00000000000000000000000033333300000E1
:10D6B00000003333333300000000399999930000A0
:10D6C000000039099093000000000999999000002A
:10D6D0000000000990000000000CCCCCCCCCC000B5
:10D6E00000CCCCCCCCCCCC00009CCCCCCCCCC900DD
:10D6F000009CCCCCCCCCC9000000CCCCCCCC000065
:10D700000000444444440000000004400440000081
:10D710000000044004400000000007700770000093
:10D720000000000000000000000003333330000060
:10D730000000333333330000000033333333000051
:10D740000000333333330000000003333330000074
:10D750000000000990000000000CCCCCCCCCC00034
:10D7600000CCCCCCCCCCCC00009CCCCCCCCCC9005C
:10D77000009CCCCCCCCCC9000000CCCCCCCC0000E4
:10D7800000004444444400000000444004440000BD
:10D790000000444004440000000077700777000058
:10D7A00000000000000000000000033333300000E0
:10D7B00000003333333300000000333333330000D1
:10D7C00000003333333300000000033333300000F4
:10D7D0000000000990000000000CCCCCCCCCC000B4
:10D7E00000CCCCCCCCCCCC00009CCCCCCCCCC900DC
:10D7F000009CCCCCCCCCC9000000CCCCCCCC000064
:10D800000000444444440000000004400440000080
:10D810000000044004400000000007700770000092
:10D82000000000000000000000000333333000005F
:10D830000000333333330000000033339999000084
:10D8400000003339909900000000033999990000D5
:10D85000000000099900000000000CCCCCC00000C2
:10D860000000CCCCCCCC00000000CCCCC99900008E
:10D870000000CCCCCC00000000000CCCCC000000A0
:10D880000000044444400000000004400440000044
:10D890000000444000440000000077700077000062
:10D8A00000000000000000000000033333300000DF
:10D8B0000000333333330000000033339999000004
:10D8C0000000333990990000000003399999000055
:10D8D000000000099900000000000CCCCCC0000042
:10D8E0000000CCCCCCCC00000000CCCCC99900000E
:10D8F0000000CCCCCC00000000000CCCCC00000020
:10D9000000000444444000000000004444000000C3
:10D910000000004444000000000000777700000091
:10D920000000000000000000000007777770000092
:10D9300000007777777700000000799999970000C9
:10D940000000790990970000000009999990000063
:10D950000000000990000000000222222222200084
:10D9600000222222222222000092222222222900A8
:10D9700000922222222229000000222222220000DC
:10D9800000008888888800000000888008880000DF
:10D990000000888008880000000033300333000056
:10D9A0000000000000000000000007777770000012
:10D9B0000000777777770000000079999997000049
:10D9C00000007909909700000000099999900000E3
:10D9D0000000000990000000000222222222200004
:10D9E0000022222222222200009222222222290028
:10D9F000009222222222290000002222222200005C
:10DA000000008888888800000000088008800000E6
:10DA10000000088008800000000003300330000090
:10DA20000000000000000000000007777770000091
:10DA3000000077777777000000007777777700002E
:10DA40000000777777770000000007777770000095
:10DA50000000000990000000000222222222200083
:10DA600000222222222222000092222222222900A7
:10DA700000922222222229000000222222220000DB
:10DA800000008888888800000000888008880000DE
:10DA90000000888008880000000033300333000055
:10DAA0000000000000000000000007777770000011
:10DAB00000007777777700000000777777770000AE
:10DAC0000000777777770000000007777770000015
:10DAD0000000000990000000000222222222200003
:10DAE0000022222222222200009222222222290027
:10DAF000009222222222290000002222222200005B
:10DB000000008888888800000000088008800000E5
:10DB1000000008800880000000000330033000008F
:10DB20000000000000000000000007777770000090
:10DB300000007777777700000000777799990000E9
:10DB4000000077799099000000000779999900000A
:10DB500000000009990000000000022222200000BD
:10DB60000000222222220000000022222999000027
:10DB700000002222220000000000022222000000F9
:10DB800000000888888000000000088008800000ED
:10DB9000000088800088000000003330003300005F
:10DBA0000000000000000000000007777770000010
:10DBB0000000777777770000000077779999000069
:10DBC000000077799099000000000779999900008A
:10DBD000000000099900000000000222222000003D
:10DBE00000002222222200000000222229990000A7
:10DBF0000000222222000000000002222200000079
:10DC0000000008888880000000000088880000006C
:10DC1000000000888800000000000033330000008E
:10DC200000000000000000000000000000000000F4
:10DC300000000000000000000000000000000000E4
:10DC400000000000000000000000000000000000D4
:10DC50000000000000000000000000BBBB0000004E
:10DC6000000000BBBB000000000000BBBB000000C8
:10DC7000000000BBBB00000000000000000000002E
:10DC80000000000000000000000000000000000094
:10DC90000000000000000000000000000000000084
:10DCA0000000000000000000000000000000000074
:10DCB0000000000000000000000000000000000064
:10DCC0000000000000000000000000000000000054
:10DCD0000000000000000000000000999900000012
:10DCE00000000099990000000000009999000000D0
:10DCF00000000099990000000000000000000000F2
:10DD00000000000000000000000000000000000013
:10DD10000000000000000000000000000000000003
:10DD200000000000000000000000000000000000F3
:10DD300000000000000000000000000000000000E3
:10DD4000000BBB000000000000B000B000000000AD
:10DD500000B000BBBBBBBBB000B000B0000B0B0001
:10DD6000000BBB00000B0B000000000000000000D7
:10DD700000000000000000000000000000000000A3
:10DD80000000000000000000000000000000000093
:10DD90000000000000000000000000000000000083
:10DDA0000000000000000000000000000000000073
:10DDB0000000000000000000000000000000000063
:10DDC0000003333333333000003333333333330022
:10DDD000003BBBBBBBBBB3000033333BB3333300AF
:10DDE0000033333BB3333300003333333333330047
:10DDF0000033333333333300003BBBBBBBBBB30017
:10DE000000333333333333000000000000000000E0
:10DE10000000000000000000000000000000000002
:10DE200000000000000000000000000000000000F2
:10DE300000000000000000000000000000000000E2
:10DE4000002222222222220000288888888882003C
:10DE50000028BB8BB8BB82000028BB8BB8BB8200FC
:10DE60000028BB8BB8BB8200002888888888820085
:10DE700000222222222222000000000000000000D6
:10DE80000000000000000000000000000000000092
:10DE90000000000000000000000000000000000082
:10DEA0000000000000000000000000000000000072
:10DEB0000000000000000000000000000000000062
:10DEC000000B000BB000B000000BB0BBBB0BB000F0
:10DED000000BBBBBBBBBB000000BB9BBBB9BB00016
:10DEE000000BBBBCCBBBB000000BBBBBBBBBB000D3
:10DEF0000000000000000000000BBBBBBBBBB0007B
:10DF00000000000000000000000000000000000011
:10DF10000000000000000000000000000000000001
:10DF200000000000000000000000000000000000F1
:10DF3000000F00000000F0000000F00BB00F000028
:10DF400000000F0BB0F00000000000BFFB0000005D
:10DF50000BBB0BFFFFB0BBB0000000BFFB0000001D
:10DF600000000F0BB0F000000000F00BB00F00003D
:10DF7000000F00000000F0000000000000000000A2
:10DF80000000000000000000000000000000000091
:10DF90000000000000000000000000000000000081
:08DFA000000000000000000079
:040000030000A20057
:00000001FF
+160
View File
@@ -0,0 +1,160 @@
#!/usr/bin/env python3
"""Run The Iron Keep in tools/rv32emu.py on a model of the board's game ports
(graphics, sprites, frame counter, held keys, terminal) and take screenshots.
python3 test-keep.py [--shots DIR] [--fuzz FRAMES]
The scripted run starts a game, walks east into the next room, fires, and
checks what the status line says; --fuzz then plays random keys for a while
and checks the program never leaves its code or wrecks the stack. Screenshots
need pyte and Pillow (text is drawn with the board's font).
"""
import argparse
import os
import random
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "..", "tools"))
from boardemu import Board, load_ihx, load_font, load_syms # noqa: E402
UP, DOWN, LEFT, RIGHT, FIRE, ENTER, ESC = 1, 2, 4, 8, 16, 32, 128
def play_through(b, a, check, font):
"""Walk the castle by breadth-first search over gen.py's map: both keys
nearest the throne room, the crown behind its locked door, then the
stairs. The bot can't be seen or hurt (it keeps inv set), so this tests
the castle and the game's rules, not the bot's skill."""
sys.path.insert(0, HERE)
import gen
from collections import deque
sym = {"_" + n: a for n, a in load_syms(os.path.join(HERE, "out", "keep.sym")).items()}
big = gen.BIG
W, H = len(big[0]), len(big)
where = {ch: [(x, y) for y in range(H) for x in range(W) if big[y][x] == ch] for ch in "KCX"}
def room_of(p): return (p[1] // 14) * 4 + p[0] // 20
keys = sorted(where["K"], key=lambda p: room_of(p) not in (1, 7))[:2]
targets = keys + where["C"] + where["X"][:1]
names = ["key", "key", "crown", "stairs"]
def i16(addr):
v = b.m[addr] | b.m[addr + 1] << 8
return v - 65536 if v & 0x8000 else v
def path(s, g):
prev = {s: None}
q = deque([s])
while q:
p = q.popleft()
if p == g:
break
for dx, dy in ((1, 0), (-1, 0), (0, 1), (0, -1)):
n = (p[0] + dx, p[1] + dy)
if 0 <= n[0] < W and 0 <= n[1] < H and n not in prev and big[n[1]][n[0]] != "#":
prev[n] = p; q.append(n)
out = [g]
while prev[out[-1]] is not None:
out.append(prev[out[-1]])
return out[::-1]
t = 0
joy = 0
for frame in range(30000):
b.m[sym["_inv"]] = 2
if b.m[sym["_over"]]:
break
px, py, room = i16(sym["_px"]), i16(sym["_py"]), b.m[sym["_room"]]
if px % 16 == 0 and (py - 16) % 16 == 0:
here = ((room % 4) * 20 + px // 16, (room // 4) * 14 + (py - 16) // 16)
while t < len(targets) and here == targets[t]:
print("bot reached the %s at frame %d" % (names[t], frame))
t += 1
if t == len(targets):
joy = 0
else:
p = path(here, targets[t])
n = p[1]
joy = RIGHT if n[0] > here[0] else LEFT if n[0] < here[0] else DOWN if n[1] > here[1] else UP
elif joy == 0: # between tiles: line up with one
joy = LEFT if px % 16 else UP
if frame % 2000 == 0:
print("frame %d: room %d at %d,%d, heading for the %s" % (frame, room, px, py, names[min(t, 3)]))
b.run_frames(1, joy)
check("bot collected %d keys and the crown" % b.m[sym["_keys"]], b.m[sym["_crown"]] == 1)
check("taking the crown down the stairs wins", b.m[sym["_over"]] == 1 and b.m[sym["_won"]] == 1)
b.run_frames(80)
check("end screen", "YOU ESCAPED WITH THE CROWN" in "\n".join(b.screen_text().display))
if a.shots: b.shot(os.path.join(a.shots, "5-escaped.png"), font)
b.run_frames(10, ESC)
b.run_frames(5)
check("Esc returns to BASIC", b.pc == 0)
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--shots", help="write PNG screenshots here")
ap.add_argument("--fuzz", type=int, default=0, help="random frames to play")
ap.add_argument("--play", action="store_true",
help="then let a bot play the game through: keys, door, crown, stairs")
a = ap.parse_args()
mem, hi = load_ihx(os.path.join(HERE, "keep.ihx"))
b = Board(mem, code_end=hi)
font = None
if a.shots:
os.makedirs(a.shots, exist_ok=True)
font = load_font()
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def status():
return "\n".join(b.screen_text().display[:2])
b.run_frames(20)
check("title screen", "T H E I R O N K E E P" in "\n".join(b.screen_text().display))
if a.shots: b.shot(os.path.join(a.shots, "1-title.png"), font)
b.run_frames(5, FIRE)
b.run_frames(10)
check("game starts in the gatehouse", "GATEHOUSE" in status() and "LIVES 3" in status())
check("player sprite shown", b.spr[0][3] & 1 == 1)
if a.shots: b.shot(os.path.join(a.shots, "2-gatehouse.png"), font)
# east along the top, down to the east doorway's rows, then through it
x0, y0 = b.spr[0][0], b.spr[0][1]
b.run_frames(30, DOWN)
check("moving down moves the player", b.spr[0][1] == (y0 + 60) & 255)
b.run_frames(30, UP)
b.run_frames(8, FIRE)
check("a shot uses ammunition", "AMMO 9" in status())
b.run_frames(130, RIGHT)
check("walls stop the player", b.spr[0][0] == 290)
b.run_frames(32, DOWN)
b.run_frames(30, RIGHT)
check("east doorway leads to the guard room", "GUARD ROOM" in status())
if a.shots: b.shot(os.path.join(a.shots, "3-guardroom.png"), font)
b.run_frames(60)
print(status())
if a.play:
play_through(b, a, check, font)
if a.fuzz:
rnd = random.Random(1)
j = 0
for f in range(a.fuzz):
if f % 10 == 0:
j = rnd.choice([UP, DOWN, LEFT, RIGHT, UP | FIRE, DOWN | FIRE, LEFT | FIRE,
RIGHT | FIRE, FIRE, 0, UP | LEFT, DOWN | RIGHT])
b.run_frames(1, j)
if b.pc == 0:
break
if f % 500 == 499:
print("frame", f + 1, "-", b.screen_text().display[0].strip())
check("random play for %d frames without a crash" % a.fuzz, True)
if a.shots: b.shot(os.path.join(a.shots, "4-fuzz.png"), font)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()
+226
View File
@@ -0,0 +1,226 @@
#!/usr/bin/env python3
"""Make data.h for raid.c: the sprite shapes (drawn here from polygons and
ellipses, in the isometric directions the game uses) and the level.
The world is a 2:1 isometric grid of tiles 32 pixels wide and 16 high. The
ship flies along the rows, towards the top right of the screen; a level is a
list of rows, numbered from where it starts. Each row is space or fortress
floor, and a fortress row can end in a wall across the flight path: a low wall
to fly over, or a tall one with a doorway to fly through. Ground objects stand
on tiles: turrets that fire at you, fuel tanks to shoot for fuel, radar dishes
for points, and the reactor core at the end of the level.
"""
import math
import random
import sys
# ------------------------------------------------------------- drawing
def blank():
return [[0] * 16 for _ in range(16)]
def poly(img, pts, c):
"""Fill a polygon given in pixel coordinates (centres at +0.5)."""
n = len(pts)
for y in range(16):
for x in range(16):
px, py = x + 0.5, y + 0.5
inside = False
for k in range(n):
(x1, y1), (x2, y2) = pts[k], pts[(k + 1) % n]
if (y1 > py) != (y2 > py):
if px < x1 + (py - y1) * (x2 - x1) / (y2 - y1):
inside = not inside
if inside:
img[y][x] = c
def ellipse(img, cx, cy, rx, ry, c):
for y in range(16):
for x in range(16):
if ((x + 0.5 - cx) / rx) ** 2 + ((y + 0.5 - cy) / ry) ** 2 <= 1:
img[y][x] = c
def rect(img, x0, y0, x1, y1, c):
for y in range(y0, y1):
for x in range(x0, x1):
img[y][x] = c
# the flight direction on screen (2, -1), and across the flight path, which in
# the 2:1 isometric view is (2, 1) - the direction the tiles' other edges run
F = (2 / math.sqrt(5), -1 / math.sqrt(5))
G = (2 / math.sqrt(5), 1 / math.sqrt(5))
def iso(pts, cx=8, cy=8, fwd=F):
"""Points given as (along, across) in the flight frame -> pixels."""
side = G
return [(cx + a * fwd[0] + b * side[0], cy + a * fwd[1] + b * side[1]) for a, b in pts]
def ship(body, wing, canopy, fwd=F):
img = blank()
poly(img, iso([(-2, -7.5), (1.5, -1.2), (1.5, 1.2), (-2, 7.5), (-4, 6.5), (-3, 0), (-4, -6.5)], fwd=fwd), wing)
poly(img, iso([(7.5, 0), (2, -2.2), (-6.5, -1.8), (-6.5, 1.8), (2, 2.2)], fwd=fwd), body)
poly(img, iso([(-6.5, -3.5), (-4, -0.5), (-4, 0.5), (-6.5, 3.5), (-7.5, 3), (-7.5, -3)], fwd=fwd), wing)
ellipse(img, *iso([(2.5, 0)], fwd=fwd)[0], 1.6, 1.3, canopy)
return img
SHIP = ship(15, 12, 6)
SHADOW = [[4 if c else 0 for c in row] for row in ship(1, 1, 1)]
FIGHTER = ship(13, 1, 11, fwd=(-F[0], -F[1]))
TURRET = blank()
ellipse(TURRET, 8, 11.5, 6.5, 3.5, 8)
ellipse(TURRET, 8, 9.5, 5, 3.5, 2)
ellipse(TURRET, 7, 8.5, 2, 1.3, 10)
poly(TURRET, [(6.5, 10), (8.5, 11), (3.5, 14.5), (2, 13.5)], 7)
TANK = blank()
rect(TANK, 3, 4, 13, 12, 9)
ellipse(TANK, 8, 12, 5, 2.5, 1)
rect(TANK, 3, 4, 13, 12, 9)
ellipse(TANK, 8, 4, 5, 2.5, 11)
for y in range(6, 11):
TANK[y][5] = 15
rect(TANK, 7, 6, 9, 11, 1)
rect(TANK, 7, 6, 11, 7, 1)
rect(TANK, 7, 8, 10, 9, 1)
RADAR = blank()
rect(RADAR, 7, 7, 9, 14, 7)
ellipse(RADAR, 8, 14, 4, 1.6, 8)
poly(RADAR, [(2, 2), (13, 5), (12, 8), (3, 6)], 14)
poly(RADAR, [(4, 3.5), (11, 5.5), (10.5, 6.8), (4.5, 5)], 6)
CORE = blank()
poly(CORE, [(8, 0.5), (15, 7), (8, 14.5), (1, 7)], 5)
poly(CORE, [(8, 3), (12.5, 7), (8, 11.5), (3.5, 7)], 13)
ellipse(CORE, 8, 7, 2.2, 2.2, 15)
def burst(seed, cols):
r = random.Random(seed)
img = blank()
for k in range(70):
a = r.uniform(0, 2 * math.pi)
d = r.uniform(0, 7) ** 0.9
x, y = int(8 + d * math.cos(a)), int(8 + d * math.sin(a) * 0.8)
if 0 <= x < 16 and 0 <= y < 16:
img[y][x] = cols[0] if d < 2.5 else cols[1] if d < 5 else cols[2]
return img
BOOM1 = burst(3, (15, 11, 9))
BOOM2 = burst(5, (11, 9, 1))
SHOT = blank()
ellipse(SHOT, 8, 8, 1.6, 1.2, 15)
SHOT[8][8] = 11
SHELL = blank()
ellipse(SHELL, 8, 8, 1.8, 1.8, 9)
SHELL[8][8] = 15
SHAPES = [("S_SHIP", SHIP), ("S_SHADOW", SHADOW), ("S_FIGHTER", FIGHTER), ("S_TURRET", TURRET),
("S_TANK", TANK), ("S_RADAR", RADAR), ("S_CORE", CORE), ("S_BOOM1", BOOM1),
("S_BOOM2", BOOM2), ("S_SHOT", SHOT), ("S_SHELL", SHELL)]
# --------------------------------------------------------------- level
BW = 10 # fortress width in tiles
LOW_H, TALL_H, DOOR_H = 20, 56, 30
# row byte: bit 0 fortress floor; bits 2:1 wall at the far edge of the row
# (0 none, 1 low, 2 tall with a door); bits 6:3 the door's first tile;
# bit 7 fighters attack (space)
FORT, WALL_LOW, WALL_DOOR, FIGHTERS = 1, 2, 4, 128
O_TURRET, O_TANK, O_RADAR, O_CORE = 1, 2, 3, 4
def level():
rnd = random.Random(1983)
rows = []
objs = []
def space(n, fighters):
rows.extend([FIGHTERS if fighters else 0] * n)
def fortress(n, walls, turrets, tanks, radars, core=False):
start = len(rows)
rows.extend([FORT] * n)
wall_rows = []
r = start + 10
k = 0
while r < start + n - 6 and k < len(walls):
if walls[k] == "L":
rows[r] |= WALL_LOW
else:
rows[r] |= WALL_DOOR | (rnd.randrange(0, BW - 1) << 3)
wall_rows.append(r)
r += rnd.randrange(9, 14)
k += 1
# objects: not on the rows just behind a wall, where its face hides them
hidden = {w + d for w in wall_rows for d in range(0, 4)}
free = [(rr, i) for rr in range(start + 4, start + n - 3) for i in range(1, BW - 1)
if rr not in hidden]
rnd.shuffle(free)
used = set()
def place(t, count):
nonlocal free
for _ in range(count):
while free:
rr, i = free.pop()
if all(abs(rr - a) > 1 or abs(i - b) > 1 for a, b in used):
used.add((rr, i)); objs.append((rr, t, i)); break
place(O_TURRET, turrets)
place(O_TANK, tanks)
place(O_RADAR, radars)
if core:
objs.append((start + n - 3, O_CORE, BW // 2))
space(14, False)
fortress(120, "LDLDDLDD", 10, 7, 3)
space(60, True)
fortress(140, "DLDDLDDLD", 13, 7, 3, core=True)
space(16, False)
objs.sort()
return rows, objs
def main(out):
rows, objs = level()
L = ["/* made by gen.py - edit that instead */", ""]
for i, (n, _) in enumerate(SHAPES):
L.append("#define %s %d" % (n, i))
L.append("#define N_SHAPES %d" % len(SHAPES))
L.append("#define BW %d" % BW)
L.append("#define LOW_H %d\n#define TALL_H %d\n#define DOOR_H %d" % (LOW_H, TALL_H, DOOR_H))
L.append("#define R_FORT 1\n#define R_WALL 6\n#define R_LOW 2\n#define R_DOOR 4\n#define R_FIGHTERS 128")
L.append("#define O_TURRET %d\n#define O_TANK %d\n#define O_RADAR %d\n#define O_CORE %d" %
(O_TURRET, O_TANK, O_RADAR, O_CORE))
L.append("static const u8 shape_data[N_SHAPES * 128] = {")
for n, img in SHAPES:
b = [img[y][x] << 4 | img[y][x + 1] for y in range(16) for x in range(0, 16, 2)]
L.append(" " + ",".join(str(v) for v in b) + ", /* %s */" % n)
L.append("};")
L.append("#define LEVEL_ROWS %d" % len(rows))
L.append("static const u8 level_rows[LEVEL_ROWS] = {")
for k in range(0, len(rows), 20):
L.append(" " + ",".join(str(v) for v in rows[k:k + 20]) + ",")
L.append("};")
L.append("#define N_OBJS %d" % len(objs))
L.append("/* row (2 bytes), type, tile across */")
L.append("static const u8 level_objs[N_OBJS * 4] = {")
for r, t, i in objs:
L.append(" %d,%d,%d,%d," % (r & 255, r >> 8, t, i))
L.append("};")
open(out, "w").write("\n".join(L) + "\n")
walls = sum(1 for r in rows if r & 6)
print("data.h: %d shapes, %d rows, %d walls, %d objects" % (len(SHAPES), len(rows), walls, len(objs)))
if __name__ == "__main__":
if len(sys.argv) > 2 and sys.argv[2] == "--show":
for n, img in SHAPES:
print(n)
for row in img:
print("".join(" .:-=+*#%@ABCDEF"[c] if c else " " for c in row))
main(sys.argv[1] if len(sys.argv) > 1 else "data.h")
+4
View File
@@ -0,0 +1,4 @@
10 REM BASTION RAID: LOAD THE GAME (MEMORY FILE RAIDGAME) AND PLAY IT
20 REM THE GAME RUNS AT A200H, PAST THIS PROGRAM, AND COMES BACK HERE
30 BLOAD "RAIDGAME": CALL 41472
40 CLS
+696
View File
@@ -0,0 +1,696 @@
/* Bastion Raid: an isometric scrolling shooter for the Basys 3 RISC-V computer.
Fly over a fortress that scrolls diagonally towards you. Climb over low
walls, fly low through the doorways in tall ones, shoot turrets before they
shoot you, and shoot fuel tanks to fill up. Between fortresses, fighters
come at you through space. The reactor core waits at the far end. Your
shadow on the floor and the altimeter show how high you are.
The world is drawn into the graphics layer, which the hardware scrolls
(ports 35h-37h): each frame the picture moves two pixels left and one
down, and only the two new columns and the new row are drawn, pixel by
pixel from the world's description (world()). Everything that moves is a
sprite. It runs at A200h, loaded by the RAID program in BASIC.
World coordinates: a pixel (WX, WY) of the unscrolled world has
s = WX + 2 WY (across the flight path) and d = 2 WY - WX (along it, growing
towards the viewer). Tiles are 32 x 32 in (s, d): tile i = s >> 5,
j = d >> 5, and level row r = -j. The ship flies towards smaller d. */
typedef unsigned char u8;
typedef signed char i8;
typedef unsigned short u16;
typedef short i16;
#include "basys3.h"
#define con_stat IO8(0x00)
#define con_data IO8(0x01)
#define gx_lo IO8(0x30)
#define gx_hi IO8(0x31)
#define gy_p IO8(0x32)
#define gpix IO8(0x33)
#define gfill IO8(0x34)
#define scr_xlo IO8(0x35)
#define scr_xhi IO8(0x36)
#define scr_y IO8(0x37)
#define sp_sel IO8(0x70)
#define sp_xlo IO8(0x71)
#define sp_xhi IO8(0x72)
#define sp_y IO8(0x73)
#define sp_shape IO8(0x74)
#define sp_ctl IO8(0x75)
#define sp_load IO8(0x76)
#define sp_data IO8(0x77)
#define frame_ctr IO8(0x78)
#define joy IO8(0x88)
#include "data.h" /* out/data.h, made by gen.py */
#define J_UP 1
#define J_DOWN 2
#define J_LEFT 4
#define J_RIGHT 8
#define J_FIRE 16
#define J_ENTER 32
#define J_ESC 128
/* sprites: lower numbers in front */
#define SP_SHIP 0
#define SP_SHOT 1 /* 3 */
#define SP_FIGHT 4 /* 4 */
#define SP_SHELL 8 /* 4 */
#define SP_BOOM 12 /* 3 */
#define SP_OBJ 15 /* 12 */
#define SP_SHADOW 27
#define NSHOT 3
#define NFIGHT 4
#define NSHELL 4
#define NBOOM 3
#define NOBJ 12
#define SHIP_D 128 /* the ship's ground point is 4 tiles nearer than the screen centre */
#define MAX_ALT 48
#define BACK_H 24 /* the fortress's back wall */
#define FRONT_H 20 /* and its front face, below the floor */
/* ---------------------------------------------------------------- state */
static i16 cx, cy; /* world pixel at the screen's top left */
static u16 fbx; /* the same, in the 320 x 240 buffer */
static u8 fby;
i16 cam_d; /* d at the screen's centre */
i16 ship_s, ship_d;
u8 alt, lives, fuel, level, inv, dead;
u8 shield; /* tests only: shells, fighters and objects can't hit */
static u8 fuel_tick, fire_held, dead_time;
static u16 score;
static i16 shot_s[NSHOT], shot_d[NSHOT];
static u8 shot_alt[NSHOT], shot_life[NSHOT];
static i16 fight_s[NFIGHT], fight_d[NFIGHT];
static u8 fight_alt[NFIGHT], fight_on[NFIGHT];
static i16 shell_s[NSHELL], shell_d[NSHELL];
static u8 shell_life[NSHELL];
static i16 boom_s[NBOOM], boom_d[NBOOM];
static u8 boom_alt[NBOOM], boom_t[NBOOM];
static i16 obj_s[NOBJ], obj_d[NOBJ];
static u8 obj_type[NOBJ], obj_on[NOBJ], obj_cool[NOBJ], obj_hits[NOBJ];
static u16 next_obj;
static u8 fight_timer;
static u8 last_frame, quit, hud_dirty;
static u8 shown_fuel, shown_alt;
static u16 rnd_state;
static const char *msg;
static u8 msg_time;
/* ----------------------------------------------------------------- text */
static void putch(char c) { while (!(con_stat & 2)); con_data = c; }
static void puts_(const char *s) { while (*s) putch(*s++); }
static void putn(u16 n, u8 width)
{
char b[6];
u8 i = 0;
do { b[i++] = '0' + n % 10; n /= 10; } while (n);
while (width > i) { putch('0'); width--; }
while (i) putch(b[--i]);
}
static void at(u8 r, u8 c) { putch(27); putch('['); putn(r, 0); putch(';'); putn(c, 0); putch('H'); }
static void ink(const char *s) { putch(27); putch('['); puts_(s); putch('m'); }
static void centre(u8 r, const char *s)
{
u8 n = 0;
while (s[n]) n++;
at(r, 41 - n / 2);
puts_(s);
}
static u16 rnd(void)
{
rnd_state ^= rnd_state << 7;
rnd_state ^= rnd_state >> 9;
rnd_state ^= rnd_state << 8;
return rnd_state;
}
static void wait_frame(void)
{
while (frame_ctr == last_frame);
last_frame = frame_ctr;
}
static u8 drain_keys(void)
{
u8 c, p = 0;
while (con_stat & 1) {
c = con_data;
if (c == 'p' || c == 'P') p = 1;
}
return p;
}
/* ---------------------------------------------------------------- world */
static u8 row_info(i16 r)
{
return (r >= 0 && r < LEVEL_ROWS) ? level_rows[r] : 0;
}
/* The colour of world point (s, d). */
static u8 world(i16 s, i16 d)
{
i16 jw, jlo, k2, sb, db;
u8 ri, k, ib, g, h;
/* walls across the path: the face of the wall at the far edge of row
-jw covers the 2H units of d in front of the line d = 32 jw. The
nearest wall is tried first. */
jlo = (d >> 5) + 1;
for (jw = (d + 2 * TALL_H) >> 5; jw >= jlo; jw--) {
ri = row_info(-jw);
if (!(ri & R_WALL)) continue;
k2 = (jw << 5) - d;
h = (ri & R_DOOR) ? TALL_H : LOW_H;
k = (u8)(k2 >> 1);
if (k > h) continue;
sb = s + k2;
if (sb < 0 || sb >= 32 * BW) continue;
ib = (u8)(sb >> 5);
if (ri & R_DOOR) {
g = (ri >> 3) & 15;
if (ib >= g && ib < g + 2) {
if (k < DOOR_H) continue; /* the doorway */
if (k < DOOR_H + 3) return 14; /* its lintel */
}
}
if (k >= h - 1) return (ri & R_DOOR) ? 15 : 11; /* the top */
if (!(k & 7) || ((sb + ((k & 8) << 1)) & 31) < 2) return 8;
return (ri & R_DOOR) ? 7 : 3;
}
/* the fortress's back wall, rising from its far side (s = 0) */
if (s < 0 && s >= -2 * BACK_H) {
db = d - s;
if (row_info(-(db >> 5)) & R_FORT) {
k = (u8)((-s) >> 1);
if (k >= BACK_H - 1) return 7;
return (k & 3) ? 8 : 0;
}
}
/* its front face, below the near side (s = 32 BW) */
if (s >= 32 * BW && s < 32 * BW + 2 * FRONT_H) {
k2 = s - 32 * BW;
db = d - k2;
if (row_info(-(db >> 5)) & R_FORT)
return ((db & 31) < 2) ? 0 : 4;
}
/* the floor */
if (s >= 0 && s < 32 * BW && (row_info(-(d >> 5)) & R_FORT)) {
if ((s & 31) < 2 || (d & 31) < 2) return 0;
if ((((s >> 5) + (d >> 5)) & 3) == 0 && (s & 31) > 13 && (s & 31) < 18 &&
(d & 31) > 13 && (d & 31) < 18) return 7;
return 8;
}
/* space */
if (((s * 5 + d * 3) & 255) == 7 && !((s ^ d) & 0x60))
return (s & 16) ? 15 : 7;
return 0;
}
/* Draw screen columns xs and xs + 1 (xs even) from the world. */
static void draw_cols(u16 xs)
{
i16 wx = cx + xs, s = wx + 2 * cy, d = 2 * cy - wx;
u16 fx = fbx + xs;
u8 y, fy = fby, c0, c1, flo, fhi;
if (fx >= 320) fx -= 320;
flo = (u8)fx; fhi = (u8)(fx >> 8);
for (y = 0; y < 240; y++) {
c0 = world(s, d);
c1 = world(s + 1, d - 1);
gx_lo = flo; gx_hi = fhi; gy_p = fy;
gpix = c0; gpix = c1;
s += 2; d += 2;
if (++fy == 240) fy = 0;
}
}
/* Draw screen row ys from the world. */
static void draw_row(u8 ys)
{
i16 wy = cy + ys, s = cx + 2 * wy, d = 2 * wy - cx;
u16 x, fx = fbx;
u8 fy = fby + ys;
if (fy >= 240) fy -= 240;
gx_lo = (u8)fx; gx_hi = (u8)(fx >> 8); gy_p = fy;
for (x = 0; x < 320; x++) {
gpix = world(s, d);
s++; d--;
if (++fx == 320) { fx = 0; gx_lo = 0; gx_hi = 0; gy_p = fy; }
}
}
/* Show the camera's view. The border bit hides the screen's edges, where
advance() draws the next columns and row while this frame is on screen. */
static void set_scroll(void)
{
scr_xlo = (u8)fbx; scr_xhi = (u8)(fbx >> 8) | 2; scr_y = fby;
}
/* Put the camera at level row r (the screen centre), and draw it all. */
static void camera_at(i16 r)
{
u8 y;
/* centre: s = 16 BW (the fortress's middle), d = -32 r + 16 */
i16 sc = 16 * BW, dc = -32 * r + 16;
/* WX = (s - d) / 2, WY = (s + d) / 4, both even-friendly */
cx = ((sc - dc) >> 1) - 160;
cy = ((sc + dc) >> 2) - 120;
cx &= ~1;
cam_d = 2 * (cy + 120) - (cx + 160);
fbx = (u16)(cx % 320); if ((i16)fbx < 0) fbx += 320;
{ i16 t = cy % 240; if (t < 0) t += 240; fby = (u8)t; }
for (y = 0; y < 240; y++) draw_row(y);
}
/* Fly on: the camera moves 2 right and 1 up; draw what comes into view. */
static void advance(void)
{
cx += 2; cy -= 1;
cam_d -= 4;
fbx += 2; if (fbx >= 320) fbx -= 320;
fby = fby ? fby - 1 : 239;
draw_cols(318);
draw_row(0);
}
/* ------------------------------------------------------------- sprites */
static void spr_place(u8 n, i16 s, i16 d, u8 up, u8 shape, u8 ctl)
{
i16 x = ((s - d) >> 1) - cx - 8, y = ((s + d) >> 2) - cy - 8 - up;
sp_sel = n;
if (x < -15 || x > 319 || y < -15 || y > 239) { sp_ctl = 0; return; }
sp_xlo = (u8)x; sp_xhi = (u8)(x >> 8) & 1; sp_y = (u8)y;
sp_shape = shape; sp_ctl = ctl;
}
static void spr_off(u8 n) { sp_sel = n; sp_ctl = 0; }
static void all_off(void) { u8 i; for (i = 0; i < 32; i++) spr_off(i); }
static void load_shapes(void)
{
const u8 *p = shape_data;
u8 s, i;
for (s = 0; s < N_SHAPES; s++) {
sp_load = s;
for (i = 0; i < 128; i++) sp_data = *p++;
}
}
static i16 absd(i16 v) { return v < 0 ? -v : v; }
static void boom(i16 s, i16 d, u8 up)
{
u8 i, best = 0;
for (i = 0; i < NBOOM; i++) if (boom_t[i] < boom_t[best]) best = i;
boom_s[best] = s; boom_d[best] = d; boom_alt[best] = up; boom_t[best] = 24;
}
static void say(const char *s) { msg = s; msg_time = 120; hud_dirty = 1; }
/* ------------------------------------------------------------------ HUD */
static void bar(u8 n, u8 of)
{
u8 i;
putch('[');
for (i = 0; i < of; i++) putch(i < n ? '#' : '-');
putch(']');
}
static void hud(void)
{
hud_dirty = 0;
shown_fuel = fuel; shown_alt = alt / 6;
at(1, 1); ink("0;93"); puts_("BASTION RAID ");
ink("0;37"); puts_("SCORE "); ink("0;97"); putn(score, 6);
ink("0;37"); puts_(" LEVEL "); ink("0;97"); putn(level, 0);
ink("0;37"); puts_(" SHIPS "); ink("0;97"); putn(lives, 0);
puts_("\033[K");
at(2, 1); ink(fuel < 25 ? "0;91" : "0;37"); puts_("FUEL "); bar((fuel + 9) / 10, 10);
ink("0;37"); puts_(" ALT "); ink("0;96"); bar(shown_alt + 1, 9);
if (msg && msg_time) { ink("0;93"); puts_(" "); puts_(msg); }
puts_("\033[K");
ink("0");
}
/* ---------------------------------------------------------------- rules */
/* Does the wall at the far edge of row r stop something at s, height a? */
static u8 wall_stops(i16 r, i16 s, u8 a, u8 half)
{
u8 ri = row_info(r), g;
if (!(ri & R_WALL) || s < -half || s >= 32 * BW + half) return 0;
if (ri & R_LOW) return a <= LOW_H + 2;
if (a > TALL_H) return 0;
g = (ri >> 3) & 15;
return !(s - half >= 32 * g && s + half < 32 * g + 64 && a + 8 < DOOR_H);
}
static void crash(u8 wall)
{
u8 i;
if (inv || dead || (shield && !wall)) return;
boom(ship_s, ship_d, alt);
spr_off(SP_SHIP); spr_off(SP_SHADOW);
dead = 1; dead_time = 90;
for (i = 0; i < NSHELL; i++) { shell_life[i] = 0; spr_off(SP_SHELL + i); }
}
static void objects_in(void)
{
const u8 *o;
i16 r, ahead = -((cam_d - 12 * 32) >> 5);
u8 k;
while (next_obj < N_OBJS) {
o = level_objs + next_obj * 4;
r = o[0] | o[1] << 8;
if (r > ahead) break;
for (k = 0; k < NOBJ && obj_on[k]; k++);
if (k == NOBJ) break;
obj_on[k] = 1; obj_type[k] = o[2];
obj_s[k] = 32 * o[3] + 16; obj_d[k] = -32 * r + 16;
obj_cool[k] = 40 + (rnd() & 63);
obj_hits[k] = o[2] == O_CORE ? 4 : 1;
next_obj++;
}
}
static void hit_object(u8 k)
{
if (--obj_hits[k]) { boom(obj_s[k], obj_d[k], 4); return; }
obj_on[k] = 0;
spr_off(SP_OBJ + k);
boom(obj_s[k], obj_d[k], 0);
switch (obj_type[k]) {
case O_TURRET: score += 100; break;
case O_TANK: score += 150; fuel = fuel > 75 ? 100 : fuel + 25; say("FUEL"); break;
case O_RADAR: score += 300; break;
case O_CORE: score += 2000; say("REACTOR DESTROYED!"); break;
}
hud_dirty = 1;
}
static void objects(void)
{
static const u8 shapes[5] = { 0, S_TURRET, S_TANK, S_RADAR, S_CORE };
u8 k, i;
for (k = 0; k < NOBJ; k++) {
if (!obj_on[k]) continue;
if (obj_d[k] > cam_d + 8 * 32) { obj_on[k] = 0; spr_off(SP_OBJ + k); continue; }
spr_place(SP_OBJ + k, obj_s[k], obj_d[k], 4, shapes[obj_type[k]], 1);
if (!dead && alt < 12 && absd(obj_s[k] - ship_s) < 16 && absd(obj_d[k] - ship_d) < 16) {
hit_object(k); crash(0); continue;
}
if (obj_type[k] == O_TURRET && !dead && obj_d[k] < ship_d && obj_d[k] > ship_d - 8 * 32) {
if (obj_cool[k]) obj_cool[k]--;
else {
for (i = 0; i < NSHELL && shell_life[i]; i++);
if (i < NSHELL) {
shell_s[i] = obj_s[k]; shell_d[i] = obj_d[k]; shell_life[i] = 70;
}
obj_cool[k] = (level > 3 ? 40 : 100 - 20 * level) + (rnd() & 63);
}
}
}
}
static void shells(void)
{
u8 i;
for (i = 0; i < NSHELL; i++) {
if (!shell_life[i]) continue;
shell_life[i]--;
shell_d[i] += 6;
if (shell_s[i] < ship_s) shell_s[i]++; else if (shell_s[i] > ship_s) shell_s[i]--;
/* moving towards the viewer, it crosses the far edge of row -(d >> 5) */
if (!shell_life[i] || (((shell_d[i] - 6) >> 5) != (shell_d[i] >> 5) &&
wall_stops(-(shell_d[i] >> 5), shell_s[i], 10, 2))) {
shell_life[i] = 0; spr_off(SP_SHELL + i); continue;
}
spr_place(SP_SHELL + i, shell_s[i], shell_d[i], 10, S_SHELL, 1);
if (!dead && absd(shell_s[i] - ship_s) < 12 && absd(shell_d[i] - ship_d) < 12 && alt < 22) {
shell_life[i] = 0; spr_off(SP_SHELL + i);
crash(0);
}
}
}
static void fighters(void)
{
u8 i, k;
if (!dead && (row_info(-(cam_d >> 5) + 4) & R_FIGHTERS) && !--fight_timer) {
fight_timer = (level > 3 ? 25 : 55 - 10 * level) + (rnd() & 31);
for (i = 0; i < NFIGHT && fight_on[i]; i++);
if (i < NFIGHT) {
fight_on[i] = 1;
fight_s[i] = 24 + (rnd() % (32 * BW - 48));
fight_d[i] = cam_d - 7 * 32;
fight_alt[i] = 6 + (rnd() % 40);
}
}
for (i = 0; i < NFIGHT; i++) {
if (!fight_on[i]) continue;
fight_d[i] += 6;
if (!(last_frame & 3)) {
if (fight_alt[i] < alt) fight_alt[i]++; else if (fight_alt[i] > alt) fight_alt[i]--;
}
if (fight_d[i] > cam_d + 8 * 32) { fight_on[i] = 0; spr_off(SP_FIGHT + i); continue; }
spr_place(SP_FIGHT + i, fight_s[i], fight_d[i], fight_alt[i], S_FIGHTER, 1);
if (!dead && absd(fight_s[i] - ship_s) < 16 && absd(fight_d[i] - ship_d) < 16 &&
absd(fight_alt[i] - alt) < 10) {
fight_on[i] = 0; spr_off(SP_FIGHT + i);
boom(fight_s[i], fight_d[i], fight_alt[i]);
crash(0);
}
for (k = 0; k < NSHOT; k++)
if (shot_life[k] && absd(shot_s[k] - fight_s[i]) < 16 && absd(shot_d[k] - fight_d[i]) < 20 &&
absd(shot_alt[k] - fight_alt[i]) < 12) {
shot_life[k] = 0; spr_off(SP_SHOT + k);
fight_on[i] = 0; spr_off(SP_FIGHT + i);
boom(fight_s[i], fight_d[i], fight_alt[i]);
score += 200; hud_dirty = 1;
break;
}
}
}
static void shots(u8 j)
{
u8 i, k;
i16 od;
if (!(j & J_FIRE)) fire_held = 0;
else if (!fire_held && !dead) {
fire_held = 1;
for (i = 0; i < NSHOT && shot_life[i]; i++);
if (i < NSHOT) {
shot_s[i] = ship_s; shot_d[i] = ship_d - 12; shot_alt[i] = alt; shot_life[i] = 28;
}
}
for (i = 0; i < NSHOT; i++) {
if (!shot_life[i]) continue;
od = shot_d[i];
shot_d[i] -= 12;
shot_life[i]--;
if ((od >> 5) != (shot_d[i] >> 5) && wall_stops(-(od >> 5), shot_s[i], shot_alt[i], 2)) {
shot_life[i] = 0;
boom(shot_s[i], (od >> 5) << 5, shot_alt[i]);
}
if (shot_alt[i] < 16)
for (k = 0; k < NOBJ && shot_life[i]; k++)
if (obj_on[k] && absd(obj_s[k] - shot_s[i]) < 16 && absd(obj_d[k] - shot_d[i]) < 16) {
shot_life[i] = 0;
hit_object(k);
}
if (!shot_life[i]) { spr_off(SP_SHOT + i); continue; }
spr_place(SP_SHOT + i, shot_s[i], shot_d[i], shot_alt[i], S_SHOT, 1);
}
}
static void booms(void)
{
u8 i;
for (i = 0; i < NBOOM; i++) {
if (!boom_t[i]) continue;
boom_t[i]--;
if (!boom_t[i]) { spr_off(SP_BOOM + i); continue; }
spr_place(SP_BOOM + i, boom_s[i], boom_d[i], boom_alt[i], (boom_t[i] & 4) ? S_BOOM1 : S_BOOM2, 1);
}
}
static void fly(u8 j)
{
i16 od = ship_d;
if (dead) return;
if (j & J_LEFT) ship_s -= 3;
if (j & J_RIGHT) ship_s += 3;
if (ship_s < 16) ship_s = 16;
if (ship_s > 32 * BW - 16) ship_s = 32 * BW - 16;
if ((j & J_UP) && alt < MAX_ALT) alt++;
if ((j & J_DOWN) && alt > 2) alt--;
ship_d = cam_d + SHIP_D;
if ((od >> 5) != (ship_d >> 5) && !inv && wall_stops(-(od >> 5), ship_s, alt, 10)) {
say("YOU HIT THE WALL");
crash(1);
return;
}
if (++fuel_tick >= 24) {
fuel_tick = 0;
if (fuel) fuel--;
if (!fuel) { say("OUT OF FUEL"); crash(0); return; }
if (fuel != shown_fuel) hud_dirty = 1;
}
if (alt / 6 != shown_alt) hud_dirty = 1;
spr_place(SP_SHIP, ship_s, ship_d, alt, S_SHIP, (inv & 4) ? 0 : 1);
if ((row_info(-(ship_d >> 5)) & R_FORT) && ship_s >= 0 && ship_s < 32 * BW)
spr_place(SP_SHADOW, ship_s, ship_d, 0, S_SHADOW, 1);
else
spr_off(SP_SHADOW);
}
/* ------------------------------------------------------------- screens */
static void clear_all(void)
{
gfill = 0; /* also sprites off, scroll to 0 */
while (gfill & 1);
all_off();
}
static u8 wait_fire(void)
{
u8 j;
while (joy & (J_FIRE | J_ENTER | J_ESC)) { wait_frame(); drain_keys(); }
for (;;) {
wait_frame(); drain_keys(); rnd();
j = joy;
if (j & J_ESC) return 0;
if (j & (J_FIRE | J_ENTER)) return 1;
}
}
static u8 title(void)
{
clear_all();
puts_("\033[0m\033[2J");
camera_at(12);
set_scroll();
ship_s = 16 * BW; ship_d = cam_d + SHIP_D; alt = 24;
spr_place(SP_SHIP, ship_s, ship_d, alt, S_SHIP, 1);
ink("1;93"); centre(5, "B A S T I O N R A I D");
ink("0;97");
centre(8, "Fly over the fortress and destroy its reactor core.");
centre(10, "UP / DOWN climb and dive LEFT / RIGHT steer SPACE fire");
centre(12, "Climb over low walls. Fly low through the doors in tall ones.");
centre(13, "Your shadow and the altimeter show your height.");
centre(14, "Shoot fuel tanks for fuel. Turrets and fighters shoot back.");
centre(16, "P pauses. ESC gives up.");
ink("1;96"); centre(28, "PRESS SPACE TO BEGIN");
ink("0");
return wait_fire();
}
static void reset_level(void)
{
u8 i;
for (i = 0; i < NSHOT; i++) shot_life[i] = 0;
for (i = 0; i < NFIGHT; i++) fight_on[i] = 0;
for (i = 0; i < NSHELL; i++) shell_life[i] = 0;
for (i = 0; i < NBOOM; i++) boom_t[i] = 0;
for (i = 0; i < NOBJ; i++) obj_on[i] = 0;
next_obj = 0;
fight_timer = 30;
clear_all();
camera_at(-6);
set_scroll();
ship_s = 16 * BW; alt = 24; ship_d = cam_d + SHIP_D;
inv = 90; dead = 0;
}
/* Play until the ships run out (or Esc); returns 1 to go on to the title. */
static void play(void)
{
u8 j, i;
lives = 3; score = 0; level = 1;
puts_("\033[2J");
for (;;) {
fuel = 100; fuel_tick = 0; fire_held = 1;
reset_level();
msg = 0; msg_time = 0;
say("LEVEL START");
hud();
for (;;) {
wait_frame();
set_scroll();
if (drain_keys()) {
at(2, 50); ink("0;93"); puts_("PAUSED - P TO GO ON\033[K"); ink("0");
for (;;) {
wait_frame();
if (joy & J_ESC) { quit = 1; return; }
if (drain_keys()) break;
}
hud_dirty = 1;
}
j = joy;
if (j & J_ESC) return;
fly(j);
objects_in();
objects();
shots(j);
shells();
fighters();
booms();
if (inv) inv--;
if (msg_time && !--msg_time) hud_dirty = 1;
if (hud_dirty) hud();
if (dead) {
if (!--dead_time) {
if (!--lives) {
hud();
ink("0;44;97");
centre(13, " ");
centre(14, " GAME OVER ");
centre(15, " ");
ink("0");
for (i = 0; i < 120; i++) { wait_frame(); drain_keys(); }
return;
}
dead = 0; inv = 120;
if (fuel < 40) fuel = 40;
alt = 24;
hud_dirty = 1;
}
}
if (-(ship_d >> 5) >= LEVEL_ROWS) break; /* through the level */
advance();
}
score += 500;
level++;
say("LEVEL CLEARED");
hud();
for (i = 0; i < 90; i++) { wait_frame(); drain_keys(); }
}
}
int main(void)
{
rnd_state = 0xBEEF ^ frame_ctr;
last_frame = frame_ctr;
puts_("\033[?3l\033[?25l");
load_shapes();
quit = 0;
while (!quit) {
if (!title()) break;
play();
}
clear_all();
puts_("\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+758
View File
@@ -0,0 +1,758 @@
:10A20000973200009382C2F3173300001303C30197
:10A2100063F8620023A00200938242006FF05FFFA8
:10A2200017130000670003E0834700F093F727004F
:10A23000E38C07FEA300A0F067800000130101FF7C
:10A240002324810023261100130405000345040084
:10A25000631A05008320C10003248100130101015A
:10A26000678000001304140097000000E78000FCE2
:10A270006FF0DFFD130101FE232C8100232A9100E2
:10A28000232E110093840500130400009306A00000
:10A29000130690003377D5029307040093870701D4
:10A2A000B3872700130414001374F40F1307070374
:10A2B000238CE7FE930705003355D502E36CF6FCCB
:10A2C000636E9400631804028320C101032481019A
:10A2D000832441011301010267800000130500037C
:10A2E0009384F4FF97000000E78040F493F4F40FA8
:10A2F0006FF01FFD1304F4FF1374F40F93070401B0
:10A30000B387270003C587FF97000000E78000F2AE
:10A310006FF05FFB130101FF2322910093040500FE
:10A320001305B001232611002324810013840500A6
:10A3300097000000E78080EF1305B005970000004C
:10A34000E780C0EE9305000013850400970000002D
:10A35000E78080F21305B00397000000E78000ED6E
:10A36000130504009305000097000000E780C0F08B
:10A37000032481008320C10083244100130580044D
:10A380001301010117030000670043EA130101FFF5
:10A3900023248100130405001305B00123261100B6
:10A3A00097000000E78080E81305B00597000000E3
:10A3B000E780C0E71305040097000000E78040E84D
:10A3C000032481008320C1001305D006130101017D
:10A3D00017030000670083E5130101FF23248100B8
:10A3E0002326110013840500930700003307F400AF
:10A3F00003470700631A070293D7170093059002DB
:10A40000B385F54093F5F50F97000000E780C0F0A5
:10A4100013050400032481008320C10013010101FE
:10A42000170300006700C3E19387170093F7F70F46
:10A430006FF0DFFBB7D7000003D547141317750083
:10A440003345E50013150501135505011357950014
:10A450003345E500131785003345E5001315050165
:10A46000135505012392A71467800000B7D7000099
:10A4700003C7A714834680F7E38EE6FE034780F701
:10A480002385E7146780000013050000930600058C
:10A49000834700F093F7170063940700678000007C
:10A4A000034710F01377F70D6304D70093070500F7
:10A4B000138507006FF0DFFDB7D7000003C7E72162
:10A4C000A30AE0F283D7E72193D7870093E7270019
:10A4D000230BF0F2B7D7000083C7C721A30BF0F21C
:10A4E00067800000B7D8000083D828223388C54091
:10A4F000135818403308184113180801135808015D
:10A50000B7D80000839808222308A0F61305780026
:10A5100013150501135505011303E014636EA3021F
:10A52000B385C500130680FF33061641B306D64037
:10A5300093D52540B385D500139605019395050164
:10A5400093D50541930610FF1356060163C6D50047
:10A550009306F00E63D6B600A30A00F667800000EB
:10A56000930688FF93F5F60F93D68600A308B0F6FE
:10A5700093F616002309D0F69376F60FA309D0F6CA
:10A58000230AE0F6A30AF0F667800000B7D70000C0
:10A590009386071B03C8160003C7071B9387071B77
:10A5A0003337E800B308D70003C8260083C6080085
:10A5B0006374D80013072000B7D6000013181700E3
:10A5C0009386061CB38606012390A600B7D600002A
:10A5D0009386861BB38606012390B600B7D600008B
:10A5E0009386461BB306D700B387E70013078001A5
:10A5F0002380C6002380E70067800000130101FF6D
:10A6000023202101130905001305B005232481002F
:10A6100023229100232611009384050013040000D7
:10A6200097000000E78080C01305D0026374240106
:10A6300013053002130414001374F40F9700000084
:10A64000E780C0BEE39284FE032481008320C10022
:10A6500083244100032901001305D00513010101E2
:10A6600017030000670083BC130101FE23282101AA
:10A6700037D9000003473921B7D7000023840714D6
:10A68000B7D70000A383E714B7D70000232C8100BD
:10A6900003C4572193076000930510003354F4025C
:10A6A00013051000B7D70000232E1100232A9100B4
:10A6B000232631012324410137CA0000B7C400001A
:10A6C000B7C900002383871497000000E780C0C447
:10A6D0001305CA6D97000000E78080CB37C50000E6
:10A6E0001305456E97000000E78080B51385446D23
:10A6F00097000000E780C0C937C500001305456F0B
:10A7000097000000E780C0B31385C96F9700000071
:10A71000E78000C8B7D7000003D5A72093056000E5
:10A7200097000000E78040B51385446D9700000056
:10A73000E78000C637C5000013054570970000008C
:10A74000E78000B01385C96F97000000E78040C420
:10A75000B7D7000003C5272193050000970000002C
:10A76000E78080B11385446D97000000E78040C208
:10A7700037C500001305057197000000E78040AC65
:10A780001385C96F97000000E78080C0B7D700002D
:10A7900003C5472193050000B7C9000097000000DA
:10A7A000E78080AD1385C97197000000E78040A95C
:10A7B000130520009305100097000000E780C0B546
:10A7C00003473921930780011385446D63E6E70051
:10A7D00037C500001305C56C97000000E78040BB3B
:10A7E00037C500001305057297000000E78040A5FB
:10A7F000034539219307A0009305A0001305950098
:10A800003345F5021375F50F97000000E78040DF30
:10A810001385446D97000000E78080B737C50000BE
:10A820001305857297000000E78080A137C50000FE
:10A830001305057397000000E78080B51305140029
:10A84000930590001375F50F97000000E78040DB3B
:10A85000B7D7000003A40714630C0402B7D70000A5
:10A8600083C7C713638607021305CA6D97000000EC
:10A87000E78000B237C5000013058573970000001C
:10A88000E780009C1305040097000000E780409BD0
:10A890001385C97197000000E780809A0324810125
:10A8A0008320C10183244101032901018329C100BF
:10A8B000032A810037C500001305C5731301010287
:10A8C000170300006700C3AC230A00F2834740F37C
:10A8D00093F71700E39C07FE130700022308F0F626
:10A8E00093871700A30A00F693F7F70FE398E7FEA4
:10A8F00067800000130101FF23248100B7D7000007
:10A9000037D4000083C717210347042123261100F1
:10A91000B3E7E7006392070AB7D7000083C7F720C1
:10A9200063840700630A0508B7D7000003C65721F0
:10A93000B7D7000083956721B7D70000039587211B
:10A9400097000000E780C0C4230800F6A30A00F6C1
:10A950009307B0012308F0F6A30A00F6930710004E
:10A960002308F4201307A005B7D700002386E720AB
:10A97000B7D700002384071C130780002308E0F6E4
:10A98000A30A00F69387871CA38007001307900093
:10A990002308E0F6A30A00F6238107001307A000AE
:10A9A0002308E0F6A30A00F6A38107009307B0008E
:10A9B0002308F0F6A30A00F68320C10003248100D7
:10A9C0001301010167800000B7D7000093870715C6
:10A9D000B387A70083C6070037D7000013070718FF
:10A9E0009386F6FF93F6F60F2380D700B7D70000C3
:10A9F0001316150093878719638006023307C70073
:10AA0000B387C700831507000395070013064000AE
:10AA1000170300006700C3B7B7D60000130101FF9A
:10AA2000938686162324810023261100B386A60070
:10AA3000238006009306F50093F6F60F2308D0F660
:10AA40003307C700B387C700A30A00F683150700C2
:10AA5000130405000395070013060000970000008B
:10AA6000E78000B3B7D7000093874717B387870005
:10AA700083C70700130730006380E70C6368F702A1
:10AA8000130710006388E70413072000638EE704B0
:10AA90008320C10003248100B7D7000013071000F2
:10AAA0002384E714130101016780000013074000AD
:10AAB000E390E7FE37D700008357A7209387077DF1
:10AAC0002315F720B7C7000037D700009387877496
:10AAD0006F00400537D700008357A72093874706AC
:10AAE0002315F7206FF0DFFA37D700008357A72030
:10AAF0001306B004938767092315F72037D70000A2
:10AB000083463721938706006374D6009307B00409
:10AB100093879701A309F720B7C7000037D7000034
:10AB2000938707742320F714B7D700001307800713
:10AB3000238EE7126FF0DFF537D700008357A72089
:10AB40009387C7126FF0DFF9131705011357070139
:10AB50009307D01563ECE706B7D700009387C7A526
:10AB60003385A7008347050013F5670063020506D8
:10AB70003307D0401305000063CCE5041387F613B8
:10AB80006348B70413F52700630605001335760103
:10AB90006780000013078003636CC7029397270048
:10ABA00093F7071E3387D54013051000634CF70059
:10ABB000B385D5009387070463D6F5001336660185
:10ABC000133516001375F50F67800000130500009C
:10ABD000678000009386050793D6564013D65540EC
:10ABE0009396060193D6060113160601139E0501DE
:10ABF000B30ED04013560601135E0E019396560015
:10AC0000130F1600B386C641130101FF131F0F0176
:10AC1000939E0E0113930601B7D2000023268100F4
:10AC2000232491002322210193070500135F0F4184
:10AC300093DE0E01135303019303D0159382C2A533
:10AC40001304F0139304D0013305D041131505010B
:10AC5000135505416350E5099396070193D6060104
:10AC60001385060313150501135505011307F0029B
:10AC7000636EA7143307DE4013170701135707410C
:10AC8000135757403307E0401317070113570701C5
:10AC90009306D01563EAE622B7D600009386C6A5D0
:10ACA0003387E6000347070013771700630E07207A
:10ACB000B307F04093D717401307600113057000E6
:10ACC0006346F70A93F73700B337F0001395370060
:10ACD0006F00C00963EED30D3387D20183460700AE
:10ACE00013F766006306070C1315030193FF460074
:10ACF000135505419308400163840F0093088003B6
:10AD0000135515401377F50F63E4E80A3308F30091
:10AD10001318080113580801636C0409131808017B
:10AD200013580841135558401379F50F63860F02E5
:10AD300093D6360093F6F600636ED900938616001C
:10AD400063CAA60063F6E406930700021305E00059
:10AD500063FEE700930F40009388F8FF63421703F8
:10AD60001305F00063940F001305B0000324C10025
:10AD70008324810003294100130101016780000041
:10AD80009377770013058000E38207FE13171700FF
:10AD900013770701330707011377E701E30807FC7F
:10ADA00013057000E3940FFC130530006FF01FFCD7
:10ADB000938E1E00930603FE939E0E0113930601CD
:10ADC00093DE0E01135303016FF01FE8138506ECA9
:10ADD000131505011355050113077002636CA704D1
:10ADE0003307AE4013150701131707011357074127
:10ADF000135757403307E040131707011357070154
:10AE00009306D0151355050163E0E60CB7D6000094
:10AE10009386C6A53387E60003470700137717001C
:10AE20006304070A1375E5013335A00013152500E7
:10AE30006FF0DFF31307F0136368D708B306C04061
:10AE40009396060193D606011307D015636ED706B5
:10AE500037D700001307C7A53307D70003470700FC
:10AE6000137717006302070613F7E70113050000C5
:10AE7000E30E07EE13F7E501E30A07EE13D7574099
:10AE80003306E6001376360013058000E31006EE65
:10AE900093F7F701938727FF9397070193D707014C
:10AEA00013073000E364F7EC13F5F501130525FFF4
:10AEB00013150501135505019307300033B5A7009D
:10AEC000130575006FF09FEA1307500093063000DA
:10AED000B386D502130500003387E7023307D70096
:10AEE0001377F70F93067000E312D7E8B3C5F500A8
:10AEF00093F50506E39C05E693F707011305F000BB
:10AF0000E39607E6130570006FF05FE6B7D7000021
:10AF100083D7072237D7000003572722B307F5004E
:10AF20009397170093970701130101FE93D7070129
:10AF300023244101338AE700B387E740232A9100A5
:10AF400093940701B7D700002328210103D9E721F3
:10AF5000B7D7000083C7C721232C8100131A0A0129
:10AF60003305F500232E1100232631012322510140
:10AF70001374F50F9307F00E135A0A0193D40401CA
:10AF800063F68700130404011374F40F9377F90F29
:10AF90002308F0F293578900A308F0F2230980F206
:10AFA00093090000930A0014B385344133053A0134
:10AFB000939505011315050193D5054113550541D9
:10AFC0001309190097000000E78000C11319090157
:10AFD000A309A0F213590901631A5901230800F2C9
:10AFE000A30800F2230980F21309000093891900D5
:10AFF0009399090193D90901E39859FB8320C10171
:10B000000324810183244101032901018329C10013
:10B01000032A8100832A4100130101026780000096
:10B02000130101F923261106232481062322910608
:10B0300023202107232E3105232C4105232A5105E6
:10B0400023286105232671052324810523229105E8
:10B050002320A105232EB103834780F737C7FFFFC5
:10B060001307F7EEB3C7E70037D700002312F71432
:10B07000034780F737C50000B7D700001305C57533
:10B080002385E71497F0FFFFE780801BB7D7000008
:10B090009387C7BB130700009306B000230BE0F6AD
:10B0A0001386070883C5070093871700A30BB0F624
:10B0B000E39AC7FE130717001377F70FE310D7FEC5
:10B0C000B7D70000A3840714B7D7000093878790F1
:10B0D0002322F102B7D700009387C79A2324F102F5
:10B0E000B7D7000003C497146310041E97F0FFFF46
:10B0F000E780C07D37C500001305857697F0FFFF18
:10B10000E78000149307800637D700002311F72249
:10B11000B7D60000130740F52390E622130700E995
:10B12000B7D60000239DE62037D70000231FF72065
:10B130009307400437D70000230EF7209304000F35
:10B1400013050400130414001374F40F9700000097
:10B15000E78000DCE31694FE97F0FFFFE7800036FF
:10B160009307000A37D40000231CF420B7D700004F
:10B1700003D6A721B7D70000B7D4000013060608EE
:10B180001316060113560641239BC720930780011F
:10B1900013070000930680019305000AA38AF42098
:10B1A000130500009307100097F0FFFFE780C033FE
:10B1B00037C500001305457797F0FFFFE780401D76
:10B1C000B7C500009385C5771305500097F0FFFFC2
:10B1D000E780C02037C500001305C56F97F0FFFF5B
:10B1E000E780001BB7C500009385457913058000F3
:10B1F00097F0FFFFE780801EB7C500009385857C30
:10B200001305A00097F0FFFFE780401DB7D50000B1
:10B210009385C5801305C00097F0FFFFE780001CF1
:10B22000B7D500009385C5841305D00097F0FFFFC4
:10B23000E780C01AB7D500009385C5871305E000E5
:10B2400097F0FFFFE7808019B7D500009385858BC5
:10B250001305000197F0FFFFE780401837D5000085
:10B260001305458D97F0FFFFE7808012B7D50000EA
:10B270001305C0019385C58D97F0FFFFE780001689
:10B28000B7C700001385C77397F0FFFFE780401032
:10B29000834780F893F7070B639C070897F0FFFF3D
:10B2A000E780001D97F0FFFFE780401E97F0FFFF4B
:10B2B000E7808018834780F813F7F70F939787018B
:10B2C00093D7874163D0070897F0FFFFE7800060BE
:10B2D00037D500001305458F97F0FFFFE78040F654
:10B2E000834700F093F71700638407006F10803ADC
:10B2F0008320C10603248106832441060329010615
:10B300008329C105032A8105832A4105032B0105F1
:10B31000832BC104032C8104832C4104032D0104DD
:10B32000832DC1031305000013010107678000008E
:10B3300097F0FFFFE780C01397F0FFFFE78000154D
:10B340006FF01FF593770703E38A07F4B7D7000080
:10B3500013073000238AE72037C50000B7D7000065
:10B360002395072037D70000930710001305C576F3
:10B370002309F72097F0FFFFE78080ECB7D70000A4
:10B3800093874717232EF100B7D700001307400615
:10B39000A389E720B7D7000037DA0000238707200A
:10B3A00013071000B7D70000A386E72093074A1FB2
:10B3B00023810700B7D70000B7D9000023AE071CD0
:10B3C000B7D7000023A4071CB7DA00009387091B36
:10B3D0002381070093878A1623A0070023A2070072
:10B3E00023A40700B7D7000037D70000239607141F
:10B3F0009307E001A305F7142398091A231A0A1EDC
:10B4000097F0FFFFE780804C930780F437D7000068
:10B410002311F722930740FE37D700002310F722AD
:10B420009307000D37D70000231DF72093078008EE
:10B4300037D70000231FF720930740FD37D70000C0
:10B44000230EF72093074A1F2322F1009387091B3D
:10B450002320F10293878A16130900002326F100A6
:10B460009309000F13050900130919001379F90F47
:10B4700097000000E780C0A9E31639FF97F0FFFFAF
:10B48000E780C0039307000A231CF4209307800180
:10B49000A38AF420B7D7000083D7A72137D70000AD
:10B4A00093870708231BF7209307A00537D70000D1
:10B4B000A308F72003274102B7D7000023880720FD
:10B4C000B7D7000023A0E714B7D700001307800701
:10B4D000238EE712B7D70000130710002384E71468
:10B4E00097F0FFFFE7808018B7D700009387C7A5C4
:10B4F000232CF10097F0FFFFE78080F797F0FFFF24
:10B50000E780C0FB97F0FFFFE78040F86302050883
:10B51000930520031305200097F0FFFFE780C0DFAD
:10B5200037C500001305C56D97F0FFFFE78040E6C3
:10B5300037D500001305459197F0FFFFE78040D015
:10B54000B7C700001385C77397F0FFFFE78040E49B
:10B5500097F0FFFFE780C0F1834780F8939787015A
:10B5600093D7874163DA07009307100037D70000AD
:10B57000A304F7146FF0DFB697F0FFFFE78000F148
:10B58000E30805FCB7D70000130710002384E71475
:10B59000834780F813F9F70F9397870193D7874173
:10B5A000E3C007B4B7D7000083C70721639807102B
:10B5B000B7D7000003956721937749006388070098
:10B5C000835784219387D7FF231CF4209377890026
:10B5D000638807008357842193873700231CF42056
:10B5E000031784219307F00063C8E7009307000165
:10B5F000231CF4206F00C00093070013E3CAE7FE8A
:10B6000093771900638C070083C754211307F00256
:10B610006366F70093871700A38AF42093772900C5
:10B62000638C070083C75421130720006376F7005B
:10B630009387F7FFA38AF420B7D7000083D9A72107
:10B64000B7D700001355554093890908939909010C
:10B6500093D90941239B372193D759406302F516AB
:10B66000B7D7000083C71721639C071403C6542172
:10B67000831584219306A0003305A04097F0FFFFB7
:10B68000E780C04C630E0512B7D700009387C792BE
:10B6900037D700002320F714B7D70000130780071F
:10B6A000238EE712B7D70000130710002384E71496
:10B6B0001305100097F0FFFFE7800024B7D70000C4
:10B6C0008397A721B7DB000037DC0000938707E8EA
:10B6D00093D75740B307F0409399070193D9094195
:10B6E000938B8B19130C0C18B7D7000003DBC7140E
:10B6F0009307B00263E0672383278102931A2B002C
:10B70000B38AFA0003D70A009317070193D70741BA
:10B7100063C2F920130A00001306C0008327C1008A
:10B72000B307FA0083C607006392061E9306100053
:10B730002380D7008327C10183C62A00B38747012E
:10B740002380D70083C73A0093161A003386DB00A4
:10B7500093975700938707012310F600930700FE85
:10B760003307F702B306DC00130707012390E60056
:10B7700097F0FFFFE78040CCB7D700009387C7154D
:10B780001375F50303C72A00B38747011305850224
:10B790002380A700930740006304F70013071000FD
:10B7A000B7D7000093870715B38747012380E700C9
:10B7B000130B1B00B7D70000239667156FF0DFF25D
:10B7C00037D700008347E72093067001938717005F
:10B7D00093F7F70F63EEF60A2307F72083C6542189
:10B7E000B7D7000003C7671493076000B3D7F6020A
:10B7F0006308F700B7D70000130710002384E7148D
:10B80000B7D7000083C717218315842113070000D1
:10B8100093D7270093C7170093F7170013860900E3
:10B820001305000097F0FFFFE78000CCB7D70000BA
:10B83000039667211307D01593575640B307F0407E
:10B840009397070193D70701636AF70A03278101DA
:10B85000B307F70083C7070093F717006380070A51
:10B86000831584219307F0131397050113570701DC
:10B8700063E6E70893071000130710009306000023
:10B880001305B00197F0FFFFE78000C66FF01FE3DC
:10B890002307072037D70000834737216386070037
:10B8A0009387F7FFA309F72083473721639A07029D
:10B8B000B7D700009387079437D700002320F714E9
:10B8C000B7D7000013078007238EE712B7D7000011
:10B8D000130710002384E714130500006FF09FDDA9
:10B8E00037D7000003477714E30AF7EEB7D7000015
:10B8F000130710002384E7146FF05FEE9307B00185
:10B900002308F0F6A30A00F66FF05FDB130A1A00B3
:10B91000E316CAE0832AC100B7DB0000B7D90000F4
:10B9200037DA0000938BCB15130A0A18938989190B
:10B93000930C0000130BC00083C70A006388070242
:10B94000B7D700008397A721031C0A0093FDFC0FC3
:10B950001385FD00938707101375F50F63D08713C8
:10B9600023800A002308A0F6A30A00F6938C1C008B
:10B97000938A1A00938B1B0093892900130A2A00CB
:10B98000E39C6CFBB7D600009386861FB7D70000F8
:10B99000137909012328D10083C7471F37D7000037
:10B9A00037D60000B7D9000063160922A306072086
:10B9B000032B4100832B01019389C91F13094620E2
:10B9C000930A1000130DF000930C400083470B0006
:10B9D00063880736039C09009387F7FF2300FB0069
:10B9E000130A4CFF83C70B00931D0A01131A0A01A7
:10B9F000135A0A412324F1002390490113555C4056
:10BA000093575A4093DD0D016300F5048317090035
:10BA10000326810093062000938507003305A0408C
:10BA2000232AF10097F0FFFFE7804012630E050024
:10BA3000032681000325410123000B0093750CFEB2
:10BA400097F0FFFFE780C0B4832781006376FD2C69
:10BA500083470B0013F5FA0F6380072E83268100BE
:10BA600083150900930710001307900013060A00BE
:10BA700097F0FFFFE78040A76F00802C8327C1016C
:10BA800037D700001307479AB387FC0003CD0700A0
:10BA900083950900930710003307A70103470700A8
:10BAA0009306400013060C002324B10097F0FFFF1B
:10BAB000E78080A3B7D7000083C70721E39807EA90
:10BAC00003C754219307B00083258100E3E8E73CD6
:10BAD000835784219306F000B387F540139707013D
:10BAE000135707411357F740B347F700B387E740B1
:10BAF0009397070193D70741E3C2F63AB7D70000FF
:10BB000083D76721B307FC401397070113570741F9
:10BB10001357F740B347F700B387E7409397070100
:10BB200093D70741E3CCF63613850D0097F0FFFF5E
:10BB3000E780C0E91305000097F0FFFFE780C0DB56
:10BB40006FF0DFE2938717006398A704B7D7000070
:10BB500083C7272113073000130C8002636AF700A4
:10BB60001307C0FEB387E7029387470613FCF70F5E
:10BB700097F0FFFFE780408C1375F503330CAC00A2
:10BB800023808B016FF09FDE37D700009307000002
:10BB90001307871C13054000B386E70003C60600A1
:10BBA000E31206FA37D70000939717001307471DD3
:10BBB0003307F7002310B70037D700001307C71C5F
:10BBC000B307F70023908701930760042380F600F2
:10BBD0006FF0DFF7B7D500008346D72083C5052176
:10BBE000B3E6B600E39606DC93061000A306D72062
:10BBF000638007028327410083C717006386070617
:10BC00008327410083C72700E39407DA93072000C6
:10BC100083558421B7D600001397170093864620DA
:10BC2000B386E6002390B600B7D600009386C61F01
:10BC30003387E600B7D6000083D66621938646FF99
:10BC40002310D7000327010183C654213387E7005F
:10BC50002300D70003274100B387E7001307C00183
:10BC60002380E7006FF0DFD4930710006FF05FFAD6
:10BC70008327C100B307FC0083C707006380070860
:10BC80000355090083570700B387A74013950701A1
:10BC9000135505411355F540B347F500B387A74049
:10BCA0009397070193D70741634AF60483D70600A9
:10BCB000B387B74113950701135505411355F54057
:10BCC000B347F500B387A7409397070193D7074180
:10BCD0006346F60223000B001375FC0F2326D102E6
:10BCE000232AE10097F0FFFFE78040CE8326C102C0
:10BCF000032741019305C0001306F000130C1C003C
:10BD00001307270093862600E304BCD483470B0067
:10BD1000E39007F66FF0DFD3B7D7000093860718DC
:10BD2000B7D7000013878719130C00001306F00023
:10BD30009305C0006FF09FFD2308A0F6A30A00F64C
:10BD4000938A1A00130B1B001309290093892900F9
:10BD5000938B1B00E39C9AC737DC0000B7DB000025
:10BD600037DA0000130C8C1C938B4B1D130ACA1C72
:10BD70001309800037DB0000930AC00003470C0062
:10BD80006300070803560A001307F7FF83970B00A9
:10BD900013066600131D0601831684211377F70F1F
:10BDA000135D0D41931906012300EC002310AA0135
:10BDB00093D9090163D4D718938717002390FB0008
:10BDC000630A07029307ADFF13555D4093D75740B1
:10BDD000839C0B00638AA716930620001306A0001D
:10BDE00093850C003305A04097F0FFFFE78000D655
:10BDF000630C051423000C00230820F7A30A00F6A7
:10BE0000130919001379F90F130C1C00938B2B00E5
:10BE1000130A2A00E31459F7B7D7000083C7072194
:10BE2000B7D90000138DC91D6398071CB7D7000050
:10BE30000397A7219307400013575740B387E74064
:10BE40009397070193D707011307D0156366F71A75
:10BE500003278101B307F7008387070063DE071814
:10BE600037D700008347B7149387F7FF93F7F70F8F
:10BE7000A305F71463920718B7D7000083C72721DB
:10BE800013073000130A9001636AF700130760FF7D
:10BE9000B387E7029387770313FAF70F97E0FFFF63
:10BEA000E78080591375F501330AAA00B7D700005F
:10BEB000A385471593064000130A0000B307AA01A3
:10BEC00003C7070063160712130710002380E7005B
:10BED00097E0FFFFE7804056930600113375D502C7
:10BEE00037D7000093171A001307C71E3307F70050
:10BEF000130585012310A70037D700001307471E3D
:10BF00003307F700B7D7000083D7A721938707F23D
:10BF10002310F70097E0FFFFE7800052130780022D
:10BF20003375E502B7D700009387071EB307FA0001
:10BF3000130565002380A7006F00000CE3D2F6E82C
:10BF40009387F7FF6FF09FE7930710001307A00098
:10BF50009306A00013060D0093850C00130509003D
:10BF600097E0FFFFE780405883470B21E39A07E8FB
:10BF7000835784211307B000B385FC4093970501D4
:10BF800093D7074193D7F740B3C5B700B385F540C2
:10BF90009395050193D50541E344B7E6B7D7000073
:10BFA00083D76721B389F9409397090193D7074154
:10BFB00093D7F740B3C93701B389F9409399090181
:10BFC00093D90941E34E37E303C754219307500146
:10BFD000E3E8E7E223000C00230820F71305000044
:10BFE000A30A00F697F0FFFFE78000916FF05FE192
:10BFF000130A1A00E314DAECB7DC000037DC0000A7
:10C00000B7DA0000938CCC1E130C4C1E938A0A1EC8
:10C01000930B400037DA00009309800083470D003E
:10C02000638E070403560C008347AA1413066600A8
:10C030001318060113160601135606412310CC00EF
:10C0400093F7370013580801639C070083C70A0061
:10C0500003C7542163F4E70E938717002380FA0087
:10C06000B7D700008397A7219387071063DEC70C1B
:10C0700023000D00230870F7A30A00F6938B1B0022
:10C0800093FBFB0F130D1D00938C2C00130C2C0045
:10C09000938A1A00E3943BF9032B0102B7DA0000FC
:10C0A00037DA0000B7D90000938A0A1C130A8A1BEA
:10C0B0009389491B130CF00083470B00638E070024
:10C0C0009387F7FF93F7F70F2300FB006398072888
:10C0D000230820F7A30A00F6130919001379F90FB2
:10C0E000130B1B00938A2A00130A2A009389190054
:10C0F000E31489FD37DA000083471A2163860700BD
:10C100009387F7FFA308FA2037D900008347C913A4
:10C11000B7D90000638C07269387F7FF93F7F70FD3
:10C12000230EF91263940726930710002384F91451
:10C1300097E0FFFFE78080536F00C025E372F7F2BE
:10C140009387F7FF6FF09FF1839D0C0003CB0A00EC
:10C15000930710001307200093060B0093850D0032
:10C1600013850B002324010197E0FFFFE780C03710
:10C17000B7D7000083C70721639E070A835784212E
:10C180001307F000B38DFD4093970D0193D707413E
:10C1900093D7F740B3CDB701B38DFD40939D0D010B
:10C1A00093DD0D416348B709B7D7000083D76721F6
:10C1B000032881003308F8409317080193D70741FB
:10C1C00093D7F74033C807013308F8401318080124
:10C1D0001358084163400707B7D7000083C75721AA
:10C1E000330BFB4093170B0193D7074193D7F740CD
:10C1F00033CB6701330BFB40131B0B01135B0B01AC
:10C200009307900063E8670323000D00230870F78D
:10C21000A30A00F603950C0003C60A0083150C0060
:10C2200097E0FFFFE780C0361305000097E0FFFFAF
:10C23000E780806CB7D700009386C71FB7D7000090
:10C2400013864720930800001308F0001305300000
:10C250008327410013871800B385F80083C70500C2
:10C260006384070E03D30C0083570600B38767402F
:10C2700013930701135303411353F340B347F300E0
:10C28000B38767409397070193D70741634EF80A36
:10C2900003530C0083D70600B3876740139307014D
:10C2A000135303411353F340B347F300B38767407D
:10C2B0009397070193D70741130330016346F308AF
:10C2C00083270101B388F80083C7080083C80A00E8
:10C2D000B38717419398070193D8084193D8F84042
:10C2E000B3C7F800B38717419397070193D70701A6
:10C2F0009308B00063EAF804238005001377F70F72
:10C300002308E0F6A30A00F623000D00230870F7C7
:10C31000A30A00F603C60A0083150C0003950C005F
:10C3200097E0FFFFE780C02637D700008357A7209C
:10C330009387870C2315F720B7D700001307100049
:10C340002384E7146FF09FD3930807001306260099
:10C3500093862600E31EA7EE6FF05FD293F74700A7
:10C3600083950A0003160A0083C6090013870B0091
:10C37000638407001307700093071000130509007A
:10C3800097E0FFFFE78040166FF01FD583C7891441
:10C39000E39007DA37D70000834707216388070E49
:10C3A000B7D6000083C7C6209387F7FF93F7F70F30
:10C3B0002386F620639C070CB7D6000083C746216E
:10C3C0009387F7FF93F7F70F238AF6206394070804
:10C3D00097E0FFFFE780802937D500001305C5945B
:10C3E00097E0FFFFE780C0FA37D40000930544953B
:10C3F0001305D00097E0FFFFE78040FEB7D50000AF
:10C40000938545971305E00097E0FFFFE78000FD67
:10C41000930544951305F00097E0FFFFE78000FCCB
:10C4200037C500001305C57397E0FFFFE78040F6AE
:10C43000130480071304F4FF97E0FFFFE780400335
:10C440001374F40F97E0FFFFE7804004E31404FE49
:10C450006FE01FC993078007A308FA20B7D7000031
:10C4600083C6372123080720130770026366D700AD
:10C4700013078002A389E720B7D7000013078001C4
:10C48000A38AE720930710002384F914B7D700008C
:10C4900083976721130730EA93D7574063CAE71899
:10C4A00037D700008357272237D600009305F013B3
:10C4B000938627002311D72237D70000035907227C
:10C4C000B7D600001309F9FF13190901135909011F
:10C4D0002310272303D7A6211307C7FF239DE62098
:10C4E0008356E62113872600131707011357070108
:10C4F00063F8E500938626EC1397060113570701AE
:10C50000B7D60000231FE62003C7C621130AF00E8A
:10C51000630607001307F7FF137AF70F0357E621A7
:10C520009387071413191900939707011319090129
:10C5300093D7070113590901B38927013309F9403A
:10C540009307E71393970701139B0901238E462155
:10C55000939909011319090193D707019306F01361
:10C56000135B0B0193D909411359094163F8F60094
:10C570001307E7FF9317070193D70701130B0B1E50
:10C58000131B0B0193FBF70F93DA8700135B0B016F
:10C59000130C000F930509001385090013190901F5
:10C5A0009399090197E0FFFFE780006393D90901A0
:10C5B00013590901930C05009305F9FF1385190020
:10C5C000939505011315050193D5054113550541B3
:10C5D00097E0FFFFE7804060230870F3A30850F363
:10C5E000230940F393892900A30990F31309290033
:10C5F000130A1A0093970901131909019399090164
:10C60000A309A0F2137AFA0F93D7070193D909412E
:10C610001359094163148A01130A0000E39C67F768
:10C620001305000097F0FFFFE780808E6FE09FEC1E
:10C6300037D700008357A7209387471F2315F7207C
:10C6400037D7000083472721938717002309F72056
:10C65000B7D700009387479937D700002320F714F6
:10C6600093078007230EF91297E0FFFFE780000091
:10C670001309A0051309F9FF97E0FFFFE78040DFEA
:10C680001379F90F97E0FFFFE78040E0E31409FE1C
:10C690006FE09FCF834710F06FE09FC493071000B7
:10C6A0006316FDACB7D70000839767216350FCACDD
:10C6B000938717F0634CFCAA83C70B00638607CCF3
:10C6C0009387F7FF2380FB006FF04FAA303B39318F
:10C6D00000000000303B333700000000303B3933AE
:10C6E0000000000042415354494F4E2052414944FA
:10C6F0002020200053434F5245200000303B393763
:10C70000000000002020204C4556454C2000000031
:10C710002020205348495053200000001B5B4B0051
:10C720004655454C20000000202020414C5420005C
:10C73000303B39360000000020202000300000008F
:10C740004655454C0000000052454143544F52208D
:10C7500044455354524F5945442100001B5B3F331D
:10C760006C1B5B3F32356C001B5B306D1B5B324AD0
:10C7700000000000313B393300000000422041201E
:10C780005320542049204F204E2020205220412069
:10C7900049204400466C79206F7665722074686584
:10C7A00020666F72747265737320616E64206465B5
:10C7B0007374726F79206974732072656163746F2A
:10C7C0007220636F72652E005550202F20444F5702
:10C7D0004E20636C696D6220616E642064697665C9
:10C7E0002020202020204C454654202F205249470D
:10C7F00048542073746565722020202020205350F7
:10C80000414345206669726500000000436C696D14
:10C8100062206F766572206C6F772077616C6C7325
:10C820002E20466C79206C6F77207468726F756764
:10C83000682074686520646F6F727320696E20745D
:10C84000616C6C206F6E65732E000000596F7572FD
:10C8500020736861646F7720616E6420746865205E
:10C86000616C74696D657465722073686F77207987
:10C870006F7572206865696768742E0053686F6F02
:10C8800074206675656C2074616E6B7320666F72C0
:10C89000206675656C2E2054757272657473206104
:10C8A0006E642066696768746572732073686F6F61
:10C8B00074206261636B2E00502070617573657324
:10C8C0002E20204553432067697665732075702EAE
:10C8D00000000000313B3936000000005052455343
:10C8E0005320535041434520544F20424547494E21
:10C8F000000000001B5B306D1B5B324A1B5B481B5A
:10C900005B3F3235680000004C4556454C2053547F
:10C9100041525400504155534544202D205020543D
:10C920004F20474F204F4E1B5B4B0000594F552067
:10C93000484954205448452057414C4C00000000C1
:10C940004F5554204F46204655454C00303B34341B
:10C950003B393700202020202020202020202020AC
:10C9600020202020202020202020202020202020C7
:10C9700020200000202020202020202020204741AF
:10C980004D45204F56455220202020202020202099
:10C99000202000004C4556454C20434C4541524513
:10C9A0004400000000030405060000001E0001070B
:10C9B0002A0001043000020731000105340001059E
:10C9C0004000030743000203480003084B00010630
:10C9D0004B0002014F0002025F00010560000301ED
:10C9E0006500020565000208720002047900010674
:10C9F0007E0001047E00010781000105CB000107D4
:10CA0000E1000304E8000301EB000204F300010667
:10CA1000F700020104010101070101010F010108F2
:10CA20001001010216010302190101071B01010394
:10CA30001B01020724010103250102052601020151
:10CA4000280101062F010105300101083A01010406
:10CA500040010208440102024B01040500000000ED
:10CA600000000000000000000000010101010101C0
:10CA700001010101030101010101010101050101A0
:10CA80000101010101010103010101010101010194
:10CA90000101012D0101010101010101010101015A
:10CAA0002D01010101010101010101010103010148
:10CAB0000101010101012D0101010101010101013A
:10CAC0000101250101010101010101010101010132
:10CAD0000101010101010101010101010101010146
:10CAE0000101808080808080808080808080808044
:10CAF0008080808080808080808080808080808036
:10CB00008080808080808080808080808080808025
:10CB10008080808080808080808080808080010113
:10CB200001010101010101013D01010101010101B9
:10CB300001010103010101010101010101012D01B7
:10CB400001010101010101010101011501010101C1
:10CB500001010101010101010301010101010101C3
:10CB6000010101010145010101010101010145012D
:10CB700001010101010101010101010301010101A3
:10CB80000101010101011501010101010101010181
:10CB90000101010101010101010101010101010185
:10CBA000010101010101010101010000000000007B
:10CBB0000000000000000000000000000000000075
:10CBC0000000000000000000000000000000000065
:10CBD0000000000000000000000000000000000055
:10CBE0000000000000000000000FFF00CCCCCC0FC4
:10CBF000F666F000CCCCCFFFF6660000000FFFFF1A
:10CC0000FFF00000CCCFFFFFFCC00000CCCCCF0C6D
:10CC1000CCCC000000CCC000CCCC00000000C00098
:10CC200000C0000000000000000000000000000044
:10CC300000000000000000000000000000000000F4
:10CC400000000000000000000000000000000000E4
:10CC500000000000000000000000000000000000D4
:10CC600000000000000000000004440044444404AC
:10CC700044444000444444444444000000044444C8
:10CC800044400000444444444440000044444404BC
:10CC900044440000004440004444000000004000C0
:10CCA0000040000000000000000000000000000044
:10CCB0000000000000000000000000000000000074
:10CCC0000000000000000000000000000000000064
:10CCD0000000000000000100000100000000111130
:10CCE000000111000000111110D111110000011DEF
:10CCF000DDDDD11100000DDDDDDDD0000000BBBDAC
:10CD0000DDD11111000DBBBDD011111100DDD0001E
:10CD10000000000000000000000000000000000013
:10CD20000000000000000000000000000000000003
:10CD300000000000000000000000000000000000F3
:10CD400000000000000000000000000000000000E3
:10CD500000000000000000000000000000000000D3
:10CD6000000000000000000000000000000002229F
:10CD700022200000000022AA2222000000022AAA8B
:10CD8000A2222000000222AA222220000082227277
:10CD9000222228000888277722228880008777228D
:10CDA000222888000077788888888000000008881A
:10CDB000888000000000000000000000000000006B
:10CDC0000000000000000000000000000000BBBBED
:10CDD000BBBB0000000BBBBBBBBBB000000BBBBBB5
:10CDE000BBBBB0000009BBBBBBBB900000099F915F
:10CDF0001119900000099F911999900000099F91C5
:10CE00001199900000099F911999900000099F9134
:10CE100019999000000999999999900000011111B0
:10CE2000111110000000111111110000000000008C
:10CE300000000000000000000000000000000000F2
:10CE400000000000000000000000000000EE0000F4
:10CE50000000000000EEEEE000000000000E666E34
:10CE6000EEE00000000EEE66666EE00000000EEEE2
:10CE7000E66E00000000000770EE000000000007F2
:10CE800070000000000000077000000000000007B4
:10CE900070000000000000077000000000000008A3
:10CEA00080000000000088888888000000008888D2
:10CEB00088880000000000088000000000000000DA
:10CEC00000000000000000055000000000000055B8
:10CED000550000000000055DD5500000000055DD44
:10CEE000DD55000000055DFFFFD550000055DDFF5A
:10CEF000FFDD55000555DDFFFFDD555000555DFF99
:10CF0000FFD55500000555DDDD5550000000555D8D
:10CF1000D555000000000555555000000000005593
:10CF20005500000000000005500000000000000057
:10CF300000000000000000000000000000000000F1
:10CF400000000000000000000000000000000000E1
:10CF5000000000000000009900000000000099B0EF
:10CF60000009900000090B00BB0000000000BBFF9F
:10CF70000FB00000009B0FFFF0BB00000000B00FDF
:10CF8000F00BB0000090B0FFF00090000000BB0B71
:10CF9000B0000000000000B0000000000000090028
:10CFA0000000000000000000000000000000000081
:10CFB0000000000000000000000000000000000071
:10CFC0000000000000000000000000000000000061
:10CFD0000000000000000101010000000000000945
:10CFE000010000000009900009000000000000009E
:10CFF00000091000000990BBBB99910000090BBB10
:10D00000BB909100000000B0B090010000000999B1
:10D01000000900000001010009900000000000115B
:10D0200001100000000000000000000000000000EF
:10D0300000000000000000000000000000000000F0
:10D0400000000000000000000000000000000000E0
:10D0500000000000000000000000000000000000D0
:10D0600000000000000000000000000000000000C0
:10D07000000000000000000FF00000000000000FA2
:10D08000B0000000000000000000000000000000F0
:10D090000000000000000000000000000000000090
:10D0A0000000000000000000000000000000000080
:10D0B0000000000000000000000000000000000070
:10D0C0000000000000000000000000000000000060
:10D0D0000000000000000000000000000000000050
:10D0E0000000000000000000000000000000000937
:10D0F00090000000000000999900000000000099D5
:10D10000F90000000000000990000000000000008D
:10D11000000000000000000000000000000000000F
:10D1200000000000000000000000000000000000FF
:0CD13000000000000000000000000000F3
:040000030000A20057
:00000001FF
+128
View File
@@ -0,0 +1,128 @@
#!/usr/bin/env python3
"""Run Bastion Raid in tools/rv32emu.py on a model of the board (tools/boardemu.py)
and take screenshots.
python3 test-raid.py [--shots DIR] [--play]
It checks the title screen, measures the Z-80 instructions each frame takes
(a 60 Hz frame has about 550,000 at 90 MHz), flies into the fortress, and with
--play lets an autopilot fly the whole level: it steers through every doorway
at the right height, climbs over low walls and shoots ahead. The autopilot
can't be hit by shells or fighters (it keeps the ship invulnerable), so this
checks the level and the rules, not its skill.
"""
import argparse
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "..", "tools"))
sys.path.insert(0, HERE)
from boardemu import Board, load_ihx, load_font, load_syms # noqa: E402
UP, DOWN, LEFT, RIGHT, FIRE, ENTER, ESC = 1, 2, 4, 8, 16, 32, 128
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--shots", help="write PNG screenshots here")
ap.add_argument("--play", action="store_true", help="fly the level with the autopilot")
a = ap.parse_args()
mem, hi = load_ihx(os.path.join(HERE, "raid.ihx"))
b = Board(mem, code_end=hi)
font = load_font() if a.shots else None
if a.shots:
os.makedirs(a.shots, exist_ok=True)
sym = {"_" + n: a for n, a in load_syms(os.path.join(HERE, "out", "raid.sym")).items()}
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def i16(n):
v = b.m[sym[n]] | b.m[sym[n] + 1] << 8
return v - 65536 if v & 0x8000 else v
def hud():
return "\n".join(b.screen_text().display[:2])
b.run_frames(5)
check("title screen", "B A S T I O N R A I D" in "\n".join(b.screen_text().display))
if a.shots: b.shot(os.path.join(a.shots, "1-title.png"), font)
b.run_frames(3, FIRE)
b.run_frames(5)
check("game starts", "LEVEL 1" in hud() and "SHIPS 3" in hud())
c0 = b.steps
b.run_frames(60)
per = (b.steps - c0) / 60
print("%.0f instructions a frame (%.0f%% of a 60 Hz frame at 90 MHz)" % (per, per / 5500))
check("a frame fits in a 60th of a second", per < 500000)
if a.shots: b.shot(os.path.join(a.shots, "2-approach.png"), font)
alt0 = b.m[sym["_alt"]]
b.run_frames(10, UP)
check("climbing raises the ship", b.m[sym["_alt"]] == alt0 + 10)
b.run_frames(60)
if a.shots: b.shot(os.path.join(a.shots, "3-fortress.png"), font)
print(hud())
if a.play:
sys.path.insert(0, HERE)
import gen
rows, objs = gen.level()
frame = 0
best_row = 0
worst = 0
while frame < 6000 and b.pc != 0:
b.m[sym["_shield"]] = 1
b.m[sym["_fuel"]] = 100
if b.m[sym["_dead"]]:
r = -(i16("_ship_d") >> 5)
print("autopilot crashed at row %d: ship s %d, height %d; rows here %s" %
(r, i16("_ship_s"), b.m[sym["_alt"]], rows[r - 2:r + 3]))
break
ship_s, ship_d, alt = i16("_ship_s"), i16("_ship_d"), b.m[sym["_alt"]]
row = -(ship_d >> 5)
best_row = max(best_row, row)
# the next wall within 6 rows decides where to be
want_s, want_alt = 16 * gen.BW, 24
for r in range(row, min(row + 6, len(rows))):
ri = rows[r]
if ri & 4:
g = (ri >> 3) & 15
want_s, want_alt = 32 * g + 32, 10
break
if ri & 2:
want_alt = 30
break
j = 0
if ship_s < want_s - 2: j |= RIGHT
elif ship_s > want_s + 2: j |= LEFT
if alt < want_alt: j |= UP
elif alt > want_alt: j |= DOWN
if frame % 6 == 0: j |= FIRE
c0 = b.steps
b.run_frames(1, j)
worst = max(worst, b.steps - c0)
frame += 1
if frame == 700 and a.shots: b.shot(os.path.join(a.shots, "4-doorway.png"), font)
if "LEVEL 2" in hud():
break
check("autopilot flew the level (got to row %d of %d)" % (best_row, len(rows)),
"LEVEL 2" in hud())
print("slowest frame: %d instructions (%.0f%% of a 60 Hz frame)" % (worst, worst / 5500))
check("every frame fits in a 60th of a second", worst < 550000)
print(hud())
b.run_frames(120) # level 2 starts
b.run_frames(5, ESC) # give up: back to the title
b.run_frames(5)
check("Esc gives up the game", "PRESS SPACE TO BEGIN" in "\n".join(b.screen_text().display))
b.run_frames(5, ESC) # Esc at the title: back to BASIC
b.run_frames(5)
check("Esc returns to BASIC", b.pc == 0)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()
+135
View File
@@ -0,0 +1,135 @@
#!/usr/bin/env python3
"""Make data.h for swarm.c: the sprite shapes (also drawn into the graphics
for the aliens waiting in formation) and a sine table for the dives.
Shapes are 16 rows of 16 hex digits, one colour per pixel (0 is see-through).
"""
import math
import random
import sys
Z = "0" * 16
DRONE_A = [Z, Z,
"000C00000000C000",
"0000C000000C0000",
"00000CCCCCC00000",
"0000CC4CC4CC0000",
"000CCCCCCCCCC000",
"0CCCCCCCCCCCCCC0",
"0C0CCCCCCCCCC0C0",
"0C0C00000000C0C0",
"0000CC0000CC0000",
] + [Z] * 5
DRONE_B = [Z, Z,
"0000C000000C0000",
"0000C000000C0000",
"00000CCCCCC00000",
"0000CC4CC4CC0000",
"000CCCCCCCCCC000",
"00CCCCCCCCCCCC00",
"0CCCCCCCCCCCCCC0",
"C00C00000000C00C",
"000CC000000CC000",
] + [Z] * 5
FLAG_A = [Z,
"0000000BB0000000",
"000000BBBB000000",
"00B00BB99BB00B00",
"00BB0BBBBBB0BB00",
"00BBBBBBBBBBBB00",
"000BB9BBBB9BB000",
"0000BBBBBBBB0000",
"00000B9BB9B00000",
"0000B000000B0000",
"000B00000000B000",
] + [Z] * 5
FLAG_B = [Z,
"0000000BB0000000",
"000000BBBB000000",
"00000BB99BB00000",
"B0BB0BBBBBB0BB0B",
"0BBBBBBBBBBBBBB0",
"000BB9BBBB9BB000",
"0000BBBBBBBB0000",
"00000B9BB9B00000",
"00000B0000B00000",
"0000B000000B0000",
] + [Z] * 5
def recolour(img, table):
return [row.translate(str.maketrans(table)) for row in img]
WARRIOR_A, WARRIOR_B = recolour(DRONE_A, {"C": "D", "4": "B"}), recolour(DRONE_B, {"C": "D", "4": "B"})
ESCORT_A, ESCORT_B = recolour(DRONE_A, {"C": "9", "4": "F"}), recolour(DRONE_B, {"C": "9", "4": "F"})
SHIP = [Z,
"0000000FF0000000",
"0000000FF0000000",
"000000FFFF000000",
"000000F99F000000",
"00F00FFFFFF00F00",
"00F0FFFFFFFF0F00",
"00FFFFFFFFFFFF00",
"00FFF99FF99FFF00",
"00FF00FFFF00FF00",
] + [Z] * 6
SHOT = [Z] * 5 + ["0000000BB0000000"] * 6 + [Z] * 5
BULLET = [Z] * 6 + ["00000009F0000000", "0000000F90000000", "000000099000000" + "0"] + [Z] * 7
def burst(seed, cols, size):
r = random.Random(seed)
img = [["0"] * 16 for _ in range(16)]
for _ in range(80):
a = r.uniform(0, 2 * math.pi)
d = r.uniform(0, size)
x, y = int(8 + d * math.cos(a)), int(8 + d * math.sin(a))
if 0 <= x < 16 and 0 <= y < 16:
img[y][x] = cols[0] if d < size / 3 else cols[1] if d < 2 * size / 3 else cols[2]
return ["".join(row) for row in img]
BOOM1 = burst(21, "FB9", 5)
BOOM2 = burst(23, "B91", 7.5)
SHIPBOOM = burst(25, "FFB", 7.9)
def stars(seed):
r = random.Random(seed)
img = [["0"] * 16 for _ in range(16)]
for _ in range(2):
img[r.randrange(16)][r.randrange(16)] = r.choice("FE7BD6")
return ["".join(row) for row in img]
SHAPES = [("S_DRONE", DRONE_A), ("S_DRONE2", DRONE_B), ("S_WARRIOR", WARRIOR_A), ("S_WARRIOR2", WARRIOR_B),
("S_ESCORT", ESCORT_A), ("S_ESCORT2", ESCORT_B), ("S_FLAG", FLAG_A), ("S_FLAG2", FLAG_B),
("S_SHIP", SHIP), ("S_SHOT", SHOT), ("S_BULLET", BULLET),
("S_BOOM1", BOOM1), ("S_BOOM2", BOOM2), ("S_SHIPBOOM", SHIPBOOM),
("S_STARS1", stars(1)), ("S_STARS2", stars(2)), ("S_STARS3", stars(3))]
def main(out):
L = ["/* made by gen.py - edit that instead */", ""]
for i, (name, img) in enumerate(SHAPES):
assert len(img) == 16 and all(len(row) == 16 for row in img), name
L.append("#define %s %d" % (name, i))
L.append("#define N_SHAPES %d" % len(SHAPES))
L.append("static const u8 shape_data[N_SHAPES * 128] = {")
for name, img in SHAPES:
b = [int(row[x], 16) << 4 | int(row[x + 1], 16) for row in img for x in range(0, 16, 2)]
L.append(" " + ",".join(str(v) for v in b) + ", /* %s */" % name)
L.append("};")
# 64 steps round the circle, 0 = right, 16 = down; scaled by 64
L.append("static const i8 sin64[64] = {")
L.append(" " + ",".join(str(int(round(64 * math.sin(2 * math.pi * k / 64)))) for k in range(64)))
L.append("};")
open(out, "w").write("\n".join(L) + "\n")
print("data.h: %d shapes" % len(SHAPES))
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv) > 1 else "data.h")
+4
View File
@@ -0,0 +1,4 @@
10 REM STAR SWARM: LOAD THE GAME (MEMORY FILE SWARMGAM) AND PLAY IT
20 REM THE GAME RUNS AT A200H, PAST THIS PROGRAM, AND COMES BACK HERE
30 BLOAD "SWARMGAM": CALL 41472
40 CLS
+655
View File
@@ -0,0 +1,655 @@
/* Star Swarm: a swooping alien shooter for the Basys 3 RISC-V computer.
An alien swarm sways across the top of the screen in formation: drones,
warriors, escorts and two flagships. They peel off in looping dives,
firing as they come, and loop back into place if they miss. A flagship
brings its escorts; shoot the escorts first for a bonus. Your ship has one
shot in the air at a time.
There are 46 aliens and only 32 sprites, so the formation is drawn into the
graphics layer and sways by the hardware X scroll (ports 35h-36h). An alien
that dives is wiped from the graphics and becomes a sprite; when it gets
back to its place it is drawn in again. The stars are sprites too, a couple
of dots each, falling down the screen. It runs at A200h, loaded by the
SWARM program in BASIC. */
typedef unsigned char u8;
typedef signed char i8;
typedef unsigned short u16;
typedef short i16;
#include "basys3.h"
#define con_stat IO8(0x00)
#define con_data IO8(0x01)
#define gx_lo IO8(0x30)
#define gx_hi IO8(0x31)
#define gy_p IO8(0x32)
#define gpix IO8(0x33)
#define gfill IO8(0x34)
#define scr_xlo IO8(0x35)
#define scr_xhi IO8(0x36)
#define scr_y IO8(0x37)
#define sp_sel IO8(0x70)
#define sp_xlo IO8(0x71)
#define sp_xhi IO8(0x72)
#define sp_y IO8(0x73)
#define sp_shape IO8(0x74)
#define sp_ctl IO8(0x75)
#define sp_load IO8(0x76)
#define sp_data IO8(0x77)
#define frame_ctr IO8(0x78)
#define joy IO8(0x88)
#include "data.h" /* out/data.h, made by gen.py */
#define J_LEFT 4
#define J_RIGHT 8
#define J_FIRE 16
#define J_ENTER 32
#define J_ESC 128
#define COLS 10
#define ROWS 6
#define FX0 60 /* buffer x of column 0 */
#define CW 20
#define FY0 36 /* top row */
#define RH 16
#define SHIP_Y 214
/* sprites: lower numbers in front */
#define SP_SHIP 0
#define SP_SHOT 1
#define SP_DIVER 2 /* 8 */
#define SP_BUL 10 /* 6 */
#define SP_BOOM 16 /* 4 */
#define SP_STAR 20 /* 10 */
#define ND 8
#define NB 6
#define NX 4
#define NST 10
#define C_EMPTY 0
#define C_HOME 1
#define C_AWAY 2
#define P_LOOP 0
#define P_ATTACK 1
#define P_RETURN 2
/* which alien is where: 0 none, else 1 drone, 2 warrior, 3 escort, 4 flagship */
static const u8 layout[ROWS][COLS] = {
{ 0, 0, 0, 4, 0, 0, 4, 0, 0, 0 },
{ 0, 0, 3, 3, 3, 3, 3, 3, 0, 0 },
{ 0, 2, 2, 2, 2, 2, 2, 2, 2, 0 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
};
static const u8 kind_shape[5] = { 0, S_DRONE, S_WARRIOR, S_ESCORT, S_FLAG };
static const u8 home_score[5] = { 0, 30, 40, 50, 60 };
static const u8 dive_score[5] = { 0, 60, 80, 100, 150 };
/* ---------------------------------------------------------------- state */
/* not static: test-swarm.py reads these from the link map */
u8 cell[ROWS][COLS];
i16 ship_x;
u8 lives, wave, dead, shield;
u16 score;
u8 dv_on[ND];
i16 dv_x[ND], dv_y[ND]; /* 1/16 pixels, screen */
static u8 dv_row[ND], dv_col[ND], dv_ang[ND], dv_spd[ND], dv_phase[ND], dv_cool[ND], dv_shots[ND];
static i8 dv_turn[ND];
static u8 dv_lead[ND]; /* escorts: the flagship's diver, else 255 */
static u8 dv_escorts[ND], dv_esc_hit[ND];
static i16 shot_x, shot_y;
static u8 shot_on;
static i16 bx[NB], by[NB];
static i8 bvx[NB];
static u8 b_on[NB];
static i16 xx[NX], xy[NX];
static u8 xt[NX], xship[NX];
static u8 st_x[NST], st_y[NST], st_v[NST];
static i16 sway;
static i8 sway_dir;
static u8 flap, flap_timer, dive_timer, attacks, dead_time, fire_held;
static u16 next_extra;
static u8 last_frame, quit, hud_dirty;
static u16 rnd_state;
static const char *msg;
static u8 msg_time;
/* ----------------------------------------------------------------- text */
static void putch(char c) { while (!(con_stat & 2)); con_data = c; }
static void puts_(const char *s) { while (*s) putch(*s++); }
static void putn(u16 n, u8 width)
{
char b[6];
u8 i = 0;
do { b[i++] = '0' + n % 10; n /= 10; } while (n);
while (width > i) { putch('0'); width--; }
while (i) putch(b[--i]);
}
static void at(u8 r, u8 c) { putch(27); putch('['); putn(r, 0); putch(';'); putn(c, 0); putch('H'); }
static void ink(const char *s) { putch(27); putch('['); puts_(s); putch('m'); }
static void centre(u8 r, const char *s)
{
u8 n = 0;
while (s[n]) n++;
at(r, 41 - n / 2);
puts_(s);
}
static u16 rnd(void)
{
rnd_state ^= rnd_state << 7;
rnd_state ^= rnd_state >> 9;
rnd_state ^= rnd_state << 8;
return rnd_state;
}
static void wait_frame(void)
{
while (frame_ctr == last_frame);
last_frame = frame_ctr;
}
static u8 drain_keys(void)
{
u8 c, p = 0;
while (con_stat & 1) {
c = con_data;
if (c == 'p' || c == 'P') p = 1;
}
return p;
}
static void say(const char *s) { msg = s; msg_time = 120; hud_dirty = 1; }
static i16 absd(i16 v) { return v < 0 ? -v : v; }
/* ------------------------------------------------------------ graphics */
/* Draw shape (or nothing) into the graphics with its top left at buffer (x, y). */
static void blit(u16 x, u8 y, u8 shape, u8 erase)
{
const u8 *p = shape_data + ((u16)shape << 7);
u8 r, i, b;
for (r = 0; r < 16; r++) {
gx_lo = (u8)x; gx_hi = (u8)(x >> 8); gy_p = y + r;
for (i = 0; i < 8; i++) {
b = erase ? 0 : *p++;
gpix = b >> 4; gpix = b & 15;
}
}
}
static u16 cell_bx(u8 c) { return FX0 + c * CW + 2; }
static u8 cell_by(u8 r) { return FY0 + r * RH; }
static void draw_cell(u8 r, u8 c)
{
blit(cell_bx(c), cell_by(r), kind_shape[layout[r][c]] + flap, cell[r][c] != C_HOME);
}
static void draw_formation(void)
{
u8 r, c;
for (r = 0; r < ROWS; r++)
for (c = 0; c < COLS; c++)
if (layout[r][c]) draw_cell(r, c);
}
static void set_scroll(void)
{
/* the picture moves right by sway: show buffer column -sway at the left */
i16 s = -sway;
if (s < 0) s += 320;
scr_xlo = (u8)s; scr_xhi = (u8)(s >> 8); scr_y = 0;
}
/* -------------------------------------------------------------- sprites */
static void spr_at(u8 n, i16 x, i16 y, u8 shape, u8 ctl)
{
sp_sel = n;
if (x < -15 || x > 319 || y < -15 || y > 239) { sp_ctl = 0; return; }
sp_xlo = (u8)x; sp_xhi = (u8)(x >> 8) & 1; sp_y = (u8)y;
sp_shape = shape; sp_ctl = ctl;
}
static void spr_off(u8 n) { sp_sel = n; sp_ctl = 0; }
static void all_off(void) { u8 i; for (i = 0; i < 32; i++) spr_off(i); }
static void load_shapes(void)
{
const u8 *p = shape_data;
u8 s, i;
for (s = 0; s < N_SHAPES; s++) {
sp_load = s;
for (i = 0; i < 128; i++) sp_data = *p++;
}
}
static void boom(i16 x, i16 y, u8 ship)
{
u8 i, best = 0;
for (i = 0; i < NX; i++) if (xt[i] < xt[best]) best = i;
xx[best] = x; xy[best] = y; xt[best] = ship ? 60 : 18; xship[best] = ship;
}
/* ------------------------------------------------------------------ HUD */
static void hud(void)
{
hud_dirty = 0;
at(1, 1); ink("0;93"); puts_("STAR SWARM ");
ink("0;37"); puts_("SCORE "); ink("0;97"); putn(score, 6);
ink("0;37"); puts_(" SHIPS "); ink("0;97"); putn(lives, 0);
ink("0;37"); puts_(" WAVE "); ink("0;97"); putn(wave, 0);
if (msg && msg_time) { ink("0;96"); puts_(" "); puts_(msg); }
puts_("\033[K");
ink("0");
}
static void add_score(u16 n)
{
score += n;
if (score >= next_extra) { next_extra += 7000; lives++; say("BONUS SHIP"); }
hud_dirty = 1;
}
/* --------------------------------------------------------------- stars */
static void stars(void)
{
u8 i;
for (i = 0; i < NST; i++) {
st_y[i] += st_v[i];
if (st_y[i] >= 240) { st_y[i] -= 240; st_x[i] = rnd() % 304; }
spr_at(SP_STAR + i, st_x[i], st_y[i], S_STARS1 + i % 3, 1);
}
}
/* -------------------------------------------------------------- divers */
static void dive(u8 r, u8 c, u8 lead, i8 turn)
{
u8 d;
for (d = 0; d < ND && dv_on[d]; d++);
if (d == ND) return;
dv_on[d] = 1; dv_row[d] = r; dv_col[d] = c;
dv_x[d] = (i16)(cell_bx(c) + sway) << 4;
dv_y[d] = (i16)cell_by(r) << 4;
dv_ang[d] = 48; /* straight up */
dv_turn[d] = turn;
dv_spd[d] = 22;
dv_phase[d] = P_LOOP;
dv_cool[d] = 20 + (rnd() & 31);
dv_shots[d] = wave < 3 ? 2 : 3;
dv_lead[d] = lead;
dv_escorts[d] = 0; dv_esc_hit[d] = 0;
if (lead != 255) dv_escorts[lead]++;
cell[r][c] = C_AWAY;
draw_cell(r, c);
}
static u8 divers_out(void)
{
u8 d, n = 0;
for (d = 0; d < ND; d++) if (dv_on[d]) n++;
return n;
}
static void launch(void)
{
u8 r, c, k, side, lead;
i8 turn;
u8 most = 2 + wave; if (most > ND) most = ND;
if (divers_out() >= most) return;
attacks++;
/* every fourth attack, a flagship with the escorts beside it */
if ((attacks & 3) == 0) {
c = (rnd() & 1) ? 3 : 6;
if (cell[0][c] != C_HOME) c = 9 - c;
if (cell[0][c] == C_HOME) {
turn = c < 5 ? 1 : -1;
dive(0, c, 255, turn);
for (lead = 0; lead < ND; lead++)
if (dv_on[lead] && dv_row[lead] == 0 && dv_col[lead] == c) break;
if (lead < ND)
for (r = c - 1; r <= c + 1 && dv_escorts[lead] < 2; r++)
if (cell[1][r] == C_HOME) dive(1, r, lead, turn);
return;
}
}
/* otherwise one from the end of a row, from a random side */
side = rnd() & 1;
for (k = 0; k < COLS; k++) {
c = side ? COLS - 1 - k : k;
for (r = ROWS; r-- > 1;) {
if (cell[r][c] == C_HOME && !(rnd() & 1)) {
dive(r, c, 255, c < 5 ? 1 : -1);
return;
}
}
}
}
static void bullet_from(i16 x, i16 y)
{
u8 b;
for (b = 0; b < NB && b_on[b]; b++);
if (b == NB) return;
b_on[b] = 1; bx[b] = x << 4; by[b] = y << 4;
bvx[b] = ship_x + 8 > x ? 6 : -6;
if (absd(ship_x + 8 - x) < 20) bvx[b] = 0;
}
static void kill_diver(u8 d)
{
u8 r = dv_row[d], c = dv_col[d], k = layout[r][c], lead = dv_lead[d];
u16 pts = dive_score[k];
if (k == 4) {
if (dv_escorts[d] && dv_esc_hit[d] == dv_escorts[d]) { pts = 800; say("FLAGSHIP AND ESCORTS: 800"); }
else if (dv_escorts[d]) pts = 300;
}
if (lead != 255 && dv_on[lead]) dv_esc_hit[lead]++;
add_score(pts);
boom((dv_x[d] >> 4), dv_y[d] >> 4, 0);
dv_on[d] = 0;
cell[r][c] = C_EMPTY;
spr_off(SP_DIVER + d);
}
static void kill_ship(void)
{
u8 d, b;
if (dead || shield) return;
dead = 1; dead_time = 130;
boom(ship_x, SHIP_Y, 1);
spr_off(SP_SHIP);
for (b = 0; b < NB; b++) { b_on[b] = 0; spr_off(SP_BUL + b); }
for (d = 0; d < ND; d++)
if (dv_on[d] && dv_phase[d] != P_RETURN) dv_phase[d] = P_ATTACK;
}
static void divers(void)
{
u8 d, r, c, s;
i16 x, y, tx;
for (d = 0; d < ND; d++) {
if (!dv_on[d]) continue;
r = dv_row[d]; c = dv_col[d];
switch (dv_phase[d]) {
case P_LOOP: /* up and over, out to the side */
dv_ang[d] = (dv_ang[d] - dv_turn[d]) & 63;
if (dv_ang[d] == 16) { dv_phase[d] = P_ATTACK; dv_spd[d] = 30 + 2 * (wave < 8 ? wave : 8); }
break;
case P_ATTACK: /* down at the ship, firing */
if (!(last_frame & 3) && !dead) {
tx = ship_x + 8 - (dv_x[d] >> 4);
if (tx > 6 && dv_ang[d] > 9) dv_ang[d]--;
else if (tx < -6 && dv_ang[d] < 23) dv_ang[d]++;
}
y = dv_y[d] >> 4;
if (dv_cool[d]) dv_cool[d]--;
else if (dv_shots[d] && y > 70 && y < 170 && !dead) {
bullet_from((dv_x[d] >> 4) + 8, y + 12);
dv_shots[d]--;
dv_cool[d] = 16 + (rnd() & 15);
}
if (y > 244) { /* gone past: come back in at the top */
dv_phase[d] = P_RETURN;
dv_y[d] = -16 << 4;
dv_spd[d] = 24;
}
break;
case P_RETURN: /* drop back into place */
dv_x[d] = (i16)(cell_bx(c) + sway) << 4;
dv_y[d] += dv_spd[d];
if (dv_y[d] >= (i16)cell_by(r) << 4) {
dv_on[d] = 0;
cell[r][c] = C_HOME;
draw_cell(r, c);
spr_off(SP_DIVER + d);
continue;
}
break;
}
if (dv_phase[d] != P_RETURN) {
dv_x[d] += (sin64[(dv_ang[d] + 16) & 63] * dv_spd[d]) >> 6;
dv_y[d] += (sin64[dv_ang[d]] * dv_spd[d]) >> 6;
}
x = dv_x[d] >> 4; y = dv_y[d] >> 4;
if (!dead && absd(x - ship_x) < 11 && absd(y - SHIP_Y) < 10) {
kill_diver(d); kill_ship(); continue;
}
if (shot_on && absd(shot_x - (x + 8)) < 8 && absd(shot_y - (y + 7)) < 8) {
shot_on = 0; spr_off(SP_SHOT);
kill_diver(d);
continue;
}
s = kind_shape[layout[r][c]] + ((last_frame >> 3) & 1);
spr_at(SP_DIVER + d, x, y, s, 1);
}
}
static void bullets(void)
{
u8 b;
i16 x, y;
for (b = 0; b < NB; b++) {
if (!b_on[b]) continue;
bx[b] += bvx[b];
by[b] += 48;
x = bx[b] >> 4; y = by[b] >> 4;
if (y > 240) { b_on[b] = 0; spr_off(SP_BUL + b); continue; }
if (!dead && absd(x - (ship_x + 8)) < 7 && y > SHIP_Y + 2 && y < SHIP_Y + 13) {
b_on[b] = 0; spr_off(SP_BUL + b);
kill_ship();
continue;
}
spr_at(SP_BUL + b, x - 8, y - 8, S_BULLET, 1);
}
}
/* The ship's shot against the formation: which cell is it in? */
static void shot_vs_formation(void)
{
i16 bxp = shot_x - sway - FX0, byp = shot_y - FY0;
u8 r, c, ix, iy;
if (bxp < 0 || byp < 0) return;
c = (u8)(bxp / CW); ix = (u8)(bxp % CW);
r = (u8)(byp / RH); iy = (u8)(byp % RH);
if (c >= COLS || r >= ROWS || ix < 3 || ix > 16 || iy < 2 || iy > 11) return;
if (cell[r][c] != C_HOME) return;
cell[r][c] = C_EMPTY;
draw_cell(r, c);
add_score(home_score[layout[r][c]]);
boom(cell_bx(c) + sway, cell_by(r), 0);
shot_on = 0; spr_off(SP_SHOT);
}
static void ship(u8 j)
{
if (dead) return;
if ((j & J_LEFT) && ship_x > 6) ship_x -= 2;
if ((j & J_RIGHT) && ship_x < 298) ship_x += 2;
if (j & J_FIRE) {
if (!shot_on && !fire_held) {
shot_on = 1; shot_x = ship_x + 8; shot_y = SHIP_Y;
}
fire_held = 1;
} else fire_held = 0;
spr_at(SP_SHIP, ship_x, SHIP_Y, S_SHIP, 1);
}
static void shot(void)
{
if (!shot_on) return;
shot_y -= 6;
if (shot_y < 16) { shot_on = 0; spr_off(SP_SHOT); return; }
shot_vs_formation();
if (shot_on) spr_at(SP_SHOT, shot_x - 8, shot_y - 8, S_SHOT, 1);
}
static void booms(void)
{
u8 i;
for (i = 0; i < NX; i++) {
if (!xt[i]) { spr_off(SP_BOOM + i); continue; }
xt[i]--;
spr_at(SP_BOOM + i, xx[i], xy[i],
xship[i] ? S_SHIPBOOM : (xt[i] & 4) ? S_BOOM1 : S_BOOM2, xt[i] ? 1 : 0);
}
}
static u8 aliens_left(void)
{
u8 r, c, n = 0;
for (r = 0; r < ROWS; r++) for (c = 0; c < COLS; c++) if (cell[r][c]) n++;
return n;
}
static void new_wave(void)
{
u8 r, c, i;
for (r = 0; r < ROWS; r++)
for (c = 0; c < COLS; c++) cell[r][c] = layout[r][c] ? C_HOME : C_EMPTY;
for (i = 0; i < ND; i++) { dv_on[i] = 0; spr_off(SP_DIVER + i); }
for (i = 0; i < NB; i++) { b_on[i] = 0; spr_off(SP_BUL + i); }
shot_on = 0; spr_off(SP_SHOT);
sway = 0; sway_dir = 1; flap = 0; flap_timer = 24;
dive_timer = 120; attacks = 0;
draw_formation();
}
/* ------------------------------------------------------------- screens */
static void clear_all(void)
{
gfill = 0;
while (gfill & 1);
all_off();
}
static u8 wait_fire(void)
{
u8 j;
while (joy & (J_FIRE | J_ENTER | J_ESC)) { wait_frame(); drain_keys(); stars(); }
for (;;) {
wait_frame(); drain_keys(); rnd(); stars();
j = joy;
if (j & J_ESC) return 0;
if (j & (J_FIRE | J_ENTER)) return 1;
}
}
static u8 title(void)
{
clear_all();
puts_("\033[0m\033[2J");
sway = 0;
set_scroll();
blit(112, 150, S_FLAG, 0); blit(136, 150, S_ESCORT, 0);
blit(160, 150, S_WARRIOR, 0); blit(184, 150, S_DRONE, 0);
spr_at(SP_SHIP, 152, 200, S_SHIP, 1);
ink("1;93"); centre(7, "S T A R S W A R M");
ink("0;97");
centre(10, "The swarm sways overhead, and peels off to dive at you.");
centre(11, "A flagship dives with its escorts: shoot them first for a bonus.");
centre(13, "LEFT / RIGHT move SPACE fire P pause ESC give up");
ink("0;37");
centre(16, " 60 / 150 50 / 100 40 / 80 30 / 60 ");
centre(17, " IN FORMATION / DIVING");
ink("1;96"); centre(28, "PRESS SPACE TO BEGIN");
ink("0");
return wait_fire();
}
static void play(void)
{
u8 j, i;
lives = 3; score = 0; wave = 1; next_extra = 7000;
ship_x = 152; dead = 0; fire_held = 1;
for (i = 0; i < NX; i++) xt[i] = 0;
clear_all();
puts_("\033[2J");
new_wave();
msg = 0; say("HERE THEY COME");
hud();
for (;;) {
wait_frame();
set_scroll();
if (drain_keys()) {
at(1, 60); ink("0;93"); puts_("PAUSED - P TO GO ON\033[K"); ink("0");
for (;;) {
wait_frame();
if (joy & J_ESC) { quit = 1; return; }
if (drain_keys()) break;
}
hud_dirty = 1;
}
j = joy;
if (j & J_ESC) return;
/* sway: a pixel every other frame, turning at the ends */
if (last_frame & 1) {
sway += sway_dir;
if (sway >= 44 || sway <= -44) sway_dir = -sway_dir;
}
if (!--flap_timer) {
flap_timer = 24; flap ^= 1;
draw_formation();
}
if (!dead && !--dive_timer) {
i = aliens_left();
dive_timer = (i < 8 ? 30 : 100 - (wave < 6 ? wave * 10 : 60)) + (rnd() & 31);
launch();
}
ship(j);
shot();
divers();
bullets();
booms();
stars();
if (msg_time && !--msg_time) hud_dirty = 1;
if (hud_dirty) hud();
if (dead && !--dead_time) {
if (!--lives) {
hud();
ink("0;44;97");
centre(14, " ");
centre(15, " GAME OVER ");
centre(16, " ");
ink("0");
for (i = 0; i < 150; i++) { wait_frame(); drain_keys(); stars(); }
return;
}
dead = 0; ship_x = 152; fire_held = 1;
hud_dirty = 1;
}
if (!aliens_left() && !dead) {
say("SWARM DESTROYED");
hud();
for (i = 0; i < 120; i++) { wait_frame(); drain_keys(); stars(); booms(); }
wave++;
new_wave();
hud_dirty = 1;
}
}
}
int main(void)
{
u8 i;
rnd_state = 0x1979 ^ frame_ctr;
last_frame = frame_ctr;
puts_("\033[?3l\033[?25l");
load_shapes();
for (i = 0; i < NST; i++) { st_x[i] = rnd() % 304; st_y[i] = rnd() % 240; st_v[i] = 1 + (i & 1); }
quit = 0;
while (!quit) {
if (!title()) break;
play();
}
clear_all();
puts_("\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+720
View File
@@ -0,0 +1,720 @@
:10A2000097320000938242CD17330000130383E19D
:10A2100063F8620023A00200938242006FF05FFFA8
:10A2200017130000670083EA834700F093F72700C5
:10A23000E38C07FEA300A0F067800000130101FF7C
:10A240002324810023261100130405000345040084
:10A25000631A05008320C10003248100130101015A
:10A26000678000001304140097000000E78000FCE2
:10A270006FF0DFFD130101FE232C8100232A9100E2
:10A28000232E110093840500130400009306A00000
:10A29000130690003377D5029307040093870701D4
:10A2A000B3872700130414001374F40F1307070374
:10A2B000238CE7FE930705003355D502E36CF6FCCB
:10A2C000636E9400631804028320C101032481019A
:10A2D000832441011301010267800000130500037C
:10A2E0009384F4FF97000000E78040F493F4F40FA8
:10A2F0006FF01FFD1304F4FF1374F40F93070401B0
:10A30000B387270003C587FF97000000E78000F2AE
:10A310006FF05FFB130101FF2322910093040500FE
:10A320001305B001232611002324810013840500A6
:10A3300097000000E78080EF1305B005970000004C
:10A34000E780C0EE9305000013850400970000002D
:10A35000E78080F21305B00397000000E78000ED6E
:10A36000130504009305000097000000E780C0F08B
:10A37000032481008320C10083244100130580044D
:10A380001301010117030000670043EA130101FFF5
:10A3900023248100130405001305B00123261100B6
:10A3A00097000000E78080E81305B00597000000E3
:10A3B000E780C0E71305040097000000E78040E84D
:10A3C000032481008320C1001305D006130101017D
:10A3D00017030000670083E5130101FF23248100B8
:10A3E0002326110013840500930700003307F400AF
:10A3F00003470700631A070293D7170093059002DB
:10A40000B385F54093F5F50F97000000E780C0F0A5
:10A4100013050400032481008320C10013010101FE
:10A42000170300006700C3E19387170093F7F70F46
:10A430006FF0DFFBB7D7000003D5C7ED131775002A
:10A440003345E50013150501135505011357950014
:10A450003345E500131785003345E5001315050165
:10A4600013550501239EA7EC67800000B7D70000B5
:10A4700003C707EE834680F7E38EE6FE034780F7C7
:10A480002380E7EE678000001305000093060005B7
:10A49000834700F093F7170063940700678000007C
:10A4A000034710F01377F70D6304D70093070500F7
:10A4B000138507006FF0DFFDB7C70000938747657E
:10A4C000131676003306F600135385009307000039
:10A4D0001375F50F930800012308A0F23387F500E8
:10A4E0001377F70FA30860F22309E0F21307800047
:10A4F00013080000639606000348060013061600C2
:10A50000135E4800A309C0F31378F8001307F7FFA0
:10A51000A30900F31377F70FE31C07FC93871700D9
:10A5200093F7F70FE39A17FB678000001306A0006C
:10A530003306C502B7D70000938747FE37C7000030
:10A5400013154500B387C700B387B70083C607005C
:10A55000B7C700009387875DB387C700B387B7008D
:10A5600003C607009307075D13074001B387C700C1
:10A5700003C60700B7D7000083C797EE9386F6FFA0
:10A58000B336D0003306F600930745023385E50263
:10A590001376F60F93F5C70F1305E50313150501A1
:10A5A0001355050117030000670043F1130101FE75
:10A5B0002328210137C90000232C810023263101E3
:10A5C00023244101232E1100232A91001309895DC0
:10A5D000130400009309A000130A60009304000014
:10A5E000B307990083C70700638A070093F5F40F48
:10A5F0001305040097000000E78080F393841400A3
:10A60000E39034FF130414001374F40F1309A9002A
:10A61000E31644FD8320C101032481018324410109
:10A62000032901018329C100032A810013010102CA
:10A6300067800000B7D7000003D7C7EEB307E0403C
:10A640009397070193D7074163DA07009307001434
:10A65000B387E7409397070193D7074113F7F70FA5
:10A6600093D78740A30AE0F293F7F70F230BF0F29A
:10A67000A30B00F267800000B7D70000930600002C
:10A6800013070000938787F113034000B388F60097
:10A690003308F70083C808000348080063F4080182
:10A6A00013F7F60F93861600E39266FEB7D6000006
:10A6B00013181700938646F2B38606012390A6006E
:10A6C000B7D600009386C6F1B38606012390B60084
:10A6D00093062001630406009306C003B387E700D6
:10A6E0002380D700B7D70000938747F1B387E700EF
:10A6F0002380C70067800000130101FFB7D7000067
:10A70000930510001305100023261100238F07EC7A
:10A71000232481002322910097000000E780C0BF1E
:10A7200037C500001305453497000000E78040C698
:10A7300037C500001305C53497000000E78040B01E
:10A74000B7C400001385C43597000000E78040C4FB
:10A7500037C500001305453697000000E78040AE7E
:10A7600037C400001305C43697000000E78040C2DC
:10A77000B7D7000003D5C7FD930560009700000020
:10A78000E78080AF1385C43597000000E78040C0A4
:10A7900037C500001305453797000000E78040AA41
:10A7A0001305C43697000000E78080BEB7D70000CD
:10A7B00003C517FE9305000097000000E780C0ABBB
:10A7C0001385C43597000000E78080BC37C50000C2
:10A7D0001305053897000000E78080A61305C436EE
:10A7E00097000000E780C0BAB7D7000003C507FE96
:10A7F0009305000097000000E78000A8B7D700008D
:10A8000003A487ED630E0402B7D7000083C747EDAA
:10A810006388070237C500001305C538970000009C
:10A82000E78000B737C500001305453997000000E1
:10A83000E78000A11305040097000000E78040A016
:10A8400037C500001305853997000000E780409F59
:10A85000032481008320C1008324410037C5000008
:10A860001305053C1301010117030000670043B203
:10A87000B7D7000003D7C7FDB7D600003305E50002
:10A880001315050113550501239EA7FC83D726EE5A
:10A890006360F50437270000130787B5B387E70027
:10A8A00037D700002391F6EE834717FE93871700F2
:10A8B000A300F7FEB7C7000037D700009387C7395A
:10A8C000232CF7ECB7D7000013078007238AE7ECA7
:10A8D000B7D7000013071000238FE7EC6780000054
:10A8E000130101FEB7D70000232C8100232A910019
:10A8F0002328210193040500232E110013090600CB
:10A9000013040000938747FD130580003307F4000C
:10A9100003460700631C0618930710002300F70086
:10A9200013064001B7D700003386C5029387C7FAE4
:10A93000B387870023809700B7D70000938747FA33
:10A94000B38787002380B700B7D7000083D7C7EE4F
:10A9500037D70000131514009387E703B387C700A8
:10A96000130747FC3307A700939747002310F7000E
:10A970009397440037D7000093874702130747FB9C
:10A9800093F7F70F3307A700939747002310F700BB
:10A99000B7D700009387C7F9B38787001307000371
:10A9A0002380E700B7D70000938747F7B387870076
:10A9B0002380D700B7D70000938747F913076001BA
:10A9C000B38787002380E700B7D700009387C7F8D5
:10A9D000B38787002326B10023800700970000007B
:10A9E000E78080A5B7D70000938747F81375F50176
:10A9F000B3878700130545012380A700B7D7000060
:10AA000003C707FE930720008325C10033B7E70083
:10AA1000B7D700009387C7F713072700B3878700CE
:10AA20002380E700B7D700009387C7F6B387870076
:10AA300023802701B7D70000938747F63387870025
:10AA40002300070037D700001307C7F53307870037
:10AA5000230007001307F00F630AE900B3872701FB
:10AA600003C70700130717002380E7001307A000A0
:10AA70003387E402B7D70000938747FE13850400AD
:10AA8000B387E700B387B700130720002380E700F0
:10AA9000032481018320C101832441010329010191
:10AAA0001301010217030000670083A813041400B8
:10AAB000E31EA4E48320C101032481018324410116
:10AAC000032901011301010267800000B7D70000CC
:10AAD000938747FE9386C703130500001306A00063
:10AAE00013070000B385E70083C5050063860500F2
:10AAF000130515001375F50F13071700E314C7FEB0
:10AB00009387A700E39ED7FC6780000037D600003C
:10AB1000B7C5000093060000130646FE9385855DC9
:10AB20001308C003138706009307A0003305B7007E
:10AB300003450500B308C7009387F7FF3335A0002E
:10AB40002380A80093F7F70F13071700E39007FE81
:10AB50009386A600E39806FD37D70000130747FD4C
:10AB6000930720009306A000230007002308F0F6B7
:10AB700093871700A30A00F693F7F70F1307170040
:10AB8000E394D7FE37D700001307C7F293060001FE
:10AB9000230007002308F0F693871700A30A00F6A6
:10ABA00093F7F70F13071700E394D7FEB7D700000A
:10ABB000238A07F4930710002308F0F6A30A00F68F
:10ABC00037D70000231607EE37D700002305F7EE2E
:10ABD000B7D7000013078001A38407EEB7D70000A2
:10ABE0002384E7EEB7D7000013078007A383E7EEBF
:10ABF000B7D70000238307EE170300006700439BCD
:10AC0000230A00F2834740F393F71700E39C07FE03
:10AC1000130700022308F0F693871700A30A00F633
:10AC200093F7F70FE398E7FE678000009387F5003E
:10AC3000939707012308A0F693D707011305E014A3
:10AC4000636CF5009307F6009397070193D707010C
:10AC50001305E00F6376F500A30A00F66780000095
:10AC600093F7F50F93D58540A308F0F693F51500FB
:10AC70002309B0F61376F60FA309C0F6230AD0F61F
:10AC8000A30AE0F667800000130101FD232481027E
:10AC900023229102232E310137D40000B7D40000C3
:10ACA000B7D9000023202103232C4101232A51017D
:10ACB0002328610123267101232611022324810107
:10ACC0001304C4EF938484F013090000938909EFFF
:10ACD000130BF00E930B0013930A3000130AA0001D
:10ACE000B307390183C7070003470400137CF90F3A
:10ACF000B387E70093F7F70F6366FB062300F400C2
:10AD0000B3765C030346040083C5040013054C01BD
:10AD1000130710001375F50F13091900130414001D
:10AD2000938414009386E60097000000E78040F0CB
:10AD3000E31849FB8320C1020324810283244102DA
:10AD4000032901028329C101032A8101832A4101C8
:10AD5000032B0101832BC100032C8100130101038C
:10AD600067800000938707012300F40097F0FFFF3E
:10AD7000E780806C337575032380A4006FF05FF863
:10AD8000130101FE232A91002328210123263101EA
:10AD900023244101B7D4000037DA0000B7D90000FE
:10ADA00037D90000232C810023225101232E1100CA
:10ADB000938484F1130A4AF29389C9F1130949F182
:10ADC00013040001930A400103C70400631607043B
:10ADD000230880F6A30A00F6130414001374F40F7A
:10ADE00093841400130A2A00938929001309190077
:10ADF000E31C54FD8320C10103248101832441010C
:10AE0000032901018329C100032A8100832A41000B
:10AE100013010102678000001307F7FF8347090051
:10AE20001377F70F2380E40083150A0003960900C7
:10AE30009306D000639807009376470093B61600F8
:10AE40009386B6003337E000130504009700000036
:10AE5000E78000DE6FF05FF837D70000B7D600005C
:10AE60008347F7FD83C6E6FDB3E7D700639E070A75
:10AE700093071000130101FFA30FF7FC130720F83D
:10AE8000B7D70000A382E7EEB7D70000039527FEEF
:10AE9000130610009305600D2326110097F0FFFFA5
:10AEA000E780C07D37D70000230800F6A30A00F62C
:10AEB0001307C7F29307A0009306000123000700C1
:10AEC0002308F0F693871700A30A00F693F7F70F0D
:10AED00013071700E394D7FEB7D70000B7D60000DA
:10AEE0009387C7F813070000938646FD930520005B
:10AEF00013051000130680003308D700034808002C
:10AF00006308080003C807006304B8002380A70093
:10AF10001307170093871700E310C7FE8320C100B3
:10AF2000130101016780000067800000B7D70000AF
:10AF3000130101FE9387C7FA232C8100232E1100F1
:10AF4000232A91002328210123263101B387A7005A
:10AF500083C907001307A000B7D700003387E902B1
:10AF6000938747FAB387A70003C90700B7C7000054
:10AF70009387875D13040500B387E700B387270134
:10AF800083C60700B7D700009387C7F6B387A7002B
:10AF900003C70700B7C700009387075CB387D700D4
:10AFA00003C5070093074000639EF604B7D700006F
:10AFB000938747F6B387870083C607006384060438
:10AFC000B7D700009387C7F5B387870083C707000B
:10AFD0001305C0126398D702B7C70000B7D60000A8
:10AFE0009387873A23ACF6ECB7D700009306800727
:10AFF000238AD7ECB7D7000093061000238FD7EC35
:10B0000013050032B7D400009307F00F938444FD7A
:10B010006304F702B387E40083C70700638E070069
:10B02000B7D700009387C7F5B387E70003C70700CA
:10B03000130717002380E70097000000E780808354
:10B04000B7D7000013171400938747FBB387E700B7
:10B0500083950700B7D70000938747FCB387E700C5
:10B06000039507001306000093D54540135545404E
:10B0700097F0FFFFE78080601307A000B389E90223
:10B08000B7D70000938747FEB384840013042400DD
:10B09000238004001374F40FB3873701B3872701AB
:10B0A00023800700230880F6A30A00F68320C1014D
:10B0B0000324810183244101032901018329C10063
:10B0C0001301010267800000130101F92326110614
:10B0D000232481062322910623202107232E3105D4
:10B0E000232C4105232A51052328610523267105B8
:10B0F00023248105232291052320A105232EB103BA
:10B10000834780F73727000013079797B3C7E700F7
:10B1100037D70000231EF7EC834780F737C50000C0
:10B1200037D700001305453C2300F7EE97F0FFFFEB
:10B13000E7800011B7C70000938747651307000039
:10B1400093061001230BE0F61386070883C507005A
:10B1500093871700A30BB0F6E39AC7FE13071700F7
:10B160001377F70FE310D7FEB7D9000037D90000E7
:10B17000B7D4000013040000938989F0130A001368
:10B180001309C9EF130B000F938404EF930AA00077
:10B1900097F0FFFFE780402A33754503B30734017A
:10B1A0002380A70097F0FFFFE78000293375650330
:10B1B000B3072401330794002380A700937714007A
:10B1C000938717002300F70013041400E31254FDC3
:10B1D000B7D70000A38F07ECB7C7000093870754C9
:10B1E0002322F102B7D700009387C7FA232AF10080
:10B1F000B7D7000083C7F7ED6398071C97000000DE
:10B20000E78040A037C500001305053D37DD00008D
:10B2100097F0FFFFE780C00223160DEE97F0FFFFC7
:10B22000E7808041930600001306600093056009E3
:10B230001305000797F0FFFFE78040289306000002
:10B2400013064000930560091305800897F0FFFF7F
:10B25000E780C026930600001306200093056009CE
:10B260001305000A97F0FFFFE780402593060000D2
:10B2700013060000930560091305800B97F0FFFF8C
:10B28000E780C023930780F9230800F6A308F0F6AF
:10B29000230900F6930780FCA309F0F693078000CA
:10B2A000230AF0F637C5000093071000A30AF0F652
:10B2B0001305C53D97F0FFFFE780800DB7C500007F
:10B2C0009385453E1305700097F0FFFFE78000115E
:10B2D00037C500001305C53697F0FFFFE780400B28
:10B2E000B7C500009385853F1305A00097F0FFFFC9
:10B2F000E780C00EB7C50000938505431305B00075
:10B3000097F0FFFFE780800DB7C5000093854547A4
:10B310001305D00097F0FFFFE780400C37C5000011
:10B320001305C53597F0FFFFE7808006B7C500001D
:10B330009385454B1305000197F0FFFFE780000A56
:10B34000B7C500009385054F1305100197F0FFFF67
:10B35000E780C00837C500001305C55097F0FFFF10
:10B36000E7800003B7C500001305C0019385455170
:10B3700097F0FFFFE7808006B7C700001385073C02
:10B3800097F0FFFFE780C000834780F893F7070B33
:10B39000639E070897F0FFFFE780800D97F0FFFF9F
:10B3A000E780C00E97F0FFFFE780000997000000DC
:10B3B000E780C08D834780F813F7F70F93978701D5
:10B3C00093D7874163D2070897000000E780808306
:10B3D00037C500001305C55297F0FFFFE78040E630
:10B3E000834700F093F71700E39807728320C106A4
:10B3F0000324810683244106032901068329C1050C
:10B40000032A8105832A4105032B0105832BC104EF
:10B41000032C8104832C4104032D0104832DC103DB
:10B4200013050000130101076780000097F0FFFF7C
:10B43000E780000497F0FFFFE780400597000000D9
:10B44000E780C0846FF05FF493770703E38407F429
:10B450009307300037D70000A300F7FEB7D70000EE
:10B46000239E07FCB727000037D70000938787B5D6
:10B470002311F7EE37D400009307800993041000DE
:10B480002311F4FEB7D70000238297EEB7DC00004B
:10B49000B7D70000B7D9000023AC07F023809CFE8B
:10B4A000A38F09FC97F0FFFFE780C07537C5000048
:10B4B0001305453D97F0FFFFE78080D897F0FFFF29
:10B4C000E780006503274102B7D7000023ACE7EC13
:10B4D000B7D7000013078007238AE7ECB7D700002F
:10B4E000238F97EC97F0FFFFE7804021B7D700004C
:10B4F000938747FD2328F100B7D70000938747FAC9
:10B500002320F10297F0FFFFE78080F697F0FFFF1E
:10B51000E780801297F0FFFFE78040F7630205089D
:10B520009305C0031305100097F0FFFFE780C0DE0E
:10B5300037C500001305453497F0FFFFE78040E56D
:10B5400037C500001305055597F0FFFFE78040CF92
:10B55000B7C700001385073C97F0FFFFE78040E383
:10B5600097F0FFFFE780C0F0834780F8939787014B
:10B5700093D7874163DA07009307100037D700009D
:10B58000A30FF7EC6FF0DFC697F0FFFFE78000F046
:10B59000E30805FCB7D7000013071000238FE7EC82
:10B5A000834780F813F9F70F9397870193D7874163
:10B5B000E3C007C4B7D7000083C707EE93F71700AF
:10B5C000638E0702B7D600008357CDEE0387A6EE41
:10B5D00013066005B387E7009397070193D7070128
:10B5E0002316FDEE9387B7029397070193D70701C0
:10B5F0006376F6003307E0402385E6EE37D7000098
:10B60000834787EE9387F7FF93F7F70F638C070A5B
:10B610002304F7EE83C7F9FD63900724B7DA00002F
:10B6200083C47AEE9384F4FF93F4F40FA3839AEE29
:10B63000638C040A937749006390074293778900EB
:10B64000638C0700831724FE130790126346F700EC
:10B65000938727002311F4FE1379090137D70000DF
:10B6600063000904B7D60000834747EE03C646F5DA
:10B67000B3E7C70063940702831724FE1306100084
:10B68000238AC6F493878700B7D60000239CF6F47C
:10B69000B7D700009306600D239BD7F41309100061
:10B6A000831524FE230227EF930680001307100062
:10B6B0001306600D1305000097F0FFFFE780405769
:10B6C0006F008019930780012304F7EE37D700003D
:10B6D000834797EE93C71700A304F7EE97F0FFFF99
:10B6E000E78000ED6FF01FF397F0FFFFE780403E2B
:10B6F00093077000130AE00163F2A70283C70CFEF0
:10B7000013075000130A8002636AF700130760FFF3
:10B71000B387E7029387470613FAF70F97F0FFFF07
:10B72000E78080D183C70CFE1375F501330AAA00A8
:10B730009387270013F7F70FA3834AEF93F7F70FC9
:10B740009306800063F4F60013078000930700005F
:10B75000930600001306800083250101B385B6001F
:10B7600083C50500638605009387170093F7F70FDD
:10B7700093861600E392C6FE63FCE70C37D7000001
:10B78000834767EE37DA0000130A4AFE93871700F3
:10B7900093F7F70F2303F7EE93F73700639207222F
:10B7A00097F0FFFFE78040C913751500130B600089
:10B7B00063040500130B3000B3076A0103C70700D9
:10B7C000930710006308F70093079000B387674161
:10B7D00013FBF70FB3076A0103C7070093071000B5
:10B7E0006310F71E930740009304F0FF63E46701C2
:10B7F00093041000938604001306F00F93050B00CA
:10B800001305000097F0FFFFE780C00D37D7000059
:10B8100093070000130747FA9306800003260101EF
:10B820003306F600034606006300060203264101C4
:10B830003386C70003460600631806003386E70018
:10B8400003460600E30E662D93871700E398D7FCA6
:10B8500083C7F9FDE38007DE37D90000834749F548
:10B86000638A070237DA000083546AF51307F00091
:10B870009387A4FF9397070193D70741231BFAF4FB
:10B880006348F71E230A09F4930710002308F0F613
:10B89000A30A00F68327010137DA0000B7D40000BD
:10B8A0002324F100B7D700009387C7F72326F100C0
:10B8B000B7C700009387075D232CF100B7C70000CE
:10B8C00037DB000037D80000B7DA000037DC0000B3
:10B8D0009387875D130A4AFC938444FB130BCBF8D0
:10B8E000930DC8F9938A4AF9130C4CF8930B000096
:10B8F000232EF1008327810083C70700638407603C
:10B90000832741011306100083960400B387FB00D0
:10B9100003C707008327010213F9FB0FB387FB005E
:10B9200083C5070083470B006380C72C13052000E5
:10B930006388A75463960746B7D60000938646F7F8
:10B94000B386DB0083C70D0083C60600B387D740EC
:10B9500093F7F7032380FD00930600016392D74419
:10B9600083C60CFE2300CB001306800093870600DD
:10B970006374D600930780009387F70093971700AE
:10B980002380FA006F00C041B3077A0183C7A70084
:10B99000639EB7019386040013860A0093F5FB0F9C
:10B9A0001305100097F0FFFFE780C0F3938B1B0097
:10B9B000E3407BEB83470C00E3F8FDFC6FF05FE9AD
:10B9C00097F0FFFFE78040A7937A15009307900058
:10B9D000930D1000130CA0009385040063860A00E9
:10B9E000B385974093F5F50F330BBA00930B5000D6
:10B9F00003472B036312B7052324B10097F0FFFF21
:10BA0000E78080A3137515008325810093079000BC
:10BA100063140502930740009306F0FF63E4B70048
:10BA2000930610001306F00F13850B0097F0FFFF2D
:10BA3000E78040EB6FF0DFE1938BFBFF93FBFB0FA5
:10BA4000130B6BFFE3960BFA9384140093F4F40F3B
:10BA5000E39484F96FF0DFDF831724FE130760009F
:10BA6000E35EF7BC9387E7FF2311F4FE6FF01FBD81
:10BA7000B7DA000083D78AF5035BCDEE938747FCE6
:10BA8000B38767419397070193D7074163DE0702A6
:10BA900003566AF583D58AF513071000130686FF4F
:10BAA000938585FF13160601939505019306900073
:10BAB0001356064193D505411305100097F0FFFF7B
:10BAC000E78000176FF01FDD938464FD93960401F7
:10BAD0009394040193D4044193D60601E3CA04FA73
:10BAE00013064001B3CBC7021307900093FDFB0F71
:10BAF000E360B7FB93D4444093F4F40F93055000F4
:10BB0000E3E895F8B3E7C7021306D0009387D7FFA1
:10BB10009397070193D70701E36CF6F693F7F600C6
:10BB20009387E7FF9397070193D70701E362F7F63F
:10BB30001307A0003387E402B7D70000938747FEBE
:10BB400093061000B387E700B387B70103CC070063
:10BB5000E310DCF493850D00138504002324E10039
:10BB60002380070097F0FFFFE780809C0327810078
:10BB7000B7C700009387875DB387E700B387B70136
:10BB800083C70700B7C600009386865CB386F600BD
:10BB900003C50600130BEB0397F0FFFFE78080CD92
:10BBA0009307400113F5FB0F3305F502939544000D
:10BBB000938545021306000093F5C50F3305650113
:10BBC000131505011355054197F0FFFFE78000AB02
:10BBD000230A09F4230880F7A30A00F6834749F5EE
:10BBE000E38A07CA6FF0DFEAB7D7000083C707EE22
:10BBF00003C6F9FD83180A0093F73700B367F60010
:10BC0000639C0702835724FE13D54840938787001F
:10BC1000B387A7409397070193D7074113056000A7
:10BC20006356F50483C70D00130590006376F50095
:10BC30009387F7FF2380FD0083470C0093D646408F
:10BC4000638407049387F7FF2300FC009307400FEA
:10BC500063D8D714930720002300FB00930700F05C
:10BC60002390F400930780016FF09FD11305A0FF8C
:10BC7000E3D4A7FC83C70D0013056001E36EF5FA5A
:10BC8000938717006FF01FFB8327C10003C50700D0
:10BC9000E30E05FA9307600463D4D7109307900A64
:10BCA000E3C6D7FA631E060E93D84840939808015E
:10BCB00093D80801138388009386C60013130301E9
:10BCC00093960601B7D700001353034193D606415C
:10BCD0009387C7F2130F6000330EF600834E0E00F9
:10BCE00063940E08930710002300FE00B7D70000EE
:10BCF000131E1600938787F4B387C701931E430072
:10BD00002390D701B7D700009387C7F3B387C70144
:10BD1000939646002390D700831724FE130E6000ED
:10BD20009386770063D46600130EA0FFB387174194
:10BD30009396070193D6064193D6F640B3C7F60013
:10BD4000B387D74093970701B7D60000938646F391
:10BD500093D7074193083001B386C60063DEF8101D
:10BD60002380C6016F00C00013061600E316E6F735
:10BD70008327C1001305F5FF2326B1022380A70006
:10BD80002324E10297E0FFFFE780006B1375F500C5
:10BD9000032781028325C102130505012300AC009E
:10BDA00083460B0093072000638AF60483C80D00C6
:10BDB000B7C60000938646619387080193F7F7039F
:10BDC000B387F60003C50A008387070003530A0000
:10BDD000B3861601B387A7020396040093D7674082
:10BDE000B38767002310FA0083870600B387A70292
:10BDF00093D76740B387C7002390F40083180A00E5
:10BE00000396040083C7F9FD93D848401356464073
:10BE100063980720835724FEB387F8409396070161
:10BE200093D6064193D6F640B3C7F600B387D74002
:10BE30009397070193D707419306A00063C2F61EAC
:10BE40009307A6F293D6F741B3C7F600B387D7405E
:10BE50009397070193D707019306900063E2F61CBE
:10BE60001305090097F0FFFFE780800C97F0FFFFB4
:10BE7000E780C0FE6F000009238006006FF05FEFCF
:10BE8000130540013385A5028357CDEE9387E70361
:10BE9000B387A700939747002310FA0083C70A00CF
:10BEA000B386F600931747009396060193874702DF
:10BEB00093D6064193F7F70F2390D400939747004A
:10BEC000E3C0F6EE9306A000B306D70283278100F5
:10BED000130929001305070023800700B7D70000C6
:10BEE000938747FE1379F90FB387D700B387B7005D
:10BEF0002380C70097E0FFFFE7808063230820F7D7
:10BF0000A30A00F683278100938B1B00130A2A00E3
:10BF1000938717002324F1008327C1009384240012
:10BF2000130B1B00938717002326F1009307800053
:10BF3000938D1D00938A1A00130C1C00E39CFB9A3E
:10BF4000B7DB0000B7DD000037D8000037DC0000A9
:10BF5000938BCBF2938D8DF4130948F3130CCCF330
:10BF60009304A000130A80FF930A6000130B900053
:10BF700083C70B00638407048307090083D50D0082
:10BF80001307000FB385F50083570C009395050147
:10BF900093D50541938707039397070193D70741EB
:10BFA0002310FC002390BD0093D74740635EF71435
:10BFB00023800B00230890F6A30A00F613851400D3
:10BFC0009374F50F93070001938B1B00938D2D0045
:10BFD00013091900130C2C00E39CF4F897F0FFFFF1
:10BFE000E78040DA37D9000097F0FFFFE78000CA0A
:10BFF000834749EDB7D400006388071A9387F7FF9A
:10C0000093F7F70F230AF9EC6390071A93071000D0
:10C01000238FF4EC97E0FFFFE780406E6F0040193C
:10C02000B7D6000083C746F563840708B7D700007A
:10C0300083D787F513037000938787FFB387174172
:10C0400013950701135505411355F540B347F50006
:10C05000B387A7409397070193D70741634AF30437
:10C06000B7D7000083D767F5938797FFB387C7409B
:10C0700013950701135505411355F540B347F500D6
:10C08000B387A7409397070193D707416342F30211
:10C09000238A06F4930710002308F0F6130509001D
:10C0A000A30A00F697F0FFFFE78080E86FF09FE5B6
:10C0B000B7D7000083C607EE9307A0003307F70247
:10C0C0008327C10193D6360093F616001305290085
:10C0D0001375F50F3387E7003307B7008347070071
:10C0E0000327810193850800B307F70083C7070082
:10C0F00013071000B386F60093F6F60F97F0FFFFD4
:10C10000E78000B36FF01FE01396070183C7F9FDC6
:10C1100093D54540939505011356060193D5050126
:10C12000639C0704835724FEB307FA40B387F500E6
:10C1300013970701135707411357F740B347F70009
:10C14000B387E7409397070193D7074163C6FA0285
:10C15000930776F29397070193D70701636EFB006D
:10C1600023800B00230890F6A30A00F697F0FFFF48
:10C17000E780C0CE6FF09FE4130686FF938585FFAE
:10C180001316060193950501130710009306A000EE
:10C190001356064193D505411385040097F0FFFF20
:10C1A000E78000A96FF09FE183C7E4EDE39407E621
:10C1B00083C7F9FD638E070C37D70000834757EE1E
:10C1C0009387F7FF93F7F70FA302F7EE6392070C3D
:10C1D00037D70000834717FE9387F7FF93F7F70FD2
:10C1E000A300F7FE6398070897E0FFFFE780005180
:10C1F00037C500001305855697E0FFFFE78040191B
:10C2000037C40000930504571305E00097E0FFFFD3
:10C21000E780C01CB7C50000938505591305F000E1
:10C2200097E0FFFFE780801B93050457130500018B
:10C2300097E0FFFFE780801AB7C700001385073C2F
:10C2400097E0FFFFE780C0141304600997E0FFFF49
:10C25000E78000221304F4FF97E0FFFFE78000234C
:10C260001374F40F97F0FFFFE78040A2E31004FE81
:10C270006FE01FF8930780092311F4FE37D7000001
:10C2800093071000A38F09FC2302F7EE238FF4EC31
:10C2900097F0FFFFE780C08383C7F9FD3365F500A2
:10C2A0001375F50F631005A6B7C700009387075BEA
:10C2B00037D70000232CF7EC93078007230AF9EC0B
:10C2C00097E0FFFFE78080431309800797E0FFFFB7
:10C2D000E780001A1309F9FF97E0FFFFE780001BD2
:10C2E0001379F90F97F0FFFFE780409A97F0FFFF6F
:10C2F000E78040A9E31C09FC83C70CFE9387170065
:10C300002380FCFE97F0FFFFE780808093071000FA
:10C31000238FF4EC6FF00F9F834710F06FF04F8C7A
:10C3200037DC0000930BFBFF130C4CF693FAF70F6E
:10C3300093FBFB0F130B1B00330CFC00930D100041
:10C340006FF00FE7303B3933000000005354415287
:10C3500020535741524D202020000000303B3337FE
:10C360000000000053434F5245200000303B393756
:10C3700000000000202020534849505320000000B6
:10C38000202020574156452000000000303B393620
:10C3900000000000202020001B5B4B00424F4E5548
:10C3A0005320534849500000464C41475348495098
:10C3B00020414E44204553434F5254533A20383085
:10C3C000300000001B5B3F336C1B5B3F32356C0061
:10C3D0001B5B306D1B5B324A00000000313B393380
:10C3E00000000000532054204120522020205320E0
:10C3F0005720412052204D005468652073776172A8
:10C400006D207377617973206F76657268656164FA
:10C410002C20616E64207065656C73206F666620E9
:10C42000746F206469766520617420796F752E00C1
:10C430004120666C61677368697020646976657312
:10C44000207769746820697473206573636F7274F0
:10C45000733A2073686F6F74207468656D20666925
:10C4600072737420666F72206120626F6E75732E16
:10C47000000000004C454654202F205249474854A4
:10C48000206D6F76652020202020535041434520A9
:10C4900066697265202020202050207061757365C8
:10C4A0002020202020455343206769766520757041
:10C4B0000000000020203630202F20313530202091
:10C4C0002020202020203530202F20313030202007
:10C4D0002020202020203430202F20383020202001
:10C4E00020202020203330202F2036302020000034
:10C4F00020202020494E20464F524D4154494F4E56
:10C50000202F20444956494E47000000313B393620
:10C5100000000000505245535320535041434520E2
:10C52000544F20424547494E000000001B5B306DD0
:10C530001B5B324A1B5B481B5B3F323568000000C7
:10C5400048455245205448455920434F4D45000029
:10C55000504155534544202D205020544F20474FE3
:10C56000204F4E1B5B4B0000303B34343B393700CF
:10C5700020202020202020202020202020202020BB
:10C5800020202020202020202020202020200000EB
:10C590002020202020202020202047414D45204FD2
:10C5A000564552202020202020202020202000003E
:10C5B000535741524D2044455354524F594544001E
:10C5C000003C506496000000001E28323C00000031
:10C5D0000000020406000000000000040000040047
:10C5E0000000000003030303030300000002020233
:10C5F0000202020202000101010101010101010127
:10C60000010101010101010101010101010101011A
:10C610000101010100060C13181E24292D313538A3
:10C620003B3D3F4040403F3D3B3835312D29241EA6
:10C6300018130C0600FAF4EDE8E2DCD7D3CFCBC830
:10C64000C5C3C1C0C0C0C1C3C5C8CBCFD3D7DCE24E
:10C65000E8EDF4FA00000000000000000000000017
:10C6600000000000000C00000000C0000000C0003E
:10C67000000C000000000CCCCCC000000000CC4C32
:10C68000C4CC0000000CCCCCCCCCC0000CCCCCCCAE
:10C69000CCCCCCC00C0CCCCCCCCCC0C00C0C000096
:10C6A0000000C0C00000CC0000CC00000000000072
:10C6B000000000000000000000000000000000007A
:10C6C000000000000000000000000000000000006A
:10C6D000000000000000000000000000000000005A
:10C6E000000000000000C000000C00000000C000BE
:10C6F000000C000000000CCCCCC000000000CC4CB2
:10C70000C4CC0000000CCCCCCCCCC00000CCCCCC39
:10C71000CCCCCC000CCCCCCCCCCCCCC0C00C000055
:10C720000000C00C000CC000000CC00000000000A5
:10C7300000000000000000000000000000000000F9
:10C7400000000000000000000000000000000000E9
:10C7500000000000000000000000000000000000D9
:10C7600000000000000D00000000D0000000D0001C
:10C77000000D000000000DDDDDD000000000DDBD7B
:10C78000DBDD0000000DDDDDDDDDD0000DDDDDDDFC
:10C79000DDDDDDD00D0DDDDDDDDDD0D00D0D0000EA
:10C7A0000000D0D00000DD0000DD0000000000002F
:10C7B0000000000000000000000000000000000079
:10C7C0000000000000000000000000000000000069
:10C7D0000000000000000000000000000000000059
:10C7E000000000000000D000000D00000000D0009C
:10C7F000000D000000000DDDDDD000000000DDBDFB
:10C80000DBDD0000000DDDDDDDDDD00000DDDDDD88
:10C81000DDDDDD000DDDDDDDDDDDDDD0D00D000099
:10C820000000D00D000DD000000DD0000000000071
:10C8300000000000000000000000000000000000F8
:10C8400000000000000000000000000000000000E8
:10C8500000000000000000000000000000000000D8
:10C86000000000000009000000009000000090009F
:10C87000000900000000099999900000000099F952
:10C880009F9900000009999999999000099999999F
:10C890009999999009099999999990900909000095
:10C8A0000000909000009900009900000000000036
:10C8B0000000000000000000000000000000000078
:10C8C0000000000000000000000000000000000068
:10C8D0000000000000000000000000000000000058
:10C8E000000000000000900000090000000090001F
:10C8F000000900000000099999900000000099F9D2
:10C900009F99000000099999999990000099999927
:10C910009999990009999999999999909009000084
:10C92000000090090009900000099000000000003C
:10C9300000000000000000000000000000000000F7
:10C9400000000000000000000000000000000000E7
:10C950000000000000000000000000000000000BCC
:10C96000B0000000000000BBBB00000000B00BB92D
:10C970009BB00B0000BB0BBBBBB0BB0000BBBBBB89
:10C98000BBBBBB00000BB9BBBB9BB0000000BBBB7B
:10C99000BBBB000000000B9BB9B000000000B00062
:10C9A000000B0000000B00000000B00000000000C1
:10C9B0000000000000000000000000000000000077
:10C9C0000000000000000000000000000000000067
:10C9D0000000000000000000000000000000000B4C
:10C9E000B0000000000000BBBB00000000000BB95D
:10C9F0009BB00000B0BB0BBBBBB0BB0B0BBBBBBB4E
:10CA0000BBBBBBB0000BB9BBBB9BB0000000BBBB4A
:10CA1000BBBB000000000B9BB9B0000000000B0086
:10CA200000B000000000B000000B0000000000009B
:10CA300000000000000000000000000000000000F6
:10CA400000000000000000000000000000000000E6
:10CA50000000000000000000000000000000000FC7
:10CA6000F00000000000000FF0000000000000FFD8
:10CA7000FF000000000000F99F00000000F00FFF21
:10CA8000FFF00F0000F0FFFFFFFF0F0000FFFFFFB0
:10CA9000FFFFFF0000FFF99FF99FFF0000FF00FF6D
:10CAA000FF00FF0000000000000000000000000088
:10CAB0000000000000000000000000000000000076
:10CAC0000000000000000000000000000000000066
:10CAD0000000000000000000000000000000000056
:10CAE0000000000000000000000000000000000046
:10CAF0000000000000000000000000000000000B2B
:10CB0000B00000000000000BB00000000000000BAF
:10CB1000B00000000000000BB00000000000000B9F
:10CB2000B00000000000000BB0000000000000009A
:10CB300000000000000000000000000000000000F5
:10CB400000000000000000000000000000000000E5
:10CB500000000000000000000000000000000000D5
:10CB600000000000000000000000000000000000C5
:10CB700000000000000000000000000000000000B5
:10CB80000000000000000009F00000000000000F9D
:10CB9000900000000000000990000000000000006C
:10CBA0000000000000000000000000000000000085
:10CBB0000000000000000000000000000000000075
:10CBC0000000000000000000000000000000000065
:10CBD0000000000000000000000000000000000055
:10CBE00000000000000000000000000000000099AC
:10CBF0000000000000000990909000000000000B71
:10CC0000B0B9000000009BB0FB000000000090FFE6
:10CC1000FBB9900000000BFFFFB0000000000B0FFD
:10CC20000B090000000999B0B900000000000900DC
:10CC300090990000000000090000000000000000C2
:10CC400000000000000000000000000000000000E4
:10CC500000000000000000000000000000000100D3
:10CC600000100000000001000100000000000000B2
:10CC70001000000000100999000000100019009930
:10CC800090900010111099BBB0900000001000BBF4
:10CC9000B0900000001090BBB0B000100000000089
:10CCA0000090000000000099090900000000090937
:10CCB0009900010000010000910000000000100038
:10CCC0000000100000000110000100000000000042
:10CCD000000000000000000B00000000000000B099
:10CCE00000000000000000B000000000000000F0A4
:10CCF0000F00B000000000F00F0B0B000B00FF0056
:10CD0000000000000000FFFFFF0000000000F0FF37
:10CD10000FF000000000FFFFFFF0FB000000F0FF3D
:10CD2000F00F000B000FFF0F000FB0B000B00F0F9F
:10CD300000FFB0B00B0000000000B0B000B0000079
:10CD4000000B0000000B00B0000000000000000B12
:10CD500000000000000000000000000000000000D3
:10CD60000000000000000000E000000000000000E3
:10CD700000000000000000000000000000000000B3
:10CD800000000000000000000000000000000000A3
:10CD90000000000000000000000000000000000093
:10CDA0000000000000000000000000000000000083
:10CDB0000000000000000000000000000000000073
:10CDC0000000000000000000000000000000000063
:10CDD000000000F000000000000000000000000063
:10CDE0000000000000F00000000000000000000053
:10CDF0000000000000000000000000000000000033
:10CE0000070000000000000000000000000000001B
:10CE10000000000000000000000000000000000012
:10CE20000000000000000000000000000000000002
:10CE300000000000000000000000000000000000F2
:10CE400000000000000000000000000000000000E2
:10CE500000000000000000000000000000000000D2
:10CE600000000000000000000000000000000000C2
:10CE70000000000000000000000E000000000000A4
:10CE800000000000000000000000000000000000A2
:10CE90000000000000000000000000000000000092
:10CEA0000000000000000000000000000000000082
:10CEB0000000000000000000000000000000000072
:10CEC00000000000000000000000000000D0000092
:04CED000000000005E
:040000030000A20057
:00000001FF
+121
View File
@@ -0,0 +1,121 @@
#!/usr/bin/env python3
"""Run Star Swarm in tools/rv32emu.py on a model of the board (tools/boardemu.py)
and take screenshots.
python3 test-swarm.py [--shots DIR] [--play]
It checks the title screen, measures the Z-80 instructions each frame takes
(a 60 Hz frame has about 550,000 at 90 MHz), checks that the formation sways
and that aliens dive, shoots one out of the formation, and with --play lets an
autopilot clear the whole swarm. The autopilot can't be killed (it keeps the
ship shielded), so this checks the rules and the waves, not its skill.
"""
import argparse
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "..", "tools"))
from boardemu import Board, load_ihx, load_font, load_syms # noqa: E402
LEFT, RIGHT, FIRE, ESC = 4, 8, 16, 128
FX0, CW = 60, 20
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--shots", help="write PNG screenshots here")
ap.add_argument("--play", action="store_true", help="let the autopilot clear a wave")
a = ap.parse_args()
mem, hi = load_ihx(os.path.join(HERE, "swarm.ihx"))
b = Board(mem, code_end=hi)
font = load_font() if a.shots else None
if a.shots:
os.makedirs(a.shots, exist_ok=True)
sym = {"_" + n: a for n, a in load_syms(os.path.join(HERE, "out", "swarm.sym")).items()}
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def s16(n, k=0):
v = b.m[sym[n] + 2 * k] | b.m[sym[n] + 2 * k + 1] << 8
return v - 65536 if v & 0x8000 else v
def u8(n, k=0):
return b.m[sym[n] + k]
def screen():
return b.screen_text().display
b.run_frames(5)
check("title screen", "S T A R S W A R M" in "\n".join(screen()))
if a.shots: b.shot(os.path.join(a.shots, "1-title.png"), font)
b.run_frames(3, FIRE)
b.run_frames(5)
check("game starts", "WAVE 1" in screen()[0] and "SHIPS 3" in screen()[0])
check("46 aliens in formation", sum(1 for k in range(60) if b.m[sym["_cell"] + k] == 1) == 46)
s0 = b.scroll_x
c0 = b.steps
worst = 0
for f in range(240):
c1 = b.steps
b.run_frames(1)
worst = max(worst, b.steps - c1)
per = (b.steps - c0) / 240
print("%.0f instructions a frame on average, %d at most (%.0f%%)" % (per, worst, worst / 5500))
check("every frame fits in a 60th of a second", worst < 550000)
check("the formation sways", b.scroll_x != s0)
check("aliens dive", any(u8("_dv_on", k) for k in range(8)))
if a.shots: b.shot(os.path.join(a.shots, "2-dive.png"), font)
sc = s16("_score")
b.m[sym["_shield"]] = 1
b.run_frames(2, RIGHT) # off the gap between two columns
b.run_frames(3, FIRE)
b.run_frames(40)
check("a shot into the formation scores", s16("_score") > sc)
if a.play:
frame = 0
while frame < 20000 and b.pc != 0:
b.m[sym["_shield"]] = 1
sx = s16("_ship_x") + 8
# aim at a diver low down if there is one, else the nearest column
target = None
for k in range(8):
if u8("_dv_on", k) and s16("_dv_y", k) >> 4 > 60:
target = (s16("_dv_x", k) >> 4) + 8
break
if target is None:
sway = (320 - b.scroll_x) % 320
if sway > 160: sway -= 320
best = None
for c in range(10):
if any(b.m[sym["_cell"] + r * 10 + c] == 1 for r in range(6)):
x = FX0 + c * CW + 2 + 8 + sway
if best is None or abs(x - sx) < abs(best - sx):
best = x
target = best if best is not None else sx
j = FIRE if frame % 2 else 0
if target > sx + 1: j |= RIGHT
elif target < sx - 1: j |= LEFT
b.run_frames(1, j)
frame += 1
if frame == 600 and a.shots: b.shot(os.path.join(a.shots, "3-battle.png"), font)
if "WAVE 2" in screen()[0]:
break
print(screen()[0])
check("autopilot cleared the swarm in %d frames" % frame, "WAVE 2" in screen()[0])
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc gives up the game", "PRESS SPACE TO BEGIN" in "\n".join(screen()))
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc returns to BASIC", b.pc == 0)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()
+134
View File
@@ -0,0 +1,134 @@
#!/usr/bin/env python3
"""Make data.h for warden.c: the sprite shapes and the planet's mountains.
Shapes are 16 rows of 16 hex digits, one colour per pixel (0 is see-through).
The planet is 1,280 pixels round; the mountains are a height for every column,
made by midpoint displacement so that they join up where the world wraps.
"""
import random
import sys
SHIP = ["0" * 16] * 5 + [
"0F00000000000000",
"0FF0000066000000",
"BFFFFFF666600000",
"BFFFFFFFFFFFFF00",
"0CCCCCCCCCCCC000",
"0FF0000000000000",
"0F00000000000000",
] + ["0" * 16] * 4
LANDER = ["0" * 16] * 3 + [
"00000BBBBBB00000",
"0000BBBBBBBB0000",
"000AAAAAAAAAA000",
"00AA2AA2AA2AAA00",
"00AAAAAAAAAAAA00",
"000AAAAAAAAAA000",
"0000A00AA00A0000",
"000A000AA000A000",
"00A0000AA0000A00",
] + ["0" * 16] * 4
MUTANT = ["0" * 16] * 2 + [
"0000D00000D00000",
"00000D000D000000",
"000DDDDDDDDD0000",
"00DDAADDDAADD000",
"0DDDAADDDAADDD00",
"00DDDDDDDDDDD000",
"000D0D0D0D0D0000",
"00D0D0D0D0D0D000",
] + ["0" * 16] * 6
BOMBER = ["0" * 16] * 4 + [
"0000CCCCCCCC0000",
"0000C444444C0000",
"0000C4EEEE4C0000",
"0000C4EEEE4C0000",
"0000C444444C0000",
"0000CCCCCCCC0000",
] + ["0" * 16] * 6
MINE = ["0" * 16] * 6 + [
"0000000990000000",
"000000999F000000",
"000000F999000000",
"0000000990000000",
] + ["0" * 16] * 6
HUMAN = ["0" * 16] * 6 + [
"0000000FF0000000",
"0000000FF0000000",
"000000DDDD000000",
"00000D0DD0D00000",
"0000000DD0000000",
"0000000CC0000000",
"000000C00C000000",
"000000C00C000000",
] + ["0" * 16] * 2
LASER = ["0" * 16] * 8 + ["BFFFFFFFFFFFFFFB"] + ["0" * 16] * 7
SHOT = ["0" * 16] * 7 + ["0000000F90000000", "00000009F0000000"] + ["0" * 16] * 7
def burst(seed, cols, size):
import math
r = random.Random(seed)
img = [["0"] * 16 for _ in range(16)]
for _ in range(90):
a = r.uniform(0, 2 * math.pi)
d = r.uniform(0, size)
x, y = int(8 + d * math.cos(a)), int(8 + d * math.sin(a))
if 0 <= x < 16 and 0 <= y < 16:
img[y][x] = cols[0] if d < size / 3 else cols[1] if d < 2 * size / 3 else cols[2]
return ["".join(row) for row in img]
BOOM1 = burst(7, "FB9", 5)
BOOM2 = burst(9, "B91", 7.5)
SHIPBOOM = burst(11, "FFB", 7.9)
SHAPES = [("S_SHIP", SHIP), ("S_LANDER", LANDER), ("S_MUTANT", MUTANT), ("S_BOMBER", BOMBER),
("S_MINE", MINE), ("S_HUMAN", HUMAN), ("S_LASER", LASER), ("S_SHOT", SHOT),
("S_BOOM1", BOOM1), ("S_BOOM2", BOOM2), ("S_SHIPBOOM", SHIPBOOM)]
WORLD = 1280
def mountains():
r = random.Random(2084)
n = WORLD
h = [0.0] * n
step = 256 # 1280 = 5 x 256, so halving reaches every column
for i in range(0, n, step):
h[i] = r.uniform(150, 205)
amp = 48.0
while step > 1:
half = step // 2
for i in range(0, n, step):
a, b = h[i], h[(i + step) % n]
h[i + half] = (a + b) / 2 + r.uniform(-amp, amp)
step = half
amp *= 0.62
assert all(v > 0 for v in h), "a column was missed"
return [max(118, min(222, int(v))) for v in h]
def main(out):
L = ["/* made by gen.py - edit that instead */", ""]
for i, (name, img) in enumerate(SHAPES):
assert len(img) == 16 and all(len(row) == 16 for row in img), name
L.append("#define %s %d" % (name, i))
L.append("#define N_SHAPES %d" % len(SHAPES))
L.append("#define WORLD %d" % WORLD)
L.append("static const u8 shape_data[N_SHAPES * 128] = {")
for name, img in SHAPES:
b = [int(row[x], 16) << 4 | int(row[x + 1], 16) for row in img for x in range(0, 16, 2)]
L.append(" " + ",".join(str(v) for v in b) + ", /* %s */" % name)
L.append("};")
m = mountains()
L.append("static const u8 mountain[WORLD] = {")
for k in range(0, WORLD, 32):
L.append(" " + ",".join(str(v) for v in m[k:k + 32]) + ",")
L.append("};")
open(out, "w").write("\n".join(L) + "\n")
print("data.h: %d shapes, mountains %d-%d" % (len(SHAPES), min(m), max(m)))
if __name__ == "__main__":
main(sys.argv[1] if len(sys.argv) > 1 else "data.h")
+116
View File
@@ -0,0 +1,116 @@
#!/usr/bin/env python3
"""Run Star Warden in tools/rv32emu.py on a model of the board (tools/boardemu.py)
and take screenshots.
python3 test-warden.py [--shots DIR] [--play]
It checks the title screen, measures the Z-80 instructions each frame takes
(a 60 Hz frame has about 550,000 at 90 MHz), flies along the planet checking
that the picture scrolls and the scanner fills in, and with --play lets an
autopilot hunt down every enemy until the wave is cleared. The autopilot
can't be killed (it keeps the ship shielded), so this checks the rules and the
waves, not its skill.
"""
import argparse
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "..", "tools"))
from boardemu import Board, load_ihx, load_font, load_syms # noqa: E402
UP, DOWN, LEFT, RIGHT, FIRE, ENTER, SHIFT, ESC = 1, 2, 4, 8, 16, 32, 64, 128
WORLD = 1280
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--shots", help="write PNG screenshots here")
ap.add_argument("--play", action="store_true", help="let the autopilot clear a wave")
a = ap.parse_args()
mem, hi = load_ihx(os.path.join(HERE, "warden.ihx"))
b = Board(mem, code_end=hi)
font = load_font() if a.shots else None
if a.shots:
os.makedirs(a.shots, exist_ok=True)
sym = {"_" + n: a for n, a in load_syms(os.path.join(HERE, "out", "warden.sym")).items()}
errors = 0
def check(what, ok):
nonlocal errors
print(("PASS " if ok else "FAIL ") + what)
errors += not ok
def u16(n, k=0):
return b.m[sym[n] + 2 * k] | b.m[sym[n] + 2 * k + 1] << 8
def u8(n, k=0):
return b.m[sym[n] + k]
def screen():
return b.screen_text().display
b.run_frames(5)
check("title screen", "S T A R W A R D E N" in "\n".join(screen()))
if a.shots: b.shot(os.path.join(a.shots, "1-title.png"), font)
b.run_frames(3, FIRE)
b.run_frames(5)
check("game starts", "WAVE 1" in screen()[0] and "SHIPS 3" in screen()[0])
c0 = b.steps
b.run_frames(60, RIGHT)
per = (b.steps - c0) / 60
print("%.0f instructions a frame while flying (%.0f%% of a 60 Hz frame)" % (per, per / 5500))
check("a frame fits in a 60th of a second", per < 500000)
check("flying right scrolls the planet", b.scroll_x != 0)
check("the scanner shows enemies", sum(r.count("o") for r in screen()[1:4]) >= 4)
if a.shots: b.shot(os.path.join(a.shots, "2-flying.png"), font)
s0 = b.scroll_x
b.run_frames(80, LEFT)
check("turning round scrolls the other way", b.scroll_x != s0)
if a.shots: b.shot(os.path.join(a.shots, "3-turned.png"), font)
if a.play:
b.m[sym["_shield"]] = 1
worst = 0
frame = 0
while frame < 9000 and b.pc != 0:
b.m[sym["_shield"]] = 1
px, py = u16("_px"), u8("_py")
best, bd = None, 9999
for k in range(16):
if u8("_etype", k):
ex = u16("_ex", k) >> 4
d = (ex - px + WORLD // 2) % WORLD - WORLD // 2
if abs(d) < abs(bd):
bd, best = d, k
j = 0
if best is not None:
ey = u8("_ey", best)
if ey < py - 1: j |= UP
elif ey > py + 1: j |= DOWN
if abs(bd) > 90 or (bd > 0) != (b.spr[0][3] & 2 == 0):
j |= RIGHT if bd > 0 else LEFT
if abs(ey - py) < 6 and abs(bd) < 260:
j |= FIRE if frame % 2 else 0
c0 = b.steps
b.run_frames(1, j)
worst = max(worst, b.steps - c0)
frame += 1
if frame == 120 and a.shots: b.shot(os.path.join(a.shots, "4-battle.png"), font)
if "WAVE 2" in screen()[0]:
break
print(screen()[0])
check("autopilot cleared wave 1 in %d frames" % frame, "WAVE 2" in screen()[0])
print("slowest frame: %d instructions (%.0f%% of a 60 Hz frame)" % (worst, worst / 5500))
check("every frame fits in a 60th of a second", worst < 550000)
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc gives up the game", "PRESS SPACE TO BEGIN" in "\n".join(screen()))
b.run_frames(5, ESC)
b.run_frames(5)
check("Esc returns to BASIC", b.pc == 0)
sys.exit(1 if errors else 0)
if __name__ == "__main__":
main()
+4
View File
@@ -0,0 +1,4 @@
10 REM STAR WARDEN: LOAD THE GAME (MEMORY FILE WARDGAME) AND PLAY IT
20 REM THE GAME RUNS AT A200H, PAST THIS PROGRAM, AND COMES BACK HERE
30 BLOAD "WARDGAME": CALL 41472
40 CLS
+816
View File
@@ -0,0 +1,816 @@
/* Star Warden: a side-scrolling planet defence game for the Basys 3 RISC-V computer.
Landers come down to snatch the humanoids walking on the planet and carry
them off; one that reaches the top turns into a mutant that hunts you.
Shoot them, catch the humanoids they drop, and stay clear of the bombers'
mines. The long-range scanner at the top shows the whole planet. A wave
ends when every enemy is gone, with a bonus for each humanoid still alive.
The planet is 1,280 pixels round, exactly four buffers wide, so world
column x always lives in buffer column x mod 320. The hardware scrolls the
graphics (ports 35h-37h) with its edge border on, and the game draws the
columns that come into view in the hidden border. Everything that moves is
a sprite; the scanner and the score are text. It runs at A200h, loaded by
the WARDEN program in BASIC. */
typedef unsigned char u8;
typedef signed char i8;
typedef unsigned short u16;
typedef short i16;
#include "basys3.h"
#define con_stat IO8(0x00)
#define con_data IO8(0x01)
#define gx_lo IO8(0x30)
#define gx_hi IO8(0x31)
#define gy_p IO8(0x32)
#define gpix IO8(0x33)
#define gfill IO8(0x34)
#define scr_xlo IO8(0x35)
#define scr_xhi IO8(0x36)
#define scr_y IO8(0x37)
#define sp_sel IO8(0x70)
#define sp_xlo IO8(0x71)
#define sp_xhi IO8(0x72)
#define sp_y IO8(0x73)
#define sp_shape IO8(0x74)
#define sp_ctl IO8(0x75)
#define sp_load IO8(0x76)
#define sp_data IO8(0x77)
#define frame_ctr IO8(0x78)
#define joy IO8(0x88)
#include "data.h" /* out/data.h, made by gen.py */
#define J_UP 1
#define J_DOWN 2
#define J_LEFT 4
#define J_RIGHT 8
#define J_FIRE 16
#define J_ENTER 32
#define J_SHIFT 64
#define J_ESC 128
#define TOP_Y 34 /* the play area: below the scanner */
#define GROUND_Y 210 /* a humanoid's sprite stands here */
#define BOT_Y 214 /* lowest the ship goes */
/* sprites: lower numbers in front */
#define SP_SHIP 0
#define SP_LASER 1 /* 4 */
#define SP_SHOT 5 /* 4 */
#define SP_POOL 9 /* 23: enemies, humanoids, mines, explosions */
#define SP_END 32
#define NE 16 /* enemies */
#define NH 8 /* humanoids */
#define NM 6 /* mines */
#define NL 4 /* lasers */
#define NS 4 /* enemy shots */
#define NX 5 /* explosions */
#define E_NONE 0
#define E_LANDER 1
#define E_MUTANT 2
#define E_BOMBER 3
#define L_SEEK 0
#define L_DOWN 1
#define L_CARRY 2
#define H_WALK 0
#define H_HELD 1
#define H_FALL 2
#define H_GONE 3
/* ---------------------------------------------------------------- state */
/* not static: test-warden.py reads these from the link map */
u16 cam; /* world column at the screen's left */
u16 px; /* ship, world pixels */
u8 py;
u8 lives, bombs, wave, dead, inv, shield;
u16 score;
u8 etype[NE];
u16 ex[NE]; /* world x in 1/16 pixels */
u8 ey[NE];
static i16 vx; /* 1/16 pixels a frame */
static u8 pxf; /* sub-pixel */
static u8 facing; /* 0 right, 1 left */
static i16 off; /* ship's screen x */
static u8 dead_time, fire_cool, bomb_held, hyper_held;
static u16 next_extra;
static u8 estate[NE], etgt[NE], ecool[NE];
static i8 evx[NE], evy[NE];
static u8 ebase[NE];
static u16 hx[NH];
static u8 hy[NH], hstate[NH], hdir[NH], hvy[NH], hfrom[NH];
static u16 mx[NM];
static u8 my[NM];
static u16 mlife[NM];
static u16 lx[NL];
static u8 ly[NL], ldir[NL], llife[NL];
static u16 sx16[NS], sy16[NS];
static i16 svx[NS], svy[NS];
static u8 slife[NS];
static u16 xx[NX];
static u8 xy[NX], xt[NX];
static u8 pool_used, pool_last;
static u8 last_frame, quit, hud_dirty, scan_timer;
static u16 rnd_state;
static const char *msg;
static u8 msg_time;
/* ----------------------------------------------------------------- text */
static void putch(char c) { while (!(con_stat & 2)); con_data = c; }
static void puts_(const char *s) { while (*s) putch(*s++); }
static void putn(u16 n, u8 width)
{
char b[6];
u8 i = 0;
do { b[i++] = '0' + n % 10; n /= 10; } while (n);
while (width > i) { putch('0'); width--; }
while (i) putch(b[--i]);
}
static void at(u8 r, u8 c) { putch(27); putch('['); putn(r, 0); putch(';'); putn(c, 0); putch('H'); }
static void ink(const char *s) { putch(27); putch('['); puts_(s); putch('m'); }
static void centre(u8 r, const char *s)
{
u8 n = 0;
while (s[n]) n++;
at(r, 41 - n / 2);
puts_(s);
}
static u16 rnd(void)
{
rnd_state ^= rnd_state << 7;
rnd_state ^= rnd_state >> 9;
rnd_state ^= rnd_state << 8;
return rnd_state;
}
static void wait_frame(void)
{
while (frame_ctr == last_frame);
last_frame = frame_ctr;
}
static u8 drain_keys(void)
{
u8 c, p = 0;
while (con_stat & 1) {
c = con_data;
if (c == 'p' || c == 'P') p = 1;
}
return p;
}
static void say(const char *s) { msg = s; msg_time = 120; hud_dirty = 1; }
/* ---------------------------------------------------------- the planet */
static u16 wrapx(i16 x)
{
while (x < 0) x += WORLD;
while (x >= WORLD) x -= WORLD;
return (u16)x;
}
/* shortest signed distance from a to b round the planet */
static i16 dist(u16 a, u16 b)
{
i16 d = (i16)b - (i16)a;
if (d > WORLD / 2) d -= WORLD;
else if (d < -WORLD / 2) d += WORLD;
return d;
}
static i16 absd(i16 v) { return v < 0 ? -v : v; }
/* move a world x in 1/16 pixels by d, round the planet */
static u16 step16(u16 x, i16 d)
{
i16 v = (i16)x + d;
if (v < 0) v += WORLD * 16;
else if (v >= WORLD * 16) v -= WORLD * 16;
return (u16)v;
}
/* Draw world column x into its buffer column. */
static void draw_col(u16 x)
{
u16 bx = x;
u8 y, a, b, lo, hi, star_y, star_c = 0, c;
while (bx >= 320) bx -= 320;
a = mountain[x];
b = mountain[x + 1 < WORLD ? x + 1 : 0];
lo = a < b ? a : b;
hi = a < b ? b : a;
star_y = 0;
if (!((x * 37) & 3)) {
star_y = TOP_Y + (u8)((x * 71) % 110);
star_c = (x & 8) ? 15 : (x & 16) ? 14 : 7;
}
for (y = 0; y < 240; y++) {
c = 0;
if (y >= lo && y <= hi) c = 3;
else if (y == star_y) c = star_c;
else if (y == 236) c = 4; /* the planet's surface line */
gx_lo = (u8)bx; gx_hi = (u8)(bx >> 8); gy_p = y;
gpix = c;
}
}
static void set_scroll(void)
{
u16 b = cam;
while (b >= 320) b -= 320;
scr_xlo = (u8)b; scr_xhi = (u8)(b >> 8) | 2; scr_y = 0;
}
static void draw_all(void)
{
u16 i;
for (i = 0; i < 320; i++) draw_col(wrapx(cam + i));
}
/* Move the camera by d columns (at most 4 either way) and draw what comes
into view: those columns are in the hidden border until next frame. */
static void scroll_by(i16 d)
{
i16 k;
if (d > 0)
for (k = 0; k < d; k++) draw_col(wrapx(cam + 320 + k));
else
for (k = 0; k < -d; k++) draw_col(wrapx((i16)cam - 1 - k));
cam = wrapx((i16)cam + d);
}
/* -------------------------------------------------------------- sprites */
static void spr_at(u8 n, i16 x, i16 y, u8 shape, u8 ctl)
{
sp_sel = n;
if (x < -15 || x > 319 || y < -15 || y > 239) { sp_ctl = 0; return; }
sp_xlo = (u8)x; sp_xhi = (u8)(x >> 8) & 1; sp_y = (u8)y;
sp_shape = shape; sp_ctl = ctl;
}
static void spr_off(u8 n) { sp_sel = n; sp_ctl = 0; }
/* A world object at (wx, y) on screen gets the next pool sprite. */
static void pool_put(u16 wx, i16 y, u8 shape, u8 ctl)
{
i16 s = dist(cam, wx);
if (s < -15 || s > 319 || pool_used == SP_END - SP_POOL) return;
spr_at(SP_POOL + pool_used, s - 8, y - 8, shape, ctl);
pool_used++;
}
static void pool_end(void)
{
u8 i;
for (i = pool_used; i < pool_last; i++) spr_off(SP_POOL + i);
pool_last = pool_used;
pool_used = 0;
}
static void all_off(void) { u8 i; for (i = 0; i < 32; i++) spr_off(i); pool_last = 0; }
static void load_shapes(void)
{
const u8 *p = shape_data;
u8 s, i;
for (s = 0; s < N_SHAPES; s++) {
sp_load = s;
for (i = 0; i < 128; i++) sp_data = *p++;
}
}
static u8 on_screen(u16 wx)
{
i16 s = dist(cam, wx);
return s >= 0 && s < 320;
}
static void boom(u16 wx, u8 y)
{
u8 i, best = 0;
for (i = 0; i < NX; i++) if (xt[i] < xt[best]) best = i;
xx[best] = wx; xy[best] = y; xt[best] = 20;
}
/* ------------------------------------------------------------------ HUD */
static void hud(void)
{
hud_dirty = 0;
at(1, 1); ink("0;93"); puts_("STAR WARDEN ");
ink("0;37"); puts_("SCORE "); ink("0;97"); putn(score, 6);
ink("0;37"); puts_(" SHIPS "); ink("0;97"); putn(lives, 0);
ink("0;37"); puts_(" BOMBS "); ink("0;97"); putn(bombs, 0);
ink("0;37"); puts_(" WAVE "); ink("0;97"); putn(wave, 0);
if (msg && msg_time) { ink("0;96"); puts_(" "); puts_(msg); }
puts_("\033[K");
ink("0");
}
/* The scanner: the whole planet in 80 columns and 3 rows, the ship in the
middle. */
static u8 scan_col(u16 wx)
{
i16 d = dist(px, wx) + WORLD / 2;
d >>= 4;
return 1 + (u8)(d > 79 ? 79 : d);
}
static u8 scan_row(u8 y) { return 2 + (y < 100 ? 0 : y < 160 ? 1 : 2); }
static void scanner(void)
{
u8 i;
for (i = 2; i <= 4; i++) { at(i, 1); puts_("\033[2K"); }
ink("0;34");
at(2, 33); putch('['); at(4, 33); putch('[');
at(2, 48); putch(']'); at(4, 48); putch(']');
ink("0;95");
for (i = 0; i < NH; i++)
if (hstate[i] != H_GONE) { at(scan_row(hy[i]), scan_col(hx[i])); putch('.'); }
for (i = 0; i < NE; i++) {
if (!etype[i]) continue;
ink(etype[i] == E_LANDER ? "0;92" : etype[i] == E_MUTANT ? "0;95" : "0;94");
at(scan_row(ey[i]), scan_col(ex[i] >> 4));
putch(etype[i] == E_BOMBER ? '#' : 'o');
}
if (!dead) { ink("0;93"); at(scan_row(py), scan_col(px)); putch(facing ? '<' : '>'); }
ink("0");
}
/* --------------------------------------------------------------- waves */
static u8 humans_left(void)
{
u8 i, n = 0;
for (i = 0; i < NH; i++) if (hstate[i] != H_GONE) n++;
return n;
}
static void place_humans(void)
{
u8 i;
for (i = 0; i < NH; i++) {
hx[i] = wrapx(i * (WORLD / NH) + (rnd() & 63));
hy[i] = GROUND_Y; hstate[i] = H_WALK; hdir[i] = rnd() & 1;
}
}
static void spawn(u8 type)
{
u8 i;
u16 x;
for (i = 0; i < NE && etype[i]; i++);
if (i == NE) return;
do x = rnd() % WORLD; while (absd(dist(px, x)) < 260);
etype[i] = type; ex[i] = x << 4;
ey[i] = TOP_Y + 6 + (rnd() & 63);
estate[i] = L_SEEK; etgt[i] = 255;
ecool[i] = 60 + (rnd() & 127);
evx[i] = (rnd() & 1) ? 8 : -8;
evy[i] = 0;
ebase[i] = ey[i];
}
static void start_wave(void)
{
u8 i, n;
for (i = 0; i < NE; i++) etype[i] = E_NONE;
for (i = 0; i < NM; i++) mlife[i] = 0;
for (i = 0; i < NS; i++) { slife[i] = 0; spr_off(SP_SHOT + i); }
if (wave % 5 == 1) place_humans(); /* the planet is restored */
n = 4 + 2 * wave; if (n > 12) n = 12;
for (i = 0; i < n; i++) spawn(E_LANDER);
n = wave < 4 ? wave - 1 : 3;
for (i = 0; i < n; i++) spawn(E_BOMBER);
if (!humans_left())
for (i = 0; i < NE; i++) if (etype[i] == E_LANDER) etype[i] = E_MUTANT;
}
static u8 enemies_left(void)
{
u8 i, n = 0;
for (i = 0; i < NE; i++) if (etype[i]) n++;
return n;
}
/* --------------------------------------------------------------- rules */
static void add_score(u16 n)
{
score += n;
if (score >= next_extra) {
next_extra += 10000;
lives++; bombs++;
say("BONUS SHIP");
}
hud_dirty = 1;
}
static void kill_ship(void)
{
u8 i;
if (dead || inv || shield) return;
dead = 1; dead_time = 100;
boom(px, py);
spr_off(SP_SHIP);
for (i = 0; i < NS; i++) { slife[i] = 0; spr_off(SP_SHOT + i); }
}
static void kill_enemy(u8 i)
{
u8 h;
if (etype[i] == E_LANDER && estate[i] == L_CARRY && etgt[i] < NH) {
h = etgt[i];
hstate[h] = H_FALL; hvy[h] = 0; hfrom[h] = hy[h];
}
add_score(etype[i] == E_BOMBER ? 250 : 150);
boom(ex[i] >> 4, ey[i]);
etype[i] = E_NONE;
}
static void enemy_fire(u8 i, u8 often)
{
u8 k;
i16 dx, dy, m;
if (ecool[i]) { ecool[i]--; return; }
ecool[i] = often + (rnd() & 63);
if (dead || !on_screen(ex[i] >> 4)) return;
for (k = 0; k < NS && slife[k]; k++);
if (k == NS) return;
dx = dist(ex[i] >> 4, px);
dy = (i16)py - ey[i];
m = absd(dx) > absd(dy) ? absd(dx) : absd(dy);
if (!m) return;
sx16[k] = ex[i]; sy16[k] = (u16)ey[i] << 4;
svx[k] = dx * 32 / m; svy[k] = dy * 32 / m;
slife[k] = 90;
}
static void enemies(void)
{
u8 i, h, best;
i16 d, bd;
u16 x;
for (i = 0; i < NE; i++) {
if (!etype[i]) continue;
x = ex[i] >> 4;
switch (etype[i]) {
case E_LANDER:
if (estate[i] == L_SEEK) {
ex[i] = step16(ex[i], evx[i]);
if (!(last_frame & 31)) ey[i] = ebase[i] + (rnd() & 15);
/* pick a humanoid nobody is after, and go for it when above it */
if (etgt[i] == 255) {
best = 255; bd = 2000;
for (h = 0; h < NH; h++) {
if (hstate[h] != H_WALK) continue;
d = absd(dist(x, hx[h]));
if (d < bd) { bd = d; best = h; }
}
etgt[i] = best;
} else if (hstate[etgt[i]] != H_WALK) etgt[i] = 255;
else if (absd(dist(x, hx[etgt[i]])) < 3) estate[i] = L_DOWN;
enemy_fire(i, 90);
} else if (estate[i] == L_DOWN) {
h = etgt[i];
if (h >= NH || hstate[h] != H_WALK) { estate[i] = L_SEEK; etgt[i] = 255; break; }
ex[i] = (u16)hx[h] << 4;
if (ey[i] + 12 < hy[h]) ey[i]++;
else { estate[i] = L_CARRY; hstate[h] = H_HELD; }
enemy_fire(i, 120);
} else {
h = etgt[i];
if (!(last_frame & 1)) ey[i]--;
hx[h] = ex[i] >> 4; hy[h] = ey[i] + 12;
if (ey[i] <= TOP_Y) {
hstate[h] = H_GONE;
etype[i] = E_MUTANT;
say(humans_left() ? "A HUMANOID IS LOST" : "THE PLANET IS LOST");
if (!humans_left())
for (h = 0; h < NE; h++) if (etype[h] == E_LANDER) etype[h] = E_MUTANT;
}
}
break;
case E_MUTANT:
d = dist(x, px);
ex[i] = step16(ex[i], d > 0 ? 22 : -22);
if (ey[i] < py) ey[i]++; else if (ey[i] > py) ey[i]--;
if (!(rnd() & 3)) ey[i] += (rnd() & 1) ? 2 : -2;
if (ey[i] < TOP_Y) ey[i] = TOP_Y;
if (ey[i] > BOT_Y) ey[i] = BOT_Y;
enemy_fire(i, 40);
break;
case E_BOMBER:
ex[i] = step16(ex[i], evx[i]);
ey[i] = ebase[i] + ((last_frame >> 2) & 15);
if (ecool[i]) ecool[i]--;
else {
ecool[i] = 70 + (rnd() & 63);
for (h = 0; h < NM && mlife[h]; h++);
if (h < NM) { mx[h] = x; my[h] = ey[i]; mlife[h] = 400; }
}
break;
}
if (!etype[i]) continue;
x = ex[i] >> 4;
if (!dead && absd(dist(x, px)) < 12 && absd((i16)ey[i] - py) < 8) {
kill_enemy(i);
kill_ship();
continue;
}
pool_put(x, ey[i], etype[i] == E_LANDER ? S_LANDER : etype[i] == E_MUTANT ? S_MUTANT : S_BOMBER, 1);
}
}
static void humans(void)
{
u8 i;
for (i = 0; i < NH; i++) {
switch (hstate[i]) {
case H_WALK:
if (!(last_frame & 7)) {
hx[i] = wrapx((i16)hx[i] + (hdir[i] ? 1 : -1));
if (!(rnd() & 31)) hdir[i] ^= 1;
}
break;
case H_FALL:
if (!(last_frame & 3) && hvy[i] < 4) hvy[i]++;
hy[i] += hvy[i];
if (!dead && absd(dist(hx[i], px)) < 12 && absd((i16)hy[i] - py) < 12) {
hstate[i] = H_WALK; hy[i] = GROUND_Y;
add_score(500); say("HUMANOID SAVED");
break;
}
if (hy[i] >= GROUND_Y) {
hy[i] = GROUND_Y;
if (GROUND_Y - hfrom[i] > 90) { hstate[i] = H_GONE; boom(hx[i], GROUND_Y); say("A HUMANOID FELL"); }
else { hstate[i] = H_WALK; add_score(250); }
}
break;
}
if (hstate[i] != H_GONE) pool_put(hx[i], hy[i], S_HUMAN, 1);
}
}
static void mines_and_booms(void)
{
u8 i;
for (i = 0; i < NM; i++) {
if (!mlife[i]) continue;
mlife[i]--;
if (!dead && absd(dist(mx[i], px)) < 8 && absd((i16)my[i] - py) < 6) {
mlife[i] = 0; boom(mx[i], my[i]); kill_ship(); continue;
}
pool_put(mx[i], my[i], S_MINE, 1);
}
for (i = 0; i < NX; i++) {
if (!xt[i]) continue;
xt[i]--;
pool_put(xx[i], xy[i], dead && xx[i] == px ? S_SHIPBOOM : (xt[i] & 4) ? S_BOOM1 : S_BOOM2, 1);
}
}
static void shots(void)
{
u8 i;
i16 s;
for (i = 0; i < NS; i++) {
if (!slife[i]) continue;
slife[i]--;
sx16[i] = step16(sx16[i], svx[i]);
sy16[i] += svy[i];
if ((sy16[i] >> 4) < TOP_Y || (sy16[i] >> 4) > 236) slife[i] = 0;
if (!dead && slife[i] && absd(dist(sx16[i] >> 4, px)) < 7 && absd((i16)(sy16[i] >> 4) - py) < 5) {
slife[i] = 0; kill_ship();
}
if (!slife[i]) { spr_off(SP_SHOT + i); continue; }
s = dist(cam, sx16[i] >> 4);
spr_at(SP_SHOT + i, s - 8, (i16)(sy16[i] >> 4) - 8, S_SHOT, 1);
}
}
static void lasers(u8 j)
{
u8 i, k;
i16 s;
if ((j & J_FIRE) && !fire_cool && !dead) {
for (i = 0; i < NL && llife[i]; i++);
if (i < NL) {
lx[i] = wrapx((i16)px + (facing ? -14 : 14)); ly[i] = py + 1; ldir[i] = facing; llife[i] = 26;
fire_cool = 7;
}
}
if (fire_cool) fire_cool--;
for (i = 0; i < NL; i++) {
if (!llife[i]) continue;
llife[i]--;
lx[i] = wrapx((i16)lx[i] + (ldir[i] ? -12 : 12));
for (k = 0; k < NE; k++)
if (etype[k] && absd(dist(lx[i], ex[k] >> 4)) < 12 && absd((i16)ey[k] - ly[i]) < 7) {
kill_enemy(k); llife[i] = 0; break;
}
for (k = 0; k < NM && llife[i]; k++)
if (mlife[k] && absd(dist(lx[i], mx[k])) < 10 && absd((i16)my[k] - ly[i]) < 5) {
mlife[k] = 0; boom(mx[k], my[k]); add_score(50); llife[i] = 0;
}
if (!llife[i] || !on_screen(lx[i])) { llife[i] = 0; spr_off(SP_LASER + i); continue; }
s = dist(cam, lx[i]);
spr_at(SP_LASER + i, s - 8, (i16)ly[i] - 8, S_LASER, 1);
}
}
static void smart_bomb(void)
{
u8 i;
if (!bombs || dead) return;
bombs--;
for (i = 0; i < NE; i++) if (etype[i] && on_screen(ex[i] >> 4)) kill_enemy(i);
for (i = 0; i < NM; i++) if (mlife[i] && on_screen(mx[i])) { mlife[i] = 0; boom(mx[i], my[i]); }
for (i = 0; i < NS; i++) { slife[i] = 0; spr_off(SP_SHOT + i); }
say("SMART BOMB");
}
static void hyperspace(void)
{
if (dead) return;
px = rnd() % WORLD;
py = TOP_Y + 10 + (rnd() % (BOT_Y - TOP_Y - 20));
vx = 0;
off = facing ? 208 : 104;
cam = wrapx((i16)px - off);
set_scroll();
draw_all();
inv = 30;
}
static void fly(u8 j)
{
i16 target, want, d;
if (dead) return;
if (j & J_LEFT) facing = 1;
if (j & J_RIGHT) facing = 0;
if (j & (J_LEFT | J_RIGHT)) {
vx += facing ? -5 : 5;
if (vx > 64) vx = 64;
if (vx < -64) vx = -64;
} else if (vx > 0) vx -= vx > 2 ? 2 : vx;
else if (vx < 0) vx += vx < -2 ? 2 : -vx;
if ((j & J_UP) && py > TOP_Y + 4) py -= 2;
if ((j & J_DOWN) && py < BOT_Y) py += 2;
/* move the ship */
d = vx + pxf;
px = wrapx((i16)px + (d >> 4));
pxf = d & 15;
/* the camera keeps the ship a third of the way in from behind, and
swings across when it turns, 4 columns a frame at most */
target = facing ? 208 : 104;
if (off < target) off += off + 3 < target ? 3 : target - off;
else if (off > target) off -= off - 3 > target ? 3 : off - target;
want = (i16)px - off;
d = dist(cam, wrapx(want));
if (d > 4) d = 4;
if (d < -4) d = -4;
scroll_by(d);
off = dist(cam, px);
spr_at(SP_SHIP, off - 8, (i16)py - 8, S_SHIP, ((inv & 4) ? 0 : 1) | (facing ? 2 : 0));
}
/* ------------------------------------------------------------- screens */
static void clear_all(void)
{
gfill = 0;
while (gfill & 1);
all_off();
}
static u8 wait_fire(void)
{
u8 j;
while (joy & (J_FIRE | J_ENTER | J_ESC)) { wait_frame(); drain_keys(); }
for (;;) {
wait_frame(); drain_keys(); rnd();
j = joy;
if (j & J_ESC) return 0;
if (j & (J_FIRE | J_ENTER)) return 1;
}
}
static u8 title(void)
{
clear_all();
puts_("\033[0m\033[2J");
cam = 0;
set_scroll();
draw_all();
spr_at(0, 150, 150, S_SHIP, 1);
spr_at(1, 196, 120, S_LANDER, 1);
spr_at(2, 230, 140, S_MUTANT, 1);
spr_at(3, 110, 120, S_BOMBER, 1);
spr_at(4, 196, 202, S_HUMAN, 1);
ink("1;93"); centre(7, "S T A R W A R D E N");
ink("0;97");
centre(10, "Landers are stealing the humanoids from the planet.");
centre(11, "Shoot them, and catch the humanoids they drop.");
centre(12, "A lander that carries one to the top becomes a mutant.");
centre(15, "LEFT / RIGHT fly UP / DOWN climb and dive SPACE fire");
centre(16, "SHIFT smart bomb ENTER hyperspace P pause ESC give up");
ink("1;96"); centre(29, "PRESS SPACE TO BEGIN");
ink("0");
return wait_fire();
}
static void play(void)
{
u8 j, i;
lives = 3; bombs = 3; score = 0; wave = 1; next_extra = 10000;
for (i = 0; i < NX; i++) xt[i] = 0;
for (i = 0; i < NL; i++) llife[i] = 0;
px = 160; py = 120; vx = 0; pxf = 0; facing = 0; off = 104;
clear_all();
puts_("\033[2J");
cam = wrapx((i16)px - off);
set_scroll();
draw_all();
start_wave();
dead = 0; inv = 60; fire_cool = 0; bomb_held = 1; hyper_held = 1;
msg = 0; say("DEFEND THE HUMANOIDS");
hud();
for (;;) {
wait_frame();
set_scroll();
if (drain_keys()) {
at(1, 60); ink("0;93"); puts_("PAUSED - P TO GO ON\033[K"); ink("0");
for (;;) {
wait_frame();
if (joy & J_ESC) { quit = 1; return; }
if (drain_keys()) break;
}
hud_dirty = 1;
}
j = joy;
if (j & J_ESC) return;
if (j & J_SHIFT) { if (!bomb_held) smart_bomb(); bomb_held = 1; } else bomb_held = 0;
if (j & J_ENTER) { if (!hyper_held) hyperspace(); hyper_held = 1; } else hyper_held = 0;
fly(j);
lasers(j);
enemies();
humans();
mines_and_booms();
shots();
pool_end();
if (inv) inv--;
if (msg_time && !--msg_time) hud_dirty = 1;
if (hud_dirty) hud();
if (!scan_timer--) { scan_timer = 9; scanner(); }
if (dead && !--dead_time) {
if (!--lives) {
hud();
ink("0;44;97");
centre(14, " ");
centre(15, " GAME OVER ");
centre(16, " ");
ink("0");
for (i = 0; i < 150; i++) { wait_frame(); drain_keys(); }
return;
}
dead = 0; inv = 120; vx = 0;
hud_dirty = 1;
}
if (!enemies_left() && !dead) {
i = humans_left();
add_score((u16)i * 100 * (wave < 5 ? wave : 5));
say("WAVE CLEARED");
hud();
for (j = 0; j < 120; j++) { wait_frame(); set_scroll(); drain_keys(); }
wave++;
start_wave();
}
}
}
int main(void)
{
rnd_state = 0x2084 ^ frame_ctr;
last_frame = frame_ctr;
puts_("\033[?3l\033[?25l");
load_shapes();
quit = 0;
while (!quit) {
if (!title()) break;
play();
}
clear_all();
puts_("\033[0m\033[2J\033[H\033[?25h");
while (con_stat & 1) (void)con_data;
}
+953
View File
@@ -0,0 +1,953 @@
:10A2000097420000938282B617430000130383CC69
:10A2100063F8620023A00200938242006FF05FFFA8
:10A22000171300006700830C834700F093F72700A3
:10A23000E38C07FEA300A0F067800000130101FF7C
:10A240002324810023261100130405000345040084
:10A25000631A05008320C10003248100130101015A
:10A26000678000001304140097000000E78000FCE2
:10A270006FF0DFFD130101FE232C8100232A9100E2
:10A28000232E110093840500130400009306A00000
:10A29000130690003377D5029307040093870701D4
:10A2A000B3872700130414001374F40F1307070374
:10A2B000238CE7FE930705003355D502E36CF6FCCB
:10A2C000636E9400631804028320C101032481019A
:10A2D000832441011301010267800000130500037C
:10A2E0009384F4FF97000000E78040F493F4F40FA8
:10A2F0006FF01FFD1304F4FF1374F40F93070401B0
:10A30000B387270003C587FF97000000E78000F2AE
:10A310006FF05FFB130101FF2322910093040500FE
:10A320001305B001232611002324810013840500A6
:10A3300097000000E78080EF1305B005970000004C
:10A34000E780C0EE9305000013850400970000002D
:10A35000E78080F21305B00397000000E78000ED6E
:10A36000130504009305000097000000E780C0F08B
:10A37000032481008320C10083244100130580044D
:10A380001301010117030000670043EA130101FFF5
:10A3900023248100130405001305B00123261100B6
:10A3A00097000000E78080E81305B00597000000E3
:10A3B000E780C0E71305040097000000E78040E84D
:10A3C000032481008320C1001305D006130101017D
:10A3D00017030000670083E5130101FF23248100B8
:10A3E0002326110013840500930700003307F400AF
:10A3F00003470700631A070293D7170093059002DB
:10A40000B385F54093F5F50F97000000E780C0F0A5
:10A4100013050400032481008320C10013010101FE
:10A42000170300006700C3E19387170093F7F70F46
:10A430006FF0DFFBB7E7000003D507D713177500F0
:10A440003345E50013150501135505011357950014
:10A450003345E500131785003345E5001315050165
:10A46000135505012398A7D667800000B7E70000C1
:10A4700003C757D7834680F7E38EE6FE034780F78E
:10A48000A38AE7D667800000130500009306000545
:10A49000834700F093F7170063940700678000007C
:10A4A000034710F01377F70D6304D70093070500F7
:10A4B000138507006FF0DFFDB7E700001307800783
:10A4C00023A6A7D6B7E700002384E7D6B7E70000A6
:10A4D00013071000A389E7D66780000093070500E3
:10A4E00063CC07001307F04F1395070113550501BF
:10A4F000634CF7006780000093870750939707012C
:10A5000093D707416FF0DFFD130505B093170501E1
:10A5100093D707416FF05FFD3385A5409317050181
:10A5200013150501135505411307002893D707019B
:10A53000635AA700938707B0139507011355054188
:10A5400067800000130700D8E35CE5FE938707509F
:10A550006FF09FFE3305B500931705011315050134
:10A560001355054193D7070137570000634805008D
:10A5700037570000634AE50037B7FFFFB387E700AE
:10A5800013950701135505411315050113550501D1
:10A5900067800000130705009307F01363ECE70CD6
:10A5A000B7D700009386872EB386A6009305E04FA9
:10A5B00003C606009387872E9306000063E4A50078
:10A5C00093061500B387D70083C6070093070600DC
:10A5D00063F4C6009387060013F8F70F930706008D
:10A5E0006374D6009387060093F8F70F93773500CE
:10A5F000639A070893067004B306D5029307E00632
:10A600009305F000B3E6F602937785009386260261
:10A61000639A070013750501930570006304050034
:10A620009305E00013538700930700001377F70F9B
:10A630001305000F63E607011306300063FCF80002
:10A64000138605006388F600138647F1133616005B
:10A65000131626002308E0F2A30860F22309F0F2A3
:10A6600093871700A309C0F293F7F70FE394A7FCB1
:10A6700067800000130707EC131707011357070142
:10A680006FF0DFF193050000930600006FF09FF973
:10A69000B7E7000083D7C7EC1307F0136360F70236
:10A6A00013F7F70F93E70720A30AE0F293D7870089
:10A6B000230BF0F2A30B00F267800000938707ECF6
:10A6C0009397070193D707016FF05FFD130101FF17
:10A6D000232481002322910023202101232611001D
:10A6E0001304000037E90000930400140355C9EC7B
:10A6F0003305A40013150501130414001355054177
:10A700001314040197000000E78080DD1354040156
:10A7100097000000E78040E8E31A94FC8320C10022
:10A720000324810083244100032901001301010156
:10A7300067800000B7E700009305050003D5C7EC6C
:10A74000130101FF2326110097000000E78000DDC0
:10A750008320C10013150501135505011335051498
:10A760001301010167800000B7E7000093060000B5
:10A7700013070000938787D7930850003388F600AB
:10A780003386E70003480800034606006374C800E8
:10A7900013F7F60F93861600E39216FFB7E6000054
:10A7A00013161700938686D8B386C6002390A6009A
:10A7B000B7E60000938606D8B386E600B387E700C5
:10A7C000130740012380B6002380E7006780000064
:10A7D000130101FFB7E700009305100013051000F7
:10A7E00023261100A38907D6232481002322910068
:10A7F00097000000E78040B237D500001305C5FE82
:10A8000097000000E780C0B837D50000130545FF6A
:10A8100097000000E780C0A2B7D400001385440071
:10A8200097000000E780C0B637D500001305C500CB
:10A8300037D4000097000000E78080A01305440192
:10A8400097000000E780C0B4B7E7000003D507EC2D
:10A850009305600097000000E78000A21385440084
:10A8600097000000E780C0B237D500001305C5018E
:10A8700097000000E780C09C13054401970000008A
:10A88000E78000B1B7E7000003C577EC930500004F
:10A8900097000000E780409E138544009700000069
:10A8A000E78000AF37D50000130585029700000050
:10A8B000E78000991305440197000000E78040AD50
:10A8C000B7E7000003C567EC9305000097000000A0
:10A8D000E780809A1385440097000000E78040AB32
:10A8E00037D500001305450397000000E780409529
:10A8F0001305440197000000E78080A9B7E7000036
:10A9000003C557EC9305000097000000E780C09650
:10A91000B7E7000003A4C7D6630E0402B7E7000040
:10A9200083C787D66388070237D50000130505045F
:10A9300097000000E780C0A537D500001305850407
:10A9400097000000E780C08F130504009700000007
:10A95000E780008F37D500001305C504970000007D
:10A96000E780008E032481008320C10083244100FE
:10A9700037D5000013050505130101011703000079
:10A98000670003A1B7E700009305050003D5A7EC16
:10A99000130101FF2326110097000000E78000B893
:10A9A0001305052813150501135505411355454099
:10A9B0009307F00463CCA700130515001375F50F7A
:10A9C0008320C10013010101678000001305000509
:10A9D0006FF01FFF37E7000093070000130500002A
:10A9E000130747E01306300093068000B385E700A5
:10A9F00083C505006386C500130515001375F50FA3
:10AA000093871700E394D7FE67800000130101FECF
:10AA10002328210137E90000232C8100232631015E
:10AA2000232E1100232A91002324410123225101C6
:10AA3000232061019309050013040000130909EBA9
:10AA400013070001B307240183C7070063980710A9
:10AA5000930A005037EA0000130B301097000000F3
:10AA6000E780809DB37455030355AAEC93850400D9
:10AA700097000000E78080AA1357F540B347E50030
:10AA8000B387E7409397070193D70741E358FBFC4F
:10AA9000B7E7000013171400938707E9B387E700AF
:10AAA00093944400330989002390970023003901CF
:10AAB000B7E4000097000000E7800098938704E85F
:10AAC0001375F503B3878700130585022380A7005C
:10AAD000B7E70000938747E6B38787002380070026
:10AAE000B7E70000938747E51307F0FFB3878700B8
:10AAF0002380E70097000000E7800094B7E700009C
:10AB0000938747E41375F507B38787001305C503DB
:10AB10002380A70097000000E780009213751500BE
:10AB2000938404E8130780FF630405001307800083
:10AB3000B7E70000938747E3B3878700B3848400B7
:10AB40002380E70003C70400B7E70000938747E2CC
:10AB5000B38787002380E7006F00C0001304140050
:10AB6000E312E4EE8320C101032481018324410127
:10AB7000032901018329C100032A8100832A41009E
:10AB8000032B01001301010267800000130101FD86
:10AB9000B7E7000023248102232E3101232611026E
:10ABA0002322910223202103232C4101232A510136
:10ABB000232861012326710123248101232291018D
:10ABC000138707EB23200700232207002324070015
:10ABD0002326070037E70000232607DC1307C7DC1E
:10ABE000232207002324070037E70000230A07D8A1
:10ABF000930650002308D0F6A30A00F6130747D99E
:10AC0000A3000700130660002308C0F6A30A00F69D
:10AC100023010700130670002308C0F6A30A00F6FC
:10AC2000A3010700130780002308E0F6A30A00F63B
:10AC3000B7E9000003C759EC138407EB3377D70259
:10AC4000930610006316D70837E90000B7EA000042
:10AC500037EA000037EC0000130949E193040000D3
:10AC6000930B000A938ACAE0130B20FD130A4AE0F3
:10AC7000130CCCDF930C800097F0FFFFE780C07BC4
:10AC8000B38774031375F503130929003305F50021
:10AC9000131505011355054197000000E780408416
:10ACA000B387540123806701B387440123800700E1
:10ACB000231FA9FE97F0FFFFE7800078B387840188
:10ACC000137515002380A70093841400E39694FB6A
:10ACD00083C759EC9306C00093872700939717000A
:10ACE0001387070093F7F70F63F4F6001307C0000C
:10ACF0001379F70F930400006396240903C759ECF6
:10AD0000930640009307070063F4E60093074000B2
:10AD10009387F7FF93F4F70F130900006360990816
:10AD200097000000E78040CB6314050293070400FE
:10AD300013070401930610001306200083C50700C3
:10AD40006394D5002380C70093871700E398E7FE3C
:10AD50008320C102032481028324410203290102CA
:10AD60008329C101032A8101832A4101032B0101A7
:10AD7000832BC100032C8100832C410013010103AC
:10AD800067800000130510009384140097000000F2
:10AD9000E78000C893F4F40F6FF01FF6130530003E
:10ADA0001309190097000000E78080C61379F90F96
:10ADB0006FF0DFF6B7E7000003D707ECB7E6000057
:10ADC0003305E50013150501135505012390A7EC84
:10ADD00083D746E76362F50637270000130707713C
:10ADE000B387E70037E70000130101FF239AF6E677
:10ADF000834777EC37D50000130545059387170087
:10AE0000A303F7EC37E70000834767EC2326110024
:10AE1000938717002303F7EC97F0FFFFE780006AA2
:10AE20008320C100B7E7000013071000A389E7D60D
:10AE30001301010167800000B7E70000130710004D
:10AE4000A389E7D667800000130101FF2322910048
:10AE5000B7E40000938704EB23248100232611002C
:10AE6000B387A70083C7070013071000130405006A
:10AE7000938404EB6396E70CB7E70000938747E6FB
:10AE8000B387A70003C7070093072000631EF704DA
:10AE9000B7E70000938747E5B387A70083C607009D
:10AEA0009307700063E2D704B7E70000938747E099
:10AEB000B387D7002380E700B7E7000037E700003B
:10AEC000938747DF1307C7E0B387D7003307D7005F
:10AED0002380070003470700B7E700009387C7DE1A
:10AEE000B387D7002380E7001305600997000000AF
:10AEF000E78080EC37E7000093161400B7E7000006
:10AF0000130707E93307D700938707E8B38787005C
:10AF10000355070083C50700B3848400135545001B
:10AF200097000000E7808084238004008320C10014
:10AF30000324810083244100130101016780000084
:10AF400013073000E392E7FA1305A00F6FF01FFA22
:10AF5000130101FE232C810037E40000130444E4B4
:10AF6000232E1100232A91002328210123263101B9
:10AF7000232441013304A4008347040063860702AD
:10AF80009387F7FF2300F4008320C101032481018C
:10AF900083244101032901018329C100032A81007F
:10AFA00013010102678000009384050093090500E6
:10AFB00097F0FFFFE7804048B7E700001375F503FF
:10AFC00083C747ECB384A40023009400E39E07FAF0
:10AFD000B7E7000013971900938707E9B387E700E5
:10AFE00003D90700135A490013050A0097F0FFFF21
:10AFF000E7808074E30A05F8B7E40000130400005A
:10B00000938444D913074000B307940083C7070013
:10B01000639C070CB7E7000083D5A7EC13050A0073
:10B0200097F0FFFFE780804F37E70000130707E83E
:10B03000930705003305370137E700008345050016
:10B04000034787EC93D6F74033C6D7003307B740A2
:10B050003306D6401358F7403345070193060600E0
:10B0600013160601B3080541135606416354160131
:10B07000938608009396060193D60641E38606F070
:10B0800093975700B3C7D7021317570037E600004E
:10B0900013151400130606DB3306A6002310260141
:10B0A00037E60000130686DA3306A60093954500BE
:10B0B0002310B60037E60000130606DA3306A600B2
:10B0C000B38484003347D7022310F600B7E70000AB
:10B0D000938787D9B387A7002390E7009307A0053C
:10B0E0002380F4006FF05FEA13041400E31EE4F021
:10B0F0006FF09FE9230A00F2834740F393F71700AC
:10B10000E39C07FE130700022308F0F6938717005D
:10B11000A30A00F693F7F70FE398E7FEB7E70000FE
:10B12000238B07D6678000009387F500939707016C
:10B130002308A0F693D707011305E014636CF5000C
:10B140009307F6009397070193D707011305E00FC4
:10B150006376F500A30A00F66780000093F7F50F09
:10B1600093D58540A308F0F693F515002309B0F6B2
:10B170001376F60FA309C0F6230AD0F6A30AE0F669
:10B180006780000037E70000B7E6000083C636ECB2
:10B19000834747ECB3E7D700B7E6000083C626EC49
:10B1A000B3E7D700639A070893071000130101FF64
:10B1B0002302F7EC13074006B7E70000A38CE7E68D
:10B1C000B7E7000083C587ECB7E7000003D5A7EC1D
:10B1D0002326110097F0FFFFE7804059230800F66F
:10B1E000A30A00F6B7E70000238A07D81307500028
:10B1F0002308E0F6A30A00F6938747D9A380070047
:10B20000130760002308E0F6A30A00F62381070075
:10B21000130770002308E0F6A30A00F6A3810700D5
:10B22000930780002308F0F6A30A00F68320C100EC
:10B23000130101016780000067800000130101FE17
:10B24000B7E70000232A91009384050093050500C9
:10B2500003D5C7EC232E1100232C810023282101C4
:10B260002326C10097F0FFFFE780402B131505014F
:10B27000135505019307F5009397070193D707012D
:10B280001307E0146366F70437E90000034479D735
:10B2900093077001630EF402930585FF8326C100B6
:10B2A000938484FF1305940013960401939505017C
:10B2B000130710001356064193D505411375F50F7A
:10B2C0001304140097000000E78040E6A30B89D622
:10B2D0008320C10103248101832441010329010149
:10B2E0001301010267800000130101F8232E1106EB
:10B2F000232C8106232A91062328210723263107A0
:10B30000232441072322510723206107232E71059F
:10B31000232C8105232A91052328A1052326B10585
:10B32000834780F73727000013074708B3C7E700B4
:10B3300037E700002318F7D6834780F737D500009A
:10B3400037E700001305050AA30AF7D697F0FFFFB9
:10B35000E78000EFB7D700009387877E13070000D0
:10B360009306B000230BE0F61386070883C5070099
:10B3700093871700A30BB0F6E39AC7FE13071700D5
:10B380001377F70FE310D7FEB7E70000238A07D63D
:10B39000B7270000938707712319F102B7E7000070
:10B3A000938787DD2328F100B7E7000083C747D7DD
:10B3B0006390071E97000000E78000D437D5000097
:10B3C0001305C50A97F0FFFFE78080E7B7E70000A5
:10B3D000239607EC97F0FFFFE780C02B97F0FFFF65
:10B3E000E780002F930760F9230800F6A308F0F622
:10B3F000230900F6A309F0F6230A00F693071000CC
:10B40000A30AF0F62308F0F6130740FCA308E0F6C1
:10B41000230900F613068007A309C0F6230AF0F6F5
:10B42000A30AF0F6930620002308D0F6930560FEE9
:10B43000A308B0F6230900F69305C0F8A309B0F6F7
:10B44000230AD0F6A30AF0F6930630002308D0F6BC
:10B450009305E006A308B0F6230900F6A309C0F699
:10B46000230AD0F6A30AF0F6930640002308D0F68C
:10B47000A308E0F6230900F61307A0FCA309E0F6F1
:10B4800013075000230AE0F637D50000A30AF0F6B0
:10B490001305850B97F0FFFFE78080EFB7D500001D
:10B4A0009385050C1305700097F0FFFFE78000F30C
:10B4B00037D500001305450197F0FFFFE78040ED09
:10B4C000B7D500009385850D1305A00097F0FFFF09
:10B4D000E780C0F0B7D500009385C5101305B00014
:10B4E00097F0FFFFE78080EFB7D500009385C51385
:10B4F0001305C00097F0FFFFE78040EEB7D50000CE
:10B50000938545171305F00097F0FFFFE78000EDE6
:10B51000B7D500009385051B1305000197F0FFFFC9
:10B52000E780C0EB37D500001305051F97F0FFFF3C
:10B53000E78000E6B7D500001305D0019385851F8D
:10B5400097F0FFFFE78080E9B7D700001385070574
:10B5500097F0FFFFE780C0E3834780F893F7070B7E
:10B56000639C070897F0FFFFE78080F097F0FFFFEC
:10B57000E780C0F197F0FFFFE78000EC834780F899
:10B5800013F7F70F9397870193D7874163D0070885
:10B5900097000000E78040B637D50000130505216D
:10B5A00097F0FFFFE780C0C9834700F093F71700CB
:10B5B000638407006F1010238320C10703248107D1
:10B5C00083244107032901078329C106032A810631
:10B5D000832A4106032B0106832BC105032C810519
:10B5E000832C4105032D0105832DC10413050000A3
:10B5F000130101086780000097F0FFFFE78040E734
:10B6000097F0FFFFE78080E86FF01FF5937707035F
:10B61000E38A07F49307300037E70000A303F7EC51
:10B6200037E700002303F7EC03572103B7E70000D7
:10B6300013091000239007ECB7E7000037E400007F
:10B64000A38227EDB7E70000239AE7E6930784D7A4
:10B6500023820700B7E7000023AC07DAB7E4000055
:10B660009307000A2395F4ECB7ED000093078007D9
:10B670002384FDECB7E70000239F07E6B7E700004F
:10B68000A38E07E6B7E7000037E70000238E07E642
:10B6900093078006231DF7E6232C04D697000000AD
:10B6A000E78080A537D500001305050B97F0FFFF55
:10B6B000E78000B903D5A4EC130585F9131505013E
:10B6C0001355054197F0FFFFE78080E1B7E70000E1
:10B6D0002396A7EC97F0FFFFE780C0FB97F0FFFFF2
:10B6E000E78000FF97F0FFFFE780804AB7E70000A0
:10B6F0001307C003238207ECB7E70000A381E7EC40
:10B70000B7E70000238C07E6B7E70000A38B27E725
:10B7100037D50000B7E70000238B27E71305452244
:10B72000B7E7000023A607D697F0FFFFE78000D910
:10B7300097F0FFFFE780000A930784D72320F102E8
:10B7400097F0FFFFE780C0D297F0FFFFE78080F41B
:10B7500097F0FFFFE78080D3630205089305C003DD
:10B760001305100097F0FFFFE78000BB37D50000FE
:10B770001305C5FE97F0FFFFE78080C137D50000B5
:10B780001305C52397F0FFFFE78080ABB7D7000014
:10B790001385070597F0FFFFE78080BF97F0FFFF55
:10B7A000E78000CD834780F89397870193D787413F
:10B7B00063DA07009307100037E70000230AF7D683
:10B7C0006FF09FBE97F0FFFFE78040CCE30805FCD9
:10B7D000B7E7000013071000A389E7D6834780F876
:10B7E00013F4F70F9397870193D78741E3CE07BAF6
:10B7F0009377040437E9000037EA000063880712F2
:10B8000083477AE763920712B7E6000083C766ECC6
:10B81000638C0710034749EC631807109387F7FF01
:10B82000B7EB0000B7E900002383F6EC938B0BE93C
:10B83000130B0000938909EB930A0001B3073B0146
:10B8400083C707006382070203D50B001355450029
:10B8500097F0FFFFE78040EE630805001375FB0FCC
:10B8600097F0FFFFE780805E130B1B00938B2B008C
:10B87000E3165BFD37EB000037EC0000130BCBDC6D
:10B88000130C0CDE930B0000930A600083570B002F
:10B89000638A070283590C001385090097F0FFFFA4
:10B8A000E78080E9630005028327010123100B0074
:10B8B00013850900B387FB0083C5070097F0FFFFDE
:10B8C000E780C0EA938B1B00130B2B00130C2C009A
:10B8D000E39E5BFBB7E70000238A07D813075000FD
:10B8E0002308E0F6A30A00F6938747D9A380070050
:10B8F000230870F7A30A00F62381070013077000DE
:10B900002308E0F6A30A00F6A3810700930780004E
:10B910002308F0F637D50000A30A00F613054525E5
:10B9200097F0FFFFE78080B993071000A30BFAE6BA
:10B9300093770402B7E900006382070A83C769E7C7
:10B94000034749ECB3E7E7006398070897F0FFFF68
:10B95000E78080AE930700503375F5022395A4EC81
:10B9600097F0FFFFE78040AD9307000A3375F502BB
:10B97000B7E70000239F07E6B7E7000003C7C7E764
:10B98000930780061305C5022384ADEC630407000A
:10B990009307000D03D5A4EC37E70000231DF7E65D
:10B9A0003305F540131505011355054197F0FFFFC9
:10B9B000E78000B3B7E700002396A7EC97F0FFFFFE
:10B9C000E78040CD97F0FFFFE78080D0B7E7000029
:10B9D0001307E001A381E7EC93071000238BF9E63E
:10B9E000834749EC639C072413774400B7E70000C2
:10B9F0006306070013071000238EE7E61377840021
:10BA000063040700238E07E683C9C7E7B7E7000092
:10BA10008396E7E79377C40013970601135707014E
:10BA2000638C075E9307500063860900B707010027
:10BA30009387B7FFB387E7009397070193D7074131
:10BA4000130700046350F75CB7E70000239FE7E6A5
:10BA500093771400638C070083C78DEC1307600293
:10BA60006376F7009387E7FF2384FDEC9377240048
:10BA7000638C070083C78DEC1307500D6366F700D6
:10BA8000938727002384FDECB7EA000037E7000026
:10BA900083C7DAE70355E7E73305F500131A050115
:10BAA00083D7A4EC131505011355054113554540E3
:10BAB0003305F50013150501135A0A011355054105
:10BAC00097F0FFFFE780C0A1137AFA002395A4EC5A
:10BAD000A38E4AE793078006638409009307000D4D
:10BAE000B7E500000397A5E79316070193D6060173
:10BAF0006354F75613073700130630006348F70006
:10BB0000B387D74013960701135606013306D600B4
:10BB1000239DC5E6B7E7000003DAC7ECB7E70000EE
:10BB200083D7A7E73305F5401315050113550541E4
:10BB300097F0FFFFE780C09A9305050013050A0000
:10BB400097F0FFFFE780809D1307C0FF9307050074
:10BB50006354E5009307C0FF13970701135707418C
:10BB60009306400063D4E60093074000939A0701D0
:10BB700093DA0A41930700006356A054139A070111
:10BB8000B7E7000003D5C7EC135A0A0113050514E3
:10BB90003305AA00131505011355054197F0FFFF62
:10BBA000E780009497F0FFFFE780009F93071A005B
:10BBB0009397070193D70741E3C257FD37EA000087
:10BBC0000355CAEC330555011315050113550541FD
:10BBD00097F0FFFFE780C09083D5A4EC2316AAEC72
:10BBE00097F0FFFFE7808093B7E70000239DA7E66B
:10BBF000B7E7000083C737EC03C68DEC130585FF5C
:10BC000093D7270093C7170033373001130686FFF9
:10BC10009315050193F71700131717001316060164
:10BC200033E7E700930600001356064193D505411C
:10BC30001305000097F0FFFFE780404F9377040162
:10BC400037E40000B7E90000130484DB638A0700CF
:10BC5000834749EC03C789E7B3E7E70063840750EC
:10BC600083C789E7638607009387F7FF238CF9E68C
:10BC700037E9000037EA0000B7EA0000B7EB000040
:10BC8000130909DC130A4ADC938ACADB130B100080
:10BC9000938B0BEB83470400638A071A03C70A00E0
:10BCA0009387F7FF2300F40003550A009307C000B1
:10BCB00063060700B7070100938747FF3305F500C8
:10BCC000131505011355054197F0FFFFE7804081EB
:10BCD000B7EC00002324A1002310AA00938C0CE9E8
:10BCE000130C00009309B000130D0001B3077C0191
:10BCF00083C70700638E074683D50C0003258100A8
:10BD000093D5450097F0FFFFE78040819357F540BA
:10BD10003345F5003305F54013150501135505416D
:10BD20001307600063C6A944B7E70000938707E8DC
:10BD3000B307FC008346090083C70700B387D740D9
:10BD40009396070193D6064193D6F640B3C7F60003
:10BD5000B387D7409397070193D70701636AF740EA
:10BD60001375FC0F97F0FFFFE780400E23000400DF
:10BD7000B7E6000037E70000938906DE930CC7DCC6
:10BD8000130C0000130D4000834704006384070A6E
:10BD900083D70C006386070883D709000325810039
:10BDA000938507002326F10097E0FFFFE7800077E7
:10BDB0009357F5403345F5003305F540131505015C
:10BDC000135505419307900063CCA7048327010115
:10BDD000B307FC0083C5070083470900B387F5401C
:10BDE00013950701135505411355F540B347F50069
:10BDF000B387A7409397070193D707016362FD02BA
:10BE00000325C10023900C0097F0FFFFE780009608
:10BE10001305200397F0FFFFE78000FA23000400DA
:10BE2000130C1C009307600093892900938C2C004D
:10BE3000E31CFCF483470400137CFB0F639207347C
:10BE400023000400230880F7A30A00F6130B1B004D
:10BE5000930750001304140013091900130A2A0051
:10BE6000938A1A00E318FBE2B7E70000138707EB99
:10BE7000938C07EB938707EB2322F102B7E70000CF
:10BE8000938747E12324F102B7E7000037E9000078
:10BE90009387C7E02326E1002326F102130709E96F
:10BEA000B7E70000232AE10037EA000037E7000087
:10BEB000B7E90000B7EA000037EB0000B7EB00007D
:10BEC000938747E0130407E8130A4AE6938949E594
:10BED000938A4AE4130B4BE3938B4BE2130909E972
:10BEE00023240100130D2000232EF10083C70C0032
:10BEF000638A074003570900135C4700E38AA707DA
:10BF000093063000E380D712930610006390D73673
:10BF100083450A006394053283050B00130507006F
:10BF200097E0FFFFE7804063B7E7000083C757D77C
:10BF30002310A90093F7F7016392070283C70B0050
:10BF4000232CF10097E0FFFFE780004F832781015A
:10BF50001375F500B387A7002300F40003C7090099
:10BF60009307F00F631AF72637E60000130646E141
:10BF7000930600001308007D930880008327C10109
:10BF8000B387D70083C70700639C07048355060067
:10BF900013050C00232ED102232C0103232AE102D6
:10BFA000232CC10097E0FFFFE78040579355F540F1
:10BFB000B347B500B387B7400328810393970701C0
:10BFC00093D7074103268101032741038326C10339
:10BFD0009308800063D6070113F7F60F1388070054
:10BFE0009386160013062600E39A16F92380E900CB
:10BFF0009305A0050325810097F0FFFFE78080F5FA
:10C000006F00C026130700FCE3C0E7A437E7000079
:10C01000231FF7E66FF0DFA3635ED0001306200056
:10C02000938706006354D600930720003307F74038
:10C030006FF09FA1E38E06A01306E0FF9307200098
:10C0400063C8C600B307E0409397070193D7070181
:10C05000B387E7006FF09FFBE3DEE7AA1307D7FF84
:10C060001306300063C8E700B387F6401396070154
:10C0700013560601B386C640239DD5E66FF09FA9EF
:10C08000139A0701135A0A011305F5FF33054541B9
:10C09000131505011355054197E0FFFFE780404464
:10C0A00097E0FFFFE780404F93071A00939707013F
:10C0B00093D7074137E700000355C7ECE3C267FD9C
:10C0C0006FF0DFAF330B50416FF0DFFE1309190043
:10C0D000E308E9B8B307890083C70700E39807FEC0
:10C0E000B7E7000003CAC7E737050100130525FFBE
:10C0F00063140A001305E00083D7A4EC3305F500B0
:10C10000131505011355054197E0FFFFE780403DFA
:10C11000B7E7000013171900938747DCB387E700E0
:10C1200003C78DEC2390A700B7E70000938707DCD7
:10C13000B307F900130717002380E700B7E70000F3
:10C140009387C7DBB307F9002380470133098900D0
:10C150009307A0012300F90093077000238CF9E6F0
:10C160006FF01FB013090000130740006FF09FF637
:10C17000130C1C00938C2C00E31AACB76FF05FBF5C
:10C180000325810097E0FFFFE780005BE30A05CA13
:10C19000B7E700008325810003D5C7EC97E0FFFFD8
:10C1A000E780C03703460900930585FF9395050195
:10C1B000130686FF1316060113071000930660008E
:10C1C0001356064193D5054113050C0097F0FFFF68
:10C1D000E780C0F56FF09FC78327C101B387E700F1
:10C1E00083C70700638807009307F0FF2380F900E7
:10C1F0006FF01FE0B7E7000013171700938747E1C0
:10C20000B387E70083D5070013050C0097E0FFFF15
:10C21000E780C0309357F5403345F5003305F540CE
:10C22000131505011355054193072000E3C2A7DC50
:10C23000930710002300FA006FF09FDB03C609008C
:10C240006398F5469307700063ECC700B7E70000FA
:10C25000938747E0B387C70003C707006300074021
:10C260009307F0FF23000A002380F90003C60C00A7
:10C27000630A0608035C090093574C00232CF10065
:10C28000B7E7000083C747EC034C0400639C0764D6
:10C2900083D5A4EC03258101232AC10297E0FFFF87
:10C2A000E780C0279357F5403345F5003305F54047
:10C2B00013150501135505419307B00003264103EB
:10C2C00063C2A76283C78DECB307FC409396070156
:10C2D00093D6064193D6F640B3C7F600B387D7404E
:10C2E0009397070193D707019306700063ECF65EFE
:10C2F0000325810097F0FFFFE78040B597F0FFFF2F
:10C30000E78080E883278100938C1C0013092900B3
:10C310009387170093F7F70F2324F1000327810079
:10C320009307000113041400130A1A0093891900DB
:10C33000938A1A00130B1B00938B1B00E318F7BAA8
:10C3400037E40000930744E137E700002324F100BD
:10C35000B7E70000130A47E09389C7E0B7EA000097
:10C3600037E90000130747E09387C7E0B7EB000009
:10C37000938ACADF130949DF130444E1130B000059
:10C38000232CE100232EF100930C20FD938BCBDEB8
:10C39000130D200D130C300083470A0063880756E5
:10C3A000130720006386E75C83470A00638C8701DC
:10C3B00083C50900035504001306500097F0FFFFE2
:10C3C000E78000E8130B1B0093078000130A1A0094
:10C3D0001304240093891900938A1A001309190081
:10C3E000E31CFBFA37EC000037E40000130CCCDC54
:10C3F000130404DE13090000B7EA0000130B7000F9
:10C40000930B5000130A600083570C0063880708E1
:10C410009387F7FF2310FC00832701018359040051
:10C42000B307F90083CC070083C74AEC6392076621
:10C4300083D5A4EC1385090097E0FFFFE780000E89
:10C440009357F5403345F5003305F54013150501C5
:10C4500013550541634EAB6283C78DECB387FC4037
:10C4600013970701135707411357F740B347F700D6
:10C47000B387E7409397070193D7070163EAFB600F
:10C4800093850C001385090023100C0097E0FFFF33
:10C49000E780C02D97F0FFFFE78000CF1309190058
:10C4A000130C2C0013042400E31049F78329010224
:10C4B00037E4000037EA0000130484D813090000B1
:10C4C000130A0AD837EB0000930A500083C709000B
:10C4D0006382070433074901834507009387F7FF09
:10C4E00003474BEC93F7F70F2380F9000355040043
:10C4F0006308070003D7A4EC1306A0006308A70095
:10C5000093F7470013B617001306860097F0FFFF56
:10C51000E78000D31309190093891900130424003C
:10C52000E31659FB37E90000B7EA0000B7EB00005B
:10C5300037EA000037EB0000130949D9938A0ADB78
:10C54000938B0BDA130A8ADA130B8BD993095000F9
:10C55000930CA00C37ED0000834709002324F1025F
:10C560006388070603D50A0083950B0097E0FFFF59
:10C57000E78080FE83560B0003540A002390AA0034
:10C58000130C05003304D4001314040113540401E4
:10C5900023108A00135444001306E4FD1316060109
:10C5A000135606019306000063E8CC00832781023E
:10C5B0009387F7FF93F6F70F83474DEC2300D900DD
:10C5C000639607546392064E230830F7A30A00F6D9
:10C5D0009389190093F9F90F930790001309190033
:10C5E000938A2A00938B2B00130A2A00130B2B002B
:10C5F000E394F9F637E70000834777D737E6000082
:10C60000834666D763E6D754834777D7B7E90000F8
:10C61000A30B07D6230BF6D683C739EC6386070036
:10C620009387F7FFA381F9EC37E70000834787D6AC
:10C6300037E90000638C07529387F7FF93F7F70FF2
:10C640002304F7D66394075293071000A309F9D681
:10C6500097E0FFFFE78000186F00C05137E7000048
:10C6600093161600130747E13307D7000357070057
:10C6700083460400131747002310E90037E7000042
:10C680001307C7E03307C700034707001385C60039
:10C69000635AE500938616002300D40093058007B3
:10C6A0006FF05F952300AA012380B7006FF01FFF92
:10C6B000B7E7000083C757D793F7170063980700C1
:10C6C000834704009387F7FF2300F40003278102C8
:10C6D00093171600B307F700239087010347040060
:10C6E0008327C1029306C700B387C7002380D70002
:10C6F00093072002E3ECE7B6B7E70000938747E033
:10C70000B387C700130730002380E700930720009A
:10C710002380FC0097E0FFFFE780002C6304050402
:10C7200037D500001305050697E0FFFFE78000D925
:10C7300097E0FFFFE780402AE31A05B28327C10094
:10C7400093061000130620001387070183C5070016
:10C750006394D5002380C70093871700E318F7FE82
:10C760006FF0DFB037D50000130545076FF0DFFB32
:10C7700083D5A4EC13050C00232CE10097E0FFFF08
:10C78000E780C0D9032781019305A0FE6354A00070
:10C79000930560011305070097E0FFFFE780C0DB0A
:10C7A0008347040003C78DEC2310A90063FEE70450
:10C7B000938717002300F40097E0FFFFE780C0C7CE
:10C7C000137535006314050297E0FFFFE780C0C6CC
:10C7D000137515001307E0FF63040500130720001D
:10C7E00083470400B387E7002300F4008347040075
:10C7F000130710026360F702930720022300F4007E
:10C80000930580026FF00FFFE378F7FA9387F7FF45
:10C810006FF05FFA1307600DE374F7FE930760FD96
:10C820006FF0DFFD83050B001305070097E0FFFFA6
:10C83000E78080D2B7E7000083C757D703C70B0054
:10C840002310A90093D7270093F7F700B387E700D9
:10C850002300F40083C70A00638807009387F7FF6B
:10C860002380FA006FF09FA0232CF10097E0FFFFD8
:10C87000E78080BC1375F5038327810137E700004B
:10C88000130565049306C7DC2380AA001307C7DCE1
:10C890009305600003D60600631C0602B7E600009D
:10C8A00013961700938606DEB386C6002390860192
:10C8B000832601013307C700B387F60083460400CF
:10C8C0002380D700930700192310F7006FF01F9AF9
:10C8D0009387170093F7F70F93862600E39CB7FA28
:10C8E0006FF0DF9893150C019307100093D5054165
:10C8F0006306F6006304A6011306300003258101D8
:10C9000097F0FFFFE780C0936FF0DF9FB7E700006D
:10C9100083C757D793F77700E39807A883C70A0020
:10C920000355040013071000639607003707010042
:10C930001307F7FF3305E5001315050113550541EE
:10C940002324F10297E0FFFFE78080B92310A400C1
:10C9500097E0FFFFE78040AE1375F501E31605A4ED
:10C960008327810293C717002380FA006FF0DFA3AB
:10C97000B7E7000083C757D793F73700639A0700DC
:10C98000834709006366FC00938717002300F900C2
:10C9900003C70900834709003307F700B7E7000022
:10C9A00083C747EC1377F70F2380E90063960708E6
:10C9B00083D5A4EC035504002324E10297E0FFFF94
:10C9C000E780C0B59357F5403345F5003305F54092
:10C9D00013150501135505411306B0000327810205
:10C9E000634CA60483C78DECB307F7409396070109
:10C9F00093D6064193D6F640B3C7F600B387D74027
:10CA00009397070193D707016368F602930720FD08
:10CA10001305401F23000A002380F90097E0FFFF61
:10CA2000E780803937D500001305052697E0FFFF22
:10CA3000E780C0A86FF05F979307100DE3F6E796C5
:10CA4000B3077B0183C70700238099011307A00563
:10CA5000B307FD406354F702035504009307300009
:10CA60002300FA009305200D97E0FFFFE78000D038
:10CA700037D50000130505276FF05FFB23000A0080
:10CA80001305A00F97E0FFFFE78000336FF0DF9101
:10CA90001306400093850C001385090097E0FFFF03
:10CAA000E780007A6FF09F9F83D5A4EC13554C006C
:10CAB00097E0FFFFE78080A69356F540B347D50087
:10CAC000B387D7409397070193D707419306600038
:10CAD00063C2F60483C78DECB307F4409396070155
:10CAE00093D6064193D6F640B3C7F600B387D74036
:10CAF0009397070193D707019306400063ECF60074
:10CB00002300090097E0FFFFE780006883470900E2
:10CB1000E38C07AAB7E7000003D5C7EC93554C0098
:10CB200097E0FFFFE780809F930585FF93950501C0
:10CB30001307100093067000130684FF93D5054178
:10CB40001385090097E0FFFFE780405E6FF05FA864
:10CB50009386970093F6F60F2308D0F69387170075
:10CB6000A30A00F693F7F70F6FF09FA9834739D711
:10CB7000E39007AE37E70000834727D76386070CAB
:10CB80009387F7FF2309F7D6B7E6000083C746EC83
:10CB90006380073837E70000834797E79387F7FFFD
:10CBA00093F7F70FA30CF7E66394073637E7000017
:10CBB000834777EC9387F7FF93F7F70FA303F7EC1F
:10CBC000639A073297E0FFFFE780C0C037D50000C7
:10CBD0001305052997D0FFFFE780807B37D400003D
:10CBE000930584291305E00097D0FFFFE780007FBD
:10CBF000B7D500009385852B1305F00097D0FFFF74
:10CC0000E780C07D930584291305000197D0FFFFBD
:10CC1000E780C07C37D500001305050597D0FFFFDE
:10CC2000E7800077130460091304F4FF97E0FFFF27
:10CC3000E78000841374F40F97E0FFFFE78000851E
:10CC4000E31404FE6FE04FF6930790002309F7D634
:10CC5000130A200037D40000930A500013050A007D
:10CC60009305100097D0FFFFE780006B130A1A00AE
:10CC700013050428137AFA0F97D0FFFFE780405C72
:10CC8000E31E5AFD37D500001305852897D0FFFF16
:10CC9000E7800070930510021305200097D0FFFF76
:10CCA000E78080671305B00597D0FFFFE780005845
:10CCB000930510021305400097D0FFFFE780C06581
:10CCC0001305B00597D0FFFFE7804056930500039A
:10CCD0001305200097D0FFFFE78000641305D005FF
:10CCE00097D0FFFFE78080549305000313054000B1
:10CCF00097D0FFFFE78040621305D00597D0FFFF74
:10CD0000E780C05237DB000013050B0997D0FFFF07
:10CD1000E780006813040000930B3000130C30060A
:10CD2000930CF009930A800083278101B387870061
:10CD300083C70700638677058327C101130A200094
:10CD4000B387870083C707006376FC0033BAFC0013
:10CD5000130A3A008327810003D5070097E0FFFFFD
:10CD6000E78080C29305050013050A0097D0FFFFF6
:10CD7000E780805A1305E00297D0FFFFE780004B61
:10CD80008327810013041400938727002324F100D4
:10CD9000E31C54F937DA0000B7DA0000130400008E
:10CDA000930B1000130C3006930C3000130A8A0802
:10CDB000938A8A098327C10083C707006384070811
:10CDC00013050A00638A77011307200013850A0000
:10CDD0006394E70013050B0997D0FFFFE780405BE2
:10CDE000B7E70000938707E8B387870083C707008A
:10CDF000130D20006378FC00130DF009333DFD0096
:10CE0000130D3D008327410103D50700135545004D
:10CE100097E0FFFFE78040B79305050013050D007D
:10CE200097D0FFFFE780404F8327C1001305F0062E
:10CE300083C70700639497011305300297D0FFFF63
:10CE4000E780C03E8327C1001304140093871700B6
:10CE50002326F1008327410193872700232AF1002D
:10CE600093070001E318F4F4B7E7000083C747EC29
:10CE70006398070637D500001305C5FE97D0FFFF5E
:10CE8000E7800051B7E7000083C787EC130730063F
:10CE9000130420006378F7001304F0093334F4001E
:10CEA00013043400B7E7000003D5A7EC97E0FFFFB9
:10CEB000E78080AD930505001305040097D0FFFFC0
:10CEC000E7808045B7E7000083C7C7E71305E003A5
:10CED000638407001305C00397D0FFFFE780003588
:10CEE00037D500001305050597D0FFFFE780404ABE
:10CEF0006FF09FC993078007A381F9ECB7E70000A3
:10CF0000239F07E693071000238206ECA309F9D6B6
:10CF10009307000013070000130600018325410258
:10CF2000B385E50083C50500638605009387170078
:10CF300093F7F70F13071700E312C7FE03C746EC7A
:10CF4000B3E7E700638407006FE08FFF97E0FFFF20
:10CF5000E78080A837E90000034759EC93065000AA
:10CF60009307070063F4E6009307500093F7F70F69
:10CF70003305F50293074006130480073305F502D5
:10CF8000131505011355050197E0FFFFE780C0E287
:10CF900037D500001305852D97D0FFFFE78000529D
:10CFA00097E0FFFFE78000831304F4FF97D0FFFFB3
:10CFB000E780004C1374F40F97D0FFFFE780806D7B
:10CFC00097D0FFFFE780804CE31004FE834759ECC5
:10CFD00093871700A302F9EC97E0FFFFE78040BBBF
:10CFE0006FE00FF6834710F06FE00FDC303B393312
:10CFF00000000000535441522057415244454E20F6
:10D0000020200000303B33370000000053434F52D4
:10D0100045200000303B393700000000202020531D
:10D020004849505320000000202020424F4D4253D9
:10D03000200000002020205741564520000000001D
:10D04000303B393600000000202020001B5B4B00E5
:10D0500030000000424F4E555320534849500000C5
:10D06000412048554D414E4F4944204953204C4F93
:10D070005354000054484520504C414E45542049DB
:10D0800053204C4F53540000303B39320000000015
:10D09000303B393500000000303B393400000000DF
:10D0A0001B5B3F336C1B5B3F32356C001B5B306D91
:10D0B0001B5B324A00000000313B393300000000A6
:10D0C000532054204120522020205720412052201C
:10D0D000442045204E0000004C616E646572732050
:10D0E00061726520737465616C696E672074686530
:10D0F0002068756D616E6F6964732066726F6D2054
:10D1000074686520706C616E65742E0053686F6F73
:10D1100074207468656D2C20616E64206361746393
:10D1200068207468652068756D616E6F696473202E
:10D13000746865792064726F702E000041206C6104
:10D140006E646572207468617420636172726965CF
:10D1500073206F6E6520746F2074686520746F7023
:10D16000206265636F6D65732061206D7574616EFB
:10D17000742E00004C454654202F205249474854F5
:10D1800020666C79202020205550202F20444F57B6
:10D190004E20636C696D6220616E642064697665FF
:10D1A00020202020535041434520666972650000CD
:10D1B000534849465420736D61727420626F6D62EA
:10D1C00020202020454E54455220687970657273A6
:10D1D0007061636520202020502070617573652088
:10D1E0002020204553432067697665207570000034
:10D1F000313B393600000000505245535320535004
:10D2000041434520544F20424547494E000000000D
:10D210001B5B306D1B5B324A1B5B481B5B3F32352F
:10D2200068000000444546454E4420544845204887
:10D23000554D414E4F494453000000005041555355
:10D240004544202D205020544F20474F204F4E1B47
:10D250005B4B0000534D41525420424F4D42000061
:10D2600048554D414E4F49442053415645440000D6
:10D27000412048554D414E4F49442046454C4C00B5
:10D280001B5B324B00000000303B333400000000D9
:10D29000303B34343B393700202020202020202010
:10D2A000202020202020202020202020202020207E
:10D2B00020202020202000002020202020202020AE
:10D2C000202047414D45204F564552202020202008
:10D2D00020202020202000005741564520434C4567
:10D2E0004152454400000000ADABABACAEACAAACC3
:10D2F000AAA8A8A9A9A7A8A5A2A5A4A4A3A4A4A3D1
:10D30000A1A4A5A6A6A7A8A9ACADACADADAAA9AA93
:10D31000A7A7A7AAAAABA9A9AAABABABA9A5A3A388
:10D32000A2A3A29F9F9D9D9996989B999A9A9A993C
:10D330009B9EA2A5A9ACAEADAEAFB1AFB0B0AFAC45
:10D34000ABAAABADABABADAFB0AFB1B1AFAFB0AF00
:10D35000AFACACABABACAAA8A6A4A5A8A8A8A6A441
:10D36000A4A5A4A6A6A6A9A8A8A5A5A4A6A8A9AA56
:10D37000ABA9A9AAABAAA6A5A6A7AAAAADAFAEAE0D
:10D38000ADB0B0B1B0B1B3B5B8B8B7B5B5B8B8BB5A
:10D39000BBBBBCB9B8B7B8B9B7B8B5B7B7B6B4B412
:10D3A000B2B3B3B1B2B2B1B4B5B7B7B6B6B6B4B240
:10D3B000B2B5B6B7B5B3B4B5B7B4B4B0ACABABAB4C
:10D3C000AAADAEAFAFADAEABABAFB1B0B3B5B4B568
:10D3D000B5B4B3B2B5B3B5B8BABEBEC0C0BFC0C0B5
:10D3E000BEBDBABBBBB9BBBAB7B9B8B8B6B5B3B2B4
:10D3F000B1AEAEB0AFB0AFB1B3B3B2B3B3B5B9BA0B
:10D40000BABBBCBEBDBABAB9BABDBDBBBAB6B4B27E
:10D41000B2B2B3B3B4B2B1B2B0AFB0AFB0AFB0AEFE
:10D42000AEACA9A5A4A3A1A2A2A19F9F9E9FA1A0CB
:10D430009F9FA1A09E9D9D9C9EA1A1A09F9E9C9E02
:10D440009E9D9C9C989697989B9C9B9C9F9D9C9B2B
:10D450009D9D9B9B999693929190908F8F8E918F8B
:10D460008F90918E8E8D898A8A8E8F9293949493C9
:10D470009494939698999CA0A29FA0A2A2A5A5A6D9
:10D48000A6A8A7A7A8ABABADADACAAACABACADAEF4
:10D49000AFB1B1B2B2B1B3B2B3B3B1B2B1B2B0AD78
:10D4A000ADADACABABABABADAEABA9A7A6A7A5A4DE
:10D4B000A6A5A5A4A29FA09C9B9B9B9A9A9999998B
:10D4C0009B9C9E9D9D9B999B9B9B9A9998999A98B2
:10D4D00098989A999A9A9B98979796999A9A9D9FB5
:10D4E000A0A3A4A4A8A5A6A6A7A39F9F9C9A999A27
:10D4F0009A979697969897989A98969796969697B9
:10D50000959597979595969B9B9D9C9C9F9D9E9B83
:10D510009C999595949493959494949598999C9F9F
:10D52000A2A2A0A1A2A4A4A5A4A5A4A09FA09F9EDE
:10D530009E9D9E9F9F9F9FA2A5A39FA09E9F9D9DF6
:10D540009FA09F9F9EA0A2A3A5A5A7A6A7A5A5A7AC
:10D55000A8A9ABADAEADAEB0B3B2B2B4B2B3B4B4D1
:10D56000B4B4B6B8BABAB9B7B9B6B7B7B5B6B5B64E
:10D57000B4B3B1B2B1B2B1B2B0AFAFB0ADADAFACA8
:10D58000ACAEAEADB0AFADACADABA9A9A7A8A6A6E9
:10D59000A8A9A8A8ABA9A9AAACAAA9A9ABAAA9AAF3
:10D5A000ADAFB0B3B8BAB9BABABBBCC0C3C2C4C5D8
:10D5B000C4C2C4C4C4C4C5C7CACCCCCBCBCBCECEEA
:10D5C000CCCBC9C8C9C8C8C9C7C5C6C1C0C1C3C2F8
:10D5D000C2C3C2C3C7C9C7C7C7C6C6C6C6C3C2C0FF
:10D5E000C1C1C2C3C2C0C1C0BEBEBDBDBBBCBDBD4A
:10D5F000BBBDBEC0C2C2C3C3C4C3C2C2C0BFBAB92E
:10D60000B8B8B7B7B9B7B8B7B7B8B8B8B8B9BBBE94
:10D61000BFBFC1C1BFBFC0C3C6C9CACAC8CACCCCBC
:10D62000CBC9C7C8C7C7C7C6C4C2C3C5C8C8C8C690
:10D63000C4C5C5C6C6C4C2C0C0BFBDBCBEBFBDBEDA
:10D64000C0C0C0C3C5C5C5C4C4C5C8C9C9C9CBC984
:10D65000C8C9CACFD2D3D5D6D6D4D2D3D2D3D2D4B6
:10D66000D4D5D6D7D6D8D8DBDCDCD9DBD9D8D8D737
:10D67000D8D6D4D3D2D1D0D1D1D1CECFCECECCCB9F
:10D68000CDCBCBC8C4C3C0BFC1C4C3C3C1C0BDBD63
:10D69000BABABDBEBCBCBBBAB9B7B4B6B5B4B5B501
:10D6A000B3B3B6B5B6B5B6B3B2B2AFADABAAA7A673
:10D6B000A5A3A4A3A4A3A4A3A0A0A1A1A3A09E9F4B
:10D6C0009E9EA19E9F9EA09E9EA0A1A0A19F9D9C6C
:10D6D0009C9D9E9D9D9E9FA0A3A2A1A1A1A3A3A24C
:10D6E000A2A4A4A3A5A4A1A2A19F9E9E9E9E9E9D2E
:10D6F0009B9C9996959593928F8F8F8E91919393F2
:10D70000939493919191919190929191928F8E8E09
:10D710008F8D8E8D8D8C8E8E8C8B898A8B8B8A8C47
:10D720008D8B8B8988898A8B8A888885868988876A
:10D7300089898A8B89868585858687878A8986886F
:10D74000878784807D7E7C7D7B7C7D7E7D808184DF
:10D75000858588898A88878686858785868788865D
:10D7600085858986878786878686888B8B8A8C8F36
:10D770008F91939293928F8F908E8B8B898A898BC6
:10D780008B8B8A88898A898A8D8A8B8D8C8A8987F6
:10D790008485858382848383868685848688888C35
:10D7A0008E8F9192908F8D8C8A8B8A898A8D908FA3
:10D7B00091908E90908D8C8C8C8D8C8A8A8A8989A0
:10D7C0008989898989898B9092939194949694926E
:10D7D00092939694959A9B9E9FA0A0A0A2A5AAAA78
:10D7E000ADAEACABAAADAEAF0000000000000000D3
:10D7F0000000000000000000000000000000000029
:10D800000000000000000000000000000000000018
:10D810000F000000000000000FF000006600000094
:10D82000BFFFFFF666600000BFFFFFFFFFFFFF00C6
:10D830000CCCCCCCCCCCC0000FF000000000000021
:10D840000F000000000000000000000000000000C9
:10D8500000000000000000000000000000000000C8
:10D8600000000000000000000000000000000000B8
:10D8700000000000000000000000000000000000A8
:10D8800000000BBBBBB000000000BBBBBBBB00007B
:10D89000000AAAAAAAAAA00000AA2AA2AA2AAA0042
:10D8A00000AAAAAAAAAAAA00000AAAAAAAAAA0002A
:10D8B0000000A00AA00A0000000A000AA000A000C0
:10D8C00000A0000AA0000A00000000000000000004
:10D8D0000000000000000000000000000000000048
:10D8E0000000000000000000000000000000000038
:10D8F00000000000000000000000D00000D0000088
:10D9000000000D000D000000000DDDDDDDDD00007C
:10D9100000DDAADDDAADD0000DDDAADDDAADDD0077
:10D9200000DDDDDDDDDDD000000D0D0D0D0D000095
:10D9300000D0D0D0D0D0D000000000000000000007
:10D9400000000000000000000000000000000000D7
:10D9500000000000000000000000000000000000C7
:10D9600000000000000000000000000000000000B7
:10D9700000000000000000000000000000000000A7
:10D9800000000000000000000000CCCCCCCC000067
:10D990000000C444444C00000000C4EEEE4C000003
:10D9A0000000C4EEEE4C00000000C444444C0000F3
:10D9B0000000CCCCCCCC0000000000000000000037
:10D9C0000000000000000000000000000000000057
:10D9D0000000000000000000000000000000000047
:10D9E0000000000000000000000000000000000037
:10D9F0000000000000000000000000000000000027
:10DA00000000000000000000000000000000000016
:10DA1000000000000000000000000009900000006D
:10DA2000000000999F000000000000F9990000002C
:10DA3000000000099000000000000000000000004D
:10DA400000000000000000000000000000000000D6
:10DA500000000000000000000000000000000000C6
:10DA600000000000000000000000000000000000B6
:10DA700000000000000000000000000000000000A6
:10DA80000000000000000000000000000000000096
:10DA900000000000000000000000000FF000000087
:10DAA0000000000FF0000000000000DDDD000000BD
:10DAB00000000D0DD0D000000000000DD0000000CF
:10DAC0000000000CC0000000000000C00C000000BE
:10DAD000000000C00C00000000000000000000007A
:10DAE0000000000000000000000000000000000036
:10DAF0000000000000000000000000000000000026
:10DB00000000000000000000000000000000000015
:10DB10000000000000000000000000000000000005
:10DB20000000000000000000BFFFFFFFFFFFFFFB41
:10DB300000000000000000000000000000000000E5
:10DB400000000000000000000000000000000000D5
:10DB500000000000000000000000000000000000C5
:10DB600000000000000000000000000000000000B5
:10DB700000000000000000000000000000000000A5
:10DB80000000000000000000000000000000000095
:10DB90000000000000000000000000000000000085
:10DBA0000000000F9000000000000009F0000000DD
:10DBB0000000000000000000000000000000000065
:10DBC0000000000000000000000000000000000055
:10DBD0000000000000000000000000000000000045
:10DBE0000000000000000000000000000000000035
:10DBF0000000000000000000000000000000000025
:10DC000000000000099000000000990099900000B9
:10DC1000000000B0B0990000000900BFFF0900003B
:10DC2000000990FFF00000000000BBFFF0B9900079
:10DC3000000090BBFBB90000000099BBBB000000D6
:10DC40000000099009000000000000000000000032
:10DC500000000000000000000000000000000000C4
:10DC600000000000000000000000000000000000B4
:10DC70000000001000000000000000100000000084
:10DC80000010010000000000001109090900000057
:10DC900000000000999900000109090BBB00000079
:10DCA000010090BBB000900011000BBBBB990100BC
:10DCB0000019090B0B0900010000090090099010E0
:10DCC00000010000000010000100000090001000A2
:10DCD0000000101000000000000000000011000013
:10DCE000000000011000000000000B0B000000000D
:10DCF000000000000B0000000000000B000000000E
:10DD0000000B0FF0000F0B000B000000F0FF0B00EA
:10DD10000B000000FF0000000000F0F0F00F0BB05F
:10DD200000FF000F0F0F0F0B00B00F0FFFFFF00BE6
:10DD3000000F000FFFFF000B0B000F00FFFFF000B4
:10DD400000B00000FF0F0B000000B000F00000B0BA
:10DD5000000B0000B0B0000000000B0B0000000042
:08DD60000000B00000B000005B
:040000030000A20057
:00000001FF
+29
View File
@@ -0,0 +1,29 @@
/* The Basys 3 RISC-V computer's I/O, for C programs (see rtl/rv32_system.v).
*
* Port N of the Z-80 machine is the byte at FFFF_FF00 + N, so IO8(0x01) = c
* compiles to one store relative to x0. The 32-bit registers are at
* FFFF_FE00 + N. */
#ifndef BASYS3_H
#define BASYS3_H
#define IO8(n) (*(volatile unsigned char *)(0xFFFFFF00u + (n)))
#define IO32(n) (*(volatile unsigned int *)(0xFFFFFE00u + (n)))
#define IO_MS IO32(0x00) /* milliseconds since reset */
#define IO_US IO32(0x04) /* microseconds since reset */
#define IO_TIMECMP IO32(0x08) /* timer interrupt when IO_US reaches it */
#define IO_BP0 IO32(0x10) /* monitor breakpoint addresses */
#define IO_BP1 IO32(0x14)
#define ROM_TOP 0x0000A000u /* BASIC's code below: the hardware ignores writes */
#define ARENA_BASE 0x0000A000u /* BASIC's program area */
#define GAME_BASE 0x0000A200u /* machine-code programs loaded into it */
#define MC_FREE 0x00012000u /* 12000h-12FFFh: free for machine code */
#define MON_BASE 0x00020000u /* the monitor: NMI entry */
#define MON_TRAP 0x00020004u /* the monitor's trap entry (mtvec) */
/* BASIC's clock, set by DATE, at a fixed place the monitor can read */
#define CLK_SEC (*(volatile unsigned int *)0x13FF8u) /* seconds since 2000 */
#define CLK_MS (*(volatile int *)0x13FFCu) /* IO_MS at CLK_SEC */
#endif
+186
View File
@@ -0,0 +1,186 @@
/* Single-precision maths for BASIC: sinf cosf tanf atanf expf logf sqrtf
floorf. Range reduction by Cody and Waite, minimax polynomials as in the
Cephes library; good to an ulp or two, which BASIC's 6-7 printed digits
don't show. sqrtf and floorf are exact. (newlib's versions are larger and
bring in errno, stdio and malloc.) */
#include <math.h>
#include <stdint.h>
typedef union { float f; uint32_t i; } fbits;
static const float DP1 = 0.78515625f, DP2 = 2.4187564849853515625e-4f,
DP3 = 3.77489497744594108e-8f, FOPI = 1.27323954473516f;
/* x reduced to [-pi/4, pi/4]; returns the octant */
static float reduce(float x, int *oct)
{
int j = (int)(x * FOPI);
float y;
if (j & 1) j++;
y = (float)j;
*oct = j & 7;
return ((x - y * DP1) - y * DP2) - y * DP3;
}
static float sin_poly(float z)
{
float zz = z * z;
return ((-1.9515295891e-4f * zz + 8.3321608736e-3f) * zz - 1.6666654611e-1f) * zz * z + z;
}
static float cos_poly(float z)
{
float zz = z * z;
return ((2.443315711809948e-5f * zz - 1.388731625493765e-3f) * zz
+ 4.166664568298827e-2f) * zz * zz - 0.5f * zz + 1.0f;
}
float sinf(float x)
{
int neg = x < 0, j;
float z, y;
if (neg) x = -x;
z = reduce(x, &j);
if (j > 3) { neg = !neg; j -= 4; }
y = (j == 1 || j == 2) ? cos_poly(z) : sin_poly(z);
return neg ? -y : y;
}
float cosf(float x)
{
int neg = 0, j;
float z, y;
if (x < 0) x = -x;
z = reduce(x, &j);
if (j > 3) { neg = 1; j -= 4; }
if (j > 1) neg = !neg;
y = (j == 1 || j == 2) ? sin_poly(z) : cos_poly(z);
return neg ? -y : y;
}
float tanf(float x)
{
int neg = x < 0, j;
float z, zz, y;
if (neg) x = -x;
z = reduce(x, &j);
zz = z * z;
if (x > 1e-4f)
y = (((((9.38540185543e-3f * zz + 3.11992232697e-3f) * zz + 2.44301354525e-2f) * zz
+ 5.34112807005e-2f) * zz + 1.33387994085e-1f) * zz + 3.33331568548e-1f) * zz * z + z;
else
y = z;
if (j & 2) y = -1.0f / y;
return neg ? -y : y;
}
float atanf(float x)
{
int neg = x < 0;
float y, z;
if (neg) x = -x;
if (x > 2.414213562373095f) { y = 1.5707963267948966f; x = -1.0f / x; }
else if (x > 0.4142135623730950f) { y = 0.7853981633974483f; x = (x - 1.0f) / (x + 1.0f); }
else y = 0.0f;
z = x * x;
y += (((8.05374449538e-2f * z - 1.38776856032e-1f) * z + 1.99777106478e-1f) * z
- 3.33329491539e-1f) * z * x + x;
return neg ? -y : y;
}
/* x * 2^n */
static float scale2(float x, int n)
{
fbits u;
while (n > 127) { x *= 1.7014118e38f; n -= 127; }
while (n < -126) { x *= 1.1754944e-38f; n += 126; }
u.i = (uint32_t)(n + 127) << 23;
return x * u.f;
}
float expf(float x)
{
float z, y;
int n;
if (x > 88.72283905206835f) return HUGE_VALF;
if (x < -103.278929903431851f) return 0.0f;
z = floorf(1.44269504088896341f * x + 0.5f);
x -= z * 0.693359375f;
x -= z * -2.12194440e-4f;
n = (int)z;
z = x * x;
y = (((((1.9875691500e-4f * x + 1.3981999507e-3f) * x + 8.3334519073e-3f) * x
+ 4.1665795894e-2f) * x + 1.6666665459e-1f) * x + 5.0000001201e-1f) * z + x + 1.0f;
return scale2(y, n);
}
float logf(float x)
{
fbits u = { x };
int e;
float y, z;
if (x <= 0.0f) return x == 0.0f ? -HUGE_VALF : (x - x) / (x - x);
if (u.i >= 0x7F800000) return x;
e = 0;
if (u.i < 0x00800000) { u.f *= 16777216.0f; e = -24; } /* subnormal */
e += (int)(u.i >> 23) - 126;
u.i = (u.i & 0x007FFFFF) | 0x3F000000; /* [0.5, 1) */
x = u.f;
if (x < 0.707106781186547524f) { e--; x = x + x - 1.0f; }
else x = x - 1.0f;
z = x * x;
y = ((((((((7.0376836292e-2f * x - 1.1514610310e-1f) * x + 1.1676998740e-1f) * x
- 1.2420140846e-1f) * x + 1.4249322787e-1f) * x - 1.6668057665e-1f) * x
+ 2.0000714765e-1f) * x - 2.4999993993e-1f) * x + 3.3333331174e-1f) * x * z;
y += -2.12194440e-4f * (float)e;
y += -0.5f * z;
z = x + y;
return z + 0.693359375f * (float)e;
}
/* exact: one bit at a time (after fdlibm) */
float sqrtf(float x)
{
fbits u = { x };
int32_t ix = (int32_t)u.i, m, i;
uint32_t q, s, r, t;
if (ix <= 0) return (ix & 0x7FFFFFFF) == 0 ? x : (x - x) / (x - x);
if (ix >= 0x7F800000) return x;
m = ix >> 23;
if (m == 0) { /* subnormal */
for (i = 0; !(ix & 0x00800000); i++) ix <<= 1;
m -= i - 1;
}
m -= 127;
ix = (ix & 0x007FFFFF) | 0x00800000;
if (m & 1) ix += ix;
m >>= 1;
ix += ix;
q = s = 0;
r = 0x01000000;
while (r) {
t = s + r;
if (t <= (uint32_t)ix) { s = t + r; ix -= t; q += r; }
ix += ix;
r >>= 1;
}
if (ix) q += q & 1;
u.i = (q >> 1) + 0x3F000000 + ((uint32_t)m << 23);
return u.f;
}
float floorf(float x)
{
fbits u = { x };
int e = (int)(u.i >> 23 & 0xFF) - 0x7F;
uint32_t m;
if (e >= 23) return x;
if (e >= 0) {
m = 0x007FFFFF >> e;
if (!(u.i & m)) return x;
if (u.i >> 31) u.i += m;
u.i &= ~m;
} else if (!(u.i >> 31)) u.i = 0;
else if (u.i << 1) u.f = -1.0f;
return u.f;
}
+38
View File
@@ -0,0 +1,38 @@
# setjmp/longjmp: ra, sp and s0-s11 (newlib's jmp_buf has room).
.text
.globl setjmp, longjmp
setjmp:
sw ra, 0(a0)
sw sp, 4(a0)
sw s0, 8(a0)
sw s1, 12(a0)
sw s2, 16(a0)
sw s3, 20(a0)
sw s4, 24(a0)
sw s5, 28(a0)
sw s6, 32(a0)
sw s7, 36(a0)
sw s8, 40(a0)
sw s9, 44(a0)
sw s10, 48(a0)
sw s11, 52(a0)
li a0, 0
ret
longjmp:
lw ra, 0(a0)
lw sp, 4(a0)
lw s0, 8(a0)
lw s1, 12(a0)
lw s2, 16(a0)
lw s3, 20(a0)
lw s4, 24(a0)
lw s5, 28(a0)
lw s6, 32(a0)
lw s7, 36(a0)
lw s8, 40(a0)
lw s9, 44(a0)
lw s10, 48(a0)
lw s11, 52(a0)
seqz a0, a1
add a0, a0, a1 # 0 becomes 1
ret
+75
View File
@@ -0,0 +1,75 @@
/* The few string functions the programs use, small rather than fast
(newlib's versions pull in its stdio and malloc). */
#include <stddef.h>
#include <string.h>
void *memcpy(void *d, const void *s, size_t n)
{
unsigned char *p = d;
const unsigned char *q = s;
while (n--) *p++ = *q++;
return d;
}
void *memmove(void *d, const void *s, size_t n)
{
unsigned char *p = d;
const unsigned char *q = s;
if (p < q) while (n--) *p++ = *q++;
else { p += n; q += n; while (n--) *--p = *--q; }
return d;
}
void *memset(void *d, int c, size_t n)
{
unsigned char *p = d;
while (n--) *p++ = (unsigned char)c;
return d;
}
int memcmp(const void *a, const void *b, size_t n)
{
const unsigned char *p = a, *q = b;
for (; n; n--, p++, q++) if (*p != *q) return *p - *q;
return 0;
}
size_t strlen(const char *s)
{
const char *p = s;
while (*p) p++;
return p - s;
}
int strcmp(const char *a, const char *b)
{
while (*a && *a == *b) { a++; b++; }
return (unsigned char)*a - (unsigned char)*b;
}
char *strcpy(char *d, const char *s)
{
char *p = d;
while ((*p++ = *s++));
return d;
}
char *strcat(char *d, const char *s)
{
strcpy(d + strlen(d), s);
return d;
}
int strncmp(const char *a, const char *b, size_t n)
{
for (; n; n--, a++, b++)
if (*a != *b || !*a) return (unsigned char)*a - (unsigned char)*b;
return 0;
}
char *strrchr(const char *s, int c)
{
const char *r = 0;
do if (*s == (char)c) r = s; while (*s++);
return (char *)r;
}
+541
View File
@@ -0,0 +1,541 @@
/* RISC-V assembler for the monitor (included by monitor.c).
*
* The syntax the disassembler prints. A line is
* [line number] [label:] [instruction or directive] [; comment]
* (the line number is there when the source was typed into BASIC). Numbers:
* 123, 0FFH, $FF, 0x1F, 1010B, 'A'; $ alone is the current address.
* Expressions use + - * / & | and parentheses. Registers are X0-X31 or their
* ABI names (ZERO RA SP GP TP T0-T6 S0-S11 FP A0-A7); memory operands are
* offset(register). CSRs by name (MSTATUS, MEPC, CYCLE...) or number.
*
* Instructions: RV32I and M, CSRRW..CSRRCI, ECALL EBREAK MRET MNRET WFI FENCE
* FENCE.I, and the usual short forms: NOP LI LA MV NOT NEG SEQZ SNEZ SLTZ SGTZ
* J JR JAL target RET CALL BEQZ BNEZ BLEZ BGEZ BLTZ BGTZ BGT BLE BGTU BLEU CSRR
* CSRW CSRS CSRC CSRWI CSRSI CSRCI. LI is one instruction for a constant of
* -2048..2047 known by then, else LUI and ADDI; LA is AUIPC and ADDI.
* Directives: ORG, EQU (NAME EQU value), DB/DEFB/DEFM (numbers and "strings"),
* DH (16 bits), DW/DEFW (32 bits), DS/DEFS n, ALIGN n, END. Instructions must
* be on a 4-byte boundary. Labels are up to 8 characters.
*/
#define MAXSYM 200
struct sym { char name[8]; u32 val; u16 line; };
static struct sym syms[MAXSYM];
static u16 nsym;
static u32 a_pc; /* address of the line being assembled */
static u32 a_lo, a_hi; /* lowest and highest byte written */
static u8 a_pass; /* 1: sizes and labels, 2: bytes */
static u8 a_undef; /* the expression used an undefined symbol */
static u8 a_fwd; /* ... or one defined further on */
static u8 a_end; /* END seen */
static u16 a_lnum; /* source line, for forward references */
static const char *a_err; /* this line's error */
static const char *ap; /* parse pointer */
static void a_fail(const char *m) { if (!a_err) a_err = m; }
static void a_sp(void) { while (*ap == ' ' || *ap == '\t') ap++; }
static u8 a_alpha(char c) { return (c >= 'A' && c <= 'Z') || c == '_' || c == '.'; }
static u8 a_alnum(char c) { return a_alpha(c) || (c >= '0' && c <= '9'); }
static void a_emit(u8 b)
{
if (a_pass == 2) {
if (!wr(a_pc, b)) a_fail("CAN'T WRITE THERE");
if (a_pc < a_lo) a_lo = a_pc;
if (a_pc > a_hi) a_hi = a_pc;
}
a_pc++;
}
static void a_emit32(u32 w) { a_emit(w); a_emit(w >> 8); a_emit(w >> 16); a_emit(w >> 24); }
static i16 sym_find(const char *n)
{
u16 i;
for (i = 0; i < nsym; i++)
if (!memcmp(syms[i].name, n, 8)) return i;
return -1;
}
static void sym_def(const char *n, u32 v)
{
i16 i = sym_find(n);
if (a_pass == 2) {
if (i >= 0 && syms[i].val != v) a_fail("PHASE ERROR");
return;
}
if (i >= 0) { a_fail("DUPLICATE LABEL"); return; }
if (nsym == MAXSYM) { a_fail("TOO MANY LABELS"); return; }
memcpy(syms[nsym].name, n, 8);
syms[nsym].val = v;
syms[nsym++].line = a_lnum;
}
/* a name at ap (up to 8 characters kept) */
static void a_name(char *n)
{
u8 k = 0;
memset(n, 0, 8);
while (a_alnum(*ap)) { if (k < 8) n[k++] = *ap; ap++; }
}
/* ---------------------------------------------------------- expressions */
static u32 a_expr(void);
static u32 a_number(void)
{
const char *s = ap;
u32 v = 0;
u8 base = 10, n = 0;
if (s[0] == '0' && s[1] == 'X') { base = 16; s += 2; }
else {
while (a_alnum(s[n])) n++;
if (s[n - 1] == 'H') base = 16;
else if (s[n - 1] == 'B') {
u8 k;
for (k = 0; k < n - 1 && (s[k] == '0' || s[k] == '1'); k++);
if (k == n - 1) base = 2;
}
}
for (; a_alnum(*s); s++) {
char c = *s;
u8 d = c >= '0' && c <= '9' ? c - '0' : c >= 'A' && c <= 'F' ? c - 'A' + 10 : 99;
if ((c == 'H' && base == 16) || (c == 'B' && base == 2 && !a_alnum(s[1]))) { s++; break; }
if (d >= base) { a_fail("BAD NUMBER"); break; }
v = v * base + d;
}
ap = s;
return v;
}
static u32 a_factor(void)
{
u32 v;
a_sp();
if (*ap == '-') { ap++; return -a_factor(); }
if (*ap == '+') { ap++; return a_factor(); }
if (*ap == '(') {
ap++;
v = a_expr();
a_sp();
if (*ap == ')') ap++; else a_fail("MISSING )");
return v;
}
if (*ap == '\'' && ap[1] && ap[2] == '\'') { v = (u8)ap[1]; ap += 3; return v; }
if (*ap == '$') {
ap++;
if ((*ap >= '0' && *ap <= '9') || (*ap >= 'A' && *ap <= 'F')) {
v = 0;
while ((*ap >= '0' && *ap <= '9') || (*ap >= 'A' && *ap <= 'F'))
v = v * 16 + (*ap <= '9' ? *ap - '0' : *ap - 'A' + 10), ap++;
return v;
}
return a_pc;
}
if (*ap >= '0' && *ap <= '9') return a_number();
if (a_alpha(*ap)) {
char n[8];
i16 i;
a_name(n);
i = sym_find(n);
if (i >= 0) {
if (a_lnum && syms[i].line > a_lnum) a_fwd = 1;
return syms[i].val;
}
a_undef = 1;
if (a_pass == 2) a_fail("UNDEFINED LABEL");
return 0;
}
a_fail("BAD EXPRESSION");
return 0;
}
static u32 a_term(void)
{
u32 v = a_factor(), w;
for (;;) {
a_sp();
if (*ap == '*') { ap++; v *= a_factor(); }
else if (*ap == '/') { ap++; w = a_factor(); if (w) v /= w; else a_fail("DIVIDE BY 0"); }
else return v;
}
}
static u32 a_expr(void)
{
u32 v = a_term();
for (;;) {
a_sp();
if (*ap == '+') { ap++; v += a_term(); }
else if (*ap == '-') { ap++; v -= a_term(); }
else if (*ap == '&') { ap++; v &= a_term(); }
else if (*ap == '|') { ap++; v |= a_term(); }
else return v;
}
}
/* ------------------------------------------------------------- operands */
#define MAXOP 3
static char a_ops[MAXOP][40];
static u8 a_nops;
/* the operands at ap, split at commas outside brackets and quotes */
static void a_split(void)
{
a_nops = 0;
a_sp();
while (*ap) {
const char *s = ap;
u8 d = 0, q = 0, len;
while (*ap && (d || q || *ap != ',')) {
if (*ap == '\'' && !q && ap[1] && ap[2] == '\'') ap += 2;
else if (*ap == '"') q = !q;
else if (*ap == '(') d++;
else if (*ap == ')' && d) d--;
ap++;
}
len = ap - s;
while (len && (s[len - 1] == ' ' || s[len - 1] == '\t')) len--;
if (a_nops == MAXOP || len >= sizeof a_ops[0]) { a_fail("TOO MANY OPERANDS"); return; }
memcpy(a_ops[a_nops], s, len);
a_ops[a_nops++][len] = 0;
if (*ap == ',') { ap++; a_sp(); }
}
}
/* a register name, the whole of s; 0xFF if it isn't one */
static u8 a_regname(const char *s)
{
u8 i;
if (s[0] == 'X' && s[1] >= '0' && s[1] <= '9') {
u8 k = s[1] - '0';
if (s[2] >= '0' && s[2] <= '9') { k = k * 10 + s[2] - '0'; if (s[3]) return 0xFF; }
else if (s[2]) return 0xFF;
return k < 32 ? k : 0xFF;
}
if (!strcmp(s, "FP")) return 8;
for (i = 0; i < 32; i++) if (!strcmp(s, regn[i])) return i;
return 0xFF;
}
static u8 a_reg(u8 i)
{
u8 r = i < a_nops ? a_regname(a_ops[i]) : 0xFF;
if (r == 0xFF) a_fail("BAD REGISTER");
return r & 31;
}
static u32 a_val(const char *s)
{
u32 v;
ap = s;
v = a_expr();
a_sp();
if (*ap) a_fail("BAD OPERAND");
return v;
}
static u32 a_imm(u8 i)
{
if (i >= a_nops) { a_fail("MISSING OPERAND"); return 0; }
return a_val(a_ops[i]);
}
/* offset(reg) or (reg); the register number, the offset in *off */
static u8 a_mem(u8 i, i32 *off)
{
char *s, *o;
u8 n, r;
*off = 0;
if (i >= a_nops) { a_fail("MISSING OPERAND"); return 0; }
s = a_ops[i];
n = strlen(s);
if (!n || s[n - 1] != ')' || !(o = strrchr(s, '('))) { a_fail("OFFSET(REGISTER)"); return 0; }
s[n - 1] = 0;
r = a_regname(o + 1);
if (r == 0xFF) { a_fail("BAD REGISTER"); return 0; }
*o = 0;
if (o > s) *off = (i32)a_val(s);
return r;
}
static u16 a_csr(u8 i)
{
const struct csrn *c;
u32 v;
if (i >= a_nops) { a_fail("MISSING OPERAND"); return 0; }
for (c = csrs; c->s; c++) if (!strcmp(a_ops[i], c->s)) return c->n;
v = a_val(a_ops[i]);
if (v > 0xFFF) a_fail("BAD CSR");
return v;
}
/* ------------------------------------------------------------ encoding */
static i32 a_range(i32 v, i32 lo, i32 hi)
{
if (a_pass == 2 && (v < lo || v > hi)) a_fail("VALUE OUT OF RANGE");
return v;
}
static u32 enc_i(u32 base, u8 rd, u8 rs1, i32 imm)
{
imm = a_range(imm, -2048, 2047);
return base | rd << 7 | rs1 << 15 | ((u32)imm & 0xFFF) << 20;
}
static u32 enc_r(u32 base, u8 rd, u8 rs1, u8 rs2) { return base | rd << 7 | rs1 << 15 | rs2 << 20; }
static u32 enc_s(u32 base, u8 rs1, u8 rs2, i32 imm)
{
u32 v = (u32)a_range(imm, -2048, 2047);
return base | (v & 31) << 7 | rs1 << 15 | rs2 << 20 | (v >> 5 & 0x7F) << 25;
}
static u32 enc_b(u32 base, u8 rs1, u8 rs2, u32 target)
{
i32 d = (i32)(target - a_pc);
u32 v;
if (a_pass == 2 && (d & 1)) a_fail("ODD TARGET");
if (a_pass == 2 && (d < -4096 || d > 4094)) a_fail("BRANCH TOO FAR");
v = (u32)d;
return base | rs1 << 15 | rs2 << 20 | (v >> 11 & 1) << 7 | (v >> 1 & 15) << 8 |
(v >> 5 & 0x3F) << 25 | (v >> 12 & 1) << 31;
}
static u32 enc_j(u8 rd, u32 target)
{
i32 d = (i32)(target - a_pc);
u32 v;
if (a_pass == 2 && (d & 1)) a_fail("ODD TARGET");
if (a_pass == 2 && (d < -(1 << 20) || d >= (1 << 20))) a_fail("JUMP TOO FAR");
v = (u32)d;
return 0x6F | rd << 7 | (v >> 12 & 0xFF) << 12 | (v >> 11 & 1) << 20 |
(v >> 1 & 0x3FF) << 21 | (v >> 20 & 1) << 31;
}
static u32 enc_u(u32 base, u8 rd, u32 imm)
{
if (a_pass == 2 && imm > 0xFFFFF) a_fail("VALUE OUT OF RANGE");
return base | rd << 7 | (imm & 0xFFFFF) << 12;
}
/* the LUI/AUIPC part and the ADDI part of a 32-bit value */
static u32 hi20(u32 v) { return (v + 0x800) >> 12 & 0xFFFFF; }
static i32 lo12(u32 v) { return sx(v, 12); }
/* --------------------------------------------------------- instructions */
enum {
F_R, F_I, F_SH, F_LD, F_ST, F_B, F_U, F_JAL, F_JALR, F_CSR, F_CSRI, F_FIX,
P_LI, P_LA, P_MV, P_NOT, P_NEG, P_SEQZ, P_SNEZ, P_SLTZ, P_SGTZ, P_J, P_JR, P_RET,
P_CALL, P_BZ, P_BZS, P_BSW, P_CSRR, P_CSRW, P_CSRWI
};
struct ins { char name[8]; u8 f; u32 base; };
static const struct ins itab[] = {
{ "ADD", F_R, 0x00000033 }, { "SUB", F_R, 0x40000033 }, { "SLL", F_R, 0x00001033 },
{ "SLT", F_R, 0x00002033 }, { "SLTU", F_R, 0x00003033 }, { "XOR", F_R, 0x00004033 },
{ "SRL", F_R, 0x00005033 }, { "SRA", F_R, 0x40005033 }, { "OR", F_R, 0x00006033 },
{ "AND", F_R, 0x00007033 },
{ "MUL", F_R, 0x02000033 }, { "MULH", F_R, 0x02001033 }, { "MULHSU", F_R, 0x02002033 },
{ "MULHU", F_R, 0x02003033 }, { "DIV", F_R, 0x02004033 }, { "DIVU", F_R, 0x02005033 },
{ "REM", F_R, 0x02006033 }, { "REMU", F_R, 0x02007033 },
{ "ADDI", F_I, 0x00000013 }, { "SLTI", F_I, 0x00002013 }, { "SLTIU", F_I, 0x00003013 },
{ "XORI", F_I, 0x00004013 }, { "ORI", F_I, 0x00006013 }, { "ANDI", F_I, 0x00007013 },
{ "SLLI", F_SH, 0x00001013 }, { "SRLI", F_SH, 0x00005013 }, { "SRAI", F_SH, 0x40005013 },
{ "LB", F_LD, 0x00000003 }, { "LH", F_LD, 0x00001003 }, { "LW", F_LD, 0x00002003 },
{ "LBU", F_LD, 0x00004003 }, { "LHU", F_LD, 0x00005003 },
{ "SB", F_ST, 0x00000023 }, { "SH", F_ST, 0x00001023 }, { "SW", F_ST, 0x00002023 },
{ "BEQ", F_B, 0x00000063 }, { "BNE", F_B, 0x00001063 }, { "BLT", F_B, 0x00004063 },
{ "BGE", F_B, 0x00005063 }, { "BLTU", F_B, 0x00006063 }, { "BGEU", F_B, 0x00007063 },
{ "LUI", F_U, 0x00000037 }, { "AUIPC", F_U, 0x00000017 },
{ "JAL", F_JAL, 0 }, { "JALR", F_JALR, 0x00000067 },
{ "CSRRW", F_CSR, 0x00001073 }, { "CSRRS", F_CSR, 0x00002073 }, { "CSRRC", F_CSR, 0x00003073 },
{ "CSRRWI", F_CSRI, 0x00005073 }, { "CSRRSI", F_CSRI, 0x00006073 }, { "CSRRCI", F_CSRI, 0x00007073 },
{ "ECALL", F_FIX, 0x00000073 }, { "EBREAK", F_FIX, 0x00100073 }, { "MRET", F_FIX, 0x30200073 },
{ "MNRET", F_FIX, 0x70200073 }, { "WFI", F_FIX, 0x10500073 }, { "FENCE", F_FIX, 0x0FF0000F },
{ "FENCE.I", F_FIX, 0x0000100F }, { "NOP", F_FIX, 0x00000013 },
{ "LI", P_LI, 0 }, { "LA", P_LA, 0 }, { "MV", P_MV, 0 }, { "NOT", P_NOT, 0 },
{ "NEG", P_NEG, 0 }, { "SEQZ", P_SEQZ, 0 }, { "SNEZ", P_SNEZ, 0 }, { "SLTZ", P_SLTZ, 0 },
{ "SGTZ", P_SGTZ, 0 }, { "J", P_J, 0 }, { "JR", P_JR, 0 }, { "RET", P_RET, 0 },
{ "CALL", P_CALL, 0 },
{ "BEQZ", P_BZ, 0x00000063 }, { "BNEZ", P_BZ, 0x00001063 }, { "BLTZ", P_BZ, 0x00004063 },
{ "BGEZ", P_BZ, 0x00005063 }, { "BGTZ", P_BZS, 0x00004063 }, { "BLEZ", P_BZS, 0x00005063 },
{ "BGT", P_BSW, 0x00004063 }, { "BLE", P_BSW, 0x00005063 }, { "BGTU", P_BSW, 0x00006063 },
{ "BLEU", P_BSW, 0x00007063 },
{ "CSRR", P_CSRR, 0 }, { "CSRW", P_CSRW, 0x00001073 }, { "CSRS", P_CSRW, 0x00002073 },
{ "CSRC", P_CSRW, 0x00003073 }, { "CSRWI", P_CSRWI, 0x00005073 },
{ "CSRSI", P_CSRWI, 0x00006073 }, { "CSRCI", P_CSRWI, 0x00007073 },
};
#define NINS (sizeof itab / sizeof itab[0])
static void a_need(u8 n) { if (a_nops != n) a_fail(a_nops < n ? "MISSING OPERAND" : "TOO MANY OPERANDS"); }
static void a_instr(const struct ins *t)
{
u32 b = t->base, v;
i32 off;
u8 rd, rs;
if (a_pc & 3) { a_fail("NOT ON A WORD: USE ALIGN 4"); return; }
switch (t->f) {
case F_R: a_need(3); a_emit32(enc_r(b, a_reg(0), a_reg(1), a_reg(2))); return;
case F_I: a_need(3); a_emit32(enc_i(b, a_reg(0), a_reg(1), (i32)a_imm(2))); return;
case F_SH:
a_need(3);
v = a_imm(2);
if (a_pass == 2 && v > 31) a_fail("SHIFT OVER 31");
a_emit32(enc_r(b, a_reg(0), a_reg(1), v & 31));
return;
case F_LD: a_need(2); rd = a_reg(0); rs = a_mem(1, &off); a_emit32(enc_i(b, rd, rs, off)); return;
case F_ST: a_need(2); rd = a_reg(0); rs = a_mem(1, &off); a_emit32(enc_s(b, rs, rd, off)); return;
case F_B: a_need(3); a_emit32(enc_b(b, a_reg(0), a_reg(1), a_imm(2))); return;
case F_U: a_need(2); a_emit32(enc_u(b, a_reg(0), a_imm(1))); return;
case F_JAL: /* JAL target (RA) or JAL rd,target */
if (a_nops == 1) { a_emit32(enc_j(1, a_imm(0))); return; }
a_need(2); a_emit32(enc_j(a_reg(0), a_imm(1))); return;
case F_JALR: /* JALR rs / JALR rd,off(rs) / JALR rd,rs,off */
if (a_nops == 1) { a_emit32(enc_i(b, 1, a_reg(0), 0)); return; }
if (a_nops == 2) { rd = a_reg(0); rs = a_mem(1, &off); a_emit32(enc_i(b, rd, rs, off)); return; }
a_need(3); a_emit32(enc_i(b, a_reg(0), a_reg(1), (i32)a_imm(2))); return;
case F_CSR: a_need(3); a_emit32(b | a_reg(0) << 7 | a_reg(2) << 15 | (u32)a_csr(1) << 20); return;
case F_CSRI:
a_need(3);
v = a_imm(2);
if (a_pass == 2 && v > 31) a_fail("VALUE OVER 31");
a_emit32(b | a_reg(0) << 7 | (v & 31) << 15 | (u32)a_csr(1) << 20);
return;
case F_FIX: a_need(0); a_emit32(b); return;
case P_LI:
a_need(2);
rd = a_reg(0);
a_undef = a_fwd = 0;
v = a_imm(1);
if (!a_undef && !a_fwd && (i32)v >= -2048 && (i32)v <= 2047) {
a_emit32(enc_i(0x13, rd, 0, (i32)v));
} else {
a_emit32(enc_u(0x37, rd, hi20(v)));
a_emit32(enc_i(0x13, rd, rd, lo12(v)));
}
return;
case P_LA:
a_need(2);
rd = a_reg(0);
v = a_imm(1) - a_pc;
a_emit32(enc_u(0x17, rd, hi20(v)));
a_emit32(enc_i(0x13, rd, rd, lo12(v)));
return;
case P_MV: a_need(2); a_emit32(enc_i(0x13, a_reg(0), a_reg(1), 0)); return;
case P_NOT: a_need(2); a_emit32(enc_i(0x4013, a_reg(0), a_reg(1), -1)); return;
case P_NEG: a_need(2); a_emit32(enc_r(0x40000033, a_reg(0), 0, a_reg(1))); return;
case P_SEQZ: a_need(2); a_emit32(enc_i(0x3013, a_reg(0), a_reg(1), 1)); return;
case P_SNEZ: a_need(2); a_emit32(enc_r(0x3033, a_reg(0), 0, a_reg(1))); return;
case P_SLTZ: a_need(2); a_emit32(enc_r(0x2033, a_reg(0), a_reg(1), 0)); return;
case P_SGTZ: a_need(2); a_emit32(enc_r(0x2033, a_reg(0), 0, a_reg(1))); return;
case P_J: a_need(1); a_emit32(enc_j(0, a_imm(0))); return;
case P_CALL: a_need(1); a_emit32(enc_j(1, a_imm(0))); return;
case P_JR: a_need(1); a_emit32(enc_i(0x67, 0, a_reg(0), 0)); return;
case P_RET: a_need(0); a_emit32(0x00008067); return;
case P_BZ: a_need(2); a_emit32(enc_b(b, a_reg(0), 0, a_imm(1))); return;
case P_BZS: a_need(2); a_emit32(enc_b(b, 0, a_reg(0), a_imm(1))); return;
case P_BSW: a_need(3); rs = a_reg(0); a_emit32(enc_b(b, a_reg(1), rs, a_imm(2))); return;
case P_CSRR: a_need(2); a_emit32(0x2073 | a_reg(0) << 7 | (u32)a_csr(1) << 20); return;
case P_CSRW: a_need(2); a_emit32(b | a_reg(1) << 15 | (u32)a_csr(0) << 20); return;
case P_CSRWI:
a_need(2);
v = a_imm(1);
if (a_pass == 2 && v > 31) a_fail("VALUE OVER 31");
a_emit32(b | (v & 31) << 15 | (u32)a_csr(0) << 20);
return;
}
}
/* DB/DH/DW items: numbers and (for DB) strings */
static void a_data(u8 size)
{
for (;;) {
a_sp();
if (*ap == '"' && size == 1) {
ap++;
while (*ap && *ap != '"') a_emit(*ap++);
if (*ap) ap++;
} else {
u32 v = a_expr();
u8 k;
for (k = 0; k < size; k++) a_emit(v >> (8 * k));
}
a_sp();
if (*ap != ',') break;
ap++;
}
if (*ap) a_fail("BAD DATA");
}
static u8 is_w(const char *w, const char *s) { return !strncmp(w, s, 8); }
/* Assemble one source line (uppercase except in quotes). */
static void a_line(char *s)
{
char w[8], lab[8];
u8 has_lab = 0, q = 0;
u16 m;
char *c;
a_err = 0;
/* cut the comment */
for (c = s; *c; c++) {
if (*c == '"') q = !q;
else if (*c == '\'' && !q && c[1] && c[2] == '\'') c += 2;
else if (*c == ';' && !q) { *c = 0; break; }
}
ap = s;
a_sp();
if (*ap >= '0' && *ap <= '9') { /* a BASIC line number */
const char *t = ap;
while (*t >= '0' && *t <= '9') t++;
if (*t == ' ' || !*t) ap = t;
}
a_sp();
if (!*ap) return;
if (!a_alpha(*ap)) { a_fail("SYNTAX ERROR"); return; }
a_name(w);
a_sp();
if (*ap == ':') { /* LABEL: */
ap++;
memcpy(lab, w, 8); has_lab = 1;
a_sp();
if (!*ap) { sym_def(lab, a_pc); return; }
if (!a_alpha(*ap)) { a_fail("SYNTAX ERROR"); return; }
a_name(w);
a_sp();
} else if (!strncmp(ap, "EQU", 3) && !a_alnum(ap[3])) {
memcpy(lab, w, 8); has_lab = 1;
memcpy(w, "EQU\0\0\0\0\0", 8);
ap += 3;
}
if (is_w(w, "EQU")) {
u32 v = a_expr();
if (!has_lab) a_fail("EQU NEEDS A NAME");
else if (a_undef && a_pass == 1) a_fail("EQU BEFORE ITS VALUE");
else sym_def(lab, v);
return;
}
if (has_lab) sym_def(lab, a_pc);
if (is_w(w, "ORG")) { a_pc = a_expr(); return; }
if (is_w(w, "END")) { a_end = 1; return; }
if (is_w(w, "DB") || is_w(w, "DEFB") || is_w(w, "DEFM")) { a_data(1); return; }
if (is_w(w, "DH")) { a_data(2); return; }
if (is_w(w, "DW") || is_w(w, "DEFW")) { a_data(4); return; }
if (is_w(w, "DS") || is_w(w, "DEFS")) { a_pc += a_expr(); return; }
if (is_w(w, "ALIGN")) {
u32 n = a_expr();
if (!n || (n & (n - 1))) a_fail("ALIGN TO A POWER OF 2");
else while (a_pc & (n - 1)) a_emit(0);
return;
}
for (m = 0; m < NINS; m++)
if (!strncmp(w, itab[m].name, 8)) break;
if (m == NINS) { a_fail("UNKNOWN INSTRUCTION"); return; }
a_split();
if (a_err) return;
a_instr(&itab[m]);
}
/* Upper-case a source line outside quotes, in place. */
static void a_upper(char *s)
{
u8 q = 0;
for (; *s; s++) {
if (*s == '"') q = !q;
else if (*s == '\'' && !q && s[1] && s[2] == '\'') s += 2;
else if (!q && *s >= 'a' && *s <= 'z') *s -= 32;
if (*s == '\t') *s = ' ';
}
}
+20
View File
@@ -0,0 +1,20 @@
#!/bin/bash
# Compile the monitor and write monitor.mem (its 32 KB bank) and monitor.bin;
# also out/monitor-host, the disassembler and assembler for the PC (tests).
set -e
cd "$(dirname "$0")"
source ../scripts/rv_env.sh
mkdir -p out
CFLAGS="$RV_ARCH -Os -mno-relax -ffunction-sections -fdata-sections -fno-common -Wall -I../include"
${RV_PREFIX}gcc $CFLAGS -c -o out/crt0.o crt0.S
${RV_PREFIX}gcc $CFLAGS -c -o out/monitor.o monitor.c
${RV_PREFIX}gcc $CFLAGS -c -o out/string.o ../lib/string.c
${RV_PREFIX}gcc $CFLAGS -c -o out/setjmp.o ../lib/setjmp.S
${RV_PREFIX}gcc $RV_ARCH -nostdlib -nostartfiles -T monitor.ld -Wl,--no-relax -Wl,--gc-sections \
-Wl,--build-id=none -Wl,-Map,out/monitor.map -o out/monitor.elf \
out/crt0.o out/monitor.o out/string.o out/setjmp.o -lgcc
${RV_PREFIX}objcopy -O binary -j .text -j .data out/monitor.elf monitor.bin
${RV_PREFIX}nm -n out/monitor.elf > out/monitor.sym
python3 ../tools/bin2mem.py monitor.bin monitor.mem 8192
${RV_PREFIX}size out/monitor.elf
cc -O1 -Wall -o out/monitor-host monitor.c
+95
View File
@@ -0,0 +1,95 @@
# Monitor start-up. The monitor has ROM at 20000-27FFF and RAM (data only) at
# 28000-29FFF. It is entered by the NMI (20000h: the Pause key, MONITOR, a
# breakpoint or a single step) or by a trap (20004h, BASIC's mtvec: an illegal
# instruction, a misaligned access, ebreak, ecall, an unhandled interrupt).
# The program's registers go to u (struct regs in monitor.c), the monitor runs,
# and mnret takes the program back to u.pc with its registers as u has them.
.equ U, 0x29F00 # struct regs, below RAM's last 256 bytes
.equ INITED, 0x29FFC # C data is set up once, then kept
.equ U_PC, 128
.equ U_HOW, 132 # 0 NMI, 1 trap
.equ U_CAUSE, 136
.equ U_TVAL, 140
.equ U_STATUS, 144
.section .text.start, "ax"
.globl _start, u
.set u, U
_start:
j nmi_entry # 20000h
j trap_entry # 20004h
.macro SAVE_REGS
sw x1, 4(sp)
.irp n, 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
sw x\n, 4*\n(sp)
.endr
csrr t0, 0x740 # mnscratch: the program's sp
sw t0, 8(sp)
sw zero, 0(sp)
.endm
nmi_entry:
csrw 0x740, sp # mnscratch
li sp, U
SAVE_REGS
csrr t0, 0x741 # mnepc
sw t0, U_PC(sp)
sw zero, U_HOW(sp)
j common
trap_entry:
csrci 0x744, 8 # NMIE off first: no NMI may reuse mnscratch
csrw 0x740, sp
li sp, U
SAVE_REGS
csrr t0, mepc
sw t0, U_PC(sp)
li t0, 1
sw t0, U_HOW(sp)
csrr t0, mcause
sw t0, U_CAUSE(sp)
csrr t0, mtval
sw t0, U_TVAL(sp)
csrr t0, mstatus
sw t0, U_STATUS(sp)
common:
li sp, U # the monitor's stack, below u
li t0, INITED
lw t1, 0(t0)
li t2, 0xA5A5
beq t1, t2, 3f
la a0, __data_start # copy initialised data
la a1, __data_load
la a2, __data_end
1: bgeu a0, a2, 2f
lw t1, 0(a1)
sw t1, 0(a0)
addi a0, a0, 4
addi a1, a1, 4
j 1b
2: la a0, __bss_start # clear the rest
la a2, __bss_end
21: bgeu a0, a2, 22f
sw zero, 0(a0)
addi a0, a0, 4
j 21b
22: sw t2, 0(t0)
3: call mon_main
li sp, U
lw t0, U_PC(sp)
csrw 0x741, t0 # mnepc
lw t0, U_HOW(sp)
beqz t0, 4f
csrr t0, mstatus # from a trap: interrupts back as they were
andi t0, t0, 0x80
beqz t0, 4f
csrsi mstatus, 8
4: lw x1, 4(sp)
.irp n, 3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31
lw x\n, 4*\n(sp)
.endr
lw sp, 8(sp)
.word 0x70200073 # mnret
BIN
View File
Binary file not shown.
+1118
View File
File diff suppressed because it is too large. Load diff
+32
View File
@@ -0,0 +1,32 @@
/* The monitor: ROM 20000-27FFF (writes ignored), RAM 28000-29FFF (data only)
with the saved registers at 29F00h (crt0.S) and the stack below them. */
OUTPUT_ARCH("riscv")
ENTRY(_start)
MEMORY
{
ROM (rx) : ORIGIN = 0x20000, LENGTH = 0x8000
RAM (rw) : ORIGIN = 0x28000, LENGTH = 0x1B00
}
SECTIONS
{
.text : {
KEEP(*(.text.start))
*(.text .text.*)
*(.rodata .rodata.* .srodata .srodata.*)
. = ALIGN(4);
} > ROM
.data : ALIGN(4) {
__data_start = .;
*(.data .data.* .sdata .sdata.*)
. = ALIGN(4);
__data_end = .;
} > RAM AT > ROM
__data_load = LOADADDR(.data);
.bss (NOLOAD) : ALIGN(4) {
__bss_start = .;
*(.sbss .sbss.* .bss .bss.* COMMON)
. = ALIGN(4);
__bss_end = .;
} > RAM
/DISCARD/ : { *(.comment) *(.riscv.attributes) }
}
+8192
View File
File diff suppressed because it is too large. Load diff
+139
View File
@@ -0,0 +1,139 @@
#!/usr/bin/env python3
"""Assemble a test program with the monitor's assembler (out/monitor-host)
and with GNU as, and check the bytes are the same.
python3 test-asm.py
"""
import os
import re
import subprocess
import sys
import tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
# the monitor's syntax; to_gnu() below turns it into GNU as's
SRC = r"""
ORG 12000H
COUNT EQU 10
START: LA A1,MSG ; the message
LI A2,COUNT
LI A3,BIG ; defined further on: LUI and ADDI
LI T0,-2048
LI T1,2047
LOOP: LBU A0,0(A1)
BEQZ A0,DONE
WAIT: LBU T0,-256(ZERO)
ANDI T0,T0,2
BEQZ T0,WAIT
SB A0,-255(ZERO)
ADDI A1,A1,1
J LOOP
DONE: ADDI A2,A2,-1
BNEZ A2,START
MUL A4,A2,A3
DIVU A5,A4,A3
REMU A6,A4,A3
SLLI A7,A6,3
SRAI S2,A7,1
SUB S3,S2,A7
NEG S4,S3
NOT S5,S4
SEQZ S6,S5
SNEZ S7,S5
BGT S6,S7,DONE
BLEU S6,S7,LOOP
BGTZ S6,LOOP
BLEZ S6,LOOP
JAL RA,DONE
JAL DONE
JALR T0
JALR RA,4(T0)
JR T0
CSRR A0,MSTATUS
CSRW MSCRATCH,A0
CSRSI MSTATUS,8
CSRRW A1,0x305,A2
LUI S8,0x12345
AUIPC S9,1
SW S9,-4(SP)
SH S9,2(SP)
LH S10,2(SP)
LHU S11,2(SP)
LW S11,0(SP)
ECALL
EBREAK
MRET
WFI
FENCE
FENCE.I
NOP
RET
MSG: DB "HI",13,10,0
ALIGN 4
TAB: DW START,DONE,0x12345678
DH 1234,0xFFFF
DS 6
DB 'A'
BIG EQU 0x12345
"""
def to_gnu(src):
out = [".text", ".option norelax", ".option norvc", "_start:"]
for line in src.strip().splitlines():
line = line.split(";")[0]
line = line.replace("$", ".")
m = re.match(r"(\w+)\s+EQU\s+(.*)", line.strip())
if m:
out.append(".equ %s, %s" % (m.group(1).lower(), m.group(2).lower()))
continue
if line.strip().startswith("ORG"):
continue
line = re.sub(r'\bDB "(\w+)",', r'.ascii "\1"\n .byte ', line)
line = re.sub(r"\bDB\b", ".byte", line)
line = re.sub(r"\bDH\b", ".half", line)
line = re.sub(r"\bDW\b", ".word", line)
line = re.sub(r"\bDS\s+(\d+)", r".skip \1", line)
line = re.sub(r"\bALIGN (\d+)", r".balign \1, 0", line)
line = re.sub(r"\bJAL DONE\b", "jal ra,DONE", line)
line = re.sub(r"\bJALR T0\b", "jalr ra,0(t0)", line)
line = re.sub(r"\bLI A3,BIG\b", "lui a3,%hi(BIG)\n addi a3,a3,%lo(BIG)", line)
out.append(line.lower().replace("'a'", "'A'").replace('"hi"', '"HI"'))
return "\n".join(out) + "\n"
def main():
pre = subprocess.run(["bash", "-c", "source %s/../scripts/rv_env.sh; echo $RV_PREFIX" % HERE],
capture_output=True, text=True).stdout.strip()
ours = subprocess.run([os.path.join(HERE, "out", "monitor-host"), "asmbin"], input=SRC,
capture_output=True, text=True).stdout.strip()
if not re.fullmatch(r"[0-9A-F]{5} [0-9A-F]+", ours):
print("FAIL the monitor's assembler: " + ours)
sys.exit(1)
start, ours = ours.split()
tmp = tempfile.mkdtemp()
open(os.path.join(tmp, "t.s"), "w").write(to_gnu(SRC))
subprocess.run([pre + "as", "-march=rv32im_zicsr_zifencei", "-mno-relax", "-o", os.path.join(tmp, "t.o"),
os.path.join(tmp, "t.s")], check=True)
subprocess.run([pre + "ld", "-m", "elf32lriscv", "--no-relax", "-e", "0x12000", "-Ttext=0x12000", "-o", os.path.join(tmp, "t.elf"),
os.path.join(tmp, "t.o")], check=True)
subprocess.run([pre + "objcopy", "-O", "binary", os.path.join(tmp, "t.elf"), os.path.join(tmp, "t.bin")],
check=True)
gnu = open(os.path.join(tmp, "t.bin"), "rb").read().hex().upper()
# GNU pads the section's end to a word
if len(gnu) > len(ours) and set(gnu[len(ours):]) <= {"0"}:
gnu = gnu[:len(ours)]
ok = start == "12000" and ours == gnu
if not ok:
for i in range(0, max(len(ours), len(gnu)), 8):
if ours[i:i + 8] != gnu[i:i + 8]:
print("first difference at %05X: monitor %s, GNU %s" % (0x12000 + i // 2, ours[i:i + 8], gnu[i:i + 8]))
break
print("lengths: monitor %d bytes, GNU %d" % (len(ours) // 2, len(gnu) // 2))
print(("PASS" if ok else "FAIL") + " %d bytes from 12000h the same as GNU as" % (len(ours) // 2))
sys.exit(0 if ok else 1)
if __name__ == "__main__":
main()
+191
View File
@@ -0,0 +1,191 @@
#!/usr/bin/env python3
"""Check the monitor's disassembler and assembler (out/monitor-host, built by
build.sh) on random instructions of every kind:
- each word disassembles to the same instruction GNU objdump shows (same
mnemonic, registers and values), and
- the monitor's assembler turns that text back into the same word.
python3 test-disasm.py [count per kind]
"""
import os
import random
import re
import subprocess
import sys
import tempfile
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, os.path.join(HERE, "..", "tools"))
HOST = os.path.join(HERE, "out", "monitor-host")
N = int(sys.argv[1]) if len(sys.argv) > 1 else 300
random.seed(1)
def prefix():
out = subprocess.run(["bash", "-c", "source %s/../scripts/rv_env.sh; echo $RV_PREFIX" % HERE],
capture_output=True, text=True).stdout.strip()
return out
def r():
return random.randrange(32)
def words():
"""Random valid RV32IM / Zicsr / system instructions."""
ws = []
csrs = [0x300, 0x301, 0x304, 0x305, 0x340, 0x341, 0x342, 0x343, 0x344, 0x744, 0xB00,
0xC00, 0xC01, 0xC82, 0xF14, 0x7A0, 0x123]
for _ in range(N):
imm = random.randrange(4096)
ws.append(0x37 | r() << 7 | random.randrange(1 << 20) << 12) # lui
ws.append(0x17 | r() << 7 | random.randrange(1 << 20) << 12) # auipc
j = random.randrange(1 << 20) << 1 # jal
v = j
ws.append(0x6F | r() << 7 | (v >> 12 & 0xFF) << 12 | (v >> 11 & 1) << 20 |
(v >> 1 & 0x3FF) << 21 | (v >> 20 & 1) << 31)
ws.append(0x67 | r() << 7 | r() << 15 | imm << 20) # jalr
b = random.randrange(1 << 12) << 1
f3 = random.choice([0, 1, 4, 5, 6, 7])
ws.append(0x63 | f3 << 12 | r() << 15 | r() << 20 | (b >> 11 & 1) << 7 |
(b >> 1 & 15) << 8 | (b >> 5 & 0x3F) << 25 | (b >> 12 & 1) << 31)
ws.append(0x03 | random.choice([0, 1, 2, 4, 5]) << 12 | r() << 7 | r() << 15 | imm << 20)
s = random.randrange(4096)
ws.append(0x23 | random.choice([0, 1, 2]) << 12 | (s & 31) << 7 | r() << 15 | r() << 20 |
(s >> 5) << 25)
ws.append(0x13 | random.choice([0, 2, 3, 4, 6, 7]) << 12 | r() << 7 | r() << 15 | imm << 20)
ws.append(0x13 | random.choice([(1, 0), (5, 0), (5, 0x20)])[0] << 12 | r() << 7 | r() << 15 |
random.randrange(32) << 20)
f = random.choice([(0, 0), (0, 0x20), (1, 0), (2, 0), (3, 0), (4, 0), (5, 0), (5, 0x20),
(6, 0), (7, 0)] + [(k, 1) for k in range(8)])
ws.append(0x33 | f[0] << 12 | f[1] << 25 | r() << 7 | r() << 15 | r() << 20)
ws.append(0x73 | random.choice([1, 2, 3, 5, 6, 7]) << 12 | r() << 7 | r() << 15 |
random.choice(csrs) << 20)
# the special cases the disassembler shortens
ws.append(0x13 | r() << 7 | 0 << 15 | imm << 20) # li
ws.append(0x13 | r() << 7 | r() << 15) # mv
ws.append(0x00008067) # ret
ws += [0x13, 0x73, 0x00100073, 0x30200073, 0x70200073, 0x10500073, 0x0FF0000F, 0x100F]
return ws
def norm_gnu(text, addr):
"""objdump's operand text -> list of tokens to compare."""
text = re.sub(r"<[^>]*>|#.*", "", text).strip()
m = text.split(None, 1)
mn = m[0].upper()
ops = m[1].split(",") if len(m) > 1 else []
return mn, [o.strip().upper() for o in ops]
def val(tok, base10_ok=True):
tok = tok.strip()
if re.fullmatch(r"-?0X[0-9A-F]+", tok):
return int(tok, 16)
if re.fullmatch(r"-?[0-9A-F]+H", tok):
return int(tok[:-1], 16)
if re.fullmatch(r"-?\d+", tok):
return int(tok)
return None
CSRN = {"MSTATUS": 0x300, "MISA": 0x301, "MIE": 0x304, "MTVEC": 0x305, "MSCRATCH": 0x340,
"MEPC": 0x341, "MCAUSE": 0x342, "MTVAL": 0x343, "MIP": 0x344, "MNSCRATCH": 0x740,
"MNEPC": 0x741, "MNCAUSE": 0x742, "MNSTATUS": 0x744, "TSELECT": 0x7A0, "TDATA1": 0x7A1,
"TDATA2": 0x7A2, "TCONTROL": 0x7A5, "MCYCLE": 0xB00, "MINSTRET": 0xB02, "MCYCLEH": 0xB80,
"MINSTRETH": 0xB82, "CYCLE": 0xC00, "TIME": 0xC01, "INSTRET": 0xC02, "CYCLEH": 0xC80,
"TIMEH": 0xC81, "INSTRETH": 0xC82, "MHARTID": 0xF14}
def same(gnu_mn, gnu_ops, our):
m = our.split(None, 1)
mn = m[0]
ops = [o.strip() for o in m[1].split(",")] if len(m) > 1 else []
# objdump's other spellings of the same instructions
if gnu_mn == "JR" and gnu_ops and "(" in gnu_ops[0]:
gnu_mn, gnu_ops = "JALR", ["ZERO"] + gnu_ops
elif gnu_mn == "JALR" and len(gnu_ops) == 1 and "(" in gnu_ops[0]:
gnu_ops = ["RA"] + gnu_ops
elif gnu_mn.startswith("RD") and gnu_mn[2:] in CSRN:
gnu_mn, gnu_ops = "CSRR", gnu_ops + [gnu_mn[2:]]
gnu_ops = [str(CSRN[o]) if o in CSRN else o for o in gnu_ops]
ops = [str(CSRN[o]) if o in CSRN else o for o in ops]
alias = {"FENCE.I": "FENCE.I", "UNIMP": "DW"}
if alias.get(gnu_mn, gnu_mn) != mn:
return False
# objdump writes "fence iorw,iorw"
if mn == "FENCE":
return True
if len(ops) != len(gnu_ops):
return False
for a, b in zip(gnu_ops, ops):
if a == b:
continue
ma, mb = re.fullmatch(r"(.*)\((\w+)\)", a), re.fullmatch(r"(.*)\((\w+)\)", b)
if ma and mb:
if ma.group(2) != mb.group(2) or val(ma.group(1) or "0") != val(mb.group(1) or "0"):
return False
continue
va, vb = val(a), val(b)
if va is None or vb is None:
return False
if va & 0xFFFFFFFF != vb & 0xFFFFFFFF:
return False
return True
def main():
pre = prefix()
ws = words()
tmp = tempfile.mkdtemp()
with open(os.path.join(tmp, "w.bin"), "wb") as f:
for w in ws:
f.write(w.to_bytes(4, "little"))
dump = subprocess.run([pre + "objdump", "-D", "-b", "binary", "-m", "riscv:rv32",
"-M", "numeric", "--adjust-vma=0x1000", os.path.join(tmp, "w.bin")],
capture_output=True, text=True).stdout
gnu = {}
for line in dump.splitlines():
mm = re.match(r"\s*([0-9a-f]+):\s+([0-9a-f]{8})\s+(.*)", line)
if mm and int(mm.group(1), 16) >= 0x1000:
gnu[int(mm.group(1), 16) - 0x1000] = mm.group(3)
ours = subprocess.run([HOST, "dis"], input="".join("%08X\n" % w for w in ws),
capture_output=True, text=True).stdout.splitlines()
regn = ["ZERO", "RA", "SP", "GP", "TP", "T0", "T1", "T2", "S0", "S1"] + \
["A%d" % i for i in range(8)] + ["S%d" % i for i in range(2, 12)] + ["T3", "T4", "T5", "T6"]
bad_dis = 0
for i, (w, o) in enumerate(zip(ws, ours)):
g = gnu.get(4 * i)
if g is None or w == 0x70200073: # (binutils doesn't know MNRET)
continue
gm, gops = norm_gnu(g, 0x1000)
# objdump -M numeric prints x registers: map to ABI names; and its
# branch/jump targets are relative to this word (we disassemble at 1000h)
gops = [re.sub(r"\bX(\d+)\b", lambda q: regn[int(q.group(1))], t) for t in gops]
if gm in ("J", "JAL", "BEQ", "BNE", "BLT", "BGE", "BLTU", "BGEU", "BEQZ", "BNEZ", "BLTZ",
"BGEZ", "BLEZ", "BGTZ"):
v = val(gops[-1])
if v is not None:
gops[-1] = str((v - 4 * i) & 0xFFFFFFFF)
if not same(gm, gops, o):
bad_dis += 1
if bad_dis <= 15:
print("DIS %08X: objdump '%s', monitor '%s'" % (w, g, o))
# round trip through the monitor's assembler, one instruction at 1000h each
bad_asm = 0
back = subprocess.run([HOST, "asm"], input="".join("ORG 1000H\n%s\n" % o for o in ours),
capture_output=True, text=True).stdout.splitlines()
for w, o, b in zip(ws, ours, back):
want = 0x0FF0000F if (w & 0x707F) == 0x0F else w
if b.startswith("ERROR") or int(b, 16) != want:
bad_asm += 1
if bad_asm <= 15:
print("ASM %08X -> '%s' -> %s" % (w, o, b))
print("%d instructions: %d disassembled differently from objdump, %d assembled back wrong"
% (len(ws), bad_dis, bad_asm))
sys.exit(1 if bad_dis or bad_asm else 0)
if __name__ == "__main__":
main()
+502
View File
@@ -0,0 +1,502 @@
`timescale 1ns / 1ps
// Basys 3 VT220 terminal with ANSI colour, and a RISC-V computer running BASIC.
// SW15 down: keyboard (USB HID port) -> UART TX (USB-UART port),
// UART RX -> 80x30 colour text on VGA
// SW15 up: the terminal is the computer's console
//
// SW1:0 baud rate: 00 115200, 01 9600, 10 921600, 11 3000000 (8N1)
// SW2 local echo
// SW3 received LF also does CR
// SW5:4 default text colour: 00 green, 01 yellow, 10 white, 11 cyan
// SW6 Backspace sends DEL (7F) instead of BS (08)
// SW7 Enter sends CR LF instead of CR
// SW8 smooth scrolling (also turned on by ESC [ ? 4 h)
// SW9 fast smooth scrolling
// SW14 in computer mode, link the PC's UART too: bytes from the PC also go to the
// computer, and the computer's output is copied to the PC
// SW15 computer mode
// BTNC reset BTNU clear screen BTND reset the computer only
// LED7:0 last byte received LED11:8 VT220 LEDs L4..L1 (DECLL)
// LED12 bell LED13 Caps Lock LED14 UART RX activity LED15 keyboard activity
// 7-seg last scan code (left), last byte sent (right); in computer mode, ports 21h/20h
//
// The terminal sends XOFF when its input buffer passes 256 bytes and XON when
// it drains below 32, so smooth scrolling can hold the host back. In computer mode
// it does the same for the computer's input buffer, for text pasted over the UART.
module basys3_terminal #(
parameter FONT_FILE = "font.mem",
parameter MEM_FILE = "basic.mem",
parameter MEM_HI_FILE = "basic-hi.mem",
parameter MON_FILE = "monitor.mem",
parameter CLK_HZ = 100_000_000,
parameter real CPU_MULT = 9.0, // CPU clock = 100 MHz * CPU_MULT / CPU_DIV
parameter real CPU_DIV = 10.0,
parameter CPU_HZ = 90_000_000,
parameter ROM_TOP = 32'h0000_A000 // BASIC's code is below; writes there are ignored
) (
input clk,
input btnC,
input btnU,
input btnD,
input [15:0] sw,
output [15:0] led,
output [6:0] seg,
output dp,
output [3:0] an,
output [3:0] vgaRed,
output [3:0] vgaGreen,
output [3:0] vgaBlue,
output Hsync,
output Vsync,
input RsRx,
output RsTx,
input PS2Clk,
input PS2Data,
output QspiCSn,
inout [3:0] QspiDB,
output [3:0] JA // PmodAMP2: audio in, gain, -, shutdown (low)
);
localparam [7:0] XON = 8'h11, XOFF = 8'h13;
// ---------------------------------------------------------------- reset
reg [1:0] btn_sync;
reg [15:0] por = 16'd0;
reg rst = 1'b1;
always @(posedge clk) begin
btn_sync <= {btn_sync[0], btnC};
if (btn_sync[1]) por <= 16'd0;
else if (!por[15]) por <= por + 1'b1;
rst <= !por[15];
end
reg [15:0] sw_r;
reg [1:0] clr_sync, zrst_sync;
always @(posedge clk) begin
sw_r <= sw;
clr_sync <= {clr_sync[0], btnU};
zrst_sync <= {zrst_sync[0], btnD};
end
wire rv_mode = sw_r[15];
wire rv_link = sw_r[14];
reg [13:0] baud_div;
always @(posedge clk)
case (sw_r[1:0])
2'b00: baud_div <= CLK_HZ / 115200;
2'b01: baud_div <= CLK_HZ / 9600;
2'b10: baud_div <= CLK_HZ / 921600; // 108: 0.5% fast
2'b11: baud_div <= CLK_HZ / 3000000; // 33: 1% fast
endcase
reg [3:0] def_fg;
always @(posedge clk)
case (sw_r[5:4])
2'b00: def_fg <= 4'd10; // bright green
2'b01: def_fg <= 4'd11; // yellow
2'b10: def_fg <= 4'd7; // white
2'b11: def_fg <= 4'd14; // bright cyan
endcase
// ------------------------------------------------------------- keyboard
wire [7:0] scan;
wire scan_valid;
wire [7:0] key;
wire key_valid;
wire caps;
wire cursor_app, keypad_app, lnm;
ps2_rx kbd_rx (
.clk(clk), .rst(rst), .ps2_clk(PS2Clk), .ps2_data(PS2Data),
.code(scan), .valid(scan_valid)
);
wire cad;
ps2_keymap kbd_map (
.clk(clk), .rst(rst), .code(scan), .code_valid(scan_valid),
.bs_del(sw_r[6]), .enter_crlf(sw_r[7] | lnm),
.cursor_app(cursor_app), .keypad_app(keypad_app),
.ascii(key), .ascii_valid(key_valid), .caps(caps), .cad(cad)
);
// keys held down, for the computer's game ports (nothing is held outside computer mode)
wire [255:0] keys_down;
ps2_keystate kbd_state (
.clk(clk), .rst(rst | ~rv_mode), .code(scan), .code_valid(scan_valid), .down(keys_down)
);
// Ctrl-Alt-Del in computer mode restarts the computer, like BTND (held 1 us)
reg [6:0] cad_t = 7'd0;
always @(posedge clk) cad_t <= cad && rv_mode ? 7'd100 : cad_t - (cad_t != 0);
// ----------------------------------------------------------- host input
// Keyboard bytes and terminal replies go to the host: the PC's UART, or in
// computer mode the computer's input FIFO. The keyboard/reply choice sticks until
// that queue is empty, so multi-byte sequences are never interleaved.
// Flow control bytes for the PC go first.
wire [7:0] kq, rq;
wire kq_empty, rq_empty, rq_full;
wire [7:0] resp_byte;
wire resp_wr;
wire tx_busy;
reg tx_start, kq_rd, rq_rd, from_resp;
reg [7:0] tx_byte;
reg flow_pend;
reg [7:0] flow_byte;
fifo #(.W(8), .A(4)) key_fifo (
.clk(clk), .rst(rst), .din(key), .wr(key_valid),
.rd(kq_rd), .dout(kq), .empty(kq_empty), .full(), .count()
);
fifo #(.W(8), .A(4)) resp_fifo (
.clk(clk), .rst(rst), .din(resp_byte), .wr(resp_wr),
.rd(rq_rd), .dout(rq), .empty(rq_empty), .full(rq_full), .count()
);
wire [7:0] h_byte = from_resp ? rq : kq;
wire h_avail = from_resp ? !rq_empty : !kq_empty;
wire pop_pend = kq_rd | rq_rd; // last byte taken pops this clock
// computer input FIFO: keyboard/replies, and the PC's UART when linked
wire [7:0] rx_byte;
wire rx_valid;
wire [7:0] zin;
wire zin_empty, zin_full;
wire [10:0] zin_count;
reg zin_rd;
wire rx_to_z = rv_mode && rv_link && rx_valid;
wire take_z = rv_mode && h_avail && !zin_full && !rx_to_z && !pop_pend;
fifo #(.W(8), .A(10)) rv_in_fifo (
.clk(clk), .rst(rst), .din(rx_to_z ? rx_byte : h_byte), .wr(rx_to_z || take_z),
.rd(zin_rd), .dout(zin), .empty(zin_empty), .full(zin_full), .count(zin_count)
);
// computer output copied to the PC when linked
wire [7:0] mq;
wire mq_empty;
wire [10:0] mq_count;
reg mq_rd;
wire rv_out; // a computer byte is going to the screen
wire [7:0] rv_out_byte;
fifo #(.W(8), .A(10)) mirror_fifo (
.clk(clk), .rst(rst), .din(rv_out_byte), .wr(rv_out && rv_link),
.rd(mq_rd), .dout(mq), .empty(mq_empty), .full(), .count(mq_count)
);
wire [10:0] tq_count;
wire [10:0] flow_count = rv_mode ? zin_count : tq_count;
reg xoff_sent;
always @(posedge clk) begin
tx_start <= 1'b0;
kq_rd <= 1'b0;
rq_rd <= 1'b0;
mq_rd <= 1'b0;
if (rst) begin
from_resp <= 1'b0;
flow_pend <= 1'b0;
xoff_sent <= 1'b0;
end else begin
if (!flow_pend && !xoff_sent && flow_count >= 256) begin
flow_pend <= 1'b1; flow_byte <= XOFF; xoff_sent <= 1'b1;
end else if (!flow_pend && xoff_sent && flow_count < 32) begin
flow_pend <= 1'b1; flow_byte <= XON; xoff_sent <= 1'b0;
end
if (take_z) begin
kq_rd <= !from_resp;
rq_rd <= from_resp;
end else if (!h_avail)
from_resp <= !from_resp;
if (!tx_busy && !tx_start) begin
if (flow_pend) begin
tx_byte <= flow_byte;
tx_start <= 1'b1;
flow_pend <= 1'b0;
end else if (!rv_mode && h_avail && !pop_pend) begin
tx_byte <= h_byte;
tx_start <= 1'b1;
kq_rd <= !from_resp;
rq_rd <= from_resp;
end else if (rv_mode && !mq_empty && !mq_rd) begin
tx_byte <= mq;
tx_start <= 1'b1;
mq_rd <= 1'b1;
end
end
end
end
uart_tx utx (
.clk(clk), .rst(rst), .div(baud_div), .data(tx_byte),
.start(tx_start), .tx(RsTx), .busy(tx_busy)
);
// -------------------------------------------------------------- receive
uart_rx urx (
.clk(clk), .rst(rst), .div(baud_div), .rx(RsRx),
.data(rx_byte), .valid(rx_valid)
);
// 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 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"};
// (not shown in computer mode, where BASIC prints its own)
reg [7:0] ban_i;
wire [7:0] ban_c = BANNER[ban_i*8 +: 8];
// ---------------------------------------------------------------- RISC-V
wire cpu_clk, locked;
`ifdef SIMULATION
assign cpu_clk = clk;
assign locked = 1'b1;
`else
wire cpu_fb, cpu_clk_mmcm;
MMCME2_BASE #(
.CLKIN1_PERIOD(10.0), .DIVCLK_DIVIDE(1),
.CLKFBOUT_MULT_F(CPU_MULT), .CLKOUT0_DIVIDE_F(CPU_DIV)
) cpu_mmcm (
.CLKIN1(clk), .CLKFBIN(cpu_fb), .CLKFBOUT(cpu_fb), .CLKFBOUTB(),
.CLKOUT0(cpu_clk_mmcm), .CLKOUT0B(), .CLKOUT1(), .CLKOUT1B(), .CLKOUT2(),
.CLKOUT2B(), .CLKOUT3(), .CLKOUT3B(), .CLKOUT4(), .CLKOUT5(), .CLKOUT6(),
.LOCKED(locked), .PWRDWN(1'b0), .RST(1'b0)
);
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};
// computer input: one byte at a time from rv_in_fifo
reg zrx_req, zrx_pend;
reg [7:0] zrx_data;
wire zrx_ack;
(* ASYNC_REG = "TRUE" *) reg [1:0] zrx_ack_s;
always @(posedge clk) begin
zin_rd <= 1'b0;
zrx_ack_s <= {zrx_ack_s[0], zrx_ack};
if (rst)
zrx_pend <= 1'b0;
else if (!zrx_pend && !zin_empty && !zin_rd) begin
zrx_data <= zin;
zin_rd <= 1'b1;
zrx_req <= ~zrx_req;
zrx_pend <= 1'b1;
end else if (zrx_pend && zrx_ack_s[1] == zrx_req)
zrx_pend <= 1'b0;
end
initial zrx_req = 1'b0;
// computer output
wire ztx_req;
wire [7:0] ztx_data;
reg ztx_ack = 1'b0;
(* ASYNC_REG = "TRUE" *) reg [1:0] ztx_req_s;
always @(posedge clk) ztx_req_s <= {ztx_req_s[0], ztx_req};
wire [15:0] rv_seg;
wire fl_sck;
wire [16:0] gfx_addr;
wire [3:0] gfx_pix;
wire sp_go, sp_buf, sp_on, pix_frame;
wire [8:0] scroll_x;
wire [7:0] scroll_y;
wire g_border;
wire [7:0] sp_line;
wire [8:0] sp_x;
wire [3:0] sp_pix;
// The Pause key (its scan codes start E1, used by nothing else) enters the
// monitor: a toggle crosses to the CPU clock.
reg brk_t = 1'b0;
always @(posedge clk) if (scan_valid && scan == 8'hE1 && rv_mode) brk_t <= ~brk_t;
(* ASYNC_REG = "TRUE" *) reg [2:0] brk_s = 3'd0;
always @(posedge cpu_clk) brk_s <= {brk_s[1:0], brk_t};
rv32_system #(.MEM_FILE(MEM_FILE), .MEM_HI_FILE(MEM_HI_FILE), .MON_FILE(MON_FILE), .CPU_HZ(CPU_HZ), .ROM_TOP(ROM_TOP)) sys (
.clk(cpu_clk), .rst(cpu_rst_s[2]),
.rx_req(zrx_req), .rx_data(zrx_data), .rx_ack(zrx_ack),
.tx_req(ztx_req), .tx_data(ztx_data), .tx_ack(ztx_ack),
.seg_val(rv_seg), .halted(), .brk_key(brk_s[2] ^ brk_s[1]),
.vclk(pix_clk), .gfx_vaddr(gfx_addr), .gfx_vpix(gfx_pix),
.scroll_x(scroll_x), .scroll_y(scroll_y), .g_border(g_border),
.sp_go(sp_go), .sp_line(sp_line), .sp_buf(sp_buf), .sp_x(sp_x), .sp_on(sp_on),
.sp_pix(sp_pix), .sp_ftick(pix_frame), .keys(keys_down),
.fl_cs_n(QspiCSn), .fl_sck(fl_sck), .fl_mosi(QspiDB[0]), .fl_miso(QspiDB[1]),
.audio(JA[0])
);
// PmodAMP2 on JA's top row: gain high is 6 dB (low would be 12 dB), and
// the amplifier is on unless its shutdown pin is low.
assign JA[3:1] = 3'b101;
// The flash clock is the configuration clock pin, reached through STARTUPE2.
// DQ2/DQ3 (write protect, hold) stay high.
assign QspiDB[1] = 1'bz;
assign QspiDB[3:2] = 2'b11;
`ifndef SIMULATION
STARTUPE2 #(.PROG_USR("FALSE"), .SIM_CCLK_FREQ(0.0)) startup (
.CFGCLK(), .CFGMCLK(), .EOS(), .PREQ(),
.CLK(1'b0), .GSR(1'b0), .GTS(1'b0), .KEYCLEARB(1'b1), .PACK(1'b0),
.USRCCLKO(fl_sck), .USRCCLKTS(1'b0), .USRDONEO(1'b1), .USRDONETS(1'b1)
);
`endif
// terminal input: computer output or received bytes, local echo, banner
reg echo_pend;
reg [7:0] echo_byte;
reg [7:0] tin;
reg tin_wr;
wire tin_full;
assign rv_out = rv_mode && ztx_req_s[1] != ztx_ack && !tin_wr && tq_count < 1000 &&
(!rv_link || mq_count < 1000);
assign rv_out_byte = ztx_data;
always @(posedge clk) begin
tin_wr <= 1'b0;
if (kq_rd && sw_r[2] && !rv_mode) begin
echo_pend <= 1'b1;
echo_byte <= kq;
end
if (rst) begin
echo_pend <= 1'b0;
ban_i <= rv_mode ? 8'hFF : 8'd199;
end else if (rv_mode) begin
if (rv_out) begin
tin <= ztx_data;
tin_wr <= 1'b1;
ztx_ack <= ~ztx_ack;
end
end else if (rx_valid) begin
tin <= rx_byte;
tin_wr <= 1'b1;
end else if (echo_pend && !tin_full) begin
tin <= echo_byte;
tin_wr <= 1'b1;
echo_pend <= kq_rd && sw_r[2] && !rv_mode;
end else if (ban_i != 8'hFF && !tin_full && !tin_wr) begin
ban_i <= ban_i - 1'b1;
if (ban_c != 0) begin
tin <= ban_c;
tin_wr <= 1'b1;
end
end
end
wire [7:0] tq;
wire tq_empty;
wire tq_rd;
fifo #(.W(8), .A(10)) term_fifo (
.clk(clk), .rst(rst), .din(tin), .wr(tin_wr),
.rd(tq_rd), .dout(tq), .empty(tq_empty), .full(tin_full), .count(tq_count)
);
// ------------------------------------------------------------- terminal
wire [13:0] t_addr;
wire alt_scr, wide;
wire t_we;
wire [17:0] t_wdata, t_rdata;
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;
// 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),
.lf_cr(sw_r[3]), .clear_req(clr_sync[1]), .def_fg(def_fg),
.smooth_sw(sw_r[8]), .smooth_fast(sw_r[9]), .frame_tick(frame_tick),
.addr(t_addr), .we(t_we), .wdata(t_wdata), .rdata(t_rdata),
.row(cur_row), .col(cur_col), .cur_vis(cur_vis),
.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), .wide(wide)
);
wire [13:0] v_addr;
wire [17:0] v_data;
// 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 monitor uses it) has no graphics layer.
char_ram screen (
.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(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),
.scroll_x(scroll_x), .scroll_y(scroll_y), .g_border(g_border),
.sp_go(sp_go), .sp_line(sp_line), .sp_buf(sp_buf), .sp_x(sp_x), .sp_on(sp_on),
.sp_pix(alt_scr ? 4'd0 : sp_pix),
.red(vgaRed), .green(vgaGreen), .blue(vgaBlue), .hsync(Hsync), .vsync(Vsync),
.frame_tick(pix_frame)
);
// ----------------------------------------------------- status displays
reg [7:0] last_rx;
reg [7:0] last_scan;
reg [7:0] last_tx;
reg [22:0] rx_led, kb_led, bell_led; // ~84 ms pulse stretchers
always @(posedge clk) begin
if (rv_mode ? tin_wr : rx_valid) last_rx <= rv_mode ? tin : rx_byte;
if (scan_valid) last_scan <= scan;
if (tx_start) last_tx <= tx_byte;
rx_led <= (rv_mode ? tin_wr : rx_valid) ? 23'h7FFFFF : rx_led - (rx_led != 0);
kb_led <= scan_valid ? 23'h7FFFFF : kb_led - (kb_led != 0);
bell_led <= bell ? 23'h7FFFFF : bell_led - (bell_led != 0);
end
assign led = {kb_led != 0, rx_led != 0, caps, bell_led != 0, dec_leds[3:0], last_rx};
assign dp = 1'b1;
sevenseg digits (.clk(clk), .value(rv_mode ? rv_seg : {last_scan, last_tx}),
.seg(seg), .an(an));
endmodule
+38
View File
@@ -0,0 +1,38 @@
`timescale 1ns / 1ps
// Small synchronous first-word-fall-through FIFO (dout is valid while !empty).
module fifo #(
parameter W = 8,
parameter A = 4 // depth = 2**A
) (
input clk,
input rst,
input [W-1:0] din,
input wr,
input rd,
output [W-1:0] dout,
output empty,
output full,
output [A:0] count
);
reg [W-1:0] mem [0:(1<<A)-1];
reg [A:0] wp, rp;
assign empty = wp == rp;
assign full = wp[A] != rp[A] && wp[A-1:0] == rp[A-1:0];
assign dout = mem[rp[A-1:0]];
assign count = wp - rp;
always @(posedge clk)
if (wr && !full) mem[wp[A-1:0]] <= din;
always @(posedge clk) begin
if (rst) begin
wp <= 0;
rp <= 0;
end else begin
if (wr && !full) wp <= wp + 1'b1;
if (rd && !empty) rp <= rp + 1'b1;
end
end
endmodule
+100
View File
@@ -0,0 +1,100 @@
`timescale 1ns / 1ps
// SPI port to the configuration flash for the Z-80, with write protection for
// the bitstream.
//
// port 40h out: bit 0 = 1 opens a command, 0 ends it (chip select high)
// in: bit 0 = byte in progress, bit 1 = command refused
// port 41h out: send a byte (the reply byte is read back from 41h)
//
// The first byte of a command is checked before the chip is selected. Only
// read (03), read status (05), write enable/disable (06/04), read ID (9F), page
// program (02) and 4 KB sector erase (20) are allowed. Program and erase are
// abandoned after the opcode, before any address byte reaches the flash,
// unless the address is at or above PROTECT (a 64 KB boundary), so the
// bitstream below it can never be changed by the Z-80.
//
// SCK is clk/4, SPI mode 0.
module flash_spi #(
parameter [7:0] PROTECT_HI = 8'h24 // first writable address = PROTECT_HI << 16
) (
input clk,
input rst,
input wr_ctl, // port 40h written
input wr_dat, // port 41h written
input [7:0] wdata,
output [7:0] ctl_q, // port 40h read value
output [7:0] dat_q, // port 41h read value
output reg cs_n,
output reg sck,
output mosi,
input miso
);
reg [7:0] sh;
reg [3:0] nbits;
reg [1:0] ph;
reg busy, rbit;
reg open_, refused, first, guarded;
reg [1:0] nbytes;
assign mosi = sh[7];
assign ctl_q = {6'd0, refused, busy};
assign dat_q = sh;
(* IOB = "TRUE" *) reg miso_r;
always @(posedge clk) miso_r <= miso;
task start(input [7:0] b, input [3:0] n);
begin sh <= b; nbits <= n; ph <= 2'd0; busy <= 1'b1; end
endtask
always @(posedge clk)
if (rst) begin
cs_n <= 1'b1; sck <= 1'b0; open_ <= 1'b0; refused <= 1'b0;
start(8'hFF, 4'd8); // a few clocks so STARTUPE2 hands over CCLK
end else begin
if (busy) begin
ph <= ph + 1'b1;
case (ph)
2'd1: sck <= 1'b1;
2'd2: rbit <= miso_r;
2'd3: begin
sck <= 1'b0;
sh <= {sh[6:0], rbit};
nbits <= nbits - 1'b1;
if (nbits == 4'd1) busy <= 1'b0;
end
default: ;
endcase
end
if (wr_ctl && !busy) begin
open_ <= wdata[0];
first <= 1'b1;
nbytes <= 2'd0;
refused <= 1'b0;
if (!wdata[0]) cs_n <= 1'b1;
end else if (wr_dat && !busy && open_) begin
if (nbytes != 2'd3) nbytes <= nbytes + 1'b1;
if (refused)
sh <= 8'hFF;
else if (first) begin
first <= 1'b0;
guarded <= wdata == 8'h02 || wdata == 8'h20;
if (wdata == 8'h03 || wdata == 8'h05 || wdata == 8'h06 || wdata == 8'h04 ||
wdata == 8'h9F || wdata == 8'h02 || wdata == 8'h20) begin
cs_n <= 1'b0;
start(wdata, 4'd8);
end else begin
refused <= 1'b1;
sh <= 8'hFF;
end
end else if (nbytes == 2'd1 && guarded && wdata < PROTECT_HI) begin
refused <= 1'b1; // incomplete command: the flash ignores it
cs_n <= 1'b1;
sh <= 8'hFF;
end else
start(wdata, 4'd8);
end
end
endmodule
+268
View File
@@ -0,0 +1,268 @@
`timescale 1ns / 1ps
// USB keyboard input. The Basys 3's PIC24 USB host turns a USB keyboard into a
// PS/2 device (scan code set 2) on PS2Clk/PS2Data; this decodes that to ASCII.
module ps2_rx (
input clk,
input rst,
input ps2_clk,
input ps2_data,
output reg [7:0] code,
output reg valid
);
reg [2:0] cs = 3'b111, ds = 3'b111;
reg [5:0] fcnt;
reg fclk = 1'b1, fclk_d = 1'b1;
reg [10:0] sh;
reg [3:0] n;
reg [17:0] idle;
always @(posedge clk) begin
cs <= {cs[1:0], ps2_clk};
ds <= {ds[1:0], ps2_data};
// the clock only changes after 64 stable samples, which filters glitches
if (cs[2] == fclk)
fcnt <= 6'd0;
else begin
fcnt <= fcnt + 1'b1;
if (&fcnt) fclk <= cs[2];
end
fclk_d <= fclk;
end
wire fall = fclk_d & ~fclk;
wire [10:0] frame = {ds[2], sh[10:1]};
always @(posedge clk) begin
valid <= 1'b0;
if (rst) begin
n <= 4'd0;
idle <= 18'd0;
end else if (fall) begin
sh <= frame;
idle <= 18'd0;
if (n == 4'd10) begin
n <= 4'd0;
// start 0, stop 1, odd parity over data + parity bit
if (!frame[0] && frame[10] && ^frame[9:1]) begin
code <= frame[8:1];
valid <= 1'b1;
end
end else
n <= n + 1'b1;
end else if (n != 0) begin
idle <= idle + 1'b1;
if (&idle) n <= 4'd0; // ~2.6 ms without a clock: resync
end
end
endmodule
// Turns scan codes into ASCII bytes and VT220 key sequences:
// arrows ESC [ A..D, or ESC O A..D in cursor-key application mode (DECCKM)
// Home End ESC [ 1 ~ ESC [ 4 ~ (VT220 Find / Select)
// Ins Del PgUp PgDn ESC [ 2 ~ 3 ~ 5 ~ 6 ~
// F1-F4 ESC O P..S F5-F12 ESC [ 15 17 18 19 20 21 23 24 ~
// keypad digits, or ESC O p..y etc. in keypad application mode (DECKPAM)
// Alt+key ESC prefix
module ps2_keymap (
input clk,
input rst,
input [7:0] code,
input code_valid,
input bs_del, // Backspace sends DEL (7F) instead of BS (08)
input enter_crlf, // Enter sends CR LF instead of CR
input cursor_app, // DECCKM
input keypad_app, // DECKPAM
output reg [7:0] ascii,
output reg ascii_valid,
output reg caps,
output reg cad // Ctrl-Alt-Del was pressed (one clock)
);
localparam [7:0] ESC = 8'h1B;
reg e0, f0;
reg [2:0] skip;
reg lsh, rsh, lct, rct, alt;
reg [47:0] sq;
reg [2:0] cnt;
// {unshifted, shifted}
function [15:0] keymap(input [7:0] c);
case (c)
8'h0E: keymap = "`~"; 8'h16: keymap = "1!"; 8'h1E: keymap = "2@";
8'h26: keymap = "3#"; 8'h25: keymap = "4$"; 8'h2E: keymap = "5%";
8'h36: keymap = "6^"; 8'h3D: keymap = "7&"; 8'h3E: keymap = "8*";
8'h46: keymap = "9("; 8'h45: keymap = "0)"; 8'h4E: keymap = "-_";
8'h55: keymap = "=+"; 8'h0D: keymap = {8'h09, 8'h09};
8'h15: keymap = "qQ"; 8'h1D: keymap = "wW"; 8'h24: keymap = "eE";
8'h2D: keymap = "rR"; 8'h2C: keymap = "tT"; 8'h35: keymap = "yY";
8'h3C: keymap = "uU"; 8'h43: keymap = "iI"; 8'h44: keymap = "oO";
8'h4D: keymap = "pP"; 8'h54: keymap = "[{"; 8'h5B: keymap = "]}";
8'h5D: keymap = "\\|";
8'h1C: keymap = "aA"; 8'h1B: keymap = "sS"; 8'h23: keymap = "dD";
8'h2B: keymap = "fF"; 8'h34: keymap = "gG"; 8'h33: keymap = "hH";
8'h3B: keymap = "jJ"; 8'h42: keymap = "kK"; 8'h4B: keymap = "lL";
8'h4C: keymap = ";:"; 8'h52: keymap = "'\"";
8'h1A: keymap = "zZ"; 8'h22: keymap = "xX"; 8'h21: keymap = "cC";
8'h2A: keymap = "vV"; 8'h32: keymap = "bB"; 8'h31: keymap = "nN";
8'h3A: keymap = "mM"; 8'h41: keymap = ",<"; 8'h49: keymap = ".>";
8'h4A: keymap = "/?"; 8'h29: keymap = " "; 8'h76: keymap = {ESC, ESC};
// keypad, as if Num Lock were on
8'h70: keymap = "00"; 8'h69: keymap = "11"; 8'h72: keymap = "22";
8'h7A: keymap = "33"; 8'h6B: keymap = "44"; 8'h73: keymap = "55";
8'h74: keymap = "66"; 8'h6C: keymap = "77"; 8'h75: keymap = "88";
8'h7D: keymap = "99"; 8'h71: keymap = ".."; 8'h7C: keymap = "**";
8'h7B: keymap = "--"; 8'h79: keymap = "++";
default: keymap = 16'h0000;
endcase
endfunction
// final byte of ESC O x for the keypad in application mode (0 = not a keypad key)
function [7:0] kp_app(input [7:0] c);
case (c)
8'h70: kp_app = "p"; 8'h69: kp_app = "q"; 8'h72: kp_app = "r";
8'h7A: kp_app = "s"; 8'h6B: kp_app = "t"; 8'h73: kp_app = "u";
8'h74: kp_app = "v"; 8'h6C: kp_app = "w"; 8'h75: kp_app = "x";
8'h7D: kp_app = "y"; 8'h71: kp_app = "n"; 8'h7B: kp_app = "m";
8'h79: kp_app = "l"; 8'h7C: kp_app = "j";
default: kp_app = 8'h00;
endcase
endfunction
wire shift = lsh | rsh;
wire ctrl = lct | rct;
wire [15:0] km = keymap(code);
wire letter = km[15:8] >= "a" && km[15:8] <= "z";
wire [7:0] ch = (letter ? shift ^ caps : shift) ? km[7:0] : km[15:8];
wire [7:0] ckey = cursor_app ? "O" : "[";
wire [7:0] kpa = kp_app(code);
// bytes a key press produces, first byte in k_seq[47:40]
reg [2:0] k_len;
reg [47:0] k_seq;
always @* begin
k_len = 3'd0;
k_seq = 48'd0;
if (e0) case (code)
8'h75: {k_len, k_seq} = {3'd3, ESC, ckey, "A", 24'd0}; // up
8'h72: {k_len, k_seq} = {3'd3, ESC, ckey, "B", 24'd0}; // down
8'h74: {k_len, k_seq} = {3'd3, ESC, ckey, "C", 24'd0}; // right
8'h6B: {k_len, k_seq} = {3'd3, ESC, ckey, "D", 24'd0}; // left
8'h6C: {k_len, k_seq} = {3'd4, ESC, "[1~", 16'd0}; // home (Find)
8'h70: {k_len, k_seq} = {3'd4, ESC, "[2~", 16'd0}; // insert
8'h71: {k_len, k_seq} = {3'd4, ESC, "[3~", 16'd0}; // delete (Remove)
8'h69: {k_len, k_seq} = {3'd4, ESC, "[4~", 16'd0}; // end (Select)
8'h7D: {k_len, k_seq} = {3'd4, ESC, "[5~", 16'd0}; // page up
8'h7A: {k_len, k_seq} = {3'd4, ESC, "[6~", 16'd0}; // page down
8'h4A: {k_len, k_seq} = keypad_app ? {3'd3, ESC, "Oo", 24'd0} : {3'd1, "/", 40'd0};
8'h5A: {k_len, k_seq} = keypad_app ? {3'd3, ESC, "OM", 24'd0} :
enter_crlf ? {3'd2, 8'h0D, 8'h0A, 32'd0} : {3'd1, 8'h0D, 40'd0};
default: ;
endcase
else case (code)
8'h66: {k_len, k_seq} = {3'd1, bs_del ? 8'h7F : 8'h08, 40'd0};
8'h5A: {k_len, k_seq} = enter_crlf ? {3'd2, 8'h0D, 8'h0A, 32'd0} : {3'd1, 8'h0D, 40'd0};
8'h05: {k_len, k_seq} = {3'd3, ESC, "OP", 24'd0}; // F1
8'h06: {k_len, k_seq} = {3'd3, ESC, "OQ", 24'd0}; // F2
8'h04: {k_len, k_seq} = {3'd3, ESC, "OR", 24'd0}; // F3
8'h0C: {k_len, k_seq} = {3'd3, ESC, "OS", 24'd0}; // F4
8'h03: {k_len, k_seq} = {3'd5, ESC, "[15~", 8'd0}; // F5
8'h0B: {k_len, k_seq} = {3'd5, ESC, "[17~", 8'd0}; // F6
8'h83: {k_len, k_seq} = {3'd5, ESC, "[18~", 8'd0}; // F7
8'h0A: {k_len, k_seq} = {3'd5, ESC, "[19~", 8'd0}; // F8
8'h01: {k_len, k_seq} = {3'd5, ESC, "[20~", 8'd0}; // F9
8'h09: {k_len, k_seq} = {3'd5, ESC, "[21~", 8'd0}; // F10
8'h78: {k_len, k_seq} = {3'd5, ESC, "[23~", 8'd0}; // F11
8'h07: {k_len, k_seq} = {3'd5, ESC, "[24~", 8'd0}; // F12
default:
if (keypad_app && kpa != 0)
{k_len, k_seq} = {3'd3, ESC, "O", kpa, 24'd0};
else if (ch != 0) begin
k_len = 3'd1;
if (!ctrl) k_seq[47:40] = ch;
else if (ch == " ") k_seq[47:40] = 8'h00;
else if (ch >= 8'h40) k_seq[47:40] = ch & 8'h1F; // ^A..^Z, ^[ ^\ ^] ^^ ^_
else k_seq[47:40] = ch;
end
endcase
end
always @(posedge clk) begin
ascii_valid <= 1'b0;
cad <= 1'b0;
if (rst) begin
{e0, f0, lsh, rsh, lct, rct, alt, caps} <= 8'd0;
skip <= 3'd0;
cnt <= 3'd0;
end else begin
if (cnt != 0) begin
ascii <= sq[47:40];
ascii_valid <= 1'b1;
sq <= sq << 8;
cnt <= cnt - 1'b1;
end
if (code_valid) begin
if (skip != 0)
skip <= skip - 1'b1;
else if (code == 8'hE1)
skip <= 3'd7; // Pause: E1 14 77 E1 F0 14 F0 77
else if (code == 8'hE0)
e0 <= 1'b1;
else if (code == 8'hF0)
f0 <= 1'b1;
else begin
e0 <= 1'b0;
f0 <= 1'b0;
cad <= !f0 && code == 8'h71 && (lct || rct) && alt; // Delete or keypad .
if (code == 8'h12 && !e0) lsh <= !f0; // E0 12 / E0 59 are fake shifts
else if (code == 8'h59 && !e0) rsh <= !f0;
else if (code == 8'h14) begin
if (e0) rct <= !f0; else lct <= !f0;
end else if (code == 8'h11)
alt <= !f0;
else if (code == 8'h58 && !e0) begin
if (!f0) caps <= !caps;
end else if (!f0 && k_len != 0) begin
if (alt && k_len != 3'd6) begin
sq <= {ESC, k_seq[47:8]};
cnt <= k_len + 1'b1;
end else begin
sq <= k_seq;
cnt <= k_len;
end
end
end
end
end
end
endmodule
// Which keys are held down, for games: one bit per set-2 scan code, 128 plain
// codes ({0, code}) and 128 after the E0 prefix ({1, code}). The Pause key's
// E1 sequence is skipped.
module ps2_keystate (
input clk,
input rst,
input [7:0] code,
input code_valid,
output reg [255:0] down
);
reg ext, brk, pause;
reg [2:0] skip;
always @(posedge clk)
if (rst) begin
down <= 256'd0;
ext <= 1'b0; brk <= 1'b0; skip <= 3'd0;
end else if (code_valid) begin
if (skip != 3'd0) skip <= skip - 1'b1; // rest of E1 14 77 E1 F0 14 F0 77
else if (code == 8'hE1) skip <= 3'd7;
else if (code == 8'hE0) ext <= 1'b1;
else if (code == 8'hF0) brk <= 1'b1;
else begin
if (!code[7]) down[{ext, code[6:0]}] <= !brk;
ext <= 1'b0;
brk <= 1'b0;
end
end
endmodule
+551
View File
@@ -0,0 +1,551 @@
`timescale 1ns / 1ps
// RV32IM processor, machine mode only.
//
// A multi-cycle core with one instruction in flight. Each instruction is
// decoded in one cycle (D) and executed in the next (E); the decode cycle
// fetches the following word, so straight-line code takes 2 clocks per
// instruction. Taken branches and jumps take 3, loads and stores 3 (more if
// the bus waits), multiplies 4 and divides 35.
//
// Instruction port: synchronous read. i_rdata is the word at the i_addr given
// in the last cycle with i_en high, and must hold while i_en is low.
//
// Data port: d_req is high for one cycle with d_addr, d_we, d_wstrb and
// d_wdata; the bus answers with d_ack (and d_rdata for a read) one or more
// cycles later. d_addr is word-aligned for words, halfword-aligned for
// halfwords (misaligned accesses trap); d_rdata is the whole word.
// d_addr and d_wdata come early in the cycle (an adder from registers), but
// d_req comes late (after the alignment and breakpoint checks): register it,
// and the store, before it reaches block RAM write enables. A read with no
// request is harmless for RAM, so RAM can be read from d_addr every cycle.
//
// Exceptions that depend on the branch comparison or the load/store address
// (a taken branch to a misaligned target, a misaligned load/store, a load/store
// breakpoint) are raised one cycle late, from registers, to keep the long
// paths out of the trap logic; the instruction has no effect either way.
//
// Speed: 90 MHz with 1.5 ns to spare on the XC7A35T-1 (bench/), about 2100
// LUTs, 900 flip-flops and 4 DSP slices.
//
// Implemented: RV32I, M, Zicsr, Zicntr (cycle, time = cycle, instret), Zifencei,
// mret and wfi (a no-op). Machine-mode CSRs: mstatus (MIE, MPIE; MPP reads 3),
// misa, mie, mip, mtvec (direct mode), mscratch, mepc, mcause, mtval, mcycle,
// minstret, mhartid etc. (read 0), and two Sdtrig match triggers (tselect,
// tdata1, tdata2, tinfo, tcontrol) for execute, load and store address
// breakpoints. Triggers fire while tcontrol.MTE is set; a trap clears it
// (saving it in MPTE) and mret restores it.
//
// Resumable NMI (after the Smrnmi extension, for a debug monitor): while
// mnstatus.NMIE is set, `nmi` (checked as each instruction starts; it may come
// straight from comparators on dbg_pc) or `nmi_step` (after one instruction
// has completed with NMIE set) saves the pc in mnepc, clears NMIE and jumps to
// NMI_VEC; mnret goes back and sets NMIE again. nmi_taken pulses as it is
// taken. Unlike Smrnmi, NMIE resets to 1, software may clear it, and traps
// still go to mtvec while it is clear. CSRs: mnscratch, mnepc, mncause, mnstatus.
//
// Interrupts: mip.MSIP (irq_soft), MTIP (irq_timer), MEIP (irq_ext) and local
// interrupts 16-31 (irq_local), all level-sensitive. Priority: external,
// software, timer, then local 16 upward.
module rv32_cpu #(
parameter [31:0] RESET_PC = 32'h0000_0000,
parameter [31:0] NMI_VEC = 32'h0000_0000
) (
input clk,
input rst, // synchronous
output i_en,
output [31:0] i_addr,
input [31:0] i_rdata,
output d_req,
output d_we,
output [3:0] d_wstrb,
output [31:0] d_addr,
output [31:0] d_wdata,
input [31:0] d_rdata,
input d_ack,
input irq_soft,
input irq_timer,
input irq_ext,
input [15:0] irq_local,
input nmi,
input nmi_step,
output reg nmi_taken,
output [31:0] dbg_pc, // address of the instruction being run
output dbg_decode, // high in an instruction's first cycle
output reg retire // pulse: an instruction completed
);
localparam S_IF = 3'd0, S_D = 3'd1, S_E = 3'd2, S_MEM = 3'd3,
S_MUL = 3'd4, S_DIV = 3'd5, S_DIVF = 3'd6, S_XT = 3'd7;
reg [2:0] state;
reg [31:0] pc;
assign i_en = state == S_IF || state == S_D;
assign i_addr = state == S_D ? pc + 32'd4 : pc;
assign dbg_pc = pc;
assign dbg_decode = state == S_D;
// ------------------------------------------------------------------
// Registers: distributed RAM, two asynchronous read ports.
reg [31:0] rf [0:31];
integer k;
initial for (k = 0; k < 32; k = k + 1) rf[k] = 32'd0;
reg rf_we;
reg [4:0] rf_wa;
reg [31:0] rf_wd;
always @(posedge clk) if (rf_we && rf_wa != 5'd0) rf[rf_wa] <= rf_wd;
// ------------------------------------------------------------------
// Decode (state D): inst is the fetched word.
wire [31:0] inst = i_rdata;
wire [6:0] opc = inst[6:0];
wire [2:0] f3 = inst[14:12];
wire [6:0] f7 = inst[31:25];
wire [4:0] rs1 = inst[19:15], rs2 = inst[24:20];
wire [31:0] rv1 = rs1 == 5'd0 ? 32'd0 : rf[rs1];
wire [31:0] rv2 = rs2 == 5'd0 ? 32'd0 : rf[rs2];
wire [31:0] imm_i = {{20{inst[31]}}, inst[31:20]};
wire [31:0] imm_s = {{20{inst[31]}}, inst[31:25], inst[11:7]};
wire [31:0] imm_b = {{20{inst[31]}}, inst[7], inst[30:25], inst[11:8], 1'b0};
wire [31:0] imm_u = {inst[31:12], 12'd0};
wire [31:0] imm_j = {{12{inst[31]}}, inst[19:12], inst[20], inst[30:21], 1'b0};
wire is_lui = opc == 7'b0110111;
wire is_auipc = opc == 7'b0010111;
wire is_jal = opc == 7'b1101111;
wire is_jalr = opc == 7'b1100111 && f3 == 3'd0;
wire is_br = opc == 7'b1100011 && f3[2:1] != 2'b01;
wire is_ld = opc == 7'b0000011 && (f3 == 3'd0 || f3 == 3'd1 || f3 == 3'd2 ||
f3 == 3'd4 || f3 == 3'd5);
wire is_st = opc == 7'b0100011 && f3[2] == 1'b0 && f3[1:0] != 2'b11;
wire is_opi = opc == 7'b0010011 &&
(f3 == 3'd1 ? f7 == 7'd0 :
f3 == 3'd5 ? (f7 == 7'd0 || f7 == 7'b0100000) : 1'b1);
wire is_op = opc == 7'b0110011 &&
(f7 == 7'd0 || (f7 == 7'b0100000 && (f3 == 3'd0 || f3 == 3'd5)));
wire is_mdv = opc == 7'b0110011 && f7 == 7'b0000001;
wire is_fence = opc == 7'b0001111 && f3 == 3'd0;
wire is_fencei= opc == 7'b0001111 && f3 == 3'd1;
wire is_sys = opc == 7'b1110011;
wire is_csr = is_sys && f3[1:0] != 2'b00;
wire is_ecall = inst == 32'h0000_0073;
wire is_ebrk = inst == 32'h0010_0073;
wire is_mret = inst == 32'h3020_0073;
wire is_wfi = inst == 32'h1050_0073;
wire is_mnret = inst == 32'h7020_0073;
wire legal = is_lui | is_auipc | is_jal | is_jalr | is_br | is_ld | is_st |
is_opi | is_op | is_mdv | is_fence | is_fencei | is_csr |
is_ecall | is_ebrk | is_mret | is_wfi | is_mnret;
// ------------------------------------------------------------------
// Execute-stage registers, loaded at the end of D.
reg [31:0] ir; // the instruction
reg [31:0] a, b; // ALU operands
reg [31:0] sv; // store data / rs2
reg [31:0] tgt; // branch or jal target
reg [31:0] pc4;
reg e_tmis; // branch / jal target is misaligned
reg x_trap; // the deferred trap, below
reg [31:0] x_cause, x_val;
reg e_alu, e_sub, e_jal, e_jalr, e_br, e_ld, e_st, e_mul, e_div,
e_csr, e_ecall, e_ebrk, e_mret, e_mnret, e_fencei, e_ill;
wire [2:0] e_f3 = ir[14:12];
wire [4:0] e_rd = ir[11:7];
reg [2:0] alu_fn;
reg alu_alt; // sra rather than srl
// multiplier (DSP), fed straight from decode
reg signed [32:0] m_a, m_b;
reg signed [65:0] m_p, m_p2;
reg m_hi, m_cnt;
always @(posedge clk) begin
m_p <= m_a * m_b;
m_p2 <= m_p;
end
// ------------------------------------------------------------------
// Execute (state E)
wire [31:0] sum = a + b;
wire eq = a == b;
wire lt = $signed(a) < $signed(b);
wire ltu = a < b;
wire [31:0] sra_out = $signed(a) >>> b[4:0];
reg [31:0] alu_out;
always @* begin
case (alu_fn)
3'd0: alu_out = e_sub ? a - b : sum;
3'd1: alu_out = a << b[4:0];
3'd2: alu_out = {31'd0, lt};
3'd3: alu_out = {31'd0, ltu};
3'd4: alu_out = a ^ b;
3'd5: alu_out = alu_alt ? sra_out : a >> b[4:0];
3'd6: alu_out = a | b;
default: alu_out = a & b;
endcase
end
reg taken;
always @* begin
case (e_f3)
3'd0: taken = eq;
3'd1: taken = !eq;
3'd4: taken = lt;
3'd5: taken = !lt;
3'd6: taken = ltu;
default: taken = !ltu;
endcase
end
wire [31:0] jtarget = e_jalr ? {sum[31:1], 1'b0} : tgt;
wire jump = e_jal | e_jalr | (e_br & taken);
// loads and stores
wire [1:0] sz = e_f3[1:0];
wire [1:0] lo = a[1:0] + b[1:0];
wire mis = (sz == 2'd1 && lo[0]) || (sz == 2'd2 && lo != 2'd0);
// ------------------------------------------------------------------
// CSRs
reg mie_b, mpie_b; // mstatus
reg [31:0] mie_r, mtvec, mscratch, mepc, mcause, mtval;
reg [63:0] mcycle, minstret;
reg tsel, mte, mpte;
reg nmie, stepped;
reg [31:0] mnscratch, mnepc, mncause;
reg [1:0] t_m, t_x, t_s, t_l;
reg [31:0] t_a0, t_a1;
reg [31:0] irq_r;
localparam [31:0] MIE_MASK = 32'hFFFF_0888;
wire [11:0] csr_a = ir[31:20];
reg [31:0] csr_rd;
reg csr_ok;
always @* begin
csr_ok = 1'b1;
case (csr_a)
12'h300: csr_rd = {19'd0, 2'b11, 3'd0, mpie_b, 3'd0, mie_b, 3'd0};
12'h301: csr_rd = 32'h4000_1100; // RV32 I M
12'h304: csr_rd = mie_r;
12'h305: csr_rd = mtvec;
12'h310: csr_rd = 32'd0; // mstatush
12'h340: csr_rd = mscratch;
12'h341: csr_rd = mepc;
12'h342: csr_rd = mcause;
12'h343: csr_rd = mtval;
12'h344: csr_rd = irq_r;
12'h740: csr_rd = mnscratch;
12'h741: csr_rd = mnepc;
12'h742: csr_rd = mncause;
12'h744: csr_rd = {19'd0, 2'b11, 7'd0, nmie, 3'd0};
12'h7A0: csr_rd = {31'd0, tsel};
12'h7A1: csr_rd = {4'd2, 21'd0, t_m[tsel], 3'd0, t_x[tsel], t_s[tsel], t_l[tsel]};
12'h7A2: csr_rd = tsel ? t_a1 : t_a0;
12'h7A4: csr_rd = 32'h0000_0004; // tinfo: match triggers
12'h7A5: csr_rd = {24'd0, mpte, 3'd0, mte, 3'd0};
12'hB00, 12'hC00, 12'hC01: csr_rd = mcycle[31:0];
12'hB80, 12'hC80, 12'hC81: csr_rd = mcycle[63:32];
12'hB02, 12'hC02: csr_rd = minstret[31:0];
12'hB82, 12'hC82: csr_rd = minstret[63:32];
12'hF11, 12'hF12, 12'hF13, 12'hF14, 12'hF15: csr_rd = 32'd0;
default: begin csr_rd = 32'd0; csr_ok = 1'b0; end
endcase
end
wire csr_imm = e_f3[2];
wire [31:0] csr_src = csr_imm ? {27'd0, ir[19:15]} : a;
wire csr_wr = e_f3[1:0] == 2'b01 || ir[19:15] != 5'd0;
wire [31:0] csr_wv = e_f3[1:0] == 2'b01 ? csr_src :
e_f3[1:0] == 2'b10 ? csr_rd | csr_src : csr_rd & ~csr_src;
wire csr_bad = !csr_ok || (csr_wr && csr_a[11:10] == 2'b11);
wire csr_go = state == S_E && e_csr && !csr_bad && csr_wr;
// triggers
wire fire_x = mte && ((t_m[0] && t_x[0] && pc == t_a0) || (t_m[1] && t_x[1] && pc == t_a1));
// a + b == k without waiting for the carries: bit i of the sum matches k
// exactly when the carry into it is x[i] = a^b^k, and that carry is the
// majority of a, b and x one bit down (and 0 into bit 0).
function addeq(input [31:0] a, input [31:0] b, input [31:0] k);
reg [31:0] x, c;
begin
x = a ^ b ^ k;
c = (a & b) | (a & x) | (b & x);
addeq = !x[0] && x[31:1] == c[30:0];
end
endfunction
wire hit0 = addeq(a, b, t_a0), hit1 = addeq(a, b, t_a1);
wire fire_l = mte && ((t_m[0] && t_l[0] && hit0) || (t_m[1] && t_l[1] && hit1));
wire fire_s = mte && ((t_m[0] && t_s[0] && hit0) || (t_m[1] && t_s[1] && hit1));
// interrupts
wire [31:0] pend = irq_r & mie_r;
wire irq_take = mie_b && pend != 32'd0;
reg [4:0] irq_code;
integer j;
always @* begin
irq_code = 5'd16;
for (j = 31; j >= 16; j = j - 1) if (pend[j]) irq_code = j[4:0];
if (pend[7]) irq_code = 5'd7;
if (pend[3]) irq_code = 5'd3;
if (pend[11]) irq_code = 5'd11;
end
// ------------------------------------------------------------------
// Data bus
wire ls_bad = (e_ld && fire_l) || (e_st && fire_s) || mis;
wire mem_go = state == S_E && (e_ld || e_st) && !ls_bad;
assign d_req = mem_go;
assign d_we = e_st;
assign d_addr = sum;
assign d_wstrb = sz == 2'd0 ? 4'b0001 << sum[1:0] :
sz == 2'd1 ? (sum[1] ? 4'b1100 : 4'b0011) : 4'b1111;
assign d_wdata = sz == 2'd0 ? {4{sv[7:0]}} : sz == 2'd1 ? {2{sv[15:0]}} : sv;
reg [1:0] la; // address bits of the load
wire [31:0] lw_sh = d_rdata >> {la, 3'b000};
wire [31:0] ld_val = e_f3 == 3'd0 ? {{24{lw_sh[7]}}, lw_sh[7:0]} :
e_f3 == 3'd1 ? {{16{lw_sh[15]}}, lw_sh[15:0]} :
e_f3 == 3'd4 ? {24'd0, lw_sh[7:0]} :
e_f3 == 3'd5 ? {16'd0, lw_sh[15:0]} : d_rdata;
// divider
reg [31:0] dq, dr, dd, da;
reg [5:0] dcnt;
reg dneg_q, dneg_r, dzero;
wire [32:0] dtmp = {dr, dq[31]};
wire [33:0] dsub = {1'b0, dtmp} - {2'b0, dd};
// ------------------------------------------------------------------
// Traps
wire nmi_go = state == S_D && nmie && (nmi || (nmi_step && stepped));
reg trap;
reg [31:0] t_cause, t_val;
always @* begin
trap = 1'b0; t_cause = 32'd0; t_val = 32'd0;
if (state == S_D) begin
if (nmi_go) begin
trap = 1'b1; // to NMI_VEC; see below
end else if (irq_take) begin
trap = 1'b1; t_cause = {1'b1, 26'd0, irq_code};
end else if (fire_x) begin
trap = 1'b1; t_cause = 32'd3; t_val = pc;
end
end else if (state == S_XT) begin
// Traps that depend on the branch comparison or the load/store
// address are taken a cycle late, from registers, to keep those
// long paths out of the trap logic: a taken branch to a misaligned
// target, a load/store breakpoint, a misaligned load/store.
if (x_trap) begin
trap = 1'b1; t_cause = x_cause; t_val = x_val;
end
end else if (state == S_E) begin
if (e_ill || (e_csr && csr_bad)) begin
trap = 1'b1; t_cause = 32'd2; t_val = ir;
end else if ((e_jal && e_tmis) || (e_jalr && lo[1])) begin
trap = 1'b1; t_cause = 32'd0; t_val = jtarget;
end else if (e_ecall) begin
trap = 1'b1; t_cause = 32'd11;
end else if (e_ebrk) begin
trap = 1'b1; t_cause = 32'd3; t_val = pc;
end
end
end
// ------------------------------------------------------------------
// Register write-back
always @* begin
rf_we = 1'b0; rf_wa = e_rd; rf_wd = alu_out;
case (state)
S_E: begin
rf_we = !trap && (e_alu || e_jal || e_jalr || e_csr);
rf_wd = e_csr ? csr_rd : (e_jal || e_jalr) ? pc4 : alu_out;
end
S_MEM: begin rf_we = d_ack && e_ld; rf_wd = ld_val; end
S_MUL: begin rf_we = m_cnt; rf_wd = m_hi ? m_p2[63:32] : m_p2[31:0]; end
S_DIVF: begin
rf_we = 1'b1;
if (e_f3[1]) // rem, remu
rf_wd = dzero ? da : dneg_r ? -dr : dr;
else
rf_wd = dzero ? 32'hFFFF_FFFF : dneg_q ? -dq : dq;
end
default: ;
endcase
end
// an instruction completes (counted by instret unless it writes instret)
reg count_i;
wire done = (state == S_E && !trap && !e_ld && !e_st && !e_mul && !e_div) ||
(state == S_MEM && d_ack) || (state == S_MUL && m_cnt) ||
state == S_DIVF || (state == S_XT && !x_trap);
// ------------------------------------------------------------------
always @(posedge clk) begin
irq_r <= {irq_local, 4'd0, irq_ext, 3'd0, irq_timer, 3'd0, irq_soft, 3'd0};
retire <= done;
// a write to either half replaces the count for this cycle
if (!(csr_go && (csr_a == 12'hB00 || csr_a == 12'hB80))) mcycle <= mcycle + 64'd1;
// instret counts a cycle late (instructions take at least two, so the
// next one still reads the right count)
count_i <= done && !(csr_go && (csr_a == 12'hB02 || csr_a == 12'hB82));
if (count_i) minstret <= minstret + 64'd1;
if (csr_go) begin
case (csr_a)
12'h300: begin mie_b <= csr_wv[3]; mpie_b <= csr_wv[7]; end
12'h304: mie_r <= csr_wv & MIE_MASK;
12'h305: mtvec <= {csr_wv[31:2], 2'b00};
12'h340: mscratch <= csr_wv;
12'h341: mepc <= {csr_wv[31:2], 2'b00};
12'h342: mcause <= csr_wv;
12'h343: mtval <= csr_wv;
12'h740: mnscratch <= csr_wv;
12'h741: mnepc <= {csr_wv[31:2], 2'b00};
12'h742: mncause <= csr_wv;
12'h744: nmie <= csr_wv[3];
12'h7A0: if (csr_wv[31:1] == 31'd0) tsel <= csr_wv[0];
12'h7A1: begin
t_m[tsel] <= csr_wv[6]; t_x[tsel] <= csr_wv[2];
t_s[tsel] <= csr_wv[1]; t_l[tsel] <= csr_wv[0];
end
12'h7A2: if (tsel) t_a1 <= csr_wv; else t_a0 <= csr_wv;
12'h7A5: begin mte <= csr_wv[3]; mpte <= csr_wv[7]; end
12'hB00: mcycle[31:0] <= csr_wv;
12'hB80: mcycle[63:32] <= csr_wv;
12'hB02: minstret[31:0] <= csr_wv;
12'hB82: minstret[63:32] <= csr_wv;
default: ;
endcase
end
case (state)
S_IF: state <= S_D;
S_D: begin
ir <= inst;
pc4 <= pc + 32'd4;
tgt <= pc + (is_jal ? imm_j : imm_b);
a <= is_lui ? 32'd0 : (is_auipc || is_jal) ? pc : rv1;
b <= (is_lui || is_auipc) ? imm_u : is_st ? imm_s :
(is_op || is_br || is_mdv) ? rv2 : imm_i;
sv <= rv2;
alu_fn <= (is_lui || is_auipc) ? 3'd0 : f3;
alu_alt <= inst[30];
e_alu <= is_lui | is_auipc | is_opi | is_op;
e_sub <= is_op & inst[30];
e_jal <= is_jal;
e_jalr <= is_jalr;
e_br <= is_br;
e_tmis <= is_jal ? inst[21] : inst[8];
e_ld <= is_ld;
e_st <= is_st;
e_mul <= is_mdv & !f3[2];
e_div <= is_mdv & f3[2];
e_csr <= is_csr;
e_ecall <= is_ecall;
e_ebrk <= is_ebrk;
e_mret <= is_mret;
e_mnret <= is_mnret;
e_fencei<= is_fencei;
e_ill <= !legal;
m_a <= {f3[1:0] != 2'b11 && rv1[31], rv1}; // mulhu: unsigned
m_b <= {f3[1:0] == 2'b01 && rv2[31], rv2}; // mulh only: signed
m_hi <= f3[1:0] != 2'b00;
if (!trap) state <= S_E;
end
S_E: begin
// set up the multi-cycle units (unused if this traps)
la <= lo;
x_trap <= e_br ? taken : 1'b1;
x_cause <= e_br ? 32'd0 : (fire_l || fire_s) ? 32'd3 : e_st ? 32'd6 : 32'd4;
x_val <= e_br ? tgt : sum;
m_cnt <= 1'b0;
dneg_q <= !e_f3[0] && (a[31] ^ b[31]) && b != 32'd0;
dneg_r <= !e_f3[0] && a[31];
dzero <= b == 32'd0;
da <= a;
dq <= (!e_f3[0] && a[31]) ? -a : a;
dd <= (!e_f3[0] && b[31]) ? -b : b;
dr <= 32'd0;
dcnt <= 6'd0;
if (!trap) begin
if (e_ld || e_st)
state <= ls_bad ? S_XT : S_MEM;
else if (e_mul)
state <= S_MUL;
else if (e_div)
state <= S_DIV;
else if (e_br && e_tmis)
state <= S_XT;
else if (e_mret) begin
pc <= mepc; mie_b <= mpie_b; mpie_b <= 1'b1; mte <= mpte;
state <= S_IF;
end else if (e_mnret) begin
pc <= mnepc; nmie <= 1'b1;
state <= S_IF;
end else if (jump) begin
pc <= jtarget;
state <= S_IF;
end else if (e_fencei) begin
pc <= pc4;
state <= S_IF;
end else begin
pc <= pc4;
state <= S_D;
end
end
end
S_MEM: if (d_ack) begin pc <= pc4; state <= S_D; end
S_MUL: begin
m_cnt <= 1'b1;
if (m_cnt) begin pc <= pc4; state <= S_D; end
end
S_DIV: begin
if (!dsub[33]) begin dr <= dsub[31:0]; dq <= {dq[30:0], 1'b1}; end
else begin dr <= dtmp[31:0]; dq <= {dq[30:0], 1'b0}; end
dcnt <= dcnt + 6'd1;
if (dcnt == 6'd31) state <= S_DIVF;
end
S_DIVF: begin pc <= pc4; state <= S_D; end
S_XT: begin pc <= pc4; state <= S_D; end // not taken (else trap)
endcase
nmi_taken <= 1'b0;
stepped <= nmie && (stepped || done); // only counts outside the monitor
if (nmi_go) begin
mnepc <= pc;
mncause <= 32'h8000_0000;
nmie <= 1'b0;
stepped <= 1'b0;
nmi_taken <= 1'b1;
pc <= NMI_VEC;
state <= S_IF;
end else if (trap) begin
mepc <= pc;
mcause <= t_cause;
mtval <= t_val;
mpie_b <= mie_b;
mie_b <= 1'b0;
mpte <= mte;
mte <= 1'b0;
pc <= mtvec;
state <= S_IF;
end
if (rst) begin
state <= S_IF;
pc <= RESET_PC;
mie_b <= 1'b0; mpie_b <= 1'b0;
mie_r <= 32'd0;
mtvec <= RESET_PC;
mcause <= 32'd0;
tsel <= 1'b0; mte <= 1'b0; mpte <= 1'b0;
t_m <= 2'b00; t_x <= 2'b00; t_s <= 2'b00; t_l <= 2'b00;
mcycle <= 64'd0; minstret <= 64'd0;
retire <= 1'b0; count_i <= 1'b0;
nmie <= 1'b1; stepped <= 1'b0; nmi_taken <= 1'b0;
end
end
endmodule
+478
View File
@@ -0,0 +1,478 @@
`timescale 1ns / 1ps
// RISC-V computer: rv32_cpu, 80 KB of block RAM preloaded with BASIC, the
// monitor's 32 KB, and the Z-80 computer's I/O ports as memory-mapped
// registers. Runs on its own clock; the console crosses to the terminal's
// clock with toggle handshakes (one byte in flight each way).
//
// Memory map:
// 0000_0000-0001_3FFF RAM, 80 KB (BASIC). Writes below ROM_TOP are ignored
// so a crashed program cannot damage BASIC's code.
// 0002_0000-0002_7FFF the monitor's ROM (writes ignored). The NMI enters at
// 0002_0000.
// 0002_8000-0002_9FFF the monitor's RAM, for data only (it is distributed
// RAM, answering a cycle later than block RAM; code
// can't run from it).
// FFFF_FE00-FFFF_FEFF 32-bit registers (below)
// FFFF_FF00-FFFF_FFFF I/O port N at FFFF_FF00 + N, a byte each, the same
// ports as the Z-80 machine. Being within 2 KB of the top
// of memory, a port is one instruction from x0:
// lbu a0, -256+N(zero) / sb a0, -256+N(zero).
//
// I/O ports (FFFF_FF00 + N):
// 00 in bit 0 = key waiting, bit 1 = console can take a byte
// 01 in key (clears bit 0) out byte to the terminal
// 10 in millisecond counter bits 7:0; reading it latches bits 31:8 for 11-13
// 20 out 7-segment display, right two digits 21 out left two digits
// 30 out graphics X bits 7:0 31 out X bit 8 32 out Y
// 33 out plot colour at (X,Y), then X+1 in colour at (X,Y)
// 34 out fill the whole graphics screen in bit 0 = fill busy
// (also turns the sprites off and the scroll back to 0, unless
// bit 7 is set)
// 35 out scroll: buffer X shown at the left, bits 7:0
// 36 out bit 0 = scroll X bit 8; bit 1 = hide the graphics in a border (4
// columns each side, 2 rows top and bottom) to draw in unseen
// 37 out scroll: buffer Y shown at the top (0-239)
// 40 SPI flash control, 41 SPI flash data (see flash_spi.v)
// 60 out bit 1 = stop again (NMI) after one instruction of the program
// in why the last NMI came: 1 Pause key, 2 port 61, 3 step,
// 4 breakpoint 0, 5 breakpoint 1
// 61 out enter the monitor (NMI)
// 67 out bit 0 = breakpoint 0 on, bit 1 = breakpoint 1 on
// 70-78, 80-87 sprites (see sprites.v)
// 88 in keys held: bit 0 up (W, cursor or keypad 8), 1 down (S, 2), 2 left
// (A, 4), 3 right (D, 6), 4 fire (Space or Ctrl), 5 Enter, 6 Shift,
// 7 Esc
// 89 out pick a scan code (set 2; add 128 for an E0 code)
// in 1 if that key is held, else 0
// 90-9D sound (see sound.v)
// The Z-80's debug-read ports (62-64) are gone: the monitor runs in the same
// address space as the program. Its breakpoint addresses are 32 bits, below.
//
// 32-bit registers (FFFF_FE00 + N):
// 00 in milliseconds since reset
// 04 in microseconds since reset (wraps every 71 minutes)
// 08 timer compare: the timer interrupt (mip.MTIP) is pending while the
// microsecond counter is at or past it (by less than 2^31)
// 10 breakpoint 0 address 14 breakpoint 1 address
//
// The monitor: an NMI (the Pause key, port 61, a breakpoint or a single step)
// saves the program's pc in mnepc and jumps to the monitor ROM, which leaves
// with mnret. NMIs wait while the program counter is in the monitor's ROM.
// Breakpoints compare the address of each instruction as it starts, so the
// instruction at a breakpoint has not run.
module rv32_system #(
parameter MEM_FILE = "basic.mem", // RAM 0000-FFFF
parameter MEM_HI_FILE = "basic-hi.mem", // RAM 10000-13FFF
parameter MON_FILE = "monitor.mem",
parameter CPU_HZ = 90_000_000,
parameter ROM_TOP = 32'h0000_A000
) (
input clk, // CPU clock
input rst, // synchronous to clk
// keyboard side: a byte is offered when rx_req != rx_ack
input rx_req,
input [7:0] rx_data,
output reg rx_ack,
// screen side: a byte is offered when tx_req != tx_ack
output reg tx_req,
output reg [7:0] tx_data,
input tx_ack,
output reg [15:0] seg_val,
output halted,
input brk_key, // pulse: the Pause key, enter the monitor
// graphics layer read port, on the video clock
input vclk,
input [16:0] gfx_vaddr,
output reg [3:0] gfx_vpix, // (combinational from registers)
output reg [8:0] scroll_x,
output reg [7:0] scroll_y,
output reg g_border,
// sprites, on the video clock (see sprites.v)
input sp_go,
input [7:0] sp_line,
input sp_buf,
input [8:0] sp_x,
input sp_on,
output [3:0] sp_pix,
input sp_ftick,
// keys held down (one bit per scan code, from the keyboard's clock)
input [255:0] keys,
// configuration flash (SPI)
output fl_cs_n,
output fl_sck,
output fl_mosi,
input fl_miso,
// sound, for a PmodAMP2
output audio
);
localparam [31:0] MON_BASE = 32'h0002_0000;
assign halted = 1'b0;
// ------------------------------------------------------------------ CPU
wire i_en, d_req, d_we, dbg_decode, retire, nmi_taken;
wire [31:0] i_addr, d_addr, d_wdata, dbg_pc;
wire [3:0] d_wstrb;
reg [31:0] i_rdata, d_rdata;
reg d_ack;
wire nmi, timer_irq;
reg step_arm;
rv32_cpu #(.RESET_PC(32'h0), .NMI_VEC(MON_BASE)) cpu (
.clk(clk), .rst(rst),
.i_en(i_en), .i_addr(i_addr), .i_rdata(i_rdata),
.d_req(d_req), .d_we(d_we), .d_wstrb(d_wstrb), .d_addr(d_addr),
.d_wdata(d_wdata), .d_rdata(d_rdata), .d_ack(d_ack),
.irq_soft(1'b0), .irq_timer(timer_irq), .irq_ext(1'b0), .irq_local(16'd0),
.nmi(nmi), .nmi_step(step_arm), .nmi_taken(nmi_taken),
.dbg_pc(dbg_pc), .dbg_decode(dbg_decode), .retire(retire)
);
// ------------------------------------------------------------- memories
// Each block RAM: instruction fetch on port A; loads and stores on port B,
// where a store is written the cycle after the request (its address
// replacing the core's for that cycle).
// RAM is two arrays (64 KB and 16 KB) so that it takes 20 block RAMs, not 32.
wire d_main = d_addr[31:17] == 15'd0 && (!d_addr[16] || d_addr[15:14] == 2'd0);
wire d_mon = d_addr[31:15] == MON_BASE[31:15];
wire d_mram = d_addr[31:13] == 19'h14; // 0002_8000-0002_9FFF
wire d_io = d_addr[31:12] == 20'hFFFFF;
integer b;
(* ram_style = "block" *) reg [31:0] ram0 [0:16383]; // 0000_0000-0000_FFFF
(* ram_style = "block" *) reg [31:0] ram1 [0:4095]; // 0001_0000-0001_3FFF
initial $readmemh(MEM_FILE, ram0);
initial $readmemh(MEM_HI_FILE, ram1);
reg [31:0] ram0_iq, ram1_iq, ram0_dq, ram1_dq, w_d;
reg [13:0] w_a;
reg [3:0] w_s;
reg w_r0, w_r1, d_hi;
wire [13:0] r0_b = w_r0 ? w_a : d_addr[15:2];
wire [11:0] r1_b = w_r1 ? w_a[11:0] : d_addr[13:2];
always @(posedge clk) if (i_en) ram0_iq <= ram0[i_addr[15:2]];
always @(posedge clk) if (i_en) ram1_iq <= ram1[i_addr[13:2]];
always @(posedge clk) begin
if (w_r0)
for (b = 0; b < 4; b = b + 1)
if (w_s[b]) ram0[r0_b][8*b +: 8] <= w_d[8*b +: 8];
ram0_dq <= ram0[r0_b];
end
always @(posedge clk) begin
if (w_r1)
for (b = 0; b < 4; b = b + 1)
if (w_s[b]) ram1[r1_b][8*b +: 8] <= w_d[8*b +: 8];
ram1_dq <= ram1[r1_b];
end
(* ram_style = "block" *) reg [31:0] mrom [0:8191];
initial $readmemh(MON_FILE, mrom);
reg [31:0] mon_iq, mon_dq;
always @(posedge clk) if (i_en) mon_iq <= mrom[i_addr[14:2]];
always @(posedge clk) mon_dq <= mrom[d_addr[14:2]];
// the monitor's RAM: an access is registered, then the distributed RAM is
// read (or written) in the next cycle
(* ram_style = "distributed" *) reg [31:0] mram [0:2047];
reg [10:0] mr_a;
reg mr_go, mr_we;
reg [31:0] mr_q;
always @(posedge clk) begin
if (mr_go && mr_we)
for (b = 0; b < 4; b = b + 1)
if (w_s[b]) mram[mr_a][8*b +: 8] <= w_d[8*b +: 8];
mr_q <= mram[mr_a];
end
// instruction fetch: anything outside the two memories reads as 0 (illegal)
reg [1:0] i_sel;
always @(posedge clk)
if (i_en) i_sel <= i_addr[31:16] == 16'h0000 ? 2'd0 :
i_addr[31:14] == 18'h4 ? 2'd1 :
i_addr[31:15] == MON_BASE[31:15] ? 2'd2 : 2'd3;
always @* case (i_sel)
2'd0: i_rdata = ram0_iq;
2'd1: i_rdata = ram1_iq;
2'd2: i_rdata = mon_iq;
default: i_rdata = 32'd0;
endcase
// ----------------------------------------------------------- data bus
reg [2:0] d_sel; // 0 nothing, 1 RAM, 2 monitor ROM, 3 I/O, 4 monitor RAM
reg io_go, io_we;
reg [11:0] io_a;
reg [31:0] io_ww; // the store data as it came
reg [7:0] io_wd; // the byte for a port
reg [31:0] io_q;
always @(posedge clk) begin
w_r0 <= d_req && d_we && d_main && !d_addr[16] && d_addr >= ROM_TOP;
w_r1 <= d_req && d_we && d_main && d_addr[16] && d_addr >= ROM_TOP;
mr_go <= d_req && d_mram;
mr_we <= d_we;
mr_a <= d_addr[12:2];
w_a <= d_addr[15:2];
w_d <= d_wdata;
w_s <= d_wstrb;
io_go <= d_req && d_io;
io_we <= d_we;
io_a <= d_addr[11:0];
io_ww <= d_wdata;
io_wd <= d_wdata >> {d_addr[1:0], 3'b000};
if (d_req) begin
d_sel <= d_main ? 3'd1 : d_mon ? 3'd2 : d_io ? 3'd3 : d_mram ? 3'd4 : 3'd0;
d_hi <= d_addr[16];
end
// block RAM answers the next cycle, I/O and the monitor's RAM the one after
d_ack <= (d_req && !d_io && !d_mram) || io_go || mr_go;
if (rst) begin io_go <= 1'b0; d_ack <= 1'b0; w_r0 <= 1'b0; w_r1 <= 1'b0; mr_go <= 1'b0; end
end
always @* case (d_sel)
3'd1: d_rdata = d_hi ? ram1_dq : ram0_dq;
3'd2: d_rdata = mon_dq;
3'd3: d_rdata = io_q;
3'd4: d_rdata = mr_q;
default: d_rdata = 32'd0;
endcase
// the port strobes, a cycle after the request
wire io_bytes = io_a[11:8] == 4'hF;
wire io_wr = io_go && io_we && io_bytes;
wire io_rd = io_go && !io_we && io_bytes;
wire [7:0] io_addr = io_a[7:0];
wire wr_word = io_go && io_we && io_a[11:8] == 4'hE;
reg [7:0] io_din;
reg [31:0] word_q;
// ------------------------------------------------------------- console
(* ASYNC_REG = "TRUE" *) reg [1:0] rx_req_s, tx_ack_s;
always @(posedge clk) begin
rx_req_s <= {rx_req_s[0], rx_req};
tx_ack_s <= {tx_ack_s[0], tx_ack};
end
wire rx_avail = rx_req_s[1] != rx_ack;
wire tx_ready = tx_ack_s[1] == tx_req;
// ------------------------------------------------------------ graphics
// 76800 pixels: a 64K array and an 11264 one, so they take about 9.5 block
// RAMs (one array would be built as 96K deep, 12 of them).
localparam GFX_PIXELS = 320 * 240;
(* ram_style = "block" *) reg [3:0] gfx_lo [0:65535];
(* ram_style = "block" *) reg [3:0] gfx_hi [0:GFX_PIXELS - 65536 - 1];
integer gi;
initial begin
for (gi = 0; gi < 65536; gi = gi + 1) gfx_lo[gi] = 4'd0;
for (gi = 0; gi < GFX_PIXELS - 65536; gi = gi + 1) gfx_hi[gi] = 4'd0;
end
reg [8:0] gx;
reg [7:0] gy;
reg [3:0] gfx_q_lo, gfx_q_hi, fill_c;
reg gfx_q_sel;
reg [16:0] fill_a;
reg fill_busy;
wire [16:0] gxy = {gy, 8'd0} + {2'd0, gy, 6'd0} + {8'd0, gx};
wire plot = io_wr && io_addr == 8'h33 && gx < 9'd320 && gy < 8'd240;
wire [16:0] g_addr = fill_busy ? fill_a : gxy;
wire g_we = fill_busy || plot;
wire [3:0] g_wd = fill_busy ? fill_c : io_wd[3:0];
wire [3:0] gfx_q = gfx_q_sel ? gfx_q_hi : gfx_q_lo;
always @(posedge clk) begin
if (g_we && !g_addr[16]) gfx_lo[g_addr[15:0]] <= g_wd;
gfx_q_lo <= gfx_lo[g_addr[15:0]];
end
always @(posedge clk) begin
if (g_we && g_addr[16]) gfx_hi[g_addr[13:0]] <= g_wd;
gfx_q_hi <= gfx_hi[g_addr[13:0]];
gfx_q_sel <= g_addr[16];
end
reg [3:0] gv_lo, gv_hi;
reg gv_sel;
always @(posedge vclk) begin
gv_lo <= gfx_lo[gfx_vaddr[15:0]];
gv_hi <= gfx_hi[gfx_vaddr[13:0]];
gv_sel <= gfx_vaddr[16];
end
always @* gfx_vpix = gv_sel ? gv_hi : gv_lo;
// whole-array view for testbenches
function [3:0] gfx(input [16:0] n);
gfx = n[16] ? gfx_hi[n[13:0]] : gfx_lo[n[15:0]];
endfunction
always @(posedge clk)
if (rst) begin
fill_busy <= 1'b0;
gx <= 9'd0; gy <= 8'd0;
scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0;
end else begin
if (io_wr) case (io_addr)
8'h30: gx[7:0] <= io_wd;
8'h31: gx[8] <= io_wd[0];
8'h32: gy <= io_wd;
8'h33: gx <= gx + 1'b1;
8'h34: begin
fill_busy <= 1'b1; fill_a <= 17'd0; fill_c <= io_wd[3:0];
if (!io_wd[7]) begin scroll_x <= 9'd0; scroll_y <= 8'd0; g_border <= 1'b0; end
end
8'h35: scroll_x[7:0] <= io_wd;
8'h36: begin scroll_x[8] <= io_wd[0]; g_border <= io_wd[1]; end
8'h37: scroll_y <= io_wd >= 8'd240 ? io_wd - 8'd240 : io_wd;
default: ;
endcase
if (fill_busy) begin
fill_a <= fill_a + 1'b1;
if (fill_a == GFX_PIXELS - 1) fill_busy <= 1'b0;
end
end
// ------------------------------------------------------------- sprites
wire [7:0] spr_q;
sprites spr (
.clk(clk), .rst(rst),
.io_wr(io_wr), .io_port(io_addr), .io_wd(io_wd), .io_q(spr_q),
.vclk(vclk), .go(sp_go), .line(sp_line), .rd_buf(sp_buf), .rd_x(sp_x),
.rd_on(sp_on), .pix(sp_pix), .gfx_pix(gfx_vpix), .ftick(sp_ftick)
);
// ---------------------------------------------------------------- sound
wire [7:0] snd_q;
sound #(.CPU_HZ(CPU_HZ)) snd (
.clk(clk), .rst(rst), .hush(io_wr && io_addr == 8'h34 && !io_wd[7]),
.io_wr(io_wr), .io_port(io_addr), .io_wd(io_wd), .io_q(snd_q), .audio(audio)
);
// ---------------------------------------------------------- held keys
// The keyboard's clock writes these; a bit that changes while it is read
// just reads as before or after.
reg [7:0] key_sel = 8'd0;
always @(posedge clk) if (io_wr && io_addr == 8'h89) key_sel <= io_wd;
wire [7:0] joy = {
keys[8'h76], // Esc
keys[8'h12] | keys[8'h59], // Shift
keys[8'h5A] | keys[8'hDA], // Enter
keys[8'h29] | keys[8'h14] | keys[8'h94], // Space, Ctrl
keys[8'h23] | keys[8'hF4] | keys[8'h74], // D, right, keypad 6
keys[8'h1C] | keys[8'hEB] | keys[8'h6B], // A, left, keypad 4
keys[8'h1B] | keys[8'hF2] | keys[8'h72], // S, down, keypad 2
keys[8'h1D] | keys[8'hF5] | keys[8'h75] // W, up, keypad 8
};
// --------------------------------------------------------------- flash
wire [7:0] fl_ctl_q, fl_dat_q;
flash_spi flash (
.clk(clk), .rst(rst),
.wr_ctl(io_wr && io_addr == 8'h40), .wr_dat(io_wr && io_addr == 8'h41),
.wdata(io_wd), .ctl_q(fl_ctl_q), .dat_q(fl_dat_q),
.cs_n(fl_cs_n), .sck(fl_sck), .mosi(fl_mosi), .miso(fl_miso)
);
// ------------------------------------------------- milli/microseconds
localparam MS_DIV = CPU_HZ / 1000;
localparam US_DIV = CPU_HZ / 1000000;
reg [31:0] ms, ms_latch, us, tcmp;
reg [17:0] ms_div;
reg [7:0] us_div;
always @(posedge clk)
if (rst) begin
ms <= 0; ms_div <= 0; us <= 0; us_div <= 0; tcmp <= 32'h8000_0000;
end else begin
if (ms_div == MS_DIV - 1) begin ms <= ms + 1'b1; ms_div <= 0; end
else ms_div <= ms_div + 1'b1;
if (us_div == US_DIV - 1) begin us <= us + 1'b1; us_div <= 0; end
else us_div <= us_div + 1'b1;
if (wr_word && io_a[7:0] == 8'h08) tcmp <= io_ww;
end
wire [31:0] t_left = us - tcmp;
reg timer_r;
always @(posedge clk) timer_r <= !t_left[31];
assign timer_irq = timer_r;
// ------------------------------------------------------------- monitor
// An NMI request waits (in nmi_r) until the core takes it; breakpoint hits
// go straight to the core as their instruction starts.
reg [31:0] bp0, bp1;
reg [1:0] bp_on, hit_d;
reg [2:0] why, why_r;
reg nmi_r, in_mon;
wire hit0 = bp_on[0] && dbg_decode && dbg_pc == bp0;
wire hit1 = bp_on[1] && dbg_decode && dbg_pc == bp1;
assign nmi = nmi_r || hit0 || hit1;
always @(posedge clk) begin
in_mon <= dbg_pc[31:15] == MON_BASE[31:15];
hit_d <= {hit1, hit0};
if (rst) begin
nmi_r <= 1'b0; step_arm <= 1'b0; bp_on <= 2'b00; why <= 3'd0;
bp0 <= 32'd0; bp1 <= 32'd0;
end else begin
if (io_wr) case (io_addr)
8'h60: step_arm <= io_wd[1];
8'h67: bp_on <= io_wd[1:0];
default: ;
endcase
if (wr_word && io_a[7:0] == 8'h10) bp0 <= io_ww;
if (wr_word && io_a[7:0] == 8'h14) bp1 <= io_ww;
if (!nmi_r && !in_mon) begin
if (brk_key) begin
nmi_r <= 1'b1; why_r <= 3'd1;
end else if (io_wr && io_addr == 8'h61) begin
nmi_r <= 1'b1; why_r <= 3'd2;
end
end
if (nmi_taken) begin
why <= nmi_r ? why_r : hit_d[0] ? 3'd4 : hit_d[1] ? 3'd5 : 3'd3;
nmi_r <= 1'b0;
step_arm <= 1'b0;
end
end
end
// -------------------------------------------------------- port reads
always @* case (io_addr)
8'h00: io_din = {6'd0, tx_ready, rx_avail};
8'h01: io_din = rx_data;
8'h10: io_din = ms[7:0]; // sampled on the same edge that fills ms_latch
8'h11: io_din = ms_latch[15:8];
8'h12: io_din = ms_latch[23:16];
8'h13: io_din = ms_latch[31:24];
8'h33: io_din = {4'd0, gfx_q};
8'h34: io_din = {7'd0, fill_busy};
8'h40: io_din = fl_ctl_q;
8'h41: io_din = fl_dat_q;
8'h60: io_din = {5'd0, why};
8'h78, 8'h80, 8'h81, 8'h82, 8'h83, 8'h84, 8'h85, 8'h86, 8'h87:
io_din = spr_q;
8'h88: io_din = joy;
8'h89: io_din = {7'd0, keys[key_sel]};
8'h98, 8'h99, 8'h9A:
io_din = snd_q;
default: io_din = 8'hFF;
endcase
always @* case (io_a[7:0])
8'h00: word_q = ms;
8'h04: word_q = us;
8'h08: word_q = tcmp;
8'h10: word_q = bp0;
8'h14: word_q = bp1;
default: word_q = 32'd0;
endcase
always @(posedge clk) begin
if (io_go) io_q <= io_bytes ? {24'd0, io_din} << {io_a[1:0], 3'b000} : word_q;
if (rst) begin
rx_ack <= rx_req_s[1];
tx_req <= tx_ack_s[1];
seg_val <= 16'h0032;
ms_latch <= 32'd0;
end else begin
if (io_rd && io_addr == 8'h01 && rx_avail) rx_ack <= ~rx_ack;
if (io_rd && io_addr == 8'h10) ms_latch <= ms;
if (io_wr && io_addr == 8'h01 && tx_ready) begin
tx_data <= io_wd;
tx_req <= ~tx_req;
end
if (io_wr && io_addr == 8'h20) seg_val[7:0] <= io_wd;
if (io_wr && io_addr == 8'h21) seg_val[15:8] <= io_wd;
end
end
endmodule
+29
View File
@@ -0,0 +1,29 @@
`timescale 1ns / 1ps
// Four-digit hex display, digit 3 (left) = value[15:12]. Segments active low.
module sevenseg (
input clk,
input [15:0] value,
output reg [6:0] seg,
output reg [3:0] an
);
reg [17:0] cnt;
always @(posedge clk) cnt <= cnt + 1'b1;
wire [1:0] d = cnt[17:16];
wire [3:0] x = value[d*4 +: 4];
always @(posedge clk) begin
an <= ~(4'b0001 << d);
case (x) // gfedcba
4'h0: seg <= 7'b1000000; 4'h1: seg <= 7'b1111001;
4'h2: seg <= 7'b0100100; 4'h3: seg <= 7'b0110000;
4'h4: seg <= 7'b0011001; 4'h5: seg <= 7'b0010010;
4'h6: seg <= 7'b0000010; 4'h7: seg <= 7'b1111000;
4'h8: seg <= 7'b0000000; 4'h9: seg <= 7'b0010000;
4'hA: seg <= 7'b0001000; 4'hB: seg <= 7'b0000011;
4'hC: seg <= 7'b1000110; 4'hD: seg <= 7'b0100001;
4'hE: seg <= 7'b0000110; 4'hF: seg <= 7'b0001110;
endcase
end
endmodule
+173
View File
@@ -0,0 +1,173 @@
`timescale 1ns / 1ps
// A small sound chip for the Z-80, in the style of the 8-bit era's
// programmable sound generators, played through a Digilent PmodAMP2.
//
// Three square-wave voices and a noise source. Each voice plays its tone,
// the noise, or both (the mixer), at one of 16 volumes on a log scale (about
// 3 dB a step). A volume write starts an envelope: an optional rise from 0 at
// the attack rate, then an optional fall to 0 at the decay rate, so a game
// can fire off a shot or an explosion with a few OUTs and leave it.
//
// 90h/91h voice 0 pitch, low byte / high 4 bits frequency = 125000 / pitch Hz
// 92h/93h voice 1 pitch (pitch 0 counts as 1)
// 94h/95h voice 2 pitch
// 96h noise pitch, 0-31: higher is lower and rougher
// 97h mixer: bits 0-2 tone on for voices 0-2, bits 3-5 noise on for them
// 98h-9Ah voice 0-2 volume 0-15, which (re)starts its envelope; reading
// gives the volume it is at now, so 0 means the sound has ended
// 9Bh-9Dh voice 0-2 envelope: high nibble attack, low nibble decay, each
// n x 4 ms a volume step (0 = jump straight up / hold)
//
// A voice with neither its tone nor the noise on outputs a steady level, as
// on the old chips. `hush` (CLS) and reset silence everything.
//
// The three voices are added up and turned into a single bit by a first-order
// sigma-delta modulator at the full clock rate; the PmodAMP2 filters it and
// drives headphones or a speaker.
module sound #(
parameter CPU_HZ = 90_000_000
) (
input clk,
input rst,
input hush,
input io_wr,
input [7:0] io_port,
input [7:0] io_wd,
output reg [7:0] io_q, // for reads of 98h-9Ah
output reg audio = 1'b0
);
localparam TICK_DIV = CPU_HZ / 250_000; // tone counters run at 250 kHz
localparam MS4_DIV = CPU_HZ / 250; // envelope steps in 4 ms units
reg [11:0] pitch [0:2];
reg [4:0] npitch;
reg [5:0] mixer;
reg [3:0] vol [0:2]; // the volume asked for
reg [3:0] cur [0:2]; // the volume now
reg [3:0] att [0:2], dec [0:2];
reg rising [0:2];
// ---------------------------------------------------------------- ticks
reg [9:0] tdiv = 10'd0;
reg tick = 1'b0;
reg [19:0] ediv = 20'd0;
reg etick = 1'b0;
always @(posedge clk) begin
tick <= tdiv == 0;
tdiv <= tdiv == 0 ? TICK_DIV - 1 : tdiv - 1'b1;
etick <= ediv == 0;
ediv <= ediv == 0 ? MS4_DIV - 1 : ediv - 1'b1;
end
// --------------------------------------------------------------- voices
reg [11:0] tcnt [0:2];
reg [2:0] tone = 3'd0;
reg [4:0] ncnt = 5'd0;
reg [16:0] lfsr = 17'd1;
reg [3:0] ecnt [0:2];
integer i;
initial for (i = 0; i < 3; i = i + 1) begin
tcnt[i] = 12'd0; ecnt[i] = 4'd0; cur[i] = 4'd0; vol[i] = 4'd0;
end
always @(posedge clk) begin
if (rst || hush) begin
for (i = 0; i < 3; i = i + 1) begin
vol[i] <= 4'd0; cur[i] <= 4'd0; rising[i] <= 1'b0;
if (rst) begin pitch[i] <= 12'd0; att[i] <= 4'd0; dec[i] <= 4'd0; end
end
mixer <= 6'd0;
if (rst) begin npitch <= 5'd0; lfsr <= 17'd1; tone <= 3'd0; end
end else begin
if (io_wr) case (io_port)
8'h90: pitch[0][7:0] <= io_wd;
8'h91: pitch[0][11:8] <= io_wd[3:0];
8'h92: pitch[1][7:0] <= io_wd;
8'h93: pitch[1][11:8] <= io_wd[3:0];
8'h94: pitch[2][7:0] <= io_wd;
8'h95: pitch[2][11:8] <= io_wd[3:0];
8'h96: npitch <= io_wd[4:0];
8'h97: mixer <= io_wd[5:0];
8'h9B: {att[0], dec[0]} <= io_wd;
8'h9C: {att[1], dec[1]} <= io_wd;
8'h9D: {att[2], dec[2]} <= io_wd;
default: ;
endcase
// tones: each output flips every `pitch` ticks
if (tick) begin
for (i = 0; i < 3; i = i + 1)
if (tcnt[i] <= 12'd1) begin
tcnt[i] <= pitch[i];
tone[i] <= ~tone[i];
end else
tcnt[i] <= tcnt[i] - 1'b1;
if (ncnt == 0) begin
ncnt <= npitch;
lfsr <= {lfsr[0] ^ lfsr[3], lfsr[16:1]};
end else
ncnt <= ncnt - 1'b1;
end
// envelopes
for (i = 0; i < 3; i = i + 1) begin
if (io_wr && io_port == 8'h98 + i) begin
vol[i] <= io_wd[3:0];
ecnt[i] <= 4'd0;
if (att[i] != 0 && io_wd[3:0] != 0) begin
cur[i] <= 4'd0; rising[i] <= 1'b1;
end else begin
cur[i] <= io_wd[3:0]; rising[i] <= 1'b0;
end
end else if (etick) begin
if (rising[i]) begin
if (ecnt[i] + 1'b1 >= att[i]) begin
ecnt[i] <= 4'd0;
cur[i] <= cur[i] + 1'b1;
if (cur[i] + 1'b1 >= vol[i]) rising[i] <= 1'b0;
end else
ecnt[i] <= ecnt[i] + 1'b1;
end else if (dec[i] != 0 && cur[i] != 0) begin
if (ecnt[i] + 1'b1 >= dec[i]) begin
ecnt[i] <= 4'd0;
cur[i] <= cur[i] - 1'b1;
end else
ecnt[i] <= ecnt[i] + 1'b1;
end
end
end
end
end
always @* case (io_port)
8'h98: io_q = {4'd0, cur[0]};
8'h99: io_q = {4'd0, cur[1]};
8'h9A: io_q = {4'd0, cur[2]};
default: io_q = 8'hFF;
endcase
// ---------------------------------------------------------------- mixing
function [7:0] level(input [3:0] v); // about 3 dB a step
case (v)
4'd0: level = 8'd0; 4'd1: level = 8'd2; 4'd2: level = 8'd3; 4'd3: level = 8'd4;
4'd4: level = 8'd6; 4'd5: level = 8'd8; 4'd6: level = 8'd11; 4'd7: level = 8'd16;
4'd8: level = 8'd23; 4'd9: level = 8'd32; 4'd10: level = 8'd45; 4'd11: level = 8'd64;
4'd12: level = 8'd90; 4'd13: level = 8'd128; 4'd14: level = 8'd181; default: level = 8'd255;
endcase
endfunction
wire [2:0] on;
genvar g;
generate for (g = 0; g < 3; g = g + 1) begin : mix
assign on[g] = (tone[g] | ~mixer[g]) & (lfsr[0] | ~mixer[g + 3]);
end endgenerate
reg [9:0] sum = 10'd0;
reg [10:0] acc = 11'd0;
always @(posedge clk) begin
sum <= (on[0] ? level(cur[0]) : 10'd0) + (on[1] ? level(cur[1]) : 10'd0) +
(on[2] ? level(cur[2]) : 10'd0);
acc <= rst ? 11'd0 : {1'b0, acc[9:0]} + {1'b0, sum}; // carry out = the output bit
audio <= acc[10];
end
endmodule
+237
View File
@@ -0,0 +1,237 @@
`timescale 1ns / 1ps
// Hardware sprites over the 320x240 graphics layer.
//
// 32 sprites of 16x16 graphics pixels in the 16 colours (0 = transparent),
// drawn from 64 shapes. Sprite 0 is on top of sprite 1 and so on; all sprites
// are over the graphics and under the text. Each sprite has a position, a
// shape, X and Y flip and an on/off bit.
//
// Z-80 ports (low address byte):
// 70 out select a sprite, 0-31
// 71 out X bits 7:0 72 out X bit 8
// 73 out Y; moves the selected sprite to the X and Y given (both at once)
// 74 out shape, 0-63
// 75 out bit 0 = on, bit 1 = flip left-right, bit 2 = flip upside down
// 76 out start loading shape n (0-63) at its top left
// 77 out two pixels of the shape, left one in bits 7:4, then move on;
// 128 bytes make a shape, a row of 16 pixels at a time
// 78 in frame counter, +1 at the start of each vertical blank
// 80-83 in sprites 0-7, 8-15, 16-23, 24-31 that touched another sprite
// in the last frame
// 84-87 in sprites that touched a graphics pixel (not colour 0)
// 34 out (the graphics fill) also turns every sprite off unless bit 7 is set
//
// X and Y are the top-left pixel and wrap at 512 and 256, so a sprite can go
// partly off the left or top edge: X = 500 is 12 pixels left of the screen.
//
// Drawing: the video asks for each graphics line one line ahead (go, line).
// The engine clears that line's buffer, then walks the sprites from 31 down
// to 0, writing each visible pixel, so lower numbers end up on top. A pixel
// written over another sprite's records a collision for both. The video reads
// the other buffer, one pixel per clock. A line takes at most about 900 of the
// 6,752 pixel clocks that a graphics line lasts.
module sprites (
// Z-80 side
input clk,
input rst,
input io_wr,
input [7:0] io_port,
input [7:0] io_wd,
output reg [7:0] io_q,
// video side (pixel clock)
input vclk,
input go, // prepare graphics line `line`
input [7:0] line,
input rd_buf, // buffer of the line on screen
input [8:0] rd_x, // graphics x on screen, registered
input rd_on, // inside the graphics area
output reg [3:0] pix, // sprite colour there, a clock later
input [3:0] gfx_pix, // graphics colour there, same clock as pix
input ftick // start of vertical blank
);
integer i;
// ------------------------------------------------------------ Z-80 side
reg [4:0] sel = 5'd0;
reg [8:0] x_stage = 9'd0;
reg [31:0] en = 32'd0;
reg [12:0] pat_a = 13'd0;
(* ram_style = "distributed" *) reg [16:0] pos [0:31]; // {y, x}
(* ram_style = "distributed" *) reg [7:0] shp [0:31]; // {flip y, flip x, shape}
(* ram_style = "block" *) reg [7:0] pat [0:8191]; // shape, row, pixel pair
initial begin
for (i = 0; i < 32; i = i + 1) begin pos[i] = 17'd0; shp[i] = 8'd0; end
for (i = 0; i < 8192; i = i + 1) pat[i] = 8'd0;
end
wire [7:0] shp_sel = shp[sel];
always @(posedge clk) begin
if (io_wr) case (io_port)
8'h73: pos[sel] <= {io_wd, x_stage};
8'h74: shp[sel] <= {shp_sel[7:6], io_wd[5:0]};
8'h75: shp[sel] <= {io_wd[2:1], shp_sel[5:0]};
default: ;
endcase
end
always @(posedge clk) if (io_wr && io_port == 8'h77) pat[pat_a] <= io_wd;
always @(posedge clk)
if (rst) begin
en <= 32'd0;
sel <= 5'd0;
x_stage <= 9'd0;
pat_a <= 13'd0;
end else if (io_wr) case (io_port)
8'h34: if (!io_wd[7]) en <= 32'd0;
8'h70: sel <= io_wd[4:0];
8'h71: x_stage[7:0] <= io_wd;
8'h72: x_stage[8] <= io_wd[0];
8'h75: en[sel] <= io_wd[0];
8'h76: pat_a <= {io_wd[5:0], 7'd0};
8'h77: pat_a <= pat_a + 1'b1;
default: ;
endcase
// ----------------------------------------------------------- line fill
// line buffers: {buffer, x} -> {colour, sprite}
(* ram_style = "distributed" *) reg [8:0] lb [0:1023];
initial for (i = 0; i < 1024; i = i + 1) lb[i] = 9'd0;
localparam S_IDLE = 2'd0, S_CLEAR = 2'd1, S_SPR = 2'd2;
reg [1:0] st = S_IDLE;
reg [8:0] cx;
reg wb;
reg [7:0] ln;
reg [4:0] s;
reg drawing = 1'b0;
reg [3:0] col;
reg [8:0] sx;
reg [3:0] srow;
reg sfx;
reg [5:0] sshape;
// the sprite being considered
wire [16:0] s_pos = pos[s];
wire [7:0] s_shp = shp[s];
wire [7:0] s_row = ln - s_pos[16:9];
wire s_vis = en[s] && s_row < 8'd16;
// pixel pipeline: address, pattern read, write
reg [12:0] pat_ra;
reg [7:0] pat_q;
reg p0_v = 1'b0, p1_v = 1'b0, p0_n, p1_n;
reg [8:0] p0_x, p1_x;
reg [4:0] p0_s, p1_s;
wire [3:0] cc = sfx ? ~col : col;
always @(posedge vclk) pat_q <= pat[pat_ra];
always @(posedge vclk) begin
p0_v <= 1'b0;
if (go) begin
st <= S_CLEAR;
cx <= 9'd0;
wb <= line[0];
ln <= line;
drawing <= 1'b0;
end else case (st)
S_CLEAR: begin
cx <= cx + 1'b1;
if (cx == 9'd319) begin
st <= S_SPR;
s <= 5'd31;
end
end
S_SPR:
if (!drawing) begin
if (s_vis) begin
drawing <= 1'b1;
col <= 4'd0;
sx <= s_pos[8:0];
srow <= s_shp[7] ? ~s_row[3:0] : s_row[3:0];
sfx <= s_shp[6];
sshape <= s_shp[5:0];
end else if (s == 5'd0)
st <= S_IDLE;
else
s <= s - 1'b1;
end else begin
pat_ra <= {sshape, srow, cc[3:1]};
p0_n <= cc[0];
p0_x <= sx + col;
p0_s <= s;
p0_v <= 1'b1;
col <= col + 1'b1;
if (col == 4'd15) begin
drawing <= 1'b0;
if (s == 5'd0) st <= S_IDLE;
else s <= s - 1'b1;
end
end
default: ;
endcase
{p1_v, p1_n, p1_x, p1_s} <= {p0_v, p0_n, p0_x, p0_s};
end
wire [3:0] w_col = p1_n ? pat_q[3:0] : pat_q[7:4];
wire w_spr = p1_v && w_col != 4'd0 && p1_x < 9'd320;
wire w_clr = st == S_CLEAR;
wire [9:0] wa = {wb, w_clr ? cx : p1_x};
wire [8:0] old = lb[wa];
always @(posedge vclk)
if (w_clr || w_spr) lb[wa] <= w_clr ? 9'd0 : {w_col, p1_s};
// ----------------------------------------------------------- video read
wire [8:0] rd = lb[{rd_buf, rd_x}];
reg [4:0] pix_id;
always @(posedge vclk) begin
pix <= rd_on ? rd[8:5] : 4'd0;
pix_id <= rd[4:0];
end
// ----------------------------------------------------------- collisions
reg [31:0] ss_acc = 32'd0, bg_acc = 32'd0, ss_frame = 32'd0, bg_frame = 32'd0;
reg ft = 1'b0;
always @(posedge vclk)
if (ftick) begin
ss_frame <= ss_acc;
bg_frame <= bg_acc;
ss_acc <= 32'd0;
bg_acc <= 32'd0;
ft <= ~ft;
end else begin
if (pix != 4'd0 && gfx_pix != 4'd0) bg_acc[pix_id] <= 1'b1;
if (w_spr && old[8:5] != 4'd0) begin
ss_acc[p1_s] <= 1'b1;
ss_acc[old[4:0]] <= 1'b1;
end
end
// A frame's results are steady for a whole frame after ft flips, so the
// Z-80 side copies them once it has seen the flip.
(* ASYNC_REG = "TRUE" *) reg [2:0] ft_s = 3'd0;
reg [7:0] frames = 8'd0;
reg [31:0] ss_cpu = 32'd0, bg_cpu = 32'd0;
always @(posedge clk) begin
ft_s <= {ft_s[1:0], ft};
if (ft_s[2] != ft_s[1]) begin
frames <= frames + 1'b1;
ss_cpu <= ss_frame;
bg_cpu <= bg_frame;
end
end
always @* case (io_port)
8'h78: io_q = frames;
8'h80: io_q = ss_cpu[7:0];
8'h81: io_q = ss_cpu[15:8];
8'h82: io_q = ss_cpu[23:16];
8'h83: io_q = ss_cpu[31:24];
8'h84: io_q = bg_cpu[7:0];
8'h85: io_q = bg_cpu[15:8];
8'h86: io_q = bg_cpu[23:16];
8'h87: io_q = bg_cpu[31:24];
default: io_q = 8'hFF;
endcase
endmodule
+789
View File
@@ -0,0 +1,789 @@
`timescale 1ns / 1ps
// VT220 terminal engine with ANSI colour. Consumes received bytes and keeps
// the character buffer up to date: 80x32, or 132x64 after DECCOLM (CSI ?3h).
// The 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.
//
// Supported (beyond C0 controls):
// 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) 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.
// OSC, DCS, APC, PM and SOS strings (e.g. titles, OSC 3008) are discarded.
// Scrolling copies rows in memory. With smooth scroll on, a line feed at the
// bottom margin first slides the region up a few pixels per frame.
module term_ctrl (
input clk,
input rst,
input [7:0] in_byte,
input in_empty,
output reg in_rd,
input lf_cr, // LF also returns the carriage
input clear_req,
input [3:0] def_fg, // default foreground colour index
input smooth_sw, // force smooth scrolling on
input smooth_fast,
input frame_tick, // start of vertical blanking
// character RAM port
output reg [13:0] addr,
output reg we,
output reg [17:0] wdata,
input [17:0] rdata,
// to the display
output reg [5:0] row,
output reg [7:0] col,
output reg cur_vis,
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,
// replies to the host (DA, DSR)
output reg [7:0] resp_byte,
output reg resp_wr,
input resp_full,
// modes the keyboard needs
output reg cursor_app,
output reg keypad_app,
output reg lnm,
output reg [3:0] dec_leds,
output reg bell,
output reg alt_scr, // showing and writing the alternate screen
output reg wide // 132 columns (DECCOLM)
);
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,
S_PUT = 4'd8, S_TAB = 4'd9, S_SMOOTH = 4'd10, S_SMEND = 4'd11,
S_OP = 4'd12, S_EXEC = 4'd13, S_SCR1 = 4'd14, S_SCR2 = 4'd15;
localparam [2:0] P_NORM = 3'd0, P_ESC = 3'd1, P_ESCI = 3'd2, P_CSI = 3'd3, P_STR = 3'd4;
localparam [1:0] K_SGR = 2'd0, K_SET = 2'd1, K_RST = 2'd2, K_LL = 2'd3;
reg [3:0] st, ret_st, sm_ret;
reg [2:0] ps;
reg [7:0] c;
// modes and attributes
reg pend, awm, irm, om, sclm;
reg bold, ul, blink, rev, conceal;
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 [131:0] tabs;
reg colm_clr; // DECCOLM: clear the screen when idle
// saved cursor (DECSC)
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;
// parser
reg [7:0] par [0:15];
reg [3:0] pidx;
reg priv, gtm, ign;
reg [7:0] cint, inter;
// scroll request
reg sc_dir;
reg [5:0] sc_t, sc_b;
reg [7:0] sc_n;
reg [6:0] sc_h, sc_k; // region height, lines to move
// memory copy / fill engine
reg [13:0] c_src, c_dst, c_cnt, f_addr, f_cnt;
reg c_dir;
reg [17:0] f_cell;
// parameter loop (SGR, SM/RM, DECLL)
reg [1:0] pk;
reg [3:0] pi;
reg [1:0] ext; // 38/48 extended colour state
reg ext_bg;
reg [1:0] ext_n;
reg [7:0] ext_r, ext_g;
reg [95:0] rbuf;
reg [3:0] rlen;
reg [7:0] rep;
reg tdir;
integer i;
// ------------------------------------------------------------ helpers
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 [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);
reg [11:0] v;
begin
v = p * 10 + (d - "0");
acc = v > 255 ? 8'd255 : v[7:0];
end
endfunction
// DEC special graphics to CP437
function [7:0] gfx(input [7:0] ch);
case (ch)
8'h5F: gfx = 8'h20; 8'h60: gfx = 8'h04; 8'h61: gfx = 8'hB1;
8'h66: gfx = 8'hF8; 8'h67: gfx = 8'hF1; 8'h6A: gfx = 8'hD9;
8'h6B: gfx = 8'hBF; 8'h6C: gfx = 8'hDA; 8'h6D: gfx = 8'hC0;
8'h6E: gfx = 8'hC5; 8'h6F: gfx = 8'hC4; 8'h70: gfx = 8'hC4;
8'h71: gfx = 8'hC4; 8'h72: gfx = 8'hC4; 8'h73: gfx = 8'hC4;
8'h74: gfx = 8'hC3; 8'h75: gfx = 8'hB4; 8'h76: gfx = 8'hC1;
8'h77: gfx = 8'hC2; 8'h78: gfx = 8'hB3; 8'h79: gfx = 8'hF3;
8'h7A: gfx = 8'hF2; 8'h7B: gfx = 8'hE3; 8'h7C: gfx = 8'hD8;
8'h7D: gfx = 8'h9C; 8'h7E: gfx = 8'hFA;
default: gfx = 8'hFE; // control-code pictures
endcase
endfunction
function [1:0] charset(input [7:0] ch);
charset = ch == "0" ? 2'd1 : ch == "A" ? 2'd2 : 2'd0;
endfunction
// colour index from levels: bit0 red, bit1 green, bit2 blue, bit3 bright
function [3:0] approx(input hr, input hg, input hb, input bright, input dim);
approx = (hr | hg | hb) ? {bright, hb, hg, hr} : (dim ? 4'd8 : 4'd0);
endfunction
function [3:0] map256(input [7:0] n);
reg [7:0] k;
reg [2:0] r, g, b, mx;
begin
if (n < 16)
map256 = n[3:0];
else if (n >= 232)
map256 = n < 238 ? 4'd0 : n < 244 ? 4'd8 : n < 250 ? 4'd7 : 4'd15;
else begin
k = n - 8'd16;
r = k / 36;
g = (k / 6) % 6;
b = k % 6;
mx = r > g ? (r > b ? r : b) : (g > b ? g : b);
map256 = approx(r >= 3, g >= 3, b >= 3, mx >= 4, mx >= 2);
end
end
endfunction
function [3:0] rgb16(input [7:0] r, input [7:0] g, input [7:0] b);
reg [7:0] mx;
begin
mx = r > g ? (r > b ? r : b) : (g > b ? g : b);
rgb16 = approx(r[7], g[7], b[7], mx >= 192, mx >= 64);
end
endfunction
function [7:0] next_tab(input [131:0] t, input [7:0] cc, input [7:0] last);
integer k;
begin
next_tab = last;
for (k = 131; k >= 0; k = k - 1)
if (k > cc && k < last && t[k]) next_tab = k;
end
endfunction
function [7:0] prev_tab(input [131:0] t, input [7:0] cc);
integer k;
begin
prev_tab = 8'd0;
for (k = 0; k < 132; k = k + 1)
if (k < cc && t[k]) prev_tab = k;
end
endfunction
// --------------------------------------------------- derived values
wire [17:0] BLANK = {2'b00, 4'd0, def_fg, 8'h20};
wire [7:0] p0 = par[0];
wire [7:0] p1 = par[1];
reg [7:0] n8; // count parameter (default 1), registered
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;
end
wire [3:0] fgb = bold ? (fg | 4'd8) : fg;
wire [3:0] dfg = rev ? bg : fgb;
wire [3:0] dbg = rev ? fgb : bg;
wire [1:0] cs = gl ? g1 : g0;
wire [7:0] glyph = (cs == 2'd1 && c >= 8'h5F && c <= 8'h7E) ? gfx(c) :
(cs == 2'd2 && c == "#") ? 8'h9C : c;
wire [17:0] new_cell = {blink, ul, dbg, conceal ? dbg : dfg, glyph};
wire smooth = sclm | smooth_sw;
wire [4:0] sm_step = smooth_fast ? 5'd4 : 5'd2;
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 : 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 [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 [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;
ret_st <= rs;
st <= S_OP; // decided next clock, off the critical path
end
endtask
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 [5:0] t, input [5:0] b, input [7:0] n, input [3:0] rs);
begin
sc_dir <= dir;
sc_t <= t;
sc_b <= b;
sc_n <= n;
ret_st <= rs;
st <= S_SCR1;
end
endtask
task line_feed(input [3:0] rs);
begin
if (row == sbot) begin
if (smooth) begin
sm_on <= 1'b1; sm_dir <= 1'b0; sm_off <= 5'd0; sm_ret <= rs;
st <= S_SMOOTH;
end else
scroll(1'b0, stop, sbot, 8'd1, rs);
end else begin
if (row != LAST_ROW) row <= row + 1'b1;
st <= rs;
end
end
endtask
task reverse_index;
begin
if (row == stop) begin
if (smooth) begin
sm_on <= 1'b1; sm_dir <= 1'b1; sm_off <= 5'd0; sm_ret <= S_IDLE;
st <= S_SMOOTH;
end else
scroll(1'b1, stop, sbot, 8'd1, S_IDLE);
end else if (row != 0)
row <= row - 1'b1;
end
endtask
task reset_attrs;
begin
{bold, ul, blink, rev, conceal} <= 5'b0;
fg <= def_fg;
bg <= 4'd0;
end
endtask
task soft_reset;
begin
reset_attrs;
{pend, irm, om, cursor_app, keypad_app, gl} <= 6'b0;
{g0, g1} <= 4'b0;
awm <= 1'b1;
cur_vis <= 1'b1;
alt_scr <= 1'b0;
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;
sv_fg <= def_fg;
end
endtask
task full_reset;
begin
soft_reset;
{row, col} <= 0;
{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 < 132; i = i + 1) tabs[i] <= i != 0 && i % 8 == 0;
fill(14'd0, ALL);
end
endtask
task respond(input [95:0] s, input [3:0] n);
begin
rbuf <= s;
rlen <= n;
st <= S_RESP;
end
endtask
function [7:0] tens(input [7:0] v);
tens = "0" + (v / 8'd10) % 8'd10;
endfunction
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
// one SGR parameter
task sgr(input [7:0] v);
begin
case (ext)
2'd0:
if (v == 0) reset_attrs;
else if (v == 1) bold <= 1'b1;
else if (v == 4 || v == 21) ul <= 1'b1;
else if (v == 5 || v == 6) blink <= 1'b1;
else if (v == 7) rev <= 1'b1;
else if (v == 8) conceal <= 1'b1;
else if (v == 22) bold <= 1'b0;
else if (v == 24) ul <= 1'b0;
else if (v == 25) blink <= 1'b0;
else if (v == 27) rev <= 1'b0;
else if (v == 28) conceal <= 1'b0;
else if (v >= 30 && v <= 37) fg <= v - 8'd30;
else if (v == 38 || v == 48) begin ext <= 2'd1; ext_bg <= v == 48; end
else if (v == 39) fg <= def_fg;
else if (v >= 40 && v <= 47) bg <= v - 8'd40;
else if (v == 49) bg <= 4'd0;
else if (v >= 90 && v <= 97) fg <= v - 8'd82;
else if (v >= 100 && v <= 107) bg <= v - 8'd92;
2'd1: // after 38/48: 5 = indexed, 2 = RGB
if (v == 5) ext <= 2'd2;
else if (v == 2) begin ext <= 2'd3; ext_n <= 2'd0; end
else ext <= 2'd0;
2'd2: begin
if (ext_bg) bg <= map256(v); else fg <= map256(v);
ext <= 2'd0;
end
2'd3: begin
ext_n <= ext_n + 1'b1;
if (ext_n == 0) ext_r <= v;
else if (ext_n == 1) ext_g <= v;
else begin
if (ext_bg) bg <= rgb16(ext_r, ext_g, v); else fg <= rgb16(ext_r, ext_g, v);
ext <= 2'd0;
end
end
endcase
end
endtask
// one SM/RM parameter
task set_mode(input [7:0] v, input on);
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 : 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;
end else begin
if (v == 4) irm <= on;
if (v == 20) lnm <= on;
end
end
endtask
task plist(input [1:0] kind);
begin
pk <= kind;
pi <= 4'd0;
ext <= 2'd0;
pv_ok <= 1'b0;
st <= S_PLOOP;
end
endtask
// ------------------------------------------------------------ main
always @(posedge clk) begin
in_rd <= 1'b0;
we <= 1'b0;
bell <= 1'b0;
resp_wr <= 1'b0;
if (rst) begin
full_reset;
cursor_app <= 1'b0;
end else case (st)
S_IDLE:
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;
st <= S_DISP;
end
// ---- 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[6:0];
st <= S_SCR2;
end
S_SCR2:
if (!sc_dir)
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, 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:
st <= c_cnt != 0 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st;
S_CRD: begin
addr <= c_src;
st <= S_CWAIT;
end
S_CWAIT:
st <= S_CWR;
S_CWR: begin
addr <= c_dst;
wdata <= rdata;
we <= 1'b1;
c_src <= c_dir ? c_src - 1'b1 : c_src + 1'b1;
c_dst <= c_dir ? c_dst - 1'b1 : c_dst + 1'b1;
c_cnt <= c_cnt - 1'b1;
st <= c_cnt != 1 ? S_CRD : f_cnt != 0 ? S_FILL : ret_st;
end
S_FILL: begin
addr <= f_addr;
wdata <= f_cell;
we <= 1'b1;
f_addr <= f_addr + 1'b1;
f_cnt <= f_cnt - 1'b1;
if (f_cnt == 1) st <= ret_st;
end
S_SMOOTH:
// the region slides during active video; the copy runs in blanking
if (frame_tick) begin
if (sm_off + sm_step >= 16)
scroll(sm_dir, stop, sbot, 8'd1, S_SMEND);
else
sm_off <= sm_off + sm_step;
end
S_SMEND: begin
sm_on <= 1'b0;
sm_off <= 5'd0;
st <= sm_ret;
end
S_RESP:
if (!resp_full) begin
resp_byte <= rbuf[95:88];
resp_wr <= 1'b1;
rbuf <= rbuf << 8;
rlen <= rlen - 1'b1;
if (rlen == 1) st <= S_IDLE;
end
S_TAB: begin
col <= tdir ? prev_tab(tabs, col) : next_tab(tabs, col, LAST_COL);
rep <= rep - 1'b1;
if (rep == 1) st <= S_IDLE;
end
S_PLOOP: if (!pv_ok) begin
// fetch the parameter one clock ahead, then act on it
pv <= par[pi];
pv_ok <= 1'b1;
end else begin
pv_ok <= 1'b0;
case (pk)
K_SGR: sgr(pv);
K_SET: set_mode(pv, 1'b1);
K_RST: set_mode(pv, 1'b0);
K_LL:
if (pv == 0) dec_leds <= 4'd0;
else if (pv >= 1 && pv <= 4) dec_leds[pv - 1] <= 1'b1;
else if (pv >= 21 && pv <= 24) dec_leds[pv - 21] <= 1'b0;
endcase
pi <= pi + 1'b1;
if (pi == pidx) st <= S_IDLE;
end
S_PUT: begin // write the character at the cursor
we <= 1'b1;
addr <= here;
wdata <= new_cell;
if (col == LAST_COL) pend <= awm;
else col <= col + 1'b1;
st <= S_IDLE;
end
S_DISP: begin
st <= S_IDLE;
if (ps == P_STR) begin
// inside OSC/DCS/APC/PM/SOS: discard until BEL or ESC \ (ST)
if (c == 8'h07 || c == 8'h18 || c == 8'h1A) ps <= P_NORM;
else if (c == 8'h1B) ps <= P_ESC;
end else if (c == 8'h1B) begin
ps <= P_ESC;
end else if (c == 8'h18 || c == 8'h1A) begin
ps <= P_NORM; // CAN / SUB cancel a sequence
end else if (c < 8'h20) begin
// C0 controls act immediately, even inside an escape sequence
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, LAST_COL); end
8'h0A, 8'h0B, 8'h0C: begin
pend <= 1'b0;
if (lf_cr || lnm) col <= 8'd0;
line_feed(S_IDLE);
end
8'h0D: begin pend <= 1'b0; col <= 8'd0; end
8'h0E: gl <= 1'b1;
8'h0F: gl <= 1'b0;
default: ;
endcase
end else if (c == 8'h7F) begin
// DEL is ignored
end else case (ps)
P_NORM:
if (pend && awm) begin
pend <= 1'b0;
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), {6'd0, LAST_COL - col}, 1'b1,
14'd0, 14'd0, BLANK, S_PUT);
else
st <= S_PUT;
P_ESC: begin
ps <= P_NORM;
if (c >= 8'h20 && c <= 8'h2F) begin
inter <= c;
ps <= P_ESCI;
end else case (c)
"]", "P", "_", "^", "X":
ps <= P_STR; // string sequences are ignored
"[": begin
ps <= P_CSI;
for (i = 0; i < 16; i = i + 1) par[i] <= 8'd0;
{pidx, priv, gtm, ign} <= 0;
cint <= 8'd0;
end
"D": begin pend <= 1'b0; 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
{sv_row, sv_col, sv_pend, sv_om, sv_gl} <= {row, col, pend, om, gl};
{sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal} <= {bold, ul, blink, rev, conceal};
{sv_fg, sv_bg, sv_g0, sv_g1} <= {fg, bg, g0, g1};
end
"8": begin
{row, col, pend, om, gl} <= {sv_row, sv_col, sv_pend, sv_om, sv_gl};
{bold, ul, blink, rev, conceal} <= {sv_bold, sv_ul, sv_blink, sv_rev, sv_conceal};
{fg, bg, g0, g1} <= {sv_fg, sv_bg, sv_g0, sv_g1};
end
"=": keypad_app <= 1'b1;
">": keypad_app <= 1'b0;
"Z": respond({8'h1B, "[?62;22c", 24'd0}, 4'd9);
"c": begin full_reset; cursor_app <= 1'b0; end
default: ;
endcase
end
P_ESCI: begin
ps <= P_NORM;
if (c >= 8'h20 && c <= 8'h2F)
ps <= P_ESCI; // further intermediates
else case (inter)
"(": g0 <= charset(c);
")": g1 <= charset(c);
"#": if (c == "8") begin // DECALN: fill the screen with E
{row, col, pend, stop} <= 0;
sbot <= LAST_ROW;
op(14'd0, 14'd0, 14'd0, 1'b0, 14'd0, CELLS, {2'b00, 4'd0, def_fg, "E"}, S_IDLE);
end
default: ;
endcase
end
P_CSI:
if (c >= "0" && c <= "9")
par[pidx] <= acc(par[pidx], c);
else if (c == ";" || c == ":") begin
if (pidx != 4'd15) pidx <= pidx + 1'b1;
end else if (c == "?")
priv <= 1'b1;
else if (c == ">")
gtm <= 1'b1;
else if (c == "<" || c == "=")
ign <= 1'b1;
else if (c >= 8'h20 && c <= 8'h2F)
cint <= c;
else if (c >= 8'h40 && c <= 8'h7E) begin
ps <= P_NORM;
st <= S_EXEC; // run it next clock, off the parser's path
end
default: ps <= P_NORM;
endcase
end
// ---- CSI final byte (in c): parameters and cursor are unchanged
S_EXEC: begin
st <= S_IDLE;
if (ign) begin
end else if (cint == "!") begin
if (c == "p") soft_reset;
end else if (cint != 0) begin
end else if (gtm) begin
if (c == "c") respond({8'h1B, "[>1;10;0c", 16'd0}, 4'd10);
end else if (priv) begin
case (c)
"h": plist(K_SET);
"l": plist(K_RST);
"J": begin
pend <= 1'b0;
if (p0 == 0) fill(here, CELLS - here);
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), STRIDE);
end
"n": if (p0 == 6) cursor_report;
default: ;
endcase
end else case (c)
"A": begin pend <= 1'b0; row <= row_up; end
"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 <= 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
"I": begin pend <= 1'b0; rep <= n8; tdir <= 1'b0; st <= S_TAB; end
"Z": begin pend <= 1'b0; rep <= n8; tdir <= 1'b1; st <= S_TAB; end
"J": begin
pend <= 1'b0;
if (p0 == 0) fill(here, CELLS - here);
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), STRIDE);
end
"L": if (row >= stop && row <= sbot) begin
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 <= 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), {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, {6'd0, room - nc}, 1'b0,
rc(row, NCOLS - nc), {6'd0, nc}, BLANK, S_IDLE);
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 <= 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 > 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
"s": {sv_row, sv_col, sv_pend} <= {row, col, pend};
"u": {row, col, pend} <= {sv_row, sv_col, sv_pend};
"n": if (p0 == 5) respond({8'h1B, "[0n", 64'd0}, 4'd4);
else if (p0 == 6) cursor_report;
"c": if (p0 == 0) respond({8'h1B, "[?62;22c", 24'd0}, 4'd9);
default: ;
endcase
end
default: st <= S_IDLE;
endcase
end
endmodule
+245
View File
@@ -0,0 +1,245 @@
`timescale 1ns / 1ps
// 1280x1024 @ 60 Hz VGA text display (108 MHz pixel clock), two layouts:
// 80 columns: 80x32 cells of 16x32 pixels (the 8x16 font doubled)
// 132 columns: 132x64 cells of 8x16 pixels, centred (112-pixel side borders)
// 16 colours, underline, blink, block cursor and smooth-scroll offset for the
// scroll region. The character buffer has a 132-cell stride in both layouts.
// Hardware sprites (sprites.v) sit between the graphics and the text.
// A 320x240 graphics layer (each pixel 4x4, the top 960 lines, so 80-column
// text row r still starts at graphics line 8(r-1)) shows through wherever the
// text is background; colour 0 is transparent. It scrolls: the picture starts
// at (scroll_x, scroll_y) of the buffer and wraps round at 320 and 240. With
// g_border the graphics are hidden in a border (4 columns each side, 2 rows
// top and bottom), where a scrolling program can draw what comes next unseen.
// One pixel per clock, pipelined:
// clock 0 position -> row, column, glyph line (registered)
// clock 1 character RAM and graphics reads
// clock 2 font ROM read
// clock 3 colour out
module text_display #(
parameter FONT_FILE = "font.mem"
) (
input clk, // pixel clock, 108 MHz
input rst,
input wide, // 132-column layout
input [5:0] cur_row,
input [7:0] cur_col,
input cur_vis,
input [5:0] stop, // scroll region (rows, inclusive)
input [5:0] sbot,
input sm_on, // smooth scroll in progress
input sm_dir, // 0 = up, 1 = down
input [4:0] sm_off, // font lines the region has moved
output [13:0] ram_addr, // row * 132 + column
input [17:0] ram_data, // {blink, underline, bg, fg, code}
output [16:0] gfx_addr, // graphics pixel, read a clock later
input [3:0] gfx_pix,
input [8:0] scroll_x, // graphics pixel shown at the top left
input [7:0] scroll_y,
input g_border, // hide 4 graphics columns each side, 2 rows top and bottom
// sprites: the line to prepare next, and the pixel on screen
output reg sp_go,
output reg [7:0] sp_line,
output sp_buf,
output [8:0] sp_x,
output sp_on,
input [3:0] sp_pix, // read a clock after sp_x, like gfx_pix
output reg [3:0] red,
output reg [3:0] green,
output reg [3:0] blue,
output reg hsync,
output reg vsync,
output reg frame_tick // one clock at the start of vertical blanking
);
localparam H_VIS = 1280, H_FP = 48, H_SYNC = 112, H_TOTAL = 1688;
localparam V_VIS = 1024, V_FP = 1, V_SYNC = 3, V_TOTAL = 1066;
localparam WIDE_X0 = 112; // 132 x 8 = 1056 pixels, centred
reg [10:0] h, v;
reg [5:0] frame = 6'd0;
always @(posedge clk) begin
frame_tick <= h == 0 && v == V_VIS;
// each graphics line is 4 scan lines; ask for the next one as it starts
sp_go <= h == 0 && v[1:0] == 2'd0 && (v < 11'd956 || v == 11'd1064);
sp_line <= v == 11'd1064 ? 8'd0 : v[9:2] + 1'b1;
if (rst) begin
h <= 0;
v <= 0;
end else if (h == H_TOTAL - 1) begin
h <= 0;
if (v == V_TOTAL - 1) begin
v <= 0;
frame <= frame + 1'b1;
end else
v <= v + 1'b1;
end else
h <= h + 1'b1;
end
// graphics pixel for this h, v with the scroll added: counters set from
// the scroll registers at the end of each line and each frame
reg [8:0] g_col = 9'd0;
reg [7:0] g_row = 8'd0;
reg [1:0] g_sub = 2'd0;
always @(posedge clk)
if (h == H_TOTAL - 1) begin
g_col <= scroll_x;
g_sub <= 2'd0;
if (v == V_TOTAL - 1) g_row <= scroll_y;
else if (v[1:0] == 2'd3) g_row <= g_row == 8'd239 ? 8'd0 : g_row + 1'b1;
end else begin
g_sub <= g_sub + 1'b1;
if (g_sub == 2'd3) g_col <= g_col >= 9'd319 ? 9'd0 : g_col + 1'b1;
end
// ---- clock 0: where this pixel is in the text, in font units
// yl counts font lines (a 16-line font: 32 pixels a row in 80 columns)
wire [9:0] yl = wide ? v[9:0] : {1'b0, v[9:1]};
wire [10:0] xw = h - WIDE_X0;
wire [10:0] xl = wide ? xw : {1'b0, h[10:1]}; // font pixels across
wire text_on = wide ? h >= WIDE_X0 && h < WIDE_X0 + 1056 : 1'b1;
wire in_region = sm_on && v < V_VIS && yl[9:4] >= stop && yl[9:4] <= sbot;
wire [9:0] ys = !in_region ? yl : sm_dir ? yl - sm_off : yl + sm_off;
wire gap = in_region && (sm_dir ? yl < {stop, 4'b0} + sm_off : ys[9:4] > sbot);
wire [7:0] tcol = xl[10:3];
wire [5:0] trow = ys[9:4];
reg [13:0] a_base; // row * 132
reg [7:0] a_col;
reg [3:0] a_line;
reg [2:0] a_bit;
reg a_blank, a_text, a_vis, a_cur, a_hs, a_vs, a_gon, a_ghide;
wire [8:0] gcol_s = h[10:2]; // graphics pixel on the screen, unscrolled
wire [7:0] grow_s = v[9:2];
reg [16:0] a_gfx;
reg [8:0] a_spx;
reg a_spbuf;
always @(posedge clk) begin
a_base <= {trow, 7'd0} + {6'd0, trow, 2'd0};
a_col <= tcol;
a_line <= ys[3:0];
a_bit <= xl[2:0];
a_blank <= gap;
a_text <= text_on;
a_vis <= h < H_VIS && v < V_VIS;
a_cur <= cur_vis && frame[5] && tcol == cur_col && ys[9:4] == cur_row && !gap;
a_hs <= h >= H_VIS + H_FP && h < H_VIS + H_FP + H_SYNC; // positive sync
a_vs <= v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC;
a_gon <= v < 960;
a_ghide <= g_border && (gcol_s < 9'd4 || gcol_s >= 9'd316 || grow_s < 8'd2 || grow_s >= 8'd238);
a_gfx <= {1'b0, g_row, 8'd0} + {3'd0, g_row, 6'd0} + {8'd0, g_col};
a_spx <= h[10:2];
a_spbuf <= v[2];
end
// ---- clock 1: character RAM and graphics reads
assign ram_addr = a_base + {6'd0, a_col};
assign gfx_addr = a_gfx;
assign sp_x = a_spx;
assign sp_buf = a_spbuf;
assign sp_on = a_gon;
reg [3:0] b_line;
reg [2:0] b_bit;
reg b_blank, b_text, b_vis, b_cur, b_hs, b_vs, b_gon, b_ghide;
always @(posedge clk)
{b_line, b_bit, b_blank, b_text, b_vis, b_cur, b_hs, b_vs, b_gon, b_ghide} <=
{a_line, a_bit, a_blank, a_text, a_vis, a_cur, a_hs, a_vs, a_gon, a_ghide};
// ---- clock 2: font ROM
reg [7:0] font [0:4095];
initial $readmemh(FONT_FILE, font);
reg [7:0] glyph;
reg [17:0] c_cell;
reg [3:0] c_gfx, c_spr, c_line;
reg [2:0] c_bit;
reg c_blank, c_text, c_vis, c_cur, c_hs, c_vs;
always @(posedge clk) begin
glyph <= font[{ram_data[7:0], b_line}];
c_cell <= ram_data;
c_gfx <= b_gon && !b_ghide ? gfx_pix : 4'd0;
c_spr <= sp_pix;
{c_line, c_bit, c_blank, c_text, c_vis, c_cur, c_hs, c_vs} <=
{b_line, b_bit, b_blank, b_text, b_vis, b_cur, b_hs, b_vs};
end
function [11:0] palette(input [3:0] n);
case (n)
4'd0: palette = 12'h000; 4'd1: palette = 12'hA00;
4'd2: palette = 12'h0A0; 4'd3: palette = 12'hA50;
4'd4: palette = 12'h00A; 4'd5: palette = 12'hA0A;
4'd6: palette = 12'h0AA; 4'd7: palette = 12'hAAA;
4'd8: palette = 12'h555; 4'd9: palette = 12'hF55;
4'd10: palette = 12'h5F5; 4'd11: palette = 12'hFF5;
4'd12: palette = 12'h55F; 4'd13: palette = 12'hF5F;
4'd14: palette = 12'h5FF; 4'd15: palette = 12'hFFF;
endcase
endfunction
// ---- clock 3: colour
wire ink = (glyph[~c_bit] | (c_cell[16] && c_line == 4'd15)) && !(c_cell[17] && frame[5]);
wire fg = c_text && !c_blank && (ink ^ c_cur);
wire [3:0] idx = fg ? c_cell[11:8] : c_spr != 4'd0 ? c_spr : c_gfx != 4'd0 ? c_gfx :
c_blank || !c_text ? 4'd0 : c_cell[15:12];
always @(posedge clk) begin
{red, green, blue} <= c_vis ? palette(idx) : 12'h000;
hsync <= c_hs;
vsync <= c_vs;
end
endmodule
// Two character buffers of 132x64 cells (the normal and the alternate screen,
// picked by a_alt/b_alt): read/write port for the terminal engine, read-only
// port for video on the pixel clock.
module char_ram (
input clk,
input a_alt,
input [13:0] a_addr,
input a_we,
input [17:0] a_wdata,
output [17:0] a_rdata,
input vclk,
input b_alt,
input [13:0] b_addr,
output [17:0] b_rdata
);
// Two pages of 132 x 64 cells: 16896, just past 16K. Kept as a 16K array
// and a 512-entry one, so it takes about 9.5 block RAMs rather than 18
// (one array of 16896 would be built as 32K deep).
localparam PAGE = 132 * 64;
(* ram_style = "block" *) reg [17:0] lo [0:16383];
(* ram_style = "block" *) reg [17:0] hi [0:2 * PAGE - 16384 - 1];
integer i;
initial begin
for (i = 0; i < 16384; i = i + 1) lo[i] = {10'h00A, 8'h20};
for (i = 0; i < 2 * PAGE - 16384; i = i + 1) hi[i] = {10'h00A, 8'h20};
end
wire [14:0] aa = {1'b0, a_addr} + (a_alt ? PAGE : 0);
wire [14:0] ba = {1'b0, b_addr} + (b_alt ? PAGE : 0);
reg [17:0] a_lo, a_hi, b_lo, b_hi;
reg a_sel, b_sel;
always @(posedge clk) begin
if (a_we && !aa[14]) lo[aa[13:0]] <= a_wdata;
a_lo <= lo[aa[13:0]];
end
always @(posedge clk) begin
if (a_we && aa[14]) hi[aa[8:0]] <= a_wdata;
a_hi <= hi[aa[8:0]];
a_sel <= aa[14];
end
assign a_rdata = a_sel ? a_hi : a_lo;
always @(posedge vclk) begin
b_lo <= lo[ba[13:0]];
b_hi <= hi[ba[8:0]];
b_sel <= ba[14];
end
assign b_rdata = b_sel ? b_hi : b_lo;
// whole-array view for testbenches
function [17:0] cell_at(input [14:0] n);
cell_at = n[14] ? hi[n[8:0]] : lo[n[13:0]];
endfunction
endmodule
+106
View File
@@ -0,0 +1,106 @@
`timescale 1ns / 1ps
// 8N1 UART receiver and transmitter. div = clock frequency / baud rate.
module uart_rx (
input clk,
input rst,
input [13:0] div,
input rx,
output reg [7:0] data,
output reg valid
);
localparam IDLE = 3'd0, START = 3'd1, DATA = 3'd2, STOP = 3'd3, BREAK = 3'd4;
reg [2:0] sync = 3'b111;
reg [2:0] st;
reg [13:0] cnt;
reg [2:0] bitn;
reg [7:0] sh;
wire rxs = sync[2];
always @(posedge clk) sync <= {sync[1:0], rx};
always @(posedge clk) begin
valid <= 1'b0;
if (rst) begin
st <= IDLE;
end else case (st)
IDLE:
if (!rxs) begin
st <= START;
cnt <= {1'b0, div[13:1]}; // sample mid-bit
end
START:
if (cnt == 0) begin
if (!rxs) begin
st <= DATA;
cnt <= div - 1'b1;
bitn <= 3'd0;
end else
st <= IDLE; // glitch, not a start bit
end else
cnt <= cnt - 1'b1;
DATA:
if (cnt == 0) begin
sh <= {rxs, sh[7:1]};
cnt <= div - 1'b1;
bitn <= bitn + 1'b1;
if (bitn == 3'd7) st <= STOP;
end else
cnt <= cnt - 1'b1;
STOP:
if (cnt == 0) begin
if (rxs) begin
data <= sh;
valid <= 1'b1;
st <= IDLE;
end else
st <= BREAK; // bad stop bit: drop it
end else
cnt <= cnt - 1'b1;
BREAK:
if (rxs) st <= IDLE; // wait for the line to go idle
default:
st <= IDLE;
endcase
end
endmodule
module uart_tx (
input clk,
input rst,
input [13:0] div,
input [7:0] data,
input start,
output reg tx = 1'b1,
output reg busy
);
reg [9:0] sh;
reg [3:0] n;
reg [13:0] cnt;
always @(posedge clk) begin
if (rst) begin
tx <= 1'b1;
busy <= 1'b0;
end else if (!busy) begin
if (start) begin
sh <= {1'b1, data, 1'b0};
n <= 4'd0;
cnt <= 14'd0;
busy <= 1'b1;
end
end else if (cnt == 0) begin
if (n == 4'd10)
busy <= 1'b0;
else begin
tx <= sh[0];
sh <= {1'b1, sh[9:1]};
n <= n + 1'b1;
cnt <= div - 1'b1;
end
end else
cnt <= cnt - 1'b1;
end
endmodule
+12
View File
@@ -0,0 +1,12 @@
#!/bin/bash
# Build the bitstream; add "program" or "flash" to load it onto a connected board.
set -e
cd "$(dirname "$0")/.."
source scripts/vivado_env.sh
mkdir -p build
cd build
vivado -mode batch -nojournal -log build.log -source ../scripts/build.tcl
case "$1" in
program) vivado -mode batch -nojournal -log program.log -source ../scripts/program.tcl ;;
flash) vivado -mode batch -nojournal -log program.log -source ../scripts/program.tcl -tclargs flash ;;
esac
+37
View File
@@ -0,0 +1,37 @@
# Non-project Vivado build: vivado -mode batch -source scripts/build.tcl
# The CPU clock is 100 MHz * CPU_MULT / CPU_DIV (environment variables).
set root [file normalize [file dirname [info script]]/..]
set out $root/build
file mkdir $out
set mult [expr {[info exists ::env(CPU_MULT)] ? $::env(CPU_MULT) : 9.0}]
set div [expr {[info exists ::env(CPU_DIV)] ? $::env(CPU_DIV) : 10.0}]
set hz [expr {int(round(100e6 * $mult / $div))}]
puts "CPU clock: [expr {$hz / 1e6}] MHz"
read_verilog [glob $root/rtl/*.v]
read_mem $root/font/font.mem
read_mem $root/basic/basic.mem
read_mem $root/basic/basic-hi.mem
read_mem $root/monitor/monitor.mem
read_xdc $root/constraints/basys3.xdc
synth_design -top basys3_terminal -part xc7a35tcpg236-1 \
-generic CPU_MULT=$mult -generic CPU_DIV=$div -generic CPU_HZ=$hz
opt_design -directive Explore
place_design -directive ExtraTimingOpt
phys_opt_design -directive AggressiveExplore
route_design -directive Explore
phys_opt_design -directive AggressiveExplore
write_checkpoint -force $out/routed.dcp
report_utilization -file $out/utilization.rpt
report_timing_summary -file $out/timing.rpt
report_timing -max_paths 30 -nworst 1 -unique_pins -file $out/paths.rpt
write_bitstream -force $out/basys3_terminal.bit
write_cfgmem -force -format mcs -interface SPIx4 -size 4 \
-loadbit "up 0x0 $out/basys3_terminal.bit" $out/basys3_terminal.mcs
set wns [get_property SLACK [get_timing_paths -max_paths 1 -setup]]
puts "Worst setup slack: $wns ns"
if {$wns < 0} { error "timing not met" }
+52
View File
@@ -0,0 +1,52 @@
#!/bin/bash
# Build riscv-tests (rv32ui, rv32um, rv32mi) and our own tests in tests/, run
# them all on rtl/rv32_cpu.v in xsim. Fetches riscv-tests into third_party/.
# scripts/cputest.sh [name-filter]
set -e
cd "$(dirname "$0")/.."
ROOT=$(pwd)
source scripts/rv_env.sh
RT=$ROOT/third_party/riscv-tests
if [ ! -d "$RT/isa" ]; then
git clone --recursive https://github.com/riscv-software-src/riscv-tests.git "$RT"
fi
# Not applicable to this core:
# ma_data assumes misaligned loads/stores work; here they trap (allowed)
# pmpaddr needs PMP
SKIP="rv32ui-ma_data rv32mi-pmpaddr"
OUT=$ROOT/build/tests
mkdir -p "$OUT"
LIST=$OUT/list.txt
: > "$LIST"
build() { # name source
local name=$1 src=$2
case " $SKIP " in *" $name "*) return ;; esac
[ -n "$FILTER" ] && [[ $name != *$FILTER* ]] && return
${RV_PREFIX}gcc $RV_ARCH -static -mcmodel=medany -fvisibility=hidden -nostdlib -nostartfiles -Wl,--build-id=none \
-I"$RT/env/p" -I"$RT/isa/macros/scalar" -I"$ROOT/tests" -T"$ROOT/tests/link.ld" \
"$src" -o "$OUT/$name.elf"
${RV_PREFIX}objcopy -O binary "$OUT/$name.elf" "$OUT/$name.bin"
python3 "$ROOT/tools/bin2mem.py" "$OUT/$name.bin" "$OUT/$name.mem"
local th=$(${RV_PREFIX}nm "$OUT/$name.elf" | awk '$3=="tohost"{print $1}')
echo "$OUT/$name.mem $th $name" >> "$LIST"
}
FILTER=$1
for suite in rv32ui rv32um rv32mi; do
for t in $(sed -n "/^${suite}_sc_tests/,/^\$/p" "$RT/isa/$suite/Makefrag" | grep -v = | tr -d '\\'); do
build "$suite-$t" "$RT/isa/$suite/$t.S"
done
done
for s in tests/*.S; do [ -e "$s" ] || continue; build "own-$(basename "$s" .S)" "$s"; done
echo "$(wc -l < "$LIST") tests built"
source scripts/vivado_env.sh
mkdir -p sim/cpu
cd sim/cpu
rm -f xsim.log
xvlog "$ROOT/rtl/rv32_cpu.v" "$ROOT/sim/tb_cpu.v" | grep -E "^(ERROR|WARNING)" || true
[ -d xsim.dir/work ] && ! grep -q ERROR xvlog.log || { echo "compile failed"; exit 1; }
xelab -debug off tb_cpu -s tb_cpu >/dev/null
xsim tb_cpu -R -testplusarg list="$LIST" | grep -E '^(PASS|FAIL|[0-9]+ passed)'
grep -q " passed, 0 failed" xsim.log
+43
View File
@@ -0,0 +1,43 @@
# Program the Basys 3 over its USB-JTAG port.
# vivado -mode batch -source scripts/program.tcl (SRAM, lost at power-off)
# vivado -mode batch -source scripts/program.tcl -tclargs flash (QSPI flash, survives power-off)
set root [file normalize [file dirname [info script]]/..]
set mode [expr {[llength $argv] > 0 ? [lindex $argv 0] : "sram"}]
open_hw_manager
connect_hw_server
open_hw_target
set dev [lindex [get_hw_devices xc7a35t*] 0]
current_hw_device $dev
refresh_hw_device -update_hw_probes false $dev
if {$mode eq "flash"} {
# Board revisions differ: older Basys 3s have Spansion flash, newer ones Macronix.
# Vivado checks the chip ID before erasing, so a wrong guess is harmless.
set done 0
foreach part {mx25l3273f-spi-x1_x2_x4 s25fl032p-spi-x1_x2_x4} {
create_hw_cfgmem -hw_device $dev [lindex [get_cfgmem_parts $part] 0]
set mem [get_property PROGRAM.HW_CFGMEM $dev]
set_property PROGRAM.FILES [list $root/build/basys3_terminal.mcs] $mem
set_property PROGRAM.ADDRESS_RANGE use_file $mem
set_property PROGRAM.ERASE 1 $mem
set_property PROGRAM.CFG_PROGRAM 1 $mem
set_property PROGRAM.VERIFY 1 $mem
create_hw_bitstream -hw_device $dev [get_property PROGRAM.HW_CFGMEM_BITFILE $dev]
program_hw_devices $dev
if {![catch {program_hw_cfgmem -hw_cfgmem $mem}]} {
set done 1
break
}
delete_hw_cfgmem $mem
}
if {!$done} { error "flash part not recognised" }
# reload the FPGA from flash (works when JP1 is set to QSPI)
boot_hw_device $dev
puts "Flash programmed; with JP1 on QSPI the board now boots from it."
} else {
set_property PROGRAM.FILE $root/build/basys3_terminal.bit $dev
program_hw_devices $dev
puts "FPGA configured."
}
close_hw_manager
+13
View File
@@ -0,0 +1,13 @@
# Sourced by the build scripts: finds a RISC-V GCC.
# RV_PREFIX overrides; otherwise a riscv64/riscv32-unknown-elf-gcc on PATH, else
# the one that ships with Vivado (for MicroBlaze V; it has the rv32im multilib).
if [ -z "$RV_PREFIX" ]; then
for p in riscv64-unknown-elf- riscv32-unknown-elf-; do
command -v ${p}gcc >/dev/null && { RV_PREFIX=$p; break; }
done
fi
if [ -z "$RV_PREFIX" ]; then
RV_PREFIX=$(ls -d "$HOME"/vivado/*/gnu/riscv/lin/bin 2>/dev/null | sort -V | tail -1)/riscv64-unknown-elf-
fi
[ -x "${RV_PREFIX}gcc" ] || command -v "${RV_PREFIX}gcc" >/dev/null || { echo "no RISC-V gcc found; set RV_PREFIX" >&2; exit 1; }
RV_ARCH="-march=rv32im_zicsr_zifencei -mabi=ilp32"
+11
View File
@@ -0,0 +1,11 @@
#!/bin/bash
# Run the sound chip's testbench (sim/tb_sound.v) in Vivado's simulator.
set -e
cd "$(dirname "$0")/.."
ROOT=$(pwd)
source scripts/vivado_env.sh
mkdir -p sim/work-sound
cd sim/work-sound
xvlog "$ROOT"/rtl/sound.v "$ROOT"/sim/tb_sound.v
xelab -debug typical tb_sound -s tb_sound
xsim tb_sound -R
Executable
+25
View File
@@ -0,0 +1,25 @@
#!/bin/bash
# Run the whole-design testbench in Vivado's simulator; output lands in sim/work.
# scripts/sim.sh [quick] quick: only the computer, up to the monitor
set -e
cd "$(dirname "$0")/.."
ROOT=$(pwd)
source scripts/vivado_env.sh
DEFS=""
[ "$1" = quick ] && DEFS="-d SKIP_TERMINAL -d STOP_BEFORE_MONITOR"
MON=$ROOT/monitor/monitor.mem
[ -e "$MON" ] || { mkdir -p sim/work; MON=$ROOT/sim/work/empty.mem; : > "$MON"; }
mkdir -p sim/work
cd sim/work
xvlog -d SIMULATION $DEFS -d FONT_FILE="\"$ROOT/font/font.mem\"" -d MEM_FILE="\"$ROOT/basic/basic.mem\"" \
-d MEM_HI_FILE="\"$ROOT/basic/basic-hi.mem\"" -d MON_FILE="\"$MON\"" \
"$ROOT"/rtl/*.v "$ROOT"/sim/spiflash_model.v "$ROOT"/sim/tb_terminal.v | grep -E '^(ERROR|WARNING)' || true
xelab -debug off tb_terminal -s tb_terminal | grep -E '^(ERROR|WARNING)' || true
xsim tb_terminal -R | grep -v -E '^(INFO|#|\*\*|Vivado|source|run|exit|Time:|Copyright)'
if command -v python3 >/dev/null; then
for f in frame_*.ppm; do
[ -e "$f" ] || continue
python3 -c "import sys; from PIL import Image; Image.open(sys.argv[1]).save(sys.argv[2])" \
"$f" "${f%.ppm}.png" && echo "wrote sim/work/${f%.ppm}.png"
done
fi
+13
View File
@@ -0,0 +1,13 @@
# Synthesis only, for resource counts: vivado -mode batch -source scripts/synth.tcl
set root [file normalize [file dirname [info script]]/..]
set out $root/build
file mkdir $out
read_verilog [glob $root/rtl/*.v]
read_mem $root/font/font.mem
read_mem $root/basic/basic.mem
read_mem $root/basic/basic-hi.mem
read_mem $root/monitor/monitor.mem
read_xdc $root/constraints/basys3.xdc
synth_design -top basys3_terminal -part xc7a35tcpg236-1
report_utilization -file $out/synth_util.rpt
report_utilization -hierarchical -hierarchical_depth 2 -file $out/synth_hier.rpt
+14
View File
@@ -0,0 +1,14 @@
# Sourced by build.sh and sim.sh: sets up Vivado's environment.
# VIVADO picks the install; default is the newest in ~/vivado/<version>/Vivado.
VIVADO=${VIVADO:-$(ls -d "$HOME"/vivado/*/Vivado 2>/dev/null | sort -V | tail -1)}
source "$VIVADO/settings64.sh"
# Vivado still links against ncurses 5; newer distros only ship 6, which works.
COMPAT_LIB="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/build/compat-lib"
mkdir -p "$COMPAT_LIB"
for l in ncurses tinfo; do
if ! ldconfig -p | grep -q "lib$l.so.5" && [ -e /usr/lib/x86_64-linux-gnu/lib$l.so.6 ]; then
ln -sf /usr/lib/x86_64-linux-gnu/lib$l.so.6 "$COMPAT_LIB/lib$l.so.5"
fi
done
export LD_LIBRARY_PATH="$COMPAT_LIB${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
+82
View File
@@ -0,0 +1,82 @@
`timescale 1ns / 1ps
// Behavioural SPI flash (mode 0) with the commands the Z-80 uses:
// 03 read, 05 status, 06/04 write enable/disable, 9F ID (Macronix MX25L3273F),
// 02 page program, 20 4 KB erase. Reports any program or erase below PROTECT.
module spiflash_model #(
parameter PROTECT = 24'h240000
) (
input cs_n,
input sck,
input mosi,
output reg miso = 1'b1
);
reg [7:0] mem [0:(1 << 22) - 1];
reg [7:0] in_sh, out_sh, cmd;
reg [7:0] pagebuf [0:255];
reg [23:0] addr;
reg [2:0] bitn;
integer nbytes, ndata, i;
reg wel = 1'b0;
realtime busy_until = 0;
integer violations = 0, programs = 0, erases = 0;
initial for (i = 0; i < (1 << 22); i = i + 1) mem[i] = 8'hFF;
always @(negedge cs_n) begin
bitn = 0; nbytes = 0; ndata = 0; out_sh = 8'hFF;
end
always @(posedge sck) if (!cs_n) begin
in_sh = {in_sh[6:0], mosi};
bitn = bitn + 1;
if (bitn == 0) begin // a whole byte
if (nbytes == 0) cmd = in_sh;
else if (nbytes <= 3) addr = {addr[15:0], in_sh};
else if (cmd == 8'h02 && ndata < 256) begin pagebuf[ndata] = in_sh; ndata = ndata + 1; end
nbytes = nbytes + 1;
case (cmd)
8'h03: out_sh = nbytes >= 4 ? mem[addr + nbytes - 4] : 8'hFF;
8'h05: out_sh = {6'd0, wel, $realtime < busy_until};
8'h9F: out_sh = nbytes == 1 ? 8'hC2 : nbytes == 2 ? 8'h20 : 8'h16;
default: out_sh = 8'hFF;
endcase
end
end
always @(negedge sck) if (!cs_n) begin
miso = out_sh[7];
out_sh = {out_sh[6:0], 1'b1};
end
task busy_for(input integer ns);
busy_until = $realtime + ns;
endtask
always @(posedge cs_n) if (bitn == 0) begin
case (cmd)
8'h06: wel = 1'b1;
8'h04: wel = 1'b0;
8'h02: if (wel && nbytes >= 5) begin
if (addr < PROTECT) begin
$display("FLASH VIOLATION: program at %h", addr); violations = violations + 1;
end
for (i = 0; i < ndata; i = i + 1)
mem[{addr[23:8], addr[7:0] + i[7:0]}] = mem[{addr[23:8], addr[7:0] + i[7:0]}] & pagebuf[i];
programs = programs + 1;
wel = 1'b0;
busy_for(3000);
end
8'h20: if (wel && nbytes == 4) begin
if (addr < PROTECT) begin
$display("FLASH VIOLATION: erase at %h", addr); violations = violations + 1;
end
for (i = 0; i < 4096; i = i + 1) mem[{addr[23:12], 12'd0} + i] = 8'hFF;
erases = erases + 1;
wel = 1'b0;
busy_for(20000);
end
default: ;
endcase
end
endmodule
+118
View File
@@ -0,0 +1,118 @@
`timescale 1ns / 1ps
// Runs a list of test images on rv32_cpu, one after another.
// +list=<file>: lines of "<image.mem> <tohost address, hex> <name>"
// A test ends when it writes tohost: 1 is a pass, anything else a failure
// (riscv-tests put the failing test number in bits 31:1).
// The data bus answers after a pseudo-random 1-3 cycles to exercise d_ack.
// Address FFFF_FF00 is a test device: writing N raises the interrupt lines
// in N[5:0] (bit 0 soft, 1 timer, 2 external, 3 local 16, 4 nmi, 5 nmi_step)
// N[31:8] cycles later; any write first drops them. NMI_VEC is 8000h.
module tb_cpu;
reg clk = 0, rst = 1;
always #5 clk = !clk;
wire i_en, d_req, d_we;
wire [31:0] i_addr, d_addr, d_wdata;
wire [3:0] d_wstrb;
reg [31:0] i_rdata, d_rdata;
reg d_ack;
reg [5:0] irq;
wire retire;
rv32_cpu #(.NMI_VEC(32'h8000)) cpu (
.clk(clk), .rst(rst),
.i_en(i_en), .i_addr(i_addr), .i_rdata(i_rdata),
.d_req(d_req), .d_we(d_we), .d_wstrb(d_wstrb), .d_addr(d_addr),
.d_wdata(d_wdata), .d_rdata(d_rdata), .d_ack(d_ack),
.irq_soft(irq[0]), .irq_timer(irq[1]), .irq_ext(irq[2]),
.irq_local({15'd0, irq[3]}), .nmi(irq[4]), .nmi_step(irq[5]), .nmi_taken(),
.dbg_pc(), .dbg_decode(), .retire(retire)
);
localparam WORDS = 16384; // 64 KB
reg [31:0] mem [0:WORDS-1];
reg [31:0] tohost;
reg done;
reg [31:0] result;
always @(posedge clk) if (i_en) i_rdata <= mem[i_addr[15:2]];
// data bus with random wait states
reg [1:0] wait_n;
reg busy, bwe;
reg [31:0] baddr, bdata;
reg [3:0] bstrb;
reg [31:0] lfsr = 32'h1234_5678;
reg [5:0] irq_set;
reg [23:0] irq_delay;
always @(posedge clk) begin
lfsr <= {lfsr[30:0], lfsr[31] ^ lfsr[21] ^ lfsr[1] ^ lfsr[0]};
d_ack <= 1'b0;
if (d_req) begin
busy <= 1'b1; baddr <= d_addr; bwe <= d_we; bdata <= d_wdata; bstrb <= d_wstrb;
wait_n <= lfsr[1:0] == 2'd3 ? 2'd2 : lfsr[1:0];
end else if (busy) begin
if (wait_n != 0) wait_n <= wait_n - 2'd1;
else begin
busy <= 1'b0;
d_ack <= 1'b1;
d_rdata <= mem[baddr[15:2]];
if (bwe) begin
if (baddr == 32'hFFFF_FF00) begin
irq_set <= bdata[5:0]; irq_delay <= bdata[31:8];
irq <= 6'd0;
end else if (baddr == tohost) begin
done <= 1'b1; result <= bdata;
end else if (baddr[31:16] == 16'd0) begin
if (bstrb[0]) mem[baddr[15:2]][7:0] <= bdata[7:0];
if (bstrb[1]) mem[baddr[15:2]][15:8] <= bdata[15:8];
if (bstrb[2]) mem[baddr[15:2]][23:16] <= bdata[23:16];
if (bstrb[3]) mem[baddr[15:2]][31:24] <= bdata[31:24];
end
end
end
end
if (irq_set != 6'd0) begin
if (irq_delay == 0) begin irq <= irq_set; irq_set <= 6'd0; end
else irq_delay <= irq_delay - 1;
end
if (rst) begin
busy <= 1'b0; done <= 1'b0; irq <= 6'd0; irq_set <= 6'd0;
end
end
integer fd, n, cycles, pass = 0, fail = 0, i;
reg [8*200-1:0] memf, name, list;
initial begin
if (!$value$plusargs("list=%s", list)) begin
$display("need +list=file"); $finish;
end
fd = $fopen(list, "r");
while (!$feof(fd)) begin
n = $fscanf(fd, "%s %h %s\n", memf, tohost, name);
if (n == 3) begin
for (i = 0; i < WORDS; i = i + 1) mem[i] = 32'd0;
$readmemh(memf, mem);
rst = 1;
repeat (4) @(posedge clk);
rst = 0;
cycles = 0;
while (!done && cycles < 200000) begin
@(posedge clk);
cycles = cycles + 1;
end
if (done && result == 32'd1) begin
pass = pass + 1;
$display("PASS %0s (%0d cycles)", name, cycles);
end else begin
fail = fail + 1;
if (done) $display("FAIL %0s test %0d", name, result >> 1);
else $display("FAIL %0s timeout, pc=%h", name, cpu.pc);
end
end
end
$display("%0d passed, %0d failed", pass, fail);
$finish;
end
endmodule
+142
View File
@@ -0,0 +1,142 @@
`timescale 1ns / 1ps
// Testbench for rtl/sound.v at the Z-80's 90 MHz clock: pitch, mixer, noise,
// volume levels, the attack and decay envelopes, reading back and hushing.
// scripts/sim-sound.sh
module tb_sound;
reg clk = 0, rst = 1, hush = 0, io_wr = 0;
reg [7:0] port = 0, wd = 0;
wire [7:0] q;
wire audio;
integer errors = 0;
sound #(.CPU_HZ(90_000_000)) dut (
.clk(clk), .rst(rst), .hush(hush), .io_wr(io_wr), .io_port(port), .io_wd(wd),
.io_q(q), .audio(audio)
);
always #5.556 clk = ~clk;
task out(input [7:0] p, input [7:0] v);
begin
@(posedge clk) begin port <= p; wd <= v; io_wr <= 1; end
@(posedge clk) io_wr <= 0;
end
endtask
task rd(input [7:0] p); // q then holds the port's value
begin port = p; #1; end
endtask
task check(input [8*48-1:0] what, input ok);
begin
if (ok) $display("PASS %0s", what);
else begin $display("FAIL %0s", what); errors = errors + 1; end
end
endtask
// count rising edges of voice 0's tone over a time, and the output's duty
integer edges, ones, total;
real t0, duty;
task measure(input real ms);
begin
edges = 0; ones = 0; total = 0;
t0 = $realtime;
while ($realtime - t0 < ms * 1e6) begin
@(posedge clk);
total = total + 1;
ones = ones + audio;
end
duty = 1.0 * ones / total;
end
endtask
reg t_prev = 0;
integer tone_edges = 0;
always @(posedge clk) begin
t_prev <= dut.tone[0];
if (dut.tone[0] && !t_prev) tone_edges = tone_edges + 1;
end
real d15, d7;
integer n0, v, k, rose_at, fell_at;
initial begin
repeat (10) @(posedge clk);
rst = 0;
repeat (10) @(posedge clk);
measure(1.0);
check("no output after reset", ones == 0);
// voice 0 at 440 Hz: 125000 / 284 = 440.1
out(8'h90, 284 % 256); out(8'h91, 284 / 256);
out(8'h97, 8'b000_001);
out(8'h98, 8'd15);
n0 = tone_edges;
measure(50.0);
$display(" voice 0: %0d cycles in 50 ms (%.1f Hz), duty %.3f", tone_edges - n0,
(tone_edges - n0) / 0.05, duty);
check("pitch 284 plays about 440 Hz", tone_edges - n0 >= 21 && tone_edges - n0 <= 23);
d15 = duty;
check("one voice at full volume: high 1/8 of the time", d15 > 0.11 && d15 < 0.13);
out(8'h98, 8'd7);
measure(50.0);
d7 = duty;
$display(" volume 15 duty %.4f, volume 7 duty %.4f (ratio %.1f)", d15, d7, d15 / d7);
check("volume 7 is about 24 dB quieter", d15 / d7 > 12 && d15 / d7 < 20);
rd(8'h98);
check("volume reads back", q == 8'd7);
// decay: 15 steps of 2 x 4 ms from 15 = 120 ms
out(8'h9B, 8'h02);
out(8'h98, 8'd15);
t0 = $realtime;
wait (dut.cur[0] == 0);
$display(" decay from 15 took %.1f ms", ($realtime - t0) / 1e6);
check("decay 2 fades 15 steps in 120 ms", ($realtime - t0) / 1e6 > 115 && ($realtime - t0) / 1e6 < 125);
rd(8'h98);
check("a finished sound reads back 0", q == 8'd0);
// attack then decay: up 12 steps of 1 x 4 ms, then down
out(8'h9B, 8'h13);
out(8'h98, 8'd12);
t0 = $realtime;
check("attack starts from 0", dut.cur[0] == 0);
wait (dut.cur[0] == 12);
rose_at = ($realtime - t0) / 1e6;
wait (dut.cur[0] == 0);
fell_at = ($realtime - t0) / 1e6;
$display(" attack reached 12 at %0d ms, faded out at %0d ms", rose_at, fell_at);
check("attack 1 rises 12 steps in 48 ms", rose_at >= 46 && rose_at <= 50);
check("decay 3 then falls 12 steps in 144 ms", fell_at - rose_at >= 140 && fell_at - rose_at <= 150);
// noise on voice 1, no tone
out(8'h96, 8'd4);
out(8'h97, 8'b010_000);
out(8'h9C, 8'h00);
out(8'h99, 8'd15);
n0 = 0;
for (k = 0; k < 2000; k = k + 1) begin
repeat (360) @(posedge clk);
n0 = n0 + dut.lfsr[0];
end
$display(" noise was high %0d of 2000 samples", n0);
check("noise is about half high, half low", n0 > 800 && n0 < 1200);
// all three voices and CLS
out(8'h92, 8'd142); out(8'h94, 8'd71);
out(8'h97, 8'b000_111);
out(8'h98, 8'd15); out(8'h99, 8'd15); out(8'h9A, 8'd15);
measure(20.0);
check("three voices make sound", duty > 0.2);
@(posedge clk) hush <= 1;
@(posedge clk) hush <= 0;
repeat (4) @(posedge clk); // what was already in the mixer
measure(2.0);
$display(" after CLS: output high %0d times in 2 ms", ones);
rd(8'h99);
check("CLS silences everything", ones == 0 && q == 0);
rd(8'h97);
check("other ports read FFh", q == 8'hFF);
if (errors == 0) $display("ALL SOUND TESTS PASSED");
else $display("%0d SOUND TESTS FAILED", errors);
$finish;
end
endmodule
+601
View File
@@ -0,0 +1,601 @@
`timescale 1ns / 1ps
// Whole-design test: keystrokes on PS/2 -> UART TX bytes, host queries ->
// replies, UART RX text -> screen buffer (printed as text) and VGA frames
// (written as frame_*.ppm).
module tb_terminal;
reg clk = 0;
always #5 clk = ~clk;
reg [15:0] sw = 16'h0200; // 115200 baud, fast smooth scroll when enabled
reg btnC = 0, btnU = 0, btnD = 0;
reg rsrx = 1;
reg ps2c = 1, ps2d = 1;
wire rstx, hs, vs;
wire [3:0] r, g, b;
wire [15:0] led;
wire qcs;
wire [3:0] qdb;
basys3_terminal #(.FONT_FILE(`FONT_FILE), .MEM_FILE(`MEM_FILE), .MEM_HI_FILE(`MEM_HI_FILE), .MON_FILE(`MON_FILE)) dut (
.clk(clk), .btnC(btnC), .btnU(btnU), .btnD(btnD), .sw(sw), .led(led), .seg(), .dp(), .an(),
.vgaRed(r), .vgaGreen(g), .vgaBlue(b), .Hsync(hs), .Vsync(vs),
.RsRx(rsrx), .RsTx(rstx), .PS2Clk(ps2c), .PS2Data(ps2d),
.QspiCSn(qcs), .QspiDB(qdb)
);
spiflash_model flash (.cs_n(qcs), .sck(dut.fl_sck), .mosi(qdb[0]), .miso(qdb[1]));
real BIT_NS = 1_000_000_000.0 / 115200; // changed for the 3 Mbaud test
integer errors = 0;
task uart_send(input [7:0] d);
integer i;
begin
rsrx = 0; #(BIT_NS);
for (i = 0; i < 8; i = i + 1) begin rsrx = d[i]; #(BIT_NS); end
rsrx = 1; #(BIT_NS);
end
endtask
task uart_str(input [8*200-1:0] s);
integer i;
begin
for (i = 199; i >= 0; i = i - 1)
if (s[i*8 +: 8] != 0) uart_send(s[i*8 +: 8]);
end
endtask
task ps2_send(input [7:0] d);
integer i;
reg [10:0] f;
begin
f = {1'b1, ~^d, d, 1'b0};
for (i = 0; i < 11; i = i + 1) begin
ps2d = f[i]; #10000;
ps2c = 0; #20000;
ps2c = 1; #10000;
end
#100000;
end
endtask
task key(input [7:0] code); // press and release
begin ps2_send(code); ps2_send(8'hF0); ps2_send(code); end
endtask
task ext_key(input [7:0] code);
begin ps2_send(8'hE0); ps2_send(code); ps2_send(8'hE0); ps2_send(8'hF0); ps2_send(code); end
endtask
// everything the terminal transmits
reg [8*32-1:0] tx_log = 0;
integer tx_n = 0;
initial forever begin : txmon
integer i;
reg [7:0] d;
@(negedge rstx);
#(BIT_NS / 2);
for (i = 0; i < 8; i = i + 1) begin #(BIT_NS); d[i] = rstx; end
#(BIT_NS);
if (!rstx) $display("TX framing error");
tx_log = {tx_log[8*31-1:0], d};
tx_n = tx_n + 1;
end
task expect_tx(input [8*32-1:0] want, input integer n, input [8*40-1:0] what);
reg [8*32-1:0] mask;
begin
#1_200_000; // a 10-byte reply takes ~0.9 ms
mask = {256{1'b1}} >> (256 - 8 * n);
if (tx_n == n && (tx_log & mask) == (want & mask))
$display("PASS %0s", what);
else begin
$display("FAIL %0s: got %0d bytes %h", what, tx_n, tx_log & mask);
errors = errors + 1;
end
tx_n = 0;
tx_log = 0;
end
endtask
function [7:0] cellraw(input integer rr, input integer cc);
reg [17:0] v;
begin
v = dut.screen.cell_at((dut.alt_scr ? 8448 : 0) + rr * 132 + cc); // the page on show
cellraw = v[7:0];
end
endfunction
function [7:0] cellch(input integer rr, input integer cc);
reg [7:0] v;
begin
v = cellraw(rr, cc);
cellch = v < 8'h20 || v >= 8'h7F ? "~" : v;
end
endfunction
task dump_screen;
integer rr, cc;
reg [8*80-1:0] line;
begin
$display("+--------------------------------------------------------------------------------+");
for (rr = 0; rr < 30; rr = rr + 1) begin
for (cc = 0; cc < 80; cc = cc + 1)
line[(79 - cc) * 8 +: 8] = cellch(rr, cc);
$display("|%s| %0d", line, rr);
end
$display("+--------------------------------------------------------------------------------+");
$display("cursor row %0d col %0d, region %0d-%0d", dut.term.row, dut.term.col,
dut.term.stop, dut.term.sbot);
end
endtask
task expect_row(input integer rr, input [8*40-1:0] want);
integer cc, n;
reg [8*40-1:0] got;
begin
n = 0;
for (cc = 0; cc < 40; cc = cc + 1) if (want[cc*8 +: 8] != 0) n = cc + 1;
got = 0;
for (cc = 0; cc < n; cc = cc + 1) got[(n - 1 - cc) * 8 +: 8] = cellraw(rr, cc);
if (got == want) $display("PASS row %0d", rr);
else begin
$display("FAIL row %0d: want %h got %h", rr, want, got);
errors = errors + 1;
end
end
endtask
// wait (up to 100 ms) for a row to read want, then check it
task wait_row(input integer rr, input [8*40-1:0] want);
integer cc, n;
reg [8*40-1:0] got;
realtime t0;
begin
t0 = $realtime;
n = 0;
for (cc = 0; cc < 40; cc = cc + 1) if (want[cc*8 +: 8] != 0) n = cc + 1;
got = 0;
while (got != want && $realtime - t0 < 100_000_000) begin
#100_000;
for (cc = 0; cc < n; cc = cc + 1) got[(n - 1 - cc) * 8 +: 8] = cellraw(rr, cc);
end
expect_row(rr, want);
end
endtask
// wait (up to 300 ms) for text at row rr from column c0
task wait_at(input integer rr, input integer c0, input [8*40-1:0] want);
integer cc, n;
reg [8*40-1:0] got;
realtime t0;
begin
t0 = $realtime;
n = 0;
for (cc = 0; cc < 40; cc = cc + 1) if (want[cc*8 +: 8] != 0) n = cc + 1;
got = 0;
while (got != want && $realtime - t0 < 300_000_000) begin
#100_000;
for (cc = 0; cc < n; cc = cc + 1) got[(n - 1 - cc) * 8 +: 8] = cellraw(rr, c0 + cc);
end
if (got == want) $display("PASS row %0d col %0d: %0s", rr, c0, want);
else begin
$display("FAIL row %0d col %0d: want \"%0s\" got \"%0s\"", rr, c0, want, got);
errors = errors + 1;
end
end
endtask
reg [31:0] pc_a, pc_b, jp_to;
reg [15:0] jp_txt;
integer i;
// a hex number on the screen at (rr, c0), up to a character that isn't one
function [31:0] hex_at(input integer rr, input integer c0);
integer cc;
reg [7:0] ch;
reg go;
begin
hex_at = 0;
go = 1;
for (cc = c0; cc < c0 + 9; cc = cc + 1) begin
ch = cellraw(rr, cc);
if (go && ch >= "0" && ch <= "9") hex_at = hex_at * 16 + (ch - "0");
else if (go && ch >= "A" && ch <= "F") hex_at = hex_at * 16 + (ch - "A" + 10);
else go = 0;
end
end
endfunction
// the PC in the monitor's register display
function [31:0] screen_pc(input integer dummy);
screen_pc = hex_at(2, 4);
endfunction
// 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;
begin
@(posedge vs);
cap = 1;
@(negedge vs);
@(posedge vs);
cap = 0;
fd = $fopen(name, "w");
$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: 1688 x 1066 pixel clocks (10 ns here)
realtime t_hs = 0, t_vs = 0, t_cmd;
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 @(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
initial begin
#2_000_000; // reset, clear, banner
`ifndef SKIP_TERMINAL
// ---- keyboard: a, shift+b, Enter, up, ctrl+c, F5
key(8'h1C);
ps2_send(8'h12); key(8'h32); ps2_send(8'hF0); ps2_send(8'h12);
key(8'h5A);
ext_key(8'h75);
ps2_send(8'h14); key(8'h21); ps2_send(8'hF0); ps2_send(8'h14);
key(8'h03);
expect_tx({8'h61, 8'h42, 8'h0D, 8'h1B, "[A", 8'h03, 8'h1B, "[15~"}, 12, "keyboard -> UART");
// ---- host queries and modes
uart_str("\033[c");
expect_tx({8'h1B, "[?62;22c"}, 9, "DA reply");
uart_str("\033[>c");
expect_tx({8'h1B, "[>1;10;0c"}, 10, "secondary DA reply");
uart_str("\033[5;10H\033[6n");
expect_tx({8'h1B, "[05;10R"}, 8, "cursor position report");
uart_str("\033[?1h");
ext_key(8'h75);
expect_tx({8'h1B, "OA"}, 3, "DECCKM up arrow");
uart_str("\033[?1l\033=");
key(8'h69);
expect_tx({8'h1B, "Oq"}, 3, "DECKPAM keypad 1");
uart_str("\033>\033[1;3q");
#50_000;
if (led[11:8] == 4'b0101) $display("PASS DECLL LEDs");
else begin $display("FAIL DECLL LEDs %b", led[11:8]); errors = errors + 1; end
// ---- screen
uart_str({"\033[2J\033[H\033[1mBold\033[0m \033[31mred \033[32mgreen \033[44;33myellow-on-blue\033[0m ",
"\033[4munderline\033[0m \033[7mreverse\033[0m \033[5mblink\033[0m\015\012"});
uart_str("\033)0\016lqqqqk\015\012x x\015\012mqqqqj\017\015\012");
uart_str("abcdefgh\033[4D\033[2@XY\033[3P\015\012");
uart_str("\033[38;5;196m256-red \033[38;2;0;0;255mrgb-blue \033[90mgrey \033[103;30mon-yellow\033[0m\015\012");
uart_str({"\033]3008;start=1f2e;user=brad;type=shell\033\\\033]0;title\007",
"line A\015\012line B\015\012line C\015\012line D\015\012"});
uart_str("\033[8;1H\033[Linserted\033[10;1H\033[M");
uart_str("\033[13;1Habove region\033[19;1Hbelow region\033[15;18r\033[15;1H");
uart_str("r1\015\012r2\015\012r3\015\012r4\015\012r5\015\012r6");
uart_str("\033[1;1H\033[4h>\033[4l\033[30;70Hcorner\033[1;1H");
#200_000;
expect_row(0, ">Bold red green yellow-on-blue");
expect_row(1, {8'hDA, 8'hC4, 8'hC4, 8'hC4, 8'hC4, 8'hBF});
expect_row(2, {8'hB3, " ", 8'hB3});
expect_row(4, "abcdXYh");
expect_row(6, "line A");
expect_row(7, "inserted");
expect_row(8, "line B");
expect_row(9, "line D");
expect_row(12, "above region");
expect_row(14, "r3");
expect_row(17, "r6");
expect_row(18, "below region");
dump_screen;
capture_frame("frame_1.ppm");
// ---- smooth scroll inside the region
uart_str("\033[18;1H\033[?4h\015\012r7");
capture_frame("frame_2.ppm"); // mid-scroll
#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
`endif
// ---- BASIC as the console, input pasted over the UART
uart_str("\033[2J\033[H");
#100_000;
sw[15] = 1; sw[14] = 1; // the computer has been waiting to print its banner
#3_000_000;
uart_str("PRINT 2+2\015");
#2_000_000;
uart_str("10 FOR I=1 TO 3: PRINT I*I;: NEXT I\015RUN\015");
#5_000_000;
expect_row(1, "DARTMOUTH-STYLE BASIC");
expect_row(3, "READY");
expect_row(4, "PRINT 2+2");
expect_row(5, " 4");
expect_row(7, "READY");
expect_row(10, " 1 4 9");
expect_row(11, "READY");
// ---- graphics
uart_str({"CLS:INK 11:CIRCLE 160,120,50,12:FILL 10,10,40,30,4:",
"LINE 0,0,319,239,14:PRINT POINT(160,70)\015"});
t_cmd = $realtime;
while (cellraw(0, 2) != "2" && $realtime - t_cmd < 20_000_000) #10_000;
$display("graphics line ran in %0.2f ms", ($realtime - t_cmd) / 1e6);
expect_row(0, " 12");
if (dut.sys.gfx(70 * 320 + 160) == 12 && dut.sys.gfx(20 * 320 + 20) == 4 &&
dut.sys.gfx(120 * 320 + 160) == 14 && dut.sys.gfx(5 * 320 + 100) == 0)
$display("PASS graphics memory");
else begin
$display("FAIL graphics memory");
errors = errors + 1;
end
capture_frame("frame_3.ppm");
// ---- sprites: shape 0 solid colour 9, shape 1 solid 2, shape 2 left
// half 14; sprite 0 over sprite 1, sprite 2 flipped at the right edge
// over a filled strip
uart_str({"CLS:OUT 118,0:FOR I=1 TO 128:OUT 119,153:NEXT I:",
"OUT 118,1:FOR I=1 TO 128:OUT 119,34:NEXT I\015"});
uart_str({"OUT 118,2:FOR I=1 TO 16:FOR J=1 TO 4:OUT 119,238:NEXT J:",
"FOR J=1 TO 4:OUT 119,0:NEXT J:NEXT I\015"});
uart_str({"OUT 112,0:OUT 113,100:OUT 114,0:OUT 115,150:OUT 116,0:OUT 117,1:",
"OUT 112,1:OUT 113,108:OUT 114,0:OUT 115,158:OUT 116,1:OUT 117,1\015"});
uart_str({"FILL 0,200,319,239,4:OUT 112,2:OUT 113,54:OUT 114,1:OUT 115,230:",
"OUT 116,2:OUT 117,3\015"});
#40_000_000;
@(posedge vs); @(posedge vs); @(posedge vs);
uart_str("LOCATE 30,1:PRINT INP(128);INP(129);INP(132)\015");
wait_row(29, " 3 0 4");
capture_frame("frame_5.ppm");
// graphics pixel (x, y) is screen pixel (4x+2, 4y+2)
if (fb[(4*151+2)*1280 + 4*101+2] == 12'hF55 && // sprite 0
fb[(4*162+2)*1280 + 4*112+2] == 12'hF55 && // sprite 0 over sprite 1
fb[(4*170+2)*1280 + 4*120+2] == 12'h0A0 && // sprite 1
fb[(4*150+2)*1280 + 4*99+2] == 12'h000 && // beside them
fb[(4*235+2)*1280 + 4*312+2] == 12'h00A && // flipped: clear half
fb[(4*235+2)*1280 + 4*318+2] == 12'h5FF) // flipped: coloured half
$display("PASS sprites on screen");
else begin
$display("FAIL sprites on screen: %h %h %h %h %h %h",
fb[(4*151+2)*1280 + 4*101+2], fb[(4*162+2)*1280 + 4*112+2],
fb[(4*170+2)*1280 + 4*120+2], fb[(4*150+2)*1280 + 4*99+2],
fb[(4*235+2)*1280 + 4*312+2], fb[(4*235+2)*1280 + 4*318+2]);
errors = errors + 1;
end
uart_str("CLS\015");
#5_000_000;
if (dut.sys.spr.en == 32'd0) $display("PASS CLS turns sprites off");
else begin
$display("FAIL CLS left sprites on: %h", dut.sys.spr.en);
errors = errors + 1;
end
// ---- held keys: Ctrl and right Shift (they type nothing) read on
// port 136 as fire (16) + shift (64), and right Shift on port 137
ps2_send(8'h14); ps2_send(8'h59);
#2_000_000;
uart_str("CLS:OUT 137,89:LOCATE 30,1:PRINT INP(136);INP(137)\015");
wait_row(29, " 80 1");
ps2_send(8'hF0); ps2_send(8'h14); ps2_send(8'hF0); ps2_send(8'h59);
#2_000_000;
uart_str("CLS:LOCATE 30,1:PRINT INP(136);INP(137)\015");
wait_row(29, " 0 0");
// ---- graphics scroll: buffer (5,3) at the top left, so (10,100)
// shows at (5,97) and (2,1) wraps round to (317,238)
uart_str("CLS:PLOT 10,100,9:PLOT 2,1,12:OUT 53,5:OUT 54,0:OUT 55,3\015");
#5_000_000;
capture_frame("frame_6.ppm");
if (fb[(4*97+2)*1280 + 4*5+2] == 12'hF55 && fb[(4*100+2)*1280 + 4*10+2] == 12'h000 &&
fb[(4*238+2)*1280 + 4*317+2] == 12'h55F)
$display("PASS graphics scroll");
else begin
$display("FAIL graphics scroll: %h %h %h", fb[(4*97+2)*1280 + 4*5+2],
fb[(4*100+2)*1280 + 4*10+2], fb[(4*238+2)*1280 + 4*317+2]);
errors = errors + 1;
end
// the border hides the graphics at the screen's edges, not inside
uart_str("OUT 54,2\015");
#5_000_000;
capture_frame("frame_7.ppm");
if (fb[(4*97+2)*1280 + 4*5+2] == 12'hF55 && fb[(4*238+2)*1280 + 4*317+2] == 12'h000)
$display("PASS graphics border");
else begin
$display("FAIL graphics border: %h %h", fb[(4*97+2)*1280 + 4*5+2], fb[(4*238+2)*1280 + 4*317+2]);
errors = errors + 1;
end
uart_str("CLS\015");
#5_000_000;
if (dut.sys.scroll_x == 0 && dut.sys.scroll_y == 0 && !dut.sys.g_border) $display("PASS CLS resets the scroll");
else begin
$display("FAIL CLS left the scroll at %0d,%0d", dut.sys.scroll_x, dut.sys.scroll_y);
errors = errors + 1;
end
// ---- 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",
"LOAD \"TEST1\"\015RUN\015DIR\015"});
#20_000_000;
expect_row(13, "SAVED 42");
wait_row(17, "TEST1 21 2026-11-28 09:30");
wait_row(18, "1 FILE, 1780K FREE");
uart_str("OUT 64,1:OUT 65,6:OUT 64,0:OUT 64,1:OUT 65,32:OUT 65,0:OUT 65,0:OUT 65,0:OUT 64,0\015");
#3_000_000;
if (flash.programs > 0 && flash.erases > 0 && flash.violations == 0)
$display("PASS flash: %0d programs, %0d erases, bitstream untouched",
flash.programs, flash.erases);
else begin
$display("FAIL flash: %0d programs, %0d erases, %0d violations",
flash.programs, flash.erases, flash.violations);
errors = errors + 1;
end
// ---- a data file: write it, read it back
uart_str({"CLS:OPEN \"D\" FOR OUTPUT AS 1:PRINT #1,\"XY\";\",\";7:CLOSE\015",
"OPEN \"D\" FOR INPUT AS 1:INPUT #1,A$,B:PRINT A$;B*6;EOF(1):CLOSE\015"});
wait_row(3, "XY 42 -1");
`ifndef STOP_BEFORE_MONITOR
// ---- the machine-code monitor
uart_str("MONITOR\015");
wait_at(0, 50, "STOPPED BY MONITOR COMMAND");
wait_at(2, 0, " PC ");
wait_at(29, 0, "> ");
pc_a = screen_pc(0);
wait_at(9, 0, ">");
jp_txt = {cellraw(9, 19), cellraw(9, 20)};
jp_to = hex_at(9, 21);
dump_screen;
uart_str("\015"); // Enter: one step
wait_at(0, 50, "STOPPED BY STEP");
wait_at(29, 0, "> ");
pc_b = screen_pc(0);
// MONITOR stops on a J (to the end of BASIC's statement routine): one
// step must land exactly on its target
if (jp_txt == "J " && pc_b == jp_to) $display("PASS step: PC %h -> %h", pc_a, pc_b);
else begin $display("FAIL step: PC %h -> %h, wanted %h (%0s)", pc_a, pc_b, jp_to, jp_txt); errors = errors + 1; end
// a breakpoint where the step stopped (the end of BASIC's statement
// routine), hit again by OUT; leaving from it steps off it first
uart_str("B "); for (i = 0; i < 8; i = i + 1) uart_send(cellraw(2, 4 + i)); uart_send(8'h0D);
wait_at(29, 40, "BREAKPOINT SET");
uart_str("X\015");
wait_row(3, "XY 42 -1"); // BASIC's screen is back as it was
uart_str("OUT 32,7\015");
wait_at(0, 50, "STOPPED BY BREAKPOINT");
wait_at(29, 0, "> ");
if (screen_pc(0) == pc_b) $display("PASS breakpoint at %h", pc_b);
else begin $display("FAIL breakpoint: PC %h, wanted %h", screen_pc(0), pc_b); errors = errors + 1; end
uart_str("B\015");
wait_at(29, 40, "BREAKPOINT CLEARED");
uart_str("X\015");
#5_000_000;
// the Pause key
ps2_send(8'hE1); ps2_send(8'h14); ps2_send(8'h77);
ps2_send(8'hE1); ps2_send(8'hF0); ps2_send(8'h14); ps2_send(8'hF0); ps2_send(8'h77);
wait_at(0, 50, "STOPPED BY PAUSE KEY");
wait_at(29, 0, "> ");
uart_str("X\015");
#2_000_000;
uart_str("CLS:PRINT 3*4\015");
wait_row(0, " 12");
// ---- 3 Mbaud (SW1:0 = 11): a program in, its output on screen and back
sw[1:0] = 2'b11;
BIT_NS = 1_000_000_000.0 / 3_000_000;
#200_000;
tx_n = 0; tx_log = 0;
uart_str({"CLS:NEW\01510 FOR I=1 TO 3:PRINT I*111;:NEXT I\01520 PRINT \"FAST\"\015",
"RUN\015"});
wait_row(5, " 111 222 333 FAST");
#2_000_000;
if (tx_log[8*7-1:0] == "READY\015\012") $display("PASS 3 Mbaud: %0d bytes back, ending READY", tx_n);
else begin $display("FAIL 3 Mbaud: %0d bytes back, last %h", tx_n, tx_log[8*7-1:0]); errors = errors + 1; end
// ---- memory files: the monitor's SAVE/LOAD/DIR, BASIC's BSAVE/BLOAD
uart_str("CLS:POKE 73728,72:POKE 73729,73:MONITOR\015");
wait_at(29, 0, "> ");
uart_str("SAVE \"MEM1\" 12000 12001\015");
wait_at(29, 40, "SAVED");
uart_str("E 12000 00 00\015");
wait_at(29, 0, "> ");
uart_str("LOAD \"MEM1\"\015");
wait_at(29, 40, "LOADED 12000-12001");
wait_at(22, 0, " 12000 48 49 ");
uart_str("DIR\015");
wait_at(29, 40, "M TO SHOW MEMORY AGAIN");
uart_str("X\015");
#3_000_000;
uart_str("CLS:POKE 73728,0:BLOAD \"MEM1\":BSAVE \"MEM2\",73728,2:BLOAD \"MEM2\",73730:PRINT PEEK(73730);PEEK(73731)\015");
wait_row(0, " 72 73");
// ---- the assembler: source typed into BASIC, saved, assembled by the monitor
uart_str({"NEW\01510 ORG 12000H\01520 START: LA A1,MSG\01530 LOOP: LBU A0,0(A1)\015",
"40 BEQZ A0,DONE\01550 WAIT: LBU T0,-256(ZERO)\01560 ANDI T0,T0,2\015"});
uart_str({"70 BEQZ T0,WAIT\01580 SB A0,-255(ZERO)\01590 ADDI A1,A1,1\015100 J LOOP\015",
"110 DONE: RET\015"});
uart_str({"120 MSG: DB \"ASM OK\",0 ; THE MESSAGE\015SAVE \"HELLO\"\015MONITOR\015"});
wait_at(29, 0, "> ");
uart_str("ASM \"HELLO\"\015");
wait_at(29, 40, "ASSEMBLED 12000-12032");
uart_str("A 12100\015LI A0,66\015\015");
wait_at(9, 2, "12100 04200513 LI A0,66");
// an Intel HEX record above 64K (an extended address first) and the end record
uart_str(":020000040001F9\015:03210000AABBCCAB\015:00000001FF\015");
wait_at(29, 40, "HEX LOADED 12100-12102");
if (dut.sys.ram1[12'h840][23:0] == 24'hCCBBAA)
$display("PASS hex record in memory");
else begin
$display("FAIL hex record: %h", dut.sys.ram1[12'h840]);
errors = errors + 1;
end
uart_str("X\015");
#3_000_000;
uart_str("CLS:CALL 73728\015");
wait_row(0, "ASM OK");
// ---- Ctrl-Alt-Del restarts the computer
ps2_send(8'h14); ps2_send(8'h11); ps2_send(8'hE0); ps2_send(8'h71);
ps2_send(8'hE0); ps2_send(8'hF0); ps2_send(8'h71); ps2_send(8'hF0); ps2_send(8'h11);
ps2_send(8'hF0); ps2_send(8'h14);
// the restarted BASIC's banner, on whichever row it lands
t_cmd = $realtime;
while ({cellraw(2, 0), cellraw(2, 1)} != "DA" && {cellraw(3, 0), cellraw(3, 1)} != "DA" &&
{cellraw(4, 0), cellraw(4, 1)} != "DA" && $realtime - t_cmd < 50_000_000) #100_000;
if ($realtime - t_cmd < 50_000_000) $display("PASS Ctrl-Alt-Del restarted BASIC");
else begin $display("FAIL Ctrl-Alt-Del: no banner"); errors = errors + 1; end
dump_screen;
`endif
if (errors == 0) $display("ALL TESTS PASSED");
else $display("%0d TESTS FAILED", errors);
$finish;
end
endmodule
+24
View File
@@ -0,0 +1,24 @@
# terminfo for the Basys 3 FPGA terminal: a VT220 with 16 ANSI colours,
# underline, blink, 80x32 (basys3) or 132x64 after ESC[?3h (basys3-w).
# tic -x -o ~/.terminfo terminfo/basys3.ti then export TERM=basys3
basys3|Basys 3 FPGA VT220 terminal with ANSI colour,
lines#32, cols#80,
colors#16, pairs#256,
op=\E[39;49m,
setaf=\E[%?%p1%{8}%<%t3%p1%d%e9%p1%{8}%-%d%;m,
setab=\E[%?%p1%{8}%<%t4%p1%d%e10%p1%{8}%-%d%;m,
invis=\E[8m,
rmso=\E[27m, rmul=\E[24m,
sgr0=\E[m\017\E(B,
kbs=\177,
khome=\E[1~, kend=\E[4~,
kf5=\E[15~,
indn=\E[%p1%dS, rin=\E[%p1%dT,
cnl=\E[%p1%dE, cpl=\E[%p1%dF,
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,
+178
View File
@@ -0,0 +1,178 @@
# Interrupts: each source, masking by mie and mstatus.MIE, priority, and code
# that is interrupted again and again giving the same result as without.
# Uses the test device in sim/tb_cpu.v at FFFFFF00.
#include "riscv_test.h"
#include "test_macros.h"
#define DEV -256 /* 0xFFFFFF00 */
#define SOFT 1
#define TIMER 2
#define EXT 4
#define LOCAL 8
# raise lines after delay cycles
#define RAISE(lines, delay) li t0, ((delay) << 8) | (lines); sw t0, DEV(zero)
# spin until the handler has run (s2 != count), or fail after a while
#define WAIT_IRQ(count) \
li t1, 1000; \
1: li t0, count; bne s2, t0, 2f; addi t1, t1, -1; bnez t1, 1b; j fail; 2:
RVTEST_RV32M
RVTEST_CODE_BEGIN
li s2, 0
csrsi mstatus, MSTATUS_MIE
# each source, with its own enable bit
li TESTNUM, 2
li t0, MIP_MTIP; csrw mie, t0
RAISE(TIMER, 20)
WAIT_IRQ(0)
li t0, 0x80000007; bne s1, t0, fail
li TESTNUM, 3
li t0, MIP_MEIP; csrw mie, t0
RAISE(EXT, 5)
WAIT_IRQ(1)
li t0, 0x8000000b; bne s1, t0, fail
li TESTNUM, 4
li t0, MIP_MSIP; csrw mie, t0
RAISE(SOFT, 0)
WAIT_IRQ(2)
li t0, 0x80000003; bne s1, t0, fail
li TESTNUM, 5
li t0, 1 << 16; csrw mie, t0
RAISE(LOCAL, 3)
WAIT_IRQ(3)
li t0, 0x80000010; bne s1, t0, fail
# masked by mie: pending shows in mip but nothing is taken
li TESTNUM, 6
csrw mie, zero
RAISE(TIMER, 0)
li t1, 100
1: addi t1, t1, -1; bnez t1, 1b
li t0, 4; bne s2, t0, fail
csrr t0, mip; andi t0, t0, MIP_MTIP; beqz t0, fail
li t0, MIP_MTIP; csrs mie, t0 # now it is taken
WAIT_IRQ(4)
# masked by mstatus.MIE
li TESTNUM, 7
csrci mstatus, MSTATUS_MIE
RAISE(EXT, 0)
li t0, MIP_MEIP; csrw mie, t0
li t1, 100
1: addi t1, t1, -1; bnez t1, 1b
li t0, 5; bne s2, t0, fail
csrsi mstatus, MSTATUS_MIE
WAIT_IRQ(5)
li t0, 0x8000000b; bne s1, t0, fail
# the handler saw MPIE set and MIE clear
li t0, MSTATUS_MPIE; and t1, s3, t0; beqz t1, fail
andi t1, s3, MSTATUS_MIE; bnez t1, fail
# and mret turned MIE back on
csrr t0, mstatus; andi t0, t0, MSTATUS_MIE; beqz t0, fail
# priority: external, then software, then timer, then local
li TESTNUM, 8
li t0, MIP_MEIP | MIP_MSIP | MIP_MTIP | (1 << 16); csrw mie, t0
csrci mstatus, MSTATUS_MIE
RAISE(EXT | SOFT | TIMER | LOCAL, 0)
li t1, 10
1: addi t1, t1, -1; bnez t1, 1b
csrsi mstatus, MSTATUS_MIE
WAIT_IRQ(6)
li t0, 0x8000000b; bne s1, t0, fail
li TESTNUM, 9
csrci mstatus, MSTATUS_MIE
RAISE(SOFT | TIMER | LOCAL, 0)
li t1, 10
1: addi t1, t1, -1; bnez t1, 1b
csrsi mstatus, MSTATUS_MIE
WAIT_IRQ(7)
li t0, 0x80000003; bne s1, t0, fail
li TESTNUM, 10
csrci mstatus, MSTATUS_MIE
RAISE(TIMER | LOCAL, 0)
li t1, 10
1: addi t1, t1, -1; bnez t1, 1b
csrsi mstatus, MSTATUS_MIE
WAIT_IRQ(8)
li t0, 0x80000007; bne s1, t0, fail
# a workout: once quietly, once with the timer firing every few cycles
li TESTNUM, 11
csrw mie, zero
jal workout
mv s5, a0
li s4, 1 # handler re-arms the timer
li t0, MIP_MTIP; csrw mie, t0
RAISE(TIMER, 3)
jal workout
csrw mie, zero
li s4, 0
bne a0, s5, fail
li t0, 100; blt s2, t0, fail # it really was interrupted a lot
TEST_PASSFAIL
# mixes ALU, mul, div, loads, stores and branches; result in a0
workout:
la a1, buf
li a0, 12345
li a2, 150
1:
mul a3, a0, a2
addi a3, a3, 77
divu a4, a3, a2
rem a5, a3, a2
xor a0, a0, a4
add a0, a0, a5
slli a6, a2, 2
andi a6, a6, 60
add a6, a6, a1
sw a0, 0(a6)
lh a7, 2(a6)
lbu a3, 1(a6)
add a0, a0, a7
sub a0, a0, a3
mulh a4, a0, a0
add a0, a0, a4
sb a0, 5(a6)
lw a4, 4(a6)
add a0, a0, a4
addi a2, a2, -1
bnez a2, 1b
ret
.align 2
.global mtvec_handler
mtvec_handler:
csrr s1, mcause
csrr s3, mstatus
bgez s1, fail # not an interrupt
addi s2, s2, 1
li t5, DEV
beqz s4, 1f
andi t6, s2, 31 # re-arm after 20..51 cycles
addi t6, t6, 20
slli t6, t6, 8
ori t6, t6, TIMER
sw t6, 0(t5)
mret
1:
sw zero, 0(t5)
mret
RVTEST_CODE_END
.data
RVTEST_DATA_BEGIN
TEST_DATA
buf: .fill 32, 4, 0
RVTEST_DATA_END
+19
View File
@@ -0,0 +1,19 @@
/* riscv-tests image: everything from address 0 (the core's reset vector);
tests/nmi.S puts its NMI handler at 8000h, tb_cpu.v's NMI_VEC. */
OUTPUT_ARCH( "riscv" )
ENTRY(_start)
SECTIONS
{
. = 0x0;
.text.init : { *(.text.init) }
. = ALIGN(0x1000);
.tohost : { *(.tohost) }
. = ALIGN(0x1000);
.text : { *(.text) }
. = ALIGN(0x1000);
.data : { *(.data) }
.bss : { *(.bss) }
. = 0x8000;
.nmi : { KEEP(*(.nmi)) }
_end = .;
}
+89
View File
@@ -0,0 +1,89 @@
# Resumable NMI: one from the nmi line, then single steps from nmi_step.
# The handler is at 8000h (tb_cpu.v's NMI_VEC); the test device is FFFFFF00.
#include "riscv_test.h"
#include "test_macros.h"
#define DEV -256
#define NMI 16
#define STEP 32
#define RAISE(lines, delay) li t0, ((delay) << 8) | (lines); sw t0, DEV(zero)
RVTEST_RV32M
RVTEST_CODE_BEGIN
li s2, 0
# NMIE starts set
li TESTNUM, 2
csrr t0, CSR_MNSTATUS; andi t0, t0, MNSTATUS_NMIE; beqz t0, fail
# an NMI from the line: handler runs once, mnret comes back here
li TESTNUM, 3
li s4, 0 # handler mode: drop the line
RAISE(NMI, 10)
li t1, 1000
1: bnez s2, 2f; addi t1, t1, -1; bnez t1, 1b; j fail
2: li t0, 1; bne s2, t0, fail
csrr t0, CSR_MNSTATUS; andi t0, t0, MNSTATUS_NMIE; beqz t0, fail
andi t0, s5, MNSTATUS_NMIE; bnez t0, fail # clear inside the handler
# NMIs are masked while NMIE is clear
li TESTNUM, 4
csrci CSR_MNSTATUS, MNSTATUS_NMIE
RAISE(NMI, 0)
li t1, 50
1: addi t1, t1, -1; bnez t1, 1b
li t0, 1; bne s2, t0, fail
csrsi CSR_MNSTATUS, MNSTATUS_NMIE
li t1, 50
1: addi t1, t1, -1; bnez t1, 1b
li t0, 2; bne s2, t0, fail
# single steps: the handler logs mnepc and stops after four
li TESTNUM, 5
li s2, 0
li s4, 1 # handler mode: log, stop after 4
la s6, log
RAISE(STEP, 0)
step0:
addi a0, x0, 1
step1:
addi a0, a0, 1
step2:
addi a0, a0, 1
step3:
addi a0, a0, 1
addi a0, a0, 1
li t0, 4; bne s2, t0, fail
li t0, 5; bne a0, t0, fail # every instruction ran once
# the stops were after the RAISE's store, then after each instruction
la s6, log
la t0, step0; lw t1, 0(s6); bne t0, t1, fail
la t0, step1; lw t1, 4(s6); bne t0, t1, fail
la t0, step2; lw t1, 8(s6); bne t0, t1, fail
la t0, step3; lw t1, 12(s6); bne t0, t1, fail
TEST_PASSFAIL
.section .nmi, "ax"
nmi_handler:
csrr s5, CSR_MNSTATUS
addi s2, s2, 1
bnez s4, 1f
sw zero, DEV(zero) # drop the line
.word 0x70200073 # mnret
1:
csrr t5, CSR_MNEPC
sw t5, 0(s6)
addi s6, s6, 4
li t5, 4
bne s2, t5, 2f
sw zero, DEV(zero) # stop stepping
2:
.word 0x70200073 # mnret
.data
RVTEST_DATA_BEGIN
TEST_DATA
log: .fill 8, 4, 0
RVTEST_DATA_END
+16
View File
@@ -0,0 +1,16 @@
#!/usr/bin/env python3
"""Binary image -> $readmemh file of 32-bit little-endian words.
bin2mem.py image.bin image.mem [words] (pads with zeros to `words`)
"""
import sys
data = open(sys.argv[1], 'rb').read()
data += b'\0' * (-len(data) % 4)
words = int(sys.argv[3]) if len(sys.argv) > 3 else len(data) // 4
if len(data) // 4 > words:
raise SystemExit("%s: %d bytes, more than %d words" % (sys.argv[1], len(data), words))
data += b'\0' * (words * 4 - len(data))
with open(sys.argv[2], 'w') as f:
for i in range(0, len(data), 4):
f.write('%08x\n' % int.from_bytes(data[i:i + 4], 'little'))
+224
View File
@@ -0,0 +1,224 @@
#!/usr/bin/env python3
"""A model of the Basys 3 RISC-V computer's game hardware around
tools/rv32emu.py, for testing programs on the PC: the terminal (port 1), the
graphics layer with scrolling (30h-37h), the sprites (70h-78h) and the
held-key port (88h).
from boardemu import Board, load_ihx, load_font
mem, end = load_ihx("prog.ihx")
b = Board(mem, code_end=end) # runs from GAME_BASE, returns to address 0
b.run_frames(60, joy=FIRE)
b.shot("screen.png", load_font())
The frame counter (78h) moves on when the program comes back to read it after
doing some work (more than 100 instructions since it last moved), so a program
that waits for it to change runs one frame per wait, however long its frames
take, and two reads close together see the same frame. Screenshots need
pyte and Pillow, and are drawn the way the board's video combines the layers:
text over sprites over graphics.
"""
import os
import sys
HERE = os.path.dirname(os.path.abspath(__file__))
sys.path.insert(0, HERE)
from rv32emu import RV32 # noqa: E402
GAME_BASE = 0xA200 # where BASIC's loader programs put machine code
STACK_TOP = 0x13FF0
PAL = ["000", "A00", "0A0", "A50", "00A", "A0A", "0AA", "AAA",
"555", "F55", "5F5", "FF5", "55F", "F5F", "5FF", "FFF"]
PAL = [tuple(int(c, 16) * 17 for c in p) for p in PAL]
UP, DOWN, LEFT, RIGHT, FIRE, ENTER, ESC = 1, 2, 4, 8, 16, 32, 128
class Board(RV32):
def __init__(self, mem, code_end=0x14000, start=GAME_BASE, rom_top=0):
super().__init__(mem, rom_top=rom_top)
self.code_end = code_end
self.code_start = start
self.steps = 0 # instructions run
self.scroll_x = self.scroll_y = 0
self.border = False
self.pc, self.sp = start, STACK_TOP
self.x[1] = 0 # ra: the program returns to 0
self.frame = 0
self.frame_at = -1000
self.joy = 0
self.gfx = bytearray(320 * 240)
self.gxv = self.gyv = 0
self.spr = [[0, 0, 0, 0] for _ in range(32)] # x, y, shape, ctl
self.sel = 0
self.xst = 0
self.pat = bytearray(8192)
self.pat_a = 0
self.text = bytearray()
self.keys_in = bytearray()
def io_in(self, port):
p = port & 0xFF
if p == 0:
return (1 if self.keys_in else 0) | 2
if p == 1:
return self.keys_in.pop(0) if self.keys_in else 0
if p == 0x78:
if self.steps - self.frame_at > 100:
self.frame += 1
self.frame_at = self.steps
return self.frame & 0xFF
if p == 0x88:
return self.joy
if p == 0x33:
return self.gfx[self.gyv * 320 + self.gxv] if self.gxv < 320 and self.gyv < 240 else 0
if p == 0x34:
return 0
return 0xFF
def io_out(self, port, v):
p = port & 0xFF
if p == 1:
self.text.append(v)
elif p == 0x30: self.gxv = (self.gxv & 0x100) | v
elif p == 0x31: self.gxv = (self.gxv & 0xFF) | (v & 1) << 8
elif p == 0x32: self.gyv = v
elif p == 0x33:
if self.gxv < 320 and self.gyv < 240:
self.gfx[self.gyv * 320 + self.gxv] = v & 15
self.gxv = (self.gxv + 1) & 0x1FF
elif p == 0x34:
self.gfx[:] = bytes([v & 15]) * (320 * 240)
if not v & 0x80:
for s in self.spr: s[3] &= ~1
self.scroll_x = self.scroll_y = 0
self.border = False
elif p == 0x35: self.scroll_x = (self.scroll_x & 0x100) | v
elif p == 0x36:
self.scroll_x = (self.scroll_x & 0xFF) | (v & 1) << 8
self.border = bool(v & 2)
elif p == 0x37: self.scroll_y = v - 240 if v >= 240 else v
elif p == 0x70: self.sel = v & 31
elif p == 0x71: self.xst = (self.xst & 0x100) | v
elif p == 0x72: self.xst = (self.xst & 0xFF) | (v & 1) << 8
elif p == 0x73: self.spr[self.sel][0], self.spr[self.sel][1] = self.xst, v
elif p == 0x74: self.spr[self.sel][2] = v & 63
elif p == 0x75: self.spr[self.sel][3] = v & 7
elif p == 0x76: self.pat_a = (v & 63) * 128
elif p == 0x77:
self.pat[self.pat_a & 8191] = v
self.pat_a += 1
def run_frames(self, n, joy=0):
"""Run until the program has waited for n more frames (or returned)."""
self.joy = joy
end = self.frame + n
while self.frame < end and self.pc != 0:
self.step()
self.steps += 1
if not (self.code_start <= self.pc < self.code_end or self.pc == 0):
raise SystemExit("PC left the program: %05X" % self.pc)
if self.sp < STACK_TOP - 0x1000:
raise SystemExit("stack ran into memory: SP %05X" % self.sp)
def screen_text(self):
import pyte
scr = pyte.Screen(80, 32)
pyte.ByteStream(scr).feed(bytes(self.text))
return scr
def shot(self, path, font):
from PIL import Image
scr = self.screen_text()
img = Image.new("RGB", (1280, 1024))
px = img.load()
lay = bytearray(320 * 240) # the graphics as scrolled onto the screen
for y in range(240):
fy = (y + self.scroll_y) % 240
row = self.gfx[fy * 320:(fy + 1) * 320]
sx = self.scroll_x % 320
lay[y * 320:(y + 1) * 320] = row[sx:] + row[:sx]
if self.border: # hidden: 4 columns each side, 2 rows top and bottom
if y < 2 or y >= 238:
lay[y * 320:(y + 1) * 320] = bytes(320)
else:
lay[y * 320:y * 320 + 4] = bytes(4)
lay[y * 320 + 316:(y + 1) * 320] = bytes(4)
for s in reversed(self.spr): # sprite 0 in front
x0, y0, shape, ctl = s
if not ctl & 1:
continue
for r in range(16):
for c in range(16):
rr = 15 - r if ctl & 4 else r
cc = 15 - c if ctl & 2 else c
b = self.pat[shape * 128 + rr * 8 + cc // 2]
col = b & 15 if cc & 1 else b >> 4
x, y = (x0 + c) & 511, (y0 + r) & 255
if col and x < 320 and y < 240:
lay[y * 320 + x] = col | 16
for y in range(1024):
for x in range(1280):
gx, gy = x >> 2, y >> 2
c = lay[gy * 320 + gx] & 15 if gy < 240 else 0
px[x, y] = PAL[c]
names = ["black", "red", "green", "brown", "blue", "magenta", "cyan", "white"]
for r in range(32):
for c in range(80):
ch = scr.buffer[r][c]
code = ord(ch.data[0]) if ch.data else 32
if code == 32 and ch.bg == "default":
continue
def colour(n, d):
if n in names: return PAL[names.index(n)]
if n.startswith("bright") and n[6:] in names: return PAL[names.index(n[6:]) + 8]
try:
v = int(n, 16); return ((v >> 16) & 255, (v >> 8) & 255, v & 255)
except ValueError:
return d
fg = colour(ch.fg, PAL[10])
bg = colour(ch.bg, None)
for y in range(16):
bits = font[(code if code < 256 else 63) * 16 + y]
for x in range(8):
on = bits & (0x80 >> x)
for dy in (0, 1):
for dx in (0, 1):
X, Y = c * 16 + 2 * x + dx, r * 32 + 2 * y + dy
if on: px[X, Y] = fg
elif bg is not None and px[X, Y] == (0, 0, 0): px[X, Y] = bg
img.save(path)
def load_ihx(path):
"""Memory image and the end of the loaded bytes (Intel HEX with type 04
extended linear addresses, as objcopy writes it)."""
mem = bytearray(0x28000)
hi = 0
base = 0
for line in open(path):
if not line.startswith(":"):
continue
n, a, t = int(line[1:3], 16), int(line[3:7], 16), int(line[7:9], 16)
if t == 0:
mem[base + a:base + a + n] = bytes.fromhex(line[9:9 + 2 * n])
hi = max(hi, base + a + n)
elif t == 4:
base = int(line[9:13], 16) << 16
return mem, hi
def load_syms(path):
"""Symbol addresses from `nm` output."""
sym = {}
for line in open(path):
f = line.split()
if len(f) == 3:
sym[f[2]] = int(f[0], 16)
return sym
def load_font():
root = os.path.join(HERE, "..")
return [int(l.split("//")[0], 16) for l in open(os.path.join(root, "font", "font.mem"))
if l.split("//")[0].strip()]
+76
View File
@@ -0,0 +1,76 @@
#!/usr/bin/env python3
"""Copy the program in the Basys 3 RISC-V computer's memory to a file on the PC, or send
BASIC a command and print what it answers.
The board needs SW15 and SW14 up (computer mode, PC serial linked: the computer's
output is then copied to the PC), and nothing else may be using the port
(stop the getty first). The baud rate must match SW1:0. BASIC's clock is set
to the PC's first (unless --no-date), so files saved get today's date.
tools/get-bas.py myprog.bas --baud 9600 # LIST into myprog.bas
tools/get-bas.py --cmd 'LOAD "DEMO"' --baud 9600 # run a command, show output
"""
import argparse
import os
import re
import select
import termios
import time
BAUDS = {9600: termios.B9600, 115200: termios.B115200, 921600: termios.B921600,
3000000: termios.B3000000}
ap = argparse.ArgumentParser()
ap.add_argument("file", nargs="?", help="where to write the listing")
ap.add_argument("--cmd", action="append", help="send this command instead (repeatable)")
ap.add_argument("--port", default="/dev/ttyUSB1")
ap.add_argument("--baud", type=int, default=115200, choices=sorted(BAUDS))
ap.add_argument("--timeout", type=float, default=30, help="seconds to wait for READY")
ap.add_argument("--no-date", action="store_true", help="don't set BASIC's clock")
a = ap.parse_args()
if not a.file and not a.cmd:
ap.error("give a file name or --cmd")
fd = os.open(a.port, os.O_RDWR | os.O_NOCTTY)
t = termios.tcgetattr(fd)
t[0] = termios.IXON
t[1] = 0
t[2] = termios.CS8 | termios.CREAD | termios.CLOCAL
t[3] = 0
t[4] = t[5] = BAUDS[a.baud]
t[6][termios.VMIN] = 0
t[6][termios.VTIME] = 0
termios.tcsetattr(fd, termios.TCSANOW, t)
termios.tcflush(fd, termios.TCIFLUSH)
def run(cmd):
"""Send one line; return everything BASIC prints up to its next READY."""
os.write(fd, cmd.encode("ascii") + b"\r")
buf = b""
end = time.time() + a.timeout
while time.time() < end:
r, _, _ = select.select([fd], [], [], 0.2)
if r:
buf += os.read(fd, 4096)
if re.search(rb"\r\nREADY\r\n$", buf):
break
text = buf.decode("latin-1").replace("\r", "")
lines = text.split("\n")
if lines and lines[0].strip() == cmd.strip(): # BASIC echoes the command
lines = lines[1:]
return "\n".join(l for l in lines if l != "READY").strip("\n")
if not a.no_date:
run(time.strftime('DATE "%Y-%m-%d %H:%M:%S"'))
if a.cmd:
for c in a.cmd:
print(run(c))
else:
out = run("LIST")
prog = [l for l in out.split("\n") if re.match(r"\d+ ", l)]
with open(a.file, "w") as f:
f.write("\n".join(prog) + "\n")
print("%d lines written to %s" % (len(prog), a.file))
os.close(fd)
+108
View File
@@ -0,0 +1,108 @@
#!/usr/bin/env python3
"""Convert a Linux console PSF font (8x16) into a CP437-ordered font ROM.
Output is a $readmemh file with 4096 lines: byte (code*16 + row), MSB = leftmost pixel.
usage: psf2mem.py [font.psf.gz[,fallback.psf.gz...]] [out.mem] [preview.png]
Glyphs missing from the first font are taken from the fallbacks in order.
"""
import gzip
import struct
import sys
CP437_LOW = ("\u0000☺☻♥♦♣♠•◘○◙♂♀♪♫☼►◄↕‼¶§▬↨↑↓→←∟↔▲▼")
DEFAULT_FONTS = ",".join("/usr/share/consolefonts/%s.psf.gz" % f for f in
("Uni2-VGA16", "FullGreek-VGA16", "Vietnamese-TerminusBoldVGA16"))
# Fonts often carry the triangle pointers under their newer code points.
CP437_ALT = {"►": "▶", "◄": "◀"}
def cp437(code):
if code < 0x20:
return CP437_LOW[code]
if code == 0x7F:
return "⌂"
return bytes([code]).decode("cp437")
def load_psf(path):
raw = gzip.open(path).read() if path.endswith(".gz") else open(path, "rb").read()
if raw[:2] == b"\x36\x04":
mode, charsize = raw[2], raw[3]
count = 512 if mode & 1 else 256
width, height, hdr = 8, charsize, 4
has_table = bool(mode & 6)
glyphs = [raw[hdr + i * charsize: hdr + (i + 1) * charsize] for i in range(count)]
table = {}
if has_table:
pos, g = hdr + count * charsize, 0
while g < count and pos + 1 < len(raw):
(u,) = struct.unpack_from("<H", raw, pos)
pos += 2
if u == 0xFFFF:
g += 1
elif u != 0xFFFE:
table.setdefault(chr(u), g)
return glyphs, table, width, height
if raw[:4] == b"\x72\xb5\x4a\x86":
_, hdr, flags, count, charsize, height, width = struct.unpack_from("<7I", raw, 4)
glyphs = [raw[hdr + i * charsize: hdr + (i + 1) * charsize] for i in range(count)]
table = {}
if flags & 1:
data = raw[hdr + count * charsize:]
for g, entry in enumerate(data.split(b"\xff")[:count]):
for seq in entry.split(b"\xfe")[:1]:
for ch in seq.decode("utf-8", "ignore"):
table.setdefault(ch, g)
return glyphs, table, width, height
raise SystemExit("not a PSF font: " + path)
def main():
src = sys.argv[1] if len(sys.argv) > 1 and sys.argv[1] else DEFAULT_FONTS
out = sys.argv[2] if len(sys.argv) > 2 else "font.mem"
fonts = []
for path in src.split(","):
glyphs, table, width, height = load_psf(path)
if width != 8 or height != 16:
raise SystemExit(f"{path}: need an 8x16 font, got {width}x{height}")
fonts.append((glyphs, table))
missing = []
rom = []
for code in range(256):
ch = cp437(code)
glyph = None
if code not in (0, 0x20):
for glyphs, table in fonts:
g = table.get(ch, code if not table else None)
if g is None and ch in CP437_ALT:
g = table.get(CP437_ALT[ch])
if g is not None:
glyph = glyphs[g][:16]
break
if glyph is None:
missing.append(f"{code:02X}")
rom.extend(glyph or [0] * 16)
with open(out, "w") as f:
f.write("// CP437 order, 256 glyphs x 16 rows, from: %s\n"
% ", ".join(p.split("/")[-1] for p in src.split(",")))
for b in rom:
f.write(f"{b:02x}\n")
print(f"wrote {out}; glyphs missing: {' '.join(missing) or 'none'}")
if len(sys.argv) > 3:
from PIL import Image
img = Image.new("1", (32 * 9, 8 * 17), 0)
for code in range(256):
ox, oy = (code % 32) * 9, (code // 32) * 17
for r in range(16):
for c in range(8):
if rom[code * 16 + r] & (0x80 >> c):
img.putpixel((ox + c, oy + r), 1)
img.save(sys.argv[3])
if __name__ == "__main__":
main()
Loaded 100 of 103 files, more files were not shown because too many files have changed in this diff. Show more