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 1061

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

powered by: WebSVN 2.1.0

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