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 717

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 706 markom
/* Most of the OR1K simulation is done in here.
21 2 cvs
 
22 706 markom
   When SIMPLE_EXECUTION is defined below a file insnset.c is included!
23
*/
24
 
25 2 cvs
#include <stdlib.h>
26
#include <stdio.h>
27
#include <string.h>
28
#include <ctype.h>
29
 
30 123 markom
#include "config.h"
31 2 cvs
#include "arch.h"
32 133 markom
#include "opcode/or32.h"
33 2 cvs
#include "branch_predict.h"
34
#include "abstract.h"
35 261 markom
#include "labels.h"
36 2 cvs
#include "parse.h"
37
#include "execute.h"
38
#include "stats.h"
39 54 lampret
#include "except.h"
40
#include "sprs.h"
41 102 lampret
#include "sim-config.h"
42 123 markom
#include "debug_unit.h"
43 2 cvs
 
44
/* General purpose registers. */
45
machword reg[MAX_GPRS];
46
 
47
/* Instruction queue */
48
struct iqueue_entry iqueue[20];
49
 
50 83 lampret
/* Is current insn in execution a delay insn? */
51
int delay_insn;
52
 
53 2 cvs
/* Benchmark multi issue execution */
54
int multissue[20];
55
int supercycles;
56 6 lampret
int issued_per_cycle = 4;
57
int hazardwait = 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 626 markom
/* Store buffer analysis - stores are accumulated and commited when IO is idle */
102
static int sbuf_head = 0, sbuf_tail = 0, sbuf_count = 0;
103
static int sbuf_buf[MAX_SBUF_LEN] = {0};
104
static int sbuf_prev_cycles = 0;
105
 
106
/* Num cycles waiting for stores to complete */
107
int sbuf_wait_cyc = 0;
108
 
109
/* Number of total store cycles */
110
int sbuf_total_cyc = 0;
111
 
112 714 markom
/* Whether we are doing statistical analysis */
113
int do_stats = 0;
114
 
115 138 markom
/* Local data needed for execution.  */
116
static int next_delay_insn;
117
static int breakpoint;
118
static unsigned long *op;
119
static int num_op;
120
 
121 2 cvs
/* Implementation specific.
122
   Get an actual value of a specific register. */
123
 
124 560 markom
unsigned long evalsim_reg32(int regno)
125 2 cvs
{
126 138 markom
  if (regno < MAX_GPRS) {
127 560 markom
    return reg[regno];
128
  } else {
129
    printf("\nABORT: read out of registers\n");
130
    cont_run = 0;
131
    return 0;
132
  }
133
}
134
 
135
/* Implementation specific.
136
   Set a specific register with value. */
137
 
138
void setsim_reg32(int regno, unsigned long value)
139
{
140
  if (regno == 0)               /* gpr0 is always zero */
141
    value = 0;
142
 
143
  if (regno < MAX_GPRS) {
144
    reg[regno] = value;
145
  } else {
146
    printf("\nABORT: write out of registers\n");
147
    cont_run = 0;
148
  }
149
}
150
 
151
/* Implementation specific.
152
   Get an actual value of a specific register. */
153
 
154
inline static unsigned long eval_reg32(int regno)
155
{
156
  if (regno < MAX_GPRS) {
157 713 markom
#if RAW_RANGE_STATS
158 535 markom
      int delta = (cycles - raw_stats.reg[regno]);
159 713 markom
      if ((unsigned long)delta < (unsigned long)MAX_RAW_RANGE)
160 535 markom
        raw_stats.range[delta]++;
161 713 markom
#endif /* RAW_RANGE */
162 138 markom
    return reg[regno];
163
  } else {
164 393 markom
    printf("\nABORT: read out of registers\n");
165 138 markom
    cont_run = 0;
166
    return 0;
167
  }
168 2 cvs
}
169
 
170
/* Implementation specific.
171
   Set a specific register with value. */
172
 
173 560 markom
inline static void set_reg32(int regno, unsigned long value)
174 2 cvs
{
175 262 markom
#if 0   
176 138 markom
  if (strcmp(regstr, FRAME_REG) == 0) {
177
    printf("FP (%s) modified by insn at %x. ", FRAME_REG, pc);
178
    printf("Old:%.8lx  New:%.8lx\n", eval_reg(regno), value);
179
  }
180
 
181
  if (strcmp(regstr, STACK_REG) == 0) {
182
    printf("SP (%s) modified by insn at %x. ", STACK_REG, pc);
183
    printf("Old:%.8lx  New:%.8lx\n", eval_reg(regmo), value);
184
  }
185 2 cvs
#endif
186 560 markom
 
187 138 markom
  if (regno < MAX_GPRS) {
188
    reg[regno] = value;
189 713 markom
#if RAW_RANGE_STATS
190
    raw_stats.reg[regno] = cycles;
191
#endif /* RAW_RANGE */
192 138 markom
  } else {
193 393 markom
    printf("\nABORT: write out of registers\n");
194 138 markom
    cont_run = 0;
195
  }
196 2 cvs
}
197
 
198
/* Does srcoperand depend on computation of dstoperand? Return
199
   non-zero if yes.
200
 
201 262 markom
 Cycle t                 Cycle t+1
202
dst: irrelevant         src: immediate                  always 0
203
dst: reg1 direct        src: reg2 direct                0 if reg1 != reg2
204
dst: reg1 disp          src: reg2 direct                always 0
205
dst: reg1 direct        src: reg2 disp                  0 if reg1 != reg2
206
dst: reg1 disp          src: reg2 disp                  always 1 (store must
207
                                                        finish before load)
208 138 markom
dst: flag               src: flag                       always 1
209 2 cvs
*/
210
 
211 138 markom
int depend_operands(prev, next)
212
     struct iqueue_entry *prev;
213
     struct iqueue_entry *next;
214 2 cvs
{
215 138 markom
  /* Find destination type. */
216
  unsigned long type = 0;
217
  int i = 0;
218
  if (or32_opcodes[prev->insn_index].flags & OR32_W_FLAG
219
      && or32_opcodes[next->insn_index].flags & OR32_R_FLAG)
220
    return 1;
221 2 cvs
 
222 138 markom
  while (!(prev->op[i + MAX_OPERANDS] & OPTYPE_LAST))
223
    if (prev->op[i + MAX_OPERANDS] & OPTYPE_DST)
224
      {
225 262 markom
        type = prev->op[i + MAX_OPERANDS];
226
        break;
227 138 markom
      }
228
    else
229
      i++;
230 560 markom
 
231 138 markom
  /* We search all source operands - if we find confict => return 1 */
232
  i = 0;
233
  while (!(next->op[i + MAX_OPERANDS] & OPTYPE_LAST))
234
    if (!(next->op[i + MAX_OPERANDS] & OPTYPE_DST))
235
      {
236 262 markom
        if (next->op[i + MAX_OPERANDS] & OPTYPE_DIS)
237
          if (type & OPTYPE_DIS)
238
            return 1;
239
          else if (next->op[i] == prev->op[i]
240
                   && (next->op[i + MAX_OPERANDS] & OPTYPE_REG))
241
            return 1;
242
        if (next->op[i] == prev->op[i]
243
            && (next->op[i + MAX_OPERANDS] & OPTYPE_REG)
244
            && (type & OPTYPE_REG))
245
          return 1;
246
        i++;
247 138 markom
      }
248
    else
249
      i++;
250
  return 0;
251
}
252 2 cvs
 
253 717 markom
/* Sets a new SPR_SR_OV value, based on next register value */
254 2 cvs
 
255 615 markom
#if SET_OV_FLAG
256 717 markom
#define set_ov_flag(value) (((value) & 0x80000000 ? setsprbits (SPR_SR, SPR_SR_OV, 1) : setsprbits (SPR_SR, SPR_SR_OV, 0)), value)
257
#else
258
#define set_ov_flag(value) (value)
259 615 markom
#endif
260 605 markom
 
261 123 markom
/* Modified by CZ 26/05/01 for new mode execution */
262
/* Fetch returns nonzero if instruction should NOT be executed.  */
263 557 markom
static inline int fetch()
264
{
265 221 markom
  struct mem_entry *entry;
266 123 markom
 
267 557 markom
  /* Update the pc for pending exceptions, or get physical pc */
268 574 markom
  if (!pending.valid)
269 631 simons
    pc_phy = immu_translate(pc, 0);
270 574 markom
 
271 557 markom
  if(pending.valid)
272
    except_handle_backend(pending.type, pending.address, pending.saved);
273 464 simons
 
274 557 markom
  if (CHECK_BREAKPOINTS) {
275
    /* MM: Check for breakpoint.  This has to be done in fetch cycle,
276
       because of peripheria.
277
       MM1709: if we cannot access the memory entry, we could not set the
278
       breakpoint earlier, so just chech the breakpoint list.  */
279
    if (has_breakpoint (pc_phy) && !break_just_hit) {
280
      break_just_hit = 1;
281
      return 1; /* Breakpoint set. */
282
    }
283
    break_just_hit = 0;
284 431 markom
  }
285 535 markom
  instructions++;
286 378 markom
 
287 538 markom
  pc_phy &= ~0x03;
288
 
289 378 markom
  /* Fetch instruction. */
290 560 markom
  iqueue[0].insn_addr = pc;
291 717 markom
  iqueue[0].insn = eval_insn (pc_phy, &breakpoint);
292 557 markom
 
293 378 markom
  /* update_pc will be called after execution */
294 77 lampret
 
295 378 markom
  return 0;
296 2 cvs
}
297
 
298 479 markom
/* This code actually updates the PC value.  */
299 626 markom
static inline void update_pc ()
300 142 chris
{
301 713 markom
  delay_insn = next_delay_insn;
302 479 markom
  pcprev = pc; /* Store value for later */
303
  pc = pcnext;
304
  pcnext = delay_insn ? pcdelay : pcnext + 4;
305 2 cvs
}
306
 
307 717 markom
#if SIMPLE_EXECUTION
308
static inline
309
#endif
310
void analysis (struct iqueue_entry *current)
311 2 cvs
{
312 713 markom
  if (config.cpu.dependstats) {
313
    /* Dynamic, dependency stats. */
314
    adddstats(icomplet[0].insn_index, current->insn_index, 1, check_depend());
315
 
316
    /* Dynamic, functional units stats. */
317
    addfstats(icomplet[0].func_unit, current->func_unit, 1, check_depend());
318
 
319
    /* Dynamic, single stats. */
320
    addsstats(current->insn_index, 1);
321
  }
322
 
323
  if (config.cpu.superscalar) {
324
    if ((current->func_unit == it_branch) || (current->func_unit == it_jump))
325
      storecycles += 0;
326
 
327
    if (current->func_unit == it_store)
328
      storecycles += 1;
329
 
330
    if (current->func_unit == it_load)
331
      loadcycles += 1;
332
#if 0        
333
    if ((icomplet[0].func_unit == it_load) && check_depend())
334
      loadcycles++;
335
#endif
336
 
337
    /* Pseudo multiple issue benchmark */
338
    if ((multissue[current->func_unit] < 1) || (check_depend())
339
     || (issued_per_cycle < 1)) {
340
      int i;
341
      for (i = 0; i < 20; i++)
342
        multissue[i] = 2;
343
      issued_per_cycle = 2;
344
      supercycles++;
345
      if (check_depend())
346
        hazardwait++;
347
      multissue[it_unknown] = 2;
348
      multissue[it_shift] = 2;
349
      multissue[it_compare] = 1;
350
      multissue[it_branch] = 1;
351
      multissue[it_jump] = 1;
352
      multissue[it_extend] = 2;
353
      multissue[it_nop] = 2;
354
      multissue[it_move] = 2;
355
      multissue[it_movimm] = 2;
356
      multissue[it_arith] = 2;
357
      multissue[it_store] = 2;
358
      multissue[it_load] = 2;
359
    }
360
    multissue[current->func_unit]--;
361
    issued_per_cycle--;
362
  }
363
 
364 394 markom
  if (config.cpu.dependstats)
365 123 markom
    /* Instruction waits in completition buffer until retired. */
366 713 markom
    memcpy (&icomplet[0], current, sizeof (struct iqueue_entry));
367 138 markom
 
368 394 markom
  if (config.sim.history) {
369 263 markom
    int i;
370
 
371 123 markom
    /* History of execution */
372
    for (i = HISTEXEC_LEN - 1; i; i--)
373
      histexec[i] = histexec[i - 1];
374 262 markom
    histexec[0] = icomplet[0].insn_addr;        /* add last insn */
375 123 markom
  }
376 714 markom
 
377
  if (config.sim.exe_log) dump_exe_log();
378 2 cvs
}
379
 
380 626 markom
/* Store buffer analysis - stores are accumulated and commited when IO is idle */
381 630 markom
static inline sbuf_store (int cyc) {
382 626 markom
  int delta = cycles - sbuf_prev_cycles;
383
  sbuf_total_cyc += cyc;
384 630 markom
  sbuf_prev_cycles = cycles;
385 626 markom
 
386 630 markom
  //printf (">STORE %i,%i,%i,%i,%i\n", delta, sbuf_count, sbuf_tail, sbuf_head, sbuf_buf[sbuf_tail], sbuf_buf[sbuf_head]);
387
  //printf ("|%i,%i\n", sbuf_total_cyc, sbuf_wait_cyc);
388 626 markom
  /* Take stores from buffer, that occured meanwhile */
389 630 markom
  while (sbuf_count && delta >= sbuf_buf[sbuf_tail]) {
390 626 markom
    delta -= sbuf_buf[sbuf_tail];
391
    sbuf_tail = (sbuf_tail + 1) % MAX_SBUF_LEN;
392
    sbuf_count--;
393
  }
394
  if (sbuf_count)
395
    sbuf_buf[sbuf_tail] -= delta;
396 630 markom
 
397 626 markom
  /* Store buffer is full, take one out */
398
  if (sbuf_count >= config.cpu.sbuf_len) {
399
    sbuf_wait_cyc += sbuf_buf[sbuf_tail];
400
    mem_cycles += sbuf_buf[sbuf_tail];
401 630 markom
    sbuf_prev_cycles += sbuf_buf[sbuf_tail];
402 626 markom
    sbuf_tail = (sbuf_tail + 1) % MAX_SBUF_LEN;
403
    sbuf_count--;
404
  }
405
  /* Put newest store in the buffer */
406
  sbuf_buf[sbuf_head] = cyc;
407
  sbuf_head = (sbuf_head + 1) % MAX_SBUF_LEN;
408
  sbuf_count++;
409 630 markom
  //printf ("|STORE %i,%i,%i,%i,%i\n", delta, sbuf_count, sbuf_tail, sbuf_head, sbuf_buf[sbuf_tail], sbuf_buf[sbuf_head]);
410 626 markom
}
411 294 markom
 
412 626 markom
/* Store buffer analysis - previous stores should commit, before any load */
413
static inline sbuf_load () {
414 629 markom
  int delta = cycles - sbuf_prev_cycles;
415 630 markom
  sbuf_prev_cycles = cycles;
416
 
417
  //printf (">LOAD  %i,%i,%i,%i,%i\n", delta, sbuf_count, sbuf_tail, sbuf_head, sbuf_buf[sbuf_tail], sbuf_buf[sbuf_head]);
418
  //printf ("|%i,%i\n", sbuf_total_cyc, sbuf_wait_cyc);
419 629 markom
  /* Take stores from buffer, that occured meanwhile */
420 630 markom
  while (sbuf_count && delta >= sbuf_buf[sbuf_tail]) {
421 629 markom
    delta -= sbuf_buf[sbuf_tail];
422
    sbuf_tail = (sbuf_tail + 1) % MAX_SBUF_LEN;
423
    sbuf_count--;
424
  }
425
  if (sbuf_count)
426
    sbuf_buf[sbuf_tail] -= delta;
427
 
428 626 markom
  /* Wait for all stores to complete */
429
  while (sbuf_count > 0) {
430
    sbuf_wait_cyc += sbuf_buf[sbuf_tail];
431
    mem_cycles += sbuf_buf[sbuf_tail];
432 630 markom
    sbuf_prev_cycles += sbuf_buf[sbuf_tail];
433 626 markom
    sbuf_tail = (sbuf_tail + 1) % MAX_SBUF_LEN;
434
    sbuf_count--;
435
  }
436 630 markom
  //printf ("|LOAD  %i,%i,%i,%i,%i\n", delta, sbuf_count, sbuf_tail, sbuf_head, sbuf_buf[sbuf_tail], sbuf_buf[sbuf_head]);
437 626 markom
}
438
 
439 712 markom
/* Outputs dissasembled instruction */
440 713 markom
void dump_exe_log ()
441 294 markom
{
442 479 markom
  unsigned long i = iqueue[0].insn_addr;
443 294 markom
 
444 479 markom
  if (i == 0xffffffff) return;
445 672 markom
  if (config.sim.exe_log_start <= instructions && (config.sim.exe_log_end <= 0 || instructions <= config.sim.exe_log_end)) {
446 693 markom
    if (config.sim.exe_log_marker && instructions % config.sim.exe_log_marker == 0) {
447
      fprintf (runtime.sim.fexe_log, "--------------------- %8i instruction ---------------------\n", instructions);
448
    }
449 672 markom
    switch (config.sim.exe_log_type) {
450
    case EXE_LOG_HARDWARE:
451
      fprintf (runtime.sim.fexe_log, "\nEXECUTED(): %.8lx:  ", i);
452
      fprintf (runtime.sim.fexe_log, "%.2x%.2x", evalsim_mem8(i), evalsim_mem8(i + 1));
453
      fprintf (runtime.sim.fexe_log, "%.2x%.2x", evalsim_mem8(i + 2), evalsim_mem8(i + 3));
454
      for(i = 0; i < MAX_GPRS; i++) {
455
        if (i % 4 == 0)
456
          fprintf(runtime.sim.fexe_log, "\n");
457
        fprintf (runtime.sim.fexe_log, "GPR%2u: %.8lx  ", i, reg[i]);
458
      }
459
      fprintf (runtime.sim.fexe_log, "\n");
460
      fprintf (runtime.sim.fexe_log, "SR   : %.8lx  ", mfspr(SPR_SR));
461
      fprintf (runtime.sim.fexe_log, "EPCR0: %.8lx  ", mfspr(SPR_EPCR_BASE));
462
      fprintf (runtime.sim.fexe_log, "EEAR0: %.8lx  ", mfspr(SPR_EEAR_BASE));
463
      fprintf (runtime.sim.fexe_log, "ESR0 : %.8lx\n", mfspr(SPR_ESR_BASE));
464
      break;
465 675 markom
    case EXE_LOG_SIMPLE:
466 672 markom
    case EXE_LOG_SOFTWARE:
467
      {
468 713 markom
        extern char *disassembled;
469
        disassemble_index (iqueue[0].insn, iqueue[0].insn_index);
470 714 markom
        {
471 672 markom
          struct label_entry *entry;
472
          entry = get_label(i);
473 714 markom
          if (entry)
474
            fprintf (runtime.sim.fexe_log, "%s:\n", entry->name);
475 672 markom
        }
476 714 markom
 
477 675 markom
        if (config.sim.exe_log_type == EXE_LOG_SOFTWARE) {
478 678 markom
          int i;
479
          for (i = 0; i < num_op; i++)
480 677 markom
            if (op[i + MAX_OPERANDS] & OPTYPE_DIS) {
481
              fprintf (runtime.sim.fexe_log, "EA =%08x ", op[i]);
482
            } else if ((op[i + MAX_OPERANDS] & OPTYPE_REG) && op[i]) {
483
              fprintf (runtime.sim.fexe_log, "r%-2i=%08x ", op[i], evalsim_reg32 (op[i]));
484
            } else
485 675 markom
            fprintf (runtime.sim.fexe_log, "             ");
486 678 markom
          for (; i < 3; i++)
487
            fprintf (runtime.sim.fexe_log, "             ");
488 675 markom
        }
489 677 markom
        fprintf (runtime.sim.fexe_log, "%.8lx ", i);
490 713 markom
        fprintf (runtime.sim.fexe_log, "%s\n", disassembled);
491 672 markom
      }
492
    }
493
  }
494 294 markom
}
495
 
496 713 markom
/* Dump registers - 'r' or 't' command */
497 2 cvs
void dumpreg()
498
{
499 269 markom
  int i;
500 535 markom
  char temp[100];
501 269 markom
 
502 306 markom
  dumpmemory(iqueue[0].insn_addr, iqueue[0].insn_addr + 4, 1, 0);
503 537 markom
  generate_time_pretty (temp, cycles);
504 535 markom
  printf(" (executed) [time %s, #%i]\n", temp, instructions);
505 293 markom
  if (config.cpu.superscalar)
506
    printf ("Superscalar CYCLES: %u", supercycles);
507
  if (config.cpu.hazards)
508
    printf ("  HAZARDWAIT: %u\n", hazardwait);
509
  else
510
    if (config.cpu.superscalar)
511
      printf ("\n");
512
 
513 306 markom
  dumpmemory(pc, pc + 4, 1, 0);
514 269 markom
  printf(" (next insn) %s", (delay_insn?"(delay insn)":""));
515
  for(i = 0; i < MAX_GPRS; i++) {
516
    if (i % 4 == 0)
517
      printf("\n");
518 560 markom
    printf("GPR%.2u: %.8lx  ", i, evalsim_reg32(i));
519 269 markom
  }
520
  printf("flag: %u\n", flag);
521 2 cvs
}
522 123 markom
 
523 713 markom
/* Generated/built in decoding/executing function */
524 712 markom
static inline void decode_execute (struct iqueue_entry *current);
525 706 markom
 
526
/* Wrapper around real decode_execute function -- some statistics here only */
527
static inline void decode_execute_wrapper (struct iqueue_entry *current)
528 123 markom
{
529 712 markom
  breakpoint = 0;
530 123 markom
  next_delay_insn = 0;
531 138 markom
 
532 123 markom
#ifndef HAS_EXECUTION
533
#error HAS_EXECUTION has to be defined in order to execute programs.
534
#endif
535 706 markom
 
536
  if(config.debug.enabled && CheckDebugUnit(DebugInstructionFetch, pc_phy))
537 717 markom
    breakpoint = 1;
538 706 markom
 
539 712 markom
  decode_execute (current);
540 706 markom
 
541 717 markom
#if SET_OV_FLAG
542 458 simons
  /* Check for range exception */
543 557 markom
  if (testsprbits (SPR_SR, SPR_SR_OVE) && testsprbits (SPR_SR, SPR_SR_OV))
544 611 simons
    except_handle (EXCEPT_RANGE, mfspr(SPR_EEAR_BASE));
545 717 markom
#endif
546 458 simons
 
547 123 markom
  if(breakpoint)
548 611 simons
    except_handle(EXCEPT_TRAP, mfspr(SPR_EEAR_BASE));
549 123 markom
}
550
 
551 706 markom
/* Reset the CPU */
552 557 markom
void cpu_reset()
553
{
554 606 markom
  int i;
555 557 markom
  cycles = 0;
556
  instructions = 0;
557
  supercycles = 0;
558
  loadcycles = 0;
559
  storecycles = 0;
560 606 markom
  for (i = 0; i < MAX_GPRS; i++)
561
    set_reg32 (i, 0);
562 557 markom
  memset(iqueue, 0, sizeof(iqueue));
563
  memset(icomplet, 0, sizeof(icomplet));
564 626 markom
 
565
  sbuf_head = 0;
566
  sbuf_tail = 0;
567
  sbuf_count = 0;
568
  sbuf_prev_cycles = 0;
569 557 markom
 
570
  /* Cpu configuration */
571
  mtspr(SPR_UPR, config.cpu.upr);
572
  setsprbits(SPR_VR, SPR_VR_VER, config.cpu.ver);
573
  setsprbits(SPR_VR, SPR_VR_REV, config.cpu.rev);
574
  mtspr(SPR_SR, config.cpu.sr);
575
 
576
  pcnext = 0x0; /* MM1409: All programs should start at reset vector entry!  */
577
  printf ("Starting at 0x%08x\n", pcnext);
578
  pc = pcnext;
579
  pc_phy = pc;
580
  pcnext += 4;
581
  debug(1, "reset ...\n");
582
 
583
  /* MM1409: All programs should set their stack pointer!  */
584
  except_handle(EXCEPT_RESET, 0);
585
}
586
 
587 713 markom
/* Simulates one CPU clock cycle */
588 557 markom
inline int cpu_clock ()
589
{
590
  if(fetch()) {
591
    printf ("Breakpoint hit.\n");
592
    cont_run = 0; /* memory breakpoint encountered */
593
    return 1;
594
  }
595 706 markom
  decode_execute_wrapper (&iqueue[0]);
596 557 markom
  update_pc();
597
  return 0;
598
}
599
 
600 713 markom
/* If decoding cannot be found, call this function */
601 706 markom
void l_invalid () {
602 713 markom
  /* It would be hard to handle this case for statistics; we skip it
603
     since it should not occur anyway:
604
  IFF (config.cpu.dependstats) current->func_unit = it_unknown; */
605 706 markom
  except_handle(EXCEPT_ILLEGAL, iqueue[0].insn_addr);
606 123 markom
}
607 641 ivang
 
608 717 markom
#if !SIMPLE_EXECUTION
609
 
610
/* Include decode_execute function */
611
#include "execgen.c"
612
 
613
#else /* SIMPLE_EXECUTION */
614
 
615 706 markom
#define INSTRUCTION(name) void name ()
616
#include "insnset.c"
617 641 ivang
 
618 717 markom
/* Implementation specific.
619
   Parses and returns operands. */
620 641 ivang
 
621 717 markom
static void
622
eval_operands (unsigned long insn, int insn_index, int* breakpoint)
623
{
624
  struct insn_op_struct *opd = op_start[insn_index];
625
  unsigned long data = 0;
626
  int dis = 0;
627
  int no = 0;
628
 
629
  while (1)
630
    {
631
      unsigned long tmp = 0, nbits = 0;
632
      while (1)
633
        {
634
          tmp |= ((insn  >> (opd->type & OPTYPE_SHR)) & ((1 << opd->data) - 1)) << nbits;
635
          nbits += opd->data;
636
          if (opd->type & OPTYPE_OP)
637
            break;
638
          opd++;
639
        }
640
 
641
      /* Do we have to sign extend? */
642
      if (opd->type & OPTYPE_SIG)
643
        {
644
          int sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
645
          if (tmp & (1 << sbit))
646
            tmp |= 0xFFFFFFFF << sbit;
647
        }
648
      if (opd->type & OPTYPE_DIS) {
649
        /* We have to read register later.  */
650
        data += tmp;
651
        dis = 1;
652
      } else
653
        {
654
          if (dis && (opd->type & OPTYPE_REG))
655
            op[no] = data + eval_reg32 (tmp);
656
          else
657
            op[no] = tmp;
658
          op[no + MAX_OPERANDS] = opd->type | (dis ? OPTYPE_DIS : 0);
659
          no++;
660
          data = 0;
661
          dis = 0;
662
        }
663
      if(opd->type & OPTYPE_LAST) {
664
        num_op = no;
665
        return;
666
      }
667
      opd++;
668
    }
669
  num_op = no;
670
}
671
 
672
/* Implementation specific.
673
   Evaluates source operand op_no. */
674
 
675
inline static unsigned long eval_operand32 (int op_no, int *breakpoint)
676
{
677
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS)
678
    /* memory accesses are not cached */
679
    return eval_mem32 (op[op_no], breakpoint);
680
  else if (op[op_no + MAX_OPERANDS] & OPTYPE_REG) {
681
    return eval_reg32 (op[op_no]);
682
  } else {
683
    return op[op_no];
684
  }
685
}
686
 
687
/* Implementation specific.
688
   Evaluates source operand op_no. */
689
 
690
static unsigned long eval_operand16 (int op_no, int *breakpoint)
691
{
692
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS) {
693
    return eval_mem16 (op[op_no], breakpoint);
694
  }
695
  else {
696
    fprintf (stderr, "Invalid operand type.\n");
697
    exit (1);
698
  }
699
}
700
 
701
/* Implementation specific.
702
   Evaluates source operand op_no. */
703
 
704
static unsigned long eval_operand8 (int op_no, int *breakpoint)
705
{
706
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS)
707
    return eval_mem8 (op[op_no], breakpoint);
708
  else {
709
    fprintf (stderr, "Invalid operand type.\n");
710
    exit (1);
711
  }
712
}
713
 
714
/* Implementation specific.
715
   Set destination operand (register direct, register indirect
716
   (with displacement) with value. */
717
 
718
inline static void set_operand32(int op_no, unsigned long value, int* breakpoint)
719
{
720
  /* Mark this as destination operand.  */
721
  IFF (config.cpu.dependstats) op[op_no + MAX_OPERANDS] |= OPTYPE_DST;
722
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS) {
723
    set_mem32(op[op_no], value, breakpoint);
724
  } else if (op[op_no + MAX_OPERANDS] & OPTYPE_REG) {
725
    set_reg32(op[op_no], value);
726
  } else {
727
    fprintf (stderr, "Invalid operand type.\n");
728
    exit (1);
729
  }
730
}
731
 
732
/* Implementation specific.
733
   Set destination operand (register direct, register indirect
734
   (with displacement) with value. */
735
 
736
void set_operand16(int op_no, unsigned long value, int* breakpoint)
737
{
738
  /* Mark this as destination operand.  */
739
  op[op_no + MAX_OPERANDS] |= OPTYPE_DST;
740
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS) {
741
    set_mem16(op[op_no], value, breakpoint);
742
  }
743
  else
744
    {
745
      fprintf (stderr, "Invalid operand type.\n");
746
      exit (1);
747
    }
748
}
749
 
750
/* Implementation specific.
751
   Set destination operand (register direct, register indirect
752
   (with displacement) with value. */
753
 
754
void set_operand8(int op_no, unsigned long value, int* breakpoint)
755
{
756
  /* Mark this as destination operand.  */
757
  op[op_no + MAX_OPERANDS] |= OPTYPE_DST;
758
  if (op[op_no + MAX_OPERANDS] & OPTYPE_DIS)
759
    set_mem8(op[op_no], value, breakpoint);
760
  else
761
    {
762
      fprintf (stderr, "Invalid operand type.\n");
763
      exit (1);
764
    }
765
}
766
 
767 713 markom
/* Simple and rather slow decoding function based on built automata. */
768 712 markom
static inline void decode_execute (struct iqueue_entry *current)
769 706 markom
{
770
  int insn_index;
771
 
772
  current->insn_index = insn_index = insn_decode(current->insn);
773 641 ivang
 
774 706 markom
  if (insn_index < 0)
775
    l_invalid();
776 123 markom
  else {
777 713 markom
    op = &current->op[0];
778
    eval_operands (current->insn, insn_index, &breakpoint);
779 706 markom
    or32_opcodes[insn_index].exec();
780 123 markom
  }
781 717 markom
  if (do_stats) analysis(&iqueue[0]);
782 123 markom
}
783
 
784 717 markom
#endif /* !SIMPLE_EXECUTION */

powered by: WebSVN 2.1.0

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