1
0
mirror of https://github.com/sehugg/8bitworkshop.git synced 2024-12-11 02:49:22 +00:00
8bitworkshop/presets/coleco/hello.c

102 lines
2.1 KiB
C
Raw Normal View History

2017-05-02 13:09:53 +00:00
2019-12-03 04:16:11 +00:00
/*
This is a demonstration of integrating the C
standard I/O (stdio) functions with the display,
so that standard functions like printf() can be used.
*/
2017-05-02 13:09:53 +00:00
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <cv.h>
#include <cvu.h>
2017-05-02 13:09:53 +00:00
2019-12-03 04:16:11 +00:00
// overrides value in common.h
2017-05-02 13:09:53 +00:00
#define COLS 40
2019-08-19 14:24:22 +00:00
#include "common.h"
//#link "common.c"
2020-07-01 02:14:33 +00:00
//#link "fonts.s"
2017-05-02 13:09:53 +00:00
void setup_40_column_font() {
2019-12-03 04:16:11 +00:00
cv_set_screen_mode(CV_SCREENMODE_TEXT);
2017-05-02 13:09:53 +00:00
cv_set_image_table(IMAGE);
2019-08-19 14:24:22 +00:00
cv_set_character_pattern_t(PATTERN);
2019-12-03 04:16:11 +00:00
copy_default_character_set();
2017-05-02 13:09:53 +00:00
}
2019-12-03 04:16:11 +00:00
char cursor_x; // current cursor column
char cursor_y; // current cursor row
2017-05-02 13:09:53 +00:00
2019-12-03 04:16:11 +00:00
// clear the screen to ' ' (blank spaces)
2017-05-02 13:09:53 +00:00
void clrscr() {
cvu_vmemset(IMAGE, ' ', COLS*ROWS);
}
void setup_stdio() {
cursor_x = 0;
cursor_y = 0;
clrscr();
}
2019-12-03 04:16:11 +00:00
// scroll the screen upward, by copying each row
// of VRAM to its previous row.
2017-05-02 13:09:53 +00:00
void scrollup() {
char buf[COLS];
char y;
for (y=0; y<ROWS-1; y++) {
cvu_vmemtomemcpy(buf, IMAGE + COLS*y + COLS, COLS);
cvu_memtovmemcpy(IMAGE + COLS*y, buf, COLS);
}
cvu_vmemset(IMAGE + COLS*(ROWS-1), ' ', COLS);
}
2019-12-03 04:16:11 +00:00
// move cursor to next line, scrolling when it hits the bottom.
2017-05-02 13:09:53 +00:00
void newline() {
if (cursor_y >= ROWS-1) {
scrollup();
} else {
cursor_y++;
}
}
2019-12-03 04:16:11 +00:00
// write a character to the screen.
2017-05-02 13:09:53 +00:00
int putchar(int ch) {
switch (ch) {
case '\n':
2019-12-03 04:16:11 +00:00
newline(); // move cursor to next line
2017-05-02 13:09:53 +00:00
case '\r':
2019-12-03 04:16:11 +00:00
cursor_x = 0; // move cursor to start of line
2017-05-02 13:09:53 +00:00
return 0;
}
2019-12-03 04:16:11 +00:00
// output character to VRAM at cursor position
2017-05-02 13:09:53 +00:00
cvu_voutb(ch, IMAGE + COLS*cursor_y + cursor_x);
2019-12-03 04:16:11 +00:00
// move cursor to right, going to next line if neccessary
2017-05-02 13:09:53 +00:00
cursor_x++;
if (cursor_x >= COLS) {
newline();
cursor_x = 0;
}
}
2019-12-03 04:16:11 +00:00
#ifdef __MAIN__
// test program
2017-05-02 13:09:53 +00:00
void main() {
unsigned char byteval = 123;
signed char charval = 123;
short shortval = 12345;
setup_40_column_font();
setup_stdio();
cv_set_colors(CV_COLOR_LIGHT_GREEN, CV_COLOR_BLACK);
cv_set_screen_active(true);
printf("HELLO WORLD!\n");
while (1) {
printf("char %d byte %u sh %d\n",
charval++, byteval++, shortval++);
}
}
2019-12-03 04:16:11 +00:00
#endif