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