forked from sonicretro/s1disasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMacros.asm
333 lines (270 loc) · 8.81 KB
/
Macros.asm
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
; >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
; simplifying macros and functions
; makes a VDP address difference
vdpCommDelta function addr,((addr&$3FFF)<<16)|((addr&vram_fg)>>14)
; makes a VDP command
vdpComm function addr,type,rwd,(((type&rwd)&3)<<30)|((addr&$3FFF)<<16)|(((type&rwd)&$FC)<<2)|((addr&vram_fg)>>14)
; values for the type argument
VRAM = %100001
CRAM = %101011
VSRAM = %100101
; values for the rwd argument
READ = %001100
WRITE = %000111
DMA = %100111
; ---------------------------------------------------------------------------
; Set a VRAM address via the VDP control port.
; input: 16-bit VRAM address, control port (default is (vdp_control_port).l)
; ---------------------------------------------------------------------------
locVRAM: macro loc,controlport
if ("controlport"=="")
move.l #($40000000+(((loc)&$3FFF)<<16)+(((loc)&vram_fg)>>14)),(vdp_control_port).l
else
move.l #($40000000+(((loc)&$3FFF)<<16)+(((loc)&vram_fg)>>14)),controlport
endif
endm
; ---------------------------------------------------------------------------
; DMA copy data from 68K (ROM/RAM) to the VRAM
; input: source, length, destination
; ---------------------------------------------------------------------------
writeVRAM: macro source,length,destination
lea (vdp_control_port).l,a5
move.l #(($9400|((((length)>>1)&$FF00)>>8))<<16)|($9300|(((length)>>1)&$FF)),(a5)
move.l #(($9600|((((source)>>1)&$FF00)>>8))<<16)|($9500|(((source)>>1)&$FF)),(a5)
move.w #$9700|(((((source)>>1)&$FF0000)>>16)&$7F),(a5)
move.w #$4000+((destination)&$3FFF),(a5)
move.w #$80+(((destination)&vram_fg)>>14),(v_vdp_buffer2).w
move.w (v_vdp_buffer2).w,(a5)
endm
; ---------------------------------------------------------------------------
; DMA copy data from 68K (ROM/RAM) to the CRAM
; input: source, length, destination
; ---------------------------------------------------------------------------
writeCRAM: macro source,length,destination
lea (vdp_control_port).l,a5
move.l #$94000000+(((length>>1)&$FF00)<<8)+$9300+((length>>1)&$FF),(a5)
move.l #$96000000+(((source>>1)&$FF00)<<8)+$9500+((source>>1)&$FF),(a5)
move.w #$9700+((((source>>1)&$FF0000)>>16)&$7F),(a5)
move.w #$C000+(destination&$3FFF),(a5)
move.w #$80+((destination&vram_fg)>>14),(v_vdp_buffer2).w
move.w (v_vdp_buffer2).w,(a5)
endm
; ---------------------------------------------------------------------------
; DMA fill VRAM with a value
; input: value, length, destination
; ---------------------------------------------------------------------------
fillVRAM: macro value,length,loc
lea (vdp_control_port).l,a5
move.w #$8F01,(a5)
move.l #$94000000+((length&$FF00)<<8)+$9300+(length&$FF),(a5)
move.w #$9780,(a5)
move.l #$40000080+((loc&$3FFF)<<16)+((loc&vram_fg)>>14),(a5)
move.w #value,(vdp_data_port).l
endm
; ---------------------------------------------------------------------------
; Fill portion of RAM with 0
; input: start, end
; ---------------------------------------------------------------------------
clearRAM: macro start,end
lea (start).w,a1
moveq #0,d0
move.w #((end)-(start))/4-1,d1
.loop:
move.l d0,(a1)+
dbf d1,.loop
if (end-start)&2
move.w d0,(a1)+
endif
if (end-start)&1
move.b d0,(a1)+
endif
endm
; ---------------------------------------------------------------------------
; Copy a tilemap from 68K (ROM/RAM) to the VRAM without using DMA
; input: source, destination, width [cells], height [cells]
; ---------------------------------------------------------------------------
copyTilemap: macro source,destination,width,height
lea (source).l,a1
locVRAM destination,d0
moveq #width,d1
moveq #height,d2
bsr.w TilemapToVRAM
endm
; ---------------------------------------------------------------------------
; stop the Z80
; ---------------------------------------------------------------------------
stopZ80: macro
move.w #$100,(z80_bus_request).l
endm
; ---------------------------------------------------------------------------
; wait for Z80 to stop
; ---------------------------------------------------------------------------
waitZ80: macro
.wait: btst #0,(z80_bus_request).l
bne.s .wait
endm
; ---------------------------------------------------------------------------
; reset the Z80
; ---------------------------------------------------------------------------
resetZ80: macro
move.w #$100,(z80_reset).l
endm
resetZ80a: macro
move.w #0,(z80_reset).l
endm
; ---------------------------------------------------------------------------
; start the Z80
; ---------------------------------------------------------------------------
startZ80: macro
move.w #0,(z80_bus_request).l
endm
; ---------------------------------------------------------------------------
; disable interrupts
; ---------------------------------------------------------------------------
disable_ints: macro
move #$2700,sr
endm
; ---------------------------------------------------------------------------
; enable interrupts
; ---------------------------------------------------------------------------
enable_ints: macro
move #$2300,sr
endm
; ---------------------------------------------------------------------------
; long conditional jumps
; ---------------------------------------------------------------------------
jhi: macro loc
bls.s .nojump
jmp loc
.nojump:
endm
jcc: macro loc
bcs.s .nojump
jmp loc
.nojump:
endm
jhs: macro loc
jcc loc
endm
jls: macro loc
bhi.s .nojump
jmp loc
.nojump:
endm
jcs: macro loc
bcc.s .nojump
jmp loc
.nojump:
endm
jlo: macro loc
jcs loc
endm
jeq: macro loc
bne.s .nojump
jmp loc
.nojump:
endm
jne: macro loc
beq.s .nojump
jmp loc
.nojump:
endm
jgt: macro loc
ble.s .nojump
jmp loc
.nojump:
endm
jge: macro loc
blt.s .nojump
jmp loc
.nojump:
endm
jle: macro loc
bgt.s .nojump
jmp loc
.nojump:
endm
jlt: macro loc
bge.s .nojump
jmp loc
.nojump:
endm
jpl: macro loc
bmi.s .nojump
jmp loc
.nojump:
endm
jmi: macro loc
bpl.s .nojump
jmp loc
.nojump:
endm
; ---------------------------------------------------------------------------
; check if object moves out of range
; input: location to jump to if out of range, x-axis pos (obX(a0) by default)
; ---------------------------------------------------------------------------
out_of_range: macro exit,pos
if ("pos"<>"")
move.w pos,d0 ; get object position (if specified as not obX)
else
move.w obX(a0),d0 ; get object position
endif
andi.w #$FF80,d0 ; round down to nearest $80
sub.w (v_screenposx_coarse).w,d0 ; get approximate distance between object and screen
cmpi.w #128+320+192,d0
jhi exit
endm
; ---------------------------------------------------------------------------
; bankswitch between SRAM and ROM
; (remember to enable SRAM in the header first!)
; ---------------------------------------------------------------------------
gotoSRAM: macro
move.b #1,(sram_port).l
endm
gotoROM: macro
move.b #0,(sram_port).l
endm
; ---------------------------------------------------------------------------
; compare the size of an index with ZoneCount constant
; (should be used immediately after the index)
; input: index address, element size
; ---------------------------------------------------------------------------
zonewarning: macro loc,elementsize
._end:
if (._end-loc)-(ZoneCount*elementsize)<>0
warning "Size of loc (\{(._end-loc)/elementsize}) does not match ZoneCount (\{ZoneCount})."
endif
endm
; ---------------------------------------------------------------------------
; produce a packed art-tile
; ---------------------------------------------------------------------------
make_art_tile function addr,pal,pri,((pri&1)<<15)|((pal&3)<<13)|addr
; ---------------------------------------------------------------------------
; sprite mappings and DPLCs macros
; ---------------------------------------------------------------------------
SonicMappingsVer = 1
SonicDplcVer = 1
include "_maps/MapMacros.asm"
; ---------------------------------------------------------------------------
; turn a sample rate into a djnz loop counter
; ---------------------------------------------------------------------------
pcmLoopCounter function sampleRate,baseCycles, 1+(53693175/15/(sampleRate)-(baseCycles)+(13/2))/13
dpcmLoopCounter function sampleRate, pcmLoopCounter(sampleRate,301/2) ; 301 is the number of cycles zPlayPCMLoop takes.
; macro to replace the destination with its absolute value
abs macro destination
tst.ATTRIBUTE destination
bpl.s .skip
neg.ATTRIBUTE destination
.skip:
endm
absw macro destination ; use a short branch instead
abs.ATTRIBUTE destination
endm
; macro to move the absolute value of the source in the destination
mvabs macro source,destination
move.ATTRIBUTE source,destination
bpl.s .skip
neg.ATTRIBUTE destination
.skip:
endm
tiles_to_bytes function addr,((addr&$7FF)<<5)