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