1 |
879 |
markom |
/* verilog.c -- OpenRISC Custom Unit Compiler, verilog generator
|
2 |
|
|
* Copyright (C) 2002 Marko Mlinar, markom@opencores.org
|
3 |
|
|
*
|
4 |
|
|
* This file is part of OpenRISC 1000 Architectural Simulator.
|
5 |
|
|
*
|
6 |
|
|
* This program is free software; you can redistribute it and/or modify
|
7 |
|
|
* it under the terms of the GNU General Public License as published by
|
8 |
|
|
* the Free Software Foundation; either version 2 of the License, or
|
9 |
|
|
* (at your option) any later version.
|
10 |
|
|
*
|
11 |
|
|
* This program is distributed in the hope that it will be useful,
|
12 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
14 |
|
|
* GNU General Public License for more details.
|
15 |
|
|
*
|
16 |
|
|
* You should have received a copy of the GNU General Public License
|
17 |
|
|
* along with this program; if not, write to the Free Software
|
18 |
|
|
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
|
19 |
|
|
|
20 |
|
|
#include <stdio.h>
|
21 |
|
|
#include <stdlib.h>
|
22 |
|
|
#include <stdarg.h>
|
23 |
|
|
#include <assert.h>
|
24 |
|
|
#include "cuc.h"
|
25 |
|
|
#include "insn.h"
|
26 |
|
|
|
27 |
907 |
markom |
/* returns log2(x) */
|
28 |
|
|
int log2 (unsigned long x)
|
29 |
879 |
markom |
{
|
30 |
|
|
int c = 0;
|
31 |
907 |
markom |
assert (x >= 0);
|
32 |
|
|
if (!x) return 0; /* not by the book, but practical */
|
33 |
|
|
while (x != 1) x >>= 1, c++;
|
34 |
|
|
return c;
|
35 |
|
|
}
|
36 |
|
|
|
37 |
|
|
/* Find index of load/store/call */
|
38 |
|
|
int find_lsc_index (cuc_func *f, int ref)
|
39 |
|
|
{
|
40 |
|
|
int c = 0;
|
41 |
879 |
markom |
int i;
|
42 |
907 |
markom |
int load;
|
43 |
|
|
|
44 |
|
|
if (f->INSN(ref).index == II_CALL) {
|
45 |
|
|
for (i = 0; i < f->nmsched; i++) {
|
46 |
|
|
if (f->msched[i] == ref) break;
|
47 |
|
|
if (f->mtype[i] & MT_CALL) c++;
|
48 |
|
|
}
|
49 |
|
|
} else {
|
50 |
|
|
load = II_IS_LOAD (f->INSN(ref).index);
|
51 |
|
|
for (i = 0; i < f->nmsched; i++) {
|
52 |
|
|
if (f->msched[i] == ref) break;
|
53 |
|
|
if (load && (f->mtype[i] & MT_LOAD)
|
54 |
|
|
|| !load && (f->mtype[i] & MT_STORE)) c++;
|
55 |
|
|
}
|
56 |
879 |
markom |
}
|
57 |
|
|
return c;
|
58 |
|
|
}
|
59 |
|
|
|
60 |
|
|
/* Print out dependencies as verilog expression */
|
61 |
|
|
void print_deps (FILE *fo, cuc_func *f, int b, dep_list *t, int registered)
|
62 |
|
|
{
|
63 |
|
|
if (t) {
|
64 |
|
|
int first = 0;
|
65 |
|
|
while (t) {
|
66 |
907 |
markom |
if (f->INSN(t->ref).type & IT_MEMORY) {
|
67 |
|
|
fprintf (fo, "%s%c_end[%i]", first ? " && " : "",
|
68 |
|
|
II_IS_LOAD (f->INSN(t->ref).index) ? 'l' : 's', find_lsc_index (f, t->ref));
|
69 |
|
|
} else if (f->INSN(t->ref).index == II_CALL) {
|
70 |
|
|
int x;
|
71 |
|
|
fprintf (fo, "%sf_end[%i]", first ? " && " : "", find_lsc_index (f, t->ref));
|
72 |
|
|
} else {
|
73 |
|
|
printf ("print_deps: err %x\n", t->ref);
|
74 |
|
|
assert (0);
|
75 |
|
|
}
|
76 |
879 |
markom |
first = 1;
|
77 |
|
|
t = t->next;
|
78 |
|
|
}
|
79 |
|
|
} else {
|
80 |
|
|
if (registered) fprintf (fo, "bb_start_r[%i]", b);
|
81 |
|
|
else fprintf (fo, "bb_start[%i]", b);
|
82 |
|
|
}
|
83 |
|
|
}
|
84 |
|
|
|
85 |
|
|
char *print_op_v (cuc_func *f, char *s, int ref, int j)
|
86 |
|
|
{
|
87 |
|
|
unsigned long op = f->INSN(ref).op[j];
|
88 |
|
|
unsigned long opt = f->INSN(ref).opt[j];
|
89 |
|
|
switch (opt & ~OPT_DEST) {
|
90 |
|
|
case OPT_NONE: assert (0); break;
|
91 |
906 |
markom |
case OPT_CONST: if (f->INSN(ref).type & IT_COND) {
|
92 |
|
|
assert (op == 0 || op == 1);
|
93 |
|
|
sprintf (s, "1'b%x", op);
|
94 |
|
|
} else sprintf (s, "32'h%x", op);
|
95 |
|
|
break;
|
96 |
879 |
markom |
case OPT_REGISTER:
|
97 |
|
|
if (opt & OPT_DEST) sprintf (s, "t%x_%x", REF_BB(ref), REF_I(ref));
|
98 |
|
|
else sprintf (s, "r%i_%c", op, opt & OPT_DEST ? 'o' : 'i');
|
99 |
|
|
break;
|
100 |
907 |
markom |
#if 0
|
101 |
|
|
case OPT_FREG: assert (opt & OPT_DEST);
|
102 |
|
|
sprintf (s, "fr%i_o", op);
|
103 |
|
|
break;
|
104 |
|
|
#endif
|
105 |
879 |
markom |
case OPT_REF: sprintf (s, "t%x_%x", REF_BB(op), REF_I(op)); break;
|
106 |
|
|
}
|
107 |
|
|
return s;
|
108 |
|
|
}
|
109 |
|
|
|
110 |
|
|
/* Prints out specified instruction */
|
111 |
|
|
void print_insn_v (FILE *fo, cuc_func *f, int b, int i)
|
112 |
|
|
{
|
113 |
|
|
cuc_insn *ii = &f->bb[b].insn[i];
|
114 |
|
|
char *s = known[ii->index].rtl;
|
115 |
|
|
char tmp[200] = "";
|
116 |
883 |
markom |
|
117 |
879 |
markom |
while (*s) {
|
118 |
|
|
if (*s <= MAX_OPERANDS) {
|
119 |
|
|
char t[30];
|
120 |
|
|
sprintf (tmp, "%s%s", tmp, print_op_v (f, t, REF(b, i), *s - 1));
|
121 |
|
|
} else if (*s == '\b') sprintf (tmp, "%s%i", b);
|
122 |
|
|
else sprintf (tmp, "%s%c", tmp, *s);
|
123 |
|
|
s++;
|
124 |
|
|
}
|
125 |
|
|
fprintf (fo, "%-40s /* %s */\n", tmp, ii->disasm);
|
126 |
|
|
if (ii->type & IT_MEMORY) {
|
127 |
907 |
markom |
int j, nls = find_lsc_index (f, REF (b, i));
|
128 |
879 |
markom |
if (II_IS_LOAD (ii->index)) {
|
129 |
|
|
int nm;
|
130 |
|
|
for (nm = 0; nm < f->nmsched; nm++) if (f->msched[nm] == REF (b, i)) break;
|
131 |
|
|
assert (nm < f->nmsched);
|
132 |
|
|
|
133 |
|
|
fprintf (fo, " if (rst) t%x_%x <= #1 32'h0;\n", b, i);
|
134 |
|
|
fprintf (fo, " else if (l_end[%i]) t%x_%x <= #1 ", nls, b, i);
|
135 |
|
|
switch (f->mtype[nm] & (MT_WIDTH | MT_SIGNED)) {
|
136 |
|
|
case 1: fprintf (fo, "lwb_dat_i & 32'hff;\n");
|
137 |
|
|
break;
|
138 |
|
|
case 2: fprintf (fo, "lwb_dat_i & 32'hffff;\n");
|
139 |
|
|
break;
|
140 |
|
|
case 4 | MT_SIGNED:
|
141 |
|
|
case 4: fprintf (fo, "lwb_dat_i;\n");
|
142 |
|
|
break;
|
143 |
|
|
case 1 | MT_SIGNED:
|
144 |
|
|
fprintf (fo, "{24{lwb_dat_i[7]}, lwb_dat_i[7:0]};\n");
|
145 |
|
|
break;
|
146 |
|
|
case 2 | MT_SIGNED:
|
147 |
|
|
fprintf (fo, "{16{lwb_dat_i[15]}, lwb_dat_i[15:0]};\n");
|
148 |
|
|
break;
|
149 |
|
|
default: assert (0);
|
150 |
|
|
}
|
151 |
|
|
}
|
152 |
|
|
} else if (ii->index == II_LRBB) {
|
153 |
|
|
fprintf (fo, " if (rst) t%x_%x <= #1 1'b0;\n", b, i);
|
154 |
|
|
assert (f->bb[b].prev[0] >= 0);
|
155 |
|
|
fprintf (fo, " else if (bb_start[%i]) t%x_%x <= #1 bb_stb[%i];\n", b, b, i, f->bb[b].prev[0]);
|
156 |
|
|
} else if (ii->index == II_REG) {
|
157 |
|
|
fprintf (fo, " if (rst) t%x_%x <= #1 32'h0;\n", b, i);
|
158 |
|
|
assert (ii->opt[1] == OPT_REF);
|
159 |
|
|
fprintf (fo, " else if (");
|
160 |
|
|
if (f->bb[b].mdep) print_deps (fo, f, b, f->bb[b].mdep, 0);
|
161 |
|
|
else fprintf (fo, "bb_stb[%i]", b);
|
162 |
|
|
fprintf (fo, ") t%x_%x <= #1 t%x_%x;\n", b, i,
|
163 |
|
|
REF_BB (ii->op[1]), REF_I (ii->op[1]));
|
164 |
|
|
}
|
165 |
|
|
}
|
166 |
|
|
|
167 |
|
|
/* Outputs binary number */
|
168 |
907 |
markom |
static char *bin_str (unsigned long x, int len)
|
169 |
879 |
markom |
{
|
170 |
|
|
static char bx[33];
|
171 |
|
|
char *s = bx;
|
172 |
|
|
while (len > 0) *s++ = '0' + ((x >> --len) & 1);
|
173 |
|
|
*s = '\0';
|
174 |
|
|
return bx;
|
175 |
|
|
}
|
176 |
|
|
|
177 |
|
|
/* Returns index of branch instruction inside a block b */
|
178 |
907 |
markom |
static int branch_index (cuc_bb *bb)
|
179 |
879 |
markom |
{
|
180 |
|
|
int i;
|
181 |
|
|
for (i = bb->ninsn - 1; i >= 0; i--)
|
182 |
|
|
if (bb->insn[i].type & IT_BRANCH) return i;
|
183 |
|
|
return -1;
|
184 |
|
|
}
|
185 |
|
|
|
186 |
907 |
markom |
static void print_turn_off_dep (FILE *fo, cuc_func *f, dep_list *dep)
|
187 |
|
|
{
|
188 |
|
|
while (dep) {
|
189 |
|
|
assert (f->INSN(dep->ref).type & IT_MEMORY || f->INSN(dep->ref).index == II_CALL);
|
190 |
|
|
fprintf (fo, " %c_stb[%i] <= #1 1'b0;\n", f->INSN(dep->ref).index == II_CALL ? 'f'
|
191 |
|
|
: II_IS_LOAD (f->INSN(dep->ref).index) ? 'l' : 's', find_lsc_index (f, dep->ref));
|
192 |
|
|
dep = dep->next;
|
193 |
|
|
}
|
194 |
|
|
}
|
195 |
|
|
|
196 |
|
|
static int func_index (cuc_func *f, int ref)
|
197 |
|
|
{
|
198 |
|
|
int i;
|
199 |
|
|
unsigned long addr;
|
200 |
|
|
assert (f->INSN(ref).index == II_CALL && f->INSN(ref).opt[0] & OPT_CONST);
|
201 |
|
|
addr = f->INSN(ref).op[0];
|
202 |
|
|
for (i = 0; i < f->nfdeps; i++)
|
203 |
|
|
if (f->fdeps[i]->start_addr == addr) return i;
|
204 |
|
|
|
205 |
|
|
assert (0);
|
206 |
|
|
return -1;
|
207 |
|
|
}
|
208 |
|
|
|
209 |
879 |
markom |
/* Generates verilog file out of insn dataflow */
|
210 |
|
|
void output_verilog (cuc_func *f, char *filename)
|
211 |
|
|
{
|
212 |
|
|
FILE *fo;
|
213 |
|
|
int b, i, j;
|
214 |
|
|
int ci = 0, co = 0;
|
215 |
907 |
markom |
int nloads = 0, nstores = 0, ncalls = 0;
|
216 |
879 |
markom |
char tmp[256];
|
217 |
|
|
cuc_bb *end_bb = NULL;
|
218 |
|
|
int end_bb_no = -1;
|
219 |
|
|
sprintf (tmp, "%s.v", filename);
|
220 |
|
|
|
221 |
902 |
markom |
log ("Generating verilog file \"%s\"\n", tmp);
|
222 |
|
|
printf ("Generating verilog file \"%s\"\n", tmp);
|
223 |
879 |
markom |
if ((fo = fopen (tmp, "wt+")) == NULL) {
|
224 |
|
|
fprintf (stderr, "Cannot open '%s'\n", tmp);
|
225 |
|
|
exit (1);
|
226 |
|
|
}
|
227 |
|
|
|
228 |
883 |
markom |
for (b = 0; b < f->num_bb; b++)
|
229 |
879 |
markom |
if (f->bb[b].type & BB_END) end_bb = &f->bb[end_bb_no = b];
|
230 |
|
|
assert (end_bb && end_bb->type & BB_END);
|
231 |
|
|
|
232 |
|
|
/* output header */
|
233 |
902 |
markom |
fprintf (fo, "/* %s -- generated by OpenRISC Custom Unit Compiler\n", tmp);
|
234 |
|
|
fprintf (fo, " (C) 2002 OpenCores.\n");
|
235 |
|
|
fprintf (fo, " function \"%s\"\n", filename);
|
236 |
|
|
fprintf (fo, " at %08x - %08x\n", f->start_addr, f->end_addr);
|
237 |
|
|
fprintf (fo, " num BBs %i */\n\n", f->num_bb);
|
238 |
879 |
markom |
fprintf (fo, "module %s (clk, rst,\n", filename);
|
239 |
|
|
fprintf (fo, " lwb_adr_o, lwb_dat_i, lwb_cycstb_o,\n");
|
240 |
|
|
fprintf (fo, " lwb_sel_o, lwb_linbrst_o, lwb_ack_i,\n");
|
241 |
|
|
fprintf (fo, " swb_adr_o, swb_dat_o, swb_cycstb_o,\n");
|
242 |
|
|
fprintf (fo, " swb_sel_o, swb_linbrst_o, swb_ack_i,\n");
|
243 |
|
|
|
244 |
|
|
fprintf (fo, "/* inputs */ ");
|
245 |
|
|
for (i = 0; i < MAX_REGS; i++)
|
246 |
883 |
markom |
if (f->used_regs[i]) {
|
247 |
879 |
markom |
fprintf (fo, "r%i_i, ", i);
|
248 |
|
|
ci++;
|
249 |
|
|
}
|
250 |
|
|
if (!ci) fprintf (fo, "/* NONE */");
|
251 |
|
|
|
252 |
|
|
fprintf (fo, "\n/* outputs */ ");
|
253 |
|
|
for (i = 0; i < MAX_REGS; i++)
|
254 |
883 |
markom |
if (f->lur[i] >= 0 && !f->saved_regs[i]) {
|
255 |
879 |
markom |
fprintf (fo, "r%i_o, ", i);
|
256 |
|
|
co++;
|
257 |
|
|
}
|
258 |
|
|
|
259 |
|
|
if (!co) fprintf (fo, "/* NONE */");
|
260 |
907 |
markom |
if (f->nfdeps) {
|
261 |
|
|
fprintf (fo, "\n/* f. calls */, fstart_o, %sfend_i, fr11_i, ",
|
262 |
|
|
log2 (f->nfdeps) > 0 ? "fid_o, " : "");
|
263 |
|
|
for (i = 0; i < 6; i++) fprintf (fo, "fr%i_o, ", i + 3);
|
264 |
|
|
}
|
265 |
908 |
markom |
fprintf (fo, "\n start_i, end_o, busy_o);\n\n");
|
266 |
879 |
markom |
|
267 |
|
|
fprintf (fo, "input clk, rst;\n");
|
268 |
|
|
fprintf (fo, "input start_i;\t/* Module starts when set to 1 */ \n");
|
269 |
908 |
markom |
fprintf (fo, "output end_o;\t/* Set when module finishes, cleared upon start_i == 1 */\n");
|
270 |
|
|
fprintf (fo, "output busy_o;\t/* Set when module should not be interrupted */\n");
|
271 |
|
|
fprintf (fo, "\n/* Bus signals */\n");
|
272 |
879 |
markom |
fprintf (fo, "output lwb_cycstb_o, swb_cycstb_o;\n");
|
273 |
|
|
fprintf (fo, "input lwb_ack_i, swb_ack_i;\n");
|
274 |
|
|
fprintf (fo, "output [3:0] lwb_sel_o, swb_sel_o;\n");
|
275 |
|
|
fprintf (fo, "output [31:0] lwb_adr_o, swb_adr_o;\n");
|
276 |
|
|
fprintf (fo, "output lwb_linbrst_o, swb_linbrst_o;\n");
|
277 |
|
|
fprintf (fo, "input [31:0] lwb_dat_i;\n");
|
278 |
|
|
fprintf (fo, "output [31:0] swb_dat_o;\n\n");
|
279 |
|
|
|
280 |
|
|
fprintf (fo, "reg lwb_cycstb_o, swb_cycstb_o;\n");
|
281 |
|
|
fprintf (fo, "reg [31:0] lwb_adr_o, swb_adr_o;\n");
|
282 |
|
|
fprintf (fo, "reg [3:0] lwb_sel_o, swb_sel_o;\n");
|
283 |
|
|
fprintf (fo, "reg [31:0] swb_dat_o;\n");
|
284 |
|
|
fprintf (fo, "reg lwb_linbrst_o, swb_linbrst_o;\n");
|
285 |
|
|
|
286 |
|
|
if (ci || co) fprintf (fo, "\n/* module ports */\n");
|
287 |
|
|
if (ci) {
|
288 |
|
|
int first = 1;
|
289 |
|
|
fprintf (fo, "input [31:0]");
|
290 |
|
|
for (i = 0; i < MAX_REGS; i++)
|
291 |
883 |
markom |
if (f->used_regs[i]) {
|
292 |
879 |
markom |
fprintf (fo, "%sr%i_i", first ? " " : ", ", i);
|
293 |
|
|
first = 0;
|
294 |
|
|
}
|
295 |
|
|
fprintf (fo, ";\n");
|
296 |
|
|
}
|
297 |
|
|
|
298 |
|
|
if (co) {
|
299 |
|
|
int first = 1;
|
300 |
|
|
fprintf (fo, "output [31:0]");
|
301 |
|
|
for (i = 0; i < MAX_REGS; i++)
|
302 |
883 |
markom |
if (f->lur[i] >= 0 && !f->saved_regs[i]) {
|
303 |
879 |
markom |
fprintf (fo, "%sr%i_o", first ? " " : ", ", i);
|
304 |
|
|
first = 0;
|
305 |
|
|
}
|
306 |
|
|
fprintf (fo, ";\n");
|
307 |
|
|
}
|
308 |
|
|
|
309 |
907 |
markom |
if (f->nfdeps) {
|
310 |
|
|
fprintf (fo, "\n/* Function calls */\n");
|
311 |
|
|
fprintf (fo, "output [31:0] fr3_o");
|
312 |
|
|
for (i = 1; i < 6; i++) fprintf (fo, ", fr%i_o", i + 3);
|
313 |
|
|
fprintf (fo, ";\n");
|
314 |
|
|
if (log2(f->nfdeps) > 0) fprintf (fo, "output [%i:0] fid_o;\n", log2(f->nfdeps));
|
315 |
|
|
fprintf (fo, "output fstart_o;\n");
|
316 |
|
|
fprintf (fo, "input fend_i;\n");
|
317 |
|
|
}
|
318 |
|
|
|
319 |
879 |
markom |
/* Count loads & stores */
|
320 |
|
|
for (i = 0; i < f->nmsched; i++)
|
321 |
907 |
markom |
if (f->mtype[i] & MT_STORE) nstores++;
|
322 |
|
|
else if (f->mtype[i] & MT_LOAD) nloads++;
|
323 |
|
|
else if (f->mtype[i] & MT_CALL) ncalls++;
|
324 |
879 |
markom |
|
325 |
|
|
/* Output internal registers for loads */
|
326 |
|
|
if (nloads) {
|
327 |
|
|
int first = 1;
|
328 |
883 |
markom |
int num = 0;
|
329 |
879 |
markom |
fprintf (fo, "\n/* internal registers for loads */\n");
|
330 |
|
|
for (i = 0; i < f->nmsched; i++)
|
331 |
907 |
markom |
if (f->mtype[i] & MT_LOAD) {
|
332 |
879 |
markom |
fprintf (fo, "%st%x_%x", first ? "reg [31:0] " : ", ",
|
333 |
|
|
REF_BB(f->msched[i]), REF_I(f->msched[i]));
|
334 |
883 |
markom |
|
335 |
|
|
if (num >= 8) {
|
336 |
|
|
fprintf (fo, ";\n");
|
337 |
|
|
first = 1;
|
338 |
|
|
num = 0;
|
339 |
|
|
} else {
|
340 |
|
|
first = 0;
|
341 |
|
|
num++;
|
342 |
|
|
}
|
343 |
879 |
markom |
}
|
344 |
|
|
if (!first) fprintf (fo, ";\n");
|
345 |
|
|
}
|
346 |
|
|
|
347 |
907 |
markom |
/* Internal register for function return value */
|
348 |
|
|
if (f->nfdeps) {
|
349 |
|
|
fprintf (fo, "\n/* Internal register for function return value */\n");
|
350 |
|
|
fprintf (fo, "reg [31:0] fr11_r;\n");
|
351 |
|
|
}
|
352 |
|
|
|
353 |
879 |
markom |
fprintf (fo, "\n/* 'zero or one' hot state machines */\n");
|
354 |
|
|
if (nloads) fprintf (fo, "reg [%i:0] l_stb; /* loads */\n", nloads - 1);
|
355 |
|
|
if (nstores) fprintf (fo, "reg [%i:0] s_stb; /* stores */\n", nstores - 1);
|
356 |
|
|
fprintf (fo, "reg [%i:0] bb_stb; /* basic blocks */\n", f->num_bb - 1);
|
357 |
|
|
|
358 |
|
|
{
|
359 |
883 |
markom |
int first = 2;
|
360 |
879 |
markom |
int num = 0;
|
361 |
|
|
for (b = 0; b < f->num_bb; b++)
|
362 |
|
|
for (i = 0; i < f->bb[b].ninsn; i++)
|
363 |
|
|
if (f->bb[b].insn[i].type & IT_COND
|
364 |
|
|
&& f->bb[b].insn[i].index != II_REG
|
365 |
|
|
&& f->bb[b].insn[i].index != II_LRBB) {
|
366 |
883 |
markom |
if (first == 2) fprintf (fo, "\n/* basic block condition wires */\n");
|
367 |
879 |
markom |
fprintf (fo, "%st%x_%x", first ? "wire " : ", ", b, i);
|
368 |
883 |
markom |
if (num >= 8) {
|
369 |
879 |
markom |
fprintf (fo, ";\n");
|
370 |
|
|
first = 1;
|
371 |
|
|
num = 0;
|
372 |
|
|
} else {
|
373 |
|
|
first = 0;
|
374 |
|
|
num++;
|
375 |
|
|
}
|
376 |
|
|
}
|
377 |
|
|
if (!first) fprintf (fo, ";\n");
|
378 |
|
|
|
379 |
|
|
fprintf (fo, "\n/* forward declaration of normal wires */\n");
|
380 |
|
|
num = 0;
|
381 |
|
|
first = 1;
|
382 |
|
|
for (b = 0; b < f->num_bb; b++)
|
383 |
|
|
for (i = 0; i < f->bb[b].ninsn; i++)
|
384 |
|
|
if (!(f->bb[b].insn[i].type & (IT_COND | IT_BRANCH))
|
385 |
|
|
&& f->bb[b].insn[i].index != II_REG
|
386 |
|
|
&& f->bb[b].insn[i].index != II_LRBB) {
|
387 |
|
|
/* Exclude loads */
|
388 |
|
|
if (f->bb[b].insn[i].type & IT_MEMORY && II_IS_LOAD (f->bb[b].insn[i].index)) continue;
|
389 |
|
|
fprintf (fo, "%st%x_%x", first ? "wire [31:0] " : ", ", b, i);
|
390 |
883 |
markom |
if (num >= 8) {
|
391 |
879 |
markom |
fprintf (fo, ";\n");
|
392 |
|
|
first = 1;
|
393 |
|
|
num = 0;
|
394 |
|
|
} else {
|
395 |
|
|
first = 0;
|
396 |
|
|
num++;
|
397 |
|
|
}
|
398 |
|
|
}
|
399 |
|
|
if (!first) fprintf (fo, ";\n");
|
400 |
|
|
|
401 |
|
|
fprintf (fo, "\n/* forward declaration registers */\n");
|
402 |
|
|
num = 0;
|
403 |
|
|
first = 1;
|
404 |
|
|
for (b = 0; b < f->num_bb; b++)
|
405 |
|
|
for (i = 0; i < f->bb[b].ninsn; i++)
|
406 |
|
|
if (f->bb[b].insn[i].index == II_REG
|
407 |
|
|
&& f->bb[b].insn[i].index != II_LRBB) {
|
408 |
|
|
fprintf (fo, "%st%x_%x", first ? "reg [31:0] " : ", ", b, i);
|
409 |
883 |
markom |
if (num >= 8) {
|
410 |
879 |
markom |
fprintf (fo, ";\n");
|
411 |
|
|
first = 1;
|
412 |
|
|
num = 0;
|
413 |
|
|
} else {
|
414 |
|
|
first = 0;
|
415 |
|
|
num++;
|
416 |
|
|
}
|
417 |
|
|
}
|
418 |
|
|
if (!first) fprintf (fo, ";\n");
|
419 |
|
|
|
420 |
|
|
num = 0;
|
421 |
|
|
first = 1;
|
422 |
|
|
for (b = 0; b < f->num_bb; b++)
|
423 |
|
|
for (i = 0; i < f->bb[b].ninsn; i++)
|
424 |
|
|
if (f->bb[b].insn[i].index != II_REG
|
425 |
|
|
&& f->bb[b].insn[i].index == II_LRBB) {
|
426 |
|
|
fprintf (fo, "%st%x_%x", first ? "reg " : ", ", b, i);
|
427 |
883 |
markom |
if (num >= 8) {
|
428 |
879 |
markom |
fprintf (fo, ";\n");
|
429 |
|
|
first = 1;
|
430 |
|
|
num = 0;
|
431 |
|
|
} else {
|
432 |
|
|
first = 0;
|
433 |
|
|
num++;
|
434 |
|
|
}
|
435 |
|
|
}
|
436 |
|
|
if (!first) fprintf (fo, ";\n");
|
437 |
|
|
}
|
438 |
|
|
|
439 |
|
|
if (nloads || nstores) fprintf (fo, "\n/* dependencies */\n");
|
440 |
|
|
if (nloads) fprintf (fo, "wire [%i:0] l_end = l_stb & {%i{lwb_ack_i}};\n",
|
441 |
|
|
nloads - 1, nloads);
|
442 |
|
|
if (nstores) fprintf (fo, "wire [%i:0] s_end = s_stb & {%i{swb_ack_i}};\n",
|
443 |
|
|
nstores - 1, nstores);
|
444 |
907 |
markom |
if (ncalls) fprintf (fo, "wire [%i:0] f_end = f_stb & {%i{fend_i}};\n",
|
445 |
|
|
ncalls - 1, ncalls);
|
446 |
879 |
markom |
|
447 |
|
|
fprintf (fo, "\n/* last dependency */\n");
|
448 |
|
|
fprintf (fo, "wire end_o = bb_stb[%i]", end_bb_no);
|
449 |
|
|
if (end_bb->mdep) {
|
450 |
|
|
fprintf (fo, " && ");
|
451 |
|
|
print_deps (fo, f, end_bb_no, end_bb->mdep, 0);
|
452 |
|
|
}
|
453 |
908 |
markom |
fprintf (fo, "wire busy_o = |bb_stb\n");
|
454 |
907 |
markom |
|
455 |
879 |
markom |
/* Is there a loop right at end? */
|
456 |
|
|
if (end_bb->next[0] >= 0) {
|
457 |
|
|
int bidx = branch_index (end_bb);
|
458 |
|
|
char t[30];
|
459 |
|
|
print_op_v (f, t, REF (end_bb_no, bidx), 1);
|
460 |
|
|
fprintf (fo, " && !%s", t);
|
461 |
|
|
}
|
462 |
|
|
fprintf (fo, ";\n");
|
463 |
|
|
|
464 |
|
|
fprintf (fo, "\n/* Basic block triggers */\n");
|
465 |
|
|
fprintf (fo, "wire [%2i:0] bb_start = {\n", f->num_bb - 1);
|
466 |
|
|
for (b = f->num_bb - 1; b >= 0; b--) {
|
467 |
|
|
fprintf (fo, " /* bb_start[%2i] */ ", b);
|
468 |
|
|
if (f->bb[b].prev[0] < 0) fprintf (fo, "start_i");
|
469 |
|
|
else {
|
470 |
|
|
cuc_bb *prev = &f->bb[f->bb[b].prev[0]];
|
471 |
|
|
int t;
|
472 |
|
|
if (prev->mdep) {
|
473 |
|
|
print_deps (fo, f, f->bb[b].prev[0], prev->mdep, 0);
|
474 |
|
|
fprintf (fo, " && ");
|
475 |
|
|
}
|
476 |
|
|
fprintf (fo, "bb_stb[%i]", f->bb[b].prev[0]);
|
477 |
|
|
if (prev->next[0] >= 0 && prev->next[1] >= 0) {
|
478 |
|
|
int bidx = branch_index (&f->bb[f->bb[b].prev[0]]);
|
479 |
|
|
assert (bidx >= 0);
|
480 |
|
|
fprintf (fo, " && ");
|
481 |
|
|
t = prev->next[0] == b;
|
482 |
|
|
fprintf (fo, "%st%x_%x", t ? "" : "!", f->bb[b].prev[0], bidx);
|
483 |
|
|
}
|
484 |
|
|
if (f->bb[b].prev[1] >= 0) {
|
485 |
|
|
prev = &f->bb[f->bb[b].prev[1]];
|
486 |
|
|
fprintf (fo, "\n || ");
|
487 |
|
|
if (prev->mdep) {
|
488 |
|
|
print_deps (fo, f, f->bb[b].prev[1], prev->mdep, 0);
|
489 |
|
|
fprintf (fo, " && ");
|
490 |
|
|
}
|
491 |
|
|
fprintf (fo, "bb_stb[%i]", f->bb[b].prev[1]);
|
492 |
|
|
if (prev->next[0] >= 0 && prev->next[1] >= 0) {
|
493 |
|
|
int bidx = branch_index (&f->bb[f->bb[b].prev[1]]);
|
494 |
|
|
assert (bidx >= 0);
|
495 |
|
|
fprintf (fo, " && ");
|
496 |
|
|
t = prev->next[0] == b;
|
497 |
|
|
fprintf (fo, "%st%x_%x", t ? "" : "!", f->bb[b].prev[1], bidx);
|
498 |
|
|
}
|
499 |
|
|
}
|
500 |
|
|
}
|
501 |
|
|
if (b == 0) fprintf (fo, "};\n");
|
502 |
|
|
else fprintf (fo, ",\n");
|
503 |
|
|
}
|
504 |
|
|
|
505 |
|
|
fprintf (fo, "\n/* Register the bb_start */\n");
|
506 |
|
|
fprintf (fo, "reg [%2i:0] bb_start_r;\n\n", f->num_bb - 1);
|
507 |
|
|
fprintf (fo, "always @(posedge rst or posedge clk)\n");
|
508 |
|
|
fprintf (fo, "begin\n");
|
509 |
902 |
markom |
fprintf (fo, " if (rst) bb_start_r <= #1 %i'b0;\n", f->num_bb);
|
510 |
|
|
fprintf (fo, " else if (end_o) bb_start_r <= #1 %i'b0;\n", f->num_bb);
|
511 |
879 |
markom |
fprintf (fo, " else bb_start_r <= #1 bb_start;\n");
|
512 |
|
|
fprintf (fo, "end\n");
|
513 |
|
|
|
514 |
|
|
fprintf (fo, "\n/* Logic */\n");
|
515 |
|
|
/* output body */
|
516 |
|
|
for (b = 0; b < f->num_bb; b++) {
|
517 |
|
|
fprintf (fo, "\t\t/* BB%i */\n", b);
|
518 |
|
|
for (i = 0; i < f->bb[b].ninsn; i++)
|
519 |
|
|
print_insn_v (fo, f, b, i);
|
520 |
|
|
fprintf (fo, "\n");
|
521 |
|
|
}
|
522 |
|
|
|
523 |
|
|
if (co) {
|
524 |
|
|
fprintf (fo, "\n/* Outputs */\n");
|
525 |
|
|
for (i = 0; i < MAX_REGS; i++)
|
526 |
883 |
markom |
if (f->lur[i] >= 0 && !f->saved_regs[i])
|
527 |
|
|
fprintf (fo, "assign r%i_o = t%x_%x;\n", i, REF_BB(f->lur[i]),
|
528 |
|
|
REF_I(f->lur[i]));
|
529 |
879 |
markom |
}
|
530 |
|
|
|
531 |
|
|
if (nstores) {
|
532 |
|
|
int cur_store = 0;
|
533 |
|
|
fprintf (fo, "\n/* Memory stores */\n");
|
534 |
|
|
fprintf (fo, "always @(posedge clk or posedge rst)\nbegin\n");
|
535 |
|
|
fprintf (fo, " if (rst) swb_dat_o <= #1 32'h0;\n");
|
536 |
|
|
for (i = 0; i < f->nmsched; i++)
|
537 |
907 |
markom |
if (f->mtype[i] & MT_STORE) {
|
538 |
879 |
markom |
char t[30];
|
539 |
|
|
fprintf (fo, " else if (s_stb[%i]) swb_dat_o <= #1 %s;\n", cur_store++,
|
540 |
|
|
print_op_v (f, t, f->msched[i], 0));
|
541 |
|
|
//printf ("msched[%i] = %x (mtype %x) %x\n", i, f->msched[i], f->mtype[i], f->INSN(f->msched[i]).op[0]);
|
542 |
|
|
}
|
543 |
|
|
fprintf (fo, "end\n");
|
544 |
|
|
}
|
545 |
|
|
|
546 |
|
|
if (nloads) {
|
547 |
|
|
int cur_load = 0;
|
548 |
|
|
fprintf (fo, "\n/* Load state machine */\n");
|
549 |
|
|
fprintf (fo, "always @(posedge clk or posedge rst)\n");
|
550 |
|
|
fprintf (fo, "begin\n");
|
551 |
|
|
fprintf (fo, " if (rst) begin\n");
|
552 |
|
|
fprintf (fo, " l_stb <= #1 %i'h0;\n", nloads);
|
553 |
|
|
fprintf (fo, " lwb_cycstb_o <= #1 1'b0;\n");
|
554 |
|
|
fprintf (fo, " lwb_sel_o[3:0] <= #1 4'b0000;\n");
|
555 |
|
|
fprintf (fo, " lwb_linbrst_o <= #1 1'b0;\n");
|
556 |
|
|
fprintf (fo, " lwb_adr_o <= #1 32'h0;\n");
|
557 |
|
|
fprintf (fo, " end else begin\n");
|
558 |
883 |
markom |
cucdebug (1, "loads \n");
|
559 |
907 |
markom |
for (i = 0; i < f->nmsched; i++) if (f->mtype[i] & MT_LOAD) {
|
560 |
879 |
markom |
char t[30];
|
561 |
|
|
dep_list *dep = f->INSN(f->msched[i]).dep;
|
562 |
883 |
markom |
cucdebug (1, "msched[%i] = %x (mtype %x)\n", i, f->msched[i], f->mtype[i]);
|
563 |
879 |
markom |
assert (f->INSN(f->msched[i]).opt[1] & (OPT_REF | OPT_REGISTER));
|
564 |
|
|
fprintf (fo, " if (");
|
565 |
|
|
print_deps (fo, f, REF_BB(f->msched[i]), f->INSN(f->msched[i]).dep, 1);
|
566 |
|
|
fprintf (fo, ") begin\n");
|
567 |
907 |
markom |
print_turn_off_dep (fo, f, dep);
|
568 |
879 |
markom |
fprintf (fo, " l_stb[%i] <= #1 1'b1;\n", cur_load++);
|
569 |
|
|
fprintf (fo, " lwb_cycstb_o <= #1 1'b1;\n");
|
570 |
|
|
fprintf (fo, " lwb_sel_o[3:0] <= #1 4'b");
|
571 |
|
|
switch (f->mtype[i] & MT_WIDTH) {
|
572 |
902 |
markom |
case 1: fprintf (fo, "0001 << (%s & 32'h3);\n",
|
573 |
879 |
markom |
print_op_v (f, t, f->msched[i], 1)); break;
|
574 |
902 |
markom |
case 2: fprintf (fo, "0011 << ((%s & 32'h1) << 1);\n",
|
575 |
879 |
markom |
print_op_v (f, t, f->msched[i], 1)); break;
|
576 |
|
|
case 4: fprintf (fo, "1111;\n"); break;
|
577 |
|
|
default: assert (0);
|
578 |
|
|
}
|
579 |
|
|
fprintf (fo, " lwb_linbrst_o <= #1 1'b%i;\n",
|
580 |
|
|
(f->mtype[i] & MT_BURST) && !(f->mtype[i] & MT_BURSTE) ? 1 : 0);
|
581 |
|
|
fprintf (fo, " lwb_adr_o <= #1 t%x_%x & ~32'h3;\n",
|
582 |
|
|
REF_BB(f->INSN(f->msched[i]).op[1]), REF_I(f->INSN(f->msched[i]).op[1]));
|
583 |
|
|
fprintf (fo, " end\n");
|
584 |
|
|
}
|
585 |
|
|
fprintf (fo, " if (l_end[%i]) begin\n", nloads - 1);
|
586 |
|
|
fprintf (fo, " l_stb <= #1 %i'h0;\n", nloads);
|
587 |
|
|
fprintf (fo, " lwb_cycstb_o <= #1 1'b0;\n");
|
588 |
|
|
fprintf (fo, " lwb_sel_o[3:0] <= #1 4'b0000;\n");
|
589 |
|
|
fprintf (fo, " lwb_linbrst_o <= #1 1'b0;\n");
|
590 |
|
|
fprintf (fo, " lwb_adr_o <= #1 32'h0;\n");
|
591 |
|
|
fprintf (fo, " end\n");
|
592 |
|
|
fprintf (fo, " end\n");
|
593 |
|
|
fprintf (fo, "end\n");
|
594 |
|
|
}
|
595 |
|
|
|
596 |
|
|
if (nstores) {
|
597 |
|
|
int cur_store = 0;
|
598 |
|
|
fprintf (fo, "\n/* Store state machine */\n");
|
599 |
|
|
fprintf (fo, "always @(posedge clk or posedge rst)\n");
|
600 |
|
|
fprintf (fo, "begin\n");
|
601 |
|
|
fprintf (fo, " if (rst) begin\n");
|
602 |
|
|
fprintf (fo, " s_stb <= #1 %i'h0;\n", nstores);
|
603 |
|
|
fprintf (fo, " swb_cycstb_o <= #1 1'b0;\n");
|
604 |
|
|
fprintf (fo, " swb_sel_o[3:0] <= #1 4'b0000;\n");
|
605 |
|
|
fprintf (fo, " swb_linbrst_o <= #1 1'b0;\n");
|
606 |
|
|
fprintf (fo, " swb_adr_o <= #1 32'h0;\n");
|
607 |
|
|
fprintf (fo, " end else begin\n");
|
608 |
883 |
markom |
cucdebug (1, "stores \n");
|
609 |
907 |
markom |
for (i = 0; i < f->nmsched; i++) if (f->mtype[i] & MT_STORE) {
|
610 |
879 |
markom |
char t[30];
|
611 |
|
|
dep_list *dep = f->INSN(f->msched[i]).dep;
|
612 |
883 |
markom |
cucdebug (1, "msched[%i] = %x (mtype %x)\n", i, f->msched[i], f->mtype[i]);
|
613 |
879 |
markom |
assert (f->INSN(f->msched[i]).opt[1] & (OPT_REF | OPT_REGISTER));
|
614 |
|
|
fprintf (fo, " if (");
|
615 |
|
|
print_deps (fo, f, REF_BB(f->msched[i]), f->INSN(f->msched[i]).dep, 1);
|
616 |
|
|
fprintf (fo, ") begin\n");
|
617 |
907 |
markom |
print_turn_off_dep (fo, f, dep);
|
618 |
879 |
markom |
fprintf (fo, " s_stb[%i] <= #1 1'b1;\n", cur_store++);
|
619 |
|
|
fprintf (fo, " swb_cycstb_o <= #1 1'b1;\n");
|
620 |
|
|
fprintf (fo, " swb_sel_o[3:0] <= #1 4'b");
|
621 |
|
|
switch (f->mtype[i] & MT_WIDTH) {
|
622 |
902 |
markom |
case 1: fprintf (fo, "0001 << (%s & 32'h3);\n",
|
623 |
879 |
markom |
print_op_v (f, t, f->msched[i], 1)); break;
|
624 |
902 |
markom |
case 2: fprintf (fo, "0011 << ((%s & 32'h1) << 1);\n",
|
625 |
879 |
markom |
print_op_v (f, t, f->msched[i], 1)); break;
|
626 |
|
|
case 4: fprintf (fo, "1111;\n"); break;
|
627 |
|
|
default: assert (0);
|
628 |
|
|
}
|
629 |
|
|
fprintf (fo, " swb_linbrst_o <= #1 1'b%i;\n",
|
630 |
|
|
(f->mtype[i] & MT_BURST) && !(f->mtype[i] & MT_BURSTE) ? 1 : 0);
|
631 |
|
|
fprintf (fo, " swb_adr_o <= #1 t%x_%x & ~32'h3;\n",
|
632 |
|
|
REF_BB(f->INSN(f->msched[i]).op[1]), REF_I(f->INSN(f->msched[i]).op[1]));
|
633 |
|
|
fprintf (fo, " end\n");
|
634 |
|
|
}
|
635 |
|
|
fprintf (fo, " if (s_end[%i]) begin\n", nstores - 1);
|
636 |
|
|
fprintf (fo, " s_stb <= #1 %i'h0;\n", nstores);
|
637 |
|
|
fprintf (fo, " swb_cycstb_o <= #1 1'b0;\n");
|
638 |
|
|
fprintf (fo, " swb_sel_o[3:0] <= #1 4'b0000;\n");
|
639 |
|
|
fprintf (fo, " swb_linbrst_o <= #1 1'b0;\n");
|
640 |
|
|
fprintf (fo, " swb_adr_o <= #1 32'h0;\n");
|
641 |
|
|
fprintf (fo, " end\n");
|
642 |
|
|
fprintf (fo, " end\n");
|
643 |
|
|
fprintf (fo, "end\n");
|
644 |
|
|
}
|
645 |
|
|
|
646 |
907 |
markom |
if (ncalls) {
|
647 |
|
|
int cur_call = 0;
|
648 |
|
|
fprintf (fo, "\n/* Function calls state machine */\n");
|
649 |
|
|
fprintf (fo, "always @(posedge clk or posedge rst)\n");
|
650 |
|
|
fprintf (fo, "begin\n");
|
651 |
|
|
fprintf (fo, " if (rst) begin\n");
|
652 |
|
|
fprintf (fo, " f_stb <= #1 %i'h0;\n", nstores);
|
653 |
|
|
for (i = 0; i < 6; i++) fprintf (fo, " fr%i_o <= #1 32'h0;\n", i + 3);
|
654 |
|
|
if (log2(ncalls)) fprintf (fo, " fid_o <= #1 %i'h0;\n", log2 (f->nfdeps));
|
655 |
|
|
fprintf (fo, " fstart_o <= #1 1'b0;\n");
|
656 |
|
|
//fprintf (fo, " f11_r <= #1 32'h0;\n");
|
657 |
|
|
fprintf (fo, " end else begin\n");
|
658 |
|
|
cucdebug (1, "calls \n");
|
659 |
|
|
for (i = 0; i < f->nmsched; i++) if (f->mtype[i] & MT_CALL) {
|
660 |
|
|
char t[30];
|
661 |
|
|
dep_list *dep = f->INSN(f->msched[i]).dep;
|
662 |
|
|
cucdebug (1, "msched[%i] = %x (mtype %x)\n", i, f->msched[i], f->mtype[i]);
|
663 |
|
|
assert (f->INSN(f->msched[i]).opt[1] & (OPT_REF | OPT_REGISTER));
|
664 |
|
|
fprintf (fo, " if (");
|
665 |
|
|
print_deps (fo, f, REF_BB(f->msched[i]), f->INSN(f->msched[i]).dep, 1);
|
666 |
|
|
fprintf (fo, ") begin\n");
|
667 |
|
|
print_turn_off_dep (fo, f, dep);
|
668 |
|
|
fprintf (fo, " f_stb[%i] <= #1 1'b1;\n", cur_call++);
|
669 |
|
|
fprintf (fo, " fstart_o <= #1 1'b1;\n");
|
670 |
|
|
if (log2 (f->nfdeps))
|
671 |
|
|
fprintf (fo, " fid_o <= #1 %i'h%x;\n", log2 (f->nfdeps), func_index (f, f->msched[i]));
|
672 |
|
|
|
673 |
|
|
for (j = 0; j < 6; j++)
|
674 |
|
|
fprintf (fo, " fr%i_o <= #1 t%x_%x;\n", j + 3,
|
675 |
|
|
REF_BB (f->msched[i]), REF_I (f->msched[i]) - 6 + i);
|
676 |
|
|
fprintf (fo, " end\n");
|
677 |
|
|
}
|
678 |
|
|
fprintf (fo, " if (f_end[%i]) begin\n", ncalls - 1);
|
679 |
|
|
fprintf (fo, " f_stb <= #1 %i'h0;\n", ncalls);
|
680 |
|
|
fprintf (fo, " f_start_o <= #1 1'b0;\n");
|
681 |
|
|
fprintf (fo, " end\n");
|
682 |
|
|
fprintf (fo, " end\n");
|
683 |
|
|
fprintf (fo, "end\n");
|
684 |
|
|
}
|
685 |
|
|
|
686 |
879 |
markom |
fprintf (fo, "\n/* Basic blocks state machine */\n");
|
687 |
|
|
fprintf (fo, "always @(posedge clk or posedge rst)\n");
|
688 |
|
|
fprintf (fo, "begin\n");
|
689 |
902 |
markom |
fprintf (fo, " if (rst) bb_stb <= #1 %i'h%x;\n", f->num_bb, 0);
|
690 |
|
|
fprintf (fo, " else if (end_o) bb_stb <= #1 %i'h%x;\n", f->num_bb, 0);
|
691 |
879 |
markom |
for (i = 0; i < f->num_bb; i++) {
|
692 |
902 |
markom |
fprintf (fo, " else if (bb_start[%i]) begin\n", i);
|
693 |
879 |
markom |
fprintf (fo, " bb_stb <= #1 %i'h%x;\n", f->num_bb, 1 << i);
|
694 |
|
|
}
|
695 |
|
|
fprintf (fo, " end else if (end_o) begin\n");
|
696 |
|
|
fprintf (fo, " bb_stb <= #1 %i'h%x;\n", f->num_bb, 0);
|
697 |
|
|
fprintf (fo, " end\n");
|
698 |
|
|
fprintf (fo, "end\n");
|
699 |
|
|
|
700 |
|
|
/* output footer */
|
701 |
|
|
fprintf (fo, "\nendmodule\n");
|
702 |
|
|
|
703 |
|
|
fclose (fo);
|
704 |
|
|
}
|
705 |
|
|
|