Skip to content

Free games for graphing calculators: NumWorks, TI-Nspire CX, Casio fx-CG50 and soon TI-84 Plus CE. Balatro, Celeste, Hollow Knight, Minecraft and more.

Donkey Kong: the arcade's program, ported from its disassembly

Mason36317df5c0e2609
diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml
index eab5d823..e544e78f 100644
--- a/.github/workflows/build.yml
+++ b/.github/workflows/build.yml
@@ -72,6 +72,7 @@ jobs:
make -C games/leveldevil host test
make -C games/cookieclicker host test
make -C games/jetpackjoyride host test
+ make -C games/donkeykong host test
make -C games/invaders host test
make -C games/sudoku host test
make -C games/uttt host test
@@ -79,8 +80,6 @@ jobs:
make -C games/flappydunk host test
make -C games/towerblock host test
make -C games/doodlejump host test
- make -C games/donkeykong check test NWLINK=nwlink
- python3 games/donkeykong/tests/test_arm.py --nwlink nwlink
diff --git a/Makefile b/Makefile
index a86b1357..48736cc9 100644
--- a/Makefile
+++ b/Makefile
@@ -24,7 +24,7 @@ RAM_LIMIT = 148928
# custom builds of Epsilon 25.2's source give apps less: 133652 bytes; every game fits there too, in NumPlay and on
# its own (their big buffers are on the stack, 32 KB apart from that RAM, or in the RAM the software has left)
CUSTOM_RAM = 133652
-BIG_APPS = DonkeyKong Celeste HollowKnight Undertale ChampionIsland FinalEarth2 NumPowder NumBlocks NumDrive SubwaySurfers FiveNightsAtFreddys Doom BindingOfIsaac HillClimbRacing PlantsVsZombies SonicTheHedgehog CookieClicker LevelDevil JetpackJoyride
+BIG_APPS = Celeste HollowKnight Undertale ChampionIsland FinalEarth2 NumPowder NumBlocks NumDrive SubwaySurfers FiveNightsAtFreddys Doom BindingOfIsaac HillClimbRacing PlantsVsZombies SonicTheHedgehog CookieClicker LevelDevil JetpackJoyride DonkeyKong
# the calculator's space for installed apps (0x90200000 to 0x903F0000 in its flash)
APP_SPACE = 2031616
# Upsilon, the N0110 and N0115's custom software, gives .nwa apps less RAM: NumPlay-Upsilon.nwa has
@@ -222,12 +222,9 @@ APP_sonic = games/sonic/output/sonic.nwa:SonicTheHedgehog.nwa
APP_cookieclicker = games/cookieclicker/output/cookieclicker.nwa:CookieClicker.nwa
APP_leveldevil = games/leveldevil/output/leveldevil.nwa:LevelDevil.nwa
APP_jetpackjoyride = games/jetpackjoyride/output/jetpackjoyride.nwa:JetpackJoyride.nwa
-
APP_donkeykong = games/donkeykong/output/donkeykong.nwa:DonkeyKong.nwa
apps: | $(B)
- $(MAKE) -C games/donkeykong build NWLINK="$(NWLINK)"
- cp $(word 1,$(subst :, ,$(APP_donkeykong))) $(B)/apps/$(word 2,$(subst :, ,$(APP_donkeykong)))
$(MAKE) -C games/numdash build NWLINK="node node_modules/nwlink/bin/nwlink"
$(MAKE) -C games/crossyroad NWLINK="$(NWLINK)"
$(MAKE) -C games/numdrive NWLINK="$(NWLINK)"
@@ -252,6 +249,7 @@ apps: | $(B)
$(MAKE) -C games/cookieclicker build NWLINK="$(NWLINK)"
$(MAKE) -C games/leveldevil build NWLINK="$(NWLINK)"
$(MAKE) -C games/jetpackjoyride build NWLINK="$(NWLINK)"
+ $(MAKE) -C games/donkeykong build NWLINK="$(NWLINK)"
cd games/tetris/tetris && NWLINK="$(NWLINK)" $(CARGO) build --release --quiet
cp $(word 1,$(subst :, ,$(APP_numdash))) $(B)/apps/$(word 2,$(subst :, ,$(APP_numdash)))
cp $(word 1,$(subst :, ,$(APP_crossyroad))) $(B)/apps/$(word 2,$(subst :, ,$(APP_crossyroad)))
@@ -278,6 +276,7 @@ apps: | $(B)
cp $(word 1,$(subst :, ,$(APP_cookieclicker))) $(B)/apps/$(word 2,$(subst :, ,$(APP_cookieclicker)))
cp $(word 1,$(subst :, ,$(APP_leveldevil))) $(B)/apps/$(word 2,$(subst :, ,$(APP_leveldevil)))
cp $(word 1,$(subst :, ,$(APP_jetpackjoyride))) $(B)/apps/$(word 2,$(subst :, ,$(APP_jetpackjoyride)))
+ cp $(word 1,$(subst :, ,$(APP_donkeykong))) $(B)/apps/$(word 2,$(subst :, ,$(APP_donkeykong)))
arm-none-eabi-strip --strip-unneeded $(B)/apps/Tetris.nwa
STD_APPS = $(foreach g,$(STD_GAMES),$(B)/apps/$(NWA_$(g)))
@@ -343,7 +342,7 @@ clean:
-$(MAKE) -C games/sonic clean
-$(MAKE) -C games/cookieclicker clean
-$(MAKE) -C games/leveldevil clean
- -$(MAKE) -C games/donkeykong clean
-$(MAKE) -C games/jetpackjoyride clean
+ -$(MAKE) -C games/donkeykong clean
-cd games/tetris/tetris && $(CARGO) clean
-$(foreach g,$(STD_GAMES),$(MAKE) -C games/$(g) clean;)
diff --git a/games/donkeykong/.gitignore b/games/donkeykong/.gitignore
index fd6aad7f..79fafc9f 100644
--- a/games/donkeykong/.gitignore
+++ b/games/donkeykong/.gitignore
@@ -1,2 +1,2 @@
-output/
-__pycache__/
+/output/
+/build/
diff --git a/games/donkeykong/Makefile b/games/donkeykong/Makefile
index 7cb1582f..983541af 100644
--- a/games/donkeykong/Makefile
+++ b/games/donkeykong/Makefile
@@ -1,43 +1,63 @@
-# Donkey Kong: native standalone arcade adaptation by Bobydeluxe.
+# Donkey Kong on the NumWorks calculator.
+# make output/donkeykong.nwa
+# make check link it like the calculator does (output/donkeykong.bin)
+# make host build/play: the game on a computer, for tests and pictures
+# make test the rules against the arcade's numbers, a bot through the four boards, random keys, saves
+# (with the sanitizers)
+# make rom LST=FILE src/rom.h again, from tcdev's listing of the arcade's program ROM (tools/rom.py)
+# make art src/art.h again (tools/art.py)
+Q ?= @
CC = arm-none-eabi-gcc
NWLINK ?= npx --yes -- [email protected]
-PYTHON ?= python3
-HOST_CC ?= cc
-ARM_LD ?= arm-none-eabi-ld
-OBJCOPY ?= arm-none-eabi-objcopy
-SRC = src/main.c src/game.c src/save.c src/render.c
-OBJECTS = $(patsubst src/%.c,output/%.o,$(SRC))
-HEADERS = $(wildcard src/*.h) ../common/epsilon_app.h ../common/epsilon_files.h
-PACKER = tools/compact_nwa.py tools/standalone.ld
-CFLAGS = -std=c11 $(shell $(NWLINK) eadk-cflags-device) -Os -Wall -Wextra -Werror
-CFLAGS += -ffunction-sections -fdata-sections -fstack-usage -fno-tree-loop-distribute-patterns
-CFLAGS += -flto -fno-fat-lto-objects -fvisibility=internal
-LINK = -nostartfiles --specs=nano.specs -Wl,--relocatable,--gc-sections,-e,main -flinker-output=nolto-rel
-.PHONY: build check test test-arm assets clean
-build: output/donkeykong.nwa
-output:
- mkdir -p $@
-output/%.o: src/%.c $(HEADERS) | output
- $(CC) $(CFLAGS) -c $< -o $@
-output/icon.o: src/icon.png | output
- $(NWLINK) png-icon-o $< $@
-output/icon-compact.o: output/icon.o $(PACKER)
- $(PYTHON) tools/compact_nwa.py icon $< $@ --objcopy $(OBJCOPY)
-output/donkeykong-debug.nwa: $(OBJECTS) output/icon-compact.o $(PACKER)
- $(CC) $(CFLAGS) $(LINK) -Wl,-u,eadk_app_name,-u,eadk_app_icon,-u,eadk_api_level $(OBJECTS) output/icon-compact.o -o output/standalone.o
- $(PYTHON) tools/compact_nwa.py app output/standalone.o $@ --ld $(ARM_LD)
-output/donkeykong.nwa: output/donkeykong-debug.nwa $(PACKER)
- $(PYTHON) tools/compact_nwa.py release $< $@ --objcopy $(OBJCOPY)
-check: build
- $(NWLINK) nwa-bin output/donkeykong.nwa output/donkeykong.bin --ram-length 107674
- $(NWLINK) nwa-bin output/donkeykong-debug.nwa output/donkeykong-debug.bin --ram-length 107674
- $(PYTHON) -c "from pathlib import Path; assert Path('output/donkeykong.bin').read_bytes() == Path('output/donkeykong-debug.bin').read_bytes()"
-test: | output
- $(HOST_CC) -std=c11 -O2 -Wall -Wextra -Werror -Isrc tests/test.c src/game.c src/save.c src/render.c -o output/test
- cd output && ./test
-test-arm:
- $(PYTHON) tests/test_arm.py --nwlink "$(NWLINK)"
-assets:
- $(PYTHON) tools/assets.py
+BUILD_DIR = output
+L = ../../launcher/src
+
+SRC = src/main.c src/sim.c src/draw.c src/save.c src/plat_eadk.c
+HEADERS = $(wildcard src/*.h) ../common/epsilon_app.h ../common/epsilon_files.h $(L)/gfx.h $(L)/np.h $(L)/font.h
+
+CFLAGS = -std=gnu11 $(shell $(NWLINK) eadk-cflags-device) -DPLATFORM_DEVICE=1
+CFLAGS += -Os -Wall -Wextra -Wno-unused-parameter -fno-math-errno
+CFLAGS += -fno-tree-loop-distribute-patterns -flto -fno-fat-lto-objects -fwhole-program -fvisibility=internal
+CFLAGS += -ffunction-sections -fdata-sections
+LDFLAGS = -Wl,--relocatable -nostartfiles --specs=nano.specs
+LDFLAGS += -Wl,-e,main -Wl,-u,eadk_app_name -Wl,-u,eadk_app_icon -Wl,-u,eadk_api_level
+LDFLAGS += -Wl,--gc-sections -flinker-output=nolto-rel
+
+.PHONY: build check clean host test rom art
+build: $(BUILD_DIR)/donkeykong.nwa
+
+# apps get 133652 bytes of RAM on custom builds of Epsilon 25.2 (148928 or more on official ones)
+check: $(BUILD_DIR)/donkeykong.nwa
+ $(Q) $(NWLINK) nwa-bin --ram-length 133652 $< $(BUILD_DIR)/donkeykong.bin
+ @echo "BIN $(BUILD_DIR)/donkeykong.bin: $$(wc -c < $(BUILD_DIR)/donkeykong.bin) bytes"
+
+$(BUILD_DIR)/donkeykong.nwa: $(SRC) $(HEADERS) $(L)/gfx.c $(L)/font_data.c $(L)/xfont.c $(BUILD_DIR)/icon.o
+ @echo "LD $@"
+ $(Q) $(CC) $(CFLAGS) $(LDFLAGS) $(SRC) $(L)/gfx.c $(L)/font_data.c $(L)/xfont.c $(BUILD_DIR)/icon.o -lm -o $@
+ $(Q) arm-none-eabi-strip --strip-unneeded $@
+
+$(BUILD_DIR)/icon.o: src/icon.png | $(BUILD_DIR)
+ @echo "ICON $<"
+ $(Q) $(NWLINK) png-icon-o $< $@
+
+$(BUILD_DIR):
+ $(Q) mkdir -p $@
+
+# on a computer: tests/play.c runs the game with scripted keys, checks it and saves pictures
+HOST_FLAGS = -std=gnu11 -O2 -g -Wall -Wextra -Wno-unused-parameter -Isrc -DHOST \
+ -fsanitize=address,undefined -fno-sanitize-recover=undefined
+host: build/play
+build/play: $(filter-out src/plat_eadk.c,$(SRC)) tests/play.c $(HEADERS) $(L)/gfx.c $(L)/font_data.c $(L)/xfont.c
+ $(Q) mkdir -p build
+ $(Q) cc $(HOST_FLAGS) tests/play.c src/sim.c src/draw.c src/save.c $(L)/gfx.c $(L)/font_data.c $(L)/xfont.c -lm -o $@
+ @echo "HOST $@"
+test: build/play
+ build/play test
+
+rom:
+ python3 -I tools/rom.py $(LST) src/rom.h
+art:
+ python3 -I tools/art.py src/art.h
+
clean:
- rm -rf output
+ $(Q) rm -rf $(BUILD_DIR) build
diff --git a/games/donkeykong/VALIDATION.md b/games/donkeykong/VALIDATION.md
deleted file mode 100644
index 4c99c291..00000000
--- a/games/donkeykong/VALIDATION.md
+++ /dev/null
@@ -1,63 +0,0 @@
-# Donkey Kong validation
-
-Verified locally on 2026-10-21, including an isolated publication snapshot
-based on upstream `09943ea31e9198816ead6e33c75788cda6810048`.
-
-## Footprint
-
-| Item | Measured size |
-| --- | ---: |
-| Release `.nwa` transport file | 21,912 bytes |
-| Installed flash image | 15,824 bytes |
-| Static RAM (`.bss`) | 6,209 bytes |
-| Maximum stack observed in ARM checks | 316 bytes |
-| Game state, including all entity pools | 864 bytes |
-| LCD drawing strip | 5,120 bytes |
-| Record save payload | 32 bytes |
-| Packed launcher icon | 346 bytes |
-
-The renderer draws directly from packed 4-bit sprites into one eight-row strip.
-Strip hashes avoid sending unchanged rows. There is no heap, full framebuffer,
-floating-point physics, external asset pack, or ROM. Fixed 60 Hz simulation
-uses a bounded catch-up loop and does not advance while paused.
-
-## Tests
-
-- C11 host build with `-O2 -Wall -Wextra -Werror`.
-- Actual walking, jumping and climbing routes through all four boards;
- obstacles and items are disabled for those geometry checks. The elevator
- route boards a moving lift and transfers to the right-hand girders.
-- Separate live obstacle checks for barrel jumping, one-time jump awards,
- hammer hits, body collisions and capped scores/extra lives.
-- Broken/retracting ladders, hammer restrictions, held jump input, fatal
- falls, timeouts, life loss, game over, rivet holes and round progression.
-- 48 seeded random-input simulations (up to 18,000 steps each), checking
- entity bounds and invariants at every step.
-- Save round-trip, every-byte corruption, truncated/null input, preserving
- the prior decoded record on failure.
-- Host and actual ARM screenshots of the boards, title, pause, instructions
- and storage-full dialog; inspected visually.
-- Actual ARM app on Epsilon N0120 (153,676-byte app RAM), Epsilon N0110
- (148,928-byte app RAM) and Upsilon (107,674-byte app RAM): launch, pause,
- resume, save, reload records, Home/On-Off exit.
-- Full filesystem: keep playing, retry exit, and leave without saving;
- unrelated files remain byte-identical.
-- Exit checks: no invalid accesses, firmware RAM changes, unbalanced Home
- locks, or uncleared app RAM.
-- Release and debug artifacts install to identical bytes on the smallest
- RAM profile.
-- Publication builds with the current upstream Home/exit and file helpers;
- the exit helper waits for held keys to be released before returning Home.
-- `make build check test` passes from the isolated snapshot. Release
- generation lists `DonkeyKong.nwa` once, with Bobydeluxe's credit. The
- combined launcher manifest is unchanged, and no other game is modified.
-
-The four-board ARM render sample recorded 193 frames, a slowest draw of
-11 ms, and no draws over 17 ms at the emulator's 216 MHz instruction budget.
-This is an emulator measurement, not a physical-device benchmark. There has
-been no physical-calculator playtest or exhaustive human playthrough of every
-difficulty level. N0115 shares the N0110 architecture but was not separately
-benchmarked here.
-
-Reproducible local reports: `output/build-report.json` and
-`output/arm/report.json`. Outputs and toolchain binaries are ignored by Git.
diff --git a/games/donkeykong/build.py b/games/donkeykong/build.py
deleted file mode 100644
index 037b9e51..00000000
--- a/games/donkeykong/build.py
+++ /dev/null
@@ -1,70 +0,0 @@
-#!/usr/bin/env python3
-"""Build a standalone Donkey Kong .nwa, without changing the NumPlay launcher.
-
-python build.py --arm-bin /path/to/arm/bin --nwlink /path/to/nwlink
-NWLINK may also name the nwlink JavaScript entry point (run with Node).
-"""
-import argparse
-import json
-import os
-from pathlib import Path
-import re
-import shlex
-import shutil
-import subprocess
-import sys
-
-ROOT = Path(__file__).resolve().parent
-REPO = ROOT.parents[1]
-OUT = ROOT/'output'
-
-def run(args):
- args = [str(x) for x in args]
- subprocess.run(args, cwd=ROOT, check=True)
-
-def main():
- ap = argparse.ArgumentParser(description=__doc__)
- ap.add_argument('--arm-bin', default=os.environ.get('ARM_BIN',''))
- ap.add_argument('--nwlink', default=os.environ.get('NWLINK','nwlink'))
- a = ap.parse_args()
- OUT.mkdir(exist_ok=True)
- arm = lambda name: str(Path(a.arm_bin)/('arm-none-eabi-'+name)) if a.arm_bin else 'arm-none-eabi-'+name
- if Path(a.nwlink).is_file():
- nw = [a.nwlink] if a.nwlink.endswith(('.exe','.cmd')) else ['node',a.nwlink]
- else:
- nw = ([v.replace('"','') for v in re.findall(r'(?:[^\s"]|"[^"]*")+',a.nwlink)]
- if os.name=='nt' else shlex.split(a.nwlink))
- flags = subprocess.check_output(nw+['eadk-cflags-device'],cwd=ROOT,text=True).strip()
- # nwlink quotes the include directory. Preserve backslashes inside Windows paths.
- target = ([v.replace('"','') for v in re.findall(r'(?:[^\s"]|"[^"]*")+',flags)]
- if os.name=='nt' else shlex.split(flags))
- opts = ['-std=c11',*target,'-Os','-Wall','-Wextra','-Werror','-ffunction-sections',
- '-fdata-sections','-fstack-usage','-fno-tree-loop-distribute-patterns',
- '-flto','-fno-fat-lto-objects','-fvisibility=internal']
- run([sys.executable,'tools/assets.py'])
- objects = []
- for name in ('main','game','save','render'):
- obj = OUT/(name+'.o')
- run([arm('gcc'),*opts,'-c','src/'+name+'.c','-o',obj]);objects.append(obj)
- packer = ROOT/'tools/compact_nwa.py'
- run([*nw,'png-icon-o','src/icon.png',OUT/'icon.o'])
- run([sys.executable,packer,'icon',OUT/'icon.o',OUT/'icon-compact.o','--objcopy',arm('objcopy')])
- run([arm('gcc'),*opts,'-nostartfiles','--specs=nano.specs',
- '-Wl,--relocatable,--gc-sections,-e,main','-flinker-output=nolto-rel',
- '-Wl,-u,eadk_app_name,-u,eadk_app_icon,-u,eadk_api_level',
- *objects,OUT/'icon-compact.o','-o',OUT/'standalone.o'])
- run([sys.executable,packer,'app',OUT/'standalone.o',OUT/'donkeykong-debug.nwa','--ld',arm('ld')])
- run([sys.executable,packer,'release',OUT/'donkeykong-debug.nwa',OUT/'donkeykong.nwa','--objcopy',arm('objcopy')])
- for name in ('donkeykong','donkeykong-debug'):
- run([*nw,'nwa-bin',OUT/(name+'.nwa'),OUT/(name+'.bin'),'--ram-length','107674'])
- installed = (OUT/'donkeykong.bin').read_bytes()
- assert installed == (OUT/'donkeykong-debug.bin').read_bytes(), 'Release changes installed bytes'
- apps = REPO/'build/apps';apps.mkdir(parents=True,exist_ok=True)
- shutil.copyfile(OUT/'donkeykong.nwa',apps/'DonkeyKong.nwa')
- report = dict(nwa_bytes=(OUT/'donkeykong.nwa').stat().st_size, installed_bytes=len(installed),
- release_installed_matches_debug=True, artifact=str(apps/'DonkeyKong.nwa'))
- (OUT/'build-report.json').write_text(json.dumps(report,indent=2)+'\n')
- print(json.dumps(report,indent=2))
-
-if __name__=='__main__':
- main()
diff --git a/games/donkeykong/src/art.h b/games/donkeykong/src/art.h
new file mode 100644
index 00000000..668e9071
--- /dev/null
+++ b/games/donkeykong/src/art.h
@@ -0,0 +1,191 @@
+/* Generated by tools/art.py: Donkey Kong's pictures for this port (no Nintendo graphics data). */
+#ifndef DK_ART_H
+#define DK_ART_H
+#include <stdint.h>
+enum { PAL_MARIO, PAL_KONG, PAL_KFACE, PAL_PAUL_TOP, PAL_PAUL_LOW, PAL_BARREL, PAL_BLUE, PAL_FIRE, PAL_FIRE_BLUE, PAL_HAMMER, PAL_HAMMER_FLASH, PAL_POINTS, PAL_PIE, PAL_SPRING, PAL_ELEVATOR, PAL_MOTOR, PAL_LADDER_WHITE, PAL_PULLEY, PAL_FLAME, PAL_ITEMS, PAL_HEART, PAL_BLACK, PAL_SMASH, PAL_DIZZY, PAL_COUNT };
+static const uint16_t dk_pal[PAL_COUNT][3] = {
+ {0xF902, 0x299F, 0xFDCA},
+ {0xB880, 0xFDCA, 0xFB40},
+ {0xB880, 0xFDCA, 0xFFFF},
+ {0xFB40, 0xFFBD, 0xFA35},
+ {0xFA35, 0xFFFF, 0x1096},
+ {0xFB40, 0xFDCA, 0x211F},
+ {0x211F, 0x079F, 0xFFFF},
+ {0xFE00, 0xF900, 0xFFFF},
+ {0x94BF, 0xF900, 0x079F},
+ {0xFDC0, 0x9100, 0xFFFF},
+ {0xF900, 0xFF0E, 0xFFFF},
+ {0xB880, 0xFDCA, 0xFFFF},
+ {0xFDD3, 0x211E, 0xFB80},
+ {0xA53F, 0xF900, 0xFFFF},
+ {0xF96C, 0xA000, 0xF96C},
+ {0xFE00, 0xA000, 0xFB40},
+ {0xFFFF, 0xFFFF, 0xFFFF},
+ {0x9CDF, 0xF800, 0x079F},
+ {0xF900, 0xFDC0, 0xFFFF},
+ {0xFA35, 0xFFFF, 0x1096},
+ {0xFB40, 0xFFFF, 0xFA35},
+ {0x0000, 0x0000, 0x0000},
+ {0x211F, 0x079F, 0xFFFF},
+ {0xFFFF, 0xFE00, 0x079F},
+};
+static const uint8_t dk_spr[91][64] = {
+ {0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x05,0x55,0x54,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF0,0x00,0x00,0x2A,0x68,0x00,0x0A,0x9A,0x6A,0x00,0x2A,0x95,0x6A,0x80,0x3E,0x75,0xDB,0xC0,0x3F,0x55,0x5F,0xC0,0x0D,0x55,0x57,0x00,0x01,0x50,0x54,0x00,0x0A,0xA0,0xAA,0x00}, /* 00 */
+ {0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x05,0x55,0x54,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF0,0x00,0x03,0x29,0x6A,0x00,0x0F,0xA5,0x6A,0xC0,0x0C,0x55,0x5A,0xF0,0x00,0x55,0x54,0xF0,0x01,0x55,0x54,0x00,0x05,0x50,0x55,0x00,0x0A,0x80,0x2A,0x80,0x2A,0x00,0x0A,0x80}, /* 01 */
+ {0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x05,0x55,0x54,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF0,0x00,0x00,0x2A,0x68,0x00,0x00,0xA9,0x6A,0x00,0x00,0x97,0x5A,0x00,0x00,0x55,0x7E,0x00,0x00,0x15,0x7D,0x00,0x00,0x2A,0x54,0x00,0x00,0x2A,0xA8,0x00,0x00,0x0A,0xA0,0x00}, /* 02 */
+ {0x00,0x00,0x03,0xC0,0x00,0x15,0x53,0xC0,0x00,0x55,0x52,0x80,0x00,0x55,0x56,0x80,0x00,0xAA,0xAA,0x80,0x02,0xAA,0xAA,0x00,0x02,0x9A,0x98,0x00,0x0A,0x9A,0x98,0x00,0x28,0x55,0x54,0x00,0x3C,0x55,0x54,0x00,0x3C,0x55,0x54,0x00,0x00,0x54,0x54,0x00,0x02,0x94,0x54,0x00,0x02,0xA0,0x5A,0x00,0x00,0x00,0x2A,0x00,0x00,0x00,0x2A,0x00}, /* 03 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x00,0x55,0x54,0x00,0xF0,0x55,0x54,0x3C,0xFA,0xAA,0xAA,0xBC,0x2A,0xAA,0xAA,0xA0,0x0A,0x6A,0xA6,0x80,0x00,0x55,0x54,0x00,0x00,0x55,0x54,0x00,0x00,0x55,0x54,0x00,0x00,0x54,0x54,0x00,0x00,0x54,0x54,0x00,0x02,0xA8,0xAA,0x00,0x02,0xA0,0x2A,0x00}, /* 04 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x54,0x00,0xF1,0x55,0x55,0x3C,0xFA,0xAA,0xAA,0xBC,0x2A,0xAA,0xAA,0xA0,0x0A,0x6A,0xA6,0x80,0x01,0x55,0x55,0x00,0x01,0x55,0x55,0x00,0x01,0x54,0x55,0x00,0x0A,0xA0,0x2A,0x80,0x0A,0x80,0x0A,0x80,0x00,0x00,0x00,0x00}, /* 05 */
+ {0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x00,0x55,0x54,0x00,0x00,0x55,0x54,0x00,0x00,0xAA,0xA8,0x00,0x02,0xAA,0xAA,0x00,0x0A,0x6A,0xA6,0x80,0x3A,0x6A,0xA6,0xB0,0x3E,0x55,0x56,0xF0,0x3C,0x55,0x54,0xF0,0x00,0x55,0x54,0x00,0x00,0x55,0x54,0x00,0x00,0x54,0x54,0x00,0x00,0x54,0x54,0x00,0x02,0xA8,0xAA,0x00,0x02,0xA0,0x2A,0x00}, /* 06 */
+ {0x00,0x00,0xF0,0x00,0x00,0x15,0xF0,0x00,0x05,0x55,0x64,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF8,0x00,0x00,0x2A,0x68,0x00,0x02,0x9A,0x6A,0x00,0x02,0x95,0x6A,0x00,0x01,0x55,0x55,0x00,0x01,0x55,0x55,0x00,0x01,0x50,0x54,0x00,0x0A,0xA0,0xAA,0x00,0x00,0x00,0x00,0x00}, /* 08 */
+ {0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x05,0x55,0x54,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF0,0x00,0x3E,0xAA,0xA8,0x00,0x3E,0xA6,0x9A,0x00,0x00,0x25,0x5A,0x80,0x00,0x15,0x55,0x00,0x00,0x55,0x55,0x00,0x01,0x50,0x54,0x00,0x0A,0xA0,0xAA,0x00,0x00,0x00,0x00,0x00}, /* 09 */
+ {0x00,0x00,0xF0,0x00,0x00,0x15,0xF0,0x00,0x05,0x55,0x64,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF8,0x00,0x00,0x29,0x6A,0x00,0x00,0xA5,0x6A,0x00,0x00,0x55,0x5A,0x00,0x00,0x55,0x54,0x00,0x01,0x55,0x54,0x00,0x05,0x50,0x55,0x00,0x0A,0x80,0x2A,0x80,0x2A,0x00,0x0A,0x80}, /* 0A */
+ {0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x05,0x55,0x54,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF0,0x00,0x3E,0xAA,0xAA,0x00,0x3E,0xA9,0x6A,0x80,0x00,0x15,0x5A,0x80,0x00,0x55,0x54,0x00,0x01,0x55,0x54,0x00,0x05,0x50,0x55,0x00,0x0A,0x80,0x2A,0x80,0x2A,0x00,0x0A,0x80}, /* 0B */
+ {0x00,0x00,0xF0,0x00,0x00,0x15,0xF0,0x00,0x05,0x55,0x64,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF8,0x00,0x00,0x2A,0x68,0x00,0x00,0xA9,0x6A,0x00,0x00,0x95,0x5A,0x00,0x00,0x15,0x54,0x00,0x00,0x2A,0x54,0x00,0x00,0x2A,0xA8,0x00,0x00,0x0A,0xA0,0x00,0x00,0x00,0x00,0x00}, /* 0C */
+ {0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x05,0x55,0x54,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF0,0x00,0x3E,0xAA,0xA8,0x00,0x3E,0xA9,0x6A,0x00,0x00,0x15,0x5A,0x00,0x00,0x15,0x54,0x00,0x00,0x2A,0x54,0x00,0x00,0x2A,0xA8,0x00,0x00,0x0A,0xA0,0x00,0x00,0x00,0x00,0x00}, /* 0D */
+ {0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x05,0x55,0x54,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF0,0x00,0x3C,0xA9,0xA8,0xF0,0x3E,0x69,0xAA,0xF0,0x01,0x55,0x40,0x00,0x05,0x55,0x50,0x00,0x05,0x41,0x54,0x00,0x2A,0x00,0xAA,0x00,0x28,0x00,0x2A,0x80,0x00,0x00,0x00,0x00}, /* 0E */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x15,0x50,0x00,0x05,0x55,0x54,0x00,0x00,0xEF,0xA8,0x00,0x0F,0xEF,0xEE,0x00,0x3F,0xBF,0xAE,0x00,0x0A,0xAF,0xFA,0x00,0x03,0xFF,0xF0,0x00,0x00,0xAA,0x6A,0x00,0x03,0xA9,0x6A,0xC0,0x0F,0xA5,0x5A,0xF0,0x3C,0x55,0x54,0xF0,0x01,0x54,0x55,0x00,0x0A,0xA0,0x2A,0x80,0x2A,0x80,0x0A,0xA0}, /* 0F */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x54,0x00,0x00,0x05,0x55,0x00,0x00,0x15,0x69,0x00,0x01,0x55,0xAA,0x80,0x05,0x55,0xA6,0x80,0x05,0x15,0xAA,0x00,0x14,0x05,0xE8,0x00,0x10,0x03,0xFE,0x00,0x00,0x0F,0xFF,0xA0,0x00,0x3F,0xFF,0xC0,0x00,0x3F,0xFC,0x00,0x00,0x0F,0xFC,0x00,0x00,0x3F,0xFF,0x00,0x00,0x3F,0xFF,0xC0}, /* 10 */
+ {0x00,0x15,0x55,0x40,0x00,0x55,0x55,0x50,0x00,0x55,0x55,0x50,0x01,0x55,0x55,0x54,0x01,0x59,0x55,0x54,0x05,0x5A,0x55,0x54,0x05,0x56,0xA5,0x54,0x15,0x55,0x6A,0xA0,0x15,0x55,0x55,0x40,0x00,0xA0,0x28,0x00,0x03,0xF0,0x3C,0x00,0x0F,0xF0,0x3F,0x00,0x0F,0x00,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 11 */
+ {0x00,0x15,0x55,0x40,0x00,0x55,0x55,0x50,0x00,0x55,0x55,0x54,0x01,0x55,0x55,0x55,0x01,0x65,0x55,0x55,0x05,0x5A,0x55,0x54,0x05,0x55,0xA9,0x50,0x05,0x55,0x5A,0x80,0x01,0x55,0x55,0x00,0x00,0x28,0x28,0x00,0x00,0xFC,0x3C,0x00,0x03,0xFC,0x3F,0x00,0x03,0xC0,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 12 */
+ {0x00,0x55,0x55,0x40,0x01,0x55,0x55,0x50,0x01,0x55,0x55,0x50,0x05,0x55,0x55,0x54,0x05,0x65,0x56,0x54,0x15,0x69,0x5A,0x55,0x15,0x5A,0xA9,0x55,0x55,0x55,0x55,0x55,0x15,0x55,0x55,0x54,0x00,0x28,0x28,0x00,0x00,0xFC,0x3F,0x00,0x03,0xFC,0x3F,0xC0,0x03,0xC0,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 13 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x54,0x00,0x00,0x15,0x55,0x80,0x00,0x15,0x55,0x60,0x00,0x05,0x55,0x5A,0x00,0x01,0x55,0x52,0x80,0x00,0x50,0xA0,0xAC,0x02,0x80,0x28,0x3F,0x0F,0x00,0x0A,0xF0,0x3F,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 14 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2A,0xA8,0x00,0x02,0x95,0x56,0x80,0x09,0x55,0x5F,0x60,0x09,0x55,0x77,0x60,0x25,0x55,0xD5,0x58,0x25,0x57,0x55,0x58,0x25,0x5D,0x55,0x58,0x09,0xF5,0x55,0x60,0x09,0xF5,0x55,0x60,0x02,0x95,0x56,0x80,0x00,0x2A,0xA8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 15 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A,0xAA,0xAA,0xA0,0x2F,0xFF,0xFF,0xF8,0x25,0x55,0x55,0x58,0x25,0x55,0x55,0x58,0x2F,0xFF,0xFF,0xF8,0x25,0x55,0x55,0x58,0x25,0x55,0x55,0x58,0x2F,0xFF,0xFF,0xF8,0x0A,0xAA,0xAA,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 16 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A,0xAA,0xAA,0xA0,0x2F,0xFF,0xFF,0xF8,0x25,0x55,0x55,0x58,0x25,0x65,0x59,0x58,0x2F,0xFF,0xFF,0xF8,0x25,0x59,0x56,0x58,0x25,0x55,0x55,0x58,0x2F,0xFF,0xFF,0xF8,0x0A,0xAA,0xAA,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 17 */
+ {0x00,0xAA,0xAA,0x00,0x02,0xFF,0xFF,0x80,0x09,0x55,0x55,0x60,0x09,0x5A,0x55,0x60,0x09,0x55,0x55,0x60,0x0B,0xFF,0xFF,0xE0,0x09,0x55,0x55,0x60,0x09,0x56,0x95,0x60,0x09,0x55,0x55,0x60,0x09,0x55,0x55,0x60,0x0B,0xFF,0xFF,0xE0,0x09,0x55,0x55,0x60,0x09,0x55,0x69,0x60,0x09,0x55,0x55,0x60,0x02,0xFF,0xFF,0x80,0x00,0xAA,0xAA,0x00}, /* 18 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x2A,0xA8,0x00,0x02,0x95,0x56,0x80,0x09,0x5F,0xF5,0x60,0x09,0x75,0x5D,0x60,0x25,0xD7,0xD7,0x58,0x25,0xDD,0x77,0x58,0x25,0xD7,0xDD,0x58,0x09,0x75,0x5D,0x60,0x09,0x5F,0xF5,0x60,0x02,0x95,0x56,0x80,0x00,0x2A,0xA8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 19 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A,0xAA,0xAA,0xA0,0x25,0x55,0x55,0x58,0x2F,0xFF,0xFF,0xF8,0x2F,0xFF,0xFF,0xF8,0x25,0x55,0x55,0x58,0x2F,0xFF,0xFF,0xF8,0x2F,0xFF,0xFF,0xF8,0x25,0x55,0x55,0x58,0x0A,0xAA,0xAA,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 1A */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A,0xAA,0xAA,0xA0,0x25,0x55,0x55,0x58,0x2F,0xFF,0xFF,0xF8,0x2F,0xDF,0xF7,0xF8,0x25,0x55,0x55,0x58,0x2F,0xF7,0xFD,0xF8,0x2F,0xFF,0xFF,0xF8,0x25,0x55,0x55,0x58,0x0A,0xAA,0xAA,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 1B */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x55,0x00,0x01,0xF5,0x55,0x40,0x01,0xD5,0x55,0x40,0x01,0x55,0x55,0x40,0x00,0x55,0x55,0x00,0x00,0x0A,0xA0,0x00,0x00,0x0A,0xA0,0x00,0x00,0x02,0x80,0x00,0x00,0x02,0x80,0x00,0x00,0x02,0x80,0x00,0x00,0x02,0x80,0x00,0x00,0x02,0x80,0x00,0x00,0x02,0x80,0x00}, /* 1E */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x54,0x00,0x00,0x07,0xD5,0x00,0x00,0x07,0x55,0x00,0x00,0x05,0x55,0xA0,0x00,0x05,0x55,0xAA,0xAA,0x05,0x55,0xAA,0xAA,0x05,0x55,0x00,0x00,0x05,0x55,0x00,0x00,0x01,0x54,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 1F */
+ {0x00,0x15,0x54,0x00,0x01,0x55,0x55,0x40,0x05,0x55,0x55,0x50,0x15,0xAA,0xAA,0x54,0x16,0x5A,0xA5,0x94,0x1A,0xE6,0x9E,0xA4,0x1A,0xBA,0xAB,0xA4,0x1A,0xAA,0xAA,0xA4,0x56,0xA9,0x6A,0x95,0x59,0x55,0x55,0x65,0x59,0xFF,0xFF,0x65,0x59,0x55,0x55,0x65,0x59,0xFF,0xFF,0x65,0x15,0x55,0x55,0x54,0x05,0x55,0x55,0x50,0x00,0x55,0x55,0x00}, /* 20 */
+ {0x00,0x15,0x54,0x00,0x01,0x55,0x55,0x40,0x05,0x55,0x55,0x50,0x15,0xAA,0xAA,0x54,0x16,0x5A,0xA5,0x94,0x1A,0xB6,0x9B,0xA4,0x1A,0xEA,0xAE,0xA4,0x1A,0xAA,0xAA,0xA4,0x56,0xA9,0x6A,0x95,0x59,0x55,0x55,0x65,0x59,0xFF,0xFF,0x65,0x59,0x55,0x55,0x65,0x59,0xFF,0xFF,0x65,0x15,0x55,0x55,0x54,0x05,0x55,0x55,0x50,0x00,0x55,0x55,0x00}, /* 21 */
+ {0x00,0x15,0x54,0x00,0x01,0x55,0x55,0x40,0x05,0x55,0x55,0x50,0x15,0xAA,0xAA,0x54,0x16,0x5A,0xA5,0x94,0x1A,0xE6,0x9E,0xA4,0x1A,0xEA,0xAE,0xA4,0x1A,0xAA,0xAA,0xA4,0x56,0xA9,0x6A,0x95,0x59,0x55,0x55,0x65,0x59,0xFF,0xFF,0x65,0x59,0x55,0x55,0x65,0x59,0xFF,0xFF,0x65,0x15,0x55,0x55,0x54,0x05,0x55,0x55,0x50,0x00,0x55,0x55,0x00}, /* 22 */
+ {0x00,0x15,0x54,0x00,0x01,0x55,0x55,0x40,0x05,0x55,0x55,0x50,0x15,0xAA,0xAA,0x54,0x16,0xAA,0xAA,0x94,0x1A,0xFA,0xAF,0xA4,0x1A,0x7A,0xAD,0xA4,0x1A,0xAA,0xAA,0xA4,0x56,0xA9,0x6A,0x95,0x5A,0xAA,0xAA,0xA5,0x59,0x55,0x55,0x65,0x5A,0xAA,0xAA,0xA5,0x56,0xAA,0xAA,0x95,0x15,0xAA,0xAA,0x54,0x05,0x55,0x55,0x50,0x00,0x55,0x55,0x00}, /* 23 */
+ {0x00,0x15,0x54,0x00,0x01,0x55,0x55,0x40,0x05,0x55,0x55,0x50,0x15,0xAA,0xAA,0x54,0x16,0x5A,0xA5,0x94,0x1A,0xF6,0x9F,0xA4,0x1A,0x7A,0xAD,0xA4,0x1A,0xAA,0xAA,0xA4,0x56,0xA9,0x6A,0x95,0x59,0x55,0x55,0x65,0x59,0xFF,0xFF,0x65,0x59,0x55,0x55,0x65,0x59,0xFF,0xFF,0x65,0x15,0x55,0x55,0x54,0x05,0x55,0x55,0x50,0x00,0x55,0x55,0x00}, /* 24 */
+ {0x01,0x6A,0xA9,0x40,0x05,0xAA,0xAA,0x50,0x06,0xAA,0xAA,0x90,0x06,0xAB,0xEA,0x90,0x06,0xAA,0xAA,0x90,0x05,0xAA,0xAA,0x50,0x01,0x6A,0xA9,0x40,0x01,0x5A,0xA5,0x40,0x01,0x56,0x95,0x40,0x01,0x55,0x55,0x40,0x01,0x55,0x55,0x40,0x00,0x54,0x15,0x00,0x00,0x50,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 25 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x55,0x40,0x00,0x15,0x55,0x50,0x01,0x55,0x55,0x50,0x05,0x55,0x55,0x50,0x15,0x55,0x55,0x40,0x15,0x55,0x50,0x00,0x15,0x54,0x00,0x00,0x35,0x5C,0x00,0x00,0xAA,0xAA,0x00,0x00,0xAA,0xAA,0x80,0x00,0x8A,0x8A,0x80,0x00,0x00,0x00,0x00,0x00}, /* 26 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x50,0x00,0x01,0x55,0x55,0x00,0x01,0x55,0x55,0x40,0x01,0x55,0x55,0x50,0x00,0x55,0x55,0x50,0x00,0x15,0x55,0x50,0x00,0x05,0x55,0x50,0x00,0x01,0x55,0x50,0x00,0x00,0x55,0x50,0x00,0x00,0x55,0x50,0x00,0x00,0xD5,0x70,0x00,0x02,0xAA,0xA8,0x00,0x0A,0xAA,0xAA,0x00,0x0A,0x8A,0x8A}, /* 27 */
+ {0x00,0x0A,0xA0,0x00,0x00,0x2A,0xA8,0x00,0x00,0x2A,0xA8,0x00,0x00,0x0D,0x70,0x00,0x00,0x05,0x50,0x00,0x00,0x15,0x54,0x00,0x00,0x55,0x54,0x00,0x01,0x57,0x54,0x00,0x05,0x5D,0x54,0x00,0x15,0x75,0x50,0x00,0x55,0x55,0x40,0x00,0x55,0x55,0x00,0x00,0x55,0x54,0x00,0x00,0x55,0x50,0x00,0x00,0x15,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 28 */
+ {0x00,0x00,0x54,0x00,0x00,0x05,0x55,0x40,0x00,0x15,0x55,0x50,0x00,0x55,0x55,0x50,0x01,0x55,0x55,0x50,0x01,0x57,0x55,0x40,0x05,0x57,0xD5,0x00,0x05,0x55,0xD4,0x00,0x05,0x55,0x50,0x00,0x05,0x55,0x50,0x00,0x05,0x55,0x40,0x00,0x05,0x55,0x40,0x00,0x0D,0x55,0xC0,0x00,0x2A,0xAA,0x80,0x00,0x2A,0xAA,0xA0,0x00,0x2A,0x28,0xA0,0x00}, /* 29 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x54,0x00,0x00,0x15,0x55,0x40,0x00,0x55,0x55,0x50,0x00,0x55,0x55,0x54,0x00,0x55,0x55,0x55,0x00,0x15,0x55,0x55,0x40,0x05,0x55,0x55,0x40,0x00,0x55,0x55,0x40,0x00,0x05,0x55,0x40,0x00,0x01,0x55,0x40,0x00,0x01,0x55,0x40,0x00,0x01,0x55,0x00}, /* 2A */
+ {0x00,0x01,0x55,0x00,0x00,0x05,0x54,0x00,0x00,0x15,0x50,0x00,0x00,0xD5,0xC0,0x00,0x0A,0xAA,0x00,0x00,0x2A,0xAA,0x00,0x00,0x2A,0xAA,0x00,0x00,0x0A,0xA8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 2B */
+ {0x55,0x55,0x50,0x00,0x55,0x55,0x50,0x00,0x55,0x55,0x54,0x00,0x55,0x55,0x54,0x00,0x55,0x55,0x54,0x00,0x55,0x55,0x54,0x00,0x55,0x55,0x54,0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x15,0x55,0x57,0x00,0x2A,0xAA,0xAA,0x80,0x2A,0xAA,0xAA,0xA0,0xA8,0xA8,0xA2,0xA0,0x00,0x00,0x00,0x00}, /* 2C */
+ {0x00,0x00,0x55,0x55,0x00,0x01,0x55,0x55,0x00,0x05,0x55,0x55,0x00,0x15,0x55,0x55,0x00,0x55,0x55,0x51,0x01,0x55,0x55,0x41,0x05,0x55,0x55,0x01,0x15,0x55,0x54,0x01,0x15,0x55,0x50,0x01,0x55,0x55,0x40,0x01,0xD5,0x57,0x00,0x01,0xAA,0xAA,0x00,0x01,0xAA,0xAA,0x80,0x00,0xA2,0xA2,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 2D */
+ {0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x54,0x55,0x55,0x55,0x50,0x15,0x55,0x55,0x40,0x05,0x55,0x55,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 2E */
+ {0x05,0x55,0x55,0x40,0x15,0x55,0x55,0x54,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55}, /* 2F */
+ {0x00,0x15,0x54,0x00,0x01,0x55,0x55,0x40,0x05,0x55,0x55,0x50,0x15,0x5A,0xA9,0x54,0x15,0x6A,0xAA,0x54,0x55,0xAF,0xAA,0x95,0x56,0xA7,0xAA,0x95,0xAA,0xAA,0xAA,0x95,0xAA,0xAA,0xAA,0x95,0xAA,0xAA,0xAA,0x55,0x95,0x55,0x5A,0x55,0xAA,0xAA,0xAA,0x55,0x2A,0xAA,0xA9,0x55,0x05,0x55,0x55,0x55,0x01,0x55,0x55,0x55,0x00,0x15,0x55,0x55}, /* 30 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x40,0x00,0x00,0x55,0x54,0x00,0x00,0x15,0x55,0x40,0x00,0x01,0x55,0x50,0x00,0x00,0x15,0x5C,0x00,0x00,0x0A,0xAA,0x00,0x00,0x2A,0xAA,0x80,0x00,0x2A,0xAA,0x80,0x00,0x0A,0xAA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 31 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x55,0x00,0x05,0x55,0x55,0x00,0x15,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x01,0x55,0x55,0x55,0x01,0x55,0x55,0x55,0x01,0x55,0x55,0x55,0x01,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x15,0x55,0x55}, /* 32 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x55,0x00,0x00,0x55,0x55,0x50,0x00,0x55,0x55,0x54,0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x40,0x55,0x55,0x55,0x40,0x55,0x55,0x55,0x40,0x55,0x55,0x55,0x40,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x54,0x00}, /* 33 */
+ {0x2A,0xA8,0x00,0x00,0x2A,0xAA,0x00,0x00,0x0A,0xAA,0x00,0x00,0x03,0x5C,0x00,0x00,0x01,0x54,0x00,0x00,0x01,0x55,0x00,0x00,0x01,0x55,0x40,0x00,0x00,0x55,0x50,0x00,0x00,0x15,0x54,0x00,0x00,0x05,0x55,0x00,0x00,0x01,0x55,0x40,0x00,0x00,0x55,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 34 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x00,0x00,0x00,0x55,0x50,0x00,0x00,0x15,0x54,0x00,0x00,0x05,0x55,0x00,0x00,0x01,0x55,0xC0,0x00,0x00,0x5A,0xA0,0x00,0x00,0x2A,0xA8,0x00,0x00,0x2A,0xA8,0x00,0x00,0x0A,0xA0,0x00,0x00,0x00,0x00,0x00}, /* 35 */
+ {0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x15,0x55,0x55,0x00,0x15,0x54,0x55,0x00,0x15,0x54,0x00,0x00,0x15,0x54,0x00,0x00,0x15,0x54,0x00,0x00,0x15,0x54,0x00,0x00,0x15,0x54,0x00,0x00,0x35,0x5C,0x00,0x00,0xAA,0xAA,0x00,0x00,0xAA,0xAA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 36 */
+ {0x55,0x55,0x55,0x00,0x55,0x55,0x55,0x00,0x55,0x55,0x54,0x00,0x55,0x55,0x54,0x00,0x55,0x55,0x50,0x00,0x05,0x55,0x40,0x00,0x0D,0x57,0x00,0x00,0x2A,0xAA,0x80,0x00,0x2A,0xAA,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 37 */
+ {0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x80,0x15,0x00,0x02,0xA0,0x04,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x0C,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x0C,0x00,0x10,0x00,0x00,0x00,0x10,0x02,0x00,0x01,0x55,0x0A,0x80,0x00,0x10,0x02,0x00,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 39 */
+ {0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55}, /* 3A */
+ {0x00,0x00,0x00,0x00,0x01,0x55,0x55,0x40,0x00,0x55,0x55,0x00,0x00,0x20,0x08,0x00,0x00,0x08,0x20,0x00,0x00,0x02,0x80,0x00,0x00,0x08,0x20,0x00,0x00,0x20,0x08,0x00,0x00,0x08,0x20,0x00,0x00,0x02,0x80,0x00,0x00,0x08,0x20,0x00,0x00,0x20,0x08,0x00,0x00,0x55,0x55,0x00,0x01,0x55,0x55,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 3B */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x55,0x55,0x40,0x00,0x55,0x55,0x00,0x00,0xAA,0xAA,0x00,0x00,0x22,0x88,0x00,0x00,0xAA,0xAA,0x00,0x00,0x22,0x88,0x00,0x00,0xAA,0xAA,0x00,0x00,0x55,0x55,0x00,0x01,0x55,0x55,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 3C */
+ {0x00,0x00,0x08,0x00,0x00,0x02,0x0A,0x00,0x00,0x0A,0x2A,0x00,0x00,0x0A,0xA6,0x08,0x00,0x29,0x66,0xA8,0x00,0xA5,0x55,0x60,0x02,0x95,0x55,0x68,0x02,0x5F,0x5F,0x58,0x0A,0x5E,0x5E,0x58,0x09,0x5E,0x5E,0x58,0x09,0x55,0x55,0x58,0x09,0x55,0x55,0x58,0x0A,0x55,0x55,0x68,0x02,0x95,0x55,0xA0,0x00,0xA9,0x5A,0x80,0x00,0x0A,0xA8,0x00}, /* 3D */
+ {0x00,0x00,0x00,0x00,0x02,0x00,0x02,0x00,0x02,0x82,0x0A,0x00,0x02,0xA2,0xAA,0x00,0x00,0x9A,0x5A,0x20,0x00,0xA5,0x56,0xA0,0x02,0x95,0x55,0x68,0x02,0x5F,0x5F,0x58,0x0A,0x5E,0x5E,0x58,0x09,0x5E,0x5E,0x58,0x09,0x55,0x55,0x58,0x09,0x55,0x55,0x58,0x0A,0x55,0x55,0x68,0x02,0x95,0x55,0xA0,0x00,0xA9,0x5A,0x80,0x00,0x0A,0xA8,0x00}, /* 3E */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x40,0x04,0x00,0x00,0x40,0x14,0x00,0x01,0x41,0x15,0x04,0x01,0x51,0x59,0x04,0x05,0x95,0x99,0x54,0x06,0x96,0xA9,0x64,0x16,0xA6,0xBA,0x64,0x1A,0xEA,0xFA,0xA4,0x1B,0xEA,0xFE,0xE4,0x1B,0xFA,0xFE,0xF4}, /* 40 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x00,0x00,0x04,0x01,0x00,0x10,0x05,0x01,0x00,0x10,0x15,0x05,0x10,0x14,0x19,0x05,0x50,0x15,0x59,0x59,0x40,0x06,0x9A,0x6A,0x50,0x16,0xAA,0xAE,0x90,0x1A,0xEA,0xFE,0x94,0x1B,0xFA,0xFE,0xE4,0x1B,0xFB,0xFE,0xF4}, /* 41 */
+ {0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x40,0x04,0x04,0x01,0x40,0x04,0x14,0x41,0x44,0x05,0x15,0x45,0x44,0x15,0x19,0x56,0x54,0x19,0x5A,0x5A,0x64,0x1A,0x6A,0x6A,0x64,0x5A,0xAE,0xAE,0xA5,0x6B,0xAE,0xAF,0xA9,0x6B,0xEB,0xBF,0xB9,0x6F,0xEB,0xFF,0xB9,0x6F,0xEF,0xFF,0xBD,0x6F,0xFF,0xFF,0xFD,0x1B,0xFF,0xFF,0xF4,0x05,0xBF,0xFF,0x90}, /* 42 */
+ {0x00,0x00,0x00,0x00,0x01,0x00,0x10,0x00,0x01,0x40,0x10,0x10,0x11,0x41,0x14,0x10,0x11,0x51,0x54,0x50,0x15,0x95,0x64,0x54,0x19,0xA5,0xA5,0x64,0x19,0xA9,0xA9,0xA4,0x5A,0xBA,0xBA,0xA5,0x6A,0xFA,0xBA,0xE9,0x6E,0xFE,0xEB,0xE9,0x6E,0xFF,0xEB,0xF9,0x7E,0xFF,0xFB,0xF9,0x7F,0xFF,0xFF,0xF9,0x1F,0xFF,0xFF,0xE4,0x06,0xFF,0xFE,0x50}, /* 43 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x55,0x55,0x55,0xAA,0xAA,0xAA,0xAA,0x03,0xC0,0x03,0xC0,0x0C,0x30,0x0C,0x30,0x30,0x0C,0x30,0x0C,0xC0,0x03,0xC0,0x03,0x55,0x55,0x55,0x55,0xAA,0xAA,0xAA,0xAA,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 44 */
+ {0x00,0x00,0x00,0x00,0x05,0x55,0x55,0x50,0x15,0x55,0x55,0x54,0x1A,0xAA,0xAA,0xA4,0x1B,0xFF,0xFF,0xE4,0x1B,0xBB,0xBB,0xE4,0x1B,0xFF,0xFF,0xE4,0x1A,0xAA,0xAA,0xA4,0x15,0x55,0x55,0x54,0x01,0x55,0x55,0x40,0x00,0x16,0x94,0x00,0x00,0x06,0x90,0x00,0x00,0x02,0x80,0x00,0x00,0x02,0x80,0x00,0x00,0x02,0x80,0x00,0x00,0x02,0x80,0x00}, /* 45 */
+ {0x01,0x00,0x00,0x40,0x01,0x55,0x55,0x40,0x01,0x00,0x00,0x40,0x01,0x00,0x00,0x40,0x01,0x00,0x00,0x40,0x01,0x55,0x55,0x40,0x01,0x00,0x00,0x40,0x01,0x00,0x00,0x40,0x01,0x00,0x00,0x40,0x01,0x55,0x55,0x40,0x01,0x00,0x00,0x40,0x01,0x00,0x00,0x40,0x01,0x00,0x00,0x40,0x01,0x55,0x55,0x40,0x01,0x00,0x00,0x40,0x01,0x00,0x00,0x40}, /* 46 */
+ {0x00,0x00,0x00,0x00,0x55,0x55,0x55,0x55,0x75,0x55,0x55,0x55,0x7A,0xAA,0xAA,0xA9,0x75,0x55,0x55,0x55,0x76,0xA6,0x99,0x55,0x76,0x65,0x96,0x55,0x76,0x65,0x96,0x55,0x76,0xA6,0xA6,0xA5,0x75,0x55,0x55,0x55,0x7A,0xAA,0xAA,0xA9,0x75,0x55,0x55,0x55,0x75,0xF7,0xDF,0x7D,0x75,0x55,0x55,0x55,0x75,0x55,0x55,0x55,0x55,0x55,0x55,0x55}, /* 49 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x50,0x00,0x00,0x57,0x55,0x00,0x01,0x75,0x5D,0x40,0x05,0x55,0xD5,0x50,0x17,0x55,0x57,0x54,0x2A,0xAA,0xAA,0xA8,0x0A,0xAA,0xAA,0xA0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 4B */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x55,0x00,0x05,0x75,0x5D,0x50,0x15,0x57,0x55,0x54,0x5D,0x55,0x55,0xD5,0xAA,0xAA,0xAA,0xAA,0x2A,0xAA,0xAA,0xA8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 4C */
+ {0x00,0x02,0x00,0x00,0x00,0x0A,0x00,0x80,0x00,0x0A,0x82,0x80,0x00,0x29,0xAA,0x00,0x00,0xA5,0x56,0x80,0x02,0x95,0x55,0xA0,0x02,0x5F,0x5F,0x60,0x0A,0x5E,0x5E,0x60,0x09,0x55,0x55,0x68,0x09,0x56,0x95,0x58,0x09,0x55,0x55,0x58,0x0A,0x55,0x55,0x68,0x02,0x95,0x55,0xA0,0x02,0x8A,0xA8,0xA0,0x0A,0x02,0x20,0x28,0x08,0x02,0x20,0x08}, /* 4D */
+ {0x00,0x00,0x00,0x80,0x02,0x00,0x02,0x80,0x02,0x82,0x0A,0x80,0x00,0xAA,0xA6,0x00,0x00,0xA5,0x56,0x80,0x02,0x95,0x55,0xA0,0x02,0x5F,0x5F,0x60,0x0A,0x5E,0x5E,0x60,0x09,0x55,0x55,0x68,0x09,0x56,0x95,0x58,0x09,0x55,0x55,0x58,0x0A,0x55,0x55,0x68,0x02,0x95,0x55,0xA0,0x00,0xAA,0xAA,0x80,0x02,0x88,0x08,0xA0,0x02,0x08,0x08,0x20}, /* 4E */
+ {0x00,0x00,0x00,0x00,0x00,0x55,0x40,0x00,0x01,0x55,0x50,0x00,0x05,0x6A,0x54,0x00,0x05,0xAF,0x94,0x00,0x16,0xBE,0xA5,0x00,0x16,0xFA,0xA5,0x00,0x16,0xEA,0xA5,0x00,0x05,0xAA,0x94,0x00,0x01,0x55,0x50,0x00,0x00,0x55,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 50 */
+ {0x00,0x00,0x00,0x00,0x00,0x55,0x40,0x00,0x01,0x55,0x50,0x00,0x05,0x7F,0x54,0x00,0x05,0xFA,0xD4,0x00,0x17,0xEB,0xF5,0x00,0x17,0xAF,0xF5,0x00,0x17,0xBF,0xF5,0x00,0x05,0xFF,0xD4,0x00,0x01,0x55,0x50,0x00,0x00,0x55,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 51 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x55,0x40,0x00,0x01,0x55,0x50,0x00,0x05,0xAA,0x94,0x00,0x16,0xEA,0xA5,0x00,0x16,0xFA,0xA5,0x00,0x16,0xBE,0xA5,0x00,0x05,0xAF,0x94,0x00,0x05,0x6A,0x54,0x00,0x01,0x55,0x50,0x00,0x00,0x55,0x40,0x00,0x00,0x00,0x00,0x00}, /* 52 */
+ {0x00,0x00,0x00,0x00,0x00,0x3F,0xFC,0x00,0x03,0xEA,0xAB,0xC0,0x0E,0x80,0x02,0xB0,0x0E,0x00,0x00,0xB0,0x38,0x00,0x00,0x2C,0x38,0x00,0x00,0x2C,0x38,0x00,0x00,0x2C,0x38,0x00,0x00,0x2C,0x38,0x00,0x00,0x2C,0x0E,0x00,0x00,0xB0,0x0E,0x80,0x02,0xB0,0x03,0xEA,0xAB,0xC0,0x00,0x3F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 60 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xF0,0x00,0x00,0x3A,0xAC,0x00,0x00,0xE0,0x0B,0x00,0x00,0xE0,0x0B,0x00,0x00,0xE0,0x0B,0x00,0x00,0xE0,0x0B,0x00,0x00,0x3A,0xAC,0x00,0x00,0x0F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 61 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xF0,0x00,0x00,0x3A,0xAC,0x00,0x00,0x38,0x2C,0x00,0x00,0x3A,0xAC,0x00,0x00,0x0F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 62 */
+ {0x00,0x00,0x00,0x00,0x03,0x00,0x03,0x00,0x00,0xC3,0x0C,0x00,0x00,0x33,0x30,0x00,0x0C,0x0A,0x80,0xC0,0x03,0x25,0x63,0x00,0x00,0x27,0x60,0x00,0x3F,0x25,0x63,0xF0,0x00,0x27,0x60,0x00,0x03,0x25,0x63,0x00,0x0C,0x0A,0x80,0xC0,0x00,0x33,0x30,0x00,0x00,0xC3,0x0C,0x00,0x03,0x00,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 63 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0A,0xA0,0x00,0x00,0x2A,0xA8,0x00,0x00,0x25,0x58,0x00,0x00,0xAA,0xAA,0x00,0x16,0xAA,0xAA,0x94,0x05,0x55,0x55,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 73 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x50,0x00,0x00,0x10,0x04,0x00,0x00,0x10,0x04,0x00,0x01,0x55,0x55,0x40,0x05,0xA5,0x55,0x50,0x06,0x55,0x55,0x50,0x05,0x56,0x95,0x50,0x05,0x56,0x95,0x50,0x05,0x55,0x55,0x50,0x01,0x55,0x55,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 74 */
+ {0x00,0x00,0x00,0x00,0x00,0x03,0x00,0x00,0x00,0x16,0x50,0x00,0x01,0x69,0x69,0x40,0x05,0xA5,0x5A,0x50,0x16,0x95,0x56,0x94,0x25,0x96,0x59,0x68,0x20,0x82,0x08,0x08,0x00,0x02,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x02,0x08,0x00,0x00,0x00,0xA0,0x00,0x00,0x00,0x00,0x00}, /* 75 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xF0,0x0F,0xF0,0x3E,0xBC,0x3F,0xFC,0xFA,0xFF,0xFF,0xFF,0xFB,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x3F,0xFF,0xFF,0xFC,0x0F,0xFF,0xFF,0xF0,0x03,0xFF,0xFF,0xC0,0x00,0xFF,0xFF,0x00,0x00,0x3F,0xFC,0x00,0x00,0x0F,0xF0,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00}, /* 76 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0xFC,0x3F,0xC0,0x0F,0xAF,0xFF,0xF0,0x3E,0xBF,0xFF,0xFC,0x3E,0xFF,0xFF,0xFC,0x3F,0xFF,0xFF,0xFC,0x3F,0xFF,0xFF,0xFC,0x0F,0xFF,0xFF,0xF0,0x03,0xFF,0xFF,0xC0,0x00,0xFF,0xFF,0x00,0x00,0x3F,0xFC,0x00,0x00,0x0F,0xF0,0x00,0x00,0x03,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 77 */
+ {0x0A,0xA0,0xAA,0x00,0x01,0x50,0x54,0x00,0x0D,0x55,0x57,0x00,0x3F,0x55,0x5F,0xC0,0x3E,0x75,0xDB,0xC0,0x2A,0x95,0x6A,0x80,0x0A,0x9A,0x6A,0x00,0x00,0x2A,0x68,0x00,0x03,0xFF,0xF0,0x00,0x0A,0xAF,0xFA,0x00,0x3F,0xBF,0xAE,0x00,0x0F,0xEF,0xEE,0x00,0x00,0xEF,0xA8,0x00,0x05,0x55,0x54,0x00,0x00,0x15,0x50,0x00,0x00,0x00,0x00,0x00}, /* 78 */
+ {0x00,0x00,0x00,0x00,0x03,0xE0,0x0C,0x00,0x8F,0xE8,0x2F,0x10,0x97,0xA8,0xEF,0x10,0x95,0x68,0xEB,0xD0,0x95,0xD6,0xEE,0x94,0x05,0x5A,0xFF,0xD4,0x05,0x5A,0xFF,0xD4,0x95,0xD5,0xFB,0x94,0x95,0x6A,0xFA,0x94,0x97,0xAA,0x2F,0x90,0x8F,0xE8,0x2A,0x00,0x03,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 79 */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xF0,0x00,0x00,0x3F,0xFC,0x00,0x00,0x3F,0xFC,0x00,0x00,0x0F,0xF0,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x58,0xA0,0x00,0x05,0x5A,0xA5,0x6A,0x2F,0xDA,0xA5,0x5A,0x2F,0xFA,0x55,0x5A,0x0F,0xC3,0xC5,0x40}, /* 7A */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0F,0xCF,0xC0,0x0F,0x0C,0xCC,0xC0,0x03,0x0C,0xCC,0xC0,0x03,0x0C,0xCC,0xC0,0x0F,0xCF,0xCF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 7B */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xCF,0xCF,0xC0,0x00,0xCC,0xCC,0xC0,0x0F,0xCC,0xCC,0xC0,0x0C,0x0C,0xCC,0xC0,0x0F,0xCF,0xCF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 7C */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xCF,0xCF,0xC0,0x00,0xCC,0xCC,0xC0,0x03,0xCC,0xCC,0xC0,0x00,0xCC,0xCC,0xC0,0x0F,0xCF,0xCF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 7D */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xCF,0xCF,0xC0,0x0C,0x0C,0xCC,0xC0,0x0F,0xCC,0xCC,0xC0,0x00,0xCC,0xCC,0xC0,0x0F,0xCF,0xCF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 7E */
+ {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0xCF,0xCF,0xC0,0x0C,0xCC,0xCC,0xC0,0x0F,0xCC,0xCC,0xC0,0x0C,0xCC,0xCC,0xC0,0x0F,0xCF,0xCF,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, /* 7F */
+};
+static const uint8_t dk_spr_pal[91] = {PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_PAUL_TOP,PAL_PAUL_LOW,PAL_PAUL_LOW,PAL_PAUL_LOW,PAL_PAUL_LOW,PAL_BARREL,PAL_BARREL,PAL_BARREL,PAL_BARREL,PAL_BLUE,PAL_BLUE,PAL_BLUE,PAL_HAMMER,PAL_HAMMER,PAL_KFACE,PAL_KFACE,PAL_KFACE,PAL_KFACE,PAL_KFACE,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KFACE,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_KONG,PAL_DIZZY,PAL_BLACK,PAL_SPRING,PAL_SPRING,PAL_FIRE,PAL_FIRE,PAL_FLAME,PAL_FLAME,PAL_FLAME,PAL_FLAME,PAL_ELEVATOR,PAL_MOTOR,PAL_LADDER_WHITE,PAL_BLUE,PAL_PIE,PAL_PIE,PAL_FIRE,PAL_FIRE,PAL_PULLEY,PAL_PULLEY,PAL_PULLEY,PAL_SMASH,PAL_SMASH,PAL_SMASH,PAL_SMASH,PAL_ITEMS,PAL_ITEMS,PAL_ITEMS,PAL_HEART,PAL_HEART,PAL_MARIO,PAL_MARIO,PAL_MARIO,PAL_POINTS,PAL_POINTS,PAL_POINTS,PAL_POINTS,PAL_POINTS};
+static const uint8_t dk_spr_of[128] = {0,1,2,3,4,5,6,255,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,255,255,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,52,255,53,54,55,56,57,58,255,59,60,61,62,63,64,65,255,255,66,255,67,68,69,70,255,71,72,73,255,255,255,255,255,255,255,255,255,255,255,255,255,74,75,76,77,255,255,255,255,255,255,255,255,255,255,255,255,255,255,255,78,79,80,81,82,83,84,85,86,87,88,89,90};
+static const uint8_t dk_tile_girder[16] = {0x55,0x55,0xAA,0xAA,0x03,0xC0,0x0C,0x30,0x30,0x0C,0xFF,0xFF,0x55,0x55,0xAA,0xAA};
+static const uint8_t dk_tile_rivet_girder[16] = {0x55,0x55,0xAA,0xAA,0xBE,0xBE,0xFF,0xFF,0xFF,0xFF,0xBE,0xBE,0xAA,0xAA,0xAA,0xAA};
+static const uint8_t dk_tile_belt[16] = {0x55,0x55,0xAA,0xAA,0x80,0x20,0x80,0x20,0x80,0x20,0x80,0x20,0xAA,0xAA,0x55,0x55};
+static const uint8_t dk_tile_ladder[16] = {0x40,0x01,0x55,0x55,0x40,0x01,0x40,0x01,0x40,0x01,0x55,0x55,0x40,0x01,0x40,0x01};
+static const uint8_t dk_tile_rivet_top[16] = {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x50,0x15,0x54,0x15,0x54};
+static const uint8_t dk_tile_rivet_low[16] = {0xF5,0x5F,0xF5,0x5F,0xB5,0x5E,0xF5,0x5F,0xF5,0x5F,0xB5,0x5E,0xA5,0x5A,0xA5,0x5A};
+static const uint8_t dk_tile_cross[16] = {0x40,0x01,0x10,0x04,0x04,0x10,0x01,0x40,0x01,0x40,0x04,0x10,0x10,0x04,0x40,0x01};
+static const char dk_font_chars[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:=!?',<>/m()";
+static const uint8_t dk_font[50][8] = {
+ {0x3C,0x66,0x6E,0x7E,0x76,0x66,0x3C,0x00},
+ {0x18,0x38,0x18,0x18,0x18,0x18,0x3C,0x00},
+ {0x3C,0x66,0x06,0x1C,0x30,0x60,0x7E,0x00},
+ {0x3C,0x66,0x06,0x1C,0x06,0x66,0x3C,0x00},
+ {0x0C,0x1C,0x3C,0x6C,0x7E,0x0C,0x0C,0x00},
+ {0x7E,0x60,0x7C,0x06,0x06,0x66,0x3C,0x00},
+ {0x1C,0x30,0x60,0x7C,0x66,0x66,0x3C,0x00},
+ {0x7E,0x06,0x0C,0x18,0x30,0x30,0x30,0x00},
+ {0x3C,0x66,0x66,0x3C,0x66,0x66,0x3C,0x00},
+ {0x3C,0x66,0x66,0x3E,0x06,0x0C,0x38,0x00},
+ {0x3C,0x66,0x66,0x66,0x7E,0x66,0x66,0x00},
+ {0x7C,0x66,0x66,0x7C,0x66,0x66,0x7C,0x00},
+ {0x3C,0x66,0x60,0x60,0x60,0x66,0x3C,0x00},
+ {0x78,0x6C,0x66,0x66,0x66,0x6C,0x78,0x00},
+ {0x7E,0x60,0x60,0x7C,0x60,0x60,0x7E,0x00},
+ {0x7E,0x60,0x60,0x7C,0x60,0x60,0x60,0x00},
+ {0x3C,0x66,0x60,0x7E,0x66,0x66,0x3E,0x00},
+ {0x66,0x66,0x66,0x7E,0x66,0x66,0x66,0x00},
+ {0x3C,0x18,0x18,0x18,0x18,0x18,0x3C,0x00},
+ {0x1E,0x0C,0x0C,0x0C,0x0C,0x6C,0x38,0x00},
+ {0x66,0x6C,0x78,0x70,0x78,0x6C,0x66,0x00},
+ {0x60,0x60,0x60,0x60,0x60,0x60,0x7E,0x00},
+ {0x66,0x7E,0x7E,0x7E,0x66,0x66,0x66,0x00},
+ {0x66,0x66,0x76,0x7E,0x6E,0x66,0x66,0x00},
+ {0x3C,0x66,0x66,0x66,0x66,0x66,0x3C,0x00},
+ {0x7C,0x66,0x66,0x7C,0x60,0x60,0x60,0x00},
+ {0x3C,0x66,0x66,0x66,0x7E,0x6C,0x3E,0x00},
+ {0x7C,0x66,0x66,0x7C,0x78,0x6C,0x66,0x00},
+ {0x3E,0x60,0x60,0x3C,0x06,0x06,0x7C,0x00},
+ {0x7E,0x18,0x18,0x18,0x18,0x18,0x18,0x00},
+ {0x66,0x66,0x66,0x66,0x66,0x66,0x3C,0x00},
+ {0x66,0x66,0x66,0x66,0x66,0x3C,0x18,0x00},
+ {0x66,0x66,0x66,0x7E,0x7E,0x7E,0x3C,0x00},
+ {0x66,0x66,0x3C,0x18,0x3C,0x66,0x66,0x00},
+ {0x66,0x66,0x66,0x3C,0x18,0x18,0x18,0x00},
+ {0x7E,0x06,0x0C,0x18,0x30,0x60,0x7E,0x00},
+ {0x00,0x00,0x00,0x00,0x00,0x38,0x38,0x00},
+ {0x00,0x00,0x00,0x3C,0x00,0x00,0x00,0x00},
+ {0x00,0x38,0x38,0x00,0x38,0x38,0x00,0x00},
+ {0x00,0x00,0x3C,0x00,0x3C,0x00,0x00,0x00},
+ {0x18,0x18,0x18,0x18,0x18,0x00,0x18,0x00},
+ {0x3C,0x66,0x06,0x0C,0x18,0x00,0x18,0x00},
+ {0x18,0x18,0x00,0x00,0x00,0x00,0x00,0x00},
+ {0x00,0x00,0x00,0x00,0x00,0x18,0x18,0x30},
+ {0x0C,0x18,0x30,0x60,0x30,0x18,0x0C,0x00},
+ {0x60,0x30,0x18,0x0C,0x18,0x30,0x60,0x00},
+ {0x00,0x06,0x0C,0x18,0x30,0x60,0x00,0x00},
+ {0x00,0x00,0x7C,0x7E,0x7E,0x7E,0x7E,0x00},
+ {0x0C,0x18,0x30,0x30,0x30,0x18,0x0C,0x00},
+ {0x30,0x18,0x0C,0x0C,0x0C,0x18,0x30,0x00},
+};
+static const uint8_t dk_tile_char[256] = {1,2,3,4,5,6,7,8,9,10,0,0,0,0,0,0,0,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,0,39,0,45,46,0,0,40,0,0,0,41,41,43,0,0,0,0,0,0,0,37,44,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,0,0,0,0,0,0,1,2,3,4,5,6,7,8,9,10,48,48,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,42,0,0,0,0};
+static const uint8_t dk_help[3][8] = {{0x00,0x4B,0x4A,0x97,0xF4,0x97,0x00,0x00},{0x00,0xD0,0x10,0x21,0x21,0xBD,0x00,0x00},{0x00,0xE2,0x92,0xC4,0x04,0x04,0x00,0x08}};
+static const uint8_t dk_life[16] = {0x05,0x40,0x15,0x50,0x2F,0x80,0x3F,0xF0,0x09,0x80,0x29,0xA0,0x04,0x40,0x28,0xA0};
+static const uint8_t dk_goofy[384] = {0x00,0x00,0x00,0x00,0x00,0x15,0x54,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x55,0x55,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x05,0x55,0x55,0x50,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x15,0xAA,0xAA,0x54,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x15,0x16,0xAA,0xAA,0x94,0x54,0x00,0x00,0x00,0x00,0x00,0x01,0x55,0x5A,0xFA,0xAF,0xA5,0x55,0x40,0x00,0x00,0x00,0x00,0x05,0x55,0x56,0x7A,0xAD,0x95,0x55,0x50,0x00,0x00,0x00,0x00,0x15,0x55,0x56,0xAA,0xAA,0x95,0x55,0x54,0x00,0x00,0x00,0x00,0x55,0x55,0x56,0xA9,0x6A,0x95,0x55,0x55,0x00,0x00,0x00,0x00,0x55,0x55,0x5A,0xAA,0xAA,0xA5,0x55,0x55,0x00,0x00,0x00,0x01,0x55,0x55,0x59,0x55,0x55,0x65,0x55,0x55,0x40,0x00,0x00,0x01,0x55,0x55,0x5A,0xAA,0xAA,0xA5,0x55,0x55,0x40,0x00,0x00,0x01,0x55,0x54,0x56,0xAA,0xAA,0x95,0x15,0x55,0x40,0x00,0x00,0x01,0x55,0x54,0x15,0xAA,0xAA,0x54,0x15,0x55,0x40,0x00,0x00,0x01,0x55,0x50,0x05,0x55,0x55,0x50,0x05,0x55,0x40,0x00,0x00,0x01,0x55,0x50,0x00,0x55,0x55,0x00,0x05,0x55,0x40,0x00,0x00,0x01,0x55,0x50,0x01,0x6A,0xA9,0x40,0x05,0x55,0x40,0x00,0x00,0x0A,0xAA,0xA0,0x05,0xAA,0xAA,0x50,0x0A,0xAA,0xA0,0x00,0x00,0x0A,0xAA,0xA9,0x06,0xAA,0xAA,0x90,0x6A,0xAA,0xA0,0x00,0x00,0x0A,0x9A,0x69,0x46,0xA9,0x6A,0x91,0x69,0xA6,0xA0,0x00,0x00,0x01,0x55,0x55,0x46,0xAA,0xAA,0x91,0x55,0x55,0x40,0x00,0x00,0x05,0x55,0x55,0x45,0xAA,0xAA,0x51,0x55,0x55,0x50,0x00,0x00,0x05,0x55,0x55,0x01,0x6A,0xA9,0x40,0x55,0x55,0x50,0x00,0x00,0x05,0x55,0x54,0x01,0x5A,0xA5,0x40,0x15,0x55,0x50,0x00,0x00,0x05,0x55,0x50,0x01,0x56,0x95,0x40,0x05,0x55,0x50,0x00,0x00,0x05,0x55,0x40,0x01,0x55,0x55,0x40,0x01,0x55,0x50,0x00,0x00,0x05,0x55,0x00,0x01,0x55,0x55,0x40,0x00,0x55,0x50,0x00,0x00,0x05,0x55,0x00,0x00,0x54,0x15,0x00,0x00,0x55,0x50,0x00,0x00,0x05,0x55,0x00,0x00,0x50,0x05,0x00,0x00,0x55,0x50,0x00,0x00,0x2A,0xAA,0x80,0x00,0x00,0x00,0x00,0x02,0xAA,0xA8,0x00,0x00,0xAA,0xAA,0xA0,0x00,0x00,0x00,0x00,0x0A,0xAA,0xAA,0x00,0x00,0xA2,0xA2,0xA0,0x00,0x00,0x00,0x00,0x0A,0x8A,0x8A,0x00};
+#endif
diff --git a/games/donkeykong/src/assets.h b/games/donkeykong/src/assets.h
deleted file mode 100644
index 98ba6c33..00000000
--- a/games/donkeykong/src/assets.h
+++ /dev/null
@@ -1,23 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-only; generated by tools/assets.py */
-typedef struct {uint8_t w,h;const uint8_t *p;} dk_sprite;
-static const uint16_t palette[]={66,64168,65204,17403,65293,24996,43782,62828,65433,62261,30429,44031,58342,10665,48699,65535};
-static const uint8_t pixels_mario[]={0,0,17,17,16,0,0,0,17,17,17,17,0,0,5,85,34,80,0,0,5,37,82,34,82,0,0,82,85,34,34,82,0,5,82,34,37,85,0,0,0,34,34,34,0,0,0,17,49,16,0,0,0,17,19,17,49,16,0,17,17,51,51,17,16,2,33,52,52,49,34,0,34,35,51,51,34,32,0,3,51,51,51,0,0,0,51,48,51,48,0,0,85,80,0,85,80,0,85,85,0,5,85,80};
-static const dk_sprite spr_mario={13,16,pixels_mario};
-static const uint8_t pixels_kong[]={0,0,0,0,5,85,85,85,85,80,0,0,0,0,0,0,0,5,86,102,102,102,101,85,0,0,0,0,0,0,0,86,102,102,102,102,102,102,80,0,0,0,0,0,5,102,103,119,119,119,118,102,101,0,0,0,0,0,86,102,119,119,119,119,119,102,102,80,0,0,0,5,102,103,117,85,119,85,87,118,102,101,0,0,0,5,102,103,117,136,119,88,135,118,102,101,0,0,0,86,102,103,117,87,119,117,87,118,102,102,80,0,0,86,102,102,119,119,119,119,119,102,102,102,80,0,5,102,102,102,119,117,85,87,119,102,102,102,101,0,5,102,102,102,119,120,136,135,119,102,102,102,101,0,86,102,102,102,103,119,119,119,102,102,102,102,101,0,86,102,102,102,102,119,119,118,102,102,102,102,101,0,86,102,101,102,119,119,119,119,102,86,102,102,101,0,86,102,85,103,119,119,119,119,118,85,102,102,101,0,86,102,85,103,119,119,119,119,118,85,102,102,101,0,86,102,85,103,119,119,119,119,118,85,102,102,101,0,87,119,85,103,119,119,119,119,118,85,119,119,101,0,87,119,85,103,119,119,119,119,118,85,119,119,101,0,87,119,85,102,119,119,119,119,102,85,119,119,101,0,5,85,85,102,103,119,119,118,102,85,85,85,80,0,0,0,5,102,102,102,102,102,102,101,0,0,0,0,0,0,5,102,102,96,6,102,102,101,0,0,0,0,0,0,87,119,119,80,5,119,119,119,80,0,0,0,0,5,119,119,119,80,5,119,119,119,117,0,0,0};
-static const dk_sprite spr_kong={28,25,pixels_kong};
-static const uint8_t pixels_pauline[]={0,4,68,64,0,0,69,85,85,0,0,82,37,37,0,0,82,34,37,0,0,85,34,85,0,0,9,34,144,0,0,41,153,146,0,2,41,153,146,32,2,9,153,144,32,0,9,153,144,0,0,153,153,153,0,0,153,153,153,0,9,153,153,153,144,9,153,153,153,144,0,0,34,0,0,0,2,32,32,0,0,9,144,153,0,0,153,144,153,144};
-static const dk_sprite spr_pauline={10,18,pixels_pauline};
-static const uint8_t pixels_barrel[]={0,204,204,204,0,12,71,119,116,192,199,71,119,71,76,199,116,116,119,76,199,119,71,119,76,199,116,116,119,76,199,71,119,71,76,12,71,119,116,192,0,204,204,204,0};
-static const dk_sprite spr_barrel={10,9,pixels_barrel};
-static const uint8_t pixels_fire[]={0,0,16,0,0,0,1,16,1,0,0,12,16,193,0,1,12,204,193,0,1,204,204,193,0,12,204,204,204,16,204,204,204,204,193,204,196,204,76,193,204,200,204,140,193,12,196,68,204,16,0,204,76,193,0,0,192,0,192,0};
-static const dk_sprite spr_fire={10,12,pixels_fire};
-static const uint8_t pixels_tub[]={14,238,238,238,224,237,221,221,221,222,236,204,204,204,206,14,204,204,204,224,14,204,204,204,224,0,238,238,238,0,0,208,0,13,0};
-static const dk_sprite spr_tub={10,7,pixels_tub};
-static const uint8_t pixels_hammer[]={14,238,238,238,14,238,238,238,224,238,238,238,238,0,238,238,238,224,0,0,119,0,0,0,7,112,0,0,0,119,0,0,0,7,112,0,0,0,119,0,0};
-static const dk_sprite spr_hammer={9,9,pixels_hammer};
-static const uint8_t pixels_bag[]={0,4,68,0,0,4,0,4,0,9,153,153,153,9,153,153,153,153,153,148,148,153,153,153,73,73,153,153,153,153,153,144,153,153,153,144};
-static const dk_sprite spr_bag={9,8,pixels_bag};
-static const uint8_t pixels_hat[]={0,153,153,144,0,9,153,153,0,9,153,153,153,4,68,68,68,68,153,153,153,153,144};
-static const dk_sprite spr_hat={9,5,pixels_hat};
-static const uint8_t pixels_parasol[]={0,0,176,0,0,11,187,187,0,11,185,185,187,11,185,187,155,187,0,0,64,0,0,0,4,0,0,0,0,64,0,0,4,4,0,0,0,4,0,0,0};
-static const dk_sprite spr_parasol={9,9,pixels_parasol};
diff --git a/games/donkeykong/src/dk.h b/games/donkeykong/src/dk.h
new file mode 100644
index 00000000..4d51d430
--- /dev/null
+++ b/games/donkeykong/src/dk.h
@@ -0,0 +1,42 @@
+/* Donkey Kong (Nintendo, 1981): the arcade game's program, ported to C from the disassembly (sim.c).
+ *
+ * The game keeps all its state in the arcade's own memory layout: its 3 KB of work RAM (with the sprite
+ * table at 0x6900) and its 1 KB tile map. sim.c runs one frame of the original's 60 Hz vblank routine and
+ * foreground tasks per dk_tick(); draw.c paints the tile map and the sprite table with new pictures. */
+#ifndef DK_H
+#define DK_H
+#include <stdbool.h>
+#include <stdint.h>
+
+/* the joystick and the button, as the arcade reads them (IN0) */
+enum { DK_RIGHT = 1, DK_LEFT = 2, DK_UP = 4, DK_DOWN = 8, DK_JUMP = 16 };
+
+typedef struct {
+ uint8_t ram[0x1000]; /* 0x6000-0x6FFF (the arcade has 0x6000-0x6BFF) */
+ uint8_t vram[0x400]; /* 0x7400-0x77FF */
+ uint8_t palette; /* the palette bank: bit 0 REG_PALETTE_A, bit 1 REG_PALETTE_B */
+ uint8_t over; /* the game over box has been shown */
+ uint8_t pad[2];
+ uint32_t loop; /* drives RngTimer2 (the foreground loop's own counter in the arcade) */
+} dk_state;
+
+#define DK_RAM(s, a) ((s)->ram[((a) - 0x6000) & 0xFFF])
+#define DK_VRAM(s, a) ((s)->vram[((a) - 0x7400) & 0x3FF])
+
+void dk_new_game(dk_state *s, uint32_t seed);
+void dk_tick(dk_state *s, uint8_t joy);
+bool dk_valid(const dk_state *s);
+
+/* what the screens around the game read */
+uint32_t dk_score(const dk_state *s); /* the 6-digit BCD score, as a number */
+uint32_t dk_bcd(const uint8_t *p); /* 3 BCD bytes, low first */
+int dk_screen(const dk_state *s); /* 1 25m, 2 50m, 3 75m, 4 100m */
+int dk_mode(const dk_state *s); /* GameMode2 */
+int dk_level(const dk_state *s);
+int dk_lives(const dk_state *s);
+bool dk_alive(const dk_state *s);
+
+/* GameMode2 values the app looks at */
+enum { DK_MODE_INTRO = 7, DK_MODE_HOWHIGH = 8, DK_MODE_PLAY = 0x0C, DK_MODE_DEAD = 0x0D, DK_MODE_OVER = 0x10,
+ DK_MODE_CLEAR = 0x16 };
+#endif
diff --git a/games/donkeykong/src/draw.c b/games/donkeykong/src/draw.c
new file mode 100644
index 00000000..42e3b428
--- /dev/null
+++ b/games/donkeykong/src/draw.c
@@ -0,0 +1,479 @@
+/* Drawing: the arcade's tile map and sprite table with this port's pictures (art.h), in the launcher's strips.
+ *
+ * The play area is the arcade's, pixel for pixel: its 224 columns at x 96..319 and its rows 16 to 255 (the
+ * two rows above hold only 1UP and the scores). The panel on the left reads what the ROM's own display tasks
+ * wrote in the tile map's top rows (score, high score, lives, L=) and in its BONUS box. A strip is sent to the
+ * screen only where it changed. */
+#include <string.h>
+#include "../../../launcher/src/gfx.h"
+#include "draw.h"
+#include "art.h"
+#include "rom.h"
+
+#define PX 96 /* the play area's left edge */
+#define WHITE HEX(0xFFFFFF)
+#define INK HEX(0x000000)
+#define MUTED HEX(0x9AA3B5)
+#define FOCUS HEX(0xFFC21A)
+
+static const dk_state *G;
+static const dk_view *W;
+
+typedef struct {
+ uint32_t girder[3], ladder, beam[3], belt[2], rivet[2], line, cross, text, box, bonus, level;
+} bank_t;
+static const bank_t banks[4] = {
+ /* 0: the title */
+ {{0xFF2155, 0x970000, 0xFF2155}, 0x00FFFF, {0xFF2155, 0xFF2155, 0x970000}, {0, 0}, {0, 0}, 0x970000, 0, 0xFFFFFF,
+ 0x00FFFF, 0xFF47AA, 0x2020FF},
+ /* 1: 50m (and HOW HIGH CAN YOU GET?) */
+ {{0xFB7100, 0xFBBA99, 0xFB7100}, 0xFCFCFF, {0x00FFFF, 0x0000FF, 0}, {0xFB7100, 0xFBBA99}, {0xFFB600, 0x00FFFF},
+ 0xB70000, 0xFB7100, 0xFFFFFF, 0xFB7100, 0xFB7100, 0x2020FF},
+ /* 2: 25m and 75m */
+ {{0xFF2155, 0x970000, 0xFF2155}, 0x00FFFF, {0x00FFFF, 0x0000FF, 0}, {0xFB7100, 0xFBBA99}, {0xFFB600, 0x00FFFF},
+ 0xA40000, 0xFB7100, 0xFFFFFF, 0x00FFFF, 0xFF47AA, 0x2020FF},
+ /* 3: 100m */
+ {{0x0000FF, 0x00FFFF, 0x0000FF}, 0xFFB600, {0x00FFFF, 0x0000FF, 0}, {0xFB7100, 0xFBBA99}, {0xFFB600, 0x00FFFF},
+ 0x00FFFF, 0xFB7100, 0xFFFFFF, 0xFFB600, 0x2020FF, 0x2020FF},
+};
+
+static inline color_t *row_at(int y) { return gfx_buf + (y - gfx_y0) * SCREEN_W; }
+
+/* ---------------------------------------------------------------- letters in the arcade's style (8 px cells) */
+
+static void glyph_at(int x, int y, int index, color_t c, int scale) {
+ const uint8_t *g = dk_font[index];
+ for (int r = 0; r < 8; r++) {
+ for (int sy = 0; sy < scale; sy++) {
+ int yy = y + r * scale + sy;
+ if (yy < gfx_y0 || yy >= gfx_y1) continue;
+ color_t *row = row_at(yy);
+ for (int b = 0; b < 8; b++)
+ if (g[r] & 0x80 >> b)
+ for (int sx = 0; sx < scale; sx++) {
+ int xx = x + b * scale + sx;
+ if ((unsigned)xx < SCREEN_W) row[xx] = c;
+ }
+ }
+ }
+}
+
+static void atext(int x, int y, const char *s, color_t c, int scale) {
+ if (y >= gfx_y1 || y + 8 * scale <= gfx_y0) return;
+ for (; *s; s++, x += 8 * scale) {
+ const char *p = strchr(dk_font_chars, *s);
+ if (*s != ' ' && p) glyph_at(x, y, (int)(p - dk_font_chars), c, scale);
+ }
+}
+
+/* ---------------------------------------------------------------- tiles */
+
+static int bank(void) { return G ? G->palette & 3 : 0; }
+
+static void tile_rows(int x, int y, const uint8_t *pat, int row0, int row1, int shift, const color_t col[3]) {
+ /* pattern rows row0..row1-1 of an 8x8 2-bit pattern, drawn at tile rows row0+shift.. */
+ for (int r = row0; r < row1; r++) {
+ int yy = y + r + shift;
+ if (yy < gfx_y0 || yy >= gfx_y1) continue;
+ color_t *row = row_at(yy) + x;
+ uint8_t a = pat[r * 2], b = pat[r * 2 + 1];
+ uint16_t bits = (uint16_t)(a << 8 | b);
+ for (int k = 0; k < 8; k++) {
+ int v = bits >> (14 - 2 * k) & 3;
+ if (v) row[k] = col[v - 1];
+ }
+ }
+}
+
+static void tile(int x, int y, uint8_t code, int row) {
+ const bank_t *bk = &banks[bank()];
+ color_t g3[3] = {HEX(bk->girder[0]), HEX(bk->girder[1]), HEX(bk->girder[2])};
+ color_t lad[3] = {HEX(bk->ladder), HEX(bk->ladder), HEX(bk->ladder)};
+ if (code >= 0x50 && code <= 0x67) { /* the Kongs of HOW HIGH CAN YOU GET? */
+ int k = code - 0x50, cx = (5 - k / 4) * 8, cy = (3 - k % 4) * 8;
+ color_t kp[3] = {HEX(0xB81000), HEX(0xFFB855), HEX(0xFFFFFF)};
+ for (int r = 0; r < 8; r++) {
+ int yy = y + r;
+ if (yy < gfx_y0 || yy >= gfx_y1) continue;
+ color_t *out = row_at(yy) + x;
+ for (int c = 0; c < 8; c++) {
+ int px = cx + c, v = dk_goofy[(cy + r) * 12 + px / 4] >> (6 - 2 * (px & 3)) & 3;
+ if (v) out[c] = kp[v - 1];
+ }
+ }
+ return;
+ }
+ if (dk_tile_char[code]) {
+ color_t c = HEX(bk->text);
+ if (code >= 0x70 && code <= 0x79) c = HEX(0xFF2000);
+ glyph_at(x, y, dk_tile_char[code] - 1, c, 1);
+ return;
+ }
+ uint8_t hi = code & 0xF0, k = code & 0x0F;
+ if (k < 8 && (hi == 0xF0 || hi == 0xE0 || hi == 0xC0 || hi == 0xD0)) {
+ if (hi == 0xF0) tile_rows(x, y, dk_tile_girder, 0, 8 - k, k, g3);
+ else if (hi == 0xE0) tile_rows(x, y, dk_tile_girder, 8 - k, 8, k - 8, g3);
+ else if (hi == 0xC0) {
+ tile_rows(x, y, dk_tile_ladder, k, 8, 0, lad);
+ if (k) tile_rows(x, y, dk_tile_girder, 8 - k, 8, k - 8, g3);
+ } else {
+ tile_rows(x, y, dk_tile_ladder, 0, k, 0, lad);
+ tile_rows(x, y, dk_tile_girder, 0, 8 - k, k, g3);
+ }
+ return;
+ }
+ switch (code) {
+ case 0xB0: {
+ color_t b3[3] = {HEX(bk->beam[0]), HEX(bk->beam[1]), HEX(bk->beam[2])};
+ tile_rows(x, y, dk_tile_rivet_girder, 0, 8, 0, b3);
+ break;
+ }
+ case 0xB1:
+ case 0xB2:
+ case 0xB3: {
+ color_t b3[3] = {HEX(bk->belt[0]), HEX(bk->belt[1]), HEX(bk->belt[1])};
+ tile_rows(x, y, dk_tile_belt, 0, 8, 0, b3);
+ break;
+ }
+ case 0xB7:
+ case 0xB8: {
+ color_t r3[3] = {HEX(bk->rivet[0]), HEX(bk->rivet[1]), HEX(bk->beam[0])};
+ color_t b3[3] = {HEX(bk->beam[0]), HEX(bk->beam[1]), HEX(bk->beam[2])};
+ if (code == 0xB7) {
+ tile_rows(x, y, dk_tile_rivet_girder, 0, 8, 0, b3);
+ tile_rows(x, y, dk_tile_rivet_low, 0, 8, 0, r3);
+ } else {
+ tile_rows(x, y, dk_tile_rivet_top, 0, 8, 0, r3);
+ }
+ break;
+ }
+ case 0xFC:
+ case 0xFD: { /* a cable, or the bars by Kong on 100m: a line at the tile's edge */
+ int xx = code == 0xFC ? x + 7 : x;
+ if (row >= 0 && (unsigned)xx < SCREEN_W)
+ for (int r = 0; r < 8; r++)
+ if (y + r >= gfx_y0 && y + r < gfx_y1) row_at(y + r)[xx] = HEX(bk->line);
+ break;
+ }
+ case 0xFE: {
+ color_t c3[3] = {HEX(bk->cross), HEX(bk->cross), HEX(bk->cross)};
+ tile_rows(x, y, dk_tile_cross, 0, 8, 0, c3);
+ break;
+ }
+ case 0xDD:
+ case 0xDE:
+ case 0xDF:
+ case 0xED:
+ case 0xEE:
+ case 0xEF: {
+ const uint8_t *h = dk_help[(code & 0x0F) - 0x0D];
+ for (int r = 0; r < 8; r++) {
+ if (y + r < gfx_y0 || y + r >= gfx_y1) continue;
+ color_t *out = row_at(y + r) + x;
+ for (int b = 0; b < 8; b++)
+ if (h[r] & 0x80 >> b) out[b] = HEX(bk->text);
+ }
+ break;
+ }
+ }
+}
+
+/* the cells of the top rows and of the BONUS box: shown in the panel instead */
+static bool hud_cell(int col, int row) {
+ if (row <= 3) return row < 2 || (row == 3 && ((col >= 23 && col <= 28) || (col >= 5 && col <= 8)));
+ return row >= 5 && row <= 7 && col >= 3 && col <= 8;
+}
+
+static void tiles(const uint8_t *vram) {
+ int r0 = (gfx_y0 + 16) >> 3, r1 = (gfx_y1 + 16 + 7) >> 3;
+ if (r1 > 32) r1 = 32;
+ for (int r = r0; r < r1; r++)
+ for (int sc = 0; sc < 28; sc++) {
+ int col = 29 - sc;
+ uint8_t code = vram[col * 32 + r];
+ if (code == 0x10 || hud_cell(col, r)) continue;
+ tile(PX + sc * 8, r * 8 - 16, code, r);
+ }
+}
+
+/* ---------------------------------------------------------------- sprites */
+
+static void sprite(uint8_t x, uint8_t code, uint8_t color, uint8_t y) {
+ int sx = x + PX - 23, sy = y - 24;
+ if (sy >= gfx_y1 || sy + 16 <= gfx_y0) return;
+ uint8_t idx = dk_spr_of[code & 0x7F];
+ if (idx == 255) return;
+ int pal = dk_spr_pal[idx], cc = color & 0x0F;
+ uint8_t c7 = code & 0x7F;
+ if ((c7 == 0x3D || c7 == 0x3E || c7 == 0x4D || c7 == 0x4E) && cc == 0) pal = PAL_FIRE_BLUE;
+ if ((c7 == 0x1E || c7 == 0x1F) && cc == 1) pal = PAL_HAMMER_FLASH;
+ if (c7 == 0x46 && bank() != 1) pal = PAL_POINTS; /* (only on 50m) */
+ if (c7 == 0x46 && bank() == 1) pal = PAL_LADDER_WHITE;
+ const uint16_t *p = dk_pal[pal];
+ bool fx = code & 0x80, fy = color & 0x80;
+ const uint8_t *img = dk_spr[idx];
+ for (int r = 0; r < 16; r++) {
+ int yy = sy + r;
+ if (yy < gfx_y0 || yy >= gfx_y1) continue;
+ const uint8_t *src = img + (fy ? 15 - r : r) * 4;
+ color_t *out = row_at(yy);
+ for (int c = 0; c < 16; c++) {
+ int xx = sx + c;
+ if (xx < PX || xx >= SCREEN_W) continue;
+ int cx = fx ? 15 - c : c, v = src[cx >> 2] >> (6 - 2 * (cx & 3)) & 3;
+ if (v) out[xx] = p[v - 1];
+ }
+ }
+}
+
+static void sprites(const uint8_t *table) {
+ for (int i = 0; i < 0x60; i++) {
+ const uint8_t *s = table + i * 4;
+ if (s[0]) sprite(s[0], s[1], s[2], s[3]);
+ }
+}
+
+/* ---------------------------------------------------------------- the panel */
+
+static void digits_from(int x, int y, const uint16_t *cells, int n, int scale, color_t c) {
+ char buf[8];
+ for (int i = 0; i < n; i++) {
+ uint8_t v = DK_VRAM(G, cells[i]);
+ buf[i] = v <= 9 ? (char)('0' + v) : v >= 0x70 && v <= 0x79 ? (char)('0' + v - 0x70) : ' ';
+ }
+ buf[n] = 0;
+ atext(x, y, buf, c, scale);
+}
+
+static void keyline(int y) {
+ static const char *const keys[3][2] = {{"OK", "jump"}, {"arrows", "move"}, {"Back", "pause"}};
+ for (int i = 0; i < 3; i++) {
+ gfx_text(&np_font_small, 8, y + i * 15, keys[i][0], WHITE, 256);
+ gfx_text(&np_font_small, 8 + gfx_text_width(&np_font_small, keys[i][0]) + 5, y + i * 15, keys[i][1], MUTED, 256);
+ }
+}
+
+static void panel(void) {
+ if (gfx_y0 >= 240) return;
+ gfx_fill(0, gfx_y0, PX, gfx_y1 - gfx_y0, INK);
+ const bank_t *bk = &banks[bank()];
+ static const uint16_t score[6] = {0x7781, 0x7761, 0x7741, 0x7721, 0x7701, 0x76E1};
+ static const uint16_t high[6] = {0x7641, 0x7621, 0x7601, 0x75E1, 0x75C1, 0x75A1};
+ if (DK_VRAM(G, 0x7740) != 0x10) atext(8, 8, "1UP", HEX(0xFF2000), 1);
+ digits_from(8, 20, score, 6, 1, WHITE);
+ atext(8, 38, "HIGH", HEX(0xFF2000), 1);
+ atext(8, 48, "SCORE", HEX(0xFF2000), 1);
+ digits_from(8, 60, high, 6, 1, WHITE);
+ if (DK_VRAM(G, 0x7465) == 0x8D) { /* the BONUS box, as the board draws it */
+ color_t box = HEX(bk->box);
+ gfx_fill(6, 80, 84, 2, box);
+ gfx_fill(6, 116, 84, 2, box);
+ gfx_fill(6, 80, 2, 38, box);
+ gfx_fill(88, 80, 2, 38, box);
+ atext(28, 86, "BONUS", HEX(bk->bonus), 1);
+ static const uint16_t bonus[4] = {0x74E6, 0x74C6, 0x74A6, 0x7486};
+ bool red = DK_VRAM(G, 0x74C6) >= 0x70;
+ digits_from(16, 98, bonus, 4, 2, red ? HEX(0xFF2000) : WHITE);
+ }
+ if (DK_VRAM(G, 0x7503) == 0x1C) {
+ static const uint16_t lv[2] = {0x74C3, 0x74A3};
+ atext(8, 128, "L=", HEX(bk->level), 1);
+ digits_from(24, 128, lv, 2, 1, HEX(bk->level));
+ }
+ for (int i = 0; i < 6; i++) /* a little Mario for each life left */
+ if (DK_VRAM(G, 0x7783 - i * 0x20) == 0xFF) {
+ color_t lp[3] = {HEX(0xFF2010), HEX(0x2830FF), HEX(0xFFB855)};
+ int x = 8 + i * 12, y = 144;
+ for (int r = 0; r < 8; r++) {
+ if (y + r < gfx_y0 || y + r >= gfx_y1) continue;
+ color_t *out = row_at(y + r) + x;
+ for (int c = 0; c < 8; c++) {
+ int v = dk_life[r * 2 + c / 4] >> (6 - 2 * (c & 3)) & 3;
+ if (v) out[c] = lp[v - 1];
+ }
+ }
+ }
+ keyline(196);
+}
+
+/* ---------------------------------------------------------------- the title: the arcade's logo, Kong beating his chest */
+
+static uint8_t title_vram[0x400];
+static uint8_t title_spr[0x28];
+
+static void title_build(uint32_t clock) {
+ static bool built;
+ if (!built) {
+ memset(title_vram, 0x10, sizeof title_vram);
+ uint16_t hl = 0x3D08; /* 0x07F7: B0 tiles, a run down each column */
+ for (int guard = 0; guard < 64; guard++) {
+ uint8_t n = ROM(hl);
+ if (!n) break;
+ uint16_t de = ROM(hl + 1) | ROM(hl + 2) << 8;
+ for (int i = 0; i < n; i++) title_vram[(de + i - 0x7400) & 0x3FF] = 0xB0;
+ hl += 3;
+ }
+ built = true;
+ }
+ uint16_t pose = (clock / 16) & 1 ? 0x39CF : 0x39F7;
+ for (int i = 0; i < 0x28; i++) title_spr[i] = ROM(pose + i);
+ for (int i = 0; i < 10; i++) {
+ if (title_spr[i * 4]) title_spr[i * 4] = (uint8_t)(title_spr[i * 4] + 0x44);
+ title_spr[i * 4 + 3] = (uint8_t)(title_spr[i * 4 + 3] + 0x78);
+ }
+}
+
+static void title(void) {
+ gfx_fill(PX, gfx_y0, SCREEN_W - PX, gfx_y1 - gfx_y0, INK);
+ const dk_state *keep = G;
+ G = NULL; /* bank 0 */
+ tiles(title_vram);
+ for (int i = 0; i < 10; i++) {
+ const uint8_t *s = title_spr + i * 4;
+ if (s[0]) sprite(s[0], s[1], s[2], s[3]);
+ }
+ G = keep;
+ gfx_fill(0, gfx_y0, PX, gfx_y1 - gfx_y0, INK);
+ atext(8, 8, "HIGH", HEX(0xFF2000), 1);
+ atext(8, 18, "SCORE", HEX(0xFF2000), 1);
+ char b[12];
+ uint32_t v = W->best < 7650 ? 7650 : W->best;
+ for (int i = 5; i >= 0; i--) b[i] = (char)('0' + v % 10), v /= 10;
+ b[6] = 0;
+ atext(8, 30, b, WHITE, 1);
+ if (W->best_level) {
+ atext(8, 50, "BEST L=", HEX(0x2020FF), 1);
+ b[0] = (char)('0' + W->best_level / 10 % 10), b[1] = (char)('0' + W->best_level % 10), b[2] = 0;
+ atext(64, 50, b, HEX(0x2020FF), 1);
+ }
+ if ((W->clock / 32) & 1) atext(PX + 72, 200, "PRESS OK", WHITE, 1);
+ gfx_text(&np_font_small, 8, 196, "OK", WHITE, 256);
+ gfx_text(&np_font_small, 30, 196, "play", MUTED, 256);
+ gfx_text(&np_font_small, 8, 211, "Back", WHITE, 256);
+ gfx_text(&np_font_small, 39, 211, "quit", MUTED, 256);
+}
+
+/* ---------------------------------------------------------------- menus */
+
+static void card(int y, int h) {
+ gfx_fill(PX + 16, y, 192, h, HEX(0x0C1220));
+ gfx_fill(PX + 16, y, 192, 2, FOCUS);
+ gfx_fill(PX + 16, y + h - 2, 192, 2, FOCUS);
+ gfx_fill(PX + 16, y, 2, h, FOCUS);
+ gfx_fill(PX + 206, y, 2, h, FOCUS);
+}
+
+static void choice(int y, const char *s, bool on) {
+ if (on) gfx_fill(PX + 28, y - 13, 168, 18, FOCUS);
+ gfx_text_center(&np_font_body, PX + 112, y, s, on ? INK : WHITE, 256);
+}
+
+static void pause_menu(void) {
+ card(56, 128);
+ gfx_text_center(&np_font_title, PX + 112, 84, "PAUSED", WHITE, 256);
+ static const char *const items[4] = {"Resume", "New game", "How to play", "Quit game"};
+ for (int i = 0; i < 4; i++) choice(110 + i * 20, items[i], W->sel == i);
+}
+
+static void confirm_menu(void) {
+ card(76, 92);
+ gfx_text_center(&np_font_body, PX + 112, 102, W->confirm ? "Quit this game?" : "Start a new game?", WHITE, 256);
+ gfx_text_center(&np_font_small, PX + 112, 120, "Your score so far counts.", MUTED, 256);
+ choice(142, "No", W->sel == 0);
+ choice(160, "Yes", W->sel == 1);
+}
+
+static void over_menu(void) {
+ card(52, 136);
+ gfx_text_center(&np_font_title, PX + 112, 80, "GAME OVER", WHITE, 256);
+ char b[16];
+ uint32_t v = dk_score(G);
+ int n = 0;
+ do b[n++] = (char)('0' + v % 10), v /= 10;
+ while (v);
+ for (int i = 0; i < n / 2; i++) {
+ char t = b[i];
+ b[i] = b[n - 1 - i], b[n - 1 - i] = t;
+ }
+ b[n] = 0;
+ gfx_text_center(&np_font_title, PX + 112, 110, b, FOCUS, 256);
+ if (W->new_best) gfx_text_center(&np_font_body, PX + 112, 128, "New best!", WHITE, 256);
+ choice(152, "Play again", W->sel == 0);
+ choice(172, "Title", W->sel == 1);
+}
+
+static void guide(void) {
+ gfx_fill(0, gfx_y0, SCREEN_W, gfx_y1 - gfx_y0, INK);
+ gfx_text_center(&np_font_title, SCREEN_W / 2, 30, "How to play", WHITE, 256);
+ static const char *const lines[] = {
+ "Climb to Pauline. Donkey Kong throws barrels,",
+ "fires chase you, pies ride the belts,",
+ "springs bounce over the elevators.",
+ "On 100m, walk over all eight rivets.",
+ "Jump over a barrel or a fire for points,",
+ "grab a hammer to smash them (no jumping then).",
+ "Falling too far, or the bonus running out, costs a life.",
+ };
+ for (int i = 0; i < 7; i++) gfx_text_center(&np_font_small, SCREEN_W / 2, 58 + i * 16, lines[i], HEX(0xE6EBF2), 256);
+ static const char *const keys[4][2] = {
+ {"arrows", "walk, climb ladders"}, {"OK, EXE", "jump"}, {"Back", "pause"}, {"Home", "save and quit"}};
+ for (int i = 0; i < 4; i++) {
+ gfx_text_right(&np_font_body, 140, 184 + i * 14, keys[i][0], WHITE, 256);
+ gfx_text(&np_font_small, 152, 184 + i * 14, keys[i][1], MUTED, 256);
+ }
+ gfx_text_center(&np_font_small, SCREEN_W / 2, 236, "OK to go on", FOCUS, 256);
+}
+
+/* ---------------------------------------------------------------- the frame */
+
+static void scene(void *ctx) {
+ (void)ctx;
+ if (W->screen == DK_SCREEN_GUIDE) {
+ guide();
+ return;
+ }
+ if (W->screen == DK_SCREEN_TITLE) {
+ title();
+ return;
+ }
+ gfx_fill(PX, gfx_y0, SCREEN_W - PX, gfx_y1 - gfx_y0, INK);
+ tiles(G->vram);
+ sprites(&DK_RAM(G, 0x6900));
+ panel();
+ if (W->screen == DK_SCREEN_PAUSE) pause_menu();
+ if (W->screen == DK_SCREEN_CONFIRM) confirm_menu();
+ if (W->screen == DK_SCREEN_OVER) over_menu();
+}
+
+static uint32_t hashes[SCREEN_H / STRIP_H][10];
+static bool hashes_ok;
+static color_t span[SCREEN_W * STRIP_H];
+void dk_draw_invalidate(void) { hashes_ok = false; }
+
+void dk_draw(const dk_state *s, const dk_view *v) {
+ G = s, W = v;
+ if (v->screen == DK_SCREEN_TITLE) title_build(v->clock);
+ for (int strip = 0; strip < SCREEN_H / STRIP_H; strip++) {
+ gfx_y0 = strip * STRIP_H;
+ gfx_y1 = gfx_y0 + STRIP_H;
+ scene(0);
+ int first = -1, last = -1;
+ for (int seg = 0; seg < 10; seg++) {
+ uint32_t h = 2166136261u;
+ for (int y = 0; y < STRIP_H; y++) {
+ const color_t *p = gfx_buf + y * SCREEN_W + seg * 32;
+ for (int x = 0; x < 32; x++) h = (h ^ p[x]) * 16777619u;
+ }
+ if (!hashes_ok || hashes[strip][seg] != h) {
+ if (first < 0) first = seg;
+ last = seg;
+ }
+ hashes[strip][seg] = h;
+ }
+ if (first < 0) continue;
+ int x0 = first * 32, w = (last - first + 1) * 32;
+ for (int y = 0; y < STRIP_H; y++) memcpy(span + y * w, gfx_buf + y * SCREEN_W + x0, (size_t)w * sizeof(color_t));
+ np_push(x0, gfx_y0, w, STRIP_H, span);
+ }
+ hashes_ok = true;
+}
diff --git a/games/donkeykong/src/draw.h b/games/donkeykong/src/draw.h
new file mode 100644
index 00000000..e7f17acf
--- /dev/null
+++ b/games/donkeykong/src/draw.h
@@ -0,0 +1,17 @@
+/* The screens: the arcade's play area 1:1 (224 wide, its rows 16 to 255) right of a 96 px panel. */
+#ifndef DK_DRAW_H
+#define DK_DRAW_H
+#include "dk.h"
+
+enum { DK_SCREEN_TITLE, DK_SCREEN_GUIDE, DK_SCREEN_PLAY, DK_SCREEN_PAUSE, DK_SCREEN_CONFIRM, DK_SCREEN_OVER };
+
+typedef struct {
+ uint8_t screen, sel, confirm; /* confirm: 0 new game, 1 quit */
+ uint8_t new_best, has_run;
+ uint32_t best, games, clock;
+ uint16_t best_level;
+} dk_view;
+
+void dk_draw(const dk_state *s, const dk_view *v);
+void dk_draw_invalidate(void);
+#endif
diff --git a/games/donkeykong/src/font.h b/games/donkeykong/src/font.h
deleted file mode 100644
index 21da4cdf..00000000
--- a/games/donkeykong/src/font.h
+++ /dev/null
@@ -1,19 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-only; shared NumPlay 5x7 lettering. */
-static const uint8_t alphabet[][5] = {
- {0x7e,0x11,0x11,0x11,0x7e},{0x7f,0x49,0x49,0x49,0x36},{0x3e,0x41,0x41,0x41,0x22},
- {0x7f,0x41,0x41,0x22,0x1c},{0x7f,0x49,0x49,0x49,0x41},{0x7f,0x09,0x09,0x09,0x01},
- {0x3e,0x41,0x49,0x49,0x7a},{0x7f,0x08,0x08,0x08,0x7f},{0x00,0x41,0x7f,0x41,0x00},
- {0x20,0x40,0x41,0x3f,0x01},{0x7f,0x08,0x14,0x22,0x41},{0x7f,0x40,0x40,0x40,0x40},
- {0x7f,0x02,0x0c,0x02,0x7f},{0x7f,0x04,0x08,0x10,0x7f},{0x3e,0x41,0x41,0x41,0x3e},
- {0x7f,0x09,0x09,0x09,0x06},{0x3e,0x41,0x51,0x21,0x5e},{0x7f,0x09,0x19,0x29,0x46},
- {0x46,0x49,0x49,0x49,0x31},{0x01,0x01,0x7f,0x01,0x01},{0x3f,0x40,0x40,0x40,0x3f},
- {0x1f,0x20,0x40,0x20,0x1f},{0x3f,0x40,0x38,0x40,0x3f},{0x63,0x14,0x08,0x14,0x63},
- {0x07,0x08,0x70,0x08,0x07},{0x61,0x51,0x49,0x45,0x43},
- {0x3e,0x51,0x49,0x45,0x3e},{0x00,0x42,0x7f,0x40,0x00},{0x62,0x51,0x49,0x49,0x46},
- {0x22,0x41,0x49,0x49,0x36},{0x18,0x14,0x12,0x7f,0x10},{0x27,0x45,0x45,0x45,0x39},
- {0x3c,0x4a,0x49,0x49,0x30},{0x01,0x71,0x09,0x05,0x03},{0x36,0x49,0x49,0x49,0x36},
- {0x06,0x49,0x49,0x29,0x1e},
- {0,0x08,0x3e,0x08,0},{0,0x08,0x08,0x08,0},{0,0x60,0x60,0,0},
- {0,0x36,0x36,0,0},{0x20,0x10,0x08,0x04,0x02},{0,0,0x5f,0,0},
- {0x08,0x14,0x22,0x41,0},{0x41,0x22,0x14,0x08,0},{0,0,0x7f,0,0}
-};
diff --git a/games/donkeykong/src/game.c b/games/donkeykong/src/game.c
deleted file mode 100644
index 0e96823b..00000000
--- a/games/donkeykong/src/game.c
+++ /dev/null
@@ -1,384 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-only
- * Native arcade adaptation. Integer physics, bounded entity pools, no heap.
- * Layouts are redrawn for 224x224; this does not emulate the arcade ROM. */
-#include "game.h"
-#include <stdlib.h>
-#include <string.h>
-static int clamp(int x,int a,int b) {return x<a?a:x>b?b:x;}
-static uint32_t random32(dk_game *g) {
- uint32_t x=g->rng;x^=x<<13;x^=x>>17;x^=x<<5;return g->rng=x;
-}
-unsigned dk_difficulty(const dk_game *g) {return g->round>9?8:g->round-1;}
-static void platform(dk_game *g,int a,int b,int y,int z,int type,int dir) {
- g->platform[g->platform_count++]=(dk_platform){a,b,y,z,type,dir};
-}
-static void ladder(dk_game *g,int x,int lower,int upper,int broken,int retract) {
- g->ladder[g->ladder_count++]=(dk_ladder){x,lower,upper,broken,retract};
-}
-int dk_surface(const dk_game *g,unsigned index,int x) {
- if(index>=g->platform_count)return -1;
- const dk_platform *p=&g->platform[index];
- if(x<p->x1||x>p->x2)return -1;
- if(g->board==3&&index>=1&&index<=4) {
- unsigned bit=(index-1)*2;
- if((g->rivets&(1u<<bit))&&abs(x-48)<4)return -1;
- if((g->rivets&(2u<<bit))&&abs(x-176)<4)return -1;
- }
- return p->y1+(p->y2-p->y1)*(x-p->x1)/(p->x2-p->x1);
-}
-bool dk_ladder_open(const dk_game *g,unsigned index) {
- if(index>=g->ladder_count||g->ladder[index].broken)return false;
- return !g->ladder[index].retract||g->age%240<150;
-}
-int dk_belt_direction(const dk_game *g,const dk_platform *p) {
- return p->dir*((g->age/360)&1?-1:1);
-}
-static dk_enemy *spawn_enemy(dk_game *g,int type,int row,int x,int dir) {
- for(unsigned i=0;i<DK_ENEMIES;++i)if(!g->enemy[i].active) {
- dk_enemy *e=&g->enemy[i];memset(e,0,sizeof *e);
- e->active=1;e->type=type;e->row=row;e->x=x*DK_Q;e->dir=dir;e->ladder=-1;
- int y=dk_surface(g,(unsigned)row,x);e->y=(y<0?176:y)*DK_Q;e->old_x=e->x;
- return e;
- }
- return NULL;
-}
-static void load_board(dk_game *g) {
- g->age=0;g->bonus_ticks=0;g->spawn=100;g->throws=0;g->rivets=0;
- g->platform_count=g->ladder_count=0;g->phase=DK_READY;g->timer=65;
- g->bonus=(uint16_t)(g->round<5?4000+g->round*1000:8000);
- memset(g->enemy,0,sizeof g->enemy);memset(g->item,0,sizeof g->item);
- memset(&g->player,0,sizeof g->player);
- dk_player *p=&g->player;p->x=18*DK_Q;p->face=1;p->ladder=-1;
- p->grounded=1;p->support=0;p->jump_id=1;
- if(g->board==0) {
- platform(g,8,216,214,207,0,0);platform(g,8,216,177,184,0,0);
- platform(g,8,216,154,147,0,0);platform(g,8,216,117,124,0,0);
- platform(g,8,216,94,87,0,0);platform(g,8,216,57,64,0,0);
- platform(g,104,184,25,25,0,0);
- ladder(g,196,0,1,0,0);ladder(g,78,0,1,1,0);
- ladder(g,32,1,2,0,0);ladder(g,126,1,2,1,0);
- ladder(g,188,2,3,0,0);ladder(g,100,2,3,0,0);
- ladder(g,36,3,4,0,0);ladder(g,152,3,4,1,0);
- ladder(g,184,4,5,0,0);ladder(g,84,4,5,0,0);ladder(g,138,5,6,0,0);
- g->item[0]=(dk_item){58,dk_surface(g,1,58)-10,1,1};
- g->item[1]=(dk_item){160,dk_surface(g,3,160)-10,1,1};
- } else if(g->board==1) {
- platform(g,8,216,212,212,0,0);
- platform(g,8,98,176,176,1,1);platform(g,126,216,176,176,1,1);
- platform(g,8,216,140,140,0,0);
- platform(g,8,98,104,104,1,-1);platform(g,126,216,104,104,1,-1);
- platform(g,8,216,68,68,1,1);platform(g,128,208,28,28,0,0);
- ladder(g,24,0,1,0,0);ladder(g,200,0,2,0,0);
- ladder(g,56,1,3,0,0);ladder(g,166,2,3,0,0);
- ladder(g,28,3,4,0,0);ladder(g,196,3,5,0,0);
- ladder(g,54,4,6,0,1);ladder(g,172,5,6,0,1);ladder(g,184,6,7,0,0);
- g->item[0]=(dk_item){76,166,1,1};g->item[1]=(dk_item){150,94,1,1};
- g->item[2]=(dk_item){204,203,2,1};g->item[3]=(dk_item){100,131,3,1};
- g->item[4]=(dk_item){18,59,4,1};
- } else if(g->board==2) {
- platform(g,8,46,212,212,0,0);platform(g,146,216,212,212,0,0);
- platform(g,140,216,172,172,0,0);platform(g,140,216,132,132,0,0);
- platform(g,140,216,94,94,0,0);platform(g,8,82,55,55,0,0);
- platform(g,136,216,55,55,0,0);
- platform(g,8,42,168,168,0,0);platform(g,8,42,126,126,0,0);platform(g,8,42,84,84,0,0);
- platform(g,144,214,24,24,0,0);
- for(int i=0;i<3;++i)platform(g,56,80,212-i*52,212-i*52,2,-1);
- for(int i=0;i<3;++i)platform(g,104,128,56+i*52,56+i*52,2,1);
- ladder(g,200,1,2,0,0);ladder(g,160,2,3,0,0);ladder(g,198,3,4,0,0);
- ladder(g,156,4,6,0,0);ladder(g,188,6,10,0,0);
- g->item[0]=(dk_item){22,159,2,1};g->item[1]=(dk_item){200,123,3,1};
- g->item[2]=(dk_item){150,85,4,1};g->spawn=80;
- } else {
- for(int i=0;i<5;++i)platform(g,16,208,210-i*36,210-i*36,0,0);
- platform(g,144,208,28,28,0,0);
- for(int i=0;i<4;++i){ladder(g,28,i,i+1,0,0);ladder(g,196,i,i+1,0,0);}
- ladder(g,112,0,1,0,0);ladder(g,112,2,3,0,0);
- g->item[0]=(dk_item){150,164,1,1};g->item[1]=(dk_item){74,92,1,1};
- g->item[2]=(dk_item){84,201,2,1};g->item[3]=(dk_item){200,129,3,1};
- g->item[4]=(dk_item){60,57,4,1};
- spawn_enemy(g,DK_FIRE,1,142,-1);spawn_enemy(g,DK_FIRE,3,82,1);
- g->spawn=300;
- }
- p->y=dk_surface(g,0,p->x/DK_Q)*DK_Q;p->fall_from=p->y;
-}
-void dk_new(dk_game *g,uint32_t seed) {
- memset(g,0,sizeof *g);g->rng=seed?seed:0x1981a11u;g->round=1;g->lives=3;
- load_board(g);
-}
-static void points(dk_game *g,dk_stats *s,unsigned score,int x,int y) {
- if(score>DK_MAX_SCORE-g->score)g->score=DK_MAX_SCORE;else g->score+=score;
- if(g->score>s->best)s->best=g->score;
- if(!g->extra_life&&g->score>=7000){g->extra_life=1;if(g->lives<9)++g->lives;}
- if(x>=0){g->popup_x=(int16_t)x;g->popup_y=(int16_t)y;g->popup_time=50;g->popup_value=(uint16_t)score;}
-}
-static void die(dk_game *g,unsigned reason) {
- if(g->phase!=DK_RUNNING)return;
- g->phase=DK_DYING;g->timer=70;g->reason=(uint8_t)reason;g->player.hammer=0;
-}
-static void clear(dk_game *g,dk_stats *s) {
- points(g,s,g->bonus,-1,0);g->phase=DK_CLEAR;g->timer=125;
- if(g->board==3&&s->rescues<UINT32_MAX)++s->rescues;
-}
-static void update_lifts(dk_game *g) {
- if(g->board!=2)return;
- for(unsigned i=11;i<17;++i) {
- dk_platform *p=&g->platform[i];int old=p->y1;
- unsigned travel=(g->age*112u/DK_Q+(i-11)%3*52)%156;
- p->y1=p->y2=(int16_t)(p->dir<0?212-(int)travel:56+(int)travel);
- if(g->player.grounded&&g->player.support==(int)i) {
- int delta=p->y1-old;
- if(abs(delta)>4)g->player.grounded=0,g->player.support=-1;
- else g->player.y+=delta*DK_Q,g->player.fall_from+=delta*DK_Q;
- }
- }
-}
-static void move_player(dk_game *g,unsigned input) {
- dk_player *p=&g->player;int steer=!!(input&DK_RIGHT)-!!(input&DK_LEFT);
- int vertical=!!(input&DK_DOWN)-!!(input&DK_UP);
- if(p->hammer)--p->hammer;
- if((input&DK_JUMP)&&!(g->previous_input&DK_JUMP))p->jump_buffer=7;
- else if(p->jump_buffer)--p->jump_buffer;
- if(p->grounded)p->coyote=6;else if(p->coyote)--p->coyote;
- if(!p->hammer&&vertical&&p->ladder<0) {
- for(unsigned i=0;i<g->ladder_count;++i)if(dk_ladder_open(g,i)) {
- dk_ladder *l=&g->ladder[i];int top=dk_surface(g,l->upper,l->x),bottom=dk_surface(g,l->lower,l->x);
- if(top<0||bottom<0||abs(p->x-l->x*DK_Q)>5*DK_Q)continue;
- if((vertical<0&&abs(p->y-bottom*DK_Q)<=3*DK_Q)||
- (vertical>0&&abs(p->y-top*DK_Q)<=3*DK_Q)) {
- p->ladder=(int8_t)i;p->x=l->x*DK_Q;p->grounded=0;p->vy=0;p->jump_buffer=0;break;
- }
- }
- }
- if(p->ladder>=0) {
- dk_ladder *l=&g->ladder[(unsigned)p->ladder];
- int top=dk_surface(g,l->upper,l->x),bottom=dk_surface(g,l->lower,l->x);
- p->y+=vertical*200;
- if(p->y<=top*DK_Q){p->y=top*DK_Q;p->grounded=1;p->support=l->upper;p->ladder=-1;}
- else if(p->y>=bottom*DK_Q){p->y=bottom*DK_Q;p->grounded=1;p->support=l->lower;p->ladder=-1;}
- p->fall_from=p->y;return;
- }
- if(steer)p->face=(int8_t)steer;
- int belt=0;
- if(p->grounded&&p->support>=0) {
- dk_platform *floor=&g->platform[(unsigned)p->support];
- if(floor->type==DK_BELT)belt=dk_belt_direction(g,floor)*74;
- }
- p->x=clamp(p->x+steer*272+belt,6*DK_Q,218*DK_Q);
- if(p->jump_buffer&&p->coyote&&!p->hammer) {
- p->vy=-620;p->grounded=0;p->support=-1;p->jump_buffer=0;p->coyote=0;
- p->fall_from=p->y;p->air_points=0;if(++p->jump_id==0)++p->jump_id;
- }
- if(p->grounded) {
- int ground=dk_surface(g,(unsigned)p->support,p->x/DK_Q);
- if(ground>=0){p->y=ground*DK_Q;p->fall_from=p->y;p->vy=0;return;}
- p->grounded=0;p->support=-1;p->vy=0;
- }
- int old=p->y;p->vy=clamp(p->vy+40,-2000,1400);p->y+=p->vy;
- if(p->vy>=0) {
- int best=DK_H+32,index=-1;
- for(unsigned i=0;i<g->platform_count;++i) {
- int y=dk_surface(g,i,p->x/DK_Q);
- if(y>=0&&y<best&&old<=(y+2)*DK_Q&&p->y>=y*DK_Q)best=y,index=(int)i;
- }
- if(index>=0) {
- p->y=best*DK_Q;p->vy=0;p->support=(int8_t)index;p->grounded=1;
- if(p->y-p->fall_from>28*DK_Q)die(g,DK_FALL);
- p->fall_from=p->y;
- }
- }
- if(p->y>(DK_H+14)*DK_Q)die(g,DK_FALL);
-}
-static void barrel(dk_game *g,dk_enemy *e) {
- unsigned difficulty=dk_difficulty(g);
- if(e->mode) {
- e->vy=clamp(e->vy+48,0,1300);e->y+=e->vy;
- int target=dk_surface(g,(unsigned)e->row,clamp(e->x/DK_Q,8,216));
- if(target>=0&&e->y>=target*DK_Q){e->y=target*DK_Q;e->vy=0;e->mode=0;}
- return;
- }
- e->x+=e->dir*(260+(int)difficulty*14);
- dk_platform *p=&g->platform[(unsigned)e->row];
- if(e->x<=p->x1*DK_Q||e->x>=p->x2*DK_Q) {
- e->x=clamp(e->x,p->x1*DK_Q,p->x2*DK_Q);
- if(e->row==0) {
- if(e->blue)spawn_enemy(g,DK_FIRE,0,30,1);
- e->active=0;return;
- }
- --e->row;e->dir=-e->dir;e->mode=1;e->vy=0;return;
- }
- e->y=dk_surface(g,(unsigned)e->row,e->x/DK_Q)*DK_Q;
- if(!e->timer) {
- for(unsigned i=0;i<g->ladder_count;++i) {
- dk_ladder *l=&g->ladder[i];
- if(l->upper==e->row&&abs(e->x-l->x*DK_Q)<2*DK_Q&&
- (random32(g)%8<(e->blue?4:1+difficulty/3))) {
- e->row=l->lower;e->x=l->x*DK_Q;e->mode=1;e->vy=160;
- e->dir=e->row&1?1:-1;e->timer=30;break;
- }
- }
- }
-}
-static void fire(dk_game *g,dk_enemy *e) {
- int speed=58+(int)dk_difficulty(g)*8;
- if(e->ladder>=0) {
- dk_ladder *l=&g->ladder[(unsigned)e->ladder];
- int y=dk_surface(g,(unsigned)e->row,l->x);
- e->y+=(y*DK_Q>e->y?1:-1)*100;
- if(abs(e->y-y*DK_Q)<=100){e->y=y*DK_Q;e->ladder=-1;e->timer=50;}
- return;
- }
- if(!e->timer) {
- e->dir=g->player.x>e->x?1:-1;e->timer=(int16_t)(70+random32(g)%90);
- }
- int next=e->x+e->dir*speed;
- const dk_platform *p=&g->platform[(unsigned)e->row];
- if(next<(p->x1+6)*DK_Q||next>(p->x2-6)*DK_Q||dk_surface(g,(unsigned)e->row,next/DK_Q)<0)e->dir=-e->dir;
- else e->x=next;
- int floor=dk_surface(g,(unsigned)e->row,e->x/DK_Q);
- if(floor>=0)e->y=floor*DK_Q;
- if(e->timer<40)for(unsigned i=0;i<g->ladder_count;++i)if(dk_ladder_open(g,i)) {
- dk_ladder *l=&g->ladder[i];int target=-1;
- if(l->lower==e->row&&g->player.y<e->y-10*DK_Q)target=l->upper;
- if(l->upper==e->row&&g->player.y>e->y+10*DK_Q)target=l->lower;
- if(target>=0&&abs(e->x-l->x*DK_Q)<3*DK_Q) {
- e->x=l->x*DK_Q;e->row=(int8_t)target;e->ladder=(int8_t)i;break;
- }
- }
-}
-static void generate(dk_game *g) {
- if(g->spawn){--g->spawn;return;}
- unsigned d=dk_difficulty(g);
- if(g->board==0) {
- dk_enemy *e=spawn_enemy(g,DK_BARREL,5,62,1);
- if(e)e->blue=(++g->throws%5)==0;
- g->spawn=(uint16_t)(130-d*7+random32(g)%35);
- } else if(g->board==1) {
- int row=(g->throws++&1)?4:2;
- dk_platform *p=&g->platform[row];int dir=dk_belt_direction(g,p);
- spawn_enemy(g,DK_TUB,row,dir>0?p->x1+8:p->x2-8,dir);
- /* Both sides get traffic; the centre gap is crossed by climbing around it. */
- row=row==4?5:1;p=&g->platform[row];dir=dk_belt_direction(g,p);
- spawn_enemy(g,DK_TUB,row,dir>0?p->x1+8:p->x2-8,dir);
- if(g->throws%3==0)spawn_enemy(g,DK_FIRE,3,112,(random32(g)&1)?1:-1);
- g->spawn=(uint16_t)(210-d*10);
- } else if(g->board==2) {
- spawn_enemy(g,DK_SPRING,5,45,1);g->spawn=(uint16_t)(115-d*5+random32(g)%25);
- } else {
- unsigned count=0;for(unsigned i=0;i<DK_ENEMIES;++i)count+=g->enemy[i].active;
- if(count<5) {
- int row=1+(int)(random32(g)%4),dir=random32(g)&1?1:-1;
- spawn_enemy(g,DK_FIRE,row,dir>0?24:200,dir);
- }
- g->spawn=(uint16_t)(290-d*15);
- }
-}
-static void items(dk_game *g,dk_stats *s) {
- dk_player *p=&g->player;
- for(unsigned i=0;i<5;++i) {
- dk_item *v=&g->item[i];if(!v->active)continue;
- if(abs(p->x/DK_Q-v->x)<9&&p->y/DK_Q>v->y-3&&p->y/DK_Q<v->y+21) {
- if(v->type==1){if(p->ladder>=0)continue;p->hammer=480;}
- else points(g,s,300,v->x,v->y-8);
- v->active=0;
- }
- }
- if(g->board==3&&p->grounded&&p->support>=1&&p->support<=4) {
- int left=abs(p->x/DK_Q-48),right=abs(p->x/DK_Q-176);
- if(left<=3||right<=3) {
- unsigned bit=(unsigned)(p->support-1)*2+(right<left);
- if(!(g->rivets&(1u<<bit))) {
- g->rivets|=(uint8_t)(1u<<bit);points(g,s,100,p->x/DK_Q,p->y/DK_Q-22);
- if(g->rivets==255)clear(g,s);
- }
- }
- }
-}
-static void enemies(dk_game *g,dk_stats *s,int old_player_x) {
- dk_player *p=&g->player;
- for(unsigned i=0;i<DK_ENEMIES&&g->phase==DK_RUNNING;++i) {
- dk_enemy *e=&g->enemy[i];if(!e->active)continue;
- e->old_x=e->x;if(e->timer>0)--e->timer;
- if(e->type==DK_BARREL)barrel(g,e);
- else if(e->type==DK_FIRE)fire(g,e);
- else if(e->type==DK_TUB) {
- e->dir=(int8_t)dk_belt_direction(g,&g->platform[(unsigned)e->row]);
- e->x+=e->dir*(145+(int)dk_difficulty(g)*10);
- dk_platform *floor=&g->platform[(unsigned)e->row];
- if(e->x<(floor->x1+3)*DK_Q||e->x>(floor->x2-3)*DK_Q)e->active=0;
- } else {
- if(!e->mode) {
- e->x+=640+(int)dk_difficulty(g)*12;
- unsigned phase=(g->age+i*7)%30;
- int bump=(int)(phase*(30-phase))/15;
- e->y=(55-bump)*DK_Q;
- if(e->x>=207*DK_Q)e->mode=1,e->vy=0;
- } else e->vy+=60,e->y+=e->vy;
- if(e->y>235*DK_Q)e->active=0;
- }
- if(!e->active)continue;
- int dx=(e->x-p->x)/DK_Q,dy=(e->y-p->y)/DK_Q;
- if(p->hammer&&e->type!=DK_SPRING&&dx*p->face>=-4&&dx*p->face<19&&dy>-14&&dy<20) {
- e->active=0;points(g,s,300,e->x/DK_Q,e->y/DK_Q-16);continue;
- }
- if(!p->grounded&&p->ladder<0&&!p->hammer&&e->jump_id!=p->jump_id&&
- dy>=7&&dy<=30&&((old_player_x-e->old_x<0&&p->x-e->x>=0)||
- (old_player_x-e->old_x>0&&p->x-e->x<=0))) {
- e->jump_id=p->jump_id;if(p->air_points<3)++p->air_points;
- }
- /* Body collision, with a two-pixel forgiving margin on the sprite edges. */
- int height=e->type==DK_FIRE?11:8;
- if(abs(dx)<8&&dy>-height+2&&dy<12)die(g,DK_CRASH);
- }
-}
-void dk_step(dk_game *g,dk_stats *s,uint8_t input) {
- if(g->phase==DK_OVER)return;
- ++g->tick;if(g->popup_time)--g->popup_time;
- if(g->round>s->best_round)s->best_round=g->round;
- if(g->phase!=DK_RUNNING) {
- if(g->timer)--g->timer;
- if(!g->timer) {
- if(g->phase==DK_READY)g->phase=DK_RUNNING;
- else if(g->phase==DK_DYING) {
- if(g->lives)--g->lives;
- if(!g->lives)g->phase=DK_OVER;else load_board(g);
- } else if(g->phase==DK_CLEAR) {
- if(++g->board==4){g->board=0;if(g->round<999)++g->round;}
- load_board(g);
- }
- }
- g->previous_input=input;return;
- }
- ++g->age;update_lifts(g);
- int old_x=g->player.x;uint8_t was_grounded=g->player.grounded;
- move_player(g,input);
- if(g->phase==DK_RUNNING) {
- items(g,s);
- if(g->phase==DK_RUNNING){generate(g);enemies(g,s,old_x);}
- if(!was_grounded&&g->player.grounded&&g->player.air_points) {
- points(g,s,g->player.air_points==1?100:g->player.air_points==2?300:500,
- g->player.x/DK_Q,g->player.y/DK_Q-21);g->player.air_points=0;
- }
- if(g->phase==DK_RUNNING) {
- int x=g->player.x/DK_Q,y=g->player.y/DK_Q;
- if(g->board!=3&&y<=29&&x>=132&&g->player.grounded)clear(g,s);
- if(g->board==1&&abs(x-112)<10&&y>153&&y<184)die(g,DK_CRASH);
- /* Kong rides harmlessly on the conveyor board, as in the arcade game. */
- if(g->board!=1) {
- int kx=g->board==3?112:34,ky=g->board==0?58:g->board==2?55:66;
- if(abs(x-kx)<15&&y>ky-26&&y<ky+9)die(g,DK_CRASH);
- }
- if(++g->bonus_ticks>=90){g->bonus_ticks=0;if(g->bonus>=100)g->bonus-=100;if(!g->bonus)die(g,DK_TIME);}
- }
- }
- g->previous_input=input;
-}
-bool dk_valid(const dk_game *g) {
- if(!g->round||g->round>999||g->board>3||g->phase>DK_OVER||g->lives>9||
- g->score>DK_MAX_SCORE||g->platform_count>DK_PLATFORMS||g->ladder_count>DK_LADDERS||
- g->player.x<0||g->player.x>DK_W*DK_Q||g->player.y< -64*DK_Q||g->player.y>512*DK_Q)return false;
- if(g->player.support< -1||g->player.support>=g->platform_count||
- g->player.ladder< -1||g->player.ladder>=g->ladder_count)return false;
- for(unsigned i=0;i<DK_ENEMIES;++i)if(g->enemy[i].active&&
- (g->enemy[i].type<1||g->enemy[i].type>4||g->enemy[i].row<0||g->enemy[i].row>=g->platform_count))return false;
- return true;
-}
diff --git a/games/donkeykong/src/game.h b/games/donkeykong/src/game.h
deleted file mode 100644
index 46ff5c27..00000000
--- a/games/donkeykong/src/game.h
+++ /dev/null
@@ -1,62 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-only */
-#ifndef DK_GAME_H
-#define DK_GAME_H
-#include <stdbool.h>
-#include <stddef.h>
-#include <stdint.h>
-#define DK_Q 256
-#define DK_W 224
-#define DK_H 224
-#define DK_PLATFORMS 20
-#define DK_LADDERS 18
-#define DK_ENEMIES 16
-#define DK_SAVE_BYTES 32
-#define DK_MAX_SCORE 999999u
-enum { DK_LEFT=1, DK_RIGHT=2, DK_UP=4, DK_DOWN=8, DK_JUMP=16 };
-enum { DK_READY, DK_RUNNING, DK_DYING, DK_CLEAR, DK_OVER };
-enum { DK_GIRDER, DK_BELT, DK_LIFT };
-enum { DK_BARREL=1, DK_FIRE, DK_TUB, DK_SPRING };
-enum { DK_CRASH, DK_FALL, DK_TIME };
-typedef struct { int16_t x1,x2,y1,y2; int8_t type,dir; } dk_platform;
-typedef struct { int16_t x; uint8_t lower,upper,broken,retract; } dk_ladder;
-typedef struct {
- int32_t x,y,vy,fall_from;
- int8_t support,ladder,face;
- uint8_t grounded,coyote,jump_buffer,air_points;
- uint16_t hammer,jump_id;
-} dk_player;
-typedef struct {
- int32_t x,y,old_x,vy;
- int16_t timer;
- uint16_t jump_id;
- int8_t row,dir,ladder;
- uint8_t type,mode,blue,active;
-} dk_enemy;
-typedef struct { int16_t x,y; uint8_t type,active; } dk_item;
-typedef struct {
- uint32_t best,runs,rescues;
- uint16_t best_round;
-} dk_stats;
-typedef struct {
- uint32_t score,rng,tick,age;
- uint16_t round,bonus,timer,bonus_ticks,spawn,throws;
- uint16_t popup_time,popup_value;
- int16_t popup_x,popup_y;
- uint8_t board,phase,lives,extra_life,rivets,previous_input,reason;
- uint8_t platform_count,ladder_count;
- dk_player player;
- dk_platform platform[DK_PLATFORMS];
- dk_ladder ladder[DK_LADDERS];
- dk_enemy enemy[DK_ENEMIES];
- dk_item item[5];
-} dk_game;
-void dk_new(dk_game *,uint32_t seed);
-void dk_step(dk_game *,dk_stats *,uint8_t input);
-int dk_surface(const dk_game *,unsigned platform,int x);
-bool dk_ladder_open(const dk_game *,unsigned ladder);
-int dk_belt_direction(const dk_game *,const dk_platform *);
-unsigned dk_difficulty(const dk_game *);
-bool dk_valid(const dk_game *);
-void dk_encode_stats(const dk_stats *,uint8_t out[DK_SAVE_BYTES]);
-bool dk_decode_stats(dk_stats *,const uint8_t *,size_t);
-#endif
diff --git a/games/donkeykong/src/main.c b/games/donkeykong/src/main.c
index 98608707..0712d728 100644
--- a/games/donkeykong/src/main.c
+++ b/games/donkeykong/src/main.c
@@ -1,114 +1,200 @@
-/* SPDX-License-Identifier: GPL-3.0-only */
-#include <eadk.h>
-#include "render.h"
-#include "../../common/epsilon_app.h"
-#include "../../common/epsilon_files.h"
-#ifdef __ELF__
-const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name")))="Donkey Kong";
-const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level")))=0;
+/* Donkey Kong (Nintendo, 1981) on the NumWorks calculator.
+ *
+ * Kong has taken Pauline to the top of a building site: Mario (Jumpman) climbs after her over the 25m
+ * girders and their barrels, the 50m conveyor belts and pies, the 75m elevators and springs, and the 100m
+ * rivets that bring Kong down. The arcade's program is ported routine by routine (sim.c) and runs 60 times a
+ * second, as the arcade did; the pictures are new (art.h).
+ *
+ * Keys: the arrows walk and climb, OK or EXE jump, Back pauses, Home saves and quits. */
+#include <string.h>
+#include "../../../launcher/src/gfx.h"
+#include "dk.h"
+#include "draw.h"
+#include "plat.h"
+#include "save.h"
+
+static dk_state run;
+static dk_profile prof;
+static dk_view view;
+static uint32_t rng;
+static uint8_t back_to, last_dir, shown_mode;
+static color_t strip[SCREEN_W * STRIP_H];
+/* frames drawn, the slowest (ms), those over 33 ms (read by tools/emu.py), and the game's 60 Hz steps */
+#ifndef HOST
+__attribute__((used, externally_visible))
#endif
-#define KEY(k) (UINT64_C(1)<<(k))
-static dk_game game;
-static dk_stats stats;
-static uint8_t save_data[DK_SAVE_BYTES];
-static bool have_save,save_failed;
-static uint32_t last_checksum;
-uint32_t perf_frames,perf_max,perf_slow;
-static void push(int x,int y,int w,int h,const uint16_t *p) {
- eadk_display_push_rect((eadk_rect_t){x,y,w,h},p);
+uint32_t perf_frames, perf_max, perf_slow, perf_ticks;
+
+static uint32_t next_rng(void) {
+ rng ^= rng << 13, rng ^= rng >> 17, rng ^= rng << 5;
+ return rng;
}
-static bool save(void) {
- dk_encode_stats(&stats,save_data);
- uint32_t hash=save_data[28]|(uint32_t)save_data[29]<<8|(uint32_t)save_data[30]<<16|(uint32_t)save_data[31]<<24;
- if(have_save&&hash==last_checksum&&!save_failed)return true;
- save_failed=!ef_write("donkeykong.sav",save_data,DK_SAVE_BYTES);
- if(!save_failed)have_save=true,last_checksum=hash;
- return !save_failed;
+
+static void best_of_run(void) {
+ uint32_t s = dk_score(&run);
+ view.new_best = s > prof.best;
+ if (s > prof.best) prof.best = s;
}
-static void fresh(uint32_t seed) {
- if(stats.runs<UINT32_MAX)++stats.runs;
- dk_new(&game,seed^(stats.runs*0x9E3779B9u));
+
+static void new_game(void) {
+ dk_new_game(&run, next_rng());
+ uint32_t b = prof.best > 7650 ? prof.best : 7650; /* the arcade's table starts at 7650 */
+ for (int i = 0; i < 3; i++, b /= 100) DK_RAM(&run, 0x60B8 + i) = (uint8_t)((b % 100 / 10) << 4 | b % 10);
+ prof.has_run = 1;
+ if (prof.games < 0xFFFFFFFFu) prof.games++;
+ view.screen = DK_SCREEN_PLAY;
+ shown_mode = 0;
}
+
+static void end_game(void) {
+ best_of_run();
+ prof.has_run = 0;
+ save_write(&prof, 0);
+}
+
+/* the joystick: four ways like the arcade's, the latest arrow pressed winning */
+static uint8_t joystick(uint32_t live, uint32_t hit) {
+ static const uint32_t keys[4] = {JK_RIGHT, JK_LEFT, JK_UP, JK_DOWN};
+ for (int i = 0; i < 4; i++)
+ if (hit & keys[i]) last_dir = (uint8_t)(1 << i);
+ bool held = false;
+ for (int i = 0; i < 4; i++)
+ if (last_dir == 1 << i && (live & keys[i])) held = true;
+ if (!held) {
+ last_dir = 0;
+ for (int i = 0; i < 4 && !last_dir; i++)
+ if (live & keys[i]) last_dir = (uint8_t)(1 << i);
+ }
+ return (uint8_t)(last_dir | ((live & JK_OK) ? DK_JUMP : 0));
+}
+
+static void track(void) { /* totals: boards cleared, the highest level */
+ int m = dk_mode(&run);
+ if (m == DK_MODE_CLEAR && shown_mode != DK_MODE_CLEAR && prof.boards < 0xFFFFFFFFu) prof.boards++;
+ shown_mode = (uint8_t)m;
+ if (dk_level(&run) > prof.best_level) prof.best_level = (uint16_t)dk_level(&run);
+}
+
+static void sync_view(void) {
+ view.best = prof.best, view.games = prof.games, view.best_level = prof.best_level, view.has_run = prof.has_run;
+}
+
int main(void) {
- np_app_begin();
- uint32_t length=0;const uint8_t *data=ef_read("donkeykong.sav",&length);
- dk_decode_stats(&stats,data,length);dk_new(&game,1981);
- bool ongoing=false;
- unsigned view=DK_TITLE,selected=0,error_return=DK_TITLE,error_selection=0;
- uint64_t previous=eadk_keyboard_scan(),stale=previous;
- uint32_t now=(uint32_t)eadk_timing_millis(),last=now,draw_at=now,save_at=now,accum=0;
- dk_invalidate();
- for(;;) {
- now=(uint32_t)eadk_timing_millis();uint32_t elapsed=now-last;last=now;
- uint64_t keys=eadk_keyboard_scan();stale&=keys;
- uint64_t active=keys&~stale,hit=active&~previous;previous=active;
- bool ok=(hit&(KEY(eadk_key_ok)|KEY(eadk_key_exe)))!=0;
- bool back=(hit&KEY(eadk_key_back))!=0;
- bool next=(hit&(KEY(eadk_key_down)|KEY(eadk_key_right)))!=0;
- bool prev=(hit&(KEY(eadk_key_up)|KEY(eadk_key_left)))!=0;
- if(view!=DK_SAVE_ERROR&&(keys&(KEY(eadk_key_home)|KEY(eadk_key_on_off)))) {
- if(save())break;
- error_return=view==DK_PLAY?DK_PAUSE:view;error_selection=selected;
- view=DK_SAVE_ERROR;ok=back=next=prev=false;stale=keys;
+ plat_begin();
+ gfx_buf = strip;
+ save_load(&prof, &run);
+ rng = plat_random() ^ prof.games * 0x9E3779B9u;
+ if (!rng) rng = 1;
+ view.screen = prof.has_run ? DK_SCREEN_PAUSE : DK_SCREEN_TITLE;
+ uint32_t stale = plat_keys(), prev = stale, last = plat_millis(), acc = 0, saved_at = last;
+ uint8_t shown = 0xff;
+ bool redraw = true;
+ dk_draw_invalidate();
+ for (;;) {
+ uint32_t now = plat_millis(), keys = plat_keys();
+ stale &= keys; /* keys still down from the screen before don't count */
+ uint32_t live = keys & ~stale, hit = live & ~prev;
+ prev = live;
+ if (keys & JK_HOME) {
+ if (view.screen == DK_SCREEN_OVER || !prof.has_run) save_write(&prof, 0);
+ else save_write(&prof, &run);
+ break;
}
- if(view==DK_PLAY) {
- if(back)view=DK_PAUSE,selected=0,accum=0,save();
- else {
- unsigned input=0;
- if(active&(KEY(eadk_key_left)|KEY(eadk_key_four)))input|=DK_LEFT;
- if(active&(KEY(eadk_key_right)|KEY(eadk_key_six)))input|=DK_RIGHT;
- if(active&(KEY(eadk_key_up)|KEY(eadk_key_eight)))input|=DK_UP;
- if(active&(KEY(eadk_key_down)|KEY(eadk_key_two)))input|=DK_DOWN;
- if(active&(KEY(eadk_key_ok)|KEY(eadk_key_exe)|KEY(eadk_key_shift)))input|=DK_JUMP;
- if(elapsed>66)elapsed=66;
- accum+=elapsed*60;
- while(accum>=1000)dk_step(&game,&stats,(uint8_t)input),accum-=1000;
- if(game.phase==DK_OVER)view=DK_RESULT,selected=0,ongoing=false,save();
- if(now-save_at>=5000)save(),save_at=now;
+ uint32_t dt = now - last;
+ last = now;
+ if (dt > 100) dt = 100;
+ /* 60ths of a second gone by: the game steps that many times, 4 at most (slower rather than skipping) */
+ acc += dt * 60;
+ int ticks = 0;
+ while (acc >= 1000 && ticks < 4) acc -= 1000, ticks++;
+ if (acc >= 1000) acc = 0;
+ int steps = 0;
+ switch (view.screen) {
+ case DK_SCREEN_TITLE:
+ view.clock += (uint32_t)ticks;
+ redraw |= ticks > 0;
+ if (hit & JK_BACK) {
+ save_write(&prof, 0);
+ return plat_end();
}
- } else {
- accum=0;
- if(view==DK_TITLE) {
- if(next)selected=(selected+1)%3;
- if(prev)selected=(selected+2)%3;
- if(back||(ok&&selected==2)) {
- if(save())break;
- error_return=DK_TITLE;error_selection=selected;view=DK_SAVE_ERROR;
- } else if(ok&&selected==0) {
- if(!ongoing)fresh(now),ongoing=true;
- game.previous_input=DK_JUMP;stale|=keys;view=DK_PLAY;
- } else if(ok&&selected==1)view=DK_HELP;
- } else if(view==DK_HELP) {
- if(ok||back)view=DK_TITLE,selected=1;
- } else if(view==DK_PAUSE) {
- if(next)selected=(selected+1)%3;
- if(prev)selected=(selected+2)%3;
- if(back||(ok&&selected==0))view=DK_PLAY,stale|=keys;
- else if(ok&&selected==1)view=DK_CONFIRM,selected=0;
- else if(ok&&selected==2)view=DK_TITLE,selected=0;
- } else if(view==DK_CONFIRM) {
- if(next||prev)selected=!selected;
- if(back||(ok&&selected==0))view=DK_PAUSE,selected=1;
- else if(ok)fresh(now),view=DK_PLAY,selected=0,stale|=keys;
- } else if(view==DK_RESULT) {
- if(next||prev)selected=!selected;
- if(back||(ok&&selected))view=DK_TITLE,selected=0;
- else if(ok)fresh(now),view=DK_PLAY,selected=0,ongoing=true,stale|=keys;
- } else if(view==DK_SAVE_ERROR) {
- if(back)break;
- if(ok&&save())break;
- if(hit&KEY(eadk_key_one))view=error_return,selected=error_selection;
+ if (hit & JK_OK) {
+ if (!prof.guide_seen) back_to = DK_SCREEN_PLAY, view.screen = DK_SCREEN_GUIDE;
+ else new_game();
+ }
+ break;
+ case DK_SCREEN_GUIDE:
+ if (hit & (JK_OK | JK_BACK)) {
+ prof.guide_seen = 1;
+ if (back_to == DK_SCREEN_PLAY) new_game();
+ else view.screen = back_to, view.sel = 2;
+ }
+ break;
+ case DK_SCREEN_PLAY:
+ if (hit & JK_BACK) {
+ view.screen = DK_SCREEN_PAUSE, view.sel = 0;
+ save_write(&prof, &run), saved_at = now;
+ break;
}
+ for (; steps < ticks; steps++) {
+ dk_tick(&run, joystick(live, steps ? 0 : hit));
+ perf_ticks++;
+ track();
+ }
+ if (run.over) {
+ end_game();
+ view.screen = DK_SCREEN_OVER, view.sel = 0;
+ } else if (now - saved_at > 10000) {
+ save_write(&prof, &run), saved_at = now;
+ }
+ break;
+ case DK_SCREEN_PAUSE:
+ if (hit & JK_UP) view.sel = (uint8_t)((view.sel + 3) % 4);
+ if (hit & JK_DOWN) view.sel = (uint8_t)((view.sel + 1) % 4);
+ if (hit & JK_BACK) view.screen = DK_SCREEN_PLAY, acc = 0;
+ else if (hit & JK_OK) {
+ if (view.sel == 0) view.screen = DK_SCREEN_PLAY, acc = 0;
+ else if (view.sel == 2) back_to = DK_SCREEN_PAUSE, view.screen = DK_SCREEN_GUIDE;
+ else view.confirm = view.sel == 3, view.screen = DK_SCREEN_CONFIRM, view.sel = 0;
+ }
+ break;
+ case DK_SCREEN_CONFIRM:
+ if (hit & (JK_UP | JK_DOWN)) view.sel ^= 1;
+ if ((hit & JK_BACK) || ((hit & JK_OK) && view.sel == 0)) {
+ view.sel = view.confirm ? 3 : 1, view.screen = DK_SCREEN_PAUSE;
+ } else if (hit & JK_OK) {
+ end_game();
+ if (view.confirm) view.screen = DK_SCREEN_TITLE, view.sel = 0;
+ else new_game();
+ }
+ break;
+ case DK_SCREEN_OVER:
+ if (hit & (JK_UP | JK_DOWN)) view.sel ^= 1;
+ if (hit & JK_BACK) view.screen = DK_SCREEN_TITLE;
+ else if (hit & JK_OK) {
+ if (view.sel == 0) new_game();
+ else view.screen = DK_SCREEN_TITLE;
+ }
+ break;
+ }
+ if (view.screen != shown) {
+ stale = keys, prev = 0; /* a new screen starts with all keys up */
+ shown = view.screen, redraw = true;
+ last_dir = 0;
}
- if(now-draw_at>=16||hit||!perf_frames) {
- uint32_t start=(uint32_t)eadk_timing_millis();
- dk_render(&game,&stats,view,selected,ongoing,save_failed,now,push);
- uint32_t spent=(uint32_t)eadk_timing_millis()-start;
- if(spent>perf_max)perf_max=spent;
- if(spent>17)++perf_slow;
- ++perf_frames;draw_at=now;
+ if (steps || redraw || hit) {
+ sync_view();
+ uint32_t t0 = plat_millis();
+ dk_draw(&run, &view);
+ uint32_t spent = plat_millis() - t0;
+ if (view.screen == DK_SCREEN_PLAY) {
+ perf_frames++;
+ if (spent > perf_max) perf_max = spent;
+ if (spent > 33) perf_slow++;
+ }
+ redraw = false;
+ } else {
+ plat_sleep(2);
}
- eadk_timing_msleep(2);
}
- return np_app_end();
+ return plat_end();
}
diff --git a/games/donkeykong/src/plat.h b/games/donkeykong/src/plat.h
new file mode 100644
index 00000000..175b8e89
--- /dev/null
+++ b/games/donkeykong/src/plat.h
@@ -0,0 +1,16 @@
+/* What the game needs from the calculator (plat_eadk.c), or from a computer for the tests. */
+#ifndef DK_PLAT_H
+#define DK_PLAT_H
+#include <stdbool.h>
+#include <stdint.h>
+
+enum { JK_OK = 1, JK_UP = 2, JK_DOWN = 4, JK_LEFT = 8, JK_RIGHT = 16, JK_BACK = 32, JK_HOME = 64 };
+uint32_t plat_keys(void);
+uint32_t plat_millis(void);
+void plat_sleep(uint32_t ms);
+bool plat_save(const char *name, const void *data, uint32_t len);
+const uint8_t *plat_load(const char *name, uint32_t *len);
+uint32_t plat_random(void);
+void plat_begin(void);
+int plat_end(void);
+#endif
diff --git a/games/donkeykong/src/plat_eadk.c b/games/donkeykong/src/plat_eadk.c
new file mode 100644
index 00000000..fab3acaf
--- /dev/null
+++ b/games/donkeykong/src/plat_eadk.c
@@ -0,0 +1,34 @@
+/* The calculator: keys, time, the screen and files (EADK, and NumPlay's careful file helpers). */
+#include <eadk.h>
+#include "../../common/epsilon_app.h"
+#include "../../common/epsilon_files.h"
+#include "../../../launcher/src/np.h"
+#include "plat.h"
+
+const char eadk_app_name[] __attribute__((section(".rodata.eadk_app_name"))) = "Donkey Kong";
+const uint32_t eadk_api_level __attribute__((section(".rodata.eadk_api_level"))) = 0;
+
+#define KEY(k) ((uint64_t)1 << (k))
+
+uint32_t plat_keys(void) {
+ uint64_t k = eadk_keyboard_scan();
+ uint32_t r = 0;
+ if (k & (KEY(eadk_key_ok) | KEY(eadk_key_exe))) r |= JK_OK;
+ if (k & (KEY(eadk_key_up) | KEY(eadk_key_eight))) r |= JK_UP;
+ if (k & (KEY(eadk_key_down) | KEY(eadk_key_two))) r |= JK_DOWN;
+ if (k & (KEY(eadk_key_left) | KEY(eadk_key_four))) r |= JK_LEFT;
+ if (k & (KEY(eadk_key_right) | KEY(eadk_key_six))) r |= JK_RIGHT;
+ if (k & (KEY(eadk_key_back) | KEY(eadk_key_backspace))) r |= JK_BACK;
+ if (k & (KEY(eadk_key_home) | KEY(eadk_key_on_off))) r |= JK_HOME;
+ return r;
+}
+uint32_t plat_millis(void) { return (uint32_t)eadk_timing_millis(); }
+void plat_sleep(uint32_t ms) { eadk_timing_msleep(ms); }
+void np_push(int x, int y, int w, int h, const color_t *px) {
+ eadk_display_push_rect((eadk_rect_t){(uint16_t)x, (uint16_t)y, (uint16_t)w, (uint16_t)h}, px);
+}
+bool plat_save(const char *name, const void *data, uint32_t len) { return ef_write(name, data, len); }
+const uint8_t *plat_load(const char *name, uint32_t *len) { return ef_read(name, len); }
+uint32_t plat_random(void) { return eadk_random(); }
+void plat_begin(void) { np_app_begin(); }
+int plat_end(void) { return np_app_end(); }
diff --git a/games/donkeykong/src/render.c b/games/donkeykong/src/render.c
deleted file mode 100644
index 3d3b4d81..00000000
--- a/games/donkeykong/src/render.c
+++ /dev/null
@@ -1,273 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-only
- * Original pixel art, direct packed sprites, one 8-row RGB565 strip (5 KiB).
- * Only changed strips are sent to the LCD; no heap or full framebuffer. */
-#include "render.h"
-#include <stdlib.h>
-#include <string.h>
-#include "assets.h"
-#include "font.h"
-#define C(v) ((uint16_t)((((v)>>8)&0xF800)|(((v)>>5)&0x07E0)|(((v)>>3)&31)))
-#define BLACK C(0x070B16)
-#define PANEL C(0x111A2B)
-#define DIM C(0x52617B)
-#define STEEL C(0x29364F)
-#define WHITE C(0xFFF0CD)
-#define RED C(0xFF5545)
-#define PINK C(0xF566AF)
-#define GOLD C(0xFFE16B)
-#define CYAN C(0x77DAEA)
-static uint16_t band[320*8];
-static uint32_t hashes[30];
-static bool valid;
-static int top;
-static const dk_game *g;
-static const dk_stats *s;
-static unsigned view,selected;
-static bool ongoing,failed;
-static uint32_t clock_ms;
-void dk_invalidate(void){valid=false;}
-static void pixel(int x,int y,uint16_t color) {
- if(x>=0&&x<320&&y>=top&&y<top+8)band[(y-top)*320+x]=color;
-}
-static void rect(int x,int y,int w,int h,uint16_t color) {
- int l=x<0?0:x,r=x+w>320?320:x+w,t=y<top?top:y,b=y+h>top+8?top+8:y+h;
- if(l>=r||t>=b)return;
- for(int j=t;j<b;++j)for(int i=l;i<r;++i)band[(j-top)*320+i]=color;
-}
-static void line(int a,int b,int c,int d,uint16_t color) {
- if((b<top&&d<top)||(b>=top+8&&d>=top+8))return;
- int dx=abs(c-a),sx=a<c?1:-1,dy=-abs(d-b),sy=b<d?1:-1,e=dx+dy;
- for(;;){pixel(a,b,color);if(a==c&&b==d)break;int twice=e*2;
- if(twice>=dy)e+=dy,a+=sx;
- if(twice<=dx)e+=dx,b+=sy;}
-}
-static void frame(int x,int y,int w,int h,uint16_t color) {
- rect(x,y,w,1,color);rect(x,y+h-1,w,1,color);rect(x,y,1,h,color);rect(x+w-1,y,1,h,color);
-}
-static void text(int x,int y,const char *str,int scale,uint16_t color) {
- if(y>=top+8||y+7*scale<=top)return;
- for(;*str;++str,x+=6*scale) {
- unsigned ch=(unsigned char)*str,i;if(ch>='a'&&ch<='z')ch-=32;
- if(ch>='A'&&ch<='Z')i=ch-'A';
- else if(ch>='0'&&ch<='9')i=ch-'0'+26;
- else{static const char punctuation[]="+-.:/!<>|";const char *p=strchr(punctuation,(int)ch);
- if(!p||ch==' ')continue;
- i=36+(unsigned)(p-punctuation);}
- for(int col=0;col<5;++col)for(int row=0;row<7;++row)
- if(alphabet[i][col]&(1<<row))rect(x+col*scale,y+row*scale,scale,scale,color);
- }
-}
-static void center(int x,int y,const char *str,int scale,uint16_t color) {
- text(x-((int)strlen(str)*6*scale-scale)/2,y,str,scale,color);
-}
-static void number(int x,int y,uint32_t v,int scale,uint16_t color) {
- char str[11];unsigned n=0;do{str[n++]=(char)('0'+v%10);v/=10;}while(v);
- for(unsigned i=0;i<n/2;++i){char c=str[i];str[i]=str[n-i-1];str[n-i-1]=c;}str[n]=0;text(x,y,str,scale,color);
-}
-static void sprite(int x,int y,const dk_sprite *p,int face,int scale,bool blue) {
- if(y>=top+8||y+p->h*scale<=top)return;
- int first=(top-y)/scale;if(first<0)first=0;
- int last=(top+7-y)/scale;if(last>=p->h)last=p->h-1;
- for(int row=first;row<=last;++row)for(int col=0;col<p->w;++col) {
- unsigned i=(unsigned)(row*p->w+col),v=p->p[i/2];v=(i&1)?v&15:v>>4;
- if(v)rect(x+(face<0?p->w-col-1:col)*scale,y+row*scale,scale,scale,
- blue&&(v==12||v==7)?CYAN:palette[v]);
- }
-}
-static int wx(int x){return x+88;}
-static int wy(int y){return y+8;}
-static void heart(int x,int y) {
- rect(x,y,3,3,PINK);rect(x+5,y,3,3,PINK);rect(x-1,y+2,10,3,PINK);
- rect(x,y+5,8,1,PINK);rect(x+1,y+6,6,1,PINK);rect(x+2,y+7,4,1,PINK);rect(x+3,y+8,2,1,PINK);
-}
-static void mario(int x,int feet,int face,bool ladder,bool dead,int step,bool hammer) {
- if(dead) { // The silhouette tumbles once, then stays still during the death cue.
- sprite(x-6,feet-13,&spr_mario,face,1,false);line(x-6,feet-6,x+6,feet-1,WHITE);return;
- }
- sprite(x-6,feet-16,&spr_mario,face,1,false);
- if(ladder) {
- rect(x-6,feet-9,2,3,palette[2]);rect(x+5,feet-12,2,3,palette[2]);
- rect(x-4,feet-4,8,4,BLACK);rect(x-4,feet-(step?5:3),3,3,palette[3]);
- rect(x+2,feet-(step?3:5),3,3,palette[3]);
- } else if(step) {
- rect(x-6,feet-2,13,2,BLACK);rect(x-3,feet-2,4,2,palette[5]);rect(x+3,feet-4,4,2,palette[5]);
- }
- if(hammer) sprite(x+(face<0?-17:9),feet-23+(step?7:0),&spr_hammer,face,1,false);
-}
-static void kong(int x,int feet) {
- if(g->board==3&&g->phase==DK_CLEAR)feet+=(125-g->timer)/2;
- sprite(wx(x-13),wy(feet-25),&spr_kong,1,1,false);
- // Fists move with barrel throws; the tie is red, not another bitmap frame.
- if((g->tick/24)&1){rect(wx(x-15),wy(feet-17),4,5,palette[7]);rect(wx(x+12),wy(feet-17),4,5,palette[7]);}
- rect(wx(x-1),wy(feet-10),3,5,RED);rect(wx(x-2),wy(feet-6),5,3,RED);
-}
-static void girders(void) {
- for(unsigned i=0;i<g->platform_count;++i) {
- const dk_platform *p=&g->platform[i];
- int low=p->y1<p->y2?p->y1:p->y2,high=p->y1>p->y2?p->y1:p->y2;
- if(wy(low)>top+7||wy(high+5)<top)continue;
- uint16_t color=p->type==DK_LIFT?GOLD:PINK;
- for(int x=p->x1;x<=p->x2;++x) {
- int y=dk_surface(g,i,x);if(y<0)continue;
- pixel(wx(x),wy(y),color);pixel(wx(x),wy(y+4),color);
- if(p->type==DK_BELT) {
- pixel(wx(x),wy(y+2),STEEL);
- int offset=dk_belt_direction(g,p)*(int)(g->age/4);
- if(((x+offset)%8+8)%8<2)pixel(wx(x),wy(y+2),WHITE);
- } else {
- int k=(x-p->x1)%8;pixel(wx(x),wy(y+1+(k<4?k:7-k)),color);
- }
- }
- if(p->type==DK_BELT) {
- rect(wx(p->x1),wy(low),2,5,CYAN);rect(wx(p->x2-1),wy(high),2,5,CYAN);
- }
- }
- if(g->board==2)for(int x=60;x<=124;x+=56)line(wx(x),wy(57),wx(x),wy(216),STEEL);
-}
-static void ladders(void) {
- for(unsigned i=0;i<g->ladder_count;++i) {
- const dk_ladder *l=&g->ladder[i];
- int t=dk_surface(g,l->upper,l->x)+2,b=dk_surface(g,l->lower,l->x);
- bool open=dk_ladder_open(g,i);uint16_t color=open?CYAN:DIM;
- if(l->retract&&!open)t=b-7;
- for(int y=t;y<b;++y) {
- if(l->broken&&y>t+7&&y<b-7)continue;
- pixel(wx(l->x-3),wy(y),color);pixel(wx(l->x+3),wy(y),color);
- if((y-t)%4==0)rect(wx(l->x-3),wy(y),7,1,color);
- }
- }
-}
-static void world(void) {
- rect(87,7,226,226,BLACK);frame(86,6,228,228,STEEL);
- girders();ladders();
- for(unsigned i=0;i<5;++i)if(g->item[i].active) {
- const dk_item *v=&g->item[i];const dk_sprite *p=v->type==1?&spr_hammer:v->type==2?&spr_bag:v->type==3?&spr_hat:&spr_parasol;
- sprite(wx(v->x-p->w/2),wy(v->y-p->h/2),p,1,1,false);
- }
- if(g->board==3)for(int row=1;row<=4;++row)for(int side=0;side<2;++side)
- if(!(g->rivets&(1u<<((row-1)*2+side)))) {
- int x=side?176:48,y=g->platform[row].y1;
- rect(wx(x-2),wy(y-2),5,2,CYAN);rect(wx(x),wy(y),1,5,CYAN);
- }
- if(g->board==0||g->board==1) {
- int x=g->board==0?17:112,y=g->board==0?212:176;
- rect(wx(x-7),wy(y-13),14,13,palette[3]);rect(wx(x-8),wy(y-14),16,2,CYAN);
- if((g->age/8)&1)sprite(wx(x-5),wy(y-23),&spr_fire,1,1,true);
- center(wx(x),wy(y-8),"OIL",1,WHITE);
- }
- int kx=g->board==3?112:g->board==1?80+(int)(g->age/8%42):34;
- int ky=g->board==0?58:g->board==1?68:g->board==2?55:66;
- kong(kx,ky);
- int px=g->board==0?163:g->board==1?198:g->board==2?204:185;
- int py=g->board==0?25:g->board==1?28:g->board==2?24:28;
- sprite(wx(px-5),wy(py-18),&spr_pauline,1,1,false);
- if(g->phase==DK_CLEAR&&g->board==3)heart(wx(165),wy(5));
- else if((g->tick/45)&1)text(wx(px-36),wy(4),"HELP!",1,PINK);
- for(unsigned i=0;i<DK_ENEMIES;++i) {
- const dk_enemy *e=&g->enemy[i];if(!e->active)continue;
- int x=wx(e->x/DK_Q),y=wy(e->y/DK_Q);
- if(e->type==DK_SPRING) {
- rect(x-5,y-8,10,1,CYAN);rect(x-5,y-1,10,1,CYAN);
- for(int j=0;j<3;++j)line(x-5,y-7+j*2,x+4,y-5+j*2,WHITE);
- } else {
- const dk_sprite *p=e->type==DK_BARREL?&spr_barrel:e->type==DK_FIRE?&spr_fire:&spr_tub;
- sprite(x-p->w/2,y-p->h,p,e->dir,1,e->blue);
- if(e->type==DK_BARREL)line(x-3,y-5,x+3,y-5+(int)(g->age/6%3)-1,GOLD);
- }
- }
- const dk_player *p=&g->player;
- if(g->phase!=DK_DYING||(g->timer/7)&1)
- mario(wx(p->x/DK_Q),wy(p->y/DK_Q),p->face,p->ladder>=0,g->phase==DK_DYING,
- (int)(g->tick/7&1)&&((g->previous_input&(DK_LEFT|DK_RIGHT|DK_UP|DK_DOWN))!=0),p->hammer!=0);
- if(g->popup_time)number(wx(g->popup_x-8),wy(g->popup_y),g->popup_value,1,WHITE);
-}
-static void side(void) {
- text(8,12,"1UP",1,RED);number(8,24,g->score,1,WHITE);
- text(8,43,"HI SCORE",1,DIM);number(8,55,s->best,1,GOLD);
- rect(8,73,65,1,STEEL);
- text(8,84,"ROUND",1,DIM);number(48,84,g->round,1,WHITE);
- static const char *const stage[]={"25M","50M","75M","100M"};
- text(8,103,stage[g->board],2,CYAN);
- text(8,126,"BONUS",1,DIM);number(8,138,g->bonus,1,GOLD);
- text(8,160,"LIVES",1,DIM);
- for(unsigned i=0;i<g->lives&&i<4;++i)mario(14+(int)i*16,189,1,false,false,0,false);
- if(g->lives>4)number(61,181,g->lives,1,WHITE);
- if(g->player.hammer) { // eight seconds, shown without an extra HUD row
- rect(8,197,64,3,STEEL);rect(8,197,g->player.hammer*64/480,3,GOLD);
- }
- text(8,214,"BACK",1,DIM);text(8,226,"PAUSE",1,WHITE);
-}
-static void button(int x,int y,int w,const char *label,bool on) {
- rect(x,y,w,20,on?GOLD:PANEL);
- text(x+8,y+6,label,1,on?BLACK:WHITE);
- if(on)text(x+w-12,y+6,">",1,BLACK);
-}
-static void overlay(const char *title,int height) {
- int y=(240-height)/2;rect(94,y,210,height,PANEL);frame(94,y,210,height,GOLD);
- center(199,y+12,title,1,GOLD);
-}
-static void title(void) {
- // A vertical control panel next to a live-looking attract board.
- text(7,14,"DONKEY",2,RED);text(7,33,"KONG",2,GOLD);
- text(8,58,"ARCADE",1,CYAN);rect(8,74,65,1,STEEL);
- text(8,84,"BEST",1,DIM);number(8,96,s->best,1,WHITE);
- button(4,123,76,ongoing?"RESUME":"PLAY",selected==0);
- button(4,148,76,"HOW TO",selected==1);
- button(4,173,76,"EXIT",selected==2);
- text(8,211,"BY",1,DIM);text(8,224,"BOBYDELUXE",1,WHITE);
- overlay("HOW HIGH CAN YOU GET?",54);center(199,119,"FOUR BOARDS. ONE RESCUE.",1,WHITE);
- center(199,134,clock_ms/600%2?"PRESS OK":"",1,CYAN);
-}
-static void help(void) {
- rect(0,0,320,240,BLACK);rect(0,0,320,30,PANEL);text(12,10,"HOW TO PLAY",2,GOLD);
- text(12,43,"LEFT / RIGHT",1,CYAN);text(132,43,"MOVE",1,WHITE);
- text(12,59,"UP / DOWN",1,CYAN);text(132,59,"CLIMB LADDERS",1,WHITE);
- text(12,75,"OK / EXE / SHIFT",1,CYAN);text(132,75,"JUMP",1,WHITE);
- text(12,91,"BACK",1,CYAN);text(132,91,"PAUSE",1,WHITE);
- rect(12,107,296,1,STEEL);
- text(12,119,"25M JUMP BARRELS. CLIMB TO PAULINE.",1,WHITE);
- text(12,135,"50M WATCH THE BELTS AND OIL FIRE.",1,WHITE);
- text(12,151,"75M RIDE LIFTS. DODGE THE SPRINGS.",1,WHITE);
- text(12,167,"100M REMOVE ALL EIGHT BLUE RIVETS.",1,WHITE);
- text(12,186,"HAMMER: NO JUMPING OR CLIMBING.",1,GOLD);
- text(12,201,"LONG FALLS HURT. EXTRA LIFE: 7000.",1,DIM);
- text(12,223,"ARCADE ADAPTATION / BY BOBYDELUXE",1,CYAN);
- text(12,213,"ORIGINAL GAME: NINTENDO / 1981",1,DIM);
- text(278,12,"BACK",1,WHITE);
-}
-static void scene(void) {
- rect(0,top,320,8,BLACK);
- if(view==DK_HELP){help();return;}
- side();world();
- if(view==DK_TITLE) {rect(0,0,84,240,BLACK);title();}
- else if(view==DK_PLAY) {
- if(g->phase==DK_READY) {overlay("GET READY",52);center(199,123,"SAVE PAULINE!",1,WHITE);}
- if(g->phase==DK_CLEAR) {overlay(g->board==3?"PAULINE IS SAFE!":"BOARD CLEAR",42);center(199,123,"BONUS ADDED",1,WHITE);}
- if(g->phase==DK_DYING)center(199,15,g->reason==DK_TIME?"TIME UP!":"TRY AGAIN!",1,RED);
- } else if(view==DK_PAUSE) {
- overlay("PAUSED",116);button(108,91,182,"RESUME",selected==0);
- button(108,117,182,"RESTART",selected==1);button(108,143,182,"TITLE",selected==2);
- } else if(view==DK_CONFIRM) {
- overlay("RESTART THIS RUN?",94);button(108,105,182,"KEEP PLAYING",selected==0);
- button(108,131,182,"RESTART",selected==1);
- } else if(view==DK_RESULT) {
- overlay("GAME OVER",122);center(199,82,"FINAL SCORE",1,DIM);number(170,97,g->score,2,WHITE);
- button(108,121,182,"PLAY AGAIN",selected==0);button(108,147,182,"TITLE",selected==1);
- } else if(view==DK_SAVE_ERROR) {
- overlay("STORAGE FULL",108);center(199,96,"RECORD COULD NOT BE SAVED",1,WHITE);
- center(199,114,"OK: RETRY AND EXIT",1,GOLD);center(199,132,"1: KEEP PLAYING",1,WHITE);
- center(199,150,"BACK: EXIT WITHOUT SAVING",1,DIM);
- }
- if(failed&&view!=DK_SAVE_ERROR)rect(77,3,4,4,RED);
-}
-void dk_render(const dk_game *game,const dk_stats *stats,unsigned page,unsigned selection,
- bool continuing,bool save_failed,uint32_t ms,dk_push push) {
- g=game;s=stats;view=page;selected=selection;ongoing=continuing;failed=save_failed;clock_ms=ms;
- for(unsigned strip=0;strip<30;++strip) {
- top=(int)strip*8;scene();uint32_t hash=2166136261u;
- for(unsigned i=0;i<320*8;++i)hash=(hash^band[i])*16777619u;
- if(!valid||hashes[strip]!=hash)push(0,top,320,8,band);
- hashes[strip]=hash;
- }
- valid=true;
-}
diff --git a/games/donkeykong/src/render.h b/games/donkeykong/src/render.h
deleted file mode 100644
index 6166bad3..00000000
--- a/games/donkeykong/src/render.h
+++ /dev/null
@@ -1,10 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-only */
-#ifndef DK_RENDER_H
-#define DK_RENDER_H
-#include "game.h"
-enum { DK_TITLE, DK_PLAY, DK_PAUSE, DK_HELP, DK_CONFIRM, DK_RESULT, DK_SAVE_ERROR };
-typedef void (*dk_push)(int,int,int,int,const uint16_t *);
-void dk_render(const dk_game *,const dk_stats *,unsigned view,unsigned selection,
- bool continuing,bool save_failed,uint32_t ms,dk_push);
-void dk_invalidate(void);
-#endif
diff --git a/games/donkeykong/src/rom.h b/games/donkeykong/src/rom.h
new file mode 100644
index 00000000..23222fac
--- /dev/null
+++ b/games/donkeykong/src/rom.h
@@ -0,0 +1,174 @@
+/* Generated by tools/rom.py from tcdev's listing of Donkey Kong's program ROM (data tables only).
+ * ROM(address) reads them by their address in the arcade's ROM, as sim.c's comments name them. */
+#ifndef DK_ROM_H
+#define DK_ROM_H
+#include <stdint.h>
+static const uint8_t rom_0D17[16] = {
+ 0xCA,0x76,0xCF,0x76,0xD4,0x76,0xD9,0x76,0x2A,0x75,0x2F,0x75,0x34,0x75,0x39,0x75,
+};
+static const uint8_t rom_101B[4] = {
+ 0x00,0x00,0x02,0x02,
+};
+static const uint8_t rom_1121[16] = {
+ 0x37,0x45,0x0F,0x60,0x37,0x45,0x8F,0xF7,0x77,0x45,0x0F,0x60,0x77,0x45,0x8F,0xF7,
+};
+static const uint8_t rom_117E[8] = {
+ 0x3F,0x0C,0x08,0x08,0x73,0x50,0x8D,0x50,
+};
+static const uint8_t rom_11A2[4] = {
+ 0x3B,0x00,0x02,0x02,
+};
+static const uint8_t rom_3529[60] = {
+ 0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x02,0x00,0x00,0x03,0x00,0x00,0x04,0x00,0x00,
+ 0x05,0x00,0x00,0x06,0x00,0x00,0x07,0x00,0x00,0x08,0x00,0x00,0x09,0x00,0x00,0x00,
+ 0x00,0x00,0x10,0x00,0x00,0x20,0x00,0x00,0x30,0x00,0x00,0x40,0x00,0x00,0x50,0x00,
+ 0x00,0x60,0x00,0x00,0x70,0x00,0x00,0x80,0x00,0x00,0x90,0x00,
+};
+static const uint8_t rom_364B[443] = {
+ 0x8B,0x36,0x01,0x00,0x98,0x36,0xA5,0x36,0xB2,0x36,0xBF,0x36,0x06,0x00,0xCC,0x36,
+ 0x08,0x00,0xE6,0x36,0xFD,0x36,0x0B,0x00,0x15,0x37,0x1C,0x37,0x30,0x37,0x38,0x37,
+ 0x47,0x37,0x5D,0x37,0x73,0x37,0x8B,0x37,0x00,0x61,0x22,0x61,0x44,0x61,0x66,0x61,
+ 0x88,0x61,0x9E,0x37,0xB6,0x37,0xD2,0x37,0xE1,0x37,0x1D,0x00,0x00,0x3F,0x09,0x3F,
+ 0x96,0x76,0x17,0x11,0x1D,0x15,0x10,0x10,0x1F,0x26,0x15,0x22,0x3F,0x94,0x76,0x20,
+ 0x1C,0x11,0x29,0x15,0x22,0x10,0x30,0x32,0x31,0x3F,0x94,0x76,0x20,0x1C,0x11,0x29,
+ 0x15,0x22,0x10,0x30,0x33,0x31,0x3F,0x80,0x76,0x18,0x19,0x17,0x18,0x10,0x23,0x13,
+ 0x1F,0x22,0x15,0x3F,0x9F,0x75,0x13,0x22,0x15,0x14,0x19,0x24,0x10,0x10,0x10,0x10,
+ 0x3F,0x5E,0x77,0x18,0x1F,0x27,0x10,0x18,0x19,0x17,0x18,0x10,0x13,0x11,0x1E,0x10,
+ 0x29,0x1F,0x25,0x10,0x17,0x15,0x24,0x10,0xFB,0x10,0x3F,0x29,0x77,0x1F,0x1E,0x1C,
+ 0x29,0x10,0x01,0x10,0x20,0x1C,0x11,0x29,0x15,0x22,0x10,0x12,0x25,0x24,0x24,0x1F,
+ 0x1E,0x3F,0x29,0x77,0x01,0x10,0x1F,0x22,0x10,0x02,0x10,0x20,0x1C,0x11,0x29,0x15,
+ 0x22,0x23,0x10,0x12,0x25,0x24,0x24,0x1F,0x1E,0x3F,0x27,0x76,0x20,0x25,0x23,0x18,
+ 0x3F,0x06,0x77,0x1E,0x11,0x1D,0x15,0x10,0x22,0x15,0x17,0x19,0x23,0x24,0x22,0x11,
+ 0x24,0x19,0x1F,0x1E,0x3F,0x88,0x76,0x1E,0x11,0x1D,0x15,0x2E,0x3F,0xE9,0x75,0x2D,
+ 0x2D,0x2D,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x3F,0x0B,0x77,0x11,0x10,
+ 0x12,0x10,0x13,0x10,0x14,0x10,0x15,0x10,0x16,0x10,0x17,0x10,0x18,0x10,0x19,0x10,
+ 0x1A,0x3F,0x0D,0x77,0x1B,0x10,0x1C,0x10,0x1D,0x10,0x1E,0x10,0x1F,0x10,0x20,0x10,
+ 0x21,0x10,0x22,0x10,0x23,0x10,0x24,0x3F,0x0F,0x77,0x25,0x10,0x26,0x10,0x27,0x10,
+ 0x28,0x10,0x29,0x10,0x2A,0x10,0x2B,0x10,0x2C,0x44,0x45,0x46,0x47,0x48,0x10,0x3F,
+ 0xF2,0x76,0x22,0x15,0x17,0x19,0x10,0x24,0x19,0x1D,0x15,0x10,0x10,0x30,0x03,0x00,
+ 0x31,0x10,0x3F,0x92,0x77,0x22,0x11,0x1E,0x1B,0x10,0x10,0x23,0x13,0x1F,0x22,0x15,
+ 0x10,0x10,0x1E,0x11,0x1D,0x15,0x10,0x10,0x10,0x10,0x3F,0x72,0x77,0x29,0x1F,0x25,
+ 0x22,0x10,0x1E,0x11,0x1D,0x15,0x10,0x27,0x11,0x23,0x10,0x22,0x15,0x17,0x19,0x23,
+ 0x24,0x15,0x22,0x15,0x14,0x42,0x3F,0xA7,0x76,0x19,0x1E,0x23,0x15,0x22,0x24,0x10,
+ 0x13,0x1F,0x19,0x1E,0x10,0x3F,0x0A,0x77,0x10,0x10,0x20,0x1C,0x11,0x29,0x15,0x22,
+ 0x10,0x10,0x10,0x10,0x13,0x1F,0x19,0x1E,0x3F,0xFC,0x76,0x49,0x4A,0x10,0x1E,0x19,
+ 0x1E,0x24,0x15,0x1E,0x14,0x1F,0x10,0x10,0x10,0x10,0x3F,
+};
+static const uint8_t rom_380D[1635] = {
+ 0x02,0x97,0x38,0x68,0x38,0x02,0xDF,0x54,0x10,0x54,0x02,0xEF,0x6D,0x20,0x6D,0x02,
+ 0xDF,0x8E,0x10,0x8E,0x02,0xEF,0xAF,0x20,0xAF,0x02,0xDF,0xD0,0x10,0xD0,0x02,0xEF,
+ 0xF1,0x10,0xF1,0x00,0x53,0x18,0x53,0x54,0x00,0x63,0x18,0x63,0x54,0x00,0x93,0x38,
+ 0x93,0x54,0x00,0x83,0x54,0x83,0xF1,0x00,0x93,0x54,0x93,0xF1,0xAA,0x8D,0x7D,0x8C,
+ 0x6F,0x00,0x7C,0x6E,0x00,0x7C,0x6D,0x00,0x7C,0x6C,0x00,0x7C,0x8F,0x7F,0x8E,0x47,
+ 0x27,0x08,0x50,0x2F,0xA7,0x08,0x50,0x3B,0x25,0x08,0x50,0x00,0x70,0x08,0x48,0x3B,
+ 0x23,0x07,0x40,0x46,0xA9,0x08,0x44,0x00,0x70,0x08,0x48,0x30,0x29,0x08,0x44,0x00,
+ 0x70,0x08,0x48,0x00,0x70,0x0A,0x48,0x6F,0x10,0x09,0x23,0x6F,0x11,0x0A,0x33,0x50,
+ 0x34,0x08,0x3C,0x00,0x35,0x08,0x3C,0x53,0x32,0x08,0x40,0x63,0x33,0x08,0x40,0x00,
+ 0x70,0x08,0x48,0x53,0x36,0x08,0x50,0x63,0x37,0x08,0x50,0x6B,0x31,0x08,0x41,0x00,
+ 0x70,0x08,0x48,0x6A,0x14,0x0A,0x48,0xFD,0xFD,0xFD,0xFD,0xFD,0xFD,0xFD,0xFE,0xFE,
+ 0xFE,0xFE,0xFE,0xFE,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0x01,0x01,0x01,0x7F,0xFF,0xFF,
+ 0xFF,0xFF,0xFF,0x00,0xFF,0x00,0x00,0x01,0x00,0x01,0x01,0x01,0x01,0x01,0x7F,0x04,
+ 0x7F,0xF0,0x10,0xF0,0x02,0xDF,0xF2,0x70,0xF8,0x02,0x6F,0xF8,0x10,0xF8,0xAA,0x04,
+ 0xDF,0xD0,0x90,0xD0,0x02,0xDF,0xDC,0x20,0xD1,0xAA,0xFF,0xFF,0xFF,0xFF,0xFF,0x04,
+ 0xDF,0xA8,0x20,0xA8,0x04,0x5F,0xB0,0x20,0xB0,0x02,0xDF,0xB0,0x20,0xBB,0xAA,0x04,
+ 0xDF,0x88,0x30,0x88,0x04,0xDF,0x90,0xB0,0x90,0x02,0xDF,0x9A,0x20,0x8F,0xAA,0x04,
+ 0xBF,0x68,0x20,0x68,0x04,0x3F,0x70,0x20,0x70,0x02,0xDF,0x6E,0x20,0x79,0xAA,0x02,
+ 0xDF,0x58,0xA0,0x55,0xAA,0x00,0x70,0x08,0x44,0x2B,0xAC,0x08,0x4C,0x3B,0xAE,0x08,
+ 0x4C,0x3B,0xAF,0x08,0x3C,0x4B,0xB0,0x07,0x3C,0x4B,0xAD,0x08,0x4C,0x00,0x70,0x08,
+ 0x44,0x00,0x70,0x08,0x44,0x00,0x70,0x08,0x44,0x00,0x70,0x0A,0x44,0x47,0x27,0x08,
+ 0x4C,0x2F,0xA7,0x08,0x4C,0x3B,0x25,0x08,0x4C,0x00,0x70,0x08,0x44,0x3B,0x23,0x07,
+ 0x3C,0x4B,0x2A,0x08,0x3C,0x4B,0x2B,0x08,0x4C,0x2B,0xAA,0x08,0x3C,0x2B,0xAB,0x08,
+ 0x4C,0x00,0x70,0x0A,0x44,0x00,0x70,0x08,0x44,0x4B,0x2C,0x08,0x4C,0x3B,0x2E,0x08,
+ 0x4C,0x3B,0x2F,0x08,0x3C,0x2B,0x30,0x07,0x3C,0x2B,0x2D,0x08,0x4C,0x00,0x70,0x08,
+ 0x44,0x00,0x70,0x08,0x44,0x00,0x70,0x08,0x44,0x00,0x70,0x0A,0x44,0xFD,0xFD,0xFD,
+ 0xFE,0xFE,0xFE,0xFE,0xFF,0xFF,0x00,0xFF,0x00,0x00,0x01,0x00,0x01,0x01,0x02,0x02,
+ 0x02,0x02,0x03,0x03,0x03,0x7F,0x1E,0x4E,0xBB,0x4C,0xD8,0x4E,0x59,0x4E,0x7F,0xBB,
+ 0x4D,0x7F,0x47,0x27,0x08,0x50,0x2D,0x26,0x08,0x50,0x3B,0x25,0x08,0x50,0x00,0x70,
+ 0x08,0x48,0x3B,0x24,0x07,0x40,0x4B,0x28,0x08,0x40,0x00,0x70,0x08,0x48,0x30,0x29,
+ 0x08,0x44,0x00,0x70,0x08,0x48,0x00,0x70,0x0A,0x48,0x49,0xA6,0x08,0x50,0x2F,0xA7,
+ 0x08,0x50,0x3B,0x25,0x08,0x50,0x00,0x70,0x08,0x48,0x3B,0x24,0x07,0x40,0x46,0xA9,
+ 0x08,0x44,0x00,0x70,0x08,0x48,0x2B,0xA8,0x08,0x40,0x00,0x70,0x08,0x48,0x00,0x70,
+ 0x0A,0x48,0x73,0xA7,0x88,0x60,0x8B,0x27,0x88,0x60,0x7F,0x25,0x88,0x60,0x00,0x70,
+ 0x88,0x68,0x7F,0x24,0x87,0x70,0x74,0x29,0x88,0x6C,0x00,0x70,0x88,0x68,0x8A,0xA9,
+ 0x88,0x6C,0x00,0x70,0x88,0x68,0x00,0x70,0x8A,0x68,0x05,0xAF,0xF0,0x50,0xF0,0xAA,
+ 0x05,0xAF,0xE8,0x50,0xE8,0xAA,0x05,0xAF,0xE0,0x50,0xE0,0xAA,0x05,0xAF,0xD8,0x50,
+ 0xD8,0xAA,0x05,0xB7,0x58,0x48,0x58,0xAA,0x01,0x04,0x01,0x03,0x04,0x01,0x02,0x03,
+ 0x04,0x01,0x02,0x01,0x03,0x04,0x01,0x02,0x01,0x03,0x01,0x04,0x7F,0xFF,0x00,0xFF,
+ 0xFF,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFF,0xFF,0x00,0xE8,
+ 0xE5,0xE3,0xE2,0xE1,0xE0,0xDF,0xDE,0xDD,0xDD,0xDC,0xDC,0xDC,0xDC,0xDC,0xDC,0xDD,
+ 0xDD,0xDE,0xDF,0xE0,0xE1,0xE2,0xE3,0xE4,0xE5,0xE7,0xE9,0xEB,0xED,0xF0,0xAA,0x80,
+ 0x7B,0x78,0x76,0x74,0x73,0x72,0x71,0x70,0x70,0x6F,0x6F,0x6F,0x70,0x70,0x71,0x72,
+ 0x73,0x74,0x75,0x76,0x77,0x78,0xAA,0xEE,0xF0,0xDB,0xA0,0xE6,0xC8,0xD6,0x78,0xEB,
+ 0xF0,0xDB,0xA0,0xE6,0xC8,0xE6,0xC8,0x1B,0xC8,0x23,0xA0,0x2B,0x78,0x12,0xF0,0x1B,
+ 0xC8,0x23,0xA0,0x12,0xF0,0x1B,0xC8,0x02,0x97,0x38,0x68,0x38,0x02,0x9F,0x54,0x10,
+ 0x54,0x02,0xDF,0x58,0xA0,0x55,0x02,0xEF,0x6D,0x20,0x79,0x02,0xDF,0x9A,0x10,0x8E,
+ 0x02,0xEF,0xAF,0x20,0xBB,0x02,0xDF,0xDC,0x10,0xD0,0x02,0xFF,0xF0,0x80,0xF7,0x02,
+ 0x7F,0xF8,0x00,0xF8,0x00,0xCB,0x57,0xCB,0x6F,0x00,0xCB,0x99,0xCB,0xB1,0x00,0xCB,
+ 0xDB,0xCB,0xF3,0x00,0x63,0x18,0x63,0x54,0x01,0x63,0xD5,0x63,0xF8,0x00,0x33,0x78,
+ 0x33,0x90,0x00,0x33,0xBA,0x33,0xD2,0x00,0x53,0x18,0x53,0x54,0x01,0x53,0x92,0x53,
+ 0xB8,0x00,0x5B,0x76,0x5B,0x92,0x00,0x73,0xB6,0x73,0xD6,0x00,0x83,0x95,0x83,0xB5,
+ 0x00,0x93,0x38,0x93,0x54,0x01,0xBB,0x70,0xBB,0x98,0x01,0x6B,0x54,0x6B,0x75,0xAA,
+ 0x06,0x8F,0x90,0x70,0x90,0x06,0x8F,0x98,0x70,0x98,0x06,0x8F,0xA0,0x70,0xA0,0x00,
+ 0x63,0x18,0x63,0x58,0x00,0x63,0x80,0x63,0xA8,0x00,0x63,0xD0,0x63,0xF8,0x00,0x53,
+ 0x18,0x53,0x58,0x00,0x53,0xA8,0x53,0xD0,0x00,0x9B,0x80,0x9B,0xA8,0x00,0x9B,0xD0,
+ 0x9B,0xF8,0x01,0x23,0x58,0x23,0x80,0x01,0xDB,0x58,0xDB,0x80,0x00,0x2B,0x80,0x2B,
+ 0xA8,0x00,0xD3,0x80,0xD3,0xA8,0x00,0xA3,0xA8,0xA3,0xD0,0x00,0x2B,0xD0,0x2B,0xF8,
+ 0x00,0xD3,0xD0,0xD3,0xF8,0x00,0x93,0x38,0x93,0x58,0x02,0x97,0x38,0x68,0x38,0x03,
+ 0xEF,0x58,0x10,0x58,0x03,0xF7,0x80,0x88,0x80,0x03,0x77,0x80,0x08,0x80,0x02,0xA7,
+ 0xA8,0x50,0xA8,0x02,0xE7,0xA8,0xB8,0xA8,0x02,0x3F,0xA8,0x18,0xA8,0x03,0xEF,0xD0,
+ 0x10,0xD0,0x02,0xEF,0xF8,0x10,0xF8,0xAA,0x00,0x63,0x18,0x63,0x58,0x00,0x63,0x88,
+ 0x63,0xD0,0x00,0x53,0x18,0x53,0x58,0x00,0x53,0x88,0x53,0xD0,0x00,0xE3,0x68,0xE3,
+ 0x90,0x00,0xE3,0xB8,0xE3,0xD0,0x00,0xCB,0x90,0xCB,0xB0,0x00,0xB3,0x58,0xB3,0x78,
+ 0x00,0x9B,0x80,0x9B,0xA0,0x00,0x93,0x38,0x93,0x58,0x00,0x23,0x88,0x23,0xC0,0x00,
+ 0x1B,0xC0,0x1B,0xE8,0x02,0x97,0x38,0x68,0x38,0x02,0xB7,0x58,0x10,0x58,0x02,0xEF,
+ 0x68,0xE0,0x68,0x02,0xD7,0x70,0xC8,0x70,0x02,0xBF,0x78,0xB0,0x78,0x02,0xA7,0x80,
+ 0x90,0x80,0x02,0x67,0x88,0x48,0x88,0x02,0x27,0x88,0x10,0x88,0x02,0xEF,0x90,0xC8,
+ 0x90,0x02,0xA7,0xA0,0x98,0xA0,0x02,0xBF,0xA8,0xB0,0xA8,0x02,0xD7,0xB0,0xC8,0xB0,
+ 0x02,0xEF,0xB8,0xE0,0xB8,0x02,0x27,0xC0,0x10,0xC0,0x02,0xEF,0xD0,0xD8,0xD0,0x02,
+ 0x67,0xD0,0x50,0xD0,0x02,0xCF,0xD8,0xC0,0xD8,0x02,0xB7,0xE0,0xA8,0xE0,0x02,0x9F,
+ 0xE8,0x88,0xE8,0x02,0x27,0xE8,0x10,0xE8,0x02,0xEF,0xF8,0x10,0xF8,0xAA,0x00,0x7B,
+ 0x80,0x7B,0xA8,0x00,0x7B,0xD0,0x7B,0xF8,0x00,0x33,0x58,0x33,0x80,0x00,0x53,0x58,
+ 0x53,0x80,0x00,0xAB,0x58,0xAB,0x80,0x00,0xCB,0x58,0xCB,0x80,0x00,0x2B,0x80,0x2B,
+ 0xA8,0x00,0xD3,0x80,0xD3,0xA8,0x00,0x23,0xA8,0x23,0xD0,0x00,0x5B,0xA8,0x5B,0xD0,
+ 0x00,0xA3,0xA8,0xA3,0xD0,0x00,0xDB,0xA8,0xDB,0xD0,0x00,0x1B,0xD0,0x1B,0xF8,0x00,
+ 0xE3,0xD0,0xE3,0xF8,0x05,0xB7,0x30,0x48,0x30,0x05,0xCF,0x58,0x30,0x58,0x05,0xD7,
+ 0x80,0x28,0x80,0x05,0xDF,0xA8,0x20,0xA8,0x05,0xE7,0xD0,0x18,0xD0,0x05,0xEF,0xF8,
+ 0x10,0xF8,0xAA,0x10,0x82,0x85,0x8B,0x10,0x85,0x80,0x8B,0x10,0x87,0x85,0x8B,0x81,
+ 0x80,0x80,0x8B,0x81,0x82,0x85,0x8B,0x81,0x85,0x80,0x8B,0x05,0x88,0x77,0x01,0x68,
+ 0x77,0x01,0x6C,0x77,0x03,0x49,0x77,0x05,0x08,0x77,0x01,0xE8,0x76,0x01,0xEC,0x76,
+ 0x05,0xC8,0x76,0x05,0x88,0x76,0x02,0x69,0x76,0x02,0x4A,0x76,0x05,0x28,0x76,0x05,
+ 0xE8,0x75,0x01,0xCA,0x75,0x03,0xA9,0x75,0x01,0x88,0x75,0x01,0x8C,0x75,0x05,0x48,
+ 0x75,0x01,0x28,0x75,0x01,0x2A,0x75,0x01,0x2C,0x75,0x01,0x08,0x75,0x01,0x0A,0x75,
+ 0x01,0x0C,0x75,0x03,0xC8,0x74,0x03,0xAA,0x74,0x03,0x88,0x74,0x05,0x2F,0x77,0x05,
+ 0x0F,0x77,0x02,0xF0,0x76,0x02,0xCF,0x76,0x02,0xD2,0x76,0x05,0x8F,0x76,0x05,0x6F,
+ 0x76,0x01,0x4F,0x76,0x01,0x53,0x76,0x05,0x2F,0x76,0x05,0xEF,0x75,0x02,0xD0,0x75,
+ 0x02,0xB1,0x75,0x05,0x8F,0x75,0x03,0x50,0x75,0x05,0x2F,0x75,0x01,0x0F,0x75,0x01,
+ 0x13,0x75,0x01,0xEF,0x74,0x01,0xF1,0x74,0x01,0xF3,0x74,0x02,0xD1,0x74,0x00,0x00,
+ 0x00,0x23,0x68,0x01,0x11,0x00,0x00,0x00,0x10,0xDB,0x68,0x01,0x40,0x00,0x00,0x08,
+ 0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x80,
+ 0x01,0xC0,0xFF,0x01,0xFF,0xFF,0x34,0xC3,0x39,0x00,0x67,0x80,0x69,0x1A,0x01,0x00,
+ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x04,0x00,0x10,0x00,0x00,0x00,0x00,0x00,0x1E,
+ 0x18,0x0B,0x4B,0x14,0x18,0x0B,0x4B,0x1E,0x18,0x0B,0x3B,0x14,0x18,0x0B,0x3B,0x3D,
+ 0x01,0x03,0x02,0x4D,0x01,0x04,0x01,0x27,0x70,0x01,0xE0,0x00,0x00,0x7F,0x40,0x01,
+ 0x78,0x02,0x00,0x27,0x49,0x0C,0xF0,0x7F,0x49,0x0C,0x88,0x1E,0x07,0x03,0x09,0x24,
+ 0x64,0xBB,0xC0,0x23,0x8D,0x7B,0xB4,0x1B,0x8C,0x7C,0x64,0x4B,0x0E,0x04,0x02,0x23,
+ 0x46,0x03,0x68,0xDB,0x46,0x03,0x68,0x17,0x50,0x00,0x5C,0xE7,0xD0,0x00,0x5C,0x8C,
+ 0x50,0x00,0x84,0x73,0xD0,0x00,0x84,0x17,0x50,0x00,0xD4,0xE7,0xD0,0x00,0xD4,0x53,
+ 0x73,0x0A,0xA0,0x8B,0x74,0x0A,0xF0,0xDB,0x75,0x0A,0xA0,0x5B,0x73,0x0A,0xC8,0xE3,
+ 0x74,0x0A,0x60,0x1B,0x75,0x0A,0x80,0xDB,0x73,0x0A,0xC8,0x93,0x74,0x0A,0xF0,0x33,
+ 0x75,0x0A,0x50,0x44,0x03,0x08,0x04,0x37,0xF4,0x37,0xC0,0x37,0x8C,0x77,0x70,0x77,
+ 0xA4,0x77,0xD8,
+};
+static inline uint8_t rom_byte(unsigned a) {
+ if (a - 0x0D17u < 16u) return rom_0D17[a - 0x0D17u];
+ if (a - 0x101Bu < 4u) return rom_101B[a - 0x101Bu];
+ if (a - 0x1121u < 16u) return rom_1121[a - 0x1121u];
+ if (a - 0x117Eu < 8u) return rom_117E[a - 0x117Eu];
+ if (a - 0x11A2u < 4u) return rom_11A2[a - 0x11A2u];
+ if (a - 0x3529u < 60u) return rom_3529[a - 0x3529u];
+ if (a - 0x364Bu < 443u) return rom_364B[a - 0x364Bu];
+ if (a - 0x380Du < 1635u) return rom_380D[a - 0x380Du];
+ return 0;
+}
+#define ROM(a) rom_byte((uint16_t)(a))
+#endif
diff --git a/games/donkeykong/src/save.c b/games/donkeykong/src/save.c
index 23defcbf..4d87f98f 100644
--- a/games/donkeykong/src/save.c
+++ b/games/donkeykong/src/save.c
@@ -1,16 +1,186 @@
-/* SPDX-License-Identifier: GPL-3.0-only */
-#include "game.h"
+/* The save (save.h): a magic, a version, the profile, the game packed (PackBits) and a checksum. Anything
+ * else (another version, damaged, cut short) is not read, and the copy in the Python script is tried. */
#include <string.h>
-static void put32(uint8_t *p,uint32_t x){for(unsigned i=0;i<4;++i)p[i]=(uint8_t)(x>>(i*8));}
-static uint32_t get32(const uint8_t *p){return p[0]|(uint32_t)p[1]<<8|(uint32_t)p[2]<<16|(uint32_t)p[3]<<24;}
-static uint32_t hash(const uint8_t *p){uint32_t h=2166136261u;for(unsigned i=0;i<28;++i)h=(h^p[i])*16777619u;return h;}
-void dk_encode_stats(const dk_stats *s,uint8_t out[DK_SAVE_BYTES]) {
- memset(out,0,DK_SAVE_BYTES);memcpy(out,"DKA1",4);put32(out+4,s->best);put32(out+8,s->runs);
- put32(out+12,s->rescues);put32(out+16,s->best_round);put32(out+28,hash(out));
-}
-bool dk_decode_stats(dk_stats *s,const uint8_t *data,size_t size) {
- if(!data||size!=DK_SAVE_BYTES||memcmp(data,"DKA1",4)||get32(data+28)!=hash(data))return false;
- dk_stats next={get32(data+4),get32(data+8),get32(data+12),(uint16_t)get32(data+16)};
- if(next.best>DK_MAX_SCORE||get32(data+16)>999)return false;
- *s=next;return true;
+#include "plat.h"
+#include "save.h"
+
+uint32_t dk_sum(const void *data, uint32_t n) {
+ const uint8_t *p = data;
+ uint32_t h = 2166136261u;
+ for (uint32_t i = 0; i < n; i++) h = (h ^ p[i]) * 16777619u;
+ return h;
+}
+
+#define RAW_SIZE (DK_SNAP + 0x400 + 6)
+static uint8_t raw[RAW_SIZE];
+
+static void flatten(const dk_state *s) {
+ memcpy(raw, s->ram, DK_SNAP);
+ memcpy(raw + DK_SNAP, s->vram, 0x400);
+ uint8_t *t = raw + DK_SNAP + 0x400;
+ t[0] = s->palette, t[1] = s->over;
+ for (int i = 0; i < 4; i++) t[2 + i] = (uint8_t)(s->loop >> (8 * i));
+}
+
+uint32_t dk_pack(const dk_state *s, uint8_t *out, uint32_t room) {
+ flatten(s);
+ uint32_t i = 0, o = 0;
+ while (i < RAW_SIZE) {
+ uint32_t run = 1;
+ while (i + run < RAW_SIZE && run < 129 && raw[i + run] == raw[i]) run++;
+ if (run >= 3) {
+ if (o + 2 > room) return 0;
+ out[o++] = (uint8_t)(126 + run), out[o++] = raw[i];
+ i += run;
+ continue;
+ }
+ uint32_t lit = 0; /* literals until a run of 3 */
+ while (i + lit < RAW_SIZE && lit < 128 &&
+ !(i + lit + 2 < RAW_SIZE && raw[i + lit] == raw[i + lit + 1] && raw[i + lit] == raw[i + lit + 2]))
+ lit++;
+ if (o + 1 + lit > room) return 0;
+ out[o++] = (uint8_t)(lit - 1);
+ memcpy(out + o, raw + i, lit);
+ o += lit, i += lit;
+ }
+ return o;
+}
+
+bool dk_unpack(dk_state *s, const uint8_t *in, uint32_t n) {
+ uint32_t i = 0, o = 0;
+ while (i < n) {
+ uint8_t c = in[i++];
+ if (c < 128) {
+ uint32_t lit = c + 1u;
+ if (i + lit > n || o + lit > RAW_SIZE) return false;
+ memcpy(raw + o, in + i, lit);
+ i += lit, o += lit;
+ } else {
+ uint32_t run = c - 126u;
+ if (i >= n || o + run > RAW_SIZE) return false;
+ memset(raw + o, in[i++], run);
+ o += run;
+ }
+ }
+ if (o != RAW_SIZE) return false;
+ memset(s, 0, sizeof *s);
+ memcpy(s->ram, raw, DK_SNAP);
+ memcpy(s->vram, raw + DK_SNAP, 0x400);
+ const uint8_t *t = raw + DK_SNAP + 0x400;
+ s->palette = t[0], s->over = t[1];
+ s->loop = (uint32_t)t[2] | (uint32_t)t[3] << 8 | (uint32_t)t[4] << 16 | (uint32_t)t[5] << 24;
+ return dk_valid(s);
+}
+
+/* the file: magic 4, version 2, length 2, profile 16, packed game 0+, checksum 4 */
+#define HEAD 24
+static uint8_t file[DK_SAVE_MAX];
+static void put32(uint8_t *p, uint32_t v) { for (int i = 0; i < 4; i++) p[i] = (uint8_t)(v >> (8 * i)); }
+static uint32_t get32(const uint8_t *p) { return p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; }
+
+bool dk_file_ok(const uint8_t *d, uint32_t n, dk_profile *p, dk_state *s) {
+ if (!d || n < HEAD + 4 || n > DK_SAVE_MAX) return false;
+ if (get32(d) != DK_MAGIC || (d[4] | d[5] << 8) != DK_VERSION || (uint32_t)(d[6] | d[7] << 8) != n) return false;
+ if (get32(d + n - 4) != dk_sum(d, n - 4)) return false;
+ dk_profile q = {get32(d + 8), get32(d + 12), get32(d + 16), (uint16_t)(d[20] | d[21] << 8), d[22], d[23]};
+ if (q.guide_seen > 1 || q.has_run > 1 || q.best > 999999 || q.best_level > 255) return false;
+ if (q.has_run) {
+ if (!dk_unpack(s, d + HEAD, n - HEAD - 4)) return false;
+ } else if (n != HEAD + 4) {
+ return false;
+ }
+ *p = q;
+ return true;
+}
+
+static const char head[] = "# Donkey Kong keeps a copy of your best score and of a game\n"
+ "# left with Home here, so that installing it again keeps them.\n";
+static const char b64[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
+static uint8_t copy[1 + sizeof head + 2 + sizeof DK_SAVE_NAME + 1 + (DK_SAVE_MAX + 2) / 3 * 4 + 2];
+
+static bool copy_write(const uint8_t *d, uint32_t len) {
+ uint32_t nl = sizeof DK_SAVE_NAME - 1;
+ uint8_t *p = copy;
+ *p++ = 0; /* (a script's first byte: not imported) */
+ memcpy(p, head, sizeof head - 1), p += sizeof head - 1;
+ *p++ = '#', *p++ = '>';
+ memcpy(p, DK_SAVE_NAME, nl), p += nl;
+ *p++ = ':';
+ for (uint32_t i = 0; i < len; i += 3, p += 4) {
+ uint32_t left = len - i, v = (uint32_t)d[i] << 16 | (left > 1 ? (uint32_t)d[i + 1] << 8 : 0) | (left > 2 ? d[i + 2] : 0);
+ p[0] = (uint8_t)b64[v >> 18 & 63];
+ p[1] = (uint8_t)b64[v >> 12 & 63];
+ p[2] = left > 1 ? (uint8_t)b64[v >> 6 & 63] : '=';
+ p[3] = left > 2 ? (uint8_t)b64[v & 63] : '=';
+ }
+ *p++ = '\n';
+ return plat_save(DK_COPY_NAME, copy, (uint32_t)(p - copy));
+}
+
+static int b64v(uint8_t c) {
+ for (int i = 0; i < 64; i++)
+ if ((uint8_t)b64[i] == c) return i;
+ return -1;
+}
+
+static uint32_t copy_read(void) { /* the copy, decoded into file[]: its length, or 0 */
+ uint32_t n, nl = sizeof DK_SAVE_NAME - 1;
+ const uint8_t *c = plat_load(DK_COPY_NAME, &n);
+ if (!c) return 0;
+ for (uint32_t i = 0; i + nl + 3 <= n; i++) {
+ if (c[i] != '#' || c[i + 1] != '>' || memcmp(c + i + 2, DK_SAVE_NAME, nl) || c[i + 2 + nl] != ':') continue;
+ const uint8_t *l = c + i + 3 + nl;
+ uint32_t m = 0, o = 0;
+ while (l + m < c + n && l[m] != '\n' && l[m]) m++;
+ if (m % 4 || m / 4 * 3 > DK_SAVE_MAX) return 0;
+ for (uint32_t k = 0; k < m; k += 4) {
+ int v0 = b64v(l[k]), v1 = b64v(l[k + 1]), v2 = l[k + 2] == '=' ? 0 : b64v(l[k + 2]),
+ v3 = l[k + 3] == '=' ? 0 : b64v(l[k + 3]);
+ if (v0 < 0 || v1 < 0 || v2 < 0 || v3 < 0) return 0;
+ uint32_t w = (uint32_t)(v0 << 18 | v1 << 12 | v2 << 6 | v3);
+ file[o++] = (uint8_t)(w >> 16);
+ if (l[k + 2] != '=') file[o++] = (uint8_t)(w >> 8);
+ if (l[k + 3] != '=') file[o++] = (uint8_t)w;
+ }
+ return o;
+ }
+ return 0;
+}
+
+void save_load(dk_profile *p, dk_state *s) {
+ uint32_t n;
+ const uint8_t *d = plat_load(DK_SAVE_NAME, &n);
+ if (d && n <= DK_SAVE_MAX) {
+ memcpy(file, d, n);
+ if (dk_file_ok(file, n, p, s)) return;
+ }
+ n = copy_read();
+ if (n && dk_file_ok(file, n, p, s)) {
+ plat_save(DK_SAVE_NAME, file, n); /* back where it belongs */
+ return;
+ }
+ memset(p, 0, sizeof *p);
+}
+
+bool save_write(const dk_profile *p, const dk_state *s) {
+ uint32_t n = HEAD;
+ memset(file, 0, HEAD);
+ put32(file, DK_MAGIC);
+ file[4] = DK_VERSION;
+ put32(file + 8, p->best);
+ put32(file + 12, p->games);
+ put32(file + 16, p->boards);
+ file[20] = (uint8_t)p->best_level, file[21] = (uint8_t)(p->best_level >> 8);
+ file[22] = p->guide_seen;
+ file[23] = 0;
+ if (s) {
+ uint32_t k = dk_pack(s, file + HEAD, DK_SAVE_MAX - HEAD - 4);
+ if (k) file[23] = 1, n += k;
+ }
+ file[6] = (uint8_t)(n + 4), file[7] = (uint8_t)((n + 4) >> 8);
+ put32(file + n, dk_sum(file, n));
+ n += 4;
+ bool ok = plat_save(DK_SAVE_NAME, file, n);
+ copy_write(file, n);
+ return ok;
}
diff --git a/games/donkeykong/src/save.h b/games/donkeykong/src/save.h
new file mode 100644
index 00000000..f3dea7bb
--- /dev/null
+++ b/games/donkeykong/src/save.h
@@ -0,0 +1,27 @@
+/* What's kept, in donkeykong.sav: the best score and a few totals, and a game left with Home (the arcade's
+ * RAM and tile map, packed), with a copy in a Python script, donkeykong_saves.py (installing apps from the
+ * NumWorks website keeps only scripts, so the save comes back from it). */
+#ifndef DK_SAVE_H
+#define DK_SAVE_H
+#include "dk.h"
+
+typedef struct {
+ uint32_t best, games, boards;
+ uint16_t best_level;
+ uint8_t guide_seen, has_run;
+} dk_profile;
+
+#define DK_SAVE_NAME "donkeykong.sav"
+#define DK_COPY_NAME "donkeykong_saves.py"
+#define DK_MAGIC 0x31474B44u /* "DKG1" */
+#define DK_VERSION 1
+#define DK_SNAP 0xB00 /* the RAM kept: 0x6000-0x6AFF (the rest is the arcade's stack) */
+#define DK_SAVE_MAX 4096
+
+uint32_t dk_sum(const void *data, uint32_t n);
+uint32_t dk_pack(const dk_state *s, uint8_t *out, uint32_t room); /* 0 if it doesn't fit */
+bool dk_unpack(dk_state *s, const uint8_t *in, uint32_t n);
+bool dk_file_ok(const uint8_t *d, uint32_t n, dk_profile *p, dk_state *s); /* p, s filled when true */
+void save_load(dk_profile *p, dk_state *s); /* the save, its copy, or a fresh start */
+bool save_write(const dk_profile *p, const dk_state *s);
+#endif
diff --git a/games/donkeykong/src/sim.c b/games/donkeykong/src/sim.c
new file mode 100644
index 00000000..e775477e
--- /dev/null
+++ b/games/donkeykong/src/sim.c
@@ -0,0 +1,2748 @@
+/* Donkey Kong's program, ported from the arcade's Z80 code (furrykef's dkdasm and tcdev's listing).
+ *
+ * Every routine below is one of the ROM's, named by its address, and keeps its arithmetic: 8-bit
+ * values that wrap, the same tables (rom.h), the same RAM addresses (the arcade's 0x6000-0x6BFF, with
+ * its sprite table at 0x6900) and the same tile map (0x7400-0x77FF), which the game reads back to find
+ * girders under Mario, barrels and fires. dk_tick() is one vblank: the game routine of the current
+ * GameMode2, then the foreground tasks (score, bonus box), the extra life, the difficulty clock and the
+ * oil drum's fire clock. Coins, the attract mode, two players, the name entry and sound are left out. */
+#include <string.h>
+#include "dk.h"
+#include "rom.h"
+
+static dk_state *S;
+#define R(a) (S->ram[((uint16_t)(a) - 0x6000) & 0xFFF])
+#define V(a) (S->vram[((uint16_t)(a) - 0x7400) & 0x3FF])
+#define U8(x) ((uint8_t)(x))
+
+/* the arcade's named variables */
+#define GAME_MODE1 0x6005
+#define NO_CREDITS 0x6007
+#define WAIT_LSB 0x6008
+#define WAIT_MSB 0x6009
+#define GAME_MODE2 0x600A
+#define INPUT 0x6010 /* the joystick, with bit 7 for a jump just pressed */
+#define RAW 0x6011 /* IN0 as read */
+#define RNG1 0x6018
+#define RNG2 0x6019
+#define FRAME 0x601A /* counts down once a frame */
+#define START_LIVES 0x6020
+#define EXTRA_AT 0x6021
+#define SCREEN 0x6227 /* 1 25m, 2 50m, 3 75m, 4 100m */
+#define LIVES 0x6228
+#define LEVEL 0x6229
+#define DIFFICULTY 0x6380
+#define MARIO_ALIVE 0x6200
+#define MARIO_X 0x6203
+#define MARIO_Y 0x6205
+
+/* ------------------------------------------------------------------ the Z80 helpers */
+
+static uint8_t rng(void) { /* 0x0057 */
+ uint8_t a = U8(R(RNG1) + R(FRAME) + R(RNG2));
+ R(RNG1) = a;
+ return a;
+}
+
+static bool rst18(void) { return --R(WAIT_MSB) == 0; } /* RST #18: true when the wait is over */
+
+static bool screen_in(uint8_t mask) { /* RST #30 */
+ uint8_t b = R(SCREEN), a = mask, cy = 0;
+ do {
+ cy = a & 1;
+ a = U8(a >> 1 | cy << 7);
+ } while (--b);
+ return cy;
+}
+
+static void add_each(uint16_t hl, int n, uint8_t c, int step) { /* 0x003D: (HL) += C, n times, step apart */
+ for (int i = 0; i < n; i++) R(hl + i * step) = U8(R(hl + i * step) + c);
+}
+static void move_kong(uint16_t hl, uint8_t c) { add_each(hl, 10, c, 4); } /* RST #38 */
+
+static void copy_rom(uint16_t dst, uint16_t src, int n) {
+ for (int i = 0; i < n; i++) R(dst + i) = ROM(src + i);
+}
+static void kong_pose(uint16_t src) { copy_rom(0x6908, src, 0x28); } /* 0x004E */
+
+static void write3(uint16_t hl, uint8_t a, int de) { /* 0x0514 */
+ for (int i = 0; i < 3; i++) V(hl + i * de) = U8(a - i);
+}
+
+static uint16_t vram_at(uint8_t x, uint8_t y) { /* 0x2FF0: the tile under game point (x, y) */
+ return (uint16_t)(0x7400 + ((U8(~x) & 0xF8) << 2) + (y >> 3));
+}
+#define TILE(x, y) V(vram_at(x, y))
+
+/* ------------------------------------------------------------------ foreground tasks (0x309F, 0x02E3) */
+
+static void task(uint8_t d, uint8_t e) {
+ uint8_t l = R(0x60B0);
+ if (!(R(0x6000 + l) & 0x80)) return; /* the list is full */
+ R(0x6000 + l) = d;
+ l++;
+ R(0x6000 + l) = e;
+ l++;
+ if (l < 0xC0) l = 0xC0;
+ R(0x60B0) = l;
+}
+
+static void show_digits(uint16_t ix, uint16_t hl, int n) { /* 0x057C / 0x0583 */
+ for (int b = 0; b < n; b++, hl--) {
+ V(ix) = R(hl) >> 4;
+ ix -= 0x20;
+ V(ix) = R(hl) & 0x0F;
+ ix -= 0x20;
+ }
+}
+static void show_score(void) { show_digits(0x7781, 0x60B4, 3); }
+static void show_high(void) { show_digits(0x7641, 0x60BA, 3); }
+
+static uint8_t daa_add(uint8_t a, uint8_t b, unsigned *cy) { /* ADC A,b then DAA */
+ unsigned r = a + b + *cy, h = (a & 15) + (b & 15) + *cy > 15, c = r > 0xFF, diff = 0;
+ r &= 0xFF;
+ if (h || (r & 15) > 9) diff |= 6;
+ if (c || r > 0x99) diff |= 0x60, c = 1;
+ *cy = c;
+ return U8(r + diff);
+}
+
+static uint8_t daa_sub(uint8_t a, uint8_t b) { /* SUB b then DAA */
+ unsigned r = U8(a - b), h = (a & 15) < (b & 15), c = a < b, diff = 0;
+ if (h || (r & 15) > 9) diff |= 6;
+ if (c || r > 0x99) diff |= 0x60;
+ return U8(r - diff);
+}
+
+static void add_score(uint8_t p) { /* task 0, 0x051C: p from the table at 0x3529 */
+ unsigned cy = 0;
+ for (int i = 0; i < 3; i++) R(0x60B2 + i) = daa_add(R(0x60B2 + i), ROM(0x3529 + U8(p * 3) + i), &cy);
+ show_score();
+ for (int i = 2; i >= 0; i--) { /* a new high score? */
+ if (R(0x60B2 + i) < R(0x60B8 + i)) return;
+ if (R(0x60B2 + i) > R(0x60B8 + i)) {
+ for (int k = 0; k < 3; k++) R(0x60B8 + k) = R(0x60B2 + k);
+ show_high();
+ return;
+ }
+ }
+}
+
+static void print_text(uint8_t p) { /* task 3, 0x05E9 */
+ uint8_t a2 = U8(p * 2);
+ bool erase = p & 0x80;
+ uint16_t t = ROM(0x364B + (a2 & 0x7F)) | ROM(0x364C + (a2 & 0x7F)) << 8;
+ if (t < 0x368A) return; /* (the name entry's rows, in RAM: not used) */
+ uint16_t hl = ROM(t) | ROM(t + 1) << 8, de = t + 2;
+ for (;;) {
+ uint8_t a = ROM(de);
+ if (a == 0x3F) return;
+ V(hl) = erase ? 0x10 : a;
+ de++;
+ hl -= 0x20;
+ }
+}
+
+static void bonus_box(void) { /* 0x064D..0x0690: the box and its digits */
+ uint16_t de = 0x7465, src = 0x384A;
+ for (int col = 0; col < 6; col++, de += 0x20)
+ for (int k = 0; k < 3; k++) V(de + k) = ROM(src++);
+ uint8_t t = R(0x638C), b = t & 0x0F, a = t >> 4;
+ if (!a) {
+ R(0x6089) = 3; /* (the running-out music) */
+ V(0x7486) = 0x70;
+ V(0x74A6) = 0x70;
+ b = U8(0x70 + b);
+ a = 0x10;
+ }
+ V(0x74E6) = a;
+ V(0x74C6) = b;
+}
+
+static void bonus_task(uint8_t p) { /* task 5, 0x062A */
+ if (p == 0) { /* 0x0691: the bonus into the score */
+ uint8_t b = R(0x638C);
+ add_score(b & 0x0F);
+ add_score(U8((b >> 4) + 0x0A));
+ return;
+ }
+ uint8_t t = R(0x638C);
+ if (t == 0) {
+ if (R(0x63B8)) return;
+ uint8_t a = R(0x62B0), n = 0; /* how many tens (0x063D, and its wrap on level 22) */
+ do {
+ n++;
+ a = U8(a - 10);
+ } while (a);
+ R(0x638C) = U8(n << 4 | n >> 4);
+ bonus_box();
+ return;
+ }
+ /* 0x06A8: a hundred less, in BCD */
+ if (t == 1) R(0x63B8) = 1;
+ R(0x638C) = daa_sub(t, 1);
+ bonus_box();
+}
+
+static void lives_task(uint8_t p) { /* task 6, 0x06B8: lives (less p) and L= */
+ for (int i = 0; i < 6; i++) V(0x7783 - i * 0x20) = 0x10;
+ uint8_t n = U8(R(LIVES) - p);
+ for (int i = 0; i < n && 0x7783 - i * 0x20 >= 0x7403; i++) V(0x7783 - i * 0x20) = 0xFF;
+ V(0x7503) = 0x1C;
+ V(0x74E3) = 0x34;
+ if (R(LEVEL) >= 100) R(LEVEL) = 99;
+ uint8_t a = R(LEVEL), tens = 0;
+ while (a >= 10) a -= 10, tens++;
+ V(0x74A3) = a;
+ V(0x74C3) = tens;
+}
+
+static void run_task(uint8_t t, uint8_t p) {
+ switch (t & 0x0F) {
+ case 0: add_score(p); break;
+ case 1: /* clear a score, then show it */
+ if (p == 0) R(0x60B2) = R(0x60B3) = R(0x60B4) = 0;
+ show_score();
+ break;
+ case 2:
+ if (p == 0 || p == 3) show_score();
+ if (p >= 2) show_high();
+ break;
+ case 3: print_text(p); break;
+ case 5: bonus_task(p); break;
+ case 6: lives_task(p); break;
+ }
+}
+
+static void run_tasks(void) { /* 0x02BD */
+ for (int guard = 0; guard < 64; guard++) {
+ uint8_t l = R(0x60B1), t = R(0x6000 + l);
+ if (t & 0x80) return;
+ R(0x6000 + l) = 0xFF;
+ l++;
+ uint8_t p = R(0x6000 + l);
+ R(0x6000 + l) = 0xFF;
+ l++;
+ if (l < 0xC0) l = 0xC0;
+ R(0x60B1) = l;
+ run_task(t, p);
+ }
+}
+
+/* ------------------------------------------------------------------ the foreground's own clocks */
+
+static void flash_1up(void) { /* 0x0315 */
+ uint8_t b = R(FRAME);
+ if (b & 0x0F) return;
+ if (b & 0x10) {
+ V(0x7740) = V(0x7720) = V(0x7700) = 0x10;
+ return;
+ }
+ V(0x7740) = 1;
+ V(0x7720) = 0x25;
+ V(0x7700) = 0x20;
+}
+
+static void extra_life(void) { /* 0x0350 */
+ if (R(0x622D)) return;
+ uint8_t a = U8((R(0x60B3) & 0xF0) | (R(0x60B4) & 0x0F));
+ a = U8(a >> 4 | a << 4);
+ if (a < R(EXTRA_AT)) return;
+ R(0x622D) = 1;
+ R(LIVES)++;
+ lives_task(1);
+}
+
+static void difficulty_clock(void) { /* 0x037F */
+ uint8_t a = R(0x6384)++;
+ if (a) return;
+ uint8_t b = R(0x6381)++;
+ if (b & 7) return;
+ a = U8((U8(b >> 3 | b << 5)) + R(LEVEL));
+ if (a >= 5) a = 5;
+ R(DIFFICULTY) = a;
+}
+
+static void drum_flame(uint8_t b) { /* 0x03F2 */
+ R(0x6A29) = b;
+ if (R(RNG2) & 1) return;
+ R(0x6A29) = U8(b + 1);
+}
+
+static void fire_clock(void) { /* 0x03A2: the oil drum's flame and its fires (25m, 50m) */
+ if (!screen_in(3) || !(R(MARIO_ALIVE) & 1) || (R(0x6350) & 1)) return;
+ if (--R(0x62B8)) return;
+ R(0x62B8) = 4;
+ uint8_t a = R(0x62B9);
+ if (!(a & 1)) return;
+ if (!(a & 2)) {
+ R(0x66A9) = 2;
+ R(0x66AA) = 0;
+ drum_flame(0x40);
+ R(0x62BA) = 0x10;
+ return;
+ }
+ R(0x66A9) = 2;
+ R(0x66AA) = 2;
+ drum_flame(0x42);
+ if (--R(0x62BA)) return;
+ R(0x62B9) = 1;
+ R(0x63A0) = 1;
+ R(0x62BA) = 0x10;
+}
+
+/* ------------------------------------------------------------------ screens: clearing and drawing boards */
+
+static void clear_all(void) { /* 0x0852 */
+ memset(S->vram, 0x10, sizeof S->vram);
+ memset(&R(0x6900), 0, 0x180);
+}
+
+static void clear_play(void) { /* 0x0874: rows 4-31, the middle of rows 2-3, and the sprites */
+ for (int col = 0; col < 32; col++)
+ for (int row = 4; row < 32; row++) V(0x7400 + col * 32 + row) = 0x10;
+ for (int i = 0; i < 14; i++) V(0x7522 + i * 0x20) = V(0x7523 + i * 0x20) = 0x10;
+ memset(&R(0x6900), 0, 0x180);
+}
+
+static void draw_board(uint16_t de) { /* 0x0DA7 */
+ for (;;) {
+ uint8_t type = ROM(de);
+ R(0x63B3) = type;
+ if (type == 0xAA) return;
+ uint8_t x1 = ROM(de + 1), y1 = ROM(de + 2), x2 = ROM(de + 3), y2 = ROM(de + 4);
+ uint16_t start = vram_at(x1, y1), end = vram_at(x2, y2);
+ uint8_t k1 = y1 & 7, k2 = y2 & 7;
+ uint8_t dx = U8(x2 - x1);
+ if (x2 < x1) dx = U8(-dx); /* (NEG) */
+ uint8_t dy = U8(y2 - y1);
+ de += 5;
+ if (type < 2) { /* a ladder (1: broken) */
+ uint8_t len = U8(U8(dy - 0x10) + k1);
+ uint16_t hl = start;
+ V(hl) = U8(k1 + 0xF0);
+ hl++;
+ V(hl) = U8(k1 + 0xC0);
+ if (type == 1) len = 0;
+ for (;;) {
+ uint8_t n = U8(len - 8);
+ bool borrow = len < 8;
+ len = n;
+ if (borrow) break;
+ hl++;
+ V(hl) = 0xC0;
+ }
+ hl = end;
+ V(hl) = U8(k2 + 0xD0);
+ if (type == 1) V(hl - 1) = 0xC0;
+ if (k2) V(hl + 1) = U8(k2 + 0xE0);
+ } else if (type == 2) { /* a girder, right to left: two tiles per pixel of slope */
+ uint8_t t = U8(k1 + 0xF0), left = dx;
+ uint16_t hl = start;
+ for (;;) {
+ V(hl) = t; /* 0x0E62 */
+ hl++;
+ if ((hl & 0x1F) && t != 0xF0) V(hl) = U8(t - 0x10);
+ hl += 0x1F;
+ if (left < 8) break;
+ left = U8(left - 8);
+ if (dy == 0) continue;
+ V(hl) = t; /* 0x0E8F */
+ hl++;
+ if (hl & 0x1F) V(hl) = U8(t - 0x10);
+ hl += 0x1F;
+ if (left < 8) break;
+ left = U8(left - 8);
+ if (!(dy & 0x80)) {
+ t++;
+ if (t == 0xF8) {
+ hl++;
+ t = 0xF0;
+ }
+ if (!(hl & 0x1F)) break;
+ } else {
+ t--;
+ if ((int8_t)U8(t - 0xF0) < 0) {
+ hl--;
+ t = 0xF7;
+ }
+ }
+ }
+ } else if (type == 3) { /* a conveyor */
+ uint16_t hl = start;
+ V(hl) = 0xB3;
+ hl += 0x20;
+ bool borrow = dx < 0x10;
+ uint8_t left = U8(dx - 0x10);
+ while (!borrow) {
+ V(hl) = 0xB1;
+ hl += 0x20;
+ borrow = left < 8;
+ left = U8(left - 8);
+ }
+ V(hl) = 0xB2;
+ } else if (type <= 6) { /* 4: blank (E0), 5: a 100m girder (B0), 6: the 50m cross (FE) */
+ uint8_t t = type == 4 ? 0xE0 : type == 5 ? 0xB0 : 0xFE;
+ uint16_t hl = start;
+ int left = dx;
+ for (;;) {
+ V(hl) = t;
+ hl += 0x20;
+ left -= 8;
+ if (left < 0) break;
+ }
+ }
+ }
+}
+
+/* ------------------------------------------------------------------ a board's start (0x0C92..0x0DA6, 0x0F56..0x123B) */
+
+static void copy4(uint16_t hl, uint16_t de, int b, uint8_t c) { /* 0x122A */
+ for (; b > 0; b--) {
+ for (int i = 0; i < 4; i++) R((de & 0xFF00) | U8(de + i)) = ROM(hl + i);
+ de = (de & 0xFF00) | U8(de + 4 + c);
+ }
+}
+
+static void place2(uint16_t hl, uint16_t de, int b, uint8_t c) { /* 0x11EC: X and Y from a table */
+ for (; b > 0; b--) {
+ R(de) = ROM(hl++);
+ de = (de & 0xFF00) | U8(de + 2);
+ R(de) = ROM(hl++);
+ de = (de & 0xFF00) | U8(de + c);
+ }
+}
+
+static void to_sprites(uint16_t ix, uint16_t hl, int b, int de) { /* 0x11D3 */
+ for (; b > 0; b--, ix += de, hl += 4) {
+ R(hl) = R(ix + 3);
+ R(hl + 1) = R(ix + 7);
+ R(hl + 2) = R(ix + 8);
+ R(hl + 3) = R(ix + 5);
+ }
+}
+
+static void hammers(uint16_t hl) { /* 0x11A6 */
+ place2(hl, 0x6683, 2, 0x0E);
+ copy4(0x3E08, 0x6687, 2, 0x0C);
+ R(0x6680) = 1;
+ R(0x6690) = 1;
+ to_sprites(0x6680, 0x6A18, 2, 0x10);
+}
+
+static void drum_fire(uint16_t hl) { /* 0x11FA */
+ R(0x66A0) = 1;
+ R(0x66A3) = R(0x6A28) = ROM(hl);
+ R(0x66A7) = R(0x6A29) = ROM(hl + 1);
+ R(0x66A8) = R(0x6A2A) = ROM(hl + 2);
+ R(0x66A5) = R(0x6A2B) = ROM(hl + 3);
+ R(0x66A9) = ROM(hl + 4);
+ R(0x66AA) = ROM(hl + 5);
+}
+
+static void springs_init(void) { /* 0x1186 */
+ copy4(0x11A2, 0x6507, 10, 0x0C);
+ to_sprites(0x6500, 0x6980, 10, 0x10);
+}
+
+static void board_ram(void) { /* 0x0F56 */
+ memset(&R(0x6200), 0, 0x27);
+ memset(&R(0x6280), 0, 0x880);
+ copy_rom(0x6280, 0x3D9C, 0x40);
+ uint8_t l = R(LEVEL), a = U8(U8(l * 10) + 0x28);
+ if (a >= 0x51) a = 0x50;
+ R(0x62B0) = R(0x62B1) = R(0x62B2) = a;
+ a = U8(0xDC - U8(a * 2));
+ if (a < 0x28) a = 0x28;
+ R(0x62B3) = R(0x62B4) = a;
+ R(0x6209) = 4;
+ R(0x620A) = 8;
+ uint8_t screen = R(SCREEN);
+ if (!(screen & 4)) /* black squares over the girders at the top of Kong's ladders */
+ for (int i = 0; i < 3; i++) {
+ R(0x6A00 + i * 4) = U8(0x4F + i * 0x10);
+ R(0x6A01 + i * 4) = 0x3A;
+ R(0x6A02 + i * 4) = 0x0F;
+ R(0x6A03 + i * 4) = 0x18;
+ }
+ switch (screen) {
+ case 1: /* 0x0FD7 */
+ copy_rom(0x69A8, 0x3DDC, 0x10);
+ copy4(0x3DEC, 0x6407, 5, 0x1C);
+ drum_fire(0x3DF4);
+ copy_rom(0x69FC, 0x3E00, 4);
+ hammers(0x3E0C);
+ copy4(0x101B, 0x6707, 8, 0x1C);
+ copy4(0x101B, 0x6807, 2, 0x1C);
+ break;
+ case 2: /* 0x101F */
+ copy4(0x3DEC, 0x6407, 5, 0x1C);
+ springs_init();
+ copy4(0x3E18, 0x65A7, 6, 0x0C);
+ to_sprites(0x65A0, 0x69B8, 6, 0x10);
+ drum_fire(0x3DFA);
+ copy_rom(0x69FC, 0x3E04, 4);
+ copy_rom(0x6944, 0x3E1C, 8);
+ copy_rom(0x69E4, 0x3E24, 0x18);
+ hammers(0x3E10);
+ copy_rom(0x6A0C, 0x3E3C, 0x0C);
+ R(0x62B9) = 1;
+ break;
+ case 3: /* 0x1087 */
+ copy4(0x3DEC, 0x6407, 5, 0x1C);
+ springs_init();
+ for (int i = 0; i < 6; i++) R(0x6600 + i * 0x10) = 1;
+ for (int i = 0; i < 3; i++) R(0x660D + i * 0x10) = 8;
+ place2(0x3E64, 0x6603, 6, 0x0E);
+ copy4(0x3E60, 0x6607, 6, 0x0C);
+ to_sprites(0x6600, 0x6958, 6, 0x10);
+ copy_rom(0x6A0C, 0x3E48, 0x0C);
+ R(0x6400) = 1, R(0x6403) = R(0x640E) = 0x58, R(0x6405) = R(0x640F) = 0x80;
+ R(0x6420) = 1, R(0x6423) = R(0x642E) = 0xEB, R(0x6425) = R(0x642F) = 0x60;
+ copy_rom(0x6970, 0x1121, 0x10);
+ break;
+ case 4: /* 0x1131 */
+ copy4(0x3DF0, 0x6407, 5, 0x1C);
+ hammers(0x3E14);
+ copy_rom(0x6A0C, 0x3E54, 0x0C);
+ place2(0x1182, 0x64A3, 2, 0x1E);
+ copy4(0x117E, 0x64A7, 2, 0x1C);
+ R(0x64A0) = 1;
+ R(0x64C0) = 1;
+ to_sprites(0x64A0, 0x6950, 2, 0x20);
+ break;
+ }
+}
+
+static void ladder_table(void) { /* 0x2441: ladders at 0x6300 (x), +0x15 (top), +0x2A (bottom) */
+ static const uint16_t boards[4] = {0x3AE4, 0x3B5D, 0x3BE5, 0x3C8B};
+ uint16_t hl = boards[(R(SCREEN) - 1) & 3], ix = 0x6300, iy = 0x6310;
+ for (int guard = 0; guard < 64; guard++) {
+ uint8_t a = ROM(hl);
+ if (a == 0xAA) return;
+ if (a > 1) {
+ hl += 5;
+ continue;
+ }
+ uint16_t *p = a == 0 ? &ix : &iy;
+ R(*p) = ROM(hl + 1);
+ R(*p + 0x15) = ROM(hl + 2);
+ R(*p + 0x2A) = ROM(hl + 4);
+ (*p)++;
+ hl += 5;
+ }
+}
+
+static void start_board(void) { /* 0x0C92 */
+ clear_play();
+ R(0x638C) = 0;
+ task(5, 1);
+ S->palette = 2;
+ uint16_t table;
+ switch (R(SCREEN)) {
+ case 1: table = 0x3AE4, R(0x6089) = 8; break;
+ case 2: table = 0x3B5D, S->palette = 1, R(0x6089) = 9; break;
+ case 3: /* 0x0D27: the elevator shafts */
+ for (int s = 0; s < 2; s++) {
+ uint16_t hl = s ? 0x760D : 0x770D;
+ for (int i = 0; i < 0x11; i++) V(hl++) = 0xFD;
+ hl += 0x0F;
+ for (int i = 0; i < 0x11; i++) V(hl++) = 0xFC;
+ }
+ R(0x6089) = 0x0A;
+ table = 0x3BE5;
+ break;
+ default: /* 0x0D43: the bars beside Kong */
+ for (int s = 0; s < 2; s++) {
+ uint16_t hl = s ? 0x7547 : 0x7687;
+ for (int i = 0; i < 4; i++) V(hl++) = 0xFD;
+ hl += 0x1C;
+ for (int i = 0; i < 4; i++) V(hl++) = 0xFC;
+ }
+ S->palette = 3;
+ R(0x6089) = 0x0B;
+ table = 0x3C8B;
+ break;
+ }
+ draw_board(table);
+ if (R(SCREEN) == 4) /* 0x0D00: the rivets */
+ for (int i = 0; i < 8; i++) {
+ uint16_t de = ROM(0x0D17 + i * 2) | ROM(0x0D18 + i * 2) << 8;
+ V(de) = 0xB8;
+ V(de + 1) = 0xB7;
+ }
+ if (R(SCREEN) == 2) /* 0x3FA6: the tops of the moving ladders are sprites */
+ for (int i = 0; i < 2; i++) {
+ uint16_t hl = i ? 0x748C : 0x776C;
+ V(hl) = 0x10;
+ V(hl + 2) = 0xC0;
+ }
+ board_ram(); /* 0x0D5F */
+ ladder_table();
+ R(WAIT_MSB) = 0x40;
+ R(GAME_MODE2)++;
+ kong_pose(0x385C);
+ copy_rom(0x6900, 0x3884, 8);
+ if (R(SCREEN) == 4) { /* 0x0D8B */
+ move_kong(0x6908, 0x44);
+ add_each(0x6900, 2, 0x10, 4);
+ add_each(0x6903, 2, 0xF8, 4);
+ } else if (R(SCREEN) == 1) {
+ move_kong(0x690B, 0xFC);
+ }
+}
+
+static void mario_start(void) { /* 0x123C */
+ if (!rst18()) return;
+ uint8_t x = 0x3F, y = 0xF0;
+ if (R(SCREEN) == 3) x = 0x16, y = 0xE0;
+ R(MARIO_ALIVE) = 1;
+ R(MARIO_X) = R(0x694C) = x;
+ R(0x6207) = R(0x694D) = 0x80;
+ R(0x6208) = R(0x694E) = 2;
+ R(MARIO_Y) = R(0x694F) = y;
+ R(0x620F) = 1;
+ R(GAME_MODE2)++;
+ task(6, 1);
+}
+
+/* ------------------------------------------------------------------ the intro: Kong climbs and bends the girders (0x0A76) */
+
+static void kong_climb_step(void) { /* 0x306F */
+ uint8_t a = ++R(0x62AF);
+ if (a & 7) return;
+ move_kong(0x690B, 0xFC);
+ for (int k = 0; k < 2; k++) { /* 0x3096 on 0x6909 and 0x691D, with DE = 4 (set by RST #38) */
+ uint16_t hl = k ? 0x691D : 0x6909;
+ for (int b = 0; b < 2; b++, hl += 4) R(hl) ^= 0x81;
+ }
+ R(0x692D) ^= rng() & 0x80; /* Pauline wriggles */
+}
+
+static void roll_ladder(void) { /* 0x304A: the ladder behind Kong rolls up */
+ uint8_t c = R(0x638E);
+ for (int i = 0; i < 2; i++) {
+ uint16_t hl = (i ? 0x75C0 : 0x7600) + c;
+ V(hl - 0x20) = V(hl);
+ }
+ R(0x638E)--;
+}
+
+static void intro(void) { /* 0x0A76 */
+ switch (R(0x6385)) {
+ case 0: /* 0x0A8A */
+ S->palette = 2;
+ draw_board(0x380D);
+ V(0x76A3) = V(0x7663) = 0x10;
+ V(0x75AA) = 0xD4;
+ R(0x62AF) = 0;
+ R(0x63C2) = 0xB4, R(0x63C3) = 0x38;
+ R(0x63C4) = 0xCB, R(0x63C5) = 0x38;
+ R(WAIT_MSB) = 0x40;
+ R(0x6385)++;
+ break;
+ case 1: /* 0x0ABF */
+ if (!rst18()) return;
+ kong_pose(0x388C);
+ move_kong(0x6908, 0x30);
+ move_kong(0x690B, 0x99);
+ R(0x638E) = 0x1F;
+ R(0x690C) = 0;
+ R(0x608A) = 1;
+ R(0x6385)++;
+ break;
+ case 2: /* 0x0AE8 */
+ kong_climb_step();
+ if (!(R(0x62AF) & 0x0F)) roll_ladder();
+ if (R(0x690B) >= 0x5D) return;
+ R(WAIT_MSB) = 0x20;
+ R(0x6385)++;
+ break;
+ case 3:
+ case 5: /* 0x3069 */
+ if (rst18()) R(0x6385)++;
+ break;
+ case 4: { /* 0x0B06: Kong jumps up onto the top girder */
+ if (R(FRAME) & 1) return;
+ uint16_t hl = R(0x63C2) | R(0x63C3) << 8;
+ uint8_t a = ROM(hl);
+ if (a != 0x7F) {
+ hl++;
+ R(0x63C2) = U8(hl), R(0x63C3) = hl >> 8;
+ move_kong(0x690B, a);
+ return;
+ }
+ kong_pose(0x385C);
+ copy_rom(0x6900, 0x3884, 8);
+ move_kong(0x6908, 0x50);
+ move_kong(0x690B, 0xFC);
+ do roll_ladder(); while (R(0x638E) != 0x0A);
+ R(0x6082) = 3;
+ draw_board(0x392C);
+ V(0x74AA) = V(0x748A) = 0x10;
+ R(0x638D) = 5;
+ R(WAIT_MSB) = 0x20;
+ R(0x6385)++;
+ break;
+ }
+ case 6: { /* 0x0B68: five jumps to the left, each bending a girder */
+ if (R(FRAME) & 1) return;
+ uint16_t hl = R(0x63C4) | R(0x63C5) << 8;
+ uint8_t a = ROM(hl);
+ if (a != 0x7F) {
+ hl++;
+ R(0x63C4) = U8(hl), R(0x63C5) = hl >> 8;
+ move_kong(0x690B, a);
+ move_kong(0x6908, 0xFF);
+ return;
+ }
+ R(0x63C4) = 0xCB, R(0x63C5) = 0x38;
+ R(0x6082) = 3;
+ draw_board(0x38DC + (U8(R(0x638D) - 1) << 4));
+ if (--R(0x638D)) return;
+ R(WAIT_MSB) = 0xB0;
+ R(0x6385)++;
+ break;
+ }
+ case 7: /* 0x0BB3: the roar */
+ if (R(WAIT_MSB) == 0x90) {
+ R(0x608A) = 0x0F;
+ R(0x608B) = 3;
+ R(0x6919)++;
+ } else if (R(WAIT_MSB) == 0x18) {
+ R(0x6919)--;
+ }
+ if (!rst18()) return;
+ R(0x6385) = 0;
+ R(WAIT_MSB)++;
+ R(GAME_MODE2)++;
+ break;
+ }
+}
+
+/* ------------------------------------------------------------------ HOW HIGH CAN YOU GET? (0x0BDA) */
+
+static void how_high(void) {
+ if (!rst18()) return;
+ clear_play();
+ task(6, R(MARIO_ALIVE));
+ S->palette = 1;
+ R(0x608A) = 2;
+ R(0x608B) = 3;
+ R(0x63A7) = 0;
+ R(0x63A8) = 0xDC, R(0x63A9) = 0x76;
+ if (R(0x622E) >= 6) R(0x622E) = 5;
+ if (R(0x622A) != R(0x622F)) R(0x622E)++;
+ R(0x622F) = R(0x622A);
+ uint8_t b = R(0x622E);
+ uint16_t hl = 0x75BC;
+ do {
+ for (uint8_t c = 0x50;; c++) { /* one Kong: 6 columns of 4 tiles */
+ V(hl) = c++;
+ hl--;
+ V(hl) = c++;
+ hl--;
+ V(hl) = c++;
+ hl--;
+ V(hl) = c;
+ if (c == 0x67) break;
+ hl += 0x23;
+ }
+ uint8_t a = R(0x63A7)++;
+ uint16_t t = 0x3CF0 + U8(a << 2), ix = R(0x63A8) | R(0x63A9) << 8;
+ V(ix + 0x60) = ROM(t);
+ V(ix + 0x40) = ROM(t + 1);
+ V(ix + 0x20) = ROM(t + 2);
+ V(ix - 0x20) = 0x8B;
+ ix -= 4;
+ R(0x63A8) = U8(ix), R(0x63A9) = ix >> 8;
+ hl -= 0xA1;
+ } while (--b);
+ task(3, 7);
+ R(WAIT_MSB) = 0xA0;
+ R(GAME_MODE2) += 2;
+}
+
+/* ------------------------------------------------------------------ shared: ladders, slopes, motion, collisions */
+
+/* 0x236E: a ladder at exactly x with an end at d. a = 1 at its top (b: its bottom), 0 at its bottom (b: its top);
+ c: the ladders left in the search (4 or less: a broken one, listed from 0x6310) */
+static bool find_ladder(uint8_t x, uint8_t d, uint8_t *a, uint8_t *b, uint8_t *c) {
+ int i = 0, left = 0x15;
+ while (left > 0) {
+ int at = -1;
+ while (left > 0) {
+ uint8_t v = R(0x6300 + i);
+ i++, left--;
+ if (v == x) {
+ at = i - 1;
+ break;
+ }
+ }
+ if (at < 0) return false;
+ if (d == R(0x6315 + at)) {
+ *a = 1, *b = R(0x632A + at), *c = U8(left);
+ return true;
+ }
+ if (d == R(0x632A + at)) {
+ *a = 0, *b = R(0x6315 + at), *c = U8(left);
+ return true;
+ }
+ }
+ return false;
+}
+
+static uint8_t slope(uint8_t h, uint8_t l, uint8_t b) { /* 0x2333: Y on a 25m girder after a step */
+ uint8_t a = h & 0x0F;
+ if (U8(b - 1) == 0) {
+ if (a >= 1) return l;
+ b = 1;
+ } else {
+ if (a < 0x0F) return l;
+ b = 0xFF;
+ }
+ if (l == 0xF0) return (h & 0x80) ? U8(l - b) : l;
+ if (l == 0x4C) return h < 0x98 ? l : U8(l + b);
+ return (l & 0x20) ? U8(l - b) : U8(l + b);
+}
+
+#define RRC(v) (cy = (v) & 1, (v) = U8((v) >> 1 | cy << 7))
+#define RLC(v) (cy = (v) >> 7, (v) = U8((v) << 1 | cy))
+static uint8_t step_anim(uint8_t a, uint8_t b) { /* 0x3009: the next of four 2-bit states */
+ uint8_t d = a, c, cy;
+ RRC(a);
+ if (cy) {
+ c = 0xB4;
+ RRC(a);
+ RRC(a);
+ if (cy) c = 0x1E;
+ if (b & 4) b--;
+ } else {
+ c = 0x93;
+ RRC(a);
+ RRC(a);
+ if (cy) c = 0x6C;
+ RLC(a);
+ if (!cy) c &= 0xF0;
+ }
+ for (int k = 0; k < 4; k++) {
+ a = c;
+ RRC(a);
+ RRC(a);
+ c = a;
+ if ((a & 3) == b) break;
+ }
+ a = c;
+ RRC(a);
+ RRC(a);
+ a &= 3;
+ if (a != 3) return a;
+ d &= U8(~4);
+ return U8(d - 1) ? a : 4;
+}
+
+static void motion(uint16_t ix) { /* 0x239C: 16-bit X += (+10,+11); 16-bit Y += 16t+8 - (+12,+13) */
+ unsigned s = R(ix + 4) + R(ix + 0x11);
+ R(ix + 4) = U8(s);
+ R(ix + 3) = U8(R(ix + 3) + R(ix + 0x10) + (s >> 8));
+ unsigned y = (unsigned)R(ix + 5) << 8 | R(ix + 6), v0 = (unsigned)R(ix + 0x12) << 8 | R(ix + 0x13);
+ unsigned t = R(ix + 0x14), hl = (y - v0 + (2 * t + 1) * 8) & 0xFFFF;
+ R(ix + 5) = U8(hl >> 8);
+ R(ix + 6) = U8(hl);
+ R(ix + 0x14)++;
+}
+
+static uint16_t speed_now(uint16_t ix) { /* 0x2407: 16t - v0 */
+ unsigned t = R(ix + 0x14);
+ return (uint16_t)((t * 16 - ((unsigned)R(ix + 0x12) << 8 | R(ix + 0x13))) & 0xFFFF);
+}
+
+static bool girder_tile(uint8_t t) { return t >= 0xB0 && (t & 0x0F) < 8; }
+
+/* 0x2913: is anything active in b slots from ix (de apart) within (h + its +9) across and (l + its +0A) down
+ of (iy's X, c)? *left: the slots left when it was found (0 if none) */
+static bool collide(uint16_t iy, uint8_t c, uint8_t h, uint8_t l, uint16_t ix, int b, int de, int *left) {
+ for (; b > 0; b--, ix += de) {
+ if (!(R(ix) & 1)) continue;
+ uint8_t y = R(ix + 5), a = U8(c - y);
+ if (c < y) a = U8(-a);
+ a = U8(a + 1);
+ if (a >= l && U8(a - l) >= R(ix + 0x0A)) continue;
+ uint8_t ox = R(iy + 3), x = R(ix + 3), d = U8(ox - x);
+ if (ox < x) d = U8(-d);
+ if (d >= h && U8(d - h) >= R(ix + 9)) continue;
+ if (left) *left = b;
+ return true;
+ }
+ if (left) *left = 0;
+ return false;
+}
+
+static uint16_t hit_ix;
+static int hit_de, hit_left;
+static bool hostile_hit(uint16_t iy, uint8_t c, uint8_t h, uint8_t l) { /* 0x286F, by board */
+ static const struct { uint16_t ix; uint8_t n, de; } groups[4][3] = {
+ {{0x6700, 10, 0x20}, {0x6400, 5, 0x20}, {0x66A0, 1, 0}},
+ {{0x6400, 5, 0x20}, {0x65A0, 6, 0x10}, {0x66A0, 1, 0}},
+ {{0x6400, 5, 0x20}, {0x6500, 10, 0x10}, {0, 0, 0}},
+ {{0x6400, 7, 0x20}, {0, 0, 0}, {0, 0, 0}}};
+ int s = (R(SCREEN) - 1) & 3;
+ for (int g = 0; g < 3 && groups[s][g].n; g++) {
+ R(0x63B9) = groups[s][g].n;
+ if (collide(iy, c, h, l, groups[s][g].ix, groups[s][g].n, groups[s][g].de, &hit_left)) {
+ hit_ix = groups[s][g].ix, hit_de = groups[s][g].de;
+ return true;
+ }
+ }
+ return false;
+}
+
+static uint8_t count_over(uint16_t ix, int b, uint8_t c, uint8_t h, uint8_t l) { /* 0x3EC3 */
+ for (; b > 0; b--, ix += 0x20) {
+ if (!(R(ix) & 1)) continue;
+ uint8_t y = R(ix + 5), a = U8(c - y);
+ if (c < y) a = U8(-a);
+ a = U8(a + 1);
+ if (a >= l && U8(a - l) >= R(ix + 0x0A)) continue;
+ uint8_t ox = R(MARIO_X), x = R(ix + 3), d = U8(ox - x);
+ if (ox < x) d = U8(-d);
+ if (d >= h && U8(d - h) >= R(ix + 9)) continue;
+ R(0x6060)++;
+ }
+ return R(0x6060);
+}
+
+static uint8_t jumped_over(void) { /* 0x2853, 0x3E88: at the top of a jump */
+ uint8_t c = U8(R(MARIO_Y) + 0x0C), h = (R(INPUT) & 3) ? 0x13 : 5, l = 8;
+ if (R(SCREEN) != 1) return hostile_hit(0x6200, c, h, l);
+ R(0x6060) = 0;
+ count_over(0x6700, 10, c, h, l);
+ uint8_t n = count_over(0x6400, 5, c, h, l);
+ return n == 0 ? 0 : n == 1 ? 1 : n < 3 ? 3 : 7;
+}
+
+/* ------------------------------------------------------------------ Mario */
+
+static void mario_sprite(void) { /* 0x1DA6 */
+ R(0x694C) = R(MARIO_X);
+ R(0x694D) = R(0x6207);
+ R(0x694E) = R(0x6208);
+ R(0x694F) = R(MARIO_Y);
+}
+
+static void walls(uint8_t *d, uint8_t *e) { /* 0x241F: d left wall, e right wall */
+ uint8_t x = R(MARIO_X);
+ *d = 1, *e = 0;
+ if (x < 0x16) return;
+ *d = 0, *e = 1;
+ if (x >= 0xEA) return;
+ *e = 0;
+ if (!(R(SCREEN) & 1) || R(MARIO_Y) >= 0x58 || x >= 0x6C) return;
+ *d = 1;
+}
+
+static void hammer_grab(void) { /* 0x2954 */
+ if (!screen_in(0x0B)) return;
+ int left;
+ uint8_t a = collide(0x6200, R(MARIO_Y), 4, 8, 0x6680, 2, 0x10, &left);
+ R(0x6218) = a;
+ R(0x6085) = U8(a << 6);
+ if (!left) return;
+ if (left == 1) R(0x6691) = 1;
+ else R(0x6681) = 1;
+}
+
+static void push_out(void) { /* 0x2B7A: off the side of a platform */
+ uint8_t x = R(MARIO_X);
+ x = R(0x6210) == 0 ? U8((U8(x - 8) | 7) + 4) : U8((x | 7) - 4);
+ R(MARIO_X) = R(0x694C) = x;
+}
+
+enum { CLEAR = 0, HEAD = 2, LANDED = 1 };
+static int floor_test(uint8_t h, uint8_t l, uint8_t *cout) { /* 0x2B9B: the girder in the tile at (h, l) */
+ uint8_t e = l, t = TILE(h, l), c;
+ *cout = 0;
+ if (!girder_tile(t) || t == 0xC0) return CLEAR;
+ if (t < 0xC0) {
+ c = U8((e & 0xF8) - 1);
+ } else {
+ uint8_t off = (t < 0xD0 || (t >= 0xE0 && t < 0xF0)) ? U8((t & 0x0F) - 9) : U8((t & 0x0F) - 1);
+ c = U8((e & 0xF8) + off);
+ if (c >= e) return CLEAR;
+ }
+ *cout = c;
+ uint8_t a = U8(R(0x620C) - R(MARIO_Y) + e);
+ if (a > c) return HEAD;
+ R(MARIO_Y) = U8(c - 7);
+ return LANDED;
+}
+
+static void elevator_landing(uint8_t *ra, uint8_t *rb) { /* 0x29AF */
+ *ra = *rb = 0;
+ if (!screen_in(4)) return;
+ int left;
+ if (!collide(0x6200, R(MARIO_Y), 4, 8, 0x6600, 6, 0x10, &left)) return;
+ uint16_t ix = 0x6600 + (6 - left) * 0x10;
+ uint8_t d = U8(R(ix + 5) - 4);
+ if (U8(R(0x620C) + 5) < d) {
+ R(MARIO_Y) = U8(d - 8);
+ R(0x6398) = 1;
+ *ra = *rb = 1;
+ return;
+ }
+ if (U8(R(0x620C) - 0x0E) >= d) {
+ R(MARIO_ALIVE) = 0;
+ return;
+ }
+ push_out();
+ *ra = 1;
+}
+
+static void landing(uint8_t *ra, uint8_t *rb) { /* 0x2B1C */
+ uint8_t c;
+ int r;
+ *ra = *rb = 0;
+ if (R(SCREEN) == 1) {
+ uint8_t e = U8(R(MARIO_Y) + 7);
+ r = floor_test(R(MARIO_X), e, &c);
+ if (r == LANDED) *ra = *rb = 1;
+ else if (r == HEAD && U8(e - c) < 4) R(MARIO_Y) = U8(c - 7), *ra = *rb = 1;
+ return;
+ }
+ uint8_t h = U8(R(MARIO_X) - 3), e = U8(R(MARIO_Y) + 7);
+ r = floor_test(h, e, &c);
+ if (r == LANDED) {
+ *ra = *rb = 1;
+ return;
+ }
+ if (r != HEAD) {
+ r = floor_test(U8(h + 7), e, &c);
+ if (r == LANDED) {
+ *ra = *rb = 1;
+ return;
+ }
+ if (r == CLEAR) {
+ elevator_landing(ra, rb);
+ return;
+ }
+ }
+ push_out();
+ *ra = 1;
+}
+
+static void moving_jump(void) { /* 0x1BD8: off a wall */
+ if (R(0x6220) != 1) {
+ uint16_t hl = speed_now(0x6200);
+ R(0x6212) = U8(hl >> 8);
+ R(0x6213) = U8(hl);
+ R(0x6214) = 0;
+ }
+ motion(0x6200);
+}
+
+static void jumping(void) { /* 0x1BB2 */
+ R(0x620B) = R(MARIO_X);
+ R(0x620C) = R(MARIO_Y);
+ motion(0x6200);
+ uint8_t d, e, a, b;
+ walls(&d, &e);
+ if (d == 1) {
+ R(0x6210) = 0, R(0x6211) = 0x80, R(0x6207) |= 0x80;
+ moving_jump();
+ } else if (e == 1) {
+ R(0x6210) = 0xFF, R(0x6211) = 0x80, R(0x6207) &= 0x7F;
+ moving_jump();
+ }
+ landing(&a, &b);
+ if (a == 1) {
+ if (b == 1) { /* 0x1C4F: landed */
+ R(0x6216) = 0;
+ R(MARIO_ALIVE) = R(0x6220) ^ 1;
+ R(0x6207) = U8((R(0x6207) & 0x80) | 0x0F);
+ R(0x621E) = 4;
+ R(0x621F) = 0;
+ if (R(0x6225) == 1) R(0x6225) = 0; /* (0x1D95: the bonus sound) */
+ } else { /* blocked: straight down from here */
+ R(0x621F) = 1;
+ memset(&R(0x6210), 0, 5);
+ }
+ } else if (R(0x621F) == 1) { /* 0x1C76: falling: too far? */
+ if (U8(R(MARIO_Y) - 0x0F) >= R(0x620E)) R(0x6220) = 1, R(0x6084) = 3;
+ } else if (R(0x6214) == 0x14) { /* the top of the jump: what was jumped */
+ R(0x621F) = 1;
+ uint8_t n = jumped_over();
+ if (n) R(0x6342) = n, R(0x6340) = 1, R(0x6225) = 1;
+ } else if (R(0x6214) == 0x13) {
+ hammer_grab();
+ }
+ mario_sprite();
+}
+
+static void jump_start(void) { /* 0x1B6E */
+ R(0x6216) = 1;
+ uint8_t in = R(INPUT), b = 0, c = 0;
+ if (in & 1) b = 0, c = 0x80;
+ else if (in & 2) b = 0xFF, c = 0x80;
+ R(0x6210) = b;
+ R(0x6211) = c;
+ R(0x6212) = 1;
+ R(0x6213) = 0x48;
+ R(0x6214) = 0;
+ R(0x6204) = R(0x6206) = 0;
+ R(0x6207) = U8((R(0x6207) & 0x80) | 0x0E);
+ R(0x620E) = R(MARIO_Y);
+ R(0x6081) = 3;
+}
+
+static void walk(uint8_t b) { /* 0x1C8F (b = 1), 0x1CAB (b = 0xFF) */
+ if (R(0x620F)) {
+ R(MARIO_X) = U8(R(MARIO_X) + b);
+ if (R(SCREEN) == 1) R(MARIO_Y) = slope(R(MARIO_X), R(MARIO_Y), b);
+ R(0x620F)--;
+ } else {
+ uint8_t a = step_anim(b == 1 ? 5 : 1, R(0x6202));
+ R(0x6202) = a;
+ a &= 3;
+ if (b == 1) a |= 0x80;
+ R(0x6207) = a;
+ if (a & 1) R(0x6080) = 3;
+ R(0x620F) = 2;
+ }
+ mario_sprite();
+}
+
+static void climb_step(uint8_t dy) { /* 0x1D11 */
+ uint8_t y = U8(R(MARIO_Y) + dy);
+ R(MARIO_Y) = y;
+ R(0x6222) ^= 1;
+ if (R(0x6222)) {
+ R(MARIO_X) = U8((R(MARIO_X) | 3) & ~4);
+ R(0x6224) ^= 1;
+ if (!R(0x6224)) R(0x6080) = 3;
+ } else {
+ uint8_t t = U8(y + 8);
+ if (t == R(0x621C) || t == R(0x621B)) { /* 0x1D67: off the ladder */
+ R(0x6207) = 6;
+ R(0x6219) = 0;
+ R(0x6215) = 0;
+ mario_sprite();
+ return;
+ }
+ uint8_t d = U8(t - R(0x621B)), pic = d == 8 ? 5 : d == 12 ? 4 : 3;
+ R(0x6207) = U8(((R(0x6207) & 0x80) ^ 0x80) | pic);
+ }
+ R(0x6215) = 1;
+ mario_sprite();
+}
+
+static void climb_up(void) { /* 0x1D03 */
+ if (R(0x620F)) { /* 0x1D76 */
+ if (R(0x621A)) {
+ R(0x6219) = R(0x621A);
+ if (U8(R(0x621C) - 0x13) >= R(MARIO_Y)) return; /* a broken ladder ends here */
+ }
+ R(0x620F)--;
+ return;
+ }
+ R(0x620F) = 4;
+ climb_step(0xFE);
+}
+
+static void climb_down(void) { /* 0x1CF2 */
+ if (R(0x620F)) {
+ R(0x620F)--;
+ return;
+ }
+ R(0x620F) = 3;
+ climb_step(2);
+}
+
+static void ladder_keys(void) { /* 0x1B38 */
+ if (R(INPUT) & 8) {
+ climb_down();
+ return;
+ }
+ if (!R(0x6215)) return;
+ if (R(INPUT) & 4) climb_up();
+}
+
+static void mario_control(void) { /* 0x1AC3 */
+ if (R(0x6216) == 1) {
+ jumping();
+ return;
+ }
+ if (R(0x621E)) { /* 0x1B55: landing */
+ if (--R(0x621E)) return;
+ R(0x6217) = R(0x6218);
+ R(0x6207) &= 0x80;
+ R(0x6202) = 0;
+ mario_sprite();
+ return;
+ }
+ if (R(0x6217) != 1) {
+ if (R(0x6215) == 1) {
+ ladder_keys();
+ return;
+ }
+ if (R(INPUT) & 0x80) {
+ jump_start();
+ return;
+ }
+ }
+ uint8_t d, e;
+ walls(&d, &e);
+ uint8_t in = R(INPUT);
+ if (e != 1 && (in & 1)) {
+ walk(1);
+ return;
+ }
+ if (d != 1 && (in & 2)) {
+ walk(0xFF);
+ return;
+ }
+ if (R(0x6217) == 1) return;
+ uint8_t dd = U8(R(MARIO_Y) + 8), x = U8((R(MARIO_X) | 3) & ~4), a, b, c;
+ if (!find_ladder(x, dd, &a, &b, &c)) return;
+ R(0x6207) = U8((R(0x6207) & 0x80) | 6);
+ R(0x621A) = c <= 4;
+ if (a == 0) { /* at the bottom: up only */
+ R(0x621B) = b, R(0x621C) = dd;
+ if (R(INPUT) & 4) climb_up();
+ return;
+ }
+ if (R(0x621A)) return; /* never down a broken ladder */
+ R(0x621B) = dd, R(0x621C) = b;
+ ladder_keys();
+}
+
+static void fall_check(void) { /* 0x2A85: walked off an edge? */
+ if (R(0x6215) || R(0x6216) || R(0x6398) == 1) return;
+ uint8_t h = U8(R(MARIO_X) - 3), l = U8(R(MARIO_Y) + 0x0C);
+ uint16_t hl = vram_at(h, l);
+ if (girder_tile(V(hl))) return;
+ if ((h & 7) && girder_tile(V(hl - 0x20))) return;
+ R(0x6221) = 1;
+}
+
+static void fall_start(void) { /* 0x1F46 */
+ if (!R(0x6221)) return;
+ R(0x6204) = R(0x6206) = R(0x6221) = 0;
+ memset(&R(0x6210), 0, 5);
+ R(0x6216) = 1;
+ R(0x621F) = 1;
+ R(0x620E) = R(MARIO_Y);
+}
+
+/* ------------------------------------------------------------------ 25m: barrels (0x6700, 10 slots of 0x20) */
+
+static uint8_t barrel_girder(uint16_t ix) { /* 0x2A2F: landed on a girder? (then on it) */
+ uint8_t e = U8(R(ix + 5) + 4), t = TILE(R(ix + 3), e), a;
+ if (!girder_tile(t) || t == 0xC0) return 0;
+ if (t < 0xC0) a = 0xFF;
+ else if (t < 0xD0 || (t >= 0xE0 && t < 0xF0)) a = U8((t & 0x0F) - 9);
+ else a = U8((t & 0x0F) - 1);
+ a = U8((e & 0xF8) + a);
+ if (a >= e) return 0;
+ R(ix + 5) = U8(a - 4);
+ return 1;
+}
+
+static bool barrel_drum(uint16_t ix) { /* 0x24B4: into the oil drum (a blue one lets a fire out) */
+ if (R(ix + 5) < 0xE8) return false;
+ uint8_t x = R(ix + 3);
+ if (x >= 0x2A || x < 0x20) return false;
+ if (R(ix + 0x15)) R(0x62B9) = 3;
+ R(ix) = 0;
+ R(ix + 3) = 0;
+ R(0x6082) = 3;
+ if (!R(0x6348)) R(0x6348) = 1;
+ return true;
+}
+
+static void barrel_turn(uint16_t ix, uint8_t c) { /* 0x23DE: a quarter turn every 4 frames (by flips) */
+ uint8_t a = U8(R(ix + 0x0F) - 1);
+ if (a == 0) {
+ uint8_t b = U8((R(ix + 7) >> 7) << 1 | R(ix + 8) >> 7), r = step_anim(U8(3 | c), b);
+ R(ix + 8) = U8((R(ix + 8) & 0x7F) | (r & 1) << 7);
+ R(ix + 7) = U8((R(ix + 7) & 0x7F) | ((r >> 1) & 1) << 7);
+ a = 4;
+ }
+ R(ix + 0x0F) = a;
+}
+
+static void tumble(uint16_t ix) { /* 0x1FCE */
+ uint8_t a = U8(R(ix + 0x0F) - 1);
+ if (a == 0) {
+ R(ix + 7) ^= 1;
+ a = 4;
+ }
+ R(ix + 0x0F) = a;
+}
+
+static void wild_aim(uint16_t ix) { /* 0x22CB: a wild barrel's sideways speed after each girder */
+ uint8_t a;
+ if (!R(0x6348)) {
+ uint8_t l = R(LEVEL);
+ a = l == 1 ? 1 : l == 2 ? 0xB1 : 0xE9;
+ R(ix + 0x11) = a;
+ R(ix + 0x10) = U8((a & 1) - 1);
+ return;
+ }
+ switch (U8(R(DIFFICULTY) - 1)) {
+ case 0:
+ case 1:
+ a = R(RNG1);
+ R(ix + 0x11) = a;
+ R(ix + 0x10) = U8((a & 1) - 1);
+ break;
+ case 2:
+ case 3:
+ R(ix + 0x11) = R(RNG1);
+ R(ix + 0x10) = R(MARIO_X) >= R(ix + 3) ? 1 : 0xFF;
+ break;
+ case 4: {
+ uint8_t d = U8(R(MARIO_X) - R(ix + 3)), c = R(MARIO_X) < R(ix + 3) ? 0xFF : 0, cy;
+ for (int k = 0; k < 2; k++) {
+ cy = d >> 7;
+ d = U8(d << 1 | cy);
+ c = U8(c << 1 | cy);
+ }
+ R(ix + 0x10) = c;
+ R(ix + 0x11) = d;
+ break;
+ }
+ }
+}
+
+static void barrel_ladder(uint16_t ix, uint8_t x, uint8_t d) { /* 0x216D: down this ladder? */
+ uint8_t a, b, c;
+ if (!find_ladder(x, d, &a, &b, &c) || a != 1) return;
+ R(ix + 0x17) = U8(b - 5);
+ if (R(0x6348)) { /* before the drum is lit it always takes it */
+ if (U8(R(MARIO_Y) - 4) < d) return;
+ uint8_t k = U8((R(DIFFICULTY) >> 1) + 1), r = R(RNG1);
+ if ((r & 3) >= k) return;
+ uint8_t mx = R(MARIO_X), in = R(INPUT);
+ if (mx != x && !(mx > x ? (in & 2) : (in & 1)) && (r & 0x18)) return;
+ }
+ R(ix + 7)++;
+ R(ix + 2) |= 1;
+}
+
+static void edge_step(uint16_t ix) { /* 0x2053: over an edge, falling and bouncing */
+ motion(ix);
+ if (!barrel_girder(ix)) {
+ if (U8(R(ix + 3) + 8) < 0x10) {
+ R(ix) = 0;
+ R(ix + 3) = 0;
+ return;
+ }
+ if (barrel_drum(ix)) return;
+ barrel_turn(ix, U8((R(ix + 0x10) & 1) << 2));
+ return;
+ }
+ uint8_t n = ++R(ix + 0x0E);
+ if (n >= 3) {
+ R(ix + 2) = R(ix + 0x10) == 1 ? 2 : 4;
+ return;
+ }
+ if (n == 1 && (R(ix + 0x15) || U8(R(ix + 5) - 0x16) < R(MARIO_Y))) { /* it turns back (not when well below Mario) */
+ if (R(ix + 0x10) == 0) R(ix + 0x11) = 0, R(ix + 0x10) = 0xFF;
+ else R(ix + 0x10) = 1, R(ix + 0x11) = 0;
+ }
+ uint16_t hl = speed_now(ix) >> 2; /* 0x20C3: a bounce, a quarter as fast */
+ R(ix + 0x12) = U8(hl >> 8);
+ R(ix + 0x13) = U8(hl);
+ R(ix + 0x14) = 0;
+ R(ix + 4) = 0;
+ R(ix + 6) = 0;
+}
+
+static void wild_step(uint16_t ix) { /* 0x20EC */
+ motion(ix);
+ if (U8(R(ix + 5) - 0x1A) >= R(ix + 0x19)) {
+ if (barrel_girder(ix)) {
+ if (R(ix + 5) >= 0xE0) { /* at the bottom it rolls left as a normal barrel */
+ R(ix + 7) = U8((R(ix + 7) & 0xFC) | 1);
+ R(ix + 1) = 0;
+ R(ix + 2) = 0;
+ R(ix + 0x10) = 0xFF;
+ R(ix + 0x11) = 0;
+ R(ix + 0x12) = 0;
+ R(ix + 0x13) = 0xB0;
+ R(ix + 0x0E) = 1;
+ } else {
+ wild_aim(ix);
+ R(ix + 0x19) = R(ix + 5);
+ }
+ R(ix + 0x14) = 0;
+ R(ix + 4) = 0;
+ R(ix + 6) = 0;
+ return;
+ }
+ if (barrel_drum(ix)) return;
+ }
+ if (U8(R(ix + 3) + 8) < 0x10) {
+ R(ix) = 0;
+ R(ix + 3) = 0;
+ return;
+ }
+ tumble(ix);
+}
+
+static void barrel_step(uint16_t ix) { /* 0x1F93 */
+ if (R(ix + 1) == 1) {
+ wild_step(ix);
+ return;
+ }
+ uint8_t m = R(ix + 2), b, c;
+ if (m & 1) { /* down a ladder */
+ R(ix + 5)++;
+ if (R(ix + 0x17) == R(ix + 5)) {
+ R(ix + 7) = U8((R(ix + 0x15) << 2) + 0x15);
+ R(ix + 2) ^= 7;
+ return;
+ }
+ tumble(ix);
+ return;
+ }
+ if (m & 2) b = 1, c = 0, R(ix + 3)++;
+ else if (m & 4) b = 0xFF, c = 4, R(ix + 3)--;
+ else {
+ edge_step(ix);
+ return;
+ }
+ uint8_t h = R(ix + 3), l = R(ix + 5);
+ if ((h & 7) == 3) {
+ barrel_ladder(ix, h, U8(l + 5));
+ return;
+ }
+ R(ix + 5) = U8(slope(h, U8(l - 3), b) + 3);
+ barrel_turn(ix, c);
+ if (barrel_drum(ix)) return;
+ uint8_t x = R(ix + 3);
+ if (x < 0x1C) R(ix + 0x10) = 0xFF, R(ix + 0x11) = 0xA0;
+ else if (x >= 0xE4) R(ix + 0x10) = 0, R(ix + 0x11) = 0x60;
+ else return;
+ R(ix + 0x12) = 0xFF; /* 0x2038: off the end */
+ R(ix + 0x13) = 0xF0;
+ R(ix + 0x14) = 0;
+ R(ix + 0x0E) = 0;
+ R(ix + 4) = 0;
+ R(ix + 6) = 0;
+ R(ix + 2) = 8;
+}
+
+static void barrels_roll(void) { /* 0x1F72 */
+ if (R(SCREEN) != 1) return;
+ for (int i = 0; i < 10; i++) {
+ uint16_t ix = 0x6700 + i * 0x20, hl = 0x6980 + i * 4;
+ if (R(ix) != 1) continue;
+ barrel_step(ix);
+ R(hl) = R(ix + 3); /* 0x21BA */
+ R(hl + 1) = R(ix + 7);
+ R(hl + 2) = R(ix + 8);
+ R(hl + 3) = R(ix + 5);
+ }
+}
+
+static void barrel_start(void) { /* 0x2C03: is it time for Kong to take a barrel? */
+ if (R(SCREEN) != 1 || !(R(MARIO_ALIVE) & 1) || (R(0x6393) & 1)) return;
+ uint8_t c = R(0x62B1), a;
+ if (!c) return;
+ if (U8(R(0x62B0) - 2) < c) { /* the board's first barrel is wild, the second normal */
+ a = R(0x62B0) == c ? 1 : 2;
+ R(0x6382) = a;
+ a++;
+ } else if (R(0x6382) & 2) {
+ R(0x6382) = 0, a = 3;
+ } else {
+ uint8_t b = R(DIFFICULTY), f = R(FRAME) & 0x1F;
+ while (b && f != b) b--;
+ if (!b) return;
+ if (U8(R(0x62B0) >> 1) >= c && !(R(RNG2) & 1)) return;
+ if (rng() & 0x0F) R(0x6382) = 0, a = 3;
+ else R(0x6382) = 1, a = 2;
+ }
+ R(0x638F) = a;
+ R(0x6392) = 1;
+ if (R(0x62B2) != c) return;
+ R(0x62B2) = U8(R(0x62B2) - 8); /* a blue one, if a fire can still come out */
+ for (int i = 0; i < 5; i++)
+ if (!R(0x6400 + i * 0x20)) {
+ R(0x6382) |= 0x80;
+ return;
+ }
+}
+
+static void barrel_throw(void) { /* 0x2C8F: Kong takes, holds and lets go of a barrel */
+ if (R(SCREEN) != 1 || !(R(MARIO_ALIVE) & 1)) return;
+ if (!(R(0x6393) & 1)) {
+ if (!(R(0x6392) & 1)) return;
+ int i;
+ for (i = 0; i < 10; i++)
+ if (!(R(0x6700 + i * 0x20) & 3)) break;
+ if (i == 10) return;
+ uint16_t ix = 0x6700 + i * 0x20, spr = 0x6980 + i * 4;
+ R(0x62AA) = U8(ix), R(0x62AB) = U8(ix >> 8);
+ R(ix) = 2;
+ R(0x62AC) = U8(spr), R(0x62AD) = U8(spr >> 8);
+ R(0x6393) = 1;
+ task(5, 1);
+ if (--R(0x62B1) == 0) R(0x6386) = 1;
+ uint8_t n = R(0x62B1);
+ if (n < 4) R(0x69A8 + n * 4) = 0; /* the pile by Kong gets smaller */
+ R(ix + 7) = 0x15, R(ix + 8) = 0x0B, R(ix + 0x15) = 0;
+ if (R(0x6382) & 0x80) R(ix + 7) = 0x19, R(ix + 8) = 0x0C, R(ix + 0x15) = 1;
+ }
+ if (--R(0x62AF)) return;
+ R(0x62AF) = 0x18;
+ uint8_t st = R(0x638F);
+ if (st) {
+ uint8_t c = st;
+ if (!(R(0x6382) & 1)) c--;
+ kong_pose(0x3932 + U8(c * 0x28));
+ if (!--R(0x638F)) {
+ R(0x62AF) = 1;
+ if (R(0x6382) & 1) R(0x62A8) = 0xCC, R(0x62A9) = 0x39;
+ }
+ }
+ uint16_t hl = R(0x62A8) | R(0x62A9) << 8; /* 0x2D54: along the path to the girder */
+ uint8_t a = ROM(hl);
+ uint16_t ix = R(0x62AA) | R(0x62AB) << 8, de = R(0x62AC) | R(0x62AD) << 8;
+ if (a != 0x7F) {
+ R(de) = a & 0x7F;
+ uint8_t s = R(ix + 7);
+ if (a & 0x80) s ^= 3;
+ R(de + 1) = s;
+ R(ix + 7) = s;
+ R(de + 2) = R(ix + 8);
+ hl++;
+ R(de + 3) = ROM(hl);
+ hl++;
+ R(0x62A8) = U8(hl), R(0x62A9) = U8(hl >> 8);
+ return;
+ }
+ R(0x62A8) = 0xC3, R(0x62A9) = 0x39; /* 0x2D8C: let go */
+ R(ix + 1) = 1;
+ if (!(R(0x6382) & 1)) R(ix + 1) = 0, R(ix + 2) = 2;
+ R(ix) = 1;
+ R(ix + 0x0F) = 1;
+ for (int k = 0x10; k <= 0x14; k++) R(ix + k) = 0;
+ R(0x6393) = 0;
+ R(0x6392) = 0;
+ R(ix + 3) = R(de);
+ R(ix + 5) = R(de + 3);
+ kong_pose(0x385C);
+ move_kong(0x690B, 0xFC);
+}
+
+/* ------------------------------------------------------------------ fires and firefoxes (0x6400, 5 slots of 0x20) */
+
+static void fire_anim(uint16_t ix) { /* 0x3409 */
+ if (R(ix + 0x15)) {
+ R(ix + 0x15)--;
+ return;
+ }
+ R(ix + 0x15) = 2;
+ R(ix + 7)++;
+ if ((R(ix + 7) & 0x0F) == 0x0F) R(ix + 7) ^= 2;
+}
+
+static void fire_slope(uint16_t ix) { /* 0x33C3 */
+ if (R(SCREEN) != 1) return;
+ R(ix + 0x0F) = slope(R(ix + 0x0E), R(ix + 0x0F), R(ix + 0x0D));
+}
+
+static void fire_walk(uint16_t ix) { /* 0x33AD */
+ if (R(ix + 0x0D) == 1) R(ix + 7) |= 0x80, R(ix + 0x0E)++;
+ else R(ix + 7) &= 0x7F, R(ix + 0x0E)--;
+ fire_anim(ix);
+ fire_slope(ix);
+}
+
+static void fire_climb(uint16_t ix) { /* 0x33E7: up at a third of the speed down */
+ fire_anim(ix);
+ if (R(ix + 0x0D) != 8) {
+ R(ix + 0x0F)++;
+ return;
+ }
+ if (R(ix + 0x14)) {
+ R(ix + 0x14)--;
+ return;
+ }
+ R(ix + 0x14) = 2;
+ R(ix + 0x0F)--;
+}
+
+static void fire_turn(uint16_t ix) { /* 0x330F: every 43 moves, left or right at random */
+ if (!R(ix + 0x16)) {
+ R(ix + 0x16) = 0x2B;
+ R(ix + 0x0D) = R(RNG1) & 1;
+ }
+ R(ix + 0x16)--;
+}
+
+static void fire_ladder(uint16_t ix) { /* 0x333D */
+ uint8_t dir = R(ix + 0x0D), a, b, c;
+ if (dir == 8 || dir == 4) {
+ if (U8(R(ix + 0x0F) + 8) != R(ix + 0x1F)) return;
+ R(ix + 0x0D) = 0;
+ if (dir == 8 && R(ix + 0x19) == 2) R(ix + 0x1D) = 1;
+ return;
+ }
+ if (screen_in(7) && R(ix + 0x0F) < 0x59) return; /* 0x33A1: never down from the top girder */
+ if (!find_ladder(R(ix + 0x0E), U8(R(ix + 0x0F) + 8), &a, &b, &c)) return;
+ R(ix + 0x1F) = b;
+ if (a == 0) {
+ R(ix + 0x0D) = 8;
+ return;
+ }
+ if (R(ix + 0x0F) >= R(MARIO_Y)) return; /* down only toward Mario */
+ R(ix + 0x0D) = 4;
+}
+
+static void freezer(uint16_t ix) { /* 0x32D6 */
+ if (R(ix + 0x1C)) {
+ if (--R(ix + 0x1C)) {
+ R(ix + 0x0D) = 0;
+ return;
+ }
+ } else if (R(ix + 0x1D) != 1) {
+ fire_turn(ix);
+ return;
+ } else {
+ R(ix + 0x1D) = 0;
+ if (R(MARIO_Y) >= R(ix + 0x0F)) {
+ R(ix + 0x1C) = 0xFF;
+ R(ix + 0x0D) = 0;
+ return;
+ }
+ }
+ R(ix + 0x19) = 0; /* 0x3303 */
+ R(ix + 0x1C) = 0;
+ fire_turn(ix);
+}
+
+static void fire_arc(uint16_t ix, uint16_t hl) { /* 0x3445: out of the drum, one point of the arc a frame */
+ uint8_t a = ROM(hl);
+ if (a != 0xAA) {
+ R(ix + 5) = a;
+ hl++;
+ R(ix + 0x1A) = U8(hl), R(ix + 0x1B) = U8(hl >> 8);
+ return;
+ }
+ R(ix + 0x13) = R(ix + 0x18) = R(ix + 0x0D) = R(ix + 0x1C) = 0;
+ R(ix + 0x0E) = R(ix + 3);
+ R(ix + 0x0F) = R(ix + 5);
+ R(ix + 0x1A) = R(ix + 0x1B) = 0;
+}
+
+static void fire_spawn(uint16_t ix) { /* 0x32BD */
+ uint16_t hl = R(ix + 0x1A) | R(ix + 0x1B) << 8;
+ switch (R(SCREEN)) {
+ case 1: /* 0x342C */
+ if (!hl) hl = 0x3A8C, R(ix + 3) = 0x26;
+ R(ix + 3)++;
+ fire_arc(ix, hl);
+ break;
+ case 2: /* 0x3478: toward Mario's side */
+ if (!hl) {
+ hl = 0x3AAC;
+ if (R(MARIO_X) & 0x80) R(ix + 0x0D) = 1, R(ix + 3) = 0x7E;
+ else R(ix + 0x0D) = 2, R(ix + 3) = 0x80;
+ }
+ if (R(ix + 0x0D) == 1) R(ix + 3)++;
+ else R(ix + 3)--;
+ fire_arc(ix, hl);
+ break;
+ case 4: /* 0x34B9: on the side away from Mario */
+ hl = (R(MARIO_X) & 0x80) ? 0x3AD4 : 0x3AC4;
+ hl += R(RNG2) & 6;
+ R(ix + 3) = R(ix + 0x0E) = ROM(hl);
+ R(ix + 5) = R(ix + 0x0F) = ROM(hl + 1);
+ R(ix + 0x0D) = R(ix + 0x18) = R(ix + 0x1C) = 0;
+ break;
+ }
+}
+
+static void fire_move(uint16_t ix) { /* 0x3202 */
+ if (R(ix + 0x18) == 1) {
+ fire_spawn(ix);
+ return;
+ }
+ if ((int8_t)U8(R(ix + 0x0D) - 4) >= 0) goto ladder;
+ if (R(ix + 0x19) == 2) {
+ freezer(ix);
+ } else {
+ fire_turn(ix);
+ if (R(RNG1) & 3) goto moved; /* a ladder is looked for one time in four */
+ }
+ if (R(ix + 0x0D) == 0) goto bob; /* left-moving fires skip those turns */
+ladder:
+ fire_ladder(ix);
+moved:
+ if ((int8_t)U8(R(ix + 0x0D) - 4) >= 0) {
+ fire_climb(ix);
+ goto bob;
+ }
+ fire_walk(ix);
+ if (!girder_tile(TILE(R(ix + 0x0E), U8(R(ix + 0x0F) + 0x0C)))) { /* 0x298C: the end of a girder */
+ if (R(ix + 0x0D) == 1) R(ix + 0x0D) = 2, R(ix + 0x0E)--;
+ else R(ix + 0x0D) = 1, R(ix + 0x0E)++;
+ fire_slope(ix);
+ goto bob;
+ }
+ if (R(ix + 0x0E) < 0x10) R(ix + 0x0D) = 1;
+ else if (R(ix + 0x0E) >= 0xF0) R(ix + 0x0D) = 2;
+bob: { /* 0x3257 */
+ uint8_t a = R(ix + 0x13);
+ a = a ? U8(a - 1) : 0x11;
+ R(ix + 0x13) = a;
+ R(ix + 3) = R(ix + 0x0E);
+ R(ix + 5) = U8(R(ix + 0x0F) + ROM(0x3A7A + a));
+}
+}
+
+static bool fire_count(void) { /* 0x313C: counts them, colours them, lets a new one out */
+ R(0x63A1) = 0;
+ for (int i = 0; i < 5; i++) {
+ uint16_t ix = 0x6400 + i * 0x20;
+ if (R(ix)) {
+ R(0x63A1)++;
+ R(ix + 8) = R(0x6217) == 1 ? 0 : 1;
+ continue;
+ }
+ if (R(0x63A1) == 5) continue;
+ if (R(SCREEN) == 2 && R(0x63A1) == R(DIFFICULTY)) return true;
+ if (R(0x63A0) != 1) continue;
+ R(ix) = 1;
+ R(ix + 0x18) = 1;
+ R(0x63A0) = 0;
+ R(0x63A1)++;
+ }
+ R(0x63A0) = 0;
+ return R(0x63A1) != 0;
+}
+
+static void fires(void) { /* 0x30ED */
+ uint8_t d = R(DIFFICULTY), f = R(FRAME);
+ if (d >= 6) d = 5;
+ switch (d) { /* 0x30FA: faster with the difficulty */
+ case 0:
+ case 1:
+ if ((f & 1) != 1) return;
+ break;
+ case 2:
+ if ((f & 7) >= 5) return;
+ break;
+ case 3:
+ case 4:
+ if ((f & 3) == 3) return;
+ break;
+ default:
+ if ((f & 7) == 7) return;
+ break;
+ }
+ if (!fire_count()) return;
+ if ((int8_t)U8(R(DIFFICULTY) - 3) >= 0 && (R(RNG1) & 3) == 1 && R(FRAME) == 1) /* 0x31DD: freezers */
+ R(0x6439) = R(0x6479) = 2;
+ for (int i = 0; i < 5; i++) {
+ uint16_t ix = 0x6400 + i * 0x20;
+ R(0x63C8) = U8(ix), R(0x63C9) = U8(ix >> 8);
+ if (R(ix)) fire_move(ix);
+ }
+ for (int i = 0; i < 5; i++) { /* 0x34F3 */
+ uint16_t ix = 0x6400 + i * 0x20, de = 0x69D0 + i * 4;
+ if (!R(ix)) continue;
+ R(de) = R(ix + 3);
+ R(de + 1) = R(ix + 7);
+ R(de + 2) = R(ix + 8);
+ R(de + 3) = R(ix + 5);
+ }
+}
+
+static void fire_timer(void) { /* 0x2DDB: 50m and 100m */
+ if (!screen_in(0x0A) || !(R(MARIO_ALIVE) & 1)) return;
+ uint8_t b = U8(R(DIFFICULTY) + 1) >> 1, a = 0xFF;
+ if (R(SCREEN) == 2) b++;
+ while (--b) a >>= 1;
+ if (R(FRAME) & a) return;
+ R(0x63A0) = 1;
+ R(0x639A) = 1;
+}
+
+/* ------------------------------------------------------------------ 50m: pies, conveyors, the moving ladders */
+
+static void pies(void) { /* 0x24EA */
+ if (R(SCREEN) != 2) return;
+ if (R(0x639B)) { /* 0x2523: a new pie */
+ R(0x639B)--;
+ } else if (R(0x639A)) {
+ int i;
+ for (i = 0; i < 6; i++)
+ if (!(R(0x65A0 + i * 0x10) & 1)) break;
+ if (i < 6) {
+ uint16_t ix = 0x65A0 + i * 0x10;
+ uint8_t a = rng();
+ bool right;
+ R(ix + 5) = 0x7C;
+ if (a < 0x60 || R(0x62A3) == 1) {
+ R(ix + 5) = 0xCC;
+ right = R(0x62A6) & 0x80;
+ } else {
+ right = rng() < 0x68;
+ }
+ R(ix + 3) = right ? 0xF8 : 0x07;
+ R(ix) = 1;
+ R(ix + 7) = 0x4B;
+ R(ix + 9) = 8;
+ R(ix + 0x0A) = 3;
+ R(0x639B) = 0x7C;
+ R(0x639A) = 0;
+ R(RNG2)--; /* (0x258F decrements what HL points at: RngTimer2, left there by 0x0057) */
+ }
+ }
+ for (int i = 0; i < 6; i++) { /* 0x2591: along the belts, into the fire or off the end */
+ uint16_t ix = 0x65A0 + i * 0x10;
+ if (!(R(ix) & 1)) continue;
+ uint8_t h = R(ix + 3);
+ bool gone = U8(h + 7) < 0x0E;
+ if (!gone && R(ix + 5) == 0x7C) {
+ if (h == 0x80) gone = true;
+ else R(ix + 3) = U8((h >= 0x80 ? R(0x63A5) : R(0x63A4)) + h);
+ } else if (!gone) {
+ R(ix + 3) = U8(R(0x63A6) + h);
+ }
+ if (gone) {
+ R(ix) = 0;
+ R(ix + 3) = 0;
+ R(0x69B8 + i * 4) = 0;
+ }
+ }
+ for (int i = 0; i < 6; i++) {
+ uint16_t ix = 0x65A0 + i * 0x10, hl = 0x69B8 + i * 4;
+ if (!R(ix)) continue;
+ R(hl) = R(ix + 3);
+ R(hl + 1) = R(ix + 7);
+ R(hl + 2) = R(ix + 8);
+ R(hl + 3) = R(ix + 5);
+ }
+}
+
+static uint8_t belt_vector(uint16_t hl) { /* 0x26E9: moving every other frame */
+ if (!(R(FRAME) & 1)) return 0;
+ uint8_t a = (R(hl) & 0x80) ? 0xFF : 1;
+ R(hl) = a;
+ return a;
+}
+
+static void belt_reverse(uint16_t hl) { R(hl) = (R(hl) & 0x80) ? 2 : 0xFE; } /* 0x26DE */
+
+static uint8_t pulleys(uint16_t hl, uint16_t de) { /* 0x26A6 */
+ uint8_t a;
+ hl++;
+ if (!(R(de) & 0x80)) {
+ a = U8(R(hl) + 1);
+ if (a == 0x53) a = 0x50;
+ R(hl) = a;
+ hl += 4;
+ a = U8(R(hl) - 1);
+ if (a == 0xCF) a = 0xD2;
+ } else {
+ a = U8(R(hl) - 1);
+ if (a == 0x4F) a = 0x52;
+ R(hl) = a;
+ hl += 4;
+ a = U8(R(hl) + 1);
+ if (a == 0xD3) a = 0xD0;
+ }
+ R(hl) = a;
+ return a;
+}
+
+static void belt_top(void) { /* 0x2602 */
+ if (!(R(FRAME) & 1) && !--R(0x62A0)) {
+ R(0x62A0) = 0x80;
+ belt_reverse(0x62A1);
+ }
+ R(0x63A3) = belt_vector(0x62A1);
+ if ((R(FRAME) & 0x1F) == 1) pulleys(0x69E4, 0x62A1);
+}
+
+static void conveyors(void) { /* 0x25F2 */
+ if (R(SCREEN) != 2) return;
+ belt_top();
+ if (R(MARIO_Y) < 0xC0) { /* 0x262F: while Mario is up there the middle belts run inwards */
+ if (!(R(0x62A3) & 0x80)) R(0x62A3) = 0xFF;
+ } else if (!(R(FRAME) & 1) && !--R(0x62A2)) {
+ R(0x62A2) = 0xC0;
+ belt_reverse(0x62A3);
+ }
+ uint8_t a = belt_vector(0x62A3);
+ R(0x63A5) = a;
+ R(0x63A4) = U8(-a);
+ if (!(R(FRAME) & 0x1F)) R(0x69ED) = pulleys(0x69EC, 0x62A2) & 0x7F;
+ if (!(R(FRAME) & 1) && !--R(0x62A5)) { /* 0x2679 */
+ R(0x62A5) = 0xFF;
+ belt_reverse(0x62A6);
+ }
+ R(0x63A6) = belt_vector(0x62A6);
+ if ((R(FRAME) & 0x1F) == 2) pulleys(0x69F4, 0x62A6);
+ uint8_t x = R(MARIO_X), y = R(MARIO_Y); /* 0x2AD3: Mario on a belt */
+ if (y == 0x50) a = R(0x63A3);
+ else if (y == 0x78) a = x >= 0x80 ? R(0x63A5) : R(0x63A4);
+ else if (y == 0xC8) a = R(0x63A6);
+ else return;
+ R(MARIO_X) = R(0x694C) = U8(a + x);
+ uint8_t d, e;
+ walls(&d, &e);
+ if (e == 1) R(MARIO_X)--;
+ else if (d == 1) R(MARIO_X)++;
+}
+
+static bool on_moving_ladder(uint16_t hl) { /* 0x2243 */
+ return R(MARIO_Y) < 0x7A && !R(0x6216) && R(MARIO_X) == R(hl);
+}
+
+static void ladder_sprite(uint16_t hl) { R((hl & 8) ? 0x694B : 0x6947) = R(hl); } /* 0x22BD */
+
+static void moving_ladders(void) { /* 0x2207 */
+ if (R(SCREEN) != 2) return;
+ uint16_t hl = (R(FRAME) & 1) ? 0x6280 : 0x6288;
+ switch (R(hl)) {
+ case 0: /* up */
+ if (--R(hl + 1)) {
+ if (on_moving_ladder(hl + 2)) R(0x621A) = 0;
+ return;
+ }
+ R(hl)++;
+ if (on_moving_ladder(hl + 2)) R(0x621A) = 1;
+ return;
+ case 1: /* going down */
+ if (--R(hl + 4)) return;
+ R(hl + 4) = 4;
+ R(hl + 3)++;
+ ladder_sprite(hl + 3);
+ if (R(hl + 3) == 0x78) R(hl)++;
+ if (!on_moving_ladder(hl + 2)) return;
+ {
+ uint8_t y = R(MARIO_Y);
+ if (y < 0x68 || (y & 1)) {
+ R(MARIO_Y)++;
+ R(0x694D) = 3;
+ R(0x694F)++;
+ } else {
+ R(0x6222) = (y & 2) ? 1 : 0;
+ }
+ }
+ return;
+ case 2: /* down */
+ if (!(R(RNG1) & 0x3C)) R(hl)++;
+ return;
+ case 3: /* going up */
+ if (--R(hl + 4)) return;
+ R(hl + 4) = 2;
+ R(hl + 3)--;
+ ladder_sprite(hl + 3);
+ if (R(hl + 3) != 0x68) return;
+ R(hl + 1) = 0x80;
+ R(hl) = 0;
+ return;
+ }
+}
+
+/* ------------------------------------------------------------------ 75m: elevators and springs */
+
+static void elevator_dies(void) { /* 0x277F */
+ R(MARIO_ALIVE) = 0;
+ R(0x6398) = 0;
+}
+
+static void elevators(void) { /* 0x26FA */
+ if (R(SCREEN) != 3) return;
+ if (R(MARIO_Y) >= 0xF0) {
+ elevator_dies();
+ return;
+ }
+ uint8_t f = R(FRAME);
+ bool move = R(LEVEL) == 1 ? (f & 3) == 0 : (f & 1);
+ if (!move) {
+ if (R(LEVEL) == 1 && (f & 3) != 1) return;
+ if (!R(0x6398) || R(0x6216)) return; /* 0x2745: riding */
+ uint8_t x = R(MARIO_X), y = R(MARIO_Y);
+ if (x < 0x2C || (x >= 0x43 && x < 0x6C) || x >= 0x83) {
+ R(0x6398) = 0;
+ R(0x6221) = 1;
+ } else if (x < 0x43) {
+ if (y < 0x71) elevator_dies();
+ else R(MARIO_Y) = R(0x694F) = U8(y - 1);
+ } else {
+ if (y >= 0xE8) elevator_dies();
+ else R(MARIO_Y) = R(0x694F) = U8(y + 1);
+ }
+ return;
+ }
+ for (int i = 0; i < 6; i++) { /* 0x2797 */
+ uint16_t ix = 0x6600 + i * 0x10;
+ if (!(R(ix) & 1)) continue;
+ if (R(ix + 0x0D) & 8) {
+ if (--R(ix + 5) == 0x60) R(ix + 3) = 0x77, R(ix + 0x0D) = 4;
+ } else if (++R(ix + 5) == 0xF8) {
+ R(ix) = 0;
+ }
+ }
+ if (R(0x62A7)) { /* 0x27DA: a new one every 0x34 moves */
+ R(0x62A7)--;
+ } else {
+ for (int i = 0; i < 6; i++) {
+ uint16_t ix = 0x6600 + i * 0x10;
+ if (R(ix) & 1) continue;
+ R(ix) = 1;
+ R(ix + 3) = 0x37;
+ R(ix + 5) = 0xF8;
+ R(ix + 0x0D) = 8;
+ R(0x62A7) = 0x33;
+ break;
+ }
+ }
+ for (int i = 0; i < 6; i++) {
+ R(0x6958 + i * 4) = R(0x6603 + i * 0x10);
+ R(0x695B + i * 4) = R(0x6605 + i * 0x10);
+ }
+}
+
+static void springs(void) { /* 0x2E04 */
+ if (R(SCREEN) != 3 || !(R(MARIO_ALIVE) & 1)) return;
+ for (int i = 0; i < 10; i++) {
+ uint16_t ix = 0x6500 + i * 0x10, iy = 0x6980 + i * 4;
+ if (!(R(ix) & 1)) {
+ if (!(R(0x6396) & 1)) continue;
+ R(0x6396) = 0; /* 0x2EAE */
+ R(ix + 5) = 0x50;
+ R(ix + 0x0D) = 1;
+ R(ix + 3) = U8((rng() & 0x0F) + 0xF8);
+ R(ix) = 1;
+ R(ix + 0x0E) = 0xAA, R(ix + 0x0F) = 0x39;
+ continue;
+ }
+ if (!(R(FRAME) & 0x0F)) R(iy + 1) ^= 7;
+ if (R(ix + 0x0D) == 4) {
+ uint8_t y = U8(R(ix + 5) + 3);
+ R(ix + 5) = y;
+ if (y >= 0xF8) R(ix + 3) = 0, R(ix) = 0;
+ } else {
+ R(ix + 3) = U8(R(ix + 3) + 2);
+ uint16_t hl = R(ix + 0x0E) | R(ix + 0x0F) << 8;
+ uint8_t c = ROM(hl);
+ if (c == 0x7F) {
+ hl = 0x39AA;
+ R(0x6083) = 3;
+ } else {
+ hl++;
+ R(ix + 5) = U8(c + R(ix + 5));
+ }
+ R(ix + 0x0E) = U8(hl), R(ix + 0x0F) = U8(hl >> 8);
+ if (R(ix + 3) >= 0xB7 && c == 0x7F) {
+ R(ix + 0x0D) = 4;
+ R(0x6083) = 0;
+ R(0x6084) = 3;
+ }
+ }
+ R(iy) = R(ix + 3);
+ R(iy + 3) = R(ix + 5);
+ }
+}
+
+/* ------------------------------------------------------------------ 100m: rivets */
+
+static void rivets(void) { /* 0x1A33 */
+ if (R(SCREEN) != 4) return;
+ uint8_t x = R(MARIO_X);
+ if (x == 0x4B || x == 0xB3) {
+ R(0x6291) = 1;
+ return;
+ }
+ if (R(0x6291) != 1) return;
+ R(0x6291) = 0;
+ uint8_t a = U8(R(MARIO_Y) - 1), b = 0;
+ if (a >= 0xD0) return;
+ if (a & 0x80) b |= 4;
+ if (a & 0x20) b |= 2;
+ if (((a >> 5) & 7) == 6) b |= 2;
+ if (x & 0x80) b |= 1;
+ if (!R(0x6292 + b)) return;
+ R(0x6292 + b) = 0;
+ R(0x6290)--;
+ uint16_t hl = (b & 1) ? 0x012B : 0x02CB;
+ hl += 5 * (b >> 1);
+ hl += 0x7400;
+ V(hl) = V(hl - 1) = V(hl + 1) = 0x10;
+ R(0x6340) = R(0x6342) = R(0x6225) = 1;
+ if (!R(0x6216)) R(0x6225) = 0;
+}
+
+/* ------------------------------------------------------------------ items, points, the hammer */
+
+static void items(void) { /* 0x19DA: Pauline's hat, purse and umbrella */
+ uint8_t x = R(MARIO_X);
+ for (int i = 0; i < 3; i++) {
+ uint16_t hl = 0x6A0C + i * 4;
+ if (R(hl) != x) continue;
+ if (R(MARIO_Y) != R(hl + 3) || (R(hl + 1) & 8)) return;
+ R(0x6343) = U8(hl), R(0x6344) = U8(hl >> 8);
+ R(0x6342) = 0;
+ R(0x6340) = 1;
+ return;
+ }
+}
+
+static void score_sprite(uint8_t x, uint8_t pic, uint8_t y) { /* 0x1E36 */
+ R(0x6A30) = x;
+ R(0x6A31) = pic;
+ R(0x6A32) = 7;
+ R(0x6A33) = y;
+ if (screen_in(5)) R(0x6085) = 3;
+}
+
+static void bonus_points(void) { /* 0x1DBD */
+ if (R(0x6340) == 2) {
+ if (--R(0x6341)) return;
+ R(0x6A30) = 0;
+ R(0x6340) = 0;
+ return;
+ }
+ if (R(0x6340) != 1) return;
+ R(0x6341) = 0x40;
+ R(0x6340) = 2;
+ uint8_t s = R(0x6342), pic, pts;
+ if (s & 1) { /* jumped (or a rivet): 100, 300, 500 (shown as 800: the ROM's slip) */
+ uint8_t a = s >> 1;
+ if (!(a & 1)) pts = 1, pic = 0x7B;
+ else if (!(a & 2)) pts = 3, pic = 0x7D;
+ else pts = 5, pic = 0x7F;
+ task(0, pts);
+ score_sprite(R(MARIO_X), pic, U8(R(MARIO_Y) + 0x14));
+ return;
+ }
+ if (s & 2) pts = 3, pic = 0x7D; /* a barrel smashed */
+ else if (s & 4) { /* something else smashed */
+ uint8_t r = R(RNG1);
+ if (r & 1) pts = 5, pic = 0x7E;
+ else if (r & 2) pts = 8, pic = 0x7F;
+ else pts = 3, pic = 0x7D;
+ } else { /* an item */
+ R(0x6085) = 3;
+ uint8_t l = R(LEVEL);
+ if (l == 1) pts = 3, pic = 0x7D;
+ else if (l == 2) pts = 5, pic = 0x7E;
+ else pts = 8, pic = 0x7F;
+ }
+ task(0, pts);
+ uint16_t hl = R(0x6343) | R(0x6344) << 8;
+ uint8_t x = R(hl);
+ R(hl) = 0;
+ score_sprite(x, pic, R(hl + 3));
+}
+
+static void smash(void) { /* 0x1E96: everything stops while a hit flashes */
+ switch (R(0x6345)) {
+ case 0: {
+ uint8_t g = R(0x6352);
+ uint16_t hl = g == 0x65 ? 0x69B8 : g < 0x65 ? 0x69D0 : 0x6980;
+ uint16_t ix = R(0x6351) | R(0x6352) << 8;
+ for (uint8_t n = R(0x6354); n; n--) hl += 4, ix += R(0x6353);
+ R(ix) = 0;
+ R(0x6342) = R(ix + 0x15) ? 4 : 2;
+ R(0x6A2C) = R(hl);
+ R(hl) = 0;
+ R(0x6A2D) = 0x60;
+ R(0x6A2E) = 0x0C;
+ R(0x6A2F) = R(hl + 3);
+ R(0x6345)++;
+ R(0x6346) = 6;
+ R(0x6347) = 5;
+ R(0x608A) = 6;
+ R(0x608B) = 3;
+ break;
+ }
+ case 1:
+ if (--R(0x6346)) return;
+ R(0x6346) = 6;
+ if (--R(0x6347)) {
+ R(0x6A2D) ^= 1;
+ return;
+ }
+ R(0x6347) = 4;
+ R(0x6345)++;
+ break;
+ case 2:
+ if (--R(0x6346)) return;
+ R(0x6346) = 0x0C;
+ if (--R(0x6347)) {
+ R(0x6A2D)++;
+ return;
+ }
+ R(0x6345) = 0;
+ R(0x6350) = 0;
+ R(0x6340) = 1;
+ R(0x6343) = 0x2C, R(0x6344) = 0x6A;
+ break;
+ }
+}
+
+static void hammer(void) { /* 0x2ED4 */
+ if (!screen_in(0x0B) || !(R(MARIO_ALIVE) & 1)) return;
+ uint16_t de = 0x6A18, ix = 0x6680;
+ if (!(R(ix + 1) & 1)) de = 0x6A1C, ix = 0x6690;
+ R(ix + 0x0E) = 0;
+ R(ix + 0x0F) = 0xF0;
+ uint8_t b, c;
+ if (R(0x6217) & 1) {
+ R(0x6218) = 0;
+ R(0x6089) = 4;
+ R(ix + 9) = 6;
+ R(ix + 0x0A) = 3;
+ b = 0x1E;
+ uint8_t a = R(0x6207), cy = a >> 7;
+ a = U8(a << 1);
+ if (cy) a |= 0x80, b |= 0x80;
+ c = a | 8;
+ if (R(0x6394) & 8) { /* swung down */
+ b |= 1;
+ c |= 1;
+ R(ix + 9) = 5;
+ R(ix + 0x0A) = 6;
+ R(ix + 0x0F) = 0;
+ R(ix + 0x0E) = (c & 0x80) ? 0x10 : 0xF0;
+ }
+ R(0x694D) = c;
+ c = 7;
+ if (++R(0x6394) == 0) {
+ if (++R(0x6395) == 2) { /* used up */
+ R(0x6395) = 0;
+ R(0x6217) = 0;
+ R(ix + 1) = 0;
+ R(ix + 0x0E) = U8(-R(MARIO_X));
+ R(0x694D) = R(0x6207);
+ R(ix) = 0;
+ R(0x6089) = R(0x6389);
+ } else if (R(FRAME) & 8) {
+ c = 1;
+ }
+ } else if (R(0x6395) && (R(FRAME) & 8)) {
+ c = 1; /* the second half flashes */
+ }
+ } else {
+ if (!(R(0x6218) & 1)) return;
+ R(ix + 9) = 6; /* 0x2F9C: picked up, over Mario's head */
+ R(ix + 0x0A) = 3;
+ b = U8((R(0x6207) & 0x80) | 0x1E);
+ c = 7;
+ R(0x6389) = R(0x6089);
+ }
+ uint8_t x = U8(R(MARIO_X) + R(ix + 0x0E)), y = U8(R(MARIO_Y) + R(ix + 0x0F)); /* 0x2F7C */
+ R(de) = R(ix + 3) = x;
+ R(de + 1) = b;
+ R(de + 2) = c;
+ R(de + 3) = R(ix + 5) = y;
+}
+
+static void hammer_hits(void) { /* 0x281D */
+ for (int i = 0; i < 2; i++) {
+ uint16_t iy = 0x6680 + i * 0x10;
+ if (!(R(iy + 1) & 1)) continue;
+ if (!hostile_hit(iy, R(iy + 5), R(iy + 9), R(iy + 0x0A))) return;
+ R(0x6350) = 1;
+ R(0x6354) = U8(R(0x63B9) - hit_left);
+ R(0x6353) = U8(hit_de);
+ R(0x6351) = U8(hit_ix), R(0x6352) = U8(hit_ix >> 8);
+ return;
+ }
+}
+
+/* ------------------------------------------------------------------ Kong and Pauline (0x03FB) */
+
+static void kong_and_pauline(void) {
+ if (R(SCREEN) == 2) { /* Kong rides the top belt */
+ move_kong(0x6908, R(0x63A3));
+ R(0x63B7) = U8(R(0x6910) - 0x3B);
+ }
+ if (!R(0x6391)) {
+ if (R(FRAME)) goto help;
+ R(0x6391) = 1;
+ }
+ {
+ uint8_t t = ++R(0x6390);
+ if (t == 0x80) {
+ R(0x6390) = R(0x6391) = 0;
+ if (R(0x6393)) goto help;
+ kong_pose(0x385C);
+ } else {
+ if (R(0x6393) || (t & 0x1F)) goto help;
+ kong_pose((t & 0x20) ? 0x39CF : 0x39F7); /* beating his chest */
+ R(0x6082) = 3;
+ }
+ uint8_t s = R(SCREEN);
+ if (s == 1) move_kong(0x690B, 0xFC);
+ else if (s == 2) move_kong(0x6908, R(0x63B7));
+ else if (s == 4) move_kong(0x6908, 0x44);
+ }
+help: {
+ uint8_t c = R(0x6390);
+ if (R(SCREEN) != 4) {
+ write3(0x75C4, (c && (c & 0x40)) ? 0xEF : 0x10, 0x20);
+ if (!(c & 0x40) || (c & 7)) return;
+ R(0x6905) ^= 3;
+ return;
+ }
+ write3(0x7623, 0x10, 0x20); /* 100m: she calls from Mario's side */
+ write3(0x7583, 0x10, 0x20);
+ bool left = R(MARIO_X) < 0x80;
+ if (c & 0x40) write3(left ? 0x7623 : 0x7583, left ? 0xDF : 0xEF, 0x20);
+ if (left) R(0x6901) |= 0x80, R(0x6905) |= 0x80;
+ else R(0x6901) &= 0x7F, R(0x6905) &= 0x7F;
+ if (!(c & 0x40) || (c & 7)) return;
+ R(0x6905) ^= 3;
+}
+}
+
+/* ------------------------------------------------------------------ the main routine (0x197A) */
+
+static bool board_done(void) { /* 0x1E57 */
+ uint8_t s = R(SCREEN);
+ if (s & 4) {
+ if (R(0x6290)) return false;
+ } else {
+ if (R(MARIO_Y) >= ((s & 1) ? 0x31 : 0x51)) return false;
+ R(0x694D) = (R(MARIO_X) & 0x80) ? 0x00 : 0x80; /* he faces Pauline */
+ }
+ R(GAME_MODE2) = 0x16;
+ return true;
+}
+
+static bool time_out(void) { /* 0x1A07 */
+ switch (R(0x6386)) {
+ case 1:
+ R(0x6387) = 0;
+ R(0x6386) = 2;
+ break;
+ case 2:
+ if (!--R(0x6387)) R(0x6386) = 3;
+ break;
+ case 3:
+ if (R(0x6216)) break; /* never in the middle of a jump */
+ R(GAME_MODE2)++;
+ R(WAIT_MSB) = 0x40;
+ return true;
+ }
+ return false;
+}
+
+static void bonus_tick(void) { /* 0x2FCB: all but 25m */
+ if (R(SCREEN) == 1 || --R(0x62B4)) return;
+ R(0x62B9) = 3;
+ R(0x6396) = 3;
+ task(5, 1);
+ R(0x62B4) = R(0x62B3);
+ if (!--R(0x62B1)) R(0x6386) = 1;
+}
+
+static void play(void) {
+ bonus_points();
+ if (R(0x6350)) {
+ smash();
+ return;
+ }
+ mario_control();
+ barrels_roll();
+ barrel_throw();
+ barrel_start();
+ fires();
+ springs();
+ pies();
+ fire_timer();
+ hammer();
+ moving_ladders();
+ rivets();
+ fall_check();
+ fall_start();
+ elevators();
+ conveyors();
+ items();
+ kong_and_pauline();
+ if (hostile_hit(0x6200, R(MARIO_Y), 4, 7)) R(MARIO_ALIVE) = 0; /* 0x2808 */
+ hammer_hits();
+ if (board_done() || time_out()) return;
+ bonus_tick();
+ if (R(MARIO_ALIVE)) return;
+ R(0x6082) = 3; /* 0x19CA */
+ R(GAME_MODE2)++;
+ R(WAIT_MSB) = 0x40;
+}
+
+/* ------------------------------------------------------------------ death (0x127C) and lives (0x12F2) */
+
+static void clear_sprites(uint16_t hl, int n) { /* 0x30E4 */
+ for (int i = 0; i < n; i++) R(hl + i * 4) = 0;
+}
+static void clear_hostiles(void) { /* 0x30BD */
+ clear_sprites(0x6950, 2);
+ clear_sprites(0x6980, 10);
+ clear_sprites(0x69B8, 11);
+ clear_sprites(0x6A0C, 5);
+}
+
+static void clear_box(uint16_t hl) { /* 0x1826: 5 rows by 14 columns */
+ for (int c = 0; c < 14; c++, hl -= 0x20)
+ for (int r = 0; r < 5; r++) V(hl + r) = 0x10;
+}
+
+static void dying(void) {
+ bonus_points();
+ switch (R(0x639D)) {
+ case 0:
+ if (!rst18()) return;
+ R(0x694D) = U8((R(0x694D) & 0x80) | 0x78);
+ R(0x639D)++;
+ R(0x639E) = 0x0D;
+ R(WAIT_MSB) = 8;
+ clear_hostiles();
+ R(0x6088) = 3;
+ break;
+ case 1: /* 13 turns, 8 frames apart */
+ if (!rst18()) return;
+ R(WAIT_MSB) = 8;
+ if (--R(0x639E)) {
+ uint8_t s = R(0x694D), b = U8((s & 1) << 7 | 1);
+ R(0x694D) = s ^ b;
+ R(0x694E) ^= b & 0x80;
+ return;
+ }
+ R(0x694D) = U8((R(0x694D) & 0x80) | 0x7A);
+ R(0x639D)++;
+ R(WAIT_MSB) = 0x80;
+ break;
+ case 2:
+ if (!rst18()) return;
+ R(0x694C) = 0; /* 0x30DB */
+ clear_sprites(0x6958, 6);
+ R(GAME_MODE2)++;
+ R(WAIT_MSB) = 1;
+ break;
+ }
+}
+
+static void lose_life(void) {
+ R(0x622C) = 0;
+ uint8_t n = --R(LIVES);
+ memcpy(&R(0x6040), &R(0x6228), 8);
+ if (n) {
+ R(GAME_MODE2) = 8;
+ return;
+ }
+ clear_box(0x76D4);
+ task(3, 0); /* GAME OVER */
+ R(WAIT_MSB) = 0xC0;
+ R(GAME_MODE2) = 0x10;
+}
+
+/* ------------------------------------------------------------------ a board cleared (0x1615) */
+
+static void next_board(void) { /* 0x178F / 0x193D */
+ uint16_t hl = (R(0x622A) | R(0x622B) << 8) + 1;
+ uint8_t a = ROM(hl);
+ if (a == 0x7F) hl = 0x3A73, a = ROM(hl);
+ R(0x622A) = U8(hl), R(0x622B) = U8(hl >> 8);
+ R(SCREEN) = a;
+}
+
+static void heart(void) { /* 0x1708 */
+ R(0x6A20) = 0x80, R(0x6A21) = 0x76, R(0x6A22) = 9, R(0x6A23) = 0x20;
+ R(0x6905) = 0x13;
+ write3(0x75C4, 0x10, 0x20);
+ R(0x608A) = 7;
+ R(0x608B) = 3;
+}
+
+static void kong_takes_her(void) { /* 0x168A / 0x16EE: the climbing pose, without her yet */
+ kong_pose(0x388C);
+ R(0x690C) = 0x66;
+ R(0x6924) = 0;
+ R(0x692C) = 0;
+ R(0x62AF) = 0;
+}
+
+static void board_clear(void) {
+ clear_hostiles();
+ uint8_t s = R(SCREEN), step = R(0x6388);
+ if (s == 4) {
+ bonus_points();
+ switch (step) {
+ case 0: /* 0x17B7: the middle of the rivet girders falls */
+ R(0x608A) = 0x0E;
+ R(0x608B) = 3;
+ write3(0x7623, 0x10, 0x20);
+ write3(0x7583, 0x10, 0x20);
+ clear_box(0x76DA), draw_board(0x3A47);
+ clear_box(0x76D5), draw_board(0x3A4D);
+ clear_box(0x76D0), draw_board(0x3A53);
+ clear_box(0x76CB), draw_board(0x3A59);
+ kong_pose(0x385C);
+ move_kong(0x6908, 0x44);
+ R(0x6905) = 0x13;
+ R(WAIT_MSB) = 0x20;
+ R(0x6390) = 0x80;
+ R(0x6388)++;
+ break;
+ case 1:
+ if (rst18()) R(0x6388)++;
+ break;
+ case 2: { /* 0x1839: Kong beats his chest */
+ uint8_t t = ++R(0x6390);
+ if (!t) {
+ kong_pose(0x385C);
+ move_kong(0x6908, 0x44);
+ R(WAIT_MSB) = 0x20;
+ R(0x6388)++;
+ } else if (!(t & 7)) {
+ kong_pose((t & 8) ? 0x39CF : 0x39F7);
+ move_kong(0x6908, 0x44);
+ }
+ break;
+ }
+ case 3: /* 0x186F: upside down */
+ if (!rst18()) return;
+ kong_pose(0x3A1F);
+ R(0x6084) = 3;
+ R(0x6388)++;
+ break;
+ case 4: /* 0x1880: he falls */
+ move_kong(0x690B, 1);
+ if (R(0x691B) != 0xD0) return;
+ R(0x6919) = 0x20;
+ R(0x6A24) = 0x7F, R(0x6A25) = 0x39, R(0x6A26) = 1, R(0x6A27) = 0xD8;
+ clear_box(0x76C6);
+ draw_board(0x3A5F);
+ add_each(0x6903, 2, 0x28, 4);
+ R(0x62AF) = 0;
+ R(0x6082) = 3;
+ R(0x6388)++;
+ break;
+ case 5: /* 0x18C6: Mario meets Pauline */
+ if (!--R(0x62AF)) {
+ next_board();
+ R(LEVEL)++;
+ task(5, 0);
+ R(0x622E) = 0;
+ R(0x6388) = 0;
+ R(WAIT_MSB) = 0xE0;
+ R(GAME_MODE2) = 8;
+ return;
+ }
+ if (R(0x62AF) & 7) return;
+ R(0x6A25) ^= 0x80;
+ R(0x6919) = step_anim(0, R(0x6919) & ~0x20) | 0x20;
+ if (R(0x62AF) == 0xE0) {
+ R(0x694F) = 0x50;
+ R(0x694D) = 0;
+ R(0x694C) = 0x9F;
+ if (R(MARIO_X) < 0x80) R(0x694D) = 0x80, R(0x694C) = 0x5F;
+ }
+ if (R(0x62AF) == 0xC0) {
+ R(0x608A) = (R(LEVEL) & 1) ? 0x0C : 0x05;
+ R(0x608B) = 3;
+ R(0x6A23) = 0x40, R(0x6A22) = 9, R(0x6A21) = 0x76, R(0x6A20) = 0x8F;
+ if (R(MARIO_X) < 0x80) R(0x6A20) = 0x6F;
+ }
+ break;
+ }
+ return;
+ }
+ /* 25m and 75m: 0 heart, 1 a pose, 2 the climb pose, 3 up to her, 4 away; 50m: 0 heart, 1 back along the belt */
+ if (s == 2 && step >= 2) step++;
+ switch (step) {
+ case 0:
+ heart();
+ if (s == 2) {
+ uint8_t x = U8(R(0x6910) - 0x3B);
+ kong_pose(0x385C);
+ move_kong(0x6908, x);
+ R(0x6388)++;
+ break;
+ }
+ kong_pose(0x385C);
+ R(WAIT_MSB) = 0x20;
+ R(0x6388)++;
+ if (s == 1) move_kong(0x690B, 0xFC);
+ break;
+ case 1:
+ if (s == 2) { /* 0x16BB: back along the top belt, to his ladder */
+ R(0x62A0) = 0;
+ uint8_t c = R(0x63A3), x = R(0x6910);
+ bool reverse;
+ if (x < 0x5A) reverse = c & 0x80;
+ else if (x >= 0x5D) reverse = !(c & 0x80);
+ else {
+ kong_takes_her();
+ R(0x6388)++;
+ break;
+ }
+ if (reverse) R(0x62A0) = 1;
+ belt_top();
+ move_kong(0x6908, R(0x63A3));
+ break;
+ }
+ if (!rst18()) return;
+ kong_pose(0x3932);
+ R(WAIT_MSB) = 0x20;
+ R(0x6388)++;
+ if (s == 3) move_kong(0x690B, 4);
+ break;
+ case 2:
+ if (!rst18()) return;
+ kong_takes_her();
+ R(0x6388)++;
+ if (s == 1) move_kong(0x690B, 0xFC);
+ break;
+ case 3: /* 0x1732: up the ladder; at her level he takes her */
+ kong_climb_step();
+ if (R(0x6913) >= 0x2C) return;
+ R(0x6900) = R(0x6904) = 0;
+ R(0x690C) = 0;
+ R(0x6924) = 0x6B;
+ R(0x692C) = 0x6A;
+ R(0x6A21)++; /* the heart breaks */
+ R(0x6388)++;
+ break;
+ case 4: { /* 0x1757: until he is gone */
+ kong_climb_step();
+ for (int i = 9; i >= 0; i--)
+ if (R(0x690B + i * 4) < 0x19) R(0x6908 + i * 4) = 0;
+ for (int i = 0; i < 10; i++)
+ if (R(0x6908 + i * 4)) return;
+ R(WAIT_MSB) = 0x40;
+ R(0x6388)++;
+ break;
+ }
+ case 5: /* 0x178E */
+ if (!rst18()) return;
+ next_board();
+ task(5, 0);
+ R(0x6388) = 0;
+ R(WAIT_MSB) = 0x30;
+ R(GAME_MODE2) = 8;
+ break;
+ }
+}
+
+/* ------------------------------------------------------------------ the frame */
+
+static void game_modes(void) { /* 0x06FE */
+ switch (R(GAME_MODE2)) {
+ case 0x00: /* 0x0986 */
+ clear_all();
+ R(GAME_MODE2) = 1;
+ break;
+ case 0x01: /* 0x09AB */
+ memcpy(&R(0x6228), &R(0x6040), 8);
+ R(SCREEN) = ROM(R(0x622A) | R(0x622B) << 8);
+ R(WAIT_MSB) = 1;
+ R(GAME_MODE2) = 5;
+ break;
+ case 0x05: /* 0x0A37 */
+ task(3, 4);
+ task(2, 2);
+ task(2, 0);
+ task(6, 0);
+ R(GAME_MODE2)++;
+ V(0x7740) = 1, V(0x7720) = 0x25, V(0x7700) = 0x20;
+ break;
+ case 0x06: /* 0x0A63 */
+ if (!rst18()) return;
+ clear_play();
+ R(WAIT_MSB) = 1;
+ R(GAME_MODE2) = R(0x622C) ? 7 : 8;
+ break;
+ case 0x07: intro(); break;
+ case 0x08: how_high(); break;
+ case 0x0A:
+ if (rst18()) start_board();
+ break;
+ case 0x0B: mario_start(); break;
+ case 0x0C: play(); break;
+ case 0x0D: dying(); break;
+ case 0x0E: lose_life(); break;
+ case 0x10:
+ if (!S->over && rst18()) S->over = 1;
+ break;
+ case 0x16: board_clear(); break;
+ }
+}
+
+static void foreground(void) { /* 0x02BD: what runs between two vblanks */
+ run_tasks();
+ flash_1up();
+ extra_life();
+ S->loop ^= S->loop << 13, S->loop ^= S->loop >> 17, S->loop ^= S->loop << 5;
+ R(RNG2) = U8(R(RNG2) + 1 + (S->loop & 0xFF)); /* its own turns: as many as the vblank left time for */
+ R(0x6383) = R(FRAME);
+ difficulty_clock();
+ fire_clock();
+ run_tasks();
+}
+
+void dk_tick(dk_state *s, uint8_t joy) {
+ S = s;
+ uint8_t b = joy & 0x1F, c = b & 0x0F; /* 0x0098: InputState gets bit 7 when jump is first pressed */
+ R(INPUT) = U8((U8(~R(RAW)) & b & 0x10) << 3 | c);
+ R(RAW) = b;
+ R(FRAME)--;
+ rng();
+ game_modes();
+ foreground();
+}
+
+void dk_new_game(dk_state *s, uint32_t seed) {
+ memset(s, 0, sizeof *s);
+ S = s;
+ memset(s->vram, 0x10, sizeof s->vram);
+ R(START_LIVES) = 3; /* the factory settings: 3 lives, a bonus Mario at 7000 */
+ R(EXTRA_AT) = 0x07;
+ R(GAME_MODE1) = 3;
+ R(0x60B0) = R(0x60B1) = 0xC0;
+ memset(&R(0x60C0), 0xFF, 0x40);
+ R(0x60B8) = 0x50, R(0x60B9) = 0x76, R(0x60BA) = 0x00; /* the high score table's top: 7650 */
+ static const uint8_t start[8] = {3, 0x01, 0x65, 0x3A, 0x01, 0, 0, 0}; /* 0x095E: level 1, the order, intro */
+ memcpy(&R(0x6040), start, 8);
+ task(1, 0);
+ R(RNG1) = U8(seed);
+ R(RNG2) = U8(seed >> 8);
+ R(FRAME) = U8(seed >> 16);
+ s->loop = seed | 1;
+}
+
+/* ------------------------------------------------------------------ for the app */
+
+uint32_t dk_bcd(const uint8_t *p) {
+ uint32_t v = 0;
+ for (int i = 2; i >= 0; i--) v = v * 100 + (p[i] >> 4) * 10 + (p[i] & 15);
+ return v;
+}
+uint32_t dk_score(const dk_state *s) { return dk_bcd(&DK_RAM(s, 0x60B2)); }
+int dk_screen(const dk_state *s) { return DK_RAM(s, 0x6227); }
+int dk_mode(const dk_state *s) { return DK_RAM(s, 0x600A); }
+int dk_level(const dk_state *s) { return DK_RAM(s, 0x6229); }
+int dk_lives(const dk_state *s) { return DK_RAM(s, 0x6228); }
+bool dk_alive(const dk_state *s) { return DK_RAM(s, 0x6200) & 1; }
+
+bool dk_valid(const dk_state *s) {
+ static const uint8_t modes[] = {0, 1, 5, 6, 7, 8, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x10, 0x16};
+ uint8_t m = DK_RAM(s, 0x600A);
+ bool ok = false;
+ for (unsigned i = 0; i < sizeof modes; i++) ok |= m == modes[i];
+ uint16_t order = DK_RAM(s, 0x622A) | DK_RAM(s, 0x622B) << 8;
+ return ok && DK_RAM(s, 0x6005) == 3 && order >= 0x3A65 && order < 0x3A79 && DK_RAM(s, 0x6385) < 8 &&
+ DK_RAM(s, 0x60B0) >= 0xC0 && DK_RAM(s, 0x60B1) >= 0xC0 && s->over <= 1 &&
+ (m < 0x0A || (DK_RAM(s, 0x6227) >= 1 && DK_RAM(s, 0x6227) <= 4));
+}
diff --git a/games/donkeykong/tests/play.c b/games/donkeykong/tests/play.c
new file mode 100644
index 00000000..d35f9198
--- /dev/null
+++ b/games/donkeykong/tests/play.c
@@ -0,0 +1,717 @@
+/* Donkey Kong on a computer, for tests and pictures (make host, make test).
+ *
+ * play test the rules against the arcade's numbers (the spec), a bot through the four boards on
+ * three levels and through a whole game, saves written and read back, damaged saves
+ * refused, and a few hundred sessions of the whole app with random keys, quitting with Home
+ * play shots DIR pictures of the screens and of each board (DIR/<name>.ppm)
+ * play film DIR N N frames of the bot's game, every other frame (DIR/f_NNNN.ppm)
+ */
+#define main dk_app_main
+#include "../src/main.c"
+#undef main
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <time.h>
+
+/* ---------------------------------------------------------------- the calculator, on a computer */
+static uint16_t fb[SCREEN_H][SCREEN_W];
+void np_push(int x, int y, int w, int h, const color_t *px) {
+ for (int j = 0; j < h; j++)
+ for (int i = 0; i < w; i++) fb[y + j][x + i] = px[j * w + i];
+}
+void *np_alloc(uint32_t size) { return malloc(size); }
+static double clock_ms;
+static uint32_t (*script)(uint32_t frame);
+static uint32_t frame_no, quit_at;
+uint32_t plat_keys(void) {
+ clock_ms += 1000.0 / 30;
+ frame_no++;
+ if (frame_no >= quit_at) return JK_HOME;
+ return script ? script(frame_no) : 0;
+}
+uint32_t plat_millis(void) { return (uint32_t)clock_ms; }
+void plat_sleep(uint32_t ms) { clock_ms += ms; }
+uint32_t plat_random(void) { return 0x9E3779B9u ^ frame_no; }
+void plat_begin(void) {}
+int plat_end(void) { return 0; }
+
+static struct { char name[32]; uint8_t data[8192]; uint32_t len; bool on; } files[4];
+bool plat_save(const char *name, const void *data, uint32_t len) {
+ int k = -1;
+ for (int i = 0; i < 4; i++)
+ if (files[i].on && !strcmp(files[i].name, name)) k = i;
+ for (int i = 0; i < 4 && k < 0; i++)
+ if (!files[i].on) k = i;
+ if (k < 0 || len > sizeof files[k].data) return false;
+ strcpy(files[k].name, name), memcpy(files[k].data, data, len), files[k].len = len, files[k].on = true;
+ return true;
+}
+const uint8_t *plat_load(const char *name, uint32_t *len) {
+ for (int i = 0; i < 4; i++)
+ if (files[i].on && !strcmp(files[i].name, name)) return *len = files[i].len, files[i].data;
+ return 0;
+}
+static void forget(const char *name) {
+ for (int i = 0; i < 4; i++)
+ if (files[i].on && !strcmp(files[i].name, name)) files[i].on = false;
+}
+
+static void shot(const char *path) {
+ FILE *f = fopen(path, "wb");
+ if (!f) exit(2);
+ fprintf(f, "P6 320 240 255\n");
+ for (int y = 0; y < SCREEN_H; y++)
+ for (int x = 0; x < SCREEN_W; x++) {
+ uint16_t c = fb[y][x];
+ fputc((c >> 11) * 255 / 31, f), fputc((c >> 5 & 63) * 255 / 63, f), fputc((c & 31) * 255 / 31, f);
+ }
+ fclose(f);
+}
+
+static int fails;
+#define CHECK(c, ...) do { if (!(c)) { fails++; printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0)
+#define M(s, a) DK_RAM(s, a)
+
+/* ---------------------------------------------------------------- helpers */
+
+/* a new game that starts at a place in the boards' order (0x3A65...), on a given level, past the intro */
+static void start_on(dk_state *s, uint32_t seed, int level, uint16_t order) {
+ dk_new_game(s, seed);
+ M(s, 0x6041) = (uint8_t)level;
+ M(s, 0x6042) = (uint8_t)order, M(s, 0x6043) = (uint8_t)(order >> 8);
+ M(s, 0x6044) = 0; /* (the intro is for a new game) */
+}
+static int run_until_mode(dk_state *s, int mode, int max) {
+ for (int i = 0; i < max; i++) {
+ if (dk_mode(s) == mode) return i;
+ dk_tick(s, 0);
+ }
+ return -1;
+}
+static uint16_t order_of(int screen) { return (uint16_t)(0x3A6A + screen - 1); } /* level 3: 25 50 75 100 */
+static void play_on(dk_state *s, uint32_t seed, int level, int screen) {
+ start_on(s, seed, level, order_of(screen));
+ run_until_mode(s, DK_MODE_PLAY, 4000);
+}
+static void calm(dk_state *s) { /* no barrels, fires, springs or pies */
+ for (int i = 0; i < 10; i++) M(s, 0x6700 + i * 0x20) = 0, M(s, 0x6500 + i * 0x10) = 0, M(s, 0x6980 + i * 4) = 0;
+ for (int i = 0; i < 5; i++) M(s, 0x6400 + i * 0x20) = 0, M(s, 0x69D0 + i * 4) = 0;
+ for (int i = 0; i < 6; i++) M(s, 0x65A0 + i * 0x10) = 0;
+ M(s, 0x6392) = M(s, 0x6393) = 0, M(s, 0x62B9) = 0, M(s, 0x63A0) = 0, M(s, 0x639A) = 0, M(s, 0x6396) = 0;
+}
+static void tick_calm(dk_state *s, uint8_t joy, int n) {
+ for (int i = 0; i < n; i++) calm(s), dk_tick(s, joy), M(s, 0x6393) = 1; /* (Kong keeps his barrel) */
+}
+
+/* ---------------------------------------------------------------- the rules against the spec */
+static void units(void) {
+ static dk_state s;
+ /* the boards' order (0x3A65): level 1 25 100, 2 25 75 100, 3 25 50 75 100, 4 25 50 25 75 100, then
+ 25 50 25 75 25 100 again and again */
+ {
+ static const char want[] = "14" "134" "1234" "12134" "121314" "121314";
+ char got[32] = {0};
+ int n = 0;
+ start_on(&s, 5, 1, 0x3A65);
+ run_until_mode(&s, DK_MODE_PLAY, 4000);
+ while (n < (int)sizeof want - 1) {
+ got[n++] = (char)('0' + dk_screen(&s));
+ /* clear the board at once: Pauline reached, or the rivets all gone */
+ if (dk_screen(&s) == 4) M(&s, 0x6290) = 0;
+ else M(&s, 0x6205) = 0x30, M(&s, 0x6216) = 0;
+ dk_tick(&s, 0);
+ CHECK(dk_mode(&s) == DK_MODE_CLEAR, "board %d not cleared", n);
+ run_until_mode(&s, DK_MODE_HOWHIGH, 3000);
+ run_until_mode(&s, DK_MODE_PLAY, 4000);
+ }
+ CHECK(!strcmp(got, want), "the boards came in this order: %s (not %s)", got, want);
+ CHECK(dk_level(&s) == 7, "level %d after six levels", dk_level(&s));
+ }
+ /* the bonus: 5000, 6000, 7000, 8000 from level 4 on, losing 100 every 120, 100, 80, 60 frames (not 25m);
+ level 22 wraps to 400: the kill screen */
+ for (int level = 1; level <= 5; level++) {
+ play_on(&s, 7, level, 3);
+ int want = level < 4 ? 40 + level * 10 : 80;
+ CHECK(M(&s, 0x62B0) == want && M(&s, 0x638C) == (want / 10) << 4, "level %d bonus %d (%02X)", level, M(&s, 0x62B0),
+ M(&s, 0x638C));
+ CHECK(M(&s, 0x62B3) == 220 - 2 * want, "level %d bonus clock %d", level, M(&s, 0x62B3));
+ uint8_t b0 = M(&s, 0x638C);
+ int f = 0;
+ while (M(&s, 0x638C) == b0 && f < 400) tick_calm(&s, 0, 1), f++;
+ int g = 0;
+ b0 = M(&s, 0x638C);
+ while (M(&s, 0x638C) == b0 && g < 400) tick_calm(&s, 0, 1), g++;
+ CHECK(g == 220 - 2 * want, "level %d: 100 off the bonus every %d frames", level, g);
+ }
+ play_on(&s, 7, 22, 1);
+ CHECK(M(&s, 0x62B0) == 4, "level 22 bonus %d", M(&s, 0x62B0));
+ /* 25m: 100 off the bonus for each barrel Kong takes */
+ {
+ play_on(&s, 9, 1, 1);
+ int barrels = 0, b = M(&s, 0x62B1);
+ for (int f = 0; f < 600 && dk_alive(&s); f++) {
+ bool deploying = M(&s, 0x6393);
+ dk_tick(&s, 0);
+ if (!deploying && M(&s, 0x6393)) barrels++;
+ }
+ CHECK(b - M(&s, 0x62B1) == barrels && barrels >= 3, "%d barrels, bonus %d to %d", barrels, b, M(&s, 0x62B1));
+ }
+ /* the difficulty: the level at the start of a board, +1 every 2048 frames, never above 5 */
+ {
+ play_on(&s, 11, 2, 4);
+ CHECK(M(&s, 0x6380) == 2, "difficulty %d at the start of level 2", M(&s, 0x6380));
+ M(&s, 0x600A) = DK_MODE_OVER, s.over = 1; /* (the clock runs between vblanks, whatever the game does) */
+ int seen[4] = {0}, k = 0;
+ for (int f = 0; f < 2048 * 4 + 300; f++) {
+ dk_tick(&s, 0);
+ if (f % 2048 == 100 && k < 4) seen[k++] = M(&s, 0x6380);
+ }
+ CHECK(seen[0] == 2 && seen[1] == 3 && seen[2] == 4 && seen[3] == 5 && M(&s, 0x6380) == 5,
+ "difficulty %d %d %d %d then %d", seen[0], seen[1], seen[2], seen[3], M(&s, 0x6380));
+ }
+ /* Mario: walks 2 px in 3 frames, jumps 14 px for 41 frames (top at frame 20), climbs 2 px every 5 frames up
+ and every 4 down */
+ {
+ play_on(&s, 13, 1, 4);
+ int x = M(&s, 0x6203);
+ tick_calm(&s, DK_RIGHT, 30);
+ CHECK(M(&s, 0x6203) - x == 20, "walked %d px in 30 frames", M(&s, 0x6203) - x);
+ tick_calm(&s, 0, 2);
+ int y0 = M(&s, 0x6205), top = 255, at = 0, land = 0;
+ tick_calm(&s, DK_JUMP, 1);
+ for (int f = 1; f < 60; f++) {
+ tick_calm(&s, 0, 1);
+ if (M(&s, 0x6205) < top) top = M(&s, 0x6205), at = f;
+ if (!land && !M(&s, 0x6216)) land = f;
+ }
+ CHECK(y0 - top == 14 && at >= 19 && at <= 21 && (land == 41 || land == 42), "jump: %d px, top at %d, down at %d", y0 - top, at, land);
+ play_on(&s, 13, 1, 4);
+ while (M(&s, 0x6203) > 0x1B) tick_calm(&s, DK_LEFT, 1);
+ int y = M(&s, 0x6205);
+ tick_calm(&s, DK_UP, 50);
+ CHECK(y - M(&s, 0x6205) == 20, "climbed %d px in 50 frames", y - M(&s, 0x6205));
+ y = M(&s, 0x6205);
+ tick_calm(&s, DK_DOWN, 40);
+ CHECK(M(&s, 0x6205) - y == 20, "went down %d px in 40 frames", M(&s, 0x6205) - y);
+ }
+ /* a fall of more than 15 px kills: off the middle of 75m into a shaft (40 px), but not a jump down to the next
+ step (8 px) */
+ {
+ play_on(&s, 15, 1, 3);
+ M(&s, 0x6203) = 0x60, M(&s, 0x6205) = 0xC8; /* on the girder at 0xD0, between the shafts */
+ for (int f = 0; f < 120 && dk_mode(&s) == DK_MODE_PLAY; f++) {
+ for (int e = 0; e < 6; e++) M(&s, 0x6600 + e * 0x10) = 0; /* (no elevator to land on) */
+ tick_calm(&s, DK_RIGHT, 1);
+ }
+ CHECK(dk_mode(&s) == DK_MODE_DEAD, "a 40 px fall didn't kill");
+ play_on(&s, 15, 1, 3);
+ M(&s, 0x6203) = 0xE2, M(&s, 0x6205) = 0x60; /* on the girder at 0x68, a jump left onto the one at 0x70 */
+ tick_calm(&s, DK_JUMP | DK_LEFT, 1);
+ tick_calm(&s, 0, 70);
+ CHECK(dk_mode(&s) == DK_MODE_PLAY && dk_alive(&s) && M(&s, 0x6205) == 0x68, "an 8 px drop: y %02X", M(&s, 0x6205));
+ }
+ /* points: a barrel jumped 100, two at once 300; a barrel smashed 300; an item 300, 500, 800 by level; a
+ rivet 100; the extra life at 7000 */
+ {
+ for (int two = 0; two < 2; two++) {
+ bool scored = false;
+ for (int wait = 0; wait < 60 && !scored; wait++) {
+ play_on(&s, 17, 1, 1);
+ calm(&s);
+ uint8_t x = M(&s, 0x6203), y = M(&s, 0x6205);
+ for (int b = 0; b <= two; b++) { /* barrels rolling left toward Mario */
+ uint16_t ix = (uint16_t)(0x6700 + b * 0x20);
+ M(&s, ix) = 1, M(&s, ix + 1) = 0, M(&s, ix + 2) = 4, M(&s, ix + 3) = (uint8_t)(x + 40 + b * 6);
+ M(&s, ix + 5) = (uint8_t)(y + 3), M(&s, ix + 7) = 0x15, M(&s, ix + 8) = 0x0B, M(&s, ix + 0x0F) = 4;
+ M(&s, ix + 0x15) = 0;
+ }
+ uint32_t sc = dk_score(&s);
+ for (int f = 0; f < wait; f++) dk_tick(&s, 0), M(&s, 0x6393) = 1;
+ dk_tick(&s, DK_JUMP), M(&s, 0x6393) = 1;
+ for (int f = 0; f < 70 && dk_alive(&s); f++) dk_tick(&s, 0), M(&s, 0x6393) = 1;
+ if (dk_alive(&s) && dk_score(&s) != sc) {
+ scored = true;
+ CHECK(dk_score(&s) - sc == (two ? 300u : 100u), "%d barrel(s) jumped: %u points", two + 1, dk_score(&s) - sc);
+ }
+ }
+ CHECK(scored, "never scored jumping %d barrel(s)", two + 1);
+ }
+ play_on(&s, 19, 1, 1);
+ calm(&s);
+ M(&s, 0x6217) = 1, M(&s, 0x6681) = 1; /* the hammer */
+ uint8_t x = M(&s, 0x6203), y = M(&s, 0x6205);
+ M(&s, 0x6700) = 1, M(&s, 0x6702) = 4, M(&s, 0x6703) = (uint8_t)(x + 30), M(&s, 0x6705) = (uint8_t)(y + 3);
+ M(&s, 0x6707) = 0x15, M(&s, 0x6715) = 0, M(&s, 0x670F) = 4;
+ uint32_t sc = dk_score(&s);
+ for (int f = 0; f < 200; f++) dk_tick(&s, 0), M(&s, 0x6393) = 1;
+ CHECK(dk_score(&s) - sc == 300, "a barrel smashed: %u points", dk_score(&s) - sc);
+ static const uint32_t item_pts[4] = {0, 300, 500, 800};
+ for (int level = 1; level <= 3; level++) {
+ start_on(&s, 21, level, 0x3A6B); /* 50m, whatever the level */
+ run_until_mode(&s, DK_MODE_PLAY, 4000);
+ M(&s, 0x6203) = 0x50, M(&s, 0x6205) = 0xA0; /* beside the hat (0x53, 0xA0) */
+ sc = dk_score(&s);
+ tick_calm(&s, DK_RIGHT, 12);
+ CHECK(dk_score(&s) - sc == item_pts[level], "level %d item: %u points", level, dk_score(&s) - sc);
+ }
+ play_on(&s, 23, 1, 4);
+ M(&s, 0x6203) = 0x44, M(&s, 0x6205) = 0xC8; /* by the lowest left rivet */
+ sc = dk_score(&s);
+ tick_calm(&s, DK_RIGHT, 20);
+ CHECK(dk_score(&s) - sc == 100 && M(&s, 0x6290) == 7, "a rivet: %u points, %d left", dk_score(&s) - sc, M(&s, 0x6290));
+ tick_calm(&s, DK_LEFT, 20); /* back over its hole: down he goes */
+ CHECK(M(&s, 0x6216) || !dk_alive(&s) || dk_mode(&s) != DK_MODE_PLAY, "walked back over a rivet's hole");
+ play_on(&s, 25, 1, 4);
+ M(&s, 0x60B2) = 0x00, M(&s, 0x60B3) = 0x69, M(&s, 0x60B4) = 0x00; /* 6900 */
+ int lives = dk_lives(&s);
+ M(&s, 0x6203) = 0x44, M(&s, 0x6205) = 0xC8;
+ tick_calm(&s, DK_RIGHT, 20); /* + 100 */
+ CHECK(dk_score(&s) == 7000 && dk_lives(&s) == lives + 1, "at %u, lives %d to %d", dk_score(&s), lives, dk_lives(&s));
+ }
+ /* the hammer lasts 512 frames */
+ {
+ play_on(&s, 27, 1, 1);
+ M(&s, 0x6217) = 1, M(&s, 0x6681) = 1;
+ int f = 0;
+ while (M(&s, 0x6217) && f < 1000) tick_calm(&s, 0, 1), f++;
+ CHECK(f == 512, "the hammer lasted %d frames", f);
+ }
+ /* the first barrel of a board is wild and blue, and lights the drum (on level 1 it falls straight down onto
+ where Mario starts: he steps aside) */
+ {
+ play_on(&s, 29, 1, 1);
+ int f = 0;
+ while (!(M(&s, 0x6700) & 1) && f < 300) dk_tick(&s, DK_RIGHT), f++;
+ CHECK(M(&s, 0x6701) == 1 && M(&s, 0x6715) == 1, "first barrel: wild %d blue %d", M(&s, 0x6701), M(&s, 0x6715));
+ for (f = 0; f < 900 && !M(&s, 0x6348); f++) {
+ for (int i = 1; i < 10; i++) M(&s, 0x6700 + i * 0x20) = 0; /* (only the first barrel) */
+ dk_tick(&s, f < 60 ? DK_RIGHT : 0);
+ }
+ CHECK(M(&s, 0x6348) == 1, "the drum wasn't lit");
+ }
+ /* fires move every other frame at difficulty 1, 7 frames in 8 at 5 */
+ {
+ int moved[2] = {0};
+ for (int k = 0; k < 2; k++) {
+ play_on(&s, 31, k ? 5 : 1, 3); /* 75m starts with two fires */
+ for (int f = 0; f < 256; f++) {
+ uint8_t fx = M(&s, 0x640E), fy = M(&s, 0x640F), keep_x = M(&s, 0x6203);
+ dk_tick(&s, 0);
+ M(&s, 0x6203) = keep_x;
+ moved[k] += M(&s, 0x640E) != fx || M(&s, 0x640F) != fy;
+ }
+ }
+ CHECK(moved[0] < moved[1], "fires moved %d times at difficulty 1, %d at 5", moved[0], moved[1]);
+ }
+ /* a broken state is refused */
+ {
+ play_on(&s, 33, 1, 1);
+ CHECK(dk_valid(&s), "a fresh game is refused");
+ static dk_state b;
+ b = s, b.ram[0x00A] = 0x42;
+ CHECK(!dk_valid(&b), "GameMode2 0x42 accepted");
+ b = s, b.ram[0x22A] = 0;
+ CHECK(!dk_valid(&b), "a broken boards' order accepted");
+ }
+}
+
+/* ---------------------------------------------------------------- a bot: beam search through the real game
+ * Each board has a route: the ladders to take, the platforms to reach, the rivets to cross. Every 4 frames
+ * each kept game is tried with each move held for 4 frames; the games where Mario lives and gets furthest
+ * along the route are kept, at most a few per place Mario is in. A jump that can't end well is dropped at
+ * once. The game is deterministic, so the moves found replay exactly. */
+/* kind: 0 a ladder's foot, 1 its top, 2 pass going right, 3 left, 4 ride an elevator down past y */
+typedef struct { uint8_t x, y, kind; } waypoint;
+static const waypoint route25[] = {{0xCB, 0xEB, 0}, {0xCB, 0xD3, 1}, {0x73, 0xCE, 0}, {0x73, 0xAE, 1}, {0x83, 0xAD, 0},
+ {0x83, 0x8D, 1}, {0x5B, 0x8A, 0}, {0x5B, 0x6E, 1}, {0xCB, 0x67, 0}, {0xCB, 0x4F, 1},
+ {0x93, 0x4C, 0}, {0x93, 0x30, 1}};
+static const waypoint route50[] = {{0x63, 0xF0, 0}, {0x63, 0xC8, 1}, {0x53, 0xC8, 0}, {0x53, 0xA0, 1},
+ {0x63, 0xA0, 0}, {0x63, 0x78, 1}, {0x23, 0x78, 0}, {0x23, 0x50, 1}};
+static const waypoint route75[] = {{0x1B, 0xE0, 0}, {0x1B, 0xB8, 1}, {0x23, 0xB8, 0}, {0x23, 0x80, 1}, {0x4C, 0x80, 2},
+ {0x77, 0xC4, 4}, {0x8C, 0xE0, 2}, {0xAE, 0xD8, 2}, {0xC4, 0xD0, 2}, {0xE3, 0xC8, 0}, {0xE3, 0xB0, 1},
+ {0xCB, 0xA8, 0}, {0xCB, 0x88, 1}, {0xE3, 0x88, 0}, {0xE3, 0x60, 1}, {0xD2, 0x68, 3},
+ {0xB3, 0x70, 0}, {0xB3, 0x50, 1}, {0x93, 0x50, 0}, {0x93, 0x30, 1}};
+/* (Kong stands in the middle of the top floor: its two rivets are reached from each side) */
+static const waypoint route100[] = {{0x1B, 0xF0, 0}, {0x1B, 0xC8, 1}, {0x4E, 0xC8, 2}, {0xB6, 0xC8, 2}, {0xDB, 0xC8, 0},
+ {0xDB, 0xA0, 1}, {0xB0, 0xA0, 3}, {0x48, 0xA0, 3}, {0x2B, 0xA0, 0}, {0x2B, 0x78, 1},
+ {0x4E, 0x78, 2}, {0xB6, 0x78, 2}, {0xCB, 0x78, 0}, {0xCB, 0x50, 1}, {0xB0, 0x50, 3},
+ {0xCB, 0x50, 0}, {0xCB, 0x78, 1}, {0x53, 0x78, 0}, {0x53, 0x50, 1}, {0x48, 0x50, 3}};
+static const waypoint *const routes[5] = {0, route25, route50, route75, route100};
+static const int route_len[5] = {0, 12, 8, 20, 20};
+
+static bool reached(const dk_state *s, const waypoint *w) {
+ int x = M(s, 0x6203), dy = M(s, 0x6205) - w->y;
+ if (w->kind == 4) return M(s, 0x6398) && x >= 0x6C && x < 0x83 && dy >= 0;
+ if (M(s, 0x6216)) return false;
+ switch (w->kind) {
+ case 0: return ((x | 3) & ~4) == w->x && dy == 0;
+ case 1: return dy == 0 && !M(s, 0x6215);
+ case 2: return x >= w->x && dy >= -6 && dy <= 6;
+ default: return x <= w->x && dy >= -6 && dy <= 6;
+ }
+}
+
+static int route_score(const dk_state *s, int screen, uint8_t *wp) {
+ const waypoint *r = routes[screen];
+ while (*wp < route_len[screen] && reached(s, &r[*wp])) (*wp)++;
+ int score = *wp * 1000;
+ if (*wp < route_len[screen]) {
+ int y = M(s, 0x6216) && !M(s, 0x621F) ? M(s, 0x620E) : M(s, 0x6205); /* (a jump on its way up is no higher) */
+ int dx = abs(M(s, 0x6203) - r[*wp].x), dy = abs(y - r[*wp].y);
+ int k = r[*wp].kind;
+ score -= k == 2 || k == 3 ? 2 * dx + dy / 4 : dx + 2 * dy; /* to a platform: across first */
+ }
+ if (screen == 4) score += (8 - M(s, 0x6290)) * 3000;
+ return score;
+}
+
+/* in the air nothing can be changed: does this jump (or fall) end well? */
+static bool lands_safely(const dk_state *s) {
+ static dk_state t;
+ t = *s;
+ for (int f = 0; f < 64 && M(&t, 0x6216); f++) {
+ dk_tick(&t, 0);
+ if (dk_mode(&t) == DK_MODE_CLEAR) return true;
+ if (dk_mode(&t) != DK_MODE_PLAY || !dk_alive(&t) || M(&t, 0x6220)) return false;
+ }
+ for (int f = 0; f < 8; f++) dk_tick(&t, 0);
+ return dk_mode(&t) == DK_MODE_CLEAR || (dk_mode(&t) == DK_MODE_PLAY && dk_alive(&t));
+}
+
+static bool same_place(const dk_state *a, const dk_state *b) {
+ return M(a, 0x6203) == M(b, 0x6203) && M(a, 0x6205) == M(b, 0x6205) && M(a, 0x6216) == M(b, 0x6216) &&
+ M(a, 0x6215) == M(b, 0x6215) && M(a, 0x620F) == M(b, 0x620F) && M(a, 0x6222) == M(b, 0x6222) &&
+ M(a, 0x6398) == M(b, 0x6398) && M(a, 0x621E) == M(b, 0x621E) &&
+ (!M(a, 0x6216) || (M(a, 0x6214) == M(b, 0x6214) && M(a, 0x6210) == M(b, 0x6210)));
+}
+
+#define BEAM 72
+#define MOVES 8
+#define MAX_STEPS 1200
+static const uint8_t moves[MOVES] = {0, DK_RIGHT, DK_LEFT, DK_UP, DK_DOWN, DK_JUMP, DK_JUMP | DK_RIGHT, DK_JUMP | DK_LEFT};
+typedef struct { dk_state s; int score; uint8_t wp, parent, move; uint32_t hazards; } bot_node;
+static bot_node beam[BEAM], kids[BEAM * MOVES];
+static uint8_t hist_parent[MAX_STEPS + 1][BEAM], hist_move[MAX_STEPS + 1][BEAM];
+static uint8_t bot_path[MAX_STEPS];
+static int cmp_node(const void *a, const void *b) { return ((const bot_node *)b)->score - ((const bot_node *)a)->score; }
+static void hold(dk_state *s, uint8_t move) {
+ for (int f = 0; f < 4; f++) dk_tick(s, f ? move & ~DK_JUMP : move);
+}
+
+/* the moves (one per 4 frames) that clear the board from here, into bot_path: their count, or -1 */
+static int bot_board(const dk_state *start) {
+ int screen = dk_screen(start), n = 1;
+ beam[0].s = *start, beam[0].wp = 0, beam[0].score = 0;
+ for (int step = 0; step < MAX_STEPS; step++) {
+ int m = 0;
+ for (int i = 0; i < n; i++)
+ for (int k = 0; k < MOVES; k++) {
+ bot_node *c = &kids[m];
+ c->s = beam[i].s, c->wp = beam[i].wp, c->parent = (uint8_t)i, c->move = moves[k];
+ hold(&c->s, moves[k]);
+ int mode = dk_mode(&c->s);
+ if (mode == DK_MODE_CLEAR) {
+ bot_path[step] = moves[k];
+ for (int t = step, at = i; t > 0; t--) bot_path[t - 1] = hist_move[t][at], at = hist_parent[t][at];
+ return step + 1;
+ }
+ if (mode != DK_MODE_PLAY || !dk_alive(&c->s) || M(&c->s, 0x6220)) continue;
+ if (M(&c->s, 0x6216) && !lands_safely(&c->s)) continue;
+ c->score = route_score(&c->s, screen, &c->wp) * 16 + (int)((uint32_t)(i * 7 + k * 13) % 16);
+ c->hazards = dk_sum(&M(&c->s, 0x6400), 0x500);
+ m++;
+ }
+ if (!m) {
+ if (getenv("BOTDBG"))
+ for (int i = 0; i < n && i < 6; i++) {
+ const dk_state *a = &beam[i].s;
+ printf(" dead end %d: wp %d mario %02X,%02X jump %d lad %d ride %d |", i, beam[i].wp, M(a, 0x6203), M(a, 0x6205),
+ M(a, 0x6216), M(a, 0x6215), M(a, 0x6398));
+ for (int e = 0; e < 5; e++) if (M(a, 0x6400 + e * 32)) printf(" f%02X,%02X", M(a, 0x6403 + e * 32), M(a, 0x6405 + e * 32));
+ for (int e = 0; e < 10; e++) if (M(a, 0x6500 + e * 16) & 1) printf(" s%02X,%02X", M(a, 0x6503 + e * 16), M(a, 0x6505 + e * 16));
+ printf("\n");
+ }
+ return -1;
+ }
+ qsort(kids, (size_t)m, sizeof kids[0], cmp_node);
+ n = 0;
+ for (int i = 0; i < m && n < BEAM; i++) { /* a few games per place Mario is in, with different hazards */
+ bool skip = false;
+ int alike = 0;
+ for (int j = 0; j < n && !skip; j++)
+ if (same_place(&beam[j].s, &kids[i].s)) alike++, skip = alike >= 4 || beam[j].hazards == kids[i].hazards;
+ if (skip) continue;
+ hist_parent[step + 1][n] = kids[i].parent, hist_move[step + 1][n] = kids[i].move;
+ beam[n++] = kids[i];
+ }
+ if (getenv("BOTDBG") && step % 100 == 99) {
+ printf(" step %d:", step + 1);
+ for (int i = 0; i < n && i < 5; i++)
+ printf(" [wp%d %02X,%02X j%d l%d r%d 620F %d 621A %d]", beam[i].wp, M(&beam[i].s, 0x6203), M(&beam[i].s, 0x6205),
+ M(&beam[i].s, 0x6216), M(&beam[i].s, 0x6215), M(&beam[i].s, 0x6398), M(&beam[i].s, 0x620F), M(&beam[i].s, 0x621A));
+ printf("\n");
+ }
+ }
+ return -1;
+}
+
+static void bots(void) {
+ static dk_state s;
+ static const char *const names[5] = {"", "25m", "50m", "75m", "100m"};
+ for (int level = 1; level <= 5; level += 2)
+ for (int b = 1; b <= 4; b++) {
+ int steps = -1;
+ for (uint32_t seed = 1; seed <= 4 && steps < 0; seed++) {
+ play_on(&s, seed * 2654435761u + (uint32_t)(b * 31 + level), level, b);
+ steps = bot_board(&s);
+ }
+ CHECK(steps > 0, "the bot never cleared %s on level %d", names[b], level);
+ if (steps > 0) printf("bot: level %d %s cleared in %d frames\n", level, names[b], steps * 4);
+ }
+}
+
+/* the bot plays a whole game from the start, through the intro and the scenes between the boards */
+static int game_boards, game_levels;
+static void bot_game(dk_state *s, uint32_t seed, int boards, void (*each)(const dk_state *)) {
+ dk_new_game(s, seed);
+ game_boards = 0;
+ while (game_boards < boards && !s->over) {
+ while (dk_mode(s) != DK_MODE_PLAY && !s->over) {
+ dk_tick(s, 0);
+ if (each) each(s);
+ }
+ if (s->over) break;
+ int steps = bot_board(s);
+ for (int wait = 0; steps < 0 && wait < 3 && dk_mode(s) == DK_MODE_PLAY; wait++) { /* no way: wait a little */
+ static dk_state t;
+ t = *s;
+ for (int f = 0; f < 24 && dk_mode(&t) == DK_MODE_PLAY && dk_alive(&t); f++) dk_tick(&t, 0);
+ if (dk_mode(&t) != DK_MODE_PLAY || !dk_alive(&t)) break;
+ *s = t;
+ steps = bot_board(s);
+ }
+ if (steps < 0) { /* no way through from here: Mario loses this life */
+ while (dk_mode(s) == DK_MODE_PLAY) dk_tick(s, 0);
+ continue;
+ }
+ for (int i = 0; i < steps; i++)
+ for (int f = 0; f < 4; f++) {
+ dk_tick(s, f ? bot_path[i] & ~DK_JUMP : bot_path[i]);
+ if (each) each(s);
+ }
+ CHECK(dk_mode(s) == DK_MODE_CLEAR, "the bot's moves didn't replay (mode %X)", dk_mode(s));
+ game_boards++;
+ }
+ game_levels = dk_level(s);
+}
+
+static void whole_game(void) {
+ static dk_state s;
+ bot_game(&s, 4242, 9, 0); /* level 1 (2 boards), 2 (3), 3 (4) */
+ run_until_mode(&s, DK_MODE_PLAY, 4000);
+ game_levels = dk_level(&s);
+ CHECK(game_boards == 9 && game_levels == 4, "the bot's game: %d boards, level %d", game_boards, game_levels);
+ CHECK(dk_score(&s) > 20000, "the bot's game: %u points", dk_score(&s));
+ printf("bot: a whole game, 9 boards to level %d, %u points, %d lives left\n", game_levels, dk_score(&s), dk_lives(&s));
+}
+
+/* ---------------------------------------------------------------- saves */
+static void saves(void) {
+ static dk_state s, back;
+ dk_profile p = {12300, 7, 21, 3, 1, 1}, q;
+ play_on(&s, 77, 2, 2);
+ for (int i = 0; i < 700; i++) dk_tick(&s, (uint8_t)(i % 90 < 40 ? DK_RIGHT : i % 90 < 50 ? DK_JUMP : 0));
+ forget(DK_SAVE_NAME), forget(DK_COPY_NAME);
+ CHECK(save_write(&p, &s), "save failed");
+ memset(&back, 0, sizeof back);
+ save_load(&q, &back);
+ CHECK(q.best == 12300 && q.games == 7 && q.boards == 21 && q.best_level == 3 && q.has_run == 1, "profile back");
+ CHECK(!memcmp(back.ram, s.ram, DK_SNAP) && !memcmp(back.vram, s.vram, 0x400) && back.loop == s.loop &&
+ back.palette == s.palette, "game back");
+ for (int i = 0; i < 1200; i++) { /* the same game goes on the same way */
+ uint8_t j = (uint8_t)(i % 70 < 30 ? DK_LEFT : i % 70 < 34 ? DK_JUMP : DK_UP);
+ dk_tick(&s, j), dk_tick(&back, j);
+ }
+ CHECK(!memcmp(back.ram, s.ram, DK_SNAP) && !memcmp(back.vram, s.vram, 0x400), "the game went another way after loading");
+ /* the file gone: the copy in the script brings it back */
+ forget(DK_SAVE_NAME);
+ memset(&q, 0, sizeof q);
+ save_load(&q, &back);
+ CHECK(q.best == 12300 && q.has_run, "from the copy");
+ uint32_t n;
+ CHECK(plat_load(DK_SAVE_NAME, &n) != 0, "the save restored from the copy");
+ /* every byte damaged in turn: refused, never a crash */
+ forget(DK_COPY_NAME);
+ save_write(&p, &s);
+ forget(DK_COPY_NAME);
+ static uint8_t keep[DK_SAVE_MAX], bad[DK_SAVE_MAX];
+ const uint8_t *d = plat_load(DK_SAVE_NAME, &n);
+ memcpy(keep, d, n);
+ int refused = 0;
+ for (uint32_t i = 0; i < n; i++) {
+ memcpy(bad, keep, n);
+ bad[i] ^= (uint8_t)(1 + i % 255);
+ plat_save(DK_SAVE_NAME, bad, n);
+ forget(DK_COPY_NAME);
+ memset(&q, 0xAB, sizeof q);
+ save_load(&q, &back);
+ refused += q.best == 0 && q.has_run == 0;
+ }
+ CHECK(refused == (int)n, "damaged saves refused: %d of %u", refused, n);
+ plat_save(DK_SAVE_NAME, keep, n - 1), save_load(&q, &back);
+ CHECK(q.best == 0, "a short save read");
+ plat_save(DK_SAVE_NAME, keep, 3), save_load(&q, &back);
+ CHECK(q.best == 0, "a 3-byte save read");
+ /* garbage in the packed game, with a right checksum: refused by the unpacker or the checks */
+ memcpy(bad, keep, n);
+ for (uint32_t i = 24; i < n - 4; i++) bad[i] = (uint8_t)(i * 37);
+ uint32_t h = dk_sum(bad, n - 4);
+ for (int i = 0; i < 4; i++) bad[n - 4 + i] = (uint8_t)(h >> (8 * i));
+ plat_save(DK_SAVE_NAME, bad, n), save_load(&q, &back);
+ CHECK(!q.has_run, "a garbled game read");
+ printf("saves: %u bytes (the game packed from %d), every damaged byte refused\n", n, DK_SNAP + 0x400 + 6);
+}
+
+/* ---------------------------------------------------------------- the whole app with random keys */
+static uint32_t fuzz_rng = 77;
+static uint32_t xr(void) {
+ fuzz_rng ^= fuzz_rng << 13, fuzz_rng ^= fuzz_rng >> 17, fuzz_rng ^= fuzz_rng << 5;
+ return fuzz_rng;
+}
+static uint32_t fuzz_keys(uint32_t f) {
+ static uint32_t held, until;
+ if (f == 1) until = 0;
+ if (f >= until) {
+ uint32_t x = xr();
+ static const uint32_t pick[] = {0, JK_OK, JK_OK, JK_LEFT, JK_RIGHT, JK_UP, JK_DOWN, JK_BACK, JK_OK | JK_RIGHT,
+ JK_OK | JK_LEFT, JK_UP | JK_RIGHT, JK_DOWN, JK_RIGHT, JK_LEFT, JK_UP, 0};
+ held = pick[x % 16];
+ until = f + 1 + (x >> 8) % 30;
+ }
+ return held;
+}
+static void fuzz(void) {
+ int sessions = 300;
+ for (int s = 0; s < sessions; s++) {
+ if (s % 60 == 0) forget(DK_SAVE_NAME), forget(DK_COPY_NAME);
+ memset(&run, 0, sizeof run), memset(&prof, 0, sizeof prof), memset(&view, 0, sizeof view);
+ fuzz_rng = (uint32_t)s * 7919u + 1;
+ script = fuzz_keys, frame_no = 0, quit_at = 100 + xr() % 3000;
+ dk_app_main();
+ uint32_t n;
+ const uint8_t *d = plat_load(DK_SAVE_NAME, &n);
+ CHECK(d != 0, "session %d: no save", s);
+ if (d) {
+ dk_profile p;
+ static dk_state t;
+ static uint8_t copy_of[DK_SAVE_MAX];
+ memcpy(copy_of, d, n);
+ CHECK(dk_file_ok(copy_of, n, &p, &t), "session %d: the save doesn't read back", s);
+ if (p.has_run) CHECK(dk_valid(&t), "session %d: a broken game was saved", s);
+ }
+ }
+ script = 0;
+ printf("fuzz: %d sessions of random keys, each quit with Home and its save read back\n", sessions);
+}
+
+/* ---------------------------------------------------------------- pictures */
+static void picture(const dk_state *s, const char *dir, const char *name) {
+ char path[512];
+ snprintf(path, sizeof path, "%s/%s.ppm", dir, name);
+ view.best = 18200, view.best_level = 3;
+ dk_draw_invalidate();
+ dk_draw(s, &view);
+ shot(path);
+}
+
+/* the bot's game, a picture of each board a while into it */
+static const char *shots_dir;
+static int shot_marks, board_frames, last_board;
+static void shot_each(const dk_state *s) {
+ static const struct { int board, frames; const char *name; } when[] = {
+ {1, 420, "25m"}, {2, 600, "100m"}, {4, 300, "75m"}, {7, 420, "50m"}, {6, 700, "shot"}};
+ if (game_boards != last_board) last_board = game_boards, board_frames = 0;
+ if (dk_mode(s) == DK_MODE_PLAY) board_frames++;
+ for (unsigned i = 0; i < sizeof when / sizeof when[0]; i++)
+ if (!(shot_marks >> i & 1) && game_boards == when[i].board - 1 && board_frames == when[i].frames) {
+ picture(s, shots_dir, when[i].name);
+ shot_marks |= 1 << i;
+ }
+}
+
+static void shots(const char *dir) {
+ static dk_state s;
+ memset(&view, 0, sizeof view);
+ view.screen = DK_SCREEN_TITLE, view.clock = 40;
+ picture(&s, dir, "title");
+ view.screen = DK_SCREEN_GUIDE;
+ picture(&s, dir, "guide");
+ view.screen = DK_SCREEN_PLAY;
+ dk_new_game(&s, 12345);
+ for (int i = 0; i < 2000 && dk_mode(&s) != DK_MODE_HOWHIGH; i++) {
+ dk_tick(&s, 0);
+ if (i == 200) picture(&s, dir, "intro1");
+ if (i == 420) picture(&s, dir, "intro2");
+ }
+ picture(&s, dir, "intro3");
+ for (int i = 0; i < 40; i++) dk_tick(&s, 0);
+ picture(&s, dir, "howhigh");
+ shots_dir = dir, shot_marks = 0, last_board = -1;
+ bot_game(&s, 4242, 9, shot_each);
+ view.screen = DK_SCREEN_PAUSE, view.sel = 0;
+ picture(&s, dir, "pause");
+}
+
+static const char *film_dir;
+static int film_frames, film_done, film_tick;
+static void film_each(const dk_state *s) {
+ static char path[512];
+ if (film_done >= film_frames || film_tick++ % 2) return;
+ snprintf(path, sizeof path, "%s/f_%04d.ppm", film_dir, film_done++);
+ dk_draw(s, &view);
+ shot(path);
+}
+static void film(const char *dir, int frames) {
+ static dk_state s;
+ memset(&view, 0, sizeof view);
+ view.screen = DK_SCREEN_PLAY;
+ film_dir = dir, film_frames = frames, film_done = 0, film_tick = 0;
+ dk_draw_invalidate();
+ bot_game(&s, 4242, 9, film_each);
+}
+
+int main(int argc, char **argv) {
+ gfx_buf = strip;
+ if (argc >= 2 && !strcmp(argv[1], "test")) {
+ clock_t c0 = clock();
+ units();
+ clock_t c1 = clock();
+ bots();
+ whole_game();
+ clock_t c2 = clock();
+ saves();
+ fuzz();
+ fprintf(stderr, "(%.0f s rules, %.0f s bot, %.0f s saves and keys)\n", (double)(c1 - c0) / CLOCKS_PER_SEC,
+ (double)(c2 - c1) / CLOCKS_PER_SEC, (double)(clock() - c2) / CLOCKS_PER_SEC);
+ printf(fails ? "%d FAILED\n" : "all passed\n", fails);
+ return fails != 0;
+ }
+ if (argc >= 3 && !strcmp(argv[1], "bot1")) { /* one board: bot1 LEVEL SCREEN [SEED] */
+ static dk_state s;
+ play_on(&s, argc > 4 ? (uint32_t)atoi(argv[4]) : 1, atoi(argv[2]), atoi(argv[3]));
+ int steps = bot_board(&s);
+ printf("%d steps, best waypoint %d at %02X,%02X\n", steps, beam[0].wp, M(&beam[0].s, 0x6203), M(&beam[0].s, 0x6205));
+ return steps < 0;
+ }
+ if (argc >= 3 && !strcmp(argv[1], "shots")) {
+ shots(argv[2]);
+ return fails != 0;
+ }
+ if (argc >= 4 && !strcmp(argv[1], "film")) {
+ film(argv[2], atoi(argv[3]));
+ return 0;
+ }
+ fprintf(stderr, "usage: play test | shots DIR | film DIR N\n");
+ return 2;
+}
diff --git a/games/donkeykong/tests/test.c b/games/donkeykong/tests/test.c
deleted file mode 100644
index 4fdb19f2..00000000
--- a/games/donkeykong/tests/test.c
+++ /dev/null
@@ -1,187 +0,0 @@
-/* SPDX-License-Identifier: GPL-3.0-only */
-#include "game.h"
-#include "render.h"
-#include <assert.h>
-#include <stdio.h>
-#include <string.h>
-#include <stdlib.h>
-static dk_game game;
-static dk_stats stats;
-static uint16_t image[320*240];
-static unsigned pushes;
-static void push(int x,int y,int w,int h,const uint16_t *p) {
- assert(x>=0&&y>=0&&x+w<=320&&y+h<=240);++pushes;
- for(int j=0;j<h;++j)memcpy(image+(y+j)*320+x,p+j*w,(size_t)w*2);
-}
-static void shot(const char *name,unsigned view) {
- dk_invalidate();dk_render(&game,&stats,view,0,true,false,800,push);
- char path[100];snprintf(path,sizeof path,"%s.ppm",name);FILE *f=fopen(path,"wb");assert(f);
- fprintf(f,"P6\n320 240\n255\n");
- for(unsigned i=0;i<320*240;++i){unsigned c=image[i];unsigned char rgb[]={
- (unsigned char)((c>>11)*255/31),(unsigned char)(((c>>5)&63)*255/63),(unsigned char)((c&31)*255/31)};
- fwrite(rgb,1,3,f);}
- fclose(f);
-}
-static void step(unsigned input) {dk_step(&game,&stats,(uint8_t)input);assert(dk_valid(&game));}
-static void frames(unsigned n,unsigned input) {while(n--)step(input);}
-static void board(unsigned b) {
- dk_new(&game,1981);game.board=(uint8_t)b;game.phase=DK_CLEAR;game.timer=1;
- // Public transition is the only board loader. Choose the preceding board.
- game.board=(uint8_t)(b?b-1:3);step(0);
- if(!b)game.round=1;
- frames(65,0);assert(game.phase==DK_RUNNING&&game.board==b);
-}
-static void quiet(void) {memset(game.enemy,0,sizeof game.enemy);game.spawn=65535;}
-static void place(unsigned floor,int x) {
- dk_player *p=&game.player;memset(p,0,sizeof *p);p->x=x*DK_Q;
- p->y=dk_surface(&game,floor,x)*DK_Q;p->grounded=1;p->support=(int8_t)floor;
- p->face=1;p->ladder=-1;p->fall_from=p->y;p->jump_id=1;
-}
-static void walk(int x) {
- unsigned limit=400;
- while(game.phase==DK_RUNNING&&abs(game.player.x/DK_Q-x)>1&&limit--) {
- unsigned dir=game.player.x/DK_Q<x?DK_RIGHT:DK_LEFT;
- unsigned input=dir;
- if(game.board==3&&game.player.support>=1&&game.player.support<=4&&
- (game.player.grounded||game.player.coyote)&&!game.player.hammer) {
- unsigned row=(unsigned)game.player.support-1;
- for(unsigned side=0;side<2;++side) {
- int gap=side?176:48,delta=gap-game.player.x/DK_Q;
- if((game.rivets&(1u<<(row*2+side)))&&abs(delta)<=7&&
- ((dir==DK_RIGHT&&delta>=0)||(dir==DK_LEFT&&delta<=0)))input|=DK_JUMP;
- }
- }
- step(input);
- if(game.phase!=DK_RUNNING&&game.phase!=DK_CLEAR)
- fprintf(stderr,"Walk failure board=%u age=%lu target=%d x=%ld y=%ld floor=%d reason=%u rivets=%u\n",
- game.board,(unsigned long)game.age,x,(long)game.player.x/DK_Q,(long)game.player.y/DK_Q,game.player.support,game.reason,game.rivets);
- assert(game.phase==DK_RUNNING||game.phase==DK_CLEAR);
- }
- assert(limit);
-}
-static void climb(unsigned index) {
- const dk_ladder *l=&game.ladder[index];walk(l->x);
- unsigned limit=200;
- while(game.player.support!=l->upper||!game.player.grounded) {
- if(game.player.hammer)step(0);else step(DK_UP);
- assert(game.phase==DK_RUNNING||game.phase==DK_CLEAR);assert(--limit);
- }
-}
-static void saves(void) {
- uint8_t bytes[32];dk_stats a={999999,1234,7,999},b={0};dk_encode_stats(&a,bytes);
- assert(dk_decode_stats(&b,bytes,32));assert(b.best==a.best&&b.runs==a.runs&&b.rescues==7&&b.best_round==999);
- for(unsigned i=0;i<32;++i){bytes[i]^=1;assert(!dk_decode_stats(&b,bytes,32));bytes[i]^=1;}
- assert(!dk_decode_stats(&b,bytes,31));assert(!dk_decode_stats(&b,NULL,0));
- assert(b.best==999999&&b.runs==1234);
-}
-static void mechanics(void) {
- board(0);quiet();place(0,196);climb(0);assert(game.player.support==1);
- place(0,78);frames(60,DK_UP);assert(game.player.support==0&&game.player.ladder<0);
- place(0,196);game.player.hammer=100;frames(10,DK_UP|DK_JUMP);
- assert(game.player.grounded&&game.player.ladder<0&&game.player.hammer==90);
- // Jump has an edge, holding the key cannot bounce automatically.
- place(0,100);step(0);frames(70,DK_JUMP);assert(game.player.grounded&&game.player.jump_id==2);
- place(5,8);frames(20,DK_LEFT);frames(100,0);assert(game.phase==DK_READY||game.phase==DK_DYING);
- board(0);quiet();game.bonus=100;game.bonus_ticks=89;step(0);
- assert(game.phase==DK_DYING&&game.reason==DK_TIME);frames(70,0);assert(game.lives==2);
- frames(65,0);quiet();game.lives=1;game.bonus=0;game.bonus_ticks=89;step(0);frames(70,0);assert(game.phase==DK_OVER);
- board(1);quiet();place(4,54);game.age=180;step(DK_UP);assert(game.player.ladder<0);
- game.age=240;step(DK_UP);assert(game.player.ladder==6);
- // A retracting ladder never drops a player already climbing it.
- game.age=150;frames(50,DK_UP);assert(game.player.support==6&&game.player.grounded);
- board(0);quiet();place(0,100);game.player.hammer=50;
- game.enemy[0]=(dk_enemy){.active=1,.type=DK_BARREL,.row=0,.x=110*DK_Q,
- .y=dk_surface(&game,0,110)*DK_Q,.dir=-1,.ladder=-1};
- step(0);assert(game.score==300&&!game.enemy[0].active);frames(10,0);assert(game.score==300);
- board(0);quiet();place(0,100);
- game.enemy[0]=(dk_enemy){.active=1,.type=DK_BARREL,.row=0,.x=104*DK_Q,
- .y=dk_surface(&game,0,104)*DK_Q,.dir=-1,.ladder=-1};
- step(0);assert(game.phase==DK_DYING&&game.reason==DK_CRASH);
- // Jumping across one barrel scores exactly once, upon a safe landing.
- board(0);quiet();place(0,110);game.previous_input=0;
- game.enemy[0]=(dk_enemy){.active=1,.type=DK_BARREL,.row=0,.x=132*DK_Q,
- .y=dk_surface(&game,0,132)*DK_Q,.dir=-1,.ladder=-1};
- step(DK_RIGHT|DK_JUMP);frames(34,DK_RIGHT);
- assert(game.phase==DK_RUNNING&&game.score==100&&game.player.grounded);
- frames(20,0);assert(game.score==100);
- // The extra life is granted once; scores saturate instead of wrapping.
- board(0);quiet();place(0,100);game.player.hammer=100;game.score=6990;
- game.enemy[0]=(dk_enemy){.active=1,.type=DK_BARREL,.row=0,.x=110*DK_Q,
- .y=dk_surface(&game,0,110)*DK_Q,.dir=-1,.ladder=-1};
- step(0);assert(game.lives==4&&game.extra_life);
- game.score=DK_MAX_SCORE-10;
- game.enemy[0]=(dk_enemy){.active=1,.type=DK_BARREL,.row=0,.x=110*DK_Q,
- .y=dk_surface(&game,0,110)*DK_Q,.dir=-1,.ladder=-1};
- step(0);assert(game.score==DK_MAX_SCORE&&game.lives==4);
- game.round=999;assert(dk_difficulty(&game)==8);
-}
-static void routes(void) {
- // Actual walking/climbing routes, hazards suppressed to test the geometry.
- board(0);quiet();memset(game.item,0,sizeof game.item);
- unsigned path[]={0,2,4,6,8,10};for(unsigned i=0;i<6;++i)climb(path[i]);
- assert(game.phase==DK_CLEAR&&game.score>0);frames(125,0);assert(game.board==1&&game.phase==DK_READY);
- board(1);quiet();memset(game.item,0,sizeof game.item);
- climb(0);climb(2);climb(4); // left half, safe from central oil
- while(!dk_ladder_open(&game,6))step(0);
- climb(6);walk(184);climb(8);assert(game.phase==DK_CLEAR);
- // Full elevator route: catch a lift, transfer via the top ledge, then
- // step down on the right-hand girders and use the final ladders.
- board(2);quiet();walk(40);frames(24,DK_JUMP|DK_RIGHT);
- unsigned wait=120;
- while(!game.player.grounded&&wait--)step(0);
- assert(wait&&game.player.support>=11&&game.player.support<=13);
- wait=500;
- while(game.player.y>71*DK_Q&&wait--)step(0);
- assert(wait);step(DK_JUMP);frames(40,0);
- assert(game.player.support==5&&game.player.grounded);walk(78);
- wait=400;
- while(wait--) {
- bool soon=false;
- for(unsigned i=14;i<17;++i)if(game.platform[i].y1>201)soon=true;
- if(soon)break;
- step(0);
- }
- assert(wait);frames(35,DK_JUMP|DK_RIGHT);wait=70;
- while(!game.player.grounded&&wait--)step(0);
- assert(wait&&game.player.support>=14&&game.player.support<=16);
- walk(154);assert(game.player.support==4);climb(3);climb(4);
- assert(game.phase==DK_CLEAR);
- // An entire rivet board with real movement and jumps over each open gap.
- board(3);quiet();memset(game.item,0,sizeof game.item);
- climb(0);walk(196);climb(3);walk(28);climb(4);walk(196);climb(7);walk(176);
- walk(196);
- wait=150;while(game.player.support!=3||!game.player.grounded){step(DK_DOWN);assert(--wait);}
- walk(28);climb(6);walk(48);assert(game.phase==DK_CLEAR);
- // Each rivet is consumed once; clearing all eight advances the round.
- board(3);quiet();memset(game.item,0,sizeof game.item);
- for(unsigned row=1;row<=4;++row)for(unsigned side=0;side<2;++side) {
- place(row,side?176:48);step(0);
- assert(game.rivets&(1u<<((row-1)*2+side)));
- assert(dk_surface(&game,row,side?176:48)==-1);
- }
- assert(game.phase==DK_CLEAR&&stats.rescues);frames(125,0);assert(game.board==0&&game.round==2);
- // Landing on an ascending lift carries the player; a wrap cannot teleport them.
- board(2);quiet();place(11,68);int before=game.player.y;
- frames(30,0);assert(game.player.grounded&&game.player.support==11&&game.player.y<before);
- game.age=355;step(0);assert(!game.player.grounded||game.player.support!=11);
-}
-static void soak(void) {
- uint32_t seed=0xabc123;
- for(unsigned b=0;b<4;++b)for(unsigned run=0;run<12;++run) {
- board(b);game.rng+=run*19;
- for(unsigned i=0;i<18000;++i) {
- seed^=seed<<13;seed^=seed>>17;seed^=seed<<5;
- unsigned input=(seed>>8)&31;if(i%12)input=game.previous_input;
- step(input);if(game.phase==DK_OVER)break;
- }
- }
-}
-int main(void) {
- saves();mechanics();routes();soak();
- memset(&stats,0,sizeof stats);stats.best=12000;
- for(unsigned b=0;b<4;++b) {board(b);frames(100,0);char name[20];snprintf(name,sizeof name,"board%u",b);shot(name,DK_PLAY);
- FILE *f;snprintf(name,sizeof name,"fixture%u.bin",b);f=fopen(name,"wb");assert(f);fwrite(&game,1,sizeof game,f);fclose(f);}
- board(0);shot("title",DK_TITLE);shot("pause",DK_PAUSE);shot("help",DK_HELP);
- unsigned p=pushes;dk_render(&game,&stats,DK_HELP,0,true,false,800,push);assert(p==pushes);
- printf("PASS: physics, collisions, routes, timers, saves, bounded soak, rendering. Game RAM %zu bytes.\n",sizeof game);
-}
diff --git a/games/donkeykong/tests/test_arm.py b/games/donkeykong/tests/test_arm.py
deleted file mode 100644
index c42ba941..00000000
--- a/games/donkeykong/tests/test_arm.py
+++ /dev/null
@@ -1,113 +0,0 @@
-#!/usr/bin/env python3
-"""Actual ARM .nwa smoke tests: menu/pause, all boards, save/reload and safe exit.
-Requires the repository emulator's dependencies (Unicorn, pyelftools, Pillow).
-"""
-import argparse
-import io
-import json
-import os
-from pathlib import Path
-import re
-import shlex
-import struct
-import tempfile
-import types
-
-ROOT = Path(__file__).resolve().parents[1]
-REPO = ROOT.parents[1]
-emu_path = REPO/'tools/emu.py'
-emu = types.ModuleType('dk_emu');emu.__file__=str(emu_path);emu.io=io
-source = emu_path.read_text().replace('ELFFile(open(elf, "rb"))','ELFFile(io.BytesIO(open(elf, "rb").read()))')
-source = source.replace('cmd = [nwlink, "nwa-elf",','cmd = nwlink + ["nwa-elf",')
-source = source.replace('chunk = CPU_HZ // 1000','chunk = CPU_HZ // 10000')
-source = source.replace('self.now_ms += 1\n','self.now_ms += 0.1\n')
-source = source.replace(' next_frame = self.now_ms if frame_ms else None',
- ' self.checkpoint = until_ms\n next_frame = self.now_ms if frame_ms else None')
-source = source.replace(' self.slept_ms += r0\n',
- ' self.slept_ms += r0\n if self.now_ms >= self.checkpoint: uc.emu_stop()\n')
-exec(compile(source,str(emu_path),'exec'),emu.__dict__)
-
-def presses(*items):
- return [(t,t+d,emu.KEYS[k]) for t,k,d in items]
-
-def storage(path,items):
- data=bytearray(emu.STORAGE_SIZE);offset=0
- for name,value in items:
- record=name.encode()+b'\0'+value;size=2+len(record)
- struct.pack_into('<H',data,offset,size);data[offset+2:offset+size]=record;offset+=size
- assert offset+2<=len(data)
- path.write_bytes(data)
-
-def records(data):
- result={};offset=0
- while offset+2<=len(data):
- n=struct.unpack_from('<H',data,offset)[0]
- if not n:break
- assert n>=3 and offset+n<=len(data)
- name,value=data[offset+2:offset+n].split(b'\0',1);result[name.decode()]=value;offset+=n
- return result
-
-def check_exit(c,upsilon=False):
- assert c.exited,'App did not exit'
- assert not c.violations,sorted(set(c.violations))
- assert not c.firmware_ram_changes(),'Firmware RAM changed'
- assert c.app_ram_clean(),'App RAM not cleared'
- assert c.locks==0 and c.max_locks==(0 if upsilon else 1)
- assert c.stack_used()<4096,c.stack_used()
-
-def uint(c,name):
- return struct.unpack('<I',bytes(c.uc.mem_read(c.symbols[name],4)))[0]
-
-def main():
- ap=argparse.ArgumentParser(description=__doc__)
- ap.add_argument('--nwlink',default='nwlink')
- a=ap.parse_args()
- if Path(a.nwlink).is_file():
- nw=[a.nwlink] if a.nwlink.endswith(('.exe','.cmd')) else ['node',a.nwlink]
- else:
- nw=([v.replace('"','') for v in re.findall(r'(?:[^\s"]|"[^"]*")+',a.nwlink)]
- if os.name=='nt' else shlex.split(a.nwlink))
- out=ROOT/'output/arm';out.mkdir(parents=True,exist_ok=True);tempfile.tempdir=str(out)
- nwa=str(ROOT/'output/donkeykong-debug.nwa');report=[];sentinel=b'print(42)\n'
- for profile,model,system,ram in [('n0120','n0120','epsilon',153676),('n0110','n0110','epsilon',148928),('upsilon','n0110','upsilon',107674)]:
- emu.configure(model,system,ram);st=out/(profile+'.bin');storage(st,[('keep.py',sentinel)])
- c=emu.Calculator(nwa,storage_file=str(st),nwlink=nw);c.enable_checks()
- c.keys=presses((300,'ok',80),(1600,'right',200),(1900,'ok',60),(2200,'back',80),
- (3300,'back',80),(3800,'home',180))
- c.run(250);c.save_png(str(out/(profile+'-title.png')))
- c.run(2350);c.save_png(str(out/(profile+'-pause.png')))
- before=bytes(c.uc.mem_read(c.symbols['game'],864));c.run(3200)
- assert bytes(c.uc.mem_read(c.symbols['game'],864))==before,'Pause advanced the game'
- # Seed a known high score to exercise the real storage integration.
- c.uc.mem_write(c.symbols['stats'],struct.pack('<I',12345))
- c.run(4200);check_exit(c,system=='upsilon')
- r=records(st.read_bytes());assert r['keep.py']==sentinel
- saved=r['donkeykong.sav'];assert len(saved)==32 and struct.unpack_from('<I',saved,4)[0]==12345
- d=emu.Calculator(nwa,storage_file=str(st),nwlink=nw);d.enable_checks()
- d.keys=presses((400,'onoff',150));d.run(250)
- assert uint(d,'stats')==12345;d.run(900);check_exit(d,system=='upsilon')
- assert records(st.read_bytes())['keep.py']==sentinel
- report.append(dict(profile=profile,pause_save_reload_exit='pass',stack_bytes=max(c.stack_used(),d.stack_used())))
- print(profile,'PASS',flush=True)
- # Real ARM code for every board (host fixture sets a representative scene).
- emu.configure();c=emu.Calculator(nwa,nwlink=nw);c.enable_checks();c.keys=presses((200,'ok',60))
- c.run(400)
- for b in range(4):
- fixture=(ROOT/f'output/fixture{b}.bin').read_bytes();assert len(fixture)==864
- c.uc.mem_write(c.symbols['game'],fixture)
- end=c.now_ms+700;c.run(end);c.save_png(str(out/f'board{b}.png'))
- assert not c.violations,sorted(set(c.violations))
- report.append(dict(boards='four ARM scenes',frames=uint(c,'perf_frames'),slowest_draw_ms=uint(c,'perf_max'),
- frames_over_17ms=uint(c,'perf_slow'),stack_bytes=c.stack_used()))
- # A completely full filesystem must never delete another file or trap Home.
- st=out/'full.bin';payload=b'x'*(emu.STORAGE_SIZE-2-2-len('keep.py')-1)
- storage(st,[('keep.py',payload)]);c=emu.Calculator(nwa,storage_file=str(st),nwlink=nw);c.enable_checks()
- c.keys=presses((400,'home',180),(1100,'1',80),(1600,'home',100),(2200,'back',80))
- c.run(900);assert not c.exited;c.save_png(str(out/'storage-full.png'))
- c.run(1400);assert not c.exited
- c.run(2600);check_exit(c);assert records(st.read_bytes())=={'keep.py':payload}
- report.append(dict(full_storage='continue, retry exit, leave without deleting files: pass'))
- (out/'report.json').write_text(json.dumps(report,indent=2)+'\n');print(json.dumps(report,indent=2))
-
-if __name__=='__main__':
- main()
diff --git a/games/donkeykong/tools/art.py b/games/donkeykong/tools/art.py
new file mode 100644
index 00000000..9de3855e
--- /dev/null
+++ b/games/donkeykong/tools/art.py
@@ -0,0 +1,1627 @@
+#!/usr/bin/env python3
+"""src/art.h: Donkey Kong's pictures, drawn for this port (no Nintendo graphics data).
+
+Bobydeluxe drew the first set for his adaptation; they are redrawn here at the arcade's sizes, with its
+poses and animation frames: every sprite is a 16x16 cell with three colours and transparency, looked up
+by the arcade's sprite code (see the dkdasm notes), and Kong is assembled from parts by the game's own
+pose tables. Tiles (girders, ladders, belts, rivets, letters) are 8x8.
+
+ python3 -I tools/art.py src/art.h [preview.png] (the preview needs Pillow)
+"""
+import sys
+
+# ---------------------------------------------------------------- palettes (index 1..3; 0 is transparent)
+PAL = {
+ "mario": [0xFF2010, 0x2830FF, 0xFFB855],
+ "kong": [0xB81000, 0xFFB855, 0xFF6800],
+ "kface": [0xB81000, 0xFFB855, 0xFFFFFF],
+ "paul_top": [0xFF6800, 0xFFF4E8, 0xFF47AA],
+ "paul_low": [0xFF47AA, 0xFFFFFF, 0x1010B0],
+ "barrel": [0xFF6800, 0xFFB855, 0x2020FF],
+ "blue": [0x2020FF, 0x00F0FF, 0xFFFFFF],
+ "fire": [0xFFC200, 0xFF2000, 0xFFFFFF],
+ "fire_blue": [0x9797FF, 0xFF2000, 0x00F0FF],
+ "hammer": [0xFFB800, 0x972000, 0xFFFFFF],
+ "hammer_flash": [0xFF2000, 0xFFE070, 0xFFFFFF],
+ "points": [0xB81000, 0xFFB855, 0xFFFFFF],
+ "pie": [0xF8B999, 0x2020F0, 0xFF7100],
+ "spring": [0xA4A5FF, 0xFF2000, 0xFFFFFF],
+ "elevator": [0xFF2C62, 0xA40000, 0xFF2C62],
+ "motor": [0xFFC200, 0xA40000, 0xFF6800],
+ "ladder_white": [0xFCFCFF, 0xFCFCFF, 0xFCFCFF],
+ "pulley": [0x9B9BFF, 0xFA0000, 0x00F0FF],
+ "flame": [0xFF2000, 0xFFB800, 0xFFFFFF],
+ "items": [0xFF47AA, 0xFFFFFF, 0x1010B0],
+ "heart": [0xFF6800, 0xFFFFFF, 0xFF47AA],
+ "black": [0x000000, 0x000000, 0x000000],
+ "smash": [0x2020FF, 0x00F0FF, 0xFFFFFF],
+ "dizzy": [0xFFFFFF, 0xFFC200, 0x00F0FF],
+}
+
+# ---------------------------------------------------------------- sprites: code -> (palette, 16 rows); letters map to 1..3
+
+
+def grid(rows, keys):
+ out = []
+ for r in rows:
+ r = r.ljust(16, ".")[:16]
+ out.append([0 if c == "." else keys.index(c) + 1 for c in r])
+ while len(out) < 16:
+ out.append([0] * 16)
+ return out
+
+
+def mirror(g):
+ return [list(reversed(r)) for r in g]
+
+
+def vflip(g):
+ return [list(r) for r in reversed(g)]
+
+
+def rot_cw(g):
+ return [[g[15 - x][y] for x in range(16)] for y in range(16)]
+
+
+def rot_ccw(g):
+ return [[g[x][15 - y] for x in range(16)] for y in range(16)]
+
+
+def shift(g, dx, dy):
+ out = [[0] * 16 for _ in range(16)]
+ for y in range(16):
+ for x in range(16):
+ sx, sy = x - dx, y - dy
+ if 0 <= sx < 16 and 0 <= sy < 16:
+ out[y][x] = g[sy][sx]
+ return out
+
+
+S = {} # code -> (palette, grid)
+
+# Mario (Jumpman): drawn facing right, stored facing left as the arcade's codes 0x00-0x0F are
+M = "RBP" # red, blue, peach
+HEAD = [
+ "......RRRRR.....",
+ ".....RRRRRRRRR..",
+ ".....BBBPPBP....",
+ "....BPBPPPBPPP..",
+ "....BPBBPPPBPPP.",
+ "....BBPPPPBBBB..",
+ "......PPPPPPP...",
+]
+mario = {
+ 0x00: ["................"] + HEAD + [
+ ".....BBRBBB.....",
+ "....BBBRBBRBBB..",
+ "...BBBBRRRRBBBB.",
+ "...PPBRPRRPRBPP.",
+ "...PPPRRRRRRPPP.",
+ "....PRRRRRRRRP..",
+ ".....RRR..RRR...",
+ "....BBBB..BBBB..",
+ ],
+ 0x01: ["................"] + HEAD + [
+ "....BBBRRBB.P...",
+ "...PBBBRRRBBPP..",
+ "..PPBBRRRRRR.P..",
+ "..PP.RRRRRRR....",
+ ".....RRRRRRRR...",
+ "....RRRR..RRRR..",
+ "...BBBB....BBB..",
+ "...BBB......BBB.",
+ ],
+ 0x02: ["................"] + HEAD + [
+ ".....BBRBBB.....",
+ "....BBBRRBBB....",
+ "....BBRRPRRB....",
+ "....BPPRRRRR....",
+ "....RPPRRRR.....",
+ ".....RRRBBB.....",
+ ".....BBBBBB.....",
+ "......BBBB......",
+ ],
+ # climbing, seen from behind: left hand up / over the top / back
+ 0x03: [
+ "...PP...........",
+ "...PP.RRRRR.....",
+ "...BB.RRRRRR....",
+ "...BBRRRRRRR....",
+ "...BBBBBBBBB....",
+ "....BBBBBBBBB...",
+ ".....BRBBBRBB...",
+ ".....BRBBBRBBB..",
+ ".....RRRRRRR.BB.",
+ ".....RRRRRRR.PP.",
+ ".....RRRRRRR.PP.",
+ ".....RRR.RRR....",
+ ".....RRR.RRBB...",
+ "....BBRR..BBB...",
+ "....BBB.........",
+ "....BBB.........",
+ ],
+ 0x04: [
+ "................",
+ "................",
+ "................",
+ "......RRRRR.....",
+ ".....RRRRRRR....",
+ ".PP..RRRRRRR..PP",
+ ".PPBBBBBBBBBBBPP",
+ "..BBBBBBBBBBBBB.",
+ "...BBRBBBBBRBB..",
+ ".....RRRRRRR....",
+ ".....RRRRRRR....",
+ ".....RRRRRRR....",
+ ".....RRR.RRR....",
+ ".....RRR.RRR....",
+ "....BBBB.BBBB...",
+ "....BBB...BBB...",
+ ],
+ 0x05: [
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ ".....RRRRRRR....",
+ ".PP.RRRRRRRRR.PP",
+ ".PPBBBBBBBBBBBPP",
+ "..BBBBBBBBBBBBB.",
+ "...BBRBBBBBRBB..",
+ "....RRRRRRRRR...",
+ "....RRRRRRRRR...",
+ "....RRRR.RRRR...",
+ "...BBBB...BBBB..",
+ "...BBB.....BBB..",
+ "................",
+ ],
+ 0x06: [
+ "................",
+ "......RRRRR.....",
+ ".....RRRRRRR....",
+ ".....RRRRRRR....",
+ ".....BBBBBBB....",
+ "....BBBBBBBBB...",
+ "...BBRBBBBBRBB..",
+ "..PBBRBBBBBRBBP.",
+ "..PPBRRRRRRRBPP.",
+ "..PP.RRRRRRR.PP.",
+ ".....RRRRRRR....",
+ ".....RRRRRRR....",
+ ".....RRR.RRR....",
+ ".....RRR.RRR....",
+ "....BBBB.BBBB...",
+ "....BBB...BBB...",
+ ],
+ # with the hammer: held up (even) or swung (odd), standing and the two running steps
+ 0x08: [
+ "......PP........",
+ "......PPRRR.....",
+ ".....RBRRRRRRR..",
+ ".....BBBPPBP....",
+ "....BPBPPPBPPP..",
+ "....BPBBPPPBPPP.",
+ "....BBPPPPBBBB..",
+ ".....BPPPPPPP...",
+ ".....BBRBBB.....",
+ "....BBBRBBRBB...",
+ "....BBBRRRRBB...",
+ "....RRRRRRRRR...",
+ "....RRRRRRRRR...",
+ ".....RRR..RRR...",
+ "....BBBB..BBBB..",
+ "................",
+ ],
+ 0x09: ["................"] + HEAD + [
+ ".....BBBBBBBBPP.",
+ "....BBRBBRBBBPP.",
+ "...BBBRRRRB.....",
+ "....RRRRRRR.....",
+ "....RRRRRRRR....",
+ ".....RRR..RRR...",
+ "....BBBB..BBBB..",
+ "................",
+ ],
+ 0x0A: [
+ "......PP........",
+ "......PPRRR.....",
+ ".....RBRRRRRRR..",
+ ".....BBBPPBP....",
+ "....BPBPPPBPPP..",
+ "....BPBBPPPBPPP.",
+ "....BBPPPPBBBB..",
+ ".....BPPPPPPP...",
+ "....BBBRRBB.....",
+ "....BBBRRRBB....",
+ "....BBRRRRRR....",
+ ".....RRRRRRR....",
+ ".....RRRRRRRR...",
+ "....RRRR..RRRR..",
+ "...BBBB....BBB..",
+ "...BBB......BBB.",
+ ],
+ 0x0B: ["................"] + HEAD + [
+ "....BBBBBBBBBPP.",
+ "...BBBBRRBBBBPP.",
+ "...BBBRRRRR.....",
+ ".....RRRRRRR....",
+ ".....RRRRRRRR...",
+ "....RRRR..RRRR..",
+ "...BBBB....BBB..",
+ "...BBB......BBB.",
+ ],
+ 0x0C: [
+ "......PP........",
+ "......PPRRR.....",
+ ".....RBRRRRRRR..",
+ ".....BBBPPBP....",
+ "....BPBPPPBPPP..",
+ "....BPBBPPPBPPP.",
+ "....BBPPPPBBBB..",
+ ".....BPPPPPPP...",
+ ".....BBRBBB.....",
+ "....BBBRRBBB....",
+ "....BBRRRRRB....",
+ ".....RRRRRR.....",
+ ".....RRRBBB.....",
+ ".....BBBBBB.....",
+ "......BBBB......",
+ "................",
+ ],
+ 0x0D: ["................"] + HEAD + [
+ ".....BBBBBBBBPP.",
+ "....BBBRRBBBBPP.",
+ "....BBRRRRR.....",
+ ".....RRRRRR.....",
+ ".....RRRBBB.....",
+ ".....BBBBBB.....",
+ "......BBBB......",
+ "................",
+ ],
+ 0x0E: ["................"] + HEAD + [
+ "..PP.BBBRBBB.PP.",
+ "..PPBBBBRBBRBPP.",
+ ".......RRRRRR...",
+ "......RRRRRRRR..",
+ ".....RRRR..RRR..",
+ "....BBBB....BBB.",
+ "...BBBB......BB.",
+ "................",
+ ],
+ 0x0F: ["................", "................"] + HEAD + [
+ "....BBBRBBBB....",
+ "...PBBBRRBBBP...",
+ "..PPBBRRRRBBPP..",
+ "..PP.RRRRRRR.PP.",
+ "....RRRR.RRRR...",
+ "...BBBB...BBBB..",
+ "..BBBB.....BBBB.",
+ ],
+}
+for code, rows in mario.items():
+ S[code] = ("mario", mirror(grid(rows, M)))
+stand = grid(mario[0x00], M)
+S[0x78] = ("mario", mirror(vflip(stand))) # dying: upside down
+S[0x79] = ("mario", rot_cw(mirror(stand))) # dying: head to the right
+S[0x7A] = ("mario", grid([ # dead, with a halo
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "..PPPP..........",
+ ".PPPPPP.........",
+ ".PPPPPP.........",
+ "..PPPP..........",
+ "................",
+ "...RRRB.BB......",
+ "..RRRRBBBBRRRBBB",
+ ".BPPPRBBBBRRRRBB",
+ ".BPPPPBBRRRRRRBB",
+ "..PPP..PP.RRR...",
+], M))
+
+# Pauline (facing right): her top half, her lower half in three poses, carried by Kong
+P_TOP = "OWP" # orange hair, skin, pink dress
+S[0x10] = ("paul_top", grid([
+ "................",
+ "................",
+ ".......OOOO.....",
+ "......OOOOOO....",
+ ".....OOOOWWO....",
+ "...OOOOOWWWWW...",
+ "..OOOOOOWWOWW...",
+ "..OO.OOOWWWW....",
+ ".OO...OOPWW.....",
+ ".O.....PPPPW....",
+ "......PPPPPPWW..",
+ ".....PPPPPPPP...",
+ ".....PPPPPP.....",
+ "......PPPPP.....",
+ ".....PPPPPPP....",
+ ".....PPPPPPPP...",
+], P_TOP))
+P_LOW = "PWB" # pink dress, white trim, blue shoes
+S[0x11] = ("paul_low", grid([
+ ".....PPPPPPPP...",
+ "....PPPPPPPPPP..",
+ "....PPPPPPPPPP..",
+ "...PPPPPPPPPPPP.",
+ "...PPPWPPPPPPPP.",
+ "..PPPPWWPPPPPPP.",
+ "..PPPPPWWWPPPPP.",
+ ".PPPPPPPPWWWWW..",
+ ".PPPPPPPPPPPP...",
+ "....WW...WW.....",
+ "...BBB...BB.....",
+ "..BBBB...BBB....",
+ "..BB......BB....",
+], P_LOW))
+S[0x12] = ("paul_low", grid([
+ ".....PPPPPPPP...",
+ "....PPPPPPPPPP..",
+ "....PPPPPPPPPPP.",
+ "...PPPPPPPPPPPPP",
+ "...PPWPPPPPPPPPP",
+ "..PPPPWWPPPPPPP.",
+ "..PPPPPPWWWPPP..",
+ "..PPPPPPPPWWW...",
+ "...PPPPPPPPP....",
+ ".....WW..WW.....",
+ "....BBB..BB.....",
+ "...BBBB..BBB....",
+ "...BB.....BB....",
+], P_LOW))
+S[0x13] = ("paul_low", grid([
+ "....PPPPPPPPP...",
+ "...PPPPPPPPPPP..",
+ "...PPPPPPPPPPP..",
+ "..PPPPPPPPPPPPP.",
+ "..PPPWPPPPPWPPP.",
+ ".PPPPWWPPPWWPPPP",
+ ".PPPPPWWWWWPPPPP",
+ "PPPPPPPPPPPPPPPP",
+ ".PPPPPPPPPPPPPP.",
+ ".....WW..WW.....",
+ "....BBB..BBB....",
+ "...BBBB..BBBB...",
+ "...BB......BB...",
+], P_LOW))
+S[0x14] = ("paul_low", grid([
+ "................",
+ "................",
+ "..PPPPP.........",
+ ".PPPPPPPW.......",
+ ".PPPPPPPPW......",
+ "..PPPPPPPPWW....",
+ "...PPPPPPP.WW...",
+ "....PP..WW..WWB.",
+ "...WW....WW..BBB",
+ "..BB......WWBB..",
+ ".BBB.......BB...",
+], P_LOW))
+
+# barrels: rolling (turned a quarter at a time by flips), falling down a ladder (seen from the side),
+# standing (Kong's pile), and the blue one
+B = "OPL" # orange, peach, blue
+S[0x15] = ("barrel", grid([
+ "................",
+ "................",
+ ".....PPPPPP.....",
+ "...PPOOOOOOPP...",
+ "..POOOOOOOLLOP..",
+ "..POOOOOOLOLOP..",
+ ".POOOOOOLOOOOOP.",
+ ".POOOOOLOOOOOOP.",
+ ".POOOOLOOOOOOOP.",
+ "..POLLOOOOOOOP..",
+ "..POLLOOOOOOOP..",
+ "...PPOOOOOOPP...",
+ ".....PPPPPP.....",
+], B))
+SIDE = [
+ "................",
+ "................",
+ "..PPPPPPPPPPPP..",
+ ".PLLLLLLLLLLLLP.",
+ ".POOOOOOOOOOOOP.",
+ ".POOOOOOOOOOOOP.",
+ ".PLLLLLLLLLLLLP.",
+ ".POOOOOOOOOOOOP.",
+ ".POOOOOOOOOOOOP.",
+ ".PLLLLLLLLLLLLP.",
+ "..PPPPPPPPPPPP..",
+]
+S[0x16] = ("barrel", grid(SIDE, B))
+S[0x17] = ("barrel", grid([
+ "................",
+ "................",
+ "..PPPPPPPPPPPP..",
+ ".PLLLLLLLLLLLLP.",
+ ".POOOOOOOOOOOOP.",
+ ".POOOPOOOOPOOOP.",
+ ".PLLLLLLLLLLLLP.",
+ ".POOOOPOOOOPOOP.",
+ ".POOOOOOOOOOOOP.",
+ ".PLLLLLLLLLLLLP.",
+ "..PPPPPPPPPPPP..",
+], B))
+S[0x18] = ("barrel", grid([
+ "....PPPPPPPP....",
+ "...PLLLLLLLLP...",
+ "..POOOOOOOOOOP..",
+ "..POOOPPOOOOOP..",
+ "..POOOOOOOOOOP..",
+ "..PLLLLLLLLLLP..",
+ "..POOOOOOOOOOP..",
+ "..POOOOPPOOOOP..",
+ "..POOOOOOOOOOP..",
+ "..POOOOOOOOOOP..",
+ "..PLLLLLLLLLLP..",
+ "..POOOOOOOOOOP..",
+ "..POOOOOOPPOOP..",
+ "..POOOOOOOOOOP..",
+ "...PLLLLLLLLP...",
+ "....PPPPPPPP....",
+], B))
+BL = "LCW" # blue, cyan, white
+S[0x19] = ("blue", grid([
+ "................",
+ "................",
+ ".....CCCCCC.....",
+ "...CCLLLLLLCC...",
+ "..CLLLWWWWLLLC..",
+ "..CLLWLLLLWLLC..",
+ ".CLLWLLWWLLWLLC.",
+ ".CLLWLWLLWLWLLC.",
+ ".CLLWLLWWLWLLLC.",
+ "..CLLWLLLLWLLC..",
+ "..CLLLWWWWLLLC..",
+ "...CCLLLLLLCC...",
+ ".....CCCCCC.....",
+], BL))
+S[0x1A] = ("blue", grid([r.replace("P", "C").replace("O", "W").replace("L", "L") for r in SIDE], BL))
+S[0x1B] = ("blue", grid([
+ "................",
+ "................",
+ "..CCCCCCCCCCCC..",
+ ".CLLLLLLLLLLLLC.",
+ ".CWWWWWWWWWWWWC.",
+ ".CWWWLWWWWLWWWC.",
+ ".CLLLLLLLLLLLLC.",
+ ".CWWWWLWWWWLWWC.",
+ ".CWWWWWWWWWWWWC.",
+ ".CLLLLLLLLLLLLC.",
+ "..CCCCCCCCCCCC..",
+], BL))
+
+# the hammer: upright over Mario's head, and swung down in front of him (drawn for facing left)
+H = "YRW" # head, handle, shine
+S[0x1E] = ("hammer", grid([
+ "................",
+ "................",
+ "................",
+ "....YYYYYYYY....",
+ "...YWWYYYYYYY...",
+ "...YWYYYYYYYY...",
+ "...YYYYYYYYYY...",
+ "....YYYYYYYY....",
+ "......RRRR......",
+ "......RRRR......",
+ ".......RR.......",
+ ".......RR.......",
+ ".......RR.......",
+ ".......RR.......",
+ ".......RR.......",
+ ".......RR.......",
+], H))
+S[0x1F] = ("hammer", grid([
+ "................",
+ "................",
+ "................",
+ "................",
+ "...YYYY.........",
+ "..YWWYYY........",
+ "..YWYYYY........",
+ "..YYYYYYRR......",
+ "..YYYYYYRRRRRRRR",
+ "..YYYYYYRRRRRRRR",
+ "..YYYYYY........",
+ "..YYYYYY........",
+ "...YYYY.........",
+ "................",
+ "................",
+ "................",
+], H))
+
+# fires (0x3D/0x3E) and firefoxes on 100m (0x4D/0x4E): a living flame with eyes
+F = "YRW" # body, flame / eyes, white
+S[0x3D] = ("fire", grid([
+ "..........R.....",
+ ".......R..RR....",
+ "......RR.RRR....",
+ "......RRRRYR..R.",
+ ".....RRYYRYRRRR.",
+ "....RRYYYYYYYR..",
+ "...RRYYYYYYYYRR.",
+ "...RYYWWYYWWYYR.",
+ "..RRYYWRYYWRYYR.",
+ "..RYYYWRYYWRYYR.",
+ "..RYYYYYYYYYYYR.",
+ "..RYYYYYYYYYYYR.",
+ "..RRYYYYYYYYYRR.",
+ "...RRYYYYYYYRR..",
+ "....RRRYYYRRR...",
+ "......RRRRR.....",
+], F))
+S[0x3E] = ("fire", grid([
+ "................",
+ "...R.......R....",
+ "...RR..R..RR....",
+ "...RRR.RRRRR....",
+ "....RYRRYYRR.R..",
+ "....RRYYYYYRRR..",
+ "...RRYYYYYYYYRR.",
+ "...RYYWWYYWWYYR.",
+ "..RRYYWRYYWRYYR.",
+ "..RYYYWRYYWRYYR.",
+ "..RYYYYYYYYYYYR.",
+ "..RYYYYYYYYYYYR.",
+ "..RRYYYYYYYYYRR.",
+ "...RRYYYYYYYRR..",
+ "....RRRYYYRRR...",
+ "......RRRRR.....",
+], F))
+S[0x4D] = ("fire", grid([
+ ".......R........",
+ "......RR....R...",
+ "......RRR..RR...",
+ ".....RRYRRRR....",
+ "....RRYYYYYRR...",
+ "...RRYYYYYYYRR..",
+ "...RYYWWYYWWYR..",
+ "..RRYYWRYYWRYR..",
+ "..RYYYYYYYYYYRR.",
+ "..RYYYYRRYYYYYR.",
+ "..RYYYYYYYYYYYR.",
+ "..RRYYYYYYYYYRR.",
+ "...RRYYYYYYYRR..",
+ "...RR.RRRRR.RR..",
+ "..RR...R.R...RR.",
+ "..R....R.R....R.",
+], F))
+S[0x4E] = ("fire", grid([
+ "............R...",
+ "...R.......RR...",
+ "...RR..R..RRR...",
+ "....RRRRRRYR....",
+ "....RRYYYYYRR...",
+ "...RRYYYYYYYRR..",
+ "...RYYWWYYWWYR..",
+ "..RRYYWRYYWRYR..",
+ "..RYYYYYYYYYYRR.",
+ "..RYYYYRRYYYYYR.",
+ "..RYYYYYYYYYYYR.",
+ "..RRYYYYYYYYYRR.",
+ "...RRYYYYYYYRR..",
+ "....RRRRRRRRR...",
+ "...RR.R...R.RR..",
+ "...R..R...R..R..",
+], F))
+
+# the oil drum, its flames, the drum on 50m is the same
+DR = "LCW"
+S[0x49] = ("blue", grid([
+ "................",
+ "LLLLLLLLLLLLLLLL",
+ "LWLLLLLLLLLLLLLL",
+ "LWCCCCCCCCCCCCCL",
+ "LWLLLLLLLLLLLLLL",
+ "LWLCCCLCCLCLLLLL",
+ "LWLCLCLLCLLCLLLL",
+ "LWLCLCLLCLLCLLLL",
+ "LWLCCCLCCCLCCCLL",
+ "LWLLLLLLLLLLLLLL",
+ "LWCCCCCCCCCCCCCL",
+ "LWLLLLLLLLLLLLLL",
+ "LWLLWWLWWLWWLWWL",
+ "LWLLLLLLLLLLLLLL",
+ "LWLLLLLLLLLLLLLL",
+ "LLLLLLLLLLLLLLLL",
+], DR))
+FL = "RYW"
+flames = [
+ [
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "..........R.....",
+ "....R.....R.....",
+ "....R....RR.....",
+ "...RR..R.RRR..R.",
+ "...RRR.RRRYR..R.",
+ "..RRYRRRYRYRRRR.",
+ "..RYYRRYYYYRRYR.",
+ ".RRYYYRYYWYYRYR.",
+ ".RYYWYYYWWYYYYR.",
+ ".RYWWYYYWWWYWYR.",
+ ".RYWWWYYWWWYWWR.",
+ ],
+ [
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "......R.........",
+ "......R....R....",
+ ".R....RR...R....",
+ ".R...RRR..RR.R..",
+ ".RR..RYR..RRRR..",
+ ".RRRRRYRRRYRR...",
+ "..RYYRYYRYYYRR..",
+ ".RRYYYYYYYWYYR..",
+ ".RYYWYYYWWWYYRR.",
+ ".RYWWWYYWWWYWYR.",
+ ".RYWWWYWWWWYWWR.",
+ ],
+]
+S[0x40] = ("flame", grid(flames[0], FL))
+S[0x41] = ("flame", grid(flames[1], FL))
+big = [
+ [
+ "................",
+ "......R.....R...",
+ "..R...R....RR...",
+ "..R..RR.R..RR.R.",
+ "..RR.RRRR.RRR.R.",
+ ".RRR.RYRRRRYRRR.",
+ ".RYRRRYYRRYYRYR.",
+ ".RYYRYYYRYYYRYR.",
+ "RRYYYYWYYYWYYYRR",
+ "RYYWYYWYYYWWYYYR",
+ "RYYWWYYWYWWWYWYR",
+ "RYWWWYYWWWWWYWYR",
+ "RYWWWYWWWWWWYWWR",
+ "RYWWWWWWWWWWWWWR",
+ ".RYWWWWWWWWWWWR.",
+ "..RRYWWWWWWWYR..",
+ ],
+]
+S[0x42] = ("flame", grid(big[0], FL))
+S[0x43] = ("flame", mirror(grid(big[0], FL)))
+
+# springs on 75m: open and squashed
+SP = "LRW"
+S[0x3B] = ("spring", grid([
+ "................",
+ "...LLLLLLLLLL...",
+ "....LLLLLLLL....",
+ ".....R....R.....",
+ "......R..R......",
+ ".......RR.......",
+ "......R..R......",
+ ".....R....R.....",
+ "......R..R......",
+ ".......RR.......",
+ "......R..R......",
+ ".....R....R.....",
+ "....LLLLLLLL....",
+ "...LLLLLLLLLL...",
+ "................",
+ "................",
+], SP))
+S[0x3C] = ("spring", grid([
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "...LLLLLLLLLL...",
+ "....LLLLLLLL....",
+ "....RRRRRRRR....",
+ ".....R.RR.R.....",
+ "....RRRRRRRR....",
+ ".....R.RR.R.....",
+ "....RRRRRRRR....",
+ "....LLLLLLLL....",
+ "...LLLLLLLLLL...",
+ "................",
+ "................",
+], SP))
+
+# 75m: an elevator's platform (a short girder) and the shafts' motors
+EL = "ADT"
+S[0x44] = ("elevator", grid([
+ "................",
+ "................",
+ "................",
+ "................",
+ "AAAAAAAAAAAAAAAA",
+ "DDDDDDDDDDDDDDDD",
+ "...TT......TT...",
+ "..T..T....T..T..",
+ ".T....T..T....T.",
+ "T......TT......T",
+ "AAAAAAAAAAAAAAAA",
+ "DDDDDDDDDDDDDDDD",
+], EL))
+MO = "YRO"
+S[0x45] = ("motor", grid([
+ "................",
+ "..YYYYYYYYYYYY..",
+ ".YYYYYYYYYYYYYY.",
+ ".YRRRRRRRRRRRRY.",
+ ".YROOOOOOOOOORY.",
+ ".YROROROROROORY.",
+ ".YROOOOOOOOOORY.",
+ ".YRRRRRRRRRRRRY.",
+ ".YYYYYYYYYYYYYY.",
+ "...YYYYYYYYYY...",
+ ".....YYRRYY.....",
+ "......YRRY......",
+ ".......RR.......",
+ ".......RR.......",
+ ".......RR.......",
+ ".......RR.......",
+], MO))
+
+# 50m: the ladders that slide (a sprite there), the pies, the belts' pulleys
+S[0x46] = ("ladder_white", grid([
+ "...A........A...",
+ "...AAAAAAAAAA...",
+ "...A........A...",
+ "...A........A...",
+ "...A........A...",
+ "...AAAAAAAAAA...",
+ "...A........A...",
+ "...A........A...",
+ "...A........A...",
+ "...AAAAAAAAAA...",
+ "...A........A...",
+ "...A........A...",
+ "...A........A...",
+ "...AAAAAAAAAA...",
+ "...A........A...",
+ "...A........A...",
+], "A"))
+PI = "PBO"
+S[0x4B] = ("pie", grid([
+ "................",
+ "................",
+ "................",
+ "................",
+ "......PPPP......",
+ "....PPPOPPPP....",
+ "...PPOPPPPOPP...",
+ "..PPPPPPOPPPPP..",
+ ".PPOPPPPPPPOPPP.",
+ ".BBBBBBBBBBBBBB.",
+ "..BBBBBBBBBBBB..",
+ "................",
+], PI))
+S[0x4C] = ("pie", grid([
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "....PPPPPPPP....",
+ "..PPPOPPPPOPPP..",
+ ".PPPPPPOPPPPPPP.",
+ "PPOPPPPPPPPPOPPP",
+ "BBBBBBBBBBBBBBBB",
+ ".BBBBBBBBBBBBBB.",
+ "................",
+], PI))
+PU = "LRC"
+pulley = [
+ "................",
+ "....LLLLL.......",
+ "...LLLLLLL......",
+ "..LLLRRRLLL.....",
+ "..LLRRCCRLL.....",
+ ".LLRRCCRRRLL....",
+ ".LLRCCRRRRLL....",
+ ".LLRCRRRRRLL....",
+ "..LLRRRRRLL.....",
+ "...LLLLLLL......",
+ "....LLLLL.......",
+]
+S[0x50] = ("pulley", grid(pulley, PU))
+S[0x51] = ("pulley", grid([r.replace("C", "x").replace("R", "C").replace("x", "R") for r in pulley], PU))
+S[0x52] = ("pulley", vflip(grid(pulley, PU)))
+
+# a hammer's hit: a ring, a smaller ring, a burst
+SM = "LCW"
+S[0x60] = ("smash", grid([
+ "................",
+ ".....WWWWWW.....",
+ "...WWCCCCCCWW...",
+ "..WCC......CCW..",
+ "..WC........CW..",
+ ".WC..........CW.",
+ ".WC..........CW.",
+ ".WC..........CW.",
+ ".WC..........CW.",
+ ".WC..........CW.",
+ "..WC........CW..",
+ "..WCC......CCW..",
+ "...WWCCCCCCWW...",
+ ".....WWWWWW.....",
+], SM))
+S[0x61] = ("smash", grid([
+ "................",
+ "................",
+ "................",
+ "......WWWW......",
+ ".....WCCCCW.....",
+ "....WC....CW....",
+ "....WC....CW....",
+ "....WC....CW....",
+ "....WC....CW....",
+ ".....WCCCCW.....",
+ "......WWWW......",
+], SM))
+S[0x62] = ("smash", grid([
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "......WWWW......",
+ ".....WCCCCW.....",
+ ".....WC..CW.....",
+ ".....WCCCCW.....",
+ "......WWWW......",
+], SM))
+S[0x63] = ("smash", grid([
+ "................",
+ "...W.......W....",
+ "....W..W..W.....",
+ ".....W.W.W......",
+ "..W...CCC...W...",
+ "...W.CLLLC.W....",
+ ".....CLWLC......",
+ ".WWW.CLLLC.WWW..",
+ ".....CLWLC......",
+ "...W.CLLLC.W....",
+ "..W...CCC...W...",
+ ".....W.W.W......",
+ "....W..W..W.....",
+ "...W.......W....",
+], SM))
+
+# Pauline's things and the heart
+IT = "PWB"
+S[0x73] = ("items", grid([
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "......WWWW......",
+ ".....WWWWWW.....",
+ ".....WPPPPW.....",
+ "....WWWWWWWW....",
+ ".PPWWWWWWWWWWPP.",
+ "..PPPPPPPPPPPP..",
+ "................",
+ "................",
+ "................",
+ "................",
+], IT))
+S[0x74] = ("items", grid([
+ "................",
+ "................",
+ "................",
+ "................",
+ "......PPPP......",
+ ".....P....P.....",
+ ".....P....P.....",
+ "...PPPPPPPPPP...",
+ "..PPWWPPPPPPPP..",
+ "..PWPPPPPPPPPP..",
+ "..PPPPPWWPPPPP..",
+ "..PPPPPWWPPPPP..",
+ "..PPPPPPPPPPPP..",
+ "...PPPPPPPPPP...",
+ "................",
+ "................",
+], IT))
+S[0x75] = ("items", grid([
+ "................",
+ ".......B........",
+ ".....PPWPP......",
+ "...PPWWPPWWPP...",
+ "..PPWWPPPPWWPP..",
+ ".PPWWPPPPPPWWPP.",
+ ".WPPWPPWPPWPPWW.",
+ ".W..W..W..W...W.",
+ ".......W........",
+ ".......W........",
+ ".......W........",
+ ".......W........",
+ ".......W........",
+ ".......W..W.....",
+ "........WW......",
+ "................",
+], IT))
+HE = "OWP"
+heart = [
+ "................",
+ "................",
+ "..PPPP....PPPP..",
+ ".PPWWPP..PPPPPP.",
+ "PPWWPPPPPPPPPPPP",
+ "PPWPPPPPPPPPPPPP",
+ "PPPPPPPPPPPPPPPP",
+ "PPPPPPPPPPPPPPPP",
+ ".PPPPPPPPPPPPPP.",
+ "..PPPPPPPPPPPP..",
+ "...PPPPPPPPPP...",
+ "....PPPPPPPP....",
+ ".....PPPPPP.....",
+ "......PPPP......",
+ ".......PP.......",
+]
+S[0x76] = ("heart", grid(heart, HE))
+broken = [list(r) for r in grid(heart, HE)]
+for y in range(16): # a crack down the middle, the halves apart
+ for x in range(16):
+ if x in (7, 8):
+ broken[y][x] = 0
+ for x in range(7, 0, -1):
+ broken[y][x] = broken[y][x - 1]
+ broken[y][0] = 0
+ for x in range(8, 15):
+ broken[y][x] = broken[y][x + 1]
+ broken[y][15] = 0
+S[0x77] = ("heart", broken)
+
+# the points: 100, 200, 300, 500, 800 (in Kong's face colours, white digits)
+DIG = {
+ "0": ["###", "#.#", "#.#", "#.#", "###"],
+ "1": [".#.", "##.", ".#.", ".#.", "###"],
+ "2": ["###", "..#", "###", "#..", "###"],
+ "3": ["###", "..#", ".##", "..#", "###"],
+ "5": ["###", "#..", "###", "..#", "###"],
+ "8": ["###", "#.#", "###", "#.#", "###"],
+}
+for code, txt in ((0x7B, "100"), (0x7C, "200"), (0x7D, "300"), (0x7E, "500"), (0x7F, "800")):
+ g = [[0] * 16 for _ in range(16)]
+ for i, ch in enumerate(txt):
+ for y, row in enumerate(DIG[ch]):
+ for x, c in enumerate(row):
+ if c == "#":
+ g[5 + y][2 + i * 4 + x] = 3
+ S[code] = ("points", g)
+
+S[0x3A] = ("black", [[1] * 16 for _ in range(16)]) # a black square (hides the girders over Kong's ladders)
+S[0x39] = ("dizzy", grid([ # stars over Kong's head (100m)
+ "................",
+ "..W.........Y...",
+ ".WWW.......YYY..",
+ "..W.........Y...",
+ "................",
+ "......C.........",
+ ".....CCC........",
+ "......C......W..",
+ ".............W..",
+ "...Y.......WWWWW",
+ "..YYY........W..",
+ "...Y.........W..",
+], "WYC"))
+
+# ---------------------------------------------------------------- Kong, in parts placed by the game's pose tables
+K = "BPO" # body, peach, orange
+KF = "BPW" # body, peach, white (his face)
+# front view, left half (columns 0..19 of 40), assembled by mirroring for the right half
+KONG_PARTS = {
+ 0x23: ("kface", [ # the face (centre top)
+ ".....BBBBBB.....",
+ "...BBBBBBBBBB...",
+ "..BBBBBBBBBBBB..",
+ ".BBBPPPPPPPPBBB.",
+ ".BBPPPPPPPPPPBB.",
+ ".BPPWWPPPPWWPPB.",
+ ".BPPBWPPPPWBPPB.",
+ ".BPPPPPPPPPPPPB.",
+ "BBBPPPPBBPPPPBBB",
+ "BBPPPPPPPPPPPPBB",
+ "BBPBBBBBBBBBBPBB",
+ "BBPPPPPPPPPPPPBB",
+ "BBBPPPPPPPPPPBBB",
+ ".BBBPPPPPPPPBBB.",
+ "..BBBBBBBBBBBB..",
+ "....BBBBBBBB....",
+ ], KF),
+ 0x24: ("kface", [ # growling, teeth out
+ ".....BBBBBB.....",
+ "...BBBBBBBBBB...",
+ "..BBBBBBBBBBBB..",
+ ".BBBPPPPPPPPBBB.",
+ ".BBPBBPPPPBBPBB.",
+ ".BPPWWBPPBWWPPB.",
+ ".BPPBWPPPPWBPPB.",
+ ".BPPPPPPPPPPPPB.",
+ "BBBPPPPBBPPPPBBB",
+ "BBPBBBBBBBBBBPBB",
+ "BBPBWWWWWWWWBPBB",
+ "BBPBBBBBBBBBBPBB",
+ "BBPBWWWWWWWWBPBB",
+ ".BBBBBBBBBBBBBB.",
+ "..BBBBBBBBBBBB..",
+ "....BBBBBBBB....",
+ ], KF),
+ 0x25: ("kong", [ # chest and hips (centre bottom)
+ "...BBPPPPPPBB...",
+ "..BBPPPPPPPPBB..",
+ "..BPPPPPPPPPPB..",
+ "..BPPPPOOPPPPB..",
+ "..BPPPPPPPPPPB..",
+ "..BBPPPPPPPPBB..",
+ "...BBPPPPPPBB...",
+ "...BBBPPPPBBB...",
+ "...BBBBPPBBBB...",
+ "...BBBBBBBBBB...",
+ "...BBBBBBBBBB...",
+ "....BBB..BBB....",
+ "....BB....BB....",
+ "................",
+ "................",
+ "................",
+ ], K),
+ 0x27: ("kong", [ # his right leg, standing (right of centre)
+ "................",
+ "................",
+ "....BBBBBB......",
+ "...BBBBBBBBB....",
+ "...BBBBBBBBBB...",
+ "...BBBBBBBBBBB..",
+ "....BBBBBBBBBB..",
+ ".....BBBBBBBBB..",
+ "......BBBBBBBB..",
+ ".......BBBBBBB..",
+ "........BBBBBB..",
+ "........BBBBBB..",
+ "........OBBBBO..",
+ ".......PPPPPPPP.",
+ "......PPPPPPPPPP",
+ "......PPP.PPP.PP",
+ ], K),
+ 0x26: ("kong", [ # his left leg, lifted (left of centre)
+ "................",
+ "................",
+ "................",
+ "................",
+ ".......BBBBBB...",
+ ".....BBBBBBBBB..",
+ "...BBBBBBBBBBB..",
+ "..BBBBBBBBBBBB..",
+ ".BBBBBBBBBBBB...",
+ ".BBBBBBBBB......",
+ ".BBBBBB.........",
+ ".OBBBBO.........",
+ "PPPPPPPP........",
+ "PPPPPPPPP.......",
+ "P.PPP.PPP.......",
+ "................",
+ ], K),
+ 0x29: ("kong", [ # his left arm, hanging (left)
+ "........BBB.....",
+ "......BBBBBBB...",
+ ".....BBBBBBBBB..",
+ "....BBBBBBBBBB..",
+ "...BBBBBBBBBBB..",
+ "...BBBBOBBBBB...",
+ "..BBBBBOOBBB....",
+ "..BBBBBBOBB.....",
+ "..BBBBBBBB......",
+ "..BBBBBBBB......",
+ "..BBBBBBB.......",
+ "..BBBBBBB.......",
+ "..OBBBBBO.......",
+ ".PPPPPPPP.......",
+ ".PPPPPPPPP......",
+ ".PPP.PP.PP......",
+ ], K),
+ 0x28: ("kong", [ # his right arm, raised to beat his chest (right)
+ "......PPPP......",
+ ".....PPPPPP.....",
+ ".....PPPPPP.....",
+ "......OBBO......",
+ "......BBBB......",
+ ".....BBBBBB.....",
+ "....BBBBBBB.....",
+ "...BBBBOBBB.....",
+ "..BBBBOBBBB.....",
+ ".BBBBOBBBB......",
+ "BBBBBBBBB.......",
+ "BBBBBBBB........",
+ "BBBBBBB.........",
+ "BBBBBB..........",
+ ".BBB............",
+ "................",
+ ], K),
+ # holding a barrel in front (0x395A): upper and lower arm, drawn for his right side
+ 0x2A: ("kong", [
+ "................",
+ "................",
+ "................",
+ "................",
+ "..BBBBB.........",
+ ".BBBBBBBB.......",
+ "BBBBBBBBBB......",
+ "BBBBBBBBBBB.....",
+ "BBBBBBBBBBBB....",
+ ".BBBBBBBBBBBB...",
+ "..BBBBBBBBBBB...",
+ "....BBBBBBBBB...",
+ "......BBBBBBB...",
+ ".......BBBBBB...",
+ ".......BBBBBB...",
+ ".......BBBBB....",
+ ], K),
+ 0x2B: ("kong", [
+ ".......BBBBB....",
+ "......BBBBB.....",
+ ".....BBBBB......",
+ "....OBBBO.......",
+ "..PPPPPP........",
+ ".PPPPPPP........",
+ ".PPPPPPP........",
+ "..PPPPP.........",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ ], K),
+}
+# side view, facing left, taking a barrel from the pile (0x3982): a 3x2 block of parts
+SIDE_ROWS = [
+ ".....BBBBBB..... ..BBBBBBBBBBB... ................",
+ "...BBBBBBBBBB... .BBBBBBBBBBBBBB. ................",
+ "..BBBBBBBBBBBB.. BBBBBBBBBBBBBBBB B...............",
+ ".BBBBBPPPPPBBBB. BBBBBBBBBBBBBBBB BB..............",
+ ".BBBBPPPPPPPBBB. BBBBBBBBBBBBBBBB BBB.............",
+ "BBBBPPWWPPPPPBBB BBBBBBBBBBBBBBBB BBBB............",
+ "BBBPPPBWPPPPPBBB BBBBBBBBBBBBBBBB BBBBB...........",
+ "PPPPPPPPPPPPPBBB BBBBBBBBBBBBBBBB BBBBB...........",
+ "PPPPPPPPPPPPPBBB BBBBBBBBBBBBBBBB BBBBBB..........",
+ "PPPPPPPPPPPPBBBB BBBBBBBBBBBBBBBB BBBBBB..........",
+ "PBBBBBBBBBPPBBBB BBBBBBBBBBBBBBBB BBBBBBB.........",
+ "PPPPPPPPPPPPBBBB BBBBBBBBBBBBBBBB BBBBBBB.........",
+ ".PPPPPPPPPPBBBBB BBBBBBBBBBBBBBBB BBBBBBBB........",
+ "..BBBBBBBBBBBBBB BBBBBBBBBBBBBBBB BBBBBBBB........",
+ "...BBBBBBBBBBBBB BBBBBBBBBBBBBBBB BBBBBBBBB.......",
+ ".....BBBBBBBBBBB BBBBBBBBBBBBBBBB BBBBBBBBB.......",
+ "........BBBBBBBB BBBBBBBBBBBBBBBB BBBBBBBBBB......",
+ ".......BBBBBBBBB BBBBBBBBBBBBBBBB BBBBBBBBBB......",
+ "......BBBBBBBBBB BBBBBBBBBBBBBBBB BBBBBBBBBBB.....",
+ ".....BBBBBBBBBBB BBBBBBBBBBBBBBBB BBBBBBBBBBB.....",
+ "....BBBBBBBBBB.B BBBBBBBBBBBBBBBB BBBBBBBBBBB.....",
+ "...BBBBBBBBBB..B BBBBBBBBBBBBBBBB BBBBBBBBBBB.....",
+ "..BBBBBBBBBB...B BBBBBBBBBBBBBBBB BBBBBBBBBBB.....",
+ ".BBBBBBBBBB....B BBBBBBBBBBBBBBBB BBBBBBBBBBBB....",
+ ".BBBBBBBBB.....B BBBBBBBBBBBBBBBB BBBBBBBBBBBB....",
+ "BBBBBBBBB......B BBBBBBBBBBBBBBBB BBBBBBBBBBBB....",
+ "OBBBBBBO.......B BBBBBBBBBBBBBBB. BBBBBBBBBBBB....",
+ "PPPPPPPP.......B BBBBBBBBBBBBBB.. .BBBBBBBBBBO....",
+ "PPPPPPPPP....... .BBBBBBBBBBBB... .PPPPPPPPPPPP...",
+ "PP.PPP.PP....... ..BBBBBBBBBB.... .PPPPPPPPPPPPP..",
+ "................ ................ PPP.PPP.PP.PPP..",
+ "................ ................ ................",
+]
+side = [r.split(" ") for r in SIDE_ROWS]
+SIDE_CODES = {0x30: (0, 0, "kface"), 0x2F: (1, 0, "kong"), 0x2D: (0, 1, "kong"), 0x2E: (1, 1, "kong"),
+ 0x2C: (2, 1, "kong")}
+# climbing his ladders, seen from behind (0x388C)
+CLIMB = {
+ 0x32: [ # his back, left
+ "................",
+ "................",
+ "................",
+ "................",
+ "........BBBBBBBB",
+ "......BBBBBBBBBB",
+ ".....BBBBBBBBBBB",
+ "....BBBBBBBBBBBB",
+ "....BBBBBBBBBBBB",
+ "...BBBBBBBBBBBBB",
+ "...BBBBBBBBBBBBB",
+ "...BBBBBBBBBBBBB",
+ "...BBBBBBBBBBBBB",
+ "....BBBBBBBBBBBB",
+ "....BBBBBBBBBBBB",
+ ".....BBBBBBBBBBB",
+ ],
+ 0x34: [ # left arm up, reaching for a rung
+ ".PPPPPP.........",
+ ".PPPPPPP........",
+ "..PPPPPP........",
+ "...OBBO.........",
+ "...BBBB.........",
+ "...BBBBB........",
+ "...BBBBBB.......",
+ "....BBBBBB......",
+ ".....BBBBBB.....",
+ "......BBBBBB....",
+ ".......BBBBBB...",
+ "........BBBBB...",
+ "................",
+ "................",
+ "................",
+ "................",
+ ],
+ 0x35: [ # right arm down, holding on
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "BBBB............",
+ "BBBBBB..........",
+ ".BBBBBB.........",
+ "..BBBBBB........",
+ "...BBBBBO.......",
+ "....BBPPPP......",
+ ".....PPPPPP.....",
+ ".....PPPPPP.....",
+ "......PPPP......",
+ "................",
+ ],
+ 0x36: [ # left foot, on a rung
+ "....BBBBBBBBBBBB",
+ "....BBBBBBBBBBBB",
+ ".....BBBBBBBBBBB",
+ ".....BBBBBB.BBBB",
+ ".....BBBBBB.....",
+ ".....BBBBBB.....",
+ ".....BBBBBB.....",
+ ".....BBBBBB.....",
+ ".....BBBBBB.....",
+ ".....OBBBBO.....",
+ "....PPPPPPPP....",
+ "....PPPPPPPP....",
+ "................",
+ "................",
+ "................",
+ "................",
+ ],
+ 0x37: [ # right foot, stepping up
+ "BBBBBBBBBBBB....",
+ "BBBBBBBBBBBB....",
+ "BBBBBBBBBBB.....",
+ "BBBBBBBBBBB.....",
+ "BBBBBBBBBB......",
+ "..BBBBBBB.......",
+ "..OBBBBO........",
+ ".PPPPPPPP.......",
+ ".PPPPPPPP.......",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ "................",
+ ],
+ 0x31: [ # his right arm round Pauline
+ "................",
+ "................",
+ "................",
+ "BBBBB...........",
+ "BBBBBBB.........",
+ ".BBBBBBBB.......",
+ "...BBBBBBB......",
+ ".....BBBBBO.....",
+ "......PPPPPP....",
+ ".....PPPPPPPP...",
+ ".....PPPPPPPP...",
+ "......PPPPPP....",
+ "................",
+ "................",
+ "................",
+ "................",
+ ],
+}
+for code, (pal, rows, keys) in {c: (v[0], v[1], v[2]) for c, v in KONG_PARTS.items()}.items():
+ S[code] = (pal, grid(rows, keys))
+for code, (cx, cy, pal) in SIDE_CODES.items():
+ rows = [side[cy * 16 + y][cx] for y in range(16)]
+ S[code] = (pal, grid(rows, KF if pal == "kface" else K))
+for code, rows in CLIMB.items():
+ S[code] = ("kong", grid(rows, K))
+S[0x33] = ("kong", mirror(grid(CLIMB[0x32], K)))
+# dizzy faces after his fall (shown upside down by the game)
+for i, code in enumerate((0x20, 0x21, 0x22)):
+ face = [list(r) for r in grid(KONG_PARTS[0x24][1], KF)]
+ eyes = [(5, 4), (5, 10)]
+ spin = [[(0, 0), (1, 1)], [(0, 1), (1, 0)], [(0, 0), (1, 0)]][i]
+ for (ey, ex) in eyes:
+ for yy in range(2):
+ for xx in range(2):
+ face[ey + yy][ex + xx] = 2
+ for (dy, dx) in spin:
+ face[ey + dy][ex + dx] = 3
+ S[code] = ("kface", face)
+
+# ---------------------------------------------------------------- tiles (8x8, 2 bits): drawn by the renderer
+TILES = {
+ "girder": [ # 1 rail, 2 its edge, 3 the truss (25m, 75m, 50m)
+ "11111111",
+ "22222222",
+ "...33...",
+ "..3..3..",
+ ".3....3.",
+ "33333333",
+ "11111111",
+ "22222222",
+ ],
+ "rivet_girder": [ # 100m: 1 the top edge, 2 the beam, 3 its holes
+ "11111111",
+ "22222222",
+ "23322332",
+ "33333333",
+ "33333333",
+ "23322332",
+ "22222222",
+ "22222222",
+ ],
+ "belt": [ # 50m: 1 orange, 2 peach
+ "11111111",
+ "22222222",
+ "2....2..",
+ "2....2..",
+ "2....2..",
+ "2....2..",
+ "22222222",
+ "11111111",
+ ],
+ "ladder": [
+ "1......1",
+ "11111111",
+ "1......1",
+ "1......1",
+ "1......1",
+ "11111111",
+ "1......1",
+ "1......1",
+ ],
+ "rivet_top": [ # B8: the rivet's head over the girder
+ "........",
+ "........",
+ "........",
+ "........",
+ "........",
+ "..1111..",
+ ".111111.",
+ ".111111.",
+ ],
+ "rivet_low": [ # B7: its foot in the girder
+ "33111133",
+ "33111133",
+ "23111132",
+ "33111133",
+ "33111133",
+ "23111132",
+ "22111122",
+ "22111122",
+ ],
+ "cross": [ # FE: the lattice under the 50m drum
+ "1......1",
+ ".1....1.",
+ "..1..1..",
+ "...11...",
+ "...11...",
+ "..1..1..",
+ ".1....1.",
+ "1......1",
+ ],
+}
+
+# letters: Bobydeluxe's 5x7 font from his adaptation (columns, bit 0 at the top), drawn bold
+FONT5 = [
+ 0x7e, 0x11, 0x11, 0x11, 0x7e, 0x7f, 0x49, 0x49, 0x49, 0x36, 0x3e, 0x41, 0x41, 0x41, 0x22, 0x7f, 0x41, 0x41, 0x22, 0x1c,
+ 0x7f, 0x49, 0x49, 0x49, 0x41, 0x7f, 0x09, 0x09, 0x09, 0x01, 0x3e, 0x41, 0x49, 0x49, 0x7a, 0x7f, 0x08, 0x08, 0x08, 0x7f,
+ 0x00, 0x41, 0x7f, 0x41, 0x00, 0x20, 0x40, 0x41, 0x3f, 0x01, 0x7f, 0x08, 0x14, 0x22, 0x41, 0x7f, 0x40, 0x40, 0x40, 0x40,
+ 0x7f, 0x02, 0x0c, 0x02, 0x7f, 0x7f, 0x04, 0x08, 0x10, 0x7f, 0x3e, 0x41, 0x41, 0x41, 0x3e, 0x7f, 0x09, 0x09, 0x09, 0x06,
+ 0x3e, 0x41, 0x51, 0x21, 0x5e, 0x7f, 0x09, 0x19, 0x29, 0x46, 0x46, 0x49, 0x49, 0x49, 0x31, 0x01, 0x01, 0x7f, 0x01, 0x01,
+ 0x3f, 0x40, 0x40, 0x40, 0x3f, 0x1f, 0x20, 0x40, 0x20, 0x1f, 0x3f, 0x40, 0x38, 0x40, 0x3f, 0x63, 0x14, 0x08, 0x14, 0x63,
+ 0x07, 0x08, 0x70, 0x08, 0x07, 0x61, 0x51, 0x49, 0x45, 0x43,
+]
+DIGITS5 = [0x3e, 0x51, 0x49, 0x45, 0x3e, 0x00, 0x42, 0x7f, 0x40, 0x00, 0x62, 0x51, 0x49, 0x49, 0x46, 0x22, 0x41, 0x49,
+ 0x49, 0x36, 0x18, 0x14, 0x12, 0x7f, 0x10, 0x27, 0x45, 0x45, 0x45, 0x39, 0x3c, 0x4a, 0x49, 0x49, 0x30, 0x01,
+ 0x71, 0x09, 0x05, 0x03, 0x36, 0x49, 0x49, 0x49, 0x36, 0x06, 0x49, 0x49, 0x29, 0x1e]
+EXTRA5 = {".": [0, 0x60, 0x60, 0, 0], "-": [0, 0x08, 0x08, 0x08, 0], ":": [0, 0x36, 0x36, 0, 0],
+ "=": [0, 0x14, 0x14, 0x14, 0], "!": [0, 0, 0x5f, 0, 0], "?": [0x02, 0x01, 0x51, 0x09, 0x06],
+ "'": [0, 0, 0x03, 0, 0], ",": [0, 0x80, 0x60, 0, 0], "<": [0x08, 0x14, 0x22, 0x41, 0],
+ ">": [0x41, 0x22, 0x14, 0x08, 0], "/": [0x20, 0x10, 0x08, 0x04, 0x02], "m": [0x7c, 0x04, 0x78, 0x04, 0x78],
+ "(": [0, 0x1c, 0x22, 0x41, 0], ")": [0, 0x41, 0x22, 0x1c, 0]}
+
+
+def glyph(ch):
+ if "A" <= ch <= "Z":
+ return FONT5[(ord(ch) - 65) * 5:(ord(ch) - 65) * 5 + 5]
+ if "0" <= ch <= "9":
+ return DIGITS5[(ord(ch) - 48) * 5:(ord(ch) - 48) * 5 + 5]
+ return EXTRA5.get(ch, [0] * 5)
+
+
+def bold_rows(ch):
+ """8 rows of 8 bits: the 5x7 glyph, each column doubled to the right (6 wide), one pixel in."""
+ cols = glyph(ch)
+ rows = []
+ for y in range(8):
+ bits = 0
+ for x, c in enumerate(cols):
+ if c >> y & 1:
+ bits |= 0x80 >> (x + 1) | 0x80 >> (x + 2)
+ rows.append(bits)
+ return rows
+
+
+FONT_CHARS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ.-:=!?',<>/m()"
+
+# the arcade's tile codes for the letters it prints in the play area
+TILE_CHAR = {}
+for i in range(10):
+ TILE_CHAR[i] = str(i)
+ TILE_CHAR[0x70 + i] = str(i)
+ TILE_CHAR[0x80 + i] = str(i)
+for i in range(26):
+ TILE_CHAR[0x11 + i] = chr(65 + i)
+TILE_CHAR.update({0x2B: ".", 0x2C: "-", 0x2E: ":", 0x30: "<", 0x31: ">", 0x34: "=", 0x38: "!", 0x39: "!",
+ 0x3A: "'", 0x42: ".", 0x43: ",", 0x8A: "m", 0x8B: "m", 0xFB: "?"})
+
+# HELP!, three tiles (0xED-0xEF facing right, 0xDD-0xDF facing left): small italic capitals
+HELP = [
+ "...........................",
+ ".#..#.####.#....###...#.....",
+ ".#..#.#....#....#..#..#.....",
+ "#..#.###..#....###...#......",
+ "####.#....#....#.....#......",
+ "#..#.####.####.#.....#......",
+ "...........................",
+ "....................#.......",
+]
+
+
+def help_tiles():
+ rows = [r.ljust(24, ".")[:24] for r in HELP]
+ out = []
+ for t in range(3):
+ out.append([sum(0x80 >> x for x in range(8) if rows[y][t * 8 + x] == "#") for y in range(8)])
+ return out
+
+
+# the little Mario for each life left (tile 0xFF, shown in the side panel)
+LIFE = [
+ "..RRR...",
+ ".RRRRR..",
+ ".BPPB...",
+ ".PPPPP..",
+ "..BRB...",
+ ".BBRBB..",
+ "..R.R...",
+ ".BB.BB..",
+]
+
+# ---------------------------------------------------------------- output
+
+
+def pack2(g, w):
+ out = []
+ for row in g:
+ for x in range(0, w, 4):
+ b = 0
+ for k in range(4):
+ b |= (row[x + k] & 3) << (6 - 2 * k)
+ out.append(b)
+ return out
+
+
+def rgb565(c):
+ r, g, b = c >> 16 & 255, c >> 8 & 255, c & 255
+ return (r >> 3) << 11 | (g >> 2) << 5 | b >> 3
+
+
+def goofy():
+ """The 48x32 Kong of 'HOW HIGH CAN YOU GET?', assembled from the standing pose."""
+ canvas = [[0] * 48 for _ in range(32)]
+ pose = [(0x47, 0x27, 0, 0x50), (0x2F, 0x27, 1, 0x50), (0x3B, 0x25, 0, 0x50), (0x3B, 0x23, 0, 0x40),
+ (0x46, 0x29, 1, 0x44), (0x30, 0x29, 0, 0x44)]
+ face_map = {1: 1, 2: 2, 3: 3}
+ for (x, code, flip, y) in pose:
+ pal, g = S[code]
+ if flip:
+ g = mirror(g)
+ for yy in range(16):
+ for xx in range(16):
+ v = g[yy][xx]
+ if not v:
+ continue
+ if pal == "kface" and v == 3:
+ v = 3 # white eyes stay white (3 in the goofy palette)
+ elif pal == "kong" and v == 3:
+ v = 1 # orange shading folds into the body colour
+ cx, cy = x - 7 + xx - 0x28 + 4, y - 8 + yy - 0x38
+ if 0 <= cx < 48 and 0 <= cy < 32:
+ canvas[cy][cx] = face_map[v]
+ return canvas
+
+
+def main():
+ out = ["/* Generated by tools/art.py: Donkey Kong's pictures for this port (no Nintendo graphics data). */",
+ "#ifndef DK_ART_H", "#define DK_ART_H", "#include <stdint.h>"]
+ names = list(PAL)
+ out.append("enum { " + ", ".join(f"PAL_{n.upper()}" for n in names) + ", PAL_COUNT };")
+ out.append("static const uint16_t dk_pal[PAL_COUNT][3] = {")
+ for n in names:
+ out.append(" {" + ", ".join(f"0x{rgb565(c):04X}" for c in PAL[n]) + "},")
+ out.append("};")
+ codes = sorted(S)
+ index = [255] * 128
+ for i, c in enumerate(codes):
+ index[c] = i
+ out.append(f"static const uint8_t dk_spr[{len(codes)}][64] = {{")
+ for c in codes:
+ out.append(" {" + ",".join(f"0x{b:02X}" for b in pack2(S[c][1], 16)) + f"}}, /* {c:02X} */")
+ out.append("};")
+ out.append(f"static const uint8_t dk_spr_pal[{len(codes)}] = {{" +
+ ",".join(f"PAL_{S[c][0].upper()}" for c in codes) + "};")
+ out.append("static const uint8_t dk_spr_of[128] = {" + ",".join(str(v) for v in index) + "};")
+ for name, rows in TILES.items():
+ g = [[0 if ch == "." else int(ch) for ch in r] for r in rows]
+ out.append(f"static const uint8_t dk_tile_{name}[16] = {{" + ",".join(f"0x{b:02X}" for b in pack2(g, 8)) + "};")
+ out.append(f"static const char dk_font_chars[] = \"{FONT_CHARS}\";")
+ out.append(f"static const uint8_t dk_font[{len(FONT_CHARS)}][8] = {{")
+ for ch in FONT_CHARS:
+ out.append(" {" + ",".join(f"0x{b:02X}" for b in bold_rows(ch)) + "},")
+ out.append("};")
+ tc = [0] * 256
+ for code, ch in TILE_CHAR.items():
+ tc[code] = FONT_CHARS.index(ch) + 1
+ out.append("static const uint8_t dk_tile_char[256] = {" + ",".join(str(v) for v in tc) + "};")
+ out.append("static const uint8_t dk_help[3][8] = {" + ",".join(
+ "{" + ",".join(f"0x{b:02X}" for b in t) + "}" for t in help_tiles()) + "};")
+ lg = [[0 if ch == "." else "RBP".index(ch) + 1 for ch in r] for r in LIFE]
+ out.append("static const uint8_t dk_life[16] = {" + ",".join(f"0x{b:02X}" for b in pack2(lg, 8)) + "};")
+ out.append("static const uint8_t dk_goofy[384] = {" + ",".join(f"0x{b:02X}" for b in pack2(goofy(), 48)) + "};")
+ out.append("#endif")
+ open(sys.argv[1], "w").write("\n".join(out) + "\n")
+ if len(sys.argv) > 2:
+ preview(sys.argv[2])
+
+
+def preview(path):
+ from PIL import Image
+ codes = sorted(S)
+ cols = 12
+ im = Image.new("RGB", (cols * 18 * 4, ((len(codes) + cols - 1) // cols) * 18 * 4), (40, 40, 40))
+ for i, c in enumerate(codes):
+ pal, g = S[c]
+ ox, oy = (i % cols) * 18 * 4, (i // cols) * 18 * 4
+ for y in range(16):
+ for x in range(16):
+ v = g[y][x]
+ col = (0, 0, 0) if not v else tuple(PAL[pal][v - 1].to_bytes(3, "big"))
+ for dy in range(4):
+ for dx in range(4):
+ im.putpixel((ox + x * 4 + dx, oy + y * 4 + dy), col)
+ im.save(path)
+
+
+if __name__ == "__main__":
+ main()
diff --git a/games/donkeykong/tools/assets.py b/games/donkeykong/tools/assets.py
deleted file mode 100644
index b386a830..00000000
--- a/games/donkeykong/tools/assets.py
+++ /dev/null
@@ -1,98 +0,0 @@
-#!/usr/bin/env python3
-"""Hand-drawn pixel art. Pack two palette indices per byte; no runtime decoding buffer.
-Also makes the launcher icon with only the Python standard library.
-These are original drawings, not extracted Nintendo sprites.
-"""
-from pathlib import Path
-import struct
-import zlib
-
-ROOT = Path(__file__).resolve().parents[1]
-COLORS = [0x070B16, 0xFF5545, 0xFFD6A0, 0x407EDB, 0xFFE16B, 0x663520,
- 0xAD6134, 0xF5AE66, 0xFFF0CD, 0xF566AF, 0x77DAEA, 0xAE7DFF,
- 0xE77F37, 0x2A344A, 0xB9C5D9, 0xFFFFFF]
-ART = {
-"mario": [
-"....11111....", "...11111111..", "...555225....", "..525522252..",
-"..5255222252.", "..552222555..", "....222222...", "...11311.....",
-"..111311311..", ".11113333111.", ".22134343122.", ".22233333222.",
-"...3333333...", "...333.333...", "..555...555..", ".5555...5555."],
-"kong": [
-".........5555555555.........", ".......5566666666555.......",
-"......566666666666665......", ".....56667777777766665.....",
-"....5666777777777766665....", "...566677555775557766665...",
-"...566677588775887766665...", "..56666775577775577666665..",
-"..56666677777777776666665..", ".5666666777555577766666665.",
-".5666666777888877766666665.", "56666666677777776666666665",
-"56666666667777766666666665", "56666566777777776656666665",
-"56665567777777777655666665", "56665567777777777655666665",
-"56665567777777777655666665", "57775567777777777655777765",
-"57775567777777777655777765", "57775566777777776655777765",
-".555556667777776665555555.", ".....566666666666665......",
-".....566666..6666665......", "....5777775..57777775.....",
-"...57777775..577777775...."],
-"pauline": [
-"...4444...", "..455555..", "..522525..", "..522225..", "..552255..",
-"...9229...", "..299992..", ".22999922.", ".2.9999.2.", "...9999...",
-"..999999..", "..999999..", ".99999999.", ".99999999.", "....22....",
-"...22.2...", "...99.99..", "..999.999."],
-"barrel": [
-"..CCCCCC..", ".C477774C.", "C74777474C", "C77474774C", "C77747774C",
-"C77474774C", "C74777474C", ".C477774C.", "..CCCCCC.."],
-"fire": [
-"....1.....", "...11..1..", "...C1.C1..", ".1.CCCC1..", ".1CCCCC1..",
-".CCCCCCC1.", "CCCCCCCCC1", "CCC4CC4CC1", "CCC8CC8CC1", ".CC444CC1.",
-"..CC4CC1..", "..C...C..."],
-"tub": [".EEEEEEEE.", "EDDDDDDDDE", "ECCCCCCCCE", ".ECCCCCCE.", ".ECCCCCCE.", "..EEEEEE..", "..D....D.."],
-"hammer": [".EEEEEEE.", "EEEEEEEE.", "EEEEEEEE.", ".EEEEEEE.", "....77...", "....77...", "....77...", "....77...", "....77..."],
-"bag": ["...444...", "..4...4..", ".9999999.", "999999999", "999494999", "999494999", "999999999", ".9999999."],
-"hat": ["..99999..", "..99999..", ".9999999.", "444444444", "999999999"],
-"parasol": ["....B....", "..BBBBB..", ".BB9B9BB.", "BB9BB9BBB", "....4....", "....4....", "....4....", "..4.4....", "...4....."],
-}
-
-def rows(name):
- art = ART[name]
- width = max(map(len, art))
- return [row.ljust(width, '.') for row in art]
-
-def main():
- result = ['/* SPDX-License-Identifier: GPL-3.0-only; generated by tools/assets.py */',
- 'typedef struct {uint8_t w,h;const uint8_t *p;} dk_sprite;']
- result.append('static const uint16_t palette[]={' + ','.join(
- str(((c >> 8) & 0xF800) | ((c >> 5) & 0x7E0) | ((c >> 3) & 31)) for c in COLORS) + '};')
- for name in ART:
- art = rows(name)
- values = [0 if ch == '.' else int(ch, 16) for row in art for ch in row]
- if len(values) % 2:
- values.append(0)
- data = [values[i] << 4 | values[i+1] for i in range(0, len(values), 2)]
- result.append('static const uint8_t pixels_' + name + '[]={' + ','.join(map(str, data)) + '};')
- result.append(f'static const dk_sprite spr_{name}={{{len(art[0])},{len(art)},pixels_{name}}};')
- (ROOT/'src/assets.h').write_text('\n'.join(result)+'\n')
- # A tiny arcade cabinet icon: ape, red girders and blue ladders.
- pixels = [[COLORS[0]]*55 for _ in range(56)]
- for y in (6, 38, 50):
- for x in range(3, 52):
- pixels[y][x] = COLORS[1]
- pixels[y+3][x] = COLORS[9]
- if x%6 < 2:
- pixels[y+1][x] = pixels[y+2][x] = COLORS[9]
- for y in range(41, 50):
- for x in (9, 15, 39, 45):
- pixels[y][x] = COLORS[10]
- if y%3 == 0:
- for x in list(range(9, 16))+list(range(39, 46)):
- pixels[y][x] = COLORS[10]
- for y, row in enumerate(rows('kong')):
- for x, ch in enumerate(row):
- if ch != '.':
- pixels[y+11][x+14] = COLORS[int(ch,16)]
- raw = b''.join(b'\0'+b''.join(bytes((c >> 16, c >> 8 & 255, c & 255)) for c in row) for row in pixels)
- def chunk(kind, data):
- return struct.pack('>I',len(data))+kind+data+struct.pack('>I',zlib.crc32(kind+data))
- png = b'\x89PNG\r\n\x1a\n'+chunk(b'IHDR',struct.pack('>IIBBBBB',55,56,8,2,0,0,0))
- png += chunk(b'IDAT',zlib.compress(raw,9))+chunk(b'IEND',b'')
- (ROOT/'src/icon.png').write_bytes(png)
-
-if __name__ == '__main__':
- main()
diff --git a/games/donkeykong/tools/compact_nwa.py b/games/donkeykong/tools/compact_nwa.py
deleted file mode 100644
index f320bb54..00000000
--- a/games/donkeykong/tools/compact_nwa.py
+++ /dev/null
@@ -1,228 +0,0 @@
-#!/usr/bin/env python3
-"""Lossless standalone NWA packing. Python standard library + ARM binutils.
-
-Keep the standard ELF/NWI formats: no runtime decoder, extra RAM, or archive.
-"""
-import argparse
-from bisect import bisect_left, insort
-import json
-from pathlib import Path
-import subprocess
-import tempfile
-
-ICON_BYTES = 55 * 56 * 2
-
-
-def decode_lz4(data, expected_size):
- """Decode a raw LZ4 block, with an exact output bound for launcher icons."""
- out = bytearray()
- pos = 0
-
- def length(value):
- nonlocal pos
- if value == 15:
- while True:
- if pos >= len(data):
- raise ValueError('Truncated LZ4 length')
- extra = data[pos]
- pos += 1
- value += extra
- if extra != 255:
- break
- return value
-
- while pos < len(data):
- token = data[pos]
- pos += 1
- size = length(token >> 4)
- if pos + size > len(data) or len(out) + size > expected_size:
- raise ValueError('Invalid LZ4 literal run')
- out += data[pos:pos + size]
- pos += size
- if pos == len(data):
- break
- if pos + 2 > len(data):
- raise ValueError('Truncated LZ4 offset')
- distance = data[pos] | data[pos + 1] << 8
- pos += 2
- size = length(token & 15) + 4
- if not distance or distance > len(out) or len(out) + size > expected_size:
- raise ValueError('Invalid LZ4 match')
- for _ in range(size):
- out.append(out[-distance])
- if len(out) != expected_size:
- raise ValueError('Unexpected decoded icon size')
- return bytes(out)
-
-
-def _extension_cost(value, threshold):
- return 0 if value < threshold else 1 + (value - threshold) // 255
-
-
-def compress_lz4(data):
- """Find a minimum-size raw LZ4 parse for a small (<= 65535 byte) image.
-
- A suffix array finds the longest legal previous match. Dynamic programming
- chooses literal/match lengths, including extension-byte costs. The final
- five bytes are literals; the last match starts at least twelve bytes from
- the end, as required by standard LZ4 block decoders.
- """
- n = len(data)
- if n > 65535:
- raise ValueError('Only small build-time assets are supported')
- if not n:
- return b'\0'
- suffixes = sorted(range(n), key=lambda i: data[i:])
- rank = [0] * n
- for r, pos in enumerate(suffixes):
- rank[pos] = r
- seen = []
- matches = [(0, 0)] * n
- for i in range(n - 11):
- r = rank[i]
- at = bisect_left(seen, r)
- best, distance = 0, 0
- # Among earlier suffixes, a lexicographic neighbour has the longest
- # common prefix. Overlapping matches are legal in LZ4.
- for q in seen[max(0, at - 1):at + 1]:
- j = suffixes[q]
- lo, hi = 0, n - i - 5
- while lo < hi:
- mid = (lo + hi + 1) // 2
- if data[i:i + mid] == data[j:j + mid]:
- lo = mid
- else:
- hi = mid - 1
- if lo > best:
- best, distance = lo, i - j
- matches[i] = best, distance
- insort(seen, r)
-
- dp = [0] * (n + 1)
- plan = [None] * n
- match_cost = [0] * n
- match_length = [0] * n
- starts = []
- for i in range(n - 1, -1, -1):
- best = n - i + 1 + _extension_cost(n - i, 15)
- longest, _ = matches[i]
- if longest >= 4:
- cost, size = min((2 + _extension_cost(size, 19) + dp[i + size], size)
- for size in range(4, longest + 1))
- match_cost[i], match_length[i] = cost, size
- starts.append(i)
- for j in reversed(starts):
- literal = j - i
- if literal + 3 >= best:
- break
- cost = 1 + literal + _extension_cost(literal, 15) + match_cost[j]
- if cost < best:
- best = cost
- plan[i] = j, match_length[j], matches[j][1]
- dp[i] = best
-
- out = bytearray()
-
- def emit_length(value):
- while value >= 255:
- out.append(255)
- value -= 255
- out.append(value)
-
- anchor = 0
- while anchor < n:
- entry = plan[anchor]
- if entry is None:
- literal = n - anchor
- out.append(min(literal, 15) << 4)
- if literal >= 15:
- emit_length(literal - 15)
- out += data[anchor:]
- break
- j, size, distance = entry
- literal = j - anchor
- out.append(min(literal, 15) << 4 | min(size - 4, 15))
- if literal >= 15:
- emit_length(literal - 15)
- out += data[anchor:j]
- out += bytes((distance & 255, distance >> 8))
- if size >= 19:
- emit_length(size - 19)
- anchor = j + size
- if len(out) != dp[0] or decode_lz4(out, n) != data:
- raise ValueError('Lossless compression verification failed')
- return bytes(out)
-
-
-def compact_icon(source, output, objcopy):
- output.parent.mkdir(parents=True, exist_ok=True)
- with tempfile.TemporaryDirectory(dir=output.parent) as folder:
- folder = Path(folder)
- original, packed, obj = folder/'original.nwi', folder/'packed.nwi', folder/'icon.o'
- section = '.rodata.eadk_app_icon'
- subprocess.run([objcopy, '--dump-section=' + section + '=' + str(original),
- str(source), str(obj)], check=True)
- before = original.read_bytes()
- pixels = decode_lz4(before, ICON_BYTES)
- after = compress_lz4(pixels)
- if len(after) > len(before):
- after = before
- packed.write_bytes(after)
- subprocess.run([objcopy, '--update-section=' + section + '=' + str(packed),
- str(obj), str(output)], check=True)
- # Verify the bytes actually stored in the resulting ELF, too.
- subprocess.run([objcopy, '--dump-section=' + section + '=' + str(packed),
- str(output), str(folder/'verify.o')], check=True)
- if decode_lz4(packed.read_bytes(), ICON_BYTES) != pixels:
- raise ValueError('Packed icon pixels changed')
- return dict(source_icon_bytes=len(before), icon_bytes=len(after), decoded_icon_bytes=ICON_BYTES)
-
-
-def compact_app(source, output, linker):
- output.parent.mkdir(parents=True, exist_ok=True)
- script = Path(__file__).with_name('standalone.ld')
- with tempfile.TemporaryDirectory(dir=output.parent) as folder:
- packed = Path(folder)/'packed.nwa'
- subprocess.run([linker, '--relocatable', '--script', str(script), str(source),
- '-o', str(packed)], check=True)
- output.write_bytes(packed.read_bytes())
-
-
-def compact_release(source, output, objcopy):
- """Remove unused ARM mapping symbols from the transport ELF only.
-
- GNU objcopy preserves $t/$d symbols specially even when unused. Rename
- them in a temporary release copy, then let objcopy decide which symbols
- relocations still need. Keep the debug ELF intact for ARM disassembly.
- Python builders compare release/debug bytes on all supported profiles;
- Make's check target compares them on the smallest RAM profile.
- """
- output.parent.mkdir(parents=True, exist_ok=True)
- with tempfile.TemporaryDirectory(dir=output.parent) as folder:
- folder = Path(folder)
- stripped, renamed = folder/'stripped.nwa', folder/'renamed.nwa'
- subprocess.run([objcopy, '--strip-unneeded', str(source), str(stripped)], check=True)
- subprocess.run([objcopy, '--redefine-sym=$t=unused_thumb_marker',
- '--redefine-sym=$d=unused_data_marker', str(stripped),
- str(renamed)], check=True)
- subprocess.run([objcopy, '--strip-unneeded', str(renamed), str(output)], check=True)
-
-
-def main():
- ap = argparse.ArgumentParser(description=__doc__)
- ap.add_argument('kind', choices=('icon', 'app', 'release'))
- ap.add_argument('source', type=Path)
- ap.add_argument('output', type=Path)
- ap.add_argument('--objcopy', default='arm-none-eabi-objcopy')
- ap.add_argument('--ld', default='arm-none-eabi-ld')
- args = ap.parse_args()
- if args.kind == 'icon':
- print(json.dumps(compact_icon(args.source, args.output, args.objcopy)))
- elif args.kind == 'release':
- compact_release(args.source, args.output, args.objcopy)
- else:
- compact_app(args.source, args.output, args.ld)
-
-
-if __name__ == '__main__':
- main()
diff --git a/games/donkeykong/tools/rom.py b/games/donkeykong/tools/rom.py
new file mode 100644
index 00000000..d5ba6c21
--- /dev/null
+++ b/games/donkeykong/tools/rom.py
@@ -0,0 +1,90 @@
+#!/usr/bin/env python3
+"""src/rom.h: the data tables of Donkey Kong's program ROM (boards, Kong's poses, paths, timings),
+read from tcdev's annotated listing of it (dk.txt, "Donkey Kong Disassembly v0.1"), which shows every
+byte. Only data is taken; the game's code is ported by hand in sim.c. No graphics ROM is read.
+
+ python3 -I tools/rom.py path/to/dk.txt src/rom.h
+"""
+import re
+import sys
+
+# (first address, end address): each becomes rom_XXXX[], read in sim.c with ROM(address)
+RANGES = [
+ (0x0D17, 0x0D27), # the rivets' places in video RAM
+ (0x101B, 0x101F), # a new barrel
+ (0x1121, 0x1131), # the elevator shafts' motors
+ (0x117E, 0x1186), # 100m: the squares beside Kong
+ (0x11A2, 0x11A6), # a new spring
+ (0x3529, 0x3565), # points, 3 BCD bytes each
+ (0x364B, 0x3806), # the messages (GAME OVER, HOW HIGH CAN YOU GET?)
+ (0x380D, 0x3E70), # boards, Kong's poses and moves, paths, levels, start values
+]
+
+
+def number(tok):
+ tok = tok.strip()
+ return int(tok[:-1], 16) if tok[-1] in "hH" else int(tok)
+
+
+def rebuild(path):
+ rom, known = bytearray(0x4000), bytearray(0x4000)
+ last, ptr = None, 0
+ for line in open(path, encoding="latin-1"):
+ m = re.match(r"^(?:ROM:)?([0-9A-F]{4}) (.{0,17})(.*)$", line.rstrip("\n"))
+ if not m:
+ continue
+ addr, hexcol, code = int(m.group(1), 16), m.group(2), m.group(3).split(";", 1)[0]
+ d = re.search(r"\b(db|dw)\s+(.+)$", code)
+ if d:
+ if addr != last:
+ ptr = addr
+ last = addr
+ for op in re.findall(r"'[^']*'|[^,]+", d.group(2)):
+ op = op.strip()
+ if not op:
+ continue
+ if op.startswith("'"):
+ vals = [ord(c) for c in op[1:-1]]
+ else:
+ try:
+ v = number(op)
+ vals = [v & 255] if d.group(1) == "db" else [v & 255, v >> 8]
+ except ValueError: # a label: its bytes are in the listing's byte column
+ vals = [int(b, 16) for b in re.findall(r"\b([0-9A-F]{2})\b", hexcol)[:2]]
+ for v in vals:
+ if ptr < 0x4000:
+ rom[ptr], known[ptr] = v, 1
+ ptr += 1
+ continue
+ b = re.match(r"((?:[0-9A-F]{2} ){1,4})", hexcol)
+ if b and "+" not in hexcol and code.strip():
+ for i, v in enumerate(b.group(1).split()):
+ rom[addr + i], known[addr + i] = int(v, 16), 1
+ last = None
+ return rom, known
+
+
+def main():
+ rom, known = rebuild(sys.argv[1])
+ out = ["/* Generated by tools/rom.py from tcdev's listing of Donkey Kong's program ROM (data tables only).",
+ " * ROM(address) reads them by their address in the arcade's ROM, as sim.c's comments name them. */",
+ "#ifndef DK_ROM_H", "#define DK_ROM_H", "#include <stdint.h>"]
+ for a, b in RANGES:
+ if not all(known[a:b]):
+ sys.exit(f"bytes missing in {a:04X}-{b:04X}")
+ out.append(f"static const uint8_t rom_{a:04X}[{b - a}] = {{")
+ for i in range(a, b, 16):
+ out.append(" " + ",".join(f"0x{v:02X}" for v in rom[i:min(b, i + 16)]) + ",")
+ out.append("};")
+ out.append("static inline uint8_t rom_byte(unsigned a) {")
+ for a, b in RANGES:
+ out.append(f" if (a - 0x{a:04X}u < {b - a}u) return rom_{a:04X}[a - 0x{a:04X}u];")
+ out.append(" return 0;")
+ out.append("}")
+ out.append("#define ROM(a) rom_byte((uint16_t)(a))")
+ out.append("#endif")
+ open(sys.argv[2], "w").write("\n".join(out) + "\n")
+
+
+if __name__ == "__main__":
+ main()
diff --git a/games/donkeykong/tools/standalone.ld b/games/donkeykong/tools/standalone.ld
deleted file mode 100644
index 28679582..00000000
--- a/games/donkeykong/tools/standalone.ld
+++ /dev/null
@@ -1,15 +0,0 @@
-/* Compact a standalone NWA after the first link has removed unused code.
- * Keep EADK metadata separate, and preserve mergeable strings for nwlink.
- * NumPlay modules retain their original sections for launcher garbage collection.
- */
-SECTIONS {
- .rodata.eadk_app_name : { *(.rodata.eadk_app_name) }
- .rodata.eadk_app_icon : { *(.rodata.eadk_app_icon) }
- .rodata.eadk_api_level : { *(.rodata.eadk_api_level) }
- .text : { *(.text.startup.main) *(.text .text.*) }
- .rodata.str1.1 : { *(.rodata*.str1.1) }
- .rodata : { *(.rodata .rodata.*) }
- .data : { *(.data .data.*) }
- .bss : { *(.bss .bss.*) *(COMMON) }
- /DISCARD/ : { *(.comment) *(.note.GNU-stack) }
-}
diff --git a/tools/release.py b/tools/release.py
index 40f004de..d1a77874 100644
--- a/tools/release.py
+++ b/tools/release.py
@@ -23,7 +23,6 @@ REPO = "https://github.com/Mason363/NumPlay"
EXTRAS = set()
# games too big to fit next to NumPlay on the calculator: only on their own, in a section of their own
ALONE = {
- "donkeykong": ("Donkey Kong", "the four arcade boards, adapted by Bobydeluxe; installed separately"),
"celeste": ("Celeste", "the whole climb up Celeste Mountain"),
"hollowknight": ("Hollow Knight", "from King's Pass to Hornet in Greenpath"),
"undertale": ("Undertale", "the whole Underground, to the barrier and the neutral ending"),
@@ -41,6 +40,7 @@ ALONE = {
"numvisuals": ("NumVisuals", "moving backgrounds with a clock, a stopwatch, a timer, a counter or your own words"),
"cookieclicker": ("Cookie Clicker", "the whole game, to the grandmapocalypse, ascension and the dragon"),
"jetpackjoyride": ("Jetpack Joyride", "Barry's run through the lab, with five vehicles and the Final Spin"),
+ "donkeykong": ("Donkey Kong", "the 1981 arcade game: barrels, pies, elevators and rivets, up to Pauline"),
}
VARIANTS = [
("NumPlay-Invisible.nwa", "The same app, hidden: a blank icon with no name"),