32x: RLE mode + tweaks, VR runs but unstable as everything else
[picodrive.git] / cpu / sh2mame / sh2pico.c
CommitLineData
eaa10a6e 1#include <string.h>
2
3// MAME types
4typedef signed char INT8;
5typedef signed short INT16;
6typedef signed int INT32;
7typedef unsigned int UINT32;
8typedef unsigned short UINT16;
9typedef unsigned char UINT8;
10
11// pico memhandlers
b78efee2 12unsigned int p32x_sh2_read8(unsigned int a, int id);
13unsigned int p32x_sh2_read16(unsigned int a, int id);
14unsigned int p32x_sh2_read32(unsigned int a, int id);
15void p32x_sh2_write8(unsigned int a, unsigned int d, int id);
16void p32x_sh2_write16(unsigned int a, unsigned int d, int id);
17void p32x_sh2_write32(unsigned int a, unsigned int d, int id);
18
19#define RB(a) p32x_sh2_read8(a,sh2->is_slave)
20#define RW(a) p32x_sh2_read16(a,sh2->is_slave)
21#define RL(a) p32x_sh2_read32(a,sh2->is_slave)
22#define WB(a,d) p32x_sh2_write8(a,d,sh2->is_slave)
23#define WW(a,d) p32x_sh2_write16(a,d,sh2->is_slave)
24#define WL(a,d) p32x_sh2_write32(a,d,sh2->is_slave)
eaa10a6e 25
26// some stuff from sh2comn.h
27#define T 0x00000001
28#define S 0x00000002
29#define I 0x000000f0
30#define Q 0x00000100
31#define M 0x00000200
32
33#define AM 0xc7ffffff
34
35#define FLAGS (M|Q|I|S|T)
36
37#define Rn ((opcode>>8)&15)
38#define Rm ((opcode>>4)&15)
39
40#include "sh2.c"
41
42void sh2_reset(SH2 *sh2)
43{
44 int save_is_slave;
4ea707e1 45 void *save_irqcallback;
eaa10a6e 46
4ea707e1 47 save_irqcallback = sh2->irq_callback;
eaa10a6e 48 save_is_slave = sh2->is_slave;
49
50 memset(sh2, 0, sizeof(SH2));
51
52 sh2->is_slave = save_is_slave;
4ea707e1 53 sh2->irq_callback = save_irqcallback;
eaa10a6e 54
55 sh2->pc = RL(0);
56 sh2->r[15] = RL(4);
57 sh2->sr = I;
58
59 sh2->internal_irq_level = -1;
60}
61
62/* Execute cycles - returns number of cycles actually run */
63int sh2_execute(SH2 *sh2_, int cycles)
64{
65 sh2 = sh2_;
66 sh2_icount = cycles;
67
68 do
69 {
70 UINT32 opcode;
71
3cf9570b 72 if (sh2->delay)
73 {
a44737c1 74 sh2->ppc = sh2->delay;
3cf9570b 75 opcode = RW(sh2->delay);
76 sh2->pc -= 2;
77 }
78 else
a44737c1 79 {
80 sh2->ppc = sh2->pc;
3cf9570b 81 opcode = RW(sh2->pc);
a44737c1 82 }
eaa10a6e 83
84 sh2->delay = 0;
85 sh2->pc += 2;
eaa10a6e 86
87 switch (opcode & ( 15 << 12))
88 {
89 case 0<<12: op0000(opcode); break;
90 case 1<<12: op0001(opcode); break;
91 case 2<<12: op0010(opcode); break;
92 case 3<<12: op0011(opcode); break;
93 case 4<<12: op0100(opcode); break;
94 case 5<<12: op0101(opcode); break;
95 case 6<<12: op0110(opcode); break;
96 case 7<<12: op0111(opcode); break;
97 case 8<<12: op1000(opcode); break;
98 case 9<<12: op1001(opcode); break;
99 case 10<<12: op1010(opcode); break;
100 case 11<<12: op1011(opcode); break;
101 case 12<<12: op1100(opcode); break;
102 case 13<<12: op1101(opcode); break;
103 case 14<<12: op1110(opcode); break;
104 default: op1111(opcode); break;
105 }
106
107 if (sh2->test_irq && !sh2->delay)
108 {
4ea707e1 109 if (sh2->pending_irq)
110 sh2_irl_irq(sh2, sh2->pending_irq);
eaa10a6e 111 sh2->test_irq = 0;
112 }
113 sh2_icount--;
114 }
115 while (sh2_icount > 0);
116
117 return cycles - sh2_icount;
118}
119
b78efee2 120void sh2_init(SH2 *sh2, int is_slave)
eaa10a6e 121{
122 memset(sh2, 0, sizeof(*sh2));
b78efee2 123 sh2->is_slave = is_slave;
eaa10a6e 124}
125
4ea707e1 126void sh2_irl_irq(SH2 *sh2, int level)
127{
128 int vector;
129
130 sh2->pending_irq = level;
131
132 if (level <= ((sh2->sr >> 4) & 0x0f))
133 /* masked */
134 return;
135
b78efee2 136 sh2->irq_callback(sh2->is_slave, level);
4ea707e1 137 vector = 64 + level/2;
138
139 sh2->r[15] -= 4;
140 WL(sh2->r[15], sh2->sr); /* push SR onto stack */
141 sh2->r[15] -= 4;
142 WL(sh2->r[15], sh2->pc); /* push PC onto stack */
143
144 /* set I flags in SR */
145 sh2->sr = (sh2->sr & ~I) | (level << 4);
146
147 /* fetch PC */
148 sh2->pc = RL(sh2->vbr + vector * 4);
149
150 /* 13 cycles at best */
151 sh2_icount -= 13;
152}
eaa10a6e 153