SVP save support
[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 /* save state stuff */
15 typedef enum {
16         CHUNK_IRAM = CHUNK_CARTHW,
17         CHUNK_DRAM,
18         CHUNK_SSP
19 } chunk_name_e;
20
21 static carthw_state_chunk svp_states[] =
22 {
23         { CHUNK_IRAM, 0x800,                 NULL },
24         { CHUNK_DRAM, sizeof(svp->dram),     NULL },
25         { CHUNK_SSP,  sizeof(svp->ssp1601),  NULL },
26         { 0,          0,                     NULL }
27 };
28
29
30 static void PicoSVPReset(void)
31 {
32         elprintf(EL_SVP, "SVP reset");
33
34         memcpy(svp->iram_rom + 0x800, Pico.rom + 0x800, 0x20000 - 0x800);
35         ssp1601_reset(&svp->ssp1601);
36 }
37
38
39 static void PicoSVPLine(int count)
40 {
41         // ???
42         ssp1601_run(PicoSVPCycles * count);
43
44         // test mode
45         //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff;
46 }
47
48
49 static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp)
50 {
51         if (source < Pico.romsize) // Rom
52         {
53                 source -= 2;
54                 *srcp = (unsigned short *)(Pico.rom + (source&~1));
55                 *limitp = (unsigned short *)(Pico.rom + Pico.romsize);
56                 return 1;
57         }
58         else if ((source & 0xfe0000) == 0x300000)
59         {
60                 elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len);
61                 source &= 0x1fffe;
62                 source -= 2;
63                 *srcp = (unsigned short *)(svp->dram + source);
64                 *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram));
65                 return 1;
66         }
67         else
68                 elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len);
69
70         return 0;
71 }
72
73
74 void PicoSVPInit(void)
75 {
76         void *tmp;
77
78         elprintf(EL_SVP, "SVP init");
79
80         tmp = realloc(Pico.rom, 0x200000 + sizeof(*svp));
81         if (tmp == NULL)
82         {
83                 elprintf(EL_STATUS|EL_SVP, "OOM for SVP data");
84                 return;
85         }
86
87         Pico.rom = tmp;
88         svp = (void *) ((char *)tmp + 0x200000);
89         memset(svp, 0, sizeof(*svp));
90
91         // init ok, setup hooks..
92         PicoRead16Hook = PicoSVPRead16;
93         PicoWrite8Hook = PicoSVPWrite8;
94         PicoWrite16Hook = PicoSVPWrite16;
95         PicoDmaHook = PicoSVPDma;
96         PicoResetHook = PicoSVPReset;
97         PicoLineHook = PicoSVPLine;
98
99         // save state stuff
100         svp_states[0].ptr = svp->iram_rom;
101         svp_states[1].ptr = svp->dram;
102         svp_states[2].ptr = &svp->ssp1601;
103         carthw_chunks = svp_states;
104 }
105