armsupp.cpp 28 KB

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