How mohxmmad/ihdl works
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: simulator.go Fixed-point simulator · circuit.go Compiled circuit · display.go Display frames · main.go ihdl CLI
Components 16
CLI & Modes
- ihdl CLI · Selects interactive or batch execution.
cmd/ihdl/main.go - Paired file mode · Runs .iinp/.iout input-output pairs.
cmd/ihdl/main.go - Interactive controls · Edits inputs, buttons, and clock modes.
internal/terminal.go
Language Front End
- Top-level .ihdl
- Module import loading · Resolves imports by module name.
internal/parser.go - Declarations & operations
internal/parser.go - Module compiler · Validates types, ports, connections, displays, and gates.
internal/compile.go
Circuit & Simulation
- Compiled circuit · Contains ports, wires, operations, instances, and display mappings.
internal/circuit.go - Instance scopes · Maps parent signals through ordered child ports.
internal/circuit.go - Fixed-point simulator · Evaluates pending operations until no further progress.
internal/simulator.go - Primitive evaluation · Supports short-circuiting and IGNORE propagation.
internal/simulator.go - Instance wire state · Retains non-error internal-wire values across evaluations.
internal/state.go - Persistent FLOAT state · Stores one-bit values by hierarchical path in .state.
internal/state.go
Presentation
- Display frames · Builds RGB/BW frames and reusable grids.
internal/display.go - Local browser viewer
internal/display.go - .ippf image export · Writes the latest rendered image as PNG data.
internal/display.go
Connections 19
- 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