debug bgm player, sound code refactoring
[picodrive.git] / pico / pico_cmn.c
CommitLineData
8b99ab90 1// common code for Pico.c and cd/Pico.c
fad24893 2// (c) Copyright 2007,2008 Grazvydas "notaz" Ignotas
8b99ab90 3
69996cb7 4#define CYCLES_M68K_LINE 488 // suitable for both PAL/NTSC
5#define CYCLES_M68K_VINT_LAG 68
6#define CYCLES_M68K_ASD 148
bf5fbbb4 7#define CYCLES_S68K_LINE 795
8#define CYCLES_S68K_ASD 241
69996cb7 9
10// pad delay (for 6 button pads)
11#define PAD_DELAY \
602133e1 12 if (PicoOpt&POPT_6BTN_PAD) { \
69996cb7 13 if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
14 if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
15 }
16
bf5fbbb4 17// CPUS_RUN
18#ifndef PICO_CD
538a6098 19#define CPUS_RUN(m68k_cycles,s68k_cycles) \
4b9c5888 20 SekRunM68k(m68k_cycles);
bf5fbbb4 21#else
538a6098 22#define CPUS_RUN(m68k_cycles,s68k_cycles) \
bf5fbbb4 23{ \
602133e1 24 if ((PicoOpt&POPT_EN_MCD_PSYNC) && (Pico_mcd->m.busreq&3) == 1) { \
bf5fbbb4 25 SekRunPS(m68k_cycles, s68k_cycles); /* "better/perfect sync" */ \
26 } else { \
27 SekRunM68k(m68k_cycles); \
28 if ((Pico_mcd->m.busreq&3) == 1) /* no busreq/no reset */ \
29 SekRunS68k(s68k_cycles); \
30 } \
bf5fbbb4 31}
32#endif
33
69996cb7 34// Accurate but slower frame which does hints
35static int PicoFrameHints(void)
36{
37 struct PicoVideo *pv=&Pico.video;
9761a7d0 38 int lines, y, lines_vis = 224, line_sample, skip, vcnt_wrap;
69996cb7 39 int hint; // Hint counter
40
9761a7d0 41 pv->v_counter = Pico.m.scanline = 0;
538a6098 42
602133e1 43 if ((PicoOpt&POPT_ALT_RENDERER) && !PicoSkipFrame && (pv->reg[1]&0x40)) { // fast rend., display enabled
03e4f2a3 44 // draw a frame just after vblank in alternative render mode
45 // yes, this will cause 1 frame lag, but this is inaccurate mode anyway.
46 PicoFrameFull();
47#ifdef DRAW_FINISH_FUNC
48 DRAW_FINISH_FUNC();
49#endif
50 skip = 1;
51 }
52 else skip=PicoSkipFrame;
53
69996cb7 54 if (Pico.m.pal) {
69996cb7 55 line_sample = 68;
9761a7d0 56 if (pv->reg[1]&8) lines_vis = 240;
69996cb7 57 } else {
69996cb7 58 line_sample = 93;
59 }
60
61 SekCyclesReset();
4b9c5888 62 z80_resetCycles();
bf5fbbb4 63#ifdef PICO_CD
64 SekCyclesResetS68k();
65#endif
4b9c5888 66 PsndDacLine = 0;
7b3f44c6 67 emustatus &= ~1;
69996cb7 68
69 pv->status&=~0x88; // clear V-Int, come out of vblank
70
71 hint=pv->reg[10]; // Load H-Int counter
72 //dprintf("-hint: %i", hint);
73
74 // This is to make active scan longer (needed for Double Dragon 2, mainly)
538a6098 75 CPUS_RUN(CYCLES_M68K_ASD, CYCLES_S68K_ASD);
69996cb7 76
b6d7ac70 77 for (y = 0; y < lines_vis; y++)
69996cb7 78 {
9761a7d0 79 pv->v_counter = Pico.m.scanline = y;
80 if ((pv->reg[12]&6) == 6) { // interlace mode 2
81 pv->v_counter <<= 1;
82 pv->v_counter |= pv->v_counter >> 8;
83 pv->v_counter &= 0xff;
84 }
69996cb7 85
86 // VDP FIFO
87 pv->lwrite_cnt -= 12;
88 if (pv->lwrite_cnt <= 0) {
89 pv->lwrite_cnt=0;
90 Pico.video.status|=0x200;
91 }
92
93 PAD_DELAY
bf5fbbb4 94#ifdef PICO_CD
95 check_cd_dma();
96#endif
69996cb7 97
98 // H-Interrupts:
99 if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
100 {
101 hint=pv->reg[10]; // Reload H-Int counter
102 pv->pending_ints|=0x10;
103 if (pv->reg[0]&0x10) {
104 elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
105 SekInterrupt(4);
106 }
107 }
108
109 // decide if we draw this line
b6d7ac70 110 if (!skip && (PicoOpt & POPT_ALT_RENDERER))
fad24893 111 {
b6d7ac70 112 // find the right moment for frame renderer, when display is no longer blanked
113 if ((pv->reg[1]&0x40) || y > 100) {
114 PicoFrameFull();
fad24893 115#ifdef DRAW_FINISH_FUNC
b6d7ac70 116 DRAW_FINISH_FUNC();
fad24893 117#endif
b6d7ac70 118 skip = 1;
fad24893 119 }
120 }
69996cb7 121
69996cb7 122 // get samples from sound chips
7b3f44c6 123 if ((y == 224 || y == line_sample) && PsndOut)
4b9c5888 124 {
be297089 125 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
4b9c5888 126 PicoSyncZ80(SekCycleCnt);
127 if (ym2612.dacen && PsndDacLine <= y)
128 PsndDoDAC(y);
7b3f44c6 129 PsndGetSamples(y);
4b9c5888 130 }
69996cb7 131
132 // Run scanline:
133 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
538a6098 134 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
bf5fbbb4 135
136#ifdef PICO_CD
137 update_chips();
017512f2 138#else
b0677887 139 if (PicoLineHook) PicoLineHook();
bf5fbbb4 140#endif
69996cb7 141 }
142
03e4f2a3 143 if (!skip)
b6d7ac70 144 {
145 if (DrawScanline < y)
146 PicoDrawSync(y - 1, 0);
147#ifdef DRAW_FINISH_FUNC
03e4f2a3 148 DRAW_FINISH_FUNC();
8ab3e3c1 149#endif
b6d7ac70 150 }
8ab3e3c1 151
947fb5f9 152 // V-int line (224 or 240)
153 Pico.m.scanline = y;
154 pv->v_counter = 0xe0; // bad for 240 mode
155 if ((pv->reg[12]&6) == 6) pv->v_counter = 0xc1;
156
69996cb7 157 // VDP FIFO
158 pv->lwrite_cnt=0;
159 Pico.video.status|=0x200;
160
5f9a0d16 161 memcpy(PicoPadInt, PicoPad, sizeof(PicoPadInt));
69996cb7 162 PAD_DELAY
bf5fbbb4 163#ifdef PICO_CD
164 check_cd_dma();
165#endif
69996cb7 166
167 // Last H-Int:
168 if (--hint < 0)
169 {
170 hint=pv->reg[10]; // Reload H-Int counter
171 pv->pending_ints|=0x10;
172 //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
173 if (pv->reg[0]&0x10) SekInterrupt(4);
174 }
175
69996cb7 176 pv->status|=0x08; // go into vblank
177 pv->pending_ints|=0x20;
178
179 // the following SekRun is there for several reasons:
180 // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
181 // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
182 // also delay between last H-int and V-int (Golden Axe 3)
bf5fbbb4 183 SekRunM68k(CYCLES_M68K_VINT_LAG);
4b9c5888 184
69996cb7 185 if (pv->reg[1]&0x20) {
186 elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
187 SekInterrupt(6);
188 }
be297089 189 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80)) {
4b9c5888 190 PicoSyncZ80(SekCycleCnt);
583ab72c 191 elprintf(EL_INTS, "zint");
69996cb7 192 z80_int();
583ab72c 193 }
69996cb7 194
69996cb7 195 // get samples from sound chips
7b3f44c6 196 if (y == 224 && PsndOut)
197 {
198 if (ym2612.dacen && PsndDacLine <= y)
199 PsndDoDAC(y);
200 PsndGetSamples(y);
201 }
69996cb7 202
203 // Run scanline:
204 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
bf5fbbb4 205 CPUS_RUN(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD,
538a6098 206 CYCLES_S68K_LINE - CYCLES_S68K_ASD);
bf5fbbb4 207
208#ifdef PICO_CD
017512f2 209 update_chips();
210#else
b0677887 211 if (PicoLineHook) PicoLineHook();
bf5fbbb4 212#endif
69996cb7 213
214 // PAL line count might actually be 313 according to Steve Snake, but that would complicate things.
215 lines = Pico.m.pal ? 312 : 262;
9761a7d0 216 vcnt_wrap = Pico.m.pal ? 0x103 : 0xEB; // based on Gens
69996cb7 217
b6d7ac70 218 for (y++; y < lines; y++)
69996cb7 219 {
9761a7d0 220 pv->v_counter = Pico.m.scanline = y;
221 if (y >= vcnt_wrap)
222 pv->v_counter -= Pico.m.pal ? 56 : 6;
223 if ((pv->reg[12]&6) == 6)
224 pv->v_counter = (pv->v_counter << 1) | 1;
225 pv->v_counter &= 0xff;
69996cb7 226
227 PAD_DELAY
bf5fbbb4 228#ifdef PICO_CD
229 check_cd_dma();
230#endif
69996cb7 231
69996cb7 232 // Run scanline:
233 if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
538a6098 234 CPUS_RUN(CYCLES_M68K_LINE, CYCLES_S68K_LINE);
bf5fbbb4 235
236#ifdef PICO_CD
237 update_chips();
017512f2 238#else
b0677887 239 if (PicoLineHook) PicoLineHook();
bf5fbbb4 240#endif
69996cb7 241 }
242
4b9c5888 243 // sync z80
be297089 244 if (Pico.m.z80Run && !Pico.m.z80_reset && (PicoOpt&POPT_EN_Z80))
e53704e6 245 PicoSyncZ80(Pico.m.pal ? 151809 : 127671); // cycles adjusted for converter
4b9c5888 246 if (PsndOut && ym2612.dacen && PsndDacLine <= lines-1)
247 PsndDoDAC(lines-1);
248
e53704e6 249 timers_cycle();
250
69996cb7 251 return 0;
252}
253
254#undef PAD_DELAY
bf5fbbb4 255#undef CPUS_RUN
69996cb7 256