nexhdl 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- nexhdl-0.1.0/LICENSE +21 -0
- nexhdl-0.1.0/PKG-INFO +298 -0
- nexhdl-0.1.0/README.md +271 -0
- nexhdl-0.1.0/nexhdl/__init__.py +96 -0
- nexhdl-0.1.0/nexhdl/core/__init__.py +11 -0
- nexhdl-0.1.0/nexhdl/core/context.py +65 -0
- nexhdl-0.1.0/nexhdl/core/hwmodule.py +123 -0
- nexhdl-0.1.0/nexhdl/core/instance.py +78 -0
- nexhdl-0.1.0/nexhdl/core/io.py +150 -0
- nexhdl-0.1.0/nexhdl/core/meta.py +213 -0
- nexhdl-0.1.0/nexhdl/core/module.py +191 -0
- nexhdl-0.1.0/nexhdl/core/ports.py +65 -0
- nexhdl-0.1.0/nexhdl/core/trace.py +48 -0
- nexhdl-0.1.0/nexhdl/interfaces/__init__.py +29 -0
- nexhdl-0.1.0/nexhdl/interfaces/base.py +195 -0
- nexhdl-0.1.0/nexhdl/interfaces/channel.py +101 -0
- nexhdl-0.1.0/nexhdl/interfaces/stream.py +139 -0
- nexhdl-0.1.0/nexhdl/parameters/__init__.py +18 -0
- nexhdl-0.1.0/nexhdl/parameters/meta.py +188 -0
- nexhdl-0.1.0/nexhdl/parameters/proxy.py +97 -0
- nexhdl-0.1.0/nexhdl/parameters/resolver.py +41 -0
- nexhdl-0.1.0/nexhdl/primitives/__init__.py +14 -0
- nexhdl-0.1.0/nexhdl/primitives/fifo.py +213 -0
- nexhdl-0.1.0/nexhdl/primitives/map.py +189 -0
- nexhdl-0.1.0/nexhdl/primitives/mem.py +175 -0
- nexhdl-0.1.0/nexhdl/primitives/mux.py +105 -0
- nexhdl-0.1.0/nexhdl/primitives/reg.py +274 -0
- nexhdl-0.1.0/nexhdl/primitives/wire.py +135 -0
- nexhdl-0.1.0/nexhdl/simulation/__init__.py +22 -0
- nexhdl-0.1.0/nexhdl/simulation/simulator.py +408 -0
- nexhdl-0.1.0/nexhdl/simulation/states.py +272 -0
- nexhdl-0.1.0/nexhdl/simulation/transforms.py +148 -0
- nexhdl-0.1.0/nexhdl/simulation/vcd.py +246 -0
- nexhdl-0.1.0/nexhdl/types/__init__.py +22 -0
- nexhdl-0.1.0/nexhdl/types/array.py +405 -0
- nexhdl-0.1.0/nexhdl/types/base.py +345 -0
- nexhdl-0.1.0/nexhdl/types/clock.py +303 -0
- nexhdl-0.1.0/nexhdl/types/logic.py +442 -0
- nexhdl-0.1.0/nexhdl/types/struct.py +372 -0
- nexhdl-0.1.0/nexhdl/types/union.py +300 -0
- nexhdl-0.1.0/nexhdl.egg-info/PKG-INFO +298 -0
- nexhdl-0.1.0/nexhdl.egg-info/SOURCES.txt +81 -0
- nexhdl-0.1.0/nexhdl.egg-info/dependency_links.txt +1 -0
- nexhdl-0.1.0/nexhdl.egg-info/requires.txt +5 -0
- nexhdl-0.1.0/nexhdl.egg-info/top_level.txt +1 -0
- nexhdl-0.1.0/pyproject.toml +38 -0
- nexhdl-0.1.0/setup.cfg +4 -0
- nexhdl-0.1.0/setup.py +15 -0
- nexhdl-0.1.0/test/test_Logic.py +148 -0
- nexhdl-0.1.0/test/test_all_compilation.py +392 -0
- nexhdl-0.1.0/test/test_array.py +164 -0
- nexhdl-0.1.0/test/test_bugfixes.py +340 -0
- nexhdl-0.1.0/test/test_casting.py +93 -0
- nexhdl-0.1.0/test/test_complex_types.py +75 -0
- nexhdl-0.1.0/test/test_connectivity.py +90 -0
- nexhdl-0.1.0/test/test_direct_instantiation.py +72 -0
- nexhdl-0.1.0/test/test_fifo.py +70 -0
- nexhdl-0.1.0/test/test_generic_validation.py +104 -0
- nexhdl-0.1.0/test/test_interfaces.py +226 -0
- nexhdl-0.1.0/test/test_internal_external_instances.py +189 -0
- nexhdl-0.1.0/test/test_invalid_types.py +44 -0
- nexhdl-0.1.0/test/test_map.py +211 -0
- nexhdl-0.1.0/test/test_mem.py +332 -0
- nexhdl-0.1.0/test/test_module_instance.py +53 -0
- nexhdl-0.1.0/test/test_mux.py +231 -0
- nexhdl-0.1.0/test/test_param_module.py +111 -0
- nexhdl-0.1.0/test/test_param_ports_resolution.py +183 -0
- nexhdl-0.1.0/test/test_param_validator.py +77 -0
- nexhdl-0.1.0/test/test_ports.py +75 -0
- nexhdl-0.1.0/test/test_raw_memory.py +189 -0
- nexhdl-0.1.0/test/test_reg.py +266 -0
- nexhdl-0.1.0/test/test_run.py +105 -0
- nexhdl-0.1.0/test/test_serialization.py +285 -0
- nexhdl-0.1.0/test/test_simulation_advanced.py +145 -0
- nexhdl-0.1.0/test/test_specialize_function.py +23 -0
- nexhdl-0.1.0/test/test_struct_array.py +267 -0
- nexhdl-0.1.0/test/test_struct_comprehensive.py +89 -0
- nexhdl-0.1.0/test/test_type_check_instantiation.py +99 -0
- nexhdl-0.1.0/test/test_union_comprehensive.py +69 -0
- nexhdl-0.1.0/test/test_union_module.py +90 -0
- nexhdl-0.1.0/test/test_union_struct.py +94 -0
- nexhdl-0.1.0/test/test_value_guards.py +185 -0
- nexhdl-0.1.0/test/test_wire.py +198 -0
nexhdl-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Yassinz
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
nexhdl-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,298 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: nexhdl
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: A Python-based Hardware Description Language that compiles to SystemVerilog via CIRCT
|
|
5
|
+
Author-email: Yassine <yassinz.you@gmail.com>
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: homepage, https://github.com/yassinz/nexhdl
|
|
8
|
+
Project-URL: repository, https://github.com/yassinz/nexhdl
|
|
9
|
+
Project-URL: bug_tracker, https://github.com/yassinz/nexhdl/issues
|
|
10
|
+
Classifier: Development Status :: 3 - Alpha
|
|
11
|
+
Classifier: Intended Audience :: Science/Research
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Topic :: Software Development :: Code Generators
|
|
14
|
+
Classifier: Topic :: System :: Hardware
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
18
|
+
Requires-Python: >=3.13
|
|
19
|
+
Description-Content-Type: text/markdown
|
|
20
|
+
License-File: LICENSE
|
|
21
|
+
Requires-Dist: circt
|
|
22
|
+
Requires-Dist: llvmlite
|
|
23
|
+
Provides-Extra: dev
|
|
24
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
25
|
+
Dynamic: license-file
|
|
26
|
+
Dynamic: requires-python
|
|
27
|
+
|
|
28
|
+
# NexHDL HDL
|
|
29
|
+
|
|
30
|
+
A modern, Python-based Hardware Description Language (HDL) that compiles to SystemVerilog via CIRCT.
|
|
31
|
+
|
|
32
|
+
[](https://www.python.org/downloads/)
|
|
33
|
+
[](https://opensource.org/licenses/MIT)
|
|
34
|
+
|
|
35
|
+
## Features
|
|
36
|
+
|
|
37
|
+
- **🎯 Type-Safe**: Strong typing with `Logic`, `Array`, `Struct`, and `Union`
|
|
38
|
+
- **🔧 Composable**: Types compose infinitely - `Reg[Array[4, Struct]]` just works
|
|
39
|
+
- **🐍 Pythonic**: Modern Python with PEP 695 generics - `class Adder[WIDTH](Module)`
|
|
40
|
+
- **⚡ Fast**: CIRCT backend for rapid compilation
|
|
41
|
+
- **🔬 Simulation**: Built-in LLVM-based simulator with VCD output
|
|
42
|
+
- **📦 Clean**: Predictable mapping to SystemVerilog
|
|
43
|
+
|
|
44
|
+
## Quick Start
|
|
45
|
+
|
|
46
|
+
### Installation
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
pip install nexhdl
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
### Hello World - Counter
|
|
53
|
+
|
|
54
|
+
```python
|
|
55
|
+
from nexhdl import *
|
|
56
|
+
|
|
57
|
+
class Counter(Module):
|
|
58
|
+
clk: INPUT[Clock]
|
|
59
|
+
rst: INPUT[Reset]
|
|
60
|
+
out: OUTPUT[Logic[32]]
|
|
61
|
+
|
|
62
|
+
def build(self, io):
|
|
63
|
+
count = Reg[Logic[32]]()
|
|
64
|
+
|
|
65
|
+
with io.clk.posedge, io.rst.negedge:
|
|
66
|
+
count.next = count + 1
|
|
67
|
+
|
|
68
|
+
io.out = count
|
|
69
|
+
|
|
70
|
+
# Generate Mlir and Verilog
|
|
71
|
+
counter = Counter()
|
|
72
|
+
print(counter.mlir())
|
|
73
|
+
print(counter.verilog())
|
|
74
|
+
|
|
75
|
+
# Simulate
|
|
76
|
+
with counter.simulate() as sim:
|
|
77
|
+
print(sim.llvm_ir())
|
|
78
|
+
print(sim.machine_asm())
|
|
79
|
+
sim.io.rst = 0
|
|
80
|
+
sim.tick()
|
|
81
|
+
sim.io.rst = 1
|
|
82
|
+
for i in range(10):
|
|
83
|
+
sim.tick()
|
|
84
|
+
print(f"count = {sim.io.out}")
|
|
85
|
+
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
### Parameterized Design
|
|
89
|
+
|
|
90
|
+
```python
|
|
91
|
+
class Adder[WIDTH](Module):
|
|
92
|
+
a: INPUT[Logic[WIDTH]]
|
|
93
|
+
b: INPUT[Logic[WIDTH]]
|
|
94
|
+
out: OUTPUT[Logic[WIDTH]]
|
|
95
|
+
|
|
96
|
+
def build(self, io):
|
|
97
|
+
io.out = io.a + io.b
|
|
98
|
+
|
|
99
|
+
# Use with different widths
|
|
100
|
+
adder8 = Adder[8]()
|
|
101
|
+
adder32 = Adder[32]()
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
### Using Structs
|
|
105
|
+
|
|
106
|
+
```python
|
|
107
|
+
class Point[WIDTH](Struct):
|
|
108
|
+
x: Logic[WIDTH]
|
|
109
|
+
y: Logic[WIDTH]
|
|
110
|
+
|
|
111
|
+
def __add__(self, other):
|
|
112
|
+
return Point[WIDTH](
|
|
113
|
+
x=self.x + other.x,
|
|
114
|
+
y=self.y + other.y
|
|
115
|
+
)
|
|
116
|
+
|
|
117
|
+
class PointAdder[WIDTH](Module):
|
|
118
|
+
a: INPUT[Point[WIDTH]]
|
|
119
|
+
b: INPUT[Point[WIDTH]]
|
|
120
|
+
out: OUTPUT[Point[WIDTH]]
|
|
121
|
+
|
|
122
|
+
def build(self, io):
|
|
123
|
+
io.out = io.a + io.b
|
|
124
|
+
|
|
125
|
+
addr = PointAdder[8]()
|
|
126
|
+
print(addr.verilog())
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
## Core Concepts
|
|
130
|
+
|
|
131
|
+
### Types
|
|
132
|
+
|
|
133
|
+
- **Logic[N]** - N-bit vectors with full arithmetic/bitwise operations
|
|
134
|
+
- **Array[N, T]** - Fixed-size arrays of any type
|
|
135
|
+
- **Struct** - Composite types with named fields
|
|
136
|
+
- **Union** - Tagged unions for multiple views of data
|
|
137
|
+
|
|
138
|
+
### Primitives
|
|
139
|
+
|
|
140
|
+
- **Reg[T]** - Registers with `.next` assignment
|
|
141
|
+
- **Wire[T]** - Forward-declared wires with `.assign` property
|
|
142
|
+
- **Mux[T]** - Type-safe multiplexers
|
|
143
|
+
- **Map[W, T]** - Combinational lookup tables
|
|
144
|
+
- **Mem[N, T]** - Synchronous memories with `.read` and `.write` methods
|
|
145
|
+
- **Fifo[N, T]** - FIFO with `.push` and `.pop` methods
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
### Interfaces & Streams (ESI-Style)
|
|
149
|
+
|
|
150
|
+
- **Stream[T]** - Ready/valid streaming channels with automatic direction handling
|
|
151
|
+
- **Interface / Bundle** - Custom protocol and bus groupings with `INPUT[...]` and `OUTPUT[...]` polarity flipping
|
|
152
|
+
- **StreamChannel[T]** - Internal point-to-point streaming channel wiring
|
|
153
|
+
|
|
154
|
+
```python
|
|
155
|
+
class StreamProcessor[WIDTH: int](Module):
|
|
156
|
+
clk: INPUT[Clock]
|
|
157
|
+
rst: INPUT[Reset]
|
|
158
|
+
inp: INPUT[Stream[Logic[WIDTH]]]
|
|
159
|
+
out: OUTPUT[Stream[Logic[WIDTH]]]
|
|
160
|
+
|
|
161
|
+
def build(self, io):
|
|
162
|
+
io.inp.ready = io.out.ready
|
|
163
|
+
io.out.push(io.inp.data * 2, enable=io.inp.valid)
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Registers
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
# Create register
|
|
170
|
+
reg = Reg[Logic[32]]()
|
|
171
|
+
|
|
172
|
+
# With initial value
|
|
173
|
+
reg = Reg[Logic[32]](init=42)
|
|
174
|
+
|
|
175
|
+
# Assign in clock context
|
|
176
|
+
with io.clk.posedge:
|
|
177
|
+
reg.next = reg + 1
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
### Wires
|
|
181
|
+
|
|
182
|
+
```python
|
|
183
|
+
# Create wire
|
|
184
|
+
w = Wire[Logic[32]]()
|
|
185
|
+
|
|
186
|
+
# Use before assignment
|
|
187
|
+
io.out = w
|
|
188
|
+
|
|
189
|
+
# Assign later
|
|
190
|
+
w.assign = io.a + io.b
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Multiplexers
|
|
194
|
+
|
|
195
|
+
```python
|
|
196
|
+
result = Mux[Logic[32]](sel, true_val, false_val)
|
|
197
|
+
```
|
|
198
|
+
|
|
199
|
+
## Examples
|
|
200
|
+
|
|
201
|
+
See the `examples/` directory for a variety of hardware designs:
|
|
202
|
+
- **[RV32I](examples/RV32I/)**: A complete RISC-V RV32I CPU implementation.
|
|
203
|
+
- **[Streaming Pipeline](examples/stream_pipeline.py)**: Elastic packet processing with `Stream[Packet]` and backpressure.
|
|
204
|
+
- **[DSP Components](examples/dsp.py)**: DotProduct, Pipelined MAC, and FIR filters.
|
|
205
|
+
- **[Networking](examples/crc32.py)**: CRC-32 generator and packet protocol inspectors.
|
|
206
|
+
- **[Systolic Array](examples/systolic_array.py)**: Parallel matrix multiplication engine.
|
|
207
|
+
- **[Basics](examples/alu.py)**: ALU, state machines, counters, and logic gates.
|
|
208
|
+
|
|
209
|
+
## Documentation
|
|
210
|
+
|
|
211
|
+
- [Types](docs/types.md) - Logic, Array, Struct, Union
|
|
212
|
+
- [Primitives](docs/primitives.md) - Reg, Wire, Mux, Mem, Fifo
|
|
213
|
+
- [Interfaces & Streams](docs/interfaces.md) - Interface, Bundle, Stream (ESI)
|
|
214
|
+
- [Modules](docs/modules.md) - Module definition and hierarchy
|
|
215
|
+
- [Parameterization](docs/parameterization.md) - Generic types and modules
|
|
216
|
+
- [Simulation](docs/simulation.md) - Fast JIT-based simulation and VCD tracing
|
|
217
|
+
|
|
218
|
+
## Project Structure
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
nexhdl/
|
|
222
|
+
├── nexhdl/
|
|
223
|
+
│ ├── core/ # Module system
|
|
224
|
+
│ ├── types/ # Type system (Logic, Array, Struct, Union)
|
|
225
|
+
│ ├── primitives/ # Reg, Wire, Mux, Mem, Fifo, Map
|
|
226
|
+
│ ├── interfaces/ # Interface, Bundle, Stream (ESI)
|
|
227
|
+
│ ├── simulation/ # LLVM-based simulator
|
|
228
|
+
│ └── parameters/ # PEP 695 generics & parameter resolvers
|
|
229
|
+
├── examples/ # Example designs
|
|
230
|
+
├── test/ # Tests
|
|
231
|
+
├── docs/ # Documentation
|
|
232
|
+
└── README.md
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
## Requirements
|
|
236
|
+
|
|
237
|
+
- Python 3.13+
|
|
238
|
+
- CIRCT (installed via pip)
|
|
239
|
+
- llvmlite
|
|
240
|
+
|
|
241
|
+
## Development
|
|
242
|
+
|
|
243
|
+
```bash
|
|
244
|
+
# Install in development mode
|
|
245
|
+
pip install -e ".[dev]"
|
|
246
|
+
|
|
247
|
+
# Run tests
|
|
248
|
+
python -m pytest test/
|
|
249
|
+
|
|
250
|
+
# Run specific test
|
|
251
|
+
python test/test_reg.py
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
## Design Philosophy
|
|
255
|
+
|
|
256
|
+
1. **Types compose infinitely** - Any type can contain any other type
|
|
257
|
+
2. **Python-first** - Use Python's features, don't fight them
|
|
258
|
+
3. **Predictable output** - Clear mapping to SystemVerilog
|
|
259
|
+
4. **Fast compilation** - CIRCT backend for speed
|
|
260
|
+
5. **Type safety** - Catch errors at compile time, not simulation
|
|
261
|
+
|
|
262
|
+
## Status
|
|
263
|
+
|
|
264
|
+
⚠️ **Alpha** - Work in progress. API may change.
|
|
265
|
+
|
|
266
|
+
Current focus:
|
|
267
|
+
- Core type system ✅
|
|
268
|
+
- Module system ✅
|
|
269
|
+
- Primitives (Reg, Wire, Mux, Map, Mem, Fifo) ✅
|
|
270
|
+
- Interfaces and Streams (ESI) ✅
|
|
271
|
+
- Simulation ✅
|
|
272
|
+
- Documentation 🚧
|
|
273
|
+
- Fsm and Pipeline 🚧
|
|
274
|
+
- Standard library 🚧
|
|
275
|
+
|
|
276
|
+
## Contributing
|
|
277
|
+
|
|
278
|
+
Contributions welcome! Areas of interest:
|
|
279
|
+
- Documentation improvements
|
|
280
|
+
- Bug reports and fixes
|
|
281
|
+
- Example designs
|
|
282
|
+
|
|
283
|
+
## License
|
|
284
|
+
|
|
285
|
+
MIT License - see [LICENSE](LICENSE) file for details
|
|
286
|
+
|
|
287
|
+
## Acknowledgments
|
|
288
|
+
|
|
289
|
+
- Built on [CIRCT](https://circt.llvm.org/) - Circuit IR Compilers and Tools
|
|
290
|
+
- Inspired by [Chisel](https://www.chisel-lang.org/), [Amaranth](https://amaranth-lang.org/), and [MyHDL](http://www.myhdl.org/)
|
|
291
|
+
|
|
292
|
+
## Contact
|
|
293
|
+
|
|
294
|
+
For questions, issues, or discussions, please open an issue on GitHub.
|
|
295
|
+
|
|
296
|
+
---
|
|
297
|
+
|
|
298
|
+
**Note**: This is a research/educational project. For production use, consider mature alternatives.
|
nexhdl-0.1.0/README.md
ADDED
|
@@ -0,0 +1,271 @@
|
|
|
1
|
+
# NexHDL HDL
|
|
2
|
+
|
|
3
|
+
A modern, Python-based Hardware Description Language (HDL) that compiles to SystemVerilog via CIRCT.
|
|
4
|
+
|
|
5
|
+
[](https://www.python.org/downloads/)
|
|
6
|
+
[](https://opensource.org/licenses/MIT)
|
|
7
|
+
|
|
8
|
+
## Features
|
|
9
|
+
|
|
10
|
+
- **🎯 Type-Safe**: Strong typing with `Logic`, `Array`, `Struct`, and `Union`
|
|
11
|
+
- **🔧 Composable**: Types compose infinitely - `Reg[Array[4, Struct]]` just works
|
|
12
|
+
- **🐍 Pythonic**: Modern Python with PEP 695 generics - `class Adder[WIDTH](Module)`
|
|
13
|
+
- **⚡ Fast**: CIRCT backend for rapid compilation
|
|
14
|
+
- **🔬 Simulation**: Built-in LLVM-based simulator with VCD output
|
|
15
|
+
- **📦 Clean**: Predictable mapping to SystemVerilog
|
|
16
|
+
|
|
17
|
+
## Quick Start
|
|
18
|
+
|
|
19
|
+
### Installation
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install nexhdl
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### Hello World - Counter
|
|
26
|
+
|
|
27
|
+
```python
|
|
28
|
+
from nexhdl import *
|
|
29
|
+
|
|
30
|
+
class Counter(Module):
|
|
31
|
+
clk: INPUT[Clock]
|
|
32
|
+
rst: INPUT[Reset]
|
|
33
|
+
out: OUTPUT[Logic[32]]
|
|
34
|
+
|
|
35
|
+
def build(self, io):
|
|
36
|
+
count = Reg[Logic[32]]()
|
|
37
|
+
|
|
38
|
+
with io.clk.posedge, io.rst.negedge:
|
|
39
|
+
count.next = count + 1
|
|
40
|
+
|
|
41
|
+
io.out = count
|
|
42
|
+
|
|
43
|
+
# Generate Mlir and Verilog
|
|
44
|
+
counter = Counter()
|
|
45
|
+
print(counter.mlir())
|
|
46
|
+
print(counter.verilog())
|
|
47
|
+
|
|
48
|
+
# Simulate
|
|
49
|
+
with counter.simulate() as sim:
|
|
50
|
+
print(sim.llvm_ir())
|
|
51
|
+
print(sim.machine_asm())
|
|
52
|
+
sim.io.rst = 0
|
|
53
|
+
sim.tick()
|
|
54
|
+
sim.io.rst = 1
|
|
55
|
+
for i in range(10):
|
|
56
|
+
sim.tick()
|
|
57
|
+
print(f"count = {sim.io.out}")
|
|
58
|
+
|
|
59
|
+
```
|
|
60
|
+
|
|
61
|
+
### Parameterized Design
|
|
62
|
+
|
|
63
|
+
```python
|
|
64
|
+
class Adder[WIDTH](Module):
|
|
65
|
+
a: INPUT[Logic[WIDTH]]
|
|
66
|
+
b: INPUT[Logic[WIDTH]]
|
|
67
|
+
out: OUTPUT[Logic[WIDTH]]
|
|
68
|
+
|
|
69
|
+
def build(self, io):
|
|
70
|
+
io.out = io.a + io.b
|
|
71
|
+
|
|
72
|
+
# Use with different widths
|
|
73
|
+
adder8 = Adder[8]()
|
|
74
|
+
adder32 = Adder[32]()
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### Using Structs
|
|
78
|
+
|
|
79
|
+
```python
|
|
80
|
+
class Point[WIDTH](Struct):
|
|
81
|
+
x: Logic[WIDTH]
|
|
82
|
+
y: Logic[WIDTH]
|
|
83
|
+
|
|
84
|
+
def __add__(self, other):
|
|
85
|
+
return Point[WIDTH](
|
|
86
|
+
x=self.x + other.x,
|
|
87
|
+
y=self.y + other.y
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
class PointAdder[WIDTH](Module):
|
|
91
|
+
a: INPUT[Point[WIDTH]]
|
|
92
|
+
b: INPUT[Point[WIDTH]]
|
|
93
|
+
out: OUTPUT[Point[WIDTH]]
|
|
94
|
+
|
|
95
|
+
def build(self, io):
|
|
96
|
+
io.out = io.a + io.b
|
|
97
|
+
|
|
98
|
+
addr = PointAdder[8]()
|
|
99
|
+
print(addr.verilog())
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
## Core Concepts
|
|
103
|
+
|
|
104
|
+
### Types
|
|
105
|
+
|
|
106
|
+
- **Logic[N]** - N-bit vectors with full arithmetic/bitwise operations
|
|
107
|
+
- **Array[N, T]** - Fixed-size arrays of any type
|
|
108
|
+
- **Struct** - Composite types with named fields
|
|
109
|
+
- **Union** - Tagged unions for multiple views of data
|
|
110
|
+
|
|
111
|
+
### Primitives
|
|
112
|
+
|
|
113
|
+
- **Reg[T]** - Registers with `.next` assignment
|
|
114
|
+
- **Wire[T]** - Forward-declared wires with `.assign` property
|
|
115
|
+
- **Mux[T]** - Type-safe multiplexers
|
|
116
|
+
- **Map[W, T]** - Combinational lookup tables
|
|
117
|
+
- **Mem[N, T]** - Synchronous memories with `.read` and `.write` methods
|
|
118
|
+
- **Fifo[N, T]** - FIFO with `.push` and `.pop` methods
|
|
119
|
+
|
|
120
|
+
|
|
121
|
+
### Interfaces & Streams (ESI-Style)
|
|
122
|
+
|
|
123
|
+
- **Stream[T]** - Ready/valid streaming channels with automatic direction handling
|
|
124
|
+
- **Interface / Bundle** - Custom protocol and bus groupings with `INPUT[...]` and `OUTPUT[...]` polarity flipping
|
|
125
|
+
- **StreamChannel[T]** - Internal point-to-point streaming channel wiring
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
class StreamProcessor[WIDTH: int](Module):
|
|
129
|
+
clk: INPUT[Clock]
|
|
130
|
+
rst: INPUT[Reset]
|
|
131
|
+
inp: INPUT[Stream[Logic[WIDTH]]]
|
|
132
|
+
out: OUTPUT[Stream[Logic[WIDTH]]]
|
|
133
|
+
|
|
134
|
+
def build(self, io):
|
|
135
|
+
io.inp.ready = io.out.ready
|
|
136
|
+
io.out.push(io.inp.data * 2, enable=io.inp.valid)
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Registers
|
|
140
|
+
|
|
141
|
+
```python
|
|
142
|
+
# Create register
|
|
143
|
+
reg = Reg[Logic[32]]()
|
|
144
|
+
|
|
145
|
+
# With initial value
|
|
146
|
+
reg = Reg[Logic[32]](init=42)
|
|
147
|
+
|
|
148
|
+
# Assign in clock context
|
|
149
|
+
with io.clk.posedge:
|
|
150
|
+
reg.next = reg + 1
|
|
151
|
+
```
|
|
152
|
+
|
|
153
|
+
### Wires
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
# Create wire
|
|
157
|
+
w = Wire[Logic[32]]()
|
|
158
|
+
|
|
159
|
+
# Use before assignment
|
|
160
|
+
io.out = w
|
|
161
|
+
|
|
162
|
+
# Assign later
|
|
163
|
+
w.assign = io.a + io.b
|
|
164
|
+
```
|
|
165
|
+
|
|
166
|
+
### Multiplexers
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
result = Mux[Logic[32]](sel, true_val, false_val)
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
## Examples
|
|
173
|
+
|
|
174
|
+
See the `examples/` directory for a variety of hardware designs:
|
|
175
|
+
- **[RV32I](examples/RV32I/)**: A complete RISC-V RV32I CPU implementation.
|
|
176
|
+
- **[Streaming Pipeline](examples/stream_pipeline.py)**: Elastic packet processing with `Stream[Packet]` and backpressure.
|
|
177
|
+
- **[DSP Components](examples/dsp.py)**: DotProduct, Pipelined MAC, and FIR filters.
|
|
178
|
+
- **[Networking](examples/crc32.py)**: CRC-32 generator and packet protocol inspectors.
|
|
179
|
+
- **[Systolic Array](examples/systolic_array.py)**: Parallel matrix multiplication engine.
|
|
180
|
+
- **[Basics](examples/alu.py)**: ALU, state machines, counters, and logic gates.
|
|
181
|
+
|
|
182
|
+
## Documentation
|
|
183
|
+
|
|
184
|
+
- [Types](docs/types.md) - Logic, Array, Struct, Union
|
|
185
|
+
- [Primitives](docs/primitives.md) - Reg, Wire, Mux, Mem, Fifo
|
|
186
|
+
- [Interfaces & Streams](docs/interfaces.md) - Interface, Bundle, Stream (ESI)
|
|
187
|
+
- [Modules](docs/modules.md) - Module definition and hierarchy
|
|
188
|
+
- [Parameterization](docs/parameterization.md) - Generic types and modules
|
|
189
|
+
- [Simulation](docs/simulation.md) - Fast JIT-based simulation and VCD tracing
|
|
190
|
+
|
|
191
|
+
## Project Structure
|
|
192
|
+
|
|
193
|
+
```
|
|
194
|
+
nexhdl/
|
|
195
|
+
├── nexhdl/
|
|
196
|
+
│ ├── core/ # Module system
|
|
197
|
+
│ ├── types/ # Type system (Logic, Array, Struct, Union)
|
|
198
|
+
│ ├── primitives/ # Reg, Wire, Mux, Mem, Fifo, Map
|
|
199
|
+
│ ├── interfaces/ # Interface, Bundle, Stream (ESI)
|
|
200
|
+
│ ├── simulation/ # LLVM-based simulator
|
|
201
|
+
│ └── parameters/ # PEP 695 generics & parameter resolvers
|
|
202
|
+
├── examples/ # Example designs
|
|
203
|
+
├── test/ # Tests
|
|
204
|
+
├── docs/ # Documentation
|
|
205
|
+
└── README.md
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
## Requirements
|
|
209
|
+
|
|
210
|
+
- Python 3.13+
|
|
211
|
+
- CIRCT (installed via pip)
|
|
212
|
+
- llvmlite
|
|
213
|
+
|
|
214
|
+
## Development
|
|
215
|
+
|
|
216
|
+
```bash
|
|
217
|
+
# Install in development mode
|
|
218
|
+
pip install -e ".[dev]"
|
|
219
|
+
|
|
220
|
+
# Run tests
|
|
221
|
+
python -m pytest test/
|
|
222
|
+
|
|
223
|
+
# Run specific test
|
|
224
|
+
python test/test_reg.py
|
|
225
|
+
```
|
|
226
|
+
|
|
227
|
+
## Design Philosophy
|
|
228
|
+
|
|
229
|
+
1. **Types compose infinitely** - Any type can contain any other type
|
|
230
|
+
2. **Python-first** - Use Python's features, don't fight them
|
|
231
|
+
3. **Predictable output** - Clear mapping to SystemVerilog
|
|
232
|
+
4. **Fast compilation** - CIRCT backend for speed
|
|
233
|
+
5. **Type safety** - Catch errors at compile time, not simulation
|
|
234
|
+
|
|
235
|
+
## Status
|
|
236
|
+
|
|
237
|
+
⚠️ **Alpha** - Work in progress. API may change.
|
|
238
|
+
|
|
239
|
+
Current focus:
|
|
240
|
+
- Core type system ✅
|
|
241
|
+
- Module system ✅
|
|
242
|
+
- Primitives (Reg, Wire, Mux, Map, Mem, Fifo) ✅
|
|
243
|
+
- Interfaces and Streams (ESI) ✅
|
|
244
|
+
- Simulation ✅
|
|
245
|
+
- Documentation 🚧
|
|
246
|
+
- Fsm and Pipeline 🚧
|
|
247
|
+
- Standard library 🚧
|
|
248
|
+
|
|
249
|
+
## Contributing
|
|
250
|
+
|
|
251
|
+
Contributions welcome! Areas of interest:
|
|
252
|
+
- Documentation improvements
|
|
253
|
+
- Bug reports and fixes
|
|
254
|
+
- Example designs
|
|
255
|
+
|
|
256
|
+
## License
|
|
257
|
+
|
|
258
|
+
MIT License - see [LICENSE](LICENSE) file for details
|
|
259
|
+
|
|
260
|
+
## Acknowledgments
|
|
261
|
+
|
|
262
|
+
- Built on [CIRCT](https://circt.llvm.org/) - Circuit IR Compilers and Tools
|
|
263
|
+
- Inspired by [Chisel](https://www.chisel-lang.org/), [Amaranth](https://amaranth-lang.org/), and [MyHDL](http://www.myhdl.org/)
|
|
264
|
+
|
|
265
|
+
## Contact
|
|
266
|
+
|
|
267
|
+
For questions, issues, or discussions, please open an issue on GitHub.
|
|
268
|
+
|
|
269
|
+
---
|
|
270
|
+
|
|
271
|
+
**Note**: This is a research/educational project. For production use, consider mature alternatives.
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
"""
|
|
2
|
+
NexHDL - A clean, composable HDL type system
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
__version__ = "0.1.0"
|
|
6
|
+
|
|
7
|
+
# Initialize CIRCT context management
|
|
8
|
+
import circt
|
|
9
|
+
import atexit
|
|
10
|
+
|
|
11
|
+
# Single global context - entered on module load
|
|
12
|
+
GlobalContext = circt.ir.Context()
|
|
13
|
+
GlobalContext.__enter__()
|
|
14
|
+
GlobalContext.allow_unregistered_dialects = True
|
|
15
|
+
circt.register_dialects(GlobalContext)
|
|
16
|
+
atexit.register(GlobalContext.__exit__, None, None, None)
|
|
17
|
+
|
|
18
|
+
GlobalLocation = circt.ir.Location.unknown(GlobalContext)
|
|
19
|
+
GlobalLocation.__enter__()
|
|
20
|
+
atexit.register(GlobalLocation.__exit__, None, None, None)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def get_context():
|
|
24
|
+
"""Get the global CIRCT context."""
|
|
25
|
+
return GlobalContext
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def get_location():
|
|
29
|
+
"""Get the global CIRCT location."""
|
|
30
|
+
return GlobalLocation
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
# Public API
|
|
34
|
+
from .types import (
|
|
35
|
+
NexType, NexValue,
|
|
36
|
+
Logic, LogicType,
|
|
37
|
+
Array, ArrayType,
|
|
38
|
+
Struct, StructType,
|
|
39
|
+
Union, UnionType,
|
|
40
|
+
Clock, ClockType, Reset, ResetType,
|
|
41
|
+
to_nex_type, get_value_class, wrap,
|
|
42
|
+
)
|
|
43
|
+
|
|
44
|
+
from .core import Module, INPUT, OUTPUT
|
|
45
|
+
|
|
46
|
+
from .primitives import Reg, Wire, Mux, Map, Mem, Fifo
|
|
47
|
+
|
|
48
|
+
from .parameters import ParameterProxy, ParameterResolver, clog2, Param
|
|
49
|
+
|
|
50
|
+
from .interfaces import (
|
|
51
|
+
Interface, Bundle,
|
|
52
|
+
InterfacePort, InterfaceHandle, InterfaceRole,
|
|
53
|
+
Stream, StreamHandle,
|
|
54
|
+
StreamChannel,
|
|
55
|
+
)
|
|
56
|
+
|
|
57
|
+
from .simulation import Simulator
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
__all__ = [
|
|
62
|
+
# CIRCT
|
|
63
|
+
'get_context', 'get_location',
|
|
64
|
+
|
|
65
|
+
# Base
|
|
66
|
+
'NexType', 'NexValue',
|
|
67
|
+
|
|
68
|
+
# Types
|
|
69
|
+
'Logic', 'LogicType',
|
|
70
|
+
'Array', 'ArrayType',
|
|
71
|
+
'Struct', 'StructType',
|
|
72
|
+
'Union', 'UnionType',
|
|
73
|
+
'Clock', 'ClockType',
|
|
74
|
+
'Reset', 'ResetType',
|
|
75
|
+
|
|
76
|
+
# Module
|
|
77
|
+
'Module', 'INPUT', 'OUTPUT',
|
|
78
|
+
|
|
79
|
+
# Interfaces & Streams
|
|
80
|
+
'Interface', 'Bundle',
|
|
81
|
+
'InterfacePort', 'InterfaceHandle', 'InterfaceRole',
|
|
82
|
+
'Stream', 'StreamHandle',
|
|
83
|
+
'StreamChannel',
|
|
84
|
+
|
|
85
|
+
# Primitives
|
|
86
|
+
'Reg', 'Wire', 'Mux', 'Map', 'Mem', 'Fifo',
|
|
87
|
+
|
|
88
|
+
# Utilities
|
|
89
|
+
'wrap', 'to_nex_type', 'get_value_class', 'clog2',
|
|
90
|
+
|
|
91
|
+
# Parameters
|
|
92
|
+
'ParameterProxy', 'ParameterResolver', 'Param',
|
|
93
|
+
|
|
94
|
+
# Simulators
|
|
95
|
+
'Simulator',
|
|
96
|
+
]
|