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 558

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

powered by: WebSVN 2.1.0

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