{"id":25615080,"url":"https://github.com/OpenRakis/Spice86","last_synced_at":"2025-02-22T03:01:30.693Z","repository":{"id":37740586,"uuid":"441631821","full_name":"OpenRakis/Spice86","owner":"OpenRakis","description":"Reverse engineer and rewrite real mode DOS programs! ","archived":false,"fork":false,"pushed_at":"2024-10-27T21:39:52.000Z","size":23005,"stargazers_count":219,"open_issues_count":39,"forks_count":18,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-10-30T01:02:55.274Z","etag":null,"topics":["assembly","avaloniaui","cross-platform","debugger","disassembler","dos","dotnet","emulator","gdb","gdb-commands","linux","macos","reverse-engineering","windows"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/OpenRakis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2021-12-25T07:57:10.000Z","updated_at":"2024-10-27T16:34:46.000Z","dependencies_parsed_at":"2023-09-22T22:50:18.371Z","dependency_job_id":"32bd682f-8bd2-465f-81d3-dffb02565ebe","html_url":"https://github.com/OpenRakis/Spice86","commit_stats":{"total_commits":1709,"total_committers":8,"mean_commits":213.625,"dds":0.2878876535985957,"last_synced_commit":"fdcd2656cda4aca743e576fcf486a849acb4fca3"},"previous_names":[],"tags_count":7,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenRakis%2FSpice86","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenRakis%2FSpice86/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenRakis%2FSpice86/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/OpenRakis%2FSpice86/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/OpenRakis","download_url":"https://codeload.github.com/OpenRakis/Spice86/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240118372,"owners_count":19750483,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["assembly","avaloniaui","cross-platform","debugger","disassembler","dos","dotnet","emulator","gdb","gdb-commands","linux","macos","reverse-engineering","windows"],"created_at":"2025-02-22T03:01:23.400Z","updated_at":"2025-02-22T03:01:30.685Z","avatar_url":"https://github.com/OpenRakis.png","language":"C#","readme":"# Spice86 - A PC emulator for real mode reverse engineering\n\n![Linux](https://img.shields.io/badge/-Linux-grey?logo=linux)\n![macOS](https://img.shields.io/badge/-OSX-black?logo=apple)\n![Windows](https://img.shields.io/badge/-Windows-red?logo=windows)\n[![.NET Build](https://github.com/OpenRakis/Spice86/actions/workflows/prerelease.yml/badge.svg)](https://github.com/OpenRakis/Spice86/actions/workflows/dotnet.yml)\n[![NuGet](https://img.shields.io/nuget/v/Spice86.svg)](https://www.nuget.org/packages/Spice86)\n![Licence](https://img.shields.io/badge/License-Apache_2.0-44cc11.svg)\n[![downloads](https://img.shields.io/nuget/dt/Spice86)](https://www.nuget.org/packages/Spice86)\n\nSpice86 is a tool to execute, reverse engineer and rewrite real mode DOS programs for which source code is not available.\n\nRelease are available [on Nuget](https://www.nuget.org/packages/Spice86/).\n\nPre-releases are also available [on the Release page](https://github.com/OpenRakis/Spice86/releases/tag/latest)\n\nNOTE: This is a port, and a continuation from the [original Java Spice86](https://github.com/kevinferrare/spice86).\n\nIt requires [.NET 8](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) and runs on Windows, macOS, and Linux.\n\n## Approach\nRewriting a program from only the binary is a hard task.\n\nSpice86 is a tool that helps you do so with a methodic divide and conquer approach.\n\nGeneral process:\n- You start by emulating the program in the Spice86 emulator.\n- At the end of each run, the emulator dumps some runtime data (memory dump and execution flow)\n- You load those data into [ghidra](https://github.com/NationalSecurityAgency/ghidra) via the [spice86-ghidra-plugin](https://github.com/OpenRakis/spice86-ghidra-plugin)\n- The plugin converts the assembly instructions in the memory dump to C# that can be loaded into spice86 and used either partially or completely instead of the assembly code.\n- This allows you to gradually reimplement the assembly code with your C# methods\n- This is helpful because:\n  - Small sequences of assembly can be statically analyzed and are generally easy to translate to a higher level language.\n  - You work all the time with a fully working version of the program so it is relatively easy to catch mistakes early.\n  - Rewriting code function by function allows you to discover the intent of the author.\n\n## Running your exe\nThis is a .NET program, you run it with the regular command line or dotnet run. Example with running a program called file.exe:\n```\nSpice86 -e file.exe\n```\nCOM files and BIOS files are also supported.\n\n## Dumping data\nIt is recommended to set SPICE86_DUMPS_FOLDER environment variable pointing to where the emulator should dump the runtime data.\nIf the variable is set or if --RecordedDataDirectory parameter is passed, the emulator will dump a bunch of information about the run there. If nothing is set, data will be dumped in the current directory.\nIf there is already data there the emulator will load it first and complete it, you don't need to start from zero each time!\n\n## More command line options\n\n```\n  --Debug                            (Default: false) Starts the program paused.\n  --Ems                              (Default: false) Enables EMS memory. EMS adds 8 MB of memory accessible to DOS programs through the EMM Page Frame.\n  --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).\n  -m, --Mt32RomsPath                 Zip file or directory containing the MT-32 ROM files\n  -c, --CDrive                       Path to C drive, default is exe parent\n  -r, --RecordedDataDirectory        Directory to dump data to when not specified otherwise. Working directory if blank\n  -e, --Exe                          Required. Path to executable\n  -a, --ExeArgs                      List of parameters to give to the emulated program\n  -x, --ExpectedChecksum             Hexadecimal string representing the expected SHA256 checksum of the emulated program\n  -f, --FailOnUnhandledPort          (Default: false) If true, will fail when encountering an unhandled IO port. Useful to check for unimplemented hardware. false by default.\n  -g, --GdbPort                      gdb port, if empty gdb server will not be created. If not empty, application will pause until gdb connects\n  -o, --OverrideSupplierClassName    Name of a class that will generate the initial function information. See documentation for more information.\n  -p, --ProgramEntryPointSegment     (Default: 4096) Segment where to load the program. DOS PSP and MCB will be created before it.\n  -u, --UseCodeOverride              (Default: true) \u003ctrue or false\u003e if false it will use the names provided by overrideSupplierClassName but not the code\n  -i, --InstructionsPerSecond        \u003cnumber of instructions that have to be executed by the emulator to consider a second passed\u003e if blank will use time based timer.\n  -t, --TimeMultiplier               (Default: 1) \u003ctime multiplier\u003e if \u003e1 will go faster, if \u003c1 will go slower.\n  -d, --DumpDataOnExit               (Default: true) When true, records data at runtime and dumps them at exit time\n  -h, --HeadlessMode                 (Default: false) Headless mode. If true, no GUI is shown.\n  -l, --VerboseLogs                  (Default: false) Enable verbose level logs\n  -w, --WarningLogs                  (Default: false) Enable warning level logs\n  -s, --SilencedLogs                 (Default: false) Disable all logs\n  -i, --InitializeDOS                (Default: true) Install DOS interrupt vectors or not.\n  --StructureFile                    Path to a C header file that describes the structures in the application. Works best with exports from IDA or Ghidra\n  --help                             Display this help screen.\n  --version                          Display version information.\n```\n\n## Dynamic analysis\nSpice86 speaks the [GDB](https://www.gnu.org/software/gdb/) remote protocol:\n- it supports most of the commands you need to debug.\n- it also provides custom GDB commands to do dynamic analysis.\n\n### Alternative to GDB\n\nAlternatively, Spice86 has a [home-grown debugger](https://github.com/OpenRakis/Spice86/wiki/Spice86-internal-debugger).\n\n### Connecting to GDB\nYou need to specify a port for the GDB server to start when launching Spice86:\n```\nSpice86 --GdbPort=10000\n```\n\nSpice86 will wait for GDB to connect before starting execution so that you can setup breakpoints and so on.\n\nHere is how to connect from GDB command line client and how to set the architecture:\n```\n(gdb) target remote localhost:10000\n(gdb) set architecture i8086\n```\n\n### Vanilla GDB\nYou can add breakpoints, step, view memory and so on.\n\nExample with a breakpoint on VGA VRAM writes:\n```\n(gdb) watch *0xA0000\n```\n\nViewing assembly:\n```\n(gdb) layout asm\n```\n\nRemoving a breakpoint:\n```\n(gdb) remove 1\n```\n\nSearching for a sequence of bytes in memory (start address 0, length F0000, ascii bytes of 'Spice86' string):\n```\n(gdb) find /b 0x0, 0xF0000, 0x53, 0x70, 0x69, 0x63, 0x65, 0x38, 0x36\n```\n\nGDB does not support x86 real mode segmented addressing, so pointers need to refer to the actual physical address in memory. VRAM at address A000:0000 would be 0xA0000 in GDB.\n\nSimilarly, The $pc variable in GDB will be exposed by Spice86 as the physical address pointed by CS:IP.\n\n### Custom GDB commands (where the magic happens)\nThe list of custom commands can be displayed like this:\n```\n(gdb) monitor help\n```\n\n#### Dump information about current run\n```\n(gdb) monitor dumpall\n```\nDumps everything described below in one shot. Files are created in the dump folder as explained [here](https://github.com/OpenRakis/Spice86#dumping-data)\nSeveral files are produced:\n - spice86dumpMemoryDump.bin: Snapshot of the real mode address space. Contains the instructions that are actually loaded and executed. They may differ from the exe you are running because DOS programs can rewrite some of their instructions / load additional modules in memory.\n - spice86dumpExecutionFlow.json: Contains information used by the [spice86-ghidra-plugin](https://github.com/OpenRakis/spice86-ghidra-plugin) to make sense of the memory dump, like addresses of the functions, the labels, the instructions that have been executed ...\n\n#### Special breakpoints\nBreak after x emulated CPU Cycles:\n```\n(gdb) monitor breakCycles 1000\n```\n\nBreak at the end of the emulated program:\n```\n(gdb) monitor breakStop\n```\n\n#Refreshing screen or buffers while debugging\n```\n(gdb) monitor vbuffer refresh\n```\n\nFor a pleasing and productive experience with GDB, the [seerGDB](https://github.com/epasveer/seer) client is highly recommended.\n\n\n\n## Reverse engineering process\nConcrete example with Cryo Dune [here](https://github.com/OpenRakis/Cryogenic).\n\nFirst run your program and make sure everything works fine in Spice86. If you encounter issues it could be due to unimplemented hardware / DOS / BIOS features.\n\nWhen Spice86 exits, it should dump data in current folder or in folder specified by env variable \n\nOpen the data in [ghidra](https://github.com/NationalSecurityAgency/ghidra) with the [spice86-ghidra-plugin](https://github.com/OpenRakis/spice86-ghidra-plugin) and generate code. You can import the generated files in a template project you generate via the [spice86-dotnet-templates](https://github.com/OpenRakis/spice86-dotnet-templates):\n```\ndotnet new spice86.project\n```\n\n## Overriding emulated code with C# code\n\nYou can provide your own C# code to override the program original assembly code.\n\n### Defining overrides\nSpice86 can take in input an instance of Spice86.Core.Emulator.Function.IOverrideSupplier that builds a mapping between the memory address of functions and their C# overrides.\n\nFor a complete example you can check the source code of [Cryogenic](https://github.com/OpenRakis/Cryogenic).\n\nHere is a simple example of how it would look like:\n```csharp\nnamespace My.Program;\n\n// This class is responsible for providing the overrides to spice86.\n// There is only one per program you reimplement.\npublic class MyProgramOverrideSupplier : IOverrideSupplier {\n  public IDictionary\u003cSegmentedAddress, FunctionInformation\u003e GenerateFunctionInformations(int programStartSegment,\n                                                                                 Machine machine) {\n    Dictionary\u003cSegmentedAddress, FunctionInformation\u003e res = new();\n    // In more complex examples, overrides may call each other\n    new MyOverrides(res, programStartSegment, machine);\n    return res;\n  }\n\n  public override string ToString() {\n    return \"Overrides My program exe. class is \" + GetType().FullName;\n  }\n}\n\n// This class contains the actual overrides. As the project grows, you will probably need to split the reverse engineered code in several classes.\npublic class MyOverrides : CSharpOverrideHelper {\n  private MyOverridesGlobalsOnDs globalsOnDs;\n\n  public MyOverrides(IDictionary\u003cSegmentedAddress, FunctionInformation\u003e functionInformations, ushort entrySegment, Machine machine, ILoggerService loggerService, Configuration configuration) {\n    // \"myOverides\" is a prefix that will be appended to all the function names defined in this class\n    base(functionInformations, machine,  loggerService, configuration);\n    globalsOnDs = new MyOverridesGlobalsOnDs(machine);\n    // incUnknown47A8_0x1ED_0xA1E8_0xC0B8 will get executed instead of the assembly code when a call to 1ED:A1E8 is performed.\n    // Also when dumping functions, the name myOverides.incUnknown47A8 or instead of unknown\n    // Note: the segment is provided in parameter as spice86 can load executables in different places depending on the configuration\n    DefineFunction(segment, 0xA1E8, \"incDialogueCount47A8\", IncDialogueCount47A8_0x1ED_0xA1E8_0xC0B8);\n    DefineFunction(segment, 0x0100, \"addOneToAX\", AddOneToAX_0x1ED_0x100_0x1FD0);\n  }\n\n  public Action IncDialogueCount47A8_0x1ED_0xA1E8_0xC0B8() {\n    // Accessing the memory via accessors\n    globalsOnDs.SetDialogueCount47A8(globalsOnDs.GetDialogueCount47A8() + 1);\n    // Depends on the actual return instruction performed by the function, needed to be called from the emulated code as\n    // some programs like to mess with the stack ...\n    return NearRet();\n  }\n\n  private Action AddOneToAX_0x1ED_0x100_0x1FD0() {\n    // Assembly for this would be\n    // INC AX\n    // RETF\n    // Note that you can access the whole emulator to change the state in the overrides.\n    state.AX++;\n    return NearRet();\n  }\n}\n\n// Memory accesses can be encapsulated into classes like this to give names to addresses and make the code shorter.\npublic class GlobalsOnDs : MemoryBasedDataStructureWithDsBaseAddress {\n    public GlobalsOnDs(IByteReaderWriter memory, SegmentRegisters segmentRegisters) : base(memory, segmentRegisters) {\n    }\n\n    // Getters and Setters for address 0x1DD:0x2/0x1DD2.\n    // Was accessed via the following registers: DS\n    public int Get01DD_0002_Word16() {\n        return UInt16[0x2];\n    }\n\n    // Operation not registered by running code\n    public void Set01DD_0002_Word16(byte value) {\n        UInt16[0x2] = value;\n    }\n\n    // Getters and Setters for address 0x1138:0x0/0x11380.\n    // Operation not registered by running code\n    public int Get1138_0000_Word16() {\n        return UInt16[0x0];\n    }\n}\n```\n\n*Remember*: You must tell Spice86 to use your assembly code overrides with the command line argument \"--UseCodeOverride true\" when debugging your project.\n\nAlong with the mandatory path to your DOS program, passed with the --ExePath argument.\n\n## Debugger\n\nSpice86 comes with a built-in debugger that can be used to debug the emulated program. It is a simple debugger that allows you to inspect the memory, the disassembly, the registers, and the stack.\n\n### Structure viewer\n\nThe structure viewer allows you to inspect the memory in a structured way. It is useful to inspect the memory as a structure, like the DOS PSP, the DOS MCB, the VGA registers, etc.\n\nFirst you need a C header file that describes the structures in the application. You can generate one with Ghidra or IDA. Then you can load it with the `--StructureFile` commandline argument.\nThis will enable the \"Structure view\" button in the Memory tab of the debugger.\n\nThere you enter a segment:offset address and choose the structure you want to view. The structure will be displayed in a tree view and the memory in a hex view.\n\nThe display updates whenever the application is paused, so you can step through the program and see how the structure changes.\nExporting a new C header file from Ghidra or IDA will also update the structure viewer with the new information real-time.\n\nYou can also enter the Structure view by selecting a range of bytes in the Memory tab and right-clicking on it.\n\n## Misc\n### C Drive\nIt is possible to provide a C: Drive for emulated DOS functions with the option **--CDrive**. Default is current folder. For some games you may need to set the C drive to the game folder.\n\n### Emulated program arguments\nYou can pass arguments (max 127 chars!) to the emulated program with the option **--ExeArgs**. Default is empty.\n\n### Time\nThe emulated Timer hardware of the PC (Intel 8259) supports measuring time from either:\n- The real elapsed time. Speed can be altered with parameter **--TimeMultiplier**.\n- The number of instructions the emulated CPU executed. This is the behaviour that is activated with parameter **--InstructionsPerSecond** and is forced when in GDB mode so that you can debug with peace of mind without the timer triggering.\n\n### Screen refresh\nScreen is refreshed 30 times per second and each time a VGA retrace wait is detected (see Renderer.cs).\n\n### Emulator features\nCPU:\n- Only 16 bits instructions are fully supported, memory size is 1MB\n- Most 32 bits instructions are implemented, but not validated via integration tests for now.\n- The only supported addressing mode is real mode. 286/386 Protected mode and the related instructions are not implemented.\n- Instruction set is (hopefully!) fully implemented for 8086, and validated via automated tests.\n- For 80186, BOUND instruction is missing.\n- For 80286, instructions related to protected mode are not implemented\n- For 80386, protected mode is not implemented.\n- No FPU instruction implemented apart those used for FPU detection.\n\nMemory:\n- Segmented addressing is implemented.\n- The A20 Gate is supported.\n- Helpers are available in order to convert a segmented address into a physical address, and vice-versa.\n- EMS (Expanded Memory) 3.2 is partially implemented.\n- XMS (Extended Memory) is not implemented.\n- X86 Paging (virtual memory) is not implemented.\n\nGraphics:\n- Text modes, VGA, EGA, and CGA are implemented.\n\nDOS:\n- Part of int 21 is implemented. Identifies itself as dos 5.0 for now.\n\nInput:\n- Keyboard\n- Mouse\n- No joystick for now\n\nCD-ROM:\n- No MSCDEX support for now. Some games, like DUNE, can be copied entirely from the CD and run from the hard drive.\n\nSound:\n\nOn *nix systems, you'll need to have libportaudio installed.\nWithout it, there will be no sound.\n\n- PC Speaker is implemented.\n- Adlib/SoundBlaster MIDI OPL is supported.\n- SoundBlaster PCM is supported.\n- MT-32 is supported. However, not on macOS, as a static build of MUNT is missing for that platform. (PRs welcome !)\n- General MIDI is supported.\n\nCompatibility list available [here](COMPATIBILITY.md).\n\n### How to build on your machine\n\n- Install the [.NET 8 SDK](https://dotnet.microsoft.com/en-us/download/dotnet/8.0) (once)\n- clone the repo\n- run this where Spice86.sln is located:\n\n```bash\n   dotnet build\n```\n\n### How to run\n\n```bash\n   Spice86 -e \u003cpath to executable\u003e\n```\n\nor use this where Spice86.csproj is located:\n\n```bash\n   dotnet run -e \u003cpath to executable\u003e\n```\n\n\n### Ghidra plugin\n\nThis uses Ghidra and Java 17.\n\nBefore using it, define an environnement variable named SPICE86_DUMPS_FOLDER pointing to a folder where the Spice86 dumps are located.\nThey are generated on exit.\n\nGeneral procedure, in order:\n\n1.Ghidra's own script 'ImportSymbolScript.py' (input used is \"spice86dumpGhidraSymbols.txt\")\n\n2.Ghidra's Auto-Analyze (only enable 'Dissasemble Entry Points')\n\n3.Now, you can use the plugin.\n\nRemember: if Ghidra displays SUBROUTINES, use the 'f' key to convert them into functions. The code generator only works with functions.\n\nAlso, if you have any weird behaviour, make sure you have Java 17 and ONLY Java 17. That's how Ghidra likes it.\n\n### Some screenshots\nCryo dune:\n\n![](doc/cryodune_worm.png)\n\n![](doc/cryodune_orni.png)\n\nPrince of persia:\n![](doc/prince_of_persia.PNG)\n\nStunts:\n![](doc/stunts_menu.PNG)\n\n![](doc/stunts_loop.png)\n\n![](doc/stunts_crash.PNG)\n\n![](doc/stunts_skid.PNG)\n\nBetrayal at Krondor:\n\n![](doc/BaK.png)\n\n## Credits\n\nSome emulation code was adapted from the [Aeon emulator](https://github.com/gregdivis/Aeon) by @gregdivis. Those are: The DMA Controller, the PC Speaker, the SoundBlaster, the MT-32, and General MIDI.\n\nThis project uses JetBrains Rider licenses, thanks to JetBrains' [Open Source Community Support](https://www.jetbrains.com/community/opensource/#support).\n\n![](jetbrainsrider.svg)\n\nThe UI is powered by [Avalonia UI](https://avaloniaui.net/).\n\n![](avalonia.png)\n","funding_links":[],"categories":["C# #"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOpenRakis%2FSpice86","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FOpenRakis%2FSpice86","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FOpenRakis%2FSpice86/lists"}