-
Notifications
You must be signed in to change notification settings - Fork 0
/
pio-paddle.c
217 lines (172 loc) · 5.49 KB
/
pio-paddle.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
/*
pio.c --
I/O chip and peripheral emulation.
*/
#include "shared.h"
io_state io_lut[2][256];
io_state *io_current;
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];
}
static uint8 paddle_toggle[2] = {0,0};
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();
if(sms.memctrl & 0x04)
return z80_read_unmapped();
paddle_toggle[0] = (io_current->th_level[0] == PIN_LVL_LO);
/* japanese mode: automatic flip-flop
paddle_toggle[0] ^= 1;*/
if (paddle_toggle[0])
{
/* Low position bits */
temp = (temp & 0xF0) | (input.analog[0][0] & 0x0F);
/* set TR low */
temp &= ~0x20;
}
else
{
/* High position bits */
temp = (temp & 0xF0) | ((input.analog[0][0] >> 4) & 0x0F);
}
/* Fire Button */
if(input.pad[0] & INPUT_BUTTON2) temp &= ~0x10; /* TL */
if(input.pad[0] & INPUT_BUTTON1) temp &= ~0x20; /* TR */
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_NTSC) 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;
}
}