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);
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);
250 //printf("There was an IRQ: %d\n", (status & 0xe000) >> 13);
253 write16(0xdff09c, 0x8000 | (1 << 3));
254 //PAULA_SET_IRQ(3); // IRQ 3 = INT2
262 // FIXME: Rework this to use keyboard events instead.
263 while (get_key_char(&c)) {
264 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
266 printf("Keyboard hook enabled.\n");
268 else if (c == 0x1B && kb_hook_enabled) {
270 printf("Keyboard hook disabled.\n");
272 if (!kb_hook_enabled) {
273 if (c == cfg->mouse_toggle_key) {
274 mouse_hook_enabled ^= 1;
275 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
276 mouse_dx = mouse_dy = mouse_buttons = 0;
279 cpu_emulation_running ^= 1;
280 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
283 realtime_graphics_debug ^= 1;
284 printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
289 printf("CPU emulation reset.\n");
292 printf("Quitting and exiting emulator.\n");
293 goto stop_cpu_emulation;
296 realtime_disassembly ^= 1;
297 printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
302 if (realtime_disassembly)
319 void cpu_pulse_reset(void) {
321 // printf("Status Reg%x\n",read_reg());
324 // printf("Status Reg%x\n",read_reg());
327 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
328 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
333 int cpu_irq_ack(int level) {
334 printf("cpu irq ack\n");
338 static unsigned int target = 0;
340 #define PLATFORM_CHECK_READ(a) \
341 if (address >= cfg->custom_low && address < cfg->custom_high) { \
342 unsigned int target = 0; \
343 switch(cfg->platform->id) { \
344 case PLATFORM_AMIGA: { \
345 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
346 return rtg_read((address & 0x0FFFFFFF), a); \
348 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
357 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
358 if (handle_mapped_read(cfg, address, &target, a) != -1) \
362 unsigned int m68k_read_memory_8(unsigned int address) {
363 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
365 if (mouse_hook_enabled) {
366 if (address == CIAAPRA) {
367 unsigned char result = (unsigned int)read8((uint32_t)address);
368 if (mouse_buttons & 0x01) {
369 //mouse_buttons -= 1;
370 return (unsigned int)(result ^ 0x40);
373 return (unsigned int)result;
378 return read8((uint32_t)address);
381 unsigned int m68k_read_memory_16(unsigned int address) {
382 PLATFORM_CHECK_READ(OP_TYPE_WORD);
384 if (mouse_hook_enabled) {
385 if (address == JOY0DAT) {
386 // Forward mouse valueses to Amyga.
387 unsigned short result = (mouse_dy << 8) | (mouse_dx);
388 return (unsigned int)result;
390 /*if (address == CIAAPRA) {
391 unsigned short result = (unsigned int)read16((uint32_t)address);
392 if (mouse_buttons & 0x01) {
393 return (unsigned int)(result | 0x40);
396 return (unsigned int)result;
398 if (address == POTGOR) {
399 unsigned short result = (unsigned int)read16((uint32_t)address);
400 if (mouse_buttons & 0x02) {
401 return (unsigned int)(result ^ (0x2 << 9));
404 return (unsigned int)(result & 0xFFFD);
409 return (unsigned int)read16((uint32_t)address);
412 unsigned int m68k_read_memory_32(unsigned int address) {
413 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
416 uint16_t a = read16(address);
417 uint16_t b = read16(address + 2);
418 return (a << 16) | b;
421 #define PLATFORM_CHECK_WRITE(a) \
422 if (address >= cfg->custom_low && address < cfg->custom_high) { \
423 switch(cfg->platform->id) { \
424 case PLATFORM_AMIGA: { \
425 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
426 rtg_write((address & 0x0FFFFFFF), value, a); \
429 if (custom_write_amiga(cfg, address, value, a) != -1) { \
438 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
439 if (handle_mapped_write(cfg, address, value, a) != -1) \
443 void m68k_write_memory_8(unsigned int address, unsigned int value) {
444 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
446 if (address == 0xbfe001) {
447 if (ovl != (value & (1 << 0))) {
448 ovl = (value & (1 << 0));
449 printf("OVL:%x\n", ovl);
454 write8((uint32_t)address, value);
458 void m68k_write_memory_16(unsigned int address, unsigned int value) {
459 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
462 write16((uint32_t)address, value);
466 void m68k_write_memory_32(unsigned int address, unsigned int value) {
467 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
470 write16(address, value >> 16);
471 write16(address + 2, value);