How to Write Emulator - Intro
To begin, this isn’t technically an emulator, the system I’m trying to model isn’t real. This is more of a simulator, but that’s less of a catchy search word on the Internet.
What is Core War
“Core War is a 1984 programming game created by D. G. Jones and A. K. Dewdney in which two or more battle programs (called “warriors”) compete for control of a virtual computer. These battle programs are written in an abstract assembly language called Redcode.” Wikipedia
There are some terms that are helpful to know, and which I will probably end up mixing together
- Core War - This is the programming game itself
- Warrior - A program written in Redcode
- Redcode - The assembly language
- MARS - Memory Array Redcode Simulator. The actual simulator.
- Core - The memory where the programs run
What I’m Making
I’m making a version of the MARS in C++, which can run and interpret the Redcode programming language. The aim being to create a simulator that can load and execute Redcode programs, displaying the state of the simulator and possibly allow running programs to be debugged.
Redcode is a type of assembly language, modelled after CISC assembly languages of the 1980s. It looks like this:
ADD.AB # 4, $ 3 MOV.F $ 2, @ 2 JMP.B $ -2, $ 0 DAT.F # 0, # 0
Each instruction contains an opcode and two operands. The opcode can have an optional modifier, and the operands can have optional addressing modes. The combination of the simple opcodes with the modifiers and addressing modes allows complex “Warriors” to be created. The aim being to write a warrior that will outlast all the other running warriors in the simulation.
To make this work, I need to create the following sections
- A simulation of the machine’s RAM (called the “core”). This is 8000 bytes of contiguous memory which is treated as a circular array. The ends wrap around.
- The MARS itself. A Redcode simulator follows a well defined Fetch-Decode-Execute cycle where each instruction takes one cycle to complete.
- A file parser for source code. A programmable machine is of no use if there’s no way to get code into it, after all!
- Visual display. Most CoreWar simulators show the core as a grid of blocks so you can watch the programs running. Some allow programs to be disassembled or stepped through.