some more risky timing changes
[picodrive.git] / pico / pico / pico.c
CommitLineData
cff531af 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 */
efcba75f 8#include "../pico_int.h"
9037e45d 9
406c96c5 10// x: 0x03c - 0x19d
11// y: 0x1fc - 0x2f7
12// 0x2f8 - 0x3f3
d49b10c2 13picohw_state PicoPicohw;
14
15static int prev_line_cnt_irq3 = 0, prev_line_cnt_irq5 = 0;
213c16ad 16static int fifo_bytes_line = (16000<<16)/60/262/2;
d49b10c2 17
fa22af4c 18static const int guessed_rates[] = { 8000, 14000, 12000, 14000, 16000, 18000, 16000, 16000 }; // ?
19
20#define PICOHW_FIFO_IRQ_THRESHOLD 12
21
ed367a3f 22PICO_INTERNAL void PicoReratePico(void)
23{
fa22af4c 24 int rate = guessed_rates[PicoPicohw.r12 & 7];
ed367a3f 25 if (Pico.m.pal)
e42a47e2 26 fifo_bytes_line = (rate<<16)/50/313/2;
213c16ad 27 else fifo_bytes_line = (rate<<16)/60/262/2;
28 PicoPicoPCMRerate(rate);
ed367a3f 29}
30
b0677887 31static void PicoLinePico(void)
d49b10c2 32{
b0677887 33 PicoPicohw.line_counter++;
d49b10c2 34
1e6b5e39 35#if 1
36 if ((PicoPicohw.r12 & 0x4003) && PicoPicohw.line_counter - prev_line_cnt_irq3 > 200) {
d49b10c2 37 prev_line_cnt_irq3 = PicoPicohw.line_counter;
38 // just a guess/hack, allows 101 Dalmantians to boot
fa22af4c 39 elprintf(EL_PICOHW, "irq3");
1e6b5e39 40 SekInterrupt(3);
1e6b5e39 41 return;
d49b10c2 42 }
43#endif
44
ef4eb506 45 if (PicoPicohw.fifo_bytes > 0)
1e6b5e39 46 {
b0677887 47 PicoPicohw.fifo_line_bytes += fifo_bytes_line;
ef4eb506 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 }
1e6b5e39 54 }
ef4eb506 55 else
56 PicoPicohw.fifo_line_bytes = 0;
1e6b5e39 57
fa22af4c 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
d49b10c2 68#if 0
69 if (PicoPicohw.line_counter - prev_line_cnt_irq5 > 512) {
70 prev_line_cnt_irq5 = PicoPicohw.line_counter;
fa22af4c 71 elprintf(EL_PICOHW, "irq5");
d49b10c2 72 SekInterrupt(5);
73 }
74#endif
75}
406c96c5 76
ef4eb506 77static void PicoResetPico(void)
78{
79 PicoPicoPCMReset();
80 PicoPicohw.xpcm_ptr = PicoPicohw.xpcm_buffer;
81}
82
2aa27095 83PICO_INTERNAL void PicoInitPico(void)
9037e45d 84{
45f2f245 85 elprintf(EL_STATUS, "Pico startup");
ef4eb506 86 PicoLineHook = PicoLinePico;
87 PicoResetHook = PicoResetPico;
d49b10c2 88
9037e45d 89 PicoAHW = PAHW_PICO;
d49b10c2 90 memset(&PicoPicohw, 0, sizeof(PicoPicohw));
213c16ad 91 PicoPicohw.pen_pos[0] = 0x03c + 320/2;
92 PicoPicohw.pen_pos[1] = 0x200 + 240/2;
ef4eb506 93 prev_line_cnt_irq3 = prev_line_cnt_irq5 = 0;
9037e45d 94
1e6b5e39 95 // map version register
96 PicoDetectRegion();
1e6b5e39 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 }
9037e45d 103}
104