some new cpu debug code
[picodrive.git] / pico / pico_cmn.c
1 /*
2  * common code for pico.c and cd/pico.c
3  * (C) notaz, 2007-2009
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8
9 #define CYCLES_M68K_LINE     488 // suitable for both PAL/NTSC
10 #define CYCLES_M68K_VINT_LAG  68
11 #define CYCLES_M68K_ASD      148
12 #define CYCLES_S68K_LINE     795
13 #define CYCLES_S68K_VINT_LAG 111
14 #define CYCLES_S68K_ASD      241
15
16 // pad delay (for 6 button pads)
17 #define PAD_DELAY \
18   if (PicoOpt&POPT_6BTN_PAD) { \
19     if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
20     if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
21   }
22
23 // CPUS_RUN
24 #ifndef CPUS_RUN
25 #define CPUS_RUN(m68k_cycles,s68k_cycles) \
26   SekRunM68k(m68k_cycles)
27 #endif
28
29 static __inline void SekRunM68k(int cyc)
30 {
31   int cyc_do;
32   pprof_start(m68k);
33   pevt_log_m68k_o(EVT_RUN_START);
34
35   SekCycleAim+=cyc;
36   if ((cyc_do=SekCycleAim-SekCycleCnt) <= 0)
37     goto out;
38
39 #if defined(EMU_CORE_DEBUG)
40   // this means we do run-compare
41   SekCycleCnt+=CM_compareRun(cyc_do, 0);
42 #elif defined(EMU_C68K)
43   PicoCpuCM68k.cycles=cyc_do;
44   CycloneRun(&PicoCpuCM68k);
45   SekCycleCnt+=cyc_do-PicoCpuCM68k.cycles;
46 #elif defined(EMU_M68K)
47   SekCycleCnt+=m68k_execute(cyc_do);
48 #elif defined(EMU_F68K)
49   SekCycleCnt+=fm68k_emulate(cyc_do+1, 0, 0);
50 #endif
51
52 out:
53   SekTrace(0);
54   pevt_log_m68k_o(EVT_RUN_END);
55   pprof_end(m68k);
56 }
57
58 static int PicoFrameHints(void)
59 {
60   struct PicoVideo *pv=&Pico.video;
61   int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
62   int hint; // Hint counter
63
64   pevt_log_m68k_o(EVT_FRAME_START);
65   pv->v_counter = Pico.m.scanline = 0;
66
67   if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
68     // draw a frame just after vblank in alternative render mode
69     // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
70     PicoFrameFull();
71 #ifdef DRAW_FINISH_FUNC
72     DRAW_FINISH_FUNC();
73 #endif
74     skip = 1;
75   }
76   else skip=PicoSkipFrame;
77
78   if (Pico.m.pal) {
79     line_sample = 68;
80     if (pv->reg[1]&8) lines_vis = 240;
81   } else {
82     line_sample = 93;
83   }
84
85   SekCyclesReset();
86   z80_resetCycles();
87 #ifdef PICO_CD
88   SekCyclesResetS68k();
89 #endif
90   PsndDacLine = 0;
91   emustatus &= ~1;
92
93   pv->status&=~0x88; // clear V-Int, come out of vblank
94
95   hint=pv->reg[10]; // Load H-Int counter
96   //dprintf("-hint: %i", hint);
97
98   // This is to make active scan longer (needed for Double Dragon 2, mainly)
99   CPUS_RUN(CYCLES_M68K_ASD, CYCLES_S68K_ASD);
100
101   for (y = 0; y < lines_vis; y++)
102   {
103     pv->v_counter = Pico.m.scanline = y;
104     if ((pv->reg[12]&6) == 6) { // interlace mode 2
105       pv->v_counter <<= 1;
106       pv->v_counter |= pv->v_counter >> 8;
107       pv->v_counter &= 0xff;
108     }
109
110     // VDP FIFO
111     pv->lwrite_cnt -= 12;
112     if (pv->lwrite_cnt <= 0) {
113       pv->lwrite_cnt=0;
114       Pico.video.status|=0x200;
115     }
116
117     PAD_DELAY
118 #ifdef PICO_CD
119     check_cd_dma();
120 #endif
121
122     // H-Interrupts:
123     if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
124     {
125       hint=pv->reg[10]; // Reload H-Int counter
126       pv->pending_ints|=0x10;
127       if (pv->reg[0]&0x10) {
128         elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
129         SekInterrupt(4);
130       }
131     }
132
133     // decide if we draw this line
134     if (!skip && (PicoOpt & POPT_ALT_RENDERER))
135     {
136       // find the right moment for frame renderer, when display is no longer blanked
137       if ((pv->reg[1]&0x40) || y > 100) {
138         PicoFrameFull();
139 #ifdef DRAW_FINISH_FUNC
140         DRAW_FINISH_FUNC();
141 #endif
142         skip = 1;
143       }
144     }
145
146     // get samples from sound chips
147     if ((y == 224 || y == line_sample) && PsndOut)
148     {
149       if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
150         PicoSyncZ80(SekCycleCnt);
151       if (ym2612.dacen && PsndDacLine <= y)
152         PsndDoDAC(y);
153 #ifdef PICO_32X
154       p32x_sync_sh2s(SekCyclesDoneT2());
155 #endif
156       PsndGetSamples(y);
157     }
158
159     // Run scanline:
160     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
161     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
162
163 #ifdef PICO_CD
164     update_chips();
165 #else
166     if (PicoLineHook) PicoLineHook();
167 #endif
168     pevt_log_m68k_o(EVT_NEXT_LINE);
169   }
170
171   if (!skip)
172   {
173     if (DrawScanline < y)
174       PicoDrawSync(y - 1, 0);
175 #ifdef DRAW_FINISH_FUNC
176     DRAW_FINISH_FUNC();
177 #endif
178   }
179
180   // V-int line (224 or 240)
181   Pico.m.scanline = y;
182   pv->v_counter = 0xe0; // bad for 240 mode
183   if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1;
184
185   // VDP FIFO
186   pv->lwrite_cnt=0;
187   Pico.video.status|=0x200;
188
189   memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
190   PAD_DELAY
191 #ifdef PICO_CD
192   check_cd_dma();
193 #endif
194
195   // Last H-Int:
196   if (--hint < 0)
197   {
198     hint=pv->reg[10]; // Reload H-Int counter
199     pv->pending_ints|=0x10;
200     //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
201     if (pv->reg[0]&0x10) SekInterrupt(4);
202   }
203
204   pv->status|=0x08; // go into vblank
205   pv->pending_ints|=0x20;
206
207   // the following SekRun is there for several reasons:
208   // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
209   // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
210   // also delay between last H-int and V-int (Golden Axe 3)
211   CPUS_RUN(CYCLES_M68K_VINT_LAG, CYCLES_S68K_VINT_LAG);
212
213   if (pv->reg[1]&0x20) {
214     elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
215     SekInterrupt(6);
216   }
217   if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
218     PicoSyncZ80(SekCycleCnt);
219     elprintf(EL_INTS, "zint");
220     z80_int();
221   }
222
223 #ifdef PICO_32X
224   p32x_sync_sh2s(SekCyclesDoneT2());
225   p32x_start_blank();
226 #endif
227
228   // get samples from sound chips
229   if (y == 224 && PsndOut)
230   {
231     if (ym2612.dacen && PsndDacLine <= y)
232       PsndDoDAC(y);
233     PsndGetSamples(y);
234   }
235
236   // Run scanline:
237   if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
238   CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
239     CYCLES_S68K_LINE - CYCLES_S68K_VINT_LAG - CYCLES_S68K_ASD);
240
241 #ifdef PICO_CD
242   update_chips();
243 #else
244   if (PicoLineHook) PicoLineHook();
245 #endif
246   pevt_log_m68k_o(EVT_NEXT_LINE);
247
248   lines = scanlines_total;
249   vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens, TODO: verify
250
251   for (y++; y < lines; y++)
252   {
253     pv->v_counter = Pico.m.scanline = y;
254     if (y >= vcnt_wrap)
255       pv->v_counter -= Pico.m.pal ? 56 : 6;
256     if ((pv->reg[12]&6) == 6)
257       pv->v_counter = (pv->v_counter << 1) | 1;
258     pv->v_counter &= 0xff;
259
260     PAD_DELAY
261 #ifdef PICO_CD
262     check_cd_dma();
263 #endif
264
265     // Run scanline:
266     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
267     CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
268
269 #ifdef PICO_CD
270     update_chips();
271 #else
272     if (PicoLineHook) PicoLineHook();
273 #endif
274     pevt_log_m68k_o(EVT_NEXT_LINE);
275   }
276
277   // sync z80
278   if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
279     PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
280   if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
281     PsndDoDAC(lines-1);
282
283 #ifdef PICO_32X
284   p32x_sync_sh2s(SekCyclesDoneT2());
285 #endif
286   timers_cycle();
287
288   return 0;
289 }
290
291 #undef PAD_DELAY
292 #undef CPUS_RUN
293