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 unsigned char read_ranges;
29 unsigned int read_addr[8];
30 unsigned int read_upper[8];
31 unsigned char *read_data[8];
32 unsigned char write_ranges;
33 unsigned int write_addr[8];
34 unsigned int write_upper[8];
35 unsigned char *write_data[8];
37 int kb_hook_enabled = 0;
38 int mouse_hook_enabled = 0;
39 int cpu_emulation_running = 1;
41 char mouse_dx = 0, mouse_dy = 0;
42 char mouse_buttons = 0;
44 extern volatile unsigned int *gpio;
45 extern volatile uint16_t srdata;
47 #define KICKBASE 0xF80000
48 #define KICKSIZE 0x7FFFF
50 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
52 int gayle_emulation_enabled = 1;
54 // Configurable emulator options
55 unsigned int cpu_type = M68K_CPU_TYPE_68000;
56 unsigned int loop_cycles = 300;
57 struct emulator_config *cfg = NULL;
58 char keyboard_file[256] = "/dev/input/event0";
60 //unsigned char g_kick[524288];
61 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
63 static volatile unsigned char maprom;
65 void sigint_handler(int sig_num) {
67 //cpu_emulation_running = 0;
70 printf("Received sigint %d, exiting.\n", sig_num);
76 if (cfg->platform->shutdown) {
77 cfg->platform->shutdown(cfg);
83 int main(int argc, char *argv[]) {
85 const struct sched_param priority = {99};
87 // Some command line switch stuffles
88 for (g = 1; g < argc; g++) {
89 if (strcmp(argv[g], "--disable-gayle") == 0) {
90 gayle_emulation_enabled = 0;
92 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
94 printf("%s switch found, but no CPU type specified.\n", argv[g]);
97 cpu_type = get_m68k_cpu_type(argv[g]);
100 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
102 printf("%s switch found, but no config filename specified.\n", argv[g]);
105 cfg = load_config_file(argv[g]);
108 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
110 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
113 strcpy(keyboard_file, argv[g]);
119 printf("No config file specified. Trying to load default.cfg...\n");
120 cfg = load_config_file("default.cfg");
122 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
123 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
125 printf("Failed to allocate memory for emulator config!\n");
128 memset(cfg, 0x00, sizeof(struct emulator_config));
133 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
134 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
137 cfg->platform = make_platform_config("none", "generic");
138 cfg->platform->platform_initial_setup(cfg);
141 if (cfg->mouse_enabled) {
142 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
143 if (mouse_fd == -1) {
144 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
145 cfg->mouse_enabled = 0;
149 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
150 if (keyboard_fd == -1) {
151 printf("Failed to open keyboard event source.\n");
156 signal(SIGINT, sigint_handler);
159 //goto skip_everything;
161 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
163 printf("Enable 200MHz GPCLK0 on GPIO4\n");
164 gpio_enable_200mhz();
166 // reset cpld statemachine first
174 // reset amiga and statemachine
178 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
179 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
184 printf("Setting CPU type to %d.\n", cpu_type);
185 m68k_set_cpu_type(cpu_type);
189 m68k_set_reg(M68K_REG_PC, 0xF80002);
191 m68k_set_reg(M68K_REG_PC, 0x0);
198 if (mouse_hook_enabled) {
199 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
200 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
204 if (cpu_emulation_running)
205 m68k_execute(loop_cycles);
207 // FIXME: Rework this to use keyboard events instead.
208 /*while (get_key_char(&c)) {
209 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
211 printf("Keyboard hook enabled.\n");
213 else if (c == 0x1B && kb_hook_enabled) {
215 printf("Keyboard hook disabled.\n");
217 if (!kb_hook_enabled) {
218 if (c == cfg->mouse_toggle_key) {
219 mouse_hook_enabled ^= 1;
220 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
221 mouse_dx = mouse_dy = mouse_buttons = 0;
224 cpu_emulation_running ^= 1;
225 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
230 printf("CPU emulation reset.\n");
233 printf("Quitting and exiting emulator.\n");
234 goto stop_cpu_emulation;
253 void cpu_pulse_reset(void) {
255 // printf("Status Reg%x\n",read_reg());
258 // printf("Status Reg%x\n",read_reg());
261 int cpu_irq_ack(int level) {
262 printf("cpu irq ack\n");
266 static unsigned int target = 0;
268 #define PLATFORM_CHECK_READ(a) \
269 if (address >= cfg->custom_low && address < cfg->custom_high) { \
270 unsigned int target = 0; \
271 switch(cfg->platform->id) { \
272 case PLATFORM_AMIGA: { \
273 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
274 return rtg_read((address & 0x0FFFFFFF), a); \
276 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
285 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
286 if (handle_mapped_read(cfg, address, &target, a) != -1) \
290 unsigned int m68k_read_memory_8(unsigned int address) {
291 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
294 return read8((uint32_t)address);
297 unsigned int m68k_read_memory_16(unsigned int address) {
298 PLATFORM_CHECK_READ(OP_TYPE_WORD);
300 if (mouse_hook_enabled) {
301 if (address == JOY0DAT) {
302 // Forward mouse valueses to Amyga.
303 unsigned short result = (mouse_dy << 8) | (mouse_dx);
304 mouse_dx = mouse_dy = 0;
305 return (unsigned int)result;
307 if (address == CIAAPRA) {
308 unsigned short result = (unsigned int)read16((uint32_t)address);
309 if (mouse_buttons & 0x01) {
311 return (unsigned int)(result | 0x40);
314 return (unsigned int)result;
316 if (address == POTGOR) {
317 unsigned short result = (unsigned int)read16((uint32_t)address);
318 if (mouse_buttons & 0x02) {
320 return (unsigned int)(result | 0x2);
323 return (unsigned int)result;
329 return (unsigned int)read16((uint32_t)address);
332 unsigned int m68k_read_memory_32(unsigned int address) {
333 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
336 uint16_t a = read16(address);
337 uint16_t b = read16(address + 2);
338 return (a << 16) | b;
341 #define PLATFORM_CHECK_WRITE(a) \
342 if (address >= cfg->custom_low && address < cfg->custom_high) { \
343 switch(cfg->platform->id) { \
344 case PLATFORM_AMIGA: { \
345 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
346 rtg_write((address & 0x0FFFFFFF), value, a); \
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);