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

Subversion Repositories or1k

[/] [or1k/] [trunk/] [or1ksim/] [cpu/] [or32/] [execute.c] - Blame information for rev 1482

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

powered by: WebSVN 2.1.0

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