new acc mode timing, VDP FIFO code
[picodrive.git] / Pico / PicoFrameHints.c
1 #define CYCLES_M68K_LINE     488 // suitable for both PAL/NTSC
2 #define CYCLES_M68K_VINT_LAG  68
3 #define CYCLES_M68K_ASD      148
4 #define CYCLES_Z80_LINE      228
5 #define CYCLES_Z80_ASD        69
6
7 // pad delay (for 6 button pads)
8 #define PAD_DELAY \
9   if (PicoOpt&0x20) { \
10     if(Pico.m.padDelay[0]++ > 25) Pico.m.padTHPhase[0]=0; \
11     if(Pico.m.padDelay[1]++ > 25) Pico.m.padTHPhase[1]=0; \
12   }
13
14 #define Z80_RUN(z80_cycles) \
15 { \
16   if ((PicoOpt&4) && Pico.m.z80Run) \
17   { \
18     if (Pico.m.z80Run & 2) z80CycleAim += z80_cycles; \
19     else { \
20       int cnt = SekCyclesDone() - z80startCycle; \
21       cnt = (cnt>>1)-(cnt>>5); \
22       if (cnt > (z80_cycles)) cnt = z80_cycles; \
23       Pico.m.z80Run |= 2; \
24       z80CycleAim+=cnt; \
25     } \
26     total_z80+=z80_run(z80CycleAim-total_z80); \
27   } \
28 }
29
30 // Accurate but slower frame which does hints
31 static int PicoFrameHints(void)
32 {
33   struct PicoVideo *pv=&Pico.video;
34   int total_z80=0,lines,y,lines_vis = 224,z80CycleAim = 0,line_sample;
35   int skip=PicoSkipFrame || (PicoOpt&0x10);
36   int hint; // Hint counter
37
38   if (Pico.m.pal) {
39     //cycles_68k = (int) ((double) OSC_PAL  /  7 / 50 / 312 + 0.4); // should compile to a constant (488)
40     //cycles_z80 = (int) ((double) OSC_PAL  / 15 / 50 / 312 + 0.4); // 228
41     line_sample = 68;
42     if(pv->reg[1]&8) lines_vis = 240;
43   } else {
44     //cycles_68k = (int) ((double) OSC_NTSC /  7 / 60 / 262 + 0.4); // 488
45     //cycles_z80 = (int) ((double) OSC_NTSC / 15 / 60 / 262 + 0.4); // 228
46     line_sample = 93;
47   }
48
49   SekCyclesReset();
50
51   pv->status&=~0x88; // clear V-Int, come out of vblank
52
53   hint=pv->reg[10]; // Load H-Int counter
54   //dprintf("-hint: %i", hint);
55
56   // This is to make active scan longer (needed for Double Dragon 2, mainly)
57   SekRun(CYCLES_M68K_ASD);
58   Z80_RUN(CYCLES_Z80_ASD);
59
60   for (y=0;y<lines_vis;y++)
61   {
62     Pico.m.scanline=(short)y;
63
64     // VDP FIFO
65     pv->lwrite_cnt -= 12;
66     if (pv->lwrite_cnt <= 0) {
67       pv->lwrite_cnt=0;
68       Pico.video.status|=0x200;
69     }
70
71     PAD_DELAY
72
73     // H-Interrupts:
74     if (--hint < 0) // y <= lines_vis: Comix Zone, Golden Axe
75     {
76       hint=pv->reg[10]; // Reload H-Int counter
77       pv->pending_ints|=0x10;
78       if (pv->reg[0]&0x10) {
79         elprintf(EL_INTS, "hint: @ %06x [%i]", SekPc, SekCycleCnt);
80         SekInterrupt(4);
81       }
82     }
83
84     // decide if we draw this line
85 #if CAN_HANDLE_240_LINES
86     if(!skip && ((!(pv->reg[1]&8) && y<224) || (pv->reg[1]&8)) )
87 #else
88     if(!skip && y<224)
89 #endif
90       PicoLine(y);
91
92     if(PicoOpt&1)
93       sound_timers_and_dac(y);
94
95     // get samples from sound chips
96     if(y == 32 && PsndOut)
97       emustatus &= ~1;
98     else if((y == 224 || y == line_sample) && PsndOut)
99       getSamples(y);
100
101     // Run scanline:
102     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
103     SekRun(CYCLES_M68K_LINE);
104     Z80_RUN(CYCLES_Z80_LINE);
105   }
106
107   // V-int line (224 or 240)
108   Pico.m.scanline=(short)y;
109
110   // VDP FIFO
111   pv->lwrite_cnt=0;
112   Pico.video.status|=0x200;
113
114   PAD_DELAY
115
116   // Last H-Int:
117   if (--hint < 0)
118   {
119     hint=pv->reg[10]; // Reload H-Int counter
120     pv->pending_ints|=0x10;
121     //printf("rhint: %i @ %06x [%i|%i]\n", hint, SekPc, y, SekCycleCnt);
122     if (pv->reg[0]&0x10) SekInterrupt(4);
123   }
124
125   // V-Interrupt:
126   pv->status|=0x08; // go into vblank
127   pv->pending_ints|=0x20;
128
129   // the following SekRun is there for several reasons:
130   // there must be a delay after vblank bit is set and irq is asserted (Mazin Saga)
131   // also delay between F bit (bit 7) is set in SR and IRQ happens (Ex-Mutants)
132   // also delay between last H-int and V-int (Golden Axe 3)
133   SekRun(CYCLES_M68K_VINT_LAG);
134   if (pv->reg[1]&0x20) {
135     elprintf(EL_INTS, "vint: @ %06x [%i]", SekPc, SekCycleCnt);
136     SekInterrupt(6);
137   }
138   if (Pico.m.z80Run && (PicoOpt&4))
139     z80_int();
140
141   if (PicoOpt&1)
142     sound_timers_and_dac(y);
143
144   // get samples from sound chips
145   if ((y == 224) && PsndOut)
146     getSamples(y);
147
148   // Run scanline:
149   if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
150   SekRun(CYCLES_M68K_LINE - CYCLES_M68K_VINT_LAG - CYCLES_M68K_ASD);
151   Z80_RUN(CYCLES_Z80_LINE - CYCLES_Z80_ASD);
152
153   // PAL line count might actually be 313 according to Steve Snake, but that would complicate things.
154   lines = Pico.m.pal ? 312 : 262;
155
156   for (y++;y<lines;y++)
157   {
158     Pico.m.scanline=(short)y;
159
160     PAD_DELAY
161
162     if(PicoOpt&1)
163       sound_timers_and_dac(y);
164
165     // Run scanline:
166     if (Pico.m.dma_xfers) SekCyclesBurn(CheckDMA());
167     SekRun(CYCLES_M68K_LINE);
168     Z80_RUN(CYCLES_Z80_LINE);
169   }
170
171   // draw a frame just after vblank in alternative render mode
172   if (!PicoSkipFrame && (PicoOpt&0x10))
173     PicoFrameFull();
174
175   return 0;
176 }
177
178 #undef PAD_DELAY
179 #undef Z80_RUN
180