# fjpolo/ostang architecture

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

- Interactive diagram: https://gitdiagram.com/fjpolo/ostang
- Repository: https://github.com/fjpolo/ostang

## Overview

##### Main Components
- FPGA/SoC “OSTang Top” Module:
- Top-level hardware integration (likely represented by src/ostang_top.sv)
- It brings together the various hardware blocks.
- CPU Core:
- Responsible for running the baremetal firmware (firmware/firmware.c, firmware.elf).
- Firmware and Baremetal Environment:
- Contains makefiles, build scripts (buildall.bat, firmware/build.bat/sh) and binary outputs (firmware.bin, firmware.elf)
- Integrated FAT file system support (firmware/fatfs directories give diskio, ff.c, ff.h, etc.)
- Video and Audio Modules:
- HDMI interface modules under src/hdmi2 (HDMI transmitter, audio packets, serializers, etc.)
- PLL components for HDMI (e.g., gowin_pll_hdmi.v in both nano20k and primer25k directories)
- Input/Controller Interfaces:
- Multiple controller modules including NES, SNES, DualShock, and USB gamepad support (files under src/usb_gamepad, src/controller_ds2.sv, src/controller_snes.v)
- Additional legacy or specialized controllers (look for ostang_snescontroller.cst and related files).
- System I/O and Configuration:
- Modules under src/iosys (e.g., simpleuart, spi_master, textdisp) for peripheral management and system interfacing.
- Gowin DPB menu (configuration) components in src/iosys.
- Clock and PLL Blocks:
- Specific PLL configurations are given in the nano20k and primer25k directories – these set up clock domains for HDMI, OS controllers, and USB.
- Memory and Storage:
- There is mention of SDRAM (src/sdram_nes.v), as well as EEPROM and potential SD card access (spi_sd.c in firmware)
- FATFS integration in the firmware is likely used to manage file systems on SD cards or external flash.
- Additional Utility Components:
- Assets (graphics, fonts, and test patterns) for video display.
- UART modules (a debug interface), and USB HID host implementations (src/usb_hid_host.v, related ROM files).

- A central FPGA/SoC block (“OSTang Top”) that connects to the CPU core (picorv32) and the rest of the hardware IP blocks.
- Dedicated sub-blocks for HDMI/audio processing, each with inputs/outputs (data streams, clock signals) coming from the PLL modules.
- Control path from input devices (USB gamepad, legacy controllers, etc.) flowing into dedicated controller interface blocks feeding data into the main processing system.
- Memory interfaces (SDRAM controller, EEPROM access, SPI for SD card and SPI flash) that link to both the CPU and peripheral modules.
- Separate design flows/configurations available (nano20k versus primer25k) by optionally annotating multiple configuration blocks that feed precise clock and power domains into the system.

##### Important Architectural Patterns and Principles
- Modularity: The system is organized into clear, function-specific modules (video processing, input control, memory, I/O) which suggests a highly modular design to ease testing and integration.
- Separation of concerns: The firmware layer (baremetal C and assembly) is kept distinct from the Hardware RTL, which itself is broken into logical blocks (e.g., clocking, data processing, interface).
- Use of standard libraries: Incorporation of FATFS for filesystem support in the firmware and reuse of open-source cores like picorv32 indicates a lean, reusable architecture.
- Multiple target configurations: With directories for nano20k and primer25k, the design supports variations based on board or FPGA family (likely differing in resources or performance).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% FPGA/SoC Block
    subgraph "FPGA/SoC"
        OST["FPGA: OSTang Top"]:::hardware
    end

    %% CPU and Firmware Block
    subgraph "CPU & Firmware"
        CPU_HW["CPU: picorv32 (Hardware)"]:::firmwareHardware
        CPU_FW["CPU: picorv32 (Firmware)"]:::firmwareSoftware
        FW["Firmware & Baremetal"]:::firmwareSoftware
        FAT["FATFS Support"]:::firmwareSoftware
        SPI["SD/Flash SPI Interface"]:::firmwareSoftware
    end

    %% HDMI & Audio Block
    subgraph "HDMI & Audio"
        HDMI["HDMI Core Modules"]:::hardware
        HDMI_PLL_N["nano20k HDMI PLL"]:::hardware
        HDMI_PLL_P["primer25k HDMI PLL"]:::hardware
    end

    %% Input/Controller Block
    subgraph "Input & Controllers"
        USBG["USB Gamepad"]:::interface
        DS["DualShock Controller"]:::interface
        SNES["SNES Controller"]:::interface
    end

    %% System I/O and Configuration Block
    subgraph "System I/O & Config"
        IOSYS["I/O System Modules"]:::hardware
        UART["UART"]:::hardware
    end

    %% Clock and PLL Block
    subgraph "Clock & PLL"
        PLL_OS_N["nano20k OS/USB PLL"]:::clock
        PLL_OS_P["primer25k OS/USB PLL"]:::clock
        PLL_P27["primer25k Additional PLL"]:::clock
    end

    %% Memory and Storage Block
    subgraph "Memory & Storage"
        SDRAM["SDRAM Support"]:::memory
        EEPROM["EEPROM Interface"]:::memory
    end

    %% Additional Utility Components
    subgraph "Utilities"
        ASSETS["Assets (Graphics/Fonts/Test Patterns)"]:::utility
        USB_HID["USB HID Host"]:::utility
    end

    %% Connections from FPGA Top
    OST -->|"integrates"| CPU_HW
    OST -->|"integrates"| HDMI
    OST -->|"manages"| IOSYS
    OST -->|"interfaces with"| SDRAM
    OST -->|"interfaces with"| EEPROM
    OST -->|"bridges"| USB_HID

    %% CPU Core and Firmware Relationships
    CPU_HW -->|"drives"| CPU_FW
    CPU_FW -->|"runs"| FW
    FW -->|"uses"| FAT
    FW -->|"accesses"| SPI
    CPU_FW -->|"communicates via"| IOSYS

    %% HDMI & Audio Connections
    HDMI_PLL_N -->|"clocks"| HDMI
    HDMI_PLL_P -->|"clocks"| HDMI

    %% Input Controllers Connections
    USBG -->|"feeds data to"| IOSYS
    DS -->|"feeds data to"| IOSYS
    SNES -->|"feeds data to"| IOSYS

    %% Clock & PLL to System I/O
    PLL_OS_N -->|"provides clock"| IOSYS
    PLL_OS_P -->|"provides clock"| IOSYS
    PLL_P27 -->|"supplements clock"| IOSYS

    %% Styles
    classDef hardware fill:#FFCCCC,stroke:#800000,stroke-width:2px;
    classDef firmwareSoftware fill:#CCFFCC,stroke:#008000,stroke-width:2px;
    classDef firmwareHardware fill:#E0F7FA,stroke:#006064,stroke-width:2px;
    classDef interface fill:#CCCCFF,stroke:#000080,stroke-width:2px;
    classDef clock fill:#FFFFCC,stroke:#808000,stroke-width:2px;
    classDef memory fill:#CCFFFF,stroke:#008080,stroke-width:2px;
    classDef utility fill:#FFCCFF,stroke:#800080,stroke-width:2px;

    %% Click Events
    click OST "https://github.com/fjpolo/ostang/blob/master/src/ostang_top.sv"
    click CPU_HW "https://github.com/fjpolo/ostang/blob/master/src/iosys/picorv32.v"
    click CPU_FW "https://github.com/fjpolo/ostang/blob/master/firmware/picorv32.c"
    click FW "https://github.com/fjpolo/ostang/blob/master/firmware/firmware.c"
    click FAT "https://github.com/fjpolo/ostang/tree/master/firmware/fatfs/"
    click SPI "https://github.com/fjpolo/ostang/blob/master/firmware/spi_sd.c"
    click HDMI "https://github.com/fjpolo/ostang/blob/master/src/hdmi2/hdmi.sv"
    click HDMI_PLL_N "https://github.com/fjpolo/ostang/blob/master/src/nano20k/gowin_pll_hdmi.v"
    click HDMI_PLL_P "https://github.com/fjpolo/ostang/blob/master/src/primer25k/gowin_pll_hdmi.v"
    click USBG "https://github.com/fjpolo/ostang/tree/master/src/usb_gamepad/"
    click DS "https://github.com/fjpolo/ostang/blob/master/src/controller_ds2.sv"
    click SNES "https://github.com/fjpolo/ostang/blob/master/src/controller_snes.v"
    click IOSYS "https://github.com/fjpolo/ostang/blob/master/src/iosys/iosys.v"
    click UART "https://github.com/fjpolo/ostang/blob/master/src/hw_uart.v"
    click PLL_OS_N "https://github.com/fjpolo/ostang/blob/master/src/nano20k/gowin_pll_os.v"
    click PLL_OS_P "https://github.com/fjpolo/ostang/blob/master/src/primer25k/gowin_pll_os.v"
    click PLL_P27 "https://github.com/fjpolo/ostang/blob/master/primer25k/gowin_pll_27.v"
    click SDRAM "https://github.com/fjpolo/ostang/blob/master/src/sdram_nes.v"
    click EEPROM "https://github.com/fjpolo/ostang/blob/master/src/EEPROM_24C0x.sv"
    click ASSETS "https://github.com/fjpolo/ostang/tree/master/src/assets/"
    click USB_HID "https://github.com/fjpolo/ostang/blob/master/src/usb_hid_host.v"
```
