# thomas-migneret/corewar architecture

> Architecture diagram of the GitHub repository thomas-migneret/corewar, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-11-23). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/thomas-migneret/corewar
- Repository: https://github.com/thomas-migneret/corewar

## Overview

This repository implements the Corewar virtual machine: command-line champion selection and `.cor` loading lead into a shared-memory, cycle-based simulation, which executes champion instructions, removes inactive processes, and reports the winner. The README also describes an assembler, but no assembler sources are present in the supplied tree; it is therefore not represented as an implemented subsystem. VM internals are partly unsampled, so documented VM relationships are cited to the README rather than inferred from filenames.

Main entry points: [`src/main.c`](https://github.com/thomas-migneret/corewar/blob/main/src/main.c) (Corewar entry), [`src/vm/run_cycle.c`](https://github.com/thomas-migneret/corewar/blob/main/src/vm/run_cycle.c) (Cycle scheduler), [`src/vm/init_vm.c`](https://github.com/thomas-migneret/corewar/blob/main/src/vm/init_vm.c) (VM initialization), [`src/vm/run_vm.c`](https://github.com/thomas-migneret/corewar/blob/main/src/vm/run_vm.c) (Simulation loop)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_input["Inputs and loading"]
  node_cli["CLI options<br/>[recup_arg.c]"]
  node_binary["Champion loader<br/>[open_file.c]"]
  node_formats["Opcode and format definitions<br/>[op.h]"]
end

subgraph group_runtime["VM runtime"]
  node_entry["Corewar entry<br/>[main.c]"]
  node_vmsetup["VM initialization<br/>[init_vm.c]"]
  node_champions["Champion placement<br/>[load_champions.c]"]
  node_arena[("Circular arena<br/>[memory.c]")]
  node_processes["Process state<br/>[init_processes.c]"]
  node_scheduler["Cycle scheduler<br/>[run_cycle.c]"]
  node_vmrun["Simulation loop<br/>[run_vm.c]"]
end

subgraph group_instructions["Instruction execution"]
  node_decode["Opcode decoding<br/>[decode.c]"]
  node_validation["Argument validation"]
  node_dispatch["Instruction dispatcher"]
  node_instruction_families["Instruction implementations"]
end

subgraph group_outcomes["Game outcomes"]
  node_live["Live tracking<br/>[instruction_live.c]"]
  node_death["Process death checks<br/>[death_checks.c]"]
  node_winner["Winner announcement<br/>[winner.c]"]
  node_dump["Arena dump<br/>[dump_arena.c]"]
end

node_player(("Game launcher"))
node_operator(("Game operator"))

node_player -->|"starts game"| node_entry
node_entry -->|"parses options"| node_cli
node_entry -->|"loads files"| node_binary
node_entry -->|"initializes VM"| node_vmsetup
node_entry -->|"runs simulation"| node_vmrun
node_cli -.->|"selects champions"| node_binary
node_binary -->|"reads binary layout"| node_formats
node_vmsetup -.->|"places champions"| node_champions
node_champions -.->|"loads code"| node_arena
node_vmsetup -.->|"creates processes"| node_processes
node_vmrun -.->|"advances cycles"| node_scheduler
node_scheduler -.->|"decodes instructions"| node_decode
node_decode -->|"uses opcode table"| node_formats
node_decode -.->|"validates arguments"| node_validation
node_scheduler -.->|"executes instruction"| node_dispatch
node_dispatch -->|"dispatches opcode"| node_instruction_families
node_instruction_families -.->|"reads and writes"| node_arena
node_instruction_families -.->|"runs live opcode"| node_live
node_scheduler -.->|"checks survival"| node_death
node_death -.->|"removes inactive"| node_processes
node_vmrun -.->|"reports winner"| node_winner
node_operator -.->|"requests dump"| node_dump
node_dump -.->|"prints arena state"| node_arena

click node_cli "https://github.com/thomas-migneret/corewar/blob/main/src/parcing/recup_arg.c"
click node_binary "https://github.com/thomas-migneret/corewar/blob/main/src/parcing/open_file.c"
click node_formats "https://github.com/thomas-migneret/corewar/blob/main/include/op.h"
click node_entry "https://github.com/thomas-migneret/corewar/blob/main/src/main.c"
click node_vmsetup "https://github.com/thomas-migneret/corewar/blob/main/src/vm/init_vm.c"
click node_champions "https://github.com/thomas-migneret/corewar/blob/main/src/vm/load_champions.c"
click node_arena "https://github.com/thomas-migneret/corewar/blob/main/src/vm/memory.c"
click node_processes "https://github.com/thomas-migneret/corewar/blob/main/src/vm/init_processes.c"
click node_scheduler "https://github.com/thomas-migneret/corewar/blob/main/src/vm/run_cycle.c"
click node_vmrun "https://github.com/thomas-migneret/corewar/blob/main/src/vm/run_vm.c"
click node_decode "https://github.com/thomas-migneret/corewar/blob/main/src/vm/decode.c"
click node_validation "https://github.com/thomas-migneret/corewar/blob/main/src/vm/decode_validation.c"
click node_dispatch "https://github.com/thomas-migneret/corewar/blob/main/src/vm/execute_instruction.c"
click node_instruction_families "https://github.com/thomas-migneret/corewar/tree/main/src/vm"
click node_live "https://github.com/thomas-migneret/corewar/blob/main/src/vm/instruction_live.c"
click node_death "https://github.com/thomas-migneret/corewar/blob/main/src/vm/death_checks.c"
click node_winner "https://github.com/thomas-migneret/corewar/blob/main/src/vm/winner.c"
click node_dump "https://github.com/thomas-migneret/corewar/blob/main/src/vm/dump_arena.c"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_cli,node_binary,node_formats toneBlue
class node_entry,node_vmsetup,node_champions,node_arena,node_processes,node_scheduler,node_vmrun toneAmber
class node_decode,node_validation,node_dispatch,node_instruction_families toneMint
class node_live,node_death,node_winner,node_dump toneRose
class node_player,node_operator toneIndigo
```

## Components

### Inputs and loading

- [CLI options](https://github.com/thomas-migneret/corewar/blob/main/src/parcing/recup_arg.c): `src/parcing/recup_arg.c`
- [Champion loader](https://github.com/thomas-migneret/corewar/blob/main/src/parcing/open_file.c): `src/parcing/open_file.c`
- [Opcode and format definitions](https://github.com/thomas-migneret/corewar/blob/main/include/op.h): `include/op.h`

### VM runtime

- [Corewar entry](https://github.com/thomas-migneret/corewar/blob/main/src/main.c): `src/main.c`
- [VM initialization](https://github.com/thomas-migneret/corewar/blob/main/src/vm/init_vm.c): `src/vm/init_vm.c`
- [Champion placement](https://github.com/thomas-migneret/corewar/blob/main/src/vm/load_champions.c): `src/vm/load_champions.c`
- [Circular arena](https://github.com/thomas-migneret/corewar/blob/main/src/vm/memory.c): `src/vm/memory.c`
- [Process state](https://github.com/thomas-migneret/corewar/blob/main/src/vm/init_processes.c): `src/vm/init_processes.c`
- [Cycle scheduler](https://github.com/thomas-migneret/corewar/blob/main/src/vm/run_cycle.c): `src/vm/run_cycle.c`
- [Simulation loop](https://github.com/thomas-migneret/corewar/blob/main/src/vm/run_vm.c): `src/vm/run_vm.c`

### Instruction execution

- [Opcode decoding](https://github.com/thomas-migneret/corewar/blob/main/src/vm/decode.c): `src/vm/decode.c`
- [Argument validation](https://github.com/thomas-migneret/corewar/blob/main/src/vm/decode_validation.c): `src/vm/decode_validation.c`
- [Instruction dispatcher](https://github.com/thomas-migneret/corewar/blob/main/src/vm/execute_instruction.c): `src/vm/execute_instruction.c`
- [Instruction implementations](https://github.com/thomas-migneret/corewar/tree/main/src/vm): `src/vm`

### Game outcomes

- [Live tracking](https://github.com/thomas-migneret/corewar/blob/main/src/vm/instruction_live.c): `src/vm/instruction_live.c`
- [Process death checks](https://github.com/thomas-migneret/corewar/blob/main/src/vm/death_checks.c): `src/vm/death_checks.c`
- [Winner announcement](https://github.com/thomas-migneret/corewar/blob/main/src/vm/winner.c): `src/vm/winner.c`
- [Arena dump](https://github.com/thomas-migneret/corewar/blob/main/src/vm/dump_arena.c): `src/vm/dump_arena.c`

### Other components

- **Game launcher**
- **Game operator**

## Connections

- Game launcher → Corewar entry: starts game (evidence: `src/main.c`)
- Corewar entry → CLI options: parses options (evidence: `src/main.c`)
- Corewar entry → Champion loader: loads files (evidence: `src/main.c`)
- Corewar entry → VM initialization: initializes VM (evidence: `src/main.c`)
- Corewar entry → Simulation loop: runs simulation (evidence: `src/main.c`)
- CLI options → Champion loader: selects champions
- Champion loader → Opcode and format definitions: reads binary layout (evidence: `src/parcing/open_file.c`)
- VM initialization → Champion placement: places champions
- Champion placement → Circular arena: loads code
- VM initialization → Process state: creates processes
- Simulation loop → Cycle scheduler: advances cycles
- Cycle scheduler → Opcode decoding: decodes instructions
- Opcode decoding → Opcode and format definitions: uses opcode table (evidence: `src/vm/decode.c`)
- Opcode decoding → Argument validation: validates arguments
- Cycle scheduler → Instruction dispatcher: executes instruction
- Instruction dispatcher → Instruction implementations: dispatches opcode (evidence: `src/vm/execute_instruction.c`)
- Instruction implementations → Circular arena: reads and writes
- Instruction implementations → Live tracking: runs live opcode
- Cycle scheduler → Process death checks: checks survival
- Process death checks → Process state: removes inactive
- Simulation loop → Winner announcement: reports winner
- Game operator → Arena dump: requests dump
- Arena dump → Circular arena: prints arena state
