527b2fa9514a841b063b3608a531e77e8c10a47d
[picodrive.git] / cpu / sh2mame / sh2pico.c
1 #include <string.h>
2
3 // MAME types
4 typedef signed char  INT8;
5 typedef signed short INT16;
6 typedef signed int   INT32;
7 typedef unsigned int   UINT32;
8 typedef unsigned short UINT16;
9 typedef unsigned char  UINT8;
10
11 // pico memhandlers
12 unsigned int p32x_sh2_read8(unsigned int a, int id);
13 unsigned int p32x_sh2_read16(unsigned int a, int id);
14 unsigned int p32x_sh2_read32(unsigned int a, int id);
15 void p32x_sh2_write8(unsigned int a, unsigned int d, int id);
16 void p32x_sh2_write16(unsigned int a, unsigned int d, int id);
17 void 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)
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
42 void sh2_reset(SH2 *sh2)
43 {
44         int save_is_slave;
45         void *save_irqcallback;
46
47         save_irqcallback = sh2->irq_callback;
48         save_is_slave = sh2->is_slave;
49
50         memset(sh2, 0, sizeof(SH2));
51
52         sh2->is_slave = save_is_slave;
53         sh2->irq_callback = save_irqcallback;
54
55         sh2->pc = RL(0);
56         sh2->r[15] = RL(4);
57         sh2->sr = I;
58 }
59
60 static void sh2_do_irq(SH2 *sh2, int level, int vector)
61 {
62         sh2->irq_callback(sh2->is_slave, level);
63
64         sh2->r[15] -= 4;
65         WL(sh2->r[15], sh2->sr);                /* push SR onto stack */
66         sh2->r[15] -= 4;
67         WL(sh2->r[15], sh2->pc);                /* push PC onto stack */
68
69         /* set I flags in SR */
70         sh2->sr = (sh2->sr & ~I) | (level << 4);
71
72         /* fetch PC */
73         sh2->pc = RL(sh2->vbr + vector * 4);
74
75         /* 13 cycles at best */
76         sh2_icount -= 13;
77 }
78
79 /* Execute cycles - returns number of cycles actually run */
80 int sh2_execute(SH2 *sh2_, int cycles)
81 {
82         sh2 = sh2_;
83         sh2_icount = cycles;
84         sh2->cycles_aim += cycles;
85
86         do
87         {
88                 UINT32 opcode;
89
90                 if (sh2->delay)
91                 {
92                         sh2->ppc = sh2->delay;
93                         opcode = RW(sh2->delay);
94                         sh2->pc -= 2;
95                 }
96                 else
97                 {
98                         sh2->ppc = sh2->pc;
99                         opcode = RW(sh2->pc);
100                 }
101
102                 sh2->delay = 0;
103                 sh2->pc += 2;
104
105                 switch (opcode & ( 15 << 12))
106                 {
107                 case  0<<12: op0000(opcode); break;
108                 case  1<<12: op0001(opcode); break;
109                 case  2<<12: op0010(opcode); break;
110                 case  3<<12: op0011(opcode); break;
111                 case  4<<12: op0100(opcode); break;
112                 case  5<<12: op0101(opcode); break;
113                 case  6<<12: op0110(opcode); break;
114                 case  7<<12: op0111(opcode); break;
115                 case  8<<12: op1000(opcode); break;
116                 case  9<<12: op1001(opcode); break;
117                 case 10<<12: op1010(opcode); break;
118                 case 11<<12: op1011(opcode); break;
119                 case 12<<12: op1100(opcode); break;
120                 case 13<<12: op1101(opcode); break;
121                 case 14<<12: op1110(opcode); break;
122                 default: op1111(opcode); break;
123                 }
124
125                 if (sh2->test_irq && !sh2->delay)
126                 {
127                         if (sh2->pending_irl > sh2->pending_int_irq)
128                                 sh2_irl_irq(sh2, sh2->pending_irl);
129                         else
130                                 sh2_internal_irq(sh2, sh2->pending_int_irq, sh2->pending_int_vector);
131                         sh2->test_irq = 0;
132                 }
133                 sh2_icount--;
134         }
135         while (sh2_icount > 0 || sh2->delay);   /* can't interrupt before delay */
136
137         return cycles - sh2_icount;
138 }
139
140 void sh2_init(SH2 *sh2, int is_slave)
141 {
142         memset(sh2, 0, sizeof(*sh2));
143         sh2->is_slave = is_slave;
144 }
145
146 void sh2_irl_irq(SH2 *sh2, int level)
147 {
148         sh2->pending_irl = level;
149         if (level <= ((sh2->sr >> 4) & 0x0f))
150                 return;
151
152         sh2_do_irq(sh2, level, 64 + level/2);
153 }
154
155 void sh2_internal_irq(SH2 *sh2, int level, int vector)
156 {
157         sh2->pending_int_irq = level;
158         sh2->pending_int_vector = vector;
159         if (level <= ((sh2->sr >> 4) & 0x0f))
160                 return;
161
162         sh2_do_irq(sh2, level, vector);
163         sh2->pending_int_irq = 0; // auto-clear
164 }
165