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 "gpio/gpio.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 extern volatile unsigned int *gpio;
35 extern volatile uint16_t srdata;
37 #define KICKBASE 0xF80000
38 #define KICKSIZE 0x7FFFF
40 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
42 int gayle_emulation_enabled = 1;
44 // Configurable emulator options
45 unsigned int cpu_type = M68K_CPU_TYPE_68000;
46 unsigned int loop_cycles = 300;
47 struct emulator_config *cfg = NULL;
48 char keyboard_file[256] = "/dev/input/event0";
50 //unsigned char g_kick[524288];
51 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
53 static volatile unsigned char maprom;
55 void sigint_handler(int sig_num) {
57 //cpu_emulation_running = 0;
60 printf("Received sigint %d, exiting.\n", sig_num);
66 if (cfg->platform->shutdown) {
67 cfg->platform->shutdown(cfg);
73 int main(int argc, char *argv[]) {
75 const struct sched_param priority = {99};
77 // Some command line switch stuffles
78 for (g = 1; g < argc; g++) {
79 if (strcmp(argv[g], "--disable-gayle") == 0) {
80 gayle_emulation_enabled = 0;
82 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
84 printf("%s switch found, but no CPU type specified.\n", argv[g]);
87 cpu_type = get_m68k_cpu_type(argv[g]);
90 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
92 printf("%s switch found, but no config filename specified.\n", argv[g]);
95 cfg = load_config_file(argv[g]);
98 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
100 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
103 strcpy(keyboard_file, argv[g]);
109 printf("No config file specified. Trying to load default.cfg...\n");
110 cfg = load_config_file("default.cfg");
112 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
113 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
115 printf("Failed to allocate memory for emulator config!\n");
118 memset(cfg, 0x00, sizeof(struct emulator_config));
123 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
124 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
127 cfg->platform = make_platform_config("none", "generic");
128 cfg->platform->platform_initial_setup(cfg);
131 if (cfg->mouse_enabled) {
132 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
133 if (mouse_fd == -1) {
134 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
135 cfg->mouse_enabled = 0;
139 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
140 if (keyboard_fd == -1) {
141 printf("Failed to open keyboard event source.\n");
144 sched_setscheduler(0, SCHED_FIFO, &priority);
145 mlockall(MCL_CURRENT); // lock in memory to keep us from paging out
149 signal(SIGINT, sigint_handler);
152 //goto skip_everything;
154 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
156 printf("Enable 200MHz GPCLK0 on GPIO4\n");
157 gpio_enable_200mhz();
159 // reset cpld statemachine first
167 // reset amiga and statemachine
171 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
172 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
177 printf("Setting CPU type to %d.\n", cpu_type);
178 m68k_set_cpu_type(cpu_type);
182 m68k_set_reg(M68K_REG_PC, 0xF80002);
184 m68k_set_reg(M68K_REG_PC, 0x0);
191 if (mouse_hook_enabled) {
192 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
193 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
197 if (cpu_emulation_running)
198 m68k_execute(loop_cycles);
200 // FIXME: Rework this to use keyboard events instead.
201 /*while (get_key_char(&c)) {
202 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
204 printf("Keyboard hook enabled.\n");
206 else if (c == 0x1B && kb_hook_enabled) {
208 printf("Keyboard hook disabled.\n");
210 if (!kb_hook_enabled) {
211 if (c == cfg->mouse_toggle_key) {
212 mouse_hook_enabled ^= 1;
213 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
214 mouse_dx = mouse_dy = mouse_buttons = 0;
217 cpu_emulation_running ^= 1;
218 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
223 printf("CPU emulation reset.\n");
226 printf("Quitting and exiting emulator.\n");
227 goto stop_cpu_emulation;
246 void cpu_pulse_reset(void) {
248 // printf("Status Reg%x\n",read_reg());
251 // printf("Status Reg%x\n",read_reg());
254 int cpu_irq_ack(int level) {
255 printf("cpu irq ack\n");
259 static unsigned int target = 0;
261 #define PLATFORM_CHECK_READ(a) \
262 if (address >= cfg->custom_low && address < cfg->custom_high) { \
263 unsigned int target = 0; \
264 switch(cfg->platform->id) { \
265 case PLATFORM_AMIGA: { \
266 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
275 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
276 if (handle_mapped_read(cfg, address, &target, a) != -1) \
280 unsigned int m68k_read_memory_8(unsigned int address) {
281 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
284 return read8((uint32_t)address);
287 unsigned int m68k_read_memory_16(unsigned int address) {
288 PLATFORM_CHECK_READ(OP_TYPE_WORD);
290 if (mouse_hook_enabled) {
291 if (address == JOY0DAT) {
292 // Forward mouse valueses to Amyga.
293 unsigned short result = (mouse_dy << 8) | (mouse_dx);
294 mouse_dx = mouse_dy = 0;
295 return (unsigned int)result;
297 if (address == CIAAPRA) {
298 unsigned short result = (unsigned int)read16((uint32_t)address);
299 if (mouse_buttons & 0x01) {
301 return (unsigned int)(result | 0x40);
304 return (unsigned int)result;
306 if (address == POTGOR) {
307 unsigned short result = (unsigned int)read16((uint32_t)address);
308 if (mouse_buttons & 0x02) {
310 return (unsigned int)(result | 0x2);
313 return (unsigned int)result;
319 return (unsigned int)read16((uint32_t)address);
322 unsigned int m68k_read_memory_32(unsigned int address) {
323 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
326 uint16_t a = read16(address);
327 uint16_t b = read16(address + 2);
328 return (a << 16) | b;
331 #define PLATFORM_CHECK_WRITE(a) \
332 if (address >= cfg->custom_low && address < cfg->custom_high) { \
333 switch(cfg->platform->id) { \
334 case PLATFORM_AMIGA: { \
335 if (custom_write_amiga(cfg, address, value, a) != -1) { \
344 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
345 if (handle_mapped_write(cfg, address, value, a) != -1) \
349 void m68k_write_memory_8(unsigned int address, unsigned int value) {
350 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
352 if (address == 0xbfe001) {
353 if (ovl != (value & (1 << 0))) {
354 ovl = (value & (1 << 0));
355 printf("OVL:%x\n", ovl);
360 write8((uint32_t)address, value);
364 void m68k_write_memory_16(unsigned int address, unsigned int value) {
365 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
368 write16((uint32_t)address, value);
372 void m68k_write_memory_32(unsigned int address, unsigned int value) {
373 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
376 write16(address, value >> 16);
377 write16(address + 2, value);