-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpyhdl.py
More file actions
125 lines (92 loc) · 2.82 KB
/
pyhdl.py
File metadata and controls
125 lines (92 loc) · 2.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
"""
PyHDL Core Module
=================
Defines the base classes and types for PyHDL hardware description.
These classes serve as markers for the transpiler and enable Python's
type hints to work during development, while having no runtime behavior.
Classes:
Module: Base class for all hardware modules
bit: Represents a hardware signal type with optional width
BitMeta: Metaclass enabling bit[N] syntax and .posedge/.negedge attributes
Functions:
In(t): Marks a port as input
Out(t): Marks a port as output
"""
# Exported symbols for 'from pyhdl import *'
__all__ = ['Module', 'bit', 'Enum', 'In', 'Out']
class Module:
"""Base class for all hardware modules.
Hardware modules are defined by inheriting from this class.
The transpiler converts class definitions to SystemVerilog modules.
Example:
class MyALU(Module):
a = In(bit[8])
b = In(bit[8])
result = Out(bit[8])
result = a + b
"""
pass
class BitMeta(type):
"""Metaclass for the bit type.
Enables the following syntax:
- bit[8] : 8-bit signal
- bit[16] : 16-bit signal
- clk.posedge : Positive edge trigger
- rst.negedge : Negative edge trigger
"""
def __getitem__(cls, params):
"""Enable subscript syntax: bit[N] for N-bit signals."""
return cls
@property
def posedge(cls):
"""Marker for positive edge sensitivity."""
return cls
@property
def negedge(cls):
"""Marker for negative edge sensitivity."""
return cls
class bit(metaclass=BitMeta):
"""Represents a hardware signal type.
Usage:
signal = bit # 1-bit signal
signal = bit[8] # 8-bit signal
signal = bit[N][M] # N-element array of M-bit signals
"""
pass
class Enum:
"""Base class for state enumeration in FSMs.
Example:
class State(Enum):
IDLE = 0
RUN = 1
DONE = 2
"""
pass
def In(t):
"""Marks a port as input direction.
Args:
t: The signal type (e.g., bit, bit[8])
Returns:
The type unchanged (marker for transpiler)
Example:
clk = In(bit)
data = In(bit[8])
"""
return t
def Out(t):
"""Marks a port as output direction.
Args:
t: The signal type (e.g., bit, bit[8])
Returns:
The type unchanged (marker for transpiler)
Example:
result = Out(bit[8])
valid = Out(bit)
"""
return t
# =============================================================================
# CLI Entry Point
# =============================================================================
if __name__ == '__main__':
from compiler import run_compiler
run_compiler()