armsupp.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954
  1. /* armsupp.c -- ARMulator support code: 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 "armdefs.h"
  15. #include "armemu.h"
  16. //#include "ansidecl.h"
  17. #include "skyeye_defs.h"
  18. unsigned xscale_cp15_cp_access_allowed (ARMul_State * state, unsigned reg,
  19. unsigned cpnum);
  20. //extern int skyeye_instr_debug;
  21. /* Definitions for the support routines. */
  22. static ARMword ModeToBank (ARMword);
  23. static void EnvokeList (ARMul_State *, unsigned int, unsigned int);
  24. struct EventNode
  25. { /* An event list node. */
  26. unsigned (*func) (ARMul_State *); /* The function to call. */
  27. struct EventNode *next;
  28. };
  29. /* This routine returns the value of a register from a mode. */
  30. ARMword
  31. ARMul_GetReg (ARMul_State * state, unsigned mode, unsigned reg)
  32. {
  33. mode &= MODEBITS;
  34. if (mode != state->Mode)
  35. return (state->RegBank[ModeToBank ((ARMword) mode)][reg]);
  36. else
  37. return (state->Reg[reg]);
  38. }
  39. /* This routine sets the value of a register for a mode. */
  40. void
  41. ARMul_SetReg (ARMul_State * state, unsigned mode, unsigned reg, ARMword value)
  42. {
  43. mode &= MODEBITS;
  44. if (mode != state->Mode)
  45. state->RegBank[ModeToBank ((ARMword) mode)][reg] = value;
  46. else
  47. state->Reg[reg] = value;
  48. }
  49. /* This routine returns the value of the PC, mode independently. */
  50. ARMword
  51. ARMul_GetPC (ARMul_State * state)
  52. {
  53. if (state->Mode > SVC26MODE)
  54. return state->Reg[15];
  55. else
  56. return R15PC;
  57. }
  58. /* This routine returns the value of the PC, mode independently. */
  59. ARMword
  60. ARMul_GetNextPC (ARMul_State * state)
  61. {
  62. if (state->Mode > SVC26MODE)
  63. return state->Reg[15] + isize;
  64. else
  65. return (state->Reg[15] + isize) & R15PCBITS;
  66. }
  67. /* This routine sets the value of the PC. */
  68. void
  69. ARMul_SetPC (ARMul_State * state, ARMword value)
  70. {
  71. if (ARMul_MODE32BIT)
  72. state->Reg[15] = value & PCBITS;
  73. else
  74. state->Reg[15] = R15CCINTMODE | (value & R15PCBITS);
  75. FLUSHPIPE;
  76. }
  77. /* This routine returns the value of register 15, mode independently. */
  78. ARMword
  79. ARMul_GetR15 (ARMul_State * state)
  80. {
  81. if (state->Mode > SVC26MODE)
  82. return (state->Reg[15]);
  83. else
  84. return (R15PC | ECC | ER15INT | EMODE);
  85. }
  86. /* This routine sets the value of Register 15. */
  87. void
  88. ARMul_SetR15 (ARMul_State * state, ARMword value)
  89. {
  90. if (ARMul_MODE32BIT)
  91. state->Reg[15] = value & PCBITS;
  92. else {
  93. state->Reg[15] = value;
  94. ARMul_R15Altered (state);
  95. }
  96. FLUSHPIPE;
  97. }
  98. /* This routine returns the value of the CPSR. */
  99. ARMword
  100. ARMul_GetCPSR (ARMul_State * state)
  101. {
  102. //chy 2003-08-20: below is from gdb20030716, maybe isn't suitable for system simulator
  103. //return (CPSR | state->Cpsr); for gdb20030716
  104. // NOTE(bunnei): Changed this from [now] commented out macro "CPSR"
  105. return ((ECC | EINT | EMODE | (TFLAG << 5))); //had be tested in old skyeye with gdb5.0-5.3
  106. }
  107. /* This routine sets the value of the CPSR. */
  108. void
  109. ARMul_SetCPSR (ARMul_State * state, ARMword value)
  110. {
  111. state->Cpsr = value;
  112. ARMul_CPSRAltered (state);
  113. }
  114. /* This routine does all the nasty bits involved in a write to the CPSR,
  115. including updating the register bank, given a MSR instruction. */
  116. void
  117. ARMul_FixCPSR (ARMul_State * state, ARMword instr, ARMword rhs)
  118. {
  119. state->Cpsr = ARMul_GetCPSR (state);
  120. //chy 2006-02-16 , should not consider system mode, don't conside 26bit mode
  121. if (state->Mode != USER26MODE && state->Mode != USER32MODE ) {
  122. /* In user mode, only write flags. */
  123. if (BIT (16))
  124. SETPSR_C (state->Cpsr, rhs);
  125. if (BIT (17))
  126. SETPSR_X (state->Cpsr, rhs);
  127. if (BIT (18))
  128. SETPSR_S (state->Cpsr, rhs);
  129. }
  130. if (BIT (19))
  131. SETPSR_F (state->Cpsr, rhs);
  132. ARMul_CPSRAltered (state);
  133. }
  134. /* Get an SPSR from the specified mode. */
  135. ARMword
  136. ARMul_GetSPSR (ARMul_State * state, ARMword mode)
  137. {
  138. ARMword bank = ModeToBank (mode & MODEBITS);
  139. if (!BANK_CAN_ACCESS_SPSR (bank))
  140. return ARMul_GetCPSR (state);
  141. return state->Spsr[bank];
  142. }
  143. /* This routine does a write to an SPSR. */
  144. void
  145. ARMul_SetSPSR (ARMul_State * state, ARMword mode, ARMword value)
  146. {
  147. ARMword bank = ModeToBank (mode & MODEBITS);
  148. if (BANK_CAN_ACCESS_SPSR (bank))
  149. state->Spsr[bank] = value;
  150. }
  151. /* This routine does a write to the current SPSR, given an MSR instruction. */
  152. void
  153. ARMul_FixSPSR (ARMul_State * state, ARMword instr, ARMword rhs)
  154. {
  155. if (BANK_CAN_ACCESS_SPSR (state->Bank)) {
  156. if (BIT (16))
  157. SETPSR_C (state->Spsr[state->Bank], rhs);
  158. if (BIT (17))
  159. SETPSR_X (state->Spsr[state->Bank], rhs);
  160. if (BIT (18))
  161. SETPSR_S (state->Spsr[state->Bank], rhs);
  162. if (BIT (19))
  163. SETPSR_F (state->Spsr[state->Bank], rhs);
  164. }
  165. }
  166. /* This routine updates the state of the emulator after the Cpsr has been
  167. changed. Both the processor flags and register bank are updated. */
  168. void
  169. ARMul_CPSRAltered (ARMul_State * state)
  170. {
  171. ARMword oldmode;
  172. if (state->prog32Sig == LOW)
  173. state->Cpsr &= (CCBITS | INTBITS | R15MODEBITS);
  174. oldmode = state->Mode;
  175. if (state->Mode != (state->Cpsr & MODEBITS)) {
  176. state->Mode =
  177. ARMul_SwitchMode (state, state->Mode,
  178. state->Cpsr & MODEBITS);
  179. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  180. }
  181. //state->Cpsr &= ~MODEBITS;
  182. ASSIGNINT (state->Cpsr & INTBITS);
  183. //state->Cpsr &= ~INTBITS;
  184. ASSIGNN ((state->Cpsr & NBIT) != 0);
  185. //state->Cpsr &= ~NBIT;
  186. ASSIGNZ ((state->Cpsr & ZBIT) != 0);
  187. //state->Cpsr &= ~ZBIT;
  188. ASSIGNC ((state->Cpsr & CBIT) != 0);
  189. //state->Cpsr &= ~CBIT;
  190. ASSIGNV ((state->Cpsr & VBIT) != 0);
  191. //state->Cpsr &= ~VBIT;
  192. ASSIGNS ((state->Cpsr & SBIT) != 0);
  193. //state->Cpsr &= ~SBIT;
  194. #ifdef MODET
  195. ASSIGNT ((state->Cpsr & TBIT) != 0);
  196. //state->Cpsr &= ~TBIT;
  197. #endif
  198. if (oldmode > SVC26MODE) {
  199. if (state->Mode <= SVC26MODE) {
  200. state->Emulate = CHANGEMODE;
  201. state->Reg[15] = ECC | ER15INT | EMODE | R15PC;
  202. }
  203. }
  204. else {
  205. if (state->Mode > SVC26MODE) {
  206. state->Emulate = CHANGEMODE;
  207. state->Reg[15] = R15PC;
  208. }
  209. else
  210. state->Reg[15] = ECC | ER15INT | EMODE | R15PC;
  211. }
  212. }
  213. /* This routine updates the state of the emulator after register 15 has
  214. been changed. Both the processor flags and register bank are updated.
  215. This routine should only be called from a 26 bit mode. */
  216. void
  217. ARMul_R15Altered (ARMul_State * state)
  218. {
  219. if (state->Mode != R15MODE) {
  220. state->Mode = ARMul_SwitchMode (state, state->Mode, R15MODE);
  221. state->NtransSig = (state->Mode & 3) ? HIGH : LOW;
  222. }
  223. if (state->Mode > SVC26MODE)
  224. state->Emulate = CHANGEMODE;
  225. ASSIGNR15INT (R15INT);
  226. ASSIGNN ((state->Reg[15] & NBIT) != 0);
  227. ASSIGNZ ((state->Reg[15] & ZBIT) != 0);
  228. ASSIGNC ((state->Reg[15] & CBIT) != 0);
  229. ASSIGNV ((state->Reg[15] & VBIT) != 0);
  230. }
  231. /* This routine controls the saving and restoring of registers across mode
  232. changes. The regbank matrix is largely unused, only rows 13 and 14 are
  233. used across all modes, 8 to 14 are used for FIQ, all others use the USER
  234. column. It's easier this way. old and new parameter are modes numbers.
  235. Notice the side effect of changing the Bank variable. */
  236. ARMword
  237. ARMul_SwitchMode (ARMul_State * state, ARMword oldmode, ARMword newmode)
  238. {
  239. unsigned i;
  240. ARMword oldbank;
  241. ARMword newbank;
  242. static int revision_value = 53;
  243. oldbank = ModeToBank (oldmode);
  244. newbank = state->Bank = ModeToBank (newmode);
  245. /* Do we really need to do it? */
  246. if (oldbank != newbank) {
  247. if (oldbank == 3 && newbank == 2) {
  248. //printf("icounter is %d PC is %x MODE CHANGED : %d --> %d\n", state->NumInstrs, state->pc, oldbank, newbank);
  249. if (state->NumInstrs >= 5832487) {
  250. // printf("%d, ", state->NumInstrs + revision_value);
  251. // printf("revision_value : %d\n", revision_value);
  252. revision_value ++;
  253. }
  254. }
  255. /* Save away the old registers. */
  256. switch (oldbank) {
  257. case USERBANK:
  258. case IRQBANK:
  259. case SVCBANK:
  260. case ABORTBANK:
  261. case UNDEFBANK:
  262. if (newbank == FIQBANK)
  263. for (i = 8; i < 13; i++)
  264. state->RegBank[USERBANK][i] =
  265. state->Reg[i];
  266. state->RegBank[oldbank][13] = state->Reg[13];
  267. state->RegBank[oldbank][14] = state->Reg[14];
  268. break;
  269. case FIQBANK:
  270. for (i = 8; i < 15; i++)
  271. state->RegBank[FIQBANK][i] = state->Reg[i];
  272. break;
  273. case DUMMYBANK:
  274. for (i = 8; i < 15; i++)
  275. state->RegBank[DUMMYBANK][i] = 0;
  276. break;
  277. default:
  278. abort ();
  279. }
  280. /* Restore the new registers. */
  281. switch (newbank) {
  282. case USERBANK:
  283. case IRQBANK:
  284. case SVCBANK:
  285. case ABORTBANK:
  286. case UNDEFBANK:
  287. if (oldbank == FIQBANK)
  288. for (i = 8; i < 13; i++)
  289. state->Reg[i] =
  290. state->RegBank[USERBANK][i];
  291. state->Reg[13] = state->RegBank[newbank][13];
  292. state->Reg[14] = state->RegBank[newbank][14];
  293. break;
  294. case FIQBANK:
  295. for (i = 8; i < 15; i++)
  296. state->Reg[i] = state->RegBank[FIQBANK][i];
  297. break;
  298. case DUMMYBANK:
  299. for (i = 8; i < 15; i++)
  300. state->Reg[i] = 0;
  301. break;
  302. default:
  303. abort ();
  304. }
  305. }
  306. return newmode;
  307. }
  308. /* Given a processor mode, this routine returns the
  309. register bank that will be accessed in that mode. */
  310. static ARMword
  311. ModeToBank (ARMword mode)
  312. {
  313. static ARMword bankofmode[] = {
  314. USERBANK, FIQBANK, IRQBANK, SVCBANK,
  315. DUMMYBANK, DUMMYBANK, DUMMYBANK, DUMMYBANK,
  316. DUMMYBANK, DUMMYBANK, DUMMYBANK, DUMMYBANK,
  317. DUMMYBANK, DUMMYBANK, DUMMYBANK, DUMMYBANK,
  318. USERBANK, FIQBANK, IRQBANK, SVCBANK,
  319. DUMMYBANK, DUMMYBANK, DUMMYBANK, ABORTBANK,
  320. DUMMYBANK, DUMMYBANK, DUMMYBANK, UNDEFBANK,
  321. DUMMYBANK, DUMMYBANK, DUMMYBANK, SYSTEMBANK
  322. };
  323. if (mode >= (sizeof (bankofmode) / sizeof (bankofmode[0])))
  324. return DUMMYBANK;
  325. return bankofmode[mode];
  326. }
  327. /* Returns the register number of the nth register in a reg list. */
  328. unsigned
  329. ARMul_NthReg (ARMword instr, unsigned number)
  330. {
  331. unsigned bit, upto;
  332. for (bit = 0, upto = 0; upto <= number; bit++)
  333. if (BIT (bit))
  334. upto++;
  335. return (bit - 1);
  336. }
  337. /* Assigns the N and Z flags depending on the value of result. */
  338. void
  339. ARMul_NegZero (ARMul_State * state, ARMword result)
  340. {
  341. if (NEG (result)) {
  342. SETN;
  343. CLEARZ;
  344. }
  345. else if (result == 0) {
  346. CLEARN;
  347. SETZ;
  348. }
  349. else {
  350. CLEARN;
  351. CLEARZ;
  352. }
  353. }
  354. /* Compute whether an addition of A and B, giving RESULT, overflowed. */
  355. int
  356. AddOverflow (ARMword a, ARMword b, ARMword result)
  357. {
  358. return ((NEG (a) && NEG (b) && POS (result))
  359. || (POS (a) && POS (b) && NEG (result)));
  360. }
  361. /* Compute whether a subtraction of A and B, giving RESULT, overflowed. */
  362. int
  363. SubOverflow (ARMword a, ARMword b, ARMword result)
  364. {
  365. return ((NEG (a) && POS (b) && POS (result))
  366. || (POS (a) && NEG (b) && NEG (result)));
  367. }
  368. /* Assigns the C flag after an addition of a and b to give result. */
  369. void
  370. ARMul_AddCarry (ARMul_State * state, ARMword a, ARMword b, ARMword result)
  371. {
  372. ASSIGNC ((NEG (a) && NEG (b)) ||
  373. (NEG (a) && POS (result)) || (NEG (b) && POS (result)));
  374. }
  375. /* Assigns the V flag after an addition of a and b to give result. */
  376. void
  377. ARMul_AddOverflow (ARMul_State * state, ARMword a, ARMword b, ARMword result)
  378. {
  379. ASSIGNV (AddOverflow (a, b, result));
  380. }
  381. /* Assigns the C flag after an subtraction of a and b to give result. */
  382. void
  383. ARMul_SubCarry (ARMul_State * state, ARMword a, ARMword b, ARMword result)
  384. {
  385. ASSIGNC ((NEG (a) && POS (b)) ||
  386. (NEG (a) && POS (result)) || (POS (b) && POS (result)));
  387. }
  388. /* Assigns the V flag after an subtraction of a and b to give result. */
  389. void
  390. ARMul_SubOverflow (ARMul_State * state, ARMword a, ARMword b, ARMword result)
  391. {
  392. ASSIGNV (SubOverflow (a, b, result));
  393. }
  394. /* This function does the work of generating the addresses used in an
  395. LDC instruction. The code here is always post-indexed, it's up to the
  396. caller to get the input address correct and to handle base register
  397. modification. It also handles the Busy-Waiting. */
  398. void
  399. ARMul_LDC (ARMul_State * state, ARMword instr, ARMword address)
  400. {
  401. unsigned cpab;
  402. ARMword data;
  403. UNDEF_LSCPCBaseWb;
  404. //printf("SKYEYE ARMul_LDC, CPnum is %x, instr %x, addr %x\n",CPNum, instr, address);
  405. /*chy 2004-05-23 should update this function in the future,should concern dataabort*/
  406. // chy 2004-05-25 , fix it now,so needn't printf
  407. // printf("SKYEYE ARMul_LDC, should update this function!!!!!\n");
  408. //exit(-1);
  409. if (!CP_ACCESS_ALLOWED (state, CPNum)) {
  410. /*
  411. printf
  412. ("SKYEYE ARMul_LDC,NOT ALLOW, underinstr, CPnum is %x, instr %x, addr %x\n",
  413. CPNum, instr, address);
  414. */
  415. ARMul_UndefInstr (state, instr);
  416. return;
  417. }
  418. if (ADDREXCEPT (address))
  419. INTERNALABORT (address);
  420. cpab = (state->LDC[CPNum]) (state, ARMul_FIRST, instr, 0);
  421. while (cpab == ARMul_BUSY) {
  422. ARMul_Icycles (state, 1, 0);
  423. if (IntPending (state)) {
  424. cpab = (state->LDC[CPNum]) (state, ARMul_INTERRUPT,
  425. instr, 0);
  426. return;
  427. }
  428. else
  429. cpab = (state->LDC[CPNum]) (state, ARMul_BUSY, instr,
  430. 0);
  431. }
  432. if (cpab == ARMul_CANT) {
  433. /*
  434. printf
  435. ("SKYEYE ARMul_LDC,NOT CAN, underinstr, CPnum is %x, instr %x, addr %x\n",
  436. CPNum, instr, address);
  437. */
  438. CPTAKEABORT;
  439. return;
  440. }
  441. cpab = (state->LDC[CPNum]) (state, ARMul_TRANSFER, instr, 0);
  442. data = ARMul_LoadWordN (state, address);
  443. //chy 2004-05-25
  444. if (state->abortSig || state->Aborted)
  445. goto L_ldc_takeabort;
  446. BUSUSEDINCPCN;
  447. //chy 2004-05-25
  448. /*
  449. if (BIT (21))
  450. LSBase = state->Base;
  451. */
  452. cpab = (state->LDC[CPNum]) (state, ARMul_DATA, instr, data);
  453. while (cpab == ARMul_INC) {
  454. address += 4;
  455. data = ARMul_LoadWordN (state, address);
  456. //chy 2004-05-25
  457. if (state->abortSig || state->Aborted)
  458. goto L_ldc_takeabort;
  459. cpab = (state->LDC[CPNum]) (state, ARMul_DATA, instr, data);
  460. }
  461. //chy 2004-05-25
  462. L_ldc_takeabort:
  463. if (BIT (21)) {
  464. if (!
  465. ((state->abortSig || state->Aborted)
  466. && state->lateabtSig == LOW))
  467. LSBase = state->Base;
  468. }
  469. if (state->abortSig || state->Aborted)
  470. TAKEABORT;
  471. }
  472. /* This function does the work of generating the addresses used in an
  473. STC instruction. The code here is always post-indexed, it's up to the
  474. caller to get the input address correct and to handle base register
  475. modification. It also handles the Busy-Waiting. */
  476. void
  477. ARMul_STC (ARMul_State * state, ARMword instr, ARMword address)
  478. {
  479. unsigned cpab;
  480. ARMword data;
  481. UNDEF_LSCPCBaseWb;
  482. //printf("SKYEYE ARMul_STC, CPnum is %x, instr %x, addr %x\n",CPNum, instr, address);
  483. /*chy 2004-05-23 should update this function in the future,should concern dataabort */
  484. // skyeye_instr_debug=0;printf("SKYEYE debug end!!!!\n");
  485. // chy 2004-05-25 , fix it now,so needn't printf
  486. // printf("SKYEYE ARMul_STC, should update this function!!!!!\n");
  487. //exit(-1);
  488. if (!CP_ACCESS_ALLOWED (state, CPNum)) {
  489. /*
  490. printf
  491. ("SKYEYE ARMul_STC,NOT ALLOW, undefinstr, CPnum is %x, instr %x, addr %x\n",
  492. CPNum, instr, address);
  493. */
  494. ARMul_UndefInstr (state, instr);
  495. return;
  496. }
  497. if (ADDREXCEPT (address) || VECTORACCESS (address))
  498. INTERNALABORT (address);
  499. cpab = (state->STC[CPNum]) (state, ARMul_FIRST, instr, &data);
  500. while (cpab == ARMul_BUSY) {
  501. ARMul_Icycles (state, 1, 0);
  502. if (IntPending (state)) {
  503. cpab = (state->STC[CPNum]) (state, ARMul_INTERRUPT,
  504. instr, 0);
  505. return;
  506. }
  507. else
  508. cpab = (state->STC[CPNum]) (state, ARMul_BUSY, instr,
  509. &data);
  510. }
  511. if (cpab == ARMul_CANT) {
  512. /*
  513. printf
  514. ("SKYEYE ARMul_STC,CANT, undefinstr, CPnum is %x, instr %x, addr %x\n",
  515. CPNum, instr, address);
  516. */
  517. CPTAKEABORT;
  518. return;
  519. }
  520. #ifndef MODE32
  521. if (ADDREXCEPT (address) || VECTORACCESS (address))
  522. INTERNALABORT (address);
  523. #endif
  524. BUSUSEDINCPCN;
  525. //chy 2004-05-25
  526. /*
  527. if (BIT (21))
  528. LSBase = state->Base;
  529. */
  530. cpab = (state->STC[CPNum]) (state, ARMul_DATA, instr, &data);
  531. ARMul_StoreWordN (state, address, data);
  532. //chy 2004-05-25
  533. if (state->abortSig || state->Aborted)
  534. goto L_stc_takeabort;
  535. while (cpab == ARMul_INC) {
  536. address += 4;
  537. cpab = (state->STC[CPNum]) (state, ARMul_DATA, instr, &data);
  538. ARMul_StoreWordN (state, address, data);
  539. //chy 2004-05-25
  540. if (state->abortSig || state->Aborted)
  541. goto L_stc_takeabort;
  542. }
  543. //chy 2004-05-25
  544. L_stc_takeabort:
  545. if (BIT (21)) {
  546. if (!
  547. ((state->abortSig || state->Aborted)
  548. && state->lateabtSig == LOW))
  549. LSBase = state->Base;
  550. }
  551. if (state->abortSig || state->Aborted)
  552. TAKEABORT;
  553. }
  554. /* This function does the Busy-Waiting for an MCR instruction. */
  555. void
  556. ARMul_MCR (ARMul_State * state, ARMword instr, ARMword source)
  557. {
  558. unsigned cpab;
  559. //printf("SKYEYE ARMul_MCR, CPnum is %x, source %x\n",CPNum, source);
  560. if (!CP_ACCESS_ALLOWED (state, CPNum)) {
  561. //chy 2004-07-19 should fix in the future ????!!!!
  562. //printf("SKYEYE ARMul_MCR, ACCESS_not ALLOWed, UndefinedInstr CPnum is %x, source %x\n",CPNum, source);
  563. ARMul_UndefInstr (state, instr);
  564. return;
  565. }
  566. cpab = (state->MCR[CPNum]) (state, ARMul_FIRST, instr, source);
  567. while (cpab == ARMul_BUSY) {
  568. ARMul_Icycles (state, 1, 0);
  569. if (IntPending (state)) {
  570. cpab = (state->MCR[CPNum]) (state, ARMul_INTERRUPT,
  571. instr, 0);
  572. return;
  573. }
  574. else
  575. cpab = (state->MCR[CPNum]) (state, ARMul_BUSY, instr,
  576. source);
  577. }
  578. if (cpab == ARMul_CANT) {
  579. printf ("SKYEYE ARMul_MCR, CANT, UndefinedInstr %x CPnum is %x, source %x\n", instr, CPNum, source);
  580. ARMul_Abort (state, ARMul_UndefinedInstrV);
  581. }
  582. else {
  583. BUSUSEDINCPCN;
  584. ARMul_Ccycles (state, 1, 0);
  585. }
  586. }
  587. /* This function does the Busy-Waiting for an MCRR instruction. */
  588. void
  589. ARMul_MCRR (ARMul_State * state, ARMword instr, ARMword source1, ARMword source2)
  590. {
  591. unsigned cpab;
  592. if (!CP_ACCESS_ALLOWED (state, CPNum)) {
  593. ARMul_UndefInstr (state, instr);
  594. return;
  595. }
  596. cpab = (state->MCRR[CPNum]) (state, ARMul_FIRST, instr, source1, source2);
  597. while (cpab == ARMul_BUSY) {
  598. ARMul_Icycles (state, 1, 0);
  599. if (IntPending (state)) {
  600. cpab = (state->MCRR[CPNum]) (state, ARMul_INTERRUPT,
  601. instr, 0, 0);
  602. return;
  603. }
  604. else
  605. cpab = (state->MCRR[CPNum]) (state, ARMul_BUSY, instr,
  606. source1, source2);
  607. }
  608. if (cpab == ARMul_CANT) {
  609. printf ("In %s, CoProcesscor returned CANT, CPnum is %x, instr %x, source %x %x\n", __FUNCTION__, CPNum, instr, source1, source2);
  610. ARMul_Abort (state, ARMul_UndefinedInstrV);
  611. }
  612. else {
  613. BUSUSEDINCPCN;
  614. ARMul_Ccycles (state, 1, 0);
  615. }
  616. }
  617. /* This function does the Busy-Waiting for an MRC instruction. */
  618. ARMword
  619. ARMul_MRC (ARMul_State * state, ARMword instr)
  620. {
  621. unsigned cpab;
  622. ARMword result = 0;
  623. //printf("SKYEYE ARMul_MRC, CPnum is %x, instr %x\n",CPNum, instr);
  624. if (!CP_ACCESS_ALLOWED (state, CPNum)) {
  625. //chy 2004-07-19 should fix in the future????!!!!
  626. //printf("SKYEYE ARMul_MRC,NOT ALLOWed UndefInstr CPnum is %x, instr %x\n",CPNum, instr);
  627. ARMul_UndefInstr (state, instr);
  628. return -1;
  629. }
  630. cpab = (state->MRC[CPNum]) (state, ARMul_FIRST, instr, &result);
  631. while (cpab == ARMul_BUSY) {
  632. ARMul_Icycles (state, 1, 0);
  633. if (IntPending (state)) {
  634. cpab = (state->MRC[CPNum]) (state, ARMul_INTERRUPT,
  635. instr, 0);
  636. return (0);
  637. }
  638. else
  639. cpab = (state->MRC[CPNum]) (state, ARMul_BUSY, instr,
  640. &result);
  641. }
  642. if (cpab == ARMul_CANT) {
  643. printf ("SKYEYE ARMul_MRC,CANT UndefInstr CPnum is %x, instr %x\n", CPNum, instr);
  644. ARMul_Abort (state, ARMul_UndefinedInstrV);
  645. /* Parent will destroy the flags otherwise. */
  646. result = ECC;
  647. }
  648. else {
  649. BUSUSEDINCPCN;
  650. ARMul_Ccycles (state, 1, 0);
  651. ARMul_Icycles (state, 1, 0);
  652. }
  653. return result;
  654. }
  655. /* This function does the Busy-Waiting for an MRRC instruction. (to verify) */
  656. void
  657. ARMul_MRRC (ARMul_State * state, ARMword instr, ARMword * dest1, ARMword * dest2)
  658. {
  659. unsigned cpab;
  660. ARMword result1 = 0;
  661. ARMword result2 = 0;
  662. if (!CP_ACCESS_ALLOWED (state, CPNum)) {
  663. ARMul_UndefInstr (state, instr);
  664. return;
  665. }
  666. cpab = (state->MRRC[CPNum]) (state, ARMul_FIRST, instr, &result1, &result2);
  667. while (cpab == ARMul_BUSY) {
  668. ARMul_Icycles (state, 1, 0);
  669. if (IntPending (state)) {
  670. cpab = (state->MRRC[CPNum]) (state, ARMul_INTERRUPT,
  671. instr, 0, 0);
  672. return;
  673. }
  674. else
  675. cpab = (state->MRRC[CPNum]) (state, ARMul_BUSY, instr,
  676. &result1, &result2);
  677. }
  678. if (cpab == ARMul_CANT) {
  679. printf ("In %s, CoProcesscor returned CANT, CPnum is %x, instr %x\n", __FUNCTION__, CPNum, instr);
  680. ARMul_Abort (state, ARMul_UndefinedInstrV);
  681. }
  682. else {
  683. BUSUSEDINCPCN;
  684. ARMul_Ccycles (state, 1, 0);
  685. ARMul_Icycles (state, 1, 0);
  686. }
  687. *dest1 = result1;
  688. *dest2 = result2;
  689. }
  690. /* This function does the Busy-Waiting for an CDP instruction. */
  691. void
  692. ARMul_CDP (ARMul_State * state, ARMword instr)
  693. {
  694. unsigned cpab;
  695. if (!CP_ACCESS_ALLOWED (state, CPNum)) {
  696. ARMul_UndefInstr (state, instr);
  697. return;
  698. }
  699. cpab = (state->CDP[CPNum]) (state, ARMul_FIRST, instr);
  700. while (cpab == ARMul_BUSY) {
  701. ARMul_Icycles (state, 1, 0);
  702. if (IntPending (state)) {
  703. cpab = (state->CDP[CPNum]) (state, ARMul_INTERRUPT,
  704. instr);
  705. return;
  706. }
  707. else
  708. cpab = (state->CDP[CPNum]) (state, ARMul_BUSY, instr);
  709. }
  710. if (cpab == ARMul_CANT)
  711. ARMul_Abort (state, ARMul_UndefinedInstrV);
  712. else
  713. BUSUSEDN;
  714. }
  715. /* This function handles Undefined instructions, as CP isntruction. */
  716. void
  717. ARMul_UndefInstr (ARMul_State * state, ARMword instr)
  718. {
  719. ERROR_LOG(ARM11, "Undefined instruction!! Instr: 0x%x", instr);
  720. ARMul_Abort (state, ARMul_UndefinedInstrV);
  721. }
  722. /* Return TRUE if an interrupt is pending, FALSE otherwise. */
  723. unsigned
  724. IntPending (ARMul_State * state)
  725. {
  726. /* Any exceptions. */
  727. if (state->NresetSig == LOW) {
  728. ARMul_Abort (state, ARMul_ResetV);
  729. return TRUE;
  730. }
  731. else if (!state->NfiqSig && !FFLAG) {
  732. ARMul_Abort (state, ARMul_FIQV);
  733. return TRUE;
  734. }
  735. else if (!state->NirqSig && !IFLAG) {
  736. ARMul_Abort (state, ARMul_IRQV);
  737. return TRUE;
  738. }
  739. return FALSE;
  740. }
  741. /* Align a word access to a non word boundary. */
  742. ARMword
  743. ARMul_Align (ARMul_State *state, ARMword address, ARMword data)
  744. {
  745. /* This code assumes the address is really unaligned,
  746. as a shift by 32 is undefined in C. */
  747. address = (address & 3) << 3; /* Get the word address. */
  748. return ((data >> address) | (data << (32 - address))); /* rot right */
  749. }
  750. /* This routine is used to call another routine after a certain number of
  751. cycles have been executed. The first parameter is the number of cycles
  752. delay before the function is called, the second argument is a pointer
  753. to the function. A delay of zero doesn't work, just call the function. */
  754. void
  755. ARMul_ScheduleEvent (ARMul_State * state, unsigned int delay,
  756. unsigned (*what) (ARMul_State *))
  757. {
  758. unsigned int when;
  759. struct EventNode *event;
  760. if (state->EventSet++ == 0)
  761. state->Now = ARMul_Time (state);
  762. when = (state->Now + delay) % EVENTLISTSIZE;
  763. event = (struct EventNode *) malloc (sizeof (struct EventNode));
  764. _dbg_assert_msg_(ARM11, event, "SKYEYE:ARMul_ScheduleEvent: malloc event error\n");
  765. event->func = what;
  766. event->next = *(state->EventPtr + when);
  767. *(state->EventPtr + when) = event;
  768. }
  769. /* This routine is called at the beginning of
  770. every cycle, to envoke scheduled events. */
  771. void
  772. ARMul_EnvokeEvent (ARMul_State * state)
  773. {
  774. static unsigned int then;
  775. then = state->Now;
  776. state->Now = ARMul_Time (state) % EVENTLISTSIZE;
  777. if (then < state->Now)
  778. /* Schedule events. */
  779. EnvokeList (state, then, state->Now);
  780. else if (then > state->Now) {
  781. /* Need to wrap around the list. */
  782. EnvokeList (state, then, EVENTLISTSIZE - 1L);
  783. EnvokeList (state, 0L, state->Now);
  784. }
  785. }
  786. /* Envokes all the entries in a range. */
  787. static void
  788. EnvokeList (ARMul_State * state, unsigned int from, unsigned int to)
  789. {
  790. for (; from <= to; from++) {
  791. struct EventNode *anevent;
  792. anevent = *(state->EventPtr + from);
  793. while (anevent) {
  794. (anevent->func) (state);
  795. state->EventSet--;
  796. anevent = anevent->next;
  797. }
  798. *(state->EventPtr + from) = NULL;
  799. }
  800. }
  801. /* This routine is returns the number of clock ticks since the last reset. */
  802. unsigned int
  803. ARMul_Time (ARMul_State * state)
  804. {
  805. return (state->NumScycles + state->NumNcycles +
  806. state->NumIcycles + state->NumCcycles + state->NumFcycles);
  807. }