cff531af |
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 | */ |
d4ca252d |
28 | |
9c9cda8c |
29 | #include <pico/pico_int.h> |
30 | #include <cpu/drc/cmn.h> |
e807ac75 |
31 | #include "compiler.h" |
f53f286a |
32 | |
93f9619e |
33 | #define SVP_CYCLES_LINE 850 |
34 | |
f8ef8ff7 |
35 | svp_t *svp = NULL; |
1ca2ea4f |
36 | static int svp_dyn_ready = 0; |
f8ef8ff7 |
37 | |
945c2fdc |
38 | /* save state stuff */ |
39 | typedef enum { |
40 | CHUNK_IRAM = CHUNK_CARTHW, |
41 | CHUNK_DRAM, |
42 | CHUNK_SSP |
43 | } chunk_name_e; |
44 | |
45 | static carthw_state_chunk svp_states[] = |
46 | { |
47 | { CHUNK_IRAM, 0x800, NULL }, |
48 | { CHUNK_DRAM, sizeof(svp->dram), NULL }, |
e122fae6 |
49 | { CHUNK_SSP, sizeof(svp->ssp1601) - sizeof(svp->ssp1601.drc), NULL }, |
945c2fdc |
50 | { 0, 0, NULL } |
51 | }; |
52 | |
53 | |
017512f2 |
54 | static void PicoSVPReset(void) |
55 | { |
56 | elprintf(EL_SVP, "SVP reset"); |
57 | |
5de27868 |
58 | memcpy(svp->iram_rom + 0x800, Pico.rom + 0x800, 0x20000 - 0x800); |
017512f2 |
59 | ssp1601_reset(&svp->ssp1601); |
7fd5d17b |
60 | #ifdef _SVP_DRC |
93f9619e |
61 | if ((PicoIn.opt & POPT_EN_DRC) && svp_dyn_ready) |
726bbb3e |
62 | ssp1601_dyn_reset(&svp->ssp1601); |
6fc57144 |
63 | #endif |
017512f2 |
64 | } |
65 | |
66 | |
e7aac062 |
67 | static void PicoSVPLine(void) |
017512f2 |
68 | { |
e7aac062 |
69 | int count = 1; |
d4d62665 |
70 | #if defined(__arm__) || defined(PSP) |
e7aac062 |
71 | // performance hack |
72 | static int delay_lines = 0; |
73 | delay_lines++; |
74 | if ((Pico.m.scanline&0xf) != 0xf && Pico.m.scanline != 261 && Pico.m.scanline != 311) |
75 | return; |
76 | count = delay_lines; |
77 | delay_lines = 0; |
78 | #endif |
79 | |
7fd5d17b |
80 | #ifdef _SVP_DRC |
93f9619e |
81 | if ((PicoIn.opt & POPT_EN_DRC) && svp_dyn_ready) |
82 | ssp1601_dyn_run(SVP_CYCLES_LINE * count); |
6fc57144 |
83 | else |
84 | #endif |
85 | { |
93f9619e |
86 | ssp1601_run(SVP_CYCLES_LINE * count); |
1b13dae0 |
87 | svp_dyn_ready = 0; // just in case |
88 | } |
d26dc685 |
89 | |
90 | // test mode |
93f9619e |
91 | //if (Pico.m.frame_count == 13) PicoIn.pad[0] |= 0xff; |
017512f2 |
92 | } |
93 | |
94 | |
4cc0fcaf |
95 | static int PicoSVPDma(u32 source, int len, unsigned short **base, u32 *mask) |
017512f2 |
96 | { |
d4ca252d |
97 | if (source < Pico.romsize) // Rom |
98 | { |
0c7d1ba3 |
99 | *base = (unsigned short *)(Pico.rom + (source & 0xfe0000)); |
100 | *mask = 0x1ffff; |
101 | return source - 2; |
50483b53 |
102 | } |
103 | else if ((source & 0xfe0000) == 0x300000) |
017512f2 |
104 | { |
5de27868 |
105 | elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len); |
0c7d1ba3 |
106 | *base = (unsigned short *)svp->dram; |
107 | *mask = 0x1ffff; |
108 | return source - 2; |
017512f2 |
109 | } |
d26dc685 |
110 | else |
111 | elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len); |
017512f2 |
112 | |
113 | return 0; |
114 | } |
115 | |
116 | |
f53f286a |
117 | void PicoSVPInit(void) |
e807ac75 |
118 | { |
7fd5d17b |
119 | #ifdef _SVP_DRC |
72f63cf0 |
120 | // this is to unmap tcache and make |
121 | // mem available for large ROMs, MCD, etc. |
122 | drc_cmn_cleanup(); |
123 | #endif |
e807ac75 |
124 | } |
125 | |
679af8a3 |
126 | static void PicoSVPExit(void) |
127 | { |
7fd5d17b |
128 | #ifdef _SVP_DRC |
679af8a3 |
129 | ssp1601_dyn_exit(); |
130 | #endif |
131 | } |
132 | |
e807ac75 |
133 | |
134 | void PicoSVPStartup(void) |
f53f286a |
135 | { |
a736af3e |
136 | int ret; |
f8ef8ff7 |
137 | |
45f2f245 |
138 | elprintf(EL_STATUS, "SVP startup"); |
f8ef8ff7 |
139 | |
a736af3e |
140 | ret = PicoCartResize(Pico.romsize + sizeof(*svp)); |
141 | if (ret != 0) { |
017512f2 |
142 | elprintf(EL_STATUS|EL_SVP, "OOM for SVP data"); |
f8ef8ff7 |
143 | return; |
144 | } |
145 | |
a736af3e |
146 | svp = (void *) ((char *)Pico.rom + Pico.romsize); |
f8ef8ff7 |
147 | memset(svp, 0, sizeof(*svp)); |
148 | |
726bbb3e |
149 | // init SVP compiler |
1ca2ea4f |
150 | svp_dyn_ready = 0; |
7fd5d17b |
151 | #ifdef _SVP_DRC |
93f9619e |
152 | if (PicoIn.opt & POPT_EN_DRC) { |
41397701 |
153 | if (ssp1601_dyn_startup()) |
154 | return; |
1ca2ea4f |
155 | svp_dyn_ready = 1; |
726bbb3e |
156 | } |
6fc57144 |
157 | #endif |
726bbb3e |
158 | |
f8ef8ff7 |
159 | // init ok, setup hooks.. |
45f2f245 |
160 | PicoCartMemSetup = PicoSVPMemSetup; |
f8ef8ff7 |
161 | PicoDmaHook = PicoSVPDma; |
017512f2 |
162 | PicoResetHook = PicoSVPReset; |
163 | PicoLineHook = PicoSVPLine; |
679af8a3 |
164 | PicoCartUnloadHook = PicoSVPExit; |
945c2fdc |
165 | |
166 | // save state stuff |
167 | svp_states[0].ptr = svp->iram_rom; |
168 | svp_states[1].ptr = svp->dram; |
169 | svp_states[2].ptr = &svp->ssp1601; |
170 | carthw_chunks = svp_states; |
93f9619e |
171 | PicoIn.AHW |= PAHW_SVP; |
f53f286a |
172 | } |
173 | |