# Spice86 - A PC emulator for real mode reverse engineering ![Linux](https://img.shields.io/badge/-Linux-grey?logo=linux) ![macOS](https://img.shields.io/badge/-OSX-black?logo=apple) ![Windows](https://img.shields.io/badge/-Windows-red?logo=windows) [![.NET Build](https://github.com/OpenRakis/Spice86/actions/workflows/prerelease.yml/badge.svg)](https://github.com/OpenRakis/Spice86/actions/workflows/prerelease.yml) [![NuGet](https://img.shields.io/nuget/v/Spice86.svg)](https://www.nuget.org/packages/Spice86) ![Licence](https://img.shields.io/badge/License-Apache_2.0-44cc11.svg) [![downloads](https://img.shields.io/nuget/dt/Spice86)](https://www.nuget.org/packages/Spice86) Spice86 is a tool to execute, reverse engineer and rewrite real mode DOS programs for which source code is not available. Releases are available [on Nuget](https://www.nuget.org/packages/Spice86/). Pre-releases are also available [on the Release page](https://github.com/OpenRakis/Spice86/releases/tag/latest) NOTE: This is a port, and a continuation from the [original Java Spice86](https://github.com/kevinferrare/spice86). It requires [.NET 10](https://dotnet.microsoft.com/en-us/download/dotnet/10.0) and runs on Windows, macOS, and Linux. ## Approach Rewriting a program from only the binary is a hard task. Spice86 is a tool that helps you do so with a methodic divide and conquer approach. General process: - You start by emulating the program in the Spice86 emulator. - As the program runs, Spice86 builds a Control Flow Graph (CFG) of the executed code. - At the end of each run, the emulator dumps some runtime data (memory dump, execution flow and the CFG). - From that CFG, Spice86 **directly generates a self-contained, runnable C# project** that overrides the original assembly with equivalent C# code. - You then gradually rewrite the generated C# functions, replacing the mechanical translation with readable, intentful code (a task an LLM can help with). - This is helpful because: - Small sequences of assembly can be statically analyzed and are generally easy to translate to a higher level language. - You work all the time with a fully working version of the program so it is relatively easy to catch mistakes early. - Rewriting code function by function allows you to discover the intent of the author. Spice86 also dumps Ghidra-compatible symbols, so you can **optionally** load the memory dump into [Ghidra](https://github.com/NationalSecurityAgency/ghidra) via the [spice86-ghidra-plugin](https://github.com/OpenRakis/spice86-ghidra-plugin) for deeper static analysis. This is no longer required to produce C# overrides; it is just an extra analysis aid. ## Running your exe | Command | Description | |---------|-------------| | `Spice86 -e file.exe` | Run the specified executable | | `Spice86 -e file.com` | Run a COM file | | `Spice86 -e file.bat` | Run a DOS batch file | | `Spice86 -e file.bin` | Run a BIOS file | ## Dumping data | Setting | Description | |---------|-------------| | Environment Variable | `SPICE86_DUMPS_FOLDER` - Set this to control the base directory where data is dumped | | Command Line | Use `--RecordedDataDirectory` to specify the base dump location | | Default Location | Current directory if neither of the above is specified | **Note:** Regardless of the base directory setting, dumps are always placed in a subdirectory named with the program's SHA-256 hash. This ensures that multiple executables from the same game (e.g., SETUP.EXE, GAME.EXE) have isolated dump folders. The emulator dumps the following files: - **spice86dumpMemoryDump.bin**: Snapshot of the real mode address space - **spice86dumpExecutionFlow.json**: Contains function addresses, labels, and executed instructions - **spice86dumpGhidraSymbols.txt**: Ghidra-compatible symbols (function and label addresses), for optional import into Ghidra - **spice86dumpCfgBlocks.json**: The explored CFG as basic blocks with execution-context metadata - **spice86dumpCfgPartitions.json**: The recovered function/helper partitions and inter-partition transfers, derived from the block graph - **spice86dumpCfgGeneratedOverrides.cs**: C# overrides generated from the explored CFG - **GeneratedProject/**: A self-contained, runnable C# override project (see [Generated C# override project](#generated-c-override-project)) When there is already data in the specified location, the emulator will load it first and complement it. **CFG graph reload:** The CFG instruction graph is also dumped (**spice86dumpCfgReload.json**) and, by default, reloaded from the recorded data directory at startup so previously explored program structure is preserved across runs. Disable with `--ReloadCfgGraph false`. It is a no-op if the file is absent. **Code overrides:** When code overrides are active (`--UseCodeOverride true` with an `--OverrideSupplierClassName`), the CFG and execution flow dumps (**spice86dumpExecutionFlow.json**, **spice86dumpCfgBlocks.json**, **spice86dumpCfgPartitions.json**, **spice86dumpCfgReload.json**) are not written. Overrides replace the emulated asm, so the recorded graph would jump between overrides and no longer reflect the program. ### CPU Heavy Logging For detailed debugging, you can enable CPU heavy logging which records every executed instruction to a file. **⚠️ Warning:** This feature has a significant performance impact and should only be used for debugging purposes. | Setting | Description | |---------|-------------| | `--CpuHeavyLog` | Enables CPU heavy logging (default: false) | | `--CpuHeavyLogDumpFile` | Custom path for the log file (optional). If set, will enable CpuHeavyLog as well. Default Location: `{DumpDirectory}/cpu_heavy.log` | | `--CpuHeavyLogExpressions` | Named expressions appended to every CPU heavy log line. Repeat the option for each expression to append (for example: `--CpuHeavyLogExpressions "life=AX+1" --CpuHeavyLogExpressions "myvalue=word ptr DS:[0x456]"`). Only active when `--CpuHeavyLog` is true. | **Log Format:** Each line contains: `SegmentedAddress InstructionString` **Example:** ``` 017D:0000 mov AX,0xDD1D 017D:0003 call near 0xE4AD 017D:E4AD mov SI,0x0080 ``` **Usage:** ```bash # Enable with default location Spice86 -e program.exe --CpuHeavyLog # Use custom log file path Spice86 -e program.exe --CpuHeavyLog --CpuHeavyLogDumpFile "C:\logs\cpu.log" # Append named expressions to each log line (example) Spice86 -e program.exe --CpuHeavyLog \ --CpuHeavyLogExpressions "life=AX+1" \ --CpuHeavyLogExpressions "myvalue=word ptr DS:[0x456]" ``` ## Command line options ``` --Debug (Default: false) Starts the program paused and pauses once again when stopping. --Cycles (Default: null) Target CPU cycles per ms, for the rare speed sensitive game. 3000 by default. Overrides Instructions per second option below if used. --Xms (Default: true) Enables 15 MB of XMS memory. --Ems (Default: true) Enables EMS memory. EMS adds 8 MB of memory accessible to DOS programs through the EMM Page Frame. --A20Gate (Default: false) Disables the 20th address line to support programs relying on the rollover of memory addresses above the HMA (slightly above 1 MB). -m, --Mt32RomsPath Zip file or directory containing the MT-32 ROM files -c, --CDrive Path to C drive, default is exe parent -r, --RecordedDataDirectory Directory to dump data to when not specified otherwise. If blank dumps to SPICE86_DUMPS_FOLDER, and if not defined dumps to a sub directory named with the program SHA 256 signature -e, --Exe Required. Path to executable -a, --ExeArgs List of parameters to give to the emulated program -x, --ExpectedChecksum Hexadecimal string representing the expected SHA256 checksum of the emulated program -f, --FailOnUnhandledPort (Default: false) If true, will fail when encountering an unhandled IO port. Useful to check for unimplemented hardware. false by default. -g, --GdbPort GDB port. If 0, GDB server will be disabled. Default is 10000. -o, --OverrideSupplierClassName Name of a class that will generate the initial function information. See documentation for more information. -p, --ProgramEntryPointSegment (Default: 4096) Segment where to load the program. DOS PSP and MCB will be created before it. -u, --UseCodeOverride (Default: true) if false it will use the names provided by overrideSupplierClassName but not the code -i, --InstructionTimeScale if blank will use time based timer. --ClockJitterSeed Optional integer seed enabling small deterministic clock jitter (±0.01 ms). Omit to disable. --ClockStartTime Optional UTC start date/time for the emulated clock (parseable by DateTime.Parse, e.g. 1993-06-01T00:00:00Z). When omitted defaults to the current UTC time. -t, --TimeMultiplier (Default: 1)