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;
46 extern uint8_t realtime_graphics_debug;
48 #define KICKBASE 0xF80000
49 #define KICKSIZE 0x7FFFF
51 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
53 int gayle_emulation_enabled = 1;
57 void *iplThread(void *args) {
58 printf("IPL thread running\n");
61 if (!gpio_get_irq()) {
68 if (gayle_emulation_enabled) {
69 if ((gayle_int & 0x80) && get_ide(0)->drive->intrq) {
82 // Configurable emulator options
83 unsigned int cpu_type = M68K_CPU_TYPE_68000;
84 unsigned int loop_cycles = 300;
85 struct emulator_config *cfg = NULL;
86 char keyboard_file[256] = "/dev/input/event1";
88 //unsigned char g_kick[524288];
89 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
91 static volatile unsigned char maprom;
93 void sigint_handler(int sig_num) {
95 //cpu_emulation_running = 0;
98 printf("Received sigint %d, exiting.\n", sig_num);
104 if (cfg->platform->shutdown) {
105 cfg->platform->shutdown(cfg);
111 int main(int argc, char *argv[]) {
113 const struct sched_param priority = {99};
115 // Some command line switch stuffles
116 for (g = 1; g < argc; g++) {
117 if (strcmp(argv[g], "--disable-gayle") == 0) {
118 gayle_emulation_enabled = 0;
120 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
122 printf("%s switch found, but no CPU type specified.\n", argv[g]);
125 cpu_type = get_m68k_cpu_type(argv[g]);
128 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
130 printf("%s switch found, but no config filename specified.\n", argv[g]);
133 cfg = load_config_file(argv[g]);
136 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
138 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
141 strcpy(keyboard_file, argv[g]);
147 printf("No config file specified. Trying to load default.cfg...\n");
148 cfg = load_config_file("default.cfg");
150 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
151 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
153 printf("Failed to allocate memory for emulator config!\n");
156 memset(cfg, 0x00, sizeof(struct emulator_config));
161 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
162 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
165 cfg->platform = make_platform_config("none", "generic");
166 cfg->platform->platform_initial_setup(cfg);
169 if (cfg->mouse_enabled) {
170 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
171 if (mouse_fd == -1) {
172 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
173 cfg->mouse_enabled = 0;
177 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
178 if (keyboard_fd == -1) {
179 printf("Failed to open keyboard event source.\n");
184 signal(SIGINT, sigint_handler);
187 //goto skip_everything;
189 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
191 printf("Enable 200MHz GPCLK0 on GPIO4\n");
192 gpio_enable_200mhz();
194 // reset cpld statemachine first
202 // reset amiga and statemachine
206 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
207 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
212 printf("Setting CPU type to %d.\n", cpu_type);
213 m68k_set_cpu_type(cpu_type);
217 m68k_set_reg(M68K_REG_PC, 0xF80002);
219 m68k_set_reg(M68K_REG_PC, 0x0);
226 err = pthread_create(&id, NULL, &iplThread, NULL);
228 printf("can't create IPL thread :[%s]", strerror(err));
230 printf("IPL Thread created successfully\n");
234 if (mouse_hook_enabled) {
235 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
236 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
240 if (cpu_emulation_running)
241 m68k_execute(loop_cycles);
244 unsigned int status = read_reg();
245 m68k_set_irq((status & 0xe000) >> 13);
248 write16(0xdff09c, 0x8000 | (1 << 3));
249 //PAULA_SET_IRQ(3); // IRQ 3 = INT2
257 // FIXME: Rework this to use keyboard events instead.
258 while (get_key_char(&c)) {
259 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
261 printf("Keyboard hook enabled.\n");
263 else if (c == 0x1B && kb_hook_enabled) {
265 printf("Keyboard hook disabled.\n");
267 if (!kb_hook_enabled) {
268 if (c == cfg->mouse_toggle_key) {
269 mouse_hook_enabled ^= 1;
270 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
271 mouse_dx = mouse_dy = mouse_buttons = 0;
274 cpu_emulation_running ^= 1;
275 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
278 realtime_graphics_debug ^= 1;
279 printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
284 printf("CPU emulation reset.\n");
287 printf("Quitting and exiting emulator.\n");
288 goto stop_cpu_emulation;
307 void cpu_pulse_reset(void) {
309 // printf("Status Reg%x\n",read_reg());
312 // printf("Status Reg%x\n",read_reg());
315 int cpu_irq_ack(int level) {
316 printf("cpu irq ack\n");
320 static unsigned int target = 0;
322 #define PLATFORM_CHECK_READ(a) \
323 if (address >= cfg->custom_low && address < cfg->custom_high) { \
324 unsigned int target = 0; \
325 switch(cfg->platform->id) { \
326 case PLATFORM_AMIGA: { \
327 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
328 return rtg_read((address & 0x0FFFFFFF), a); \
330 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
339 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
340 if (handle_mapped_read(cfg, address, &target, a) != -1) \
344 unsigned int m68k_read_memory_8(unsigned int address) {
345 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
348 return read8((uint32_t)address);
351 unsigned int m68k_read_memory_16(unsigned int address) {
352 PLATFORM_CHECK_READ(OP_TYPE_WORD);
354 if (mouse_hook_enabled) {
355 if (address == JOY0DAT) {
356 // Forward mouse valueses to Amyga.
357 unsigned short result = (mouse_dy << 8) | (mouse_dx);
358 mouse_dx = mouse_dy = 0;
359 return (unsigned int)result;
361 if (address == CIAAPRA) {
362 unsigned short result = (unsigned int)read16((uint32_t)address);
363 if (mouse_buttons & 0x01) {
365 return (unsigned int)(result | 0x40);
368 return (unsigned int)result;
370 if (address == POTGOR) {
371 unsigned short result = (unsigned int)read16((uint32_t)address);
372 if (mouse_buttons & 0x02) {
374 return (unsigned int)(result | 0x2);
377 return (unsigned int)result;
383 return (unsigned int)read16((uint32_t)address);
386 unsigned int m68k_read_memory_32(unsigned int address) {
387 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
390 uint16_t a = read16(address);
391 uint16_t b = read16(address + 2);
392 return (a << 16) | b;
395 #define PLATFORM_CHECK_WRITE(a) \
396 if (address >= cfg->custom_low && address < cfg->custom_high) { \
397 switch(cfg->platform->id) { \
398 case PLATFORM_AMIGA: { \
399 if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
400 rtg_write((address & 0x0FFFFFFF), value, a); \
403 if (custom_write_amiga(cfg, address, value, a) != -1) { \
412 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
413 if (handle_mapped_write(cfg, address, value, a) != -1) \
417 void m68k_write_memory_8(unsigned int address, unsigned int value) {
418 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
420 if (address == 0xbfe001) {
421 if (ovl != (value & (1 << 0))) {
422 ovl = (value & (1 << 0));
423 printf("OVL:%x\n", ovl);
428 write8((uint32_t)address, value);
432 void m68k_write_memory_16(unsigned int address, unsigned int value) {
433 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
436 write16((uint32_t)address, value);
440 void m68k_write_memory_32(unsigned int address, unsigned int value) {
441 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
444 write16(address, value >> 16);
445 write16(address + 2, value);