diff --git a/.gitignore b/.gitignore index 7612eea..6515833 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ sim/cpu/ __pycache__/ monitor/out/ games/keep/out/ +games/raid/out/ diff --git a/README.md b/README.md index f477937..e575db4 100644 --- a/README.md +++ b/README.md @@ -232,6 +232,13 @@ sprite-graphics collision flags. BASIC moves them with `OUT` (ports 112–119) and reads the frame counter and collisions with `INP` (120, 128–135); see the manual. `basic/examples/sprites.bas` is a small example. +**Scrolling:** ports 35h–37h set the point of the 320×240 graphics shown at +the top left; the picture wraps round. Bit 1 of port 36h hides the graphics in +a border (4 columns each side, 2 rows top and bottom) so a program can draw the +strip scrolling into view unseen. The video keeps its own column and row +counters, loaded from the scroll registers at the end of each line and frame, +so scrolling adds no arithmetic to the pixel pipeline. + **Held keys:** port 88h reads the arrows/WASD, fire, Enter, Shift and Esc as held-down bits, and 89h any key by scan code (`rtl/ps2_keyboard.v`, `ps2_keystate`), so games don't depend on typematic repeat. @@ -248,6 +255,19 @@ tools/send-bas.py games/keep/keep.bas --baud 3000000 # the loade python3 games/keep/test-keep.py --play --shots /tmp/keep # emulator test (pyte, Pillow) ``` +**Bastion Raid** (`games/raid/`) is an isometric scrolling shooter: fly over a +fortress, climb over low walls and through the doorways in tall ones, shoot +turrets, fuel tanks and fighters, and destroy the reactor core. `RUN "RAID"`. +It draws the world pixel by pixel from a formula into the scrolled graphics: +two columns and a row a frame, about 40% of the Z-80's time. Build and install +it the same way (`games/raid/build.sh`, then `send-hex.py raid.ihx --save +RAIDGAME` and `raid.bas` saved as `RAID`). `test-raid.py --play` flies the +whole level with an autopilot, checks every frame fits in a 60th of a second, +and takes screenshots. + +`tools/boardemu.py` models the board's game hardware (graphics, scrolling, +sprites, held keys, terminal) around `tools/z80emu.py` for both tests. + `gen.py` holds the art and the castle map and checks that every room joins up and every key, door and the crown can be reached. `test-keep.py` runs the game in `tools/z80emu.py` on a model of the board's ports, takes screenshots, and @@ -412,7 +432,8 @@ stack fill the top half. | 20h / 21h | | 7-segment display, right / left byte | | 30h, 31h, 32h | | graphics X low byte, X bit 8, Y | | 33h | colour at X,Y | plot at X,Y, then X+1 | -| 34h | bit 0: fill busy | fill the whole graphics layer with a colour; also turns the sprites off unless bit 7 is set | +| 34h | bit 0: fill busy | fill the whole graphics layer with a colour; also turns the sprites off and resets the scroll unless bit 7 is set | +| 35h, 36h, 37h | | scroll: X low byte; X bit 8 and bit 1 = hide the border; Y (0–239) | | 40h | bit 0: SPI byte busy, bit 1: command refused | 1 starts a flash command (chip select low), 0 ends it | | 41h | byte received during the last transfer | send a byte to the flash | | 60h | why the monitor was entered (1 Pause, 2 port 61h, 3 step, 4/5 breakpoint) | bit 0: monitor bank in; bit 1: stop after one instruction | @@ -508,6 +529,8 @@ older boards, Macronix `mx25l3273f` on newer ones). - It holds Ctrl and right Shift on the PS/2 line and reads them back on the held-key ports, then releases them. - In the monitor, it sends an Intel HEX record and checks memory. +- It scrolls the graphics and checks where pixels land, wrapping round, with + and without the border, and that `CLS` resets it. - Against a model flash (`sim/spiflash_model.v`) it sets the date, saves, loads, runs and lists a program (checking the date in `DIR`), tries to erase address 0 and checks the model saw no write below 240000h, then writes a data @@ -534,6 +557,8 @@ which Vivado needs on newer distros. | `tools/send-bas.py`, `tools/get-bas.py` | Send a BASIC program to the board / copy one back, run commands | | `tools/send-hex.py` | Load an Intel HEX file through the monitor and save it as a memory file | | `games/keep/` | The Iron Keep: game (C), art and map generator, loader, emulator test | +| `games/raid/` | Bastion Raid: game (C), art and level generator, loader, emulator test with autopilot | +| `tools/boardemu.py` | Model of the board's game hardware around `z80emu.py`, with screenshots | | `basic/` | BASIC interpreter (C), start-up code, build script, `basic.mem` | | `tools/z80emu.py`, `tools/cputest.py`, `scripts/cpusim.sh` | CPU test bench: reference emulator, random tests | | `rtl/term_ctrl.v` | VT220 engine (parser, modes, attributes, copy/fill engine, smooth scroll) | diff --git a/docs/basic-manual.md b/docs/basic-manual.md index 8cb9752..ba08dac 100644 --- a/docs/basic-manual.md +++ b/docs/basic-manual.md @@ -507,6 +507,35 @@ does restarting the Z-80. `OUT 117, 0` hides the picked one. `basic/examples/sprites.bas` is a complete example: a robot steered with W A S D among bouncing balls, with shapes written as rows of hex digits. +### Scrolling + +The hardware can scroll the graphics: it shows them starting from any point of +the 320 × 240 picture, wrapping round at the edges, so the whole picture moves +without anything being redrawn. Sprites and text don't move with it. + +| Port | `OUT` | +|---|---| +| 53 | X of the picture to show at the left edge, `X AND 255` | +| 54 | `INT(X / 256)`, plus 2 to hide the edges (below) | +| 55 | Y of the picture to show at the top, 0–239 | + +``` +10 CLS: FOR I = 0 TO 15: FILL I * 20, 0, I * 20 + 19, 239, I: NEXT I +20 FOR X = 0 TO 319 +30 OUT 53, X AND 255: OUT 54, INT(X / 256) +40 F = INP(120) +50 IF INP(120) = F THEN 50 +60 NEXT X +``` + +A program that scrolls a long scene has to draw the strip that comes into view +at one edge each time. To do that unseen, add 2 to the value sent to port 54: +the graphics are then hidden in a border, 4 pixels wide at the sides and 2 at +the top and bottom, and the new strip can be drawn there while it is hidden. +Bastion Raid (section 14) works this way. `PLOT`, `LINE` and the rest still use +the picture's own coordinates. `CLS` puts the scroll back to 0 and turns the +border off. + ### Speed `CLS` clears the graphics in under a millisecond, and a line right across the @@ -568,7 +597,8 @@ kept free for it. | 33 | — | left two digits of the 7-segment display | | 48, 49, 50 | — | graphics X (low byte, high bit), Y | | 51 | colour at X, Y | plot a pixel at X, Y and move right | -| 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour; also hides the sprites, unless 128 is added | +| 52 | bit 0: screen fill in progress | fill the whole graphics layer with a colour; also hides the sprites and resets the scroll, unless 128 is added | +| 53, 54, 55 | — | graphics scroll and border (section 7) | | 64 | bit 0: flash byte in progress; bit 1: last flash command refused | 1 selects the flash and starts a command, 0 ends it | | 65 | the byte the flash sent back during the last `OUT 65` | a byte to the flash | | 96 | why the monitor was entered (used by the monitor) | monitor bank and single step (used by the monitor) | @@ -1111,6 +1141,34 @@ the rest of BASIC's program area while it runs, so save your own program first. It is written in C (`games/keep/` on the PC, with its build and install steps). +### Bastion Raid + +An isometric scrolling shooter, also in machine code. Fly over a fortress that +scrolls towards you and destroy the reactor core at its far end. + +``` +RUN "RAID" +``` + +- **Up and down** climb and dive; **left and right** steer across the + fortress; **Space** or **Ctrl** fires straight ahead. +- **Walls:** climb over the low ones. The tall ones are too high: fly low + through the doorway, the gap with the bright lintel. +- **Height:** your shadow on the floor and the ALT bar show how high you are. + Shots only hit things on the ground when you fly low. +- **Fuel** runs down; shoot the red fuel tanks to fill up. Turrets fire at you + when you are in front of them, and between the two fortresses fighters come + at you at all heights. +- **Score:** 100 a turret, 150 a fuel tank, 300 a radar dish, 200 a fighter, + 2,000 for the reactor and 500 for each level. At each level the turrets fire + sooner and the fighters come more often. +- **P** pauses; **Esc** gives up, and Esc at the title goes back to BASIC. + +`RAID` loads the memory file `RAIDGAME` at 8600h, like `KEEP`. The game draws +the fortress pixel by pixel into the scrolled graphics, two new columns and a +row each frame, and everything that moves is a sprite (`games/raid/` on the +PC). + ## 15. Limits at a glance | Item | Limit | diff --git a/docs/basic-manual.pdf b/docs/basic-manual.pdf index fe6b086..3a1236e 100644 Binary files a/docs/basic-manual.pdf and b/docs/basic-manual.pdf differ diff --git a/games/keep/build.sh b/games/keep/build.sh index aba272e..cdbc042 100644 --- a/games/keep/build.sh +++ b/games/keep/build.sh @@ -1,5 +1,5 @@ #!/bin/bash -# Build The Iron Keep: keep.ihx (Intel HEX for tools/send-hex.py) and keep.bin. +# Build The Iron Keep: keep.ihx, Intel HEX for tools/send-hex.py. # The program runs at 8600h with its variables at D000h, inside BASIC's # program area (8482h-D6D5h) and clear of the KEEP loader program at its start. set -e diff --git a/games/keep/test-keep.py b/games/keep/test-keep.py index fce3d95..dd059e8 100644 --- a/games/keep/test-keep.py +++ b/games/keep/test-keep.py @@ -16,141 +16,11 @@ import sys HERE = os.path.dirname(os.path.abspath(__file__)) sys.path.insert(0, os.path.join(HERE, "..", "..", "tools")) -from z80emu import Z80 # noqa: E402 +from boardemu import Board, load_ihx, load_font # noqa: E402 -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(Z80): - def __init__(self, mem): - super().__init__(mem) - self.frame = 0 - 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: # each read is a new frame - self.frame += 1 - 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 - 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 + 2 * n # wait_frame reads the counter twice - while self.frame < end and self.pc != 0: - self.step() - if not (0x8600 <= self.pc < self.code_end or self.pc == 0): - raise SystemExit("PC left the program: %04X" % self.pc) - if self.sp < 0xF000: - raise SystemExit("stack ran into memory: SP %04X" % 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(self.gfx) - 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 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 @@ -226,17 +96,6 @@ def play_through(b, a, check, font): check("Esc returns to BASIC", b.pc == 0) -def load(path): - mem = bytearray(65536) - hi = 0 - for line in open(path): - n, a, t = int(line[1:3], 16), int(line[3:7], 16), int(line[7:9], 16) - if t == 0: - mem[a:a + n] = bytes.fromhex(line[9:9 + 2 * n]) - hi = max(hi, a + n) - return mem, hi - - def main(): ap = argparse.ArgumentParser() ap.add_argument("--shots", help="write PNG screenshots here") @@ -244,18 +103,12 @@ def main(): 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(os.path.join(HERE, "out", "keep.ihx")) - b = Board(mem) - b.code_end = hi - b.m[0] = 0x76 # HALT: where main returns to - b.pc, b.sp = 0x8600, 0xFFF0 - b.push(0x0000) + mem, hi = load_ihx(os.path.join(HERE, "out", "keep.ihx")) + b = Board(mem, code_end=hi) font = None if a.shots: os.makedirs(a.shots, exist_ok=True) - root = os.path.join(HERE, "..", "..") - font = [int(l.split("//")[0], 16) for l in open(os.path.join(root, "font", "font.mem")) - if l.split("//")[0].strip()] + font = load_font() errors = 0 def check(what, ok): diff --git a/games/raid/build.sh b/games/raid/build.sh new file mode 100644 index 0000000..3daed31 --- /dev/null +++ b/games/raid/build.sh @@ -0,0 +1,25 @@ +#!/bin/bash +# Build Bastion Raid: raid.ihx, Intel HEX for tools/send-hex.py. +# The program runs at 8600h with its variables at D000h, inside BASIC's +# program area (8482h-D6D5h) and clear of the RAID loader program at its start. +set -e +cd "$(dirname "$0")" +mkdir -p out +python3 gen.py out/data.h +sdasz80 -plosgff out/crt0.rel crt0.s +sdcc -mz80 --opt-code-size --max-allocs-per-node 20000 --std-sdcc11 -Iout -c -o out/raid.rel raid.c +sdcc -mz80 --no-std-crt0 --code-loc 0x8620 --data-loc 0xD000 -o out/raid.ihx out/crt0.rel out/raid.rel +python3 - <<'EOF' +import re +m = open("out/raid.map").read() +def sym(n): + return int(re.search(r"([0-9A-F]{8})\s+" + n + r"\b", m).group(1), 16) +code_end = sym("s__CODE") + sym("l__CODE") +data_end = sym("s__DATA") + sym("l__DATA") +init = sym("l__INITIALIZED") + sym("l__INITIALIZER") +print("code 8600-%04X (%d bytes), variables D000-%04X" % (code_end - 1, code_end - 0x8600, data_end - 1)) +assert code_end <= 0xD000, "code runs into the variables" +assert data_end <= 0xD6D6, "variables run past BASIC's program area" +assert init == 0, "initialised variables are not supported" +EOF +cp out/raid.ihx raid.ihx diff --git a/games/raid/crt0.s b/games/raid/crt0.s new file mode 100644 index 0000000..8a1ba73 --- /dev/null +++ b/games/raid/crt0.s @@ -0,0 +1,37 @@ +; Start-up for a machine-code program that BASIC CALLs at 8600h: clear the +; variables, run main, and return to BASIC. The program has no initialised +; variables (gen.py's tables are const, so they stay in the code). + .module crt0 + .globl _main + .globl s__DATA, l__DATA + + .area _HEADER (ABS) + .org 0x8600 +start: + ld hl, #s__DATA + ld bc, #l__DATA + ld a, b + or c + jr z, go + ld (hl), #0 + dec bc + ld a, b + or c + jr z, go + ld d, h + ld e, l + inc de + ldir +go: + jp _main ; main's RET goes back to BASIC + + .area _HOME + .area _CODE + .area _INITIALIZER + .area _GSINIT + .area _GSFINAL + .area _DATA + .area _INITIALIZED + .area _BSEG + .area _BSS + .area _HEAP diff --git a/games/raid/gen.py b/games/raid/gen.py new file mode 100644 index 0000000..c695573 --- /dev/null +++ b/games/raid/gen.py @@ -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") diff --git a/games/raid/raid.bas b/games/raid/raid.bas new file mode 100644 index 0000000..5f21d08 --- /dev/null +++ b/games/raid/raid.bas @@ -0,0 +1,4 @@ +10 REM BASTION RAID: LOAD THE GAME (MEMORY FILE RAIDGAME) AND PLAY IT +20 REM THE GAME RUNS AT 8600H, PAST THIS PROGRAM, AND COMES BACK HERE +30 BLOAD "RAIDGAME": CALL 34304 +40 CLS diff --git a/games/raid/raid.c b/games/raid/raid.c new file mode 100644 index 0000000..3321fab --- /dev/null +++ b/games/raid/raid.c @@ -0,0 +1,694 @@ +/* Bastion Raid: an isometric scrolling shooter for the Basys 3 Z-80. + + 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 8600h, 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 int u16; +typedef int i16; + +__sfr __at 0x00 con_stat; +__sfr __at 0x01 con_data; +__sfr __at 0x30 gx_lo; +__sfr __at 0x31 gx_hi; +__sfr __at 0x32 gy_p; +__sfr __at 0x33 gpix; +__sfr __at 0x34 gfill; +__sfr __at 0x35 scr_xlo; +__sfr __at 0x36 scr_xhi; +__sfr __at 0x37 scr_y; +__sfr __at 0x70 sp_sel; +__sfr __at 0x71 sp_xlo; +__sfr __at 0x72 sp_xhi; +__sfr __at 0x73 sp_y; +__sfr __at 0x74 sp_shape; +__sfr __at 0x75 sp_ctl; +__sfr __at 0x76 sp_load; +__sfr __at 0x77 sp_data; +__sfr __at 0x78 frame_ctr; +__sfr __at 0x88 joy; + +#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(); } + } +} + +void 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; +} diff --git a/games/raid/raid.ihx b/games/raid/raid.ihx new file mode 100644 index 0000000..7a9d8d1 --- /dev/null +++ b/games/raid/raid.ihx @@ -0,0 +1,309 @@ +:198600002100D001D60078B1280C36000B78B12805545D13EDB0C389AB4D +:208620004FDB00CB4F28FA79D301C9000000000000000000000000000000000000000000BE +:208640000000000000000000000000000000000000000000000000000FFF00CCCCCC0FF6A3 +:2086600066F000CCCCCFFFF6660000000FFFFFFFF00000CCCFFFFFFCC00000CCCCCF0CCC52 +:20868000CC000000CCC000CCCC00000000C00000C00000000000000000000000000000006A +:2086A0000000000000000000000000000000000000000000000000000000000000000000BA +:2086C00000000000000000000000000000000000000000000000000004440044444404443E +:2086E0004440004444444444440000000444444440000044444444444000004444440444A6 +:20870000440000004440004444000000004000004000000000000000000000000000000089 +:20872000000000000000000000000000000000000000000000000000000000000000000039 +:20874000000000000001000001000000001111000111000000111110D111110000011DDDC3 +:20876000DDD11100000DDDDDDDD0000000BBBDDDD11111000DBBBDD011111100DDD0000049 +:208780000000000000000000000000000000000000000000000000000000000000000000D9 +:2087A0000000000000000000000000000000000000000000000000000000000000000000B9 +:2087C000000000000000000000000000000000000000000000000000000000000002222253 +:2087E000200000000022AA2222000000022AAAA2222000000222AA22222000008222722225 +:20880000222800088827772222888000877722222888000077788888888000000008888883 +:208820008000000000000000000000000000000000000000000000000000000000BBBBBB87 +:20884000BB0000000BBBBBBBBBB000000BBBBBBBBBB0000009BBBBBBBB900000099F911140 +:2088600019900000099F911999900000099F911199900000099F911999900000099F911994 +:20888000999000000999999999900000011111111110000000111111110000000000000019 +:2088A00000000000000000000000000000000000000000000000000000000000EE000000CA +:2088C00000000000EEEEE000000000000E666EEEE00000000EEE66666EE00000000EEEE634 +:2088E0006E00000000000770EE000000000007700000000000000770000000000000077040 +:208900000000000000000770000000000000088000000000008888888800000000888888A0 +:20892000880000000000088000000000000000000000000000000550000000000000555528 +:208940000000000000055DD5500000000055DDDD55000000055DFFFFD550000055DDFFFF77 +:20896000DD55000555DDFFFFDD555000555DFFFFD55500000555DDDD5550000000555DD5F4 +:20898000550000000005555550000000000055550000000000000550000000000000000084 +:2089A0000000000000000000000000000000000000000000000000000000000000000000B7 +:2089C0000000000000009900000000000099B00009900000090B00BB0000000000BBFF0F84 +:2089E000B00000009B0FFFF0BB00000000B00FF00BB0000090B0FFF00090000000BB0BB0D4 +:208A0000000000000000B0000000000000090000000000000000000000000000000000009D +:208A2000000000000000000000000000000000000000000000000000000000000000000036 +:208A4000000000000001010100000000000009010000000009900009000000000000000067 +:208A6000091000000990BBBB99910000090BBBBB909100000000B0B0900100000009990066 +:208A8000090000000101000990000000000011011000000000000000000000000000000010 +:208AA0000000000000000000000000000000000000000000000000000000000000000000B6 +:208AC000000000000000000000000000000000000000000000000000000000000000000096 +:208AE0000000000000000FF00000000000000FB000000000000000000000000000000000B8 +:208B0000000000000000000000000000000000000000000000000000000000000000000055 +:208B2000000000000000000000000000000000000000000000000000000000000000000035 +:208B400000000000000000000000000000000000000000000000000000000000000009907C +:208B6000000000000000999900000000000099F90000000000000990000000000000000098 +:208B80000000000000000000000000000000000000000000000000000000000000000000D5 +:208BA0000000000000000000000000000000000000000000000000000001010101010101AE +:208BC00001010103010101010101010105010101010101010101030101010101010101016D +:208BE00001012D0101010101010101010101012D01010101010101010101010103010101FB +:208C000001010101012D0101010101010101010101250101010101010101010101010101E4 +:208C20000101010101010101010101010101010101808080808080808080808080808080A3 +:208C4000808080808080808080808080808080808080808080808080808080808080808014 +:208C600080808080808080808080808080010101010101010101013D010101010101010125 +:208C8000010103010101010101010101012D01010101010101010101010115010101010172 +:208CA00001010101010101030101010101010101010101014501010101010101014501010A +:208CC00001010101010101010101030101010101010101010115010101010101010101015E +:208CE00001010101010101010101010101010101010101010101010101000000000000005B +:208D00000000000000000000001E0001072A00010430000207310001053400010540000311 +:208D20000743000203480003084B0001064B0002014F0002025F000105600003016500026E +:208D4000056500020872000204790001067E0001047E00010781000105CB000107E1000360 +:208D600004E8000301EB000204F3000106F700020104010101070101010F010108100101E2 +:208D80000216010302190101071B0101031B010207240101032501020526010201280101A4 +:208DA000062F010105300101083A01010440010208440102024B0104057EB7C823E5CD2022 +:208DC00086E118F5DDE5DD210000DD39F5F5F53B4D44DD36FF00DD5EFF16002100003919C9 +:208DE000DD34FFE5C5110A006960CD3DACC1E17BC63077110A006960CDEFAB4B4278B12074 +:208E0000D5DD4EFFDD46047990300AC53E30CD2086C10518F279B728110D5916002100006D +:208E200039197EC5CD2086C118EBDDF9DDE1E133E967E53E1BCD20863E5BCD2086E14C265E +:208E400000E5AFF53369CDC48D3E3BCD2086E1AF67F533CDC48D3E48C32086E53E1BCD208C +:208E6000863E5BCD2086E1CDB98D3E6DC32086470E00692600197EB728030C18F52E00CBE9 +:208E80002DCB193E2991D56F78CD318ED1EBC3B98D2AD1D0292929292929293AD1D0AD3218 +:208EA000D1D03AD2D0AC32D2D0CB3F4F3AD1D0A932D1D021D2D0AE32D2D0ED5BD1D0C9DB33 +:208EC0007821CCD09628F8DB7832CCD0C90E00DB000F300EDB01FE702804D65020F10E01CB +:208EE00018ED79C9CB7C200E7DD65E7CDE01300601AB8B097EC9AFC9DDE5DD210000DD399F +:208F0000FD21F3FFFD39FDF9DD75FCDD74FDDD73FADD72FBDD7EFADD77F3DD7EFBDD77F4AB +:208F20000605DDCBF42EDDCBF31E10F6DD7EF3C601DD77F5DD7EF4CE00DD77F6DD6EFADDB6 +:208F400066FB11700019DD75FEDD74FF0605DDCBFF2EDDCBFE1E10F6DD7EFEDD96F5DD7EB0 +:208F6000FFDD9EF6E2698FEE80FA5490AFDD96FE6F9FDD96FF67CDE48E47E606CA4490DD67 +:208F80006EFEDD66FF29292929297DDD96FA5F7CDD9EFB5778E604DD77F7DD36F800AFDD80 +:208FA000B6F70E3820020E146B62CB2CCB1DDD75F979DD96F9DA44907BDD86FC5F7ADD8ED2 +:208FC000FD57CB7AC244907BD6407ADE0130756B623E05CB2CCB1D3D20F9DDB6F728247840 +:208FE0000F0F0FE60F477D90381904047D903013DD7EF9D61E384DDD7EF9D62130053E0EB9 +:20900000C3C79106000BDD6EF97D91380EAFDDB6F70E0F20020E0B79C3C791DD7EF9E60726 +:20902000280F7DE6086F260029197DE61FD60230053E08C3C791AFDDB6F70E0720020E0346 +:2090400079C3C791DD6EFEDD66FF2BDD75FEDD74FFC3588FDD7EFD07E601DD77FEB72856AF +:20906000DD7EFCD6D0DD7EFD173F1FDE7F3847DD7EFADD96FC4FDD7EFBDD9EFD5F0605CB34 +:209080002BCB1910FAAF916F9F9367CDE48E0F3025AFDD96FC4F9FDD96FD47CB28CB1979B9 +:2090A000FE1738053E07C3C791E60328053E08C3C791AFC3C791DD7EFCD640DD7EFD173FA2 +:2090C0001FDE813E0017DD77FFDDCBFF462051DD7EFCD668DD7EFDDE013045DD7EFCC6C0EE +:2090E0004FDD7EFDCEFE47DD7EFA915FDD7EFB98574B620605CB2CCB1910FAAF916F9F94AD +:20910000D567CDE48ED10F30177BE61FD6023006DD36FF001804DD36FF04DD7EFFC3C79166 +:20912000DD4EFECB412060DD7EFFB7285AAFDD96F36F9FDD96F467CDE48E0F304ADD7EFCD7 +:20914000E61F5FD602380ADD7EFAE61F4FD6023003AF1873DD6EFCDD66FD3E05CB2CCB1DFA +:209160003D20F9DD7EF385E60320183E0DBB30137BD612300E3E0DB9300979D61230043EAB +:209180000718443E081840DD4EFCDD46FD6960292909EBDD4EFADD46FB69602909190E0013 +:2091A0007DD607B12020DD7EFCDDAEFA4FDD7EFDDDAEFB79E660200EDDCBFC6628043E0FEB +:2091C00018053E071801AFDDF9DDE1C9DDE5DD210000DD39F5F5F53B4D442A00D009EB2A6F +:2091E00002D0297B85DD77FE7A8CDD77FFBFED52EB2A04D0093A06D04F7DD6407CDE013854 +:20920000087DC6C06F7CCEFE67DD75F9DD74FADD36FB00DD7EFEDD77FCDD7EFFDD77FDDD50 +:2092200073FEDD72FFC5DD5EFEDD56FFDD6EFCDD66FDCDF88EC147DD5EFEDD56FF1BDD6E8C +:20924000FCDD66FD23C5CDF88E5FC1DD7EF9D330DD7EFAD33179D33278D3337BD333DD7EEF +:20926000FCC602DD77FC3003DD34FDDD7EFEC602DD77FE3003DD34FF0C79D6F020014FDD4B +:2092800034FBDD7EFBD6F0389CDDF9DDE1C9CD1EAC21FBFF39F9DD77FB4F06002A02D009C5 +:2092A000294D443A00D081DD77FE3A01D088DD77FF2100D07996235F789E57ED4B04D03AFC +:2092C00006D0DD86FB6FD6F038047DC6106F79D33078D3317DD332AFDD77FCDD77FDE5C5B3 +:2092E000D5DD6EFEDD66FFCDF88ED1C1E1D333DD34FE2003DD34FF1B0379D6402012783D6C +:20930000200E0100003E00D3303E00D3317DD332DD34FC2003DD34FDDD7EFCD640DD7EFD16 +:20932000DE0138BADDF9DDE1C93A04D0D3353A05D0F602D3363A06D0D337C94D442909290A +:2093400009290929092909290929092909290929092929292929EB21100019EB3EA0934FC7 +:209360009F9247CB28CB192100D079C660772378CEFF7721A00019EBCB2ACB1BCB2ACB1B3D +:209380002102D07BC68877237ACEFF773A00D0E6FE3200D02A02D011780019294D443A0037 +:2093A000D0C6A05F3A01D0CE00572107D079937723789A771140012A00D0CD30ACED530488 +:2093C000D02A04D0CB7C280C2104D07EC64077237ECE017711F0002A02D0CD30ACCB7A285A +:2093E0000521F00019EB2106D0730E00C579CD8E92C10C79D6F038F4C92A00D0232322004D +:20940000D02A02D02B2202D02107D07EC6FC77237ECEFF772A04D023232204D01140012A17 +:2094200004D0BFED52380C2104D07EC6C077237ECEFE773A06D0B728063A06D03D18023E28 +:20944000EF3206D0213E01CDCC91AFC38E92CD1EACD3707BDD96044F7ADD9E0547CB28CBDF +:20946000192100D07996234F789E4779C6F84F78CEFF47DD6E04DD660519EBCB2ACB1BCBB1 +:209480002ACB1B2102D07B96235F7A9E577BC6F86F7ACEFF67DD5E061600BFED5279D6F1DC +:2094A00078173F1FDE7F38273E3FB93E0198E2B394EE80FACF947DD6F17C173F1FDE7F3838 +:2094C0000E3EEFBD3E009CE2CC94EE80F2D494AFD375181579D37178E601D3727DD373DD8B +:2094E0007E07D374DD7E08D375DDE1E1F1F133E9D370AFD375C90E00C579CDF094C10C7972 +:20950000D62038F4C9012B861E007BD37616000AD37703147AD68038F61C7BD60B38EBC9E9 +:20952000CB7C2807AF955F9F9457C9EBC9DDE5DD210000DD39F5F5DD75FEDD74FF3333D570 +:209540000E00592166D0160019562166D00600097A9630014B1C7BD60338E8591600EB29C3 +:20956000EB2157D019DD7EFE7723DD7EFF77215DD019DD7EFC7723DD7EFD772163D006005A +:2095800009DD7E04772166D00600093618DDF9DDE1E133E922D3D021D5D0367821CED03679 +:2095A00001C967E53E5BCD2086E10E0079BD30139430043E2318023E2DE5C5CD2086C1E1B4 +:2095C0000C18E93E5DC32086AF32CED03A0FD032CFD03A0DD02E06CDEAAB7B32D0D03E01D8 +:2095E0006FCD318E21C996CD5B8E21CE96CDB98D21DE96CD5B8E21E396CDB98D21EA96CD3C +:209600005B8E3E06F5332A17D0CDC48D21DE96CD5B8E21EF96CDB98D21EA96CD5B8E3A1021 +:20962000D06FAF67F533CDC48D21DE96CD5B8E21F996CDB98D21EA96CD5B8E3A0ED06FAFF4 +:2096400067F533CDC48D210397CDB98D2E013E02CD318E3A0FD0D61930052107971803215C +:20966000DE96CD5B8E210C97CDB98D3A0FD026006F11090019110A00CD4CAC7B2E0ACDA206 +:209680009521DE96CD5B8E211297CDB98D211A97CD5B8E3AD0D03C2E09CDA2953AD4D02135 +:2096A000D3D0B628183AD5D0B7281221C996CD5B8E211F97CDB98D2AD3D0CDB98D21039786 +:2096C000CDB98D212397C35B8E303B39330042415354494F4E205241494420202000303BFE +:2096E00033370053434F52452000303B3937002020204C4556454C200020202053484950FD +:209700005320001B5B4B00303B3931004655454C2000202020414C542000303B3936002099 +:209720002020003000DDE5DD210000DD39D5CDE48E5FE606282EDD4E05AF4791579F906F82 +:20974000DD7EFE92DD7EFF9DE24D97EE80FA649721400109DD7EFE95DD7EFF9CE26197EEE7 +:2097600080FA6797AF1870CB4B280C3E16DD96043E0017EE0118603E38DD96043003AF1882 +:20978000567B0F0F0FE60F6FDD7EFE915FDD7EFF9857260029292929297B957A9CE2A297A1 +:2097A000EE80FAD097DD7EFE814FDD7EFF8847114000197995789CE2BC97EE80F2D097DD23 +:2097C0004E04060021080009111E00BFED523803AF18023E01EE01C1DDE1E1C1E94F3A11FC +:2097E000D0B7C03A12D0B7C03A13D0B7280379B7C83A0DD0F533ED5B0BD02A09D0CD2D95A4 +:20980000AFCDF0943E1BCDF0942112D036012116D0365A0E002153D0060009360079C608EF +:20982000C5CDF094C10C79D60438EAC9CD1EAC21F7FF39F93A07D0C6804F3A08D0CEFE5F44 +:209840000605CB2BCB1910FAAF91DD77F79F93DD77F8112C002AC9D0BFED52D271992AC943 +:20986000D0292911098D19EB1A4F6B62236EDD71F9DD75FADD7EF7DD96F9DD7EF8DD9EFA3B +:20988000E28598EE80FA7199DD36FF00DD7EFFD60C30143EA5DD86FF6F3ED0CE00677EB799 +:2098A0002805DD34FF18E5DD7EFFD60CCA7199DD6EFF260001A5D0093601DD7EFFC6994F30 +:2098C0003E00CED04721020019DD75FBDD74FC7E02DD6EFF2600294D442169D009DD75FD33 +:2098E000DD74FE210300195E1600EB2929292929EB21100019EBDD6EFDDD66FE73237221DE +:2099000081D009EBDD4EF9DD46FA6960290929092909290929092909290929092909290904 +:209920002929292929011000097D12137C12DD6EFF260011B1D019E5CD918EE17BE63FC6DD +:209940002877DD7EFFC6BDDD77FE3E00CED0DD77FFDD6EFBDD66FC7ED6043E0428023E0182 +:20996000DD6EFEDD66FF772AC9D02322C9D0C35298DDF9DDE1C9CD1EACF53BDD77FF01BD62 +:20998000D0DD6EFF260009354EDD6EFF260029E379B7281D0181D0E1E5095E23560169D0D8 +:2099A000E1E5097E23666F3E04F533CD2D95C3529A01A5D0DD6EFF2600093600DD7EFFC675 +:2099C0000FCDF0940181D0E1E5095E23560169D0E1E5097E23666FAFF533CD2D950199D0E0 +:2099E000DD6EFF2600097EFE01280EFE022817FE03283AD604284418542117D07EC66477C0 +:209A0000304B233418472117D07EC69677300223343E4B210FD09630043E6418053A0FD008 +:209A2000C619320FD021579ACD949518202117D07EC62C77237ECE017718122117D07EC6AA +:209A4000D077237ECE0777215C9ACD949521CED03601DDF9DDE1C94655454C00524541432B +:209A6000544F522044455354524F5945442100CD1EAC21F6FF39F9DD36FE00DD7EFEC6A549 +:209A8000DD77FC3E00CED0DD77FDDD6EFCDD66FD7EB7CA719CDD5EFE1600EB29EB2181D0F6 +:209AA00019DD75F8DD74F9C1E1E5C57EDD77F6237EDD77F72A07D001000109DD7EFEC60FBF +:209AC0004F7DDD96F67CDD9EF7E2CE9AEE80F2E09ADD6EFCDD66FD360079CDF094C3719CE8 +:209AE0003E99DD86FEDD77FC3ED0CE00DD77FDDD6EFCDD66FD7EC6816F3E00CE9C67462180 +:209B000069D019DD75FADD74FB5E23563E01F533C5333E04F533DD6EF6DD66F7E579CD4EC7 +:209B2000943A12D0B720563A0DD0D60C304FDD6EFADD66FB4E23462109D07996235F789EF5 +:209B400057EBCD20957BD6107A173F1FDE80302DD1E1E5D54E2346210BD07996235F789E70 +:209B600057EBCD20957BD6107A173F1FDE80300DDD7EFECD7699AFCDDD97C3719CDD6EFCFA +:209B8000DD66FD7E3DC2719C3A12D0B7C2719CD1E1E5D54E2346210BD0799623789EE2A36D +:209BA0009BEE80F2719C3A0BD05F3A0CD03D577B917A98E2B89BEE80F2719C3EB1DD86FE6F +:209BC0004F3ED0CE00470AB728053D02C3719CDD36FF00DD7EFFD6043E00175FCB432814D2 +:209BE000DD7EFFC6536F3E00CED0677EB72805DD34FF18DF7BB72849DD6EFF260029DD7549 +:209C0000FCDD74FDDD7EFCC6435F3E00CED057DD6EFADD66FB7E236612137C12DD7EFCC6AE +:209C20004B5F3E00CED057DD6EF8DD66F97E236612137C123E53DD86FF6F3ED0CE00673633 +:209C400046DD71FCDD70FD3E032110D09630040E28180E3A10D04F87878187874F3E64913A +:209C60004FC5CD918EC17BE63F81DD6EFCDD66FD77DD34FEDD7EFED60CDA7B9ADDF9DDE137 +:209C8000C90003040506CD1EAC21F8FF39F9DD36FF00DD7EFFC6534F3E00CED0470AB7CA86 +:209CA000F19D3D02DD5EFF1600EB29EB214BD019E3E1E57E2366C606DD77FD7CCE00DD77C8 +:209CC000FEE1E5DD7EFD7723DD7EFE772143D019DD75FADD74FB5E23562109D07B96237A9A +:209CE0009EE2E69CEE80F2F59C13DD6EFADD66FB732372181A2109D07E23937E9AE2029DDA +:209D0000EE80F20F9D1BDD6EFADD66FB7323720A5FDD7EFFC608DD77FC7BB7284BE1E55EE7 +:209D200023567BC6FA6F7ACEFF673E05CB2CCB1D3D20F93E05CB2ACB1B3D20F9BFED522840 +:209D400032DD6EFADD66FB7E23666FAF93DD77FD9F92DD77FEC5010A02C5EBDD6EFDDD66B5 +:209D6000FECD2597C1B7280BAF02DD7EFCCDF094C3F19DE1E55E2356DD6EFADD66FB7E2346 +:209D8000666FC5010A01C53E0AF533EBE5DD7EFCCD4E94C13A12D0B72057DD6EFADD66FB84 +:209DA0005E23562109D07B96235F7A9E57C5EBCD2095C17BD60C7A173F1FDE803033E1E50A +:209DC0005E2356210BD07B96235F7A9E57C5EBCD2095C17BD60C7A173F1FDE8030133A0D87 +:209DE000D0D616300CAF02DD7EFCCDF094AFCDDD97DD34FFDD7EFFD604DA929CDDF9DDE142 +:209E0000C9CD1EAC21ECFF39F93A12D0B7C2DA9E2A07D00605CB2CCB1D10FAAF954F9F94DC +:209E20004721040009CDE48E07D2DA9E21CBD0353ACBD0B7C2DA9E3E032110D09630040E4C +:209E400019180D3A10D04F878781874F3E37914FC5CD918EC17BE61F8132CBD0DD36FF004F +:209E6000DD7EFFD6043E00174FDD6EFF2600113FD019CB4128097EB72805DD34FF18E17940 +:209E8000B728573601DD6EFF2600294D44212BD009E5C5CD918EEB111001CD3DACC1E17B90 +:209EA000C6185F7ACE007323772133D009EB3A07D0C6204F3A08D0CEFF477912137812DD87 +:209EC0007EFFC63B4F3E00CED047C5CD918EEB112800CD3DACC17BC60602DD36FE00DD7E91 +:209EE000FEC63FDD77EC3E00CED0DD77EDE1E57EDD77FFB7CA8BA1DD6EFE260029DD75FCD8 +:209F0000DD74FD3E33DD86FCDD77EE3ED0DD8EFDDD77EFD1E1E5D54E234621060009EBDD0D +:209F20006EEEDD66EF7323723ACCD0E60320213E3BDD86FE4F3ED0CE00470A5F210DD09642 +:209F400030057B3C0218093A0DD09330037B3D02C1E1E5C55E23562A07D001000109DD7ED1 +:209F6000FEC6044F7D937C9AE26D9FEE80F27B9FE13600E579CDF094C38BA13E3BDD86FEAD +:209F8000DD77F03ED0CE00DD77F1DD6EF0DD66F1463E2BDD86FCDD77F23ED0DD8EFDDD77CF +:209FA000F3DD6EF2DD66F37E23666FDD71F4C53E01F5333E02F533C533EBE579CD4E94C13E +:209FC0003A12D0B7C25CA0DD6EF2DD66F34623562109D07896235F7A9E57C5EBCD2095C1D2 +:209FE0007BD6107A173F1FDE803071DD6EEEDD66EF462356210BD07896235F7A9E57C5EB3D +:20A00000CD2095C17BD6107A173F1FDE80304DDD6EF0DD66F16E26003A0DD05F1600BFED97 +:20A0200052C5CD2095C17BD60A7A173F1FDE80302BE13600E579CDF094DD6EF0DD66F14648 +:20A04000DD6EEEDD66EF5E2356DD6EF2DD66F37E23666FC533CD2D95AFCDDD97DD36F50056 +:20A06000DD36FF003E28DD86FFDD77F63ED0CE00DD77F7DD6EF6DD66F77EB7CA7AA1DD6EB5 +:20A08000FF2600294D442119D0095E2356DD6EF2DD66F37E23666F7B956F7A9CC567CD2060 +:20A0A00095C17BD6107A173F1FDE80D27AA1211FD0094E2346DD6EEEDD66EF5E2356799391 +:20A0C0006F789A67CD20957BD6147A173F1FDE80D27AA13E25DD86FF6F3ED0CE00677EDDA5 +:20A0E00077F8DD36F900DD6EF0DD66F17EDD77FADD36FB00DD7EF8DD96FADD77FC9FDD967A +:20A10000FBDD77FDDD6EFCDD66FDCD20957BD60C7A173F1FDE803062DD6EF6DD66F73600FD +:20A12000DD7EF5DD77FF3CCDF094E13600E5DD7EF4CDF094DD6EF0DD66F146D1E1E5D57EB4 +:20A14000DD77FC237EDD77FDDD6EF2DD66F37EDD77FA237EDD77FBC533DD5EFCDD56FDDD52 +:20A160006EFADD66FBCD2D952117D07EC6C8773002233421CED036011811DD34FFDD7EFF0D +:20A18000DD77F5DD7EFFD603DA64A0DD34FEDD7EFED604DADE9EDDF9DDE1C9CD1EAC21F3C5 +:20A1A000FF39F9CB672007AF3215D0C32BA23A15D0B720773A12D0B720712115D03601DDD4 +:20A1C00036FF00DD7EFFD6033E00175FDD7EFFC6284F3E00CED047CB4328090AB72805DD9F +:20A1E00034FF18DF7BB728432119D0DD5EFF1600EB29EB193A09D077233A0AD077211FD0DE +:20A2000019EB3A0BD0C6F4DD77FD3A0CD0CEFFDD77FEDD7EFD1213DD7EFE121125D0DD6EAC +:20A22000FF2600193A0DD0773E1C02DD36FF00DD7EFFC628DD77F33E00CED0DD77F4E1E56B +:20A240007EDD77FEB7CA54A4DD6EFF260029DD75F5DD74F63E1FDD86F5DD77F73ED0DD8E15 +:20A26000F6DD77F8DD6EF7DD66F87EDD77F9237EDD77FADD7EF9DD77FBDD7EFADD77FCDD40 +:20A280007EF9C6F4DD77FDDD7EFACEFFDD77FEDD6EF7DD66F8DD7EFD7723DD7EFE77E1E5C3 +:20A2A0007E3DE1E577DD7EFBDD77FDDD7EFCDD77FE0605DDCBFE2EDDCBFD1E10F6DD6EF76C +:20A2C000DD66F85E23560605CB2ACB1B10FADD6EFDDD66FEBFED52285B3E25DD86FF4F3E20 +:20A2E000D0CE00470ADD77FCFD2119D0E1D1D5E5FD19FD5E00FD5601AFDD96FD6F9FDD9647 +:20A30000FEC5FDE52602E533DD66FCE53367CD2597FDE1C1B7281DE13600E50A47DD6EFDE1 +:20A32000DD66FE2929292929EBFD6E00FD6601C533CD2D953E25DD86FFDD77F93ED0CE00E0 +:20A34000DD77FADD6EF9DD66FA7ED610D2E6A33E19DD86F5DD77FB3ED0DD8EF6DD77FC1E94 +:20A36000007BD60CD2E6A3E1E57EB7287921A5D01600197EB7286B6B2600294D442169D057 +:20A38000097EDD77FD237EDD77FEDD6EFBDD66FC562366DD7EFD926FDD7EFE9C67C5D5CD72 +:20A3A0002095EBD1C17DD6107C173F1FDE8030322181D0094E2346DD6EF7DD66F8562366C9 +:20A3C00079926F789C67D5CD20954B42D179D61078173F1FDE80300AE13600E5D57BCD7660 +:20A3E00099D11CC361A3E1E57EDD77FEDD7EFF3CDD77FDDD7EFEB72008DD7EFDCDF094189A +:20A4000053DD6EF9DD66FA7EDD77FEDD6EF7DD66F87EDD77FB237EDD77FCDD7EF5C619DD51 +:20A4200077F93E00CED0DD77FADD6EF9DD66FA7EDD77F9237EDD77FA210901E5DD7EFEF5E9 +:20A4400033DD6EFBDD66FCE5DD5EF9DD56FADD7EFDCD4E94DD34FFDD7EFFD603DA2FA2DD2C +:20A46000F9DDE1C9CD1EACF5F5DD36FF00DD7EFFC666DD77FD3E00CED0DD77FEDD6EFDDD9F +:20A4800066FE5E7BB728641DDD6EFDDD66FE73DD7EFFC60CDD77FC7BB72008DD7EFCCDF039 +:20A4A000941848DD6EFDDD66FECB563E0720023E08DD77FD3E63DD86FF4F3ED0CE00470A81 +:20A4C000DD77FEDD5EFF1600EB29EB215DD0194E23462157D0195E23562601DD6EFDE5DD54 +:20A4E0007EFEF533C5DD7EFCCD4E94DD34FFDD7EFFD603DA6DA4DDF9DDE1C9F54F3A0BD009 +:20A50000FD210000FD39FD77003A0CD0FD77013A12D0B7C299A6CB51280C2109D07EC6FD89 +:20A5200077237ECEFF77CB59280B2109D07EC60377300223343A09D0D6103A0AD0173F1FA5 +:20A54000DE8030062110002209D03E30FD2109D0FDBE003E01FD9E01E25DA5EE80F266A5F1 +:20A560002130012209D0CB41280B3A0DD0D6303004210DD034CB49280E3E02FD210DD0FD4A +:20A5800096003003FD3500210BD03A07D0C68077233A08D0CE0077D1D50605CB2ACB1B10E0 +:20A5A000FA2A0BD00605CB2CCB1D10FABFED52282E3A11D0B72028934F9F92473E0AF53370 +:20A5C0003A0DD0F533ED5B09D06960CD2597B7280E219BA6CD94953E01CDDD97C399A6FD05 +:20A5E0002114D0FD34003A14D0D618382FFD3600003A0FD0B72804210FD0353A0FD0B7205E +:20A600000D21ACA6CD9495AFCDDD97C399A63A0FD021CFD096280521CED036013A0DD02EFB +:20A6200006CDEAAB3AD0D093280521CED036013A11D0CB572803AF18023E01F533AFF533B3 +:20A640003A0DD0F5332A0BD0E5ED5B09D0AFCD4E942A0BD00605CB2CCB1D10FAAF956F9F07 +:20A660009467CDE48E0F302C3A0AD0CB7F20253A09D0D6403A0AD0173F1FDE813016210119 +:20A6800001E5AFF5332A0BD0E5ED5B09D03E1BCD4E9418053E1BCDF094F1C9594F55204804 +:20A6A0004954205448452057414C4C004F5554204F46204655454C003E00D334DB340FD27E +:20A6C000F69418F8DB88E6B02808CDBF8ECDCD8E18F2CDBF8ECDCD8ECD918EDB88CB7F2868 +:20A6E00002AFC9E63028EB3E01C9CDB8A62185A7CDB98D210C00CD3B93CD299321A00022F0 +:20A7000009D0210BD03A07D0C68077233A08D0CE0077210DD036183E01F533211800E52A21 +:20A720000BD0E511A000AFCD4E94218EA7CD5B8E1193A73E05CD6F8E21ABA7CD5B8E11B0FC +:20A74000A73E08CD6F8E11E4A73E0ACD6F8E1125A83E0CCD6F8E1163A83E0DCD6F8E1193CD +:20A76000A83E0ECD6F8E11CFA83E10CD6F8E21E8A8CD5B8E11EDA83E1CCD6F8E2102A9CDB1 +:20A780005B8EC3C4A61B5B306D1B5B324A00313B393300422041205320542049204F204EF6 +:20A7A0002020205220412049204400303B393700466C79206F7665722074686520666F720F +:20A7C000747265737320616E642064657374726F79206974732072656163746F7220636F59 +:20A7E00072652E005550202F20444F574E20636C696D6220616E6420646976652020202046 +:20A8000020204C454654202F20524947485420737465657220202020202053504143452051 +:20A820006669726500436C696D62206F766572206C6F772077616C6C732E20466C79206CF5 +:20A840006F77207468726F7567682074686520646F6F727320696E2074616C6C206F6E6523 +:20A86000732E00596F757220736861646F7720616E642074686520616C74696D6574657247 +:20A880002073686F7720796F7572206865696768742E0053686F6F74206675656C20746153 +:20A8A0006E6B7320666F72206675656C2E205475727265747320616E6420666967687465E8 +:20A8C00072732073686F6F74206261636B2E0050207061757365732E20204553432067692D +:20A8E0007665732075702E00313B393600505245535320535041434520544F2042454749E9 +:20A900004E0030000E002128D006000936000C79D60338F20E00213FD006000936000C79BD +:20A92000D60438F20E002153D006000936000C79D60438F20E002166D006000936000C79C4 +:20A94000D60338F20E0021A5D006000936000C79D60C38F221000022C9D021CBD0361ECDC1 +:20A96000B8A621FAFFCD3B93CD299321A0002209D0210DD03618210BD03A07D0C680772346 +:20A980003A08D0CE00772111D0365AAF3212D0C9210ED036032100002217D02110D03601A8 +:20A9A0002106ABCDB98D210FD03664AF3214D02115D03601CD04A921000022D3D0AF32D500 +:20A9C000D0210BABCD9495CDC895CDBF8ECD2993CDCD8EB728322E323E02CD318E2117ABC5 +:20A9E000CD5B8E211CABCDB98D2133ABCD5B8ECDBF8EDB8807300621CDD03601C9CDCD8E51 +:20AA0000B728EC21CED03601DB884FCB79C0C579CDFBA4CD2C98CD6F9AC179CD9BA1CD8618 +:20AA20009CCD019ECD64A43A11D0B728042111D0353AD5D0B7280F21D5D0353AD5D0B72086 +:20AA40000521CED036013ACED0B72803CDC8953A12D0B728672116D0353A16D0B7205D2104 +:20AA60000ED0353A0ED0B72034CDC8952135ABCD5B8E113DAB3E0DCD6F8E115CAB3E0ECD80 +:20AA80006F8E113DAB3E0FCD6F8E2133ABCD5B8E0678C5CDBF8ECDCD8EC110F6C9AF3212EC +:20AAA000D02111D036783A0FD0D6283005210FD03628210DD0361821CED036012A0BD0061F +:20AAC00005CB2CCB1D10FAAF954F9F944779D65E78173F1FDE813006CDF993C3CAA9211785 +:20AAE000D07EC6F477237ECE01772110D034217BABCD9495CDC895065AC5CDBF8ECDCD8EED +:20AB0000C110F6C3A6A91B5B324A004C4556454C20535441525400303B3933005041555334 +:20AB20004544202D205020544F20474F204F4E1B5B4B003000303B34343B393700202020CA +:20AB4000202020202020202020202020202020202020202020202020202020002020202015 +:20AB600020202020202047414D45204F5645522020202020202020202020004C4556454CC7 +:20AB800020434C454152454400DB780E00EEEF32D1D079EEBE32D2D0DB7832CCD021CCABE2 +:20ABA000CDB98DCD0595AF32CDD03ACDD0B7200BCDEAA6B72805CD90A918EFCDB8A621D872 +:20ABC000ABCDB98DDB000FD0DB0118F81B5B3F336C1B5B3F32356C001B5B306D1B5B324A30 +:20ABE0001B5B481B5B3F323568005D6F2600547BE680B220110610ED6A17933001833FED12 +:20AC00006A10F65FEBC906097D6C2600CB1DED6AED523001193F1710F5CB10505FC9E1DD5F +:20AC2000E5DD210000DD39E95D6FCD44ACC372ACCD4CACC372AC5D6FCDECABEBC9CDEFABD7 +:20AC4000EBC95D6F7D079F677B079F577CAA177CF517300697956F9F9467CB7A28069793A5 +:1CAC60005F9F9257CDEFABF1D04797935F9F925778C917EBD097935F9F9257C984 +:00000001FF diff --git a/games/raid/test-raid.py b/games/raid/test-raid.py new file mode 100644 index 0000000..764974f --- /dev/null +++ b/games/raid/test-raid.py @@ -0,0 +1,133 @@ +#!/usr/bin/env python3 +"""Run Bastion Raid in tools/z80emu.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 # 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, "out", "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 = {} + for line in open(os.path.join(HERE, "out", "raid.map")): + f = line.split() + if len(f) >= 2 and f[1].startswith("_"): + try: sym[f[1]] = int(f[0], 16) + except ValueError: pass + 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() diff --git a/rtl/basys3_terminal.v b/rtl/basys3_terminal.v index 5164bf9..fd18a73 100644 --- a/rtl/basys3_terminal.v +++ b/rtl/basys3_terminal.v @@ -326,6 +326,9 @@ module basys3_terminal #( 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; @@ -343,6 +346,7 @@ module basys3_terminal #( .tx_req(ztx_req), .tx_data(ztx_data), .tx_ack(ztx_ack), .seg_val(z80_seg), .halted(), .brk_key(brk_s[2] ^ brk_s[1]), .vclk(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]) @@ -460,6 +464,7 @@ module basys3_terminal #( .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), diff --git a/rtl/text_display.v b/rtl/text_display.v index 90151db..153f975 100644 --- a/rtl/text_display.v +++ b/rtl/text_display.v @@ -8,7 +8,10 @@ // 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. +// 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 @@ -32,6 +35,9 @@ module text_display #( 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, @@ -72,6 +78,22 @@ module text_display #( 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]}; @@ -88,7 +110,9 @@ module text_display #( reg [7:0] a_col; reg [3:0] a_line; reg [2:0] a_bit; - reg a_blank, a_text, a_vis, a_cur, a_hs, a_vs, a_gon; + reg 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; @@ -104,7 +128,8 @@ module text_display #( a_hs <= h >= H_VIS + H_FP && h < H_VIS + H_FP + H_SYNC; // positive sync a_vs <= v >= V_VIS + V_FP && v < V_VIS + V_FP + V_SYNC; a_gon <= v < 960; - a_gfx <= {v[9:2], 8'd0} + {2'd0, v[9:2], 6'd0} + {8'd0, h[10:2]}; + 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 @@ -117,10 +142,10 @@ module text_display #( 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; + 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} <= - {a_line, a_bit, a_blank, a_text, a_vis, a_cur, a_hs, a_vs, a_gon}; + {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]; @@ -133,7 +158,7 @@ module text_display #( always @(posedge clk) begin glyph <= font[{ram_data[7:0], b_line}]; c_cell <= ram_data; - c_gfx <= b_gon ? gfx_pix : 4'd0; + c_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}; diff --git a/rtl/z80_system.v b/rtl/z80_system.v index 2eb80da..ae66d9f 100644 --- a/rtl/z80_system.v +++ b/rtl/z80_system.v @@ -12,7 +12,12 @@ // 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 unless bit 7 is set) +// (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 0 = monitor bank in; bit 1 (with bit 0 = 0) = stop again after // one instruction of the program @@ -63,6 +68,9 @@ module z80_system #( input vclk, input [16:0] gfx_vaddr, output reg [3:0] gfx_vpix, + 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, @@ -147,13 +155,20 @@ module z80_system #( 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[7:0]) 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]; end + 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 diff --git a/sim/tb_terminal.v b/sim/tb_terminal.v index ffd7e6b..443401c 100644 --- a/sim/tb_terminal.v +++ b/sim/tb_terminal.v @@ -409,6 +409,37 @@ module tb_terminal; 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.z80.scroll_x == 0 && dut.z80.scroll_y == 0 && !dut.z80.g_border) $display("PASS CLS resets the scroll"); + else begin + $display("FAIL CLS left the scroll at %0d,%0d", dut.z80.scroll_x, dut.z80.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"}); diff --git a/tools/boardemu.py b/tools/boardemu.py new file mode 100644 index 0000000..e450714 --- /dev/null +++ b/tools/boardemu.py @@ -0,0 +1,199 @@ +#!/usr/bin/env python3 +"""A model of the Basys 3 Z-80's game hardware around tools/z80emu.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 8600h, returns to a HALT at 0 + b.run_frames(60, joy=FIRE) + b.shot("screen.png", load_font()) + +Each read of the frame counter (78h) counts as a new frame, so a program that +waits for the counter to change runs one frame per wait. 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 z80emu import Z80 # noqa: E402 + +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(Z80): + def __init__(self, mem, code_end=0x10000, start=0x8600): + super().__init__(mem) + self.code_end = code_end + self.steps = 0 # instructions run + self.scroll_x = self.scroll_y = 0 + self.border = False + self.m[0] = 0x76 # HALT: where the program returns to + self.pc, self.sp = start, 0xFFF0 + self.push(0x0000) + self.frame = 0 + 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: # each read is a new frame + self.frame += 1 + 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 + 2 * n # wait_frame reads the counter twice + while self.frame < end and self.pc != 0: + self.step() + self.steps += 1 + if not (0x8600 <= self.pc < self.code_end or self.pc == 0): + raise SystemExit("PC left the program: %04X" % self.pc) + if self.sp < 0xF000: + raise SystemExit("stack ran into memory: SP %04X" % 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.""" + mem = bytearray(65536) + hi = 0 + for line in open(path): + n, a, t = int(line[1:3], 16), int(line[3:7], 16), int(line[7:9], 16) + if t == 0: + mem[a:a + n] = bytes.fromhex(line[9:9 + 2 * n]) + hi = max(hi, a + n) + return mem, hi + + + +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()] diff --git a/tools/send-hex.py b/tools/send-hex.py index 6789c8f..807754d 100644 --- a/tools/send-hex.py +++ b/tools/send-hex.py @@ -27,6 +27,7 @@ ap.add_argument("file", help="Intel HEX file") ap.add_argument("--save", metavar="NAME", help="save the loaded bytes in flash under this name") ap.add_argument("--port", default="/dev/ttyUSB1") ap.add_argument("--baud", type=int, default=115200, choices=sorted(BAUDS)) +ap.add_argument("--no-date", action="store_true", help="don't set BASIC's clock (which dates the file)") a = ap.parse_args() records = [l.strip() for l in open(a.file) if l.startswith(":")] @@ -77,6 +78,9 @@ def wait_for(pattern, timeout=10.0): PROMPT = rb"\x1b\[30;1H> .*?\x1b\[\?25h" send("\r") time.sleep(0.3) +if not a.no_date: # the file gets the PC's date + send(time.strftime('DATE "%Y-%m-%d %H:%M:%S"\r')) + wait_for(rb"READY\r\n") send("MONITOR\r") wait_for(rb"Z-80 MONITOR") wait_for(PROMPT)