1 // Pico Library - Internal Header File
\r
3 // (c) Copyright 2004 Dave, All rights reserved.
\r
4 // (c) Copyright 2006,2007 Grazvydas "notaz" Ignotas, all rights reserved.
\r
5 // Free for non-commercial use.
\r
7 // For commercial use, separate licencing terms must be obtained.
\r
9 #ifndef PICO_INTERNAL_INCLUDED
\r
10 #define PICO_INTERNAL_INCLUDED
\r
16 #include "carthw/carthw.h"
\r
19 #define USE_POLL_DETECT
\r
21 #ifndef PICO_INTERNAL
\r
22 #define PICO_INTERNAL
\r
24 #ifndef PICO_INTERNAL_ASM
\r
25 #define PICO_INTERNAL_ASM
\r
28 // to select core, define EMU_C68K, EMU_M68K or EMU_F68K in your makefile or project
\r
35 // ----------------------- 68000 CPU -----------------------
\r
37 #include "../cpu/Cyclone/Cyclone.h"
\r
38 extern struct Cyclone PicoCpuCM68k, PicoCpuCS68k;
\r
39 #define SekCyclesLeftNoMCD PicoCpuCM68k.cycles // cycles left for this run
\r
40 #define SekCyclesLeft \
\r
41 (((PicoMCD&1) && (PicoOpt & 0x2000)) ? (SekCycleAim-SekCycleCnt) : SekCyclesLeftNoMCD)
\r
42 #define SekCyclesLeftS68k \
\r
43 ((PicoOpt & 0x2000) ? (SekCycleAimS68k-SekCycleCntS68k) : PicoCpuCS68k.cycles)
\r
44 #define SekSetCyclesLeftNoMCD(c) PicoCpuCM68k.cycles=c
\r
45 #define SekSetCyclesLeft(c) { \
\r
46 if ((PicoMCD&1) && (PicoOpt & 0x2000)) SekCycleCnt=SekCycleAim-(c); else SekSetCyclesLeftNoMCD(c); \
\r
48 #define SekPc (PicoCpuCM68k.pc-PicoCpuCM68k.membase)
\r
49 #define SekPcS68k (PicoCpuCS68k.pc-PicoCpuCS68k.membase)
\r
50 #define SekSetStop(x) { PicoCpuCM68k.state_flags&=~1; if (x) { PicoCpuCM68k.state_flags|=1; PicoCpuCM68k.cycles=0; } }
\r
51 #define SekSetStopS68k(x) { PicoCpuCS68k.state_flags&=~1; if (x) { PicoCpuCS68k.state_flags|=1; PicoCpuCS68k.cycles=0; } }
\r
52 #define SekIsStoppedS68k() (PicoCpuCS68k.state_flags&1)
\r
53 #define SekShouldInterrupt (PicoCpuCM68k.irq > (PicoCpuCM68k.srh&7))
\r
55 #define SekInterrupt(i) PicoCpuCM68k.irq=i
\r
58 #define EMU_CORE_DEBUG
\r
63 #include "../cpu/fame/fame.h"
\r
64 extern M68K_CONTEXT PicoCpuFM68k, PicoCpuFS68k;
\r
65 #define SekCyclesLeftNoMCD PicoCpuFM68k.io_cycle_counter
\r
66 #define SekCyclesLeft \
\r
67 (((PicoMCD&1) && (PicoOpt & 0x2000)) ? (SekCycleAim-SekCycleCnt) : SekCyclesLeftNoMCD)
\r
68 #define SekCyclesLeftS68k \
\r
69 ((PicoOpt & 0x2000) ? (SekCycleAimS68k-SekCycleCntS68k) : PicoCpuFS68k.io_cycle_counter)
\r
70 #define SekSetCyclesLeftNoMCD(c) PicoCpuFM68k.io_cycle_counter=c
\r
71 #define SekSetCyclesLeft(c) { \
\r
72 if ((PicoMCD&1) && (PicoOpt & 0x2000)) SekCycleCnt=SekCycleAim-(c); else SekSetCyclesLeftNoMCD(c); \
\r
74 #define SekPc fm68k_get_pc(&PicoCpuFM68k)
\r
75 #define SekPcS68k fm68k_get_pc(&PicoCpuFS68k)
\r
76 #define SekSetStop(x) { \
\r
77 PicoCpuFM68k.execinfo &= ~FM68K_HALTED; \
\r
78 if (x) { PicoCpuFM68k.execinfo |= FM68K_HALTED; PicoCpuFM68k.io_cycle_counter = 0; } \
\r
80 #define SekSetStopS68k(x) { \
\r
81 PicoCpuFS68k.execinfo &= ~FM68K_HALTED; \
\r
82 if (x) { PicoCpuFS68k.execinfo |= FM68K_HALTED; PicoCpuFS68k.io_cycle_counter = 0; } \
\r
84 #define SekIsStoppedS68k() (PicoCpuFS68k.execinfo&FM68K_HALTED)
\r
85 #define SekShouldInterrupt fm68k_would_interrupt()
\r
87 #define SekInterrupt(irq) PicoCpuFM68k.interrupts[0]=irq
\r
90 #define EMU_CORE_DEBUG
\r
95 #include "../cpu/musashi/m68kcpu.h"
\r
96 extern m68ki_cpu_core PicoCpuMM68k, PicoCpuMS68k;
\r
97 #ifndef SekCyclesLeft
\r
98 #define SekCyclesLeftNoMCD PicoCpuMM68k.cyc_remaining_cycles
\r
99 #define SekCyclesLeft \
\r
100 (((PicoMCD&1) && (PicoOpt & 0x2000)) ? (SekCycleAim-SekCycleCnt) : SekCyclesLeftNoMCD)
\r
101 #define SekCyclesLeftS68k \
\r
102 ((PicoOpt & 0x2000) ? (SekCycleAimS68k-SekCycleCntS68k) : PicoCpuMS68k.cyc_remaining_cycles)
\r
103 #define SekSetCyclesLeftNoMCD(c) SET_CYCLES(c)
\r
104 #define SekSetCyclesLeft(c) { \
\r
105 if ((PicoMCD&1) && (PicoOpt & 0x2000)) SekCycleCnt=SekCycleAim-(c); else SET_CYCLES(c); \
\r
107 #define SekPc m68k_get_reg(&PicoCpuMM68k, M68K_REG_PC)
\r
108 #define SekPcS68k m68k_get_reg(&PicoCpuMS68k, M68K_REG_PC)
\r
109 #define SekSetStop(x) { \
\r
110 if(x) { SET_CYCLES(0); PicoCpuMM68k.stopped=STOP_LEVEL_STOP; } \
\r
111 else PicoCpuMM68k.stopped=0; \
\r
113 #define SekSetStopS68k(x) { \
\r
114 if(x) { SET_CYCLES(0); PicoCpuMS68k.stopped=STOP_LEVEL_STOP; } \
\r
115 else PicoCpuMS68k.stopped=0; \
\r
117 #define SekIsStoppedS68k() (PicoCpuMS68k.stopped==STOP_LEVEL_STOP)
\r
118 #define SekShouldInterrupt (CPU_INT_LEVEL > FLAG_INT_MASK)
\r
120 #define SekInterrupt(irq) { \
\r
121 void *oldcontext = m68ki_cpu_p; \
\r
122 m68k_set_context(&PicoCpuMM68k); \
\r
123 m68k_set_irq(irq); \
\r
124 m68k_set_context(oldcontext); \
\r
130 extern int SekCycleCnt; // cycles done in this frame
\r
131 extern int SekCycleAim; // cycle aim
\r
132 extern unsigned int SekCycleCntT; // total cycle counter, updated once per frame
\r
134 #define SekCyclesReset() { \
\r
135 SekCycleCntT+=SekCycleAim; \
\r
136 SekCycleCnt-=SekCycleAim; \
\r
139 #define SekCyclesBurn(c) SekCycleCnt+=c
\r
140 #define SekCyclesDone() (SekCycleAim-SekCyclesLeft) // nuber of cycles done in this frame (can be checked anywhere)
\r
141 #define SekCyclesDoneT() (SekCycleCntT+SekCyclesDone()) // total nuber of cycles done for this rom
\r
143 #define SekEndRun(after) { \
\r
144 SekCycleCnt -= SekCyclesLeft - after; \
\r
145 if(SekCycleCnt < 0) SekCycleCnt = 0; \
\r
146 SekSetCyclesLeft(after); \
\r
149 extern int SekCycleCntS68k;
\r
150 extern int SekCycleAimS68k;
\r
152 #define SekCyclesResetS68k() { \
\r
153 SekCycleCntS68k-=SekCycleAimS68k; \
\r
154 SekCycleAimS68k=0; \
\r
156 #define SekCyclesDoneS68k() (SekCycleAimS68k-SekCyclesLeftS68k)
\r
158 #ifdef EMU_CORE_DEBUG
\r
159 #undef SekSetCyclesLeftNoMCD
\r
160 #undef SekSetCyclesLeft
\r
161 #undef SekCyclesBurn
\r
163 #define SekSetCyclesLeftNoMCD(c)
\r
164 #define SekSetCyclesLeft(c)
\r
165 #define SekCyclesBurn(c) c
\r
166 #define SekEndRun(c)
\r
169 // ----------------------- Z80 CPU -----------------------
\r
171 #if defined(_USE_MZ80)
\r
172 #include "../../cpu/mz80/mz80.h"
\r
174 #define z80_run(cycles) mz80_run(cycles)
\r
175 #define z80_run_nr(cycles) mz80_run(cycles)
\r
176 #define z80_int() mz80int(0)
\r
177 #define z80_resetCycles() mz80GetElapsedTicks(1)
\r
179 #elif defined(_USE_DRZ80)
\r
180 #include "../../cpu/DrZ80/drz80.h"
\r
182 extern struct DrZ80 drZ80;
\r
184 #define z80_run(cycles) ((cycles) - DrZ80Run(&drZ80, cycles))
\r
185 #define z80_run_nr(cycles) DrZ80Run(&drZ80, cycles)
\r
186 #define z80_int() { \
\r
187 drZ80.z80irqvector = 0xFF; /* default IRQ vector RST opcode */ \
\r
188 drZ80.Z80_IRQ = 1; \
\r
190 #define z80_resetCycles()
\r
192 #elif defined(_USE_CZ80)
\r
193 #include "../../cpu/cz80/cz80.h"
\r
195 #define z80_run(cycles) Cz80_Exec(&CZ80, cycles)
\r
196 #define z80_run_nr(cycles) Cz80_Exec(&CZ80, cycles)
\r
197 #define z80_int() Cz80_Set_IRQ(&CZ80, 0, HOLD_LINE)
\r
198 #define z80_resetCycles()
\r
202 #define z80_run(cycles) (cycles)
\r
203 #define z80_run_nr(cycles)
\r
205 #define z80_resetCycles()
\r
209 // ---------------------------------------------------------
\r
211 extern int PicoMCD;
\r
213 // main oscillator clock which controls timing
\r
214 #define OSC_NTSC 53693100
\r
215 // seems to be accurate, see scans from http://www.hot.ee/tmeeco/
\r
216 #define OSC_PAL 53203424
\r
220 unsigned char reg[0x20];
\r
221 unsigned int command; // 32-bit Command
\r
222 unsigned char pending; // 1 if waiting for second half of 32-bit command
\r
223 unsigned char type; // Command type (v/c/vsram read/write)
\r
224 unsigned short addr; // Read/Write address
\r
225 int status; // Status bits
\r
226 unsigned char pending_ints; // pending interrupts: ??VH????
\r
227 signed char lwrite_cnt; // VDP write count during active display line
\r
228 unsigned char pad[0x12];
\r
233 unsigned char rotate;
\r
234 unsigned char z80Run;
\r
235 unsigned char padTHPhase[2]; // 02 phase of gamepad TH switches
\r
236 short scanline; // 04 0 to 261||311; -1 in fast mode
\r
237 char dirtyPal; // 06 Is the palette dirty (1 - change @ this frame, 2 - some time before)
\r
238 unsigned char hardware; // 07 Hardware value for country
\r
239 unsigned char pal; // 08 1=PAL 0=NTSC
\r
240 unsigned char sram_reg; // SRAM mode register. bit0: allow read? bit1: deny write? bit2: EEPROM? bit4: detected? (header or by access)
\r
241 unsigned short z80_bank68k; // 0a
\r
242 unsigned short z80_lastaddr; // this is for Z80 faking
\r
243 unsigned char z80_fakeval;
\r
244 unsigned char pad0;
\r
245 unsigned char padDelay[2]; // 10 gamepad phase time outs, so we count a delay
\r
246 unsigned short eeprom_addr; // EEPROM address register
\r
247 unsigned char eeprom_cycle; // EEPROM SRAM cycle number
\r
248 unsigned char eeprom_slave; // EEPROM slave word for X24C02 and better SRAMs
\r
249 unsigned char prot_bytes[2]; // simple protection faking
\r
250 unsigned short dma_xfers;
\r
251 unsigned char pad[2];
\r
252 unsigned int frame_count; // mainly for movies
\r
255 // some assembly stuff depend on these, do not touch!
\r
258 unsigned char ram[0x10000]; // 0x00000 scratch ram
\r
259 unsigned short vram[0x8000]; // 0x10000
\r
260 unsigned char zram[0x2000]; // 0x20000 Z80 ram
\r
261 unsigned char ioports[0x10];
\r
262 unsigned int pad[0x3c]; // unused
\r
263 unsigned short cram[0x40]; // 0x22100
\r
264 unsigned short vsram[0x40]; // 0x22180
\r
266 unsigned char *rom; // 0x22200
\r
267 unsigned int romsize; // 0x22204
\r
270 struct PicoVideo video;
\r
276 unsigned char *data; // actual data
\r
277 unsigned int start; // start address in 68k address space
\r
279 unsigned char unused1; // 0c: unused
\r
280 unsigned char unused2;
\r
281 unsigned char changed;
\r
282 unsigned char eeprom_type; // eeprom type: 0: 7bit (24C01), 2: device with 2 addr words (X24C02+), 3: dev with 3 addr words
\r
283 unsigned char eeprom_abits; // eeprom access must be odd addr for: bit0 ~ cl, bit1 ~ out
\r
284 unsigned char eeprom_bit_cl; // bit number for cl
\r
285 unsigned char eeprom_bit_in; // bit number for in
\r
286 unsigned char eeprom_bit_out; // bit number for out
\r
290 #include "cd/cd_sys.h"
\r
291 #include "cd/LC89510.h"
\r
292 #include "cd/gfx_cd.h"
\r
296 unsigned char control; // reg7
\r
297 unsigned char enabled; // reg8
\r
298 unsigned char cur_ch;
\r
299 unsigned char bank;
\r
302 struct pcm_chan // 08, size 0x10
\r
304 unsigned char regs[8];
\r
305 unsigned int addr; // .08: played sample address
\r
312 unsigned short hint_vector;
\r
313 unsigned char busreq;
\r
314 unsigned char s68k_pend_ints;
\r
315 unsigned int state_flags; // 04: emu state: reset_pending, dmna_pending
\r
316 unsigned int counter75hz;
\r
317 unsigned short audio_offset; // 0c: for savestates: play pointer offset (0-1023)
\r
318 unsigned char audio_track; // playing audio track # (zero based)
\r
320 int timer_int3; // 10
\r
321 unsigned int timer_stopwatch;
\r
322 unsigned char bcram_reg; // 18: battery-backed RAM cart register
\r
323 unsigned char pad2;
\r
324 unsigned short pad3;
\r
330 unsigned char bios[0x20000]; // 000000: 128K
\r
331 union { // 020000: 512K
\r
332 unsigned char prg_ram[0x80000];
\r
333 unsigned char prg_ram_b[4][0x20000];
\r
335 union { // 0a0000: 256K
\r
337 unsigned char word_ram2M[0x40000];
\r
338 unsigned char unused[0x20000];
\r
341 unsigned char unused[0x20000];
\r
342 unsigned char word_ram1M[2][0x20000];
\r
345 union { // 100000: 64K
\r
346 unsigned char pcm_ram[0x10000];
\r
347 unsigned char pcm_ram_b[0x10][0x1000];
\r
349 unsigned char s68k_regs[0x200]; // 110000: GA, not CPU regs
\r
350 unsigned char bram[0x2000]; // 110200: 8K
\r
351 struct mcd_misc m; // 112200: misc
\r
352 struct mcd_pcm pcm; // 112240:
\r
353 _scd_toc TOC; // not to be saved
\r
360 #define Pico_mcd ((mcd_state *)Pico.rom)
\r
363 PICO_INTERNAL int PicoAreaPackCpu(unsigned char *cpu, int is_sub);
\r
364 PICO_INTERNAL int PicoAreaUnpackCpu(unsigned char *cpu, int is_sub);
\r
367 PICO_INTERNAL int PicoCdSaveState(void *file);
\r
368 PICO_INTERNAL int PicoCdLoadState(void *file);
\r
371 PICO_INTERNAL void PicoCartDetect(void);
\r
374 int CM_compareRun(int cyc, int is_sub);
\r
377 PICO_INTERNAL int PicoLine(int scan);
\r
378 PICO_INTERNAL void PicoFrameStart(void);
\r
381 PICO_INTERNAL void PicoFrameFull();
\r
384 PICO_INTERNAL int PicoInitPc(unsigned int pc);
\r
385 PICO_INTERNAL_ASM unsigned int PicoRead32(unsigned int a);
\r
386 PICO_INTERNAL void PicoMemSetup(void);
\r
387 PICO_INTERNAL_ASM void PicoMemReset(void);
\r
388 PICO_INTERNAL void PicoMemResetHooks(void);
\r
389 PICO_INTERNAL int PadRead(int i);
\r
390 PICO_INTERNAL unsigned char z80_read(unsigned short a);
\r
392 PICO_INTERNAL_ASM void z80_write(unsigned char data, unsigned short a);
\r
393 PICO_INTERNAL void z80_write16(unsigned short data, unsigned short a);
\r
394 PICO_INTERNAL unsigned short z80_read16(unsigned short a);
\r
396 PICO_INTERNAL_ASM void z80_write(unsigned int a, unsigned char data);
\r
398 extern unsigned int (*PicoRead16Hook)(unsigned int a, int realsize);
\r
399 extern void (*PicoWrite8Hook) (unsigned int a,unsigned int d,int realsize);
\r
400 extern void (*PicoWrite16Hook)(unsigned int a,unsigned int d,int realsize);
\r
403 PICO_INTERNAL void PicoMemSetupCD(void);
\r
404 PICO_INTERNAL_ASM void PicoMemResetCD(int r3);
\r
405 PICO_INTERNAL_ASM void PicoMemResetCDdecode(int r3);
\r
408 extern struct Pico Pico;
\r
409 extern struct PicoSRAM SRam;
\r
410 extern int emustatus;
\r
411 extern int z80startCycle, z80stopCycle; // in 68k cycles
\r
412 extern void (*PicoResetHook)(void);
\r
413 extern void (*PicoLineHook)(void);
\r
414 PICO_INTERNAL int CheckDMA(void);
\r
417 PICO_INTERNAL int PicoInitMCD(void);
\r
418 PICO_INTERNAL void PicoExitMCD(void);
\r
419 PICO_INTERNAL int PicoResetMCD(int hard);
\r
420 PICO_INTERNAL int PicoFrameMCD(void);
\r
423 PICO_INTERNAL int SekInit(void);
\r
424 PICO_INTERNAL int SekReset(void);
\r
425 PICO_INTERNAL void SekState(int *data);
\r
426 PICO_INTERNAL void SekSetRealTAS(int use_real);
\r
429 PICO_INTERNAL int SekInitS68k(void);
\r
430 PICO_INTERNAL int SekResetS68k(void);
\r
431 PICO_INTERNAL int SekInterruptS68k(int irq);
\r
434 extern int PsndLen_exc_cnt;
\r
435 extern int PsndLen_exc_add;
\r
438 PICO_INTERNAL_ASM void PicoVideoWrite(unsigned int a,unsigned short d);
\r
439 PICO_INTERNAL_ASM unsigned int PicoVideoRead(unsigned int a);
\r
440 extern int (*PicoDmaHook)(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp);
\r
443 PICO_INTERNAL void SRAMWriteEEPROM(unsigned int d);
\r
444 PICO_INTERNAL void SRAMUpdPending(unsigned int a, unsigned int d);
\r
445 PICO_INTERNAL_ASM unsigned int SRAMReadEEPROM(void);
\r
446 PICO_INTERNAL_ASM void memcpy16(unsigned short *dest, unsigned short *src, int count);
\r
447 PICO_INTERNAL_ASM void memcpy16bswap(unsigned short *dest, void *src, int count);
\r
448 PICO_INTERNAL_ASM void memcpy32(int *dest, int *src, int count); // 32bit word count
\r
449 PICO_INTERNAL_ASM void memset32(int *dest, int c, int count);
\r
452 PICO_INTERNAL_ASM void wram_2M_to_1M(unsigned char *m);
\r
453 PICO_INTERNAL_ASM void wram_1M_to_2M(unsigned char *m);
\r
456 PICO_INTERNAL void PicoCDBufferRead(void *dest, int lba);
\r
459 PICO_INTERNAL void PsndReset(void);
\r
460 PICO_INTERNAL void Psnd_timers_and_dac(int raster);
\r
461 PICO_INTERNAL int PsndRender(int offset, int length);
\r
462 PICO_INTERNAL void PsndClear(void);
\r
463 // z80 functionality wrappers
\r
464 PICO_INTERNAL void z80_init(void);
\r
465 PICO_INTERNAL void z80_pack(unsigned char *data);
\r
466 PICO_INTERNAL void z80_unpack(unsigned char *data);
\r
467 PICO_INTERNAL void z80_reset(void);
\r
468 PICO_INTERNAL void z80_exit(void);
\r
472 } // End of extern "C"
\r
475 // emulation event logging
\r
477 #define EL_LOGMASK 0
\r
480 #define EL_HVCNT 0x00000001 /* hv counter reads */
\r
481 #define EL_SR 0x00000002 /* SR reads */
\r
482 #define EL_INTS 0x00000004 /* ints and acks */
\r
483 #define EL_YM2612R 0x00000008 /* 68k ym2612 reads */
\r
484 #define EL_INTSW 0x00000010 /* log irq switching on/off */
\r
485 #define EL_ASVDP 0x00000020 /* VDP accesses during active scan */
\r
486 #define EL_VDPDMA 0x00000040 /* VDP DMA transfers and their timing */
\r
487 #define EL_BUSREQ 0x00000080 /* z80 busreq r/w or reset w */
\r
488 #define EL_Z80BNK 0x00000100 /* z80 i/o through bank area */
\r
489 #define EL_SRAMIO 0x00000200 /* sram i/o */
\r
490 #define EL_EEPROM 0x00000400 /* eeprom debug */
\r
491 #define EL_UIO 0x00000800 /* unmapped i/o */
\r
492 #define EL_IO 0x00001000 /* all i/o */
\r
493 #define EL_CDPOLL 0x00002000 /* MCD: log poll detection */
\r
494 #define EL_SVP 0x00004000 /* SVP stuff */
\r
496 #define EL_STATUS 0x40000000 /* status messages */
\r
497 #define EL_ANOMALY 0x80000000 /* some unexpected conditions (during emulation) */
\r
500 extern void lprintf(const char *fmt, ...);
\r
501 #define elprintf(w,f,...) \
\r
503 if ((w) & EL_LOGMASK) \
\r
504 lprintf("%05i:%03i: " f "\n",Pico.m.frame_count,Pico.m.scanline,##__VA_ARGS__); \
\r
507 #define elprintf(w,f,...)
\r
510 #endif // PICO_INTERNAL_INCLUDED
\r