/
myLib.c
executable file
·169 lines (140 loc) · 3.88 KB
/
myLib.c
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
#include "myLib.h"
unsigned short *videoBuffer = (u16 *)0x6000000;
unsigned short *frontBuffer = (u16 *)0x6000000;
unsigned short *backBuffer = (u16 *)0x600A000;
DMA *dma = (DMA *)0x40000B0;
void setPixel3(int row, int col, unsigned short color)
{
videoBuffer[OFFSET(row, col, SCREENWIDTH)] = color;
}
void drawRect3(int row, int col, int height, int width, unsigned short color)
{
unsigned short c = color;
int i;
for(i = 0; i < height; i++)
{
DMANow(3, &c, &videoBuffer[OFFSET(row+i, col, SCREENWIDTH)], (width) | DMA_SOURCE_FIXED);
}
}
void drawImage3(const unsigned short* image, int row, int col, int height, int width)
{
int i;
for(i = 0; i < height; i++)
{
DMANow(3, (unsigned short*)&image[OFFSET(i,0, width)], &videoBuffer[OFFSET(row+i, col, SCREENWIDTH)], (width));
}
}
void fillScreen3(unsigned short color)
{
unsigned short c = color;
DMANow(3, &c, videoBuffer, (240*160) | DMA_SOURCE_FIXED);
}
void setPixel4(int row, int col, unsigned char colorIndex)
{
unsigned short pixels = videoBuffer[OFFSET(row, col/2, SCREENWIDTH/2)];
if(col % 2 == 0) // even
{
pixels &= 0xFF << 8;
videoBuffer[OFFSET(row, col/2, SCREENWIDTH/2)] = pixels | colorIndex;
}
else // odd
{
pixels &= 0xFF;
videoBuffer[OFFSET(row, col/2, SCREENWIDTH/2)] = pixels | colorIndex << 8;
}
}
void drawRect4(int row, int col, int height, int width, unsigned char colorIndex)
{
unsigned short pixels = colorIndex << 8 | colorIndex;
int r;
for(r = 0; r < height; r++)
{
if(col % 2 == 0) // even starting col
{
DMANow(3, &pixels, &videoBuffer[OFFSET(row + r, col/2, SCREENWIDTH/2)], (width/2) | DMA_SOURCE_FIXED);
if(width % 2 == 1) // if width is odd
{
setPixel4(row+r, col+width - 1, colorIndex);
}
}
else // old starting col
{
setPixel4(row+r, col, colorIndex);
if(width % 2 == 1) // if width is odd
{
DMANow(3, &pixels, &videoBuffer[OFFSET(row + r, (col+1)/2, SCREENWIDTH/2)], (width/2) | DMA_SOURCE_FIXED);
}
else // width is even
{
DMANow(3, &pixels, &videoBuffer[OFFSET(row + r, (col+1)/2, SCREENWIDTH/2)], ((width/2)-1) | DMA_SOURCE_FIXED);
setPixel4(row+r, col+width - 1, colorIndex);
}
}
}
}
void fillScreen4(unsigned char colorIndex)
{
unsigned short pixels = colorIndex << 8 | colorIndex;
DMANow(3, &pixels, videoBuffer, ((240 * 160)/2) | DMA_SOURCE_FIXED);
}
void drawBackgroundImage4(const unsigned short* image)
{
DMANow(3, (unsigned short*)image, videoBuffer, ((240 * 160)/2));
}
void drawImage4(const unsigned short* image, int row, int col, int height, int width)
{
if(col%2)
{
col++;
}
int r;
for(r = 0; r < height; r++)
{
DMANow(3, (unsigned short*)&image[OFFSET(r,0,width/2)], &videoBuffer[OFFSET(row + r, col/2, SCREENWIDTH/2)], width/2);
}
}
void drawSubImage4(const unsigned short* sourceImage, int sourceRow, int sourceCol,
int row, int col, int height, int width)
{
if(sourceCol%2)
{
sourceCol++;
}
if(col%2)
{
col++;
}
int r;
for(r = 0; r < height; r++)
{
DMANow(3, (unsigned short*)&sourceImage[OFFSET(sourceRow + r, sourceCol/2,width/2)], &videoBuffer[OFFSET(row + r, col/2, SCREENWIDTH/2)], width/2);
}
}
void loadPalette(const unsigned short* palette)
{
DMANow(3, (unsigned short*)palette, PALETTE, 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 = DMA_ON | control;
}
void waitForVblank()
{
while(SCANLINECOUNTER > 160);
while(SCANLINECOUNTER < 160);
}
void flipPage()
{
if(REG_DISPCTL & BACKBUFFER)
{
REG_DISPCTL &= ~BACKBUFFER;
videoBuffer = backBuffer;
}
else
{
REG_DISPCTL |= BACKBUFFER;
videoBuffer = frontBuffer;
}
}