PicoInt.h 8.8 KB

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