]> git.sesse.net Git - pistorm/blobdiff - emulator.c
Fix masked FillRect, add buptest bus tester
[pistorm] / emulator.c
index c6bf30cbcd3717ebe8dd3a38480080f729070ba3..88e243393dd08b6f43efbdf6f4437eb4f45698c8 100644 (file)
@@ -14,7 +14,6 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <sys/ioctl.h>
-#include <SDL2/SDL.h>
 #include "m68k.h"
 #include "main.h"
 #include "platforms/platforms.h"
 #include "platforms/amiga/rtg/rtg.h"
 #include "gpio/gpio.h"
 
+unsigned char read_ranges;
+unsigned int read_addr[8];
+unsigned int read_upper[8];
+unsigned char *read_data[8];
+unsigned char write_ranges;
+unsigned int write_addr[8];
+unsigned int write_upper[8];
+unsigned char *write_data[8];
+
 int kb_hook_enabled = 0;
 int mouse_hook_enabled = 0;
 int cpu_emulation_running = 1;
@@ -35,6 +43,10 @@ char mouse_buttons = 0;
 
 extern volatile unsigned int *gpio;
 extern volatile uint16_t srdata;
+extern uint8_t realtime_graphics_debug;
+uint8_t realtime_disassembly;
+
+char disasm_buf[4096];
 
 #define KICKBASE 0xF80000
 #define KICKSIZE 0x7FFFF
@@ -42,12 +54,39 @@ extern volatile uint16_t srdata;
 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
 int mem_fd_gpclk;
 int gayle_emulation_enabled = 1;
+int irq;
+int gayleirq;
+
+void *iplThread(void *args) {
+  printf("IPL thread running\n");
+
+  while (1) {
+    if (!gpio_get_irq()) {
+      irq = 1;
+      m68k_end_timeslice();
+    }
+    else
+      irq = 0;
+
+    if (gayle_emulation_enabled) {
+      if ((gayle_int & 0x80) && get_ide(0)->drive->intrq) {
+        gayleirq = 1;
+        m68k_end_timeslice();
+      }
+      else
+        gayleirq = 0;
+    }
+    usleep(0);
+  }
+  return args;
+}
+
 
 // Configurable emulator options
 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";
+char keyboard_file[256] = "/dev/input/event1";
 
 //unsigned char g_kick[524288];
 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
@@ -138,30 +177,11 @@ int main(int argc, char *argv[]) {
     }
   }
 
-  // Initialize SDL.
-  printf("Initializing SDL2...\n");
-  if (SDL_Init(0) < 0) {
-      printf("Failed to initialize SDL2.\n");
-  }
-  else {
-    printf("Initializing SDL2 Video...\n");
-    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
-        printf("Failed to initialize SDL2 Video. Trying again.\n");
-        if (SDL_Init(SDL_INIT_VIDEO) < 0) {
-            printf("Failed to initialize SDL2 Video again. Dying.\n");
-        }
-    }
-    printf("Initialized SDL2 Video.\n");
-  }
-
   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
-
   InitGayle();
 
   signal(SIGINT, sigint_handler);
@@ -184,17 +204,13 @@ int main(int argc, char *argv[]) {
 
   // reset amiga and statemachine
   skip_everything:;
-  cpu_pulse_reset();
-  ovl = 1;
-  m68k_write_memory_8(0xbfe201, 0x0001);  // AMIGA OVL
-  m68k_write_memory_8(0xbfe001, 0x0001);  // AMIGA OVL high (ROM@0x0)
 
   usleep(1500);
 
   m68k_init();
   printf("Setting CPU type to %d.\n", cpu_type);
   m68k_set_cpu_type(cpu_type);
-  m68k_pulse_reset();
+  cpu_pulse_reset();
 
   if (maprom == 1) {
     m68k_set_reg(M68K_REG_PC, 0xF80002);
@@ -204,19 +220,47 @@ int main(int argc, char *argv[]) {
 
   char c = 0;
 
+  pthread_t id;
+  int err;
+  err = pthread_create(&id, NULL, &iplThread, NULL);
+  if (err != 0)
+    printf("can't create IPL thread :[%s]", strerror(err));
+  else
+    printf("IPL Thread created successfully\n");
+
   m68k_pulse_reset();
   while (42) {
     if (mouse_hook_enabled) {
-      if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
-        //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
-      }
+      get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons);
     }
 
     if (cpu_emulation_running)
       m68k_execute(loop_cycles);
+
+disasm_run:;
+    if (realtime_disassembly) {
+      m68k_execute(1);
+      m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
+      printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
+    }
     
+    if (irq) {
+      unsigned int status = read_reg();
+      m68k_set_irq((status & 0xe000) >> 13);
+      //printf("There was an IRQ: %d\n", (status & 0xe000) >> 13);
+    }
+    else if (gayleirq) {
+      write16(0xdff09c, 0x8000 | (1 << 3));
+      //PAULA_SET_IRQ(3); // IRQ 3 = INT2
+      m68k_set_irq(2);
+    }
+    else {
+        m68k_set_irq(0);
+    }
+
+  //usleep(0);
     // FIXME: Rework this to use keyboard events instead.
-    /*while (get_key_char(&c)) {
+    while (get_key_char(&c)) {
       if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
         kb_hook_enabled = 1;
         printf("Keyboard hook enabled.\n");
@@ -235,6 +279,10 @@ int main(int argc, char *argv[]) {
           cpu_emulation_running ^= 1;
           printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
         }
+        if (c == 'g') {
+          realtime_graphics_debug ^= 1;
+          printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
+        }
         if (c == 'R') {
           cpu_pulse_reset();
           m68k_pulse_reset();
@@ -244,11 +292,18 @@ int main(int argc, char *argv[]) {
           printf("Quitting and exiting emulator.\n");
           goto stop_cpu_emulation;
         }
+        if (c == 'd') {
+          realtime_disassembly ^= 1;
+          printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
+        }
       }
-    }*/
+    }
+
+    if (realtime_disassembly)
+      goto disasm_run;
 
     //gpio_handle_irq();
-    GPIO_HANDLE_IRQ;
+    //GPIO_HANDLE_IRQ;
   }
 
   stop_cpu_emulation:;
@@ -267,6 +322,12 @@ void cpu_pulse_reset(void) {
   usleep(100000);
   write_reg(0x02);
   // printf("Status Reg%x\n",read_reg());
+
+  ovl = 1;
+  m68k_write_memory_8(0xbfe201, 0x0001);  // AMIGA OVL
+  m68k_write_memory_8(0xbfe001, 0x0001);  // AMIGA OVL high (ROM@0x0)
+
+  m68k_pulse_reset();
 }
 
 int cpu_irq_ack(int level) {
@@ -281,7 +342,7 @@ static unsigned int target = 0;
     unsigned int target = 0; \
     switch(cfg->platform->id) { \
       case PLATFORM_AMIGA: { \
-        if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
+        if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
           return rtg_read((address & 0x0FFFFFFF), a); \
         } \
         if (custom_read_amiga(cfg, address, &target, a) != -1) { \
@@ -301,8 +362,20 @@ static unsigned int target = 0;
 unsigned int m68k_read_memory_8(unsigned int address) {
   PLATFORM_CHECK_READ(OP_TYPE_BYTE);
 
-    address &=0xFFFFFF;
-    return read8((uint32_t)address);
+  if (mouse_hook_enabled) {
+    if (address == CIAAPRA) {
+      unsigned char result = (unsigned int)read8((uint32_t)address);
+      if (mouse_buttons & 0x01) {
+        //mouse_buttons -= 1;
+        return (unsigned int)(result ^ 0x40);
+      }
+      else
+          return (unsigned int)result;
+    }
+  }
+
+  address &=0xFFFFFF;
+  return read8((uint32_t)address);
 }
 
 unsigned int m68k_read_memory_16(unsigned int address) {
@@ -312,31 +385,30 @@ unsigned int m68k_read_memory_16(unsigned int address) {
     if (address == JOY0DAT) {
       // Forward mouse valueses to Amyga.
       unsigned short result = (mouse_dy << 8) | (mouse_dx);
-      mouse_dx = mouse_dy = 0;
       return (unsigned int)result;
     }
-    if (address == CIAAPRA) {
+    /*if (address == CIAAPRA) {
       unsigned short result = (unsigned int)read16((uint32_t)address);
       if (mouse_buttons & 0x01) {
-        mouse_buttons -= 1;
         return (unsigned int)(result | 0x40);
       }
       else
           return (unsigned int)result;
-    }
+    }*/
     if (address == POTGOR) {
       unsigned short result = (unsigned int)read16((uint32_t)address);
       if (mouse_buttons & 0x02) {
-        mouse_buttons -= 2;
-        return (unsigned int)(result | 0x2);
+        return (unsigned int)(result ^ (0x2 << 9));
       }
       else
-          return (unsigned int)result;
+          return (unsigned int)(result & 0xFFFD);
     }
   }
 
-
   address &=0xFFFFFF;
+  /*if (address & 0x01) {
+    return ((read8(address) << 8) | read8(address + 1));
+  }*/
   return (unsigned int)read16((uint32_t)address);
 }
 
@@ -344,6 +416,11 @@ unsigned int m68k_read_memory_32(unsigned int address) {
   PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
 
   address &=0xFFFFFF;
+  /*if (address & 0x01) {
+    uint32_t c = be32toh(read32(address));
+    c = (c >> 8) | (read8(address + 3) << 24);
+    return htobe32(c);
+  }*/
   uint16_t a = read16(address);
   uint16_t b = read16(address + 2);
   return (a << 16) | b;
@@ -353,7 +430,7 @@ unsigned int m68k_read_memory_32(unsigned int address) {
   if (address >= cfg->custom_low && address < cfg->custom_high) { \
     switch(cfg->platform->id) { \
       case PLATFORM_AMIGA: { \
-        if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
+        if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
           rtg_write((address & 0x0FFFFFFF), value, a); \
           return; \
         } \