Ea.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  1. #include "app.h"
  2. int earead_check_addrerr = 1, eawrite_check_addrerr = 0;
  3. // some ops use non-standard cycle counts for EAs, so are listed here.
  4. // all constants borrowed from the MUSASHI core by Karl Stenerud.
  5. /* Extra cycles for JMP instruction (000, 010) */
  6. int g_jmp_cycle_table[8] =
  7. {
  8. 4, /* EA_MODE_AI */
  9. 6, /* EA_MODE_DI */
  10. 10, /* EA_MODE_IX */
  11. 6, /* EA_MODE_AW */
  12. 8, /* EA_MODE_AL */
  13. 6, /* EA_MODE_PCDI */
  14. 10, /* EA_MODE_PCIX */
  15. 0, /* EA_MODE_I */
  16. };
  17. /* Extra cycles for JSR instruction (000, 010) */
  18. int g_jsr_cycle_table[8] =
  19. {
  20. 4, /* EA_MODE_AI */
  21. 6, /* EA_MODE_DI */
  22. 10, /* EA_MODE_IX */
  23. 6, /* EA_MODE_AW */
  24. 8, /* EA_MODE_AL */
  25. 6, /* EA_MODE_PCDI */
  26. 10, /* EA_MODE_PCIX */
  27. 0, /* EA_MODE_I */
  28. };
  29. /* Extra cycles for LEA instruction (000, 010) */
  30. int g_lea_cycle_table[8] =
  31. {
  32. 4, /* EA_MODE_AI */
  33. 8, /* EA_MODE_DI */
  34. 12, /* EA_MODE_IX */
  35. 8, /* EA_MODE_AW */
  36. 12, /* EA_MODE_AL */
  37. 8, /* EA_MODE_PCDI */
  38. 12, /* EA_MODE_PCIX */
  39. 0, /* EA_MODE_I */
  40. };
  41. /* Extra cycles for PEA instruction (000, 010) */
  42. int g_pea_cycle_table[8] =
  43. {
  44. 6, /* EA_MODE_AI */
  45. 10, /* EA_MODE_DI */
  46. 14, /* EA_MODE_IX */
  47. 10, /* EA_MODE_AW */
  48. 14, /* EA_MODE_AL */
  49. 10, /* EA_MODE_PCDI */
  50. 14, /* EA_MODE_PCIX */
  51. 0, /* EA_MODE_I */
  52. };
  53. /* Extra cycles for MOVEM instruction (000, 010) */
  54. int g_movem_cycle_table[8] =
  55. {
  56. 0, /* EA_MODE_AI */
  57. 4, /* EA_MODE_DI */
  58. 6, /* EA_MODE_IX */
  59. 4, /* EA_MODE_AW */
  60. 8, /* EA_MODE_AL */
  61. 0, /* EA_MODE_PCDI */
  62. 0, /* EA_MODE_PCIX */
  63. 0, /* EA_MODE_I */
  64. };
  65. // add nonstandard EA
  66. int Ea_add_ns(int *tab, int ea)
  67. {
  68. if(ea<0x10) return 0;
  69. if((ea&0x38)==0x10) return tab[0]; // (An) (ai)
  70. if(ea<0x28) return 0;
  71. if(ea<0x30) return tab[1]; // ($nn,An) (di)
  72. if(ea<0x38) return tab[2]; // ($nn,An,Rn) (ix)
  73. if(ea==0x38) return tab[3]; // (aw)
  74. if(ea==0x39) return tab[4]; // (al)
  75. if(ea==0x3a) return tab[5]; // ($nn,PC) (pcdi)
  76. if(ea==0x3b) return tab[6]; // ($nn,pc,Rn) (pcix)
  77. if(ea==0x3c) return tab[7]; // #$nnnn (i)
  78. return 0;
  79. }
  80. // ---------------------------------------------------------------------------
  81. // Gets the offset of a register for an ea, and puts it in 'r'
  82. // Shifted left by 'shift'
  83. // Doesn't trash anything
  84. static int EaCalcReg(int r,int ea,int mask,int forceor,int shift,int noshift=0)
  85. {
  86. int i=0,low=0,needor=0;
  87. int lsl=0;
  88. for (i=mask|0x8000; (i&1)==0; i>>=1) low++; // Find out how high up the EA mask is
  89. mask&=0xf<<low; // This is the max we can do
  90. if (ea>=8)
  91. {
  92. needor=1; // Need to OR to access A0-7
  93. if ((g_op>>low)&8) { needor=0; mask|=8<<low; } // Ah - no we don't actually need to or, since the bit is high in r8
  94. if (forceor) needor=1; // Special case for 0x30-0x38 EAs ;)
  95. }
  96. ot(" and r%d,r8,#0x%.4x\n",r,mask);
  97. if (needor) ot(" orr r%d,r%d,#0x%x ;@ A0-7\n",r,r,8<<low);
  98. // Find out amount to shift left:
  99. lsl=shift-low;
  100. if (lsl&&!noshift)
  101. {
  102. ot(" mov r%d,r%d,",r,r);
  103. if (lsl>0) ot("lsl #%d\n", lsl);
  104. else ot("lsr #%d\n",-lsl);
  105. }
  106. return 0;
  107. }
  108. // EaCalc - ARM Register 'a' = Effective Address
  109. // If ea>=0x10, trashes r0,r2 and r3, else nothing
  110. // size values 0, 1, 2 ~ byte, word, long
  111. // mask shows usable bits in r8
  112. int EaCalc(int a,int mask,int ea,int size,int top,int sign_extend)
  113. {
  114. char text[32]="";
  115. int func=0;
  116. DisaPc=2; DisaGetEa(text,ea,size); // Get text version of the effective address
  117. func=0x68+(size<<2); // Get correct read handler
  118. if (ea<0x10)
  119. {
  120. int noshift=0;
  121. if (size>=2||(size==0&&(top||!sign_extend))) noshift=1; // Saves one opcode
  122. ot(";@ EaCalc : Get register index into r%d:\n",a);
  123. EaCalcReg(a,ea,mask,0,2,noshift);
  124. return 0;
  125. }
  126. ot(";@ EaCalc : Get '%s' into r%d:\n",text,a);
  127. // (An), (An)+, -(An)
  128. if (ea<0x28)
  129. {
  130. int step=1<<size, strr=a;
  131. int low=0,lsl=0,i;
  132. if ((ea&7)==7 && step<2) step=2; // move.b (a7)+ or -(a7) steps by 2 not 1
  133. if (ea==0x1f||ea==0x27) // A7 handlers are always separate
  134. {
  135. ot(" ldr r%d,[r7,#0x3c] ;@ A7\n",a);
  136. }
  137. else
  138. {
  139. EaCalcReg(2,ea,mask,0,0,1);
  140. if(mask)
  141. for (i=mask|0x8000; (i&1)==0; i>>=1) low++; // Find out how high up the EA mask is
  142. lsl=2-low; // Having a lsl #x here saves one opcode
  143. if (lsl>=0) ot(" ldr r%d,[r7,r2,lsl #%i]\n",a,lsl);
  144. else if (lsl<0) ot(" ldr r%d,[r7,r2,lsr #%i]\n",a,-lsl);
  145. }
  146. if ((ea&0x38)==0x18) // (An)+
  147. {
  148. ot(" add r3,r%d,#%d ;@ Post-increment An\n",a,step);
  149. strr=3;
  150. }
  151. if ((ea&0x38)==0x20) // -(An)
  152. ot(" sub r%d,r%d,#%d ;@ Pre-decrement An\n",a,a,step);
  153. if ((ea&0x38)==0x18||(ea&0x38)==0x20)
  154. {
  155. if (ea==0x1f||ea==0x27)
  156. {
  157. ot(" str r%d,[r7,#0x3c] ;@ A7\n",strr);
  158. }
  159. else
  160. {
  161. if (lsl>=0) ot(" str r%d,[r7,r2,lsl #%i]\n",strr,lsl);
  162. else if (lsl<0) ot(" str r%d,[r7,r2,lsr #%i]\n",strr,-lsl);
  163. }
  164. }
  165. if ((ea&0x38)==0x20) Cycles+=size<2 ? 6:10; // -(An) Extra cycles
  166. else Cycles+=size<2 ? 4:8; // (An),(An)+ Extra cycles
  167. return 0;
  168. }
  169. if (ea<0x30) // ($nn,An) (di)
  170. {
  171. ot(" ldrsh r0,[r4],#2 ;@ Fetch offset\n"); pc_dirty=1;
  172. EaCalcReg(2,8,mask,0,0);
  173. ot(" ldr r2,[r7,r2,lsl #2]\n");
  174. ot(" add r%d,r0,r2 ;@ Add on offset\n",a);
  175. Cycles+=size<2 ? 8:12; // Extra cycles
  176. return 0;
  177. }
  178. if (ea<0x38) // ($nn,An,Rn) (ix)
  179. {
  180. ot(";@ Get extension word into r3:\n");
  181. ot(" ldrh r3,[r4],#2 ;@ ($Disp,PC,Rn)\n"); pc_dirty=1;
  182. ot(" mov r2,r3,lsr #10\n");
  183. ot(" tst r3,#0x0800 ;@ Is Rn Word or Long\n");
  184. ot(" and r2,r2,#0x3c ;@ r2=Index of Rn\n");
  185. ot(" ldreqsh r2,[r7,r2] ;@ r2=Rn.w\n");
  186. ot(" ldrne r2,[r7,r2] ;@ r2=Rn.l\n");
  187. ot(" mov r0,r3,asl #24 ;@ r0=Get 8-bit signed Disp\n");
  188. ot(" add r3,r2,r0,asr #24 ;@ r3=Disp+Rn\n");
  189. EaCalcReg(2,8,mask,1,0);
  190. ot(" ldr r2,[r7,r2,lsl #2]\n");
  191. ot(" add r%d,r2,r3 ;@ r%d=Disp+An+Rn\n",a,a);
  192. Cycles+=size<2 ? 10:14; // Extra cycles
  193. return 0;
  194. }
  195. if (ea==0x38) // (aw)
  196. {
  197. ot(" ldrsh r%d,[r4],#2 ;@ Fetch Absolute Short address\n",a); pc_dirty=1;
  198. Cycles+=size<2 ? 8:12; // Extra cycles
  199. return 0;
  200. }
  201. if (ea==0x39) // (al)
  202. {
  203. ot(" ldrh r2,[r4],#2 ;@ Fetch Absolute Long address\n");
  204. ot(" ldrh r0,[r4],#2\n"); pc_dirty=1;
  205. ot(" orr r%d,r0,r2,lsl #16\n",a);
  206. Cycles+=size<2 ? 12:16; // Extra cycles
  207. return 0;
  208. }
  209. if (ea==0x3a) // ($nn,PC) (pcdi)
  210. {
  211. ot(" ldr r0,[r7,#0x60] ;@ Get Memory base\n");
  212. ot(" sub r0,r4,r0 ;@ Real PC\n");
  213. ot(" ldrsh r2,[r4],#2 ;@ Fetch extension\n"); pc_dirty=1;
  214. ot(" mov r0,r0,lsl #8\n");
  215. ot(" add r%d,r2,r0,asr #8 ;@ ($nn,PC)\n",a);
  216. Cycles+=size<2 ? 8:12; // Extra cycles
  217. return 0;
  218. }
  219. if (ea==0x3b) // ($nn,pc,Rn) (pcix)
  220. {
  221. ot(" ldr r0,[r7,#0x60] ;@ Get Memory base\n");
  222. ot(" ldrh r3,[r4] ;@ Get extension word\n");
  223. ot(" sub r0,r4,r0 ;@ r0=PC\n");
  224. ot(" add r4,r4,#2\n"); pc_dirty=1;
  225. ot(" mov r0,r0,asl #8 ;@ use only 24bits of PC\n");
  226. ot(" mov r2,r3,lsr #10\n");
  227. ot(" tst r3,#0x0800 ;@ Is Rn Word or Long\n");
  228. ot(" and r2,r2,#0x3c ;@ r2=Index of Rn\n");
  229. ot(" ldreqsh r2,[r7,r2] ;@ r2=Rn.w\n");
  230. ot(" ldrne r2,[r7,r2] ;@ r2=Rn.l\n");
  231. ot(" mov r3,r3,asl #24 ;@ r3=Get 8-bit signed Disp\n");
  232. ot(" add r2,r2,r3,asr #24 ;@ r2=Disp+Rn\n");
  233. ot(" add r%d,r2,r0,asr #8 ;@ r%d=Disp+PC+Rn\n",a,a);
  234. Cycles+=size<2 ? 10:14; // Extra cycles
  235. return 0;
  236. }
  237. if (ea==0x3c) // #$nnnn (i)
  238. {
  239. if (size<2)
  240. {
  241. ot(" ldr%s r%d,[r4],#2 ;@ Fetch immediate value\n",Sarm[size&3],a); pc_dirty=1;
  242. Cycles+=4; // Extra cycles
  243. return 0;
  244. }
  245. ot(" ldrh r2,[r4],#2 ;@ Fetch immediate value\n");
  246. ot(" ldrh r3,[r4],#2\n"); pc_dirty=1;
  247. ot(" orr r%d,r3,r2,lsl #16\n",a);
  248. Cycles+=8; // Extra cycles
  249. return 0;
  250. }
  251. return 1;
  252. }
  253. // ---------------------------------------------------------------------------
  254. // Read effective address in (ARM Register 'a') to ARM register 'v'
  255. // 'a' and 'v' can be anything but 0 is generally best (for both)
  256. // If (ea<0x10) nothing is trashed, else r0-r3 is trashed
  257. // If 'top' is given, the ARM register v shifted to the top, e.g. 0xc000 -> 0xc0000000
  258. // If top is 0 and sign_extend is not, then ARM register v is sign extended,
  259. // e.g. 0xc000 -> 0xffffc000 (else it may or may not be sign extended)
  260. int EaRead(int a,int v,int ea,int size,int mask,int top,int sign_extend)
  261. {
  262. char text[32]="";
  263. int shift=0;
  264. shift=32-(8<<size);
  265. DisaPc=2; DisaGetEa(text,ea,size); // Get text version of the effective address
  266. if (ea<0x10)
  267. {
  268. int lsl=0,low=0,nsarm=size&3,i;
  269. if (size>=2||(size==0&&(top||!sign_extend))) {
  270. if(mask)
  271. for (i=mask|0x8000; (i&1)==0; i>>=1) low++; // Find out how high up the EA mask is
  272. lsl=2-low; // Having a lsl #2 here saves one opcode
  273. }
  274. if (top||!sign_extend) nsarm=3;
  275. ot(";@ EaRead : Read register[r%d] into r%d:\n",a,v);
  276. if (lsl>0) ot(" ldr%s r%d,[r7,r%d,lsl #%i]\n",Narm[nsarm],v,a,lsl);
  277. else if (lsl<0) ot(" ldr%s r%d,[r7,r%d,lsr #%i]\n",Narm[nsarm],v,a,-lsl);
  278. else ot(" ldr%s r%d,[r7,r%d]\n",Sarm[nsarm],v,a);
  279. if (top&&shift) ot(" mov r%d,r%d,asl #%d\n",v,v,shift);
  280. ot("\n"); return 0;
  281. }
  282. ot(";@ EaRead : Read '%s' (address in r%d) into r%d:\n",text,a,v);
  283. if (ea==0x3c)
  284. {
  285. int asl=0;
  286. if (top) asl=shift;
  287. if (asl) ot(" mov r%d,r%d,asl #%d\n",v,a,asl);
  288. else if (v!=a) ot(" mov r%d,r%d\n",v,a);
  289. ot("\n"); return 0;
  290. }
  291. if (ea>=0x3a && ea<=0x3b) MemHandler(2,size,a,earead_check_addrerr); // Fetch
  292. else MemHandler(0,size,a,earead_check_addrerr); // Read
  293. // defaults to 1, as most things begins with a read
  294. earead_check_addrerr=1;
  295. if (sign_extend)
  296. {
  297. int d_reg=0;
  298. if (shift) {
  299. ot(" mov r%d,r%d,asl #%d\n",v,d_reg,shift);
  300. d_reg=v;
  301. }
  302. if (!top && shift) {
  303. ot(" mov r%d,r%d,asr #%d\n",v,d_reg,shift);
  304. d_reg=v;
  305. }
  306. if (d_reg != v)
  307. ot(" mov r%d,r%d\n",v,d_reg);
  308. }
  309. else
  310. {
  311. if (top && shift)
  312. ot(" mov r%d,r0,asl #%d\n",v,shift);
  313. else if (v!=0)
  314. ot(" mov r%d,r0\n",v);
  315. }
  316. ot("\n"); return 0;
  317. }
  318. // calculate EA and read
  319. // if (ea < 0x10) nothing is trashed
  320. // if (ea == 0x3c) r2 and r3 are trashed
  321. // else r0-r3 are trashed
  322. // size values 0, 1, 2 ~ byte, word, long
  323. // r_ea is reg to store ea in (-1 means ea is not needed), r is dst reg
  324. // if sign_extend is 0, non-32bit values will have MS bits undefined
  325. int EaCalcRead(int r_ea,int r,int ea,int size,int mask,int sign_extend)
  326. {
  327. if (ea<0x10)
  328. {
  329. if (r_ea==-1)
  330. {
  331. r_ea=r;
  332. if (!sign_extend) size=2;
  333. }
  334. }
  335. else if (ea==0x3c) // #imm
  336. {
  337. r_ea=r;
  338. }
  339. else
  340. {
  341. if (r_ea==-1) r_ea=0;
  342. }
  343. EaCalc (r_ea,mask,ea,size,0,sign_extend);
  344. EaRead (r_ea, r,ea,size,mask,0,sign_extend);
  345. return 0;
  346. }
  347. int EaCalcReadNoSE(int r_ea,int r,int ea,int size,int mask)
  348. {
  349. return EaCalcRead(r_ea,r,ea,size,mask,0);
  350. }
  351. // Return 1 if we can read this ea
  352. int EaCanRead(int ea,int size)
  353. {
  354. if (size<0)
  355. {
  356. // LEA:
  357. // These don't make sense?:
  358. if (ea< 0x10) return 0; // Register
  359. if (ea==0x3c) return 0; // Immediate
  360. if (ea>=0x18 && ea<0x28) return 0; // Pre/Post inc/dec An
  361. }
  362. if (ea<=0x3c) return 1;
  363. return 0;
  364. }
  365. // ---------------------------------------------------------------------------
  366. // Write effective address (ARM Register 'a') with ARM register 'v'
  367. // Trashes r0-r3,r12,lr; 'a' can be 0 or 2+, 'v' can be 1 or higher
  368. // If a==0 and v==1 it's faster though.
  369. int EaWrite(int a,int v,int ea,int size,int mask,int top,int sign_extend_ea)
  370. {
  371. char text[32]="";
  372. int shift=0;
  373. if(a == 1) { printf("Error! EaWrite a==1 !\n"); return 1; }
  374. if (top) shift=32-(8<<size);
  375. DisaPc=2; DisaGetEa(text,ea,size); // Get text version of the effective address
  376. if (ea<0x10)
  377. {
  378. int lsl=0,low=0,i;
  379. if (size>=2||(size==0&&(top||!sign_extend_ea))) {
  380. if(mask)
  381. for (i=mask|0x8000; (i&1)==0; i>>=1) low++; // Find out how high up the EA mask is
  382. lsl=2-low; // Having a lsl #x here saves one opcode
  383. }
  384. ot(";@ EaWrite: r%d into register[r%d]:\n",v,a);
  385. if (shift) ot(" mov r%d,r%d,asr #%d\n",v,v,shift);
  386. if (lsl>0) ot(" str%s r%d,[r7,r%d,lsl #%i]\n",Narm[size&3],v,a,lsl);
  387. else if (lsl<0) ot(" str%s r%d,[r7,r%d,lsr #%i]\n",Narm[size&3],v,a,-lsl);
  388. else ot(" str%s r%d,[r7,r%d]\n",Narm[size&3],v,a);
  389. ot("\n"); return 0;
  390. }
  391. ot(";@ EaWrite: Write r%d into '%s' (address in r%d):\n",v,text,a);
  392. if (ea==0x3c) { ot("Error! Write EA=0x%x\n\n",ea); return 1; }
  393. if (shift) ot(" mov r1,r%d,asr #%d\n",v,shift);
  394. else if (v!=1) ot(" mov r1,r%d\n",v);
  395. MemHandler(1,size,a,eawrite_check_addrerr); // Call write handler
  396. // not check by default, because most cases are rmw and
  397. // address was already checked before reading
  398. eawrite_check_addrerr = 0;
  399. ot("\n"); return 0;
  400. }
  401. // Return 1 if we can write this ea
  402. int EaCanWrite(int ea)
  403. {
  404. if (ea<=0x39) return 1; // 3b?
  405. return 0;
  406. }
  407. // ---------------------------------------------------------------------------
  408. // Return 1 if EA is An reg
  409. int EaAn(int ea)
  410. {
  411. if((ea&0x38)==8) return 1;
  412. return 0;
  413. }