14 #include <sys/types.h>
16 #include <sys/ioctl.h>
20 #include "platforms/platforms.h"
21 #include "input/input.h"
23 #include "platforms/amiga/Gayle.h"
24 #include "platforms/amiga/gayle-ide/ide.h"
25 #include "platforms/amiga/amiga-registers.h"
26 #include "platforms/amiga/rtg/rtg.h"
27 #include "gpio/gpio.h"
29 int kb_hook_enabled = 0;
30 int mouse_hook_enabled = 0;
31 int cpu_emulation_running = 1;
33 char mouse_dx = 0, mouse_dy = 0;
34 char mouse_buttons = 0;
36 extern volatile unsigned int *gpio;
37 extern volatile uint16_t srdata;
39 #define KICKBASE 0xF80000
40 #define KICKSIZE 0x7FFFF
42 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
44 int gayle_emulation_enabled = 1;
46 // Configurable emulator options
47 unsigned int cpu_type = M68K_CPU_TYPE_68000;
48 unsigned int loop_cycles = 300;
49 struct emulator_config *cfg = NULL;
50 char keyboard_file[256] = "/dev/input/event0";
52 //unsigned char g_kick[524288];
53 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
55 static volatile unsigned char maprom;
57 void sigint_handler(int sig_num) {
59 //cpu_emulation_running = 0;
62 printf("Received sigint %d, exiting.\n", sig_num);
68 if (cfg->platform->shutdown) {
69 cfg->platform->shutdown(cfg);
75 int main(int argc, char *argv[]) {
77 const struct sched_param priority = {99};
79 // Some command line switch stuffles
80 for (g = 1; g < argc; g++) {
81 if (strcmp(argv[g], "--disable-gayle") == 0) {
82 gayle_emulation_enabled = 0;
84 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
86 printf("%s switch found, but no CPU type specified.\n", argv[g]);
89 cpu_type = get_m68k_cpu_type(argv[g]);
92 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
94 printf("%s switch found, but no config filename specified.\n", argv[g]);
97 cfg = load_config_file(argv[g]);
100 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
102 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
105 strcpy(keyboard_file, argv[g]);
111 printf("No config file specified. Trying to load default.cfg...\n");
112 cfg = load_config_file("default.cfg");
114 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
115 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
117 printf("Failed to allocate memory for emulator config!\n");
120 memset(cfg, 0x00, sizeof(struct emulator_config));
125 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
126 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
129 cfg->platform = make_platform_config("none", "generic");
130 cfg->platform->platform_initial_setup(cfg);
133 if (cfg->mouse_enabled) {
134 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
135 if (mouse_fd == -1) {
136 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
137 cfg->mouse_enabled = 0;
142 printf("Initializing SDL2...\n");
143 if (SDL_Init(0) < 0) {
144 printf("Failed to initialize SDL2.\n");
147 printf("Initializing SDL2 Video...\n");
148 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
149 printf("Failed to initialize SDL2 Video. Trying again.\n");
150 if (SDL_Init(SDL_INIT_VIDEO) < 0) {
151 printf("Failed to initialize SDL2 Video again. Dying.\n");
154 printf("Initialized SDL2 Video.\n");
157 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
158 if (keyboard_fd == -1) {
159 printf("Failed to open keyboard event source.\n");
162 sched_setscheduler(0, SCHED_FIFO, &priority);
163 mlockall(MCL_CURRENT); // lock in memory to keep us from paging out
167 signal(SIGINT, sigint_handler);
170 //goto skip_everything;
172 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
174 printf("Enable 200MHz GPCLK0 on GPIO4\n");
175 gpio_enable_200mhz();
177 // reset cpld statemachine first
185 // reset amiga and statemachine
189 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
190 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
195 printf("Setting CPU type to %d.\n", cpu_type);
196 m68k_set_cpu_type(cpu_type);
200 m68k_set_reg(M68K_REG_PC, 0xF80002);
202 m68k_set_reg(M68K_REG_PC, 0x0);
209 if (mouse_hook_enabled) {
210 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
211 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
215 if (cpu_emulation_running)
216 m68k_execute(loop_cycles);
218 // FIXME: Rework this to use keyboard events instead.
219 /*while (get_key_char(&c)) {
220 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
222 printf("Keyboard hook enabled.\n");
224 else if (c == 0x1B && kb_hook_enabled) {
226 printf("Keyboard hook disabled.\n");
228 if (!kb_hook_enabled) {
229 if (c == cfg->mouse_toggle_key) {
230 mouse_hook_enabled ^= 1;
231 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
232 mouse_dx = mouse_dy = mouse_buttons = 0;
235 cpu_emulation_running ^= 1;
236 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
241 printf("CPU emulation reset.\n");
244 printf("Quitting and exiting emulator.\n");
245 goto stop_cpu_emulation;
264 void cpu_pulse_reset(void) {
266 // printf("Status Reg%x\n",read_reg());
269 // printf("Status Reg%x\n",read_reg());
272 int cpu_irq_ack(int level) {
273 printf("cpu irq ack\n");
277 static unsigned int target = 0;
279 #define PLATFORM_CHECK_READ(a) \
280 if (address >= cfg->custom_low && address < cfg->custom_high) { \
281 unsigned int target = 0; \
282 switch(cfg->platform->id) { \
283 case PLATFORM_AMIGA: { \
284 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
285 return rtg_read((address & 0x0FFFFFFF), a); \
287 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
296 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
297 if (handle_mapped_read(cfg, address, &target, a) != -1) \
301 unsigned int m68k_read_memory_8(unsigned int address) {
302 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
305 return read8((uint32_t)address);
308 unsigned int m68k_read_memory_16(unsigned int address) {
309 PLATFORM_CHECK_READ(OP_TYPE_WORD);
311 if (mouse_hook_enabled) {
312 if (address == JOY0DAT) {
313 // Forward mouse valueses to Amyga.
314 unsigned short result = (mouse_dy << 8) | (mouse_dx);
315 mouse_dx = mouse_dy = 0;
316 return (unsigned int)result;
318 if (address == CIAAPRA) {
319 unsigned short result = (unsigned int)read16((uint32_t)address);
320 if (mouse_buttons & 0x01) {
322 return (unsigned int)(result | 0x40);
325 return (unsigned int)result;
327 if (address == POTGOR) {
328 unsigned short result = (unsigned int)read16((uint32_t)address);
329 if (mouse_buttons & 0x02) {
331 return (unsigned int)(result | 0x2);
334 return (unsigned int)result;
340 return (unsigned int)read16((uint32_t)address);
343 unsigned int m68k_read_memory_32(unsigned int address) {
344 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
347 uint16_t a = read16(address);
348 uint16_t b = read16(address + 2);
349 return (a << 16) | b;
352 #define PLATFORM_CHECK_WRITE(a) \
353 if (address >= cfg->custom_low && address < cfg->custom_high) { \
354 switch(cfg->platform->id) { \
355 case PLATFORM_AMIGA: { \
356 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
357 rtg_write((address & 0x0FFFFFFF), value, a); \
360 if (custom_write_amiga(cfg, address, value, a) != -1) { \
369 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
370 if (handle_mapped_write(cfg, address, value, a) != -1) \
374 void m68k_write_memory_8(unsigned int address, unsigned int value) {
375 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
377 if (address == 0xbfe001) {
378 if (ovl != (value & (1 << 0))) {
379 ovl = (value & (1 << 0));
380 printf("OVL:%x\n", ovl);
385 write8((uint32_t)address, value);
389 void m68k_write_memory_16(unsigned int address, unsigned int value) {
390 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
393 write16((uint32_t)address, value);
397 void m68k_write_memory_32(unsigned int address, unsigned int value) {
398 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
401 write16(address, value >> 16);
402 write16(address + 2, value);