# ewriq/evm architecture

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

- Interactive diagram: https://gitdiagram.com/ewriq/evm
- Repository: https://github.com/ewriq/evm

## Overview

EVM is an experimental CPU simulator. Its documented workflow is assembly source through assembler and binary loader into CPU execution. The graph includes instruction parsing, label resolution, binary encoding and format handling, plus CPU logic and storage stages documented in the README and visible in sampled execution code. Source excerpts establish the principal entry-point and execution relationships; unsampled internals are represented only where the tree and README support the subsystem, without asserting unverified calls.

Main entry points: [`app/internal/cpu/execute.go`](https://github.com/ewriq/evm/blob/main/app/internal/cpu/execute.go) (Instruction Execution), [`app/cmd/asm/main.go`](https://github.com/ewriq/evm/blob/main/app/cmd/asm/main.go) (Assembler and Labels), [`app/cmd/vm/main.go`](https://github.com/ewriq/evm/blob/main/app/cmd/vm/main.go) (VM Entry Point), [`app/internal/cpu/run.go`](https://github.com/ewriq/evm/blob/main/app/internal/cpu/run.go) (CPU Runtime)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_assembly["Assembly and Encoding"]
  node_assembly_source["Assembly Source<br/>[a.vm]"]
  node_assembler["Assembler and Labels<br/>[main.go]"]
  node_instruction_parser["Instruction Parser<br/>[parser.go]"]
  node_encoder["Instruction Encoder<br/>[encode.go]"]
end

subgraph group_binary["Binary Format and Loading"]
  node_binary_format["Binary Header<br/>[header.go]"]
  node_binary_output["Executable Binary<br/>[a.bin]"]
  node_binary_loader["Binary Loader<br/>[load.go]"]
  node_vm_entry["VM Entry Point<br/>[main.go]"]
end

subgraph group_execution["CPU Execution"]
  node_instruction_set["Opcode System<br/>[struct.go]"]
  node_decoder["Instruction Decoder<br/>[decode.go]"]
  node_cpu["CPU Runtime<br/>[run.go]"]
  node_control["Control Unit<br/>[control.go]"]
  node_execution_unit["Instruction Execution<br/>[execute.go]"]
  node_alu["Arithmetic Logic<br/>[alu.go]"]
end

subgraph group_storage["CPU Storage"]
  node_registers["Register File<br/>[controller.go]"]
  node_memory[("Memory<br/>[memory.go]")]
  node_memory_interface["Memory Access"]
  node_stack["Stack Operations<br/>[execute.go]"]
  node_call_stack["Call Stack<br/>[execute.go]"]
end

node_programmer(("Programmer"))

node_programmer -->|"writes"| node_assembly_source
node_assembly_source -->|"assembles"| node_assembler
node_assembler -->|"parses"| node_instruction_parser
node_assembler -->|"encodes"| node_encoder
node_assembler -->|"builds header"| node_binary_format
node_assembler -->|"saves"| node_binary_output
node_instruction_parser -->|"creates instructions"| node_instruction_set
node_encoder -.->|"encodes instructions"| node_instruction_set
node_binary_output -->|"runs"| node_vm_entry
node_vm_entry -->|"loads binary"| node_binary_loader
node_vm_entry -->|"validates header"| node_binary_format
node_vm_entry -->|"loads code"| node_cpu
node_vm_entry -->|"creates memory"| node_memory
node_cpu -.->|"fetches instructions"| node_decoder
node_cpu -.->|"coordinates"| node_control
node_control -.->|"dispatches"| node_execution_unit
node_execution_unit -->|"calculates"| node_alu
node_execution_unit -->|"reads and writes"| node_registers
node_execution_unit -->|"reads and writes"| node_memory_interface
node_memory_interface -->|"accesses"| node_memory
node_execution_unit -->|"handles stack"| node_stack
node_execution_unit -->|"handles calls"| node_call_stack

click node_assembly_source "https://github.com/ewriq/evm/blob/main/app/a.vm"
click node_assembler "https://github.com/ewriq/evm/blob/main/app/cmd/asm/main.go"
click node_instruction_parser "https://github.com/ewriq/evm/blob/main/app/internal/loader/parser.go"
click node_instruction_set "https://github.com/ewriq/evm/blob/main/app/internal/opcode/struct.go"
click node_encoder "https://github.com/ewriq/evm/blob/main/app/internal/crypto/encode.go"
click node_binary_format "https://github.com/ewriq/evm/blob/main/app/internal/loader/header.go"
click node_binary_output "https://github.com/ewriq/evm/blob/main/app/a.bin"
click node_binary_loader "https://github.com/ewriq/evm/blob/main/app/internal/loader/load.go"
click node_vm_entry "https://github.com/ewriq/evm/blob/main/app/cmd/vm/main.go"
click node_decoder "https://github.com/ewriq/evm/blob/main/app/internal/crypto/decode.go"
click node_cpu "https://github.com/ewriq/evm/blob/main/app/internal/cpu/run.go"
click node_control "https://github.com/ewriq/evm/blob/main/app/internal/cpu/control.go"
click node_execution_unit "https://github.com/ewriq/evm/blob/main/app/internal/cpu/execute.go"
click node_alu "https://github.com/ewriq/evm/blob/main/app/internal/cpu/alu.go"
click node_registers "https://github.com/ewriq/evm/blob/main/app/internal/register/controller.go"
click node_memory "https://github.com/ewriq/evm/blob/main/app/internal/memory/memory.go"
click node_memory_interface "https://github.com/ewriq/evm/blob/main/app/internal/memory/memory_interface.go"
click node_stack "https://github.com/ewriq/evm/blob/main/app/internal/cpu/execute.go"
click node_call_stack "https://github.com/ewriq/evm/blob/main/app/internal/cpu/execute.go"

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_assembly_source,node_assembler,node_instruction_parser,node_encoder toneBlue
class node_binary_format,node_binary_output,node_binary_loader,node_vm_entry toneAmber
class node_instruction_set,node_decoder,node_cpu,node_control,node_execution_unit,node_alu toneMint
class node_registers,node_memory,node_memory_interface,node_stack,node_call_stack toneRose
class node_programmer toneIndigo
```

## Components

### Assembly and Encoding

- [Assembly Source](https://github.com/ewriq/evm/blob/main/app/a.vm): `app/a.vm`
- [Assembler and Labels](https://github.com/ewriq/evm/blob/main/app/cmd/asm/main.go): `app/cmd/asm/main.go`
- [Instruction Parser](https://github.com/ewriq/evm/blob/main/app/internal/loader/parser.go): `app/internal/loader/parser.go`
- [Instruction Encoder](https://github.com/ewriq/evm/blob/main/app/internal/crypto/encode.go): `app/internal/crypto/encode.go`

### Binary Format and Loading

- [Binary Header](https://github.com/ewriq/evm/blob/main/app/internal/loader/header.go): `app/internal/loader/header.go`
- [Executable Binary](https://github.com/ewriq/evm/blob/main/app/a.bin): `app/a.bin`
- [Binary Loader](https://github.com/ewriq/evm/blob/main/app/internal/loader/load.go): `app/internal/loader/load.go`
- [VM Entry Point](https://github.com/ewriq/evm/blob/main/app/cmd/vm/main.go): `app/cmd/vm/main.go`

### CPU Execution

- [Opcode System](https://github.com/ewriq/evm/blob/main/app/internal/opcode/struct.go): `app/internal/opcode/struct.go`
- [Instruction Decoder](https://github.com/ewriq/evm/blob/main/app/internal/crypto/decode.go): `app/internal/crypto/decode.go`
- [CPU Runtime](https://github.com/ewriq/evm/blob/main/app/internal/cpu/run.go): `app/internal/cpu/run.go`
- [Control Unit](https://github.com/ewriq/evm/blob/main/app/internal/cpu/control.go): `app/internal/cpu/control.go`
- [Instruction Execution](https://github.com/ewriq/evm/blob/main/app/internal/cpu/execute.go): `app/internal/cpu/execute.go`
- [Arithmetic Logic](https://github.com/ewriq/evm/blob/main/app/internal/cpu/alu.go): `app/internal/cpu/alu.go`

### CPU Storage

- [Register File](https://github.com/ewriq/evm/blob/main/app/internal/register/controller.go): `app/internal/register/controller.go`
- [Memory](https://github.com/ewriq/evm/blob/main/app/internal/memory/memory.go): `app/internal/memory/memory.go`
- [Memory Access](https://github.com/ewriq/evm/blob/main/app/internal/memory/memory_interface.go): `app/internal/memory/memory_interface.go`
- [Stack Operations](https://github.com/ewriq/evm/blob/main/app/internal/cpu/execute.go): `app/internal/cpu/execute.go`
- [Call Stack](https://github.com/ewriq/evm/blob/main/app/internal/cpu/execute.go): `app/internal/cpu/execute.go`

### Other components

- **Programmer**

## Connections

- Programmer → Assembly Source: writes (evidence: `README.md`)
- Assembly Source → Assembler and Labels: assembles (evidence: `README.md`)
- Assembler and Labels → Instruction Parser: parses (evidence: `app/cmd/asm/main.go`)
- Assembler and Labels → Instruction Encoder: encodes (evidence: `app/cmd/asm/main.go`)
- Assembler and Labels → Binary Header: builds header (evidence: `app/cmd/asm/main.go`)
- Assembler and Labels → Executable Binary: saves (evidence: `app/cmd/asm/main.go`)
- Instruction Parser → Opcode System: creates instructions (evidence: `app/internal/loader/parser.go`)
- Instruction Encoder → Opcode System: encodes instructions
- Executable Binary → VM Entry Point: runs (evidence: `README.md`)
- VM Entry Point → Binary Loader: loads binary (evidence: `app/cmd/vm/main.go`)
- VM Entry Point → Binary Header: validates header (evidence: `app/cmd/vm/main.go`)
- VM Entry Point → CPU Runtime: loads code (evidence: `app/cmd/vm/main.go`)
- VM Entry Point → Memory: creates memory (evidence: `app/cmd/vm/main.go`)
- CPU Runtime → Instruction Decoder: fetches instructions
- CPU Runtime → Control Unit: coordinates
- Control Unit → Instruction Execution: dispatches
- Instruction Execution → Arithmetic Logic: calculates (evidence: `app/internal/cpu/execute.go`)
- Instruction Execution → Register File: reads and writes (evidence: `app/internal/cpu/execute.go`)
- Instruction Execution → Memory Access: reads and writes (evidence: `app/internal/cpu/execute.go`)
- Memory Access → Memory: accesses (evidence: `app/internal/memory/memory_interface.go`)
- Instruction Execution → Stack Operations: handles stack (evidence: `app/internal/cpu/execute.go`)
- Instruction Execution → Call Stack: handles calls (evidence: `app/internal/cpu/execute.go`)
