8861de04d1164b4c2fe88f89f6f7c1b16c3945f3
[picodrive.git] / pico / carthw / svp / svp.c
1 /*
2  * The SVP chip emulator
3  *
4  * Copyright (c) GraÅžvydas "notaz" Ignotas, 2008
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *     * Redistributions of source code must retain the above copyright
9  *       notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above copyright
11  *       notice, this list of conditions and the following disclaimer in the
12  *       documentation and/or other materials provided with the distribution.
13  *     * Neither the name of the organization nor the
14  *       names of its contributors may be used to endorse or promote products
15  *       derived from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
24  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
25  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include <pico/pico_int.h>
30 #include <cpu/drc/cmn.h>
31 #include "compiler.h"
32
33 svp_t *svp = NULL;
34 int PicoSVPCycles = 850; // cycles/line, just a guess
35 static int svp_dyn_ready = 0;
36
37 /* save state stuff */
38 typedef enum {
39         CHUNK_IRAM = CHUNK_CARTHW,
40         CHUNK_DRAM,
41         CHUNK_SSP
42 } chunk_name_e;
43
44 static carthw_state_chunk svp_states[] =
45 {
46         { CHUNK_IRAM, 0x800,                 NULL },
47         { CHUNK_DRAM, sizeof(svp->dram),     NULL },
48         { CHUNK_SSP,  sizeof(svp->ssp1601) - sizeof(svp->ssp1601.drc),  NULL },
49         { 0,          0,                     NULL }
50 };
51
52
53 static void PicoSVPReset(void)
54 {
55         elprintf(EL_SVP, "SVP reset");
56
57         memcpy(svp->iram_rom + 0x800, Pico.rom + 0x800, 0x20000 - 0x800);
58         ssp1601_reset(&svp->ssp1601);
59 #ifdef _SVP_DRC
60         if ((PicoOpt & POPT_EN_DRC) && svp_dyn_ready)
61                 ssp1601_dyn_reset(&svp->ssp1601);
62 #endif
63 }
64
65
66 static void PicoSVPLine(void)
67 {
68         int count = 1;
69 #if defined(__arm__) || defined(PSP)
70         // performance hack
71         static int delay_lines = 0;
72         delay_lines++;
73         if ((Pico.m.scanline&0xf) != 0xf && Pico.m.scanline != 261 && Pico.m.scanline != 311)
74                 return;
75         count = delay_lines;
76         delay_lines = 0;
77 #endif
78
79 #ifdef _SVP_DRC
80         if ((PicoOpt & POPT_EN_DRC) && svp_dyn_ready)
81                 ssp1601_dyn_run(PicoSVPCycles * count);
82         else
83 #endif
84         {
85                 ssp1601_run(PicoSVPCycles * count);
86                 svp_dyn_ready = 0; // just in case
87         }
88
89         // test mode
90         //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff;
91 }
92
93
94 static int PicoSVPDma(unsigned int source, int len, unsigned short **base, unsigned int *mask)
95 {
96         if (source < Pico.romsize) // Rom
97         {
98                 *base = (unsigned short *)(Pico.rom + (source & 0xfe0000));
99                 *mask = 0x1ffff;
100                 return source - 2;
101         }
102         else if ((source & 0xfe0000) == 0x300000)
103         {
104                 elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len);
105                 *base = (unsigned short *)svp->dram;
106                 *mask = 0x1ffff;
107                 return source - 2;
108         }
109         else
110                 elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len);
111
112         return 0;
113 }
114
115
116 void PicoSVPInit(void)
117 {
118 #ifdef _SVP_DRC
119         // this is to unmap tcache and make
120         // mem available for large ROMs, MCD, etc.
121         drc_cmn_cleanup();
122 #endif
123 }
124
125 static void PicoSVPExit(void)
126 {
127 #ifdef _SVP_DRC
128         ssp1601_dyn_exit();
129 #endif
130 }
131
132
133 void PicoSVPStartup(void)
134 {
135         int ret;
136
137         elprintf(EL_STATUS, "SVP startup");
138
139         ret = PicoCartResize(Pico.romsize + sizeof(*svp));
140         if (ret != 0) {
141                 elprintf(EL_STATUS|EL_SVP, "OOM for SVP data");
142                 return;
143         }
144
145         svp = (void *) ((char *)Pico.rom + Pico.romsize);
146         memset(svp, 0, sizeof(*svp));
147
148         // init SVP compiler
149         svp_dyn_ready = 0;
150 #ifdef _SVP_DRC
151         if (PicoOpt & POPT_EN_DRC) {
152                 if (ssp1601_dyn_startup())
153                         return;
154                 svp_dyn_ready = 1;
155         }
156 #endif
157
158         // init ok, setup hooks..
159         PicoCartMemSetup = PicoSVPMemSetup;
160         PicoDmaHook = PicoSVPDma;
161         PicoResetHook = PicoSVPReset;
162         PicoLineHook = PicoSVPLine;
163         PicoCartUnloadHook = PicoSVPExit;
164
165         // save state stuff
166         svp_states[0].ptr = svp->iram_rom;
167         svp_states[1].ptr = svp->dram;
168         svp_states[2].ptr = &svp->ssp1601;
169         carthw_chunks = svp_states;
170         PicoAHW |= PAHW_SVP;
171 }
172