# janestreet/hardcaml architecture

> Architecture diagram of the GitHub repository janestreet/hardcaml, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2024-12-27). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/janestreet/hardcaml
- Repository: https://github.com/janestreet/hardcaml

## Overview

##### Main Components

a) Core Library Components:
- Circuit Generation Engine
- Simulation Engine (Cyclesim)
- RTL Generation Backend (Verilog/VHDL)
- Bits Operations System
- Interface System
- Always API (for sequential logic)
- Property/Assertion System

b) Integration Components:
- External Tools Interface
- Verification Systems
- Waveform Generation
- Test Infrastructure

**┌─────────────────────────────────────────────────────────┐**
- User Design Layer                      │
- (OCaml code using Hardcaml primitives and interfaces)  │
- ┤
- Hardcaml Core                         │
- ┬──────────────┬───────────┬──────────────┤
- Circuit     │  Simulation  │    RTL    │  Interface   │
- Generation  │    Engine    │ Generation│    System    │
- ┴──────────────┴───────────┴──────────────┤
- Backend Targets                       │
- (Verilog, VHDL, C, Verilator, etc.)            │
- ┘

##### Key Relationships

- Bidirectional flow between User Design and Hardcaml Core
- Transformation paths from Circuit Generation to various backends
- Interaction between Simulation Engine and Waveform Generation
- How the Interface System connects with all major components

##### Specific Implementation Details

- Signal processing system (signal.ml)
- Combinational and sequential logic handling (comb.ml, sequential_logic.ml)
- Circuit optimization and deduplication (dedup.ml)
- Waveform generation (wave_*.ml files)
- Multiple clock domain support (shown in test/multi_clock_domain_examples)

##### External Tool Integration

Connections to:
- Verilator
- Yosys
- Xilinx tools
- Waveform viewers

##### Documentation Integration

- Reference key documentation files from docs/ directory
- Version information and compatibility notes

Remember to highlight the extensibility of the system, as shown by the numerous additional libraries in the ecosystem (Hardcaml_waveterm, Hardcaml_c, etc.).

This architecture should be drawn using a professional diagramming tool that supports UML notation and allows for clear representation of hierarchical relationships and data flows.

## Diagram (Mermaid)

```mermaid
graph TB
    subgraph User["User Design Layer"]
        UserCode["OCaml Code Using Hardcaml"]:::user
    end

    subgraph Core["Hardcaml Core"]
        subgraph CircuitGen["Circuit Generation"]
            Circuit["Circuit Engine"]:::core
            click Circuit "https://github.com/janestreet/hardcaml/blob/master/src/circuit.ml"
            CircuitDB["Circuit Database"]:::core
            click CircuitDB "https://github.com/janestreet/hardcaml/blob/master/src/circuit_database.ml"
            Dedup["Circuit Optimization"]:::core
            click Dedup "https://github.com/janestreet/hardcaml/blob/master/src/dedup.ml"
        end

        subgraph Sim["Simulation System"]
            CycleSim["Cyclesim Engine"]:::sim
            click CycleSim "https://github.com/janestreet/hardcaml/blob/master/src/cyclesim.ml"
            CycleOps["Cyclesim Operations"]:::sim
            click CycleOps "https://github.com/janestreet/hardcaml/blob/master/src/cyclesim_ops.ml"
            WaveGen["Waveform Generation"]:::sim
            click WaveGen "https://github.com/janestreet/hardcaml/blob/master/src/wave_data.ml"
        end

        subgraph RTL["RTL Generation"]
            RTLCore["RTL Core"]:::rtl
            click RTLCore "https://github.com/janestreet/hardcaml/blob/master/src/rtl.ml"
            Verilog["Verilog Backend"]:::rtl
            click Verilog "https://github.com/janestreet/hardcaml/blob/master/src/rtl_verilog_of_ast.ml"
            VHDL["VHDL Backend"]:::rtl
            click VHDL "https://github.com/janestreet/hardcaml/blob/master/src/rtl_vhdl_of_ast.ml"
        end

        subgraph Core_Components["Core Components"]
            Bits["Bits Operations"]:::core
            click Bits "https://github.com/janestreet/hardcaml/blob/master/src/bits.ml"
            Interface["Interface System"]:::core
            click Interface "https://github.com/janestreet/hardcaml/blob/master/src/interface.ml"
            Always["Always API"]:::core
            click Always "https://github.com/janestreet/hardcaml/blob/master/src/always.ml"
            Sequential["Sequential Logic"]:::core
            click Sequential "https://github.com/janestreet/hardcaml/blob/master/src/sequential_logic.ml"
            Property["Property System"]:::core
            click Property "https://github.com/janestreet/hardcaml/blob/master/src/property.ml"
        end
    end

    subgraph Backend["Backend Targets"]
        VerilatorInt["Verilator"]:::backend
        YosysInt["Yosys"]:::backend
        XilinxInt["Xilinx Tools"]:::backend
        WaveViewer["Waveform Viewers"]:::backend
    end

    %% Relationships
    UserCode --> Circuit
    Circuit --> CircuitDB
    Circuit --> Dedup
    Circuit --> CycleSim
    CycleSim --> CycleOps
    CycleSim --> WaveGen
    Circuit --> RTLCore
    RTLCore --> Verilog
    RTLCore --> VHDL
    
    %% Core Component Relations
    Circuit --> Bits
    Circuit --> Interface
    Circuit --> Always
    Circuit --> Sequential
    Circuit --> Property
    
    %% Backend Relations
    Verilog --> VerilatorInt
    Verilog --> YosysInt
    Verilog --> XilinxInt
    WaveGen --> WaveViewer

    %% Styling
    classDef user fill:#e1f5fe,stroke:#01579b
    classDef core fill:#e8f5e9,stroke:#2e7d32
    classDef sim fill:#fff3e0,stroke:#ef6c00
    classDef rtl fill:#f3e5f5,stroke:#7b1fa2
    classDef backend fill:#eceff1,stroke:#455a64

    %% Legend
    subgraph Legend
        UserL["User Components"]:::user
        CoreL["Core Components"]:::core
        SimL["Simulation Components"]:::sim
        RTLL["RTL Components"]:::rtl
        BackendL["Backend Components"]:::backend
    end
```
