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