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 uint8_t mouse_dx = 0, mouse_dy = 0;
47 uint8_t mouse_buttons = 0;
48 uint8_t mouse_extra = 0;
50 extern uint8_t gayle_int;
51 extern uint8_t gayle_ide_enabled;
52 extern uint8_t gayle_emulation_enabled;
53 extern uint8_t gayle_a4k_int;
54 extern volatile unsigned int *gpio;
55 extern volatile uint16_t srdata;
56 extern uint8_t realtime_graphics_debug;
57 uint8_t realtime_disassembly, int2_enabled = 0;
58 uint32_t do_disasm = 0, old_level;
60 char disasm_buf[4096];
62 #define KICKBASE 0xF80000
63 #define KICKSIZE 0x7FFFF
65 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
74 extern m68ki_cpu_core m68ki_cpu;
75 extern int m68ki_initial_cycles;
76 extern int m68ki_remaining_cycles;
78 #define M68K_SET_IRQ(i) old_level = CPU_INT_LEVEL; \
79 CPU_INT_LEVEL = (i << 8); \
80 if(old_level != 0x0700 && CPU_INT_LEVEL == 0x0700) \
81 m68ki_cpu.nmi_pending = TRUE;
82 #define M68K_END_TIMESLICE m68ki_initial_cycles = GET_CYCLES(); \
85 #define M68K_SET_IRQ m68k_set_irq
86 #define M68K_END_TIMESLICE m68k_end_timeslice()
89 #define NOP asm("nop"); asm("nop"); asm("nop"); asm("nop");
91 #define DEBUG_EMULATOR
98 // Configurable emulator options
99 unsigned int cpu_type = M68K_CPU_TYPE_68000;
100 unsigned int loop_cycles = 300, irq_status = 0;
101 struct emulator_config *cfg = NULL;
102 char keyboard_file[256] = "/dev/input/event1";
104 uint64_t trig_irq = 0, serv_irq = 0;
105 uint16_t irq_delay = 0;
107 void *iplThread(void *args) {
108 printf("IPL thread running\n");
109 uint16_t old_irq = 0;
113 value = *(gpio + 13);
115 if (!(value & (1 << PIN_IPL_ZERO))) {
134 if (gayle_ide_enabled) {
135 if (((gayle_int & 0x80) || gayle_a4k_int) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
136 //get_ide(0)->drive[0].intrq = 0;
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 NOP NOP NOP NOP NOP NOP NOP NOP*/
154 void stop_cpu_emulation(uint8_t disasm_cur) {
157 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
158 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), \
159 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));
160 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), \
161 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));
162 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
163 realtime_disassembly = 1;
166 cpu_emulation_running = 0;
171 static volatile unsigned char maprom;
173 void sigint_handler(int sig_num) {
175 //cpu_emulation_running = 0;
178 printf("Received sigint %d, exiting.\n", sig_num);
184 if (cfg->platform->shutdown) {
185 cfg->platform->shutdown(cfg);
188 printf("IRQs triggered: %lld\n", trig_irq);
189 printf("IRQs serviced: %lld\n", serv_irq);
194 int main(int argc, char *argv[]) {
196 //const struct sched_param priority = {99};
199 irq_delay = atoi(argv[1]);
200 printf("Setting IRQ delay to %d loops (%s).\n", irq_delay, argv[1]);
203 // Some command line switch stuffles
204 for (g = 1; g < argc; g++) {
205 if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
207 printf("%s switch found, but no CPU type specified.\n", argv[g]);
210 cpu_type = get_m68k_cpu_type(argv[g]);
213 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
215 printf("%s switch found, but no config filename specified.\n", argv[g]);
218 cfg = load_config_file(argv[g]);
221 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
223 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
226 strcpy(keyboard_file, argv[g]);
232 printf("No config file specified. Trying to load default.cfg...\n");
233 cfg = load_config_file("default.cfg");
235 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
236 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
238 printf("Failed to allocate memory for emulator config!\n");
241 memset(cfg, 0x00, sizeof(struct emulator_config));
246 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
247 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
250 cfg->platform = make_platform_config("none", "generic");
251 cfg->platform->platform_initial_setup(cfg);
254 if (cfg->mouse_enabled) {
255 mouse_fd = open(cfg->mouse_file, O_RDWR | O_NONBLOCK);
256 if (mouse_fd == -1) {
257 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
258 cfg->mouse_enabled = 0;
261 * *-*-*-* magic numbers! *-*-*-*
262 * great, so waaaay back in the history of the pc, the ps/2 protocol set the standard for mice
263 * and in the process, the mouse sample rate was defined as a way of putting mice into vendor-specific modes.
264 * as the ancient gpm command explains, almost everything except incredibly old mice talk the IntelliMouse
265 * protocol, which reports four bytes. by default, every mouse starts in 3-byte mode (don't report wheel or
266 * additional buttons) until imps2 magic is sent. so, command $f3 is "set sample rate", followed by a byte.
268 uint8_t mouse_init[] = { 0xf4, 0xf3, 0x64 }; // enable, then set sample rate 100
269 uint8_t imps2_init[] = { 0xf3, 0xc8, 0xf3, 0x64, 0xf3, 0x50 }; // magic sequence; set sample 200, 100, 80
270 if (write(mouse_fd, mouse_init, sizeof(mouse_init)) != -1) {
271 if (write(mouse_fd, imps2_init, sizeof(imps2_init)) == -1)
272 printf("[MOUSE] Couldn't enable scroll wheel events; is this mouse from the 1980s?\n");
274 printf("[MOUSE] Mouse didn't respond to normal PS/2 init; have you plugged a brick in by mistake?\n");
278 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
279 if (keyboard_fd == -1) {
280 printf("Failed to open keyboard event source.\n");
285 signal(SIGINT, sigint_handler);
288 //goto skip_everything;
290 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
292 printf("Enable 200MHz GPCLK0 on GPIO4\n");
293 gpio_enable_200mhz();
295 // reset cpld statemachine first
303 // reset amiga and statemachine
309 printf("Setting CPU type to %d.\n", cpu_type);
310 m68k_set_cpu_type(cpu_type);
314 m68k_set_reg(M68K_REG_PC, 0xF80002);
316 m68k_set_reg(M68K_REG_PC, 0x0);
319 ps_reset_state_machine();
324 printf("Setting CPU type to %d.\n", cpu_type);
325 m68k_set_cpu_type(cpu_type);
328 char c = 0, c_code = 0, c_type = 0;
329 uint32_t last_irq = 0, last_last_irq = 0;
333 err = pthread_create(&id, NULL, &iplThread, NULL);
335 printf("can't create IPL thread :[%s]", strerror(err));
337 printf("IPL Thread created successfully\n");
341 if (mouse_hook_enabled) {
342 get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons, &mouse_extra);
345 if (realtime_disassembly && (do_disasm || cpu_emulation_running)) {
346 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
347 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), \
348 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));
349 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), \
350 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));
351 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
357 if (cpu_emulation_running)
358 m68k_execute(loop_cycles);
363 last_irq = ((read_reg() & 0xe000) >> 13);
364 if (last_irq != last_last_irq) {
365 last_last_irq = last_irq;
366 M68K_SET_IRQ(last_irq);
370 if (gayleirq && int2_enabled) {
371 write16(0xdff09c, 0x8000 | (1 << 3) && last_irq != 2);
372 last_last_irq = last_irq;
382 last_last_irq = last_irq;
387 while (get_key_char(&c, &c_code, &c_type)) {
388 if (c && c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
390 printf("Keyboard hook enabled.\n");
392 else if (kb_hook_enabled) {
393 if (c == 0x1B && c_type) {
395 printf("Keyboard hook disabled.\n");
398 /*printf("Key code: %.2X - ", c_code);
401 printf("released.\n");
404 printf("pressed.\n");
410 printf("unknown.\n");
413 if (queue_keypress(c_code, c_type, cfg->platform->id) && int2_enabled && last_irq != 2) {
420 // pause pressed; trigger nmi (int level 7)
421 if (c == 0x01 && c_type) {
422 printf("[INT] Sending NMI\n");
427 if (!kb_hook_enabled && c_type) {
428 if (c && c == cfg->mouse_toggle_key) {
429 mouse_hook_enabled ^= 1;
430 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
431 mouse_dx = mouse_dy = mouse_buttons = mouse_extra = 0;
434 cpu_emulation_running ^= 1;
435 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
438 realtime_graphics_debug ^= 1;
439 printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
443 //m68k_pulse_reset();
444 printf("CPU emulation reset.\n");
447 printf("Quitting and exiting emulator.\n");
448 goto stop_cpu_emulation;
451 realtime_disassembly ^= 1;
453 printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
456 int r = get_mapped_item_by_address(cfg, 0x08000000);
458 printf("Dumping first 16MB of mapped range %d.\n", r);
459 FILE *dmp = fopen("./memdmp.bin", "wb+");
460 fwrite(cfg->map_data[r], 16 * SIZE_MEGA, 1, dmp);
464 if (c == 's' && realtime_disassembly) {
467 if (c == 'S' && realtime_disassembly) {
473 if (mouse_hook_enabled && (mouse_extra != 0x00)) {
474 // mouse wheel events have occurred; unlike l/m/r buttons, these are queued as keypresses, so add to end of buffer
475 switch (mouse_extra) {
478 queue_keypress(0xfe, KEYPRESS_PRESS, PLATFORM_AMIGA);
482 queue_keypress(0xff, KEYPRESS_PRESS, PLATFORM_AMIGA);
486 // dampen the scroll wheel until next while loop iteration
501 void cpu_pulse_reset(void) {
504 // printf("Status Reg%x\n",read_reg());
507 // printf("Status Reg%x\n",read_reg());
508 if (cfg->platform->handle_reset)
509 cfg->platform->handle_reset(cfg);
512 m68k_write_memory_16(INTENA, 0x7FFF);
514 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
515 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
520 int cpu_irq_ack(int level) {
521 printf("cpu irq ack\n");
525 static unsigned int target = 0;
526 static uint8_t send_keypress = 0;
528 uint8_t cdtv_dmac_reg_idx_read();
529 void cdtv_dmac_reg_idx_write(uint8_t value);
530 uint32_t cdtv_dmac_read(uint32_t address, uint8_t type);
531 void cdtv_dmac_write(uint32_t address, uint32_t value, uint8_t type);
533 #define PLATFORM_CHECK_READ(a) \
534 if (address >= cfg->custom_low && address < cfg->custom_high) { \
535 unsigned int target = 0; \
536 switch(cfg->platform->id) { \
537 case PLATFORM_AMIGA: { \
538 if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
539 return handle_piscsi_read(address, a); \
541 if (address >= PINET_OFFSET && address < PINET_UPPER) { \
542 return handle_pinet_read(address, a); \
544 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
545 return rtg_read((address & 0x0FFFFFFF), a); \
547 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
556 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
557 if (handle_mapped_read(cfg, address, &target, a) != -1) \
561 unsigned int m68k_read_memory_8(unsigned int address) {
562 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
564 /*if (address >= 0xE90000 && address < 0xF00000) {
565 printf("BYTE read from DMAC @%.8X:", address);
566 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_BYTE);
569 cpu_emulation_running = 0;
573 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
574 stop_cpu_emulation(1);
578 if (address & 0xFF000000)
581 unsigned char result = (unsigned int)read8((uint32_t)address);
583 if (mouse_hook_enabled) {
584 if (address == CIAAPRA) {
585 if (mouse_buttons & 0x01) {
586 //mouse_buttons -= 1;
587 return (unsigned int)(result ^ 0x40);
590 return (unsigned int)result;
594 if (kb_hook_enabled) {
595 if (address == CIAAICR) {
596 if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
601 if (send_keypress == 2) {
607 if (address == CIAADAT) {
608 //if (send_keypress) {
609 uint8_t c = 0, t = 0;
610 pop_queued_key(&c, &t);
612 result = ((c << 1) | t) ^ 0xFF;
623 unsigned int m68k_read_memory_16(unsigned int address) {
624 PLATFORM_CHECK_READ(OP_TYPE_WORD);
626 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
627 stop_cpu_emulation(1);
630 /*if (address >= 0xE90000 && address < 0xF00000) {
631 printf("WORD read from DMAC @%.8X:", address);
632 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_WORD);
635 cpu_emulation_running = 0;
639 if (mouse_hook_enabled) {
640 if (address == JOY0DAT) {
641 // Forward mouse valueses to Amyga.
642 unsigned short result = (mouse_dy << 8) | (mouse_dx);
643 return (unsigned int)result;
645 /*if (address == CIAAPRA) {
646 unsigned short result = (unsigned int)read16((uint32_t)address);
647 if (mouse_buttons & 0x01) {
648 return (unsigned int)(result | 0x40);
651 return (unsigned int)result;
653 if (address == POTGOR) {
654 unsigned short result = (unsigned int)read16((uint32_t)address);
655 // bit 1 rmb, bit 2 mmb
656 if (mouse_buttons & 0x06) {
657 return (unsigned int)((result ^ ((mouse_buttons & 0x02) << 9)) // move rmb to bit 10
658 & (result ^ ((mouse_buttons & 0x04) << 6))); // move mmb to bit 8
660 return (unsigned int)(result & 0xfffd);
664 if (address & 0xFF000000)
667 if (address & 0x01) {
668 return ((read8(address) << 8) | read8(address + 1));
670 return (unsigned int)read16((uint32_t)address);
673 unsigned int m68k_read_memory_32(unsigned int address) {
674 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
676 /*if (m68k_get_reg(NULL, M68K_REG_PC) >= 0x080032F0 && m68k_get_reg(NULL, M68K_REG_PC) <= 0x080032F0 + 0x4000) {
677 stop_cpu_emulation(1);
680 /*if (address >= 0xE90000 && address < 0xF00000) {
681 printf("LONGWORD read from DMAC @%.8X:", address);
682 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_LONGWORD);
685 cpu_emulation_running = 0;
689 if (address & 0xFF000000)
692 if (address & 0x01) {
693 uint32_t c = read8(address);
694 c |= (be16toh(read16(address+1)) << 8);
695 c |= (read8(address + 3) << 24);
698 uint16_t a = read16(address);
699 uint16_t b = read16(address + 2);
700 return (a << 16) | b;
703 #define PLATFORM_CHECK_WRITE(a) \
704 if (address >= cfg->custom_low && address < cfg->custom_high) { \
705 switch(cfg->platform->id) { \
706 case PLATFORM_AMIGA: { \
707 if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
708 handle_piscsi_write(address, value, a); \
710 if (address >= PINET_OFFSET && address < PINET_UPPER) { \
711 handle_pinet_write(address, value, a); \
713 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
714 rtg_write((address & 0x0FFFFFFF), value, a); \
717 if (custom_write_amiga(cfg, address, value, a) != -1) { \
726 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
727 if (handle_mapped_write(cfg, address, value, a) != -1) \
731 void m68k_write_memory_8(unsigned int address, unsigned int value) {
732 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
734 /*if (address >= 0xE90000 && address < 0xF00000) {
735 printf("BYTE write to DMAC @%.8X: %.2X\n", address, value);
736 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_BYTE);
738 cpu_emulation_running = 0;
742 if (address == 0xbfe001) {
743 if (ovl != (value & (1 << 0))) {
744 ovl = (value & (1 << 0));
745 printf("OVL:%x\n", ovl);
749 if (address & 0xFF000000)
752 write8((uint32_t)address, value);
756 void m68k_write_memory_16(unsigned int address, unsigned int value) {
757 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
759 /*if (address >= 0xE90000 && address < 0xF00000) {
760 printf("WORD write to DMAC @%.8X: %.4X\n", address, value);
761 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_WORD);
763 cpu_emulation_running = 0;
767 if (address == 0xDFF030) {
768 char *serdat = (char *)&value;
769 // SERDAT word. see amiga dev docs appendix a; upper byte is control codes, and bit 0 is always 1.
770 // ignore this upper byte as it's not viewable data, only display lower byte.
771 printf("%c", serdat[0]);
773 if (address == 0xDFF09A) {
774 if (!(value & 0x8000)) {
779 else if (value & 0x04) {
784 if (address & 0xFF000000)
788 printf("Unaligned WORD write!\n");
790 write16((uint32_t)address, value);
794 void m68k_write_memory_32(unsigned int address, unsigned int value) {
795 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
797 /*if (address >= 0xE90000 && address < 0xF00000) {
798 printf("LONGWORD write to DMAC @%.8X: %.8X\n", address, value);
799 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_LONGWORD);
801 cpu_emulation_running = 0;
805 if (address & 0xFF000000)
809 printf("Unaligned LONGWORD write!\n");
811 write16(address, value >> 16);
812 write16(address + 2, value);