-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathGameBoyAdvanceIO.as
More file actions
218 lines (178 loc) · 5.92 KB
/
GameBoyAdvanceIO.as
File metadata and controls
218 lines (178 loc) · 5.92 KB
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
package {
import utils.Logger;
public class GameBoyAdvanceIO {
public var emulatorCore = emulatorCore;
//State Machine Tracking:
public var systemStatus = 0;
public var executeDynarec = false;
public var cyclesToIterate = 0;
public var cyclesIteratedPreviously = 0;
public var accumulatedClocks = 0;
public var nextEventClocks = 0;
public var BIOSFound = false;
//Initialize the various handler objects:
public var memory;
public var dma;
public var gfx;
public var sound;
public var timer;
public var irq;
public var serial;
public var joypad;
public var cartridge;
public var wait;
public var cpu;
public var stepHandle;
public var test:String = "";
public function GameBoyAdvanceIO(emulatorCore:EmulatorCore) {
// constructor code
this.emulatorCore = emulatorCore;
//State Machine Tracking:
this.systemStatus = 0;
this.executeDynarec = false;
this.cyclesToIterate = 0;
this.cyclesIteratedPreviously = 0;
this.accumulatedClocks = 0;
this.nextEventClocks = 0;
this.BIOSFound = false;
//Initialize the various handler objects:
this.memory = new GameBoyAdvanceMemory(this);
this.dma = new GameBoyAdvanceDMA(this);
this.gfx = new GameBoyAdvanceGraphics(this);
this.sound = new GameBoyAdvanceSound(this);
this.timer = new GameBoyAdvanceTimer(this);
this.irq = new GameBoyAdvanceIRQ(this);
this.serial = new GameBoyAdvanceSerial(this);
this.joypad = new GameBoyAdvanceJoyPad(this);
this.cartridge = new GameBoyAdvanceCartridge(this);
this.wait = new GameBoyAdvanceWait(this);
this.cpu = new CPU(this);
this.memory.loadReferences();
this.preprocessSystemStepper();
}
public function iterate() {
//Find out how many clocks to iterate through this run:
this.cyclesToIterate = ((this.emulatorCore.CPUCyclesTotal | 0) - (this.cyclesIteratedPreviously | 0)) | 0;
//Update our core event prediction:
this.updateCoreEventTime();
//If clocks remaining, run iterator:
this.runIterator();
//Spill our core event clocking:
this.updateCoreClocking();
//Ensure audio buffers at least once per iteration:
this.sound.audioJIT();
//If we clocked just a little too much, subtract the extra from the next run:
this.cyclesIteratedPreviously = this.cyclesToIterate | 0;
}
public function runIterator() {
//Clock through the state machine:
//trace("cycles tyo iterate: " + this.cyclesToIterate / 3 );
var st = (new Date()).milliseconds;
while ((this.cyclesToIterate / 3 | 0) > 0) {
//Handle the current system state selected:
this.stepHandle();
}
var ed = (new Date()).milliseconds;
trace("take took: " + (ed - st));
}
public function updateCore(clocks) {
clocks = clocks | 0;
//This is used during normal/dma modes of operation:
this.accumulatedClocks = ((this.accumulatedClocks | 0) + (clocks | 0)) | 0;
if ((this.accumulatedClocks | 0) >= (this.nextEventClocks | 0)) {
this.updateCoreSpill();
}
}
public function updateCoreSpill() {
this.updateCoreClocking();
this.updateCoreEventTime();
}
public function updateCoreClocking() {
var clocks = this.accumulatedClocks | 0;
//Decrement the clocks per iteration counter:
this.cyclesToIterate = ((this.cyclesToIterate | 0) - (clocks | 0)) | 0;
//Clock all components:
this.gfx.addClocks(clocks | 0);
this.timer.addClocks(clocks | 0);
this.serial.addClocks(clocks | 0);
this.accumulatedClocks = 0;
}
public function updateCoreEventTime() {
this.nextEventClocks = this.cyclesUntilNextEvent() | 0;
}
public function preprocessSystemStepper() {
switch (this.systemStatus | 0) {
case 0: //CPU Handle State
this.stepHandle = this.handleCPU;
break;
case 1: //DMA Handle State
this.stepHandle = this.handleDMA;
break;
case 2: //Handle Halt State
this.stepHandle = this.handleHalt;
break;
case 3: //DMA Inside Halt State
this.stepHandle = this.handleDMA;
break;
case 4: //Handle Stop State
this.stepHandle = this.handleStop;
break;
default:
throw(new Error("Invalid state selected."));
}
}
public function handleCPU() {
//Execute next instruction:
if (!this.executeDynarec) {
//Interpreter:
this.cpu.executeIteration();
}
else {
//LLE Dynarec JIT
this.executeDynarec = !!this.cpu.dynarec.enter();
}
}
public function handleDMA() {
if (this.dma.perform()) {
//If DMA is done, exit it:
this.deflagStepper(0x1);
}
}
public function handleHalt() {
if (!this.irq.IRQMatch()) {
//Clock up to next IRQ match or DMA:
this.updateCore(this.cyclesUntilNextEvent() | 0);
}
else {
//Exit HALT promptly:
this.deflagStepper(0x2);
}
}
public function handleStop() {
//Update sound system to add silence to buffer:
this.sound.addClocks(this.cyclesToIterate | 0);
this.cyclesToIterate = 0;
//Exits when user presses joypad or from an external irq outside of GBA internal.
}
public function cyclesUntilNextEvent() {
//Find the clocks to the next event:
var clocks = this.irq.nextEventTime() | 0;
var dmaClocks = this.dma.nextEventTime() | 0;
clocks = ((clocks > -1) ? ((dmaClocks > -1) ? Math.min(clocks | 0, dmaClocks | 0) : (clocks | 0)) : (dmaClocks | 0)) | 0;
clocks = ((clocks == -1 || clocks > this.cyclesToIterate) ? (this.cyclesToIterate | 0) : (clocks | 0)) | 0;
return clocks | 0;
}
public function deflagStepper(statusFlag) {
//Deflag a system event to step through:
statusFlag = statusFlag | 0;
this.systemStatus = ((this.systemStatus | 0) & (~statusFlag)) | 0;
this.preprocessSystemStepper();
}
public function flagStepper(statusFlag) {
//Flag a system event to step through:
statusFlag = statusFlag | 0;
this.systemStatus = ((this.systemStatus | 0) | (statusFlag | 0)) | 0;
this.preprocessSystemStepper();
}
}
}