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 | |
f8ef8ff7 |
33 | svp_t *svp = NULL; |
3d48ce71 |
34 | int PicoSVPCycles = 850; // cycles/line, just a guess |
1ca2ea4f |
35 | static int svp_dyn_ready = 0; |
f8ef8ff7 |
36 | |
945c2fdc |
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 }, |
e122fae6 |
48 | { CHUNK_SSP, sizeof(svp->ssp1601) - sizeof(svp->ssp1601.drc), NULL }, |
945c2fdc |
49 | { 0, 0, NULL } |
50 | }; |
51 | |
52 | |
017512f2 |
53 | static void PicoSVPReset(void) |
54 | { |
55 | elprintf(EL_SVP, "SVP reset"); |
56 | |
5de27868 |
57 | memcpy(svp->iram_rom + 0x800, Pico.rom + 0x800, 0x20000 - 0x800); |
017512f2 |
58 | ssp1601_reset(&svp->ssp1601); |
7fd5d17b |
59 | #ifdef _SVP_DRC |
92dfd9af |
60 | if ((PicoOpt & POPT_EN_DRC) && svp_dyn_ready) |
726bbb3e |
61 | ssp1601_dyn_reset(&svp->ssp1601); |
6fc57144 |
62 | #endif |
017512f2 |
63 | } |
64 | |
65 | |
e7aac062 |
66 | static void PicoSVPLine(void) |
017512f2 |
67 | { |
e7aac062 |
68 | int count = 1; |
d4d62665 |
69 | #if defined(__arm__) || defined(PSP) |
e7aac062 |
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 | |
7fd5d17b |
79 | #ifdef _SVP_DRC |
92dfd9af |
80 | if ((PicoOpt & POPT_EN_DRC) && svp_dyn_ready) |
726bbb3e |
81 | ssp1601_dyn_run(PicoSVPCycles * count); |
6fc57144 |
82 | else |
83 | #endif |
84 | { |
1ca2ea4f |
85 | ssp1601_run(PicoSVPCycles * count); |
1b13dae0 |
86 | svp_dyn_ready = 0; // just in case |
87 | } |
d26dc685 |
88 | |
89 | // test mode |
90 | //if (Pico.m.frame_count == 13) PicoPad[0] |= 0xff; |
017512f2 |
91 | } |
92 | |
93 | |
5de27868 |
94 | static int PicoSVPDma(unsigned int source, int len, unsigned short **srcp, unsigned short **limitp) |
017512f2 |
95 | { |
d4ca252d |
96 | if (source < Pico.romsize) // Rom |
97 | { |
50483b53 |
98 | source -= 2; |
99 | *srcp = (unsigned short *)(Pico.rom + (source&~1)); |
100 | *limitp = (unsigned short *)(Pico.rom + Pico.romsize); |
101 | return 1; |
102 | } |
103 | else if ((source & 0xfe0000) == 0x300000) |
017512f2 |
104 | { |
5de27868 |
105 | elprintf(EL_VDPDMA|EL_SVP, "SVP DmaSlow from %06x, len=%i", source, len); |
017512f2 |
106 | source &= 0x1fffe; |
30752975 |
107 | source -= 2; |
5de27868 |
108 | *srcp = (unsigned short *)(svp->dram + source); |
109 | *limitp = (unsigned short *)(svp->dram + sizeof(svp->dram)); |
017512f2 |
110 | return 1; |
111 | } |
d26dc685 |
112 | else |
113 | elprintf(EL_VDPDMA|EL_SVP|EL_ANOMALY, "SVP FIXME unhandled DmaSlow from %06x, len=%i", source, len); |
017512f2 |
114 | |
115 | return 0; |
116 | } |
117 | |
118 | |
f53f286a |
119 | void PicoSVPInit(void) |
e807ac75 |
120 | { |
7fd5d17b |
121 | #ifdef _SVP_DRC |
72f63cf0 |
122 | // this is to unmap tcache and make |
123 | // mem available for large ROMs, MCD, etc. |
124 | drc_cmn_cleanup(); |
125 | #endif |
e807ac75 |
126 | } |
127 | |
679af8a3 |
128 | static void PicoSVPExit(void) |
129 | { |
7fd5d17b |
130 | #ifdef _SVP_DRC |
679af8a3 |
131 | ssp1601_dyn_exit(); |
132 | #endif |
133 | } |
134 | |
e807ac75 |
135 | |
136 | void PicoSVPStartup(void) |
f53f286a |
137 | { |
a736af3e |
138 | int ret; |
f8ef8ff7 |
139 | |
45f2f245 |
140 | elprintf(EL_STATUS, "SVP startup"); |
f8ef8ff7 |
141 | |
a736af3e |
142 | ret = PicoCartResize(Pico.romsize + sizeof(*svp)); |
143 | if (ret != 0) { |
017512f2 |
144 | elprintf(EL_STATUS|EL_SVP, "OOM for SVP data"); |
f8ef8ff7 |
145 | return; |
146 | } |
147 | |
a736af3e |
148 | svp = (void *) ((char *)Pico.rom + Pico.romsize); |
f8ef8ff7 |
149 | memset(svp, 0, sizeof(*svp)); |
150 | |
726bbb3e |
151 | // init SVP compiler |
1ca2ea4f |
152 | svp_dyn_ready = 0; |
7fd5d17b |
153 | #ifdef _SVP_DRC |
92dfd9af |
154 | if (PicoOpt & POPT_EN_DRC) { |
41397701 |
155 | if (ssp1601_dyn_startup()) |
156 | return; |
1ca2ea4f |
157 | svp_dyn_ready = 1; |
726bbb3e |
158 | } |
6fc57144 |
159 | #endif |
726bbb3e |
160 | |
f8ef8ff7 |
161 | // init ok, setup hooks.. |
45f2f245 |
162 | PicoCartMemSetup = PicoSVPMemSetup; |
f8ef8ff7 |
163 | PicoDmaHook = PicoSVPDma; |
017512f2 |
164 | PicoResetHook = PicoSVPReset; |
165 | PicoLineHook = PicoSVPLine; |
679af8a3 |
166 | PicoCartUnloadHook = PicoSVPExit; |
945c2fdc |
167 | |
168 | // save state stuff |
169 | svp_states[0].ptr = svp->iram_rom; |
170 | svp_states[1].ptr = svp->dram; |
171 | svp_states[2].ptr = &svp->ssp1601; |
172 | carthw_chunks = svp_states; |
602133e1 |
173 | PicoAHW |= PAHW_SVP; |
f53f286a |
174 | } |
175 | |