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/piscsi/piscsi.h"
27 #include "platforms/amiga/piscsi/piscsi-enums.h"
28 #include "platforms/amiga/net/pi-net.h"
29 #include "platforms/amiga/net/pi-net-enums.h"
30 #include "gpio/gpio.h"
32 unsigned char read_ranges;
33 unsigned int read_addr[8];
34 unsigned int read_upper[8];
35 unsigned char *read_data[8];
36 unsigned char write_ranges;
37 unsigned int write_addr[8];
38 unsigned int write_upper[8];
39 unsigned char *write_data[8];
41 int kb_hook_enabled = 0;
42 int mouse_hook_enabled = 0;
43 int cpu_emulation_running = 1;
45 char mouse_dx = 0, mouse_dy = 0;
46 char mouse_buttons = 0;
48 extern volatile unsigned int *gpio;
49 extern volatile uint16_t srdata;
50 extern uint8_t realtime_graphics_debug;
51 uint8_t realtime_disassembly;
52 uint32_t do_disasm = 0;
54 char disasm_buf[4096];
56 #define KICKBASE 0xF80000
57 #define KICKSIZE 0x7FFFF
59 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
61 int gayle_emulation_enabled = 1;
65 // Configurable emulator options
66 unsigned int cpu_type = M68K_CPU_TYPE_68000;
67 unsigned int loop_cycles = 300;
68 struct emulator_config *cfg = NULL;
69 char keyboard_file[256] = "/dev/input/event1";
71 void *iplThread(void *args) {
72 printf("IPL thread running\n");
75 if (!gpio_get_irq()) {
82 if (gayle_emulation_enabled) {
83 if (((gayle_int & 0x80) || gayle_a4k_int) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
84 //get_ide(0)->drive[0].intrq = 0;
96 void stop_cpu_emulation(uint8_t disasm_cur) {
99 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
100 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), \
101 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));
102 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), \
103 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));
104 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
105 realtime_disassembly = 1;
108 cpu_emulation_running = 0;
112 //unsigned char g_kick[524288];
113 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
115 static volatile unsigned char maprom;
117 void sigint_handler(int sig_num) {
119 //cpu_emulation_running = 0;
122 printf("Received sigint %d, exiting.\n", sig_num);
128 if (cfg->platform->shutdown) {
129 cfg->platform->shutdown(cfg);
135 int main(int argc, char *argv[]) {
137 const struct sched_param priority = {99};
139 // Some command line switch stuffles
140 for (g = 1; g < argc; g++) {
141 if (strcmp(argv[g], "--disable-gayle") == 0) {
142 gayle_emulation_enabled = 0;
144 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
146 printf("%s switch found, but no CPU type specified.\n", argv[g]);
149 cpu_type = get_m68k_cpu_type(argv[g]);
152 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
154 printf("%s switch found, but no config filename specified.\n", argv[g]);
157 cfg = load_config_file(argv[g]);
160 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
162 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
165 strcpy(keyboard_file, argv[g]);
171 printf("No config file specified. Trying to load default.cfg...\n");
172 cfg = load_config_file("default.cfg");
174 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
175 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
177 printf("Failed to allocate memory for emulator config!\n");
180 memset(cfg, 0x00, sizeof(struct emulator_config));
185 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
186 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
189 cfg->platform = make_platform_config("none", "generic");
190 cfg->platform->platform_initial_setup(cfg);
193 if (cfg->mouse_enabled) {
194 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
195 if (mouse_fd == -1) {
196 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
197 cfg->mouse_enabled = 0;
201 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
202 if (keyboard_fd == -1) {
203 printf("Failed to open keyboard event source.\n");
208 signal(SIGINT, sigint_handler);
211 //goto skip_everything;
213 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
215 printf("Enable 200MHz GPCLK0 on GPIO4\n");
216 gpio_enable_200mhz();
218 // reset cpld statemachine first
226 // reset amiga and statemachine
232 printf("Setting CPU type to %d.\n", cpu_type);
233 m68k_set_cpu_type(cpu_type);
237 m68k_set_reg(M68K_REG_PC, 0xF80002);
239 m68k_set_reg(M68K_REG_PC, 0x0);
242 char c = 0, c_code = 0, c_type = 0;
246 err = pthread_create(&id, NULL, &iplThread, NULL);
248 printf("can't create IPL thread :[%s]", strerror(err));
250 printf("IPL Thread created successfully\n");
254 if (mouse_hook_enabled) {
255 get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons);
258 if (realtime_disassembly && (do_disasm || cpu_emulation_running)) {
259 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
260 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), \
261 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));
262 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), \
263 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));
264 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
270 if (cpu_emulation_running)
271 m68k_execute(loop_cycles);
275 unsigned int status = read_reg();
276 m68k_set_irq((status & 0xe000) >> 13);
279 write16(0xdff09c, 0x8000 | (1 << 3));
280 //PAULA_SET_IRQ(3); // IRQ 3 = INT2
287 while (get_key_char(&c, &c_code, &c_type)) {
288 if (c && c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
290 printf("Keyboard hook enabled.\n");
292 else if (kb_hook_enabled) {
293 if (c == 0x1B && c_type) {
295 printf("Keyboard hook disabled.\n");
298 /*printf("Key code: %.2X - ", c_code);
301 printf("released.\n");
304 printf("pressed.\n");
310 printf("unknown.\n");
313 if (queue_keypress(c_code, c_type, cfg->platform->id)) {
319 if (!kb_hook_enabled && c_type) {
320 if (c && c == cfg->mouse_toggle_key) {
321 mouse_hook_enabled ^= 1;
322 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
323 mouse_dx = mouse_dy = mouse_buttons = 0;
326 cpu_emulation_running ^= 1;
327 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
330 realtime_graphics_debug ^= 1;
331 printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
335 //m68k_pulse_reset();
336 printf("CPU emulation reset.\n");
339 printf("Quitting and exiting emulator.\n");
340 goto stop_cpu_emulation;
343 realtime_disassembly ^= 1;
345 printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
347 if (c == 's' && realtime_disassembly) {
350 if (c == 'S' && realtime_disassembly) {
370 void cpu_pulse_reset(void) {
372 // printf("Status Reg%x\n",read_reg());
375 // printf("Status Reg%x\n",read_reg());
376 if (cfg->platform->handle_reset)
377 cfg->platform->handle_reset(cfg);
380 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
381 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
386 int cpu_irq_ack(int level) {
387 printf("cpu irq ack\n");
391 static unsigned int target = 0;
392 static uint8_t send_keypress = 0;
394 uint8_t cdtv_dmac_reg_idx_read();
395 void cdtv_dmac_reg_idx_write(uint8_t value);
396 uint32_t cdtv_dmac_read(uint32_t address, uint8_t type);
397 void cdtv_dmac_write(uint32_t address, uint32_t value, uint8_t type);
399 #define PLATFORM_CHECK_READ(a) \
400 if (address >= cfg->custom_low && address < cfg->custom_high) { \
401 unsigned int target = 0; \
402 switch(cfg->platform->id) { \
403 case PLATFORM_AMIGA: { \
404 if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
405 return handle_piscsi_read(address, a); \
407 if (address >= PINET_OFFSET && address < PINET_UPPER) { \
408 return handle_pinet_read(address, a); \
410 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
411 return rtg_read((address & 0x0FFFFFFF), a); \
413 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
422 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
423 if (handle_mapped_read(cfg, address, &target, a) != -1) \
427 unsigned int m68k_read_memory_8(unsigned int address) {
428 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
430 /*if (address >= 0xE90000 && address < 0xF00000) {
431 printf("BYTE read from DMAC @%.8X:", address);
432 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_BYTE);
434 m68k_end_timeslice();
435 cpu_emulation_running = 0;
439 if (mouse_hook_enabled) {
440 if (address == CIAAPRA) {
441 unsigned char result = (unsigned int)read8((uint32_t)address);
442 if (mouse_buttons & 0x01) {
443 //mouse_buttons -= 1;
444 return (unsigned int)(result ^ 0x40);
447 return (unsigned int)result;
450 if (kb_hook_enabled) {
451 unsigned char result = (unsigned int)read8((uint32_t)address);
452 if (address == CIAAICR) {
453 if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
458 if (send_keypress == 2) {
464 if (address == CIAADAT) {
466 uint8_t c = 0, t = 0;
467 pop_queued_key(&c, &t);
469 result = ((c << 1) | t) ^ 0xFF;
476 if (address & 0xFF000000)
479 return read8((uint32_t)address);
482 unsigned int m68k_read_memory_16(unsigned int address) {
483 PLATFORM_CHECK_READ(OP_TYPE_WORD);
485 /*if (address >= 0xE90000 && address < 0xF00000) {
486 printf("WORD read from DMAC @%.8X:", address);
487 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_WORD);
489 m68k_end_timeslice();
490 cpu_emulation_running = 0;
494 if (mouse_hook_enabled) {
495 if (address == JOY0DAT) {
496 // Forward mouse valueses to Amyga.
497 unsigned short result = (mouse_dy << 8) | (mouse_dx);
498 return (unsigned int)result;
500 /*if (address == CIAAPRA) {
501 unsigned short result = (unsigned int)read16((uint32_t)address);
502 if (mouse_buttons & 0x01) {
503 return (unsigned int)(result | 0x40);
506 return (unsigned int)result;
508 if (address == POTGOR) {
509 unsigned short result = (unsigned int)read16((uint32_t)address);
510 if (mouse_buttons & 0x02) {
511 return (unsigned int)(result ^ (0x2 << 9));
514 return (unsigned int)(result & 0xFFFD);
518 if (address & 0xFF000000)
521 if (address & 0x01) {
522 return ((read8(address) << 8) | read8(address + 1));
524 return (unsigned int)read16((uint32_t)address);
527 unsigned int m68k_read_memory_32(unsigned int address) {
528 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
530 /*if (address >= 0xE90000 && address < 0xF00000) {
531 printf("LONGWORD read from DMAC @%.8X:", address);
532 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_LONGWORD);
534 m68k_end_timeslice();
535 cpu_emulation_running = 0;
539 if (address & 0xFF000000)
542 if (address & 0x01) {
543 uint32_t c = read8(address);
544 c |= (be16toh(read16(address+1)) << 8);
545 c |= (read8(address + 3) << 24);
548 uint16_t a = read16(address);
549 uint16_t b = read16(address + 2);
550 return (a << 16) | b;
553 #define PLATFORM_CHECK_WRITE(a) \
554 if (address >= cfg->custom_low && address < cfg->custom_high) { \
555 switch(cfg->platform->id) { \
556 case PLATFORM_AMIGA: { \
557 if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
558 handle_piscsi_write(address, value, a); \
560 if (address >= PINET_OFFSET && address < PINET_UPPER) { \
561 handle_pinet_write(address, value, a); \
563 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
564 rtg_write((address & 0x0FFFFFFF), value, a); \
567 if (custom_write_amiga(cfg, address, value, a) != -1) { \
576 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
577 if (handle_mapped_write(cfg, address, value, a) != -1) \
581 void m68k_write_memory_8(unsigned int address, unsigned int value) {
582 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
584 /*if (address >= 0xE90000 && address < 0xF00000) {
585 printf("BYTE write to DMAC @%.8X: %.2X\n", address, value);
586 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_BYTE);
587 m68k_end_timeslice();
588 cpu_emulation_running = 0;
592 if (address == 0xbfe001) {
593 if (ovl != (value & (1 << 0))) {
594 ovl = (value & (1 << 0));
595 printf("OVL:%x\n", ovl);
599 if (address & 0xFF000000)
602 write8((uint32_t)address, value);
606 void m68k_write_memory_16(unsigned int address, unsigned int value) {
607 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
609 /*if (address >= 0xE90000 && address < 0xF00000) {
610 printf("WORD write to DMAC @%.8X: %.4X\n", address, value);
611 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_WORD);
612 m68k_end_timeslice();
613 cpu_emulation_running = 0;
617 if (address == 0xDFF030) {
618 char *beb = (char *)&value;
619 printf("%c%c", beb[1], beb[0]);
622 if (address & 0xFF000000)
626 printf("Unaligned WORD write!\n");
628 write16((uint32_t)address, value);
632 void m68k_write_memory_32(unsigned int address, unsigned int value) {
633 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
635 /*if (address >= 0xE90000 && address < 0xF00000) {
636 printf("LONGWORD write to DMAC @%.8X: %.8X\n", address, value);
637 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_LONGWORD);
638 m68k_end_timeslice();
639 cpu_emulation_running = 0;
643 if (address & 0xFF000000)
647 printf("Unaligned LONGWORD write!\n");
649 write16(address, value >> 16);
650 write16(address + 2, value);