cff531af |
1 | /* |
2 | * basic, incomplete SSP160x (SSP1601?) interpreter |
3 | * with SVP memory controller emu |
4 | * |
5 | * Copyright (c) GraÅžvydas "notaz" Ignotas, 2008 |
6 | * |
7 | * Redistribution and use in source and binary forms, with or without |
8 | * modification, are permitted provided that the following conditions are met: |
9 | * * Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * * Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
14 | * * Neither the name of the organization nor the |
15 | * names of its contributors may be used to endorse or promote products |
16 | * derived from this software without specific prior written permission. |
17 | * |
18 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
19 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
20 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
21 | * ARE DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
22 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
23 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
24 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
25 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
26 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
27 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
28 | */ |
d4ca252d |
29 | |
e477f8f7 |
30 | //#define USE_DEBUGGER |
31 | /* detect ops with unimplemented/invalid fields. |
32 | * Useful for homebrew or if a new VR revision pops up. */ |
33 | //#define DO_CHECKS |
34 | |
f8ef8ff7 |
35 | /* |
36 | * Register info |
f8ef8ff7 |
37 | * |
38 | * 0. "-" |
39 | * size: 16 |
40 | * desc: Constant register with all bits set (0xffff). |
41 | * |
42 | * 1. "X" |
43 | * size: 16 |
d4ca252d |
44 | * desc: Generic register. When set, updates P (P = X * Y * 2) |
f8ef8ff7 |
45 | * |
46 | * 2. "Y" |
47 | * size: 16 |
d4ca252d |
48 | * desc: Generic register. When set, updates P (P = X * Y * 2) |
f8ef8ff7 |
49 | * |
50 | * 3. "A" |
51 | * size: 32 |
52 | * desc: Accumulator. |
53 | * |
54 | * 4. "ST" |
55 | * size: 16 |
56 | * desc: Status register. From MAME: bits 0-9 are CONTROL, other FLAG |
57 | * fedc ba98 7654 3210 |
5de27868 |
58 | * 210 - RPL (?) "Loop size". If non-zero, makes (rX+) and (rX-) respectively |
59 | * modulo-increment and modulo-decrement. The value shows which |
60 | * power of 2 to use, i.e. 4 means modulo by 16. |
61 | * (e: fir16_32.sc, IIR_4B.SC, DECIM.SC) |
f8ef8ff7 |
62 | * 43 - RB (?) |
5de27868 |
63 | * 5 - GP0_0 (ST5?) Changed before acessing PM0 (affects banking?). |
64 | * 6 - GP0_1 (ST6?) Cleared before acessing PM0 (affects banking?). Set after. |
65 | * datasheet says these (5,6) bits correspond to hardware pins. |
66 | * 7 - IE (?) Not directly used by SVP code (never set, but preserved)? |
67 | * 8 - OP (?) Not used by SVP code (only cleared)? (MAME: saturated value |
68 | * (probably means clamping? i.e. 0x7ffc + 9 -> 0x7fff)) |
f8ef8ff7 |
69 | * 9 - MACS (?) Not used by SVP code (only cleared)? (e: "mac shift") |
70 | * a - GPI_0 Interrupt 0 enable/status? |
71 | * b - GPI_1 Interrupt 1 enable/status? |
72 | * c - L L flag. Carry? |
73 | * d - Z Zero flag. |
74 | * e - OV Overflow flag. |
75 | * f - N Negative flag. |
76 | * seen directly changing code sequences: |
77 | * ldi ST, 0 ld A, ST ld A, ST ld A, ST ldi st, 20h |
78 | * ldi ST, 60h ori A, 60h and A, E8h and A, E8h |
79 | * ld ST, A ld ST, A ori 3 |
80 | * ld ST, A |
81 | * |
82 | * 5. "STACK" |
83 | * size: 16 |
84 | * desc: hw stack of 6 levels (according to datasheet) |
85 | * |
86 | * 6. "PC" |
87 | * size: 16 |
88 | * desc: Program counter. |
89 | * |
90 | * 7. "P" |
91 | * size: 32 |
d4ca252d |
92 | * desc: multiply result register. P = X * Y * 2 |
f8ef8ff7 |
93 | * probably affected by MACS bit in ST. |
94 | * |
95 | * 8. "PM0" (PM from PMAR name from Tasco's docs) |
96 | * size: 16? |
97 | * desc: Programmable Memory access register. |
98 | * On reset, or when one (both?) GP0 bits are clear, |
d26dc685 |
99 | * acts as status for XST, mapped at 015004 at 68k side: |
100 | * bit0: ssp has written something to XST (cleared when 015004 is read) |
101 | * bit1: 68k has written something through a1500{0|2} (cleared on PM0 read) |
f8ef8ff7 |
102 | * |
103 | * 9. "PM1" |
104 | * size: 16? |
105 | * desc: Programmable Memory access register. |
106 | * This reg. is only used as PMAR. |
107 | * |
108 | * 10. "PM2" |
109 | * size: 16? |
110 | * desc: Programmable Memory access register. |
111 | * This reg. is only used as PMAR. |
112 | * |
113 | * 11. "XST" |
114 | * size: 16? |
d26dc685 |
115 | * desc: eXternal STate. Mapped to a15000 and a15002 at 68k side. |
f8ef8ff7 |
116 | * Can be programmed as PMAR? (only seen in test mode code) |
d26dc685 |
117 | * Affects PM0 when written to? |
f8ef8ff7 |
118 | * |
119 | * 12. "PM4" |
120 | * size: 16? |
121 | * desc: Programmable Memory access register. |
122 | * This reg. is only used as PMAR. The most used PMAR by VR. |
123 | * |
124 | * 13. (unused by VR) |
125 | * |
126 | * 14. "PMC" (PMC from PMAC name from Tasco's docs) |
127 | * size: 32? |
128 | * desc: Programmable Memory access Control. Set using 2 16bit writes, |
129 | * first address, then mode word. After setting PMAC, PMAR sould |
3554b0a4 |
130 | * be blind accessed (ld -, PMx or ld PMx, -) to program it for |
131 | * reading and writing respectively. |
132 | * Reading the register also shifts it's state (from "waiting for |
133 | * address" to "waiting for mode" and back). Reads always return |
134 | * address related to last PMx register accressed. |
689fb2c0 |
135 | * (note: addresses do not wrap). |
f8ef8ff7 |
136 | * |
137 | * 15. "AL" |
138 | * size: 16 |
d4ca252d |
139 | * desc: Accumulator Low. 16 least significant bits of accumulator. |
f8ef8ff7 |
140 | * (normally reading acc (ld X, A) you get 16 most significant bits). |
141 | * |
142 | * |
143 | * There are 8 8-bit pointer registers rX. r0-r3 (ri) point to RAM0, r4-r7 (rj) point to RAM1. |
5de27868 |
144 | * They can be accessed directly, or 2 indirection levels can be used [ (rX), ((rX)) ], |
145 | * which work similar to * and ** operators in C, only they use different memory banks and |
146 | * ((rX)) also does post-increment. First indirection level (rX) accesses RAMx, second accesses |
147 | * program memory at address read from (rX), and increments value in (rX). |
f8ef8ff7 |
148 | * |
149 | * r0,r1,r2,r4,r5,r6 can be modified [ex: ldi r0, 5]. |
150 | * 3 modifiers can be applied (optional): |
5de27868 |
151 | * + : post-increment [ex: ld a, (r0+) ]. Can be made modulo-increment by setting RPL bits in ST. |
152 | * - : post-decrement. Can be made modulo-decrement by setting RPL bits in ST (not sure). |
153 | * +!: post-increment, unaffected by RPL (probably). |
154 | * These are only used on 1st indirection level, so things like [ld a, ((r0+))] and [ld X, r6-] |
155 | * ar probably invalid. |
f8ef8ff7 |
156 | * |
e477f8f7 |
157 | * r3 and r7 are special and can not be changed (at least Samsung samples and VR code never do). |
f8ef8ff7 |
158 | * They are fixed to the start of their RAM banks. (They are probably changeable for ssp1605+, |
159 | * Samsung's old DSP page claims that). |
160 | * 1 of these 4 modifiers must be used (short form direct addressing?): |
161 | * |00: RAMx[0] [ex: (r3|00), 0] (based on sample code) |
162 | * |01: RAMx[1] |
163 | * |10: RAMx[2] ? maybe 10h? accortding to Div_c_dp.sc, 2 |
164 | * |11: RAMx[3] |
165 | * |
166 | * |
167 | * Instruction notes |
168 | * |
d26dc685 |
169 | * ld a, * doesn't affect flags! (e: A_LAW.SC, Div_c_dp.sc) |
170 | * |
f8ef8ff7 |
171 | * mld (rj), (ri) [, b] |
172 | * operation: A = 0; P = (rj) * (ri) |
173 | * notes: based on IIR_4B.SC sample. flags? what is b??? |
f8ef8ff7 |
174 | * |
175 | * mpya (rj), (ri) [, b] |
176 | * name: multiply and add? |
177 | * operation: A += P; P = (rj) * (ri) |
178 | * |
179 | * mpys (rj), (ri), b |
180 | * name: multiply and subtract? |
181 | * notes: not used by VR code. |
017512f2 |
182 | * |
d26dc685 |
183 | * mod cond, op |
184 | * mod cond, shr does arithmetic shift |
30752975 |
185 | * |
67256d4b |
186 | * 'ld -, AL' and probably 'ld AL, -' are for dummy assigns |
187 | * |
30752975 |
188 | * memory map: |
189 | * 000000 - 1fffff ROM, accessable by both |
190 | * 200000 - 2fffff unused? |
d26dc685 |
191 | * 300000 - 31ffff DRAM, both |
30752975 |
192 | * 320000 - 38ffff unused? |
193 | * 390000 - 3907ff IRAM. can only be accessed by ssp? |
d26dc685 |
194 | * 390000 - 39ffff similar mapping to "cell arrange" in Sega CD, 68k only? |
195 | * 3a0000 - 3affff similar mapping to "cell arrange" in Sega CD, a bit different |
30752975 |
196 | * |
197 | * 30fe02 - 0 if SVP busy, 1 if done (set by SVP, checked and cleared by 68k) |
198 | * 30fe06 - also sync related. |
e477f8f7 |
199 | * 30fe08 - job number [1-12] for SVP. 0 means no job. Set by 68k, read-cleared by VR. |
67256d4b |
200 | * |
e477f8f7 |
201 | * Assumptions and limitations in this code |
202 | * only Z and N status flags are emulated (others unused by VR) |
203 | * so all condition checks except N and Z are ignored (not used by VR) |
204 | * modifiers for 'OP a, ri' and ((ri)) are ignored (not used by VR) |
205 | * loop repeat mode when (ri) is destination is ignored |
206 | * ops not used by VR are not implemented |
f8ef8ff7 |
207 | */ |
208 | |
f821bb70 |
209 | #include <pico/pico_int.h> |
f8ef8ff7 |
210 | |
017512f2 |
211 | #define u32 unsigned int |
212 | |
213 | // 0 |
214 | #define rX ssp->gr[SSP_X].h |
215 | #define rY ssp->gr[SSP_Y].h |
216 | #define rA ssp->gr[SSP_A].h |
217 | #define rST ssp->gr[SSP_ST].h // 4 |
218 | #define rSTACK ssp->gr[SSP_STACK].h |
219 | #define rPC ssp->gr[SSP_PC].h |
220 | #define rP ssp->gr[SSP_P] |
221 | #define rPM0 ssp->gr[SSP_PM0].h // 8 |
222 | #define rPM1 ssp->gr[SSP_PM1].h |
223 | #define rPM2 ssp->gr[SSP_PM2].h |
224 | #define rXST ssp->gr[SSP_XST].h |
225 | #define rPM4 ssp->gr[SSP_PM4].h // 12 |
226 | // 13 |
6b6a3e50 |
227 | #define rPMC ssp->gr[SSP_PMC] // will keep addr in .l, mode in .h |
f8ef8ff7 |
228 | #define rAL ssp->gr[SSP_A].l |
229 | |
5de27868 |
230 | #define rA32 ssp->gr[SSP_A].v |
231 | #define rIJ ssp->r |
232 | |
233 | #define IJind (((op>>6)&4)|(op&3)) |
234 | |
235 | #define GET_PC() (PC - (unsigned short *)svp->iram_rom) |
b8a1c09a |
236 | #define GET_PPC_OFFS() ((unsigned char *)PC - svp->iram_rom - 2) |
5de27868 |
237 | #define SET_PC(d) PC = (unsigned short *)svp->iram_rom + d |
017512f2 |
238 | |
239 | #define REG_READ(r) (((r) <= 4) ? ssp->gr[r].h : read_handlers[r]()) |
240 | #define REG_WRITE(r,d) { \ |
241 | int r1 = r; \ |
5de27868 |
242 | if (r1 >= 4) write_handlers[r1](d); \ |
017512f2 |
243 | else if (r1 > 0) ssp->gr[r1].h = d; \ |
244 | } |
245 | |
5de27868 |
246 | // flags |
d26dc685 |
247 | #define SSP_FLAG_L (1<<0xc) |
248 | #define SSP_FLAG_Z (1<<0xd) |
249 | #define SSP_FLAG_V (1<<0xe) |
250 | #define SSP_FLAG_N (1<<0xf) |
5de27868 |
251 | |
252 | // update ZN according to 32bit ACC. |
253 | #define UPD_ACC_ZN \ |
d26dc685 |
254 | rST &= ~(SSP_FLAG_Z|SSP_FLAG_N); \ |
255 | if (!rA32) rST |= SSP_FLAG_Z; \ |
256 | else rST |= (rA32>>16)&SSP_FLAG_N; |
5de27868 |
257 | |
258 | // it seems SVP code never checks for L and OV, so we leave them out. |
d26dc685 |
259 | // rST |= (t>>4)&SSP_FLAG_L; |
67256d4b |
260 | #define UPD_LZVN \ |
261 | rST &= ~(SSP_FLAG_L|SSP_FLAG_Z|SSP_FLAG_V|SSP_FLAG_N); \ |
262 | if (!rA32) rST |= SSP_FLAG_Z; \ |
263 | else rST |= (rA32>>16)&SSP_FLAG_N; |
264 | |
5de27868 |
265 | // standard cond processing. |
e477f8f7 |
266 | // again, only Z and N is checked, as VR doesn't seem to use any other conds. |
5de27868 |
267 | #define COND_CHECK \ |
268 | switch (op&0xf0) { \ |
269 | case 0x00: cond = 1; break; /* always true */ \ |
d26dc685 |
270 | case 0x50: cond = !((rST ^ (op<<5)) & SSP_FLAG_Z); break; /* Z matches f(?) bit */ \ |
271 | case 0x70: cond = !((rST ^ (op<<7)) & SSP_FLAG_N); break; /* N matches f(?) bit */ \ |
689fb2c0 |
272 | default:elprintf(EL_SVP|EL_ANOMALY, "ssp FIXME: unimplemented cond @ %04x", GET_PPC_OFFS()); break; \ |
5de27868 |
273 | } |
274 | |
275 | // ops with accumulator. |
276 | // how is low word really affected by these? |
30752975 |
277 | // nearly sure 'ld A' doesn't affect flags |
5de27868 |
278 | #define OP_LDA(x) \ |
30752975 |
279 | ssp->gr[SSP_A].h = x |
5de27868 |
280 | |
67256d4b |
281 | #define OP_LDA32(x) \ |
4bfc6da4 |
282 | rA32 = x |
67256d4b |
283 | |
5de27868 |
284 | #define OP_SUBA(x) { \ |
4bfc6da4 |
285 | rA32 -= (x) << 16; \ |
286 | UPD_LZVN \ |
5de27868 |
287 | } |
288 | |
67256d4b |
289 | #define OP_SUBA32(x) { \ |
4bfc6da4 |
290 | rA32 -= (x); \ |
67256d4b |
291 | UPD_LZVN \ |
292 | } |
293 | |
5de27868 |
294 | #define OP_CMPA(x) { \ |
4bfc6da4 |
295 | u32 t = rA32 - ((x) << 16); \ |
296 | rST &= ~(SSP_FLAG_L|SSP_FLAG_Z|SSP_FLAG_V|SSP_FLAG_N); \ |
297 | if (!t) rST |= SSP_FLAG_Z; \ |
298 | else rST |= (t>>16)&SSP_FLAG_N; \ |
5de27868 |
299 | } |
300 | |
67256d4b |
301 | #define OP_CMPA32(x) { \ |
4bfc6da4 |
302 | u32 t = rA32 - (x); \ |
67256d4b |
303 | rST &= ~(SSP_FLAG_L|SSP_FLAG_Z|SSP_FLAG_V|SSP_FLAG_N); \ |
304 | if (!t) rST |= SSP_FLAG_Z; \ |
305 | else rST |= (t>>16)&SSP_FLAG_N; \ |
306 | } |
307 | |
5de27868 |
308 | #define OP_ADDA(x) { \ |
4bfc6da4 |
309 | rA32 += (x) << 16; \ |
310 | UPD_LZVN \ |
5de27868 |
311 | } |
312 | |
67256d4b |
313 | #define OP_ADDA32(x) { \ |
4bfc6da4 |
314 | rA32 += (x); \ |
67256d4b |
315 | UPD_LZVN \ |
316 | } |
317 | |
5de27868 |
318 | #define OP_ANDA(x) \ |
4bfc6da4 |
319 | rA32 &= (x) << 16; \ |
5de27868 |
320 | UPD_ACC_ZN |
321 | |
67256d4b |
322 | #define OP_ANDA32(x) \ |
4bfc6da4 |
323 | rA32 &= (x); \ |
67256d4b |
324 | UPD_ACC_ZN |
325 | |
5de27868 |
326 | #define OP_ORA(x) \ |
4bfc6da4 |
327 | rA32 |= (x) << 16; \ |
5de27868 |
328 | UPD_ACC_ZN |
329 | |
67256d4b |
330 | #define OP_ORA32(x) \ |
4bfc6da4 |
331 | rA32 |= (x); \ |
67256d4b |
332 | UPD_ACC_ZN |
333 | |
5de27868 |
334 | #define OP_EORA(x) \ |
4bfc6da4 |
335 | rA32 ^= (x) << 16; \ |
5de27868 |
336 | UPD_ACC_ZN |
337 | |
67256d4b |
338 | #define OP_EORA32(x) \ |
4bfc6da4 |
339 | rA32 ^= (x); \ |
67256d4b |
340 | UPD_ACC_ZN |
341 | |
342 | |
e477f8f7 |
343 | #define OP_CHECK32(OP) { \ |
344 | if ((op & 0x0f) == SSP_P) { /* A <- P */ \ |
345 | read_P(); /* update P */ \ |
346 | OP(rP.v); \ |
347 | break; \ |
348 | } \ |
349 | if ((op & 0x0f) == SSP_A) { /* A <- A */ \ |
350 | OP(rA32); \ |
351 | break; \ |
352 | } \ |
67256d4b |
353 | } |
354 | |
5de27868 |
355 | |
e477f8f7 |
356 | #ifdef DO_CHECKS |
357 | #define CHECK_IMM16() if (op&0x1ff) elprintf(EL_ANOMALY, "imm bits! %04x @ %04x", op, GET_PPC_OFFS()) |
358 | #define CHECK_B_SET() if (op&0x100) elprintf(EL_ANOMALY, "b set! %04x @ %04x", op, GET_PPC_OFFS()) |
359 | #define CHECK_B_CLEAR() if (!(op&0x100)) elprintf(EL_ANOMALY, "b clear! %04x @ %04x", op, GET_PPC_OFFS()) |
360 | #define CHECK_MOD() if (op&0x00c) elprintf(EL_ANOMALY, "mod bits! %04x @ %04x", op, GET_PPC_OFFS()) |
361 | #define CHECK_10f() if (op&0x10f) elprintf(EL_ANOMALY, "bits 10f! %04x @ %04x", op, GET_PPC_OFFS()) |
362 | #define CHECK_008() if (op&0x008) elprintf(EL_ANOMALY, "bits 008! %04x @ %04x", op, GET_PPC_OFFS()) |
363 | #define CHECK_00f() if (op&0x00f) elprintf(EL_ANOMALY, "bits 00f! %04x @ %04x", op, GET_PPC_OFFS()) |
364 | #define CHECK_0f0() if (op&0x0f0) elprintf(EL_ANOMALY, "bits 0f0! %04x @ %04x", op, GET_PPC_OFFS()) |
365 | #define CHECK_1f0() if (op&0x1f0) elprintf(EL_ANOMALY, "bits 1f0! %04x @ %04x", op, GET_PPC_OFFS()) |
366 | #define CHECK_RPL() if (rST&7) elprintf(EL_ANOMALY, "unhandled RPL! %04x @ %04x", op, GET_PPC_OFFS()) |
367 | #define CHECK_ST(d) if((rST^d)&0xf98)elprintf(EL_ANOMALY, "ssp FIXME ST %04x -> %04x @ %04x", rST, d, GET_PPC_OFFS()) |
368 | #else |
369 | #define CHECK_IMM16() |
370 | #define CHECK_B_SET() |
371 | #define CHECK_B_CLEAR() |
372 | #define CHECK_MOD() |
373 | #define CHECK_10f() |
374 | #define CHECK_008() |
375 | #define CHECK_00f() |
376 | #define CHECK_0f0() |
377 | #define CHECK_1f0() |
378 | #define CHECK_RPL() |
379 | #define CHECK_ST(d) |
380 | #endif |
381 | |
e807ac75 |
382 | ssp1601_t *ssp = NULL; |
017512f2 |
383 | static unsigned short *PC; |
384 | static int g_cycles; |
3554b0a4 |
385 | |
386 | #ifdef USE_DEBUGGER |
5de27868 |
387 | static int running = 0; |
30752975 |
388 | static int last_iram = 0; |
3554b0a4 |
389 | #endif |
017512f2 |
390 | |
391 | // ----------------------------------------------------- |
392 | // register i/o handlers |
393 | |
394 | // 0-4, 13 |
395 | static u32 read_unknown(void) |
396 | { |
3554b0a4 |
397 | elprintf(EL_ANOMALY|EL_SVP, "ssp FIXME: unknown read @ %04x", GET_PPC_OFFS()); |
017512f2 |
398 | return 0; |
399 | } |
400 | |
401 | static void write_unknown(u32 d) |
402 | { |
3554b0a4 |
403 | elprintf(EL_ANOMALY|EL_SVP, "ssp FIXME: unknown write @ %04x", GET_PPC_OFFS()); |
5de27868 |
404 | } |
405 | |
406 | // 4 |
407 | static void write_ST(u32 d) |
408 | { |
e477f8f7 |
409 | CHECK_ST(d); |
5de27868 |
410 | rST = d; |
017512f2 |
411 | } |
412 | |
413 | // 5 |
414 | static u32 read_STACK(void) |
415 | { |
5de27868 |
416 | --rSTACK; |
417 | if ((short)rSTACK < 0) { |
418 | rSTACK = 5; |
3554b0a4 |
419 | elprintf(EL_ANOMALY|EL_SVP, "ssp FIXME: stack underflow! (%i) @ %04x", rSTACK, GET_PPC_OFFS()); |
5de27868 |
420 | } |
421 | return ssp->stack[rSTACK]; |
017512f2 |
422 | } |
423 | |
424 | static void write_STACK(u32 d) |
425 | { |
5de27868 |
426 | if (rSTACK >= 6) { |
3554b0a4 |
427 | elprintf(EL_ANOMALY|EL_SVP, "ssp FIXME: stack overflow! (%i) @ %04x", rSTACK, GET_PPC_OFFS()); |
5de27868 |
428 | rSTACK = 0; |
429 | } |
430 | ssp->stack[rSTACK++] = d; |
017512f2 |
431 | } |
432 | |
433 | // 6 |
434 | static u32 read_PC(void) |
435 | { |
436 | return GET_PC(); |
437 | } |
438 | |
439 | static void write_PC(u32 d) |
440 | { |
441 | SET_PC(d); |
442 | g_cycles--; |
443 | } |
444 | |
445 | // 7 |
446 | static u32 read_P(void) |
447 | { |
4fd5325d |
448 | int m1 = (signed short)rX; |
449 | int m2 = (signed short)rY; |
3554b0a4 |
450 | rP.v = (m1 * m2 * 2); |
017512f2 |
451 | return rP.h; |
452 | } |
453 | |
5de27868 |
454 | // ----------------------------------------------------- |
455 | |
3554b0a4 |
456 | static int get_inc(int mode) |
5de27868 |
457 | { |
3554b0a4 |
458 | int inc = (mode >> 11) & 7; |
459 | if (inc != 0) { |
460 | if (inc != 7) inc--; |
6b6a3e50 |
461 | inc = 1 << inc; // 0 1 2 4 8 16 32 128 |
3554b0a4 |
462 | if (mode & 0x8000) inc = -inc; // decrement mode |
463 | } |
464 | return inc; |
5de27868 |
465 | } |
466 | |
ede7220f |
467 | #define overwrite_write(dst, d) \ |
d26dc685 |
468 | { \ |
469 | if (d & 0xf000) { dst &= ~0xf000; dst |= d & 0xf000; } \ |
470 | if (d & 0x0f00) { dst &= ~0x0f00; dst |= d & 0x0f00; } \ |
471 | if (d & 0x00f0) { dst &= ~0x00f0; dst |= d & 0x00f0; } \ |
472 | if (d & 0x000f) { dst &= ~0x000f; dst |= d & 0x000f; } \ |
473 | } |
474 | |
017512f2 |
475 | static u32 pm_io(int reg, int write, u32 d) |
476 | { |
0e352905 |
477 | unsigned int *pmac; |
478 | |
67256d4b |
479 | if (ssp->emu_status & SSP_PMC_SET) |
480 | { |
481 | // this MUST be blind r or w |
482 | if ((*(PC-1) & 0xff0f) && (*(PC-1) & 0xfff0)) { |
3554b0a4 |
483 | elprintf(EL_SVP|EL_ANOMALY, "ssp FIXME: tried to set PM%i (%c) with non-blind i/o %08x @ %04x", |
67256d4b |
484 | reg, write ? 'w' : 'r', rPMC.v, GET_PPC_OFFS()); |
485 | ssp->emu_status &= ~SSP_PMC_SET; |
486 | return 0; |
487 | } |
5de27868 |
488 | elprintf(EL_SVP, "PM%i (%c) set to %08x @ %04x", reg, write ? 'w' : 'r', rPMC.v, GET_PPC_OFFS()); |
0e352905 |
489 | pmac = write ? ssp->pmac_write : ssp->pmac_read; |
490 | pmac[reg] = rPMC.v; |
017512f2 |
491 | ssp->emu_status &= ~SSP_PMC_SET; |
12f0f94d |
492 | if ((rPMC.v & 0x7fffff) == 0x1c8000 || (rPMC.v & 0x7fffff) == 0x1c8240) { |
bad5731d |
493 | elprintf(EL_SVP, "ssp IRAM copy from %06x to %04x", (ssp->RAM1[0]-1)<<1, (rPMC.v&0x7fff)<<1); |
3554b0a4 |
494 | #ifdef USE_DEBUGGER |
30752975 |
495 | last_iram = (ssp->RAM1[0]-1)<<1; |
3554b0a4 |
496 | #endif |
30752975 |
497 | } |
017512f2 |
498 | return 0; |
499 | } |
500 | |
5de27868 |
501 | // just in case |
67256d4b |
502 | if (ssp->emu_status & SSP_PMC_HAVE_ADDR) { |
3554b0a4 |
503 | elprintf(EL_SVP|EL_ANOMALY, "ssp FIXME: PM%i (%c) with only addr set @ %04x", |
67256d4b |
504 | reg, write ? 'w' : 'r', GET_PPC_OFFS()); |
505 | ssp->emu_status &= ~SSP_PMC_HAVE_ADDR; |
506 | } |
5de27868 |
507 | |
5de27868 |
508 | if (reg == 4 || (rST & 0x60)) |
509 | { |
30752975 |
510 | #define CADDR ((((mode<<16)&0x7f0000)|addr)<<1) |
d26dc685 |
511 | unsigned short *dram = (unsigned short *)svp->dram; |
5de27868 |
512 | if (write) |
513 | { |
6b6a3e50 |
514 | int mode = ssp->pmac_write[reg]>>16; |
515 | int addr = ssp->pmac_write[reg]&0xffff; |
3554b0a4 |
516 | if ((mode & 0xb800) == 0xb800) |
689fb2c0 |
517 | elprintf(EL_SVP|EL_ANOMALY, "ssp FIXME: mode %04x", mode); |
3554b0a4 |
518 | if ((mode & 0x43ff) == 0x0018) // DRAM |
519 | { |
520 | int inc = get_inc(mode); |
521 | elprintf(EL_SVP, "ssp PM%i DRAM w [%06x] %04x (inc %i, ovrw %i)", |
6b6a3e50 |
522 | reg, CADDR, d, inc, (mode>>10)&1); |
3554b0a4 |
523 | if (mode & 0x0400) { |
ede7220f |
524 | overwrite_write(dram[addr], d); |
3554b0a4 |
525 | } else dram[addr] = d; |
526 | ssp->pmac_write[reg] += inc; |
527 | } |
528 | else if ((mode & 0xfbff) == 0x4018) // DRAM, cell inc |
529 | { |
530 | elprintf(EL_SVP, "ssp PM%i DRAM w [%06x] %04x (cell inc, ovrw %i) @ %04x", |
531 | reg, CADDR, d, (mode>>10)&1, GET_PPC_OFFS()); |
532 | if (mode & 0x0400) { |
ede7220f |
533 | overwrite_write(dram[addr], d); |
3554b0a4 |
534 | } else dram[addr] = d; |
6b6a3e50 |
535 | ssp->pmac_write[reg] += (addr&1) ? 31 : 1; |
3554b0a4 |
536 | } |
537 | else if ((mode & 0x47ff) == 0x001c) // IRAM |
538 | { |
539 | int inc = get_inc(mode); |
540 | if ((addr&0xfc00) != 0x8000) |
541 | elprintf(EL_SVP|EL_ANOMALY, "ssp FIXME: invalid IRAM addr: %04x", addr<<1); |
6b6a3e50 |
542 | elprintf(EL_SVP, "ssp IRAM w [%06x] %04x (inc %i)", (addr<<1)&0x7ff, d, inc); |
3554b0a4 |
543 | ((unsigned short *)svp->iram_rom)[addr&0x3ff] = d; |
544 | ssp->pmac_write[reg] += inc; |
545 | } |
546 | else |
547 | { |
548 | elprintf(EL_SVP|EL_ANOMALY, "ssp FIXME: PM%i unhandled write mode %04x, [%06x] %04x @ %04x", |
549 | reg, mode, CADDR, d, GET_PPC_OFFS()); |
5de27868 |
550 | } |
551 | } |
552 | else |
553 | { |
6b6a3e50 |
554 | int mode = ssp->pmac_read[reg]>>16; |
555 | int addr = ssp->pmac_read[reg]&0xffff; |
3554b0a4 |
556 | if ((mode & 0xfff0) == 0x0800) // ROM, inc 1, verified to be correct |
557 | { |
d26dc685 |
558 | elprintf(EL_SVP, "ssp ROM r [%06x] %04x", CADDR, |
559 | ((unsigned short *)Pico.rom)[addr|((mode&0xf)<<16)]); |
6b6a3e50 |
560 | ssp->pmac_read[reg] += 1; |
d26dc685 |
561 | d = ((unsigned short *)Pico.rom)[addr|((mode&0xf)<<16)]; |
d26dc685 |
562 | } |
3554b0a4 |
563 | else if ((mode & 0x47ff) == 0x0018) // DRAM |
564 | { |
565 | int inc = get_inc(mode); |
6b6a3e50 |
566 | elprintf(EL_SVP, "ssp PM%i DRAM r [%06x] %04x (inc %i)", reg, CADDR, dram[addr]); |
3554b0a4 |
567 | d = dram[addr]; |
568 | ssp->pmac_read[reg] += inc; |
569 | } |
570 | else |
571 | { |
572 | elprintf(EL_SVP|EL_ANOMALY, "ssp FIXME: PM%i unhandled read mode %04x, [%06x] @ %04x", |
573 | reg, mode, CADDR, GET_PPC_OFFS()); |
574 | d = 0; |
5de27868 |
575 | } |
576 | } |
30752975 |
577 | |
578 | // PMC value corresponds to last PMR accessed (not sure). |
0e352905 |
579 | pmac = write ? ssp->pmac_write : ssp->pmac_read; |
580 | rPMC.v = pmac[reg]; |
30752975 |
581 | |
582 | return d; |
017512f2 |
583 | } |
584 | |
585 | return (u32)-1; |
586 | } |
587 | |
588 | // 8 |
589 | static u32 read_PM0(void) |
590 | { |
591 | u32 d = pm_io(0, 0, 0); |
592 | if (d != (u32)-1) return d; |
67256d4b |
593 | elprintf(EL_SVP, "PM0 raw r %04x @ %04x", rPM0, GET_PPC_OFFS()); |
d26dc685 |
594 | d = rPM0; |
595 | if (!(d & 2) && (GET_PPC_OFFS() == 0x800 || GET_PPC_OFFS() == 0x1851E)) { |
596 | ssp->emu_status |= SSP_WAIT_PM0; elprintf(EL_SVP, "det TIGHT loop: PM0"); |
597 | } |
598 | rPM0 &= ~2; // ? |
599 | return d; |
017512f2 |
600 | } |
601 | |
602 | static void write_PM0(u32 d) |
603 | { |
604 | u32 r = pm_io(0, 1, d); |
605 | if (r != (u32)-1) return; |
5de27868 |
606 | elprintf(EL_SVP, "PM0 raw w %04x @ %04x", d, GET_PPC_OFFS()); |
d26dc685 |
607 | rPM0 = d; |
017512f2 |
608 | } |
609 | |
610 | // 9 |
611 | static u32 read_PM1(void) |
612 | { |
613 | u32 d = pm_io(1, 0, 0); |
614 | if (d != (u32)-1) return d; |
615 | // can be removed? |
d4ca252d |
616 | elprintf(EL_SVP|EL_ANOMALY, "PM1 raw r %04x @ %04x", rPM1, GET_PPC_OFFS()); |
5de27868 |
617 | return rPM1; |
017512f2 |
618 | } |
619 | |
620 | static void write_PM1(u32 d) |
621 | { |
622 | u32 r = pm_io(1, 1, d); |
623 | if (r != (u32)-1) return; |
624 | // can be removed? |
d4ca252d |
625 | elprintf(EL_SVP|EL_ANOMALY, "PM1 raw w %04x @ %04x", d, GET_PPC_OFFS()); |
5de27868 |
626 | rPM1 = d; |
017512f2 |
627 | } |
628 | |
629 | // 10 |
630 | static u32 read_PM2(void) |
631 | { |
632 | u32 d = pm_io(2, 0, 0); |
633 | if (d != (u32)-1) return d; |
634 | // can be removed? |
d4ca252d |
635 | elprintf(EL_SVP|EL_ANOMALY, "PM2 raw r %04x @ %04x", rPM2, GET_PPC_OFFS()); |
5de27868 |
636 | return rPM2; |
017512f2 |
637 | } |
638 | |
639 | static void write_PM2(u32 d) |
640 | { |
641 | u32 r = pm_io(2, 1, d); |
642 | if (r != (u32)-1) return; |
643 | // can be removed? |
d4ca252d |
644 | elprintf(EL_SVP|EL_ANOMALY, "PM2 raw w %04x @ %04x", d, GET_PPC_OFFS()); |
5de27868 |
645 | rPM2 = d; |
017512f2 |
646 | } |
647 | |
648 | // 11 |
649 | static u32 read_XST(void) |
650 | { |
651 | // can be removed? |
652 | u32 d = pm_io(3, 0, 0); |
653 | if (d != (u32)-1) return d; |
654 | |
5de27868 |
655 | elprintf(EL_SVP, "XST raw r %04x @ %04x", rXST, GET_PPC_OFFS()); |
656 | return rXST; |
017512f2 |
657 | } |
f8ef8ff7 |
658 | |
017512f2 |
659 | static void write_XST(u32 d) |
f8ef8ff7 |
660 | { |
017512f2 |
661 | // can be removed? |
662 | u32 r = pm_io(3, 1, d); |
663 | if (r != (u32)-1) return; |
664 | |
5de27868 |
665 | elprintf(EL_SVP, "XST raw w %04x @ %04x", d, GET_PPC_OFFS()); |
d26dc685 |
666 | rPM0 |= 1; |
5de27868 |
667 | rXST = d; |
017512f2 |
668 | } |
669 | |
670 | // 12 |
671 | static u32 read_PM4(void) |
672 | { |
673 | u32 d = pm_io(4, 0, 0); |
72f63cf0 |
674 | |
30752975 |
675 | if (d == 0) { |
676 | switch (GET_PPC_OFFS()) { |
d26dc685 |
677 | case 0x0854: ssp->emu_status |= SSP_WAIT_30FE08; elprintf(EL_SVP, "det TIGHT loop: [30fe08]"); break; |
678 | case 0x4f12: ssp->emu_status |= SSP_WAIT_30FE06; elprintf(EL_SVP, "det TIGHT loop: [30fe06]"); break; |
30752975 |
679 | } |
680 | } |
72f63cf0 |
681 | |
017512f2 |
682 | if (d != (u32)-1) return d; |
683 | // can be removed? |
d4ca252d |
684 | elprintf(EL_SVP|EL_ANOMALY, "PM4 raw r %04x @ %04x", rPM4, GET_PPC_OFFS()); |
5de27868 |
685 | return rPM4; |
017512f2 |
686 | } |
687 | |
688 | static void write_PM4(u32 d) |
689 | { |
690 | u32 r = pm_io(4, 1, d); |
691 | if (r != (u32)-1) return; |
692 | // can be removed? |
d4ca252d |
693 | elprintf(EL_SVP|EL_ANOMALY, "PM4 raw w %04x @ %04x", d, GET_PPC_OFFS()); |
5de27868 |
694 | rPM4 = d; |
017512f2 |
695 | } |
696 | |
697 | // 14 |
698 | static u32 read_PMC(void) |
699 | { |
6b6a3e50 |
700 | elprintf(EL_SVP, "PMC r a %04x (st %c) @ %04x", rPMC.l, |
3554b0a4 |
701 | (ssp->emu_status & SSP_PMC_HAVE_ADDR) ? 'm' : 'a', GET_PPC_OFFS()); |
017512f2 |
702 | if (ssp->emu_status & SSP_PMC_HAVE_ADDR) { |
3554b0a4 |
703 | //if (ssp->emu_status & SSP_PMC_SET) |
704 | // elprintf(EL_ANOMALY|EL_SVP, "prev PMC not used @ %04x", GET_PPC_OFFS()); |
017512f2 |
705 | ssp->emu_status |= SSP_PMC_SET; |
5de27868 |
706 | ssp->emu_status &= ~SSP_PMC_HAVE_ADDR; |
6b6a3e50 |
707 | return ((rPMC.l << 4) & 0xfff0) | ((rPMC.l >> 4) & 0xf); |
017512f2 |
708 | } else { |
709 | ssp->emu_status |= SSP_PMC_HAVE_ADDR; |
6b6a3e50 |
710 | return rPMC.l; |
017512f2 |
711 | } |
712 | } |
713 | |
714 | static void write_PMC(u32 d) |
715 | { |
716 | if (ssp->emu_status & SSP_PMC_HAVE_ADDR) { |
3554b0a4 |
717 | //if (ssp->emu_status & SSP_PMC_SET) |
718 | // elprintf(EL_ANOMALY|EL_SVP, "prev PMC not used @ %04x", GET_PPC_OFFS()); |
017512f2 |
719 | ssp->emu_status |= SSP_PMC_SET; |
5de27868 |
720 | ssp->emu_status &= ~SSP_PMC_HAVE_ADDR; |
6b6a3e50 |
721 | rPMC.h = d; |
722 | elprintf(EL_SVP, "PMC w m %04x @ %04x", rPMC.h, GET_PPC_OFFS()); |
017512f2 |
723 | } else { |
724 | ssp->emu_status |= SSP_PMC_HAVE_ADDR; |
6b6a3e50 |
725 | rPMC.l = d; |
726 | elprintf(EL_SVP, "PMC w a %04x @ %04x", rPMC.l, GET_PPC_OFFS()); |
017512f2 |
727 | } |
728 | } |
729 | |
730 | // 15 |
731 | static u32 read_AL(void) |
732 | { |
6e39239f |
733 | if (*(PC-1) == 0x000f) |
689fb2c0 |
734 | elprintf(EL_SVP, "ssp dummy PM assign %08x @ %04x", rPMC.v, GET_PPC_OFFS()); |
6e39239f |
735 | ssp->emu_status &= ~(SSP_PMC_SET|SSP_PMC_HAVE_ADDR); // ? |
017512f2 |
736 | return rAL; |
737 | } |
738 | |
739 | static void write_AL(u32 d) |
740 | { |
741 | rAL = d; |
742 | } |
743 | |
744 | |
745 | typedef u32 (*read_func_t)(void); |
746 | typedef void (*write_func_t)(u32 d); |
747 | |
748 | static read_func_t read_handlers[16] = |
749 | { |
750 | read_unknown, read_unknown, read_unknown, read_unknown, // -, X, Y, A |
751 | read_unknown, // 4 ST |
752 | read_STACK, |
753 | read_PC, |
754 | read_P, |
755 | read_PM0, // 8 |
756 | read_PM1, |
757 | read_PM2, |
758 | read_XST, |
759 | read_PM4, // 12 |
760 | read_unknown, // 13 gr13 |
761 | read_PMC, |
762 | read_AL |
763 | }; |
764 | |
765 | static write_func_t write_handlers[16] = |
766 | { |
767 | write_unknown, write_unknown, write_unknown, write_unknown, // -, X, Y, A |
5de27868 |
768 | // write_unknown, // 4 ST |
769 | write_ST, // 4 ST (debug hook) |
017512f2 |
770 | write_STACK, |
771 | write_PC, |
772 | write_unknown, // 7 P |
773 | write_PM0, // 8 |
774 | write_PM1, |
775 | write_PM2, |
776 | write_XST, |
777 | write_PM4, // 12 |
778 | write_unknown, // 13 gr13 |
779 | write_PMC, |
780 | write_AL |
781 | }; |
782 | |
5de27868 |
783 | // ----------------------------------------------------- |
784 | // pointer register handlers |
785 | |
786 | // |
787 | #define ptr1_read(op) ptr1_read_(op&3,(op>>6)&4,(op<<1)&0x18) |
788 | |
789 | static u32 ptr1_read_(int ri, int isj2, int modi3) |
790 | { |
791 | //int t = (op&3) | ((op>>6)&4) | ((op<<1)&0x18); |
30752975 |
792 | u32 mask, add = 0, t = ri | isj2 | modi3; |
793 | unsigned char *rp = NULL; |
5de27868 |
794 | switch (t) |
795 | { |
796 | // mod=0 (00) |
797 | case 0x00: |
798 | case 0x01: |
799 | case 0x02: return ssp->RAM0[ssp->r0[t&3]]; |
800 | case 0x03: return ssp->RAM0[0]; |
801 | case 0x04: |
802 | case 0x05: |
803 | case 0x06: return ssp->RAM1[ssp->r1[t&3]]; |
804 | case 0x07: return ssp->RAM1[0]; |
805 | // mod=1 (01), "+!" |
5de27868 |
806 | case 0x08: |
5de27868 |
807 | case 0x09: |
30752975 |
808 | case 0x0a: return ssp->RAM0[ssp->r0[t&3]++]; |
5de27868 |
809 | case 0x0b: return ssp->RAM0[1]; |
810 | case 0x0c: |
5de27868 |
811 | case 0x0d: |
30752975 |
812 | case 0x0e: return ssp->RAM1[ssp->r1[t&3]++]; |
5de27868 |
813 | case 0x0f: return ssp->RAM1[1]; |
814 | // mod=2 (10), "-" |
815 | case 0x10: |
816 | case 0x11: |
30752975 |
817 | case 0x12: rp = &ssp->r0[t&3]; t = ssp->RAM0[*rp]; |
818 | if (!(rST&7)) { (*rp)--; return t; } |
819 | add = -1; goto modulo; |
5de27868 |
820 | case 0x13: return ssp->RAM0[2]; |
821 | case 0x14: |
822 | case 0x15: |
30752975 |
823 | case 0x16: rp = &ssp->r1[t&3]; t = ssp->RAM1[*rp]; |
824 | if (!(rST&7)) { (*rp)--; return t; } |
825 | add = -1; goto modulo; |
5de27868 |
826 | case 0x17: return ssp->RAM1[2]; |
30752975 |
827 | // mod=3 (11), "+" |
828 | case 0x18: |
829 | case 0x19: |
830 | case 0x1a: rp = &ssp->r0[t&3]; t = ssp->RAM0[*rp]; |
831 | if (!(rST&7)) { (*rp)++; return t; } |
832 | add = 1; goto modulo; |
5de27868 |
833 | case 0x1b: return ssp->RAM0[3]; |
30752975 |
834 | case 0x1c: |
835 | case 0x1d: |
836 | case 0x1e: rp = &ssp->r1[t&3]; t = ssp->RAM1[*rp]; |
837 | if (!(rST&7)) { (*rp)++; return t; } |
838 | add = 1; goto modulo; |
5de27868 |
839 | case 0x1f: return ssp->RAM1[3]; |
840 | } |
841 | |
842 | return 0; |
30752975 |
843 | |
844 | modulo: |
845 | mask = (1 << (rST&7)) - 1; |
846 | *rp = (*rp & ~mask) | ((*rp + add) & mask); |
847 | return t; |
5de27868 |
848 | } |
849 | |
850 | static void ptr1_write(int op, u32 d) |
851 | { |
852 | int t = (op&3) | ((op>>6)&4) | ((op<<1)&0x18); |
853 | switch (t) |
854 | { |
855 | // mod=0 (00) |
856 | case 0x00: |
857 | case 0x01: |
858 | case 0x02: ssp->RAM0[ssp->r0[t&3]] = d; return; |
859 | case 0x03: ssp->RAM0[0] = d; return; |
860 | case 0x04: |
861 | case 0x05: |
862 | case 0x06: ssp->RAM1[ssp->r1[t&3]] = d; return; |
863 | case 0x07: ssp->RAM1[0] = d; return; |
864 | // mod=1 (01), "+!" |
865 | // mod=3, "+" |
866 | case 0x08: |
5de27868 |
867 | case 0x09: |
e477f8f7 |
868 | case 0x0a: ssp->RAM0[ssp->r0[t&3]++] = d; return; |
5de27868 |
869 | case 0x0b: ssp->RAM0[1] = d; return; |
870 | case 0x0c: |
5de27868 |
871 | case 0x0d: |
e477f8f7 |
872 | case 0x0e: ssp->RAM1[ssp->r1[t&3]++] = d; return; |
5de27868 |
873 | case 0x0f: ssp->RAM1[1] = d; return; |
874 | // mod=2 (10), "-" |
875 | case 0x10: |
876 | case 0x11: |
e477f8f7 |
877 | case 0x12: ssp->RAM0[ssp->r0[t&3]--] = d; CHECK_RPL(); return; |
5de27868 |
878 | case 0x13: ssp->RAM0[2] = d; return; |
879 | case 0x14: |
880 | case 0x15: |
e477f8f7 |
881 | case 0x16: ssp->RAM1[ssp->r1[t&3]--] = d; CHECK_RPL(); return; |
5de27868 |
882 | case 0x17: ssp->RAM1[2] = d; return; |
e477f8f7 |
883 | // mod=3 (11), "+" |
884 | case 0x18: |
885 | case 0x19: |
886 | case 0x1a: ssp->RAM0[ssp->r0[t&3]++] = d; CHECK_RPL(); return; |
5de27868 |
887 | case 0x1b: ssp->RAM0[3] = d; return; |
e477f8f7 |
888 | case 0x1c: |
889 | case 0x1d: |
890 | case 0x1e: ssp->RAM1[ssp->r1[t&3]++] = d; CHECK_RPL(); return; |
5de27868 |
891 | case 0x1f: ssp->RAM1[3] = d; return; |
892 | } |
893 | } |
894 | |
895 | static u32 ptr2_read(int op) |
896 | { |
897 | int mv = 0, t = (op&3) | ((op>>6)&4) | ((op<<1)&0x18); |
898 | switch (t) |
899 | { |
900 | // mod=0 (00) |
901 | case 0x00: |
902 | case 0x01: |
903 | case 0x02: mv = ssp->RAM0[ssp->r0[t&3]]++; break; |
904 | case 0x03: mv = ssp->RAM0[0]++; break; |
905 | case 0x04: |
906 | case 0x05: |
907 | case 0x06: mv = ssp->RAM1[ssp->r1[t&3]]++; break; |
908 | case 0x07: mv = ssp->RAM1[0]++; break; |
909 | // mod=1 (01) |
910 | case 0x0b: mv = ssp->RAM0[1]++; break; |
911 | case 0x0f: mv = ssp->RAM1[1]++; break; |
912 | // mod=2 (10) |
913 | case 0x13: mv = ssp->RAM0[2]++; break; |
914 | case 0x17: mv = ssp->RAM1[2]++; break; |
915 | // mod=3 (11) |
916 | case 0x1b: mv = ssp->RAM0[3]++; break; |
917 | case 0x1f: mv = ssp->RAM1[3]++; break; |
30752975 |
918 | default: elprintf(EL_SVP|EL_ANOMALY, "ssp FIXME: invalid mod in ((rX))? @ %04x", GET_PPC_OFFS()); |
5de27868 |
919 | return 0; |
920 | } |
921 | |
922 | return ((unsigned short *)svp->iram_rom)[mv]; |
923 | } |
924 | |
925 | |
926 | // ----------------------------------------------------- |
927 | |
71bb1b7b |
928 | #if defined(USE_DEBUGGER) |
726bbb3e |
929 | static void debug_dump2file(const char *fname, void *mem, int len) |
930 | { |
931 | FILE *f = fopen(fname, "wb"); |
932 | unsigned short *p = mem; |
933 | int i; |
934 | if (f) { |
935 | for (i = 0; i < len/2; i++) p[i] = (p[i]<<8) | (p[i]>>8); |
936 | fwrite(mem, 1, len, f); |
937 | fclose(f); |
938 | for (i = 0; i < len/2; i++) p[i] = (p[i]<<8) | (p[i]>>8); |
939 | printf("dumped to %s\n", fname); |
940 | } |
941 | else |
942 | printf("dump failed\n"); |
943 | } |
944 | #endif |
945 | |
3554b0a4 |
946 | #ifdef USE_DEBUGGER |
5de27868 |
947 | static void debug_dump(void) |
f8ef8ff7 |
948 | { |
5de27868 |
949 | printf("GR0: %04x X: %04x Y: %04x A: %08x\n", ssp->gr[SSP_GR0].h, rX, rY, ssp->gr[SSP_A].v); |
6b6a3e50 |
950 | printf("PC: %04x (%04x) P: %08x\n", GET_PC(), GET_PC() << 1, rP.v); |
5de27868 |
951 | printf("PM0: %04x PM1: %04x PM2: %04x\n", rPM0, rPM1, rPM2); |
6b6a3e50 |
952 | printf("XST: %04x PM4: %04x PMC: %08x\n", rXST, rPM4, rPMC.v); |
d26dc685 |
953 | printf(" ST: %04x %c%c%c%c, GP0_0 %i, GP0_1 %i\n", rST, rST&SSP_FLAG_N?'N':'n', rST&SSP_FLAG_V?'V':'v', |
954 | rST&SSP_FLAG_Z?'Z':'z', rST&SSP_FLAG_L?'L':'l', (rST>>5)&1, (rST>>6)&1); |
5de27868 |
955 | printf("STACK: %i %04x %04x %04x %04x %04x %04x\n", rSTACK, ssp->stack[0], ssp->stack[1], |
956 | ssp->stack[2], ssp->stack[3], ssp->stack[4], ssp->stack[5]); |
957 | printf("r0-r2: %02x %02x %02x r4-r6: %02x %02x %02x\n", rIJ[0], rIJ[1], rIJ[2], rIJ[4], rIJ[5], rIJ[6]); |
958 | elprintf(EL_SVP, "cycles: %i, emu_status: %x", g_cycles, ssp->emu_status); |
959 | } |
f8ef8ff7 |
960 | |
5de27868 |
961 | static void debug_dump_mem(void) |
962 | { |
963 | int h, i; |
964 | printf("RAM0\n"); |
965 | for (h = 0; h < 32; h++) |
966 | { |
967 | if (h == 16) printf("RAM1\n"); |
968 | printf("%03x:", h*16); |
969 | for (i = 0; i < 16; i++) |
970 | printf(" %04x", ssp->RAM[h*16+i]); |
971 | printf("\n"); |
972 | } |
973 | } |
974 | |
975 | static int bpts[10] = { 0, }; |
976 | |
977 | static void debug(unsigned int pc, unsigned int op) |
978 | { |
979 | static char buffo[64] = {0,}; |
980 | char buff[64] = {0,}; |
981 | int i; |
982 | |
983 | if (running) { |
984 | for (i = 0; i < 10; i++) |
985 | if (pc != 0 && bpts[i] == pc) { |
986 | printf("breakpoint %i\n", i); |
987 | running = 0; |
988 | break; |
989 | } |
990 | } |
991 | if (running) return; |
992 | |
993 | printf("%04x (%02x) @ %04x\n", op, op >> 9, pc<<1); |
994 | |
995 | while (1) |
996 | { |
997 | printf("dbg> "); |
998 | fflush(stdout); |
999 | fgets(buff, sizeof(buff), stdin); |
1000 | if (buff[0] == '\n') strcpy(buff, buffo); |
1001 | else strcpy(buffo, buff); |
1002 | |
1003 | switch (buff[0]) { |
1004 | case 0: exit(0); |
1005 | case 'c': |
1006 | case 'r': running = 1; return; |
1007 | case 's': |
1008 | case 'n': return; |
1009 | case 'x': debug_dump(); break; |
1010 | case 'm': debug_dump_mem(); break; |
1011 | case 'b': { |
1012 | char *baddr = buff + 2; |
1013 | i = 0; |
1014 | if (buff[3] == ' ') { i = buff[2] - '0'; baddr = buff + 4; } |
1015 | bpts[i] = strtol(baddr, NULL, 16) >> 1; |
1016 | printf("breakpoint %i set @ %04x\n", i, bpts[i]<<1); |
1017 | break; |
1018 | } |
30752975 |
1019 | case 'd': |
1020 | sprintf(buff, "iramrom_%04x.bin", last_iram); |
1021 | debug_dump2file(buff, svp->iram_rom, sizeof(svp->iram_rom)); |
1022 | debug_dump2file("dram.bin", svp->dram, sizeof(svp->dram)); |
5de27868 |
1023 | break; |
5de27868 |
1024 | default: printf("unknown command\n"); break; |
1025 | } |
1026 | } |
1027 | } |
3554b0a4 |
1028 | #endif // USE_DEBUGGER |
1029 | |
5de27868 |
1030 | |
6b6a3e50 |
1031 | void ssp1601_reset(ssp1601_t *l_ssp) |
1032 | { |
1033 | ssp = l_ssp; |
1034 | ssp->emu_status = 0; |
1035 | ssp->gr[SSP_GR0].v = 0xffff0000; |
1036 | rPC = 0x400; |
1037 | rSTACK = 0; // ? using ascending stack |
1038 | rST = 0; |
1039 | } |
1040 | |
1041 | |
5de27868 |
1042 | void ssp1601_run(int cycles) |
1043 | { |
017512f2 |
1044 | SET_PC(rPC); |
71bb1b7b |
1045 | |
017512f2 |
1046 | g_cycles = cycles; |
f8ef8ff7 |
1047 | |
d26dc685 |
1048 | while (g_cycles > 0 && !(ssp->emu_status & SSP_WAIT_MASK)) |
f8ef8ff7 |
1049 | { |
5de27868 |
1050 | int op; |
1051 | u32 tmpv; |
1052 | |
1053 | op = *PC++; |
3554b0a4 |
1054 | #ifdef USE_DEBUGGER |
5de27868 |
1055 | debug(GET_PC()-1, op); |
3554b0a4 |
1056 | #endif |
f8ef8ff7 |
1057 | switch (op >> 9) |
1058 | { |
1059 | // ld d, s |
5de27868 |
1060 | case 0x00: |
e477f8f7 |
1061 | CHECK_B_SET(); |
f8ef8ff7 |
1062 | if (op == 0) break; // nop |
017512f2 |
1063 | if (op == ((SSP_A<<4)|SSP_P)) { // A <- P |
017512f2 |
1064 | read_P(); // update P |
6b6a3e50 |
1065 | rA32 = rP.v; |
f8ef8ff7 |
1066 | } |
5de27868 |
1067 | else |
f8ef8ff7 |
1068 | { |
5de27868 |
1069 | tmpv = REG_READ(op & 0x0f); |
1070 | REG_WRITE((op & 0xf0) >> 4, tmpv); |
1071 | } |
1072 | break; |
1073 | |
1074 | // ld d, (ri) |
1075 | case 0x01: tmpv = ptr1_read(op); REG_WRITE((op & 0xf0) >> 4, tmpv); break; |
1076 | |
1077 | // ld (ri), s |
1078 | case 0x02: tmpv = REG_READ((op & 0xf0) >> 4); ptr1_write(op, tmpv); break; |
1079 | |
1080 | // ldi d, imm |
e477f8f7 |
1081 | case 0x04: CHECK_10f(); tmpv = *PC++; REG_WRITE((op & 0xf0) >> 4, tmpv); g_cycles--; break; |
5de27868 |
1082 | |
1083 | // ld d, ((ri)) |
e477f8f7 |
1084 | case 0x05: CHECK_MOD(); tmpv = ptr2_read(op); REG_WRITE((op & 0xf0) >> 4, tmpv); g_cycles -= 2; break; |
5de27868 |
1085 | |
1086 | // ldi (ri), imm |
e477f8f7 |
1087 | case 0x06: tmpv = *PC++; ptr1_write(op, tmpv); g_cycles--; break; |
5de27868 |
1088 | |
1089 | // ld adr, a |
1090 | case 0x07: ssp->RAM[op & 0x1ff] = rA; break; |
1091 | |
1092 | // ld d, ri |
e477f8f7 |
1093 | case 0x09: CHECK_MOD(); tmpv = rIJ[(op&3)|((op>>6)&4)]; REG_WRITE((op & 0xf0) >> 4, tmpv); break; |
5de27868 |
1094 | |
1095 | // ld ri, s |
e477f8f7 |
1096 | case 0x0a: CHECK_MOD(); rIJ[(op&3)|((op>>6)&4)] = REG_READ((op & 0xf0) >> 4); break; |
5de27868 |
1097 | |
1098 | // ldi ri, simm |
1099 | case 0x0c: |
1100 | case 0x0d: |
1101 | case 0x0e: |
1102 | case 0x0f: rIJ[(op>>8)&7] = op; break; |
1103 | |
1104 | // call cond, addr |
1105 | case 0x24: { |
1106 | int cond = 0; |
e477f8f7 |
1107 | CHECK_00f(); |
5de27868 |
1108 | COND_CHECK |
e477f8f7 |
1109 | if (cond) { int new_PC = *PC++; write_STACK(GET_PC()); SET_PC(new_PC); } |
5de27868 |
1110 | else PC++; |
e477f8f7 |
1111 | g_cycles--; // always 2 cycles |
5de27868 |
1112 | break; |
1113 | } |
1114 | |
1115 | // ld d, (a) |
e477f8f7 |
1116 | case 0x25: |
1117 | CHECK_10f(); |
1118 | tmpv = ((unsigned short *)svp->iram_rom)[rA]; |
1119 | REG_WRITE((op & 0xf0) >> 4, tmpv); |
1120 | g_cycles -= 2; // 3 cycles total |
1121 | break; |
5de27868 |
1122 | |
1123 | // bra cond, addr |
1124 | case 0x26: { |
1125 | int cond = 0; |
e477f8f7 |
1126 | CHECK_00f(); |
5de27868 |
1127 | COND_CHECK |
e477f8f7 |
1128 | if (cond) { int new_PC = *PC++; SET_PC(new_PC); } |
5de27868 |
1129 | else PC++; |
e477f8f7 |
1130 | g_cycles--; |
5de27868 |
1131 | break; |
1132 | } |
1133 | |
1134 | // mod cond, op |
1135 | case 0x48: { |
1136 | int cond = 0; |
e477f8f7 |
1137 | CHECK_008(); |
5de27868 |
1138 | COND_CHECK |
1139 | if (cond) { |
1140 | switch (op & 7) { |
d26dc685 |
1141 | case 2: rA32 = (signed int)rA32 >> 1; break; // shr (arithmetic) |
5de27868 |
1142 | case 3: rA32 <<= 1; break; // shl |
d26dc685 |
1143 | case 6: rA32 = -(signed int)rA32; break; // neg |
1144 | case 7: if ((int)rA32 < 0) rA32 = -(signed int)rA32; break; // abs |
689fb2c0 |
1145 | default: elprintf(EL_SVP|EL_ANOMALY, "ssp FIXME: unhandled mod %i @ %04x", |
1146 | op&7, GET_PPC_OFFS()); |
5de27868 |
1147 | } |
e477f8f7 |
1148 | UPD_ACC_ZN |
f8ef8ff7 |
1149 | } |
1150 | break; |
5de27868 |
1151 | } |
1152 | |
689fb2c0 |
1153 | // mpys? |
30752975 |
1154 | case 0x1b: |
e477f8f7 |
1155 | CHECK_B_CLEAR(); |
30752975 |
1156 | read_P(); // update P |
e477f8f7 |
1157 | rA32 -= rP.v; |
1158 | UPD_ACC_ZN |
1159 | rX = ptr1_read_(op&3, 0, (op<<1)&0x18); |
1160 | rY = ptr1_read_((op>>4)&3, 4, (op>>3)&0x18); |
3554b0a4 |
1161 | break; |
1162 | |
5de27868 |
1163 | // mpya (rj), (ri), b |
1164 | case 0x4b: |
e477f8f7 |
1165 | CHECK_B_CLEAR(); |
5de27868 |
1166 | read_P(); // update P |
e477f8f7 |
1167 | rA32 += rP.v; |
1168 | UPD_ACC_ZN |
1169 | rX = ptr1_read_(op&3, 0, (op<<1)&0x18); |
1170 | rY = ptr1_read_((op>>4)&3, 4, (op>>3)&0x18); |
5de27868 |
1171 | break; |
1172 | |
1173 | // mld (rj), (ri), b |
1174 | case 0x5b: |
e477f8f7 |
1175 | CHECK_B_CLEAR(); |
3554b0a4 |
1176 | rA32 = 0; |
e477f8f7 |
1177 | rST &= 0x0fff; |
1178 | rST |= SSP_FLAG_Z; |
1179 | rX = ptr1_read_(op&3, 0, (op<<1)&0x18); |
1180 | rY = ptr1_read_((op>>4)&3, 4, (op>>3)&0x18); |
5de27868 |
1181 | break; |
1182 | |
1183 | // OP a, s |
e477f8f7 |
1184 | case 0x10: CHECK_1f0(); OP_CHECK32(OP_SUBA32); tmpv = REG_READ(op & 0x0f); OP_SUBA(tmpv); break; |
1185 | case 0x30: CHECK_1f0(); OP_CHECK32(OP_CMPA32); tmpv = REG_READ(op & 0x0f); OP_CMPA(tmpv); break; |
1186 | case 0x40: CHECK_1f0(); OP_CHECK32(OP_ADDA32); tmpv = REG_READ(op & 0x0f); OP_ADDA(tmpv); break; |
1187 | case 0x50: CHECK_1f0(); OP_CHECK32(OP_ANDA32); tmpv = REG_READ(op & 0x0f); OP_ANDA(tmpv); break; |
1188 | case 0x60: CHECK_1f0(); OP_CHECK32(OP_ORA32 ); tmpv = REG_READ(op & 0x0f); OP_ORA (tmpv); break; |
1189 | case 0x70: CHECK_1f0(); OP_CHECK32(OP_EORA32); tmpv = REG_READ(op & 0x0f); OP_EORA(tmpv); break; |
5de27868 |
1190 | |
1191 | // OP a, (ri) |
e477f8f7 |
1192 | case 0x11: CHECK_0f0(); tmpv = ptr1_read(op); OP_SUBA(tmpv); break; |
1193 | case 0x31: CHECK_0f0(); tmpv = ptr1_read(op); OP_CMPA(tmpv); break; |
1194 | case 0x41: CHECK_0f0(); tmpv = ptr1_read(op); OP_ADDA(tmpv); break; |
1195 | case 0x51: CHECK_0f0(); tmpv = ptr1_read(op); OP_ANDA(tmpv); break; |
1196 | case 0x61: CHECK_0f0(); tmpv = ptr1_read(op); OP_ORA (tmpv); break; |
1197 | case 0x71: CHECK_0f0(); tmpv = ptr1_read(op); OP_EORA(tmpv); break; |
5de27868 |
1198 | |
1199 | // OP a, adr |
1200 | case 0x03: tmpv = ssp->RAM[op & 0x1ff]; OP_LDA (tmpv); break; |
1201 | case 0x13: tmpv = ssp->RAM[op & 0x1ff]; OP_SUBA(tmpv); break; |
1202 | case 0x33: tmpv = ssp->RAM[op & 0x1ff]; OP_CMPA(tmpv); break; |
1203 | case 0x43: tmpv = ssp->RAM[op & 0x1ff]; OP_ADDA(tmpv); break; |
1204 | case 0x53: tmpv = ssp->RAM[op & 0x1ff]; OP_ANDA(tmpv); break; |
1205 | case 0x63: tmpv = ssp->RAM[op & 0x1ff]; OP_ORA (tmpv); break; |
1206 | case 0x73: tmpv = ssp->RAM[op & 0x1ff]; OP_EORA(tmpv); break; |
1207 | |
1208 | // OP a, imm |
e477f8f7 |
1209 | case 0x14: CHECK_IMM16(); tmpv = *PC++; OP_SUBA(tmpv); g_cycles--; break; |
1210 | case 0x34: CHECK_IMM16(); tmpv = *PC++; OP_CMPA(tmpv); g_cycles--; break; |
1211 | case 0x44: CHECK_IMM16(); tmpv = *PC++; OP_ADDA(tmpv); g_cycles--; break; |
1212 | case 0x54: CHECK_IMM16(); tmpv = *PC++; OP_ANDA(tmpv); g_cycles--; break; |
1213 | case 0x64: CHECK_IMM16(); tmpv = *PC++; OP_ORA (tmpv); g_cycles--; break; |
1214 | case 0x74: CHECK_IMM16(); tmpv = *PC++; OP_EORA(tmpv); g_cycles--; break; |
5de27868 |
1215 | |
1216 | // OP a, ((ri)) |
e477f8f7 |
1217 | case 0x15: CHECK_MOD(); tmpv = ptr2_read(op); OP_SUBA(tmpv); g_cycles -= 2; break; |
1218 | case 0x35: CHECK_MOD(); tmpv = ptr2_read(op); OP_CMPA(tmpv); g_cycles -= 2; break; |
1219 | case 0x45: CHECK_MOD(); tmpv = ptr2_read(op); OP_ADDA(tmpv); g_cycles -= 2; break; |
1220 | case 0x55: CHECK_MOD(); tmpv = ptr2_read(op); OP_ANDA(tmpv); g_cycles -= 2; break; |
1221 | case 0x65: CHECK_MOD(); tmpv = ptr2_read(op); OP_ORA (tmpv); g_cycles -= 2; break; |
1222 | case 0x75: CHECK_MOD(); tmpv = ptr2_read(op); OP_EORA(tmpv); g_cycles -= 2; break; |
5de27868 |
1223 | |
1224 | // OP a, ri |
e477f8f7 |
1225 | case 0x19: CHECK_MOD(); tmpv = rIJ[IJind]; OP_SUBA(tmpv); break; |
1226 | case 0x39: CHECK_MOD(); tmpv = rIJ[IJind]; OP_CMPA(tmpv); break; |
1227 | case 0x49: CHECK_MOD(); tmpv = rIJ[IJind]; OP_ADDA(tmpv); break; |
1228 | case 0x59: CHECK_MOD(); tmpv = rIJ[IJind]; OP_ANDA(tmpv); break; |
1229 | case 0x69: CHECK_MOD(); tmpv = rIJ[IJind]; OP_ORA (tmpv); break; |
1230 | case 0x79: CHECK_MOD(); tmpv = rIJ[IJind]; OP_EORA(tmpv); break; |
5de27868 |
1231 | |
1232 | // OP simm |
e477f8f7 |
1233 | case 0x1c: CHECK_B_SET(); OP_SUBA(op & 0xff); break; |
1234 | case 0x3c: CHECK_B_SET(); OP_CMPA(op & 0xff); break; |
1235 | case 0x4c: CHECK_B_SET(); OP_ADDA(op & 0xff); break; |
1236 | case 0x5c: CHECK_B_SET(); OP_ANDA(op & 0xff); break; |
1237 | case 0x6c: CHECK_B_SET(); OP_ORA (op & 0xff); break; |
1238 | case 0x7c: CHECK_B_SET(); OP_EORA(op & 0xff); break; |
f8ef8ff7 |
1239 | |
1240 | default: |
3554b0a4 |
1241 | elprintf(EL_ANOMALY|EL_SVP, "ssp FIXME unhandled op %04x @ %04x", op, GET_PPC_OFFS()); |
017512f2 |
1242 | break; |
f8ef8ff7 |
1243 | } |
017512f2 |
1244 | g_cycles--; |
f8ef8ff7 |
1245 | } |
1246 | |
1247 | rPC = GET_PC(); |
726bbb3e |
1248 | read_P(); // update P |
f8ef8ff7 |
1249 | } |
1250 | |