|
| 1 | +/** |
| 2 | + * Simulator for KOS using sdl |
| 3 | + */ |
| 4 | + |
| 5 | +#include <stdio.h> |
| 6 | +#include <libgen.h> |
| 7 | + |
| 8 | +#include <pthread.h> |
| 9 | + |
| 10 | +#include <SDL.h> |
| 11 | + |
| 12 | +#include "sim_avr.h" |
| 13 | +#include "sim_elf.h" |
| 14 | +#include "sim_gdb.h" |
| 15 | + |
| 16 | +#define GDB_PORT 1234 |
| 17 | +#define SCREEN_WIDTH 640 |
| 18 | +#define SCREEN_HEIGHT 480 |
| 19 | + |
| 20 | +static avr_t *avr; |
| 21 | + |
| 22 | +static void *avr_run_thread(void * ig) |
| 23 | +{ |
| 24 | + while (1) |
| 25 | + { |
| 26 | + avr_run(avr); |
| 27 | + } |
| 28 | + |
| 29 | + return NULL; |
| 30 | +} |
| 31 | + |
| 32 | +static SDL_Window *init_window(void) |
| 33 | +{ |
| 34 | + SDL_Window *win = 0; |
| 35 | + |
| 36 | + if (SDL_Init(SDL_INIT_VIDEO) < 0) |
| 37 | + { |
| 38 | + fprintf(stderr, "Unable to initialize SDL: %s", SDL_GetError()); |
| 39 | + exit(1); |
| 40 | + } |
| 41 | + |
| 42 | + win = SDL_CreateWindow("KOS Simulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); |
| 43 | + if (!win) |
| 44 | + { |
| 45 | + fprintf(stderr, "Unable to create SDL Window: %s", SDL_GetError()); |
| 46 | + exit(1); |
| 47 | + } |
| 48 | + |
| 49 | + return win; |
| 50 | +} |
| 51 | + |
| 52 | +static char handle_event(SDL_Event *event) |
| 53 | +{ |
| 54 | + switch (event->type) |
| 55 | + { |
| 56 | + case SDL_QUIT: |
| 57 | + return 0; |
| 58 | + } |
| 59 | + |
| 60 | + return 1; //continue |
| 61 | +} |
| 62 | + |
| 63 | +int main(int argc, char** argv) |
| 64 | +{ |
| 65 | + elf_firmware_t f; |
| 66 | + SDL_Window *win; |
| 67 | + const char *fname = "kos.elf"; |
| 68 | + SDL_Event event; |
| 69 | + |
| 70 | + if (elf_read_firmware(fname, &f) < 0) |
| 71 | + { |
| 72 | + exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + avr = avr_make_mcu_by_name(f.mmcu); |
| 76 | + avr_init(avr); |
| 77 | + avr_load_firmware(avr, &f); |
| 78 | + |
| 79 | + if (SDL_Init(SDL_INIT_VIDEO) < 0) |
| 80 | + { |
| 81 | + fprintf(stderr, "Unable to initialize SDL: %s", SDL_GetError()); |
| 82 | + exit(1); |
| 83 | + } |
| 84 | + |
| 85 | + win = SDL_CreateWindow("KOS Simulator", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); |
| 86 | + if (!win) |
| 87 | + { |
| 88 | + fprintf(stderr, "Unable to create SDL Window: %s", SDL_GetError()); |
| 89 | + exit(1); |
| 90 | + } |
| 91 | + |
| 92 | + while (SDL_WaitEvent(&event) >= 0) |
| 93 | + { |
| 94 | + if (!handle_event(&event)) |
| 95 | + break; |
| 96 | + } |
| 97 | + |
| 98 | + SDL_DestroyWindow(win); |
| 99 | + |
| 100 | + SDL_Quit(); |
| 101 | + |
| 102 | + return 0; |
| 103 | +} |
0 commit comments