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