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;
56 void *iplThread(void *args) {
57 printf("IPL thread running\n");
60 if (!gpio_get_irq()) {
67 if (gayle_emulation_enabled) {
68 if ((gayle_int & 0x80) && get_ide(0)->drive->intrq) {
81 // Configurable emulator options
82 unsigned int cpu_type = M68K_CPU_TYPE_68000;
83 unsigned int loop_cycles = 300;
84 struct emulator_config *cfg = NULL;
85 char keyboard_file[256] = "/dev/input/event0";
87 //unsigned char g_kick[524288];
88 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
90 static volatile unsigned char maprom;
92 void sigint_handler(int sig_num) {
94 //cpu_emulation_running = 0;
97 printf("Received sigint %d, exiting.\n", sig_num);
103 if (cfg->platform->shutdown) {
104 cfg->platform->shutdown(cfg);
110 int main(int argc, char *argv[]) {
112 const struct sched_param priority = {99};
114 // Some command line switch stuffles
115 for (g = 1; g < argc; g++) {
116 if (strcmp(argv[g], "--disable-gayle") == 0) {
117 gayle_emulation_enabled = 0;
119 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
121 printf("%s switch found, but no CPU type specified.\n", argv[g]);
124 cpu_type = get_m68k_cpu_type(argv[g]);
127 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
129 printf("%s switch found, but no config filename specified.\n", argv[g]);
132 cfg = load_config_file(argv[g]);
135 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
137 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
140 strcpy(keyboard_file, argv[g]);
146 printf("No config file specified. Trying to load default.cfg...\n");
147 cfg = load_config_file("default.cfg");
149 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
150 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
152 printf("Failed to allocate memory for emulator config!\n");
155 memset(cfg, 0x00, sizeof(struct emulator_config));
160 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
161 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
164 cfg->platform = make_platform_config("none", "generic");
165 cfg->platform->platform_initial_setup(cfg);
168 if (cfg->mouse_enabled) {
169 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
170 if (mouse_fd == -1) {
171 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
172 cfg->mouse_enabled = 0;
176 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
177 if (keyboard_fd == -1) {
178 printf("Failed to open keyboard event source.\n");
183 signal(SIGINT, sigint_handler);
186 //goto skip_everything;
188 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
190 printf("Enable 200MHz GPCLK0 on GPIO4\n");
191 gpio_enable_200mhz();
193 // reset cpld statemachine first
201 // reset amiga and statemachine
205 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
206 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
211 printf("Setting CPU type to %d.\n", cpu_type);
212 m68k_set_cpu_type(cpu_type);
216 m68k_set_reg(M68K_REG_PC, 0xF80002);
218 m68k_set_reg(M68K_REG_PC, 0x0);
225 err = pthread_create(&id, NULL, &iplThread, NULL);
227 printf("can't create IPL thread :[%s]", strerror(err));
229 printf("IPL Thread created successfully\n");
233 if (mouse_hook_enabled) {
234 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
235 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
239 if (cpu_emulation_running)
240 m68k_execute(loop_cycles);
243 unsigned int status = read_reg();
244 m68k_set_irq((status & 0xe000) >> 13);
247 write16(0xdff09c, 0x8000 | (1 << 3));
248 //PAULA_SET_IRQ(3); // IRQ 3 = INT2
256 // FIXME: Rework this to use keyboard events instead.
257 /*while (get_key_char(&c)) {
258 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
260 printf("Keyboard hook enabled.\n");
262 else if (c == 0x1B && kb_hook_enabled) {
264 printf("Keyboard hook disabled.\n");
266 if (!kb_hook_enabled) {
267 if (c == cfg->mouse_toggle_key) {
268 mouse_hook_enabled ^= 1;
269 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
270 mouse_dx = mouse_dy = mouse_buttons = 0;
273 cpu_emulation_running ^= 1;
274 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
279 printf("CPU emulation reset.\n");
282 printf("Quitting and exiting emulator.\n");
283 goto stop_cpu_emulation;
302 void cpu_pulse_reset(void) {
304 // printf("Status Reg%x\n",read_reg());
307 // printf("Status Reg%x\n",read_reg());
310 int cpu_irq_ack(int level) {
311 printf("cpu irq ack\n");
315 static unsigned int target = 0;
317 #define PLATFORM_CHECK_READ(a) \
318 if (address >= cfg->custom_low && address < cfg->custom_high) { \
319 unsigned int target = 0; \
320 switch(cfg->platform->id) { \
321 case PLATFORM_AMIGA: { \
322 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
323 return rtg_read((address & 0x0FFFFFFF), a); \
325 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
334 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
335 if (handle_mapped_read(cfg, address, &target, a) != -1) \
339 unsigned int m68k_read_memory_8(unsigned int address) {
340 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
343 return read8((uint32_t)address);
346 unsigned int m68k_read_memory_16(unsigned int address) {
347 PLATFORM_CHECK_READ(OP_TYPE_WORD);
349 if (mouse_hook_enabled) {
350 if (address == JOY0DAT) {
351 // Forward mouse valueses to Amyga.
352 unsigned short result = (mouse_dy << 8) | (mouse_dx);
353 mouse_dx = mouse_dy = 0;
354 return (unsigned int)result;
356 if (address == CIAAPRA) {
357 unsigned short result = (unsigned int)read16((uint32_t)address);
358 if (mouse_buttons & 0x01) {
360 return (unsigned int)(result | 0x40);
363 return (unsigned int)result;
365 if (address == POTGOR) {
366 unsigned short result = (unsigned int)read16((uint32_t)address);
367 if (mouse_buttons & 0x02) {
369 return (unsigned int)(result | 0x2);
372 return (unsigned int)result;
378 return (unsigned int)read16((uint32_t)address);
381 unsigned int m68k_read_memory_32(unsigned int address) {
382 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
385 uint16_t a = read16(address);
386 uint16_t b = read16(address + 2);
387 return (a << 16) | b;
390 #define PLATFORM_CHECK_WRITE(a) \
391 if (address >= cfg->custom_low && address < cfg->custom_high) { \
392 switch(cfg->platform->id) { \
393 case PLATFORM_AMIGA: { \
394 if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
395 rtg_write((address & 0x0FFFFFFF), value, a); \
398 if (custom_write_amiga(cfg, address, value, a) != -1) { \
407 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
408 if (handle_mapped_write(cfg, address, value, a) != -1) \
412 void m68k_write_memory_8(unsigned int address, unsigned int value) {
413 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
415 if (address == 0xbfe001) {
416 if (ovl != (value & (1 << 0))) {
417 ovl = (value & (1 << 0));
418 printf("OVL:%x\n", ovl);
423 write8((uint32_t)address, value);
427 void m68k_write_memory_16(unsigned int address, unsigned int value) {
428 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
431 write16((uint32_t)address, value);
435 void m68k_write_memory_32(unsigned int address, unsigned int value) {
436 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
439 write16(address, value >> 16);
440 write16(address + 2, value);