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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_47/] [or1ksim/] [cpu/] [or32/] [execute.c] - Blame information for rev 574

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

Line No. Rev Author Line
1 2 cvs
/* execute.c -- OR1K architecture dependent simulation
2
   Copyright (C) 1999 Damjan Lampret, lampret@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
/* Most of the OR1K simulation is done in here. */
21
 
22
#include <stdlib.h>
23
#include <stdio.h>
24
#include <string.h>
25
#include <ctype.h>
26
 
27 123 markom
#include "config.h"
28 2 cvs
#include "arch.h"
29 133 markom
#include "opcode/or32.h"
30 2 cvs
#include "branch_predict.h"
31
#include "abstract.h"
32 261 markom
#include "labels.h"
33 2 cvs
#include "parse.h"
34
#include "execute.h"
35
#include "stats.h"
36 54 lampret
#include "except.h"
37
#include "sprs.h"
38 102 lampret
#include "sim-config.h"
39 123 markom
#include "debug_unit.h"
40 2 cvs
 
41
/* General purpose registers. */
42
machword reg[MAX_GPRS];
43
 
44
/* Instruction queue */
45
struct iqueue_entry iqueue[20];
46
 
47 83 lampret
/* Is current insn in execution a delay insn? */
48
int delay_insn;
49
 
50 2 cvs
/* Benchmark multi issue execution */
51
int multissue[20];
52
int supercycles;
53 6 lampret
int issued_per_cycle = 4;
54
int hazardwait = 0;
55 2 cvs
 
56 431 markom
/* Whether break was hit - so we can step over a break */
57
static int break_just_hit = 0;
58
 
59 68 lampret
/* freemem 'pointer' */
60
extern unsigned long freemem;
61
 
62 2 cvs
/* Completition queue */
63 138 markom
struct iqueue_entry icomplet[20];
64 2 cvs
 
65 77 lampret
/* Program counter (and translated PC) */
66 2 cvs
unsigned long pc;
67 77 lampret
unsigned long pc_phy;
68 2 cvs
 
69 378 markom
/* Previous program counter */
70 479 markom
unsigned long pcprev = 0;
71 378 markom
 
72 2 cvs
/* Temporary program counter */
73 83 lampret
unsigned long pcnext;
74 2 cvs
 
75 123 markom
/* Delay instruction effective address register */
76
unsigned long pcdelay;
77
 
78 2 cvs
/* CCR */
79
int flag;
80
 
81
/* CCR (for dependency calculation) */
82
char ccr_flag[10] = "flag";
83
 
84
/* Cycles counts fetch stages */
85
int cycles;
86
 
87 537 markom
/* Each cycle has counter of mem_cycles; this value is joined with cycles
88
   at the end of the cycle; no sim originated memory accesses should be
89
   performed inbetween. */
90
int mem_cycles;
91
 
92 535 markom
/* Instructions executed */
93
int instructions;
94
 
95 6 lampret
/* Load and store stalls */
96
int loadcycles, storecycles;
97
 
98 138 markom
/* Local data needed for execution.  */
99
static struct iqueue_entry *cur;
100
static int next_delay_insn;
101
static int breakpoint;
102
static unsigned long *op;
103
static int num_op;
104
 
105 2 cvs
/* Implementation specific.
106
   Get an actual value of a specific register. */
107
 
108 560 markom
unsigned long evalsim_reg32(int regno)
109 2 cvs
{
110 138 markom
  if (regno < MAX_GPRS) {
111 560 markom
    return reg[regno];
112
  } else {
113
    printf("\nABORT: read out of registers\n");
114
    cont_run = 0;
115
    return 0;
116
  }
117
}
118
 
119
/* Implementation specific.
120
   Set a specific register with value. */
121
 
122
void setsim_reg32(int regno, unsigned long value)
123
{
124
  if (regno == 0)               /* gpr0 is always zero */
125
    value = 0;
126
 
127
  if (regno < MAX_GPRS) {
128
    reg[regno] = value;
129
  } else {
130
    printf("\nABORT: write out of registers\n");
131
    cont_run = 0;
132
  }
133
}
134
 
135
/* Implementation specific.
136
   Get an actual value of a specific register. */
137
 
138
inline static unsigned long eval_reg32(int regno)
139
{
140
  if (regno < MAX_GPRS) {
141
    IFF(config.cpu.raw_range) {
142 535 markom
      int delta = (cycles - raw_stats.reg[regno]);
143
      if (delta < config.cpu.raw_range)
144
        raw_stats.range[delta]++;
145
    }
146 344 markom
    debug(9, "atoi ret1\n");
147 138 markom
    return reg[regno];
148
  } else {
149 393 markom
    printf("\nABORT: read out of registers\n");
150 138 markom
    cont_run = 0;
151
    return 0;
152
  }
153 2 cvs
}
154
 
155
/* Implementation specific.
156
   Set a specific register with value. */
157
 
158 560 markom
inline static void set_reg32(int regno, unsigned long value)
159 2 cvs
{
160 262 markom
#if 0   
161 138 markom
  if (strcmp(regstr, FRAME_REG) == 0) {
162
    printf("FP (%s) modified by insn at %x. ", FRAME_REG, pc);
163
    printf("Old:%.8lx  New:%.8lx\n", eval_reg(regno), value);
164
  }
165
 
166
  if (strcmp(regstr, STACK_REG) == 0) {
167
    printf("SP (%s) modified by insn at %x. ", STACK_REG, pc);
168
    printf("Old:%.8lx  New:%.8lx\n", eval_reg(regmo), value);
169
  }
170 2 cvs
#endif
171 560 markom
 
172 138 markom
  if (regno < MAX_GPRS) {
173
    reg[regno] = value;
174 560 markom
    IFF(config.cpu.raw_range)
175 535 markom
      raw_stats.reg[regno] = cycles;
176 138 markom
  } else {
177 393 markom
    printf("\nABORT: write out of registers\n");
178 138 markom
    cont_run = 0;
179
  }
180 2 cvs
}
181
 
182
/* Does srcoperand depend on computation of dstoperand? Return
183
   non-zero if yes.
184
 
185 262 markom
 Cycle t                 Cycle t+1
186
dst: irrelevant         src: immediate                  always 0
187
dst: reg1 direct        src: reg2 direct                0 if reg1 != reg2
188
dst: reg1 disp          src: reg2 direct                always 0
189
dst: reg1 direct        src: reg2 disp                  0 if reg1 != reg2
190
dst: reg1 disp          src: reg2 disp                  always 1 (store must
191
                                                        finish before load)
192 138 markom
dst: flag               src: flag                       always 1
193 2 cvs
*/
194
 
195 138 markom
int depend_operands(prev, next)
196
     struct iqueue_entry *prev;
197
     struct iqueue_entry *next;
198 2 cvs
{
199 138 markom
  /* Find destination type. */
200
  unsigned long type = 0;
201
  int i = 0;
202
  if (or32_opcodes[prev->insn_index].flags & OR32_W_FLAG
203
      && or32_opcodes[next->insn_index].flags & OR32_R_FLAG)
204
    return 1;
205 2 cvs
 
206 138 markom
  while (!(prev->op[i + MAX_OPERANDS] & OPTYPE_LAST))
207
    if (prev->op[i + MAX_OPERANDS] & OPTYPE_DST)
208
      {
209 262 markom
        type = prev->op[i + MAX_OPERANDS];
210
        break;
211 138 markom
      }
212
    else
213
      i++;
214 560 markom
 
215 138 markom
  /* We search all source operands - if we find confict => return 1 */
216
  i = 0;
217
  while (!(next->op[i + MAX_OPERANDS] & OPTYPE_LAST))
218
    if (!(next->op[i + MAX_OPERANDS] & OPTYPE_DST))
219
      {
220 262 markom
        if (next->op[i + MAX_OPERANDS] & OPTYPE_DIS)
221
          if (type & OPTYPE_DIS)
222
            return 1;
223
          else if (next->op[i] == prev->op[i]
224
                   && (next->op[i + MAX_OPERANDS] & OPTYPE_REG))
225
            return 1;
226
        if (next->op[i] == prev->op[i]
227
            && (next->op[i + MAX_OPERANDS] & OPTYPE_REG)
228
            && (type & OPTYPE_REG))
229
          return 1;
230
        i++;
231 138 markom
      }
232
    else
233
      i++;
234
  return 0;
235
}
236 2 cvs
 
237 138 markom
/* Implementation specific.
238
   Parses and returns operands. */
239 2 cvs
 
240 138 markom
static void
241
eval_operands (unsigned long insn, int insn_index, int* breakpoint)
242
{
243
  struct insn_op_struct *opd = op_start[insn_index];
244
  unsigned long data = 0;
245
  int dis = 0;
246
  int no = 0;
247 560 markom
 
248 138 markom
  while (1)
249
    {
250
      unsigned long tmp = 0, nbits = 0;
251
      while (1)
252 262 markom
        {
253
          tmp |= ((insn  >> (opd->type & OPTYPE_SHR)) & ((1 << opd->data) - 1)) << nbits;
254
          nbits += opd->data;
255
          if (opd->type & OPTYPE_OP)
256
            break;
257
          opd++;
258
        }
259 2 cvs
 
260 138 markom
      /* Do we have to sign extend? */
261
      if (opd->type & OPTYPE_SIG)
262 262 markom
        {
263
          int sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
264
          if (tmp & (1 << sbit))
265
            tmp |= 0xFFFFFFFF << sbit;
266
        }
267 138 markom
      if (opd->type & OPTYPE_DIS) {
268 262 markom
        /* We have to read register later.  */
269
        data += tmp;
270
        dis = 1;
271 138 markom
      } else
272 262 markom
        {
273
          if (dis && (opd->type & OPTYPE_REG))
274 560 markom
            op[no] = data + eval_reg32 (tmp);
275 262 markom
          else
276
            op[no] = tmp;
277
          op[no + MAX_OPERANDS] = opd->type | (dis ? OPTYPE_DIS : 0);
278
          no++;
279
          data = 0;
280
          dis = 0;
281
        }
282 138 markom
      if(opd->type & OPTYPE_LAST)
283 262 markom
        return;
284 138 markom
      opd++;
285
    }
286
  num_op = no;
287 2 cvs
}
288
 
289
/* Implementation specific.
290 138 markom
   Evaluates source operand op_no. */
291
 
292 560 markom
inline static unsigned long eval_operand32 (int op_no, int *breakpoint)
293 2 cvs
{
294 344 markom
  debug (9, "%i %08X\n", op_no, op[op_no + MAX_OPERANDS]);
295 538 markom
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS)
296 560 markom
    /* memory accesses are not cached */
297 138 markom
    return eval_mem32 (op[op_no], breakpoint);
298 560 markom
  else if (op[op_no + MAX_OPERANDS] & OPTYPE_REG) {
299
    return eval_reg32 (op[op_no]);
300
  } else {
301 138 markom
    return op[op_no];
302 560 markom
  }
303 2 cvs
}
304
 
305
/* Implementation specific.
306 221 markom
   Evaluates source operand op_no. */
307
 
308 560 markom
static unsigned long eval_operand16 (int op_no, int *breakpoint)
309 221 markom
{
310 344 markom
  debug (9, "%i %08X\n", op_no, op[op_no + MAX_OPERANDS]);
311 458 simons
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS) {
312 221 markom
    return eval_mem16 (op[op_no], breakpoint);
313 458 simons
  }
314 221 markom
  else {
315
    fprintf (stderr, "Invalid operand type.\n");
316
    exit (1);
317
  }
318
}
319
 
320
/* Implementation specific.
321
   Evaluates source operand op_no. */
322
 
323 560 markom
static unsigned long eval_operand8 (int op_no, int *breakpoint)
324 221 markom
{
325 344 markom
  debug (9, "%i %08X\n", op_no, op[op_no + MAX_OPERANDS]);
326 221 markom
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS)
327
    return eval_mem8 (op[op_no], breakpoint);
328
  else {
329
    fprintf (stderr, "Invalid operand type.\n");
330
    exit (1);
331
  }
332
}
333
 
334
/* Implementation specific.
335 2 cvs
   Set destination operand (register direct, register indirect
336
   (with displacement) with value. */
337
 
338 560 markom
inline static void set_operand32(int op_no, unsigned long value, int* breakpoint)
339 2 cvs
{
340 138 markom
  /* Mark this as destination operand.  */
341 560 markom
  IFF (config.cpu.dependstats) op[op_no + MAX_OPERANDS] |= OPTYPE_DST;
342 458 simons
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS) {
343 138 markom
    set_mem32(op[op_no], value, breakpoint);
344 560 markom
  } else if (op[op_no + MAX_OPERANDS] & OPTYPE_REG) {
345 138 markom
    set_reg32(op[op_no], value);
346 560 markom
    /* TODO: OV incorrect */
347 458 simons
    value & 0x80000000 ? setsprbits (SPR_SR, SPR_SR_OV, 1) : setsprbits (SPR_SR, SPR_SR_OV, 0);
348 560 markom
  } else {
349
    fprintf (stderr, "Invalid operand type.\n");
350
    exit (1);
351 458 simons
  }
352 2 cvs
}
353
 
354 221 markom
/* Implementation specific.
355
   Set destination operand (register direct, register indirect
356
   (with displacement) with value. */
357
 
358
void set_operand16(int op_no, unsigned long value, int* breakpoint)
359
{
360
  /* Mark this as destination operand.  */
361 560 markom
  op[op_no + MAX_OPERANDS] |= OPTYPE_DST;
362 458 simons
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS) {
363 221 markom
    set_mem16(op[op_no], value, breakpoint);
364 458 simons
  }
365 574 markom
  else
366 221 markom
    {
367
      fprintf (stderr, "Invalid operand type.\n");
368
      exit (1);
369
    }
370
}
371
 
372
/* Implementation specific.
373
   Set destination operand (register direct, register indirect
374
   (with displacement) with value. */
375
 
376
void set_operand8(int op_no, unsigned long value, int* breakpoint)
377
{
378
  /* Mark this as destination operand.  */
379
  op[op_no + MAX_OPERANDS] |= OPTYPE_DST;
380
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS)
381
    set_mem8(op[op_no], value, breakpoint);
382
  else
383
    {
384
      fprintf (stderr, "Invalid operand type.\n");
385
      exit (1);
386
    }
387
}
388
 
389 123 markom
/* Modified by CZ 26/05/01 for new mode execution */
390
/* Fetch returns nonzero if instruction should NOT be executed.  */
391 557 markom
static inline int fetch()
392
{
393 221 markom
  struct mem_entry *entry;
394 344 markom
  debug(5, "fetch()\n");
395 123 markom
 
396 557 markom
  /* Update the pc for pending exceptions, or get physical pc */
397 574 markom
  if (!pending.valid)
398
    pc_phy = translate_vrt_to_phy_add(pc);
399
 
400 557 markom
  if(pending.valid)
401
    except_handle_backend(pending.type, pending.address, pending.saved);
402 464 simons
 
403 557 markom
  if (CHECK_BREAKPOINTS) {
404
    /* MM: Check for breakpoint.  This has to be done in fetch cycle,
405
       because of peripheria.
406
       MM1709: if we cannot access the memory entry, we could not set the
407
       breakpoint earlier, so just chech the breakpoint list.  */
408
    if (has_breakpoint (pc_phy) && !break_just_hit) {
409
      break_just_hit = 1;
410
      return 1; /* Breakpoint set. */
411
    }
412
    break_just_hit = 0;
413 431 markom
  }
414 535 markom
  instructions++;
415 378 markom
 
416 538 markom
  pc_phy &= ~0x03;
417
 
418 378 markom
  /* Fetch instruction. */
419 560 markom
  iqueue[0].insn_addr = pc;
420
  iqueue[0].insn = eval_insn (pc_phy, &breakpoint);;
421 557 markom
 
422 378 markom
  /* update_pc will be called after execution */
423 77 lampret
 
424 378 markom
  return 0;
425 2 cvs
}
426
 
427 479 markom
/* This code actually updates the PC value.  */
428 557 markom
static inline void update_pc()
429 142 chris
{
430 479 markom
  pcprev = pc; /* Store value for later */
431
  pc = pcnext;
432
  pcnext = delay_insn ? pcdelay : pcnext + 4;
433 2 cvs
}
434
 
435 557 markom
static inline void analysis()
436 2 cvs
{
437 394 markom
  if (config.cpu.dependstats)
438 123 markom
    /* Instruction waits in completition buffer until retired. */
439 138 markom
    memcpy (&icomplet[0], &iqueue[0], sizeof (struct iqueue_entry));
440
 
441 394 markom
  if (config.sim.history) {
442 263 markom
    int i;
443
 
444 123 markom
    /* History of execution */
445
    for (i = HISTEXEC_LEN - 1; i; i--)
446
      histexec[i] = histexec[i - 1];
447 262 markom
    histexec[0] = icomplet[0].insn_addr;        /* add last insn */
448 123 markom
  }
449 2 cvs
}
450
 
451 294 markom
 
452
/* Execution logger.  */
453
inline void dump_exe_log()
454
{
455 479 markom
  unsigned long i = iqueue[0].insn_addr;
456 294 markom
 
457 479 markom
  if (i == 0xffffffff) return;
458 361 markom
  fprintf(runtime.sim.fexe_log, "\nEXECUTED(): %.8lx:  ", i);
459
  fprintf(runtime.sim.fexe_log, "%.2x%.2x", evalsim_mem8(i), evalsim_mem8(i + 1));
460
  fprintf(runtime.sim.fexe_log, "%.2x%.2x", evalsim_mem8(i + 2), evalsim_mem8(i + 3));
461
  for(i = 0; i < MAX_GPRS; i++) {
462
    if (i % 4 == 0)
463
      fprintf(runtime.sim.fexe_log, "\n");
464
    fprintf(runtime.sim.fexe_log, "GPR%2u: %.8lx  ", i, reg[i]);
465
  }
466
  fprintf(runtime.sim.fexe_log, "\n");
467
  fprintf(runtime.sim.fexe_log, "SR   : %.8lx  ", mfspr(SPR_SR));
468
  fprintf(runtime.sim.fexe_log, "EPCR0: %.8lx  ", mfspr(SPR_EPCR_BASE));
469
  fprintf(runtime.sim.fexe_log, "EEAR0: %.8lx  ", mfspr(SPR_EEAR_BASE));
470
  fprintf(runtime.sim.fexe_log, "ESR0 : %.8lx\n", mfspr(SPR_ESR_BASE));
471 294 markom
}
472
 
473 537 markom
#if 0
474 535 markom
void print_time (int cycles, char *output)
475
{
476
  int i = 0, c_ps = config.sim.clkcycle_ps;
477
  while (c_ps % 1000 == 0 && i < 2) {
478
    c_ps /= 1000;
479
    i++;
480
  }
481
  c_ps *= cycles;
482
  sprintf (output, "%i%cs", cycles, i == 0 ? 'p' : i == 1 ? 'n': 'u');
483
}
484 537 markom
#endif
485 535 markom
 
486 2 cvs
void dumpreg()
487
{
488 269 markom
  int i;
489 535 markom
  char temp[100];
490 269 markom
 
491 306 markom
  dumpmemory(iqueue[0].insn_addr, iqueue[0].insn_addr + 4, 1, 0);
492 537 markom
  generate_time_pretty (temp, cycles);
493 535 markom
  printf(" (executed) [time %s, #%i]\n", temp, instructions);
494 293 markom
  if (config.cpu.superscalar)
495
    printf ("Superscalar CYCLES: %u", supercycles);
496
  if (config.cpu.hazards)
497
    printf ("  HAZARDWAIT: %u\n", hazardwait);
498
  else
499
    if (config.cpu.superscalar)
500
      printf ("\n");
501
 
502 306 markom
  dumpmemory(pc, pc + 4, 1, 0);
503 269 markom
  printf(" (next insn) %s", (delay_insn?"(delay insn)":""));
504
  for(i = 0; i < MAX_GPRS; i++) {
505
    if (i % 4 == 0)
506
      printf("\n");
507 560 markom
    printf("GPR%.2u: %.8lx  ", i, evalsim_reg32(i));
508 269 markom
  }
509
  printf("flag: %u\n", flag);
510 2 cvs
}
511 123 markom
 
512
/* Address calculation changed by CZ on 27/05/01 */
513 560 markom
static inline void decode_execute(struct iqueue_entry *current)
514 123 markom
{
515 560 markom
  int insn_index;
516 123 markom
  next_delay_insn = 0;
517
  breakpoint = 0;
518 560 markom
 
519 550 markom
  if(config.debug.enabled && CheckDebugUnit(DebugInstructionFetch, pc_phy))
520 123 markom
    breakpoint++;
521 138 markom
 
522 560 markom
  IFF (config.cpu.dependstats) current->func_unit = it_unknown;
523
 
524
  current->insn_index = insn_index = insn_decode(current->insn);
525 123 markom
 
526
#ifndef HAS_EXECUTION
527
#error HAS_EXECUTION has to be defined in order to execute programs.
528
#endif
529 133 markom
 
530 560 markom
  cur = current;  /* globals; needed by eval/set_operand */
531
 
532
  if (insn_index < 0)
533 123 markom
    l_invalid();
534 560 markom
  else {
535
    op = &cur->op[0];
536
    eval_operands (cur->insn, insn_index, &breakpoint);
537
    or32_opcodes[insn_index].exec();
538
  }
539
 
540 458 simons
  /* Check for range exception */
541 557 markom
  if (testsprbits (SPR_SR, SPR_SR_OVE) && testsprbits (SPR_SR, SPR_SR_OV))
542 458 simons
    except_handle (EXCEPT_RANGE, 0);
543
 
544 263 markom
  if (config.cpu.dependstats) {
545 560 markom
    iqueue[0].insn_index = insn_index;
546 123 markom
    /* Dynamic, dependency stats. */
547 535 markom
    adddstats(icomplet[0].insn_index, iqueue[0].insn_index, 1, check_depend());
548 262 markom
 
549 123 markom
    /* Dynamic, functional units stats. */
550
    addfstats(icomplet[0].func_unit, iqueue[0].func_unit, 1, check_depend());
551 262 markom
 
552 123 markom
    /* Dynamic, single stats. */
553 535 markom
    addsstats(iqueue[0].insn_index, 1);
554 123 markom
  }
555 560 markom
 
556 263 markom
  if (config.cpu.superscalar) {
557 479 markom
    if ((cur->func_unit == it_branch) || (cur->func_unit == it_jump))
558 123 markom
      storecycles += 0;
559
 
560 479 markom
    if (cur->func_unit == it_store)
561 389 lampret
      storecycles += 1;
562 123 markom
 
563 479 markom
    if (cur->func_unit == it_load)
564 389 lampret
      loadcycles += 1;
565
#if 0        
566 479 markom
    if ((icomplet[0].func_unit == it_load) && check_depend())
567 123 markom
      loadcycles++;
568 389 lampret
#endif
569 123 markom
 
570
    /* Pseudo multiple issue benchmark */
571
    if ((multissue[cur->func_unit] < 1) || (check_depend())
572 560 markom
     || (issued_per_cycle < 1)) {
573 123 markom
      int i;
574
      for (i = 0; i < 20; i++)
575 262 markom
        multissue[i] = 2;
576 123 markom
      issued_per_cycle = 2;
577
      supercycles++;
578
      if (check_depend())
579 262 markom
        hazardwait++;
580 479 markom
      multissue[it_unknown] = 2;
581
      multissue[it_shift] = 2;
582
      multissue[it_compare] = 1;
583
      multissue[it_branch] = 1;
584
      multissue[it_jump] = 1;
585
      multissue[it_extend] = 2;
586
      multissue[it_nop] = 2;
587
      multissue[it_move] = 2;
588
      multissue[it_movimm] = 2;
589
      multissue[it_arith] = 2;
590
      multissue[it_store] = 2;
591
      multissue[it_load] = 2;
592 123 markom
    }
593
    multissue[cur->func_unit]--;
594
    issued_per_cycle--;
595
  }
596
  delay_insn = next_delay_insn;
597
 
598
  if(breakpoint)
599 479 markom
    except_handle(EXCEPT_TRAP, 0);
600 123 markom
}
601
 
602 557 markom
void cpu_reset()
603
{
604
  cycles = 0;
605
  instructions = 0;
606
  supercycles = 0;
607
  loadcycles = 0;
608
  storecycles = 0;
609
  memset(reg, 0, sizeof(reg));
610
  memset(iqueue, 0, sizeof(iqueue));
611
  memset(icomplet, 0, sizeof(icomplet));
612
 
613
  /* Cpu configuration */
614
  mtspr(SPR_UPR, config.cpu.upr);
615
  setsprbits(SPR_VR, SPR_VR_VER, config.cpu.ver);
616
  setsprbits(SPR_VR, SPR_VR_REV, config.cpu.rev);
617
  mtspr(SPR_SR, config.cpu.sr);
618
 
619
  pcnext = 0x0; /* MM1409: All programs should start at reset vector entry!  */
620
  printf ("Starting at 0x%08x\n", pcnext);
621
  pc = pcnext;
622
  pc_phy = pc;
623
  pcnext += 4;
624
  debug(1, "reset ...\n");
625
 
626
  /* MM1409: All programs should set their stack pointer!  */
627
  except_handle(EXCEPT_RESET, 0);
628
}
629
 
630
inline int cpu_clock ()
631
{
632
  if(fetch()) {
633
    printf ("Breakpoint hit.\n");
634
    cont_run = 0; /* memory breakpoint encountered */
635
    return 1;
636
  }
637
  decode_execute(&iqueue[0]);
638
  update_pc();
639
  analysis();
640
  if (config.sim.exe_log) dump_exe_log();
641
  return 0;
642
}
643
 
644 123 markom
/******************************************
645
 *    Instruction specific functions.     *
646
 ******************************************/
647
 
648
void l_add() {
649 221 markom
  signed long temp1;
650 123 markom
  signed char temp4;
651
 
652 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_arith;
653 221 markom
  temp1 = (signed long)eval_operand32(2, &breakpoint)+(signed long)eval_operand32(1, &breakpoint);
654
  set_operand32(0, temp1, &breakpoint);
655 123 markom
 
656
  temp4 = temp1;
657
  if (temp4 == temp1)
658
    mstats.byteadd++;
659
}
660 557 markom
void l_sw() {
661 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_store;
662 221 markom
  set_operand32(0, eval_operand32(1, &breakpoint), &breakpoint);
663 123 markom
}
664
void l_sb() {
665 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_store;
666 221 markom
  set_operand8(0, eval_operand32(1, &breakpoint), &breakpoint);
667 123 markom
}
668
void l_sh() {
669 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_store;
670 221 markom
  set_operand16(0, eval_operand32(1, &breakpoint), &breakpoint);
671 123 markom
}
672
void l_lwz() {
673 437 simons
  unsigned long val;
674 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_load;
675 437 simons
  val = eval_operand32(1, &breakpoint);
676
  /* If eval opreand produced exception don't set anything */
677 558 markom
  if (!pending.valid)
678
    set_operand32(0, val, &breakpoint);
679 123 markom
}
680
void l_lbs() {
681 437 simons
  signed char val;
682 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_load;
683 437 simons
  val = eval_operand8(1, &breakpoint);
684
  /* If eval opreand produced exception don't set anything */
685 558 markom
  if (!pending.valid)
686
    set_operand32(0, val, &breakpoint);
687 123 markom
}
688 221 markom
void l_lbz() {
689 437 simons
  unsigned char val;
690 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_load;
691 437 simons
  val = eval_operand8(1, &breakpoint);
692
  /* If eval opreand produced exception don't set anything */
693 558 markom
  if (!pending.valid)
694
    set_operand32(0, val, &breakpoint);
695 123 markom
}
696 221 markom
void l_lhs() {
697 437 simons
  signed short val;
698 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_load;
699 437 simons
  val = eval_operand16(1, &breakpoint);
700
  /* If eval opreand produced exception don't set anything */
701 558 markom
  if (!pending.valid)
702
    set_operand32(0, val, &breakpoint);
703 123 markom
}
704 221 markom
void l_lhz() {
705 437 simons
  unsigned short val;
706 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_load;
707 437 simons
  val = eval_operand16(1, &breakpoint);
708
  /* If eval opreand produced exception don't set anything */
709 558 markom
  if (!pending.valid)
710
    set_operand32(0, val, &breakpoint);
711 123 markom
}
712
void l_movhi() {
713 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_movimm;
714 221 markom
  set_operand32(0, eval_operand32(1, &breakpoint) << 16, &breakpoint);
715 123 markom
}
716
void l_and() {
717 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_arith;
718 221 markom
  set_operand32(0, eval_operand32(1, &breakpoint) & (unsigned)eval_operand32(2, &breakpoint), &breakpoint);
719 123 markom
}
720
void l_or() {
721 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_arith;
722 221 markom
  set_operand32(0, eval_operand32(1, &breakpoint) | (unsigned)eval_operand32(2, &breakpoint), &breakpoint);
723 123 markom
}
724
void l_xor() {
725 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_arith;
726 221 markom
  set_operand32(0, eval_operand32(1, &breakpoint) ^ (signed)eval_operand32(2, &breakpoint), &breakpoint);
727 123 markom
}
728
void l_sub() {
729 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_arith;
730 221 markom
  set_operand32(0, (signed long)eval_operand32(1, &breakpoint) - (signed long)eval_operand32(2, &breakpoint), &breakpoint);
731 123 markom
}
732 174 markom
/*int mcount = 0;*/
733 123 markom
void l_mul() {
734
  signed long temp3, temp2, temp1;
735
 
736 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_arith;
737 221 markom
  set_operand32(0, (signed long)eval_operand32(1, &breakpoint) * (signed long)eval_operand32(2, &breakpoint), &breakpoint);
738 174 markom
  /*if (!(mcount++ & 1023)) {
739
    printf ("[%i]\n",mcount);
740
    }*/
741 123 markom
}
742
void l_div() {
743
  signed long temp3, temp2, temp1;
744
 
745 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_arith;
746 221 markom
  temp3 = eval_operand32(2, &breakpoint);
747
  temp2 = eval_operand32(1, &breakpoint);
748 123 markom
  if (temp3)
749
    temp1 = temp2 / temp3;
750 458 simons
  else {
751
    except_handle(EXCEPT_ILLEGAL, iqueue[0].insn_addr);
752
    return;
753
  }
754 221 markom
  set_operand32(0, temp1, &breakpoint);
755 123 markom
}
756
void l_divu() {
757
  unsigned long temp3, temp2, temp1;
758
 
759 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_arith;
760 221 markom
  temp3 = eval_operand32(2, &breakpoint);
761
  temp2 = eval_operand32(1, &breakpoint);
762 123 markom
  temp1 = temp2 / temp3;
763
  /* cycles += 16; */
764 221 markom
  set_operand32(0, temp1, &breakpoint);
765 123 markom
}
766
void l_sll() {
767
  int sign = 1;
768 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_shift;
769 221 markom
  if ((signed)eval_operand32(1, &breakpoint) < 0)
770 123 markom
    sign = -1;
771
  /* cycles += 2; */
772 221 markom
  set_operand32(0, eval_operand32(1, &breakpoint) << eval_operand32(2, &breakpoint), &breakpoint);
773 123 markom
}
774
void l_sra() {
775
  unsigned long sign = 0;
776 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_shift;
777 123 markom
 
778 221 markom
  if ((signed)eval_operand32(1, &breakpoint) < 0)
779 123 markom
    sign = -1;
780
  /* cycles += 2; */
781 221 markom
  set_operand32(0, (signed)eval_operand32(1, &breakpoint) >> eval_operand32(2, &breakpoint), &breakpoint);
782 123 markom
}
783
void l_srl() {
784 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_shift;
785 123 markom
  /* cycles += 2; */
786 221 markom
  set_operand32(0, eval_operand32(1, &breakpoint) >> eval_operand32(2, &breakpoint), &breakpoint);
787 123 markom
}
788 535 markom
void l_bf() {
789 541 markom
  if (config.bpb.enabled) {
790 535 markom
    int fwd = (eval_operand32(0, &breakpoint) >= pc) ? 1 : 0;
791 558 markom
    IFF (config.cpu.dependstats) cur->func_unit = it_branch;
792 535 markom
    mstats.bf[flag][fwd]++;
793
    bpb_update(cur->insn_addr, flag);
794
  }
795
  if (flag) {
796
    debug(5, "\nl.bf relative: pc=%x pcnext=%x\n", pc, pcnext);
797
    pcdelay = pc + (signed)eval_operand32(0, &breakpoint) * 4;
798
    btic_update(pcnext);
799
    next_delay_insn = 1;
800
  } else {
801
    btic_update(pc);
802
  }
803
}
804
void l_bnf() {
805 541 markom
  if (config.bpb.enabled) {
806 535 markom
    int fwd = (eval_operand32(0, &breakpoint) >= pc) ? 1 : 0;
807 558 markom
    IFF (config.cpu.dependstats) cur->func_unit = it_branch;
808 535 markom
    mstats.bnf[!flag][fwd]++;
809
    bpb_update(cur->insn_addr, flag == 0);
810
  }
811
  if (flag == 0) {
812
    debug(5, "\nl.bnf relative: pc=%x pcnext=%x\n", pc, pcnext);
813
    pcdelay = pc + (signed)eval_operand32(0, &breakpoint) * 4;
814
    btic_update(pcnext);
815
    next_delay_insn = 1;
816
  } else {
817
    btic_update(pc);
818
  }
819
}
820 123 markom
void l_j() {
821 344 markom
  debug(5, "\nl.j relative: pc=%x pcnext=%x\n", pc, pcnext);
822 221 markom
  pcdelay = pc + (signed)eval_operand32(0, &breakpoint) * 4;
823 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_jump;
824 123 markom
  next_delay_insn = 1;
825
}
826
void l_jal() {
827 344 markom
  debug(5, "\nl.jal relative: pc=%x pcnext=%x\n", pc, pcnext);
828 221 markom
  pcdelay = pc + (signed)eval_operand32(0, &breakpoint) * 4;
829 123 markom
 
830 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_jump;
831 138 markom
  set_reg32(LINK_REGNO, pc + 8);
832 123 markom
  next_delay_insn = 1;
833 264 markom
  if (config.sim.profile) {
834 262 markom
    struct mem_entry *entry;
835 221 markom
    struct label_entry *tmp;
836 261 markom
    if (verify_memoryarea(pcdelay) && (tmp = get_label (pcdelay)))
837 361 markom
      fprintf (runtime.sim.fprof, "+%08X %08X %08X %s\n", cycles, pc + 8, pcdelay, tmp->name);
838 221 markom
    else
839 361 markom
      fprintf (runtime.sim.fprof, "+%08X %08X %08X @%08X\n", cycles, pc + 8, pcdelay, pcdelay);
840 173 markom
  }
841 123 markom
}
842
void l_jalr() {
843 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_jump;
844 221 markom
  pcdelay = eval_operand32(0, &breakpoint);
845 138 markom
  set_reg32(LINK_REGNO, pc + 8);
846 123 markom
  next_delay_insn = 1;
847
}
848
void l_jr() {
849 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_jump;
850 221 markom
  pcdelay = eval_operand32(0, &breakpoint);
851 123 markom
  next_delay_insn = 1;
852 482 markom
  if (config.sim.profile)
853
    fprintf (runtime.sim.fprof, "-%08X %08X\n", cycles, pcdelay);
854 123 markom
}
855
void l_rfe() {
856 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_exception;
857 416 simons
  pcnext = mfspr(SPR_EPCR_BASE);
858
  mtspr(SPR_SR, mfspr(SPR_ESR_BASE));
859
}
860 123 markom
void l_nop() {
861 511 markom
  unsigned long stackaddr;
862
  int k = eval_operand32(0, &breakpoint);
863 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_nop;
864 511 markom
  switch (k) {
865
    case NOP_NOP:
866
      break;
867
    case NOP_EXIT:
868 560 markom
      printf("exit(%d)\n", evalsim_reg32 (3));
869 511 markom
      if (config.debug.gdb_enabled)
870
        set_stall_state (1);
871
      else
872
        cont_run = 0;
873
      break;
874
    case NOP_PRINTF:
875 560 markom
      stackaddr = evalsim_reg32(4);
876
      simprintf(stackaddr, evalsim_reg32(3));
877 511 markom
      debug(5, "simprintf %x\n", stackaddr);
878
      break;
879
    case NOP_REPORT:
880 560 markom
      printf("report(0x%x);\n", evalsim_reg32(3));
881 511 markom
    default:
882
      if (k >= NOP_REPORT_FIRST && k <= NOP_REPORT_LAST)
883 560 markom
      printf("report %i (0x%x);\n", k - NOP_REPORT_FIRST, evalsim_reg(3));
884 511 markom
      break;
885
  }
886 123 markom
}
887
void l_sfeq() {
888 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
889 221 markom
  flag = eval_operand32(0, &breakpoint) == eval_operand32(1, &breakpoint);
890 123 markom
  setsprbits(SPR_SR, SPR_SR_F, flag);
891
}
892 535 markom
void l_sfne() {
893 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
894 535 markom
  flag = eval_operand32(0, &breakpoint) != eval_operand32(1, &breakpoint);
895
  setsprbits(SPR_SR, SPR_SR_F, flag);
896
}
897 123 markom
void l_sfgts() {
898 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
899 221 markom
  flag = (signed)eval_operand32(0, &breakpoint) > (signed)eval_operand32(1, &breakpoint);
900 123 markom
  setsprbits(SPR_SR, SPR_SR_F, flag);
901
}
902
void l_sfges() {
903 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
904 221 markom
  flag = (signed)eval_operand32(0, &breakpoint) >= (signed)eval_operand32(1, &breakpoint);
905 123 markom
  setsprbits(SPR_SR, SPR_SR_F, flag);
906
}
907
void l_sflts() {
908 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
909 221 markom
  flag = (signed)eval_operand32(0, &breakpoint) < (signed)eval_operand32(1, &breakpoint);
910 123 markom
  setsprbits(SPR_SR, SPR_SR_F, flag);
911
}
912
void l_sfles() {
913 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
914 221 markom
  flag = (signed)eval_operand32(0, &breakpoint) <= (signed)eval_operand32(1, &breakpoint);
915 123 markom
  setsprbits(SPR_SR, SPR_SR_F, flag);
916
}
917
void l_sfgtu() {
918 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
919 221 markom
  flag = (unsigned)eval_operand32(0, &breakpoint) > (unsigned)eval_operand32(1, &breakpoint);
920 123 markom
  setsprbits(SPR_SR, SPR_SR_F, flag);
921
}
922
void l_sfgeu() {
923 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
924 221 markom
  flag = (unsigned)eval_operand32(0, &breakpoint) >= (unsigned) eval_operand32(1, &breakpoint);
925 123 markom
  setsprbits(SPR_SR, SPR_SR_F, flag);
926
}
927
void l_sfltu() {
928 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
929 221 markom
  flag = (unsigned)eval_operand32(0, &breakpoint) < (unsigned)eval_operand32(1, &breakpoint);
930 123 markom
  setsprbits(SPR_SR, SPR_SR_F, flag);
931
}
932
void l_sfleu() {
933 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_compare;
934 221 markom
  flag = (unsigned)eval_operand32(0, &breakpoint) <= (unsigned)eval_operand32(1, &breakpoint);
935 123 markom
  setsprbits(SPR_SR, SPR_SR_F, flag);
936
}
937
void l_mtspr() {
938 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_move;
939 123 markom
  if (mfspr(SPR_SR) & SPR_SR_SUPV)
940 221 markom
    mtspr(eval_operand32(0, &breakpoint) + eval_operand32(2, &breakpoint), eval_operand32(1, &breakpoint));
941 123 markom
  else {
942
    printf("WARNING: trying to write SPR while SR[SUPV] is cleared.\n");
943
    cont_run = 0;
944
  }
945
}
946
void l_mfspr() {
947 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_move;
948 123 markom
  if (mfspr(SPR_SR) & SPR_SR_SUPV)
949 221 markom
    set_operand32(0, mfspr(eval_operand32(1, &breakpoint) + eval_operand32(2, &breakpoint)), &breakpoint);
950 123 markom
  else {
951 221 markom
    set_operand32(0, 0, &breakpoint);
952 123 markom
    printf("WARNING: trying to read SPR while SR[SUPV] is cleared.\n");
953
    cont_run = 0;
954
  }
955
}
956
void l_sys() {
957 511 markom
  except_handle(EXCEPT_SYSCALL, 0);
958 123 markom
}
959 142 chris
 
960
void l_trap() {
961 479 markom
  /* TODO: some SR related code here! */
962 458 simons
  except_handle(EXCEPT_TRAP, 0);
963 142 chris
}
964
 
965 123 markom
void l_mac() {
966
  sprword lo, hi;
967
  LONGEST l;
968 498 lampret
  LONGEST x, y;
969 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_mac;
970 123 markom
  lo = mfspr (SPR_MACLO);
971
  hi = mfspr (SPR_MACHI);
972 221 markom
  x = eval_operand32(0, &breakpoint);
973
  y = eval_operand32(1, &breakpoint);
974 498 lampret
  printf ("[%08x,%08x]\t", (unsigned long)(x), (unsigned long)(y));
975 221 markom
  l = (ULONGEST)lo | ((LONGEST)hi << 32);
976
  l += (LONGEST) x * (LONGEST) y;
977 123 markom
 
978
  /* This implementation is very fast - it needs only one cycle for mac.  */
979 221 markom
  lo = ((ULONGEST)l) & 0xFFFFFFFF;
980
  hi = ((LONGEST)l) >> 32;
981 123 markom
  mtspr (SPR_MACLO, lo);
982
  mtspr (SPR_MACHI, hi);
983 498 lampret
  printf ("(%08x,%08x)\n", hi, lo);
984 123 markom
}
985
void l_msb() {
986 221 markom
  sprword lo, hi;
987 123 markom
  LONGEST l;
988 558 markom
  IFF (config.cpu.dependstats) cur->func_unit = it_mac;
989 123 markom
  lo = mfspr (SPR_MACLO);
990
  hi = mfspr (SPR_MACHI);
991 221 markom
  l = (ULONGEST)lo | ((LONGEST)hi << 32);
992
  l -= (LONGEST) eval_operand32(0, &breakpoint) * (LONGEST)eval_operand32(1, &breakpoint);
993 123 markom
 
994
  /* This implementation is very fast - it needs only one cycle for msb.  */
995 221 markom
  lo = ((ULONGEST)l) & 0xFFFFFFFF;
996
  hi = ((LONGEST)l) >> 32;
997 123 markom
  mtspr (SPR_MACLO, lo);
998 221 markom
  mtspr (SPR_MACHI, hi);
999 123 markom
}
1000
void l_macrc() {
1001
  sprword lo, hi;
1002
  LONGEST l;
1003
  /* No need for synchronization here -- all MAC instructions are 1 cycle long.  */
1004
  lo =  mfspr (SPR_MACLO);
1005 221 markom
  hi =  mfspr (SPR_MACHI);
1006
  l = (ULONGEST) lo | ((LONGEST)hi << 32);
1007
  l >>= 28;
1008
  //printf ("<%08x>\n", (unsigned long)l);
1009
  set_operand32(0, (long)l, &breakpoint);
1010 123 markom
  mtspr (SPR_MACLO, 0);
1011 221 markom
  mtspr (SPR_MACHI, 0);
1012 123 markom
}
1013 174 markom
void l_cust1() {
1014
  /*int destr = cur->insn >> 21;
1015
    int src1r = cur->insn >> 15;
1016
    int src2r = cur->insn >> 9;*/
1017
}
1018
void l_cust2() {
1019
}
1020
void l_cust3() {
1021
}
1022
void l_cust4() {
1023
}
1024 123 markom
void l_invalid() {
1025 458 simons
  except_handle(EXCEPT_ILLEGAL, iqueue[0].insn_addr);
1026 123 markom
}

powered by: WebSVN 2.1.0

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