PicoInt.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. // Pico Library - Internal Header File
  2. // (c) Copyright 2004 Dave, All rights reserved.
  3. // (c) Copyright 2006,2007 Grazvydas "notaz" Ignotas, all rights reserved.
  4. // Free for non-commercial use.
  5. // For commercial use, separate licencing terms must be obtained.
  6. #ifndef PICO_INTERNAL_INCLUDED
  7. #define PICO_INTERNAL_INCLUDED
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include "Pico.h"
  12. //
  13. #define USE_POLL_DETECT
  14. #ifndef PICO_INTERNAL
  15. #define PICO_INTERNAL
  16. #endif
  17. #ifndef PICO_INTERNAL_ASM
  18. #define PICO_INTERNAL_ASM
  19. #endif
  20. // to select core, define EMU_C68K, EMU_M68K or EMU_A68K in your makefile or project
  21. #ifdef __cplusplus
  22. extern "C" {
  23. #endif
  24. // ----------------------- 68000 CPU -----------------------
  25. #ifdef EMU_C68K
  26. #include "../cpu/Cyclone/Cyclone.h"
  27. extern struct Cyclone PicoCpu, PicoCpuS68k;
  28. #define SekCyclesLeftNoMCD PicoCpu.cycles // cycles left for this run
  29. #define SekCyclesLeft \
  30. (((PicoMCD&1) && (PicoOpt & 0x2000)) ? (SekCycleAim-SekCycleCnt) : SekCyclesLeftNoMCD)
  31. #define SekCyclesLeftS68k \
  32. ((PicoOpt & 0x2000) ? (SekCycleAimS68k-SekCycleCntS68k) : PicoCpuS68k.cycles)
  33. #define SekSetCyclesLeftNoMCD(c) PicoCpu.cycles=c
  34. #define SekSetCyclesLeft(c) { \
  35. if ((PicoMCD&1) && (PicoOpt & 0x2000)) SekCycleCnt=SekCycleAim-(c); else SekSetCyclesLeftNoMCD(c); \
  36. }
  37. #define SekPc (PicoCpu.pc-PicoCpu.membase)
  38. #define SekPcS68k (PicoCpuS68k.pc-PicoCpuS68k.membase)
  39. #define SekSetStop(x) { PicoCpu.state_flags&=~1; if (x) { PicoCpu.state_flags|=1; PicoCpu.cycles=0; } }
  40. #define SekSetStopS68k(x) { PicoCpuS68k.state_flags&=~1; if (x) { PicoCpuS68k.state_flags|=1; PicoCpuS68k.cycles=0; } }
  41. #endif
  42. #ifdef EMU_A68K
  43. void __cdecl M68000_RUN();
  44. // The format of the data in a68k.asm (at the _M68000_regs location)
  45. struct A68KContext
  46. {
  47. unsigned int d[8],a[8];
  48. unsigned int isp,srh,ccr,xc,pc,irq,sr;
  49. int (*IrqCallback) (int nIrq);
  50. unsigned int ppc;
  51. void *pResetCallback;
  52. unsigned int sfc,dfc,usp,vbr;
  53. unsigned int AsmBank,CpuVersion;
  54. };
  55. struct A68KContext M68000_regs;
  56. extern int m68k_ICount;
  57. #define SekCyclesLeft m68k_ICount
  58. #define SekSetCyclesLeft(c) m68k_ICount=c
  59. #define SekPc M68000_regs.pc
  60. #endif
  61. #ifdef EMU_M68K
  62. #include "../cpu/musashi/m68kcpu.h"
  63. extern m68ki_cpu_core PicoM68kCPU; // MD's CPU
  64. extern m68ki_cpu_core PicoS68kCPU; // Mega CD's CPU
  65. #ifndef SekCyclesLeft
  66. #define SekCyclesLeftNoMCD PicoM68kCPU.cyc_remaining_cycles
  67. #define SekCyclesLeft \
  68. (((PicoMCD&1) && (PicoOpt & 0x2000)) ? (SekCycleAim-SekCycleCnt) : SekCyclesLeftNoMCD)
  69. #define SekCyclesLeftS68k \
  70. ((PicoOpt & 0x2000) ? (SekCycleAimS68k-SekCycleCntS68k) : PicoS68kCPU.cyc_remaining_cycles)
  71. #define SekSetCyclesLeftNoMCD(c) SET_CYCLES(c)
  72. #define SekSetCyclesLeft(c) { \
  73. if ((PicoMCD&1) && (PicoOpt & 0x2000)) SekCycleCnt=SekCycleAim-(c); else SET_CYCLES(c); \
  74. }
  75. #define SekPc m68k_get_reg(&PicoM68kCPU, M68K_REG_PC)
  76. #define SekPcS68k m68k_get_reg(&PicoS68kCPU, M68K_REG_PC)
  77. #define SekSetStop(x) { \
  78. if(x) { SET_CYCLES(0); PicoM68kCPU.stopped=STOP_LEVEL_STOP; } \
  79. else PicoM68kCPU.stopped=0; \
  80. }
  81. #define SekSetStopS68k(x) { \
  82. if(x) { SET_CYCLES(0); PicoS68kCPU.stopped=STOP_LEVEL_STOP; } \
  83. else PicoS68kCPU.stopped=0; \
  84. }
  85. #endif
  86. #endif
  87. extern int SekCycleCnt; // cycles done in this frame
  88. extern int SekCycleAim; // cycle aim
  89. extern unsigned int SekCycleCntT; // total cycle counter, updated once per frame
  90. #define SekCyclesReset() {SekCycleCntT+=SekCycleCnt;SekCycleCnt=SekCycleAim=0;}
  91. #define SekCyclesBurn(c) SekCycleCnt+=c
  92. #define SekCyclesDone() (SekCycleAim-SekCyclesLeft) // nuber of cycles done in this frame (can be checked anywhere)
  93. #define SekCyclesDoneT() (SekCycleCntT+SekCyclesDone()) // total nuber of cycles done for this rom
  94. #define SekEndRun(after) { \
  95. SekCycleCnt -= SekCyclesLeft - after; \
  96. if(SekCycleCnt < 0) SekCycleCnt = 0; \
  97. SekSetCyclesLeft(after); \
  98. }
  99. extern int SekCycleCntS68k;
  100. extern int SekCycleAimS68k;
  101. #define SekCyclesResetS68k() {SekCycleCntS68k=SekCycleAimS68k=0;}
  102. #define SekCyclesDoneS68k() (SekCycleAimS68k-SekCyclesLeftS68k)
  103. // debug cyclone
  104. #if defined(EMU_C68K) && defined(EMU_M68K)
  105. #undef SekSetCyclesLeftNoMCD
  106. #undef SekSetCyclesLeft
  107. #undef SekCyclesBurn
  108. #undef SekEndRun
  109. #define SekSetCyclesLeftNoMCD(c)
  110. #define SekSetCyclesLeft(c)
  111. #define SekCyclesBurn(c) c
  112. #define SekEndRun(c)
  113. #endif
  114. extern int PicoMCD;
  115. // ---------------------------------------------------------
  116. // main oscillator clock which controls timing
  117. #define OSC_NTSC 53693100
  118. #define OSC_PAL 53203424 // not accurate
  119. struct PicoVideo
  120. {
  121. unsigned char reg[0x20];
  122. unsigned int command; // 32-bit Command
  123. unsigned char pending; // 1 if waiting for second half of 32-bit command
  124. unsigned char type; // Command type (v/c/vsram read/write)
  125. unsigned short addr; // Read/Write address
  126. int status; // Status bits
  127. unsigned char pending_ints; // pending interrupts: ??VH????
  128. unsigned char pad[0x13];
  129. };
  130. struct PicoMisc
  131. {
  132. unsigned char rotate;
  133. unsigned char z80Run;
  134. unsigned char padTHPhase[2]; // phase of gamepad TH switches
  135. short scanline; // 0 to 261||311; -1 in fast mode
  136. char dirtyPal; // Is the palette dirty (1 - change @ this frame, 2 - some time before)
  137. unsigned char hardware; // Hardware value for country
  138. unsigned char pal; // 1=PAL 0=NTSC
  139. unsigned char sram_reg; // SRAM mode register. bit0: allow read? bit1: deny write? bit2: EEPROM? bit4: detected? (header or by access)
  140. unsigned short z80_bank68k;
  141. unsigned short z80_lastaddr; // this is for Z80 faking
  142. unsigned char z80_fakeval;
  143. unsigned char pad0;
  144. unsigned char padDelay[2]; // gamepad phase time outs, so we count a delay
  145. unsigned short sram_addr; // EEPROM address register
  146. unsigned char sram_cycle; // EEPROM SRAM cycle number
  147. unsigned char sram_slave; // EEPROM slave word for X24C02 and better SRAMs
  148. unsigned char prot_bytes[2]; // simple protection faking
  149. unsigned short dma_bytes; //
  150. unsigned char pad[2];
  151. unsigned int frame_count; // mainly for movies
  152. };
  153. // some assembly stuff depend on these, do not touch!
  154. struct Pico
  155. {
  156. unsigned char ram[0x10000]; // 0x00000 scratch ram
  157. unsigned short vram[0x8000]; // 0x10000
  158. unsigned char zram[0x2000]; // 0x20000 Z80 ram
  159. unsigned char ioports[0x10];
  160. unsigned int pad[0x3c]; // unused
  161. unsigned short cram[0x40]; // 0x22100
  162. unsigned short vsram[0x40]; // 0x22180
  163. unsigned char *rom; // 0x22200
  164. unsigned int romsize; // 0x22204
  165. struct PicoMisc m;
  166. struct PicoVideo video;
  167. };
  168. // sram
  169. struct PicoSRAM
  170. {
  171. unsigned char *data; // actual data
  172. unsigned int start; // start address in 68k address space
  173. unsigned int end;
  174. unsigned char resize; // 0c: 1=SRAM size changed and needs to be reallocated on PicoReset
  175. unsigned char reg_back; // copy of Pico.m.sram_reg to set after reset
  176. unsigned char changed;
  177. unsigned char pad;
  178. };
  179. // MCD
  180. #include "cd/cd_sys.h"
  181. #include "cd/LC89510.h"
  182. #include "cd/gfx_cd.h"
  183. struct mcd_pcm
  184. {
  185. unsigned char control; // reg7
  186. unsigned char enabled; // reg8
  187. unsigned char cur_ch;
  188. unsigned char bank;
  189. int pad1;
  190. struct pcm_chan // 08, size 0x10
  191. {
  192. unsigned char regs[8];
  193. unsigned int addr; // .08: played sample address
  194. int pad;
  195. } ch[8];
  196. };
  197. struct mcd_misc
  198. {
  199. unsigned short hint_vector;
  200. unsigned char busreq;
  201. unsigned char s68k_pend_ints;
  202. unsigned int state_flags; // 04: emu state: reset_pending, dmna_pending
  203. unsigned int counter75hz;
  204. unsigned short audio_offset; // 0c: for savestates: play pointer offset (0-1023)
  205. unsigned char audio_track; // playing audio track # (zero based)
  206. char pad1;
  207. int timer_int3; // 10
  208. unsigned int timer_stopwatch;
  209. unsigned char bcram_reg; // 18: battery-backed RAM cart register
  210. unsigned char pad2;
  211. unsigned short pad3;
  212. int pad[9];
  213. };
  214. typedef struct
  215. {
  216. unsigned char bios[0x20000]; // 000000: 128K
  217. union { // 020000: 512K
  218. unsigned char prg_ram[0x80000];
  219. unsigned char prg_ram_b[4][0x20000];
  220. };
  221. union { // 0a0000: 256K
  222. struct {
  223. unsigned char word_ram2M[0x40000];
  224. unsigned char unused[0x20000];
  225. };
  226. struct {
  227. unsigned char unused[0x20000];
  228. unsigned char word_ram1M[2][0x20000];
  229. };
  230. };
  231. union { // 100000: 64K
  232. unsigned char pcm_ram[0x10000];
  233. unsigned char pcm_ram_b[0x10][0x1000];
  234. };
  235. unsigned char s68k_regs[0x200]; // 110000: GA, not CPU regs
  236. unsigned char bram[0x2000]; // 110200: 8K
  237. struct mcd_misc m; // 112200: misc
  238. struct mcd_pcm pcm; // 112240:
  239. _scd_toc TOC; // not to be saved
  240. CDD cdd;
  241. CDC cdc;
  242. _scd scd;
  243. Rot_Comp rot_comp;
  244. } mcd_state;
  245. #define Pico_mcd ((mcd_state *)Pico.rom)
  246. // Area.c
  247. PICO_INTERNAL int PicoAreaPackCpu(unsigned char *cpu, int is_sub);
  248. PICO_INTERNAL int PicoAreaUnpackCpu(unsigned char *cpu, int is_sub);
  249. // cd/Area.c
  250. PICO_INTERNAL int PicoCdSaveState(void *file);
  251. PICO_INTERNAL int PicoCdLoadState(void *file);
  252. // Draw.c
  253. PICO_INTERNAL int PicoLine(int scan);
  254. PICO_INTERNAL void PicoFrameStart(void);
  255. // Draw2.c
  256. PICO_INTERNAL void PicoFrameFull();
  257. // Memory.c
  258. PICO_INTERNAL int PicoInitPc(unsigned int pc);
  259. PICO_INTERNAL_ASM unsigned int CPU_CALL PicoRead32(unsigned int a);
  260. PICO_INTERNAL void PicoMemSetup(void);
  261. PICO_INTERNAL_ASM void PicoMemReset(void);
  262. PICO_INTERNAL unsigned char z80_read(unsigned short a);
  263. PICO_INTERNAL unsigned short z80_read16(unsigned short a);
  264. PICO_INTERNAL_ASM void z80_write(unsigned char data, unsigned short a);
  265. PICO_INTERNAL void z80_write16(unsigned short data, unsigned short a);
  266. // cd/Memory.c
  267. PICO_INTERNAL void PicoMemSetupCD(void);
  268. PICO_INTERNAL_ASM void PicoMemResetCD(int r3);
  269. PICO_INTERNAL_ASM void PicoMemResetCDdecode(int r3);
  270. // Pico.c
  271. extern struct Pico Pico;
  272. extern struct PicoSRAM SRam;
  273. extern int emustatus;
  274. extern int z80startCycle, z80stopCycle; // in 68k cycles
  275. PICO_INTERNAL int CheckDMA(void);
  276. // cd/Pico.c
  277. PICO_INTERNAL int PicoInitMCD(void);
  278. PICO_INTERNAL int PicoResetMCD(int hard);
  279. PICO_INTERNAL int PicoFrameMCD(void);
  280. // Sek.c
  281. PICO_INTERNAL int SekInit(void);
  282. PICO_INTERNAL int SekReset(void);
  283. PICO_INTERNAL int SekInterrupt(int irq);
  284. PICO_INTERNAL void SekState(unsigned char *data);
  285. PICO_INTERNAL void SekSetRealTAS(int use_real);
  286. // cd/Sek.c
  287. PICO_INTERNAL int SekInitS68k(void);
  288. PICO_INTERNAL int SekResetS68k(void);
  289. PICO_INTERNAL int SekInterruptS68k(int irq);
  290. // sound/sound.c
  291. extern int PsndLen_exc_cnt;
  292. extern int PsndLen_exc_add;
  293. // VideoPort.c
  294. PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d);
  295. PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a);
  296. // Misc.c
  297. PICO_INTERNAL void SRAMWriteEEPROM(unsigned int d);
  298. PICO_INTERNAL void SRAMUpdPending(unsigned int a, unsigned int d);
  299. PICO_INTERNAL_ASM unsigned int SRAMReadEEPROM(void);
  300. PICO_INTERNAL_ASM void memcpy16(unsigned short *dest, unsigned short *src, int count);
  301. PICO_INTERNAL_ASM void memcpy16bswap(unsigned short *dest, void *src, int count);
  302. PICO_INTERNAL_ASM void memcpy32(int *dest, int *src, int count); // 32bit word count
  303. PICO_INTERNAL_ASM void memset32(int *dest, int c, int count);
  304. // cd/Misc.c
  305. PICO_INTERNAL_ASM void wram_2M_to_1M(unsigned char *m);
  306. PICO_INTERNAL_ASM void wram_1M_to_2M(unsigned char *m);
  307. // cd/buffering.c
  308. PICO_INTERNAL void PicoCDBufferRead(void *dest, int lba);
  309. // sound/sound.c
  310. PICO_INTERNAL void sound_reset(void);
  311. PICO_INTERNAL void sound_timers_and_dac(int raster);
  312. PICO_INTERNAL int sound_render(int offset, int length);
  313. PICO_INTERNAL void sound_clear(void);
  314. // z80 functionality wrappers
  315. PICO_INTERNAL void z80_init(void);
  316. PICO_INTERNAL void z80_resetCycles(void);
  317. PICO_INTERNAL void z80_int(void);
  318. PICO_INTERNAL int z80_run(int cycles);
  319. PICO_INTERNAL void z80_pack(unsigned char *data);
  320. PICO_INTERNAL void z80_unpack(unsigned char *data);
  321. PICO_INTERNAL void z80_reset(void);
  322. PICO_INTERNAL void z80_exit(void);
  323. #ifdef __cplusplus
  324. } // End of extern "C"
  325. #endif
  326. #endif // PICO_INTERNAL_INCLUDED