]> Shamusworld >> Repos - virtualjaguar/blob - src/include/mamegpu.h
Visual Jaguar GCC/SDL v1.0.3 UN*X update
[virtualjaguar] / src / include / mamegpu.h
1 #define ZFLAG                           0x00001
2 #define CFLAG                           0x00002
3 #define NFLAG                           0x00004
4 #define IFLAG                           0x00008
5 static UINT8 *          condition_table=0;
6
7 #define CONDITION(x)    condition_table[(x) + ((jaguar_FLAGS & 7) << 5)]
8 static UINT16 *         mirror_table;
9
10
11
12
13 /*###################################################################################################
14 **      MEMORY ACCESSORS
15 **#################################################################################################*/
16
17 #define ROPCODE(pc)             (gpu_word_read(pc))
18 uint16 jaguar_ppc;
19 uint16 jaguar_op;
20 int32 jaguar_icount;
21 static const UINT32 convert_zero[32] =
22 { 32,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31 };
23
24 static uint32 cnt_opcode=0;
25 int jaguargpu_execute(int cycles)
26 {
27         int i,j;
28         /* allocate the mirror table */
29         if (!mirror_table)
30         {
31                 mirror_table = (UINT16*)malloc(65536 * sizeof(mirror_table[0]));
32
33                 /* fill in the mirror table */
34                 if (mirror_table)
35                         for (i = 0; i < 65536; i++)
36                                 mirror_table[i] = ((i >> 15) & 0x0001) | ((i >> 13) & 0x0002) |
37                                                                   ((i >> 11) & 0x0004) | ((i >> 9)  & 0x0008) |
38                                                                   ((i >> 7)  & 0x0010) | ((i >> 5)  & 0x0020) |
39                                                                   ((i >> 3)  & 0x0040) | ((i >> 1)  & 0x0080) |
40                                                                   ((i << 1)  & 0x0100) | ((i << 3)  & 0x0200) |
41                                                                   ((i << 5)  & 0x0400) | ((i << 7)  & 0x0800) |
42                                                                   ((i << 9)  & 0x1000) | ((i << 11) & 0x2000) |
43                                                                   ((i << 13) & 0x4000) | ((i << 15) & 0x8000);
44         }
45         
46         if (!condition_table)
47         {
48                 condition_table = (uint8*)malloc(32 * 8 * sizeof(condition_table[0]));
49
50                 /* fill in the condition table */
51                 if (condition_table)
52                         for (i = 0; i < 8; i++)
53                                 for (j = 0; j < 32; j++)
54                                 {
55                                         int result = 1;
56                                         if (j & 1)
57                                                 if (i & ZFLAG) result = 0;
58                                         if (j & 2)
59                                                 if (!(i & ZFLAG)) result = 0;
60                                         if (j & 4)
61                                                 if (i & (CFLAG << (j >> 4))) result = 0;
62                                         if (j & 8)
63                                                 if (!(i & (CFLAG << (j >> 4)))) result = 0;
64                                         condition_table[i * 32 + j] = result;
65                                 }
66         }
67         /* if we're halted, we shouldn't be here */
68         if (!gpu_running)
69         {
70                 return cycles;
71         }
72         jaguar_icount=cycles;
73
74         do
75         {
76                 gpu_flag_c=(gpu_flag_c?1:0);
77                 gpu_flag_z=(gpu_flag_z?1:0);
78                 gpu_flag_n=(gpu_flag_n?1:0);
79
80 //              fprintf(log_get(),"%i 0x%.8x [%i %i %i]\n",cnt_opcode++,gpu_pc,gpu_flag_c,gpu_flag_z,gpu_flag_n);
81                 jaguar_ppc = gpu_pc;
82
83                 /* instruction fetch */
84                 jaguar_op = ROPCODE(gpu_pc);
85                 gpu_pc += 2;
86
87                 /* parse the instruction */
88                 (*gpu_op_table[jaguar_op >> 10])();
89                 jaguar_icount--;
90
91         } while ((jaguar_icount > 0)&&(gpu_running));
92
93         return cycles - jaguar_icount;
94 }
95 /*###################################################################################################
96 **      OPCODES
97 **#################################################################################################*/
98
99 void abs_rn(void)
100 {
101         int dreg = jaguar_op & 31;
102         UINT32 res = gpu_reg[dreg];
103         CLR_ZNC;
104         if (res & 0x80000000)
105         {
106                 gpu_reg[dreg] = res = -res;
107                 SET_C
108         }
109         SET_Z(res);
110 }
111
112 void add_rn_rn(void)
113 {
114         int dreg = jaguar_op & 31;
115         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
116         UINT32 r2 = gpu_reg[dreg];
117         UINT32 res = r2 + r1;
118         gpu_reg[dreg] = res;
119         CLR_ZNC; SET_ZNC_ADD(r2,r1,res);
120 }
121
122 void addc_rn_rn(void)
123 {
124         int dreg = jaguar_op & 31;
125         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
126         UINT32 r2 = gpu_reg[dreg];
127         UINT32 res = r2 + r1 + gpu_flag_c;
128         gpu_reg[dreg] = res;
129         CLR_ZNC; SET_ZNC_ADD(r2,r1,res);
130 }
131
132 void addq_n_rn(void)
133 {
134         int dreg = jaguar_op & 31;
135         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
136         UINT32 r2 = gpu_reg[dreg];
137         UINT32 res = r2 + r1;
138         gpu_reg[dreg] = res;
139         CLR_ZNC; SET_ZNC_ADD(r2,r1,res);
140 }
141
142 void addqmod_n_rn(void) /* DSP only */
143 {
144         int dreg = jaguar_op & 31;
145         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
146         UINT32 r2 = gpu_reg[dreg];
147         UINT32 res = r2 + r1;
148         res = (res & ~gpu_hidata) | (r2 & ~gpu_hidata);
149         gpu_reg[dreg] = res;
150         CLR_ZNC; SET_ZNC_ADD(r2,r1,res);
151 }
152
153 void addqt_n_rn(void)
154 {
155         int dreg = jaguar_op & 31;
156         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
157         UINT32 r2 = gpu_reg[dreg];
158         UINT32 res = r2 + r1;
159         gpu_reg[dreg] = res;
160 }
161
162 void and_rn_rn(void)
163 {
164         int dreg = jaguar_op & 31;
165         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
166         UINT32 r2 = gpu_reg[dreg];
167         UINT32 res = r2 & r1;
168         gpu_reg[dreg] = res;
169         CLR_ZN; SET_ZN(res);
170 }
171
172 void bclr_n_rn(void)
173 {
174         int dreg = jaguar_op & 31;
175         UINT32 r1 = (jaguar_op >> 5) & 31;
176         UINT32 r2 = gpu_reg[dreg];
177         UINT32 res = r2 & ~(1 << r1);
178         gpu_reg[dreg] = res;
179         CLR_ZN; SET_ZN(res);
180 }
181
182 void bset_n_rn(void)
183 {
184         int dreg = jaguar_op & 31;
185         UINT32 r1 = (jaguar_op >> 5) & 31;
186         UINT32 r2 = gpu_reg[dreg];
187         UINT32 res = r2 | (1 << r1);
188         gpu_reg[dreg] = res;
189         CLR_ZN; SET_ZN(res);
190 }
191
192 void btst_n_rn(void)
193 {
194         UINT32 r1 = (jaguar_op >> 5) & 31;
195         UINT32 r2 = gpu_reg[jaguar_op & 31];
196         CLR_Z; gpu_flag_z= (~r2 >> r1) & 1;
197 }
198
199 void cmp_rn_rn(void)
200 {
201         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
202         UINT32 r2 = gpu_reg[jaguar_op & 31];
203         UINT32 res = r2 - r1;
204         CLR_ZNC; SET_ZNC_SUB(r2,r1,res);
205 }
206
207 void cmpq_n_rn(void)
208 {
209         UINT32 r1 = (INT8)(jaguar_op >> 2) >> 3;
210         UINT32 r2 = gpu_reg[jaguar_op & 31];
211         UINT32 res = r2 - r1;
212         CLR_ZNC; SET_ZNC_SUB(r2,r1,res);
213 }
214
215 void div_rn_rn(void)
216 {
217         int dreg = jaguar_op & 31;
218         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
219         UINT32 r2 = gpu_reg[dreg];
220         if (r1)
221         {
222                 if (gpu_div_control & 1)
223                 {
224                         gpu_reg[dreg] = ((UINT64)r2 << 16) / r1;
225                         gpu_remain = ((UINT64)r2 << 16) % r1;
226                 }
227                 else
228                 {
229                         gpu_reg[dreg] = r2 / r1;
230                         gpu_remain = r2 % r1;
231                 }
232         }
233         else
234                 gpu_reg[dreg] = 0xffffffff;
235 }
236
237 void illegal(void)
238 {
239 }
240
241 void imacn_rn_rn(void)
242 {
243         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
244         UINT32 r2 = gpu_reg[jaguar_op & 31];
245         gpu_acc += (INT64)((INT16)r1 * (INT16)r2);
246 }
247
248 void imult_rn_rn(void)
249 {
250         int dreg = jaguar_op & 31;
251         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
252         UINT32 r2 = gpu_reg[dreg];
253         UINT32 res = (INT16)r1 * (INT16)r2;
254         gpu_reg[dreg] = res;
255         CLR_ZN; SET_ZN(res);
256 }
257
258 void imultn_rn_rn(void)
259 {
260         int dreg = jaguar_op & 31;
261         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
262         UINT32 r2 = gpu_reg[dreg];
263         UINT32 res = (INT16)r1 * (INT16)r2;
264         gpu_acc = (INT32)res;
265         CLR_ZN; SET_ZN(res);
266
267         jaguar_op = ROPCODE(gpu_pc);
268         while ((jaguar_op >> 10) == 20)
269         {
270                 r1 = gpu_reg[(jaguar_op >> 5) & 31];
271                 r2 = gpu_reg[jaguar_op & 31];
272                 gpu_acc += (INT64)((INT16)r1 * (INT16)r2);
273                 gpu_pc += 2;
274                 jaguar_op = ROPCODE(gpu_pc);
275         }
276         if ((jaguar_op >> 10) == 19)
277         {
278                 gpu_pc += 2;
279                 gpu_reg[jaguar_op & 31] = (UINT32)gpu_acc;
280         }
281 }
282
283 void jr_cc_n(void)
284 {
285         UINT32 jaguar_FLAGS;
286
287         gpu_flag_c?(gpu_flag_c=1):(gpu_flag_c=0);
288         gpu_flag_z?(gpu_flag_z=1):(gpu_flag_z=0);
289         gpu_flag_n?(gpu_flag_n=1):(gpu_flag_n=0);
290
291         jaguar_FLAGS=(gpu_flag_n<<2)|(gpu_flag_c<<1)|gpu_flag_z;
292
293         if (CONDITION(jaguar_op & 31))
294         {
295                 INT32 r1 = (INT8)((jaguar_op >> 2) & 0xf8) >> 2;
296                 UINT32 newpc = gpu_pc + r1;
297                 jaguar_op = ROPCODE(gpu_pc);
298         //      fprintf(log_get(),"%i 0x%.8x [%i %i %i]\n",cnt_opcode++,gpu_pc,gpu_flag_c,gpu_flag_z,gpu_flag_n);
299                 gpu_pc = newpc;
300                 (*gpu_op_table[jaguar_op >> 10])();
301
302                 jaguar_icount -= 3;     /* 3 wait states guaranteed */
303         }
304 }
305
306 void jump_cc_rn(void)
307 {
308         UINT32 jaguar_FLAGS;
309
310         gpu_flag_c?(gpu_flag_c=1):(gpu_flag_c=0);
311         gpu_flag_z?(gpu_flag_z=1):(gpu_flag_z=0);
312         gpu_flag_n?(gpu_flag_n=1):(gpu_flag_n=0);
313
314         jaguar_FLAGS=(gpu_flag_n<<2)|(gpu_flag_c<<1)|gpu_flag_z;
315         if (CONDITION(jaguar_op & 31))
316         {
317                 UINT8 reg = (jaguar_op >> 5) & 31;
318
319                 /* special kludge for risky code in the cojag DSP interrupt handlers */
320                 UINT32 newpc = /*(jaguar_icount == bankswitch_icount) ?*/ gpu_reg[reg];// : gpu_reg[reg];
321                 jaguar_op = ROPCODE(gpu_pc);
322         //      fprintf(log_get(),"%i 0x%.8x [%i %i %i]\n",cnt_opcode++,gpu_pc,gpu_flag_c,gpu_flag_z,gpu_flag_n);
323                 gpu_pc = newpc;
324                 (*gpu_op_table[jaguar_op >> 10])();
325
326                 jaguar_icount -= 3;     /* 3 wait states guaranteed */
327         }
328 }
329
330 void load_rn_rn(void)
331 {
332         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
333         gpu_reg[jaguar_op & 31] = gpu_long_read(r1);
334 }
335
336 void load_r14n_rn(void)
337 {
338         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
339         gpu_reg[jaguar_op & 31] = gpu_long_read(gpu_reg[14] + 4 * r1);
340 }
341
342 void load_r15n_rn(void)
343 {
344         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
345         gpu_reg[jaguar_op & 31] = gpu_long_read(gpu_reg[15] + 4 * r1);
346 }
347
348 void load_r14rn_rn(void)
349 {
350         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
351         gpu_reg[jaguar_op & 31] = gpu_long_read(gpu_reg[14] + r1);
352 }
353
354 void load_r15rn_rn(void)
355 {
356         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
357         gpu_reg[jaguar_op & 31] = gpu_long_read(gpu_reg[15] + r1);
358 }
359
360 void loadb_rn_rn(void)
361 {
362         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
363         gpu_reg[jaguar_op & 31] = gpu_byte_read(r1);
364 }
365
366 void loadw_rn_rn(void)
367 {
368         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
369         gpu_reg[jaguar_op & 31] = gpu_word_read(r1);
370 }
371
372 void loadp_rn_rn(void)  /* GPU only */
373 {
374         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
375         gpu_hidata = gpu_word_read(r1);
376         gpu_reg[jaguar_op & 31] = gpu_word_read(r1+4);
377 }
378
379 void mirror_rn(void)    /* DSP only */
380 {
381         int dreg = jaguar_op & 31;
382         UINT32 r1 = gpu_reg[dreg];
383         UINT32 res = (mirror_table[r1 & 0xffff] << 16) | mirror_table[r1 >> 16];
384         gpu_reg[dreg] = res;
385         CLR_ZN; SET_ZN(res);
386 }
387
388 void mmult_rn_rn(void)
389 {
390         int count = gpu_matrix_control & 15, i;
391         int sreg = (jaguar_op >> 5) & 31;
392         int dreg = jaguar_op & 31;
393         UINT32 addr = gpu_pointer_to_matrix;
394         INT64 accum = 0;
395         UINT32 res;
396
397         if (!(gpu_matrix_control & 0x10))
398         {
399                 for (i = 0; i < count; i++)
400                 {
401                         accum += (INT16)(gpu_reg_bank_1[sreg + i/2] >> (16 * ((i & 1) ^ 1))) * (INT16)gpu_word_read(addr);
402                         addr += 2;
403                 }
404         }
405         else
406         {
407                 for (i = 0; i < count; i++)
408                 {
409                         accum += (INT16)(gpu_reg_bank_1[sreg + i/2] >> (16 * ((i & 1) ^ 1))) * (INT16)gpu_word_read(addr);
410                         addr += 2 * count;
411                 }
412         }
413         gpu_reg[dreg] = res = (UINT32)accum;
414         CLR_ZN; SET_ZN(res);
415 }
416
417 void move_rn_rn(void)
418 {
419         gpu_reg[jaguar_op & 31] = gpu_reg[(jaguar_op >> 5) & 31];
420 }
421
422 void move_pc_rn(void)
423 {
424         gpu_reg[jaguar_op & 31] = jaguar_ppc;
425 }
426
427 void movefa_rn_rn(void)
428 {
429         gpu_reg[jaguar_op & 31] = gpu_alternate_reg[(jaguar_op >> 5) & 31];
430 }
431
432 void movei_n_rn(void)
433 {
434         UINT32 res = ROPCODE(gpu_pc) | (ROPCODE(gpu_pc + 2) << 16);
435         gpu_pc += 4;
436         gpu_reg[jaguar_op & 31] = res;
437 }
438
439 void moveq_n_rn(void)
440 {
441         gpu_reg[jaguar_op & 31] = (jaguar_op >> 5) & 31;
442 }
443
444 void moveta_rn_rn(void)
445 {
446         gpu_alternate_reg[jaguar_op & 31] = gpu_reg[(jaguar_op >> 5) & 31];
447 }
448
449 void mtoi_rn_rn(void)
450 {
451         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
452         gpu_reg[jaguar_op & 31] = (((INT32)r1 >> 8) & 0xff800000) | (r1 & 0x007fffff);
453 }
454
455 void mult_rn_rn(void)
456 {
457         int dreg = jaguar_op & 31;
458         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
459         UINT32 r2 = gpu_reg[dreg];
460         UINT32 res = (UINT16)r1 * (UINT16)r2;
461         gpu_reg[dreg] = res;
462         CLR_ZN; SET_ZN(res);
463 }
464
465 void neg_rn(void)
466 {
467         int dreg = jaguar_op & 31;
468         UINT32 r2 = gpu_reg[dreg];
469         UINT32 res = -r2;
470         gpu_reg[dreg] = res;
471         CLR_ZNC; SET_ZNC_SUB(0,r2,res);
472 }
473
474 void nop(void)
475 {
476 }
477
478 void normi_rn_rn(void)
479 {
480         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
481         UINT32 res = 0;
482         while ((r1 & 0xffc00000) == 0)
483         {
484                 r1 <<= 1;
485                 res--;
486         }
487         while ((r1 & 0xff800000) != 0)
488         {
489                 r1 >>= 1;
490                 res++;
491         }
492         gpu_reg[jaguar_op & 31] = res;
493         CLR_ZN; SET_ZN(res);
494 }
495
496 void not_rn(void)
497 {
498         int dreg = jaguar_op & 31;
499         UINT32 res = ~gpu_reg[dreg];
500         gpu_reg[dreg] = res;
501         CLR_ZN; SET_ZN(res);
502 }
503
504 void or_rn_rn(void)
505 {
506         int dreg = jaguar_op & 31;
507         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
508         UINT32 r2 = gpu_reg[dreg];
509         UINT32 res = r1 | r2;
510         gpu_reg[dreg] = res;
511         CLR_ZN; SET_ZN(res);
512 }
513
514 void pack_rn(void)              /* GPU only */
515 {
516         int dreg = jaguar_op & 31;
517         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
518         UINT32 r2 = gpu_reg[dreg];
519         UINT32 res;
520         if (r1 == 0)    /* PACK */
521                 res = ((r2 >> 10) & 0xf000) | ((r2 >> 5) & 0x0f00) | (r2 & 0xff);
522         else                    /* UNPACK */
523                 res = ((r2 & 0xf000) << 10) | ((r2 & 0x0f00) << 5) | (r2 & 0xff);
524         gpu_reg[dreg] = res;
525         CLR_ZN; SET_ZN(res);
526 }
527
528 void resmac_rn(void)
529 {
530         gpu_reg[jaguar_op & 31] = (UINT32)gpu_acc;
531 }
532
533 void ror_rn_rn(void)
534 {
535         int dreg = jaguar_op & 31;
536         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31] & 31;
537         UINT32 r2 = gpu_reg[dreg];
538         UINT32 res = (r2 >> r1) | (r2 << (32 - r1));
539         gpu_reg[dreg] = res;
540         CLR_ZNC; SET_ZN(res); gpu_flag_c = (r2 >> 30) & 2;
541 }
542
543 void rorq_n_rn(void)
544 {
545         int dreg = jaguar_op & 31;
546         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
547         UINT32 r2 = gpu_reg[dreg];
548         UINT32 res = (r2 >> r1) | (r2 << (32 - r1));
549         gpu_reg[dreg] = res;
550         CLR_ZNC; SET_ZN(res); gpu_flag_c= (r2 >> 30) & 2;
551 }
552
553 void sat8_rn(void)              /* GPU only */
554 {
555         int dreg = jaguar_op & 31;
556         INT32 r2 = gpu_reg[dreg];
557         UINT32 res = (r2 < 0) ? 0 : (r2 > 255) ? 255 : r2;
558         gpu_reg[dreg] = res;
559         CLR_ZN; SET_ZN(res);
560 }
561
562 void sat16_rn(void)             /* GPU only */
563 {
564         int dreg = jaguar_op & 31;
565         INT32 r2 = gpu_reg[dreg];
566         UINT32 res = (r2 < 0) ? 0 : (r2 > 65535) ? 65535 : r2;
567         gpu_reg[dreg] = res;
568         CLR_ZN; SET_ZN(res);
569 }
570
571 void sat16s_rn(void)            /* DSP only */
572 {
573         int dreg = jaguar_op & 31;
574         INT32 r2 = gpu_reg[dreg];
575         UINT32 res = (r2 < -32768) ? -32768 : (r2 > 32767) ? 32767 : r2;
576         gpu_reg[dreg] = res;
577         CLR_ZN; SET_ZN(res);
578 }
579
580 void sat24_rn(void)                     /* GPU only */
581 {
582         int dreg = jaguar_op & 31;
583         INT32 r2 = gpu_reg[dreg];
584         UINT32 res = (r2 < 0) ? 0 : (r2 > 16777215) ? 16777215 : r2;
585         gpu_reg[dreg] = res;
586         CLR_ZN; SET_ZN(res);
587 }
588
589 void sat32s_rn(void)            /* DSP only */
590 {
591         int dreg = jaguar_op & 31;
592         INT32 r2 = (UINT32)gpu_reg[dreg];
593         INT32 temp = gpu_acc >> 32;
594         UINT32 res = (temp < -1) ? (INT32)0x80000000 : (temp > 0) ? (INT32)0x7fffffff : r2;
595         gpu_reg[dreg] = res;
596         CLR_ZN; SET_ZN(res);
597 }
598
599 void sh_rn_rn(void)
600 {
601         int dreg = jaguar_op & 31;
602         INT32 r1 = (INT32)gpu_reg[(jaguar_op >> 5) & 31];
603         UINT32 r2 = gpu_reg[dreg];
604         UINT32 res;
605
606         CLR_ZNC;
607         if (r1 < 0)
608         {
609                 res = (r1 <= -32) ? 0 : (r2 << -r1);
610                 gpu_flag_c= (r2 >> 30) & 2;
611         }
612         else
613         {
614                 res = (r1 >= 32) ? 0 : (r2 >> r1);
615                 gpu_flag_c= (r2 << 1) & 2;
616         }
617         gpu_reg[dreg] = res;
618         SET_ZN(res);
619 }
620
621 void sha_rn_rn(void)
622 {
623         int dreg = jaguar_op & 31;
624         INT32 r1 = (INT32)gpu_reg[(jaguar_op >> 5) & 31];
625         UINT32 r2 = gpu_reg[dreg];
626         UINT32 res;
627
628         CLR_ZNC;
629         if (r1 < 0)
630         {
631                 res = (r1 <= -32) ? 0 : (r2 << -r1);
632                 gpu_flag_c= (r2 >> 30) & 2;
633         }
634         else
635         {
636                 res = (r1 >= 32) ? ((INT32)r2 >> 31) : ((INT32)r2 >> r1);
637                 gpu_flag_c= (r2 << 1) & 2;
638         }
639         gpu_reg[dreg] = res;
640         SET_ZN(res);
641 }
642
643 void sharq_n_rn(void)
644 {
645         int dreg = jaguar_op & 31;
646         INT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
647         UINT32 r2 = gpu_reg[dreg];
648         UINT32 res = (INT32)r2 >> r1;
649         gpu_reg[dreg] = res;
650         CLR_ZNC; SET_ZN(res); gpu_flag_c= (r2 << 1) & 2;
651 }
652
653 void shlq_n_rn(void)
654 {
655         int dreg = jaguar_op & 31;
656         INT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
657         UINT32 r2 = gpu_reg[dreg];
658         UINT32 res = r2 << (32 - r1);
659         gpu_reg[dreg] = res;
660         CLR_ZNC; SET_ZN(res); gpu_flag_c= (r2 >> 30) & 2;
661 }
662
663 void shrq_n_rn(void)
664 {
665         int dreg = jaguar_op & 31;
666         INT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
667         UINT32 r2 = gpu_reg[dreg];
668         UINT32 res = r2 >> r1;
669         gpu_reg[dreg] = res;
670         CLR_ZNC; SET_ZN(res); gpu_flag_c= (r2 << 1) & 2;
671 }
672
673 void store_rn_rn(void)
674 {
675         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
676         gpu_long_write(r1, gpu_reg[jaguar_op & 31]);
677 }
678
679 void store_rn_r14n(void)
680 {
681         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
682         gpu_long_write(gpu_reg[14] + r1 * 4, gpu_reg[jaguar_op & 31]);
683 }
684
685 void store_rn_r15n(void)
686 {
687         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
688         gpu_long_write(gpu_reg[15] + r1 * 4, gpu_reg[jaguar_op & 31]);
689 }
690
691 void store_rn_r14rn(void)
692 {
693         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
694         gpu_long_write(gpu_reg[14] + r1, gpu_reg[jaguar_op & 31]);
695 }
696
697 void store_rn_r15rn(void)
698 {
699         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
700         gpu_long_write(gpu_reg[15] + r1, gpu_reg[jaguar_op & 31]);
701 }
702
703 void storeb_rn_rn(void)
704 {
705         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
706         gpu_byte_write(r1, gpu_reg[jaguar_op & 31]);
707 }
708
709 void storew_rn_rn(void)
710 {
711         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
712         gpu_word_write(r1, gpu_reg[jaguar_op & 31]);
713 }
714
715 void storep_rn_rn(void) /* GPU only */
716 {
717         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
718         gpu_long_write(r1, gpu_hidata);
719         gpu_long_write(r1+4, gpu_reg[jaguar_op & 31]);
720 }
721
722 void sub_rn_rn(void)
723 {
724         int dreg = jaguar_op & 31;
725         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
726         UINT32 r2 = gpu_reg[dreg];
727 //      fprintf(log_get(),"r%i=0x%.8x r%i=0x%.8x\n",(jaguar_op >> 5) & 31,r1,dreg,r2);
728         UINT32 res = r2 - r1;
729         gpu_reg[dreg] = res;
730         CLR_ZNC; SET_ZNC_SUB(r2,r1,res);
731 }
732
733 void subc_rn_rn(void)
734 {
735         int dreg = jaguar_op & 31;
736         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
737         UINT32 r2 = gpu_reg[dreg];
738         UINT32 res = r2 - r1 - gpu_flag_c;
739         gpu_reg[dreg] = res;
740         CLR_ZNC; SET_ZNC_SUB(r2,r1,res);
741 }
742
743 void subq_n_rn(void)
744 {
745         int dreg = jaguar_op & 31;
746         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
747         UINT32 r2 = gpu_reg[dreg];
748         UINT32 res = r2 - r1;
749         gpu_reg[dreg] = res;
750         CLR_ZNC; SET_ZNC_SUB(r2,r1,res);
751 }
752
753 void subqmod_n_rn(void) /* DSP only */
754 {
755         int dreg = jaguar_op & 31;
756         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
757         UINT32 r2 = gpu_reg[dreg];
758         UINT32 res = r2 - r1;
759 //      res = (res & ~jaguar.ctrl[D_MOD]) | (r2 & ~jaguar.ctrl[D_MOD]);
760         gpu_reg[dreg] = res;
761         CLR_ZNC; SET_ZNC_SUB(r2,r1,res);
762 }
763
764 void subqt_n_rn(void)
765 {
766         int dreg = jaguar_op & 31;
767         UINT32 r1 = convert_zero[(jaguar_op >> 5) & 31];
768         UINT32 r2 = gpu_reg[dreg];
769         UINT32 res = r2 - r1;
770         gpu_reg[dreg] = res;
771 }
772
773 void xor_rn_rn(void)
774 {
775         int dreg = jaguar_op & 31;
776         UINT32 r1 = gpu_reg[(jaguar_op >> 5) & 31];
777         UINT32 r2 = gpu_reg[dreg];
778         UINT32 res = r1 ^ r2;
779         gpu_reg[dreg] = res;
780         CLR_ZN; SET_ZN(res);
781 }