-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathir_verilog.py
328 lines (289 loc) · 10.2 KB
/
ir_verilog.py
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
320
321
322
323
324
325
326
327
328
"""Helper functions to create Verilog modules in Rapidstream IR."""
__copyright__ = """
Copyright (c) 2024 RapidStream Design Automation, Inc. and contributors.
All rights reserved. The contributor(s) of this file has/have agreed to the
RapidStream Contributor License Agreement.
"""
from typing import Any
from ir_helper import create_parameter_ir, create_port_ir
def create_const_one_driver() -> dict[str, Any]:
"""Create the Const_1_Driver module definition.
Return a dictionary IR.
"""
return {
"name": "Const_1_Driver",
"hierarchical_name": ["Const_1_Driver"],
"module_type": "verilog_module",
"parameters": [],
"ports": [create_port_ir("out", "output wire", "0", "0")],
"verilog": """
module Const_1_Driver(
output out
);
assign out = 1'b1;
endmodule
""",
"metadata": None,
"submodules_module_names": [],
}
def create_cc_master(params: dict[str, str]) -> dict[str, Any]:
"""Create the credit_control_master module definition.
Return a dictionary IR.
"""
return {
"name": "credit_control_mastser",
"hierarchical_name": ["credit_control_mastser"],
"module_type": "verilog_module",
"parameters": [
create_parameter_ir(param, val) for param, val in params.items()
],
"ports": [
create_port_ir("clk", "input wire", "0", "0"),
create_port_ir("rst_n", "input wire", "0", "0"),
create_port_ir("credit_valid_i", "input wire", "0", "0"),
create_port_ir(
"credit_i", "input wire", str(int(params["CREDIT_CNT_WIDTH"]) - 1), "0"
),
create_port_ir("empty_n_i", "input wire", "0", "0"),
create_port_ir("tready_i", "input wire", "0", "0"),
create_port_ir("read_o", "output wire", "0", "0"),
create_port_ir("tvalid_o", "output wire", "0", "0"),
],
"verilog": f"""
module credit_control_master # (
parameter INIT_CREDIT = {params['INIT_CREDIT']},
parameter CREDIT_CNT_WIDTH = {params['CREDIT_CNT_WIDTH']}
) (
input wire clk,
input wire rst_n,
input wire credit_valid_i,
input wire [CREDIT_CNT_WIDTH - 1:0] credit_i,
input wire empty_n_i,
input wire tready_i,
output wire read_o,
output wire tvalid_o
);
reg [CREDIT_CNT_WIDTH - 1:0] credit_cnt_w;
reg [CREDIT_CNT_WIDTH - 1:0] credit_cnt_r;
always @ (*) begin
credit_cnt_w = credit_cnt_r;
if (empty_n_i && tready_i && (credit_cnt_r > 0)) begin
credit_cnt_w = credit_cnt_w - 1'b1;
end
if (credit_valid_i) begin
credit_cnt_w = credit_cnt_w + credit_i;
end
end
always @(posedge clk) begin
if (!rst_n) credit_cnt_r <= INIT_CREDIT;
else credit_cnt_r <= credit_cnt_w;
end
assign read_o = tready_i & (credit_cnt_r > 0);
assign tvalid_o = empty_n_i & (credit_cnt_r > 0);
endmodule
""",
"metadata": None,
"submodules_module_names": [],
}
def create_cc_slave(params: dict[str, str]) -> dict[str, Any]:
"""Create the credit_control_slave module definition.
Return a dictionary IR.
"""
return {
"name": "credit_control_slave",
"hierarchical_name": ["credit_control_slave"],
"module_type": "verilog_module",
"parameters": [
create_parameter_ir(param, val) for param, val in params.items()
],
"ports": [
create_port_ir("clk", "input wire", "0", "0"),
create_port_ir("rst_n", "input wire", "0", "0"),
create_port_ir("read_i", "input wire", "0", "0"),
create_port_ir("empty_n_i", "input wire", "0", "0"),
create_port_ir("credit_valid_o", "output wire", "0", "0"),
create_port_ir(
"credit_o", "output wire", str(int(params["CREDIT_CNT_WIDTH"]) - 1), "0"
),
],
"verilog": f"""
module credit_control_slave # (
parameter INIT_CREDIT = {params['INIT_CREDIT']},
parameter CREDIT_CNT_WIDTH = {params['CREDIT_CNT_WIDTH']},
parameter TIMER_WIDTH = {params['TIMER_WIDTH']}
) (
input wire clk,
input wire rst_n,
input wire read_i,
input wire empty_n_i,
output wire credit_valid_o,
output wire [CREDIT_CNT_WIDTH - 1:0] credit_o
);
reg [TIMER_WIDTH - 1:0] timer;
reg [CREDIT_CNT_WIDTH - 1:0] credit_cnt_r;
reg credit_valid_o_r;
reg [CREDIT_CNT_WIDTH - 1:0] credit_o_r;
wire send_credit;
assign send_credit = (&timer) | (credit_cnt_r == (INIT_CREDIT >> 1));
always @ (posedge clk) begin
if (!rst_n) timer <= 0;
else begin
if (send_credit) timer <= 0;
else if (credit_cnt_r > 0) timer <= timer + 1;
end
end
always @ (posedge clk) begin
if (!rst_n) credit_cnt_r <= 0;
else begin
if (read_i & empty_n_i) begin
if (send_credit) begin
credit_cnt_r <= 1;
end else begin
credit_cnt_r <= credit_cnt_r + 1;
end
end
else if (send_credit) begin
credit_cnt_r <= 0;
end
end
end
always @ (posedge clk) begin
if (!rst_n) begin
credit_valid_o_r <= 0;
credit_o_r <= 0;
end else begin
credit_o_r <= credit_cnt_r;
credit_valid_o_r <= 0;
if (send_credit) begin
credit_valid_o_r <= 1'b1;
end
end
end
assign credit_valid_o = credit_valid_o_r;
assign credit_o = credit_o_r;
endmodule
""",
"metadata": None,
"submodules_module_names": [],
}
def create_cc_slave_group(params: dict[str, str]) -> dict[str, Any]:
"""Create the credit_control_slave_group module definition.
Return a dictionary IR.
"""
return {
"name": "credit_control_slave",
"hierarchical_name": ["credit_control_slave"],
"module_type": "verilog_module",
"parameters": [
create_parameter_ir(param, val) for param, val in params.items()
],
"ports": [
create_port_ir("clk", "input wire", "0", "0"),
create_port_ir("rst_n", "input wire", "0", "0"),
create_port_ir(
"read_i", "input wire", str(int(params["GROUP_SIZE"]) - 1), "0"
),
create_port_ir(
"empty_n_i", "input wire", str(int(params["GROUP_SIZE"]) - 1), "0"
),
create_port_ir("ready_i", "input wire", "0", "0"),
create_port_ir("credit_valid_o", "output wire", "0", "0"),
create_port_ir(
"credit_o",
"output wire",
str(int(params["CREDIT_CNT_WIDTH"]) * int(params["GROUP_SIZE"]) - 1),
"0",
),
],
"verilog": f"""
module credit_control_slave_group # (
parameter INIT_CREDIT = {params['INIT_CREDIT']},
parameter CREDIT_CNT_WIDTH = {params['CREDIT_CNT_WIDTH']},
parameter TIMER_WIDTH = {params['TIMER_WIDTH']},
parameter GROUP_SIZE = {params['GROUP_SIZE']}
) (
input wire clk,
input wire rst_n,
input wire [GROUP_SIZE - 1:0] read_i,
input wire [GROUP_SIZE - 1:0] empty_n_i,
input wire ready_i,
output wire credit_valid_o,
output wire [GROUP_SIZE - 1:0][CREDIT_CNT_WIDTH - 1:0] credit_o
);
reg [TIMER_WIDTH - 1:0] timer;
reg [CREDIT_CNT_WIDTH - 1:0] credit_cnt_r [GROUP_SIZE - 1:0];
reg credit_valid_o_r;
reg [CREDIT_CNT_WIDTH - 1:0] credit_o_r [GROUP_SIZE - 1:0];
wire [GROUP_SIZE - 1:0] credits_loaded;
wire [GROUP_SIZE - 1:0] credits_present;
wire send_credit;
wire credit_sent;
assign send_credit = (&timer) | (|credits_loaded);
assign credit_sent = credit_valid_o_r & ready_i;
always @ (posedge clk) begin
if (!rst_n) timer <= 0;
else begin
if (send_credit) timer <= 0;
else if (&timer) timer <= timer;
else if (|credits_present) timer <= timer + 1;
end
end
genvar i;
generate
for (i = 0; i < GROUP_SIZE; i = i + 1) begin : credit_cnt_gen_block
always @ (posedge clk) begin
if (!rst_n) credit_cnt_r[i] <= 0;
else begin
if (read_i[i] & empty_n_i[i]) begin
if (send_credit) begin
credit_cnt_r[i] <= 1;
end else begin
credit_cnt_r[i] <= credit_cnt_r[i] + 1;
end
end
else if (send_credit) begin
credit_cnt_r[i] <= 0;
end
end
end
// loaded enough credits -- half of the buffer size
assign credits_loaded[i] = credit_cnt_r[i] == (INIT_CREDIT >> 1);
assign credits_present[i] = credit_cnt_r[i] > 0;
end
endgenerate
integer j;
always @ (posedge clk) begin
if (!rst_n) begin
for (j = 0; j < GROUP_SIZE; j = j + 1) begin
credit_o_r[j] <= 0;
end
credit_valid_o_r <= 0;
end else begin
for (j = 0; j < GROUP_SIZE; j = j + 1) begin
if (send_credit) begin
credit_o_r[j] <= credit_cnt_r[j];
end
else begin
credit_o_r[j] <= credit_o_r[j];
end
end
credit_valid_o_r <= credit_valid_o_r;
if (credit_sent) begin
credit_valid_o_r <= 1'b0;
end
else if (send_credit) begin
credit_valid_o_r <= 1'b1;
end
end
end
assign credit_valid_o = credit_valid_o_r;
generate
for (i = 0; i < GROUP_SIZE; i = i + 1) begin : credit_o_assign_block
assign credit_o[i] = credit_o_r[i];
end
endgenerate
endmodule
""",
"metadata": None,
"submodules_module_names": [],
}