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