#!/usr/bin/env python3 # SPDX-License-Identifier: MIT """Generate Big Top Bounce's graphics and tables as ca65 include files. All art here is original to this project (drawn as text below). Tables that are pure arithmetic (positioning, ball velocities, brick playfield patterns) are computed here so their derivation stays readable. Output: src/gen/{font,low,top,title,logic}.inc. Run by scripts/build.sh. """ import math import os import sys OUT = os.path.join(os.path.dirname(os.path.abspath(__file__)), "..", "src", "gen") # -------------------------------------------------------------------------- # Font: 8x8 cells, rows top first; '#' lit. Stored bottom row first because the # 48-pixel text loop counts its row index down from 7. FONT = { " ": ["........"] * 8, "0": [".#####..", "##...##.", "##..###.", "##.#.##.", "###..##.", "##...##.", ".#####..", "........"], "1": ["...##...", "..###...", ".####...", "...##...", "...##...", "...##...", ".######.", "........"], "2": [".#####..", "##...##.", ".....##.", "...###..", ".###....", "##......", "#######.", "........"], "3": [".#####..", "##...##.", ".....##.", "..####..", ".....##.", "##...##.", ".#####..", "........"], "4": ["....##..", "...###..", "..####..", ".##.##..", "#######.", "....##..", "....##..", "........"], "5": ["#######.", "##......", "######..", ".....##.", ".....##.", "##...##.", ".#####..", "........"], "6": ["..####..", ".##.....", "##......", "######..", "##...##.", "##...##.", ".#####..", "........"], "7": ["#######.", ".....##.", "....##..", "...##...", "..##....", "..##....", "..##....", "........"], "8": [".#####..", "##...##.", "##...##.", ".#####..", "##...##.", "##...##.", ".#####..", "........"], "9": [".#####..", "##...##.", "##...##.", ".######.", ".....##.", "....##..", ".####...", "........"], "A": ["..###...", ".##.##..", "##...##.", "##...##.", "#######.", "##...##.", "##...##.", "........"], "B": ["######..", "##...##.", "##...##.", "######..", "##...##.", "##...##.", "######..", "........"], "C": [".#####..", "##...##.", "##......", "##......", "##......", "##...##.", ".#####..", "........"], "E": ["#######.", "##......", "##......", "#####...", "##......", "##......", "#######.", "........"], "G": [".#####..", "##...##.", "##......", "##.####.", "##...##.", "##...##.", ".######.", "........"], "I": [".######.", "...##...", "...##...", "...##...", "...##...", "...##...", ".######.", "........"], "K": ["##...##.", "##..##..", "##.##...", "####....", "##.##...", "##..##..", "##...##.", "........"], "L": ["##......", "##......", "##......", "##......", "##......", "##......", "#######.", "........"], "M": ["##...##.", "###.###.", "#######.", "##.#.##.", "##...##.", "##...##.", "##...##.", "........"], "N": ["##...##.", "###..##.", "####.##.", "##.####.", "##..###.", "##...##.", "##...##.", "........"], "O": [".#####..", "##...##.", "##...##.", "##...##.", "##...##.", "##...##.", ".#####..", "........"], "P": ["######..", "##...##.", "##...##.", "######..", "##......", "##......", "##......", "........"], "R": ["######..", "##...##.", "##...##.", "######..", "##.##...", "##..##..", "##...##.", "........"], "S": [".#####..", "##...##.", "##......", ".#####..", ".....##.", "##...##.", ".#####..", "........"], "T": ["######..", "..##....", "..##....", "..##....", "..##....", "..##....", "..##....", "........"], "U": ["##...##.", "##...##.", "##...##.", "##...##.", "##...##.", "##...##.", ".#####..", "........"], "V": ["##...##.", "##...##.", "##...##.", "##...##.", ".##.##..", "..###...", "...#....", "........"], "X": ["##...##.", ".##.##..", "..###...", "..###...", "..###...", ".##.##..", "##...##.", "........"], "Y": ["##..##..", "##..##..", ".####...", "..##....", "..##....", "..##....", "..##....", "........"], "!": ["...##...", "...##...", "...##...", "...##...", "...##...", "........", "...##...", "........"], "-": ["........", "........", "........", ".#####..", "........", "........", "........", "........"], } FONT_ORDER = "0123456789 ABCEGIKLMNOPRSTUVXY!-" assert len(FONT_ORDER) * 8 <= 256 # -------------------------------------------------------------------------- # Sprites (8 wide, rows top first). BALLOON = [ "..####..", ".######.", "##.#####", "#.######", "#.######", "########", "########", "########", ".######.", ".######.", "..####..", "...##...", "...##...", "....#...", "........", ] # A balloon row's graphic is drawn for 15 lines (b0-b14): a 12-line body (b0-b11, # about as tall as it is wide on a television), the knot, the string, a blank. BUMPER = [ "..####..", ".##..##.", "##.##.##", "#.####.#", "#.####.#", "##.##.##", ".##..##.", "..####..", "........", "........", ] # The clown, 16 rows; colours per row follow (as colour names). CLOWN_A = [ ("...##...", "RED"), ("..####..", "YELLOW"), ("..####..", "YELLOW"), (".######.", "YELLOW"), ("..#..#..", "SKIN"), ("..####..", "SKIN"), ("..#..#..", "SKIN"), ("#.####.#", "WHITE"), ("########", "WHITE"), (".######.", "BLUE"), ("#.####.#", "BLUE"), (".######.", "BLUE"), ("..####..", "BLUE"), ("..#..#..", "GREEN"), (".##..##.", "GREEN"), ("###..###", "RED"), ] CLOWN_B = CLOWN_A[:13] + [ ("..#..#..", "GREEN"), ("..#...#.", "GREEN"), (".##...##", "RED"), ] CLOWN_HIT = list(reversed(CLOWN_A)) # tumbling: upside down # Bat graphics (quad-width player: one bit = 4 colour clocks), for the 15 bat # lines Y=15..1 (table index = Y). Normal: rows at Y=10..7; kicked: Y=12..9. BAT24 = [0x78, 0xFC, 0xFC, 0x78] BAT16 = [0x60, 0xF0, 0xF0, 0x60] # The tent roof (top floor), 12 lines, symmetric playfield (reflect mode): # PF0 is the wall ($F0); PF1/PF2 draw the canvas. Columns: PF1 bits shown # D7..D0 left to right, PF2 bits D0..D7. # The tent's canopy (top floor), 10 lines, the left half of a symmetric # playfield (reflect mode): 20 cells of 4 clocks, cell 0 at the left edge, cell 19 # next to the centre. Cells 0-3 are PF0 (the wall). ROOF = [ "####################", "####################", "####################", "####.###.###.###.###", "####...#...#...#...#", "####................", "####................", "####................", "####................", "####................", ] # -------------------------------------------------------------------------- # Colours: NTSC values; PAL equivalents chosen by nearest RGB in the board's # palettes (scripts/pal_colours.py writes src/colours.inc). Here only names. def write(name, text): os.makedirs(OUT, exist_ok=True) path = os.path.join(OUT, name) with open(path, "w") as f: f.write("; SPDX-License-Identifier: MIT\n; Generated by scripts/gen_tables.py: do not edit.\n") f.write(text) def bytes_line(values, per=16): out = [] for i in range(0, len(values), per): out.append(" .byte " + ",".join(f"${v & 0xFF:02X}" for v in values[i:i + per])) return "\n".join(out) + "\n" def rowbits(s): v = 0 for ch in s: v = (v << 1) | (1 if ch == "#" else 0) return v def font_inc(): t = [] t.append(" .align 256\nFont:\n") for ch in FONT_ORDER: rows = [rowbits(r) for r in FONT[ch]] t.append(f" ; '{ch}'\n" + bytes_line(list(reversed(rows)))) return "".join(t) def font_consts(): lines = [] for i, ch in enumerate(FONT_ORDER): name = {" ": "SP", "!": "BANG", "-": "DASH"}.get(ch, ch) lines.append(f"CH_{name} = {i * 8}") return "\n".join(lines) + "\n" # Brick rows (bottom floor) and drop targets (middle floor): non-reflected # playfield, one brick per 16 colour clocks from clock 16 to 143, each lit for # 12 clocks with a 4-clock gap. Bit c of the row mask = column c (left first). # PF1 shows D7 first; PF2 and PF0 show D0 (D4 for PF0) first. def brick_tables(): TA, TB, TC, TD, TE = [], [], [], [], [] for m in range(256): b = [(m >> c) & 1 for c in range(8)] TA.append((0xE0 if b[0] else 0) | (0x0E if b[1] else 0)) # PF1 left: clocks 16-47 TB.append((0x07 if b[2] else 0) | (0x70 if b[3] else 0)) # PF2 left: 48-79 TC.append(0x70 if b[4] else 0) # PF0 right: 80-95 TD.append((0xE0 if b[5] else 0) | (0x0E if b[6] else 0)) # PF1 right: 96-127 TE.append(0xF0 | (0x07 if b[7] else 0)) # PF2 right: 128-143 + wall t = "" for name, tab in (("TA", TA), ("TB", TB), ("TC", TC), ("TD", TD), ("TE", TE)): t += f" .align 256\n{name}:\n" + bytes_line(tab) return t # Horizontal positioning. A routine that strobes RESPx after a coarse loop of # c+1 iterations puts the object at P(c); an HMOVE with motion hm then moves it # to P(c) - hm (hm from -8 to 7; DOC: SPG "Horizontal Motion"). P(c) is linear # (15 clocks per iteration) once the strobe falls in the visible line; a strobe # during horizontal blank puts the object at the left edge. The constants are # MEASURED on our board by the first session's harness `calib` command (lost in # a reboot); the bot's pixel probes re-check them, see docs/design.md. P_BALLOON_LIN = 21 # P(3) for the balloon rows' routine (strobe written at cycle 12+5c) P_BALLOON_EDGE = 3 # c = 0, 1 (strobe in horizontal blank); c = 2 is not used P_BAT_LIN = 19 # P(1) for the bat routine (strobe at cycle 21+5c) def pos_table(lin_c0, lin_p, edge_cs, edge_p, cmax, size, need): """For each x (mod 160) the (c, hm) whose final position is x. Values of x outside `need` may be unreachable; they get the last coarse value.""" cands = [] for c in range(cmax + 1): if c in edge_cs: p = edge_p elif c < lin_c0: continue # strobe too near the end of horizontal blank else: p = lin_p + 15 * (c - lin_c0) cands.append((c, p)) C, F = [], [] for i in range(size): x = i % 160 best = None for c, p in cands: for hm in range(-8, 8): if (p - hm) % 160 == x: if best is None or (c in edge_cs) < (best[0] in edge_cs) or ( (c in edge_cs) == (best[0] in edge_cs) and abs(hm) < abs(best[1])): best = (c, hm) if best is None: assert x not in need, (x, cands) best = (cmax, 0) C.append(best[0]) F.append((best[1] & 15) << 4) return C, F def balloon_tables(): # 16..136 for rows at rest; up to 149 for a row drifting in from the right # (logic.s MoveBalloons: no balloon in the mask beyond x 147, plus the copy offset 0) need = set(range(0, 12)) | set(range(16, 150)) C, F = pos_table(3, P_BALLOON_LIN, (0, 1), P_BALLOON_EDGE, 11, 160, need) t = " .align 256\nBalC:\n" + bytes_line(C) t += " .align 256\nBalF:\n" + bytes_line(F) return t def bat_tables(): C, F = pos_table(1, P_BAT_LIN, (0,), 3, 9, 160, set(range(16, 129))) # the bat only uses x 16..128; keep those inside the linear range for x in range(16, 129): assert C[x] >= 1, x t = "BatC:\n" + bytes_line(C) t += "BatF:\n" + bytes_line(F) return t # Ball velocities: visual speed S (lines per frame, a colour clock counted as # two lines wide) at an angle from vertical; vx in colour clocks, vy in lines, # both 8.8 fixed point magnitudes. Angle class a: 0 steep .. 3 shallow. SPEEDS = [1.6, 1.8, 2.0, 2.25, 2.5, 2.75, 3.0, 3.3] ANGLES = [15, 30, 45, 60] def speed_tables(): vxl, vxh, vyl, vyh = [], [], [], [] for s in SPEEDS: for a in ANGLES: r = math.radians(a) vx = round(s * math.sin(r) / 2 * 256) vy = round(s * math.cos(r) * 256) vxl.append(vx & 255); vxh.append(vx >> 8) vyl.append(vy & 255); vyh.append(vy >> 8) t = "" for name, tab in (("VXLO", vxl), ("VXHI", vxh), ("VYLO", vyl), ("VYHI", vyh)): t += f"{name}:\n" + bytes_line(tab) return t # Where on the bat the ball struck -> zone 0-7 (0 = left end). def zone_tables(): t = "" for w in (16, 24): z = [] for off in range(w + 2): # offset = ballX + 1 - batX, 0..w+1 z.append(min(7, max(0, off * 8 // (w + 2)))) t += f"Zone{w}:\n" + bytes_line(z) return t def sprite_bytes(rows): return [rowbits(r) for r in rows] def low_inc(): return brick_tables() def mid_gfx(): t = " .align 256\nGfxMid:\n" t += "BumperGfx:\n" + bytes_line(list(reversed(sprite_bytes(BUMPER)))) for name, fr in (("ClownA", CLOWN_A), ("ClownB", CLOWN_B), ("ClownHit", CLOWN_HIT)): t += f"{name}Gfx:\n" + bytes_line(list(reversed([rowbits(r) for r, _ in fr]))) for name, fr in (("ClownA", CLOWN_A), ("ClownB", CLOWN_B), ("ClownHit", CLOWN_HIT)): t += f"{name}Col:\n" + "".join(f" .byte C_CL_{c}\n" for _, c in reversed(fr)) t += " .assert >* = >GfxMid, error, \"GfxMid crosses a page\"\n" return t def bat_gfx(): t = "BatBlank = * - BatGfx\n" + bytes_line([0] * 16) for w, g in ((16, BAT16), (24, BAT24)): for kicked in (0, 1): tab = [0] * 16 top = 12 if kicked else 10 for i, v in enumerate(g): tab[top - i] = v t += f"Bat{w}{'K' if kicked else 'N'} = * - BatGfx\n" + bytes_line(tab) return t # The roof's colour per line: canvas stripes, the last line the wall colour. ROOF_COLS = ["ROOFA", "ROOFB", "ROOFA", "ROOFB", "ROOFA", "WALL2", "WALL2", "WALL2", "WALL2", "WALL2"] def roof_tables(): assert all(len(r) == 20 for r in ROOF), [len(r) for r in ROOF] pf1, pf2 = [], [] for row in ROOF: # cells 0-3 PF0 (the wall), 4-11 PF1 (D7 first), 12-19 PF2 (D0 first) cells = [ch == "#" for ch in row] v1 = 0 for i in range(8): if cells[4 + i]: v1 |= 0x80 >> i v2 = 0 for i in range(8): if cells[12 + i]: v2 |= 1 << i pf1.append(v1) pf2.append(v2) # stored bottom first (the roof loop counts X down) t = "RoofPF1:\n" + bytes_line(list(reversed(pf1))) t += "RoofPF2:\n" + bytes_line(list(reversed(pf2))) cols = [ROOF_COLS[i] for i in range(len(ROOF))] t += "RoofCol:\n" + "".join(f" .byte C_{c}\n" for c in reversed(cols)) return t def top_inc(): t = "GfxTop:\n" bal = sprite_bytes(BALLOON) t += "BalloonGfx:\n" + bytes_line(list(reversed(bal))) t += f"BAL_ROW12 = ${bal[12]:02X}\nBAL_ROW13 = ${bal[13]:02X}\n" assert bal[14] == 0 and len(bal) == 15 t += roof_tables() # NUSIZ and x offset for a row's three copies (bit0 left, bit1 middle, bit2 right) nus = [0, 0, 0, 2, 0, 4, 2, 6] off = [0, 0, 32, 0, 64, 0, 32, 0] t += "BalNus:\n" + bytes_line(nus) t += "BalOff0:\n" + bytes_line(off) t += "BalOff1:\n" + bytes_line([o + 16 for o in off]) t += " .assert >* = >BatGfx, error, \"GfxTop crosses a page\"\n" t += balloon_tables() t += mid_gfx() return t def ring_tables(): """An elliptical circus ring, 24 lines, symmetric playfield.""" rows = [] for l in range(24): dy = (l - 11.5) / 12.0 cells = [] for i in range(20): dx = (4 * i + 2 - 80) / 66.0 r = dx * dx + dy * dy cells.append(0.78 <= r <= 1.0) rows.append(cells) p0, p1, p2 = [], [], [] for cells in rows: v0 = sum(1 << (4 + i) for i in range(4) if cells[i]) v1 = sum(0x80 >> i for i in range(8) if cells[4 + i]) v2 = sum(1 << i for i in range(8) if cells[12 + i]) p0.append(v0); p1.append(v1); p2.append(v2) t = "TRingPF0:\n" + bytes_line(list(reversed(p0))) t += "TRingPF1:\n" + bytes_line(list(reversed(p1))) t += "TRingPF2:\n" + bytes_line(list(reversed(p2))) return t def title_inc(): t = " .align 256\nGfxTitle:\n" t += "TBalloonGfx:\n" + bytes_line(list(reversed(sprite_bytes(BALLOON)))) t += "TClownGfx:\n" + bytes_line(list(reversed([rowbits(r) for r, _ in CLOWN_A]))) t += "TClownCol:\n" + "".join(f" .byte C_CL_{c}\n" for _, c in reversed(CLOWN_A)) t += roof_tables().replace("RoofPF", "TRoofPF").replace("RoofCol", "TRoofCol") t += ring_tables() t += " .assert >* = >GfxTitle, error, \"GfxTitle crosses a page\"\n" return t def logic_inc(): t = speed_tables() t += zone_tables() t += bat_tables() return t def main(): write("font.inc", font_inc()) write("low.inc", low_inc()) write("top.inc", top_inc()) write("title.inc", title_inc()) write("logic.inc", logic_inc()) write("chars.inc", font_consts()) write("bat.inc", bat_gfx()) if __name__ == "__main__": main()