How procub3r/riscv-emu works
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.