commit 1307ff3d19810b99f29180a70310f5afc76e65c6 Author: Rory Rogers Date: Mon Oct 26 00:37:22 2026 +0000 Create GBA adventure with walkable map and NPC dialogue diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f4564af --- /dev/null +++ b/.gitignore @@ -0,0 +1,7 @@ +build/ +*.elf +*.gba +*.sav +*.map +.DS_Store +__pycache__/ diff --git a/Build and Play.command b/Build and Play.command new file mode 100755 index 0000000..fadbe75 --- /dev/null +++ b/Build and Play.command @@ -0,0 +1,9 @@ +#!/bin/zsh +cd -- "${0:A:h}" || exit 1 +if /usr/bin/make check; then + /usr/bin/open -a mGBA "$PWD/game.gba" +else + printf '\nBuild failed. Copy the error above so we can fix it.\n' + read -r '?Press Return to close.' + exit 1 +fi diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ffb5f02 --- /dev/null +++ b/Makefile @@ -0,0 +1,33 @@ +TOOL_PREFIX ?= /opt/homebrew/bin/arm-none-eabi- +PYTHON ?= /usr/bin/python3 +CC := $(TOOL_PREFIX)gcc +OBJCOPY := $(TOOL_PREFIX)objcopy +CFLAGS := -mcpu=arm7tdmi -marm -O2 -g -Wall -Wextra -Werror -ffreestanding -fno-builtin -fno-common -Wa,--noexecstack + +.PHONY: all play check +all: game.gba + +build: + mkdir -p build + +build/start.o: source/start.s | build + $(CC) -mcpu=arm7tdmi -marm -c $< -o $@ + +build/main.o: source/main.c | build + $(CC) $(CFLAGS) -c $< -o $@ + +build/game.elf: build/start.o build/main.o gba.ld + $(CC) -mcpu=arm7tdmi -marm -nostdlib -T gba.ld -Wl,-Map,build/game.map build/start.o build/main.o -lgcc -o $@ + +game.gba: build/game.elf tools/rom_header.py + $(OBJCOPY) -O binary $< $@ + $(PYTHON) tools/rom_header.py $@ + +check: game.gba + $(PYTHON) tools/rom_header.py --check game.gba + $(PYTHON) -m unittest discover -s tests -v + /usr/bin/clang -Wall -Wextra -Werror tests/test_game.c -o build/test_game + ./build/test_game + +play: game.gba + open -a mGBA game.gba diff --git a/README.md b/README.md new file mode 100644 index 0000000..c5c18b7 --- /dev/null +++ b/README.md @@ -0,0 +1,74 @@ +# My GBA Game + +An original Game Boy Advance adventure, built one small step at a time. + +## Play your game + +Double-click **Build and Play.command** in this folder. It builds your ROM, runs the checks, and opens mGBA. + +Or open Terminal and run: + +```sh +cd ~/gba-game +make play +``` + +The game file is **game.gba**. You can also open it directly in mGBA without rebuilding. + +### Controls (mGBA defaults) + +- **Arrow keys:** walk. +- **X (GBA A button):** talk to Mila, the person in green below the house. Walk close first, then press X again to turn the page and once more to close the conversation. Walking pauses while talking. You can also greet the golden creature when nearby. +- **Return (GBA Start button):** return to your starting position. + +If those keys do not match your emulator settings, check mGBA's keyboard input settings. + +### Your first change + +Open `source/main.c` in a text editor. Find: + +```c +static const u16 shirt = RGB(5, 14, 29); +``` + +Those numbers mean red, green, and blue, each from 0 to 31. Try `RGB(29, 6, 6)` for a red shirt. Save, then double-click **Build and Play.command** again. + +## Project files + +- `source/main.c`: map, character, creature, and game rules. +- `source/start.s`: code that starts the GBA hardware. +- `gba.ld`: tells the compiler where cartridge code and working memory go. +- `tools/rom_header.py`: adds the cartridge header the GBA expects. +- `Makefile`: builds the game with the installed ARM compiler. +- `tests/`: cartridge header and NPC interaction rule tests. + +### Change what Mila says + +Find `dialogue` near the top of `source/main.c`. Each pair of strings is one page of her conversation. Use CAPITAL letters and spaces, with no more than 52 characters on each line. Save and rebuild to try your new conversation. + +## Mac setup + +We are using Homebrew's standalone ARM compiler, not devkitPro. +The project includes its own minimal startup code and linker script; it does not require devkitPro or an additional GBA library. + +1. From the account that owns Homebrew, install the compiler and binary tools: + + ```sh + /opt/homebrew/bin/brew install arm-none-eabi-gcc arm-none-eabi-binutils + ``` + +2. Install [mGBA](https://mgba.io/downloads.html), an emulator for testing our game, either from its website or with: + + ```sh + /opt/homebrew/bin/brew install --cask mgba + ``` + +Do not run Homebrew with sudo. Homebrew installations and updates should be handled by its owning account. Rory can use the installed compiler to build this project without administrator access. + +Never share your Mac password in chat. Enter it only in the Mac's installer or Terminal prompt. + +We are making an original game. No existing Pokémon ROM or separate BIOS download is needed for this first project in mGBA. + +## Project status + +The development tools are installed. The ROM contains a walkable map, a character, an original creature to greet, and one NPC named Mila with a two-page conversation. Battles, catching creatures, sound, and saving are not implemented yet. diff --git a/gba.ld b/gba.ld new file mode 100644 index 0000000..6f9631b --- /dev/null +++ b/gba.ld @@ -0,0 +1,33 @@ +OUTPUT_ARCH(arm) +ENTRY(_start) + +MEMORY { + ROM (rx) : ORIGIN = 0x08000000, LENGTH = 32M + RAM (rwx) : ORIGIN = 0x02000000, LENGTH = 256K +} + +SECTIONS { + .text : { + KEEP(*(.gba_header)) + *(.text*) + *(.rodata*) + . = ALIGN(4); + } > ROM + + .data : { + __data_start = .; + *(.data*) + . = ALIGN(4); + __data_end = .; + } > RAM AT > ROM + __data_load = LOADADDR(.data); + + .bss (NOLOAD) : { + __bss_start = .; + *(.bss*) *(COMMON) + . = ALIGN(4); + __bss_end = .; + } > RAM + + /DISCARD/ : { *(.comment) *(.note*) *(.ARM.exidx*) *(.ARM.extab*) } +} diff --git a/source/main.c b/source/main.c new file mode 100644 index 0000000..c4c779f --- /dev/null +++ b/source/main.c @@ -0,0 +1,253 @@ +/* Our first GBA game. No downloaded game art or existing ROM is used. */ +typedef unsigned short u16; + +#define RGB(r, g, b) ((u16)((r) | ((g) << 5) | ((b) << 10))) +#define DISPLAY (*(volatile u16 *)0x04000000) +#define VCOUNT (*(volatile u16 *)0x04000006) +#define KEYS (*(volatile u16 *)0x04000130) +#define SCREEN ((volatile u16 *)0x06000000) +#define WIDTH 240 +#define HEIGHT 160 + +enum { A = 1, START = 8, RIGHT = 16, LEFT = 32, UP = 64, DOWN = 128 }; + +/* These are good first things to change! Colors use numbers from 0 to 31. */ +static const u16 grass = RGB(12, 23, 11); +static const u16 shirt = RGB(5, 14, 29); + +enum { NPC_X = 146, NPC_Y = 106 }; + +/* Change these lines to write Mila's conversation! Use CAPITAL letters. */ +static const char *const dialogue[][2] = { + {"HELLO I AM MILA", "WELCOME TO OUR VILLAGE"}, + {"THAT GOLDEN CREATURE IS FRIENDLY", "WALK OVER AND SAY HELLO"} +}; +enum { DIALOGUE_PAGES = sizeof(dialogue) / sizeof(dialogue[0]) }; + +static int near_npc(int x, int y) +{ + return x >= NPC_X - 24 && x <= NPC_X + 24 && + y >= NPC_Y - 20 && y <= NPC_Y + 20; +} + +static int next_dialogue_page(int page) +{ + return page + 1 < DIALOGUE_PAGES ? page + 1 : -1; +} + +static void rectangle(int x, int y, int w, int h, u16 color) +{ + for (int row = y; row < y + h; ++row) { + for (int col = x; col < x + w; ++col) { + if (col >= 0 && col < WIDTH && row >= 0 && row < HEIGHT) + SCREEN[row * WIDTH + col] = color; + } + } +} + +/* Tiny original 3-by-5 alphabet: one row of bits per letter. */ +static const unsigned char alphabet[26][5] = { + {2,5,7,5,5}, {6,5,6,5,6}, {3,4,4,4,3}, {6,5,5,5,6}, + {7,4,6,4,7}, {7,4,6,4,4}, {3,4,5,5,3}, {5,5,7,5,5}, + {7,2,2,2,7}, {1,1,1,5,2}, {5,5,6,5,5}, {4,4,4,4,7}, + {5,7,7,5,5}, {5,7,7,7,5}, {2,5,5,5,2}, {6,5,6,4,4}, + {2,5,5,3,1}, {6,5,6,5,5}, {3,4,2,1,6}, {7,2,2,2,2}, + {5,5,5,5,7}, {5,5,5,5,2}, {5,5,7,7,5}, {5,5,2,5,5}, + {5,5,2,2,2}, {7,1,2,4,7} +}; + +static void text(int x, int y, const char *message, u16 color) +{ + for (; *message; ++message, x += 4) { + if (*message < 'A' || *message > 'Z') continue; + const unsigned char *letter = alphabet[*message - 'A']; + for (int row = 0; row < 5; ++row) + for (int col = 0; col < 3; ++col) + if (letter[row] & (4 >> col)) + rectangle(x + col, y + row, 1, 1, color); + } +} + +/* Calculate the scenery at one pixel so we can erase the old player. */ +static u16 scenery(int x, int y) +{ + if (y < 18 || y >= 142) return RGB(3, 7, 10); + if (x < 8 || x >= 232 || y < 26 || y >= 136) { + if ((x + y) % 9 < 2) return RGB(5, 13, 8); + return RGB(6, 16, 8); + } + /* A sandy walking path. */ + u16 color = grass; + if ((x > 96 && x < 120) || (y > 106 && y < 120)) + color = RGB(27, 25, 16); + else if ((x * 7 + y * 11) % 43 == 0) + color = RGB(9, 19, 8); + /* Pond with a light shore. */ + if (x >= 22 && x < 76 && y >= 44 && y < 86) { + color = RGB(24, 25, 15); + if (x >= 25 && x < 73 && y >= 47 && y < 83) + color = ((x + y * 2) % 23 < 5) ? RGB(13, 25, 29) : RGB(5, 17, 25); + } + /* Our little starting house. */ + if (x >= 147 && x < 206 && y >= 32 && y < 49) + color = ((x + y) % 8 == 0) ? RGB(21, 7, 8) : RGB(26, 10, 10); + if (x >= 152 && x < 201 && y >= 49 && y < 72) { + color = RGB(29, 27, 20); + if (x >= 172 && x < 183 && y >= 55) color = RGB(12, 8, 6); + if (x >= 157 && x < 166 && y >= 54 && y < 62) color = RGB(8, 20, 28); + if (x >= 187 && x < 196 && y >= 54 && y < 62) color = RGB(8, 20, 28); + } + /* Flower patches. */ + if (x > 126 && x < 146 && y > 77 && y < 98 && x % 7 < 2 && y % 7 < 2) + color = RGB(31, 21, 7); + return color; +} + +static void restore(int x, int y, int w, int h) +{ + for (int row = y; row < y + h; ++row) + for (int col = x; col < x + w; ++col) + if (col >= 0 && col < WIDTH && row >= 0 && row < HEIGHT) + SCREEN[row * WIDTH + col] = scenery(col, row); +} + +static void creature(void) +{ + u16 gold = RGB(30, 23, 7), dark = RGB(7, 9, 10); + rectangle(175, 102, 14, 3, RGB(8, 16, 7)); + rectangle(175, 88, 3, 6, gold); + rectangle(185, 88, 3, 6, gold); + rectangle(175, 93, 13, 9, gold); + rectangle(178, 101, 3, 3, gold); + rectangle(184, 101, 3, 3, gold); + rectangle(178, 95, 2, 2, dark); + rectangle(184, 95, 2, 2, dark); + rectangle(181, 99, 2, 1, RGB(25, 10, 8)); +} + +/* x and y mark the player's feet. */ +static void person(int x, int y, int step, u16 clothes, u16 hat) +{ + u16 skin = RGB(30, 23, 17), dark = RGB(5, 7, 10); + rectangle(x - 7, y - 1, 15, 3, RGB(8, 16, 7)); + rectangle(x - 5, y - 19, 10, 6, hat); + rectangle(x - 6, y - 15, 13, 2, hat); + rectangle(x - 4, y - 13, 8, 5, skin); + rectangle(x - 2, y - 12, 1, 1, dark); + rectangle(x + 2, y - 12, 1, 1, dark); + rectangle(x - 5, y - 8, 10, 7, clothes); + rectangle(x - 7, y - 7, 2, 5, skin); + rectangle(x + 5, y - 7, 2, 5, skin); + rectangle(x - 4, y - 1, 3, 3 - step, dark); + rectangle(x + 1, y - 1, 3, 2 + step, dark); +} + +static void player(int x, int y, int step) +{ + person(x, y, step, shirt, RGB(28, 8, 9)); +} + +static void actors(int x, int y, int step) +{ + /* Draw people behind others first, using the position of their feet. */ + if (y < 104) player(x, y, step); + creature(); + if (y >= 104 && y < NPC_Y) player(x, y, step); + person(NPC_X, NPC_Y, 0, RGB(4, 21, 13), RGB(12, 7, 4)); + if (y >= NPC_Y) player(x, y, step); +} + +static int can_walk(int x, int y) +{ + if (x < 17 || x > 222 || y < 47 || y > 131) return 0; + if (x > 16 && x < 82 && y > 42 && y < 92) return 0; + if (x > 141 && x < 212 && y < 78) return 0; + if (x > NPC_X - 13 && x < NPC_X + 13 && + y > NPC_Y - 9 && y < NPC_Y + 9) return 0; + return 1; +} + +static void status(const char *message) +{ + rectangle(0, 0, WIDTH, 18, RGB(3, 7, 10)); + text(8, 6, message, RGB(31, 30, 25)); +} + +static void dialogue_box(int page) +{ + u16 white = RGB(31, 30, 25); + rectangle(8, 108, 224, 32, white); + rectangle(10, 110, 220, 28, RGB(3, 7, 10)); + text(16, 113, "MILA", RGB(14, 29, 19)); + text(192, 113, page == DIALOGUE_PAGES - 1 ? "A CLOSE" : "A NEXT", white); + text(16, 122, dialogue[page][0], white); + text(16, 130, dialogue[page][1], white); +} + +static void wait_for_frame(void) +{ + while (VCOUNT >= 160) { } + while (VCOUNT < 160) { } +} + +int main(void) +{ + /* Mode 3 gives us a 240x160 canvas of 15-bit colors. */ + DISPLAY = 0x0080; /* Hide the screen until the scene is drawn. */ + restore(0, 0, WIDTH, HEIGHT); + status("MY FIRST QUEST"); + text(8, 148, "ARROWS MOVE A TALK START RESET", RGB(31, 30, 25)); + int x = 108, y = 103, frame = 0, greeting = 0; + int dialogue_page = -1; /* -1 means we are not talking. */ + unsigned previous = 0; + actors(x, y, 0); + DISPLAY = 0x0403; + + for (;;) { + wait_for_frame(); + unsigned keys = (~KEYS) & 0x03ff; + unsigned pressed = keys & ~previous; + previous = keys; + /* Talking pauses walking. One fresh button press turns one page. */ + if (dialogue_page >= 0) { + if (pressed & A) { + dialogue_page = next_dialogue_page(dialogue_page); + if (dialogue_page >= 0) { + dialogue_box(dialogue_page); + } else { + restore(8, 108, 224, 32); + actors(x, y, 0); + status("MY FIRST QUEST"); + } + } + continue; + } + int old_x = x, old_y = y; + int next_x = x + !!(keys & RIGHT) - !!(keys & LEFT); + int next_y = y + !!(keys & DOWN) - !!(keys & UP); + if (can_walk(next_x, y)) x = next_x; + if (can_walk(x, next_y)) y = next_y; + if (pressed & START) { x = 108; y = 103; } + if (pressed & A) { + if (near_npc(x, y)) { + dialogue_page = 0; + greeting = 0; + status("TALKING TO MILA"); + } else if (x > 154 && x < 209 && y > 79 && y < 125) { + status("HELLO NEW FRIEND"); + greeting = 180; + } else { + status("WALK CLOSE TO MILA TO TALK"); + greeting = 180; + } + } + if (greeting && --greeting == 0) status("MY FIRST QUEST"); + if (x != old_x || y != old_y) { + restore(old_x - 8, old_y - 20, 17, 24); + actors(x, y, (frame >> 3) & 1); + } + if (dialogue_page >= 0) dialogue_box(dialogue_page); + ++frame; + if (frame == 1024) frame = 0; + } +} diff --git a/source/start.s b/source/start.s new file mode 100644 index 0000000..05e0073 --- /dev/null +++ b/source/start.s @@ -0,0 +1,45 @@ +.syntax unified +.cpu arm7tdmi +.arm + +@ A GBA cartridge starts with a branch and a 192-byte hardware header. +@ tools/rom_header.py fills in the required boot logo and checksum. +.section .gba_header, "ax", %progbits +.global _start +_start: + b reset + .space 188, 0 + +.section .text.start, "ax", %progbits +reset: + @ Give IRQ and system mode separate stacks in internal RAM. + mov r0, #0xd2 + msr cpsr_c, r0 + ldr sp, =0x03007fa0 + mov r0, #0xdf + msr cpsr_c, r0 + ldr sp, =0x03007f00 + + @ Copy initialized variables from the cartridge into working RAM. + ldr r0, =__data_load + ldr r1, =__data_start + ldr r2, =__data_end +1: + cmp r1, r2 + ldrlo r3, [r0], #4 + strlo r3, [r1], #4 + blo 1b + + @ Clear uninitialized variables, as C expects. + ldr r1, =__bss_start + ldr r2, =__bss_end + mov r3, #0 +2: + cmp r1, r2 + strlo r3, [r1], #4 + blo 2b + bl main +3: + b 3b + +.section .note.GNU-stack,"",%progbits diff --git a/tests/test_game.c b/tests/test_game.c new file mode 100644 index 0000000..40f3c6d --- /dev/null +++ b/tests/test_game.c @@ -0,0 +1,49 @@ +/* Test game rules on the Mac without touching GBA screen registers. */ +#include +#include + +#define main gba_main +#include "../source/main.c" +#undef main + +int main(void) +{ + assert(can_walk(108, 103)); + assert(!can_walk(30, 60)); /* Pond. */ + assert(!can_walk(175, 60)); /* House. */ + assert(!can_walk(0, 100)); /* Map edge. */ + + assert(!can_walk(NPC_X, NPC_Y)); + assert(!can_walk(NPC_X - 12, NPC_Y)); + assert(!can_walk(NPC_X, NPC_Y + 8)); + assert(can_walk(NPC_X - 13, NPC_Y)); + assert(can_walk(NPC_X + 13, NPC_Y)); + assert(can_walk(NPC_X, NPC_Y + 9)); + assert(can_walk(NPC_X, NPC_Y - 9)); + + assert(!near_npc(108, 103)); + assert(near_npc(NPC_X - 13, NPC_Y)); + assert(near_npc(NPC_X + 13, NPC_Y)); + assert(near_npc(NPC_X, NPC_Y + 9)); + assert(near_npc(NPC_X, NPC_Y - 9)); + assert(near_npc(NPC_X - 24, NPC_Y - 20)); + assert(!near_npc(NPC_X - 25, NPC_Y)); + assert(!near_npc(NPC_X, NPC_Y + 21)); + assert(!near_npc(182, 103)); /* The creature does not trigger Mila. */ + + assert(DIALOGUE_PAGES == 2); + assert(next_dialogue_page(0) == 1); + assert(next_dialogue_page(1) == -1); + for (int page = 0; page < DIALOGUE_PAGES; ++page) { + for (int line = 0; line < 2; ++line) { + int length = 0; + for (const char *p = dialogue[page][line]; *p; ++p) { + assert(*p == ' ' || (*p >= 'A' && *p <= 'Z')); + ++length; + } + assert(length <= 52); /* Text stays inside its box. */ + } + } + puts("NPC collision, talk distance, dialogue pages, and text checks passed"); + return 0; +} diff --git a/tests/test_rom_header.py b/tests/test_rom_header.py new file mode 100644 index 0000000..eef3927 --- /dev/null +++ b/tests/test_rom_header.py @@ -0,0 +1,58 @@ +import importlib.util +from pathlib import Path +import subprocess +import sys +import tempfile +import unittest + +ROOT = Path(__file__).resolve().parents[1] +spec = importlib.util.spec_from_file_location("rom_header", ROOT / "tools/rom_header.py") +header = importlib.util.module_from_spec(spec) +spec.loader.exec_module(header) + + +class HeaderTests(unittest.TestCase): + def setUp(self): + self.rom = bytearray((ROOT / "game.gba").read_bytes()) + + def test_built_rom_is_valid(self): + self.assertEqual(len(header.LOGO), 156) + header.verify(self.rom) + + def test_bad_logo_is_rejected(self): + self.rom[4] ^= 1 + with self.assertRaisesRegex(ValueError, "hardware header"): + header.verify(self.rom) + + def test_bad_checksum_is_rejected(self): + self.rom[160] ^= 1 + with self.assertRaisesRegex(ValueError, "checksum"): + header.verify(self.rom) + + def test_bad_entry_is_rejected(self): + self.rom[:4] = bytes.fromhex("ffffffea") + with self.assertRaisesRegex(ValueError, "Entry branch"): + header.verify(self.rom) + + def test_small_rom_is_rejected(self): + with self.assertRaisesRegex(ValueError, "size"): + header.verify(b"short") + + def test_header_writer_and_padding(self): + with tempfile.TemporaryDirectory() as directory: + path = Path(directory) / "test.gba" + raw = bytearray(256) + raw[:4] = bytes.fromhex("2e0000ea") # Branch to offset 192. + path.write_bytes(raw) + subprocess.run( + [sys.executable, str(ROOT / "tools/rom_header.py"), str(path)], + check=True, capture_output=True, text=True, + ) + result = path.read_bytes() + self.assertEqual(len(result), 32768) + self.assertEqual(result[160:172], b"FIRSTQUEST\0\0") + header.verify(result) + + +if __name__ == "__main__": + unittest.main() diff --git a/tools/rom_header.py b/tools/rom_header.py new file mode 100644 index 0000000..d8c28c1 --- /dev/null +++ b/tools/rom_header.py @@ -0,0 +1,62 @@ +"""Write or check the hardware-required GBA cartridge header (stdlib only).""" + +import argparse +from pathlib import Path + +# Standard GBA boot-logo bytes, required by the console's cartridge check. +# These are hardware header data, not game graphics. +LOGO = bytes.fromhex( + "24ffae51699aa2213d84820a84e409ad11248b98c0817f21a352be199309ce20" + "10464a4af82731ec58c7e83382e3cebf85f4df94ce4b09c194568ac01372a7fc" + "9f844d73a3ca9a615897a327fc039876231dc7610304ae56bf38840040a70efd" + "ff52fe036f9530f197fbc08560d68025a963be03014e38e2f9a234ffbb3e0344" + "780090cb88113a9465c07c6387f03cafd625e48b380aac7221d4f807" +) + + +def verify(rom): + if len(rom) < 192 or len(rom) > 32 * 1024 * 1024: + raise ValueError("ROM size is outside the GBA cartridge range") + if rom[3] != 0xEA: + raise ValueError("Missing ARM entry branch") + displacement = int.from_bytes(rom[:3], "little") + if displacement & 0x800000: + displacement -= 0x1000000 + entry = 8 + displacement * 4 + if not 192 <= entry < len(rom): + raise ValueError("Entry branch does not point to game code") + if rom[4:160] != LOGO or rom[178] != 0x96: + raise ValueError("Invalid hardware header") + if (sum(rom[160:190]) + 0x19) & 0xFF: + raise ValueError("Invalid header checksum") + if len(rom) & (len(rom) - 1): + raise ValueError("ROM is not padded to a power of two") + + +def main(): + parser = argparse.ArgumentParser(description=__doc__) + parser.add_argument("--check", action="store_true") + parser.add_argument("rom", type=Path) + args = parser.parse_args() + rom = bytearray(args.rom.read_bytes()) + if not args.check: + if len(rom) < 192: + raise ValueError("Missing cartridge header space") + rom[4:160] = LOGO + rom[160:172] = b"FIRSTQUEST".ljust(12, b"\0") + rom[172:176] = b"FQ01" + rom[176:178] = b"00" + rom[178:189] = bytes([0x96]) + bytes(10) + rom[189] = (-0x19 - sum(rom[160:189])) & 0xFF + rom[190:192] = b"\0\0" + size = max(32768, 1 << (len(rom) - 1).bit_length()) + rom.extend(b"\xff" * (size - len(rom))) + verify(rom) + args.rom.write_bytes(rom) + else: + verify(rom) + print(f"ROM header OK: {args.rom} ({len(rom):,} bytes)") + + +if __name__ == "__main__": + main()