forked from sonicretro/s1disasm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Macros.asm
263 lines (217 loc) · 7.11 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
; ---------------------------------------------------------------------------
; 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)&$C000)>>14)),(vdp_control_port).l
else
move.l #($40000000+(((loc)&$3FFF)<<16)+(((loc)&$C000)>>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 #$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 #$4000+((destination)&$3FFF),(a5)
move.w #$80+(((destination)&$C000)>>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&$C000)>>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 byte,length,loc
lea (vdp_control_port).l,a5
move.w #$8F01,(a5) ; Set increment to 1, since DMA fill writes bytes
move.l #$94000000+((((length)-1)&$FF00)<<8)+$9300+(((length)-1)&$FF),(a5)
move.w #$9780,(a5)
move.l #$40000080+(((loc)&$3FFF)<<16)+(((loc)&$C000)>>14),(a5)
move.w #(byte)|(byte)<<8,(vdp_data_port).l
.wait: move.w (a5),d1
btst #1,d1
bne.s .wait
move.w #$8F02,(a5) ; Set increment back to 2, since the VDP usually operates on words
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
; ---------------------------------------------------------------------------
; 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
move.w (v_screenposx).w,d1 ; get screen position
subi.w #128,d1
andi.w #$FF80,d1
sub.w d1,d0 ; approx distance between object and screen
cmpi.w #128+320+192,d0
bhi.ATTRIBUTE 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.