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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use crate::bits::Bits;
use num_bigint::BigUint;
use std::fmt::{Display, Formatter, LowerHex};
#[derive(Debug, Clone)]
pub struct BlackBox {
pub code: String,
pub name: String,
}
#[derive(Debug, Clone)]
pub struct Wrapper {
pub code: String,
pub cores: String,
}
#[derive(Debug, Clone)]
pub enum Verilog {
Empty,
Combinatorial(VerilogBlock),
Custom(String),
Blackbox(BlackBox),
Wrapper(Wrapper),
}
impl Default for Verilog {
fn default() -> Self {
Self::Empty
}
}
pub type VerilogBlock = Vec<VerilogStatement>;
#[derive(Debug, Clone)]
pub enum VerilogStatement {
Assignment(VerilogExpression, VerilogExpression),
SliceAssignment {
base: String,
width: usize,
offset: VerilogExpression,
replacement: VerilogExpression,
},
If(VerilogConditional),
Match(VerilogMatch),
Loop(VerilogLoop),
Comment(String),
Link(Vec<VerilogLink>),
}
#[derive(Debug, Clone)]
pub enum VerilogLink {
Forward(VerilogLinkDetails),
Backward(VerilogLinkDetails),
Bidirectional(VerilogLinkDetails),
}
#[derive(Debug, Clone)]
pub struct VerilogLinkDetails {
pub my_name: String,
pub owner_name: String,
pub other_name: String,
}
#[derive(Debug, Clone)]
pub struct VerilogIndexAssignment {
pub target: VerilogExpression,
pub index: VerilogExpression,
pub value: VerilogExpression,
}
#[derive(Debug, Clone)]
pub struct VerilogConditional {
pub test: VerilogExpression,
pub then: VerilogBlock,
pub otherwise: VerilogBlockOrConditional,
}
#[derive(Debug, Clone)]
pub struct VerilogLoop {
pub index: String,
pub from: VerilogLiteral,
pub to: VerilogLiteral,
pub block: VerilogBlock,
}
#[derive(Debug, Clone)]
pub enum VerilogBlockOrConditional {
Block(VerilogBlock),
Conditional(Box<VerilogStatement>),
None,
}
#[derive(Debug, Clone)]
pub struct VerilogMatch {
pub test: VerilogExpression,
pub cases: Vec<VerilogCase>,
}
#[derive(Debug, Clone)]
pub struct VerilogCase {
pub condition: String,
pub block: VerilogBlock,
}
#[derive(Debug, Clone)]
pub struct VerilogLiteral {
val: BigUint,
bits: usize,
}
impl VerilogLiteral {
pub fn as_usize(&self) -> usize {
let m = self.val.to_u32_digits();
match m.len() {
0 => 0,
1 => m[0] as usize,
_ => panic!("Loop index is too large!"),
}
}
}
impl From<bool> for VerilogLiteral {
fn from(x: bool) -> Self {
let bi: BigUint = if x { 1_u32 } else { 0_u32 }.into();
VerilogLiteral { val: bi, bits: 1 }
}
}
macro_rules! define_literal_from_uint {
($name: ident, $width: expr) => {
impl From<$name> for VerilogLiteral {
fn from(x: $name) -> Self {
let bi: BigUint = x.into();
VerilogLiteral {
val: bi,
bits: $width,
}
}
}
};
}
define_literal_from_uint!(u128, 128);
define_literal_from_uint!(u64, 64);
define_literal_from_uint!(u32, 32);
define_literal_from_uint!(u16, 16);
define_literal_from_uint!(u8, 8);
#[cfg(target_pointer_width = "64")]
define_literal_from_uint!(usize, 64);
#[cfg(target_pointer_width = "32")]
define_literal_from_uint!(usize, 32);
impl<const N: usize> From<Bits<N>> for VerilogLiteral {
fn from(x: Bits<N>) -> Self {
let mut z = BigUint::default();
for i in 0..N {
z.set_bit(i as u64, x.get_bit(i));
}
VerilogLiteral { val: z, bits: N }
}
}
impl Display for VerilogLiteral {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let bits = self.bits;
Display::fmt(&bits, f)?;
Display::fmt("'", f)?;
if bits % 4 != 0 && self.bits < 20 {
Display::fmt("b", f)?;
std::fmt::Binary::fmt(&self.val, f)
} else {
Display::fmt("h", f)?;
std::fmt::LowerHex::fmt(&self.val, f)
}
}
}
impl LowerHex for VerilogLiteral {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let bits = self.bits;
Display::fmt(&bits, f)?;
Display::fmt("'h", f)?;
LowerHex::fmt(&self.val, f)
}
}
#[derive(Debug, Clone)]
pub enum VerilogExpression {
Signal(String),
Literal(VerilogLiteral),
Cast(Box<VerilogExpression>, usize),
Paren(Box<VerilogExpression>),
Binary(Box<VerilogExpression>, VerilogOp, Box<VerilogExpression>),
Unary(VerilogOpUnary, Box<VerilogExpression>),
Index(Box<VerilogExpression>, Box<VerilogExpression>),
Slice(Box<VerilogExpression>, usize, Box<VerilogExpression>),
IndexReplace(
Box<VerilogExpression>,
Box<VerilogExpression>,
Box<VerilogExpression>,
),
}
#[derive(Debug, Clone)]
pub enum VerilogOp {
Add,
Sub,
Mul,
LogicalAnd,
LogicalOr,
BitXor,
BitAnd,
BitOr,
Shl,
Shr,
Eq,
Lt,
Le,
Ne,
Ge,
Gt,
}
#[derive(Debug, Clone)]
pub enum VerilogOpUnary {
Not,
Neg,
All,
Any,
Xor,
}