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

Subversion Repositories or1k

[/] [or1k/] [branches/] [stable_0_1_x/] [or1ksim/] [cpu/] [or32/] [execute.c] - Blame information for rev 1768

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