sh2.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * PicoDrive
  3. * (C) notaz, 2009,2010
  4. *
  5. * This work is licensed under the terms of MAME license.
  6. * See COPYING file in the top-level directory.
  7. */
  8. #include <string.h>
  9. #include <stddef.h>
  10. #include "sh2.h"
  11. #include "../debug.h"
  12. #include "compiler.h"
  13. #define I 0xf0
  14. int sh2_init(SH2 *sh2, int is_slave, SH2 *other_sh2)
  15. {
  16. int ret = 0;
  17. memset(sh2, 0, offsetof(SH2, mult_m68k_to_sh2));
  18. sh2->is_slave = is_slave;
  19. sh2->other_sh2 = other_sh2;
  20. pdb_register_cpu(sh2, PDBCT_SH2, is_slave ? "ssh2" : "msh2");
  21. #ifdef DRC_SH2
  22. ret = sh2_drc_init(sh2);
  23. #endif
  24. return ret;
  25. }
  26. void sh2_finish(SH2 *sh2)
  27. {
  28. #ifdef DRC_SH2
  29. sh2_drc_finish(sh2);
  30. #endif
  31. }
  32. void sh2_reset(SH2 *sh2)
  33. {
  34. sh2->pc = p32x_sh2_read32(0, sh2);
  35. sh2->r[15] = p32x_sh2_read32(4, sh2);
  36. sh2->sr = I;
  37. sh2->vbr = 0;
  38. sh2->pending_int_irq = 0;
  39. }
  40. void sh2_do_irq(SH2 *sh2, int level, int vector)
  41. {
  42. sh2->sr &= 0x3f3;
  43. sh2->r[15] -= 4;
  44. p32x_sh2_write32(sh2->r[15], sh2->sr, sh2); /* push SR onto stack */
  45. sh2->r[15] -= 4;
  46. p32x_sh2_write32(sh2->r[15], sh2->pc, sh2); /* push PC onto stack */
  47. /* set I flags in SR */
  48. sh2->sr = (sh2->sr & ~I) | (level << 4);
  49. /* fetch PC */
  50. sh2->pc = p32x_sh2_read32(sh2->vbr + vector * 4, sh2);
  51. /* 13 cycles at best */
  52. sh2->icount -= 13;
  53. }
  54. int sh2_irl_irq(SH2 *sh2, int level, int nested_call)
  55. {
  56. int taken;
  57. sh2->pending_irl = level;
  58. if (level < sh2->pending_int_irq)
  59. level = sh2->pending_int_irq;
  60. sh2->pending_level = level;
  61. taken = (level > ((sh2->sr >> 4) & 0x0f));
  62. if (taken) {
  63. if (!nested_call) {
  64. // not in memhandler, so handle this now (recompiler friendly)
  65. // do this to avoid missing irqs that other SH2 might clear
  66. int vector = sh2->irq_callback(sh2, level);
  67. sh2_do_irq(sh2, level, vector);
  68. sh2->m68krcycles_done += C_SH2_TO_M68K(*sh2, 13);
  69. }
  70. else
  71. sh2->test_irq = 1;
  72. }
  73. return taken;
  74. }
  75. void sh2_internal_irq(SH2 *sh2, int level, int vector)
  76. {
  77. // FIXME: multiple internal irqs not handled..
  78. // assuming internal irqs never clear until accepted
  79. sh2->pending_int_irq = level;
  80. sh2->pending_int_vector = vector;
  81. if (level > sh2->pending_level)
  82. sh2->pending_level = level;
  83. sh2->test_irq = 1;
  84. }
  85. #define SH2_REG_SIZE (offsetof(SH2, macl) + sizeof(sh2->macl))
  86. void sh2_pack(const SH2 *sh2, unsigned char *buff)
  87. {
  88. unsigned int *p;
  89. memcpy(buff, sh2, SH2_REG_SIZE);
  90. p = (void *)(buff + SH2_REG_SIZE);
  91. p[0] = sh2->pending_int_irq;
  92. p[1] = sh2->pending_int_vector;
  93. }
  94. void sh2_unpack(SH2 *sh2, const unsigned char *buff)
  95. {
  96. unsigned int *p;
  97. memcpy(sh2, buff, SH2_REG_SIZE);
  98. p = (void *)(buff + SH2_REG_SIZE);
  99. sh2->pending_int_irq = p[0];
  100. sh2->pending_int_vector = p[1];
  101. sh2->test_irq = 1;
  102. }
  103. #ifdef DRC_CMP
  104. /* trace/compare */
  105. #include <stdio.h>
  106. #include <stdlib.h>
  107. #include <pico/memory.h>
  108. #undef _USE_CZ80 // HACK
  109. #include <pico/pico_int.h>
  110. #include <pico/debug.h>
  111. static SH2 sh2ref[2];
  112. static unsigned int mem_val;
  113. static unsigned int local_read32(SH2 *sh2, u32 a)
  114. {
  115. const sh2_memmap *sh2_map = sh2->read16_map;
  116. u16 *pd;
  117. uptr p;
  118. sh2_map += (a >> 25);
  119. p = sh2_map->addr;
  120. if (!map_flag_set(p)) {
  121. pd = (u16 *)((p << 1) + ((a & sh2_map->mask) & ~1));
  122. return (pd[0] << 16) | pd[1];
  123. }
  124. if ((a & 0xfffff000) == 0xc0000000) {
  125. // data array
  126. pd = (u16 *)sh2->data_array + (a & 0xfff) / 2;
  127. return (pd[0] << 16) | pd[1];
  128. }
  129. if ((a & 0xdfffffc0) == 0x4000) {
  130. pd = &Pico32x.regs[(a & 0x3f) / 2];
  131. return (pd[0] << 16) | pd[1];
  132. }
  133. if ((a & 0xdffffe00) == 0x4200) {
  134. pd = &Pico32xMem->pal[(a & 0x1ff) / 2];
  135. return (pd[0] << 16) | pd[1];
  136. }
  137. return 0;
  138. }
  139. void do_sh2_trace(SH2 *current, int cycles)
  140. {
  141. static int current_slave = -1;
  142. static u32 current_m68k_pc;
  143. SH2 *sh2o = &sh2ref[current->is_slave];
  144. u32 *regs_a = (void *)current;
  145. u32 *regs_o = (void *)sh2o;
  146. unsigned char v;
  147. u32 val;
  148. int i;
  149. if (SekPc != current_m68k_pc) {
  150. current_m68k_pc = SekPc;
  151. tl_write_uint(CTL_M68KPC, current_m68k_pc);
  152. }
  153. if (current->is_slave != current_slave) {
  154. current_slave = current->is_slave;
  155. v = CTL_MASTERSLAVE | current->is_slave;
  156. tl_write(&v, sizeof(v));
  157. }
  158. for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
  159. if (i == 17) // ppc
  160. continue;
  161. if (regs_a[i] != regs_o[i]) {
  162. tl_write_uint(CTL_SH2_R + i, regs_a[i]);
  163. regs_o[i] = regs_a[i];
  164. }
  165. }
  166. if (current->ea != sh2o->ea) {
  167. tl_write_uint(CTL_EA, current->ea);
  168. sh2o->ea = current->ea;
  169. }
  170. val = local_read32(current, current->ea);
  171. if (mem_val != val) {
  172. tl_write_uint(CTL_EAVAL, val);
  173. mem_val = val;
  174. }
  175. tl_write_uint(CTL_CYCLES, cycles);
  176. }
  177. static const char *regnames[] = {
  178. "r0", "r1", "r2", "r3",
  179. "r4", "r5", "r6", "r7",
  180. "r8", "r9", "r10", "r11",
  181. "r12", "r13", "r14", "r15",
  182. "pc", "ppc", "pr", "sr",
  183. "gbr", "vbr", "mach","macl",
  184. };
  185. static void dump_regs(SH2 *sh2)
  186. {
  187. char csh2;
  188. int i;
  189. csh2 = sh2->is_slave ? 's' : 'm';
  190. for (i = 0; i < 16/2; i++)
  191. printf("%csh2 r%d: %08x r%02d: %08x\n", csh2,
  192. i, sh2->r[i], i+8, sh2->r[i+8]);
  193. printf("%csh2 PC: %08x , %08x\n", csh2, sh2->pc, sh2->ppc);
  194. printf("%csh2 SR: %03x PR: %08x\n", csh2, sh2->sr, sh2->pr);
  195. }
  196. void do_sh2_cmp(SH2 *current)
  197. {
  198. static int current_slave;
  199. static u32 current_val;
  200. SH2 *sh2o = &sh2ref[current->is_slave];
  201. u32 *regs_a = (void *)current;
  202. u32 *regs_o = (void *)sh2o;
  203. unsigned char code;
  204. int cycles_o = 666;
  205. u32 sr, val;
  206. int bad = 0;
  207. int cycles;
  208. int i, ret;
  209. sh2ref[1].is_slave = 1;
  210. while (1) {
  211. ret = tl_read(&code, 1);
  212. if (ret <= 0)
  213. break;
  214. if (code == CTL_CYCLES) {
  215. tl_read(&cycles_o, 4);
  216. break;
  217. }
  218. switch (code) {
  219. case CTL_MASTERSLAVE:
  220. case CTL_MASTERSLAVE + 1:
  221. current_slave = code & 1;
  222. break;
  223. case CTL_EA:
  224. tl_read_uint(&sh2o->ea);
  225. break;
  226. case CTL_EAVAL:
  227. tl_read_uint(&current_val);
  228. break;
  229. case CTL_M68KPC:
  230. tl_read_uint(&val);
  231. if (SekPc != val) {
  232. printf("m68k: %08x %08x\n", SekPc, val);
  233. bad = 1;
  234. }
  235. break;
  236. default:
  237. if (CTL_SH2_R <= code && code < CTL_SH2_R +
  238. offsetof(SH2, read8_map) / 4)
  239. {
  240. tl_read_uint(regs_o + code - CTL_SH2_R);
  241. }
  242. else
  243. {
  244. printf("wrong code: %02x\n", code);
  245. goto end;
  246. }
  247. break;
  248. }
  249. }
  250. if (ret <= 0) {
  251. printf("EOF?\n");
  252. goto end;
  253. }
  254. if (current->is_slave != current_slave) {
  255. printf("bad slave: %d %d\n", current->is_slave,
  256. current_slave);
  257. bad = 1;
  258. }
  259. for (i = 0; i < offsetof(SH2, read8_map) / 4; i++) {
  260. if (i == 17 || i == 19) // ppc, sr
  261. continue;
  262. if (regs_a[i] != regs_o[i]) {
  263. printf("bad %4s: %08x %08x\n",
  264. regnames[i], regs_a[i], regs_o[i]);
  265. bad = 1;
  266. }
  267. }
  268. sr = current->sr & 0x3f3;
  269. cycles = (signed int)current->sr >> 12;
  270. if (sr != sh2o->sr) {
  271. printf("bad SR: %03x %03x\n", sr, sh2o->sr);
  272. bad = 1;
  273. }
  274. if (cycles != cycles_o) {
  275. printf("bad cycles: %d %d\n", cycles, cycles_o);
  276. bad = 1;
  277. }
  278. val = local_read32(current, sh2o->ea);
  279. if (val != current_val) {
  280. printf("bad val @%08x: %08x %08x\n", sh2o->ea, val, current_val);
  281. bad = 1;
  282. }
  283. if (!bad) {
  284. sh2o->ppc = current->pc;
  285. return;
  286. }
  287. end:
  288. printf("--\n");
  289. dump_regs(sh2o);
  290. if (current->is_slave != current_slave)
  291. dump_regs(&sh2ref[current->is_slave ^ 1]);
  292. PDebugDumpMem();
  293. exit(1);
  294. }
  295. #endif // DRC_CMP