-
Notifications
You must be signed in to change notification settings - Fork 0
/
myLib.i
162 lines (124 loc) · 4.2 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
# 1 "myLib.c"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "myLib.c"
# 1 "myLib.h" 1
typedef unsigned short u16;
# 44 "myLib.h"
extern unsigned short *videoBuffer;
extern unsigned short *frontBuffer;
extern unsigned short *backBuffer;
void loadPalette(const unsigned short* palette);
void initialize();
void waitForVblank();
void flipPage();
void setPixel4(int row, int col, unsigned char color);
void fillScreen4(unsigned char color);
void drawBackgroundImage4(volatile const unsigned short*);
void drawImage4(volatile const unsigned short* image, int row, int col, int height, int width);
void drawRect4(int row, int col, int height, int width, volatile unsigned char colorIndex);
const unsigned char fontdata_6x8[12288];
void drawChar(int, int, char, unsigned char);
void drawString(int, int, char*, unsigned char);
# 89 "myLib.h"
extern unsigned int oldButtons;
extern unsigned int buttons;
# 99 "myLib.h"
void DMANow(int channel, volatile const void* source, volatile void* destination, unsigned int control);
typedef volatile struct
{
volatile const void *src;
volatile void *dst;
volatile unsigned int cnt;
} DMA;
extern DMA *dma;
# 228 "myLib.h"
typedef struct { u16 tileimg[8192]; } charblock;
typedef struct { u16 tilemap[1024]; } screenblock;
# 322 "myLib.h"
typedef struct{
unsigned short attr0;
unsigned short attr1;
unsigned short attr2;
unsigned short fill;
}OBJ_ATTR;
typedef struct {
int row;
int col;
} Sprite;
# 2 "myLib.c" 2
unsigned short *videoBuffer = (u16 *)0x6000000;
unsigned short *frontBuffer = (u16 *)0x6000000;
unsigned short *backBuffer = (u16 *)0x600A000;
DMA *dma = (DMA *)0x40000B0;
void loadPalette(const unsigned short* palette)
{
DMANow(3, (unsigned short*)palette, ((u16 *)0x5000000), 256);
}
void DMANow(int channel, volatile const void* source, volatile void* destination, unsigned int control){
dma[channel].src = source;
dma[channel].dst = destination;
dma[channel].cnt = (1 << 31) | control;
}
void waitForVblank(){
while(*(volatile u16 *)0x4000006 > 160);
while(*(volatile u16 *)0x4000006 < 160);
}
void flipPage(){
if(*(unsigned short *)0x4000000 & (1<<4))
{
*(unsigned short *)0x4000000 &= ~(1<<4);
videoBuffer = backBuffer;
}
else
{
*(unsigned short *)0x4000000 |= (1<<4);
videoBuffer = frontBuffer;
}
}
void setPixel4(int row, int col, unsigned char colorIndex) {
unsigned short pixel = videoBuffer[((row)*(240)+(col))/2];
if(col & 1) {
pixel &= 0xFF;
videoBuffer[((row)*(240)+(col))/2] = pixel | (colorIndex << 8);
} else {
pixel &= ~0xFF;
videoBuffer[((row)*(240)+(col))/2] = pixel | colorIndex;
}
}
void drawRect4(int row, int col, int height, int width, volatile unsigned char colorIndex) {
unsigned short pixels = colorIndex << 8 | colorIndex;
for(int r = 0; r < height; r++) {
if(col & 1) {
setPixel4(row + r, col, colorIndex);
if(width & 1) {
DMANow(3, &pixels, &videoBuffer[((row+r)*(240)+(col+1))/2], width/2 | (2 << 23));
} else {
DMANow(3, &pixels, &videoBuffer[((row+r)*(240)+(col+1))/2], (width/2 - 1) | (2 << 23));
setPixel4(row + r, col + width - 1, colorIndex);
}
} else {
if (width & 1) {
DMANow(3, &pixels, &videoBuffer[((row+r)*(240)+(col))/2], width/2 | (2 << 23));
setPixel4(row + r, col + width - 1, colorIndex);
} else {
DMANow(3, &pixels, &videoBuffer[((row+r)*(240)+(col))/2], width/2 | (2 << 23));
}
}
}
}
void drawBackgroundImage4(volatile const unsigned short* image) {
DMANow(3, (unsigned short*)image, videoBuffer, (240*160)/2);
}
void drawImage4(volatile const unsigned short* image, int row, int col, int height, int width) {
if(col & 1) {
col++;
}
for(int r = 0; r < height; r++) {
DMANow(3, (unsigned short*)&image[((r)*(width/2)+(0))], &videoBuffer[((row+r)*(240)+(col))/2], width/2);
}
}
void fillScreen4(unsigned char colorIndex) {
volatile unsigned short pixels = ((colorIndex << 8) | colorIndex);
DMANow(3, &pixels, videoBuffer, (2 << 23) | (240 * 160)/2);
}