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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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