ultimecia

A ps1 emulator in c
Log | Files | Refs

main.c (2806B)


      1 #include <stdlib.h>
      2 #include <unistd.h>
      3 #include <ncurses.h>
      4 #include <string.h>
      5 #include <SDL2/SDL.h>
      6 #include <lua.h>
      7 #include <lualib.h>
      8 #include <lauxlib.h>
      9 
     10 #include "cpu.h"
     11 #include "interconnect.h"
     12 #include "bios.h"
     13 #include "mem.h"
     14 #include "gpu.h"
     15 #include "sr.h"
     16 #include "log.h"
     17 #include "edea.h"
     18 #include "linenoise.h"
     19 
     20 SDL_Event ev;
     21 
     22 Interconnect *inter;
     23 CPU *cpu;
     24 lua_State *L;
     25 
     26 FILE* fp_debug;
     27 u32 running;
     28 u32 paused = 1;
     29 u32 step = 1;
     30 
     31 
     32 char *line;
     33 
     34 void 
     35 completion(const char *buf, linenoiseCompletions *lc) {
     36     if (buf[0] == 'p') {
     37         linenoiseAddCompletion(lc,"print_pc");
     38     }
     39 }
     40 
     41 
     42 static void 
     43 frame(void) 
     44 {
     45 	int c;
     46 
     47 	for (c = 0; c < 1e5; c++) { 
     48         CPU_run_next_instruction(cpu);
     49         //if (paused) goto debugger;
     50     };
     51 
     52     while(SDL_PollEvent(&ev) != 0) {
     53         switch(ev.type) {
     54             case SDL_QUIT:
     55                 SDL_Quit();
     56                 exit(1);
     57             case SDL_KEYDOWN:
     58                 if (ev.key.keysym.sym == SDLK_q) {
     59                     SDL_Quit();
     60                     exit(1);
     61                 }
     62                 //if (ev.key.keysym.sym == SDLK_p) {
     63                 //    paused = 1;
     64                 //    goto debugger;
     65                 //}
     66         }
     67     }
     68 
     69 //debugger:
     70 //    if (paused) {
     71 //        printf("[C] Emulator paused. Enter Lua commands, or call resume() to continue.\n");
     72 //        for (;;) {
     73 //            line = linenoise("> ");
     74 //
     75 //            if (line == NULL) {
     76 //                paused = 0;
     77 //                goto run_loop;
     78 //            }
     79 //
     80 //            switch (line[0]) {
     81 //            case '\0':
     82 //                linenoiseFree(line);
     83 //                continue;
     84 //            default:
     85 //                break;
     86 //            }
     87 //
     88 //            edea(line);
     89 //
     90 //            linenoiseHistoryAdd(line);
     91 //
     92 //            linenoiseFree(line);
     93 //
     94 //            if (!paused) {
     95 //                printf("[C] Resuming emulator...\n");
     96 //                goto run_loop;
     97 //            }
     98 //        }
     99 //    }
    100 }
    101 
    102 void 
    103 init(void)
    104 {
    105     REN *ren;
    106     SDL_Init(SDL_INIT_VIDEO);
    107 
    108     /* Logging */
    109     log_set_quiet(1);
    110     fp_debug = fopen("log_debug.txt", "w+");
    111     log_add_fp(fp_debug, LOG_DEBUG);
    112     linenoiseSetCompletionCallback(completion);
    113 
    114 
    115     inter = new_interconnect();
    116     cpu = new_cpu(inter);
    117     ren = inter->gpu->ren;
    118 
    119     SDL_SetRenderDrawColor(ren->renderer, 0xff, 0xff, 0xff, 0xff);
    120     SDL_RenderClear(ren->renderer);
    121     SDL_RenderPresent(ren->renderer);
    122 
    123 }
    124 
    125 void
    126 kill(void)
    127 {
    128 
    129     free(inter->bios->data);
    130     free(inter->bios);
    131     free(inter->ram->data);
    132     free(inter->ram);
    133     free(inter->dma);
    134     free(inter->gpu);
    135     free(inter->cdrom);
    136     free(inter);
    137     free(cpu);
    138     fclose(fp_debug);
    139 
    140     SDL_Quit();
    141 }
    142 
    143 int
    144 main(void)
    145 {
    146 	init();
    147 
    148     while(1) {
    149 		frame();
    150     }
    151 
    152 	kill();
    153 
    154     return 0;
    155 }