# kishore-ganesh/nesemulator architecture

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

- Interactive diagram: https://gitdiagram.com/kishore-ganesh/nesemulator
- Repository: https://github.com/kishore-ganesh/nesemulator

## Overview

##### Memory and Bus System
- Following memory components:
- 2KB RAM block
- Memory Bus (shared between CPU and PPU)
- PPU Memory (VRAM, Pattern Tables, Name Tables)
- Memory-mapped registers for component communication

##### Input/Output Systems
- On the left side, show:
- Controller Input component
- SDL Output Handler (for video rendering)
- Audio Output Handler

##### Component Interactions
- Double lines: Bus connections
- Blue: Data flow
- Red: Control signals
- Green: Memory access
- Yellow: I/O operations

##### Detailed Component Breakdowns

CPU Section:
- Instruction decoder
- Program counter
- Interrupt handler
- Cycle counter

PPU Section:
- Pattern table access
- Name table handling
- Attribute table
- Sprite rendering pipeline
- Scrolling mechanism (x/y scroll registers)

APU Section:
- 3 implemented channels (2 pulse, 1 triangle)
- Audio sampling system

##### Cartridge and Mapper
- Cartridge interface
- UNROM mapper implementation
- ROM/RAM regions

##### Timing and Synchronization
- 3:1 PPU:CPU cycle ratio
- Synchronization points between components

##### Cycle-accurate nature of the emulation
- Cycle counting mechanisms
- How components stay synchronized

##### Memory Mapping
- Detail the memory-mapped registers (PPUCTRL, PPUMASK, PPUSTATUS, etc.)
- How different components access shared memory spaces

##### Rendering Pipeline
- Pattern table to pixel conversion process
- Demonstrate the attribute table's role

##### State Management
- How the system maintains and updates component states
- Interrupt handling paths

Remember to include version information and any implementation-specific details that might differ from the original NES hardware, such as the audio resampling system that's noted as needing improvement.

## Diagram (Mermaid)

```mermaid
flowchart TB
    subgraph IO["Input/Output Layer"]
        CI["Controller Input"]
        SDL["SDL Output Handler"]
        AO["Audio Output"]
    end

    subgraph CPU["CPU Layer"]
        direction TB
        CPU6502["6502 CPU"]:::cpu
        ID["Instruction Decoder"]
        PC["Program Counter"]
        IH["Interrupt Handler"]
        CC["Cycle Counter"]
    end

    subgraph PPU["PPU Layer"]
        direction TB
        PPUCore["Picture Processing Unit"]:::ppu
        PT["Pattern Tables"]
        NT["Name Tables"]
        AT["Attribute Tables"]
        SR["Sprite Renderer"]
        SC["Scroll Registers"]
    end

    subgraph APU["APU Layer"]
        direction TB
        APUCore["Audio Processing Unit"]:::apu
        P1["Pulse Channel 1"]
        P2["Pulse Channel 2"]
        TR["Triangle Channel"]
        AS["Audio Sampler"]
    end

    subgraph MEM["Memory System"]
        direction TB
        RAM["2KB RAM"]
        VRAM["Video RAM"]
        MMR["Memory-Mapped Registers"]
        MB["Memory Bus"]:::memory
    end

    subgraph CART["Cartridge System"]
        direction TB
        CI2["Cartridge Interface"]:::cartridge
        UNROM["UNROM Mapper"]:::mapper
        ROM["ROM Region"]
        CRAM["Cartridge RAM"]
    end

    %% Component Connections
    CI --> CPU6502
    CPU6502 <--> MB
    PPUCore <--> MB
    APUCore <--> MB
    MB <--> RAM
    MB <--> MMR
    PPUCore <--> VRAM
    PPUCore --> SDL
    APUCore --> AO
    CI2 <--> MB
    UNROM <--> CI2

    %% CPU Internal Connections
    CPU6502 --- ID
    CPU6502 --- PC
    CPU6502 --- IH
    CPU6502 --- CC

    %% PPU Internal Connections
    PPUCore --- PT
    PPUCore --- NT
    PPUCore --- AT
    PPUCore --- SR
    PPUCore --- SC

    %% APU Internal Connections
    APUCore --- P1
    APUCore --- P2
    APUCore --- TR
    APUCore --- AS

    %% Cartridge Internal Connections
    CI2 --- ROM
    CI2 --- CRAM
    UNROM --- ROM

    %% Click Events
    click CPU6502 "https://github.com/kishore-ganesh/NESEmulator/blob/master/include/cpu.h"
    click PPUCore "https://github.com/kishore-ganesh/NESEmulator/blob/master/include/ppu.h"
    click APUCore "https://github.com/kishore-ganesh/NESEmulator/blob/master/include/apu.h"
    click MB "https://github.com/kishore-ganesh/NESEmulator/blob/master/include/memory.h"
    click CI2 "https://github.com/kishore-ganesh/NESEmulator/blob/master/include/cartridge.h"
    click UNROM "https://github.com/kishore-ganesh/NESEmulator/blob/master/include/unrom_mapper.h"
    click CI "https://github.com/kishore-ganesh/NESEmulator/blob/master/include/controller.h"
    click SDL "https://github.com/kishore-ganesh/NESEmulator/blob/master/include/sdl_handler.h"
    click IH "https://github.com/kishore-ganesh/NESEmulator/blob/master/include/interrupt.h"

    %% Styling
    classDef cpu fill:#f96
    classDef ppu fill:#69f
    classDef apu fill:#9f6
    classDef memory fill:#ff9
    classDef cartridge fill:#f9f
    classDef mapper fill:#9ff

    %% Legend
    subgraph Legend
        L1["CPU Components"]:::cpu
        L2["PPU Components"]:::ppu
        L3["APU Components"]:::apu
        L4["Memory Components"]:::memory
        L5["Cartridge Components"]:::cartridge
        L6["Mapper Components"]:::mapper
    end
```
