Home | History | Annotate | Line # | Download | only in arm
arminit.c revision 1.1.1.3
      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.1.2  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.1.2  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.1.2  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.1.3  christos /* The PC pipeline value depends on whether ARM
     44  1.1.1.3  christos    or Thumb instructions are being executed.  */
     45  1.1.1.3  christos ARMword isize;
     46  1.1.1.3  christos 
     47      1.1  christos /***************************************************************************\
     48      1.1  christos *         Call this routine once to set up the emulator's tables.           *
     49      1.1  christos \***************************************************************************/
     50      1.1  christos 
     51      1.1  christos void
     52      1.1  christos ARMul_EmulateInit (void)
     53      1.1  christos {
     54      1.1  christos   unsigned long i, j;
     55      1.1  christos 
     56      1.1  christos   for (i = 0; i < 4096; i++)
     57      1.1  christos     {				/* the values of 12 bit dp rhs's */
     58      1.1  christos       ARMul_ImmedTable[i] = ROTATER (i & 0xffL, (i >> 7L) & 0x1eL);
     59      1.1  christos     }
     60      1.1  christos 
     61      1.1  christos   for (i = 0; i < 256; ARMul_BitList[i++] = 0);	/* how many bits in LSM */
     62      1.1  christos   for (j = 1; j < 256; j <<= 1)
     63      1.1  christos     for (i = 0; i < 256; i++)
     64      1.1  christos       if ((i & j) > 0)
     65      1.1  christos 	ARMul_BitList[i]++;
     66      1.1  christos 
     67      1.1  christos   for (i = 0; i < 256; i++)
     68      1.1  christos     ARMul_BitList[i] *= 4;	/* you always need 4 times these values */
     69      1.1  christos 
     70      1.1  christos }
     71      1.1  christos 
     72      1.1  christos /***************************************************************************\
     73      1.1  christos *            Returns a new instantiation of the ARMulator's state           *
     74      1.1  christos \***************************************************************************/
     75      1.1  christos 
     76      1.1  christos ARMul_State *
     77      1.1  christos ARMul_NewState (void)
     78      1.1  christos {
     79      1.1  christos   ARMul_State *state;
     80      1.1  christos   unsigned i, j;
     81      1.1  christos 
     82      1.1  christos   state = (ARMul_State *) malloc (sizeof (ARMul_State));
     83      1.1  christos   memset (state, 0, sizeof (ARMul_State));
     84      1.1  christos 
     85      1.1  christos   state->Emulate = RUN;
     86      1.1  christos   for (i = 0; i < 16; i++)
     87      1.1  christos     {
     88      1.1  christos       state->Reg[i] = 0;
     89      1.1  christos       for (j = 0; j < 7; j++)
     90      1.1  christos 	state->RegBank[j][i] = 0;
     91      1.1  christos     }
     92      1.1  christos   for (i = 0; i < 7; i++)
     93      1.1  christos     state->Spsr[i] = 0;
     94      1.1  christos 
     95      1.1  christos   /* state->Mode = USER26MODE;  */
     96      1.1  christos   state->Mode = USER32MODE;
     97      1.1  christos 
     98      1.1  christos   state->CallDebug = FALSE;
     99      1.1  christos   state->Debug = FALSE;
    100      1.1  christos   state->VectorCatch = 0;
    101      1.1  christos   state->Aborted = FALSE;
    102      1.1  christos   state->Reseted = FALSE;
    103      1.1  christos   state->Inted = 3;
    104      1.1  christos   state->LastInted = 3;
    105      1.1  christos 
    106      1.1  christos   state->MemDataPtr = NULL;
    107      1.1  christos   state->MemInPtr = NULL;
    108      1.1  christos   state->MemOutPtr = NULL;
    109      1.1  christos   state->MemSparePtr = NULL;
    110      1.1  christos   state->MemSize = 0;
    111      1.1  christos 
    112      1.1  christos   state->OSptr = NULL;
    113      1.1  christos   state->CommandLine = NULL;
    114      1.1  christos 
    115      1.1  christos   state->CP14R0_CCD = -1;
    116      1.1  christos   state->LastTime = 0;
    117      1.1  christos 
    118      1.1  christos   state->EventSet = 0;
    119      1.1  christos   state->Now = 0;
    120      1.1  christos   state->EventPtr = (struct EventNode **) malloc ((unsigned) EVENTLISTSIZE *
    121      1.1  christos 						  sizeof (struct EventNode
    122      1.1  christos 							  *));
    123      1.1  christos   for (i = 0; i < EVENTLISTSIZE; i++)
    124      1.1  christos     *(state->EventPtr + i) = NULL;
    125      1.1  christos 
    126      1.1  christos   state->prog32Sig = HIGH;
    127      1.1  christos   state->data32Sig = HIGH;
    128      1.1  christos 
    129      1.1  christos   state->lateabtSig = LOW;
    130      1.1  christos   state->bigendSig = LOW;
    131      1.1  christos 
    132      1.1  christos   state->is_v4 = LOW;
    133      1.1  christos   state->is_v5 = LOW;
    134      1.1  christos   state->is_v5e = LOW;
    135      1.1  christos   state->is_XScale = LOW;
    136      1.1  christos   state->is_iWMMXt = LOW;
    137      1.1  christos   state->is_v6 = LOW;
    138      1.1  christos 
    139      1.1  christos   ARMul_Reset (state);
    140      1.1  christos 
    141      1.1  christos   return state;
    142      1.1  christos }
    143      1.1  christos 
    144      1.1  christos /***************************************************************************\
    145      1.1  christos   Call this routine to set ARMulator to model certain processor properities
    146      1.1  christos \***************************************************************************/
    147      1.1  christos 
    148      1.1  christos void
    149      1.1  christos ARMul_SelectProcessor (ARMul_State * state, unsigned properties)
    150      1.1  christos {
    151      1.1  christos   if (properties & ARM_Fix26_Prop)
    152      1.1  christos     {
    153      1.1  christos       state->prog32Sig = LOW;
    154      1.1  christos       state->data32Sig = LOW;
    155      1.1  christos     }
    156      1.1  christos   else
    157      1.1  christos     {
    158      1.1  christos       state->prog32Sig = HIGH;
    159      1.1  christos       state->data32Sig = HIGH;
    160      1.1  christos     }
    161      1.1  christos 
    162      1.1  christos   state->lateabtSig = LOW;
    163      1.1  christos 
    164      1.1  christos   state->is_v4 = (properties & (ARM_v4_Prop | ARM_v5_Prop)) ? HIGH : LOW;
    165      1.1  christos   state->is_v5 = (properties & ARM_v5_Prop) ? HIGH : LOW;
    166      1.1  christos   state->is_v5e = (properties & ARM_v5e_Prop) ? HIGH : LOW;
    167      1.1  christos   state->is_XScale = (properties & ARM_XScale_Prop) ? HIGH : LOW;
    168      1.1  christos   state->is_iWMMXt = (properties & ARM_iWMMXt_Prop) ? HIGH : LOW;
    169      1.1  christos   state->is_ep9312 = (properties & ARM_ep9312_Prop) ? HIGH : LOW;
    170      1.1  christos   state->is_v6 = (properties & ARM_v6_Prop) ? HIGH : LOW;
    171      1.1  christos 
    172      1.1  christos   /* Only initialse the coprocessor support once we
    173      1.1  christos      know what kind of chip we are dealing with.  */
    174      1.1  christos   ARMul_CoProInit (state);
    175      1.1  christos }
    176      1.1  christos 
    177      1.1  christos /***************************************************************************\
    178      1.1  christos * Call this routine to set up the initial machine state (or perform a RESET *
    179      1.1  christos \***************************************************************************/
    180      1.1  christos 
    181      1.1  christos void
    182      1.1  christos ARMul_Reset (ARMul_State * state)
    183      1.1  christos {
    184      1.1  christos   state->NextInstr = 0;
    185      1.1  christos 
    186      1.1  christos   if (state->prog32Sig)
    187      1.1  christos     {
    188      1.1  christos       state->Reg[15] = 0;
    189      1.1  christos       state->Cpsr = INTBITS | SVC32MODE;
    190      1.1  christos       state->Mode = SVC32MODE;
    191      1.1  christos     }
    192      1.1  christos   else
    193      1.1  christos     {
    194      1.1  christos       state->Reg[15] = R15INTBITS | SVC26MODE;
    195      1.1  christos       state->Cpsr = INTBITS | SVC26MODE;
    196      1.1  christos       state->Mode = SVC26MODE;
    197      1.1  christos     }
    198      1.1  christos 
    199      1.1  christos   ARMul_CPSRAltered (state);
    200      1.1  christos   state->Bank = SVCBANK;
    201      1.1  christos 
    202      1.1  christos   FLUSHPIPE;
    203      1.1  christos 
    204      1.1  christos   state->EndCondition = 0;
    205      1.1  christos 
    206      1.1  christos   state->Exception = FALSE;
    207      1.1  christos   state->NresetSig = HIGH;
    208      1.1  christos   state->NfiqSig = HIGH;
    209      1.1  christos   state->NirqSig = HIGH;
    210      1.1  christos   state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
    211      1.1  christos   state->abortSig = LOW;
    212      1.1  christos   state->AbortAddr = 1;
    213      1.1  christos 
    214      1.1  christos   state->NumInstrs = 0;
    215      1.1  christos   state->NumNcycles = 0;
    216      1.1  christos   state->NumScycles = 0;
    217      1.1  christos   state->NumIcycles = 0;
    218      1.1  christos   state->NumCcycles = 0;
    219      1.1  christos   state->NumFcycles = 0;
    220      1.1  christos #ifdef ASIM
    221      1.1  christos   (void) ARMul_MemoryInit ();
    222      1.1  christos   ARMul_OSInit (state);
    223      1.1  christos #endif
    224      1.1  christos }
    225      1.1  christos 
    226      1.1  christos 
    227      1.1  christos /***************************************************************************\
    228      1.1  christos * Emulate the execution of an entire program.  Start the correct emulator   *
    229      1.1  christos * (Emulate26 for a 26 bit ARM and Emulate32 for a 32 bit ARM), return the   *
    230      1.1  christos * address of the last instruction that is executed.                         *
    231      1.1  christos \***************************************************************************/
    232      1.1  christos 
    233      1.1  christos ARMword
    234      1.1  christos ARMul_DoProg (ARMul_State * state)
    235      1.1  christos {
    236      1.1  christos   ARMword pc = 0;
    237      1.1  christos 
    238      1.1  christos   state->Emulate = RUN;
    239      1.1  christos   while (state->Emulate != STOP)
    240      1.1  christos     {
    241      1.1  christos       state->Emulate = RUN;
    242      1.1  christos       if (state->prog32Sig && ARMul_MODE32BIT)
    243      1.1  christos 	pc = ARMul_Emulate32 (state);
    244      1.1  christos       else
    245      1.1  christos 	pc = ARMul_Emulate26 (state);
    246      1.1  christos     }
    247      1.1  christos   return (pc);
    248      1.1  christos }
    249      1.1  christos 
    250      1.1  christos /***************************************************************************\
    251      1.1  christos * Emulate the execution of one instruction.  Start the correct emulator     *
    252      1.1  christos * (Emulate26 for a 26 bit ARM and Emulate32 for a 32 bit ARM), return the   *
    253      1.1  christos * address of the instruction that is executed.                              *
    254      1.1  christos \***************************************************************************/
    255      1.1  christos 
    256      1.1  christos ARMword
    257      1.1  christos ARMul_DoInstr (ARMul_State * state)
    258      1.1  christos {
    259      1.1  christos   ARMword pc = 0;
    260      1.1  christos 
    261      1.1  christos   state->Emulate = ONCE;
    262      1.1  christos   if (state->prog32Sig && ARMul_MODE32BIT)
    263      1.1  christos     pc = ARMul_Emulate32 (state);
    264      1.1  christos   else
    265      1.1  christos     pc = ARMul_Emulate26 (state);
    266      1.1  christos 
    267      1.1  christos   return (pc);
    268      1.1  christos }
    269      1.1  christos 
    270      1.1  christos /***************************************************************************\
    271      1.1  christos * This routine causes an Abort to occur, including selecting the correct    *
    272      1.1  christos * mode, register bank, and the saving of registers.  Call with the          *
    273      1.1  christos * appropriate vector's memory address (0,4,8 ....)                          *
    274      1.1  christos \***************************************************************************/
    275      1.1  christos 
    276      1.1  christos void
    277      1.1  christos ARMul_Abort (ARMul_State * state, ARMword vector)
    278      1.1  christos {
    279      1.1  christos   ARMword temp;
    280      1.1  christos   int isize = INSN_SIZE;
    281      1.1  christos   int esize = (TFLAG ? 0 : 4);
    282      1.1  christos   int e2size = (TFLAG ? -4 : 0);
    283      1.1  christos 
    284      1.1  christos   state->Aborted = FALSE;
    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