Commit | Line | Data |
---|---|---|
c906108c SS |
1 | /* arminit.c -- ARMulator initialization: ARM6 Instruction Emulator. |
2 | Copyright (C) 1994 Advanced RISC Machines Ltd. | |
3 | ||
4 | This program is free software; you can redistribute it and/or modify | |
5 | it under the terms of the GNU General Public License as published by | |
6 | the Free Software Foundation; either version 2 of the License, or | |
7 | (at your option) any later version. | |
8 | ||
9 | This program is distributed in the hope that it will be useful, | |
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | GNU General Public License for more details. | |
13 | ||
14 | You should have received a copy of the GNU General Public License | |
15 | along with this program; if not, write to the Free Software | |
16 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */ | |
17 | ||
18 | #include "armdefs.h" | |
19 | #include "armemu.h" | |
20 | ||
21 | /***************************************************************************\ | |
22 | * Definitions for the emulator architecture * | |
23 | \***************************************************************************/ | |
24 | ||
dfcd3bfb JM |
25 | void ARMul_EmulateInit (void); |
26 | ARMul_State *ARMul_NewState (void); | |
27 | void ARMul_Reset (ARMul_State * state); | |
28 | ARMword ARMul_DoCycle (ARMul_State * state); | |
29 | unsigned ARMul_DoCoPro (ARMul_State * state); | |
30 | ARMword ARMul_DoProg (ARMul_State * state); | |
31 | ARMword ARMul_DoInstr (ARMul_State * state); | |
32 | void ARMul_Abort (ARMul_State * state, ARMword address); | |
33 | ||
34 | unsigned ARMul_MultTable[32] = | |
35 | { 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, | |
36 | 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 16 | |
37 | }; | |
38 | ARMword ARMul_ImmedTable[4096]; /* immediate DP LHS values */ | |
39 | char ARMul_BitList[256]; /* number of bits in a byte table */ | |
c906108c SS |
40 | |
41 | /***************************************************************************\ | |
42 | * Call this routine once to set up the emulator's tables. * | |
43 | \***************************************************************************/ | |
44 | ||
dfcd3bfb JM |
45 | void |
46 | ARMul_EmulateInit (void) | |
47 | { | |
48 | unsigned long i, j; | |
c906108c | 49 | |
dfcd3bfb JM |
50 | for (i = 0; i < 4096; i++) |
51 | { /* the values of 12 bit dp rhs's */ | |
52 | ARMul_ImmedTable[i] = ROTATER (i & 0xffL, (i >> 7L) & 0x1eL); | |
c906108c SS |
53 | } |
54 | ||
dfcd3bfb JM |
55 | for (i = 0; i < 256; ARMul_BitList[i++] = 0); /* how many bits in LSM */ |
56 | for (j = 1; j < 256; j <<= 1) | |
57 | for (i = 0; i < 256; i++) | |
58 | if ((i & j) > 0) | |
59 | ARMul_BitList[i]++; | |
60 | ||
61 | for (i = 0; i < 256; i++) | |
62 | ARMul_BitList[i] *= 4; /* you always need 4 times these values */ | |
c906108c | 63 | |
c906108c SS |
64 | } |
65 | ||
66 | /***************************************************************************\ | |
67 | * Returns a new instantiation of the ARMulator's state * | |
68 | \***************************************************************************/ | |
69 | ||
dfcd3bfb JM |
70 | ARMul_State * |
71 | ARMul_NewState (void) | |
72 | { | |
73 | ARMul_State *state; | |
74 | unsigned i, j; | |
75 | ||
76 | state = (ARMul_State *) malloc (sizeof (ARMul_State)); | |
77 | memset (state, 0, sizeof (ARMul_State)); | |
78 | ||
79 | state->Emulate = RUN; | |
80 | for (i = 0; i < 16; i++) | |
81 | { | |
82 | state->Reg[i] = 0; | |
83 | for (j = 0; j < 7; j++) | |
84 | state->RegBank[j][i] = 0; | |
c906108c | 85 | } |
dfcd3bfb JM |
86 | for (i = 0; i < 7; i++) |
87 | state->Spsr[i] = 0; | |
3943c96b | 88 | |
f1129fb8 NC |
89 | /* state->Mode = USER26MODE; */ |
90 | state->Mode = USER32MODE; | |
dfcd3bfb JM |
91 | |
92 | state->CallDebug = FALSE; | |
93 | state->Debug = FALSE; | |
94 | state->VectorCatch = 0; | |
95 | state->Aborted = FALSE; | |
96 | state->Reseted = FALSE; | |
97 | state->Inted = 3; | |
98 | state->LastInted = 3; | |
99 | ||
100 | state->MemDataPtr = NULL; | |
101 | state->MemInPtr = NULL; | |
102 | state->MemOutPtr = NULL; | |
103 | state->MemSparePtr = NULL; | |
104 | state->MemSize = 0; | |
105 | ||
106 | state->OSptr = NULL; | |
107 | state->CommandLine = NULL; | |
108 | ||
c3ae2f98 MG |
109 | state->CP14R0_CCD = -1; |
110 | state->LastTime = 0; | |
111 | ||
dfcd3bfb JM |
112 | state->EventSet = 0; |
113 | state->Now = 0; | |
114 | state->EventPtr = (struct EventNode **) malloc ((unsigned) EVENTLISTSIZE * | |
115 | sizeof (struct EventNode | |
116 | *)); | |
117 | for (i = 0; i < EVENTLISTSIZE; i++) | |
118 | *(state->EventPtr + i) = NULL; | |
c906108c | 119 | |
dfcd3bfb JM |
120 | state->prog32Sig = HIGH; |
121 | state->data32Sig = HIGH; | |
c906108c | 122 | |
dfcd3bfb JM |
123 | state->lateabtSig = LOW; |
124 | state->bigendSig = LOW; | |
c906108c | 125 | |
3943c96b NC |
126 | state->is_v4 = LOW; |
127 | state->is_v5 = LOW; | |
f1129fb8 NC |
128 | state->is_v5e = LOW; |
129 | state->is_XScale = LOW; | |
1e6b544a | 130 | |
dfcd3bfb | 131 | ARMul_Reset (state); |
3943c96b NC |
132 | |
133 | return state; | |
dfcd3bfb | 134 | } |
c906108c SS |
135 | |
136 | /***************************************************************************\ | |
3943c96b | 137 | Call this routine to set ARMulator to model certain processor properities |
c906108c | 138 | \***************************************************************************/ |
dfcd3bfb JM |
139 | |
140 | void | |
3943c96b | 141 | ARMul_SelectProcessor (ARMul_State * state, unsigned properties) |
dfcd3bfb | 142 | { |
3943c96b | 143 | if (properties & ARM_Fix26_Prop) |
dfcd3bfb JM |
144 | { |
145 | state->prog32Sig = LOW; | |
146 | state->data32Sig = LOW; | |
147 | } | |
148 | else | |
149 | { | |
150 | state->prog32Sig = HIGH; | |
151 | state->data32Sig = HIGH; | |
152 | } | |
153 | ||
c906108c | 154 | state->lateabtSig = LOW; |
1e6b544a | 155 | |
3943c96b NC |
156 | state->is_v4 = (properties & (ARM_v4_Prop | ARM_v5_Prop)) ? HIGH : LOW; |
157 | state->is_v5 = (properties & ARM_v5_Prop) ? HIGH : LOW; | |
f1129fb8 NC |
158 | state->is_v5e = (properties & ARM_v5e_Prop) ? HIGH : LOW; |
159 | state->is_XScale = (properties & ARM_XScale_Prop) ? HIGH : LOW; | |
c906108c SS |
160 | } |
161 | ||
162 | /***************************************************************************\ | |
163 | * Call this routine to set up the initial machine state (or perform a RESET * | |
164 | \***************************************************************************/ | |
165 | ||
dfcd3bfb JM |
166 | void |
167 | ARMul_Reset (ARMul_State * state) | |
168 | { | |
169 | state->NextInstr = 0; | |
c1a72ffd | 170 | |
dfcd3bfb JM |
171 | if (state->prog32Sig) |
172 | { | |
173 | state->Reg[15] = 0; | |
174 | state->Cpsr = INTBITS | SVC32MODE; | |
c1a72ffd | 175 | state->Mode = SVC32MODE; |
c906108c | 176 | } |
dfcd3bfb JM |
177 | else |
178 | { | |
179 | state->Reg[15] = R15INTBITS | SVC26MODE; | |
180 | state->Cpsr = INTBITS | SVC26MODE; | |
c1a72ffd | 181 | state->Mode = SVC26MODE; |
c906108c | 182 | } |
c1a72ffd | 183 | |
dfcd3bfb JM |
184 | ARMul_CPSRAltered (state); |
185 | state->Bank = SVCBANK; | |
c1a72ffd | 186 | |
dfcd3bfb JM |
187 | FLUSHPIPE; |
188 | ||
189 | state->EndCondition = 0; | |
190 | state->ErrorCode = 0; | |
191 | ||
192 | state->Exception = FALSE; | |
193 | state->NresetSig = HIGH; | |
194 | state->NfiqSig = HIGH; | |
195 | state->NirqSig = HIGH; | |
196 | state->NtransSig = (state->Mode & 3) ? HIGH : LOW; | |
197 | state->abortSig = LOW; | |
198 | state->AbortAddr = 1; | |
199 | ||
200 | state->NumInstrs = 0; | |
201 | state->NumNcycles = 0; | |
202 | state->NumScycles = 0; | |
203 | state->NumIcycles = 0; | |
204 | state->NumCcycles = 0; | |
205 | state->NumFcycles = 0; | |
206 | #ifdef ASIM | |
207 | (void) ARMul_MemoryInit (); | |
208 | ARMul_OSInit (state); | |
209 | #endif | |
c906108c SS |
210 | } |
211 | ||
212 | ||
213 | /***************************************************************************\ | |
214 | * Emulate the execution of an entire program. Start the correct emulator * | |
215 | * (Emulate26 for a 26 bit ARM and Emulate32 for a 32 bit ARM), return the * | |
216 | * address of the last instruction that is executed. * | |
217 | \***************************************************************************/ | |
218 | ||
dfcd3bfb JM |
219 | ARMword |
220 | ARMul_DoProg (ARMul_State * state) | |
221 | { | |
222 | ARMword pc = 0; | |
223 | ||
224 | state->Emulate = RUN; | |
225 | while (state->Emulate != STOP) | |
226 | { | |
227 | state->Emulate = RUN; | |
228 | if (state->prog32Sig && ARMul_MODE32BIT) | |
229 | pc = ARMul_Emulate32 (state); | |
230 | else | |
231 | pc = ARMul_Emulate26 (state); | |
c906108c | 232 | } |
dfcd3bfb JM |
233 | return (pc); |
234 | } | |
c906108c SS |
235 | |
236 | /***************************************************************************\ | |
237 | * Emulate the execution of one instruction. Start the correct emulator * | |
238 | * (Emulate26 for a 26 bit ARM and Emulate32 for a 32 bit ARM), return the * | |
239 | * address of the instruction that is executed. * | |
240 | \***************************************************************************/ | |
241 | ||
dfcd3bfb JM |
242 | ARMword |
243 | ARMul_DoInstr (ARMul_State * state) | |
244 | { | |
245 | ARMword pc = 0; | |
c906108c | 246 | |
dfcd3bfb JM |
247 | state->Emulate = ONCE; |
248 | if (state->prog32Sig && ARMul_MODE32BIT) | |
249 | pc = ARMul_Emulate32 (state); | |
250 | else | |
251 | pc = ARMul_Emulate26 (state); | |
c906108c | 252 | |
dfcd3bfb JM |
253 | return (pc); |
254 | } | |
c906108c SS |
255 | |
256 | /***************************************************************************\ | |
257 | * This routine causes an Abort to occur, including selecting the correct * | |
258 | * mode, register bank, and the saving of registers. Call with the * | |
259 | * appropriate vector's memory address (0,4,8 ....) * | |
260 | \***************************************************************************/ | |
261 | ||
dfcd3bfb JM |
262 | void |
263 | ARMul_Abort (ARMul_State * state, ARMword vector) | |
264 | { | |
265 | ARMword temp; | |
e063aa3b | 266 | int isize = INSN_SIZE; |
f1129fb8 NC |
267 | int esize = (TFLAG ? 0 : 4); |
268 | int e2size = (TFLAG ? -4 : 0); | |
c906108c | 269 | |
dfcd3bfb | 270 | state->Aborted = FALSE; |
c906108c | 271 | |
dfcd3bfb JM |
272 | if (ARMul_OSException (state, vector, ARMul_GetPC (state))) |
273 | return; | |
c906108c | 274 | |
dfcd3bfb | 275 | if (state->prog32Sig) |
c906108c | 276 | if (ARMul_MODE26BIT) |
dfcd3bfb | 277 | temp = R15PC; |
c906108c | 278 | else |
dfcd3bfb JM |
279 | temp = state->Reg[15]; |
280 | else | |
281 | temp = R15PC | ECC | ER15INT | EMODE; | |
282 | ||
283 | switch (vector) | |
284 | { | |
285 | case ARMul_ResetV: /* RESET */ | |
e063aa3b | 286 | SETABORT (INTBITS, state->prog32Sig ? SVC32MODE : SVC26MODE, 0); |
dfcd3bfb JM |
287 | break; |
288 | case ARMul_UndefinedInstrV: /* Undefined Instruction */ | |
e063aa3b | 289 | SETABORT (IBIT, state->prog32Sig ? UNDEF32MODE : SVC26MODE, isize); |
dfcd3bfb JM |
290 | break; |
291 | case ARMul_SWIV: /* Software Interrupt */ | |
e063aa3b | 292 | SETABORT (IBIT, state->prog32Sig ? SVC32MODE : SVC26MODE, isize); |
dfcd3bfb JM |
293 | break; |
294 | case ARMul_PrefetchAbortV: /* Prefetch Abort */ | |
295 | state->AbortAddr = 1; | |
f1129fb8 | 296 | SETABORT (IBIT, state->prog32Sig ? ABORT32MODE : SVC26MODE, esize); |
dfcd3bfb JM |
297 | break; |
298 | case ARMul_DataAbortV: /* Data Abort */ | |
f1129fb8 | 299 | SETABORT (IBIT, state->prog32Sig ? ABORT32MODE : SVC26MODE, e2size); |
dfcd3bfb JM |
300 | break; |
301 | case ARMul_AddrExceptnV: /* Address Exception */ | |
e063aa3b | 302 | SETABORT (IBIT, SVC26MODE, isize); |
dfcd3bfb JM |
303 | break; |
304 | case ARMul_IRQV: /* IRQ */ | |
57165fb4 NC |
305 | if ( ! state->is_XScale |
306 | || ! state->CPRead[13] (state, 0, & temp) | |
307 | || (temp & ARMul_CP13_R0_IRQ)) | |
c3ae2f98 | 308 | SETABORT (IBIT, state->prog32Sig ? IRQ32MODE : IRQ26MODE, esize); |
dfcd3bfb JM |
309 | break; |
310 | case ARMul_FIQV: /* FIQ */ | |
57165fb4 NC |
311 | if ( ! state->is_XScale |
312 | || ! state->CPRead[13] (state, 0, & temp) | |
313 | || (temp & ARMul_CP13_R0_FIQ)) | |
c3ae2f98 | 314 | SETABORT (INTBITS, state->prog32Sig ? FIQ32MODE : FIQ26MODE, esize); |
dfcd3bfb | 315 | break; |
c906108c | 316 | } |
dfcd3bfb JM |
317 | if (ARMul_MODE32BIT) |
318 | ARMul_SetR15 (state, vector); | |
319 | else | |
320 | ARMul_SetR15 (state, R15CCINTMODE | vector); | |
c906108c | 321 | } |