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