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

Subversion Repositories or1k

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

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 1346 nogj
   Copyright (C) 2005 György `nog' Jeney, nog@sdf.lonestar.org
4 2 cvs
 
5
This file is part of OpenRISC 1000 Architectural Simulator.
6
 
7
This program is free software; you can redistribute it and/or modify
8
it under the terms of the GNU General Public License as published by
9
the Free Software Foundation; either version 2 of the License, or
10
(at your option) any later version.
11
 
12
This program is distributed in the hope that it will be useful,
13
but WITHOUT ANY WARRANTY; without even the implied warranty of
14
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
GNU General Public License for more details.
16
 
17
You should have received a copy of the GNU General Public License
18
along with this program; if not, write to the Free Software
19
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
20
 
21 706 markom
/* Most of the OR1K simulation is done in here.
22 2 cvs
 
23 706 markom
   When SIMPLE_EXECUTION is defined below a file insnset.c is included!
24
*/
25
 
26 2 cvs
#include <stdlib.h>
27
#include <stdio.h>
28
#include <string.h>
29
#include <ctype.h>
30
 
31 123 markom
#include "config.h"
32 1350 nogj
 
33
#ifdef HAVE_INTTYPES_H
34
#include <inttypes.h>
35
#endif
36
 
37
#include "port.h"
38 2 cvs
#include "arch.h"
39
#include "branch_predict.h"
40
#include "abstract.h"
41 261 markom
#include "labels.h"
42 2 cvs
#include "parse.h"
43 54 lampret
#include "except.h"
44 102 lampret
#include "sim-config.h"
45 123 markom
#include "debug_unit.h"
46 1308 phoenix
#include "opcode/or32.h"
47 1432 nogj
#include "spr_defs.h"
48
#include "execute.h"
49
#include "sprs.h"
50 1308 phoenix
#include "immu.h"
51
#include "dmmu.h"
52
#include "debug.h"
53 1344 nogj
#include "stats.h"
54 2 cvs
 
55 1432 nogj
/* Current cpu state */
56
struct cpu_state cpu_state;
57 2 cvs
 
58
/* Benchmark multi issue execution */
59
int multissue[20];
60 6 lampret
int issued_per_cycle = 4;
61 2 cvs
 
62 378 markom
/* Previous program counter */
63 1350 nogj
oraddr_t pcprev = 0;
64 378 markom
 
65 2 cvs
/* Temporary program counter */
66 1350 nogj
oraddr_t pcnext;
67 2 cvs
 
68
/* CCR */
69
int flag;
70
 
71 626 markom
/* Store buffer analysis - stores are accumulated and commited when IO is idle */
72
static int sbuf_head = 0, sbuf_tail = 0, sbuf_count = 0;
73
static int sbuf_buf[MAX_SBUF_LEN] = {0};
74
static int sbuf_prev_cycles = 0;
75
 
76
/* Num cycles waiting for stores to complete */
77
int sbuf_wait_cyc = 0;
78
 
79
/* Number of total store cycles */
80
int sbuf_total_cyc = 0;
81
 
82 714 markom
/* Whether we are doing statistical analysis */
83
int do_stats = 0;
84
 
85 138 markom
/* Local data needed for execution.  */
86
static int next_delay_insn;
87
static int breakpoint;
88
 
89 1346 nogj
 
90 1352 nogj
/* History of execution */
91
struct hist_exec *hist_exec_tail = NULL;
92
 
93 2 cvs
/* Implementation specific.
94
   Get an actual value of a specific register. */
95
 
96 1350 nogj
uorreg_t evalsim_reg(unsigned int regno)
97 2 cvs
{
98 138 markom
  if (regno < MAX_GPRS) {
99 1432 nogj
    return cpu_state.reg[regno];
100 560 markom
  } else {
101 997 markom
    PRINTF("\nABORT: read out of registers\n");
102 884 markom
    runtime.sim.cont_run = 0;
103 560 markom
    return 0;
104
  }
105
}
106
 
107
/* Implementation specific.
108
   Set a specific register with value. */
109
 
110 1350 nogj
void setsim_reg(unsigned int regno, uorreg_t value)
111 560 markom
{
112
  if (regno == 0)               /* gpr0 is always zero */
113
    value = 0;
114
 
115
  if (regno < MAX_GPRS) {
116 1432 nogj
    cpu_state.reg[regno] = value;
117 560 markom
  } else {
118 997 markom
    PRINTF("\nABORT: write out of registers\n");
119 884 markom
    runtime.sim.cont_run = 0;
120 560 markom
  }
121
}
122
 
123
/* Implementation specific.
124 2 cvs
   Set a specific register with value. */
125
 
126 1350 nogj
inline static void set_reg(int regno, uorreg_t value)
127 2 cvs
{
128 1446 nogj
#if 0
129 138 markom
  if (strcmp(regstr, FRAME_REG) == 0) {
130 1432 nogj
    PRINTF("FP (%s) modified by insn at %x. ", FRAME_REG, cpu_state.pc);
131 997 markom
    PRINTF("Old:%.8lx  New:%.8lx\n", eval_reg(regno), value);
132 138 markom
  }
133
 
134
  if (strcmp(regstr, STACK_REG) == 0) {
135 1432 nogj
    PRINTF("SP (%s) modified by insn at %x. ", STACK_REG, cpu_state.pc);
136 1350 nogj
    PRINTF("Old:%.8lx  New:%.8lx\n", eval_reg(regno), value);
137 138 markom
  }
138 2 cvs
#endif
139 560 markom
 
140 138 markom
  if (regno < MAX_GPRS) {
141 1432 nogj
    cpu_state.reg[regno] = value;
142 713 markom
#if RAW_RANGE_STATS
143 884 markom
    raw_stats.reg[regno] = runtime.sim.cycles;
144 713 markom
#endif /* RAW_RANGE */
145 138 markom
  } else {
146 997 markom
    PRINTF("\nABORT: write out of registers\n");
147 884 markom
    runtime.sim.cont_run = 0;
148 138 markom
  }
149 2 cvs
}
150
 
151 1346 nogj
/* Implementation specific.
152
   Evaluates source operand opd. */
153
 
154 1350 nogj
static uorreg_t eval_operand_val(uint32_t insn, struct insn_op_struct *opd)
155 1346 nogj
{
156
  unsigned long operand = 0;
157
  unsigned long sbit;
158
  unsigned int nbits = 0;
159
 
160
  while(1) {
161
    operand |= ((insn >> (opd->type & OPTYPE_SHR)) & ((1 << opd->data) - 1)) << nbits;
162
    nbits += opd->data;
163
 
164
    if(opd->type & OPTYPE_OP)
165
      break;
166
    opd++;
167
  }
168
 
169
  if(opd->type & OPTYPE_SIG) {
170
    sbit = (opd->type & OPTYPE_SBIT) >> OPTYPE_SBIT_SHR;
171 1350 nogj
    if(operand & (1 << sbit)) operand |= ~REG_C(0) << sbit;
172 1346 nogj
  }
173
 
174
  return operand;
175
}
176
 
177
/* Does source operand depend on computation of dstoperand? Return
178 2 cvs
   non-zero if yes.
179
 
180 262 markom
 Cycle t                 Cycle t+1
181
dst: irrelevant         src: immediate                  always 0
182
dst: reg1 direct        src: reg2 direct                0 if reg1 != reg2
183
dst: reg1 disp          src: reg2 direct                always 0
184
dst: reg1 direct        src: reg2 disp                  0 if reg1 != reg2
185
dst: reg1 disp          src: reg2 disp                  always 1 (store must
186
                                                        finish before load)
187 138 markom
dst: flag               src: flag                       always 1
188 2 cvs
*/
189
 
190 1428 nogj
static int check_depend(prev, next)
191 138 markom
     struct iqueue_entry *prev;
192
     struct iqueue_entry *next;
193 2 cvs
{
194 138 markom
  /* Find destination type. */
195
  unsigned long type = 0;
196 1346 nogj
  int prev_dis, next_dis;
197 1350 nogj
  orreg_t prev_reg_val = 0;
198 1346 nogj
  struct insn_op_struct *opd;
199
 
200 138 markom
  if (or32_opcodes[prev->insn_index].flags & OR32_W_FLAG
201
      && or32_opcodes[next->insn_index].flags & OR32_R_FLAG)
202
    return 1;
203 2 cvs
 
204 1346 nogj
  opd = op_start[prev->insn_index];
205
  prev_dis = 0;
206 560 markom
 
207 1346 nogj
  while (1) {
208
    if (opd->type & OPTYPE_DIS)
209
      prev_dis = 1;
210
 
211
    if (opd->type & OPTYPE_DST) {
212
      type = opd->type;
213
      if (prev_dis)
214
        type |= OPTYPE_DIS;
215
      /* Destination is always a register */
216
      prev_reg_val = eval_operand_val (prev->insn, opd);
217
      break;
218
    }
219
    if (opd->type & OPTYPE_LAST)
220
      return 0; /* Doesn't have a destination operand */
221
    if (opd->type & OPTYPE_OP)
222
      prev_dis = 0;
223
    opd++;
224
  }
225
 
226 138 markom
  /* We search all source operands - if we find confict => return 1 */
227 1346 nogj
  opd = op_start[next->insn_index];
228
  next_dis = 0;
229
 
230
  while (1) {
231
    if (opd->type & OPTYPE_DIS)
232
      next_dis = 1;
233
    /* This instruction sequence also depends on order of execution:
234
     * l.lw r1, k(r1)
235
     * l.sw k(r1), r4
236
     * Here r1 is a destination in l.sw */
237
    /* FIXME: This situation is not handeld here when r1 == r2:
238
     * l.sw k(r1), r4
239
     * l.lw r3, k(r2)
240
     */
241
    if (!(opd->type & OPTYPE_DST) || (next_dis && (opd->type & OPTYPE_DST))) {
242
      if (opd->type & OPTYPE_REG)
243
        if (eval_operand_val (next->insn, opd) == prev_reg_val)
244 262 markom
          return 1;
245 1346 nogj
    }
246
    if (opd->type & OPTYPE_LAST)
247
      break;
248
    opd++;
249
  }
250
 
251 138 markom
  return 0;
252
}
253 2 cvs
 
254 717 markom
/* Sets a new SPR_SR_OV value, based on next register value */
255 2 cvs
 
256 615 markom
#if SET_OV_FLAG
257 1343 nogj
#define set_ov_flag(value) if((value) & 0x80000000) setsprbits (SPR_SR, SPR_SR_OV, 1); else setsprbits (SPR_SR, SPR_SR_OV, 0)
258 717 markom
#else
259 1343 nogj
#define set_ov_flag(value)
260 615 markom
#endif
261 605 markom
 
262 123 markom
/* Modified by CZ 26/05/01 for new mode execution */
263
/* Fetch returns nonzero if instruction should NOT be executed.  */
264 557 markom
static inline int fetch()
265
{
266 1386 nogj
  static int break_just_hit = 0;
267 574 markom
 
268 557 markom
  if (CHECK_BREAKPOINTS) {
269
    /* MM: Check for breakpoint.  This has to be done in fetch cycle,
270
       because of peripheria.
271
       MM1709: if we cannot access the memory entry, we could not set the
272 1386 nogj
       breakpoint earlier, so just check the breakpoint list.  */
273 1432 nogj
    if (has_breakpoint (peek_into_itlb (cpu_state.pc)) && !break_just_hit) {
274 557 markom
      break_just_hit = 1;
275
      return 1; /* Breakpoint set. */
276
    }
277
    break_just_hit = 0;
278 431 markom
  }
279 1244 hpanther
 
280 1386 nogj
  breakpoint = 0;
281 378 markom
  /* Fetch instruction. */
282 1386 nogj
  if (!except_pending)
283
    runtime.cpu.instructions++;
284 1432 nogj
  cpu_state.iqueue.insn_addr = cpu_state.pc;
285
  cpu_state.iqueue.insn = eval_insn (cpu_state.pc, &breakpoint);
286 1386 nogj
 
287 378 markom
  /* update_pc will be called after execution */
288 77 lampret
 
289 378 markom
  return 0;
290 2 cvs
}
291
 
292 479 markom
/* This code actually updates the PC value.  */
293 626 markom
static inline void update_pc ()
294 142 chris
{
295 1432 nogj
  cpu_state.delay_insn = next_delay_insn;
296
  pcprev = cpu_state.pc; /* Store value for later */
297
  cpu_state.pc = pcnext;
298
  pcnext = cpu_state.delay_insn ? cpu_state.pc_delay : pcnext + 4;
299 2 cvs
}
300
 
301 717 markom
#if SIMPLE_EXECUTION
302
static inline
303
#endif
304
void analysis (struct iqueue_entry *current)
305 2 cvs
{
306 713 markom
  if (config.cpu.dependstats) {
307
    /* Dynamic, dependency stats. */
308 1432 nogj
    adddstats(cpu_state.icomplet.insn_index, current->insn_index, 1,
309
              check_depend(&cpu_state.icomplet, current));
310 713 markom
 
311
    /* Dynamic, functional units stats. */
312 1432 nogj
    addfstats(or32_opcodes[cpu_state.icomplet.insn_index].func_unit,
313 1428 nogj
              or32_opcodes[current->insn_index].func_unit, 1,
314 1432 nogj
              check_depend(&cpu_state.icomplet, current));
315 713 markom
 
316
    /* Dynamic, single stats. */
317
    addsstats(current->insn_index, 1);
318
  }
319
 
320
  if (config.cpu.superscalar) {
321 1344 nogj
    if ((or32_opcodes[current->insn_index].func_unit == it_branch) ||
322
        (or32_opcodes[current->insn_index].func_unit == it_jump))
323 884 markom
      runtime.sim.storecycles += 0;
324 713 markom
 
325 1344 nogj
    if (or32_opcodes[current->insn_index].func_unit == it_store)
326 884 markom
      runtime.sim.storecycles += 1;
327 713 markom
 
328 1344 nogj
    if (or32_opcodes[current->insn_index].func_unit == it_load)
329 884 markom
      runtime.sim.loadcycles += 1;
330 713 markom
#if 0        
331 1432 nogj
    if ((cpu_state.icomplet.func_unit == it_load) &&
332
        check_depend(&cpu_state.icomplet, current))
333 884 markom
      runtime.sim.loadcycles++;
334 713 markom
#endif
335
 
336
    /* Pseudo multiple issue benchmark */
337 1344 nogj
    if ((multissue[or32_opcodes[current->insn_index].func_unit] < 1) ||
338 1432 nogj
        (check_depend(&cpu_state.icomplet, current)) || (issued_per_cycle < 1)) {
339 713 markom
      int i;
340
      for (i = 0; i < 20; i++)
341
        multissue[i] = 2;
342
      issued_per_cycle = 2;
343 884 markom
      runtime.cpu.supercycles++;
344 1432 nogj
      if (check_depend(&cpu_state.icomplet, current))
345 884 markom
        runtime.cpu.hazardwait++;
346 713 markom
      multissue[it_unknown] = 2;
347
      multissue[it_shift] = 2;
348
      multissue[it_compare] = 1;
349
      multissue[it_branch] = 1;
350
      multissue[it_jump] = 1;
351
      multissue[it_extend] = 2;
352
      multissue[it_nop] = 2;
353
      multissue[it_move] = 2;
354
      multissue[it_movimm] = 2;
355
      multissue[it_arith] = 2;
356
      multissue[it_store] = 2;
357
      multissue[it_load] = 2;
358
    }
359 1344 nogj
    multissue[or32_opcodes[current->insn_index].func_unit]--;
360 713 markom
    issued_per_cycle--;
361
  }
362
 
363 394 markom
  if (config.cpu.dependstats)
364 123 markom
    /* Instruction waits in completition buffer until retired. */
365 1432 nogj
    memcpy (&cpu_state.icomplet, current, sizeof (struct iqueue_entry));
366 138 markom
 
367 394 markom
  if (config.sim.history) {
368 123 markom
    /* History of execution */
369 1352 nogj
    hist_exec_tail = hist_exec_tail->next;
370 1432 nogj
    hist_exec_tail->addr = cpu_state.icomplet.insn_addr;
371 123 markom
  }
372 714 markom
 
373
  if (config.sim.exe_log) dump_exe_log();
374 2 cvs
}
375
 
376 626 markom
/* Store buffer analysis - stores are accumulated and commited when IO is idle */
377 1308 phoenix
static inline void sbuf_store (int cyc) {
378 884 markom
  int delta = runtime.sim.cycles - sbuf_prev_cycles;
379 626 markom
  sbuf_total_cyc += cyc;
380 884 markom
  sbuf_prev_cycles = runtime.sim.cycles;
381 626 markom
 
382 997 markom
  //PRINTF (">STORE %i,%i,%i,%i,%i\n", delta, sbuf_count, sbuf_tail, sbuf_head, sbuf_buf[sbuf_tail], sbuf_buf[sbuf_head]);
383
  //PRINTF ("|%i,%i\n", sbuf_total_cyc, sbuf_wait_cyc);
384 626 markom
  /* Take stores from buffer, that occured meanwhile */
385 630 markom
  while (sbuf_count && delta >= sbuf_buf[sbuf_tail]) {
386 626 markom
    delta -= sbuf_buf[sbuf_tail];
387
    sbuf_tail = (sbuf_tail + 1) % MAX_SBUF_LEN;
388
    sbuf_count--;
389
  }
390
  if (sbuf_count)
391
    sbuf_buf[sbuf_tail] -= delta;
392 630 markom
 
393 626 markom
  /* Store buffer is full, take one out */
394
  if (sbuf_count >= config.cpu.sbuf_len) {
395
    sbuf_wait_cyc += sbuf_buf[sbuf_tail];
396 884 markom
    runtime.sim.mem_cycles += sbuf_buf[sbuf_tail];
397 630 markom
    sbuf_prev_cycles += sbuf_buf[sbuf_tail];
398 626 markom
    sbuf_tail = (sbuf_tail + 1) % MAX_SBUF_LEN;
399
    sbuf_count--;
400
  }
401
  /* Put newest store in the buffer */
402
  sbuf_buf[sbuf_head] = cyc;
403
  sbuf_head = (sbuf_head + 1) % MAX_SBUF_LEN;
404
  sbuf_count++;
405 997 markom
  //PRINTF ("|STORE %i,%i,%i,%i,%i\n", delta, sbuf_count, sbuf_tail, sbuf_head, sbuf_buf[sbuf_tail], sbuf_buf[sbuf_head]);
406 626 markom
}
407 294 markom
 
408 626 markom
/* Store buffer analysis - previous stores should commit, before any load */
409 1308 phoenix
static inline void sbuf_load () {
410 884 markom
  int delta = runtime.sim.cycles - sbuf_prev_cycles;
411
  sbuf_prev_cycles = runtime.sim.cycles;
412 630 markom
 
413 997 markom
  //PRINTF (">LOAD  %i,%i,%i,%i,%i\n", delta, sbuf_count, sbuf_tail, sbuf_head, sbuf_buf[sbuf_tail], sbuf_buf[sbuf_head]);
414
  //PRINTF ("|%i,%i\n", sbuf_total_cyc, sbuf_wait_cyc);
415 629 markom
  /* Take stores from buffer, that occured meanwhile */
416 630 markom
  while (sbuf_count && delta >= sbuf_buf[sbuf_tail]) {
417 629 markom
    delta -= sbuf_buf[sbuf_tail];
418
    sbuf_tail = (sbuf_tail + 1) % MAX_SBUF_LEN;
419
    sbuf_count--;
420
  }
421
  if (sbuf_count)
422
    sbuf_buf[sbuf_tail] -= delta;
423
 
424 626 markom
  /* Wait for all stores to complete */
425
  while (sbuf_count > 0) {
426
    sbuf_wait_cyc += sbuf_buf[sbuf_tail];
427 884 markom
    runtime.sim.mem_cycles += sbuf_buf[sbuf_tail];
428 630 markom
    sbuf_prev_cycles += sbuf_buf[sbuf_tail];
429 626 markom
    sbuf_tail = (sbuf_tail + 1) % MAX_SBUF_LEN;
430
    sbuf_count--;
431
  }
432 997 markom
  //PRINTF ("|LOAD  %i,%i,%i,%i,%i\n", delta, sbuf_count, sbuf_tail, sbuf_head, sbuf_buf[sbuf_tail], sbuf_buf[sbuf_head]);
433 626 markom
}
434
 
435 712 markom
/* Outputs dissasembled instruction */
436 713 markom
void dump_exe_log ()
437 294 markom
{
438 1432 nogj
  oraddr_t insn_addr = cpu_state.iqueue.insn_addr;
439 1350 nogj
  unsigned int i, j;
440
  uorreg_t operand;
441 294 markom
 
442 1346 nogj
  if (insn_addr == 0xffffffff) return;
443
  if ((config.sim.exe_log_start <= runtime.cpu.instructions) &&
444
      ((config.sim.exe_log_end <= 0) ||
445
       (runtime.cpu.instructions <= config.sim.exe_log_end))) {
446
    if (config.sim.exe_log_marker &&
447
        !(runtime.cpu.instructions % config.sim.exe_log_marker)) {
448 1343 nogj
      fprintf (runtime.sim.fexe_log, "--------------------- %8lli instruction ---------------------\n", runtime.cpu.instructions);
449 693 markom
    }
450 672 markom
    switch (config.sim.exe_log_type) {
451
    case EXE_LOG_HARDWARE:
452 1350 nogj
      fprintf (runtime.sim.fexe_log, "\nEXECUTED(%11llu): %"PRIxADDR":  ",
453 1346 nogj
               runtime.cpu.instructions, insn_addr);
454
      fprintf (runtime.sim.fexe_log, "%.2x%.2x", evalsim_mem8_void(insn_addr),
455
               evalsim_mem8_void(insn_addr + 1));
456
      fprintf (runtime.sim.fexe_log, "%.2x%.2x",
457
               evalsim_mem8_void(insn_addr + 2),
458
               evalsim_mem8_void(insn_addr + 3));
459 672 markom
      for(i = 0; i < MAX_GPRS; i++) {
460
        if (i % 4 == 0)
461
          fprintf(runtime.sim.fexe_log, "\n");
462 1432 nogj
        fprintf (runtime.sim.fexe_log, "GPR%2u: %"PRIxREG"  ", i,
463
                 cpu_state.reg[i]);
464 672 markom
      }
465
      fprintf (runtime.sim.fexe_log, "\n");
466
      fprintf (runtime.sim.fexe_log, "SR   : %.8lx  ", mfspr(SPR_SR));
467
      fprintf (runtime.sim.fexe_log, "EPCR0: %.8lx  ", mfspr(SPR_EPCR_BASE));
468
      fprintf (runtime.sim.fexe_log, "EEAR0: %.8lx  ", mfspr(SPR_EEAR_BASE));
469
      fprintf (runtime.sim.fexe_log, "ESR0 : %.8lx\n", mfspr(SPR_ESR_BASE));
470
      break;
471 675 markom
    case EXE_LOG_SIMPLE:
472 672 markom
    case EXE_LOG_SOFTWARE:
473
      {
474 713 markom
        extern char *disassembled;
475 1432 nogj
        disassemble_index (cpu_state.iqueue.insn, cpu_state.iqueue.insn_index);
476 714 markom
        {
477 672 markom
          struct label_entry *entry;
478 1346 nogj
          entry = get_label(insn_addr);
479 714 markom
          if (entry)
480
            fprintf (runtime.sim.fexe_log, "%s:\n", entry->name);
481 672 markom
        }
482 714 markom
 
483 675 markom
        if (config.sim.exe_log_type == EXE_LOG_SOFTWARE) {
484 1432 nogj
          struct insn_op_struct *opd = op_start[cpu_state.iqueue.insn_index];
485 1204 phoenix
 
486 1346 nogj
          j = 0;
487
          while (1) {
488 1432 nogj
            operand = eval_operand_val (cpu_state.iqueue.insn, opd);
489 1346 nogj
            while (!(opd->type & OPTYPE_OP))
490
              opd++;
491
            if (opd->type & OPTYPE_DIS) {
492 1350 nogj
              fprintf (runtime.sim.fexe_log, "EA =%"PRIxADDR" PA =%"PRIxADDR" ",
493 1432 nogj
                       cpu_state.insn_ea, peek_into_dtlb(cpu_state.insn_ea,0,0));
494 1346 nogj
              opd++; /* Skip of register operand */
495
              j++;
496 1350 nogj
            } else if ((opd->type & OPTYPE_REG) && operand) {
497
              fprintf (runtime.sim.fexe_log, "r%-2i=%"PRIxREG" ",
498
                       (int)operand, evalsim_reg (operand));
499 677 markom
            } else
500 1204 phoenix
              fprintf (runtime.sim.fexe_log, "             ");
501 1346 nogj
            j++;
502
            if(opd->type & OPTYPE_LAST)
503
              break;
504
            opd++;
505
          }
506 1432 nogj
          if(or32_opcodes[cpu_state.iqueue.insn_index].flags & OR32_R_FLAG) {
507 1430 nogj
            fprintf (runtime.sim.fexe_log, "SR =%08x",
508
                     cpu_state.sprs[SPR_SR]);
509
            j++;
510
          }
511 1346 nogj
          while(j < 3) {
512 678 markom
            fprintf (runtime.sim.fexe_log, "             ");
513 1346 nogj
            j++;
514
          }
515 675 markom
        }
516 1350 nogj
        fprintf (runtime.sim.fexe_log, "%"PRIxADDR" ", insn_addr);
517 713 markom
        fprintf (runtime.sim.fexe_log, "%s\n", disassembled);
518 672 markom
      }
519
    }
520
  }
521 294 markom
}
522
 
523 713 markom
/* Dump registers - 'r' or 't' command */
524 2 cvs
void dumpreg()
525
{
526 269 markom
  int i;
527 1350 nogj
  oraddr_t physical_pc;
528 269 markom
 
529 1432 nogj
  if ((physical_pc = peek_into_itlb(cpu_state.iqueue.insn_addr))) {
530 1178 phoenix
    /*
531 1432 nogj
     * PRINTF("\t\t\tEA: %08x <--> PA: %08x\n", cpu_state.iqueue.insn_addr, physical_pc);
532 1178 phoenix
     */
533
    dumpmemory(physical_pc, physical_pc + 4, 1, 0);
534
  }
535
  else {
536
    PRINTF("INTERNAL SIMULATOR ERROR:\n");
537
    PRINTF("no translation for currently executed instruction\n");
538
  }
539
 
540 1319 phoenix
  // generate_time_pretty (temp, runtime.sim.cycles * config.sim.clkcycle_ps);
541 1350 nogj
  PRINTF(" (executed) [cycle %lld, #%lld]\n", runtime.sim.cycles,
542
         runtime.cpu.instructions);
543 293 markom
  if (config.cpu.superscalar)
544 997 markom
    PRINTF ("Superscalar CYCLES: %u", runtime.cpu.supercycles);
545 293 markom
  if (config.cpu.hazards)
546 997 markom
    PRINTF ("  HAZARDWAIT: %u\n", runtime.cpu.hazardwait);
547 293 markom
  else
548
    if (config.cpu.superscalar)
549 997 markom
      PRINTF ("\n");
550 293 markom
 
551 1432 nogj
  if ((physical_pc = peek_into_itlb(cpu_state.pc))) {
552 1178 phoenix
    /*
553 1432 nogj
     * PRINTF("\t\t\tEA: %08x <--> PA: %08x\n", cpu_state.pc, physical_pc);
554 1178 phoenix
     */
555
    dumpmemory(physical_pc, physical_pc + 4, 1, 0);
556
  }
557 1174 phoenix
  else
558 1432 nogj
    PRINTF("%"PRIxADDR": : xxxxxxxx  ITLB miss follows", cpu_state.pc);
559 1174 phoenix
 
560 1432 nogj
  PRINTF(" (next insn) %s", (cpu_state.delay_insn?"(delay insn)":""));
561 269 markom
  for(i = 0; i < MAX_GPRS; i++) {
562
    if (i % 4 == 0)
563 997 markom
      PRINTF("\n");
564 1350 nogj
    PRINTF("GPR%.2u: %"PRIxREG"  ", i, evalsim_reg(i));
565 269 markom
  }
566 997 markom
  PRINTF("flag: %u\n", flag);
567 2 cvs
}
568 123 markom
 
569 713 markom
/* Generated/built in decoding/executing function */
570 712 markom
static inline void decode_execute (struct iqueue_entry *current);
571 706 markom
 
572
/* Wrapper around real decode_execute function -- some statistics here only */
573
static inline void decode_execute_wrapper (struct iqueue_entry *current)
574 123 markom
{
575 712 markom
  breakpoint = 0;
576 138 markom
 
577 123 markom
#ifndef HAS_EXECUTION
578
#error HAS_EXECUTION has to be defined in order to execute programs.
579
#endif
580 706 markom
 
581 712 markom
  decode_execute (current);
582 706 markom
 
583 717 markom
#if SET_OV_FLAG
584 458 simons
  /* Check for range exception */
585 557 markom
  if (testsprbits (SPR_SR, SPR_SR_OVE) && testsprbits (SPR_SR, SPR_SR_OV))
586 611 simons
    except_handle (EXCEPT_RANGE, mfspr(SPR_EEAR_BASE));
587 717 markom
#endif
588 458 simons
 
589 123 markom
  if(breakpoint)
590 611 simons
    except_handle(EXCEPT_TRAP, mfspr(SPR_EEAR_BASE));
591 123 markom
}
592
 
593 706 markom
/* Reset the CPU */
594 557 markom
void cpu_reset()
595
{
596 606 markom
  int i;
597 1352 nogj
  struct hist_exec *hist_exec_head = NULL;
598
  struct hist_exec *hist_exec_new;
599
 
600 884 markom
  runtime.sim.cycles = 0;
601
  runtime.sim.loadcycles = 0;
602
  runtime.sim.storecycles = 0;
603
  runtime.cpu.instructions = 0;
604
  runtime.cpu.supercycles = 0;
605
  runtime.cpu.hazardwait = 0;
606 606 markom
  for (i = 0; i < MAX_GPRS; i++)
607 1350 nogj
    set_reg (i, 0);
608 1432 nogj
  memset(&cpu_state.iqueue, 0, sizeof(cpu_state.iqueue));
609
  memset(&cpu_state.icomplet, 0, sizeof(cpu_state.icomplet));
610 626 markom
 
611
  sbuf_head = 0;
612
  sbuf_tail = 0;
613
  sbuf_count = 0;
614
  sbuf_prev_cycles = 0;
615 557 markom
 
616 1352 nogj
  /* Initialise execution history circular buffer */
617
  for (i = 0; i < HISTEXEC_LEN; i++) {
618
    hist_exec_new = malloc(sizeof(struct hist_exec));
619
    if(!hist_exec_new) {
620
      fprintf(stderr, "Out-of-memory\n");
621
      exit(1);
622
    }
623
    if(!hist_exec_head)
624
      hist_exec_head = hist_exec_new;
625
    else
626
      hist_exec_tail->next = hist_exec_new;
627
 
628
    hist_exec_new->prev = hist_exec_tail;
629
    hist_exec_tail = hist_exec_new;
630
  }
631
  /* Make hist_exec_tail->next point to hist_exec_head */
632
  hist_exec_tail->next = hist_exec_head;
633
  hist_exec_head->prev = hist_exec_tail;
634
 
635 557 markom
  /* Cpu configuration */
636 1442 nogj
  cpu_state.sprs[SPR_UPR] = config.cpu.upr;
637 557 markom
  setsprbits(SPR_VR, SPR_VR_VER, config.cpu.ver);
638
  setsprbits(SPR_VR, SPR_VR_REV, config.cpu.rev);
639 1442 nogj
  cpu_state.sprs[SPR_SR] = config.cpu.sr;
640 557 markom
 
641
  pcnext = 0x0; /* MM1409: All programs should start at reset vector entry!  */
642 1350 nogj
  if (config.sim.verbose) PRINTF ("Starting at 0x%"PRIxADDR"\n", pcnext);
643 1432 nogj
  cpu_state.pc = pcnext;
644 557 markom
  pcnext += 4;
645
  debug(1, "reset ...\n");
646
 
647
  /* MM1409: All programs should set their stack pointer!  */
648
  except_handle(EXCEPT_RESET, 0);
649 1386 nogj
  update_pc();
650
  except_pending = 0;
651 557 markom
}
652
 
653 713 markom
/* Simulates one CPU clock cycle */
654 557 markom
inline int cpu_clock ()
655
{
656 1386 nogj
  except_pending = 0;
657
  next_delay_insn = 0;
658 557 markom
  if(fetch()) {
659 997 markom
    PRINTF ("Breakpoint hit.\n");
660 884 markom
    runtime.sim.cont_run = 0; /* memory breakpoint encountered */
661 557 markom
    return 1;
662
  }
663 1386 nogj
 
664
  if(except_pending) {
665
    update_pc();
666
    except_pending = 0;
667
    return 0;
668
  }
669
 
670
  if(breakpoint) {
671
    except_handle(EXCEPT_TRAP, mfspr(SPR_EEAR_BASE));
672
    update_pc();
673
    except_pending = 0;
674
    return 0;
675
  }
676
 
677 1432 nogj
  decode_execute_wrapper (&cpu_state.iqueue);
678 557 markom
  update_pc();
679
  return 0;
680
}
681
 
682 713 markom
/* If decoding cannot be found, call this function */
683 1342 nogj
#if SIMPLE_EXECUTION
684
void l_invalid (struct iqueue_entry *current) {
685
#else
686 706 markom
void l_invalid () {
687 1342 nogj
#endif
688 1432 nogj
  except_handle(EXCEPT_ILLEGAL, cpu_state.iqueue.insn_addr);
689 123 markom
}
690 641 ivang
 
691 717 markom
#if !SIMPLE_EXECUTION
692
 
693
/* Include decode_execute function */
694
#include "execgen.c"
695
 
696
#else /* SIMPLE_EXECUTION */
697
 
698 720 markom
 
699 1342 nogj
#define INSTRUCTION(name) void name (struct iqueue_entry *current)
700 641 ivang
 
701 717 markom
/* Implementation specific.
702 1350 nogj
   Get an actual value of a specific register. */
703
 
704
static uorreg_t eval_reg(unsigned int regno)
705
{
706
  if (regno < MAX_GPRS) {
707
#if RAW_RANGE_STATS
708
      int delta = (runtime.sim.cycles - raw_stats.reg[regno]);
709
      if ((unsigned long)delta < (unsigned long)MAX_RAW_RANGE)
710
        raw_stats.range[delta]++;
711
#endif /* RAW_RANGE */
712 1432 nogj
    return cpu_state.reg[regno];
713 1350 nogj
  } else {
714
    PRINTF("\nABORT: read out of registers\n");
715
    runtime.sim.cont_run = 0;
716
    return 0;
717
  }
718
}
719
 
720
/* Implementation specific.
721 1346 nogj
   Evaluates source operand op_no. */
722 641 ivang
 
723 1350 nogj
static uorreg_t eval_operand (int op_no, unsigned long insn_index, uint32_t insn)
724 717 markom
{
725
  struct insn_op_struct *opd = op_start[insn_index];
726 1350 nogj
  uorreg_t ret;
727 717 markom
 
728 1346 nogj
  while (op_no) {
729
    if(opd->type & OPTYPE_LAST) {
730
      fprintf (stderr, "Instruction requested more operands than it has\n");
731
      exit (1);
732 717 markom
    }
733 1346 nogj
    if((opd->type & OPTYPE_OP) && !(opd->type & OPTYPE_DIS))
734
      op_no--;
735
    opd++;
736
  }
737 717 markom
 
738 1346 nogj
  if (opd->type & OPTYPE_DIS) {
739
    ret = eval_operand_val (insn, opd);
740
    while (!(opd->type & OPTYPE_OP))
741
      opd++;
742
    opd++;
743 1350 nogj
    ret += eval_reg (eval_operand_val (insn, opd));
744 1432 nogj
    cpu_state.insn_ea = ret;
745 1346 nogj
    return ret;
746
  }
747
  if (opd->type & OPTYPE_REG)
748 1350 nogj
    return eval_reg (eval_operand_val (insn, opd));
749 717 markom
 
750 1346 nogj
  return eval_operand_val (insn, opd);
751 717 markom
}
752
 
753
/* Implementation specific.
754 1342 nogj
   Set destination operand (reister direct) with value. */
755 717 markom
 
756 1350 nogj
inline static void set_operand(int op_no, orreg_t value,
757
                               unsigned long insn_index, uint32_t insn)
758 717 markom
{
759 1346 nogj
  struct insn_op_struct *opd = op_start[insn_index];
760
 
761
  while (op_no) {
762
    if(opd->type & OPTYPE_LAST) {
763
      fprintf (stderr, "Instruction requested more operands than it has\n");
764
      exit (1);
765
    }
766
    if((opd->type & OPTYPE_OP) && !(opd->type & OPTYPE_DIS))
767
      op_no--;
768
    opd++;
769
  }
770
 
771
  if (!(opd->type & OPTYPE_REG)) {
772 1342 nogj
    fprintf (stderr, "Trying to set a non-register operand\n");
773 717 markom
    exit (1);
774
  }
775 1350 nogj
  set_reg (eval_operand_val (insn, opd), value);
776 717 markom
}
777
 
778 713 markom
/* Simple and rather slow decoding function based on built automata. */
779 712 markom
static inline void decode_execute (struct iqueue_entry *current)
780 706 markom
{
781
  int insn_index;
782
 
783
  current->insn_index = insn_index = insn_decode(current->insn);
784 641 ivang
 
785 706 markom
  if (insn_index < 0)
786 1342 nogj
    l_invalid(current);
787 123 markom
  else {
788 1342 nogj
    or32_opcodes[insn_index].exec(current);
789 123 markom
  }
790 1432 nogj
 
791
  if (do_stats) analysis(&cpu_state.iqueue);
792 123 markom
}
793
 
794 1346 nogj
#define SET_PARAM0(val) set_operand(0, val, current->insn_index, current->insn)
795 1342 nogj
 
796 1346 nogj
#define PARAM0 eval_operand(0, current->insn_index, current->insn)
797
#define PARAM1 eval_operand(1, current->insn_index, current->insn)
798
#define PARAM2 eval_operand(2, current->insn_index, current->insn)
799 1342 nogj
 
800 720 markom
#include "insnset.c"
801
 
802 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.