dcbefcd3ca52d322107348d84cc5eb643311bb95
[picodrive.git] / Pico / carthw / svp / svp.c
1 // The SVP chip emulator
2
3 // (c) Copyright 2008, Grazvydas "notaz" Ignotas
4 // Free for non-commercial use.
5
6 // For commercial use, separate licencing terms must be obtained.
7
8
9 #include "../../PicoInt.h"
10
11 svp_t *svp = NULL;
12 int PicoSVPCycles = 1000; // cycles/line
13
14 static void PicoSVPReset(void)
15 {
16         elprintf(EL_SVP, "SVP reset");
17
18         memcpy(svp->iram_rom + 0x800, Pico.rom + 0x800, 0x20000 - 0x800);
19         ssp1601_reset(&svp->ssp1601);
20 }
21
22
23 static void PicoSVPLine(int count)
24 {
25         // ???
26         ssp1601_run(PicoSVPCycles * count);
27
28         // test mode
29         //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff;
30 }
31
32
33 static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp)
34 {
35         if (source < Pico.romsize) // Rom
36         {
37                 source -= 2;
38                 *srcp = (unsigned short *)(Pico.rom + (source&~1));
39                 *limitp = (unsigned short *)(Pico.rom + Pico.romsize);
40                 return 1;
41         }
42         else if ((source & 0xfe0000) == 0x300000)
43         {
44                 elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len);
45                 source &= 0x1fffe;
46                 source -= 2;
47                 *srcp = (unsigned short *)(svp->dram + source);
48                 *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram));
49                 return 1;
50         }
51         else
52                 elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len);
53
54         return 0;
55 }
56
57
58 void PicoSVPInit(void)
59 {
60         void *tmp;
61
62         elprintf(EL_SVP, "SVP init");
63
64         tmp = realloc(Pico.rom, 0x200000 + sizeof(*svp));
65         if (tmp == NULL)
66         {
67                 elprintf(EL_STATUS|EL_SVP, "OOM for SVP data");
68                 return;
69         }
70
71         Pico.rom = tmp;
72         svp = (void *) ((char *)tmp + 0x200000);
73         memset(svp, 0, sizeof(*svp));
74
75         // init ok, setup hooks..
76         PicoRead16Hook = PicoSVPRead16;
77         PicoWrite8Hook = PicoSVPWrite8;
78         PicoWrite16Hook = PicoSVPWrite16;
79         PicoDmaHook = PicoSVPDma;
80         PicoResetHook = PicoSVPReset;
81         PicoLineHook = PicoSVPLine;
82 }
83