]> git.sesse.net Git - pistorm/blob - emulator.c
Merge branch 'wip-crap' of https://github.com/beeanyew/pistorm into wip-crap
[pistorm] / emulator.c
1 #include <assert.h>
2 #include <dirent.h>
3 #include <endian.h>
4 #include <fcntl.h>
5 #include <pthread.h>
6 #include <sched.h>
7 #include <signal.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/mman.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <sys/ioctl.h>
17 #include "m68k.h"
18 #include "main.h"
19 #include "platforms/platforms.h"
20 #include "input/input.h"
21
22 #include "platforms/amiga/Gayle.h"
23 #include "platforms/amiga/gayle-ide/ide.h"
24 #include "platforms/amiga/amiga-registers.h"
25 #include "platforms/amiga/rtg/rtg.h"
26 #include "platforms/amiga/hunk-reloc.h"
27 #include "platforms/amiga/piscsi/piscsi.h"
28 #include "platforms/amiga/piscsi/piscsi-enums.h"
29 #include "platforms/amiga/net/pi-net.h"
30 #include "platforms/amiga/net/pi-net-enums.h"
31 #include "gpio/ps_protocol.h"
32
33 unsigned char read_ranges;
34 unsigned int read_addr[8];
35 unsigned int read_upper[8];
36 unsigned char *read_data[8];
37 unsigned char write_ranges;
38 unsigned int write_addr[8];
39 unsigned int write_upper[8];
40 unsigned char *write_data[8];
41
42 int kb_hook_enabled = 0;
43 int mouse_hook_enabled = 0;
44 int cpu_emulation_running = 1;
45
46 uint8_t mouse_dx = 0, mouse_dy = 0;
47 uint8_t mouse_buttons = 0;
48 uint8_t mouse_extra = 0;
49
50 extern uint8_t gayle_int;
51 extern uint8_t gayle_ide_enabled;
52 extern uint8_t gayle_emulation_enabled;
53 extern uint8_t gayle_a4k_int;
54 extern volatile unsigned int *gpio;
55 extern volatile uint16_t srdata;
56 extern uint8_t realtime_graphics_debug;
57 uint8_t realtime_disassembly, int2_enabled = 0;
58 uint32_t do_disasm = 0, old_level;
59
60 char disasm_buf[4096];
61
62 #define KICKBASE 0xF80000
63 #define KICKSIZE 0x7FFFF
64
65 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
66 int mem_fd_gpclk;
67 int irq;
68 int gayleirq;
69
70 //#define MUSASHI_HAX
71
72 #ifdef MUSASHI_HAX
73 #include "m68kcpu.h"
74 extern m68ki_cpu_core m68ki_cpu;
75 extern int m68ki_initial_cycles;
76 extern int m68ki_remaining_cycles;
77
78 #define M68K_SET_IRQ(i) old_level = CPU_INT_LEVEL; \
79         CPU_INT_LEVEL = (i << 8); \
80         if(old_level != 0x0700 && CPU_INT_LEVEL == 0x0700) \
81                 m68ki_cpu.nmi_pending = TRUE;
82 #define M68K_END_TIMESLICE      m68ki_initial_cycles = GET_CYCLES(); \
83         SET_CYCLES(0);
84 #else
85 #define M68K_SET_IRQ m68k_set_irq
86 #define M68K_END_TIMESLICE m68k_end_timeslice()
87 #endif
88
89 #define NOP asm("nop"); asm("nop"); asm("nop"); asm("nop");
90
91 #define DEBUG_EMULATOR
92 #ifdef DEBUG_EMULATOR
93 #define DEBUG printf
94 #else
95 #define DEBUG(...)
96 #endif
97
98 // Configurable emulator options
99 unsigned int cpu_type = M68K_CPU_TYPE_68000;
100 unsigned int loop_cycles = 300, irq_status = 0;
101 struct emulator_config *cfg = NULL;
102 char keyboard_file[256] = "/dev/input/event1";
103
104 uint64_t trig_irq = 0, serv_irq = 0;
105 uint16_t irq_delay = 0;
106
107 void *iplThread(void *args) {
108   printf("IPL thread running\n");
109   uint16_t old_irq = 0;
110   uint32_t value;
111
112   while (1) {
113     value = *(gpio + 13);
114
115     if (!(value & (1 << PIN_IPL_ZERO))) {
116       irq = 1;
117       old_irq = irq_delay;
118       NOP
119       M68K_END_TIMESLICE;
120     }
121     else {
122       if (irq) {
123         if (old_irq) {
124           old_irq--;
125         }
126         else {
127           irq = 0;
128         }
129         NOP
130         M68K_END_TIMESLICE;
131       }
132     }
133
134     if (gayle_ide_enabled) {
135       if (((gayle_int & 0x80) || gayle_a4k_int) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
136         //get_ide(0)->drive[0].intrq = 0;
137         gayleirq = 1;
138         M68K_END_TIMESLICE;
139       }
140       else
141         gayleirq = 0;
142     }
143     //usleep(0);
144     NOP NOP NOP NOP NOP NOP NOP NOP
145     NOP NOP NOP NOP NOP NOP NOP NOP
146     NOP NOP NOP NOP NOP NOP NOP NOP
147     /*NOP NOP NOP NOP NOP NOP NOP NOP
148     NOP NOP NOP NOP NOP NOP NOP NOP
149     NOP NOP NOP NOP NOP NOP NOP NOP*/
150   }
151   return args;
152 }
153
154 void stop_cpu_emulation(uint8_t disasm_cur) {
155   M68K_END_TIMESLICE;
156   if (disasm_cur) {
157     m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
158     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), \
159             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));
160     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), \
161             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));
162     printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
163     realtime_disassembly = 1;
164   }
165
166   cpu_emulation_running = 0;
167   do_disasm = 0;
168 }
169
170 unsigned int ovl;
171 static volatile unsigned char maprom;
172
173 void sigint_handler(int sig_num) {
174   //if (sig_num) { }
175   //cpu_emulation_running = 0;
176
177   //return;
178   printf("Received sigint %d, exiting.\n", sig_num);
179   if (mouse_fd != -1)
180     close(mouse_fd);
181   if (mem_fd)
182     close(mem_fd);
183
184   if (cfg->platform->shutdown) {
185     cfg->platform->shutdown(cfg);
186   }
187
188   printf("IRQs triggered: %lld\n", trig_irq);
189   printf("IRQs serviced: %lld\n", serv_irq);
190
191   exit(0);
192 }
193
194 int main(int argc, char *argv[]) {
195   int g;
196   //const struct sched_param priority = {99};
197
198   if (argc > 1) {
199     irq_delay = atoi(argv[1]);
200     printf("Setting IRQ delay to %d loops (%s).\n", irq_delay, argv[1]);
201   }
202
203   // Some command line switch stuffles
204   for (g = 1; g < argc; g++) {
205     if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
206       if (g + 1 >= argc) {
207         printf("%s switch found, but no CPU type specified.\n", argv[g]);
208       } else {
209         g++;
210         cpu_type = get_m68k_cpu_type(argv[g]);
211       }
212     }
213     else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
214       if (g + 1 >= argc) {
215         printf("%s switch found, but no config filename specified.\n", argv[g]);
216       } else {
217         g++;
218         cfg = load_config_file(argv[g]);
219       }
220     }
221     else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
222       if (g + 1 >= argc) {
223         printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
224       } else {
225         g++;
226         strcpy(keyboard_file, argv[g]);
227       }
228     }
229   }
230
231   if (!cfg) {
232     printf("No config file specified. Trying to load default.cfg...\n");
233     cfg = load_config_file("default.cfg");
234     if (!cfg) {
235       printf("Couldn't load default.cfg, empty emulator config will be used.\n");
236       cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
237       if (!cfg) {
238         printf("Failed to allocate memory for emulator config!\n");
239         return 1;
240       }
241       memset(cfg, 0x00, sizeof(struct emulator_config));
242     }
243   }
244
245   if (cfg) {
246     if (cfg->cpu_type) cpu_type = cfg->cpu_type;
247     if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
248
249     if (!cfg->platform)
250       cfg->platform = make_platform_config("none", "generic");
251     cfg->platform->platform_initial_setup(cfg);
252   }
253
254   if (cfg->mouse_enabled) {
255     mouse_fd = open(cfg->mouse_file, O_RDWR | O_NONBLOCK);
256     if (mouse_fd == -1) {
257       printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
258       cfg->mouse_enabled = 0;
259     } else {
260       /**
261        * *-*-*-* magic numbers! *-*-*-*
262        * great, so waaaay back in the history of the pc, the ps/2 protocol set the standard for mice
263        * and in the process, the mouse sample rate was defined as a way of putting mice into vendor-specific modes.
264        * as the ancient gpm command explains, almost everything except incredibly old mice talk the IntelliMouse
265        * protocol, which reports four bytes. by default, every mouse starts in 3-byte mode (don't report wheel or
266        * additional buttons) until imps2 magic is sent. so, command $f3 is "set sample rate", followed by a byte.
267        */
268       uint8_t mouse_init[] = { 0xf4, 0xf3, 0x64 }; // enable, then set sample rate 100
269       uint8_t imps2_init[] = { 0xf3, 0xc8, 0xf3, 0x64, 0xf3, 0x50 }; // magic sequence; set sample 200, 100, 80
270       if (write(mouse_fd, mouse_init, sizeof(mouse_init)) != -1) {
271         if (write(mouse_fd, imps2_init, sizeof(imps2_init)) == -1)
272           printf("[MOUSE] Couldn't enable scroll wheel events; is this mouse from the 1980s?\n");
273       } else
274         printf("[MOUSE] Mouse didn't respond to normal PS/2 init; have you plugged a brick in by mistake?\n");
275     }
276   }
277
278   keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
279   if (keyboard_fd == -1) {
280     printf("Failed to open keyboard event source.\n");
281   }
282
283   InitGayle();
284
285   signal(SIGINT, sigint_handler);
286   /*setup_io();
287
288   //goto skip_everything;
289
290   // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
291   // on pi model
292   printf("Enable 200MHz GPCLK0 on GPIO4\n");
293   gpio_enable_200mhz();
294
295   // reset cpld statemachine first
296
297   write_reg(0x01);
298   usleep(100);
299   usleep(1500);
300   write_reg(0x00);
301   usleep(100);
302
303   // reset amiga and statemachine
304   skip_everything:;
305
306   usleep(1500);
307
308   m68k_init();
309   printf("Setting CPU type to %d.\n", cpu_type);
310   m68k_set_cpu_type(cpu_type);
311   cpu_pulse_reset();
312
313   if (maprom == 1) {
314     m68k_set_reg(M68K_REG_PC, 0xF80002);
315   } else {
316     m68k_set_reg(M68K_REG_PC, 0x0);
317   }*/
318   ps_setup_protocol();
319   ps_reset_state_machine();
320   ps_pulse_reset();
321
322   usleep(1500);
323   m68k_init();
324   printf("Setting CPU type to %d.\n", cpu_type);
325   m68k_set_cpu_type(cpu_type);
326   cpu_pulse_reset();
327
328   char c = 0, c_code = 0, c_type = 0;
329   uint32_t last_irq = 0, last_last_irq = 0;
330
331   pthread_t id;
332   int err;
333   err = pthread_create(&id, NULL, &iplThread, NULL);
334   if (err != 0)
335     printf("can't create IPL thread :[%s]", strerror(err));
336   else
337     printf("IPL Thread created successfully\n");
338
339   m68k_pulse_reset();
340   while (42) {
341     if (mouse_hook_enabled) {
342       get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons, &mouse_extra);
343     }
344
345     if (realtime_disassembly && (do_disasm || cpu_emulation_running)) {
346       m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
347       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), \
348               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));
349       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), \
350               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));
351       printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
352       if (do_disasm)
353         do_disasm--;
354       m68k_execute(1);
355     }
356     else {
357       if (cpu_emulation_running)
358         m68k_execute(loop_cycles);
359     }
360
361     if (irq) {
362       while (irq) {
363         last_irq = ((read_reg() & 0xe000) >> 13);
364         if (last_irq != last_last_irq) {
365           last_last_irq = last_irq;
366           M68K_SET_IRQ(last_irq);
367         }
368         m68k_execute(5);
369       }
370       if (gayleirq && int2_enabled) {
371         write16(0xdff09c, 0x8000 | (1 << 3) && last_irq != 2);
372         last_last_irq = last_irq;
373         last_irq = 2;
374         M68K_SET_IRQ(2);
375       }
376       M68K_SET_IRQ(0);
377       last_last_irq = 0;
378     }
379     /*else {
380       if (last_irq != 0) {
381         M68K_SET_IRQ(0);
382         last_last_irq = last_irq;
383         last_irq = 0;
384       }
385     }*/
386
387     while (get_key_char(&c, &c_code, &c_type)) {
388       if (c && c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
389         kb_hook_enabled = 1;
390         printf("Keyboard hook enabled.\n");
391       }
392       else if (kb_hook_enabled) {
393         if (c == 0x1B && c_type) {
394           kb_hook_enabled = 0;
395           printf("Keyboard hook disabled.\n");
396         }
397         else {
398           /*printf("Key code: %.2X - ", c_code);
399           switch (c_type) {
400             case 0:
401               printf("released.\n");
402               break;
403             case 1:
404               printf("pressed.\n");
405               break;
406             case 2:
407               printf("repeat.\n");
408               break;
409             default:
410               printf("unknown.\n");
411               break;
412           }*/
413           if (queue_keypress(c_code, c_type, cfg->platform->id) && int2_enabled && last_irq != 2) {
414             //last_irq = 0;
415             //M68K_SET_IRQ(2);
416           }
417         }
418       }
419
420       // pause pressed; trigger nmi (int level 7)
421       if (c == 0x01 && c_type) {
422         printf("[INT] Sending NMI\n");
423         last_irq = 7;
424         M68K_SET_IRQ(7);
425       }
426
427       if (!kb_hook_enabled && c_type) {
428         if (c && c == cfg->mouse_toggle_key) {
429           mouse_hook_enabled ^= 1;
430           printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
431           mouse_dx = mouse_dy = mouse_buttons = mouse_extra = 0;
432         }
433         if (c == 'r') {
434           cpu_emulation_running ^= 1;
435           printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
436         }
437         if (c == 'g') {
438           realtime_graphics_debug ^= 1;
439           printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
440         }
441         if (c == 'R') {
442           cpu_pulse_reset();
443           //m68k_pulse_reset();
444           printf("CPU emulation reset.\n");
445         }
446         if (c == 'q') {
447           printf("Quitting and exiting emulator.\n");
448           goto stop_cpu_emulation;
449         }
450         if (c == 'd') {
451           realtime_disassembly ^= 1;
452           do_disasm = 1;
453           printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
454         }
455         if (c == 'D') {
456           int r = get_mapped_item_by_address(cfg, 0x08000000);
457           if (r != -1) {
458             printf("Dumping first 16MB of mapped range %d.\n", r);
459             FILE *dmp = fopen("./memdmp.bin", "wb+");
460             fwrite(cfg->map_data[r], 16 * SIZE_MEGA, 1, dmp);
461             fclose(dmp);
462           }
463         }
464         if (c == 's' && realtime_disassembly) {
465           do_disasm = 1;
466         }
467         if (c == 'S' && realtime_disassembly) {
468           do_disasm = 128;
469         }
470       }
471     }
472
473     if (mouse_hook_enabled && (mouse_extra != 0x00)) {
474       // mouse wheel events have occurred; unlike l/m/r buttons, these are queued as keypresses, so add to end of buffer
475       switch (mouse_extra) {
476         case 0xff:
477           // wheel up
478           queue_keypress(0xfe, KEYPRESS_PRESS, PLATFORM_AMIGA);
479           break;
480         case 0x01:
481           // wheel down
482           queue_keypress(0xff, KEYPRESS_PRESS, PLATFORM_AMIGA);
483           break;
484       }
485
486       // dampen the scroll wheel until next while loop iteration
487       mouse_extra = 0x00;
488     }
489   }
490
491   stop_cpu_emulation:;
492
493   if (mouse_fd != -1)
494     close(mouse_fd);
495   if (mem_fd)
496     close(mem_fd);
497
498   return 0;
499 }
500
501 void cpu_pulse_reset(void) {
502   ps_pulse_reset();
503   //write_reg(0x00);
504   // printf("Status Reg%x\n",read_reg());
505   //usleep(100000);
506   //write_reg(0x02);
507   // printf("Status Reg%x\n",read_reg());
508   if (cfg->platform->handle_reset)
509     cfg->platform->handle_reset(cfg);
510
511   
512   m68k_write_memory_16(INTENA, 0x7FFF);
513   ovl = 1;
514   m68k_write_memory_8(0xbfe201, 0x0001);  // AMIGA OVL
515   m68k_write_memory_8(0xbfe001, 0x0001);  // AMIGA OVL high (ROM@0x0)
516
517   m68k_pulse_reset();
518 }
519
520 int cpu_irq_ack(int level) {
521   printf("cpu irq ack\n");
522   return level;
523 }
524
525 static unsigned int target = 0;
526 static uint8_t send_keypress = 0;
527
528 uint8_t cdtv_dmac_reg_idx_read();
529 void cdtv_dmac_reg_idx_write(uint8_t value);
530 uint32_t cdtv_dmac_read(uint32_t address, uint8_t type);
531 void cdtv_dmac_write(uint32_t address, uint32_t value, uint8_t type);
532
533 #define PLATFORM_CHECK_READ(a) \
534   if (address >= cfg->custom_low && address < cfg->custom_high) { \
535     unsigned int target = 0; \
536     switch(cfg->platform->id) { \
537       case PLATFORM_AMIGA: { \
538         if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
539           return handle_piscsi_read(address, a); \
540         } \
541         if (address >= PINET_OFFSET && address < PINET_UPPER) { \
542           return handle_pinet_read(address, a); \
543         } \
544         if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
545           return rtg_read((address & 0x0FFFFFFF), a); \
546         } \
547         if (custom_read_amiga(cfg, address, &target, a) != -1) { \
548           return target; \
549         } \
550         break; \
551       } \
552       default: \
553         break; \
554     } \
555   } \
556   if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
557     if (handle_mapped_read(cfg, address, &target, a) != -1) \
558       return target; \
559   }
560
561 unsigned int m68k_read_memory_8(unsigned int address) {
562   PLATFORM_CHECK_READ(OP_TYPE_BYTE);
563
564   /*if (address >= 0xE90000 && address < 0xF00000) {
565     printf("BYTE read from DMAC @%.8X:", address);
566     uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_BYTE);
567     printf("%.2X\n", v);
568     M68K_END_TIMESLICE;
569     cpu_emulation_running = 0;
570     return v;
571   }*/
572
573   /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
574     stop_cpu_emulation(1);
575   }*/
576
577
578   if (address & 0xFF000000)
579     return 0;
580
581   unsigned char result = (unsigned int)read8((uint32_t)address);
582
583   if (mouse_hook_enabled) {
584     if (address == CIAAPRA) {
585       if (mouse_buttons & 0x01) {
586         //mouse_buttons -= 1;
587         return (unsigned int)(result ^ 0x40);
588       }
589
590       return (unsigned int)result;
591     }
592   }
593
594   if (kb_hook_enabled) {
595     if (address == CIAAICR) {
596       if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
597         result |= 0x08;
598         if (!send_keypress)
599           send_keypress = 1;
600       }
601       if (send_keypress == 2) {
602         //result |= 0x02;
603         send_keypress = 0;
604       }
605       return result;
606     }
607     if (address == CIAADAT) {
608       //if (send_keypress) {
609         uint8_t c = 0, t = 0;
610         pop_queued_key(&c, &t);
611         t ^= 0x01;
612         result = ((c << 1) | t) ^ 0xFF;
613         send_keypress = 2;
614         //M68K_SET_IRQ(0);
615       //}
616       return result;
617     }
618   }
619
620   return result;
621 }
622
623 unsigned int m68k_read_memory_16(unsigned int address) {
624   PLATFORM_CHECK_READ(OP_TYPE_WORD);
625
626   /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
627     stop_cpu_emulation(1);
628   }*/
629
630   /*if (address >= 0xE90000 && address < 0xF00000) {
631     printf("WORD read from DMAC @%.8X:", address);
632     uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_WORD);
633     printf("%.2X\n", v);
634     M68K_END_TIMESLICE;
635     cpu_emulation_running = 0;
636     return v;
637   }*/
638
639   if (mouse_hook_enabled) {
640     if (address == JOY0DAT) {
641       // Forward mouse valueses to Amyga.
642       unsigned short result = (mouse_dy << 8) | (mouse_dx);
643       return (unsigned int)result;
644     }
645     /*if (address == CIAAPRA) {
646       unsigned short result = (unsigned int)read16((uint32_t)address);
647       if (mouse_buttons & 0x01) {
648         return (unsigned int)(result | 0x40);
649       }
650       else
651           return (unsigned int)result;
652     }*/
653     if (address == POTGOR) {
654       unsigned short result = (unsigned int)read16((uint32_t)address);
655       // bit 1 rmb, bit 2 mmb
656       if (mouse_buttons & 0x06) {
657         return (unsigned int)((result ^ ((mouse_buttons & 0x02) << 9))   // move rmb to bit 10
658                             & (result ^ ((mouse_buttons & 0x04) << 6))); // move mmb to bit 8
659       }
660       return (unsigned int)(result & 0xfffd);
661     }
662   }
663
664   if (address & 0xFF000000)
665     return 0;
666
667   if (address & 0x01) {
668     return ((read8(address) << 8) | read8(address + 1));
669   }
670   return (unsigned int)read16((uint32_t)address);
671 }
672
673 unsigned int m68k_read_memory_32(unsigned int address) {
674   PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
675
676   /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
677     stop_cpu_emulation(1);
678   }*/
679
680   /*if (address >= 0xE90000 && address < 0xF00000) {
681     printf("LONGWORD read from DMAC @%.8X:", address);
682     uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_LONGWORD);
683     printf("%.2X\n", v);
684     M68K_END_TIMESLICE;
685     cpu_emulation_running = 0;
686     return v;
687   }*/
688
689   if (address & 0xFF000000)
690     return 0;
691
692   if (address & 0x01) {
693     uint32_t c = read8(address);
694     c |= (be16toh(read16(address+1)) << 8);
695     c |= (read8(address + 3) << 24);
696     return htobe32(c);
697   }
698   uint16_t a = read16(address);
699   uint16_t b = read16(address + 2);
700   return (a << 16) | b;
701 }
702
703 #define PLATFORM_CHECK_WRITE(a) \
704   if (address >= cfg->custom_low && address < cfg->custom_high) { \
705     switch(cfg->platform->id) { \
706       case PLATFORM_AMIGA: { \
707         if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
708           handle_piscsi_write(address, value, a); \
709         } \
710         if (address >= PINET_OFFSET && address < PINET_UPPER) { \
711           handle_pinet_write(address, value, a); \
712         } \
713         if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
714           rtg_write((address & 0x0FFFFFFF), value, a); \
715           return; \
716         } \
717         if (custom_write_amiga(cfg, address, value, a) != -1) { \
718           return; \
719         } \
720         break; \
721       } \
722       default: \
723         break; \
724     } \
725   } \
726   if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
727     if (handle_mapped_write(cfg, address, value, a) != -1) \
728       return; \
729   }
730
731 void m68k_write_memory_8(unsigned int address, unsigned int value) {
732   PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
733
734   /*if (address >= 0xE90000 && address < 0xF00000) {
735     printf("BYTE write to DMAC @%.8X: %.2X\n", address, value);
736     cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_BYTE);
737     M68K_END_TIMESLICE;
738     cpu_emulation_running = 0;
739     return;
740   }*/
741
742   if (address == 0xbfe001) {
743     if (ovl != (value & (1 << 0))) {
744       ovl = (value & (1 << 0));
745       printf("OVL:%x\n", ovl);
746     }
747   }
748
749   if (address & 0xFF000000)
750     return;
751
752   write8((uint32_t)address, value);
753   return;
754 }
755
756 void m68k_write_memory_16(unsigned int address, unsigned int value) {
757   PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
758
759   /*if (address >= 0xE90000 && address < 0xF00000) {
760     printf("WORD write to DMAC @%.8X: %.4X\n", address, value);
761     cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_WORD);
762     M68K_END_TIMESLICE;
763     cpu_emulation_running = 0;
764     return;
765   }*/
766
767   if (address == 0xDFF030) {
768     char *serdat = (char *)&value;
769     // SERDAT word. see amiga dev docs appendix a; upper byte is control codes, and bit 0 is always 1.
770     // ignore this upper byte as it's not viewable data, only display lower byte.
771     printf("%c", serdat[0]);
772   }
773   if (address == 0xDFF09A) {
774     if (!(value & 0x8000)) {
775       if (value & 0x04) {
776         int2_enabled = 0;
777       }
778     }
779     else if (value & 0x04) {
780       int2_enabled = 1;
781     }
782   }
783
784   if (address & 0xFF000000)
785     return;
786
787   if (address & 0x01)
788     printf("Unaligned WORD write!\n");
789
790   write16((uint32_t)address, value);
791   return;
792 }
793
794 void m68k_write_memory_32(unsigned int address, unsigned int value) {
795   PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
796
797   /*if (address >= 0xE90000 && address < 0xF00000) {
798     printf("LONGWORD write to DMAC @%.8X: %.8X\n", address, value);
799     cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_LONGWORD);
800     M68K_END_TIMESLICE;
801     cpu_emulation_running = 0;
802     return;
803   }*/
804
805   if (address & 0xFF000000)
806     return;
807
808   if (address & 0x01)
809     printf("Unaligned LONGWORD write!\n");
810
811   write16(address, value >> 16);
812   write16(address + 2, value);
813   return;
814 }