d1e7f8f204d8904c8fcd2e534147a320f3563351
[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 "../../pico_int.h"
10 #include "compiler.h"
11
12 svp_t *svp = NULL;
13 int PicoSVPCycles = 850; // cycles/line, just a guess
14 static int svp_dyn_ready = 0;
15
16 /* save state stuff */
17 typedef enum {
18         CHUNK_IRAM = CHUNK_CARTHW,
19         CHUNK_DRAM,
20         CHUNK_SSP
21 } chunk_name_e;
22
23 static carthw_state_chunk svp_states[] =
24 {
25         { CHUNK_IRAM, 0x800,                 NULL },
26         { CHUNK_DRAM, sizeof(svp->dram),     NULL },
27         { CHUNK_SSP,  sizeof(svp->ssp1601) - sizeof(svp->ssp1601.drc),  NULL },
28         { 0,          0,                     NULL }
29 };
30
31
32 static void PicoSVPReset(void)
33 {
34         elprintf(EL_SVP, "SVP reset");
35
36         memcpy(svp->iram_rom + 0x800, Pico.rom + 0x800, 0x20000 - 0x800);
37         ssp1601_reset(&svp->ssp1601);
38 #ifndef PSP
39         if ((PicoOpt&POPT_EN_SVP_DRC) && svp_dyn_ready)
40                 ssp1601_dyn_reset(&svp->ssp1601);
41 #endif
42 }
43
44
45 static void PicoSVPLine(void)
46 {
47         int count = 1;
48 #if defined(ARM) || defined(PSP)
49         // performance hack
50         static int delay_lines = 0;
51         delay_lines++;
52         if ((Pico.m.scanline&0xf) != 0xf && Pico.m.scanline != 261 && Pico.m.scanline != 311)
53                 return;
54         count = delay_lines;
55         delay_lines = 0;
56 #endif
57
58 #ifndef PSP
59         if ((PicoOpt&POPT_EN_SVP_DRC) && svp_dyn_ready)
60                 ssp1601_dyn_run(PicoSVPCycles * count);
61         else
62 #endif
63         {
64                 ssp1601_run(PicoSVPCycles * count);
65                 svp_dyn_ready = 0; // just in case
66         }
67
68         // test mode
69         //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff;
70 }
71
72
73 static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp)
74 {
75         if (source < Pico.romsize) // Rom
76         {
77                 source -= 2;
78                 *srcp = (unsigned short *)(Pico.rom + (source&~1));
79                 *limitp = (unsigned short *)(Pico.rom + Pico.romsize);
80                 return 1;
81         }
82         else if ((source & 0xfe0000) == 0x300000)
83         {
84                 elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len);
85                 source &= 0x1fffe;
86                 source -= 2;
87                 *srcp = (unsigned short *)(svp->dram + source);
88                 *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram));
89                 return 1;
90         }
91         else
92                 elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len);
93
94         return 0;
95 }
96
97
98 void PicoSVPInit(void)
99 {
100 }
101
102 static void PicoSVPExit(void)
103 {
104 #ifndef PSP
105         ssp1601_dyn_exit();
106 #endif
107 }
108
109
110 void PicoSVPStartup(void)
111 {
112         void *tmp;
113
114         elprintf(EL_STATUS, "SVP startup");
115
116         tmp = realloc(Pico.rom, 0x200000 + sizeof(*svp));
117         if (tmp == NULL)
118         {
119                 elprintf(EL_STATUS|EL_SVP, "OOM for SVP data");
120                 return;
121         }
122
123         //PicoOpt &= ~0x20000;
124         Pico.rom = tmp;
125         svp = (void *) ((char *)tmp + 0x200000);
126         memset(svp, 0, sizeof(*svp));
127
128         // init SVP compiler
129         svp_dyn_ready = 0;
130 #ifndef PSP
131         if (PicoOpt & POPT_EN_SVP_DRC) {
132                 if (ssp1601_dyn_startup())
133                         return;
134                 svp_dyn_ready = 1;
135         }
136 #endif
137
138         // init ok, setup hooks..
139         PicoCartMemSetup = PicoSVPMemSetup;
140         PicoDmaHook = PicoSVPDma;
141         PicoResetHook = PicoSVPReset;
142         PicoLineHook = PicoSVPLine;
143         PicoCartUnloadHook = PicoSVPExit;
144
145         // save state stuff
146         svp_states[0].ptr = svp->iram_rom;
147         svp_states[1].ptr = svp->dram;
148         svp_states[2].ptr = &svp->ssp1601;
149         carthw_chunks = svp_states;
150         PicoAHW |= PAHW_SVP;
151 }
152
153