arminit.c 10 KB

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