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