arminit.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  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 2 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, write to the Free Software
  13. Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  14. //#include <unistd.h>
  15. #include "core/arm/skyeye_common/armdefs.h"
  16. #include "core/arm/skyeye_common/armemu.h"
  17. /***************************************************************************\
  18. * Definitions for the emulator architecture *
  19. \***************************************************************************/
  20. void ARMul_EmulateInit (void);
  21. ARMul_State *ARMul_NewState (ARMul_State * state);
  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. //chy 2006-02-22 add test debugmode
  35. extern int debugmode;
  36. extern int remote_interrupt( void );
  37. void arm_dyncom_Abort(ARMul_State * state, ARMword vector)
  38. {
  39. ARMul_Abort(state, vector);
  40. }
  41. /* ahe-ykl : the following code to initialize user mode
  42. code is architecture dependent and probably model dependant. */
  43. /*#include "skyeye_arch.h"
  44. #include "skyeye_pref.h"
  45. #include "skyeye_exec_info.h"
  46. #include "bank_defs.h"*/
  47. //#include "armcpu.h"
  48. //#include "skyeye_callback.h"
  49. /*
  50. ARM_CPU_State* cpu = get_current_cpu();
  51. arm_core_t* core = &cpu->core[0];
  52. uint32_t sp = info->initial_sp;
  53. core->Cpsr = 0x10; // User mode
  54. // FIXME: may need to add thumb
  55. core->Reg[13] = sp;
  56. core->Reg[10] = info->start_data;
  57. core->Reg[0] = 0;
  58. bus_read(32, sp + 4, &(core->Reg[1]));
  59. bus_read(32, sp + 8, &(core->Reg[2]));
  60. */
  61. /***************************************************************************\
  62. * Call this routine once to set up the emulator's tables. *
  63. \***************************************************************************/
  64. void
  65. ARMul_EmulateInit (void)
  66. {
  67. unsigned int i, j;
  68. for (i = 0; i < 4096; i++) { /* the values of 12 bit dp rhs's */
  69. ARMul_ImmedTable[i] = ROTATER (i & 0xffL, (i >> 7L) & 0x1eL);
  70. }
  71. for (i = 0; i < 256; ARMul_BitList[i++] = 0); /* how many bits in LSM */
  72. for (j = 1; j < 256; j <<= 1)
  73. for (i = 0; i < 256; i++)
  74. if ((i & j) > 0)
  75. ARMul_BitList[i]++;
  76. for (i = 0; i < 256; i++)
  77. ARMul_BitList[i] *= 4; /* you always need 4 times these values */
  78. }
  79. /***************************************************************************\
  80. * Returns a new instantiation of the ARMulator's state *
  81. \***************************************************************************/
  82. ARMul_State *
  83. ARMul_NewState (ARMul_State *state)
  84. {
  85. unsigned i, j;
  86. memset (state, 0, sizeof (ARMul_State));
  87. state->Emulate = RUN;
  88. for (i = 0; i < 16; i++) {
  89. state->Reg[i] = 0;
  90. for (j = 0; j < 7; j++)
  91. state->RegBank[j][i] = 0;
  92. }
  93. for (i = 0; i < 7; i++)
  94. state->Spsr[i] = 0;
  95. state->Mode = 0;
  96. state->CallDebug = FALSE;
  97. state->Debug = FALSE;
  98. state->VectorCatch = 0;
  99. state->Aborted = FALSE;
  100. state->Reseted = FALSE;
  101. state->Inted = 3;
  102. state->LastInted = 3;
  103. state->CommandLine = NULL;
  104. state->EventSet = 0;
  105. state->Now = 0;
  106. state->EventPtr =
  107. (struct EventNode **) malloc ((unsigned) EVENTLISTSIZE *
  108. sizeof (struct EventNode *));
  109. #if DIFF_STATE
  110. state->state_log = fopen("/data/state.log", "w");
  111. printf("create pc log file.\n");
  112. #endif
  113. if (state->EventPtr == NULL) {
  114. printf ("SKYEYE: ARMul_NewState malloc state->EventPtr error\n");
  115. exit(-1);
  116. //skyeye_exit (-1);
  117. }
  118. for (i = 0; i < EVENTLISTSIZE; i++)
  119. *(state->EventPtr + i) = NULL;
  120. #if SAVE_LOG
  121. state->state_log = fopen("/tmp/state.log", "w");
  122. printf("create pc log file.\n");
  123. #else
  124. #if DIFF_LOG
  125. state->state_log = fopen("/tmp/state.log", "r");
  126. printf("loaded pc log file.\n");
  127. #endif
  128. #endif
  129. #ifdef ARM61
  130. state->prog32Sig = LOW;
  131. state->data32Sig = LOW;
  132. #else
  133. state->prog32Sig = HIGH;
  134. state->data32Sig = HIGH;
  135. #endif
  136. state->lateabtSig = HIGH;
  137. state->bigendSig = LOW;
  138. //chy:2003-08-19
  139. state->LastTime = 0;
  140. state->CP14R0_CCD = -1;
  141. /* ahe-ykl: common function for interpret and dyncom */
  142. /*sky_pref_t *pref = get_skyeye_pref();
  143. if (pref->user_mode_sim)
  144. register_callback(arm_user_mode_init, Bootmach_callback);
  145. */
  146. memset(&state->exclusive_tag_array[0], 0xFF, sizeof(state->exclusive_tag_array[0]) * 128);
  147. state->exclusive_access_state = 0;
  148. //state->cpu = (cpu_config_t *) malloc (sizeof (cpu_config_t));
  149. //state->mem_bank = (mem_config_t *) malloc (sizeof (mem_config_t));
  150. return (state);
  151. }
  152. /***************************************************************************\
  153. * Call this routine to set ARMulator to model a certain processor *
  154. \***************************************************************************/
  155. void
  156. ARMul_SelectProcessor (ARMul_State * state, unsigned properties)
  157. {
  158. if (properties & ARM_Fix26_Prop) {
  159. state->prog32Sig = LOW;
  160. state->data32Sig = LOW;
  161. } else {
  162. state->prog32Sig = HIGH;
  163. state->data32Sig = HIGH;
  164. }
  165. /* 2004-05-09 chy
  166. below line sould be in skyeye_mach_XXX.c 's XXX_mach_init function
  167. */
  168. // state->lateabtSig = HIGH;
  169. state->is_v4 =
  170. (properties & (ARM_v4_Prop | ARM_v5_Prop)) ? HIGH : LOW;
  171. state->is_v5 = (properties & ARM_v5_Prop) ? HIGH : LOW;
  172. state->is_v5e = (properties & ARM_v5e_Prop) ? HIGH : LOW;
  173. state->is_XScale = (properties & ARM_XScale_Prop) ? HIGH : LOW;
  174. state->is_iWMMXt = (properties & ARM_iWMMXt_Prop) ? HIGH : LOW;
  175. /* state->is_v6 = LOW */;
  176. /* jeff.du 2010-08-05 */
  177. state->is_v6 = (properties & ARM_v6_Prop) ? HIGH : LOW;
  178. state->is_ep9312 = (properties & ARM_ep9312_Prop) ? HIGH : LOW;
  179. //chy 2005-09-19
  180. state->is_pxa27x = (properties & ARM_PXA27X_Prop) ? HIGH : LOW;
  181. /* shenoubang 2012-3-11 */
  182. state->is_v7 = (properties & ARM_v7_Prop) ? HIGH : LOW;
  183. /* Only initialse the coprocessor support once we
  184. know what kind of chip we are dealing with. */
  185. //ARMul_CoProInit (state);
  186. }
  187. /***************************************************************************\
  188. * Call this routine to set up the initial machine state (or perform a RESET *
  189. \***************************************************************************/
  190. void
  191. ARMul_Reset (ARMul_State * state)
  192. {
  193. //fprintf(stderr,"armul_reset 0: state-> Cpsr 0x%x, Mode %d\n",state->Cpsr,state->Mode);
  194. state->NextInstr = 0;
  195. if (state->prog32Sig) {
  196. state->Reg[15] = 0;
  197. state->Cpsr = INTBITS | SVC32MODE;
  198. state->Mode = SVC32MODE;
  199. } else {
  200. state->Reg[15] = R15INTBITS | SVC26MODE;
  201. state->Cpsr = INTBITS | SVC26MODE;
  202. state->Mode = SVC26MODE;
  203. }
  204. //fprintf(stderr,"armul_reset 1: state-> Cpsr 0x%x, Mode %d\n",state->Cpsr,state->Mode);
  205. //ARMul_CPSRAltered (state);
  206. state->Bank = SVCBANK;
  207. FLUSHPIPE;
  208. state->EndCondition = 0;
  209. state->ErrorCode = 0;
  210. //fprintf(stderr,"armul_reset 2: state-> Cpsr 0x%x, Mode %d\n",state->Cpsr,state->Mode);
  211. state->NresetSig = HIGH;
  212. state->NfiqSig = HIGH;
  213. state->NirqSig = HIGH;
  214. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  215. state->abortSig = LOW;
  216. state->AbortAddr = 1;
  217. state->NumInstrs = 0;
  218. state->NumNcycles = 0;
  219. state->NumScycles = 0;
  220. state->NumIcycles = 0;
  221. state->NumCcycles = 0;
  222. state->NumFcycles = 0;
  223. //fprintf(stderr,"armul_reset 3: state-> Cpsr 0x%x, Mode %d\n",state->Cpsr,state->Mode);
  224. //mmu_reset (state);
  225. //fprintf(stderr,"armul_reset 4: state-> Cpsr 0x%x, Mode %d\n",state->Cpsr,state->Mode);
  226. //mem_reset (state); /* move to memory/ram.c */
  227. //fprintf(stderr,"armul_reset 5: state-> Cpsr 0x%x, Mode %d\n",state->Cpsr,state->Mode);
  228. /*remove later. walimis 03.7.17 */
  229. //io_reset(state);
  230. //lcd_disable(state);
  231. /*ywc 2005-04-07 move from ARMul_NewState , because skyeye_config.no_dbct will
  232. *be configured in skyeye_option_init and it is called after ARMul_NewState*/
  233. state->tea_break_ok = 0;
  234. state->tea_break_addr = 0;
  235. state->tea_pc = 0;
  236. #ifdef DBCT
  237. if (!skyeye_config.no_dbct) {
  238. //teawater add for arm2x86 2005.02.14-------------------------------------------
  239. if (arm2x86_init (state)) {
  240. printf ("SKYEYE: arm2x86_init error\n");
  241. //skyeye_exit (-1);
  242. }
  243. //AJ2D--------------------------------------------------------------------------
  244. }
  245. #endif
  246. }
  247. /***************************************************************************\
  248. * Emulate the execution of an entire program. Start the correct emulator *
  249. * (Emulate26 for a 26 bit ARM and Emulate32 for a 32 bit ARM), return the *
  250. * address of the last instruction that is executed. *
  251. \***************************************************************************/
  252. //teawater add DBCT_TEST_SPEED 2005.10.04---------------------------------------
  253. #ifdef DBCT_TEST_SPEED
  254. static ARMul_State *dbct_test_speed_state = NULL;
  255. static void
  256. dbct_test_speed_sig(int signo)
  257. {
  258. printf("\n0x%llx %llu\n", dbct_test_speed_state->instr_count, dbct_test_speed_state->instr_count);
  259. exit(0);
  260. //skyeye_exit(0);
  261. }
  262. #endif //DBCT_TEST_SPEED
  263. //AJ2D--------------------------------------------------------------------------
  264. ARMword
  265. ARMul_DoProg (ARMul_State * state)
  266. {
  267. ARMword pc = 0;
  268. /*
  269. * 2007-01-24 removed the term-io functions by Anthony Lee,
  270. * moved to "device/uart/skyeye_uart_stdio.c".
  271. */
  272. //teawater add DBCT_TEST_SPEED 2005.10.04---------------------------------------
  273. #ifdef DBCT_TEST_SPEED
  274. {
  275. if (!dbct_test_speed_state) {
  276. //init timer
  277. struct itimerval value;
  278. struct sigaction act;
  279. dbct_test_speed_state = state;
  280. state->instr_count = 0;
  281. act.sa_handler = dbct_test_speed_sig;
  282. act.sa_flags = SA_RESTART;
  283. //cygwin don't support ITIMER_VIRTUAL or ITIMER_PROF
  284. #ifndef __CYGWIN__
  285. if (sigaction(SIGVTALRM, &act, NULL) == -1) {
  286. #else
  287. if (sigaction(SIGALRM, &act, NULL) == -1) {
  288. #endif //__CYGWIN__
  289. fprintf(stderr, "init timer error.\n");
  290. exit(-1);
  291. //skyeye_exit(-1);
  292. }
  293. if (skyeye_config.dbct_test_speed_sec) {
  294. value.it_value.tv_sec = skyeye_config.dbct_test_speed_sec;
  295. } else {
  296. value.it_value.tv_sec = DBCT_TEST_SPEED_SEC;
  297. }
  298. printf("dbct_test_speed_sec = %ld\n", value.it_value.tv_sec);
  299. value.it_value.tv_usec = 0;
  300. value.it_interval.tv_sec = 0;
  301. value.it_interval.tv_usec = 0;
  302. #ifndef __CYGWIN__
  303. if (setitimer(ITIMER_VIRTUAL, &value, NULL) == -1) {
  304. #else
  305. if (setitimer(ITIMER_REAL, &value, NULL) == -1) {
  306. #endif //__CYGWIN__
  307. fprintf(stderr, "init timer error.\n");
  308. //skyeye_exit(-1);
  309. }
  310. }
  311. }
  312. #endif //DBCT_TEST_SPEED
  313. //AJ2D--------------------------------------------------------------------------
  314. state->Emulate = RUN;
  315. while (state->Emulate != STOP) {
  316. state->Emulate = RUN;
  317. /*ywc 2005-03-31 */
  318. if (state->prog32Sig && ARMul_MODE32BIT) {
  319. #ifdef DBCT
  320. if (skyeye_config.no_dbct) {
  321. pc = ARMul_Emulate32 (state);
  322. } else {
  323. pc = ARMul_Emulate32_dbct (state);
  324. }
  325. #else
  326. pc = ARMul_Emulate32 (state);
  327. #endif
  328. }
  329. else {
  330. //pc = ARMul_Emulate26 (state);
  331. }
  332. //chy 2006-02-22, should test debugmode first
  333. //chy 2006-04-14, put below codes in ARMul_Emulate
  334. #if 0
  335. if(debugmode)
  336. if(remote_interrupt())
  337. state->Emulate = STOP;
  338. #endif
  339. }
  340. /*
  341. * 2007-01-24 removed the term-io functions by Anthony Lee,
  342. * moved to "device/uart/skyeye_uart_stdio.c".
  343. */
  344. return (pc);
  345. }
  346. /***************************************************************************\
  347. * Emulate the execution of one instruction. Start the correct emulator *
  348. * (Emulate26 for a 26 bit ARM and Emulate32 for a 32 bit ARM), return the *
  349. * address of the instruction that is executed. *
  350. \***************************************************************************/
  351. ARMword
  352. ARMul_DoInstr (ARMul_State * state)
  353. {
  354. ARMword pc = 0;
  355. state->Emulate = ONCE;
  356. /*ywc 2005-03-31 */
  357. if (state->prog32Sig && ARMul_MODE32BIT) {
  358. #ifdef DBCT
  359. if (skyeye_config.no_dbct) {
  360. pc = ARMul_Emulate32 (state);
  361. } else {
  362. //teawater add compile switch for DBCT GDB RSP function 2005.10.21--------------
  363. #ifndef DBCT_GDBRSP
  364. printf("DBCT GDBRSP function switch is off.\n");
  365. printf("To use this function, open \"#define DBCT_GDBRSP\" in arch/arm/common/armdefs.h & recompile skyeye.\n");
  366. skyeye_exit(-1);
  367. #endif //DBCT_GDBRSP
  368. //AJ2D--------------------------------------------------------------------------
  369. pc = ARMul_Emulate32_dbct (state);
  370. }
  371. #else
  372. pc = ARMul_Emulate32 (state);
  373. #endif
  374. }
  375. //else
  376. //pc = ARMul_Emulate26 (state);
  377. return (pc);
  378. }
  379. /***************************************************************************\
  380. * This routine causes an Abort to occur, including selecting the correct *
  381. * mode, register bank, and the saving of registers. Call with the *
  382. * appropriate vector's memory address (0,4,8 ....) *
  383. \***************************************************************************/
  384. void
  385. ARMul_Abort (ARMul_State * state, ARMword vector)
  386. {
  387. ARMword temp;
  388. int isize = INSN_SIZE;
  389. int esize = (TFLAG ? 0 : 4);
  390. int e2size = (TFLAG ? -4 : 0);
  391. state->Aborted = FALSE;
  392. if (state->prog32Sig)
  393. if (ARMul_MODE26BIT)
  394. temp = R15PC;
  395. else
  396. temp = state->Reg[15];
  397. else
  398. temp = R15PC | ECC | ER15INT | EMODE;
  399. switch (vector) {
  400. case ARMul_ResetV: /* RESET */
  401. SETABORT (INTBITS, state->prog32Sig ? SVC32MODE : SVC26MODE,
  402. 0);
  403. break;
  404. case ARMul_UndefinedInstrV: /* Undefined Instruction */
  405. SETABORT (IBIT, state->prog32Sig ? UNDEF32MODE : SVC26MODE,
  406. isize);
  407. break;
  408. case ARMul_SWIV: /* Software Interrupt */
  409. SETABORT (IBIT, state->prog32Sig ? SVC32MODE : SVC26MODE,
  410. isize);
  411. break;
  412. case ARMul_PrefetchAbortV: /* Prefetch Abort */
  413. state->AbortAddr = 1;
  414. SETABORT (IBIT, state->prog32Sig ? ABORT32MODE : SVC26MODE,
  415. esize);
  416. break;
  417. case ARMul_DataAbortV: /* Data Abort */
  418. SETABORT (IBIT, state->prog32Sig ? ABORT32MODE : SVC26MODE,
  419. e2size);
  420. break;
  421. case ARMul_AddrExceptnV: /* Address Exception */
  422. SETABORT (IBIT, SVC26MODE, isize);
  423. break;
  424. case ARMul_IRQV: /* IRQ */
  425. //chy 2003-09-02 the if sentence seems no use
  426. #if 0
  427. if (!state->is_XScale || !state->CPRead[13] (state, 0, &temp)
  428. || (temp & ARMul_CP13_R0_IRQ))
  429. #endif
  430. SETABORT (IBIT,
  431. state->prog32Sig ? IRQ32MODE : IRQ26MODE,
  432. esize);
  433. break;
  434. case ARMul_FIQV: /* FIQ */
  435. //chy 2003-09-02 the if sentence seems no use
  436. #if 0
  437. if (!state->is_XScale || !state->CPRead[13] (state, 0, &temp)
  438. || (temp & ARMul_CP13_R0_FIQ))
  439. #endif
  440. SETABORT (INTBITS,
  441. state->prog32Sig ? FIQ32MODE : FIQ26MODE,
  442. esize);
  443. break;
  444. }
  445. if (ARMul_MODE32BIT) {
  446. /*if (state->mmu.control & CONTROL_VECTOR)
  447. vector += 0xffff0000; //for v4 high exception address*/
  448. if (state->vector_remap_flag)
  449. vector += state->vector_remap_addr; /* support some remap function in LPC processor */
  450. ARMul_SetR15 (state, vector);
  451. } else
  452. ARMul_SetR15 (state, R15CCINTMODE | vector);
  453. }