forked from derselbst/lazyusf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexception.c
291 lines (260 loc) · 6.91 KB
/
exception.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/*
* Project 64 - A Nintendo 64 emulator.
*
* (c) Copyright 2001 zilmar (zilmar@emulation64.com) and
* Jabo (jabo@emulation64.com).
*
* pj64 homepage: www.pj64.net
*
* Permission to use, copy, modify and distribute Project64 in both binary and
* source form, for non-commercial purposes, is hereby granted without fee,
* providing that this license information and copyright notice appear with
* all copies and any derived work.
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event shall the authors be held liable for any damages
* arising from the use of this software.
*
* Project64 is freeware for PERSONAL USE only. Commercial users should
* seek permission of the copyright holders first. Commercial use includes
* charging money for Project64 or software derived from Project64.
*
* The copyright holders request that bug fixes and improvements to the code
* should be forwarded to them so if they want them.
*
*/
#include <stdio.h>
#include "cpu.h"
#include "exception.h"
#include "tlb.h"
#include "registers.h"
extern uintptr_t *TLB_Map;
void CheckInterrupts ( void )
{
MI_INTR_REG &= ~MI_INTR_AI;
MI_INTR_REG |= (AudioIntrReg & MI_INTR_AI);
if ((MI_INTR_MASK_REG & MI_INTR_REG) != 0)
{
FAKE_CAUSE_REGISTER |= CAUSE_IP2;
}
else
{
FAKE_CAUSE_REGISTER &= ~CAUSE_IP2;
}
if (( STATUS_REGISTER & STATUS_IE ) == 0 )
{
return;
}
if (( STATUS_REGISTER & STATUS_EXL ) != 0 )
{
return;
}
if (( STATUS_REGISTER & STATUS_ERL ) != 0 )
{
return;
}
if (( STATUS_REGISTER & FAKE_CAUSE_REGISTER & 0xFF00) != 0)
{
if (!CPU_Action->DoInterrupt)
{
CPU_Action->DoSomething = 1;
CPU_Action->DoInterrupt = 1;
}
}
}
void DoAddressError ( uint32_t DelaySlot, uint32_t BadVaddr, uint32_t FromRead)
{
int address = TLB_Map[BadVaddr >> 12] + BadVaddr;
printf("AddressError at %08x (%08x) Vaddr=%08x %08x %08x\n", PROGRAM_COUNTER, address, BadVaddr, GPR[0x8].UW[0], GPR[0x9].UW[0]);
if (FromRead)
{
CAUSE_REGISTER = EXC_RADE;
}
else
{
CAUSE_REGISTER = EXC_WADE;
}
BAD_VADDR_REGISTER = BadVaddr;
if (DelaySlot)
{
CAUSE_REGISTER |= CAUSE_BD;
EPC_REGISTER = PROGRAM_COUNTER - 4;
}
else
{
EPC_REGISTER = PROGRAM_COUNTER;
}
STATUS_REGISTER |= STATUS_EXL;
PROGRAM_COUNTER = 0x80000180;
}
void DoBreakException ( uint32_t DelaySlot)
{
CAUSE_REGISTER = EXC_BREAK;
if (DelaySlot)
{
CAUSE_REGISTER |= CAUSE_BD;
EPC_REGISTER = PROGRAM_COUNTER - 4;
}
else
{
EPC_REGISTER = PROGRAM_COUNTER;
}
STATUS_REGISTER |= STATUS_EXL;
PROGRAM_COUNTER = 0x80000180;
}
void DoCopUnusableException ( uint32_t DelaySlot, uint32_t Coprocessor )
{
CAUSE_REGISTER = EXC_CPU;
if (Coprocessor == 1)
{
CAUSE_REGISTER |= 0x10000000;
}
if (DelaySlot)
{
CAUSE_REGISTER |= CAUSE_BD;
EPC_REGISTER = PROGRAM_COUNTER - 4;
}
else
{
EPC_REGISTER = PROGRAM_COUNTER;
}
STATUS_REGISTER |= STATUS_EXL;
PROGRAM_COUNTER = 0x80000180;
}
void DoIntrException ( uint32_t DelaySlot )
{
if (( STATUS_REGISTER & STATUS_IE ) == 0 )
{
return;
}
if (( STATUS_REGISTER & STATUS_EXL ) != 0 )
{
return;
}
if (( STATUS_REGISTER & STATUS_ERL ) != 0 )
{
return;
}
CAUSE_REGISTER = FAKE_CAUSE_REGISTER;
CAUSE_REGISTER |= EXC_INT;
EPC_REGISTER = PROGRAM_COUNTER;
if (DelaySlot)
{
CAUSE_REGISTER |= CAUSE_BD;
EPC_REGISTER -= 4;
}
STATUS_REGISTER |= STATUS_EXL;
PROGRAM_COUNTER = 0x80000180;
}
/*
#ifdef USEX64
void CompileDoIntrException(void) {
uint8_t * Jump, * Jump2;
MoveVariableToX86reg(&STATUS_REGISTER, x86_TEMPD);
AndConstToX86Reg(x86_TEMPD,7);
CompConstToX86reg(x86_TEMPD,1);
JneLabel8(0);
Jump = RecompPos - 1;
Push(x86_RAX);
MoveVariableToX86reg(&FAKE_CAUSE_REGISTER, x86_EAX);
OrConstToX86Reg(EXC_INT, x86_EAX);
CompConstToX86reg(x86_ECX,0);
MoveVariableToX86reg(&PROGRAM_COUNTER, x86_ECX);
JeLabel8(0);
Jump2 = RecompPos - 1;
OrConstToX86Reg(CAUSE_BD, x86_EAX);
SubConstFromX86Reg(x86_ECX,4);
SetJump8(Jump2, RecompPos);
MoveX86regToVariable(x86_ECX, &EPC_REGISTER);
MoveX86regToVariable(x86_EAX, &CAUSE_REGISTER);
OrConstToVariable(STATUS_EXL, &STATUS_REGISTER);
MoveConstToVariable(0x80000180, &PROGRAM_COUNTER);
Pop(x86_RAX);
SetJump8(Jump, RecompPos);
}
#endif
#ifdef USEX64
void CompileCheckInterrupts(void) {
uint8_t * Jump, * Jump2, * Jump3;
BreakPoint();
Push(x86_EAX);
Push(x86_EDX);
MoveVariableToX86reg(&MI_INTR_REG, x86_EAX);
MoveVariableToX86reg(&FAKE_CAUSE_REGISTER, x86_EDX);
AndConstToX86Reg(x86_EAX, ~MI_INTR_AI);
OrConstToX86Reg(CAUSE_IP2, x86_EDX);
TestVariableToX86Reg(x86_EAX, &MI_INTR_MASK_REG);
JnzLabel8(0);
Jump = RecompPos - 1;
AndConstToX86Reg(x86_EDX, ~CAUSE_IP2);
SetJump8(Jump, RecompPos);
MoveVariableToX86reg(&STATUS_REGISTER, x86_TEMPD);
AndConstToX86Reg(x86_TEMPD,7);
CompConstToX86reg(x86_TEMPD,1);
JneLabel8(0);
Jump2 = RecompPos - 1;
MoveVariableToX86reg(&STATUS_REGISTER, x86_TEMPD);
AddX86RegToX86Reg(x86_TEMPD, x86_EDX);
AndConstToX86Reg(x86_TEMPD,0xFF00);
CompConstToVariable(0, &CPU_Action->DoInterrupt);
JneLabel8(0);
Jump = RecompPos - 1;
MoveConstToVariable(1, &CPU_Action->DoInterrupt);
MoveConstToVariable(1, &CPU_Action->DoSomething);
SetJump8(Jump, RecompPos);
SetJump8(Jump2, RecompPos);
MoveX86regToVariable(x86_EAX, &MI_INTR_REG);
MoveX86regToVariable(x86_EDX, &FAKE_CAUSE_REGISTER);
Pop(x86_EDX);
Pop(x86_EAX);
}
#endif
*/
void DoTLBMiss ( uint32_t DelaySlot, uint32_t BadVaddr )
{
CAUSE_REGISTER = EXC_RMISS;
BAD_VADDR_REGISTER = BadVaddr;
CONTEXT_REGISTER &= 0xFF80000F;
CONTEXT_REGISTER |= (BadVaddr >> 9) & 0x007FFFF0;
ENTRYHI_REGISTER = (BadVaddr & 0xFFFFE000);
if ((STATUS_REGISTER & STATUS_EXL) == 0)
{
if (DelaySlot)
{
CAUSE_REGISTER |= CAUSE_BD;
EPC_REGISTER = PROGRAM_COUNTER - 4;
}
else
{
EPC_REGISTER = PROGRAM_COUNTER;
}
if (AddressDefined(BadVaddr))
{
PROGRAM_COUNTER = 0x80000180;
}
else
{
PROGRAM_COUNTER = 0x80000000;
}
STATUS_REGISTER |= STATUS_EXL;
}
else
{
PROGRAM_COUNTER = 0x80000180;
}
}
void DoSysCallException ( uint32_t DelaySlot)
{
CAUSE_REGISTER = EXC_SYSCALL;
if (DelaySlot)
{
CAUSE_REGISTER |= CAUSE_BD;
EPC_REGISTER = PROGRAM_COUNTER - 4;
}
else
{
EPC_REGISTER = PROGRAM_COUNTER;
}
STATUS_REGISTER |= STATUS_EXL;
PROGRAM_COUNTER = 0x80000180;
}