diff --git a/ka/main.k b/ka/main.k index 702b3fa..ef47f56 100644 --- a/ka/main.k +++ b/ka/main.k @@ -5,5 +5,7 @@ ; Main function ; main: + call showoff + hlt ret diff --git a/vm/Makefile b/vm/Makefile index eb0ee6e..d07358a 100644 --- a/vm/Makefile +++ b/vm/Makefile @@ -54,6 +54,6 @@ clean: @rm -f $(OBJDIR)/*/*.d k.exe: in/instrs.lst $(obj) - @gcc -O2 -lSDL2 -Wall $(obj) -o k.exe + @gcc -O2 -lSDL2 -lSDL2_ttf -Wall $(obj) -o k.exe @echo ${CL2}[$@] ${CL}made successfully.${CL3} diff --git a/vm/a.sym b/vm/a.sym index d472d56..31372cb 100644 --- a/vm/a.sym +++ b/vm/a.sym @@ -82,13 +82,13 @@ itoa_test 1052600 devtest 1052764 str_test 1052848 main 1053184 -errno 1053192 -trap0_test.msg 1053200 -printf_test.fmt 1053223 -printf_test.str 1053271 -strchr_test.str 1053279 -itoa_test.buf 1053295 -devtest.buf 1053335 -str_test.msg 1053375 -str_test.buf1 1053391 -str_test.buf2 1053431 +errno 1053208 +trap0_test.msg 1053216 +printf_test.fmt 1053239 +printf_test.str 1053287 +strchr_test.str 1053295 +itoa_test.buf 1053311 +devtest.buf 1053351 +str_test.msg 1053391 +str_test.buf1 1053407 +str_test.buf2 1053447 diff --git a/vm/cn/console.c b/vm/cn/console.c index 85a0816..a1f4cf9 100644 --- a/vm/cn/console.c +++ b/vm/cn/console.c @@ -3,14 +3,166 @@ #include +#define SCREEN_WIDTH (1280) +#define SCREEN_HEIGHT (720) + +SDL_Window *scr_win = NULL; +SDL_Renderer *scr_rend = NULL; + +TTF_Font *scr_font = NULL; +SDL_Color scr_text_color = { 255, 255, 255, 0 }; + +SDL_Rect *scr_rects[SCREEN_WIDTH][SCREEN_HEIGHT] = { 0 }; +SDL_Texture *scr_texts[SCREEN_WIDTH][SCREEN_HEIGHT] = { 0 }; + +//----------------------------------------------------------------------------// + void console_init(ctx_t *ctx) { -/* - if (SDL_Init(SDL_INIT_VIDEO) < 0) + if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) < 0) { - log("Couldn't initialize SDL: %s", SDL_GetError()); + logerr("Couldn't initialize SDL: %s", SDL_GetError()); + die(-1); } - SDL_Window *win = SDL_CreateWindow("Hello World!", 100, 100, 640, 480, SDL_WINDOW_SHOWN);*/ + SDL_CreateWindowAndRenderer(SCREEN_WIDTH, SCREEN_HEIGHT, 0, + &scr_win, &scr_rend); + + if (scr_win == NULL || scr_rend == NULL) + { + logerr("Couldn't create SDL screen window/renderer: %s", SDL_GetError()); + die(-1); + } + + TTF_Init(); + + // XXX I have to find a solution about this... + // including FreeMono.ttf directly in the project + // would force it into GPL... + scr_font = TTF_OpenFont + ("/usr/share/fonts/truetype/freefont/FreeMono.ttf", 15); + + if (scr_font == NULL) + { + logerr("Couldn't open the FreeMono font\n"); + die(-1); + } } +//----------------------------------------------------------------------------// + +void console_exit(ctx_t *ctx) +{ + size_t x, y; + + for (y = 0; y < SCREEN_HEIGHT; y++) + for (x = 0; x < SCREEN_WIDTH; x++) + if (scr_texts[x][y] != NULL) + { + if (scr_rects[x][y]) + free(scr_rects[x][y]); + + SDL_DestroyTexture(scr_texts[x][y]); + } + + if (scr_win) + SDL_DestroyWindow(scr_win); + + TTF_Quit(); + SDL_Quit(); +} + +//----------------------------------------------------------------------------// + +void console_render(ctx_t *ctx) +{ + size_t x, y; + + SDL_SetRenderDrawColor(scr_rend, 20, 20, 20, 0); + SDL_RenderClear(scr_rend); + + for (y = 0; y < SCREEN_HEIGHT; y++) + for (x = 0; x < SCREEN_WIDTH; x++) + if (scr_texts[x][y] != NULL) + SDL_RenderCopy(scr_rend, scr_texts[x][y], + NULL, scr_rects[x][y]); + + SDL_RenderPresent(scr_rend); +} + +//----------------------------------------------------------------------------// + +void console_putat(ctx_t *ctx, char ch, int x, int y) +{ + char str[2] = { ch, 0 }; + SDL_Surface *surf; + + // trace("putat: %c %d %d\n", ch, x, y); + + // todo: to be made line-by-line + + surf = TTF_RenderText_Solid(scr_font, str, scr_text_color); + + if (scr_texts[x][y]) + { + if (scr_rects[x][y]) + { + free(scr_rects[x][y]); + scr_rects[x][y] = NULL; + } + + SDL_DestroyTexture(scr_texts[x][y]); + } + + scr_texts[x][y] = SDL_CreateTextureFromSurface(scr_rend, surf); + scr_rects[x][y] = malloc(sizeof(SDL_Rect)); + + if (scr_rects[x][y] == NULL) + { + logerr("console_putat: not enough memory\n"); + SDL_FreeSurface(surf); + die(-1); + } + + scr_rects[x][y]->x = x * surf->w; + scr_rects[x][y]->y = y * surf->h; + scr_rects[x][y]->w = surf->w; + scr_rects[x][y]->h = surf->h; + + SDL_FreeSurface(surf); + + console_render(ctx); +} + +#define CONSOLE_WIDTH 80 +#define CONSOLE_HEIGHT 25 + +int csn_x = 0; +int csn_y = 0; + +void console_putc(ctx_t *ctx, char ch) +{ + if (ch < ' ' && ch != '\n') + ch = 127; + + if (ch == '\n') + csn_x = CONSOLE_WIDTH; + + else + { + console_putat(ctx, ch, csn_x, csn_y); + csn_x++; + } + + if (csn_x == CONSOLE_WIDTH) + { + csn_x = 0; + csn_y++; + } + + if (csn_y == CONSOLE_HEIGHT) + csn_y = 0; +} + +//----------------------------------------------------------------------------// + diff --git a/vm/cn/console.h b/vm/cn/console.h index 5961475..ddd1aa4 100644 --- a/vm/cn/console.h +++ b/vm/cn/console.h @@ -2,9 +2,13 @@ // See the LICENSE file in the project root for more information. #include - -//#include - +#include +#include void console_init(ctx_t *ctx); +void console_exit(ctx_t *ctx); + +void console_render(ctx_t *ctx); +void console_putat(ctx_t *ctx, char ch, int x, int y); +void console_putc(ctx_t *ctx, char ch); diff --git a/vm/in/INSTRS b/vm/in/INSTRS index 3ff5402..8e849b6 100644 --- a/vm/in/INSTRS +++ b/vm/in/INSTRS @@ -473,6 +473,8 @@ trap rim # iret +hlt + #---------------------------------------------------------------------------# # Misc. instructions # #---------------------------------------------------------------------------# diff --git a/vm/in/instrs.c b/vm/in/instrs.c index dd3f39a..55947a7 100644 --- a/vm/in/instrs.c +++ b/vm/in/instrs.c @@ -4,6 +4,8 @@ #include #include +#include + #define _NEED_ARCH_I #include @@ -50,10 +52,10 @@ IMPL_OUT; IMPL_START_1(prn) { - if (p1->mlen > 4) { + if (p1->mlen > 1) { trace("prn warning: large access size\n"); } - putchar((int)v1); + console_putc(ctx, (char)v1); } IMPL_END; diff --git a/vm/in/super.c b/vm/in/super.c index d388606..ccc1115 100644 --- a/vm/in/super.c +++ b/vm/in/super.c @@ -3,9 +3,11 @@ #include -// -// Supervisor only instructions -// +IMPL_START_0(hlt) +{ + while (1) getchar(); +} +IMPL_END; IMPL_START_0(cli) { diff --git a/vm/pc/arch.h b/vm/pc/arch.h index 80d253b..40f89ef 100644 --- a/vm/pc/arch.h +++ b/vm/pc/arch.h @@ -15,7 +15,6 @@ #include #undef dev_t -#define packed __attribute__ ((__packed__)) #define static_assert _Static_assert #define alignof _Alignof diff --git a/vm/pc/die.c b/vm/pc/die.c index d6ce469..bab8bfb 100644 --- a/vm/pc/die.c +++ b/vm/pc/die.c @@ -2,6 +2,7 @@ // See the LICENSE file in the project root for more information. #include +#include void die(int code) { @@ -12,6 +13,8 @@ void die(int code) if (main_ctx.mp) free(main_ctx.mp); + console_exit(&main_ctx); + // // Shut down devices // diff --git a/vm/pc/main.c b/vm/pc/main.c index accb8aa..3605602 100644 --- a/vm/pc/main.c +++ b/vm/pc/main.c @@ -34,7 +34,7 @@ ctx_t main_ctx; void sigcommon(void) { - die(-13); + _except(&main_ctx, 1023, "SIGNAL'ed"); } void sigint(int _) @@ -53,12 +53,14 @@ void main_loop(void) // // Start decoding // - while (1) { + while (!dying) { decode(&main_ctx); if (main_ctx.step) getchar(); } + + die(0); } int main(int argc, char **argv)