-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlibrary.s
211 lines (174 loc) · 3.93 KB
/
library.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
;;; C-Callable functions for mode 96
#include "common.def.h"
;;; TODO:
;;; * for SetTile/GetTile*, do bounds checking?
.global ClearVram
.global SetTile
.global GetTile
.global SetTileColor
.global GetTileColor
.global SetTileBoth
.global SetFont
.global FillColor
.global SetTileTable
.global InitializeVideoMode
.global DisplayLogo
.global VideoModeVsync
.section .text
;;; ========================================
;;; ClearVram
ClearVram:
ldi r30, lo8(VRAM_SIZE)
ldi r31, hi8(VRAM_SIZE)
ldi XL, lo8(vram)
ldi XH, hi8(vram)
0:
st X+, r1
sbiw r30, 1
brne 0b
clr r1
ret
;;; ========================================
;;; FillColor
;;; r24: X pos (8-bit)
;;; r22: Y pos (8-bit)
;;; r20: number of cells to set the color of (8-bit)
;;; r18: color palette index (4-bit)
FillColor:
push r16
mov r16, r20
movw r20, r18
0:
rcall SetTileColor
inc r24
dec r16
brne 0b
pop r16
ret
;;; ================================================================================
;;; Tile getter and setter functions
;;; ========================================
;;; get_tile_addr_text
;;; A helper function, not intended for use outside this library
;;; r24: X pos (8-bit)
;;; r22: Y pos (8-bit)
;;; Returns:
;;; Z: set to the address of the text of tile X,Y
get_tile_addr_text:
ldi ZL, lo8(vram)
ldi ZH, hi8(vram)
;; add width of screen in bytes * Y pos
ldi r18, SCREEN_TILES_H*3/2 ; SCREEN_TILES_H must be even
mul r22, r18
add ZL, r0
adc ZH, r1
;; get byte index from X value. index = X/2*3 + (2 if x is odd else 0)
;;
;; Note: these Python 3 expressions are equivalent:
;; (x//2)*3 + (2 if x%2 else 0)
;; x//2 + x + (1 if x%2 else 0)
mov r18, r24
lsr r18
add r18, r24
sbrc r24, 0 ; skip if even
inc r18
clr r19
add ZL, r18
adc ZH, r19
clr r1
ret
;;; ========================================
;;; get_tile_addr_color
;;; A helper function, not intended for use outside this library
;;; r24: X pos (8-bit)
;;; r22: Y pos (8-bit)
;;; Returns:
;;; Z: set to the address of the color of tile X,Y
get_tile_addr_color:
ldi ZL, lo8(vram)
ldi ZH, hi8(vram)
;; add width of screen in bytes * Y pos
ldi r18, SCREEN_TILES_H*3/2 ; SCREEN_TILES_H must be even
mul r22, r18
add ZL, r0
adc ZH, r1
;; get byte index from X value
mov r18, r24
lsr r18
add r18, r24
sbrs r24, 0 ; skip if odd
inc r18
clr r19
add ZL, r18
adc ZH, r19
clr r1
ret
;;; ========================================
;;; SetTile/SetFont
;;; r24: X pos (8-bit)
;;; r22: Y pos (8-bit)
;;; r20: tile number (8-bit)
SetTile:
SetFont:
rcall get_tile_addr_text
st Z, r20
ret
;;; ========================================
;;; GetTile
;;; r24: X pos (8-bit)
;;; r22: Y pos (8-bit)
;;; Returns:
;;; r24: tile number (8-bit)
GetTile:
rcall get_tile_addr_text
ld r24, Z
ret
;;; ========================================
;;; SetTileColor
;;; r24: X pos (8-bit)
;;; r22: Y pos (8-bit)
;;; r20: color palette index (4-bit)
SetTileColor:
rcall get_tile_addr_color
ld r18, Z
sbrs r24, 0 ; skip if X pos is odd
swap r18
andi r18, 0xF0 ; wipe current color
or r18, r20 ; add new color
sbrs r24, 0 ; skip if X pos is odd
swap r18
st Z, r18 ; write back new color
ret
;;; ========================================
;;; GetTileColor
;;; r24: X pos (8-bit)
;;; r22: Y pos (8-bit)
;;; Returns:
;;; r24: color palette index (4-bit)
GetTileColor:
rcall get_tile_addr_color
ld r18, Z
sbrs r24, 0 ; skip if X pos is odd
swap r18
andi r18, 0x0F ; only return this tile's color
movw r24, r18
ret
;;; ========================================
;;; SetTileBoth
;;; r24: X pos (8-bit)
;;; r22: Y pos (8-bit)
;;; r20: tile number (8-bit)
;;; r18: color palette index (4-bit)
SetTileBoth:
push r18
rcall SetTile
pop r20
rcall SetTileColor
ret
;;; ================================================================================
;;; Miscellaneous callback functions or functions that aren't applicable to this mode
VideoModeVsync:
SetTileTable:
InitializeVideoMode:
DisplayLogo:
ret