Skip to content

Commit 65fcf12

Browse files
committed
Completed SDL template and added to global makefile
1 parent 5b3cd5d commit 65fcf12

File tree

3 files changed

+275
-31
lines changed

3 files changed

+275
-31
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ all:
1111
make -C SDL2_image
1212
make -C SDL2_mixer
1313
make -C template
14+
make -C template_sdl
1415
make -C abbaye
1516

1617
clean:
@@ -25,4 +26,5 @@ clean:
2526
make -C SDL2_image -k clean
2627
make -C SDL2_mixer -k clean
2728
make -C template -k clean
29+
make -C template_sdl -k clean
2830
make -C abbaye -k clean

template_sdl/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ OBJS = \
2424
../kernel/start.o \
2525
../kernel/syscalls.o \
2626
../kernel/platform.o \
27+
../kernel/wiring.o \
2728
main.o
2829

2930
-include $(DEPDIR)/*.Po

template_sdl/main.c

Lines changed: 272 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
#include <SDL.h>
66

7+
#include "../kernel/platform.h"
8+
#include "../kernel/wiring.h"
9+
710
#define FONT vgafont8
811
#define BIT_SHIFT (7 - s_bit_no)
912

@@ -141,6 +144,26 @@ static unsigned char vgafont8[128 * 8]= {
141144
0x00, 0x10, 0x38, 0x6c, 0xc6, 0xc6, 0xfe, 0x00,
142145
};
143146

147+
static unsigned char keyNormal_it[] = {
148+
0x0, 0x0, 0x0, 0x0, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
149+
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '1', '2',
150+
'3', '4', '5', '6', '7', '8', '9', '0', '\r', 0x0, '\b', '\t', ' ', '\'', 0x0, 0x0,
151+
'+', '<', 0x0, 0x0, 0x0, '\\', ',', '.', '-', 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
152+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
153+
0x0, 0x0, 0x0, 0x0, '/', '*', '-', '+', '\r', '1', '2', '3', '4', '5', '6', '7',
154+
'8', '9', '0', '.', '<', 0x0, 0x0, '='
155+
};
156+
157+
static unsigned char keyShift_it[] = {
158+
0x0, 0x0, 0x0, 0x0, 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L',
159+
'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '!', '"',
160+
0x0, '$', '%', '&', '/', '(', ')', '=', '\r', 0x0, '\b', '\t', ' ', '?', '^', 0x0,
161+
'*', '>', 0x0, 0x0, 0x0, '|', ';', ':', '_', 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
162+
0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
163+
0x0, 0x0, 0x0, 0x0, '/', '*', '-', '+', '\r', '1', '2', '3', '4', '5', '6', '7',
164+
'8', '9', '0', '.', '>', 0x0, 0x0, '='
165+
};
166+
144167
#if defined(__cplusplus)
145168
extern "C" {
146169
#endif
@@ -153,63 +176,281 @@ __attribute__ ((interrupt ("IRQ"))) void interrupt_irq() {
153176
}
154177
#endif
155178

179+
struct _screen {
180+
unsigned char c;
181+
struct {
182+
unsigned char r;
183+
unsigned char g;
184+
unsigned char b;
185+
} fore;
186+
struct {
187+
unsigned char r;
188+
unsigned char g;
189+
unsigned char b;
190+
} back;
191+
};
192+
193+
#define txt_width 40
194+
#define txt_height 25
195+
156196
static int cur_x;
157197
static int cur_y;
198+
static int cursor_visible;
199+
static struct _screen screen[txt_width * txt_height];
200+
static SDL_Rect crect;
158201

159-
void SDL_DrawChar(SDL_Renderer *renderer, int c) {
160-
int s_offset = (int) c * CHAR_W * CHAR_H;
161-
int x = cur_x * CHAR_W;
162-
int y = cur_y * CHAR_H;
163-
164-
for (int c_y = 0; c_y < CHAR_H; c_y++) {
165-
for (int c_x = 0; c_x < CHAR_W; c_x++) {
166-
int s_byte_no = s_offset / 8;
167-
int s_bit_no = s_offset % 8;
168-
169-
unsigned char s_byte = FONT[s_byte_no];
170-
if ((s_byte >> BIT_SHIFT) & 0x1)
171-
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
172-
else
173-
SDL_SetRenderDrawColor(renderer, 98, 0, 32, 255);
174-
SDL_RenderDrawPoint(renderer, x + c_x, y + c_y);
175-
s_offset++;
202+
void SDL_DrawString(const char *s) {
203+
char c;
204+
205+
while (*s) {
206+
c = *s++;
207+
if (c == '\r') {
208+
cur_x = 0;
209+
}
210+
else if (c == '\n') {
211+
cur_y++;
212+
if (cur_y >= txt_height) {
213+
memcpy(&screen[0], &screen[txt_width], sizeof(screen) - sizeof(struct _screen) * txt_width);
214+
for (int i = txt_width * (txt_height - 1); i < txt_width * txt_height; i++) {
215+
screen[i].c = ' ';
216+
screen[i].fore.r = 255;
217+
screen[i].fore.g = 255;
218+
screen[i].fore.b = 255;
219+
screen[i].back.r = 98;
220+
screen[i].back.g = 0;
221+
screen[i].back.b = 32;
222+
}
223+
cur_y--;
224+
}
225+
}
226+
else {
227+
screen[cur_y * txt_width + cur_x].c = c;
228+
cur_x++;
229+
if (cur_x >= txt_width) {
230+
cur_x = 0;
231+
cur_y++;
232+
if (cur_y >= txt_height) {
233+
memcpy(&screen[0], &screen[txt_width], sizeof(screen) - sizeof(struct _screen) * txt_width);
234+
for (int i = txt_width * (txt_height - 1); i < txt_width * txt_height; i++) {
235+
screen[i].c = ' ';
236+
screen[i].fore.r = 255;
237+
screen[i].fore.g = 255;
238+
screen[i].fore.b = 255;
239+
screen[i].back.r = 98;
240+
screen[i].back.g = 0;
241+
screen[i].back.b = 32;
242+
}
243+
cur_y--;
244+
}
245+
}
176246
}
177247
}
248+
cursor_visible = 1;
249+
}
178250

179-
cur_x++;
251+
void SDL_DrawStringAt(int y, int x, const char *s) {
252+
cur_x = x % txt_width;
253+
cur_y = y % txt_height;
254+
SDL_DrawString(s);
180255
}
181256

182-
void SDL_DrawString(SDL_Renderer *renderer, const char *s) {
183-
while (*s) {
184-
SDL_DrawChar(renderer, *s++);
257+
void SDL_DrawChar(char c) {
258+
if (c == '\r') {
259+
cur_x = 0;
260+
}
261+
else if (c == '\n') {
262+
cur_y++;
263+
if (cur_y >= txt_height) {
264+
memcpy(&screen[0], &screen[txt_width], sizeof(screen) - sizeof(struct _screen) * txt_width);
265+
for (int i = txt_width * (txt_height - 1); i < txt_width * txt_height; i++) {
266+
screen[i].c = ' ';
267+
screen[i].fore.r = 255;
268+
screen[i].fore.g = 255;
269+
screen[i].fore.b = 255;
270+
screen[i].back.r = 98;
271+
screen[i].back.g = 0;
272+
screen[i].back.b = 32;
273+
}
274+
cur_y--;
275+
}
276+
}
277+
else {
278+
screen[cur_y * txt_width + cur_x].c = c;
279+
cur_x++;
280+
if (cur_x >= txt_width) {
281+
cur_x = 0;
282+
cur_y++;
283+
if (cur_y >= txt_height) {
284+
memcpy(&screen[0], &screen[txt_width], sizeof(screen) - sizeof(struct _screen) * txt_width);
285+
for (int i = txt_width * (txt_height - 1); i < txt_width * txt_height; i++) {
286+
screen[i].c = ' ';
287+
screen[i].fore.r = 255;
288+
screen[i].fore.g = 255;
289+
screen[i].fore.b = 255;
290+
screen[i].back.r = 98;
291+
screen[i].back.g = 0;
292+
screen[i].back.b = 32;
293+
}
294+
cur_y--;
295+
}
296+
}
297+
}
298+
cursor_visible = 1;
299+
}
300+
301+
void SDL_DrawCharAt(int y, int x, char c) {
302+
cur_x = x % txt_width;
303+
cur_y = y % txt_height;
304+
SDL_DrawChar(c);
305+
}
306+
307+
void SDL_InitConsole(int w, int h) {
308+
crect.x = (w - txt_width * CHAR_W) / 2;
309+
crect.y = (h - txt_height * CHAR_H) / 2;
310+
crect.w = txt_width * CHAR_W;
311+
crect.h = txt_height * CHAR_H;
312+
313+
for (int i = 0; i < txt_width * txt_height; i++) {
314+
screen[i].c = ' ';
315+
screen[i].fore.r = 255;
316+
screen[i].fore.g = 255;
317+
screen[i].fore.b = 255;
318+
screen[i].back.r = 98;
319+
screen[i].back.g = 0;
320+
screen[i].back.b = 32;
321+
}
322+
323+
cur_x = 0;
324+
cur_y = 0;
325+
}
326+
327+
void SDL_RenderConsole(SDL_Renderer *renderer) {
328+
int x, y, c_x;
329+
int index = 0;
330+
331+
for (y = crect.y; index < txt_width * txt_height; y += CHAR_H) {
332+
for (c_x = 0, x = crect.x; c_x < txt_width && index < txt_width * txt_height; index++, c_x++, x += CHAR_W) {
333+
int s_offset = (int) screen[index].c * CHAR_W * CHAR_H;
334+
for (int f_y = 0; f_y < CHAR_H; f_y++) {
335+
for (int f_x = 0; f_x < CHAR_W; f_x++) {
336+
int s_byte_no = s_offset / 8;
337+
int s_bit_no = s_offset % 8;
338+
339+
unsigned char s_byte = FONT[s_byte_no];
340+
if ((s_byte >> BIT_SHIFT) & 0x1)
341+
SDL_SetRenderDrawColor(renderer, screen[index].fore.r, screen[index].fore.g, screen[index].fore.b, 255);
342+
else
343+
SDL_SetRenderDrawColor(renderer, screen[index].back.r, screen[index].back.g, screen[index].back.b, 255);
344+
SDL_RenderDrawPoint(renderer, x + f_x, y + f_y);
345+
s_offset++;
346+
}
347+
}
348+
}
349+
}
350+
351+
if (cursor_visible) {
352+
SDL_Rect rect;
353+
rect.x = crect.x + cur_x * CHAR_W;
354+
rect.y = crect.y + cur_y * CHAR_H;
355+
rect.w = CHAR_W;
356+
rect.h = CHAR_H;
357+
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
358+
SDL_RenderFillRect(renderer, &rect);
185359
}
186360
}
187361

188362
void main() {
189363
int w, h;
190364
SDL_Window *screen;
191365
SDL_Renderer *renderer;
366+
SDL_Event event;
367+
struct timer_wait tw;
368+
int led_status = LOW;
369+
370+
SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_EVENTS);
192371

193372
// Default screen resolution (set in config.txt or auto-detected)
194373
//SDL_CreateWindowAndRenderer(0, 0, SDL_WINDOW_FULLSCREEN_DESKTOP, &screen, &renderer);
195374

196375
// Sets a specific screen resolution
197-
SDL_CreateWindowAndRenderer(320, 200, SDL_WINDOW_FULLSCREEN, &screen, &renderer);
376+
SDL_CreateWindowAndRenderer(32 + 320 + 32, 32 + 200 + 32, SDL_WINDOW_FULLSCREEN, &screen, &renderer);
198377

199378
SDL_GetWindowSize(screen, &w, &h);
379+
SDL_InitConsole(w, h);
200380

201-
SDL_SetRenderDrawColor(renderer, 98, 0, 32, 255);
202-
SDL_RenderClear(renderer);
381+
SDL_DrawStringAt(1, (txt_width - 22) / 2, "**** RASPBERRY-PI ****");
382+
SDL_DrawStringAt(3, (txt_width - 30) / 2, "BARE-METAL SDL SYSTEM TEMPLATE\r\n");
203383

204-
cur_y = 1;
205-
cur_x = ((w / CHAR_W) - 22) / 2;
206-
SDL_DrawString(renderer, "**** RASPBERRY-PI ****");
207-
cur_y = 3;
208-
cur_x = ((w / CHAR_W) - 30) / 2;
209-
SDL_DrawString(renderer, "BARE-METAL SDL SYSTEM TEMPLATE");
384+
pinMode(16, OUTPUT);
385+
register_timer(&tw, 250000);
210386

211-
SDL_RenderPresent(renderer);
387+
SDL_DrawString("\r\nREADY\r\n");
212388

213389
while(1) {
390+
while (SDL_PollEvent(&event)) {
391+
if (event.type == SDL_KEYDOWN) {
392+
switch(event.key.keysym.scancode) {
393+
case SDL_SCANCODE_UP:
394+
if (cur_y > 0)
395+
cur_y--;
396+
break;
397+
case SDL_SCANCODE_DOWN:
398+
if (cur_y < txt_height - 1)
399+
cur_y++;
400+
break;
401+
case SDL_SCANCODE_LEFT:
402+
if (cur_x > 0)
403+
cur_x--;
404+
else if (cur_y > 0) {
405+
cur_y--;
406+
cur_x = txt_width - 1;
407+
}
408+
break;
409+
case SDL_SCANCODE_RIGHT:
410+
if (cur_x < txt_width - 1)
411+
cur_x++;
412+
else if (cur_y < txt_height - 1) {
413+
cur_y++;
414+
cur_x = 0;
415+
}
416+
break;
417+
case SDL_SCANCODE_HOME:
418+
cur_x = 0;
419+
break;
420+
case SDL_SCANCODE_END:
421+
cur_x = txt_width - 1;
422+
break;
423+
case SDL_SCANCODE_RETURN:
424+
SDL_DrawString("\r\n");
425+
break;
426+
default: {
427+
SDL_Keymod mod = SDL_GetModState();
428+
if ((mod & (KMOD_LSHIFT | KMOD_RSHIFT)) != 0) {
429+
char c = keyShift_it[event.key.keysym.scancode];
430+
if (c >= ' ')
431+
SDL_DrawChar(c);
432+
}
433+
else {
434+
char c = keyNormal_it[event.key.keysym.scancode];
435+
if (c >= ' ')
436+
SDL_DrawChar(c);
437+
}
438+
}
439+
}
440+
}
441+
}
442+
443+
if (compare_timer(&tw)) {
444+
led_status = led_status == LOW ? HIGH : LOW;
445+
digitalWrite(16, led_status);
446+
cursor_visible = cursor_visible ? 0 : 1;
447+
}
448+
449+
SDL_SetRenderDrawColor(renderer, 213, 41, 82, 255);
450+
SDL_RenderClear(renderer);
451+
452+
SDL_RenderConsole(renderer);
453+
454+
SDL_RenderPresent(renderer);
214455
}
215456
}

0 commit comments

Comments
 (0)