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