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");
147 signal(SIGINT, sigint_handler);
150 //goto skip_everything;
152 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
154 printf("Enable 200MHz GPCLK0 on GPIO4\n");
155 gpio_enable_200mhz();
157 // reset cpld statemachine first
165 // reset amiga and statemachine
169 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
170 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
175 printf("Setting CPU type to %d.\n", cpu_type);
176 m68k_set_cpu_type(cpu_type);
180 m68k_set_reg(M68K_REG_PC, 0xF80002);
182 m68k_set_reg(M68K_REG_PC, 0x0);
189 if (mouse_hook_enabled) {
190 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
191 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
195 if (cpu_emulation_running)
196 m68k_execute(loop_cycles);
198 // FIXME: Rework this to use keyboard events instead.
199 /*while (get_key_char(&c)) {
200 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
202 printf("Keyboard hook enabled.\n");
204 else if (c == 0x1B && kb_hook_enabled) {
206 printf("Keyboard hook disabled.\n");
208 if (!kb_hook_enabled) {
209 if (c == cfg->mouse_toggle_key) {
210 mouse_hook_enabled ^= 1;
211 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
212 mouse_dx = mouse_dy = mouse_buttons = 0;
215 cpu_emulation_running ^= 1;
216 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
221 printf("CPU emulation reset.\n");
224 printf("Quitting and exiting emulator.\n");
225 goto stop_cpu_emulation;
244 void cpu_pulse_reset(void) {
246 // printf("Status Reg%x\n",read_reg());
249 // printf("Status Reg%x\n",read_reg());
252 int cpu_irq_ack(int level) {
253 printf("cpu irq ack\n");
257 static unsigned int target = 0;
259 #define PLATFORM_CHECK_READ(a) \
260 if (address >= cfg->custom_low && address < cfg->custom_high) { \
261 unsigned int target = 0; \
262 switch(cfg->platform->id) { \
263 case PLATFORM_AMIGA: { \
264 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
265 return rtg_read((address & 0x0FFFFFFF), a); \
267 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
276 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
277 if (handle_mapped_read(cfg, address, &target, a) != -1) \
281 unsigned int m68k_read_memory_8(unsigned int address) {
282 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
285 return read8((uint32_t)address);
288 unsigned int m68k_read_memory_16(unsigned int address) {
289 PLATFORM_CHECK_READ(OP_TYPE_WORD);
291 if (mouse_hook_enabled) {
292 if (address == JOY0DAT) {
293 // Forward mouse valueses to Amyga.
294 unsigned short result = (mouse_dy << 8) | (mouse_dx);
295 mouse_dx = mouse_dy = 0;
296 return (unsigned int)result;
298 if (address == CIAAPRA) {
299 unsigned short result = (unsigned int)read16((uint32_t)address);
300 if (mouse_buttons & 0x01) {
302 return (unsigned int)(result | 0x40);
305 return (unsigned int)result;
307 if (address == POTGOR) {
308 unsigned short result = (unsigned int)read16((uint32_t)address);
309 if (mouse_buttons & 0x02) {
311 return (unsigned int)(result | 0x2);
314 return (unsigned int)result;
320 return (unsigned int)read16((uint32_t)address);
323 unsigned int m68k_read_memory_32(unsigned int address) {
324 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
327 uint16_t a = read16(address);
328 uint16_t b = read16(address + 2);
329 return (a << 16) | b;
332 #define PLATFORM_CHECK_WRITE(a) \
333 if (address >= cfg->custom_low && address < cfg->custom_high) { \
334 switch(cfg->platform->id) { \
335 case PLATFORM_AMIGA: { \
336 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
337 rtg_write((address & 0x0FFFFFFF), value, a); \
340 if (custom_write_amiga(cfg, address, value, a) != -1) { \
349 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
350 if (handle_mapped_write(cfg, address, value, a) != -1) \
354 void m68k_write_memory_8(unsigned int address, unsigned int value) {
355 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
357 if (address == 0xbfe001) {
358 if (ovl != (value & (1 << 0))) {
359 ovl = (value & (1 << 0));
360 printf("OVL:%x\n", ovl);
365 write8((uint32_t)address, value);
369 void m68k_write_memory_16(unsigned int address, unsigned int value) {
370 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
373 write16((uint32_t)address, value);
377 void m68k_write_memory_32(unsigned int address, unsigned int value) {
378 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
381 write16(address, value >> 16);
382 write16(address + 2, value);