-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgbemu.S
267 lines (221 loc) · 7.46 KB
/
gbemu.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
/************************************************************************
* gbemu.S
* Emulation of the Gameboy CPU
*
************************************************************************
*
* 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: gbemu.S,v $
* Revision 1.5 2006/10/23 15:38:53 bodowenzel
* Support for Rana
*
* Revision 1.4 2004/12/28 13:56:35 bodowenzel
* Split up all C-Code to multi-segmented
*
* 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:38:24 bodowenzel
* Import
*
************************************************************************
* Pre-CVS History (developed as PalmBoy):
*
* 2001-09-22 Bodo Wenzel Some optimization
* 2001-09-17 Bodo Wenzel Saving states now possible
* 2001-07-21 Bodo Wenzel Changed syntax to CPP + GAS
* 2001-02-05 Bodo Wenzel New stack management
* 2001-01-28 Bodo Wenzel Correction at debug modes
* 2000-10-03 Bodo Wenzel Interrupts
* 2000-09-27 Bodo Wenzel DAA implemented
* 2000-09-09 Bodo Wenzel Some improvements
* 2000-06-06 Bodo Wenzel New memory management
* 2000-05-04 Bodo Wenzel Squeezing code snippets
* 2000-04-28 Bodo Wenzel Several emulations in one executable
* 2000-02-29 Bodo Wenzel Creation
************************************************************************
*/
/* ======================================================================
* The registers of cpus are mapped that way:
* Motorola Gameboy
* d0 (scratch) data byte
* d1 (scratch) lower address byte
* d2 (scratch) higher address byte
* d3 h(23..16) / l(7..0)
* d4 d(23..16) / e(7..0)
* d5 b(23..16) / c(7..0)
* d6 flags(31..16) / opcode timer (15..0)
* d7 a(7..0)
* a0 PC as 68000 address
* a1 SP as 68000 address
* a2 (scratch) base pointer for memory access
* a3 (scratch) pointer for memory access
* a4 (pointer to label "Gbemu")
* a5 (pointer to globals)
* a6 (scratch) temporary return address
* a7 (SP for 68000)
* ======================================================================
*/
/* === Includes ======================================================= */
#include "gbemu.h"
#include "emulation.h"
#include "video.h"
/* === Constants ====================================================== */
offsetOp = 0x00 /* the code snippets are */
offsetCb = offsetOp+gbemuOpSize /* interleaved this way */
offsetRd = offsetCb+gbemuCbSize /* on pages of 256 bytes */
offsetWr = offsetRd+gbemuRdSize
offsetIn = offsetWr+gbemuWrSize
offsetOut = offsetIn+gbemuInSize
offsetXt = offsetOut+gbemuOutSize
dontOptHi = 0x5500 /* otherwise the offsets will be */
dontOptLo = 0x55 /* optimized to non-functioning */
m68kFlagZ = 2+16 /* masks for Motorola status */
m68kFlagC = 4+16
m68kMaskZ = 1<<(m68kFlagZ-16)
m68kMaskC = 1<<(m68kFlagC-16)
gbFlagZ = -0x80 /* masks for GB status */
gbFlagC = 0x10
/* === Macros for less typing ========================================= */
/* --- Administration ------------------------------------------------- */
.macro return ret /* return to caller with code */
moveq #\ret,%d0
jmp indexToAddress(gbemuReturn)
.endm
.macro next_pc /* execute next instruction */
dbra %d6,goon\@
jsr indexToAddress(gbemuJmpLcdLine)
goon\@: move.b (%a0)+,jump\@+2-Gbemu(%a4)
jump\@: jmp offsetOp+dontOptHi(%a4)
.endm
/* --- Jumps and calls ------------------------------------------------ */
.macro do_jp /* get jump address */
move.b (%a0)+,%d1
mem_rd (%a0)+
move.l %a2,[email protected](%a5)
.endm
.macro do_push addr /* push address */
sp_push
move.l \addr,%d0
sub.l [email protected](%a5),%d0
move.w %d0,-(%sp)
move.b (%sp)+,-(%a1)
move.b %d0,-(%a1)
.endm
.macro do_call /* push address and get call addr */
do_push %a3
do_jp
.endm
.macro do_ret /* get return address from stack */
move.b (%a1)+,%d1
mem_rd (%a1)+
move.l %a2,[email protected](%a5)
move.l %a3,%a0
sp_pop
.endm
/* --- Memory and input/output ---------------------------------------- */
.macro mem_rd hi /* read mem (input, hi=0xff) */
lea ret\@-Gbemu(%a4),%a6
move.b \hi,jump\@+2-Gbemu(%a4)
jump\@: jmp offsetRd+dontOptHi(%a4)
ret\@:
.endm
.macro mem_wr hi /* write mem (output, hi=0xff) */
lea ret\@-Gbemu(%a4),%a6
move.b \hi,jump\@+2-Gbemu(%a4)
jump\@: jmp offsetWr+dontOptHi(%a4)
ret\@:
.endm
.macro io_rd lo /* read input */
lea ret\@-Gbemu(%a4),%a6
move.b \lo,jump\@+2-Gbemu(%a4)
jump\@: jmp offsetIn+dontOptHi(%a4)
ret\@:
.endm
.macro io_wr lo /* write output */
lea ret\@-Gbemu(%a4),%a6
move.b \lo,jump\@+2-Gbemu(%a4)
jump\@: jmp offsetOut+dontOptHi(%a4)
ret\@:
.endm
.macro sp_push /* check and correct pushing SP */
subq.b #2,[email protected](%a5)
jcc goon\@
jsr indexToAddress(gbemuCodeSpPushCorrect)
/* Here could be a check for writing into ROM... */
goon\@:
.endm
.macro sp_pop /* check and correct popping SP */
addq.b #2,[email protected](%a5)
jcc goon\@
jsr indexToAddress(gbemuCodeSpPopCorrect)
goon\@:
.endm
/* === Macros for opcodes ============================================= */
#include "gbemu_op.inc"
/* === Macros for opcodes after prefix byte 0xcb ====================== */
#include "gbemu_cb.inc"
/* === Macros for reading and writing memory ========================== */
#include "gbemu_rw.inc"
/* === Macros for input and output ==================================== */
#include "gbemu_io.inc"
/* === Macros for extensions ========================================== */
#include "gbemu_xt.inc"
/* === Placing the code =============================================== */
.section kernel,"x"
/* === And now produce the code! ====================================== */
.macro combo num
.org ((0x\num ^ 0x80) << 8) + offsetOp
op_\num
.org ((0x\num ^ 0x80) << 8) + offsetCb
cb_\num
.org ((0x\num ^ 0x80) << 8) + offsetRd
rd_\num
.org ((0x\num ^ 0x80) << 8) + offsetWr
wr_\num
.org ((0x\num ^ 0x80) << 8) + offsetIn
in_\num
.org ((0x\num ^ 0x80) << 8) + offsetOut
out_\num
.org ((0x\num ^ 0x80) << 8) + offsetXt
xt_\num
.endm
/* -------------------------------------------------------------------- */
.org 0x0000 /* first codes 0x80 to 0xff */
.irpc i,89abcdef
.irpc j,0123456789abcdef
combo \i\j
.endr
.endr
.org 0x8000 /* now codes 0x00 to 0x7f */
Gbemu: /* "The Middle of the Code" */
/* that way we can reach all 64K */
.irpc i,01234567
.irpc j,0123456789abcdef
combo \i\j
.endr
.endr
/* === The end ======================================================== */