14 #include <sys/types.h>
16 #include <sys/ioctl.h>
19 #include "platforms/platforms.h"
20 #include "input/input.h"
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"
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];
42 int kb_hook_enabled = 0;
43 int mouse_hook_enabled = 0;
44 int cpu_emulation_running = 1;
46 char mouse_dx = 0, mouse_dy = 0;
47 char mouse_buttons = 0;
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;
59 char disasm_buf[4096];
61 #define KICKBASE 0xF80000
62 #define KICKSIZE 0x7FFFF
64 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
69 // Configurable emulator options
70 unsigned int cpu_type = M68K_CPU_TYPE_68000;
71 unsigned int loop_cycles = 300;
72 struct emulator_config *cfg = NULL;
73 char keyboard_file[256] = "/dev/input/event1";
75 void *iplThread(void *args) {
76 printf("IPL thread running\n");
79 if (!gpio_get_irq()) {
85 if (gayle_ide_enabled) {
86 if (((gayle_int & 0x80) || gayle_a4k_int) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
87 //get_ide(0)->drive[0].intrq = 0;
99 void stop_cpu_emulation(uint8_t disasm_cur) {
100 m68k_end_timeslice();
102 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
103 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), \
104 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));
105 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), \
106 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));
107 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
108 realtime_disassembly = 1;
111 cpu_emulation_running = 0;
116 static volatile unsigned char maprom;
118 void sigint_handler(int sig_num) {
120 //cpu_emulation_running = 0;
123 printf("Received sigint %d, exiting.\n", sig_num);
129 if (cfg->platform->shutdown) {
130 cfg->platform->shutdown(cfg);
136 int main(int argc, char *argv[]) {
138 //const struct sched_param priority = {99};
140 // Some command line switch stuffles
141 for (g = 1; g < argc; g++) {
142 if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
144 printf("%s switch found, but no CPU type specified.\n", argv[g]);
147 cpu_type = get_m68k_cpu_type(argv[g]);
150 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
152 printf("%s switch found, but no config filename specified.\n", argv[g]);
155 cfg = load_config_file(argv[g]);
158 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
160 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
163 strcpy(keyboard_file, argv[g]);
169 printf("No config file specified. Trying to load default.cfg...\n");
170 cfg = load_config_file("default.cfg");
172 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
173 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
175 printf("Failed to allocate memory for emulator config!\n");
178 memset(cfg, 0x00, sizeof(struct emulator_config));
183 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
184 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
187 cfg->platform = make_platform_config("none", "generic");
188 cfg->platform->platform_initial_setup(cfg);
191 if (cfg->mouse_enabled) {
192 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
193 if (mouse_fd == -1) {
194 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
195 cfg->mouse_enabled = 0;
199 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
200 if (keyboard_fd == -1) {
201 printf("Failed to open keyboard event source.\n");
206 signal(SIGINT, sigint_handler);
209 //goto skip_everything;
211 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
213 printf("Enable 200MHz GPCLK0 on GPIO4\n");
214 gpio_enable_200mhz();
216 // reset cpld statemachine first
224 // reset amiga and statemachine
230 printf("Setting CPU type to %d.\n", cpu_type);
231 m68k_set_cpu_type(cpu_type);
235 m68k_set_reg(M68K_REG_PC, 0xF80002);
237 m68k_set_reg(M68K_REG_PC, 0x0);
240 ps_reset_state_machine();
245 printf("Setting CPU type to %d.\n", cpu_type);
246 m68k_set_cpu_type(cpu_type);
249 char c = 0, c_code = 0, c_type = 0;
253 err = pthread_create(&id, NULL, &iplThread, NULL);
255 printf("can't create IPL thread :[%s]", strerror(err));
257 printf("IPL Thread created successfully\n");
261 if (mouse_hook_enabled) {
262 get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons);
265 if (realtime_disassembly && (do_disasm || cpu_emulation_running)) {
266 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
267 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), \
268 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));
269 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), \
270 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));
271 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
277 if (cpu_emulation_running)
278 m68k_execute(loop_cycles);
282 unsigned int status = read_reg();
283 m68k_set_irq((status & 0xe000) >> 13);
286 else if (gayleirq && int2_enabled) {
287 write16(0xdff09c, 0x8000 | (1 << 3));
295 while (get_key_char(&c, &c_code, &c_type)) {
296 if (c && c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
298 printf("Keyboard hook enabled.\n");
300 else if (kb_hook_enabled) {
301 if (c == 0x1B && c_type) {
303 printf("Keyboard hook disabled.\n");
306 /*printf("Key code: %.2X - ", c_code);
309 printf("released.\n");
312 printf("pressed.\n");
318 printf("unknown.\n");
321 if (queue_keypress(c_code, c_type, cfg->platform->id) && int2_enabled) {
327 if (!kb_hook_enabled && c_type) {
328 if (c && c == cfg->mouse_toggle_key) {
329 mouse_hook_enabled ^= 1;
330 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
331 mouse_dx = mouse_dy = mouse_buttons = 0;
334 cpu_emulation_running ^= 1;
335 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
338 realtime_graphics_debug ^= 1;
339 printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
343 //m68k_pulse_reset();
344 printf("CPU emulation reset.\n");
347 printf("Quitting and exiting emulator.\n");
348 goto stop_cpu_emulation;
351 realtime_disassembly ^= 1;
353 printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
356 int r = get_mapped_item_by_address(cfg, 0x08000000);
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);
364 if (c == 's' && realtime_disassembly) {
367 if (c == 'S' && realtime_disassembly) {
384 void cpu_pulse_reset(void) {
387 // printf("Status Reg%x\n",read_reg());
390 // printf("Status Reg%x\n",read_reg());
391 if (cfg->platform->handle_reset)
392 cfg->platform->handle_reset(cfg);
395 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
396 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
401 int cpu_irq_ack(int level) {
402 printf("cpu irq ack\n");
406 static unsigned int target = 0;
407 static uint8_t send_keypress = 0;
409 uint8_t cdtv_dmac_reg_idx_read();
410 void cdtv_dmac_reg_idx_write(uint8_t value);
411 uint32_t cdtv_dmac_read(uint32_t address, uint8_t type);
412 void cdtv_dmac_write(uint32_t address, uint32_t value, uint8_t type);
414 #define PLATFORM_CHECK_READ(a) \
415 if (address >= cfg->custom_low && address < cfg->custom_high) { \
416 unsigned int target = 0; \
417 switch(cfg->platform->id) { \
418 case PLATFORM_AMIGA: { \
419 if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
420 return handle_piscsi_read(address, a); \
422 if (address >= PINET_OFFSET && address < PINET_UPPER) { \
423 return handle_pinet_read(address, a); \
425 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
426 return rtg_read((address & 0x0FFFFFFF), a); \
428 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
437 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
438 if (handle_mapped_read(cfg, address, &target, a) != -1) \
442 unsigned int m68k_read_memory_8(unsigned int address) {
443 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
445 /*if (address >= 0xE90000 && address < 0xF00000) {
446 printf("BYTE read from DMAC @%.8X:", address);
447 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_BYTE);
449 m68k_end_timeslice();
450 cpu_emulation_running = 0;
454 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
455 stop_cpu_emulation(1);
459 if (address & 0xFF000000)
462 unsigned char result = (unsigned int)read8((uint32_t)address);
464 if (mouse_hook_enabled) {
465 if (address == CIAAPRA) {
466 if (mouse_buttons & 0x01) {
467 //mouse_buttons -= 1;
468 return (unsigned int)(result ^ 0x40);
471 return (unsigned int)result;
474 if (kb_hook_enabled) {
475 if (address == CIAAICR) {
476 if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
481 if (send_keypress == 2) {
487 if (address == CIAADAT) {
489 uint8_t c = 0, t = 0;
490 pop_queued_key(&c, &t);
492 result = ((c << 1) | t) ^ 0xFF;
502 unsigned int m68k_read_memory_16(unsigned int address) {
503 PLATFORM_CHECK_READ(OP_TYPE_WORD);
505 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
506 stop_cpu_emulation(1);
509 /*if (address >= 0xE90000 && address < 0xF00000) {
510 printf("WORD read from DMAC @%.8X:", address);
511 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_WORD);
513 m68k_end_timeslice();
514 cpu_emulation_running = 0;
518 if (mouse_hook_enabled) {
519 if (address == JOY0DAT) {
520 // Forward mouse valueses to Amyga.
521 unsigned short result = (mouse_dy << 8) | (mouse_dx);
522 return (unsigned int)result;
524 /*if (address == CIAAPRA) {
525 unsigned short result = (unsigned int)read16((uint32_t)address);
526 if (mouse_buttons & 0x01) {
527 return (unsigned int)(result | 0x40);
530 return (unsigned int)result;
532 if (address == POTGOR) {
533 unsigned short result = (unsigned int)read16((uint32_t)address);
534 if (mouse_buttons & 0x02) {
535 return (unsigned int)(result ^ (0x2 << 9));
538 return (unsigned int)(result & 0xFFFD);
542 if (address & 0xFF000000)
545 if (address & 0x01) {
546 return ((read8(address) << 8) | read8(address + 1));
548 return (unsigned int)read16((uint32_t)address);
551 unsigned int m68k_read_memory_32(unsigned int address) {
552 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
554 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
555 stop_cpu_emulation(1);
558 /*if (address >= 0xE90000 && address < 0xF00000) {
559 printf("LONGWORD read from DMAC @%.8X:", address);
560 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_LONGWORD);
562 m68k_end_timeslice();
563 cpu_emulation_running = 0;
567 if (address & 0xFF000000)
570 if (address & 0x01) {
571 uint32_t c = read8(address);
572 c |= (be16toh(read16(address+1)) << 8);
573 c |= (read8(address + 3) << 24);
576 uint16_t a = read16(address);
577 uint16_t b = read16(address + 2);
578 return (a << 16) | b;
581 #define PLATFORM_CHECK_WRITE(a) \
582 if (address >= cfg->custom_low && address < cfg->custom_high) { \
583 switch(cfg->platform->id) { \
584 case PLATFORM_AMIGA: { \
585 if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
586 handle_piscsi_write(address, value, a); \
588 if (address >= PINET_OFFSET && address < PINET_UPPER) { \
589 handle_pinet_write(address, value, a); \
591 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
592 rtg_write((address & 0x0FFFFFFF), value, a); \
595 if (custom_write_amiga(cfg, address, value, a) != -1) { \
604 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
605 if (handle_mapped_write(cfg, address, value, a) != -1) \
609 void m68k_write_memory_8(unsigned int address, unsigned int value) {
610 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
612 /*if (address >= 0xE90000 && address < 0xF00000) {
613 printf("BYTE write to DMAC @%.8X: %.2X\n", address, value);
614 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_BYTE);
615 m68k_end_timeslice();
616 cpu_emulation_running = 0;
620 if (address == 0xbfe001) {
621 if (ovl != (value & (1 << 0))) {
622 ovl = (value & (1 << 0));
623 printf("OVL:%x\n", ovl);
627 if (address & 0xFF000000)
630 write8((uint32_t)address, value);
634 void m68k_write_memory_16(unsigned int address, unsigned int value) {
635 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
637 /*if (address >= 0xE90000 && address < 0xF00000) {
638 printf("WORD write to DMAC @%.8X: %.4X\n", address, value);
639 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_WORD);
640 m68k_end_timeslice();
641 cpu_emulation_running = 0;
645 if (address == 0xDFF030) {
646 char *beb = (char *)&value;
647 printf("%c%c", beb[1], beb[0]);
649 if (address == 0xDFF09A) {
650 if (!(value & 0x8000)) {
655 else if (value & 0x04) {
660 if (address & 0xFF000000)
664 printf("Unaligned WORD write!\n");
666 write16((uint32_t)address, value);
670 void m68k_write_memory_32(unsigned int address, unsigned int value) {
671 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
673 /*if (address >= 0xE90000 && address < 0xF00000) {
674 printf("LONGWORD write to DMAC @%.8X: %.8X\n", address, value);
675 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_LONGWORD);
676 m68k_end_timeslice();
677 cpu_emulation_running = 0;
681 if (address & 0xFF000000)
685 printf("Unaligned LONGWORD write!\n");
687 write16(address, value >> 16);
688 write16(address + 2, value);