sh2.c 7.3 KB

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