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

Subversion Repositories or1k

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

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

powered by: WebSVN 2.1.0

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