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