-
Notifications
You must be signed in to change notification settings - Fork 0
/
pio.c
291 lines (231 loc) · 7.87 KB
/
pio.c
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*
pio.c --
I/O chip and peripheral emulation.
*/
#include "shared.h"
io_state io_lut[2][256];
io_state *io_current;
int contador;
extern int paddlec;
int paddlew;
void pio_init(void)
{
int i, j;
/* Make pin state LUT */
for(j = 0; j < 2; j++)
{
for(i = 0; i < 0x100; i++)
{
/* Common control: pin direction */
io_lut[j][i].tr_dir[0] = (i & 0x01) ? PIN_DIR_IN : PIN_DIR_OUT;
io_lut[j][i].th_dir[0] = (i & 0x02) ? PIN_DIR_IN : PIN_DIR_OUT;
io_lut[j][i].tr_dir[1] = (i & 0x04) ? PIN_DIR_IN : PIN_DIR_OUT;
io_lut[j][i].th_dir[1] = (i & 0x08) ? PIN_DIR_IN : PIN_DIR_OUT;
if(j == 1)
{
/* Programmable output state (Export machines only) */
io_lut[j][i].tr_level[0] = (i & 0x01) ? PIN_LVL_HI : (i & 0x10) ? PIN_LVL_HI : PIN_LVL_LO;
io_lut[j][i].th_level[0] = (i & 0x02) ? PIN_LVL_HI : (i & 0x20) ? PIN_LVL_HI : PIN_LVL_LO;
io_lut[j][i].tr_level[1] = (i & 0x04) ? PIN_LVL_HI : (i & 0x40) ? PIN_LVL_HI : PIN_LVL_LO;
io_lut[j][i].th_level[1] = (i & 0x08) ? PIN_LVL_HI : (i & 0x80) ? PIN_LVL_HI : PIN_LVL_LO;
}
else
{
/* Fixed output state (Domestic machines only) */
io_lut[j][i].tr_level[0] = (i & 0x01) ? PIN_LVL_HI : PIN_LVL_LO;
io_lut[j][i].th_level[0] = (i & 0x02) ? PIN_LVL_HI : PIN_LVL_LO;
io_lut[j][i].tr_level[1] = (i & 0x04) ? PIN_LVL_HI : PIN_LVL_LO;
io_lut[j][i].th_level[1] = (i & 0x08) ? PIN_LVL_HI : PIN_LVL_LO;
}
}
}
// hack dos code doesn't call system_reset
pio_reset();
}
void pio_reset(void)
{
/* GG SIO power-on defaults */
sms.sio.pdr = 0x7F;
sms.sio.ddr = 0xFF;
sms.sio.txdata = 0x00;
sms.sio.rxdata = 0xFF;
sms.sio.sctrl = 0x00;
/* SMS I/O power-on defaults */
ioctrl_w(0xFF);
}
void pio_shutdown(void)
{
/* Nothing to do */
}
void system_assign_device(int port, int type)
{
sms.device[port].type = type;
}
void ioctrl_w(uint8 data)
{
sms.ioctrl = data;
io_current = &io_lut[sms.territory][data];
}
uint8 device_r(int offset)
{
uint8 temp = 0x7F;
switch(sms.device[offset].type)
{
case DEVICE_NONE:
break;
case DEVICE_PAD2B:
break;
case DEVICE_PADDLE:
break;
}
return temp;
}
uint8 input_r(int offset)
{
uint8 temp = 0xFF;
/*
If I/O chip is disabled, reads return last byte of instruction that
read the I/O port.
*/
if(sms.memctrl & 0x04)
return z80_read_unmapped();
offset &= 1;
if(offset == 0)
{
/* Input port #0 */
if (paddlec==0){
if(input.pad[0] & INPUT_UP) temp &= ~0x01; /* D0 */
if(input.pad[0] & INPUT_DOWN) temp &= ~0x02; /* D1 */
if(input.pad[0] & INPUT_LEFT) temp &= ~0x04; /* D2 */
if(input.pad[0] & INPUT_RIGHT) temp &= ~0x08; /* D3 */
if(input.pad[0] & INPUT_BUTTON2) temp &= ~0x10; /* TL */
if(input.pad[0] & INPUT_BUTTON1) temp &= ~0x20; /* TR */
}
else{
/*NOTA: Simulador de paddle */
if(input.pad[0] & INPUT_LEFT)
{contador--;}
if(input.pad[0] & INPUT_RIGHT)
{contador++;}
if (contador==128)
{contador=127;}
if (contador==0)
{contador=1;}
temp = (temp & 0xF0) | ((contador >> 3) & 0x0F);
if(input.pad[0] & INPUT_BUTTON1) temp &= ~0x10; /* TL */
}
if(sms.console == CONSOLE_GG)
{
uint8 state = sio_r(0x01);
temp = (temp & 0x3F) | (state & 0x03) << 6; /* Insert D1,D0 */
}
else
{
if(input.pad[1] & INPUT_UP) temp &= ~0x40; /* D0 */
if(input.pad[1] & INPUT_DOWN) temp &= ~0x80; /* D1 */
}
/* Adjust TR state if it is an output */
if(io_current->tr_dir[0] == PIN_DIR_OUT) {
temp &= ~0x20;
temp |= (io_current->tr_level[0] == PIN_LVL_HI) ? 0x20 : 0x00;
}
}
else
{
/* Input port #1 */
if(sms.console == CONSOLE_GG)
{
uint8 state = sio_r(0x01);
temp = (temp & 0xF0) | ((state & 0x3C) >> 2); /* Insert TR,TL,D3,D2 */
temp = (temp & 0x7F) | ((state & 0x40) << 1); /* Insert TH */
}
else
{
if(input.pad[1] & INPUT_LEFT) temp &= ~0x01; /* D2 */
if(input.pad[1] & INPUT_RIGHT) temp &= ~0x02; /* D3 */
if(input.pad[1] & INPUT_BUTTON2) temp &= ~0x04; /* TL */
if(input.pad[1] & INPUT_BUTTON1) temp &= ~0x08; /* TR */
/* Adjust TR state if it is an output */
if(io_current->tr_dir[1] == PIN_DIR_OUT) {
temp &= ~0x08;
temp |= (io_current->tr_level[1] == PIN_LVL_HI) ? 0x08 : 0x00;
}
/* Adjust TH state if it is an output */
if(io_current->th_dir[1] == PIN_DIR_OUT) {
temp &= ~0x80;
temp |= (io_current->th_level[1] == PIN_LVL_HI) ? 0x80 : 0x00;
}
if(input.system & INPUT_RESET) temp &= ~0x10;
}
/* /CONT fixed at '1' for SMS/SMS2/GG */
/* /CONT fixed at '0' for GEN/MD */
if(IS_MD) temp &= ~0x20;
/* Adjust TH state if it is an output */
if(io_current->th_dir[0] == PIN_DIR_OUT) {
temp &= ~0x40;
temp |= (io_current->th_level[0] == PIN_LVL_HI) ? 0x40 : 0x00;
}
}
return temp;
}
uint8 sio_r(int offset)
{
uint8 temp;
switch(offset & 0xFF)
{
case 0: /* Input port #2 */
temp = 0xE0;
if(input.system & INPUT_START) temp &= ~0x80;
if(sms.territory == TERRITORY_DOMESTIC) temp &= ~0x40;
if(sms.display == DISPLAY_PAL) temp &= ~0x20;
return temp;
case 1: /* Parallel data register */
temp = 0x00;
temp |= (sms.sio.ddr & 0x01) ? 0x01 : (sms.sio.pdr & 0x01);
temp |= (sms.sio.ddr & 0x02) ? 0x02 : (sms.sio.pdr & 0x02);
temp |= (sms.sio.ddr & 0x04) ? 0x04 : (sms.sio.pdr & 0x04);
temp |= (sms.sio.ddr & 0x08) ? 0x08 : (sms.sio.pdr & 0x08);
temp |= (sms.sio.ddr & 0x10) ? 0x10 : (sms.sio.pdr & 0x10);
temp |= (sms.sio.ddr & 0x20) ? 0x20 : (sms.sio.pdr & 0x20);
temp |= (sms.sio.ddr & 0x40) ? 0x40 : (sms.sio.pdr & 0x40);
temp |= (sms.sio.pdr & 0x80);
return temp;
case 2: /* Data direction register and NMI enable */
return sms.sio.ddr;
case 3: /* Transmit data buffer */
return sms.sio.txdata;
case 4: /* Receive data buffer */
return sms.sio.rxdata;
case 5: /* Serial control */
return sms.sio.sctrl;
case 6: /* Stereo sound control */
return 0xFF;
}
/* Just to please compiler */
return -1;
}
void sio_w(int offset, int data)
{
switch(offset & 0xFF)
{
case 0: /* Input port #2 (read-only) */
return;
case 1: /* Parallel data register */
sms.sio.pdr = data;
return;
case 2: /* Data direction register and NMI enable */
sms.sio.ddr = data;
return;
case 3: /* Transmit data buffer */
sms.sio.txdata = data;
return;
case 4: /* Receive data buffer */
return;
case 5: /* Serial control */
sms.sio.sctrl = data & 0xF8;
return;
case 6: /* Stereo output control */
psg_stereo_w(data);
return;
}
}