OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc2/] [or1ksim/] [cuc/] [verilog.c] - Blame information for rev 907

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
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 879 markom
  fprintf (fo, "\n              start_i, end_o);\n\n");
266
 
267
  fprintf (fo, "input         clk, rst;\n");
268
  fprintf (fo, "input         start_i;\t/* Module starts when set to 1 */ \n");
269
  fprintf (fo, "output        end_o;\t/* Set when module finishes, cleared upon start_i == 1 */\n\n");
270
  fprintf (fo, "/* Bus signals */\n");
271
  fprintf (fo, "output        lwb_cycstb_o, swb_cycstb_o;\n");
272
  fprintf (fo, "input         lwb_ack_i, swb_ack_i;\n");
273
  fprintf (fo, "output  [3:0] lwb_sel_o, swb_sel_o;\n");
274
  fprintf (fo, "output [31:0] lwb_adr_o, swb_adr_o;\n");
275
  fprintf (fo, "output        lwb_linbrst_o, swb_linbrst_o;\n");
276
  fprintf (fo, "input  [31:0] lwb_dat_i;\n");
277
  fprintf (fo, "output [31:0] swb_dat_o;\n\n");
278
 
279
  fprintf (fo, "reg           lwb_cycstb_o, swb_cycstb_o;\n");
280
  fprintf (fo, "reg    [31:0] lwb_adr_o, swb_adr_o;\n");
281
  fprintf (fo, "reg     [3:0] lwb_sel_o, swb_sel_o;\n");
282
  fprintf (fo, "reg    [31:0] swb_dat_o;\n");
283
  fprintf (fo, "reg           lwb_linbrst_o, swb_linbrst_o;\n");
284
 
285
  if (ci || co) fprintf (fo, "\n/* module ports */\n");
286
  if (ci) {
287
    int first = 1;
288
    fprintf (fo, "input  [31:0]");
289
    for (i = 0; i < MAX_REGS; i++)
290 883 markom
      if (f->used_regs[i]) {
291 879 markom
        fprintf (fo, "%sr%i_i", first ? " " : ", ", i);
292
        first = 0;
293
      }
294
    fprintf (fo, ";\n");
295
  }
296
 
297
  if (co) {
298
    int first = 1;
299
    fprintf (fo, "output [31:0]");
300
    for (i = 0; i < MAX_REGS; i++)
301 883 markom
      if (f->lur[i] >= 0 && !f->saved_regs[i]) {
302 879 markom
        fprintf (fo, "%sr%i_o", first ? " " : ", ", i);
303
        first = 0;
304
      }
305
    fprintf (fo, ";\n");
306
  }
307
 
308 907 markom
  if (f->nfdeps) {
309
    fprintf (fo, "\n/* Function calls */\n");
310
    fprintf (fo, "output [31:0] fr3_o");
311
    for (i = 1; i < 6; i++) fprintf (fo, ", fr%i_o", i + 3);
312
    fprintf (fo, ";\n");
313
    if (log2(f->nfdeps) > 0) fprintf (fo, "output [%i:0] fid_o;\n", log2(f->nfdeps));
314
    fprintf (fo, "output        fstart_o;\n");
315
    fprintf (fo, "input         fend_i;\n");
316
  }
317
 
318 879 markom
  /* Count loads & stores */
319
  for (i = 0; i < f->nmsched; i++)
320 907 markom
    if (f->mtype[i] & MT_STORE) nstores++;
321
    else if (f->mtype[i] & MT_LOAD) nloads++;
322
    else if (f->mtype[i] & MT_CALL) ncalls++;
323 879 markom
 
324
  /* Output internal registers for loads */
325
  if (nloads) {
326
    int first = 1;
327 883 markom
    int num = 0;
328 879 markom
    fprintf (fo, "\n/* internal registers for loads */\n");
329
    for (i = 0; i < f->nmsched; i++)
330 907 markom
      if (f->mtype[i] & MT_LOAD) {
331 879 markom
        fprintf (fo, "%st%x_%x", first ? "reg    [31:0] " : ", ",
332
                REF_BB(f->msched[i]), REF_I(f->msched[i]));
333 883 markom
 
334
        if (num >= 8) {
335
          fprintf (fo, ";\n");
336
          first = 1;
337
          num = 0;
338
        } else {
339
          first = 0;
340
          num++;
341
        }
342 879 markom
      }
343
    if (!first) fprintf (fo, ";\n");
344
  }
345
 
346 907 markom
  /* Internal register for function return value */
347
  if (f->nfdeps) {
348
    fprintf (fo, "\n/* Internal register for function return value */\n");
349
    fprintf (fo, "reg     [31:0] fr11_r;\n");
350
  }
351
 
352 879 markom
  fprintf (fo, "\n/* 'zero or one' hot state machines */\n");
353
  if (nloads) fprintf (fo, "reg     [%i:0] l_stb; /* loads */\n", nloads - 1);
354
  if (nstores) fprintf (fo, "reg     [%i:0] s_stb; /* stores */\n", nstores - 1);
355
  fprintf (fo, "reg     [%i:0] bb_stb; /* basic blocks */\n", f->num_bb - 1);
356
 
357
  {
358 883 markom
    int first = 2;
359 879 markom
    int num = 0;
360
    for (b = 0; b < f->num_bb; b++)
361
      for (i = 0; i < f->bb[b].ninsn; i++)
362
        if (f->bb[b].insn[i].type & IT_COND
363
         && f->bb[b].insn[i].index != II_REG
364
         && f->bb[b].insn[i].index != II_LRBB) {
365 883 markom
          if (first == 2) fprintf (fo, "\n/* basic block condition wires */\n");
366 879 markom
          fprintf (fo, "%st%x_%x", first ? "wire          " : ", ", b, i);
367 883 markom
          if (num >= 8) {
368 879 markom
            fprintf (fo, ";\n");
369
            first = 1;
370
            num = 0;
371
          } else {
372
            first = 0;
373
            num++;
374
          }
375
        }
376
    if (!first) fprintf (fo, ";\n");
377
 
378
    fprintf (fo, "\n/* forward declaration of normal wires */\n");
379
    num = 0;
380
    first = 1;
381
    for (b = 0; b < f->num_bb; b++)
382
      for (i = 0; i < f->bb[b].ninsn; i++)
383
        if (!(f->bb[b].insn[i].type & (IT_COND | IT_BRANCH))
384
         && f->bb[b].insn[i].index != II_REG
385
         && f->bb[b].insn[i].index != II_LRBB) {
386
          /* Exclude loads */
387
          if (f->bb[b].insn[i].type & IT_MEMORY && II_IS_LOAD (f->bb[b].insn[i].index)) continue;
388
          fprintf (fo, "%st%x_%x", first ? "wire   [31:0] " : ", ", b, i);
389 883 markom
          if (num >= 8) {
390 879 markom
            fprintf (fo, ";\n");
391
            first = 1;
392
            num = 0;
393
          } else {
394
            first = 0;
395
            num++;
396
          }
397
        }
398
    if (!first) fprintf (fo, ";\n");
399
 
400
    fprintf (fo, "\n/* forward declaration registers */\n");
401
    num = 0;
402
    first = 1;
403
    for (b = 0; b < f->num_bb; b++)
404
      for (i = 0; i < f->bb[b].ninsn; i++)
405
        if (f->bb[b].insn[i].index == II_REG
406
         && f->bb[b].insn[i].index != II_LRBB) {
407
          fprintf (fo, "%st%x_%x", first ? "reg    [31:0] " : ", ", b, i);
408 883 markom
          if (num >= 8) {
409 879 markom
            fprintf (fo, ";\n");
410
            first = 1;
411
            num = 0;
412
          } else {
413
            first = 0;
414
            num++;
415
          }
416
        }
417
    if (!first) fprintf (fo, ";\n");
418
 
419
    num = 0;
420
    first = 1;
421
    for (b = 0; b < f->num_bb; b++)
422
      for (i = 0; i < f->bb[b].ninsn; i++)
423
        if (f->bb[b].insn[i].index != II_REG
424
         && f->bb[b].insn[i].index == II_LRBB) {
425
          fprintf (fo, "%st%x_%x", first ? "reg           " : ", ", b, i);
426 883 markom
          if (num >= 8) {
427 879 markom
            fprintf (fo, ";\n");
428
            first = 1;
429
            num = 0;
430
          } else {
431
            first = 0;
432
            num++;
433
          }
434
        }
435
    if (!first) fprintf (fo, ";\n");
436
  }
437
 
438
  if (nloads || nstores) fprintf (fo, "\n/* dependencies */\n");
439
  if (nloads) fprintf (fo, "wire    [%i:0] l_end = l_stb & {%i{lwb_ack_i}};\n",
440
                  nloads - 1, nloads);
441
  if (nstores) fprintf (fo, "wire    [%i:0] s_end = s_stb & {%i{swb_ack_i}};\n",
442
                  nstores - 1, nstores);
443 907 markom
  if (ncalls) fprintf (fo, "wire    [%i:0] f_end = f_stb & {%i{fend_i}};\n",
444
                  ncalls - 1, ncalls);
445 879 markom
 
446
  fprintf (fo, "\n/* last dependency */\n");
447
  fprintf (fo, "wire   end_o = bb_stb[%i]", end_bb_no);
448
  if (end_bb->mdep) {
449
    fprintf (fo, " && ");
450
    print_deps (fo, f, end_bb_no, end_bb->mdep, 0);
451
  }
452 907 markom
 
453 879 markom
  /* Is there a loop right at end? */
454
  if (end_bb->next[0] >= 0) {
455
    int bidx = branch_index (end_bb);
456
    char t[30];
457
    print_op_v (f, t, REF (end_bb_no, bidx), 1);
458
    fprintf (fo, " && !%s", t);
459
  }
460
  fprintf (fo, ";\n");
461
 
462
  fprintf (fo, "\n/* Basic block triggers */\n");
463
  fprintf (fo, "wire   [%2i:0] bb_start = {\n", f->num_bb - 1);
464
  for (b = f->num_bb - 1; b >= 0; b--) {
465
    fprintf (fo, "    /* bb_start[%2i] */ ", b);
466
    if (f->bb[b].prev[0] < 0) fprintf (fo, "start_i");
467
    else {
468
      cuc_bb *prev = &f->bb[f->bb[b].prev[0]];
469
      int t;
470
      if (prev->mdep) {
471
        print_deps (fo, f, f->bb[b].prev[0], prev->mdep, 0);
472
        fprintf (fo, " && ");
473
      }
474
      fprintf (fo, "bb_stb[%i]", f->bb[b].prev[0]);
475
      if (prev->next[0] >= 0 && prev->next[1] >= 0) {
476
        int bidx = branch_index (&f->bb[f->bb[b].prev[0]]);
477
        assert (bidx >= 0);
478
        fprintf (fo, " && ");
479
        t = prev->next[0] == b;
480
        fprintf (fo, "%st%x_%x", t ? "" : "!", f->bb[b].prev[0], bidx);
481
      }
482
      if (f->bb[b].prev[1] >= 0) {
483
        prev = &f->bb[f->bb[b].prev[1]];
484
        fprintf (fo, "\n                    || ");
485
        if (prev->mdep) {
486
          print_deps (fo, f, f->bb[b].prev[1], prev->mdep, 0);
487
          fprintf (fo, " && ");
488
        }
489
        fprintf (fo, "bb_stb[%i]", f->bb[b].prev[1]);
490
        if (prev->next[0] >= 0 && prev->next[1] >= 0) {
491
          int bidx = branch_index (&f->bb[f->bb[b].prev[1]]);
492
          assert (bidx >= 0);
493
          fprintf (fo, " && ");
494
          t = prev->next[0] == b;
495
          fprintf (fo, "%st%x_%x", t ? "" : "!", f->bb[b].prev[1], bidx);
496
        }
497
      }
498
    }
499
    if (b == 0) fprintf (fo, "};\n");
500
    else fprintf (fo, ",\n");
501
  }
502
 
503
  fprintf (fo, "\n/* Register the bb_start */\n");
504
  fprintf (fo, "reg   [%2i:0] bb_start_r;\n\n", f->num_bb - 1);
505
  fprintf (fo, "always @(posedge rst or posedge clk)\n");
506
  fprintf (fo, "begin\n");
507 902 markom
  fprintf (fo, "  if (rst) bb_start_r <= #1 %i'b0;\n", f->num_bb);
508
  fprintf (fo, "  else if (end_o) bb_start_r <= #1 %i'b0;\n", f->num_bb);
509 879 markom
  fprintf (fo, "  else bb_start_r <= #1 bb_start;\n");
510
  fprintf (fo, "end\n");
511
 
512
  fprintf (fo, "\n/* Logic */\n");
513
  /* output body */
514
  for (b = 0; b < f->num_bb; b++) {
515
    fprintf (fo, "\t\t/* BB%i */\n", b);
516
    for (i = 0; i < f->bb[b].ninsn; i++)
517
      print_insn_v (fo, f, b, i);
518
    fprintf (fo, "\n");
519
  }
520
 
521
  if (co) {
522
    fprintf (fo, "\n/* Outputs */\n");
523
    for (i = 0; i < MAX_REGS; i++)
524 883 markom
      if (f->lur[i] >= 0 && !f->saved_regs[i])
525
        fprintf (fo, "assign r%i_o = t%x_%x;\n", i, REF_BB(f->lur[i]),
526
                        REF_I(f->lur[i]));
527 879 markom
  }
528
 
529
  if (nstores) {
530
    int cur_store = 0;
531
    fprintf (fo, "\n/* Memory stores */\n");
532
    fprintf (fo, "always @(posedge clk or posedge rst)\nbegin\n");
533
    fprintf (fo, "  if (rst) swb_dat_o <= #1 32'h0;\n");
534
    for (i = 0; i < f->nmsched; i++)
535 907 markom
      if (f->mtype[i] & MT_STORE) {
536 879 markom
        char t[30];
537
        fprintf (fo, "  else if (s_stb[%i]) swb_dat_o <= #1 %s;\n", cur_store++,
538
                        print_op_v (f, t, f->msched[i], 0));
539
        //printf ("msched[%i] = %x (mtype %x) %x\n", i, f->msched[i], f->mtype[i], f->INSN(f->msched[i]).op[0]);
540
      }
541
    fprintf (fo, "end\n");
542
  }
543
 
544
  if (nloads) {
545
    int cur_load = 0;
546
    fprintf (fo, "\n/* Load state machine */\n");
547
    fprintf (fo, "always @(posedge clk or posedge rst)\n");
548
    fprintf (fo, "begin\n");
549
    fprintf (fo, "  if (rst) begin\n");
550
    fprintf (fo, "    l_stb <= #1 %i'h0;\n", nloads);
551
    fprintf (fo, "    lwb_cycstb_o <= #1 1'b0;\n");
552
    fprintf (fo, "    lwb_sel_o[3:0] <= #1 4'b0000;\n");
553
    fprintf (fo, "    lwb_linbrst_o <= #1 1'b0;\n");
554
    fprintf (fo, "    lwb_adr_o <= #1 32'h0;\n");
555
    fprintf (fo, "  end else begin\n");
556 883 markom
    cucdebug (1, "loads \n");
557 907 markom
    for (i = 0; i < f->nmsched; i++) if (f->mtype[i] & MT_LOAD) {
558 879 markom
      char t[30];
559
      dep_list *dep = f->INSN(f->msched[i]).dep;
560 883 markom
      cucdebug (1, "msched[%i] = %x (mtype %x)\n", i, f->msched[i], f->mtype[i]);
561 879 markom
      assert (f->INSN(f->msched[i]).opt[1] & (OPT_REF | OPT_REGISTER));
562
      fprintf (fo, "    if (");
563
      print_deps (fo, f, REF_BB(f->msched[i]), f->INSN(f->msched[i]).dep, 1);
564
      fprintf (fo, ") begin\n");
565 907 markom
      print_turn_off_dep (fo, f, dep);
566 879 markom
      fprintf (fo, "      l_stb[%i] <= #1 1'b1;\n", cur_load++);
567
      fprintf (fo, "      lwb_cycstb_o <= #1 1'b1;\n");
568
      fprintf (fo, "      lwb_sel_o[3:0] <= #1 4'b");
569
      switch (f->mtype[i] & MT_WIDTH) {
570 902 markom
        case 1: fprintf (fo, "0001 << (%s & 32'h3);\n",
571 879 markom
                                print_op_v (f, t, f->msched[i], 1)); break;
572 902 markom
        case 2: fprintf (fo, "0011 << ((%s & 32'h1) << 1);\n",
573 879 markom
                                print_op_v (f, t, f->msched[i], 1)); break;
574
        case 4: fprintf (fo, "1111;\n"); break;
575
        default: assert (0);
576
      }
577
      fprintf (fo, "      lwb_linbrst_o <= #1 1'b%i;\n",
578
                      (f->mtype[i] & MT_BURST) && !(f->mtype[i] & MT_BURSTE) ? 1 : 0);
579
      fprintf (fo, "      lwb_adr_o <= #1 t%x_%x & ~32'h3;\n",
580
                      REF_BB(f->INSN(f->msched[i]).op[1]), REF_I(f->INSN(f->msched[i]).op[1]));
581
      fprintf (fo, "    end\n");
582
    }
583
    fprintf (fo, "    if (l_end[%i]) begin\n", nloads - 1);
584
    fprintf (fo, "      l_stb <= #1 %i'h0;\n", nloads);
585
    fprintf (fo, "      lwb_cycstb_o <= #1 1'b0;\n");
586
    fprintf (fo, "      lwb_sel_o[3:0] <= #1 4'b0000;\n");
587
    fprintf (fo, "      lwb_linbrst_o <= #1 1'b0;\n");
588
    fprintf (fo, "      lwb_adr_o <= #1 32'h0;\n");
589
    fprintf (fo, "    end\n");
590
    fprintf (fo, "  end\n");
591
    fprintf (fo, "end\n");
592
  }
593
 
594
  if (nstores) {
595
    int cur_store = 0;
596
    fprintf (fo, "\n/* Store state machine */\n");
597
    fprintf (fo, "always @(posedge clk or posedge rst)\n");
598
    fprintf (fo, "begin\n");
599
    fprintf (fo, "  if (rst) begin\n");
600
    fprintf (fo, "    s_stb <= #1 %i'h0;\n", nstores);
601
    fprintf (fo, "    swb_cycstb_o <= #1 1'b0;\n");
602
    fprintf (fo, "    swb_sel_o[3:0] <= #1 4'b0000;\n");
603
    fprintf (fo, "    swb_linbrst_o <= #1 1'b0;\n");
604
    fprintf (fo, "    swb_adr_o <= #1 32'h0;\n");
605
    fprintf (fo, "  end else begin\n");
606 883 markom
    cucdebug (1, "stores \n");
607 907 markom
    for (i = 0; i < f->nmsched; i++) if (f->mtype[i] & MT_STORE) {
608 879 markom
      char t[30];
609
      dep_list *dep = f->INSN(f->msched[i]).dep;
610 883 markom
      cucdebug (1, "msched[%i] = %x (mtype %x)\n", i, f->msched[i], f->mtype[i]);
611 879 markom
      assert (f->INSN(f->msched[i]).opt[1] & (OPT_REF | OPT_REGISTER));
612
      fprintf (fo, "    if (");
613
      print_deps (fo, f, REF_BB(f->msched[i]), f->INSN(f->msched[i]).dep, 1);
614
      fprintf (fo, ") begin\n");
615 907 markom
      print_turn_off_dep (fo, f, dep);
616 879 markom
      fprintf (fo, "      s_stb[%i] <= #1 1'b1;\n", cur_store++);
617
      fprintf (fo, "      swb_cycstb_o <= #1 1'b1;\n");
618
      fprintf (fo, "      swb_sel_o[3:0] <= #1 4'b");
619
      switch (f->mtype[i] & MT_WIDTH) {
620 902 markom
        case 1: fprintf (fo, "0001 << (%s & 32'h3);\n",
621 879 markom
                                print_op_v (f, t, f->msched[i], 1)); break;
622 902 markom
        case 2: fprintf (fo, "0011 << ((%s & 32'h1) << 1);\n",
623 879 markom
                                print_op_v (f, t, f->msched[i], 1)); break;
624
        case 4: fprintf (fo, "1111;\n"); break;
625
        default: assert (0);
626
      }
627
      fprintf (fo, "      swb_linbrst_o <= #1 1'b%i;\n",
628
                      (f->mtype[i] & MT_BURST) && !(f->mtype[i] & MT_BURSTE) ? 1 : 0);
629
      fprintf (fo, "      swb_adr_o <= #1 t%x_%x & ~32'h3;\n",
630
                      REF_BB(f->INSN(f->msched[i]).op[1]), REF_I(f->INSN(f->msched[i]).op[1]));
631
      fprintf (fo, "    end\n");
632
    }
633
    fprintf (fo, "    if (s_end[%i]) begin\n", nstores - 1);
634
    fprintf (fo, "      s_stb <= #1 %i'h0;\n", nstores);
635
    fprintf (fo, "      swb_cycstb_o <= #1 1'b0;\n");
636
    fprintf (fo, "      swb_sel_o[3:0] <= #1 4'b0000;\n");
637
    fprintf (fo, "      swb_linbrst_o <= #1 1'b0;\n");
638
    fprintf (fo, "      swb_adr_o <= #1 32'h0;\n");
639
    fprintf (fo, "    end\n");
640
    fprintf (fo, "  end\n");
641
    fprintf (fo, "end\n");
642
  }
643
 
644 907 markom
  if (ncalls) {
645
    int cur_call = 0;
646
    fprintf (fo, "\n/* Function calls state machine */\n");
647
    fprintf (fo, "always @(posedge clk or posedge rst)\n");
648
    fprintf (fo, "begin\n");
649
    fprintf (fo, "  if (rst) begin\n");
650
    fprintf (fo, "    f_stb <= #1 %i'h0;\n", nstores);
651
    for (i = 0; i < 6; i++) fprintf (fo, "    fr%i_o <= #1 32'h0;\n", i + 3);
652
    if (log2(ncalls)) fprintf (fo, "    fid_o <= #1 %i'h0;\n", log2 (f->nfdeps));
653
    fprintf (fo, "    fstart_o <= #1 1'b0;\n");
654
    //fprintf (fo, "    f11_r <= #1 32'h0;\n");
655
    fprintf (fo, "  end else begin\n");
656
    cucdebug (1, "calls \n");
657
    for (i = 0; i < f->nmsched; i++) if (f->mtype[i] & MT_CALL) {
658
      char t[30];
659
      dep_list *dep = f->INSN(f->msched[i]).dep;
660
      cucdebug (1, "msched[%i] = %x (mtype %x)\n", i, f->msched[i], f->mtype[i]);
661
      assert (f->INSN(f->msched[i]).opt[1] & (OPT_REF | OPT_REGISTER));
662
      fprintf (fo, "    if (");
663
      print_deps (fo, f, REF_BB(f->msched[i]), f->INSN(f->msched[i]).dep, 1);
664
      fprintf (fo, ") begin\n");
665
      print_turn_off_dep (fo, f, dep);
666
      fprintf (fo, "      f_stb[%i] <= #1 1'b1;\n", cur_call++);
667
      fprintf (fo, "      fstart_o <= #1 1'b1;\n");
668
      if (log2 (f->nfdeps))
669
        fprintf (fo, "      fid_o <= #1 %i'h%x;\n", log2 (f->nfdeps), func_index (f, f->msched[i]));
670
 
671
      for (j = 0; j < 6; j++)
672
        fprintf (fo, "      fr%i_o <= #1 t%x_%x;\n", j + 3,
673
                       REF_BB (f->msched[i]), REF_I (f->msched[i]) - 6 + i);
674
      fprintf (fo, "    end\n");
675
    }
676
    fprintf (fo, "    if (f_end[%i]) begin\n", ncalls - 1);
677
    fprintf (fo, "      f_stb <= #1 %i'h0;\n", ncalls);
678
    fprintf (fo, "      f_start_o <= #1 1'b0;\n");
679
    fprintf (fo, "    end\n");
680
    fprintf (fo, "  end\n");
681
    fprintf (fo, "end\n");
682
  }
683
 
684 879 markom
  fprintf (fo, "\n/* Basic blocks state machine */\n");
685
  fprintf (fo, "always @(posedge clk or posedge rst)\n");
686
  fprintf (fo, "begin\n");
687 902 markom
  fprintf (fo, "  if (rst) bb_stb <= #1 %i'h%x;\n", f->num_bb, 0);
688
  fprintf (fo, "  else if (end_o) bb_stb <= #1 %i'h%x;\n", f->num_bb, 0);
689 879 markom
  for (i = 0; i < f->num_bb; i++) {
690 902 markom
    fprintf (fo, "  else if (bb_start[%i]) begin\n", i);
691 879 markom
    fprintf (fo, "    bb_stb <= #1 %i'h%x;\n", f->num_bb, 1 << i);
692
  }
693
  fprintf (fo, "  end else if (end_o) begin\n");
694
  fprintf (fo, "    bb_stb <= #1 %i'h%x;\n", f->num_bb, 0);
695
  fprintf (fo, "  end\n");
696
  fprintf (fo, "end\n");
697
 
698
  /* output footer */
699
  fprintf (fo, "\nendmodule\n");
700
 
701
  fclose (fo);
702
}
703
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.