198b9484cSchristos /* arminit.c -- ARMulator initialization: ARM6 Instruction Emulator.
298b9484cSchristos Copyright (C) 1994 Advanced RISC Machines Ltd.
398b9484cSchristos
498b9484cSchristos This program is free software; you can redistribute it and/or modify
598b9484cSchristos it under the terms of the GNU General Public License as published by
6a2e2270fSchristos the Free Software Foundation; either version 3 of the License, or
798b9484cSchristos (at your option) any later version.
898b9484cSchristos
998b9484cSchristos This program is distributed in the hope that it will be useful,
1098b9484cSchristos but WITHOUT ANY WARRANTY; without even the implied warranty of
1198b9484cSchristos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1298b9484cSchristos GNU General Public License for more details.
1398b9484cSchristos
1498b9484cSchristos You should have received a copy of the GNU General Public License
15a2e2270fSchristos along with this program; if not, see <http://www.gnu.org/licenses/>. */
1698b9484cSchristos
17*4b169a6bSchristos /* This must come before any other includes. */
18*4b169a6bSchristos #include "defs.h"
19*4b169a6bSchristos
2098b9484cSchristos #include <string.h>
2198b9484cSchristos
2298b9484cSchristos #include "armdefs.h"
2398b9484cSchristos #include "armemu.h"
2498b9484cSchristos #include "dbg_rdi.h"
2598b9484cSchristos
2698b9484cSchristos /***************************************************************************\
2798b9484cSchristos * Definitions for the emulator architecture *
2898b9484cSchristos \***************************************************************************/
2998b9484cSchristos
3098b9484cSchristos void ARMul_EmulateInit (void);
3198b9484cSchristos ARMul_State *ARMul_NewState (void);
3298b9484cSchristos void ARMul_Reset (ARMul_State * state);
3398b9484cSchristos ARMword ARMul_DoCycle (ARMul_State * state);
3498b9484cSchristos unsigned ARMul_DoCoPro (ARMul_State * state);
3598b9484cSchristos ARMword ARMul_DoProg (ARMul_State * state);
3698b9484cSchristos ARMword ARMul_DoInstr (ARMul_State * state);
3798b9484cSchristos void ARMul_Abort (ARMul_State * state, ARMword address);
3898b9484cSchristos
3998b9484cSchristos unsigned ARMul_MultTable[32] =
4098b9484cSchristos { 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9,
4198b9484cSchristos 10, 10, 11, 11, 12, 12, 13, 13, 14, 14, 15, 15, 16, 16, 16
4298b9484cSchristos };
4398b9484cSchristos ARMword ARMul_ImmedTable[4096]; /* immediate DP LHS values */
4498b9484cSchristos char ARMul_BitList[256]; /* number of bits in a byte table */
4598b9484cSchristos
468dffb485Schristos /* The PC pipeline value depends on whether ARM
478dffb485Schristos or Thumb instructions are being executed. */
488dffb485Schristos ARMword isize;
498dffb485Schristos
5098b9484cSchristos /***************************************************************************\
5198b9484cSchristos * Call this routine once to set up the emulator's tables. *
5298b9484cSchristos \***************************************************************************/
5398b9484cSchristos
5498b9484cSchristos void
ARMul_EmulateInit(void)5598b9484cSchristos ARMul_EmulateInit (void)
5698b9484cSchristos {
5798b9484cSchristos unsigned long i, j;
5898b9484cSchristos
5998b9484cSchristos for (i = 0; i < 4096; i++)
6098b9484cSchristos { /* the values of 12 bit dp rhs's */
6198b9484cSchristos ARMul_ImmedTable[i] = ROTATER (i & 0xffL, (i >> 7L) & 0x1eL);
6298b9484cSchristos }
6398b9484cSchristos
6498b9484cSchristos for (i = 0; i < 256; ARMul_BitList[i++] = 0); /* how many bits in LSM */
6598b9484cSchristos for (j = 1; j < 256; j <<= 1)
6698b9484cSchristos for (i = 0; i < 256; i++)
6798b9484cSchristos if ((i & j) > 0)
6898b9484cSchristos ARMul_BitList[i]++;
6998b9484cSchristos
7098b9484cSchristos for (i = 0; i < 256; i++)
7198b9484cSchristos ARMul_BitList[i] *= 4; /* you always need 4 times these values */
7298b9484cSchristos
7398b9484cSchristos }
7498b9484cSchristos
7598b9484cSchristos /***************************************************************************\
7698b9484cSchristos * Returns a new instantiation of the ARMulator's state *
7798b9484cSchristos \***************************************************************************/
7898b9484cSchristos
7998b9484cSchristos ARMul_State *
ARMul_NewState(void)8098b9484cSchristos ARMul_NewState (void)
8198b9484cSchristos {
8298b9484cSchristos ARMul_State *state;
8398b9484cSchristos unsigned i, j;
8498b9484cSchristos
8598b9484cSchristos state = (ARMul_State *) malloc (sizeof (ARMul_State));
8698b9484cSchristos memset (state, 0, sizeof (ARMul_State));
8798b9484cSchristos
8898b9484cSchristos state->Emulate = RUN;
8998b9484cSchristos for (i = 0; i < 16; i++)
9098b9484cSchristos {
9198b9484cSchristos state->Reg[i] = 0;
9298b9484cSchristos for (j = 0; j < 7; j++)
9398b9484cSchristos state->RegBank[j][i] = 0;
9498b9484cSchristos }
9598b9484cSchristos for (i = 0; i < 7; i++)
9698b9484cSchristos state->Spsr[i] = 0;
9798b9484cSchristos
9898b9484cSchristos /* state->Mode = USER26MODE; */
9998b9484cSchristos state->Mode = USER32MODE;
10098b9484cSchristos
10198b9484cSchristos state->CallDebug = FALSE;
10298b9484cSchristos state->Debug = FALSE;
10398b9484cSchristos state->VectorCatch = 0;
10498b9484cSchristos state->Aborted = FALSE;
10598b9484cSchristos state->Reseted = FALSE;
10698b9484cSchristos state->Inted = 3;
10798b9484cSchristos state->LastInted = 3;
10898b9484cSchristos
10998b9484cSchristos state->MemDataPtr = NULL;
11098b9484cSchristos state->MemInPtr = NULL;
11198b9484cSchristos state->MemOutPtr = NULL;
11298b9484cSchristos state->MemSparePtr = NULL;
11398b9484cSchristos state->MemSize = 0;
11498b9484cSchristos
11598b9484cSchristos state->OSptr = NULL;
11698b9484cSchristos state->CommandLine = NULL;
11798b9484cSchristos
11898b9484cSchristos state->CP14R0_CCD = -1;
11998b9484cSchristos state->LastTime = 0;
12098b9484cSchristos
12198b9484cSchristos state->EventSet = 0;
12298b9484cSchristos state->Now = 0;
12398b9484cSchristos state->EventPtr = (struct EventNode **) malloc ((unsigned) EVENTLISTSIZE *
12498b9484cSchristos sizeof (struct EventNode
12598b9484cSchristos *));
12698b9484cSchristos for (i = 0; i < EVENTLISTSIZE; i++)
12798b9484cSchristos *(state->EventPtr + i) = NULL;
12898b9484cSchristos
12998b9484cSchristos state->prog32Sig = HIGH;
13098b9484cSchristos state->data32Sig = HIGH;
13198b9484cSchristos
13298b9484cSchristos state->lateabtSig = LOW;
13398b9484cSchristos state->bigendSig = LOW;
13498b9484cSchristos
13598b9484cSchristos state->is_v4 = LOW;
13698b9484cSchristos state->is_v5 = LOW;
13798b9484cSchristos state->is_v5e = LOW;
13898b9484cSchristos state->is_XScale = LOW;
13998b9484cSchristos state->is_iWMMXt = LOW;
14098b9484cSchristos state->is_v6 = LOW;
14198b9484cSchristos
14298b9484cSchristos ARMul_Reset (state);
14398b9484cSchristos
14498b9484cSchristos return state;
14598b9484cSchristos }
14698b9484cSchristos
14798b9484cSchristos /***************************************************************************\
14898b9484cSchristos Call this routine to set ARMulator to model certain processor properities
14998b9484cSchristos \***************************************************************************/
15098b9484cSchristos
15198b9484cSchristos void
ARMul_SelectProcessor(ARMul_State * state,unsigned properties)15298b9484cSchristos ARMul_SelectProcessor (ARMul_State * state, unsigned properties)
15398b9484cSchristos {
15498b9484cSchristos if (properties & ARM_Fix26_Prop)
15598b9484cSchristos {
15698b9484cSchristos state->prog32Sig = LOW;
15798b9484cSchristos state->data32Sig = LOW;
15898b9484cSchristos }
15998b9484cSchristos else
16098b9484cSchristos {
16198b9484cSchristos state->prog32Sig = HIGH;
16298b9484cSchristos state->data32Sig = HIGH;
16398b9484cSchristos }
16498b9484cSchristos
16598b9484cSchristos state->lateabtSig = LOW;
16698b9484cSchristos
16798b9484cSchristos state->is_v4 = (properties & (ARM_v4_Prop | ARM_v5_Prop)) ? HIGH : LOW;
16898b9484cSchristos state->is_v5 = (properties & ARM_v5_Prop) ? HIGH : LOW;
16998b9484cSchristos state->is_v5e = (properties & ARM_v5e_Prop) ? HIGH : LOW;
17098b9484cSchristos state->is_XScale = (properties & ARM_XScale_Prop) ? HIGH : LOW;
17198b9484cSchristos state->is_iWMMXt = (properties & ARM_iWMMXt_Prop) ? HIGH : LOW;
17298b9484cSchristos state->is_ep9312 = (properties & ARM_ep9312_Prop) ? HIGH : LOW;
17398b9484cSchristos state->is_v6 = (properties & ARM_v6_Prop) ? HIGH : LOW;
17498b9484cSchristos
17598b9484cSchristos /* Only initialse the coprocessor support once we
17698b9484cSchristos know what kind of chip we are dealing with. */
17798b9484cSchristos ARMul_CoProInit (state);
17898b9484cSchristos }
17998b9484cSchristos
18098b9484cSchristos /***************************************************************************\
18198b9484cSchristos * Call this routine to set up the initial machine state (or perform a RESET *
18298b9484cSchristos \***************************************************************************/
18398b9484cSchristos
18498b9484cSchristos void
ARMul_Reset(ARMul_State * state)18598b9484cSchristos ARMul_Reset (ARMul_State * state)
18698b9484cSchristos {
18798b9484cSchristos state->NextInstr = 0;
18898b9484cSchristos
18998b9484cSchristos if (state->prog32Sig)
19098b9484cSchristos {
19198b9484cSchristos state->Reg[15] = 0;
19298b9484cSchristos state->Cpsr = INTBITS | SVC32MODE;
19398b9484cSchristos state->Mode = SVC32MODE;
19498b9484cSchristos }
19598b9484cSchristos else
19698b9484cSchristos {
19798b9484cSchristos state->Reg[15] = R15INTBITS | SVC26MODE;
19898b9484cSchristos state->Cpsr = INTBITS | SVC26MODE;
19998b9484cSchristos state->Mode = SVC26MODE;
20098b9484cSchristos }
20198b9484cSchristos
20298b9484cSchristos ARMul_CPSRAltered (state);
20398b9484cSchristos state->Bank = SVCBANK;
20498b9484cSchristos
20598b9484cSchristos FLUSHPIPE;
20698b9484cSchristos
20798b9484cSchristos state->EndCondition = 0;
20898b9484cSchristos
20998b9484cSchristos state->Exception = FALSE;
21098b9484cSchristos state->NresetSig = HIGH;
21198b9484cSchristos state->NfiqSig = HIGH;
21298b9484cSchristos state->NirqSig = HIGH;
21398b9484cSchristos state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
21498b9484cSchristos state->abortSig = LOW;
21598b9484cSchristos state->AbortAddr = 1;
21698b9484cSchristos
21798b9484cSchristos state->NumInstrs = 0;
21898b9484cSchristos state->NumNcycles = 0;
21998b9484cSchristos state->NumScycles = 0;
22098b9484cSchristos state->NumIcycles = 0;
22198b9484cSchristos state->NumCcycles = 0;
22298b9484cSchristos state->NumFcycles = 0;
22398b9484cSchristos #ifdef ASIM
22498b9484cSchristos (void) ARMul_MemoryInit ();
22598b9484cSchristos ARMul_OSInit (state);
22698b9484cSchristos #endif
22798b9484cSchristos }
22898b9484cSchristos
22998b9484cSchristos
23098b9484cSchristos /***************************************************************************\
23198b9484cSchristos * Emulate the execution of an entire program. Start the correct emulator *
23298b9484cSchristos * (Emulate26 for a 26 bit ARM and Emulate32 for a 32 bit ARM), return the *
23398b9484cSchristos * address of the last instruction that is executed. *
23498b9484cSchristos \***************************************************************************/
23598b9484cSchristos
23698b9484cSchristos ARMword
ARMul_DoProg(ARMul_State * state)23798b9484cSchristos ARMul_DoProg (ARMul_State * state)
23898b9484cSchristos {
23998b9484cSchristos ARMword pc = 0;
24098b9484cSchristos
24198b9484cSchristos state->Emulate = RUN;
24298b9484cSchristos while (state->Emulate != STOP)
24398b9484cSchristos {
24498b9484cSchristos state->Emulate = RUN;
24598b9484cSchristos if (state->prog32Sig && ARMul_MODE32BIT)
24698b9484cSchristos pc = ARMul_Emulate32 (state);
24798b9484cSchristos else
24898b9484cSchristos pc = ARMul_Emulate26 (state);
24998b9484cSchristos }
25098b9484cSchristos return (pc);
25198b9484cSchristos }
25298b9484cSchristos
25398b9484cSchristos /***************************************************************************\
25498b9484cSchristos * Emulate the execution of one instruction. Start the correct emulator *
25598b9484cSchristos * (Emulate26 for a 26 bit ARM and Emulate32 for a 32 bit ARM), return the *
25698b9484cSchristos * address of the instruction that is executed. *
25798b9484cSchristos \***************************************************************************/
25898b9484cSchristos
25998b9484cSchristos ARMword
ARMul_DoInstr(ARMul_State * state)26098b9484cSchristos ARMul_DoInstr (ARMul_State * state)
26198b9484cSchristos {
26298b9484cSchristos ARMword pc = 0;
26398b9484cSchristos
26498b9484cSchristos state->Emulate = ONCE;
26598b9484cSchristos if (state->prog32Sig && ARMul_MODE32BIT)
26698b9484cSchristos pc = ARMul_Emulate32 (state);
26798b9484cSchristos else
26898b9484cSchristos pc = ARMul_Emulate26 (state);
26998b9484cSchristos
27098b9484cSchristos return (pc);
27198b9484cSchristos }
27298b9484cSchristos
27398b9484cSchristos /***************************************************************************\
27498b9484cSchristos * This routine causes an Abort to occur, including selecting the correct *
27598b9484cSchristos * mode, register bank, and the saving of registers. Call with the *
27698b9484cSchristos * appropriate vector's memory address (0,4,8 ....) *
27798b9484cSchristos \***************************************************************************/
27898b9484cSchristos
27998b9484cSchristos void
ARMul_Abort(ARMul_State * state,ARMword vector)28098b9484cSchristos ARMul_Abort (ARMul_State * state, ARMword vector)
28198b9484cSchristos {
28298b9484cSchristos ARMword temp;
28398b9484cSchristos int isize = INSN_SIZE;
28498b9484cSchristos int esize = (TFLAG ? 0 : 4);
28598b9484cSchristos int e2size = (TFLAG ? -4 : 0);
28698b9484cSchristos
28798b9484cSchristos state->Aborted = FALSE;
28898b9484cSchristos
28998b9484cSchristos if (state->prog32Sig)
29098b9484cSchristos if (ARMul_MODE26BIT)
29198b9484cSchristos temp = R15PC;
29298b9484cSchristos else
29398b9484cSchristos temp = state->Reg[15];
29498b9484cSchristos else
29598b9484cSchristos temp = R15PC | ECC | ER15INT | EMODE;
29698b9484cSchristos
29798b9484cSchristos switch (vector)
29898b9484cSchristos {
29998b9484cSchristos case ARMul_ResetV: /* RESET */
30098b9484cSchristos SETABORT (INTBITS, state->prog32Sig ? SVC32MODE : SVC26MODE, 0);
30198b9484cSchristos break;
30298b9484cSchristos case ARMul_UndefinedInstrV: /* Undefined Instruction */
30398b9484cSchristos SETABORT (IBIT, state->prog32Sig ? UNDEF32MODE : SVC26MODE, isize);
30498b9484cSchristos break;
30598b9484cSchristos case ARMul_SWIV: /* Software Interrupt */
30698b9484cSchristos SETABORT (IBIT, state->prog32Sig ? SVC32MODE : SVC26MODE, isize);
30798b9484cSchristos break;
30898b9484cSchristos case ARMul_PrefetchAbortV: /* Prefetch Abort */
30998b9484cSchristos state->AbortAddr = 1;
31098b9484cSchristos SETABORT (IBIT, state->prog32Sig ? ABORT32MODE : SVC26MODE, esize);
31198b9484cSchristos break;
31298b9484cSchristos case ARMul_DataAbortV: /* Data Abort */
31398b9484cSchristos SETABORT (IBIT, state->prog32Sig ? ABORT32MODE : SVC26MODE, e2size);
31498b9484cSchristos break;
31598b9484cSchristos case ARMul_AddrExceptnV: /* Address Exception */
31698b9484cSchristos SETABORT (IBIT, SVC26MODE, isize);
31798b9484cSchristos break;
31898b9484cSchristos case ARMul_IRQV: /* IRQ */
31998b9484cSchristos if ( ! state->is_XScale
32098b9484cSchristos || ! state->CPRead[13] (state, 0, & temp)
32198b9484cSchristos || (temp & ARMul_CP13_R0_IRQ))
32298b9484cSchristos SETABORT (IBIT, state->prog32Sig ? IRQ32MODE : IRQ26MODE, esize);
32398b9484cSchristos break;
32498b9484cSchristos case ARMul_FIQV: /* FIQ */
32598b9484cSchristos if ( ! state->is_XScale
32698b9484cSchristos || ! state->CPRead[13] (state, 0, & temp)
32798b9484cSchristos || (temp & ARMul_CP13_R0_FIQ))
32898b9484cSchristos SETABORT (INTBITS, state->prog32Sig ? FIQ32MODE : FIQ26MODE, esize);
32998b9484cSchristos break;
33098b9484cSchristos }
33198b9484cSchristos if (ARMul_MODE32BIT)
33298b9484cSchristos ARMul_SetR15 (state, vector);
33398b9484cSchristos else
33498b9484cSchristos ARMul_SetR15 (state, R15CCINTMODE | vector);
33598b9484cSchristos
33698b9484cSchristos if (ARMul_ReadWord (state, ARMul_GetPC (state)) == 0)
33798b9484cSchristos {
33898b9484cSchristos /* No vector has been installed. Rather than simulating whatever
33998b9484cSchristos random bits might happen to be at address 0x20 onwards we elect
34098b9484cSchristos to stop. */
34198b9484cSchristos switch (vector)
34298b9484cSchristos {
34398b9484cSchristos case ARMul_ResetV: state->EndCondition = RDIError_Reset; break;
34498b9484cSchristos case ARMul_UndefinedInstrV: state->EndCondition = RDIError_UndefinedInstruction; break;
34598b9484cSchristos case ARMul_SWIV: state->EndCondition = RDIError_SoftwareInterrupt; break;
34698b9484cSchristos case ARMul_PrefetchAbortV: state->EndCondition = RDIError_PrefetchAbort; break;
34798b9484cSchristos case ARMul_DataAbortV: state->EndCondition = RDIError_DataAbort; break;
34898b9484cSchristos case ARMul_AddrExceptnV: state->EndCondition = RDIError_AddressException; break;
34998b9484cSchristos case ARMul_IRQV: state->EndCondition = RDIError_IRQ; break;
35098b9484cSchristos case ARMul_FIQV: state->EndCondition = RDIError_FIQ; break;
35198b9484cSchristos default: break;
35298b9484cSchristos }
35398b9484cSchristos state->Emulate = FALSE;
35498b9484cSchristos }
35598b9484cSchristos }
356