-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathemulation_s.S
302 lines (275 loc) · 11 KB
/
emulation_s.S
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
/************************************************************************
* emulation_s.S
* Main emulation routines (assembler part)
*
************************************************************************
*
* Phoinix,
* Nintendo Gameboy(TM) emulator for the Palm OS(R) Computing Platform
*
* (c)2000-2006 Bodo Wenzel
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
************************************************************************
* History:
*
* $Log: emulation_s.S,v $
* Revision 1.9 2006/10/23 15:38:53 bodowenzel
* Support for Rana
*
* Revision 1.8 2005/05/03 08:39:11 bodowenzel
* Moved coldReset, ime, im and error into EmulationCpuState
*
* Revision 1.7 2005/04/03 14:08:33 bodowenzel
* Option to save the state for each game
*
* Revision 1.6 2004/12/28 13:56:35 bodowenzel
* Split up all C-Code to multi-segmented
*
* Revision 1.5 2004/01/11 19:07:14 bodowenzel
* Corrected copyright
*
* Revision 1.4 2003/04/19 13:17:03 bodowenzel
* Now all calls are jbsr xxx(%pc)
*
* Revision 1.3 2002/10/19 08:08:11 bodowenzel
* History cleanup, Javadoc-style header
*
* Revision 1.2 2001/12/30 18:47:11 bodowenzel
* CVS keyword Log was faulty
*
* Revision 1.1.1.1 2001/12/16 13:39:09 bodowenzel
* Import
*
************************************************************************
* Pre-CVS History (developed as PalmBoy):
*
* 2001-09-16 Bodo Wenzel Saving states now possible
* 2001-07-25 Bodo Wenzel Got from runmode.asm, changed syntax to
* CPP + GAS
* 2001-07-23 Bodo Wenzel v.4.0, Many sources converted to C,
* compiler/assembler now GCC
* 2001-07-01 Bodo Wenzel v.3.3b, Bugfix corrected ;-)
* 2001-06-23 Bodo Wenzel v.3.3, Bug in MBC1 code corrected
* 2001-02-22 Bodo Wenzel v.3.2 (v.3.14159), Hsync interrupts and
* support of more cartridges
* 2000-11-18 Bodo Wenzel v.3.1, Support of color devices
* 2000-11-03 Bodo Wenzel v.3.0a, No more "Visor crash"
* 2000-10-16 Bodo Wenzel v.3.0, Interrupts and timer
* 2000-09-30 Bodo Wenzel v.2.1, Scroller in GB code
* 2000-09-11 Bodo Wenzel v.2.0, Nearly full screen emulation
* 2000-05-07 Bodo Wenzel v.1.0, First working version
* 2000-05-06 Bodo Wenzel v.0.9, First working version with built-in
* games
* 2000-05-01 Bodo Wenzel v.0.4, Quick'n'dirty screen conversion,
* splitting in modules, and usage of more
* registers
* 2000-04-25 Bodo Wenzel v.0.3, Errors fixed with Palm IIIC, V
* 2000-04-19 Bodo Wenzel v.0.2, Creation of user interface
* 2000-04-15 Bodo Wenzel v.0.1, Creation of user interface without
* gray image
* 2000-03-28 Bodo Wenzel v.0.0, Creation of user interface without
* emulator mock-up and gray image
************************************************************************
*/
/* === Includes ======================================================= */
#include "emulation.h"
#include "video.h"
/* === Global variables =============================================== */
.bss
.globl EmulationCpuState
EmulationCpuState:
/* NOTE: this has to match the struct in emulation.h! */
.globl EmulationCpuState.pcBase
EmulationCpuState.pcBase: .space 4 /* base address of PC */
.globl EmulationCpuState.pcOffset
EmulationCpuState.pcOffset: .space 2 /* PC in GB format */
.globl EmulationCpuState.spBase
EmulationCpuState.spBase: .space 4 /* base address of SP */
.globl EmulationCpuState.spOffset
EmulationCpuState.spOffset: .space 2 /* SP in GB format */
.globl EmulationCpuState.a
EmulationCpuState.a: .space 1 /* A */
.globl EmulationCpuState.f
EmulationCpuState.f: .space 1 /* F */
.globl EmulationCpuState.b
EmulationCpuState.b: .space 1 /* B */
.globl EmulationCpuState.c
EmulationCpuState.c: .space 1 /* C */
.globl EmulationCpuState.d
EmulationCpuState.d: .space 1 /* D */
.globl EmulationCpuState.e
EmulationCpuState.e: .space 1 /* E */
.globl EmulationCpuState.h
EmulationCpuState.h: .space 1 /* H */
.globl EmulationCpuState.l
EmulationCpuState.l: .space 1 /* L */
.globl EmulationCpuState.coldReset
EmulationCpuState.coldReset: .space 1 /* cold reset flag */
.globl EmulationCpuState.ime
EmulationCpuState.ime: .space 1 /* int. master enable */
.globl EmulationCpuState.im
EmulationCpuState.im: .space 1 /* int. mask = IME & IE */
.globl EmulationCpuState.error
EmulationCpuState.error: .space 1 /* error code */
.globl EmulationIo
EmulationIo:
/* NOTE: this has to match the struct in emulation.h! */
.globl EmulationIo.p1
EmulationIo.p1: .space 1 /* joypad, used bit 1/0 */
.globl EmulationIo.sb
EmulationIo.sb: .space 1 /* serial transfer data */
.globl EmulationIo.sc
EmulationIo.sc: .space 1 /* serial transfer ctrl */
.space 1
EmulationIo.div: .space 1 /* divider, not used */
EmulationIo.tima: .space 1 /* timer cnt, not used */
.globl EmulationIo.tma
EmulationIo.tma: .space 1 /* timer modulo */
.globl EmulationIo.tac
EmulationIo.tac: .space 1 /* timer control */
.space 0x07
.globl EmulationIo.if_
EmulationIo.if_: .space 1 /* interrupt flags */
EmulationIo.ch1_ent: .space 1 /* sound, not emulated */
EmulationIo.ch1_wave: .space 1 /* sound, not emulated */
EmulationIo.ch1_env: .space 1 /* sound, not emulated */
EmulationIo.ch1_freq_lo: .space 1 /* sound, not emulated */
EmulationIo.ch1_freq_hi_kick: .space 1 /* sound, not emulated */
.space 1
EmulationIo.ch2_wave: .space 1 /* sound, not emulated */
EmulationIo.ch2_env: .space 1 /* sound, not emulated */
EmulationIo.ch2_freq_lo: .space 1 /* sound, not emulated */
EmulationIo.ch2_freq_hi_kick: .space 1 /* sound, not emulated */
EmulationIo.ch3_onoff: .space 1 /* sound, not emulated */
EmulationIo.ch2_length: .space 1 /* sound, not emulated */
EmulationIo.ch3_volume: .space 1 /* sound, not emulated */
EmulationIo.ch3_freq_lo: .space 1 /* sound, not emulated */
EmulationIo.ch3_freq_hi_kick: .space 1 /* sound, not emulated */
.space 1
EmulationIo.ch4_length: .space 1 /* sound, not emulated */
EmulationIo.ch4_env: .space 1 /* sound, not emulated */
EmulationIo.ch4_poly: .space 1 /* sound, not emulated */
EmulationIo.ch4_kick: .space 1 /* sound, not emulated */
EmulationIo.snd_vin: .space 1 /* sound, not emulated */
EmulationIo.snd_stereo: .space 1 /* sound, not emulated */
EmulationIo.snd_stat: .space 1 /* sound, not emulated */
.space 0x09
EmulationIo.wave: .space 0x10 /* sound, not emulated */
.globl EmulationIo.lcdc
EmulationIo.lcdc: .space 1 /* LCD control */
.globl EmulationIo.stat
EmulationIo.stat: .space 1 /* LCD status */
.globl EmulationIo.scy
EmulationIo.scy: .space 1 /* LCD scroll y */
.globl EmulationIo.scx
EmulationIo.scx: .space 1 /* LCD scroll x */
.globl EmulationIo.ly
EmulationIo.ly: .space 1 /* LCD y coordinate */
.globl EmulationIo.lyc
EmulationIo.lyc: .space 1 /* LCD y compare */
.globl EmulationIo.dma
EmulationIo.dma: .space 1 /* LCD DMA transfer */
.globl EmulationIo.bgp
EmulationIo.bgp: .space 1 /* LCD bg. palette */
.globl EmulationIo.obp0
EmulationIo.obp0: .space 1 /* LCD object palette 0 */
.globl EmulationIo.obp1
EmulationIo.obp1: .space 1 /* LCD object palette 1 */
.globl EmulationIo.wy
EmulationIo.wy: .space 1 /* LCD window y */
.globl EmulationIo.wx
EmulationIo.wx: .space 1 /* LCD window x */
.space 0x34
EmulationIo.ram: .space 0x7f /* CPU RAM */
.globl EmulationIo.ie
EmulationIo.ie: .space 1 /* interrupt enable */
/* === Placing the code =============================================== */
.section emulator,"x"
/* === Function for starting the emulation ============================ */
.globl EmulationStartKernel
EmulationStartKernel:
#define eskRegs %d3-%d7/%a2-%a4/%a6
movem.l eskRegs,-(%sp)
move.l [email protected](%a5),%a4
move.l [email protected](%a5),%a0
add.w [email protected](%a5),%a0
move.l [email protected](%a5),%a1
add.w [email protected](%a5),%a1
move.b [email protected](%a5),%d7
move.b [email protected](%a5),%d6
swap %d6
move.b [email protected](%a5),%d5
swap %d5
move.b [email protected](%a5),%d5
move.b [email protected](%a5),%d4
swap %d4
move.b [email protected](%a5),%d4
move.b [email protected](%a5),%d3
swap %d3
move.b [email protected](%a5),%d3 /* get CPU */
move.w [email protected](%a5),%d6
addq.w #1,%d6 /* correction of starting opcode */
/* *** HERE IS IT! THE EMULATOR STARTS EXECUTION... ******************* */
jsr (%a4)
/* ******************************************************************** */
move.b %d0,[email protected](%a5)
move.w %d6,[email protected](%a5) /* currently unnecessary */
sub.l [email protected](%a5),%a0
move.w %a0,[email protected](%a5)
move.b %d3,[email protected](%a5)
swap %d3
move.b %d3,[email protected](%a5)
move.b %d4,[email protected](%a5)
swap %d4
move.b %d4,[email protected](%a5)
move.b %d5,[email protected](%a5)
swap %d5
move.b %d5,[email protected](%a5)
swap %d6
move.b %d6,[email protected](%a5)
move.b %d7,[email protected](%a5) /* save CPU */
movem.l (%sp)+,eskRegs
rts
/* === Functions for additional support =============================== */
/* --- Emulation of opcode $10 $00 (STOP) ----------------------------- */
.globl EmulationOpcodeStop
EmulationOpcodeStop:
lea VideoClearBuffer(%pc),%a2
move.l %a2,2+indexToAddress(gbemuJmpLcdLine)
jsr indexToAddress(gbemuJmpLcdLine)
lea [email protected](%a5),%a2
move.b (%a2),-(%sp)
move.b #0x00,(%a2)
pea EosAbort(%pc) /* set up for waiting */
EosWait:
jbsr VideoUserPoll(%pc)
moveq #0x0f,%d0
lea [email protected](%a5),%a2
and.b emulationJoypadBoth(%a2),%d0
cmp.b #0x0f,%d0
beq.s EosWait /* wait for user */
addq.w #4,%sp
move.b (%sp)+,[email protected](%a5)
rts /* back to emulation */
/* If VideoUserPoll tries to return to the emulation caller, it will
* resume here:
*/
EosAbort:
move.b (%sp)+,[email protected](%a5)
subq.w #2,%a0 /* restore state before STOP */
addq.w #4,%sp /* ignore return to emulation */
rts /* return to emulation caller */
/* === The end ======================================================== */