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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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