# mohxmmad/ihdl architecture

> Architecture diagram of the GitHub repository mohxmmad/ihdl, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2026-08-30). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/mohxmmad/ihdl
- Repository: https://github.com/mohxmmad/ihdl

## Overview

#### Purpose and Entry Point
iHDL is a Go CLI for parsing, compiling, and simulating a small HDL-like module language. It supports combinational logic, hierarchical modules, typed bit/bus/pixel/grid signals, interactive clocks/buttons, persistent floating-bit state, file I/O, and rendered displays. The executable entry point is `cmd/ihdl/main.go`.

#### Source Loading, Parsing, and Module Compilation
The language front end reads top-level `.ihdl` files, resolves imports by module name, parses declarations/operations, and turns module definitions into executable circuit structures. Compilation is the primary validation boundary for signal types, widths, ports, module connections, displays/grids, and gate semantics.
- `internal/parser.go`
- `internal/compile.go`
- `internal/circuit.go`

#### Circuit Model and Hierarchy
The circuit layer represents modules, ordered ports, internal wires, primitive operations, module instances, display mappings, and typed signal values. Module instances establish isolated runtime scopes: child inputs/clocks are mapped from parent signals, child outputs return through the declared port ordering, and compatible bus/pixel conversions may occur at this boundary.
- `internal/circuit.go`
- `internal/compile.go`
- `internal/state.go`

#### Multi-pass Simulation Engine
Simulation repeatedly evaluates pending operations until no more progress is possible. This resolves dependency chains independently of declaration order; unresolved values become `err`. AND/OR short-circuit behavior and module-boundary evaluation permit outputs to resolve despite missing inputs when logically determined. `IGNORE` is an execution-state value that propagates through primitives but prevents module execution and never enters persisted state.
- `internal/simulator.go`
- `internal/circuit.go`
- `internal/state.go`

#### Stateful Runtime Semantics
Runtime state preserves non-error internal-wire values per module instance, enabling feedback patterns across evaluations. FLOAT operations separately retain persistent one-bit values, keyed by hierarchical instance path and stored beside the top-level design in a `.state` file. Inputs and clocks remain caller-controlled source values rather than retained circuit state.
- `internal/state.go`
- `internal/simulator.go`
- `internal/circuit.go`

#### Interaction and Batch Execution
The CLI selects interactive or paired `.iinp`/`.iout` file mode. Interactive control maintains editable source inputs, button pulses, clock manual/step/auto/enter modes, recomputation, and output display. Terminal behavior is separated from core compilation/simulation, allowing the latter to serve both modes.
- `cmd/ihdl/main.go`
- `internal/terminal.go`
- `internal/simulator.go`

#### Display and Export Boundary
Display declarations and PIXEL/GRID mappings are part of the circuit model; simulation produces frames from RGB/BW signals and reusable grids. The display subsystem renders frames for a local browser viewer and can export the latest image as PNG data under the `.ippf` extension. This is the primary external presentation boundary; no remote service dependency is indicated.
- `internal/display.go`
- `internal/circuit.go`
- `cmd/ihdl/main.go`

Main entry points: [`internal/simulator.go`](https://github.com/mohxmmad/ihdl/blob/main/internal/simulator.go) (Fixed-point simulator), [`internal/circuit.go`](https://github.com/mohxmmad/ihdl/blob/main/internal/circuit.go) (Compiled circuit), [`internal/display.go`](https://github.com/mohxmmad/ihdl/blob/main/internal/display.go) (Display frames), [`cmd/ihdl/main.go`](https://github.com/mohxmmad/ihdl/blob/main/cmd/ihdl/main.go) (ihdl CLI)

## Diagram (Mermaid)

```mermaid
flowchart TD

subgraph group_cli["CLI &amp; Modes"]
  node_entry["ihdl CLI<br/>Go executable<br/>[main.go]"]
  node_batch["Paired file mode<br/>batch runner<br/>[main.go]"]
  node_terminal["Interactive controls<br/>terminal UI<br/>[terminal.go]"]
end

subgraph group_frontend["Language Front End"]
  node_source["Top-level .ihdl<br/>HDL source"]
  node_loader["Module import loading<br/>source loader<br/>[parser.go]"]
  node_parser["Declarations &amp; operations<br/>parser<br/>[parser.go]"]
  node_compiler["Module compiler<br/>validation boundary<br/>[compile.go]"]
end

subgraph group_core["Circuit &amp; Simulation"]
  node_circuit{{"Compiled circuit<br/>module graph<br/>[circuit.go]"}}
  node_instances["Instance scopes<br/>hierarchy runtime<br/>[circuit.go]"]
  node_simulator["Fixed-point simulator<br/>evaluation engine<br/>[simulator.go]"]
  node_primitive["Primitive evaluation<br/>gate semantics<br/>[simulator.go]"]
  node_runtime_state["Instance wire state<br/>runtime state<br/>[state.go]"]
  node_float_state[("Persistent FLOAT state<br/>state file<br/>[state.go]")]
end

subgraph group_presentation["Presentation"]
  node_frames["Display frames<br/>rendered signals<br/>[display.go]"]
  node_viewer["Local browser viewer<br/>display renderer<br/>[display.go]"]
  node_png_export[".ippf image export<br/>PNG export<br/>[display.go]"]
end

node_entry -->|"selects"| node_batch
node_entry -->|"selects"| node_terminal
node_entry -->|"loads design"| node_loader
node_source -->|"reads"| node_loader
node_loader -->|"resolves imports"| node_parser
node_parser -->|"parsed definitions"| node_compiler
node_compiler -->|"builds"| node_circuit
node_circuit -->|"contains module instances"| node_instances
node_circuit -->|"evaluated by"| node_simulator
node_instances -->|"maps hierarchy"| node_simulator
node_simulator -->|"evaluates"| node_primitive
node_simulator -->|"reads and updates"| node_runtime_state
node_simulator -->|"persists FLOAT bits"| node_float_state
node_terminal -->|"drives inputs and clocks"| node_simulator
node_batch -->|"drives paired inputs"| node_simulator
node_simulator -->|"produces signal frames"| node_frames
node_circuit -->|"display mappings"| node_frames
node_frames -->|"renders"| node_viewer
node_frames -->|"exports"| node_png_export

click node_entry "https://github.com/mohxmmad/ihdl/blob/main/cmd/ihdl/main.go"
click node_batch "https://github.com/mohxmmad/ihdl/blob/main/cmd/ihdl/main.go"
click node_terminal "https://github.com/mohxmmad/ihdl/blob/main/internal/terminal.go"
click node_loader "https://github.com/mohxmmad/ihdl/blob/main/internal/parser.go"
click node_parser "https://github.com/mohxmmad/ihdl/blob/main/internal/parser.go"
click node_compiler "https://github.com/mohxmmad/ihdl/blob/main/internal/compile.go"
click node_circuit "https://github.com/mohxmmad/ihdl/blob/main/internal/circuit.go"
click node_instances "https://github.com/mohxmmad/ihdl/blob/main/internal/circuit.go"
click node_simulator "https://github.com/mohxmmad/ihdl/blob/main/internal/simulator.go"
click node_primitive "https://github.com/mohxmmad/ihdl/blob/main/internal/simulator.go"
click node_runtime_state "https://github.com/mohxmmad/ihdl/blob/main/internal/state.go"
click node_float_state "https://github.com/mohxmmad/ihdl/blob/main/internal/state.go"
click node_frames "https://github.com/mohxmmad/ihdl/blob/main/internal/display.go"
click node_viewer "https://github.com/mohxmmad/ihdl/blob/main/internal/display.go"
click node_png_export "https://github.com/mohxmmad/ihdl/blob/main/internal/display.go"

classDef toneNeutral fill:#f8fafc,stroke:#334155,stroke-width:1.5px,color:#0f172a
classDef toneBlue fill:#dbeafe,stroke:#2563eb,stroke-width:1.5px,color:#172554
classDef toneAmber fill:#fef3c7,stroke:#d97706,stroke-width:1.5px,color:#78350f
classDef toneMint fill:#dcfce7,stroke:#16a34a,stroke-width:1.5px,color:#14532d
classDef toneRose fill:#ffe4e6,stroke:#e11d48,stroke-width:1.5px,color:#881337
classDef toneIndigo fill:#e0e7ff,stroke:#4f46e5,stroke-width:1.5px,color:#312e81
classDef toneTeal fill:#ccfbf1,stroke:#0f766e,stroke-width:1.5px,color:#134e4a
class node_entry,node_batch,node_terminal toneBlue
class node_source,node_loader,node_parser,node_compiler toneAmber
class node_circuit,node_instances,node_simulator,node_primitive,node_runtime_state,node_float_state toneMint
class node_frames,node_viewer,node_png_export toneRose
```

## Components

### CLI & Modes

- [ihdl CLI](https://github.com/mohxmmad/ihdl/blob/main/cmd/ihdl/main.go): Go executable · Selects interactive or batch execution. · `cmd/ihdl/main.go`
- [Paired file mode](https://github.com/mohxmmad/ihdl/blob/main/cmd/ihdl/main.go): batch runner · Runs .iinp/.iout input-output pairs. · `cmd/ihdl/main.go`
- [Interactive controls](https://github.com/mohxmmad/ihdl/blob/main/internal/terminal.go): terminal UI · Edits inputs, buttons, and clock modes. · `internal/terminal.go`

### Language Front End

- **Top-level .ihdl**: HDL source
- [Module import loading](https://github.com/mohxmmad/ihdl/blob/main/internal/parser.go): source loader · Resolves imports by module name. · `internal/parser.go`
- [Declarations & operations](https://github.com/mohxmmad/ihdl/blob/main/internal/parser.go): parser · `internal/parser.go`
- [Module compiler](https://github.com/mohxmmad/ihdl/blob/main/internal/compile.go): validation boundary · Validates types, ports, connections, displays, and gates. · `internal/compile.go`

### Circuit & Simulation

- [Compiled circuit](https://github.com/mohxmmad/ihdl/blob/main/internal/circuit.go): module graph · Contains ports, wires, operations, instances, and display mappings. · `internal/circuit.go`
- [Instance scopes](https://github.com/mohxmmad/ihdl/blob/main/internal/circuit.go): hierarchy runtime · Maps parent signals through ordered child ports. · `internal/circuit.go`
- [Fixed-point simulator](https://github.com/mohxmmad/ihdl/blob/main/internal/simulator.go): evaluation engine · Evaluates pending operations until no further progress. · `internal/simulator.go`
- [Primitive evaluation](https://github.com/mohxmmad/ihdl/blob/main/internal/simulator.go): gate semantics · Supports short-circuiting and IGNORE propagation. · `internal/simulator.go`
- [Instance wire state](https://github.com/mohxmmad/ihdl/blob/main/internal/state.go): runtime state · Retains non-error internal-wire values across evaluations. · `internal/state.go`
- [Persistent FLOAT state](https://github.com/mohxmmad/ihdl/blob/main/internal/state.go): state file · Stores one-bit values by hierarchical path in .state. · `internal/state.go`

### Presentation

- [Display frames](https://github.com/mohxmmad/ihdl/blob/main/internal/display.go): rendered signals · Builds RGB/BW frames and reusable grids. · `internal/display.go`
- [Local browser viewer](https://github.com/mohxmmad/ihdl/blob/main/internal/display.go): display renderer · `internal/display.go`
- [.ippf image export](https://github.com/mohxmmad/ihdl/blob/main/internal/display.go): PNG export · Writes the latest rendered image as PNG data. · `internal/display.go`

## Connections

- ihdl CLI → Paired file mode: selects
- ihdl CLI → Interactive controls: selects
- ihdl CLI → Module import loading: loads design
- Top-level .ihdl → Module import loading: reads
- Module import loading → Declarations & operations: resolves imports
- Declarations & operations → Module compiler: parsed definitions
- Module compiler → Compiled circuit: builds
- Compiled circuit → Instance scopes: contains module instances
- Compiled circuit → Fixed-point simulator: evaluated by
- Instance scopes → Fixed-point simulator: maps hierarchy
- Fixed-point simulator → Primitive evaluation: evaluates
- Fixed-point simulator → Instance wire state: reads and updates
- Fixed-point simulator → Persistent FLOAT state: persists FLOAT bits
- Interactive controls → Fixed-point simulator: drives inputs and clocks
- Paired file mode → Fixed-point simulator: drives paired inputs
- Fixed-point simulator → Display frames: produces signal frames
- Compiled circuit → Display frames: display mappings
- Display frames → Local browser viewer: renders
- Display frames → .ippf image export: exports
