]> git.sesse.net Git - pistorm/blobdiff - emulator.c
[WIP] PiSCSI boot ROM disabled for now
[pistorm] / emulator.c
index d02dd58b6d7de641b84e637cd24e9143fabe5a3d..0de9337fe25de737379f6a7103191e1fdb9394f7 100644 (file)
 #include "platforms/amiga/gayle-ide/ide.h"
 #include "platforms/amiga/amiga-registers.h"
 #include "platforms/amiga/rtg/rtg.h"
+#include "platforms/amiga/piscsi/piscsi.h"
+#include "platforms/amiga/piscsi/piscsi-enums.h"
+#include "platforms/amiga/net/pi-net.h"
+#include "platforms/amiga/net/pi-net-enums.h"
 #include "gpio/gpio.h"
 
 unsigned char read_ranges;
@@ -45,6 +49,7 @@ extern volatile unsigned int *gpio;
 extern volatile uint16_t srdata;
 extern uint8_t realtime_graphics_debug;
 uint8_t realtime_disassembly;
+uint32_t do_disasm = 0;
 
 char disasm_buf[4096];
 
@@ -57,6 +62,12 @@ int gayle_emulation_enabled = 1;
 int irq;
 int gayleirq;
 
+// 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/event1";
+
 void *iplThread(void *args) {
   printf("IPL thread running\n");
 
@@ -69,7 +80,8 @@ void *iplThread(void *args) {
       irq = 0;
 
     if (gayle_emulation_enabled) {
-      if ((gayle_int & 0x80) && get_ide(0)->drive->intrq) {
+      if (((gayle_int & 0x80) || gayle_a4k_int) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
+        //get_ide(0)->drive[0].intrq = 0;
         gayleirq = 1;
         m68k_end_timeslice();
       }
@@ -81,12 +93,21 @@ void *iplThread(void *args) {
   return args;
 }
 
+void stop_cpu_emulation(uint8_t disasm_cur) {
+  m68k_end_timeslice();
+  if (disasm_cur) {
+    m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
+    printf("REGA: 0:$%.8X 1:$%.8X 2:$%.8X 3:$%.8X 4:$%.8X 5:$%.8X 6:$%.8X 7:$%.8X\n", m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1), m68k_get_reg(NULL, M68K_REG_A2), m68k_get_reg(NULL, M68K_REG_A3), \
+            m68k_get_reg(NULL, M68K_REG_A4), m68k_get_reg(NULL, M68K_REG_A5), m68k_get_reg(NULL, M68K_REG_A6), m68k_get_reg(NULL, M68K_REG_A7));
+    printf("REGD: 0:$%.8X 1:$%.8X 2:$%.8X 3:$%.8X 4:$%.8X 5:$%.8X 6:$%.8X 7:$%.8X\n", m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2), m68k_get_reg(NULL, M68K_REG_D3), \
+            m68k_get_reg(NULL, M68K_REG_D4), m68k_get_reg(NULL, M68K_REG_D5), m68k_get_reg(NULL, M68K_REG_D6), m68k_get_reg(NULL, M68K_REG_D7));
+    printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
+    realtime_disassembly = 1;
+  }
 
-// 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/event1";
+  cpu_emulation_running = 0;
+  do_disasm = 0;
+}
 
 //unsigned char g_kick[524288];
 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
@@ -234,16 +255,22 @@ int main(int argc, char *argv[]) {
       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);
+    if (realtime_disassembly && (do_disasm || cpu_emulation_running)) {
       m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
+      printf("REGA: 0:$%.8X 1:$%.8X 2:$%.8X 3:$%.8X 4:$%.8X 5:$%.8X 6:$%.8X 7:$%.8X\n", m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1), m68k_get_reg(NULL, M68K_REG_A2), m68k_get_reg(NULL, M68K_REG_A3), \
+              m68k_get_reg(NULL, M68K_REG_A4), m68k_get_reg(NULL, M68K_REG_A5), m68k_get_reg(NULL, M68K_REG_A6), m68k_get_reg(NULL, M68K_REG_A7));
+      printf("REGD: 0:$%.8X 1:$%.8X 2:$%.8X 3:$%.8X 4:$%.8X 5:$%.8X 6:$%.8X 7:$%.8X\n", m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2), m68k_get_reg(NULL, M68K_REG_D3), \
+              m68k_get_reg(NULL, M68K_REG_D4), m68k_get_reg(NULL, M68K_REG_D5), m68k_get_reg(NULL, M68K_REG_D6), m68k_get_reg(NULL, M68K_REG_D7));
       printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
+      if (do_disasm)
+        do_disasm--;
+      m68k_execute(1);
     }
-    
+    else {
+      if (cpu_emulation_running)
+        m68k_execute(loop_cycles);
+    }
+
     if (irq) {
       unsigned int status = read_reg();
       m68k_set_irq((status & 0xe000) >> 13);
@@ -257,11 +284,8 @@ disasm_run:;
         m68k_set_irq(0);
     }
 
-  //usleep(0);
-    // FIXME: Rework this to use keyboard events instead.
     while (get_key_char(&c, &c_code, &c_type)) {
-
-      if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
+      if (c && c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
         kb_hook_enabled = 1;
         printf("Keyboard hook enabled.\n");
       }
@@ -293,7 +317,7 @@ disasm_run:;
       }
 
       if (!kb_hook_enabled && c_type) {
-        if (c == cfg->mouse_toggle_key) {
+        if (c && 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;
@@ -308,7 +332,7 @@ disasm_run:;
         }
         if (c == 'R') {
           cpu_pulse_reset();
-          m68k_pulse_reset();
+          //m68k_pulse_reset();
           printf("CPU emulation reset.\n");
         }
         if (c == 'q') {
@@ -317,14 +341,27 @@ disasm_run:;
         }
         if (c == 'd') {
           realtime_disassembly ^= 1;
+          do_disasm = 1;
           printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
         }
+        if (c == 'D') {
+          int r = get_mapped_item_by_address(cfg, 0x08000000);
+          if (r != -1) {
+            printf("Dumping first 16MB of mapped range %d.\n", r);
+            FILE *dmp = fopen("./memdmp.bin", "wb+");
+            fwrite(cfg->map_data[r], 16 * SIZE_MEGA, 1, dmp);
+            fclose(dmp);
+          }
+        }
+        if (c == 's' && realtime_disassembly) {
+          do_disasm = 1;
+        }
+        if (c == 'S' && realtime_disassembly) {
+          do_disasm = 128;
+        }
       }
     }
 
-    if (realtime_disassembly)
-      goto disasm_run;
-
     //gpio_handle_irq();
     //GPIO_HANDLE_IRQ;
   }
@@ -345,6 +382,8 @@ void cpu_pulse_reset(void) {
   usleep(100000);
   write_reg(0x02);
   // printf("Status Reg%x\n",read_reg());
+  if (cfg->platform->handle_reset)
+    cfg->platform->handle_reset(cfg);
 
   ovl = 1;
   m68k_write_memory_8(0xbfe201, 0x0001);  // AMIGA OVL
@@ -361,11 +400,22 @@ int cpu_irq_ack(int level) {
 static unsigned int target = 0;
 static uint8_t send_keypress = 0;
 
+uint8_t cdtv_dmac_reg_idx_read();
+void cdtv_dmac_reg_idx_write(uint8_t value);
+uint32_t cdtv_dmac_read(uint32_t address, uint8_t type);
+void cdtv_dmac_write(uint32_t address, uint32_t value, uint8_t type);
+
 #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 (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
+          return handle_piscsi_read(address, a); \
+        } \
+        if (address >= PINET_OFFSET && address < PINET_UPPER) { \
+          return handle_pinet_read(address, a); \
+        } \
         if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
           return rtg_read((address & 0x0FFFFFFF), a); \
         } \
@@ -386,6 +436,20 @@ static uint8_t send_keypress = 0;
 unsigned int m68k_read_memory_8(unsigned int address) {
   PLATFORM_CHECK_READ(OP_TYPE_BYTE);
 
+  /*if (address >= 0xE90000 && address < 0xF00000) {
+    printf("BYTE read from DMAC @%.8X:", address);
+    uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_BYTE);
+    printf("%.2X\n", v);
+    m68k_end_timeslice();
+    cpu_emulation_running = 0;
+    return v;
+  }*/
+
+  /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
+    stop_cpu_emulation(1);
+  }*/
+
+
   if (mouse_hook_enabled) {
     if (address == CIAAPRA) {
       unsigned char result = (unsigned int)read8((uint32_t)address);
@@ -423,13 +487,28 @@ unsigned int m68k_read_memory_8(unsigned int address) {
     }
   }
 
-  address &=0xFFFFFF;
+  if (address & 0xFF000000)
+    return 0;
+
   return read8((uint32_t)address);
 }
 
 unsigned int m68k_read_memory_16(unsigned int address) {
   PLATFORM_CHECK_READ(OP_TYPE_WORD);
 
+  /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
+    stop_cpu_emulation(1);
+  }*/
+
+  /*if (address >= 0xE90000 && address < 0xF00000) {
+    printf("WORD read from DMAC @%.8X:", address);
+    uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_WORD);
+    printf("%.2X\n", v);
+    m68k_end_timeslice();
+    cpu_emulation_running = 0;
+    return v;
+  }*/
+
   if (mouse_hook_enabled) {
     if (address == JOY0DAT) {
       // Forward mouse valueses to Amyga.
@@ -454,7 +533,9 @@ unsigned int m68k_read_memory_16(unsigned int address) {
     }
   }
 
-  address &=0xFFFFFF;
+  if (address & 0xFF000000)
+    return 0;
+
   if (address & 0x01) {
     return ((read8(address) << 8) | read8(address + 1));
   }
@@ -464,7 +545,22 @@ unsigned int m68k_read_memory_16(unsigned int address) {
 unsigned int m68k_read_memory_32(unsigned int address) {
   PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
 
-  address &=0xFFFFFF;
+  /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
+    stop_cpu_emulation(1);
+  }*/
+
+  /*if (address >= 0xE90000 && address < 0xF00000) {
+    printf("LONGWORD read from DMAC @%.8X:", address);
+    uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_LONGWORD);
+    printf("%.2X\n", v);
+    m68k_end_timeslice();
+    cpu_emulation_running = 0;
+    return v;
+  }*/
+
+  if (address & 0xFF000000)
+    return 0;
+
   if (address & 0x01) {
     uint32_t c = read8(address);
     c |= (be16toh(read16(address+1)) << 8);
@@ -480,6 +576,12 @@ 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 >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
+          handle_piscsi_write(address, value, a); \
+        } \
+        if (address >= PINET_OFFSET && address < PINET_UPPER) { \
+          handle_pinet_write(address, value, a); \
+        } \
         if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
           rtg_write((address & 0x0FFFFFFF), value, a); \
           return; \
@@ -501,6 +603,14 @@ unsigned int m68k_read_memory_32(unsigned int address) {
 void m68k_write_memory_8(unsigned int address, unsigned int value) {
   PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
 
+  /*if (address >= 0xE90000 && address < 0xF00000) {
+    printf("BYTE write to DMAC @%.8X: %.2X\n", address, value);
+    cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_BYTE);
+    m68k_end_timeslice();
+    cpu_emulation_running = 0;
+    return;
+  }*/
+
   if (address == 0xbfe001) {
     if (ovl != (value & (1 << 0))) {
       ovl = (value & (1 << 0));
@@ -508,7 +618,9 @@ void m68k_write_memory_8(unsigned int address, unsigned int value) {
     }
   }
 
-  address &=0xFFFFFF;
+  if (address & 0xFF000000)
+    return;
+
   write8((uint32_t)address, value);
   return;
 }
@@ -516,7 +628,25 @@ void m68k_write_memory_8(unsigned int address, unsigned int value) {
 void m68k_write_memory_16(unsigned int address, unsigned int value) {
   PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
 
-  address &=0xFFFFFF;
+  /*if (address >= 0xE90000 && address < 0xF00000) {
+    printf("WORD write to DMAC @%.8X: %.4X\n", address, value);
+    cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_WORD);
+    m68k_end_timeslice();
+    cpu_emulation_running = 0;
+    return;
+  }*/
+
+  if (address == 0xDFF030) {
+    char *beb = (char *)&value;
+    printf("%c%c", beb[1], beb[0]);
+  }
+
+  if (address & 0xFF000000)
+    return;
+
+  if (address & 0x01)
+    printf("Unaligned WORD write!\n");
+
   write16((uint32_t)address, value);
   return;
 }
@@ -524,7 +654,20 @@ void m68k_write_memory_16(unsigned int address, unsigned int value) {
 void m68k_write_memory_32(unsigned int address, unsigned int value) {
   PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
 
-  address &=0xFFFFFF;
+  /*if (address >= 0xE90000 && address < 0xF00000) {
+    printf("LONGWORD write to DMAC @%.8X: %.8X\n", address, value);
+    cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_LONGWORD);
+    m68k_end_timeslice();
+    cpu_emulation_running = 0;
+    return;
+  }*/
+
+  if (address & 0xFF000000)
+    return;
+
+  if (address & 0x01)
+    printf("Unaligned LONGWORD write!\n");
+
   write16(address, value >> 16);
   write16(address + 2, value);
   return;