# procub3r/riscv-emu architecture

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

- Interactive diagram: https://gitdiagram.com/procub3r/riscv-emu
- Repository: https://github.com/procub3r/riscv-emu

## Overview

##### Project Type and Purpose
- This project is a standalone emulator for the RISC-V CPU architecture.
- Its purpose is to simulate RISC-V instructions, allowing testing (using the riscv-tests submodule) and emulation. It targets both educational and development use, showing how the CPU core behaves under different instructions and environments.

##### File Structure
- Top-level files:
- build.zig – Indicates a Zig-based build system.
- README.md – Provides information about dependencies and usage.
- The src/ directory contains the main source code:
- Core.zig: Likely implements the core CPU emulator logic (fetch-decode-execute cycle, registers, etc.).
- EEI.zig: Implements an External Environment Interface which could be used to simulate system calls, input/output, or interfacing with the test environment.
- TestEnv.zig: Defines a test execution environment that helps simulate or run RISC-V tests.
- main.zig: The entry point for bootstrapping the emulator.
- The deps/ directory includes a submodule (riscv-tests) used for testing the emulator’s behavior against official or reference tests.

##### Key Components to Capture
a. Core Emulator Engine:
- The "Core" component (from Core.zig) representing the CPU emulation logic is central.

b. External Interface:
- The "EEI" component (from EEI.zig) that enables integration for external operations, potentially handling emulated system calls.

c. Test Environment:
- The "TestEnv" component (from TestEnv.zig) that sets up a controlled environment for running tests, indicating clear separation between core emulation and testing harness.

d. Main Application Flow:
- The main execution starting point (main.zig) that ties together initialization, invocation of the CPU core, and integration of the EEI and TestEnv.

e. Dependencies and External Tools:
- A dependency on riscv-tests in the deps/ directory details the relationship with external test suites.

##### Relationships and Interactions
- The main driver (main.zig) initializes the system, creating instances of Core, EEI, and TestEnv.
- The Core component handles instruction execution, where it may call routines exposed by EEI to simulate interactions with external hardware or system calls.
- TestEnv sets up the testing environment, allowing the core to run predefined instruction sequences as defined in riscv-tests.
- The build system (build.zig) ties in the compilation and testing steps, ensuring that the emulator is tested against the provided test-suite.

##### Architectural Patterns and Design Principles
- The project seems to follow a modular design. Each major functionality (CPU emulation, external interfacing, testing) is separated into its own module.
- Emphasis is on a clean separation of concerns—a central loop (in Core) is abstracted from peripheral systems (EEI and TestEnv).
- The architecture supports extensibility; for example, expanding the EEI to simulate more system-level operations or enhancing the core to support more instructions (e.g., eventually fully implementing the RV32I spec).

- Notate the build process by mentioning build.zig as the orchestrator that compiles, runs tests, and maintains the overall flow.

##### Final Instructive Points
- Component is clearly labeled and that the flow of data/instructions is directional.
- Avoid over-cluttering, but ensure that every strategic component (Core, EEI, TestEnv, Main Entry Point, and External Dependencies) is represented.

## Diagram (Mermaid)

```mermaid
flowchart TD
    E["Build System"]:::build
    A["Main Entry Point"]:::module
    B["Emulator Core"]:::module
    C["External Environment Interface (EEI)"]:::module
    D["Test Environment"]:::module
    F(("External Dependency (riscv-tests)")):::external

    E --> A
    A --> B
    B --> C
    B --> D
    F --> D

    click A "https://github.com/procub3r/riscv-emu/blob/main/src/main.zig"
    click B "https://github.com/procub3r/riscv-emu/blob/main/src/Core.zig"
    click C "https://github.com/procub3r/riscv-emu/blob/main/src/EEI.zig"
    click D "https://github.com/procub3r/riscv-emu/blob/main/src/TestEnv.zig"
    click E "https://github.com/procub3r/riscv-emu/blob/main/build.zig"
    click F "https://github.com/procub3r/riscv-emu/tree/main/deps/riscv-tests"

    classDef module fill:#bbf,stroke:#333,stroke-width:2px;
    classDef build fill:#f96,stroke:#333,stroke-width:2px;
    classDef external fill:#cfc,stroke:#333,stroke-width:2px;
```
