some more risky timing changes
[picodrive.git] / pico / pico / pico.c
1 /*
2  * PicoDrive
3  * (C) notaz, 2008
4  *
5  * This work is licensed under the terms of MAME license.
6  * See COPYING file in the top-level directory.
7  */
8 #include "../pico_int.h"
9
10 // x: 0x03c - 0x19d
11 // y: 0x1fc - 0x2f7
12 //    0x2f8 - 0x3f3
13 picohw_state PicoPicohw;
14
15 static int prev_line_cnt_irq3 = 0, prev_line_cnt_irq5 = 0;
16 static int fifo_bytes_line = (16000<<16)/60/262/2;
17
18 static const int guessed_rates[] = { 8000, 14000, 12000, 14000, 16000, 18000, 16000, 16000 }; // ?
19
20 #define PICOHW_FIFO_IRQ_THRESHOLD 12
21
22 PICO_INTERNAL void PicoReratePico(void)
23 {
24   int rate = guessed_rates[PicoPicohw.r12 & 7];
25   if (Pico.m.pal)
26        fifo_bytes_line = (rate<<16)/50/313/2;
27   else fifo_bytes_line = (rate<<16)/60/262/2;
28   PicoPicoPCMRerate(rate);
29 }
30
31 static void PicoLinePico(void)
32 {
33   PicoPicohw.line_counter++;
34
35 #if 1
36   if ((PicoPicohw.r12 & 0x4003) && PicoPicohw.line_counter - prev_line_cnt_irq3 > 200) {
37     prev_line_cnt_irq3 = PicoPicohw.line_counter;
38     // just a guess/hack, allows 101 Dalmantians to boot
39     elprintf(EL_PICOHW, "irq3");
40     SekInterrupt(3);
41     return;
42   }
43 #endif
44
45   if (PicoPicohw.fifo_bytes > 0)
46   {
47     PicoPicohw.fifo_line_bytes += fifo_bytes_line;
48     if (PicoPicohw.fifo_line_bytes >= (1<<16)) {
49       PicoPicohw.fifo_bytes -= PicoPicohw.fifo_line_bytes >> 16;
50       PicoPicohw.fifo_line_bytes &= 0xffff;
51       if (PicoPicohw.fifo_bytes < 0)
52         PicoPicohw.fifo_bytes = 0;
53     }
54   }
55   else
56     PicoPicohw.fifo_line_bytes = 0;
57
58 #if 1
59   if (PicoPicohw.fifo_bytes_prev >= PICOHW_FIFO_IRQ_THRESHOLD &&
60       PicoPicohw.fifo_bytes < PICOHW_FIFO_IRQ_THRESHOLD) {
61     prev_line_cnt_irq3 = PicoPicohw.line_counter; // ?
62     elprintf(EL_PICOHW, "irq3, fb=%i", PicoPicohw.fifo_bytes);
63     SekInterrupt(3);
64   }
65   PicoPicohw.fifo_bytes_prev = PicoPicohw.fifo_bytes;
66 #endif
67
68 #if 0
69   if (PicoPicohw.line_counter - prev_line_cnt_irq5 > 512) {
70     prev_line_cnt_irq5 = PicoPicohw.line_counter;
71     elprintf(EL_PICOHW, "irq5");
72     SekInterrupt(5);
73   }
74 #endif
75 }
76
77 static void PicoResetPico(void)
78 {
79   PicoPicoPCMReset();
80   PicoPicohw.xpcm_ptr = PicoPicohw.xpcm_buffer;
81 }
82
83 PICO_INTERNAL void PicoInitPico(void)
84 {
85   elprintf(EL_STATUS, "Pico startup");
86   PicoLineHook = PicoLinePico;
87   PicoResetHook = PicoResetPico;
88
89   PicoAHW = PAHW_PICO;
90   memset(&PicoPicohw, 0, sizeof(PicoPicohw));
91   PicoPicohw.pen_pos[0] = 0x03c + 320/2;
92   PicoPicohw.pen_pos[1] = 0x200 + 240/2;
93   prev_line_cnt_irq3 = prev_line_cnt_irq5 = 0;
94
95   // map version register
96   PicoDetectRegion();
97   switch (Pico.m.hardware >> 6) {
98     case 0: PicoPicohw.r1 = 0x00; break;
99     case 1: PicoPicohw.r1 = 0x00; break;
100     case 2: PicoPicohw.r1 = 0x40; break;
101     case 3: PicoPicohw.r1 = 0x20; break;
102   }
103 }
104