-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdl_dsl.hpp
319 lines (269 loc) · 8.95 KB
/
hdl_dsl.hpp
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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
// Copyright 2023 Can Joshua Lehmann
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef HDL_DSL
#define HDL_DSL
#include <inttypes.h>
#include <functional>
#include <vector>
#include "hdl.hpp"
namespace hdl {
namespace dsl {
class GlobalContext {
private:
Module* _module = nullptr;
Value* _clock = nullptr;
std::vector<Value*> _conditions;
public:
GlobalContext() {}
Module& module() {
if (_module == nullptr) {
throw Error("No module");
}
return *_module;
}
Value* clock() {
if (_clock == nullptr) {
throw Error("No clock");
}
return _clock;
}
Value* condition() {
Value* condition = module().constant(BitString::from_bool(true));
for (Value* cond : _conditions) {
condition = module().op(Op::Kind::And, {
condition, cond
});
}
return condition;
}
void when(Value* cond,
const std::function<void()>& then,
const std::function<void()>& otherwise) {
_conditions.push_back(cond);
then();
_conditions.pop_back();
_conditions.push_back(_module->op(Op::Kind::Not, {cond}));
otherwise();
_conditions.pop_back();
}
void on(Value* clock, const std::function<void()>& body) {
Value* old_clock = _clock;
_clock = clock;
body();
_clock = old_clock;
}
void synth(Module& module, const std::function<void()>& body) {
Module* old_module = _module;
_module = &module;
body();
_module = old_module;
}
};
GlobalContext global_context;
template <size_t Width>
class Val {
private:
Module& _module;
Value* _value = nullptr;
protected:
void expect_same_module(const Val<Width>& other) const {
if (&_module != &other._module) {
throw Error("Not same module");
}
}
public:
Val(): _module(global_context.module()) {
_value = _module.constant(BitString(Width));
}
Val(Module& module, Value* value):
_module(module), _value(value) {}
Module& module() const { return _module; }
Value* value() const { return _value; }
};
template <class T>
class Reg: public T {
private:
static hdl::Value* create_register(const T& initial) {
hdl::Constant* const_initial = dynamic_cast<hdl::Constant*>(initial.value());
if (const_initial == nullptr) {
throw Error("Register initializer must be constant");
}
return initial.module().reg(const_initial->value, nullptr);
}
static hdl::Value* create_register() {
T initial;
return create_register(initial);
}
public:
Reg(): T(global_context.module(), create_register()) {}
Reg(const T& initial): T(initial.module(), create_register(initial)) {}
void set_name(const std::string& name) {
hdl::Reg* reg = dynamic_cast<hdl::Reg*>(this->value());
reg->name = name;
}
Reg<T>& operator=(const T& val) {
hdl::Reg* reg = dynamic_cast<hdl::Reg*>(this->value());
reg->clock = global_context.clock();
reg->next = global_context.module().op(Op::Kind::Select, {
global_context.condition(),
val.value(),
reg->next
});
return *this;
}
};
template <class T>
class Input: public T {
private:
template <size_t Width>
static hdl::Value* create_input(const char* name, const Val<Width>&) {
return global_context.module().input(name, Width);
}
public:
Input(const char* name): T(global_context.module(), create_input(name, T())) {}
};
class Bool: public Val<1> {
private:
static Value* create_bool(bool value) {
return global_context.module().constant(BitString::from_bool(value));
}
public:
using Val<1>::Val;
using Val<1>::module;
using Val<1>::value;
Bool(bool value): Val<1>(global_context.module(), create_bool(value)) {}
template <class T>
T select(const T& then, const T& otherwise) {
Value* result = Val<1>::module().op(Op::Kind::Select, {
value(),
then.value(),
otherwise.value()
});
return T(Val<1>::module(), result);
}
#define binop(op_name, kind, invert) \
Bool operator op_name(const Bool& other) const { \
Val<1>::expect_same_module(other); \
Value* result = module().op(Op::Kind::kind, {value(), other.value()}); \
if (invert) { \
result = module().op(Op::Kind::Not, {result}); \
} \
return Bool(module(), result); \
}
binop(&, And, false);
binop(|, Or, false);
binop(&&, And, false);
binop(||, Or, false);
binop(^, Xor, false);
binop(==, Eq, false);
binop(!=, Eq, true);
#undef binop
Bool operator!() const {
Value* result = module().op(Op::Kind::Not, {value()});
return Bool(module(), result);
}
Bool operator~() const {
return !(*this);
}
};
template <size_t Width>
class U: public Val<Width> {
private:
static hdl::Value* create_constant(uint64_t constant) {
BitString bit_string = BitString::from_uint(constant).truncate(Width);
return global_context.module().constant(bit_string);
}
public:
using Val<Width>::Val;
using Val<Width>::module;
using Val<Width>::value;
U(uint64_t constant):
Val<Width>(global_context.module(), create_constant(constant)) {}
#define binop(op_name, kind) \
U<Width> operator op_name(const U<Width>& other) const { \
Val<Width>::expect_same_module(other); \
Value* result = module().op(Op::Kind::kind, {value(), other.value()}); \
return U<Width>(module(), result); \
}
binop(&, And)
binop(|, Or)
binop(^, Xor)
binop(+, Add)
binop(-, Sub)
binop(<<, Shl)
binop(>>, ShrU)
#undef binop
#define cmp(op_name, kind, invert, swap_args) \
Bool operator op_name(const U<Width>& other) const { \
Val<Width>::expect_same_module(other); \
std::vector<Value*> args = {value(), other.value()}; \
if (swap_args) { \
std::swap(args[0], args[1]); \
} \
Value* result = module().op(Op::Kind::kind, args); \
if (invert) { \
result = module().op(Op::Kind::Not, {result}); \
} \
return Bool(module(), result); \
}
cmp(==, Eq, false, false)
cmp(!=, Eq, true, false)
cmp(<, LtU, false, false)
cmp(>, LtU, false, true)
cmp(>=, LtU, true, false)
cmp(<=, LtU, true, true)
#undef cmp
};
template <class T, size_t Size>
class Mem {
private:
Module& _module;
Memory* _memory = nullptr;
template <size_t Width>
static Memory* create_memory(const Val<Width>&) {
return global_context.module().memory(Width, Size);
}
public:
Mem(): _module(global_context.module()), _memory(create_memory(T())) {}
void set_name(const std::string& name) {
_memory->name = name;
}
template <size_t AddressWidth>
T operator[](const U<AddressWidth>& address) {
return T(_module, _memory->read(address.value()));
}
template <size_t AddressWidth>
void write(const U<AddressWidth>& address, const T& value) {
_memory->write(global_context.clock(), address.value(), global_context.condition(), value.value());
}
};
void on(const Bool& clock, const std::function<void()>& body) {
global_context.on(clock.value(), body);
}
void when(const Bool& cond,
const std::function<void()>& then,
const std::function<void()>& otherwise) {
global_context.when(cond.value(), then, otherwise);
}
void when(const Bool& cond,
const std::function<void()>& then) {
when(cond, then, [](){});
}
void synth(Module& module, const std::function<void()>& body) {
global_context.synth(module, body);
}
}
}
#define $ [&]()
#endif