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 557

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