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

Subversion Repositories or1k

[/] [or1k/] [tags/] [nog_patch_34/] [or1ksim/] [cpu/] [or1k/] [except.c] - Blame information for rev 167

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 33 lampret
/* except.c -- Simulation of OR1K exceptions
2
   Copyright (C) 1999 Damjan Lampret, lampret@opencores.org
3
 
4
This file is part of OpenRISC 1000 Architectural Simulator.
5
 
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
 
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
 
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
19
 
20
#include <stdlib.h>
21
#include <stdio.h>
22
#include <string.h>
23
 
24
#include "abstract.h"
25
#include "except.h"
26
#include "sprs.h"
27
 
28 139 chris
static void except_handle_backend(int,unsigned long,unsigned long);
29
 
30 33 lampret
extern int cont_run;
31
extern struct iqueue_entry iqueue[20];
32
extern unsigned long pc;
33 82 lampret
extern unsigned long pcnext;
34 123 markom
extern unsigned long pc_phy;
35 51 lampret
extern struct iqueue_entry iqueue[];
36 33 lampret
 
37 82 lampret
extern int delay_insn;
38 123 markom
int cycle_delay = 0;  /* Added by CZ 27/05/01 */
39 82 lampret
 
40 139 chris
static struct {
41
  int valid;
42
  int type;
43
  unsigned long address;
44
  unsigned long saved;
45
} pending;
46
 
47
void ClearPendingException()
48
{
49
  if(pending.valid && pending.type != EXCEPT_RESET)
50
    {
51
      pending.valid = 0;
52
      pending.type = 0;
53
      pending.address = 0;
54
      pending.saved = 0;
55
    }
56
}
57
 
58
/* The delayed_pc and delayed_pcnext are fields which hold
59
   the original value of the PC across breakpoint exceptions
60
   in the case of a development interface. Due to an implementation
61
   issues, DIR injected instructions can modify these values
62
   when in fact they should not. So we save and restore them
63
   later on. */
64
static unsigned long delayed_pc = 0;
65
static unsigned long delayed_pcnext = 0;
66
static int delayed_pc_valid = 0;
67
 
68
void PrepareExceptionPC(unsigned long t_pc,unsigned long t_pcnext)
69
{
70
  /* If a real exception occurred which has stalled
71
     the CPU, we are expecting to halt before the end
72
     of the instruction. Otherwise, if it is a single
73
     step that has caused the halt, we are expected to
74
     complete the entire instruction and stop after
75
     it is finished. */
76
 
77
  if(pending.valid)
78
    {
79
      delayed_pc = t_pc;
80
      delayed_pcnext = t_pcnext;
81
      delayed_pc_valid = 1;
82
    }
83
  else
84
    _execute_update_pc(t_pc,t_pcnext);
85
}
86
 
87
void PrepareException()
88
{
89
  if(delayed_pc_valid)
90
    {
91
      pc = delayed_pc;
92
      pcnext = delayed_pcnext;
93
      pc_phy = simulate_ic_mmu_fetch(pc);
94
      if ((pc_phy < MEMORY_START) || (pc_phy > MEMORY_START + MEMORY_LEN))
95
        except_handle(EXCEPT_BUSERR, pc);
96
      delayed_pc_valid = delayed_pc = delayed_pcnext = 0;
97
    }
98
 
99
  if(pending.valid)
100
    except_handle_backend(pending.type,pending.address,pending.saved);
101
}
102
 
103 33 lampret
/* Handle OR1K exceptions. */
104
void except_handle(int except, unsigned long ea)
105
{
106 139 chris
  pending.valid = 1;
107
  pending.type = except;
108
  pending.address = ea;
109
  pending.saved = pc;
110
 
111
  if(DebugCheckException(except))
112
    {
113
      pending.valid = 0;
114
      pending.type = 0;
115
      pending.address = 0;
116
      pending.saved = 0;
117
    }
118 152 chris
  else
119
    {
120
      printf("Exception 0x%x (%s): ", except, EXCEPT_NAME(except));
121
      printf("Iqueue[0].insn_addr: 0x%x  Eff ADDR: 0x%x\n",  iqueue[0].insn_addr, ea);
122
      printf("  pc: 0x%x  pcnext: 0x%x\n",  pc, pcnext);
123
    }
124 139 chris
 
125
  cycle_delay = 0;  /* An exception stalls the CPU 0 clock cycles */
126
}
127
 
128
static void except_handle_backend(int except, unsigned long ea, unsigned long pc_saved)
129
{
130
      pending.valid = 0;
131
      pending.type = 0;
132
      pending.address = 0;
133
      pending.saved = 0;
134
 
135 33 lampret
#if ONLY_VIRTUAL_MACHINE
136 51 lampret
        printf("WARNING: No exception processing while ONLY_VIRTUAL_MACHINE is defined.\n");
137
        cont_run = 0;
138 33 lampret
#else
139 51 lampret
 
140 82 lampret
        if (delay_insn) {
141
                printf(" INFO: Exception during execution of delay slot insn.\n");
142
                pc -= 4;
143
        }
144
#if 0
145
        if ((pcnext != (pc + 4)) && (except != EXCEPT_ITLBMISS)) {      /* Always execute delay slot insn */
146
                printf("XXXXXXXXXXXXXX\n");
147 77 lampret
                fetch();                                                /* before starting with exception */
148
                decode(&iqueue[0]);                                      /* (itlbmiss is special case) */
149 51 lampret
                execute();
150
        }
151 82 lampret
#endif
152 64 lampret
 
153 33 lampret
        if (!(mfspr(SPR_SR) & SPR_SR_EXR)) {
154 51 lampret
                printf("ABORT: Exception occured while exception detection was disabled.\n");
155 33 lampret
                cont_run = 0;
156
                return;
157
        }
158 123 markom
 
159 64 lampret
        pc_saved = pc & ~0x3;
160 51 lampret
        mtspr(SPR_EPCR_BASE, pc_saved);
161 33 lampret
        mtspr(SPR_EEAR_BASE, ea);
162 64 lampret
        mtspr(SPR_ESR_BASE, mfspr(SPR_SR));
163
 
164
        /* Address translation is always disabled when starting exception. */
165
        mtspr(SPR_SR, mfspr(SPR_SR) & ~(SPR_SR_DME));
166
        mtspr(SPR_SR, mfspr(SPR_SR) & ~(SPR_SR_IME));
167
 
168 33 lampret
        mtspr(SPR_SR, mfspr(SPR_SR) | SPR_SR_SUPV);     /* SUPV mode */
169 51 lampret
        mtspr(SPR_SR, mfspr(SPR_SR) & ~SPR_SR_EXR);     /* Disable except. */
170 167 markom
 
171 33 lampret
        pc = (unsigned long)except;
172 123 markom
 
173 139 chris
        /* This has been removed. All exceptions (not just SYSCALL) suffer
174
           from the same problem. The solution is to continue just like
175
           the pipeline would, and issue the exception on the next
176
           clock cycle. We assume now that this function is being called
177
           ->BEFORE<- the instruction fetch and after the previous update
178
           which always yields the correct behavior. This has the added
179
           advantage that a debugger can prevent an exception from
180
           taking place by resetting the pc. */
181
#if 0
182 123 markom
        /* MM: We do pc update after the execute (in the simulator), so we
183
           decrease it by 4 so that next instruction points to first exception
184 167 markom
           instruction.  Do NOT comment this out. */
185 123 markom
        if (except == EXCEPT_SYSCALL)
186
          pc -= 4;
187 139 chris
#endif
188 123 markom
        pcnext = pc+4;
189
 
190
        /* Added by CZ 27/05/01 */
191 139 chris
        pc_phy = pc;      /* An exception always turns off the MMU, so
192
                             pc is always pc_phy */
193
 
194 33 lampret
#endif
195
}

powered by: WebSVN 2.1.0

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