-
Notifications
You must be signed in to change notification settings - Fork 2
/
sim_xbus.c
292 lines (244 loc) · 5.22 KB
/
sim_xbus.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
291
292
#include "opera_clio.h"
#include "sim_xbus.h"
#include <stdint.h>
#include <string.h>
#define POLSTMASK 0x01
#define POLDTMASK 0x02
#define POLMAMASK 0x04
#define POLREMASK 0x08
#define POLST 0x10
#define POLDT 0x20
#define POLMA 0x40
#define POLRE 0x80
void
sim_xbus_execute_command_f(void)
{
if (XBUS.cmdf[0] == 0x83)
{
XBUS.stlenf = 12;
XBUS.stdevf[0] = 0x83;
XBUS.stdevf[1] = 0x01;
XBUS.stdevf[2] = 0x01;
XBUS.stdevf[3] = 0x01;
XBUS.stdevf[4] = 0x01;
XBUS.stdevf[5] = 0x01;
XBUS.stdevf[6] = 0x01;
XBUS.stdevf[7] = 0x01;
XBUS.stdevf[8] = 0x01;
XBUS.stdevf[9] = 0x01;
XBUS.stdevf[10] = 0x01;
XBUS.stdevf[11] = 0x01;
XBUS.poldevf |= POLST;
}
if (((XBUS.poldevf & POLST) && (XBUS.poldevf & POLSTMASK)) ||
((XBUS.poldevf & POLDT) && (XBUS.poldevf & POLDTMASK)))
//sim_clio_fiq_generate(4, 0);
sim_xbus_fiq_request = 1;
}
void
sim_xbus_fifo_set_cmd(const uint8_t val_)
{
if (xdev[XBUS.xb_sel_l])
{
xdev[XBUS.xb_sel_l](XBP_SET_COMMAND, (void*)(uintptr_t)val_);
if (xdev[XBUS.xb_sel_l](XBP_FIQ, NULL))
//sim_clio_fiq_generate(4, 0);
sim_xbus_fiq_request = 1;
}
else if (XBUS.xb_sel_l == 0x0F)
{
if (XBUS.cmdptrf < 7)
{
XBUS.cmdf[XBUS.cmdptrf] = val_;
XBUS.cmdptrf++;
}
if (XBUS.cmdptrf >= 7)
{
sim_xbus_execute_command_f();
XBUS.cmdptrf = 0;
}
}
}
uint32_t
sim_xbus_fifo_get_data(void)
{
if (xdev[XBUS.xb_sel_l])
return (uintptr_t)xdev[XBUS.xb_sel_l](XBP_GET_DATA, NULL);
return 0;
}
uint32_t
sim_xbus_get_poll(void)
{
uint32_t res = 0x30; // Start with 0x30 bits in poll set. DATA and STATUS ready bits???
if (XBUS.xb_sel_l == 0x0F) res = XBUS.polf; // If the lower nibble of sel matches 0x0F (device 15, CDROM drive), set result to poll.
else if (xdev[XBUS.xb_sel_l]) res = (uintptr_t)xdev[XBUS.xb_sel_l](XBP_GET_POLL, NULL);
if (XBUS.xb_sel_h & 0x80) res &= 0x0F; // If the MSB bit of sel is set, mask the lower nibble of poll?
return res;
}
uint32_t
sim_xbus_get_res(void)
{
if (xdev[XBUS.xb_sel_l])
return (uintptr_t)xdev[XBUS.xb_sel_l](XBP_RESERV, NULL);
return 0;
}
uint32_t
sim_xbus_fifo_get_status(void)
{
uint32_t rv;
rv = 0;
if (xdev[XBUS.xb_sel_l])
{
rv = (uintptr_t)xdev[XBUS.xb_sel_l](XBP_GET_STATUS, NULL);
}
else if (XBUS.xb_sel_l == 0x0F) // CD drive is selected...
{
if (XBUS.stlenf > 0) // One or more bytes in FIFO buffer.
{
rv = XBUS.stdevf[0]; // Read a new byte from the buffer.
XBUS.stlenf--; // Decrement the byte count.
if (XBUS.stlenf > 0) // Still more bytes left in buffer...
{
int i;
for (i = 0; i < XBUS.stlenf; i++) // Shift the remaining bytes down, so the latest one is in XBUS.stdevf[0].
XBUS.stdevf[i] = XBUS.stdevf[i + 1]; // Shuffle the remaining bytes down.
}
else
{
XBUS.poldevf &= ~POLST;
}
}
}
return rv;
}
void
sim_xbus_fifo_set_data(const uint8_t val_)
{
if (xdev[XBUS.xb_sel_l])
xdev[XBUS.xb_sel_l](XBP_SET_DATA, (void*)(uintptr_t)val_);
}
void
sim_xbus_set_poll(const uint8_t val_)
{
if (XBUS.xb_sel_l == 0x0F)
{
XBUS.polf &= 0xF0;
XBUS.polf |= (val_ & 0x0F);
}
if (xdev[XBUS.xb_sel_l])
{
xdev[XBUS.xb_sel_l](XBP_SET_POLL, (void*)(uintptr_t)val_);
if (xdev[XBUS.xb_sel_l](XBP_FIQ, NULL))
//sim_clio_fiq_generate(4, 0);
sim_xbus_fiq_request = 1;
}
}
void sim_xbus_set_sel(const uint8_t val_)
{
XBUS.xb_sel_l = (val_ & 0x0F);
XBUS.xb_sel_h = (val_ & 0xF0);
}
void
sim_xbus_init(sim_xbus_device zero_dev_)
{
int i;
XBUS.polf = 0x0F;
for (i = 0; i < 15; i++)
xdev[i] = NULL;
sim_xbus_attach(zero_dev_);
}
int
sim_xbus_attach(sim_xbus_device dev_)
{
int i;
for (i = 0; i < 16; i++) // Find a free device slot.
{
if (!xdev[i])
break;
}
if (i == 16) // Return -1 if no free slots.
return -1;
xdev[i] = dev_; // Else, attach dev to the first available slot.
xdev[i](XBP_INIT, NULL); // Init the xbus plugin for this slot / device.
return i; // Return the slot number.
}
void
sim_xbus_device_load(int dev_,
const char* name_)
{
xdev[dev_](XBP_RESET, (void*)name_);
}
void sim_xbus_device_eject(int dev_)
{
xdev[dev_](XBP_RESET, NULL);
}
void
sim_xbus_destroy(void)
{
unsigned i;
for (i = 0; i < 16; i++)
{
if (xdev[i])
{
xdev[i](XBP_DESTROY, NULL);
xdev[i] = NULL;
}
}
}
uint32_t
sim_xbus_state_size(void)
{
int i;
uint32_t tmp = sizeof(xbus_datum_t);
tmp += (16 * 4);
for (i = 0; i < 15; i++)
{
if (!xdev[i])
continue;
tmp += (uintptr_t)xdev[i](XBP_GET_SAVESIZE, NULL);
}
return tmp;
}
void sim_xbus_state_save(void* buf_)
{
uint32_t i;
uint32_t j;
uint32_t off;
uint32_t tmp;
memcpy(buf_, &XBUS, sizeof(xbus_datum_t));
j = off = sizeof(xbus_datum_t);
off += (16 * 4);
for (i = 0; i < 15; i++)
{
if (!xdev[i])
{
tmp = 0;
memcpy(&((uint8_t*)buf_)[j + i * 4], &tmp, 4);
}
else
{
xdev[i](XBP_GET_SAVEDATA, &((uint8_t*)buf_)[off]);
memcpy(&((uint8_t*)buf_)[j + i * 4], &off, 4);
off += (uintptr_t)xdev[i](XBP_GET_SAVESIZE, NULL);
}
}
}
void
sim_xbus_state_load(const void* buf_)
{
uint32_t i;
uint32_t j;
uint32_t offd;
j = sizeof(xbus_datum_t);
memcpy(&XBUS, buf_, j);
for (i = 0; i < 15; i++)
{
memcpy(&offd, &((uint8_t*)buf_)[j + i * 4], 4);
if (!xdev[i])
continue;
if (!offd)
xdev[i](XBP_RESET, NULL);
else
xdev[i](XBP_SET_SAVEDATA, &((uint8_t*)buf_)[offd]);
}
}