-
Notifications
You must be signed in to change notification settings - Fork 0
/
myLib.i
256 lines (148 loc) · 5.87 KB
/
myLib.i
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
# 1 "myLib.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "myLib.c"
# 1 "myLib.h" 1
typedef unsigned short u16;
# 27 "myLib.h"
extern unsigned short *videoBuffer;
# 47 "myLib.h"
void setPixel3(int col, int row, unsigned short color);
void drawRect3(int col, int row, int width, int height, volatile unsigned short color);
void fillScreen3(volatile unsigned short color);
void drawImage3(int col, int row, int width, int height, const unsigned short *image);
void drawFullscreenImage3(const unsigned short *image);
void setPixel4(int col, int row, unsigned char colorIndex);
void drawRect4(int col, int row, int width, int height, volatile unsigned char colorIndex);
void fillScreen4(volatile unsigned char colorIndex);
void drawImage4(int col, int row, int width, int height, const unsigned short *image);
void drawFullscreenImage4(const unsigned short *image);
void waitForVBlank();
void flipPage();
# 83 "myLib.h"
extern unsigned short oldButtons;
extern unsigned short buttons;
# 94 "myLib.h"
typedef volatile struct {
volatile const void *src;
volatile void *dst;
unsigned int cnt;
} DMA;
extern DMA *dma;
# 134 "myLib.h"
void DMANow(int channel, volatile const void *src, volatile void *dst, unsigned int cnt);
int collision(int colA, int rowA, int widthA, int heightA, int colB, int rowB, int widthB, int heightB);
# 2 "myLib.c" 2
unsigned short *videoBuffer = (unsigned short *)0x6000000;
DMA *dma = (DMA *)0x40000B0;
void setPixel3(int col, int row, unsigned short color) {
videoBuffer[((row)*(240)+(col))] = color;
}
void setPixel4(int col, int row, unsigned char colorIndex) {
unsigned short pData = videoBuffer[((row)*(240)+(col))/2];
unsigned short colorWord = colorIndex;
if (col & 1) {
pData &= 0x00FF;
pData |= (colorWord << 8);
}
else {
pData &= 0xFF00;
pData |= colorWord;
}
videoBuffer[((row)*(240)+(col))/2] = pData;
}
void drawRect3(int col, int row, int width, int height, volatile unsigned short color) {
for(int r = 0; r < height; r++) {
DMANow(3, &color, &videoBuffer[((row+r)*(240)+(col))], (2 << 23) | width);
}
}
void drawRect4(int col, int row, int width, int height, volatile unsigned char colorIndex) {
unsigned short colorIndexShort = colorIndex;
colorIndexShort <<= 8;
colorIndexShort |= colorIndex;
if (!(col & 1) && !(width & 1)) {
for (int y = 0; y < height; y++) {
DMANow(3, &colorIndexShort, &videoBuffer[(((row + y)*(240)+(col))/2)], (2 << 23) | (width/2));
}
} else if (!(col & 1) && (width & 1)) {
if (width > 1) {
for (int y = 0; y < height; y++) {
DMANow(3, &colorIndexShort, &videoBuffer[(((row + y)*(240)+(col))/2)], (2 << 23) | ((width - 1)/2));
setPixel4(col + width - 1, row + y, colorIndex);
}
} else if (width == 1) {
for (int y = 0; y < height; y++) {
setPixel4(col, row + y, colorIndex);
}
}
} else if ((col & 1) && (width & 1)) {
if (width > 1) {
for (int y = 0; y < height; y++) {
DMANow(3, &colorIndexShort, &videoBuffer[(((row + y)*(240)+(col + 1))/2)], (2 << 23) | ((width - 1)/2));
setPixel4(col, row + y, colorIndex);
}
} else if (width == 1) {
for (int y = 0; y < height; y++) {
setPixel4(col, row + y, colorIndex);
}
}
} else if ((col & 1) && !(width & 1)) {
if (width > 2) {
for (int y = 0; y < height; y++) {
setPixel4(col, row + y, colorIndex);
DMANow(3, &colorIndexShort, &videoBuffer[(((row + y)*(240)+(col + 1))/2)], (2 << 23) | ((width - 1)/2));
setPixel4(col + width - 1, row + y, colorIndex);
}
} else if (width == 2) {
for (int y = 0; y < height; y++) {
setPixel4(col, row + y, colorIndex);
setPixel4(col + width - 1, row + y, colorIndex);
}
}
}
}
void fillScreen3(volatile unsigned short color) {
DMANow(3, &color, videoBuffer, (2 << 23) | (240 * 160));
}
void fillScreen4(volatile unsigned char colorIndex) {
volatile unsigned short pData = colorIndex | (colorIndex << 8);
DMANow(3, &pData, videoBuffer, ((160 * 240 / 2) | (0 << 21) | (2 << 23)));
}
void drawImage3(int col, int row, int width, int height, const unsigned short *image) {
for(int r = 0; r < height; r++) {
DMANow(3, &image[((r)*(width)+(0))], &videoBuffer[((row+r)*(240)+(col))], width);
}
}
void drawImage4(int col, int row, int width, int height, const unsigned short *image) {
for (int y = 0; y < height; y++) {
DMANow(3, &image[(((y)*(width/2)+(0)))], &videoBuffer[(((row + y)*(240)+(col))/2)], (width/2));
}
}
void drawFullscreenImage3(const unsigned short *image) {
DMANow(3, image, videoBuffer, 240 * 160);
}
void drawFullscreenImage4(const unsigned short *image) {
DMANow(3, image, videoBuffer, ((160 * 240 / 2) | (0 << 21) | (0 << 23)));
}
void waitForVBlank() {
while((*(volatile unsigned short *)0x4000006) > 160);
while((*(volatile unsigned short *)0x4000006) < 160);
}
void flipPage() {
if ((*(unsigned short *)0x4000000) & (1<<4)) {
videoBuffer = ((unsigned short *)0x600A000);
} else {
videoBuffer = ((unsigned short *)0x6000000);
}
(*(unsigned short *)0x4000000) ^= (1<<4);
}
void DMANow(int channel, volatile const void *src, volatile void *dst, unsigned int cnt) {
dma[channel].cnt = 0;
dma[channel].src = src;
dma[channel].dst = dst;
dma[channel].cnt = cnt | (1 << 31);
}
int collision(int colA, int rowA, int widthA, int heightA, int colB, int rowB, int widthB, int heightB) {
return rowA < rowB + heightB - 1 && rowA + heightA - 1 > rowB
&& colA < colB + widthB - 1 && colA + widthA - 1 > colB;
}