-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.inc
130 lines (119 loc) · 2.93 KB
/
utils.inc
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
IF !DEF(UTILS_INCLUDED)
UTILS_INCLUDED SET 1
INCLUDE "ops.inc"
INCLUDE "constants.inc"
;;;
; Creates a routine to multiply A with a register so that the mult macro won't appear in too many places eating up precious bytes.
; @param r16 Specifies either BC or DE to be trashed by the operation.
; * the lower register will hold the value to be multiplied with A
;;;
placeMultiplyRoutine: macro
IF !DEF(MULT_PLACED)
MULT_PLACED SET 1
multiply:
mult LOW(\1), \1
ret
ELSE
FAIL "Multiply already placed"
ENDC
endm
;;;
; For sprite updates to work, we need to know where in memory they sit.
; Call this before using any sprite macros.
; @param \1 Memory address of sprite attributes before they are copied to V-RAM
;;;
setOAMStage: macro
OAMStage SET \1
endm
;;;
; Updates the flags of a given sprite.
; @param \1 Sprite number
; @param \2 The flag byte.
;;;
setSpriteFlags: macro
ldAny [OAMStage + (\1 * SPRITE_SIZE) + 3], \2
endm
;;;
; Sets the sprite to a new Y location.
; @param \1 Sprite number
; @param \2 Y co-ordinate.
;;;
setSpriteY: macro
ldAny [OAMStage + (\1 * SPRITE_SIZE)], \2
endm
;;;
; Sets all of a given sprite's attributes at once.
; @param \1 Sprite number
; @param \2 X position
; @param \3 Y position
; @param \4 Pointer to image tile
; @param \5 Flags
; @param \6 preserve HL - if 0, HL will be affected but will use fewer cycles/bytes, if 1, HL will be unchanged.
;;;
updateSprite: macro
IF (\6 == 1)
ldAny [OAMStage + (\1 * SPRITE_SIZE)], \3
ldAny [OAMStage + (\1 * SPRITE_SIZE) + 1], \2
ldAny [OAMStage + (\1 * SPRITE_SIZE) + 2], \4
ldAny [OAMStage + (\1 * SPRITE_SIZE) + 3], \5
ELSE
ld HL, OAMStage + (\1 * SPRITE_SIZE)
ldiAny [HL], \3
ldiAny [HL], \2
ldiAny [HL], \4
ldiAny [HL], \5
ENDC
endm
;;;
; ors a register/value with itself to see if it's zero
; @param \1 The value.
;;;
if0: macro
ld A, \1
or A, A
endm
;;;
; Pad out a number of bytes.
; @param \1 number of bytes required.
; @param \2 0 to pad with nop, 1 to pad with rst $00
;;;
pad: macro
IF \2 == 0
REPT \1
nop
ENDR
ELIF \2 == 1
REPT \1
rst $00
ENDR
ENDC
endm
;;;
; jpAny cc, [n16]
;
; I doubt there are any reason anyone would use this, but at least I learnt about daa
;;;
jrAny: macro
IF ("\1" == "NC") || ("\1" == "NZ") || ("\1" == "C") || ("\1" == "Z")
jr \1, \2
ELSE
IF ("\1" == "N") || ("\1" == "NN")
ld A, $0c
daa
bit 2, A
ELIF ("\1" == "H") || ("\1" == "NH")
; can't xor A because that will mess up the flags.
ld A, 0
daa
bit 1, A
ELSE
FAIL "unknown value for cc"
ENDC
IF STRLEN("\1") == 2
jr Z, \2
ELSE
jr NZ, \2
ENDC
ENDC
endm
ENDC