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) && get_ide(0)->drive->intrq) {
85 // Configurable emulator options
86 unsigned int cpu_type = M68K_CPU_TYPE_68000;
87 unsigned int loop_cycles = 300;
88 struct emulator_config *cfg = NULL;
89 char keyboard_file[256] = "/dev/input/event1";
91 //unsigned char g_kick[524288];
92 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
94 static volatile unsigned char maprom;
96 void sigint_handler(int sig_num) {
98 //cpu_emulation_running = 0;
101 printf("Received sigint %d, exiting.\n", sig_num);
107 if (cfg->platform->shutdown) {
108 cfg->platform->shutdown(cfg);
114 int main(int argc, char *argv[]) {
116 const struct sched_param priority = {99};
118 // Some command line switch stuffles
119 for (g = 1; g < argc; g++) {
120 if (strcmp(argv[g], "--disable-gayle") == 0) {
121 gayle_emulation_enabled = 0;
123 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
125 printf("%s switch found, but no CPU type specified.\n", argv[g]);
128 cpu_type = get_m68k_cpu_type(argv[g]);
131 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
133 printf("%s switch found, but no config filename specified.\n", argv[g]);
136 cfg = load_config_file(argv[g]);
139 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
141 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
144 strcpy(keyboard_file, argv[g]);
150 printf("No config file specified. Trying to load default.cfg...\n");
151 cfg = load_config_file("default.cfg");
153 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
154 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
156 printf("Failed to allocate memory for emulator config!\n");
159 memset(cfg, 0x00, sizeof(struct emulator_config));
164 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
165 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
168 cfg->platform = make_platform_config("none", "generic");
169 cfg->platform->platform_initial_setup(cfg);
172 if (cfg->mouse_enabled) {
173 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
174 if (mouse_fd == -1) {
175 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
176 cfg->mouse_enabled = 0;
180 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
181 if (keyboard_fd == -1) {
182 printf("Failed to open keyboard event source.\n");
187 signal(SIGINT, sigint_handler);
190 //goto skip_everything;
192 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
194 printf("Enable 200MHz GPCLK0 on GPIO4\n");
195 gpio_enable_200mhz();
197 // reset cpld statemachine first
205 // reset amiga and statemachine
211 printf("Setting CPU type to %d.\n", cpu_type);
212 m68k_set_cpu_type(cpu_type);
216 m68k_set_reg(M68K_REG_PC, 0xF80002);
218 m68k_set_reg(M68K_REG_PC, 0x0);
221 char c = 0, c_code = 0, c_type = 0;
225 err = pthread_create(&id, NULL, &iplThread, NULL);
227 printf("can't create IPL thread :[%s]", strerror(err));
229 printf("IPL Thread created successfully\n");
233 if (mouse_hook_enabled) {
234 get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons);
237 if (cpu_emulation_running)
238 m68k_execute(loop_cycles);
241 if (realtime_disassembly) {
243 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
244 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
248 unsigned int status = read_reg();
249 m68k_set_irq((status & 0xe000) >> 13);
252 write16(0xdff09c, 0x8000 | (1 << 3));
253 //PAULA_SET_IRQ(3); // IRQ 3 = INT2
261 // FIXME: Rework this to use keyboard events instead.
262 while (get_key_char(&c, &c_code, &c_type)) {
264 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
266 printf("Keyboard hook enabled.\n");
268 else if (kb_hook_enabled) {
269 if (c == 0x1B && c_type) {
271 printf("Keyboard hook disabled.\n");
274 /*printf("Key code: %.2X - ", c_code);
277 printf("released.\n");
280 printf("pressed.\n");
286 printf("unknown.\n");
289 if (queue_keypress(c_code, c_type, cfg->platform->id)) {
295 if (!kb_hook_enabled && c_type) {
296 if (c == cfg->mouse_toggle_key) {
297 mouse_hook_enabled ^= 1;
298 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
299 mouse_dx = mouse_dy = mouse_buttons = 0;
302 cpu_emulation_running ^= 1;
303 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
306 realtime_graphics_debug ^= 1;
307 printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
312 printf("CPU emulation reset.\n");
315 printf("Quitting and exiting emulator.\n");
316 goto stop_cpu_emulation;
319 realtime_disassembly ^= 1;
320 printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
325 if (realtime_disassembly)
342 void cpu_pulse_reset(void) {
344 // printf("Status Reg%x\n",read_reg());
347 // printf("Status Reg%x\n",read_reg());
350 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
351 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
356 int cpu_irq_ack(int level) {
357 printf("cpu irq ack\n");
361 static unsigned int target = 0;
362 static uint8_t send_keypress = 0;
364 #define PLATFORM_CHECK_READ(a) \
365 if (address >= cfg->custom_low && address < cfg->custom_high) { \
366 unsigned int target = 0; \
367 switch(cfg->platform->id) { \
368 case PLATFORM_AMIGA: { \
369 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
370 return rtg_read((address & 0x0FFFFFFF), a); \
372 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
381 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
382 if (handle_mapped_read(cfg, address, &target, a) != -1) \
386 unsigned int m68k_read_memory_8(unsigned int address) {
387 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
389 if (mouse_hook_enabled) {
390 if (address == CIAAPRA) {
391 unsigned char result = (unsigned int)read8((uint32_t)address);
392 if (mouse_buttons & 0x01) {
393 //mouse_buttons -= 1;
394 return (unsigned int)(result ^ 0x40);
397 return (unsigned int)result;
400 if (kb_hook_enabled) {
401 unsigned char result = (unsigned int)read8((uint32_t)address);
402 if (address == CIAAICR) {
403 if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
408 if (send_keypress == 2) {
414 if (address == CIAADAT) {
416 uint8_t c = 0, t = 0;
417 pop_queued_key(&c, &t);
419 result = ((c << 1) | t) ^ 0xFF;
427 return read8((uint32_t)address);
430 unsigned int m68k_read_memory_16(unsigned int address) {
431 PLATFORM_CHECK_READ(OP_TYPE_WORD);
433 if (mouse_hook_enabled) {
434 if (address == JOY0DAT) {
435 // Forward mouse valueses to Amyga.
436 unsigned short result = (mouse_dy << 8) | (mouse_dx);
437 return (unsigned int)result;
439 /*if (address == CIAAPRA) {
440 unsigned short result = (unsigned int)read16((uint32_t)address);
441 if (mouse_buttons & 0x01) {
442 return (unsigned int)(result | 0x40);
445 return (unsigned int)result;
447 if (address == POTGOR) {
448 unsigned short result = (unsigned int)read16((uint32_t)address);
449 if (mouse_buttons & 0x02) {
450 return (unsigned int)(result ^ (0x2 << 9));
453 return (unsigned int)(result & 0xFFFD);
458 if (address & 0x01) {
459 return ((read8(address) << 8) | read8(address + 1));
461 return (unsigned int)read16((uint32_t)address);
464 unsigned int m68k_read_memory_32(unsigned int address) {
465 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
468 if (address & 0x01) {
469 uint32_t c = read8(address);
470 c |= (be16toh(read16(address+1)) << 8);
471 c |= (read8(address + 3) << 24);
474 uint16_t a = read16(address);
475 uint16_t b = read16(address + 2);
476 return (a << 16) | b;
479 #define PLATFORM_CHECK_WRITE(a) \
480 if (address >= cfg->custom_low && address < cfg->custom_high) { \
481 switch(cfg->platform->id) { \
482 case PLATFORM_AMIGA: { \
483 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
484 rtg_write((address & 0x0FFFFFFF), value, a); \
487 if (custom_write_amiga(cfg, address, value, a) != -1) { \
496 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
497 if (handle_mapped_write(cfg, address, value, a) != -1) \
501 void m68k_write_memory_8(unsigned int address, unsigned int value) {
502 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
504 if (address == 0xbfe001) {
505 if (ovl != (value & (1 << 0))) {
506 ovl = (value & (1 << 0));
507 printf("OVL:%x\n", ovl);
512 write8((uint32_t)address, value);
516 void m68k_write_memory_16(unsigned int address, unsigned int value) {
517 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
520 write16((uint32_t)address, value);
524 void m68k_write_memory_32(unsigned int address, unsigned int value) {
525 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
528 write16(address, value >> 16);
529 write16(address + 2, value);