forked from Spritetm/hadbadge2019_fpgasoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgraphics.c
91 lines (77 loc) · 3.85 KB
/
graphics.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
#include "graphics.h"
extern volatile uint32_t MISC[];
#define MISC_REG(i) MISC[(i)/4]
extern volatile uint32_t GFXREG[];
#define GFX_REG(i) GFXREG[(i)/4]
extern uint32_t GFXPAL[];
extern uint32_t GFXTILES[];
extern uint32_t GFXTILEMAPA[];
extern uint32_t GFXTILEMAPB[];
//The bgnd.png image got linked into the binary of this app, and these two chars are the first
//and one past the last byte of it.
extern char _binary_bgnd_png_start;
extern char _binary_bgnd_png_end;
//Pointer to the framebuffer memory.
uint8_t *fbmem;
#define FB_WIDTH 512
#define FB_HEIGHT 320
void button_wait(){
//Wait until all buttons are released
while (MISC_REG(MISC_BTN_REG)) ;
//Wait until button A is pressed
while ((MISC_REG(MISC_BTN_REG) & BUTTON_A)==0) ;
}
// End graphics stuff.
void do_graphics(void){
//We're running in app context. We have full control over the badge and can do with the hardware what we want. As
//soon as main() returns, however, we will go back to the IPL.
//Blank out fb while we're loading stuff by disabling all layers. This just shows the background color.
// Blue, Green, Red
GFX_REG(GFX_BGNDCOL_REG)=0x101010; //a soft gray
GFX_REG(GFX_LAYEREN_REG)=0; //disable all gfx layers
//First, allocate some memory for the background framebuffer. We're gonna dump a fancy image into it. The image is
//going to be 8-bit, so we allocate 1 byte per pixel.
fbmem=calloc(FB_WIDTH,FB_HEIGHT);
//Tell the GFX hardware to use this, and its pitch. We also tell the GFX hardware to use palette entries starting
//from 0 for the frame buffer.
GFX_REG(GFX_FBPITCH_REG)=(0<<GFX_FBPITCH_PAL_OFF)|(FB_WIDTH<<GFX_FBPITCH_PITCH_OFF);
//Set up the framebuffer address
GFX_REG(GFX_FBADDR_REG)=((uint32_t)fbmem);
//Now, use a library function to load the image into the framebuffer memory. This function will also set up the palette entries,
//we tell it to start writing from entry 0.
/* int png_size=(&_binary_bgnd_png_end-&_binary_bgnd_png_start); */
/* int i=gfx_load_fb_mem(fbmem, &GFXPAL[0], 8, FB_WIDTH, &_binary_bgnd_png_start, png_size); */
/* printf("gfx_load_mem: %d\n", i); */
//Flush the memory region to psram so the GFX hw can stream it from there.
cache_flush(fbmem, fbmem+FB_WIDTH*FB_HEIGHT);
//The IPL leaves us with a tileset that has tile 0 to 127 map to ASCII characters, so we do not need to
//load anything specific for this. In order to get some text out, we can use the /dev/console device
//that will use these tiles to put text in a tilemap. It uses escape codes to do so, see
//ipl/gloss/console_out.c for more info.
FILE *f;
f=fopen("/dev/console", "w");
setvbuf(f, NULL, _IOLBF, 1024); //make console line buffered
//Note that without the setvbuf command, no characters would be printed until 1024 characters are
//buffered.
fprintf(f, "\033C"); //clear the console. Note '\033' is the escape character.
fprintf(f, "\0335X"); //set Xpos to 5
fprintf(f, "\0338Y"); //set Ypos to 8
fprintf(f, "Synth Demo\n"); // Print a nice greeting.
fprintf(f, "\0335X"); //set Xpos to 5
fprintf(f, "\0339Y"); //set Ypos to 8
fprintf(f, "To see what it's doing,\n"); // Print a nice greeting.
fprintf(f, "\0335X"); //set Xpos to 5
fprintf(f, "\03310Y"); //set Ypos to 8
fprintf(f, "read the code!\n"); // Print a nice greeting.
fprintf(f, "\0335X"); //set Xpos to 5
fprintf(f, "\03312Y"); //set Ypos to 8
fprintf(f, "No audio?\n"); // Print a nice greeting.
fprintf(f, "\0335X"); //set Xpos to 5
fprintf(f, "\03313Y"); //set Ypos to 8
fprintf(f, "Solder a speaker!\n"); // Print a nice greeting.
//Note that without the newline at the end, all printf's would stay in the buffer.
//The user can still see nothing of this graphics goodness, so let's re-enable the framebuffer and
//tile layer A (the default layer for the console). Also indicate the framebuffer we have is
//8-bit.
GFX_REG(GFX_LAYEREN_REG)=GFX_LAYEREN_FB_8BIT|GFX_LAYEREN_FB|GFX_LAYEREN_TILEA;
}