Edouard Silvestre
Back to projects

Epitech · 1st year · Pair project

Corewar

A virtual machine where programs fight in a shared memory.

  • C
  • Machine virtuelle
  • Assembleur
Try it
Period
April – May 2025
Team
Pair project with Ethanlsbr
Code
Private repository
cycle168 2566,144 byteschampionsAbelBillTyronpddwinsfig. 10 — Corewar · programs fighting in memory

The project

Corewar is a programming game. “Champions” written in a dedicated assembly language are loaded into the same 6,144-byte circular memory, and the virtual machine runs them in parallel.

Each instruction takes a precise number of cycles. A champion can write into memory, duplicate itself with fork, or report that it is alive. One that stops reporting in time is eliminated.

The project has two parts: the assembler, which turns a champion into bytecode, and the virtual machine, which runs the fight.

For this replay, I fixed that sign bug in a copy of our virtual machine. Without the fix, backward jumps go into empty memory and fights stop almost at once.

My part

  • The virtual machine engine: decoding and running the 16 instructions, the cycle loop
  • The end-of-game logic: lives, cycle to die, winner
  • In the assembler: the linked list and the syntax and semantic validation

What was hard

  • Respecting each instruction’s timing to the cycle, while several processes share the same memory and take turns.
  • Decoding arguments down to the bit: a coding byte says whether each argument is a register, a direct value or an address, and each type has its own size.
  • The end of the game: knowing who is still alive, when to reduce the cycle to die, and when to declare a winner.

What I learned

  • How a simple processor works: registers, program counter, carry, relative addressing and circular memory.
  • A one-line detail can change everything. While preparing this replay, I found that our 2-byte arguments were not read as signed: a backward jump went forward.
  • Splitting a project between two people: one on the assembler, the other on the virtual machine, with a shared bytecode format.

Try it.

Fights, replayed

The champions provided with the assignment fight in our virtual machine. Each square is a byte of memory, coloured by the champion that wrote it. The white dots are the processes.

Credits

  • Champions Abel, Bill, pdd and Tyron: provided with the Epitech assignment.