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 int kb_hook_enabled = 0;
29 int mouse_hook_enabled = 0;
30 int cpu_emulation_running = 1;
32 char mouse_dx = 0, mouse_dy = 0;
33 char mouse_buttons = 0;
35 extern volatile unsigned int *gpio;
36 extern volatile uint16_t srdata;
38 #define KICKBASE 0xF80000
39 #define KICKSIZE 0x7FFFF
41 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
43 int gayle_emulation_enabled = 1;
45 // Configurable emulator options
46 unsigned int cpu_type = M68K_CPU_TYPE_68000;
47 unsigned int loop_cycles = 300;
48 struct emulator_config *cfg = NULL;
49 char keyboard_file[256] = "/dev/input/event0";
51 //unsigned char g_kick[524288];
52 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
54 static volatile unsigned char maprom;
56 void sigint_handler(int sig_num) {
58 //cpu_emulation_running = 0;
61 printf("Received sigint %d, exiting.\n", sig_num);
67 if (cfg->platform->shutdown) {
68 cfg->platform->shutdown(cfg);
74 int main(int argc, char *argv[]) {
76 const struct sched_param priority = {99};
78 // Some command line switch stuffles
79 for (g = 1; g < argc; g++) {
80 if (strcmp(argv[g], "--disable-gayle") == 0) {
81 gayle_emulation_enabled = 0;
83 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
85 printf("%s switch found, but no CPU type specified.\n", argv[g]);
88 cpu_type = get_m68k_cpu_type(argv[g]);
91 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
93 printf("%s switch found, but no config filename specified.\n", argv[g]);
96 cfg = load_config_file(argv[g]);
99 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
101 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
104 strcpy(keyboard_file, argv[g]);
110 printf("No config file specified. Trying to load default.cfg...\n");
111 cfg = load_config_file("default.cfg");
113 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
114 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
116 printf("Failed to allocate memory for emulator config!\n");
119 memset(cfg, 0x00, sizeof(struct emulator_config));
124 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
125 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
128 cfg->platform = make_platform_config("none", "generic");
129 cfg->platform->platform_initial_setup(cfg);
132 if (cfg->mouse_enabled) {
133 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
134 if (mouse_fd == -1) {
135 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
136 cfg->mouse_enabled = 0;
140 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
141 if (keyboard_fd == -1) {
142 printf("Failed to open keyboard event source.\n");
145 sched_setscheduler(0, SCHED_FIFO, &priority);
146 mlockall(MCL_CURRENT); // lock in memory to keep us from paging out
150 signal(SIGINT, sigint_handler);
153 //goto skip_everything;
155 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
157 printf("Enable 200MHz GPCLK0 on GPIO4\n");
158 gpio_enable_200mhz();
160 // reset cpld statemachine first
168 // reset amiga and statemachine
172 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
173 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
178 printf("Setting CPU type to %d.\n", cpu_type);
179 m68k_set_cpu_type(cpu_type);
183 m68k_set_reg(M68K_REG_PC, 0xF80002);
185 m68k_set_reg(M68K_REG_PC, 0x0);
192 if (mouse_hook_enabled) {
193 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
194 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
198 if (cpu_emulation_running)
199 m68k_execute(loop_cycles);
201 // FIXME: Rework this to use keyboard events instead.
202 /*while (get_key_char(&c)) {
203 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
205 printf("Keyboard hook enabled.\n");
207 else if (c == 0x1B && kb_hook_enabled) {
209 printf("Keyboard hook disabled.\n");
211 if (!kb_hook_enabled) {
212 if (c == cfg->mouse_toggle_key) {
213 mouse_hook_enabled ^= 1;
214 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
215 mouse_dx = mouse_dy = mouse_buttons = 0;
218 cpu_emulation_running ^= 1;
219 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
224 printf("CPU emulation reset.\n");
227 printf("Quitting and exiting emulator.\n");
228 goto stop_cpu_emulation;
247 void cpu_pulse_reset(void) {
249 // printf("Status Reg%x\n",read_reg());
252 // printf("Status Reg%x\n",read_reg());
255 int cpu_irq_ack(int level) {
256 printf("cpu irq ack\n");
260 static unsigned int target = 0;
262 #define PLATFORM_CHECK_READ(a) \
263 if (address >= cfg->custom_low && address < cfg->custom_high) { \
264 unsigned int target = 0; \
265 switch(cfg->platform->id) { \
266 case PLATFORM_AMIGA: { \
267 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
268 return rtg_read((address & 0x0FFFFFFF), a); \
270 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
279 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
280 if (handle_mapped_read(cfg, address, &target, a) != -1) \
284 unsigned int m68k_read_memory_8(unsigned int address) {
285 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
288 return read8((uint32_t)address);
291 unsigned int m68k_read_memory_16(unsigned int address) {
292 PLATFORM_CHECK_READ(OP_TYPE_WORD);
294 if (mouse_hook_enabled) {
295 if (address == JOY0DAT) {
296 // Forward mouse valueses to Amyga.
297 unsigned short result = (mouse_dy << 8) | (mouse_dx);
298 mouse_dx = mouse_dy = 0;
299 return (unsigned int)result;
301 if (address == CIAAPRA) {
302 unsigned short result = (unsigned int)read16((uint32_t)address);
303 if (mouse_buttons & 0x01) {
305 return (unsigned int)(result | 0x40);
308 return (unsigned int)result;
310 if (address == POTGOR) {
311 unsigned short result = (unsigned int)read16((uint32_t)address);
312 if (mouse_buttons & 0x02) {
314 return (unsigned int)(result | 0x2);
317 return (unsigned int)result;
323 return (unsigned int)read16((uint32_t)address);
326 unsigned int m68k_read_memory_32(unsigned int address) {
327 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
330 uint16_t a = read16(address);
331 uint16_t b = read16(address + 2);
332 return (a << 16) | b;
335 #define PLATFORM_CHECK_WRITE(a) \
336 if (address >= cfg->custom_low && address < cfg->custom_high) { \
337 switch(cfg->platform->id) { \
338 case PLATFORM_AMIGA: { \
339 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
340 rtg_write((address & 0x0FFFFFFF), value, a); \
343 if (custom_write_amiga(cfg, address, value, a) != -1) { \
352 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
353 if (handle_mapped_write(cfg, address, value, a) != -1) \
357 void m68k_write_memory_8(unsigned int address, unsigned int value) {
358 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
360 if (address == 0xbfe001) {
361 if (ovl != (value & (1 << 0))) {
362 ovl = (value & (1 << 0));
363 printf("OVL:%x\n", ovl);
368 write8((uint32_t)address, value);
372 void m68k_write_memory_16(unsigned int address, unsigned int value) {
373 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
376 write16((uint32_t)address, value);
380 void m68k_write_memory_32(unsigned int address, unsigned int value) {
381 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
384 write16(address, value >> 16);
385 write16(address + 2, value);