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 "gpio/gpio.h"
28 unsigned char read_ranges;
29 unsigned int read_addr[8];
30 unsigned int read_upper[8];
31 unsigned char *read_data[8];
32 unsigned char write_ranges;
33 unsigned int write_addr[8];
34 unsigned int write_upper[8];
35 unsigned char *write_data[8];
37 int kb_hook_enabled = 0;
38 int mouse_hook_enabled = 0;
39 int cpu_emulation_running = 1;
41 char mouse_dx = 0, mouse_dy = 0;
42 char mouse_buttons = 0;
44 extern volatile unsigned int *gpio;
45 extern volatile uint16_t srdata;
46 extern uint8_t realtime_graphics_debug;
47 uint8_t realtime_disassembly;
49 char disasm_buf[4096];
51 #define KICKBASE 0xF80000
52 #define KICKSIZE 0x7FFFF
54 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
56 int gayle_emulation_enabled = 1;
60 void *iplThread(void *args) {
61 printf("IPL thread running\n");
64 if (!gpio_get_irq()) {
71 if (gayle_emulation_enabled) {
72 if (((gayle_int & 0x80) || gayle_a4k_int) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
73 //get_ide(0)->drive[0].intrq = 0;
86 // Configurable emulator options
87 unsigned int cpu_type = M68K_CPU_TYPE_68000;
88 unsigned int loop_cycles = 300;
89 struct emulator_config *cfg = NULL;
90 char keyboard_file[256] = "/dev/input/event1";
92 //unsigned char g_kick[524288];
93 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
95 static volatile unsigned char maprom;
97 void sigint_handler(int sig_num) {
99 //cpu_emulation_running = 0;
102 printf("Received sigint %d, exiting.\n", sig_num);
108 if (cfg->platform->shutdown) {
109 cfg->platform->shutdown(cfg);
115 int main(int argc, char *argv[]) {
117 const struct sched_param priority = {99};
119 // Some command line switch stuffles
120 for (g = 1; g < argc; g++) {
121 if (strcmp(argv[g], "--disable-gayle") == 0) {
122 gayle_emulation_enabled = 0;
124 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
126 printf("%s switch found, but no CPU type specified.\n", argv[g]);
129 cpu_type = get_m68k_cpu_type(argv[g]);
132 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
134 printf("%s switch found, but no config filename specified.\n", argv[g]);
137 cfg = load_config_file(argv[g]);
140 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
142 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
145 strcpy(keyboard_file, argv[g]);
151 printf("No config file specified. Trying to load default.cfg...\n");
152 cfg = load_config_file("default.cfg");
154 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
155 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
157 printf("Failed to allocate memory for emulator config!\n");
160 memset(cfg, 0x00, sizeof(struct emulator_config));
165 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
166 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
169 cfg->platform = make_platform_config("none", "generic");
170 cfg->platform->platform_initial_setup(cfg);
173 if (cfg->mouse_enabled) {
174 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
175 if (mouse_fd == -1) {
176 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
177 cfg->mouse_enabled = 0;
181 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
182 if (keyboard_fd == -1) {
183 printf("Failed to open keyboard event source.\n");
188 signal(SIGINT, sigint_handler);
191 //goto skip_everything;
193 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
195 printf("Enable 200MHz GPCLK0 on GPIO4\n");
196 gpio_enable_200mhz();
198 // reset cpld statemachine first
206 // reset amiga and statemachine
212 printf("Setting CPU type to %d.\n", cpu_type);
213 m68k_set_cpu_type(cpu_type);
217 m68k_set_reg(M68K_REG_PC, 0xF80002);
219 m68k_set_reg(M68K_REG_PC, 0x0);
222 char c = 0, c_code = 0, c_type = 0;
226 err = pthread_create(&id, NULL, &iplThread, NULL);
228 printf("can't create IPL thread :[%s]", strerror(err));
230 printf("IPL Thread created successfully\n");
234 if (mouse_hook_enabled) {
235 get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons);
238 if (cpu_emulation_running)
239 m68k_execute(loop_cycles);
242 if (realtime_disassembly) {
244 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
245 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
249 unsigned int status = read_reg();
250 m68k_set_irq((status & 0xe000) >> 13);
253 write16(0xdff09c, 0x8000 | (1 << 3));
254 //PAULA_SET_IRQ(3); // IRQ 3 = INT2
261 while (get_key_char(&c, &c_code, &c_type)) {
262 if (c && c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
264 printf("Keyboard hook enabled.\n");
266 else if (kb_hook_enabled) {
267 if (c == 0x1B && c_type) {
269 printf("Keyboard hook disabled.\n");
272 /*printf("Key code: %.2X - ", c_code);
275 printf("released.\n");
278 printf("pressed.\n");
284 printf("unknown.\n");
287 if (queue_keypress(c_code, c_type, cfg->platform->id)) {
293 if (!kb_hook_enabled && c_type) {
294 if (c && c == cfg->mouse_toggle_key) {
295 mouse_hook_enabled ^= 1;
296 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
297 mouse_dx = mouse_dy = mouse_buttons = 0;
300 cpu_emulation_running ^= 1;
301 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
304 realtime_graphics_debug ^= 1;
305 printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
309 //m68k_pulse_reset();
310 printf("CPU emulation reset.\n");
313 printf("Quitting and exiting emulator.\n");
314 goto stop_cpu_emulation;
317 realtime_disassembly ^= 1;
318 printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
323 if (realtime_disassembly)
340 void cpu_pulse_reset(void) {
342 // printf("Status Reg%x\n",read_reg());
345 // printf("Status Reg%x\n",read_reg());
348 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
349 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
354 int cpu_irq_ack(int level) {
355 printf("cpu irq ack\n");
359 static unsigned int target = 0;
360 static uint8_t send_keypress = 0;
362 uint8_t cdtv_dmac_reg_idx_read();
363 void cdtv_dmac_reg_idx_write(uint8_t value);
364 uint32_t cdtv_dmac_read(uint32_t address, uint8_t type);
365 void cdtv_dmac_write(uint32_t address, uint32_t value, uint8_t type);
367 #define PLATFORM_CHECK_READ(a) \
368 if (address >= cfg->custom_low && address < cfg->custom_high) { \
369 unsigned int target = 0; \
370 switch(cfg->platform->id) { \
371 case PLATFORM_AMIGA: { \
372 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
373 return rtg_read((address & 0x0FFFFFFF), a); \
375 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
384 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
385 if (handle_mapped_read(cfg, address, &target, a) != -1) \
389 unsigned int m68k_read_memory_8(unsigned int address) {
390 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
392 if (address >= 0xE90000 && address < 0xF00000) {
393 printf("BYTE read from DMAC @%.8X:", address);
394 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_BYTE);
396 m68k_end_timeslice();
397 cpu_emulation_running = 0;
401 if (mouse_hook_enabled) {
402 if (address == CIAAPRA) {
403 unsigned char result = (unsigned int)read8((uint32_t)address);
404 if (mouse_buttons & 0x01) {
405 //mouse_buttons -= 1;
406 return (unsigned int)(result ^ 0x40);
409 return (unsigned int)result;
412 if (kb_hook_enabled) {
413 unsigned char result = (unsigned int)read8((uint32_t)address);
414 if (address == CIAAICR) {
415 if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
420 if (send_keypress == 2) {
426 if (address == CIAADAT) {
428 uint8_t c = 0, t = 0;
429 pop_queued_key(&c, &t);
431 result = ((c << 1) | t) ^ 0xFF;
438 if (address & 0xFF000000)
441 return read8((uint32_t)address);
444 unsigned int m68k_read_memory_16(unsigned int address) {
445 PLATFORM_CHECK_READ(OP_TYPE_WORD);
447 if (address >= 0xE90000 && address < 0xF00000) {
448 printf("WORD read from DMAC @%.8X:", address);
449 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_WORD);
451 m68k_end_timeslice();
452 cpu_emulation_running = 0;
456 if (mouse_hook_enabled) {
457 if (address == JOY0DAT) {
458 // Forward mouse valueses to Amyga.
459 unsigned short result = (mouse_dy << 8) | (mouse_dx);
460 return (unsigned int)result;
462 /*if (address == CIAAPRA) {
463 unsigned short result = (unsigned int)read16((uint32_t)address);
464 if (mouse_buttons & 0x01) {
465 return (unsigned int)(result | 0x40);
468 return (unsigned int)result;
470 if (address == POTGOR) {
471 unsigned short result = (unsigned int)read16((uint32_t)address);
472 if (mouse_buttons & 0x02) {
473 return (unsigned int)(result ^ (0x2 << 9));
476 return (unsigned int)(result & 0xFFFD);
480 if (address & 0xFF000000)
483 if (address & 0x01) {
484 return ((read8(address) << 8) | read8(address + 1));
486 return (unsigned int)read16((uint32_t)address);
489 unsigned int m68k_read_memory_32(unsigned int address) {
490 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
492 if (address >= 0xE90000 && address < 0xF00000) {
493 printf("LONGWORD read from DMAC @%.8X:", address);
494 uint32_t v = cdtv_dmac_read(address & 0xFFFF, OP_TYPE_LONGWORD);
496 m68k_end_timeslice();
497 cpu_emulation_running = 0;
501 if (address & 0xFF000000)
504 if (address & 0x01) {
505 uint32_t c = read8(address);
506 c |= (be16toh(read16(address+1)) << 8);
507 c |= (read8(address + 3) << 24);
510 uint16_t a = read16(address);
511 uint16_t b = read16(address + 2);
512 return (a << 16) | b;
515 #define PLATFORM_CHECK_WRITE(a) \
516 if (address >= cfg->custom_low && address < cfg->custom_high) { \
517 switch(cfg->platform->id) { \
518 case PLATFORM_AMIGA: { \
519 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
520 rtg_write((address & 0x0FFFFFFF), value, a); \
523 if (custom_write_amiga(cfg, address, value, a) != -1) { \
532 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
533 if (handle_mapped_write(cfg, address, value, a) != -1) \
537 void m68k_write_memory_8(unsigned int address, unsigned int value) {
538 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
540 if (address >= 0xE90000 && address < 0xF00000) {
541 printf("BYTE write to DMAC @%.8X: %.2X\n", address, value);
542 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_BYTE);
543 m68k_end_timeslice();
544 cpu_emulation_running = 0;
548 if (address == 0xbfe001) {
549 if (ovl != (value & (1 << 0))) {
550 ovl = (value & (1 << 0));
551 printf("OVL:%x\n", ovl);
555 if (address & 0xFF000000)
558 write8((uint32_t)address, value);
562 void m68k_write_memory_16(unsigned int address, unsigned int value) {
563 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
565 if (address >= 0xE90000 && address < 0xF00000) {
566 printf("WORD write to DMAC @%.8X: %.4X\n", address, value);
567 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_WORD);
568 m68k_end_timeslice();
569 cpu_emulation_running = 0;
573 if (address & 0xFF000000)
577 printf("Unaligned WORD write!\n");
579 write16((uint32_t)address, value);
583 void m68k_write_memory_32(unsigned int address, unsigned int value) {
584 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
586 if (address >= 0xE90000 && address < 0xF00000) {
587 printf("LONGWORD write to DMAC @%.8X: %.8X\n", address, value);
588 cdtv_dmac_write(address & 0xFFFF, value, OP_TYPE_LONGWORD);
589 m68k_end_timeslice();
590 cpu_emulation_running = 0;
594 if (address & 0xFF000000)
598 printf("Unaligned LONGWORD write!\n");
600 write16(address, value >> 16);
601 write16(address + 2, value);