Lost Eden static recomp

Lost Eden static recomp is a Windows port of the Philips CD-i game Lost Eden, featuring Cartoon HD filters and run-ahead.

README

Lost Eden (CD-i) — static recompilation

A native Windows port of the Philips CD-i version of Lost Eden (Europe, PAL), made from the CD-i static recompilation template described further down. The game's 68000 code (CMDS/cdi_load, CMDS/cdi_edstart with its linked modules cdi_edlink1 and cdi_edlink2) is translated to C. The runtime emulates CD-RTOS, the CD-i video and audio hardware and the Digital Video Cartridge (MPEG-1 video and audio), which the game requires.

No game files are included in this repository or in the releases: you need your own copy of the disc as a .cue/.bin image.

Download and play

Get the latest release from the Releases page:

  • LostEden-<version>-setup.exe: installer. It asks for the folder with your .cue/.bin, needs no administrator rights, and installs to %LOCALAPPDATA%\Programs\Lost Eden (any folder can be chosen).
  • LostEden-<version>-portable.zip: unzip anywhere and run losteden.exe. The first time it asks for the disc image (or put it in a rom folder next to the program).

The program is portable: saves, quicksaves, settings, the disc location and the log are written to saves\ next to losteden.exe, never to AppData or the registry. The shaders are built into the executable, and it is linked with the static C runtime, so nothing else has to be installed. The shortcuts pass --portable, which is the default behaviour.

At start-up the game checks this repository for a newer release and offers to install it (saves and settings are kept; --no-update-check turns this off).

The release executable is built without the translated game code (RECOMP_INTERP_ONLY): it reads the game's program from your disc and runs it in the runtime's 68000 interpreter, with the same output as the recompiled build.

Controls
KeyAction
Arrow keys (Shift = faster), gamepad stick or D-padmove the pointer
Z / Space / A / Enter, gamepad Abutton 1
X / Ctrl / S, gamepad B or Ybutton 2
F2 / F3 / F4quicksave / next slot (1-10) / load
F6run-ahead 0-3 fields (default 1)
F7sprite interpolation to 60 fps on/off (default on)
F9 / F10 / F11aspect ratio / filter / fullscreen (or Alt+Enter)
Tab (hold)fast forward
Escquit
Enhancements
  • Cartoon HD and Cartoon HD+ filters (F10, runtime/cartoon.c): edge-directed 4x upscaling to 1536x960 after the xBR edge rule, run on all CPU cores and only over the parts of the picture that changed. HD+ also inks dark line art and applies an adaptive sharpen.
  • Run-ahead (runtime/frontend.c): after each real field the emulator is snapshotted, runs N more fields with the current input, shows that picture and restores the snapshot, so the game answers N fields (20 ms each) sooner. Only the real field is heard.
  • Sprite interpolation to 60 fps (runtime/motion.c): the game runs at 50 fields a second and holds most pictures for several fields. The frontend looks ahead (from a snapshot) for the next picture, finds which 8x8 blocks moved, and shows each 60 Hz frame with the moving parts at their position for that moment. Changes of shape switch when they did on the CD-i, and full motion video is shown at its own frame rate.
  • Quicksaves (runtime/state.c): F2 saves a complete snapshot of the emulated player to the selected slot (in memory and saves\quicksave_<n>.sav), F4 restores it, F3 selects one of 10 slots. Runtime heap objects (display tables, sound buffers, the MPEG decoders) live in one fixed-address arena, so a snapshot is the 68000 state, the used RAM, each module's globals and that arena.

If the PC cannot emulate the extra fields in time, interpolation and then run-ahead are turned down automatically, with a message on screen.

Building from source

  1. Put your disc image (Lost Eden (Europe).bin + .cue) in rom/.
  2. Run build.bat (needs Python 3, CMake 3.19+ and Visual Studio 2022). It extracts the program modules, recompiles them to C in gen/ and builds losteden.exe.
  3. Run losteden.exe.

package.bat builds the release packages in dist\: the portable zip and, with Inno Setup 6 installed, the single-file installer (installer\losteden.iss). Both use the interpreter-only build, so they contain nothing from the disc. The version is in runtime\version.h and VERSION.

Lost Eden-specific options: --overscan shows all 280 lines of the PAL field (by default the game's 240-line picture area is shown), --runahead N, --interpolation on|off and --filter N (0-5). For debugging there are also --mpegdump (write decoded MPEG pictures to saves/), --dumpdcp N (log the display program and compositing of field N), --data DIR (use DIR instead of saves/, for running several tests at once) and --hashlog (a checksum and cycle count of every field, for comparing runs). Headless runs leave the enhancements off unless --runahead/--interpolation are given; with them, the real fields must hash the same as without (a check that snapshots are complete). Script keys Q, E, W press F2, F3 and F4. tools/contact.py and tools/framestats.py turn frame dumps into a contact sheet and per-frame brightness figures.

What this port added to the template

  • runtime/mpeg.c: the Digital Video Cartridge's MPEG video (/mv) and MPEG audio (/ma) drivers. It demuxes the CD-i MPEG sectors and decodes them with the vendored pl_mpeg (MIT).
  • Kernel: F$Chain, F$Event (with the system line_event), F$CCtl, and the system globals pointer at address 0 (D_Ticks). The CSD now describes a player with the cartridge. Blocking calls (F$Sleep, event waits) resume by re-executing the trap, so a field always ends at the top-level dispatcher, where a snapshot can be taken.
  • PAL video (280 lines, 50 Hz), selected with "video": "pal" in game.json.
  • Analyzer: modules stored together in one file, lazy-binding call trampolines, branch tables, unrolled loops and code pointers in data. Code it still misses runs in the interpreter and is logged to saves/missed_entries.txt.
  • "interpret" in game.json lists self-modifying code, which always runs in the interpreter (Lost Eden's 3D pointer renderer patches its rotation matrix into muls.w #imm instructions).
  • Audio output is locked to the sound card's clock with about 100 ms queued, and MPEG playback follows the stream's SCR/PTS timing.
  • Frontend, snapshots, Cartoon HD upscalers, sprite interpolation, portable data folder, update check and packaging (see above).
  • Fixes: the extractor no longer relies on directory file numbers, run-length decoding is limited to CLUT7 and CLUT4, and the SC_Atten byte order is corrected.

All the details are in docs/CDI_NOTES.md (see "Additions from Lost Eden").


CD-i static recompilation template

This is a starting point for turning a Philips CD-i game into a native Windows program. The game's 68000 code is translated ahead of time into C. A runtime provides the parts of the CD-i the code expects: the CD-RTOS (OS-9) kernel, the CD file manager, the video decoder (with a Direct3D 11 presenter at any resolution) and the ADPCM audio processor.

It was extracted from the Link: The Faces of Evil project. That build is bit-identical to a pure-interpreter run of the same game, and this template reproduces it exactly.

Starting a new game

  1. Copy this folder to a new project folder.
  2. Put the disc image (.bin + .cue, raw 2352-byte sectors) in rom/.
  3. Edit game.json: set title, exe_name, and the modules to recompile (see below).
  4. Run build.bat. It extracts the program modules from the disc, recompiles them into gen/, builds with MSVC and copies <exe_name>.exe to the project root.
  5. Run the game, and use the debugging workflow below for anything that breaks.

Requirements: Windows 10+, Visual Studio 2022 (C compiler), CMake 3.19+ and Python 3.8+. No third-party libraries are needed.

game.json
{
  "title": "My CD-i Game",
  "exe_name": "mygame",
  "modules": [
    {"file": "auto", "base": "0x100000", "extra_entries": [], "a5_table": "auto"},
    {"file": "cdi_bumper", "base": "0xE0000", "a5_table": "auto"}
  ]
}
  • The first module is the program the player boots. "auto" reads its name from the disc label's application identifier (extracted/_app.txt).
  • Other modules are programs the game loads at run time with F$Load or F$Fork, for example the Philips logo "bumper". Each one is recompiled for its base address, and the runtime places it there. Ranges must not overlap and must stay below 0x140000, where the heap starts. Program modules the game loads that are not listed still run, slowly, in the built-in interpreter.
  • extra_entries are code addresses the analyzer can't discover, as absolute hex values (base + offset). The analyzer already finds:
    • call and branch targets;
    • switch tables;
    • MPW A5 jump tables;
    • Microware move.l #off,dN / jsr (pc,dN.l) calls;
    • F$Icpt handlers;
    • lea/pea function pointers that start with link or movem.
  • a5_table: "auto" detects programs built with Apple MPW-style toolchains, whose calls go through jsr N(a5).

To see what the analyzer found, run python tools/analyze.py [module index]. It writes a disassembly with recovered function names, from MacsBug symbols if present, to build/listing_<module>.asm.

Layout

PathContents
tools/cdi_extract.pyGreen Book filesystem reader. Writes program files and _app.txt (--all also dumps the real-time streams).
tools/m68k.pyMC68000 decoder
tools/os9mod.pyOS-9 module header, idata and irefs
tools/analyze.pyCode discovery (recursive descent plus the patterns above)
tools/gen.py68000→C generator, flag liveness, dispatch tables, gen/game_config.h
tools/config.pygame.json loader
tools/gen_tests.pyPer-instruction differential tests
tools/bmp2png.pyConverts frame dumps to PNG
runtime/cpu.cDispatcher and run loop
runtime/interp.c68000 interpreter, for RAM code and the --interp reference mode
runtime/alu.cShifts, division and BCD with exact flags
runtime/os9.cOS-9 kernel: memory, processes (fork/wait/exit), signals, alarms, paths, NVRAM, CSD
runtime/cdfm.cCD file manager: reads, and real-time SS_Play with PCB/PCL/CIL
runtime/video.cUCM display control (FCT/LCT), MCD212-style renderer, cursor, pointer
runtime/audio.cADPCM levels A/B/C, sound maps with gapless SM_Out, attenuation
runtime/render_d3d11.cGPU presenter: aspect modes and sharp/smooth/CRT filters
runtime/host_win32.cWindow, input (keyboard and XInput), waveOut, pacing, scripted and fuzz input
test/Instruction differential tester and ALU reference vectors
docs/CDI_NOTES.mdCD-i facts and API codes worked out during the first port

Debugging workflow

  • Find what fails. Unimplemented OS calls and SetStat/GetStat codes are logged to saves/recomp.log. Fatal errors also show a 68k register dump and a stack backtrace with function names.

  • Debug options:

    • --trace / --trace3 log syscalls with callers, signals and disc activity.
    • --headless --frames N --dump K runs fast without a window and writes every Kth frame as a BMP.
    • --script "field:key:dur,..." scripts input (keys 1 2 L R U D).
    • --fuzz SEED drives the game with random input.
    • --wav out.wav records the audio.
  • Check the translation. Run the same headless session twice, once with --interp, which executes everything in the interpreter, and compare the frame dumps and WAVs. --interp-range LO-HI (hex) interprets only the recompiled functions entered in that range: bisect with it to find the function that is translated wrongly. Headless runs are deterministic: emulated time is counted in instructions, and the clock uses a fixed epoch. Any difference points to a translation bug.

  • Unit-level checks. python tools/gen_tests.py and python test/gen_alu_vectors.py generate test sources. Then build the insn_test and alu_test targets:

    cmake --build build\cmake --config Release --target insn_test alu_test
    build\cmake\Release\insn_test.exe rom\<image>.bin 200
    build\cmake\Release\alu_test.exe
    
  • Code that jumps into RAM is not an error. It runs in the interpreter. Jumps to undiscovered addresses inside a recompiled module are fatal. Add those addresses to extra_entries, or teach analyze.py the pattern.

Things a new game may need

  • More CD-RTOS calls. The runtime implements what the first game used. See docs/CDI_NOTES.md for what's known and what isn't. The CD-i Full Functional Specification (Green Book, May 1994) documents the calls but gives no numeric function codes. Infer those from the game's library wrappers: look at the d2 values passed with os9 I$SetStt.
  • UCM drawing functions (DM_/DP_/DR_/RG_ and text). These aren't implemented yet because the first game drew everything itself.
  • PAL games. Set "video": "pal" in game.json. This selects a 625-line CSD, 280 lines and 50 Hz.
  • Full Motion Video (Digital Video Cartridge) is supported for what Lost Eden uses. See runtime/mpeg.c and docs/CDI_NOTES.md.