-
Notifications
You must be signed in to change notification settings - Fork 1
/
GameBoyAdvanceSerial.as
481 lines (471 loc) · 13.7 KB
/
GameBoyAdvanceSerial.as
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
package {
public class GameBoyAdvanceSerial {
public var IOCore;
public var SIODATA_A = 0xFFFF;
public var SIODATA_B = 0xFFFF;
public var SIODATA_C = 0xFFFF;
public var SIODATA_D = 0xFFFF;
public var SIOShiftClockExternal = true;
public var SIOShiftClockDivider = 0x40;
public var SIOCNT0_DATA = 0x0C;
public var SIOTransferStarted = false;
public var SIOMULT_PLAYER_NUMBER = 0;
public var SIOCOMMERROR = false;
public var SIOBaudRate = 0;
public var SIOCNT_UART_CTS = false;
public var SIOCNT_UART_MISC = 0;
public var SIOCNT_UART_FIFO = 0;
public var SIOCNT_IRQ = false;
public var SIOCNT_MODE = 0;
public var SIOCNT_UART_RECV_ENABLE = false;
public var SIOCNT_UART_SEND_ENABLE = false;
public var SIOCNT_UART_PARITY_ENABLE = false;
public var SIOCNT_UART_FIFO_ENABLE = false;
public var SIODATA8 = 0xFFFF;
public var RCNTMode = 0;
public var RCNTIRQ = false;
public var RCNTDataBits = 0;
public var RCNTDataBitFlow = 0;
public var JOYBUS_IRQ = false;
public var JOYBUS_CNTL_FLAGS = 0;
public var JOYBUS_RECV0 = 0xFF;
public var JOYBUS_RECV1 = 0xFF;
public var JOYBUS_RECV2 = 0xFF;
public var JOYBUS_RECV3 = 0xFF;
public var JOYBUS_SEND0 = 0xFF;
public var JOYBUS_SEND1 = 0xFF;
public var JOYBUS_SEND2 = 0xFF;
public var JOYBUS_SEND3 = 0xFF;
public var JOYBUS_STAT = 0;
public var shiftClocks = 0;
public var serialBitsShifted = 0;
public function GameBoyAdvanceSerial(IOCore) {
// constructor code
this.IOCore = IOCore;
this.initialize();
}
public function initialize() {
this.SIODATA_A = 0xFFFF;
this.SIODATA_B = 0xFFFF;
this.SIODATA_C = 0xFFFF;
this.SIODATA_D = 0xFFFF;
this.SIOShiftClockExternal = true;
this.SIOShiftClockDivider = 0x40;
this.SIOCNT0_DATA = 0x0C;
this.SIOTransferStarted = false;
this.SIOMULT_PLAYER_NUMBER = 0;
this.SIOCOMMERROR = false;
this.SIOBaudRate = 0;
this.SIOCNT_UART_CTS = false;
this.SIOCNT_UART_MISC = 0;
this.SIOCNT_UART_FIFO = 0;
this.SIOCNT_IRQ = false;
this.SIOCNT_MODE = 0;
this.SIOCNT_UART_RECV_ENABLE = false;
this.SIOCNT_UART_SEND_ENABLE = false;
this.SIOCNT_UART_PARITY_ENABLE = false;
this.SIOCNT_UART_FIFO_ENABLE = false;
this.SIODATA8 = 0xFFFF;
this.RCNTMode = 0;
this.RCNTIRQ = false;
this.RCNTDataBits = 0;
this.RCNTDataBitFlow = 0;
this.JOYBUS_IRQ = false;
this.JOYBUS_CNTL_FLAGS = 0;
this.JOYBUS_RECV0 = 0xFF;
this.JOYBUS_RECV1 = 0xFF;
this.JOYBUS_RECV2 = 0xFF;
this.JOYBUS_RECV3 = 0xFF;
this.JOYBUS_SEND0 = 0xFF;
this.JOYBUS_SEND1 = 0xFF;
this.JOYBUS_SEND2 = 0xFF;
this.JOYBUS_SEND3 = 0xFF;
this.JOYBUS_STAT = 0;
this.shiftClocks = 0;
this.serialBitsShifted = 0;
}
public var SIOMultiplayerBaudRate = [
9600,
38400,
57600,
115200
];
public function addClocks(clocks) {
if (this.RCNTMode < 2) {
switch (this.SIOCNT_MODE) {
case 0:
case 1:
if (this.SIOTransferStarted && !this.SIOShiftClockExternal) {
this.shiftClocks += clocks;
while (this.shiftClocks >= this.SIOShiftClockDivider) {
this.shiftClocks -= this.SIOShiftClockDivider;
this.clockSerial();
}
}
break;
case 2:
if (this.SIOTransferStarted && this.SIOMULT_PLAYER_NUMBER == 0) {
this.shiftClocks += clocks;
while (this.shiftClocks >= this.SIOShiftClockDivider) {
this.shiftClocks -= this.SIOShiftClockDivider;
this.clockMultiplayer();
}
}
break;
case 3:
if (this.SIOCNT_UART_SEND_ENABLE && !this.SIOCNT_UART_CTS) {
this.shiftClocks += clocks;
while (this.shiftClocks >= this.SIOShiftClockDivider) {
this.shiftClocks -= this.SIOShiftClockDivider;
this.clockUART();
}
}
}
}
}
public function clockSerial() {
//Emulate as if no slaves connected:
++this.serialBitsShifted;
if (this.SIOCNT_MODE == 0) {
//8-bit
this.SIODATA8 = ((this.SIODATA8 << 1) | 1) & 0xFFFF;
if (this.serialBitsShifted == 8) {
this.SIOTransferStarted = false;
this.serialBitsShifted = 0;
if (this.SIOCNT_IRQ) {
this.IOCore.irq.requestIRQ(0x80);
}
}
}
else {
//32-bit
this.SIODATA_D = ((this.SIODATA_D << 1) & 0xFE) | (this.SIODATA_C >> 7);
this.SIODATA_C = ((this.SIODATA_C << 1) & 0xFE) | (this.SIODATA_B >> 7);
this.SIODATA_B = ((this.SIODATA_B << 1) & 0xFE) | (this.SIODATA_A >> 7);
this.SIODATA_A = ((this.SIODATA_A << 1) & 0xFE) | 1;
if (this.serialBitsShifted == 32) {
this.SIOTransferStarted = false;
this.serialBitsShifted = 0;
if (this.SIOCNT_IRQ) {
this.IOCore.irq.requestIRQ(0x80);
}
}
}
}
public function clockMultiplayer() {
//Emulate as if no slaves connected:
this.SIODATA_A = this.SIODATA8;
this.SIODATA_B = 0xFFFF;
this.SIODATA_C = 0xFFFF;
this.SIODATA_D = 0xFFFF;
this.SIOTransferStarted = false;
this.SIOCOMMERROR = true;
if (this.SIOCNT_IRQ) {
this.IOCore.irq.requestIRQ(0x80);
}
}
public function clockUART() {
++this.serialBitsShifted;
if (this.SIOCNT_UART_FIFO_ENABLE) {
if (this.serialBitsShifted == 8) {
this.serialBitsShifted = 0;
this.SIOCNT_UART_FIFO = Math.max(this.SIOCNT_UART_FIFO - 1, 0);
if (this.SIOCNT_UART_FIFO == 0 && this.SIOCNT_IRQ) {
this.IOCore.irq.requestIRQ(0x80);
}
}
}
else {
if (this.serialBitsShifted == 8) {
this.serialBitsShifted = 0;
if (this.SIOCNT_IRQ) {
this.IOCore.irq.requestIRQ(0x80);
}
}
}
}
public function writeSIODATA_A0(data) {
this.SIODATA_A &= 0xFF00;
this.SIODATA_A |= data;
}
public function readSIODATA_A0() {
return this.SIODATA_A & 0xFF;
}
public function writeSIODATA_A1(data) {
this.SIODATA_A &= 0xFF;
this.SIODATA_A |= data << 8;
}
public function readSIODATA_A1() {
return this.SIODATA_A >> 8;
}
public function writeSIODATA_B0(data) {
this.SIODATA_B &= 0xFF00;
this.SIODATA_B |= data;
}
public function readSIODATA_B0() {
return this.SIODATA_B & 0xFF;
}
public function writeSIODATA_B1(data) {
this.SIODATA_B &= 0xFF;
this.SIODATA_B |= data << 8;
}
public function readSIODATA_B1() {
return this.SIODATA_B >> 8;
}
public function writeSIODATA_C0(data) {
this.SIODATA_C &= 0xFF00;
this.SIODATA_C |= data;
}
public function readSIODATA_C0() {
return this.SIODATA_C & 0xFF;
}
public function writeSIODATA_C1(data) {
this.SIODATA_C &= 0xFF;
this.SIODATA_C |= data << 8;
}
public function readSIODATA_C1() {
return this.SIODATA_C >> 8;
}
public function writeSIODATA_D0(data) {
this.SIODATA_D &= 0xFF00;
this.SIODATA_D |= data;
}
public function readSIODATA_D0() {
return this.SIODATA_D & 0xFF;
}
public function writeSIODATA_D1(data) {
this.SIODATA_D &= 0xFF;
this.SIODATA_D |= data << 8;
}
public function readSIODATA_D1() {
return this.SIODATA_D >> 8;
}
public function writeSIOCNT0(data) {
if (this.RCNTMode < 0x2) {
switch (this.SIOCNT_MODE) {
//8-Bit:
case 0:
//32-Bit:
case 1:
this.SIOShiftClockExternal = ((data & 0x1) == 0x1);
this.SIOShiftClockDivider = ((data & 0x2) == 0x2) ? 0x8 : 0x40;
this.SIOCNT0_DATA = data & 0xB;
if ((data & 0x80) == 0x80) {
if (!this.SIOTransferStarted) {
this.SIOTransferStarted = true;
this.serialBitsShifted = 0;
this.shiftClocks = 0;
}
}
else {
this.SIOTransferStarted = false;
}
break;
//Multiplayer:
case 2:
this.SIOBaudRate = data & 0x3;
this.SIOShiftClockDivider = this.SIOMultiplayerBaudRate[this.SIOBaudRate];
this.SIOMULT_PLAYER_NUMBER = (data >> 4) & 0x3;
this.SIOCOMMERROR = ((data & 0x40) == 0x40);
if ((data & 0x80) == 0x80) {
if (!this.SIOTransferStarted) {
this.SIOTransferStarted = true;
if (this.SIOMULT_PLAYER_NUMBER == 0) {
this.SIODATA_A = 0xFFFF;
this.SIODATA_B = 0xFFFF;
this.SIODATA_C = 0xFFFF;
this.SIODATA_D = 0xFFFF;
}
this.serialBitsShifted = 0;
this.shiftClocks = 0;
}
}
else {
this.SIOTransferStarted = false;
}
break;
//UART:
case 3:
this.SIOBaudRate = data & 0x3;
this.SIOShiftClockDivider = this.SIOMultiplayerBaudRate[this.SIOBaudRate];
this.SIOCNT_UART_MISC = (data & 0xCF) >> 2;
this.SIOCNT_UART_CTS = ((data & 0x4) == 0x4);
}
}
}
public function readSIOCNT0() {
if (this.RCNTMode < 0x2) {
switch (this.SIOCNT_MODE) {
//8-Bit:
case 0:
//32-Bit:
case 1:
return ((this.SIOTransferStarted) ? 0x80 : 0) | 0x74 | this.SIOCNT0_DATA;
//Multiplayer:
case 2:
return ((this.SIOTransferStarted) ? 0x80 : 0) | ((this.SIOCOMMERROR) ? 0x40 : 0) | (this.SIOMULT_PLAYER_NUMBER << 4) | this.SIOBaudRate;
//UART:
case 3:
return (this.SIOCNT_UART_MISC << 2) | ((this.SIOCNT_UART_FIFO == 4) ? 0x10 : 0) | 0x20 | this.SIOBaudRate;
}
}
return 0xFF;
}
public function writeSIOCNT1(data) {
this.SIOCNT_IRQ = ((data & 0x40) == 0x40);
this.SIOCNT_MODE = (data >> 4) & 0x3;
this.SIOCNT_UART_RECV_ENABLE = ((data & 0x8) == 0x8);
this.SIOCNT_UART_SEND_ENABLE = ((data & 0x4) == 0x4);
this.SIOCNT_UART_PARITY_ENABLE = ((data & 0x2) == 0x2);
this.SIOCNT_UART_FIFO_ENABLE = ((data & 0x1) == 0x1);
}
public function readSIOCNT1() {
return (0x80 | (this.SIOCNT_IRQ ? 0x40 : 0) | (this.SIOCNT_MODE << 4) | ((this.SIOCNT_UART_RECV_ENABLE) ? 0x8 : 0) |
((this.SIOCNT_UART_SEND_ENABLE) ? 0x4 : 0) | ((this.SIOCNT_UART_PARITY_ENABLE) ? 0x2 : 0) | ((this.SIOCNT_UART_FIFO_ENABLE) ? 0x2 : 0));
}
public function writeSIODATA8_0(data) {
this.SIODATA8 &= 0xFF00;
this.SIODATA8 |= data;
if (this.RCNTMode < 0x2 && this.SIOCNT_MODE == 3 && this.SIOCNT_UART_FIFO_ENABLE) {
this.SIOCNT_UART_FIFO = Math.min(this.SIOCNT_UART_FIFO + 1, 4);
}
}
public function readSIODATA8_0() {
return this.SIODATA8 & 0xFF;
}
public function writeSIODATA8_1(data) {
this.SIODATA8 &= 0xFF;
this.SIODATA8 |= data << 8;
}
public function readSIODATA8_1() {
return this.SIODATA8 >> 8;
}
public function writeRCNT0(data) {
if (this.RCNTMode == 0x2) {
//General Comm:
var oldDataBits = this.RCNTDataBits;
this.RCNTDataBits = data & 0xF; //Device manually controls SI/SO/SC/SD here.
this.RCNTDataBitFlow = data >> 4;
if (this.RCNTIRQ && ((oldDataBits ^ this.RCNTDataBits) & oldDataBits & 0x4) == 0x4) {
//SI fell low, trigger IRQ:
this.IOCore.irq.requestIRQ(0x80);
}
}
}
public function readRCNT0() {
return (this.RCNTDataBitFlow << 4) | this.RCNTDataBits;
}
public function writeRCNT1(data) {
this.RCNTMode = data >> 6;
this.RCNTIRQ = ((data & 0x1) == 0x1);
if (this.RCNTMode != 0x2) {
//Force SI/SO/SC/SD to low as we're never "hooked" up:
this.RCNTDataBits = 0;
this.RCNTDataBitFlow = 0;
}
}
public function readRCNT1() {
return (this.RCNTMode << 6) | 0x3E | ((this.RCNTIRQ) ? 0x1 : 0);
}
public function writeJOYCNT(data) {
this.JOYBUS_IRQ = ((data & 0x40) == 0x40);
this.JOYBUS_CNTL_FLAGS &= ~(data & 0x7);
}
public function readJOYCNT() {
return 0xB8 | ((this.JOYBUS_IRQ) ? 0x40 : 0) | this.JOYBUS_CNTL_FLAGS;
}
public function writeJOYBUS_RECV0(data) {
this.JOYBUS_RECV0 = data;
}
public function readJOYBUS_RECV0() {
this.JOYBUS_STAT &= 0xF7;
return this.JOYBUS_RECV0;
}
public function writeJOYBUS_RECV1(data) {
this.JOYBUS_RECV1 = data;
}
public function readJOYBUS_RECV1() {
this.JOYBUS_STAT &= 0xF7;
return this.JOYBUS_RECV1;
}
public function writeJOYBUS_RECV2(data) {
this.JOYBUS_RECV2 = data;
}
public function readJOYBUS_RECV2() {
this.JOYBUS_STAT &= 0xF7;
return this.JOYBUS_RECV2;
}
public function writeJOYBUS_RECV3(data) {
this.JOYBUS_RECV3 = data;
}
public function readJOYBUS_RECV3() {
this.JOYBUS_STAT &= 0xF7;
return this.JOYBUS_RECV3;
}
public function writeJOYBUS_SEND0(data) {
this.JOYBUS_SEND0 = data;
this.JOYBUS_STAT |= 0x2;
}
public function readJOYBUS_SEND0() {
return this.JOYBUS_SEND0;
}
public function writeJOYBUS_SEND1(data) {
this.JOYBUS_SEND1 = data;
this.JOYBUS_STAT |= 0x2;
}
public function readJOYBUS_SEND1() {
return this.JOYBUS_SEND1;
}
public function writeJOYBUS_SEND2(data) {
this.JOYBUS_SEND2 = data;
this.JOYBUS_STAT |= 0x2;
}
public function readJOYBUS_SEND2() {
return this.JOYBUS_SEND2;
}
public function writeJOYBUS_SEND3(data) {
this.JOYBUS_SEND3 = data;
this.JOYBUS_STAT |= 0x2;
}
public function readJOYBUS_SEND3() {
return this.JOYBUS_SEND3;
}
public function writeJOYBUS_STAT(data) {
this.JOYBUS_STAT = data;
}
public function readJOYBUS_STAT() {
return 0xC5 | this.JOYBUS_STAT;
}
public function nextIRQEventTime(clocks) {
if (this.SIOCNT_IRQ && this.RCNTMode < 2) {
switch (this.SIOCNT_MODE) {
case 0:
case 1:
if (this.SIOTransferStarted && !this.SIOShiftClockExternal) {
return ((((this.SIOCNT_MODE == 1) ? 31 : 7) - this.serialBitsShifted) * this.SIOShiftClockDivider) + (this.SIOShiftClockDivider - this.shiftClocks);
}
else {
return -1;
}
case 2:
if (this.SIOTransferStarted && this.SIOMULT_PLAYER_NUMBER == 0) {
return this.SIOShiftClockDivider - this.shiftClocks;
}
else {
return -1;
}
case 3:
if (this.SIOCNT_UART_SEND_ENABLE && !this.SIOCNT_UART_CTS) {
return (Math.max(((this.SIOCNT_UART_FIFO_ENABLE) ? (this.SIOCNT_UART_FIFO * 8) : 8) - 1, 0) * this.SIOShiftClockDivider) + (this.SIOShiftClockDivider - this.shiftClocks);
}
else {
return -1;
}
}
}
else {
return -1;
}
}
}
}