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

Subversion Repositories or1k

[/] [or1k/] [tags/] [rel-0-3-0-rc2/] [or1ksim/] [cpu/] [or32/] [execute.c] - Blame information for rev 1514

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

powered by: WebSVN 2.1.0

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