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