r/rust_gamedev • • 5d ago

Native, Zero-VM Rust Engine & Multi-Platform Runtime for Prehistorik 2 (1993)

https://github.com/murat-saglam/rustypre2

Over the past weeks I have been working on rustypre2, a standalone native Rust engine and multi-platform runtime for Titus Interactive's 1993 MS-DOS platformer Prehistorik 2 (PRE2.EXE, LZEXE-packed 16-bit real-mode x86).

Repository:https://github.com/murat-saglam/rustypre2
Releases (Windows, macOS Apple Silicon, Linux x86_64/ARM64):https://github.com/murat-saglam/rustypre2/releases/tag/v0.1.0

This project builds on the reverse-engineering groundwork of missingno7/pre2_port, which showed that PRE2.EXE could be lifted out of a 16-bit CPU emulator by recovering its leaf functions against lockstep memory and VRAM checkpoints. My goal with rustypre2 was to rewrite the entire engine and runtime in native Rust (2024 Edition) with zero compiler warnings, zero per-frame heap allocations on the hot path, no external audio DLLs, and deterministic 23.333 Hz frame pacing across Windows, macOS, and Linux.

Even without a CPU VM, the original game logic is tightly coupled to static lookup tables in DGROUP (0x1A0F) and CS, including jump parabolas, sine/cosine tables, camera scroll speed curves, sprite hitbox and draw-offset tables, animation scripts, and deterministic attract-mode input streams. rustypre2 embeds the 64 KB initial DGROUP image (assets/dgroup.bin) and CS table windows directly into the binary and loads decompressed .SQZ level and sprite assets at the exact paragraph boundaries ([0x2875]) the DOS allocator used.

Gameplay runs in planar EGA Mode 0x0D (320x200, 16-color planar with an 18-bit VGA DAC). Instead of redrawing the whole screen every tick, the original engine maintains a 20x12 tile ring buffer in off-screen VRAM (0xA000:0x3F40..0x5E80), a 256-slot planar sprite/tile cache at 0x5E80..0x7E80, and pans the CRTC start address with sub-byte pel-panning. In rustypre2, the four 64 KB EGA bitplanes are recycled through a thread-local PLANE_POOL to avoid 256 KB per-frame allocations. When SPRITES.SQZ is loaded (1030:2DFA), each 4-plane sprite is expanded by 1.25x into a 5-plane bank by prepending a union OR-mask silhouette plane. The sprite blitter (paint_sprite) emulates the EGA Map Mask and Data Rotate (ROR) hardware registers using unrolled 4-plane AND-mask and single-plane OR passes. Secret passage foreground tiles (flag & 0x40, routine 1030:3721) are pre-screened directly on live memory slices so we never clone the 64 KB tilemap grid or 64 KB tile graphics buffer unless an active sprite overlaps a secret passage.

Titus's .SQZ container dispatches across three different compression schemes based on the header byte: LZSS for level tilemaps and sprite sheets, LZW for front-end bitmaps (TITUS.SQZ, PRESENT.SQZ, CASTLE.SQZ, KEYB.SQZ), and Huffman + RLE for digital audio (SAMPLE.SQZ) and ending screens (THEEND.SQZ).

For audio, the .TRK music files decompress via .SQZ into 4-channel, 31-sample ProTracker (M.K.) modules. Rather than shelling out to libxmp or writing temporary files to disk, rustypre2 includes a pure-Rust 44.1 kHz stereo synthesizer (ModSynth) using Amiga PAL clock timing (3,546,895.0 / period), 4-point Catmull-Rom cubic spline interpolation, and anti-click volume ramping, mixed in real time in a single SDL2 audio callback alongside the 11 digital 8 kHz PCM sound effects from SAMPLE.SQZ.

The original game paces its main loop to 3 VGA 70 Hz vertical retraces (42.857 ms per tick). rustypre2 uses an Instant deadline pacer that subtracts simulation and render time from the wait budget and polls input at 1 ms intervals, keeping the tick rate steady even when the screen is full of enemies and particles. I also refined the vertical camera follow (1030:5663) in live play so landing (yvel == 0) no longer aborts upward camera re-centering mid-scroll.

On the desktop QoL side, pressing Alt+Enter (or Cmd+Enter on macOS) switches between a 960x600 (3x) window and borderless desktop fullscreen with a custom 1920x1080 prehistoric stone bezel overlay around a centered 960x600 viewport. Pressing P (or minimizing the window) freezes simulation and audio at the exact sample position and waits for P on restore. And for Windows builds, build.rs generates a raw x86_64 COFF .rsrc object (RT_GROUP_ICON + RT_ICON) directly in pure Rust at compile time without needing windres or rc.exe.

The repository is licensed under GPLv3 and contains only the engine code and static tables. To play, drop the .SQZ and .TRK files from your own legal copy of Prehistorik 2 (GOG or Steam) into the game_data folder next to the binary.

Happy to answer any questions about the disassembly, EGA planar tricks, or the Rust port!

0 Upvotes

0 comments sorted by