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;
53 char disasm_buf[4096];
55 #define KICKBASE 0xF80000
56 #define KICKSIZE 0x7FFFF
58 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
60 int gayle_emulation_enabled = 1;
64 void *iplThread(void *args) {
65 printf("IPL thread running\n");
68 if (!gpio_get_irq()) {
75 if (gayle_emulation_enabled) {
76 if (((gayle_int & 0x80) || gayle_a4k_int) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
77 //get_ide(0)->drive[0].intrq = 0;
90 // Configurable emulator options
91 unsigned int cpu_type = M68K_CPU_TYPE_68000;
92 unsigned int loop_cycles = 300;
93 struct emulator_config *cfg = NULL;
94 char keyboard_file[256] = "/dev/input/event1";
96 //unsigned char g_kick[524288];
97 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
99 static volatile unsigned char maprom;
101 void sigint_handler(int sig_num) {
103 //cpu_emulation_running = 0;
106 printf("Received sigint %d, exiting.\n", sig_num);
112 if (cfg->platform->shutdown) {
113 cfg->platform->shutdown(cfg);
119 int main(int argc, char *argv[]) {
121 const struct sched_param priority = {99};
123 // Some command line switch stuffles
124 for (g = 1; g < argc; g++) {
125 if (strcmp(argv[g], "--disable-gayle") == 0) {
126 gayle_emulation_enabled = 0;
128 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
130 printf("%s switch found, but no CPU type specified.\n", argv[g]);
133 cpu_type = get_m68k_cpu_type(argv[g]);
136 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
138 printf("%s switch found, but no config filename specified.\n", argv[g]);
141 cfg = load_config_file(argv[g]);
144 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
146 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
149 strcpy(keyboard_file, argv[g]);
155 printf("No config file specified. Trying to load default.cfg...\n");
156 cfg = load_config_file("default.cfg");
158 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
159 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
161 printf("Failed to allocate memory for emulator config!\n");
164 memset(cfg, 0x00, sizeof(struct emulator_config));
169 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
170 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
173 cfg->platform = make_platform_config("none", "generic");
174 cfg->platform->platform_initial_setup(cfg);
177 if (cfg->mouse_enabled) {
178 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
179 if (mouse_fd == -1) {
180 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
181 cfg->mouse_enabled = 0;
185 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
186 if (keyboard_fd == -1) {
187 printf("Failed to open keyboard event source.\n");
192 signal(SIGINT, sigint_handler);
195 //goto skip_everything;
197 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
199 printf("Enable 200MHz GPCLK0 on GPIO4\n");
200 gpio_enable_200mhz();
202 // reset cpld statemachine first
210 // reset amiga and statemachine
216 printf("Setting CPU type to %d.\n", cpu_type);
217 m68k_set_cpu_type(cpu_type);
221 m68k_set_reg(M68K_REG_PC, 0xF80002);
223 m68k_set_reg(M68K_REG_PC, 0x0);
226 char c = 0, c_code = 0, c_type = 0;
230 err = pthread_create(&id, NULL, &iplThread, NULL);
232 printf("can't create IPL thread :[%s]", strerror(err));
234 printf("IPL Thread created successfully\n");
238 if (mouse_hook_enabled) {
239 get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons);
242 if (cpu_emulation_running)
243 m68k_execute(loop_cycles);
246 if (realtime_disassembly) {
248 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
249 /*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), \
250 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));
251 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), \
252 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));*/
253 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
257 unsigned int status = read_reg();
258 m68k_set_irq((status & 0xe000) >> 13);
261 write16(0xdff09c, 0x8000 | (1 << 3));
262 //PAULA_SET_IRQ(3); // IRQ 3 = INT2
269 while (get_key_char(&c, &c_code, &c_type)) {
270 if (c && c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
272 printf("Keyboard hook enabled.\n");
274 else if (kb_hook_enabled) {
275 if (c == 0x1B && c_type) {
277 printf("Keyboard hook disabled.\n");
280 /*printf("Key code: %.2X - ", c_code);
283 printf("released.\n");
286 printf("pressed.\n");
292 printf("unknown.\n");
295 if (queue_keypress(c_code, c_type, cfg->platform->id)) {
301 if (!kb_hook_enabled && c_type) {
302 if (c && c == cfg->mouse_toggle_key) {
303 mouse_hook_enabled ^= 1;
304 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
305 mouse_dx = mouse_dy = mouse_buttons = 0;
308 cpu_emulation_running ^= 1;
309 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
312 realtime_graphics_debug ^= 1;
313 printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
317 //m68k_pulse_reset();
318 printf("CPU emulation reset.\n");
321 printf("Quitting and exiting emulator.\n");
322 goto stop_cpu_emulation;
325 realtime_disassembly ^= 1;
326 printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
331 if (realtime_disassembly)
348 void cpu_pulse_reset(void) {
350 // printf("Status Reg%x\n",read_reg());
353 // printf("Status Reg%x\n",read_reg());
356 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
357 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
362 int cpu_irq_ack(int level) {
363 printf("cpu irq ack\n");
367 static unsigned int target = 0;
368 static uint8_t send_keypress = 0;
370 uint8_t cdtv_dmac_reg_idx_read();
371 void cdtv_dmac_reg_idx_write(uint8_t value);
372 uint32_t cdtv_dmac_read(uint32_t address, uint8_t type);
373 void cdtv_dmac_write(uint32_t address, uint32_t value, uint8_t type);
375 #define PLATFORM_CHECK_READ(a) \
376 if (address >= cfg->custom_low && address < cfg->custom_high) { \
377 unsigned int target = 0; \
378 switch(cfg->platform->id) { \
379 case PLATFORM_AMIGA: { \
380 if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
381 return handle_piscsi_read(address, a); \
383 if (address >= PINET_OFFSET && address < PINET_UPPER) { \
384 return handle_pinet_read(address, a); \
386 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
387 return rtg_read((address & 0x0FFFFFFF), a); \
389 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
398 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
399 if (handle_mapped_read(cfg, address, &target, a) != -1) \
403 unsigned int m68k_read_memory_8(unsigned int address) {
404 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
406 /*if (address >= 0xE90000 && address < 0xF00000) {
407 printf("BYTE read from DMAC @%.8X:", address);
408 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_BYTE);
410 m68k_end_timeslice();
411 cpu_emulation_running = 0;
415 if (mouse_hook_enabled) {
416 if (address == CIAAPRA) {
417 unsigned char result = (unsigned int)read8((uint32_t)address);
418 if (mouse_buttons & 0x01) {
419 //mouse_buttons -= 1;
420 return (unsigned int)(result ^ 0x40);
423 return (unsigned int)result;
426 if (kb_hook_enabled) {
427 unsigned char result = (unsigned int)read8((uint32_t)address);
428 if (address == CIAAICR) {
429 if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
434 if (send_keypress == 2) {
440 if (address == CIAADAT) {
442 uint8_t c = 0, t = 0;
443 pop_queued_key(&c, &t);
445 result = ((c << 1) | t) ^ 0xFF;
452 if (address & 0xFF000000)
455 return read8((uint32_t)address);
458 unsigned int m68k_read_memory_16(unsigned int address) {
459 PLATFORM_CHECK_READ(OP_TYPE_WORD);
461 /*if (address >= 0xE90000 && address < 0xF00000) {
462 printf("WORD read from DMAC @%.8X:", address);
463 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_WORD);
465 m68k_end_timeslice();
466 cpu_emulation_running = 0;
470 if (mouse_hook_enabled) {
471 if (address == JOY0DAT) {
472 // Forward mouse valueses to Amyga.
473 unsigned short result = (mouse_dy << 8) | (mouse_dx);
474 return (unsigned int)result;
476 /*if (address == CIAAPRA) {
477 unsigned short result = (unsigned int)read16((uint32_t)address);
478 if (mouse_buttons & 0x01) {
479 return (unsigned int)(result | 0x40);
482 return (unsigned int)result;
484 if (address == POTGOR) {
485 unsigned short result = (unsigned int)read16((uint32_t)address);
486 if (mouse_buttons & 0x02) {
487 return (unsigned int)(result ^ (0x2 << 9));
490 return (unsigned int)(result & 0xFFFD);
494 if (address & 0xFF000000)
497 if (address & 0x01) {
498 return ((read8(address) << 8) | read8(address + 1));
500 return (unsigned int)read16((uint32_t)address);
503 unsigned int m68k_read_memory_32(unsigned int address) {
504 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
506 /*if (address >= 0xE90000 && address < 0xF00000) {
507 printf("LONGWORD read from DMAC @%.8X:", address);
508 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_LONGWORD);
510 m68k_end_timeslice();
511 cpu_emulation_running = 0;
515 if (address & 0xFF000000)
518 if (address & 0x01) {
519 uint32_t c = read8(address);
520 c |= (be16toh(read16(address+1)) << 8);
521 c |= (read8(address + 3) << 24);
524 uint16_t a = read16(address);
525 uint16_t b = read16(address + 2);
526 return (a << 16) | b;
529 #define PLATFORM_CHECK_WRITE(a) \
530 if (address >= cfg->custom_low && address < cfg->custom_high) { \
531 switch(cfg->platform->id) { \
532 case PLATFORM_AMIGA: { \
533 if (address >= PISCSI_OFFSET && address < PISCSI_UPPER) { \
534 handle_piscsi_write(address, value, a); \
536 if (address >= PINET_OFFSET && address < PINET_UPPER) { \
537 handle_pinet_write(address, value, a); \
539 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
540 rtg_write((address & 0x0FFFFFFF), value, a); \
543 if (custom_write_amiga(cfg, address, value, a) != -1) { \
552 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
553 if (handle_mapped_write(cfg, address, value, a) != -1) \
557 void m68k_write_memory_8(unsigned int address, unsigned int value) {
558 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
560 /*if (address >= 0xE90000 && address < 0xF00000) {
561 printf("BYTE write to DMAC @%.8X: %.2X\n", address, value);
562 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_BYTE);
563 m68k_end_timeslice();
564 cpu_emulation_running = 0;
568 if (address == 0xbfe001) {
569 if (ovl != (value & (1 << 0))) {
570 ovl = (value & (1 << 0));
571 printf("OVL:%x\n", ovl);
575 if (address & 0xFF000000)
578 write8((uint32_t)address, value);
582 void m68k_write_memory_16(unsigned int address, unsigned int value) {
583 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
585 /*if (address >= 0xE90000 && address < 0xF00000) {
586 printf("WORD write to DMAC @%.8X: %.4X\n", address, value);
587 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_WORD);
588 m68k_end_timeslice();
589 cpu_emulation_running = 0;
593 if (address == 0xDFF030) {
594 char *beb = (char *)&value;
595 printf("%c%c", beb[1], beb[0]);
598 if (address & 0xFF000000)
602 printf("Unaligned WORD write!\n");
604 write16((uint32_t)address, value);
608 void m68k_write_memory_32(unsigned int address, unsigned int value) {
609 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
611 /*if (address >= 0xE90000 && address < 0xF00000) {
612 printf("LONGWORD write to DMAC @%.8X: %.8X\n", address, value);
613 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_LONGWORD);
614 m68k_end_timeslice();
615 cpu_emulation_running = 0;
619 if (address & 0xFF000000)
623 printf("Unaligned LONGWORD write!\n");
625 write16(address, value >> 16);
626 write16(address + 2, value);