]> git.sesse.net Git - pistorm/blobdiff - emulator.c
Merge branch 'wip-crap' of https://github.com/beeanyew/pistorm into wip-crap
[pistorm] / emulator.c
index b1ac4bcc07bdfaece9805f4d1c8d5629078fe15c..290b6c051a3f2dd195c981c1f590359ff9d6da5e 100644 (file)
@@ -43,8 +43,9 @@ 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;
+uint8_t mouse_dx = 0, mouse_dy = 0;
+uint8_t mouse_buttons = 0;
+uint8_t mouse_extra = 0;
 
 extern uint8_t gayle_int;
 extern uint8_t gayle_ide_enabled;
@@ -251,10 +252,26 @@ int main(int argc, char *argv[]) {
   }
 
   if (cfg->mouse_enabled) {
-    mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
+    mouse_fd = open(cfg->mouse_file, O_RDWR | O_NONBLOCK);
     if (mouse_fd == -1) {
       printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
       cfg->mouse_enabled = 0;
+    } else {
+      /**
+       * *-*-*-* magic numbers! *-*-*-*
+       * great, so waaaay back in the history of the pc, the ps/2 protocol set the standard for mice
+       * and in the process, the mouse sample rate was defined as a way of putting mice into vendor-specific modes.
+       * as the ancient gpm command explains, almost everything except incredibly old mice talk the IntelliMouse
+       * protocol, which reports four bytes. by default, every mouse starts in 3-byte mode (don't report wheel or
+       * additional buttons) until imps2 magic is sent. so, command $f3 is "set sample rate", followed by a byte.
+       */
+      uint8_t mouse_init[] = { 0xf4, 0xf3, 0x64 }; // enable, then set sample rate 100
+      uint8_t imps2_init[] = { 0xf3, 0xc8, 0xf3, 0x64, 0xf3, 0x50 }; // magic sequence; set sample 200, 100, 80
+      if (write(mouse_fd, mouse_init, sizeof(mouse_init)) != -1) {
+        if (write(mouse_fd, imps2_init, sizeof(imps2_init)) == -1)
+          printf("[MOUSE] Couldn't enable scroll wheel events; is this mouse from the 1980s?\n");
+      } else
+        printf("[MOUSE] Mouse didn't respond to normal PS/2 init; have you plugged a brick in by mistake?\n");
     }
   }
 
@@ -322,7 +339,7 @@ int main(int argc, char *argv[]) {
   m68k_pulse_reset();
   while (42) {
     if (mouse_hook_enabled) {
-      get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons);
+      get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons, &mouse_extra);
     }
 
     if (realtime_disassembly && (do_disasm || cpu_emulation_running)) {
@@ -400,11 +417,18 @@ int main(int argc, char *argv[]) {
         }
       }
 
+      // pause pressed; trigger nmi (int level 7)
+      if (c == 0x01 && c_type) {
+        printf("[INT] Sending NMI\n");
+        last_irq = 7;
+        M68K_SET_IRQ(7);
+      }
+
       if (!kb_hook_enabled && c_type) {
         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;
+          mouse_dx = mouse_dy = mouse_buttons = mouse_extra = 0;
         }
         if (c == 'r') {
           cpu_emulation_running ^= 1;
@@ -445,6 +469,23 @@ int main(int argc, char *argv[]) {
         }
       }
     }
+
+    if (mouse_hook_enabled && (mouse_extra != 0x00)) {
+      // mouse wheel events have occurred; unlike l/m/r buttons, these are queued as keypresses, so add to end of buffer
+      switch (mouse_extra) {
+        case 0xff:
+          // wheel up
+          queue_keypress(0xfe, KEYPRESS_PRESS, PLATFORM_AMIGA);
+          break;
+        case 0x01:
+          // wheel down
+          queue_keypress(0xff, KEYPRESS_PRESS, PLATFORM_AMIGA);
+          break;
+      }
+
+      // dampen the scroll wheel until next while loop iteration
+      mouse_extra = 0x00;
+    }
   }
 
   stop_cpu_emulation:;
@@ -545,10 +586,11 @@ unsigned int m68k_read_memory_8(unsigned int address) {
         //mouse_buttons -= 1;
         return (unsigned int)(result ^ 0x40);
       }
-      else
-          return (unsigned int)result;
+
+      return (unsigned int)result;
     }
   }
+
   if (kb_hook_enabled) {
     if (address == CIAAICR) {
       if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
@@ -610,11 +652,12 @@ unsigned int m68k_read_memory_16(unsigned int address) {
     }*/
     if (address == POTGOR) {
       unsigned short result = (unsigned int)read16((uint32_t)address);
-      if (mouse_buttons & 0x02) {
-        return (unsigned int)(result ^ (0x2 << 9));
+      // bit 1 rmb, bit 2 mmb
+      if (mouse_buttons & 0x06) {
+        return (unsigned int)((result ^ ((mouse_buttons & 0x02) << 9))   // move rmb to bit 10
+                            & (result ^ ((mouse_buttons & 0x04) << 6))); // move mmb to bit 8
       }
-      else
-          return (unsigned int)(result & 0xFFFD);
+      return (unsigned int)(result & 0xfffd);
     }
   }
 
@@ -722,8 +765,10 @@ void m68k_write_memory_16(unsigned int address, unsigned int value) {
   }*/
 
   if (address == 0xDFF030) {
-    char *beb = (char *)&value;
-    printf("%c%c", beb[1], beb[0]);
+    char *serdat = (char *)&value;
+    // SERDAT word. see amiga dev docs appendix a; upper byte is control codes, and bit 0 is always 1.
+    // ignore this upper byte as it's not viewable data, only display lower byte.
+    printf("%c", serdat[0]);
   }
   if (address == 0xDFF09A) {
     if (!(value & 0x8000)) {