14 #include <sys/types.h>
16 #include <sys/ioctl.h>
19 #include "platforms/platforms.h"
20 #include "input/input.h"
21 #include "gpio/gpio.h"
23 #include "platforms/amiga/Gayle.h"
24 #include "platforms/amiga/gayle-ide/ide.h"
25 #include "platforms/amiga/amiga-registers.h"
27 int kb_hook_enabled = 0;
28 int mouse_hook_enabled = 0;
29 int cpu_emulation_running = 1;
31 char mouse_dx = 0, mouse_dy = 0;
32 char mouse_buttons = 0;
34 #define KICKBASE 0xF80000
35 #define KICKSIZE 0x7FFFF
37 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
39 int gayle_emulation_enabled = 1;
41 // Configurable emulator options
42 unsigned int cpu_type = M68K_CPU_TYPE_68000;
43 unsigned int loop_cycles = 300;
44 struct emulator_config *cfg = NULL;
45 char keyboard_file[256] = "/dev/input/event0";
47 //unsigned char g_kick[524288];
48 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
50 static volatile unsigned char maprom;
52 void sigint_handler(int sig_num) {
54 //cpu_emulation_running = 0;
57 printf("Received sigint %d, exiting.\n", sig_num);
63 if (cfg->platform->shutdown) {
64 cfg->platform->shutdown(cfg);
70 int main(int argc, char *argv[]) {
72 const struct sched_param priority = {99};
74 // Some command line switch stuffles
75 for (g = 1; g < argc; g++) {
76 if (strcmp(argv[g], "--disable-gayle") == 0) {
77 gayle_emulation_enabled = 0;
79 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
81 printf("%s switch found, but no CPU type specified.\n", argv[g]);
84 cpu_type = get_m68k_cpu_type(argv[g]);
87 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
89 printf("%s switch found, but no config filename specified.\n", argv[g]);
92 cfg = load_config_file(argv[g]);
95 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
97 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
100 strcpy(keyboard_file, argv[g]);
106 printf("No config file specified. Trying to load default.cfg...\n");
107 cfg = load_config_file("default.cfg");
109 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
110 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
112 printf("Failed to allocate memory for emulator config!\n");
115 memset(cfg, 0x00, sizeof(struct emulator_config));
120 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
121 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
124 cfg->platform = make_platform_config("none", "generic");
125 cfg->platform->platform_initial_setup(cfg);
128 if (cfg->mouse_enabled) {
129 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
130 if (mouse_fd == -1) {
131 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
132 cfg->mouse_enabled = 0;
136 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
137 if (keyboard_fd == -1) {
138 printf("Failed to open keyboard event source.\n");
141 sched_setscheduler(0, SCHED_FIFO, &priority);
142 mlockall(MCL_CURRENT); // lock in memory to keep us from paging out
146 signal(SIGINT, sigint_handler);
149 //goto skip_everything;
151 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
153 printf("Enable 200MHz GPCLK0 on GPIO4\n");
154 gpio_enable_200mhz();
156 // reset cpld statemachine first
164 // reset amiga and statemachine
168 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
169 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
174 printf("Setting CPU type to %d.\n", cpu_type);
175 m68k_set_cpu_type(cpu_type);
179 m68k_set_reg(M68K_REG_PC, 0xF80002);
181 m68k_set_reg(M68K_REG_PC, 0x0);
187 err = pthread_create(&id, NULL, &iplThread, NULL);
189 printf("\ncan't create IPL thread :[%s]", strerror(err));
191 printf("\n IPL Thread created successfully\n");
197 if (mouse_hook_enabled) {
198 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
199 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
203 if (cpu_emulation_running)
204 m68k_execute(loop_cycles);
206 // FIXME: Rework this to use keyboard events instead.
207 /*while (get_key_char(&c)) {
208 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
210 printf("Keyboard hook enabled.\n");
212 else if (c == 0x1B && kb_hook_enabled) {
214 printf("Keyboard hook disabled.\n");
216 if (!kb_hook_enabled) {
217 if (c == cfg->mouse_toggle_key) {
218 mouse_hook_enabled ^= 1;
219 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
220 mouse_dx = mouse_dy = mouse_buttons = 0;
223 cpu_emulation_running ^= 1;
224 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
229 printf("CPU emulation reset.\n");
232 printf("Quitting and exiting emulator.\n");
233 goto stop_cpu_emulation;
240 m68k_set_irq((srdata >> 13) & 0xff);
260 void cpu_pulse_reset(void) {
262 // printf("Status Reg%x\n",read_reg());
265 // printf("Status Reg%x\n",read_reg());
268 int cpu_irq_ack(int level) {
269 printf("cpu irq ack\n");
273 static unsigned int target = 0;
275 #define PLATFORM_CHECK_READ(a) \
276 if (address >= cfg->custom_low && address < cfg->custom_high) { \
277 unsigned int target = 0; \
278 switch(cfg->platform->id) { \
279 case PLATFORM_AMIGA: { \
280 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
289 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
290 if (handle_mapped_read(cfg, address, &target, a) != -1) \
294 unsigned int m68k_read_memory_8(unsigned int address) {
295 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
298 return read8((uint32_t)address);
301 unsigned int m68k_read_memory_16(unsigned int address) {
302 PLATFORM_CHECK_READ(OP_TYPE_WORD);
304 if (mouse_hook_enabled) {
305 if (address == JOY0DAT) {
306 // Forward mouse valueses to Amyga.
307 unsigned short result = (mouse_dy << 8) | (mouse_dx);
308 mouse_dx = mouse_dy = 0;
309 return (unsigned int)result;
311 if (address == CIAAPRA) {
312 unsigned short result = (unsigned int)read16((uint32_t)address);
313 if (mouse_buttons & 0x01) {
315 return (unsigned int)(result | 0x40);
318 return (unsigned int)result;
320 if (address == POTGOR) {
321 unsigned short result = (unsigned int)read16((uint32_t)address);
322 if (mouse_buttons & 0x02) {
324 return (unsigned int)(result | 0x2);
327 return (unsigned int)result;
333 return (unsigned int)read16((uint32_t)address);
336 unsigned int m68k_read_memory_32(unsigned int address) {
337 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
340 uint16_t a = read16(address);
341 uint16_t b = read16(address + 2);
342 return (a << 16) | b;
345 #define PLATFORM_CHECK_WRITE(a) \
346 if (address >= cfg->custom_low && address < cfg->custom_high) { \
347 switch(cfg->platform->id) { \
348 case PLATFORM_AMIGA: { \
349 if (custom_write_amiga(cfg, address, value, a) != -1) { \
358 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
359 if (handle_mapped_write(cfg, address, value, a) != -1) \
363 void m68k_write_memory_8(unsigned int address, unsigned int value) {
364 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
366 if (address == 0xbfe001) {
367 if (ovl != (value & (1 << 0))) {
368 ovl = (value & (1 << 0));
369 printf("OVL:%x\n", ovl);
374 write8((uint32_t)address, value);
378 void m68k_write_memory_16(unsigned int address, unsigned int value) {
379 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
382 write16((uint32_t)address, value);
386 void m68k_write_memory_32(unsigned int address, unsigned int value) {
387 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
390 write16(address, value >> 16);
391 write16(address + 2, value);