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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_2_x/] [or1ksim/] [cuc/] [verilog.c] - Blame information for rev 940

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 915 markom
#include "profiler.h"
27 879 markom
 
28 915 markom
/* Shortcut */
29
#define GEN(x...) fprintf (fo, x)
30
 
31 907 markom
/* Find index of load/store/call */
32
int find_lsc_index (cuc_func *f, int ref)
33
{
34
  int c = 0;
35 879 markom
  int i;
36 907 markom
  int load;
37
 
38
  if (f->INSN(ref).index == II_CALL) {
39
    for (i = 0; i < f->nmsched; i++) {
40
      if (f->msched[i] == ref) break;
41
      if (f->mtype[i] & MT_CALL) c++;
42
    }
43
  } else {
44
    load = II_IS_LOAD (f->INSN(ref).index);
45
    for (i = 0; i < f->nmsched; i++) {
46
      if (f->msched[i] == ref) break;
47
      if (load && (f->mtype[i] & MT_LOAD)
48
       || !load && (f->mtype[i] & MT_STORE)) c++;
49
    }
50 879 markom
  }
51
  return c;
52
}
53
 
54
/* Print out dependencies as verilog expression */
55
void print_deps (FILE *fo, cuc_func *f, int b, dep_list *t, int registered)
56
{
57
  if (t) {
58
    int first = 0;
59
    while (t) {
60 907 markom
      if (f->INSN(t->ref).type & IT_MEMORY) {
61 915 markom
        GEN ("%s%c_end[%i]", first ? " && " : "",
62 907 markom
                  II_IS_LOAD (f->INSN(t->ref).index) ? 'l' : 's', find_lsc_index (f, t->ref));
63
      } else if (f->INSN(t->ref).index == II_CALL) {
64
        int x;
65 915 markom
        GEN ("%sf_end[%i]", first ? " && " : "", find_lsc_index (f, t->ref));
66 907 markom
      } else {
67
        printf ("print_deps: err %x\n", t->ref);
68
        assert (0);
69
      }
70 879 markom
      first = 1;
71
      t = t->next;
72
    }
73
  } else {
74 915 markom
    if (registered) GEN ("bb_start_r[%i]", b);
75
    else GEN ("bb_start[%i]", b);
76 879 markom
  }
77
}
78
 
79
char *print_op_v (cuc_func *f, char *s, int ref, int j)
80
{
81
  unsigned long op = f->INSN(ref).op[j];
82
  unsigned long opt = f->INSN(ref).opt[j];
83
  switch (opt & ~OPT_DEST) {
84
    case OPT_NONE: assert (0); break;
85 937 markom
    case OPT_CONST: if (f->INSN(ref).type & IT_COND && (f->INSN(ref).index == II_CMOV
86
                     || f->INSN(ref).index == II_ADD)) {
87 906 markom
                      assert (op == 0 || op == 1);
88
                      sprintf (s, "1'b%x", op);
89
                    } else sprintf (s, "32'h%x", op);
90
                    break;
91 879 markom
    case OPT_REGISTER:
92
                    if (opt & OPT_DEST) sprintf (s, "t%x_%x", REF_BB(ref), REF_I(ref));
93
                    else sprintf (s, "r%i_%c", op, opt & OPT_DEST ? 'o' : 'i');
94
                    break;
95 907 markom
#if 0
96
    case OPT_FREG:  assert (opt & OPT_DEST);
97
                    sprintf (s, "fr%i_o", op);
98
                    break;
99
#endif
100 879 markom
    case OPT_REF:   sprintf (s, "t%x_%x", REF_BB(op), REF_I(op)); break;
101
  }
102
  return s;
103
}
104
 
105
/* Prints out specified instruction */
106
void print_insn_v (FILE *fo, cuc_func *f, int b, int i)
107
{
108
  cuc_insn *ii = &f->bb[b].insn[i];
109
  char *s = known[ii->index].rtl;
110
  char tmp[200] = "";
111 883 markom
 
112 931 markom
  assert (s);
113 879 markom
  while (*s) {
114
    if (*s <= MAX_OPERANDS) {
115
      char t[30];
116
      sprintf (tmp, "%s%s", tmp, print_op_v (f, t, REF(b, i), *s - 1));
117
    } else if (*s == '\b') sprintf (tmp, "%s%i", b);
118
    else sprintf (tmp, "%s%c", tmp, *s);
119
    s++;
120
  }
121 915 markom
  GEN ("%-40s /* %s */\n", tmp, ii->disasm);
122 879 markom
  if (ii->type & IT_MEMORY) {
123 907 markom
    int j, nls = find_lsc_index (f, REF (b, i));
124 879 markom
    if (II_IS_LOAD (ii->index)) {
125
      int nm;
126
      for (nm = 0; nm < f->nmsched; nm++) if (f->msched[nm] == REF (b, i)) break;
127
      assert (nm < f->nmsched);
128
 
129 926 markom
      GEN ("  if (l_end[%i]) t%x_%x <= #Tp ", nls, b, i);
130 879 markom
      switch (f->mtype[nm] & (MT_WIDTH | MT_SIGNED)) {
131 915 markom
        case 1: GEN ("lwb_dat_i & 32'hff;\n");
132 879 markom
                break;
133 915 markom
        case 2: GEN ("lwb_dat_i & 32'hffff;\n");
134 879 markom
                break;
135
        case 4 | MT_SIGNED:
136 915 markom
        case 4: GEN ("lwb_dat_i;\n");
137 879 markom
                break;
138
        case 1 | MT_SIGNED:
139 915 markom
                GEN ("{24{lwb_dat_i[7]}, lwb_dat_i[7:0]};\n");
140 879 markom
                break;
141
        case 2 | MT_SIGNED:
142 915 markom
                GEN ("{16{lwb_dat_i[15]}, lwb_dat_i[15:0]};\n");
143 879 markom
                break;
144
        default: assert (0);
145
      }
146
    }
147
  } else if (ii->index == II_LRBB) {
148 915 markom
    GEN ("  if (rst) t%x_%x <= #Tp 1'b0;\n", b, i);
149 932 markom
    assert (f->bb[b].prev[0] >= 0);
150
    if (f->bb[b].prev[0] == BBID_START)
151
      GEN ("  else if (bb_start[%i]) t%x_%x <= #Tp start_i;\n", b, b, i);
152
    else
153
      GEN ("  else if (bb_start[%i]) t%x_%x <= #Tp bb_stb[%i];\n", b, b, i, f->bb[b].prev[0]);
154 879 markom
  } else if (ii->index == II_REG) {
155
    assert (ii->opt[1] == OPT_REF);
156 926 markom
    GEN ("  if (");
157 879 markom
    if (f->bb[b].mdep) print_deps (fo, f, b, f->bb[b].mdep, 0);
158 915 markom
    else GEN ("bb_stb[%i]", b);
159
    GEN (") t%x_%x <= #Tp t%x_%x;\n",  b, i,
160 879 markom
                    REF_BB (ii->op[1]), REF_I (ii->op[1]));
161
  }
162
}
163
 
164
/* Outputs binary number */
165 907 markom
static char *bin_str (unsigned long x, int len)
166 879 markom
{
167
  static char bx[33];
168
  char *s = bx;
169
  while (len > 0) *s++ = '0' + ((x >> --len) & 1);
170
  *s = '\0';
171
  return bx;
172
}
173
 
174
/* Returns index of branch instruction inside a block b */
175 907 markom
static int branch_index (cuc_bb *bb)
176 879 markom
{
177
  int i;
178
  for (i = bb->ninsn - 1; i >= 0; i--)
179
    if (bb->insn[i].type & IT_BRANCH) return i;
180
  return -1;
181
}
182
 
183 907 markom
static void print_turn_off_dep (FILE *fo, cuc_func *f, dep_list *dep)
184
{
185
  while (dep) {
186
    assert (f->INSN(dep->ref).type & IT_MEMORY || f->INSN(dep->ref).index == II_CALL);
187 915 markom
    GEN ("      %c_stb[%i] <= #Tp 1'b0;\n", f->INSN(dep->ref).index == II_CALL ? 'f'
188 907 markom
            : II_IS_LOAD (f->INSN(dep->ref).index) ? 'l' : 's', find_lsc_index (f, dep->ref));
189
    dep = dep->next;
190
  }
191
}
192
 
193
static int func_index (cuc_func *f, int ref)
194
{
195
  int i;
196
  unsigned long addr;
197
  assert (f->INSN(ref).index == II_CALL && f->INSN(ref).opt[0] & OPT_CONST);
198
  addr = f->INSN(ref).op[0];
199
  for (i = 0; i < f->nfdeps; i++)
200
    if (f->fdeps[i]->start_addr == addr) return i;
201
 
202
  assert (0);
203
  return -1;
204
}
205
 
206 879 markom
/* Generates verilog file out of insn dataflow */
207 940 markom
void output_verilog (cuc_func *f, char *filename, char *funcname)
208 879 markom
{
209
  FILE *fo;
210
  int b, i, j;
211
  int ci = 0, co = 0;
212 907 markom
  int nloads = 0, nstores = 0, ncalls = 0;
213 879 markom
  char tmp[256];
214
  cuc_bb *end_bb = NULL;
215
  int end_bb_no = -1;
216
  sprintf (tmp, "%s.v", filename);
217
 
218 902 markom
  log ("Generating verilog file \"%s\"\n", tmp);
219
  printf ("Generating verilog file \"%s\"\n", tmp);
220 879 markom
  if ((fo = fopen (tmp, "wt+")) == NULL) {
221
    fprintf (stderr, "Cannot open '%s'\n", tmp);
222
    exit (1);
223
  }
224
 
225 883 markom
  for (b = 0; b < f->num_bb; b++)
226 879 markom
    if (f->bb[b].type & BB_END) end_bb = &f->bb[end_bb_no = b];
227
  assert (end_bb && end_bb->type & BB_END);
228
 
229
  /* output header */
230 915 markom
  GEN ("/* %s -- generated by OpenRISC Custom Unit Compiler\n", tmp);
231
  GEN ("   (C) 2002 OpenCores http://www.opencores.org/\n");
232 940 markom
  GEN ("   function   \"%s\"\n", funcname);
233 915 markom
  GEN ("   at         %08x - %08x\n", f->start_addr, f->end_addr);
234
  GEN ("   num BBs    %i */\n\n", f->num_bb);
235
  GEN ("module %s (clk, rst,\n", filename);
236
  GEN ("              lwb_adr_o, lwb_dat_i, lwb_cycstb_o,\n");
237
  GEN ("              lwb_sel_o, lwb_linbrst_o, lwb_ack_i,\n");
238
  GEN ("              swb_adr_o, swb_dat_o, swb_cycstb_o,\n");
239
  GEN ("              swb_sel_o, swb_linbrst_o, swb_ack_i,\n");
240 879 markom
 
241 915 markom
  GEN ("/* inputs */  ");
242 879 markom
  for (i = 0; i < MAX_REGS; i++)
243 883 markom
    if (f->used_regs[i]) {
244 915 markom
      GEN ("r%i_i, ", i);
245 879 markom
      ci++;
246
    }
247 915 markom
  if (!ci) GEN ("/* NONE */");
248 879 markom
 
249 915 markom
  GEN ("\n/* outputs */ ");
250 879 markom
  for (i = 0; i < MAX_REGS; i++)
251 883 markom
    if (f->lur[i] >= 0 && !f->saved_regs[i]) {
252 915 markom
      GEN ("r%i_o, ", i);
253 879 markom
      co++;
254
    }
255
 
256 915 markom
  if (!co) GEN ("/* NONE */");
257 907 markom
  if (f->nfdeps) {
258 915 markom
    GEN ("\n/* f. calls */, fstart_o, %sfend_i, fr11_i, ",
259 907 markom
                    log2 (f->nfdeps) > 0 ? "fid_o, " : "");
260 915 markom
    for (i = 0; i < 6; i++) GEN ("fr%i_o, ", i + 3);
261 907 markom
  }
262 915 markom
  GEN ("\n              start_i, end_o, busy_o);\n\n");
263 879 markom
 
264 915 markom
  GEN ("parameter Tp = 1;\n\n");
265
 
266
  GEN ("input         clk, rst;\n");
267
  GEN ("input         start_i;\t/* Module starts when set to 1 */ \n");
268
  GEN ("output        end_o;\t/* Set when module finishes, cleared upon start_i == 1 */\n");
269
  GEN ("output        busy_o;\t/* Set when module should not be interrupted */\n");
270
  GEN ("\n/* Bus signals */\n");
271
  GEN ("output        lwb_cycstb_o, swb_cycstb_o;\n");
272
  GEN ("input         lwb_ack_i, swb_ack_i;\n");
273
  GEN ("output  [3:0] lwb_sel_o, swb_sel_o;\n");
274
  GEN ("output [31:0] lwb_adr_o, swb_adr_o;\n");
275
  GEN ("output        lwb_linbrst_o, swb_linbrst_o;\n");
276
  GEN ("input  [31:0] lwb_dat_i;\n");
277
  GEN ("output [31:0] swb_dat_o;\n\n");
278 879 markom
 
279 915 markom
  GEN ("reg           lwb_cycstb_o, swb_cycstb_o;\n");
280
  GEN ("reg    [31:0] lwb_adr_o, swb_adr_o;\n");
281
  GEN ("reg     [3:0] lwb_sel_o, swb_sel_o;\n");
282
  GEN ("reg    [31:0] swb_dat_o;\n");
283
  GEN ("reg           lwb_linbrst_o, swb_linbrst_o;\n");
284 879 markom
 
285 915 markom
  if (ci || co) GEN ("\n/* module ports */\n");
286 879 markom
  if (ci) {
287
    int first = 1;
288 915 markom
    GEN ("input  [31:0]");
289 879 markom
    for (i = 0; i < MAX_REGS; i++)
290 883 markom
      if (f->used_regs[i]) {
291 915 markom
        GEN ("%sr%i_i", first ? " " : ", ", i);
292 879 markom
        first = 0;
293
      }
294 915 markom
    GEN (";\n");
295 879 markom
  }
296
 
297
  if (co) {
298
    int first = 1;
299 915 markom
    GEN ("output [31:0]");
300 879 markom
    for (i = 0; i < MAX_REGS; i++)
301 883 markom
      if (f->lur[i] >= 0 && !f->saved_regs[i]) {
302 915 markom
        GEN ("%sr%i_o", first ? " " : ", ", i);
303 879 markom
        first = 0;
304
      }
305 915 markom
    GEN (";\n");
306 879 markom
  }
307
 
308 907 markom
  if (f->nfdeps) {
309 915 markom
    GEN ("\n/* Function calls */\n");
310
    GEN ("output [31:0] fr3_o");
311
    for (i = 1; i < 6; i++) GEN (", fr%i_o", i + 3);
312
    GEN (";\n");
313 924 markom
    GEN ("input  [31:0] fr11_i;\n");
314 915 markom
    if (log2(f->nfdeps) > 0) GEN ("output [%i:0] fid_o;\n", log2(f->nfdeps));
315
    GEN ("output        fstart_o;\n");
316
    GEN ("input         fend_i;\n");
317 907 markom
  }
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 915 markom
    GEN ("\n/* internal registers for loads */\n");
330 879 markom
    for (i = 0; i < f->nmsched; i++)
331 907 markom
      if (f->mtype[i] & MT_LOAD) {
332 915 markom
        GEN ("%st%x_%x", first ? "reg    [31:0] " : ", ",
333 879 markom
                REF_BB(f->msched[i]), REF_I(f->msched[i]));
334 883 markom
 
335
        if (num >= 8) {
336 915 markom
          GEN (";\n");
337 883 markom
          first = 1;
338
          num = 0;
339
        } else {
340
          first = 0;
341
          num++;
342
        }
343 879 markom
      }
344 915 markom
    if (!first) GEN (";\n");
345 879 markom
  }
346
 
347 907 markom
  /* Internal register for function return value */
348
  if (f->nfdeps) {
349 915 markom
    GEN ("\n/* Internal register for function return value */\n");
350
    GEN ("reg     [31:0] fr11_r;\n");
351 907 markom
  }
352
 
353 915 markom
  GEN ("\n/* 'zero or one' hot state machines */\n");
354
  if (nloads) GEN ("reg     [%i:0] l_stb; /* loads */\n", nloads - 1);
355
  if (nstores) GEN ("reg     [%i:0] s_stb; /* stores */\n", nstores - 1);
356
  GEN ("reg     [%i:0] bb_stb; /* basic blocks */\n", f->num_bb - 1);
357 879 markom
 
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 915 markom
          if (first == 2) GEN ("\n/* basic block condition wires */\n");
367
          GEN ("%st%x_%x", first ? "wire          " : ", ", b, i);
368 883 markom
          if (num >= 8) {
369 915 markom
            GEN (";\n");
370 879 markom
            first = 1;
371
            num = 0;
372
          } else {
373
            first = 0;
374
            num++;
375
          }
376
        }
377 915 markom
    if (!first) GEN (";\n");
378 879 markom
 
379 915 markom
    GEN ("\n/* forward declaration of normal wires */\n");
380 879 markom
    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 915 markom
          GEN ("%st%x_%x", first ? "wire   [31:0] " : ", ", b, i);
390 883 markom
          if (num >= 8) {
391 915 markom
            GEN (";\n");
392 879 markom
            first = 1;
393
            num = 0;
394
          } else {
395
            first = 0;
396
            num++;
397
          }
398
        }
399 915 markom
    if (!first) GEN (";\n");
400 879 markom
 
401 915 markom
    GEN ("\n/* forward declaration registers */\n");
402 879 markom
    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 915 markom
          GEN ("%st%x_%x", first ? "reg    [31:0] " : ", ", b, i);
409 883 markom
          if (num >= 8) {
410 915 markom
            GEN (";\n");
411 879 markom
            first = 1;
412
            num = 0;
413
          } else {
414
            first = 0;
415
            num++;
416
          }
417
        }
418 915 markom
    if (!first) GEN (";\n");
419 879 markom
 
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 915 markom
          GEN ("%st%x_%x", first ? "reg           " : ", ", b, i);
427 883 markom
          if (num >= 8) {
428 915 markom
            GEN (";\n");
429 879 markom
            first = 1;
430
            num = 0;
431
          } else {
432
            first = 0;
433
            num++;
434
          }
435
        }
436 915 markom
    if (!first) GEN (";\n");
437 879 markom
  }
438
 
439 915 markom
  if (nloads || nstores) GEN ("\n/* dependencies */\n");
440
  if (nloads) GEN ("wire    [%i:0] l_end = l_stb & {%i{lwb_ack_i}};\n",
441 879 markom
                  nloads - 1, nloads);
442 915 markom
  if (nstores) GEN ("wire    [%i:0] s_end = s_stb & {%i{swb_ack_i}};\n",
443 879 markom
                  nstores - 1, nstores);
444 915 markom
  if (ncalls) GEN ("wire    [%i:0] f_end = f_stb & {%i{fend_i}};\n",
445 907 markom
                  ncalls - 1, ncalls);
446 879 markom
 
447 915 markom
  GEN ("\n/* last dependency */\n");
448
  GEN ("wire   end_o = bb_stb[%i]", end_bb_no);
449 879 markom
  if (end_bb->mdep) {
450 915 markom
    GEN (" && ");
451 879 markom
    print_deps (fo, f, end_bb_no, end_bb->mdep, 0);
452
  }
453 907 markom
 
454 925 markom
  /* Is there a loop right at the end? */
455
  if (end_bb->next[0] != BBID_END || end_bb->next[1] != BBID_END && end_bb->next[1] >= 0) {
456 879 markom
    int bidx = branch_index (end_bb);
457
    char t[30];
458
    print_op_v (f, t, REF (end_bb_no, bidx), 1);
459 915 markom
    GEN (" && !%s", t);
460 879 markom
  }
461 915 markom
  GEN (";\n");
462 924 markom
  GEN ("wire   busy_o = |bb_stb;\n");
463
 
464 879 markom
 
465 915 markom
  GEN ("\n/* Basic block triggers */\n");
466
  GEN ("wire   [%2i:0] bb_start = {\n", f->num_bb - 1);
467 879 markom
  for (b = f->num_bb - 1; b >= 0; b--) {
468 915 markom
    GEN ("    /* bb_start[%2i] */ ", b);
469 932 markom
    for (i = 0; i < 2; i++) if (f->bb[b].prev[i] >= 0 && f->bb[b].prev[i] != BBID_START) {
470 925 markom
      cuc_bb *prev = &f->bb[f->bb[b].prev[i]];
471 879 markom
      int t;
472 925 markom
      if (i) GEN ("\n                    || ");
473 879 markom
      if (prev->mdep) {
474 925 markom
        print_deps (fo, f, f->bb[b].prev[i], prev->mdep, 0);
475 915 markom
        GEN (" && ");
476 879 markom
      }
477 925 markom
      GEN ("bb_stb[%i]", f->bb[b].prev[i]);
478
      if (prev->next[0] >= 0 && prev->next[0] != BBID_END
479
       && prev->next[1] >= 0 && prev->next[1] != BBID_END) {
480
        int bi = REF (f->bb[b].prev[i], branch_index (&f->bb[f->bb[b].prev[i]]));
481 924 markom
        int ci;
482
        assert (bi >= 0);
483
        ci = f->INSN(bi).op[1];
484
        t = prev->next[0] == b;
485 915 markom
        GEN (" && ");
486 924 markom
        if (f->INSN(bi).opt[1] & OPT_REF) {
487
          GEN ("%st%x_%x", t ? "" : "!", REF_BB(ci), REF_I(ci));
488
        } else {
489
          fprintf (stderr, "%x!%x!%x\n", bi, ci, f->INSN(bi).opt[1]);
490
          assert (f->INSN(bi).opt[1] & OPT_CONST);
491
          GEN ("%s%i", t ? "" : "!", ci);
492
        }
493 879 markom
      }
494 925 markom
    } else break;
495
    if (!i) GEN ("start_i");
496 915 markom
    if (b == 0) GEN ("};\n");
497
    else GEN (",\n");
498 879 markom
  }
499
 
500 915 markom
  GEN ("\n/* Register the bb_start */\n");
501
  GEN ("reg   [%2i:0] bb_start_r;\n\n", f->num_bb - 1);
502
  GEN ("always @(posedge rst or posedge clk)\n");
503
  GEN ("begin\n");
504
  GEN ("  if (rst) bb_start_r <= #Tp %i'b0;\n", f->num_bb);
505
  GEN ("  else if (end_o) bb_start_r <= #Tp %i'b0;\n", f->num_bb);
506
  GEN ("  else bb_start_r <= #Tp bb_start;\n");
507
  GEN ("end\n");
508 879 markom
 
509 915 markom
  GEN ("\n/* Logic */\n");
510 879 markom
  /* output body */
511
  for (b = 0; b < f->num_bb; b++) {
512 915 markom
    GEN ("\t\t/* BB%i */\n", b);
513 879 markom
    for (i = 0; i < f->bb[b].ninsn; i++)
514
      print_insn_v (fo, f, b, i);
515 915 markom
    GEN ("\n");
516 879 markom
  }
517
 
518
  if (co) {
519 915 markom
    GEN ("\n/* Outputs */\n");
520 879 markom
    for (i = 0; i < MAX_REGS; i++)
521 883 markom
      if (f->lur[i] >= 0 && !f->saved_regs[i])
522 915 markom
        GEN ("assign r%i_o = t%x_%x;\n", i, REF_BB(f->lur[i]),
523 883 markom
                        REF_I(f->lur[i]));
524 879 markom
  }
525
 
526
  if (nstores) {
527
    int cur_store = 0;
528 915 markom
    GEN ("\n/* Memory stores */\n");
529
    GEN ("always @(posedge clk or posedge rst)\nbegin\n");
530
    GEN ("  if (rst) swb_dat_o <= #Tp 32'h0;\n");
531 879 markom
    for (i = 0; i < f->nmsched; i++)
532 907 markom
      if (f->mtype[i] & MT_STORE) {
533 879 markom
        char t[30];
534 915 markom
        GEN ("  else if (s_stb[%i]) swb_dat_o <= #Tp %s;\n", cur_store++,
535 879 markom
                        print_op_v (f, t, f->msched[i], 0));
536
        //printf ("msched[%i] = %x (mtype %x) %x\n", i, f->msched[i], f->mtype[i], f->INSN(f->msched[i]).op[0]);
537
      }
538 915 markom
    GEN ("end\n");
539 879 markom
  }
540
 
541
  if (nloads) {
542
    int cur_load = 0;
543 915 markom
    GEN ("\n/* Load state machine */\n");
544
    GEN ("always @(posedge clk or posedge rst)\n");
545
    GEN ("begin\n");
546
    GEN ("  if (rst) begin\n");
547
    GEN ("    l_stb <= #Tp %i'h0;\n", nloads);
548
    GEN ("    lwb_cycstb_o <= #Tp 1'b0;\n");
549
    GEN ("    lwb_sel_o[3:0] <= #Tp 4'b0000;\n");
550
    GEN ("    lwb_linbrst_o <= #Tp 1'b0;\n");
551
    GEN ("    lwb_adr_o <= #Tp 32'h0;\n");
552
    GEN ("  end else begin\n");
553 883 markom
    cucdebug (1, "loads \n");
554 907 markom
    for (i = 0; i < f->nmsched; i++) if (f->mtype[i] & MT_LOAD) {
555 879 markom
      char t[30];
556
      dep_list *dep = f->INSN(f->msched[i]).dep;
557 883 markom
      cucdebug (1, "msched[%i] = %x (mtype %x)\n", i, f->msched[i], f->mtype[i]);
558 879 markom
      assert (f->INSN(f->msched[i]).opt[1] & (OPT_REF | OPT_REGISTER));
559 915 markom
      GEN ("    if (");
560 879 markom
      print_deps (fo, f, REF_BB(f->msched[i]), f->INSN(f->msched[i]).dep, 1);
561 915 markom
      GEN (") begin\n");
562 907 markom
      print_turn_off_dep (fo, f, dep);
563 915 markom
      GEN ("      l_stb[%i] <= #Tp 1'b1;\n", cur_load++);
564
      GEN ("      lwb_cycstb_o <= #Tp 1'b1;\n");
565
      GEN ("      lwb_sel_o[3:0] <= #Tp 4'b");
566 879 markom
      switch (f->mtype[i] & MT_WIDTH) {
567 915 markom
        case 1: GEN ("0001 << (%s & 32'h3);\n",
568 879 markom
                                print_op_v (f, t, f->msched[i], 1)); break;
569 915 markom
        case 2: GEN ("0011 << ((%s & 32'h1) << 1);\n",
570 879 markom
                                print_op_v (f, t, f->msched[i], 1)); break;
571 915 markom
        case 4: GEN ("1111;\n"); break;
572 879 markom
        default: assert (0);
573
      }
574 915 markom
      GEN ("      lwb_linbrst_o <= #Tp 1'b%i;\n",
575 879 markom
                      (f->mtype[i] & MT_BURST) && !(f->mtype[i] & MT_BURSTE) ? 1 : 0);
576 915 markom
      GEN ("      lwb_adr_o <= #Tp t%x_%x & ~32'h3;\n",
577 879 markom
                      REF_BB(f->INSN(f->msched[i]).op[1]), REF_I(f->INSN(f->msched[i]).op[1]));
578 915 markom
      GEN ("    end\n");
579 879 markom
    }
580 915 markom
    GEN ("    if (l_end[%i]) begin\n", nloads - 1);
581
    GEN ("      l_stb <= #Tp %i'h0;\n", nloads);
582
    GEN ("      lwb_cycstb_o <= #Tp 1'b0;\n");
583
    GEN ("      lwb_sel_o[3:0] <= #Tp 4'b0000;\n");
584
    GEN ("      lwb_linbrst_o <= #Tp 1'b0;\n");
585
    GEN ("      lwb_adr_o <= #Tp 32'h0;\n");
586
    GEN ("    end\n");
587
    GEN ("  end\n");
588
    GEN ("end\n");
589 879 markom
  }
590
 
591
  if (nstores) {
592
    int cur_store = 0;
593 915 markom
    GEN ("\n/* Store state machine */\n");
594
    GEN ("always @(posedge clk or posedge rst)\n");
595
    GEN ("begin\n");
596
    GEN ("  if (rst) begin\n");
597
    GEN ("    s_stb <= #Tp %i'h0;\n", nstores);
598
    GEN ("    swb_cycstb_o <= #Tp 1'b0;\n");
599
    GEN ("    swb_sel_o[3:0] <= #Tp 4'b0000;\n");
600
    GEN ("    swb_linbrst_o <= #Tp 1'b0;\n");
601
    GEN ("    swb_adr_o <= #Tp 32'h0;\n");
602
    GEN ("  end else begin\n");
603 883 markom
    cucdebug (1, "stores \n");
604 907 markom
    for (i = 0; i < f->nmsched; i++) if (f->mtype[i] & MT_STORE) {
605 879 markom
      char t[30];
606
      dep_list *dep = f->INSN(f->msched[i]).dep;
607 883 markom
      cucdebug (1, "msched[%i] = %x (mtype %x)\n", i, f->msched[i], f->mtype[i]);
608 879 markom
      assert (f->INSN(f->msched[i]).opt[1] & (OPT_REF | OPT_REGISTER));
609 915 markom
      GEN ("    if (");
610 879 markom
      print_deps (fo, f, REF_BB(f->msched[i]), f->INSN(f->msched[i]).dep, 1);
611 915 markom
      GEN (") begin\n");
612 907 markom
      print_turn_off_dep (fo, f, dep);
613 915 markom
      GEN ("      s_stb[%i] <= #Tp 1'b1;\n", cur_store++);
614
      GEN ("      swb_cycstb_o <= #Tp 1'b1;\n");
615
      GEN ("      swb_sel_o[3:0] <= #Tp 4'b");
616 879 markom
      switch (f->mtype[i] & MT_WIDTH) {
617 915 markom
        case 1: GEN ("0001 << (%s & 32'h3);\n",
618 879 markom
                                print_op_v (f, t, f->msched[i], 1)); break;
619 915 markom
        case 2: GEN ("0011 << ((%s & 32'h1) << 1);\n",
620 879 markom
                                print_op_v (f, t, f->msched[i], 1)); break;
621 915 markom
        case 4: GEN ("1111;\n"); break;
622 879 markom
        default: assert (0);
623
      }
624 915 markom
      GEN ("      swb_linbrst_o <= #Tp 1'b%i;\n",
625 879 markom
                      (f->mtype[i] & MT_BURST) && !(f->mtype[i] & MT_BURSTE) ? 1 : 0);
626 915 markom
      GEN ("      swb_adr_o <= #Tp t%x_%x & ~32'h3;\n",
627 879 markom
                      REF_BB(f->INSN(f->msched[i]).op[1]), REF_I(f->INSN(f->msched[i]).op[1]));
628 915 markom
      GEN ("    end\n");
629 879 markom
    }
630 915 markom
    GEN ("    if (s_end[%i]) begin\n", nstores - 1);
631
    GEN ("      s_stb <= #Tp %i'h0;\n", nstores);
632
    GEN ("      swb_cycstb_o <= #Tp 1'b0;\n");
633
    GEN ("      swb_sel_o[3:0] <= #Tp 4'b0000;\n");
634
    GEN ("      swb_linbrst_o <= #Tp 1'b0;\n");
635
    GEN ("      swb_adr_o <= #Tp 32'h0;\n");
636
    GEN ("    end\n");
637
    GEN ("  end\n");
638
    GEN ("end\n");
639 879 markom
  }
640
 
641 907 markom
  if (ncalls) {
642
    int cur_call = 0;
643 915 markom
    GEN ("\n/* Function calls state machine */\n");
644
    GEN ("always @(posedge clk or posedge rst)\n");
645
    GEN ("begin\n");
646
    GEN ("  if (rst) begin\n");
647
    GEN ("    f_stb <= #Tp %i'h0;\n", nstores);
648
    for (i = 0; i < 6; i++) GEN ("    fr%i_o <= #Tp 32'h0;\n", i + 3);
649
    if (log2(ncalls)) GEN ("    fid_o <= #Tp %i'h0;\n", log2 (f->nfdeps));
650
    GEN ("    fstart_o <= #Tp 1'b0;\n");
651
    //GEN ("    f11_r <= #Tp 32'h0;\n");
652
    GEN ("  end else begin\n");
653 907 markom
    cucdebug (1, "calls \n");
654
    for (i = 0; i < f->nmsched; i++) if (f->mtype[i] & MT_CALL) {
655
      char t[30];
656
      dep_list *dep = f->INSN(f->msched[i]).dep;
657
      cucdebug (1, "msched[%i] = %x (mtype %x)\n", i, f->msched[i], f->mtype[i]);
658
      assert (f->INSN(f->msched[i]).opt[1] & (OPT_REF | OPT_REGISTER));
659 915 markom
      GEN ("    if (");
660 907 markom
      print_deps (fo, f, REF_BB(f->msched[i]), f->INSN(f->msched[i]).dep, 1);
661 915 markom
      GEN (") begin\n");
662 907 markom
      print_turn_off_dep (fo, f, dep);
663 915 markom
      GEN ("      f_stb[%i] <= #Tp 1'b1;\n", cur_call++);
664
      GEN ("      fstart_o <= #Tp 1'b1;\n");
665 907 markom
      if (log2 (f->nfdeps))
666 915 markom
        GEN ("      fid_o <= #Tp %i'h%x;\n", log2 (f->nfdeps), func_index (f, f->msched[i]));
667 907 markom
 
668
      for (j = 0; j < 6; j++)
669 915 markom
        GEN ("      fr%i_o <= #Tp t%x_%x;\n", j + 3,
670 907 markom
                       REF_BB (f->msched[i]), REF_I (f->msched[i]) - 6 + i);
671 915 markom
      GEN ("    end\n");
672 907 markom
    }
673 915 markom
    GEN ("    if (f_end[%i]) begin\n", ncalls - 1);
674
    GEN ("      f_stb <= #Tp %i'h0;\n", ncalls);
675
    GEN ("      f_start_o <= #Tp 1'b0;\n");
676
    GEN ("    end\n");
677
    GEN ("  end\n");
678
    GEN ("end\n");
679 907 markom
  }
680
 
681 915 markom
  GEN ("\n/* Basic blocks state machine */\n");
682
  GEN ("always @(posedge clk or posedge rst)\n");
683
  GEN ("begin\n");
684
  GEN ("  if (rst) bb_stb <= #Tp %i'h%x;\n", f->num_bb, 0);
685
  GEN ("  else if (end_o) bb_stb <= #Tp %i'h%x;\n", f->num_bb, 0);
686 879 markom
  for (i = 0; i < f->num_bb; i++) {
687 924 markom
    GEN ("  end else if (bb_start[%i]) begin\n", i);
688 915 markom
    GEN ("    bb_stb <= #Tp %i'h%x;\n", f->num_bb, 1 << i);
689 879 markom
  }
690 915 markom
  GEN ("  end else if (end_o) begin\n");
691
  GEN ("    bb_stb <= #Tp %i'h%x;\n", f->num_bb, 0);
692
  GEN ("  end\n");
693
  GEN ("end\n");
694 879 markom
 
695
  /* output footer */
696 915 markom
  GEN ("\nendmodule\n");
697 879 markom
 
698
  fclose (fo);
699
}
700
 
701 915 markom
void generate_main (int nfuncs, cuc_func **f, char *filename)
702
{
703
  FILE *fo;
704
  int i, j, nrf, first;
705
  char tmp[256];
706
  int ncallees[MAX_FUNCS];
707
  int maxncallees = 0;
708
  sprintf (tmp, "%s.v", filename);
709
 
710
  for (i = 0, nrf = 0; i < nfuncs; i++) {
711
    if (f[i]) f[i]->tmp = nrf++;
712
    ncallees[i] = 0;
713
  }
714
  if (!nrf) return;
715
 
716
  for (i = 0; i < nfuncs; i++) if (f[i])
717
    for (j = 0; j < f[i]->nfdeps; j++)
718
      ncallees[f[i]->fdeps[j]->tmp]++;
719
 
720
  for (i = 0; i < nrf; i++)
721
    if (maxncallees < ncallees[i]) maxncallees = ncallees[i];
722
 
723
  log ("Generating verilog file \"%s\"\n", tmp);
724
  printf ("Generating verilog file \"%s\"\n", tmp);
725
  if ((fo = fopen (tmp, "wt+")) == NULL) {
726
    fprintf (stderr, "Cannot open '%s'\n", tmp);
727
    exit (1);
728
  }
729
 
730
  /* output header */
731
  GEN ("/* %s -- generated by OpenRISC Custom Unit Compiler\n", tmp);
732
  GEN ("   (C) 2002 OpenCores http://www.opencores.org/ */\n\n");
733
  GEN ("/* Includes %i functions:", nrf);
734
  for (i = 0; i < nfuncs; i++) if (f[i])
735
    GEN ("\n%s", prof_func[i].name);
736
  GEN (" */\n\n");
737
 
738
  GEN ("`include \"timescale.v\"\n\n");
739
  GEN ("module %s (clk, rst,\n", filename);
740
  GEN ("              /* Load and store master Wishbone ports */\n");
741
  GEN ("              lwb_adr_o, lwb_dat_i, lwb_cycstb_o,\n");
742
  GEN ("              lwb_sel_o, lwb_linbrst_o, lwb_ack_i,\n");
743
  GEN ("              swb_adr_o, swb_dat_o, swb_cycstb_o,\n");
744
  GEN ("              swb_sel_o, swb_linbrst_o, swb_ack_i,\n\n");
745
  GEN ("              /* cuc interface */\n");
746
  GEN ("              cuc_stb_i, cuc_adr_i, cuc_dat_i, cuc_dat_o, cuc_we_i, cuc_ack_o);\n\n");
747
 
748
  GEN ("parameter Tp = 1;\n");
749
  GEN ("\n/* module ports */\n");
750
  GEN ("input         clk, rst, cuc_stb_i, cuc_we_i;\n");
751
  GEN ("input         lwb_ack_i, swb_ack_i, lwb_linbrst_o, swb_linbrst_o;\n");
752
  GEN ("output        lwb_cycstb_o, lwb_linbrst_o;\n");
753
  GEN ("output        swb_cycstb_o swb_linbrst_o;\n");
754
  GEN ("output        cuc_ack_o;\n");
755
  GEN ("output  [3:0] lwb_sel_o, swb_sel_o;\n");
756
  GEN ("output [31:0] lwb_adr_o, swb_adr_o, swb_dat_o, cuc_dat_o;\n");
757
  GEN ("input  [15:0] cuc_adr_i;\n");
758
  GEN ("input  [31:0] lwb_dat_i, cuc_dat_i;\n\n");
759 924 markom
 
760
  GEN ("wire   [%2i:0] i_we, i_re, i_start, i_finish, i_selected, i_first_reg;\n", nrf - 1);
761
  GEN ("wire   [%2i:0] i_bidok, main_start, main_end;\n", nrf - 1);
762
  GEN ("reg    [%2i:0] i_go_bsy, main_start_r;\n", nrf - 1);
763 915 markom
 
764 924 markom
  GEN ("assign i_selected = {\n");
765
  for (i = 0; i < nrf; i++)
766
    GEN ("    cuc_adr_i[15:6] == %i%s\n", i, i < nrf - 1 ? "," : "};");
767
 
768
  GEN ("assign i_first_reg = {\n");
769
  for (i = 0; i < nrf; i++) {
770
    for (j = 0; j <= MAX_REGS; j++) if (f[i]->used_regs[j]) break;
771
    GEN ("    cuc_adr_i[5:0] == %i%s\n", j, i < nrf - 1 ? "," : "};");
772
  }
773
 
774
  GEN ("assign i_we = {%i{cuc_stb_i && cuc_we_i}} & i_selected;\n", nrf);
775
  GEN ("assign i_re = {%i{cuc_stb_i && !cuc_we_i}} & i_selected;\n", nrf);
776
 
777
  GEN ("assign i_start = i_go_bsy & {%i{cuc_ack_o}};\n", nrf);
778
  GEN ("assign i_start_bidok = {\n");
779
  for (i = 0; i < nrf; i++)
780
    GEN ("    i_start_bid[%i] < %i%s\n", i, i, i < nrf - 1 ? "," : "};");
781
  GEN ("assign main_start = start_i & i_selected & i_first_reg & i_we;\n");
782
  GEN ("assign main_end = {%i{end_i}} & i_selected;\n");
783
 
784
  GEN ("\nalways @(posedge clk or posedge rst)\n");
785
  GEN ("begin\n");
786
  GEN ("  if (rst) i_go_bsy <= #Tp %i'b0;\n", nrf);
787
  GEN ("  else i_go_bsy <= #Tp i_we | ~i_finish & i_go_bsy;\n");
788
  GEN ("end\n");
789
 
790 915 markom
  /* Function specific data */
791
  for (i = 0; i < nfuncs; i++) if (f[i]) {
792
    int ci = 0, co = 0;
793
    int fn = f[i]->tmp;
794
    GEN ("\n/* Registers for function %s */\n", prof_func[i].name);
795
    for (j = 0, first = 1; j < MAX_REGS; j++) if (f[i]->used_regs[j]) {
796 924 markom
      GEN ("%s i%i_r%ii", first ? "/* inputs */\nreg    [31:0]" : ",", fn, j);
797 915 markom
      first = 0;
798
      ci++;
799
    }
800
    if (ci) GEN (";\n");
801
 
802
    for (j = 0, first = 1; j < MAX_REGS; j++)
803
      if (f[i]->lur[j] >= 0 && !f[i]->saved_regs[j]) {
804 924 markom
        GEN ("%s i%i_r%io", first ? "/* outputs */\nreg    [31:0]" : ",", fn, j);
805 915 markom
        first = 0;
806
        co++;
807
      }
808
    if (co) GEN (";\n");
809 924 markom
 
810
    GEN ("always @(posedge clk or posedge rst)\n");
811
    GEN ("  if (rst) main_start_r <= #Tp %i'b0;\n", nrf);
812
    GEN ("  else main_start_r <= #Tp main_start & i_start_bidok | i_busy | ~i_end & main_start_r;\n");
813 915 markom
 
814
    if (ci) {
815
      GEN ("\n/* write register access */\n");
816
      GEN ("always @(posedge clk or posedge rst)\n");
817
      GEN ("begin\n");
818
      GEN ("  if (rst) begin\n");
819
      for (j = 0; j < MAX_REGS; j++) if (f[i]->used_regs[j])
820
        GEN ("    i%i_r%-2i <= #Tp 32'h0;\n", fn, j);
821 924 markom
      GEN ("  end else if (!i_go_bsy[%i] && i_we[%i])\n", fn, fn);
822 915 markom
      GEN ("    case (cuc_adr_i[5:0])\n");
823
      for (j = 0; j < MAX_REGS; j++) if (f[i]->used_regs[j])
824
        GEN ("      %-2i: r%i <= #Tp cuc_dat_i;\n", j, j);
825
      GEN ("    endcase\n");
826
      GEN ("end\n");
827
    }
828
 
829 924 markom
    GEN ("\n");
830 915 markom
  }
831
 
832 924 markom
  /* Generate machine for reading all function registers. Register read can be
833
     delayed till function completion */
834 915 markom
  {
835
    int co;
836
    GEN ("/* read register access - data */\n");
837
    GEN ("always @(posedge clk or posedge rst)\n");
838
    GEN ("  if (rst) cuc_dat_o <= #Tp 32'h0;\n");
839
    GEN ("  else if (cuc_stb_i && cuc_we_i) begin\n");
840
    GEN ("    ");
841
 
842
    for (i = 0; i < nfuncs; i++) if (f[i]) {
843
      co = 0;
844
      for (j = 0; j < MAX_REGS; j++)
845
        if (f[i]->lur[j] >= 0 && !f[i]->saved_regs[j]) co++;
846
 
847 924 markom
      GEN ("if (cuc_addr_i[15:6] == %i)", f[i]->tmp);
848 915 markom
      if (co) {
849
        first = 1;
850 924 markom
        GEN ("\n      case (cuc_adr_i[5:0])\n");
851 915 markom
        for (j = 0; j < MAX_REGS; j++)
852
          if (f[i]->lur[j] >= 0 && !f[i]->saved_regs[j])
853
            GEN ("      %-2i: cuc_dat_o <= #Tp i%i_r%i;\n", j, f[i]->tmp, j);
854
        GEN ("      endcase\n");
855
      } else {
856
        GEN ("      cuc_dat_o <= #Tp 32'hx;\n");
857
      }
858 924 markom
      GEN ("    else ");
859 915 markom
    }
860
    GEN ("cuc_dat_o <= #Tp 32'hx;\n");
861
    GEN ("  else cuc_dat_o <= #Tp 32'hx;\n");
862
    GEN ("end\n");
863
 
864
    GEN ("\n/* read register access - acknowledge */\n");
865
    GEN ("always @(cuc_stb_i or cuc_we_i or cuc_adr_i");
866
    for (i = 0; i < nrf; i++) GEN (" or main_end%i", i);
867
    GEN (")\n");
868 924 markom
    GEN ("  if (cuc_stb_i && cuc_we_i) cuc_ack_o <= #Tp |(i_selected & main_end)\n");
869 915 markom
    GEN ("  else cuc_ack_o <= #Tp 1'b0;\n");
870
    GEN ("end\n");
871
  }
872
 
873
  for (j = 0; j < 2; j++) {
874
    char t = j ? 's' : 'l';
875
    GEN ("\n/* %s Wishbone bridge */\n", j ? "store" : "load");
876 924 markom
    GEN ("reg [%i:0] %cm_sel;\n", log2 (nrf), t);
877
    GEN ("reg [%i:0] %cm_bid;\n", log2 (nrf), t);
878 915 markom
 
879
    GEN ("\n/* highest bid */\n");
880
    GEN ("always @(");
881
    for (i = 0; i < nrf; i++) GEN ("%si%i_%cwb_cycstb", i > 0 ? " or " : "", i, t);
882
    GEN (")\n");
883
    for (i = 0; i < nrf; i++) GEN ("  %sif (i%i_%cwb_cycstb) %cm_bid <= %i'h%x;\n",
884
                    i ? "else " : "", i, t, t, log2 (nrf) + 1, i);
885
 
886
    GEN ("\n/* selected transfer */\n");
887
    GEN ("always @(posedge clk or posedge rst)\n");
888
    GEN ("  if (rst) %cm_sel <= #Tp %cm_sel <= #Tp %i'h0;\n", t, t, log2 (nrf) + 1);
889
    GEN ("  else if (%cwb_ack_i) %cm_sel <= #Tp %i'h0;\n", t, t, log2 (nrf) + 1);
890
    GEN ("  else if (!%ccyc_ip) %cm_sel <= #Tp %cm_bid;\n", t, t, t);
891
 
892
    GEN ("\n/* Cycle */\n");
893
    GEN ("assign %cwb_cyc_o = (lwb_cycstb | swb_cycstb);\n", t);
894
    GEN ("assign %cwb_stb_o = %cwb_stb_o;\n", t, t);
895
    GEN ("\nalways @(posedge clk or posedge rst)\n");
896
    GEN ("  if (rst) %ccyc_ip <= #Tp 1'b0;\n", t);
897
    GEN ("  else if (%cwb_ack_i) %ccyc_ip <= #Tp 1'b0;\n", t, t);
898
    GEN ("  else %ccyc_ip <= #Tp %cwb_cyc_o;\n", t, t);
899
  }
900
 
901
  GEN ("\n/* Acknowledge */\n");
902
  for (i = 0; i < nrf; i++)
903
    GEN ("assign i%i_swb_ack = ((sm_bid == %i & !scyc_ip) | sm_sel == %i) & swb_ack_i;\n", i, i, i);
904
 
905
  GEN ("\n/* Data */\n");
906
  for (i = 0; i < nrf; i++) GEN ("wire [31:0] i%i_swb_dat;\n", i);
907
  GEN ("\nassign swb_dat_o = ");
908
  for (i = 0; i < nrf - 1; i++)
909
    GEN ("\n  sm_bid == %i ? i%i_swb_dat : ", i, i);
910
  GEN ("i%i_swb_dat;\n\n", nrf - 1);
911
 
912
  for (i = 0; i < nrf; i++) GEN ("wire [31:0] i%i_lwb_dat = lwb_dat_i;\n", i);
913
 
914
  GEN ("\n/* selects */\n");
915
  for (i = 0; i < nrf; i++) GEN ("wire [3:0]  i%i_swb_sel, i%i_lwb_sel;\n", i, i);
916
  GEN ("\nassign swb_sel_o = ");
917
  for (i = 0; i < nrf - 1; i++)
918
    GEN ("\n  sm_bid == %i ? i%i_swb_sel : ", i, i);
919
  GEN ("i%i_swb_sel;\n", nrf - 1);
920
  GEN ("\nassign lwb_sel_o = ");
921
  for (i = 0; i < nrf - 1; i++)
922
    GEN ("\n  lm_bid == %i ? i%i_lwb_sel : ", i, i);
923
  GEN ("i%i_lwb_sel;\n", nrf - 1);
924
 
925
  GEN ("\n/* write enable */\n");
926
  for (i = 0; i < nrf; i++) GEN ("wire i%i_swb_we, i%i_lwb_we;\n", i, i);
927
  GEN ("\nassign swb_we_o = ");
928
  for (i = 0; i < nrf - 1; i++) GEN ("\n  sm_bid == %i ? i%i_swb_we : ", i, i);
929
  GEN ("i%i_swb_we;\n", nrf - 1);
930
 
931
  GEN ("\nassign lwb_we_o = ");
932
  for (i = 0; i < nrf - 1; i++) GEN ("\n  lm_bid == %i ? i%i_lwb_we : ", i, i);
933
  GEN ("i%i_lwb_we;\n", nrf - 1);
934
 
935
  GEN ("\n/* burst enable */\n");
936
  for (i = 0; i < nrf; i++) GEN ("wire i%i_swb_linbrst, i%i_lwb_linbrst;\n", i, i);
937
  GEN ("\nassign swb_linbrst_o = ");
938
  for (i = 0; i < nrf - 1; i++) GEN ("\n  sm_bid == %i ? i%i_swb_linbrst : ", i, i);
939
  GEN ("i%i_swb_linbrst;\n", nrf - 1);
940
  GEN ("\n\nassign lwb_linbrst_o = ");
941
  for (i = 0; i < nrf - 1; i++) GEN ("\n  lm_bid == %i ? i%i_lwb_linbrst : ", i, i);
942
  GEN ("i%i_lwb_linbrst;\n", nrf - 1);
943
 
944
  /* start/end signals */
945
  GEN ("\n\n/* start/end signals */\n");
946
 
947 924 markom
  for (i = 0; i < nrf; i++) {
948
    if (log2 (maxncallees + 1))
949
      GEN ("wire [%i:0] i%i_current = i%i_busy ? i%i_current_r : i%i_start_bid;\n",
950
        log2 (maxncallees + 1), i, i, i, i, i);
951
    else GEN ("wire i%i_current = 0;\n", i);
952
  }
953
  GEN ("\n");
954
 
955 915 markom
  for (i = 0, first = 1; i < nrf; i++) {
956 924 markom
    GEN ("%si%i_current == %i && i_end[%i]", first ? "assign end_o = " : "\n  || ", i, ncallees[i], i);
957 915 markom
    first = 0;
958
  }
959
  GEN (";\n\n");
960
 
961
  for (i = 0, j = 0; i < nfuncs; i++) if (f[i]) {
962
    if (log2 (ncallees[i])) {
963
      GEN ("reg [%i:0] i%i_start_bid;\n", log2 (ncallees[i]), j);
964
      GEN ("always @(start%i", f[i]->tmp);
965
      for (j = 0, first = 1; j < f[i]->nfdeps; j++)
966
        if (f[i]->fdeps[j]) GEN (", ");
967
      GEN (")\n");
968
      GEN ("begin !!!\n"); //TODO
969
      GEN ("  \n");
970
      GEN ("end\n");
971
    }
972 924 markom
    GEN ("wire i%i_start = main_start[%i];\n", j, j);
973 915 markom
    j++;
974
  }
975
  GEN ("\n");
976
 
977
  for (i = 0; i < nfuncs; i++) if (f[i]) {
978
    int nf = f[i]->tmp;
979
    GEN ("\n%s i%i(.clk(clk), .rst(rst),\n", prof_func[i].name, nf);
980
    GEN ("");
981
    GEN ("  .lwb_adr_o(i%i_lwb_adr), .lwb_dat_i(i%i_lwb_dat), .lwb_cycstb_o(i%i_lwb_cycstb),\n",
982
                    nf, nf, nf);
983
    GEN ("  .lwb_sel_o(i%i_lwb_sel), .lwb_linbrst_o(i%i_lwb_linbrst), .lwb_ack_i(i%i_lwb_ack),\n",
984
                    nf, nf, nf);
985
    GEN ("  .swb_adr_o(i%i_swb_adr), .swb_dat_o(i%i_swb_dat), .swb_cycstb_o(i%i_swb_cycstb),\n",
986
                    nf, nf, nf);
987
    GEN ("  .swb_sel_o(i%i_swb_sel), .swb_linbrst_o(i%i_swb_linbrst), .swb_ack_i(i%i_swb_ack),\n",
988
                    nf, nf, nf);
989
    GEN ("  ");
990 924 markom
    for (j = 0, first = 1; j < MAX_REGS; j++) if (f[i]->used_regs[j])
991
      GEN (".r%i_i(i_r%ii[%i]), ", j, j, nf), first = 0;
992 915 markom
 
993 924 markom
    if (first) GEN ("\n  ");
994 915 markom
    for (j = 0, first = 1; j < MAX_REGS; j++)
995
      if (f[i]->lur[j] >= 0 && !f[i]->saved_regs[j])
996 924 markom
        GEN (".r%i_o(i_r%io[%i]), ", j, j, nf), first = 0;
997
    if (first) GEN ("\n  ");
998
    if (f[i]->nfdeps) {
999
      GEN (".fstart_o(i_fstart[%i]), .fend_i(i_fend[%i]), .fid_o(i%i_fid),\n", i, i, i),
1000
      GEN ("  .fr3_o(i%i_fr3), .fr4_o(i%i_fr4), .fr5_o(i%i_fr5), .fr6_o(i%i_fr6),\n");
1001
      GEN ("  .fr7_o(i%i_fr7), .fr8_o(i%i_fr8), .fr11_i(i%i_fr11i),\n  ");
1002
    }
1003
    GEN (".start_i(i_start[%i]), .end_o(i_end[%i]), .busy_o(i_busy[%i]));\n", nf, nf, nf);
1004 915 markom
  }
1005
 
1006
  /* output footer */
1007
  GEN ("\nendmodule\n");
1008
 
1009
  fclose (fo);
1010
}
1011
 

powered by: WebSVN 2.1.0

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