# aidanaden/chip8-zig architecture

> Architecture diagram of the GitHub repository aidanaden/chip8-zig, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-01-11). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/aidanaden/chip8-zig
- Repository: https://github.com/aidanaden/chip8-zig

## Overview

##### Main Components

a. Input Layer:
- ROM Loader (handling files from the /roms directory)
- Keyboard Input Handler

b. Core Emulator Components:
- CPU/Interpreter (represented by chip8.zig)
- Memory System
- /Graphics System
- Opcode Handler (represented by opcode.zig)
- Timer System

c. Output Layer:
- Renderer
- (Future) Audio System

##### Component Relationships

**[ROM Files] → [ROM Loader] → [Memory System] ↔ [CPU/Interpreter] → [Display Renderer]**
- ↑               ↑
- [Opcode Handler] ← [Input Handler]

##### Specific Implementation Details

- Rectangles: Core processing units (CPU, Opcode Handler)
- Rounded rectangles: I/O components (Display, Input Handler)

- Blue: Core emulation components
- Green: Input systems
- Orange: Output systems
- Gray: Storage/Memory systems

##### Technical Specifications

- Memory: Show the standard CHIP-8 4KB (4,096 bytes) memory space
- 64x32 pixel monochrome display
- Registers: 16 8-bit registers (V0 to VF)
- Stack: 16 16-bit values
- Timers: Delay and sound timers

- Audio System (mentioned in roadmap)
- WebAssembly interface
- Multi-threading support

##### Implementation Notes

Annotations for:
- The relationship between opcode.zig and chip8.zig
- The main execution loop in main.zig
- Memory layout and addressing
- Timer frequencies (60Hz)

## Diagram (Mermaid)

```mermaid
graph TB
    %% Define styles
    classDef core fill:#2196F3,stroke:#1565C0,color:white
    classDef input fill:#4CAF50,stroke:#388E3C,color:white
    classDef output fill:#FF9800,stroke:#F57C00,color:white
    classDef storage fill:#9E9E9E,stroke:#616161,color:white
    classDef future stroke-dasharray: 5 5

    %% Input Layer
    subgraph InputLayer
        ROM[("ROM Storage")]:::storage
        TestROM[("Test ROM")]:::storage
        InputHandler["Keyboard Input Handler"]:::input
    end

    %% Core Emulator
    subgraph CoreEmulator
        CPU["CPU/Interpreter"]:::core
        Memory[("Memory System\n4KB")]:::storage
        OpcodeHandler["Opcode Handler"]:::core
        TimerSystem["Timer System\n(60Hz)"]:::core
    end

    %% Output Layer
    subgraph OutputLayer
        DisplayRenderer["Display Renderer\n64x32 Monochrome"]:::output
        AudioSystem["Audio System"]:::future
    end

    %% Future Components
    subgraph FutureComponents
        WASM["WebAssembly Interface"]:::future
        MultiThread["Multi-Threading Support"]:::future
    end

    %% Relationships
    ROM --> Memory
    TestROM --> Memory
    Memory <--> CPU
    CPU --> DisplayRenderer
    InputHandler --> OpcodeHandler
    OpcodeHandler --> CPU
    TimerSystem --> CPU
    CPU -.-> AudioSystem
    CPU -.-> WASM
    CPU -.-> MultiThread

    %% Register Info
    subgraph Specifications
        Registers["16 8-bit Registers\nV0 to VF"]
        Stack["Stack:\n16 16-bit values"]
    end

    %% Click events
    click ROM "https://github.com/aidanaden/chip8-zig/tree/main/roms/"
    click TestROM "https://github.com/aidanaden/chip8-zig/blob/main/roms/test_opcode.ch8"
    click CPU "https://github.com/aidanaden/chip8-zig/blob/main/src/chip8.zig"
    click OpcodeHandler "https://github.com/aidanaden/chip8-zig/blob/main/src/opcode.zig"
    click InputHandler "https://github.com/aidanaden/chip8-zig/blob/main/src/main.zig"

    %% Legend
    subgraph Legend
        L1["Core Components"]:::core
        L2["Input Components"]:::input
        L3["Output Components"]:::output
        L4["Storage"]:::storage
        L5["Future Features"]:::future
    end
```
