]> git.sesse.net Git - pistorm/blobdiff - emulator.c
More tiny optimizations, some Z2/Z3 fixes
[pistorm] / emulator.c
index 4d7ecebb93bfe8195b2e29f16dd6dac9e3a78e07..a019512e2ff731fe27a3dfe6c2e7ee2561207614 100644 (file)
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
-#include <termios.h>
-#include <linux/input.h>
 #include "Gayle.h"
 #include "ide.h"
 #include "m68k.h"
 #include "main.h"
-#include "config_file/config_file.h"
+#include "platforms/platforms.h"
+#include "input/input.h"
 
-int kbhit()
-{
-  struct termios term;
-  tcgetattr(0, &term);
-
-  struct termios term2 = term;
-  term2.c_lflag &= ~ICANON;
-  tcsetattr(0, TCSANOW, &term2);
-
-  int byteswaiting;
-  ioctl(0, FIONREAD, &byteswaiting);
-
-  tcsetattr(0, TCSANOW, &term);
-
-  return byteswaiting > 0;
-}
+#include "platforms/amiga/amiga-registers.h"
 
 //#define BCM2708_PERI_BASE        0x20000000  //pi0-1
 //#define BCM2708_PERI_BASE    0xFE000000     //pi4
@@ -99,6 +83,7 @@ int kbhit()
 
 int kb_hook_enabled = 0;
 int mouse_hook_enabled = 0;
+int cpu_emulation_running = 1;
 
 char mouse_dx = 0, mouse_dy = 0;
 char mouse_buttons = 0;
@@ -106,7 +91,7 @@ char mouse_buttons = 0;
 #define KICKBASE 0xF80000
 #define KICKSIZE 0x7FFFF
 
-int mem_fd, mouse_fd = -1;
+int mem_fd, mouse_fd = -1, keyboard_fd = -1;
 int mem_fd_gpclk;
 int gayle_emulation_enabled = 1;
 void *gpio_map;
@@ -116,6 +101,7 @@ void *gpclk_map;
 unsigned int cpu_type = M68K_CPU_TYPE_68000;
 unsigned int loop_cycles = 300;
 struct emulator_config *cfg = NULL;
+char keyboard_file[256] = "/dev/input/event0";
 
 // I/O access
 volatile unsigned int *gpio;
@@ -166,15 +152,20 @@ volatile uint32_t srdata2_old;
 //unsigned char g_kick[524288];
 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
 unsigned char toggle;
-static volatile unsigned char ovl;
+int ovl;
 static volatile unsigned char maprom;
 
 void sigint_handler(int sig_num) {
-  printf("\n Exit Ctrl+C %d\n", sig_num);
+  //if (sig_num) { }
+  //cpu_emulation_running = 0;
+
+  //return;
+  printf("Received sigint %d, exiting.\n", sig_num);
   if (mouse_fd != -1)
     close(mouse_fd);
   if (mem_fd)
     close(mem_fd);
+
   exit(0);
 }
 
@@ -220,6 +211,14 @@ int main(int argc, char *argv[]) {
         cfg = load_config_file(argv[g]);
       }
     }
+    else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
+      if (g + 1 >= argc) {
+        printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
+      } else {
+        g++;
+        strcpy(keyboard_file, argv[g]);
+      }
+    }
   }
 
   if (!cfg) {
@@ -235,9 +234,14 @@ int main(int argc, char *argv[]) {
       memset(cfg, 0x00, sizeof(struct emulator_config));
     }
   }
-  else {
+
+  if (cfg) {
     if (cfg->cpu_type) cpu_type = cfg->cpu_type;
     if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
+
+    if (!cfg->platform)
+      cfg->platform = make_platform_config("none", "generic");
+    cfg->platform->platform_initial_setup(cfg);
   }
 
   if (cfg->mouse_enabled) {
@@ -248,6 +252,11 @@ int main(int argc, char *argv[]) {
     }
   }
 
+  keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
+  if (keyboard_fd == -1) {
+    printf("Failed to open keyboard event source.\n");
+  }
+
   sched_setscheduler(0, SCHED_FIFO, &priority);
   mlockall(MCL_CURRENT);  // lock in memory to keep us from paging out
 
@@ -331,27 +340,6 @@ int main(int argc, char *argv[]) {
   write_reg(0x00);
   usleep(100);
 
-  // load kick.rom if present
-  /*maprom = 1;
-  int fd = 0;
-  fd = open("kick.rom", O_RDONLY);
-  if (fd < 1) {
-    printf("Failed loading kick.rom, using motherboard kickstart\n");
-    maprom = 0;
-  } else {
-    int size = (int)lseek(fd, 0, SEEK_END);
-    if (size == 0x40000) {
-      lseek(fd, 0, SEEK_SET);
-      read(fd, &g_kick, size);
-      lseek(fd, 0, SEEK_SET);
-      read(fd, &g_kick[0x40000], size);
-    } else {
-      lseek(fd, 0, SEEK_SET);
-      read(fd, &g_kick, size);
-    }
-    printf("Loaded kick.rom with size %d kib\n", size / 1024);
-  }*/
-
   // reset amiga and statemachine
   skip_everything:;
   cpu_pulse_reset();
@@ -362,6 +350,7 @@ int main(int argc, char *argv[]) {
   usleep(1500);
 
   m68k_init();
+  printf("Setting CPU type to %d.\n", cpu_type);
   m68k_set_cpu_type(cpu_type);
   m68k_pulse_reset();
 
@@ -380,8 +369,7 @@ int main(int argc, char *argv[]) {
           else
               printf("\n IPL Thread created successfully\n");
 */
-
-  int cpu_emulation_running = 1;
+  char c = 0;
 
   m68k_pulse_reset();
   while (42) {
@@ -394,8 +382,8 @@ int main(int argc, char *argv[]) {
     if (cpu_emulation_running)
       m68k_execute(loop_cycles);
     
-    while (kbhit()) {
-      char c = getchar();
+    // FIXME: Rework this to use keyboard events instead.
+    /*while (get_key_char(&c)) {
       if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
         kb_hook_enabled = 1;
         printf("Keyboard hook enabled.\n");
@@ -404,16 +392,27 @@ int main(int argc, char *argv[]) {
         kb_hook_enabled = 0;
         printf("Keyboard hook disabled.\n");
       }
-      if (c == cfg->mouse_toggle_key) {
-        mouse_hook_enabled ^= 1;
-        printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
-        mouse_dx = mouse_dy = mouse_buttons = 0;
-      }
-      if (c == 'r') {
-        cpu_emulation_running ^= 1;
-        printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
+      if (!kb_hook_enabled) {
+        if (c == cfg->mouse_toggle_key) {
+          mouse_hook_enabled ^= 1;
+          printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
+          mouse_dx = mouse_dy = mouse_buttons = 0;
+        }
+        if (c == 'r') {
+          cpu_emulation_running ^= 1;
+          printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
+        }
+        if (c == 'R') {
+          cpu_pulse_reset();
+          m68k_pulse_reset();
+          printf("CPU emulation reset.\n");
+        }
+        if (c == 'q') {
+          printf("Quitting and exiting emulator.\n");
+          goto stop_cpu_emulation;
+        }
       }
-    }
+    }*/
 /*
     if (toggle == 1){
       srdata = read_reg();
@@ -425,19 +424,27 @@ int main(int argc, char *argv[]) {
 */
 
 
-    if (GET_GPIO(1) == 0){
+    if (GET_GPIO(1) == 0) {
       srdata = read_reg();
       m68k_set_irq((srdata >> 13) & 0xff);
     } else {
-      if (CheckIrq() == 1){
+      if (CheckIrq() == 1) {
         write16(0xdff09c, 0x8008);
-        m68k_set_irq(2);}
+        m68k_set_irq(2);
+      }
       else
          m68k_set_irq(0);
     };
 
   }
 
+  stop_cpu_emulation:;
+
+  if (mouse_fd != -1)
+    close(mouse_fd);
+  if (mem_fd)
+    close(mem_fd);
+
   return 0;
 }
 
@@ -456,27 +463,27 @@ int cpu_irq_ack(int level) {
 
 static unsigned int target = 0;
 
-unsigned int m68k_read_memory_8(unsigned int address) {
-  if (cfg) {
-    int ret = handle_mapped_read(cfg, address, &target, OP_TYPE_BYTE, ovl);
-    if (ret != -1)
-      return target;
-  }
-  /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
-    return g_ram[address - FASTBASE];
-  }
-
-  if (maprom == 1) {
-    if (address > KICKBASE && address < KICKBASE + KICKSIZE) {
-      return g_kick[address - KICKBASE];
-    }
+#define PLATFORM_CHECK_READ(a) \
+  if (address >= cfg->custom_low && address < cfg->custom_high) { \
+    unsigned int target = 0; \
+    switch(cfg->platform->id) { \
+      case PLATFORM_AMIGA: { \
+        if (custom_read_amiga(cfg, address, &target, a) != -1) { \
+          return target; \
+        } \
+        break; \
+      } \
+      default: \
+        break; \
+    } \
+  } \
+  if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
+    if (handle_mapped_read(cfg, address, &target, a) != -1) \
+      return target; \
   }
 
-  if (gayle_emulation_enabled) {
-    if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
-      return readGayleB(address);
-    }
-  }*/
+unsigned int m68k_read_memory_8(unsigned int address) {
+  PLATFORM_CHECK_READ(OP_TYPE_BYTE);
 
     address &=0xFFFFFF;
 //  if (address < 0xffffff) {
@@ -487,11 +494,7 @@ unsigned int m68k_read_memory_8(unsigned int address) {
 }
 
 unsigned int m68k_read_memory_16(unsigned int address) {
-  if (cfg) {
-    int ret = handle_mapped_read(cfg, address, &target, OP_TYPE_WORD, ovl);
-    if (ret != -1)
-      return target;
-  }
+  PLATFORM_CHECK_READ(OP_TYPE_WORD);
 
   if (mouse_hook_enabled) {
     if (address == JOY0DAT) {
@@ -519,21 +522,6 @@ unsigned int m68k_read_memory_16(unsigned int address) {
           return (unsigned int)result;
     }
   }
-  /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
-    return be16toh(*(uint16_t *)&g_ram[address - FASTBASE]);
-  }
-
-  if (maprom == 1) {
-    if (address > KICKBASE && address < KICKBASE + KICKSIZE) {
-      return be16toh(*(uint16_t *)&g_kick[address - KICKBASE]);
-    }
-  }
-
-  if (gayle_emulation_enabled) {
-    if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
-      return readGayle(address);
-    }
-  }*/
 
 //  if (address < 0xffffff) {
     address &=0xFFFFFF;
@@ -544,26 +532,7 @@ unsigned int m68k_read_memory_16(unsigned int address) {
 }
 
 unsigned int m68k_read_memory_32(unsigned int address) {
-  if (cfg) {
-    int ret = handle_mapped_read(cfg, address, &target, OP_TYPE_LONGWORD, ovl);
-    if (ret != -1)
-      return target;
-  }
-  /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
-    return be32toh(*(uint32_t *)&g_ram[address - FASTBASE]);
-  }
-
-  if (maprom == 1) {
-    if (address > KICKBASE && address < KICKBASE + KICKSIZE) {
-      return be32toh(*(uint32_t *)&g_kick[address - KICKBASE]);
-    }
-  }
-
-  if (gayle_emulation_enabled) {
-    if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
-      return readGayleL(address);
-    }
-  }*/
+  PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
 
 //  if (address < 0xffffff) {
     address &=0xFFFFFF;
@@ -575,23 +544,26 @@ unsigned int m68k_read_memory_32(unsigned int address) {
 //  return 1;
 }
 
-void m68k_write_memory_8(unsigned int address, unsigned int value) {
-  if (cfg) {
-    int ret = handle_mapped_write(cfg, address, value, OP_TYPE_BYTE, ovl);
-    if (ret != -1)
-      return;
-  }
-  /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
-    g_ram[address - FASTBASE] = value;
-    return;
+#define PLATFORM_CHECK_WRITE(a) \
+  if (address >= cfg->custom_low && address < cfg->custom_high) { \
+    switch(cfg->platform->id) { \
+      case PLATFORM_AMIGA: { \
+        if (custom_write_amiga(cfg, address, value, a) != -1) { \
+          return; \
+        } \
+        break; \
+      } \
+      default: \
+        break; \
+    } \
+  } \
+  if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
+    if (handle_mapped_write(cfg, address, value, a) != -1) \
+      return; \
   }
 
-  if (gayle_emulation_enabled) {
-    if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
-      writeGayleB(address, value);
-      return;
-    }
-  }*/
+void m68k_write_memory_8(unsigned int address, unsigned int value) {
+  PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
 
   if (address == 0xbfe001) {
     ovl = (value & (1 << 0));
@@ -608,22 +580,7 @@ void m68k_write_memory_8(unsigned int address, unsigned int value) {
 }
 
 void m68k_write_memory_16(unsigned int address, unsigned int value) {
-  if (cfg) {
-    int ret = handle_mapped_write(cfg, address, value, OP_TYPE_WORD, ovl);
-    if (ret != -1)
-      return;
-  }
-  /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
-    *(uint16_t *)&g_ram[address - FASTBASE] = htobe16(value);
-    return;
-  }
-
-  if (gayle_emulation_enabled) {
-    if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
-      writeGayle(address, value);
-      return;
-    }
-  }*/
+  PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
 
 //  if (address < 0xffffff) {
     address &=0xFFFFFF;
@@ -634,21 +591,7 @@ void m68k_write_memory_16(unsigned int address, unsigned int value) {
 }
 
 void m68k_write_memory_32(unsigned int address, unsigned int value) {
-  if (cfg) {
-    int ret = handle_mapped_write(cfg, address, value, OP_TYPE_LONGWORD, ovl);
-    if (ret != -1)
-      return;
-  }
-  /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
-    *(uint32_t *)&g_ram[address - FASTBASE] = htobe32(value);
-    return;
-  }
-
-  if (gayle_emulation_enabled) {
-    if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
-      writeGayleL(address, value);
-    }
-  }*/
+  PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
 
 //  if (address < 0xffffff) {
     address &=0xFFFFFF;
@@ -660,7 +603,7 @@ void m68k_write_memory_32(unsigned int address, unsigned int value) {
 //  return;
 }
 
-void write16(uint32_t address, uint32_t data) {
+inline void write16(uint32_t address, uint32_t data) {
   uint32_t addr_h_s = (address & 0x0000ffff) << 8;
   uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
   uint32_t addr_l_s = (address >> 16) << 8;
@@ -698,7 +641,7 @@ void write16(uint32_t address, uint32_t data) {
   //     asm volatile ("dmb" ::: "memory");
 }
 
-void write8(uint32_t address, uint32_t data) {
+inline void write8(uint32_t address, uint32_t data) {
   if ((address & 1) == 0)
     data = data + (data << 8);  // EVEN, A0=0,UDS
   else
@@ -740,7 +683,7 @@ void write8(uint32_t address, uint32_t data) {
   //   asm volatile ("dmb" ::: "memory");
 }
 
-uint32_t read16(uint32_t address) {
+inline uint32_t read16(uint32_t address) {
   volatile int val;
   uint32_t addr_h_s = (address & 0x0000ffff) << 8;
   uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
@@ -777,7 +720,7 @@ uint32_t read16(uint32_t address) {
   return (val >> 8) & 0xffff;
 }
 
-uint32_t read8(uint32_t address) {
+inline uint32_t read8(uint32_t address) {
   int val;
   uint32_t addr_h_s = (address & 0x0000ffff) << 8;
   uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
@@ -886,15 +829,3 @@ void setup_io() {
   gpclk = ((volatile unsigned *)gpio_map) + GPCLK_ADDR / 4;
 
 }  // setup_io
-
-int get_mouse_status(char *x, char *y, char *b) {
-  struct input_event ie;
-  if (read(mouse_fd, &ie, sizeof(struct input_event)) != -1) {
-    *b = ((char *)&ie)[0];
-    *x = ((char *)&ie)[1];
-    *y = ((char *)&ie)[2];
-    return 1;
-  }
-
-  return 0;
-}