-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyLib.c
63 lines (52 loc) · 1.84 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
#include "myLib.h"
unsigned short * videoBuffer = (unsigned short *)0x6000000;
unsigned short *frontBuffer = (u16 *)0x6000000;
unsigned short *backBuffer = (u16 *)0x600A000;
DMA *dma = (DMA *)0x40000B0;
// Functions
void DMANow(int channel, volatile const void* source, volatile const void* destination, unsigned int control) {
dma[channel].src = source;
dma[channel].dst = destination;
dma[channel].cnt = DMA_ON | control;
}
//allows use of color palette
void loadPalette(volatile const unsigned short* palette) {
DMANow(3, palette, PALETTE, 256);
}
//draws an image in mode 4
void drawImage4(volatile const unsigned short* image, int row, int col, int width, int height) {
//If this image is being drawn on an odd column, just add 1 to the column and then draw it
if (col & 1) { //if col odd
col = col + 1; // add to col if draw on odd col
}
for (int r = 0; r < height; r++) {
DMANow(3, (unsigned short*)&image[OFFSET(r,0, width)/2], &videoBuffer[OFFSET(row+r, col, 240)/2], (width/2));
}
}
//draws entire background image with dimensions that fit screen
void drawBackgroundImage4(volatile const unsigned short* image) {
DMANow(3, image, videoBuffer, (240*160)/2);
}
// draw rectangle
void drawRect(int row, int col, int height, int width, unsigned short color) {
volatile unsigned short c = color;
for (int r = 0; r < height; r++) {
DMANow(3, &c, &videoBuffer[OFFSET(row + r, col, 240)], width);
}
}
// equivalent to a delay
void waitForVblank() {
while(SCANLINECOUNTER > 160);
while(SCANLINECOUNTER < 160);
}
//flips between videobuffers
//one buffer is shown while other is drawn on
void flipPage() {
if(REG_DISPCTL & BUFFER2) {
REG_DISPCTL &= ~BUFFER2;
videoBuffer = backBuffer;
} else {
REG_DISPCTL |= BUFFER2;
videoBuffer = frontBuffer;
}
}