14 #include <sys/types.h>
16 #include <sys/ioctl.h>
21 #include "platforms/platforms.h"
22 #include "input/input.h"
24 #include "platforms/amiga/amiga-registers.h"
26 //#define BCM2708_PERI_BASE 0x20000000 //pi0-1
27 //#define BCM2708_PERI_BASE 0xFE000000 //pi4
28 #define BCM2708_PERI_BASE 0x3F000000 // pi3
29 #define BCM2708_PERI_SIZE 0x01000000
30 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
31 #define GPCLK_BASE (BCM2708_PERI_BASE + 0x101000)
32 #define GPIO_ADDR 0x200000 /* GPIO controller */
33 #define GPCLK_ADDR 0x101000
34 #define CLK_PASSWD 0x5a000000
35 #define CLK_GP0_CTL 0x070
36 #define CLK_GP0_DIV 0x074
42 #define STATUSREGADDR \
43 GPIO_CLR = 1 << SA0; \
44 GPIO_CLR = 1 << SA1; \
47 GPIO_CLR = 1 << SA0; \
48 GPIO_CLR = 1 << SA1; \
51 GPIO_SET = 1 << SA0; \
52 GPIO_CLR = 1 << SA1; \
55 GPIO_CLR = 1 << SA0; \
56 GPIO_SET = 1 << SA1; \
59 GPIO_SET = 1 << SA0; \
60 GPIO_SET = 1 << SA1; \
63 #define PAGE_SIZE (4 * 1024)
64 #define BLOCK_SIZE (4 * 1024)
66 #define GPIOSET(no, ishigh) \
71 reset |= (1 << (no)); \
74 #define FASTBASE 0x07FFFFFF
75 #define FASTSIZE 0xFFFFFFF
76 #define GAYLEBASE 0xD80000 // D7FFFF
77 #define GAYLESIZE 0x6FFFF
79 #define JOY0DAT 0xDFF00A
80 #define JOY1DAT 0xDFF00C
81 #define CIAAPRA 0xBFE001
82 #define POTGOR 0xDFF016
84 int kb_hook_enabled = 0;
85 int mouse_hook_enabled = 0;
86 int cpu_emulation_running = 1;
88 char mouse_dx = 0, mouse_dy = 0;
89 char mouse_buttons = 0;
91 #define KICKBASE 0xF80000
92 #define KICKSIZE 0x7FFFF
94 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
96 int gayle_emulation_enabled = 1;
100 // Configurable emulator options
101 unsigned int cpu_type = M68K_CPU_TYPE_68000;
102 unsigned int loop_cycles = 300;
103 struct emulator_config *cfg = NULL;
104 char keyboard_file[256] = "/dev/input/event0";
107 volatile unsigned int *gpio;
108 volatile unsigned int *gpclk;
109 volatile unsigned int gpfsel0;
110 volatile unsigned int gpfsel1;
111 volatile unsigned int gpfsel2;
112 volatile unsigned int gpfsel0_o;
113 volatile unsigned int gpfsel1_o;
114 volatile unsigned int gpfsel2_o;
116 // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or
118 #define INP_GPIO(g) *(gpio + ((g) / 10)) &= ~(7 << (((g) % 10) * 3))
119 #define OUT_GPIO(g) *(gpio + ((g) / 10)) |= (1 << (((g) % 10) * 3))
120 #define SET_GPIO_ALT(g, a) \
121 *(gpio + (((g) / 10))) |= \
122 (((a) <= 3 ? (a) + 4 : (a) == 4 ? 3 : 2) << (((g) % 10) * 3))
125 *(gpio + 7) // sets bits which are 1 ignores bits which are 0
127 *(gpio + 10) // clears bits which are 1 ignores bits which are 0
129 #define GET_GPIO(g) (*(gpio + 13) & (1 << g)) // 0 if LOW, (1<<g) if HIGH
131 #define GPIO_PULL *(gpio + 37) // Pull up/pull down
132 #define GPIO_PULLCLK0 *(gpio + 38) // Pull up/pull down clock
136 uint32_t read8(uint32_t address);
137 void write8(uint32_t address, uint32_t data);
139 uint32_t read16(uint32_t address);
140 void write16(uint32_t address, uint32_t data);
142 void write32(uint32_t address, uint32_t data);
143 uint32_t read32(uint32_t address);
145 uint16_t read_reg(void);
146 void write_reg(unsigned int value);
148 volatile uint16_t srdata;
149 volatile uint32_t srdata2;
150 volatile uint32_t srdata2_old;
152 //unsigned char g_kick[524288];
153 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
154 unsigned char toggle;
156 static volatile unsigned char maprom;
158 void sigint_handler(int sig_num) {
160 //cpu_emulation_running = 0;
163 printf("Received sigint %d, exiting.\n", sig_num);
172 void *iplThread(void *args) {
173 printf("IPL thread running/n");
177 if (GET_GPIO(1) == 0) {
179 m68k_end_timeslice();
180 //printf("thread!/n");
189 int main(int argc, char *argv[]) {
191 const struct sched_param priority = {99};
193 // Some command line switch stuffles
194 for (g = 1; g < argc; g++) {
195 if (strcmp(argv[g], "--disable-gayle") == 0) {
196 gayle_emulation_enabled = 0;
198 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
200 printf("%s switch found, but no CPU type specified.\n", argv[g]);
203 cpu_type = get_m68k_cpu_type(argv[g]);
206 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
208 printf("%s switch found, but no config filename specified.\n", argv[g]);
211 cfg = load_config_file(argv[g]);
214 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
216 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
219 strcpy(keyboard_file, argv[g]);
225 printf("No config file specified. Trying to load default.cfg...\n");
226 cfg = load_config_file("default.cfg");
228 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
229 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
231 printf("Failed to allocate memory for emulator config!\n");
234 memset(cfg, 0x00, sizeof(struct emulator_config));
239 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
240 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
243 cfg->platform = make_platform_config("none", "generic");
244 cfg->platform->platform_initial_setup(cfg);
247 if (cfg->mouse_enabled) {
248 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
249 if (mouse_fd == -1) {
250 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
251 cfg->mouse_enabled = 0;
255 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
256 if (keyboard_fd == -1) {
257 printf("Failed to open keyboard event source.\n");
260 sched_setscheduler(0, SCHED_FIFO, &priority);
261 mlockall(MCL_CURRENT); // lock in memory to keep us from paging out
265 signal(SIGINT, sigint_handler);
268 //goto skip_everything;
270 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
272 printf("Enable 200MHz GPCLK0 on GPIO4\n");
274 *(gpclk + (CLK_GP0_CTL / 4)) = CLK_PASSWD | (1 << 5);
276 while ((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7))
279 *(gpclk + (CLK_GP0_DIV / 4)) =
280 CLK_PASSWD | (6 << 12); // divider , 6=200MHz on pi3
282 *(gpclk + (CLK_GP0_CTL / 4)) =
283 CLK_PASSWD | 5 | (1 << 4); // pll? 6=plld, 5=pllc
285 while (((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7)) == 0)
289 SET_GPIO_ALT(4, 0); // gpclk0
299 // set gpio0 (aux0) and gpio1 (aux1) to input
303 // Set GPIO pins 6,7 and 8-23 to output
304 for (g = 6; g <= 23; g++) {
308 printf("Precalculate GPIO8-23 as Output\n");
309 gpfsel0_o = *(gpio); // store gpio ddr
310 printf("gpfsel0: %#x\n", gpfsel0_o);
311 gpfsel1_o = *(gpio + 1); // store gpio ddr
312 printf("gpfsel1: %#x\n", gpfsel1_o);
313 gpfsel2_o = *(gpio + 2); // store gpio ddr
314 printf("gpfsel2: %#x\n", gpfsel2_o);
316 // Set GPIO pins 8-23 to input
317 for (g = 8; g <= 23; g++) {
320 printf("Precalculate GPIO8-23 as Input\n");
321 gpfsel0 = *(gpio); // store gpio ddr
322 printf("gpfsel0: %#x\n", gpfsel0);
323 gpfsel1 = *(gpio + 1); // store gpio ddr
324 printf("gpfsel1: %#x\n", gpfsel1);
325 gpfsel2 = *(gpio + 2); // store gpio ddr
326 printf("gpfsel2: %#x\n", gpfsel2);
335 // reset cpld statemachine first
343 // reset amiga and statemachine
347 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
348 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
353 printf("Setting CPU type to %d.\n", cpu_type);
354 m68k_set_cpu_type(cpu_type);
358 m68k_set_reg(M68K_REG_PC, 0xF80002);
360 m68k_set_reg(M68K_REG_PC, 0x0);
366 err = pthread_create(&id, NULL, &iplThread, NULL);
368 printf("\ncan't create IPL thread :[%s]", strerror(err));
370 printf("\n IPL Thread created successfully\n");
376 if (mouse_hook_enabled) {
377 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
378 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
382 if (cpu_emulation_running)
383 m68k_execute(loop_cycles);
385 // FIXME: Rework this to use keyboard events instead.
386 /*while (get_key_char(&c)) {
387 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
389 printf("Keyboard hook enabled.\n");
391 else if (c == 0x1B && kb_hook_enabled) {
393 printf("Keyboard hook disabled.\n");
395 if (!kb_hook_enabled) {
396 if (c == cfg->mouse_toggle_key) {
397 mouse_hook_enabled ^= 1;
398 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
399 mouse_dx = mouse_dy = mouse_buttons = 0;
402 cpu_emulation_running ^= 1;
403 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
408 printf("CPU emulation reset.\n");
411 printf("Quitting and exiting emulator.\n");
412 goto stop_cpu_emulation;
419 m68k_set_irq((srdata >> 13) & 0xff);
427 if (GET_GPIO(1) == 0) {
429 m68k_set_irq((srdata >> 13) & 0xff);
431 if (CheckIrq() == 1) {
432 write16(0xdff09c, 0x8008);
451 void cpu_pulse_reset(void) {
453 // printf("Status Reg%x\n",read_reg());
456 // printf("Status Reg%x\n",read_reg());
459 int cpu_irq_ack(int level) {
460 printf("cpu irq ack\n");
464 static unsigned int target = 0;
466 #define PLATFORM_CHECK_READ(a) \
467 if (address >= cfg->custom_low && address < cfg->custom_high) { \
468 unsigned int target = 0; \
469 switch(cfg->platform->id) { \
470 case PLATFORM_AMIGA: { \
471 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
480 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
481 if (handle_mapped_read(cfg, address, &target, a) != -1) \
485 unsigned int m68k_read_memory_8(unsigned int address) {
486 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
489 // if (address < 0xffffff) {
490 return read8((uint32_t)address);
496 unsigned int m68k_read_memory_16(unsigned int address) {
497 PLATFORM_CHECK_READ(OP_TYPE_WORD);
499 if (mouse_hook_enabled) {
500 if (address == JOY0DAT) {
501 // Forward mouse valueses to Amyga.
502 unsigned short result = (mouse_dy << 8) | (mouse_dx);
503 mouse_dx = mouse_dy = 0;
504 return (unsigned int)result;
506 if (address == CIAAPRA) {
507 unsigned short result = (unsigned int)read16((uint32_t)address);
508 if (mouse_buttons & 0x01) {
510 return (unsigned int)(result | 0x40);
513 return (unsigned int)result;
515 if (address == POTGOR) {
516 unsigned short result = (unsigned int)read16((uint32_t)address);
517 if (mouse_buttons & 0x02) {
519 return (unsigned int)(result | 0x2);
522 return (unsigned int)result;
526 // if (address < 0xffffff) {
528 return (unsigned int)read16((uint32_t)address);
534 unsigned int m68k_read_memory_32(unsigned int address) {
535 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
537 // if (address < 0xffffff) {
539 uint16_t a = read16(address);
540 uint16_t b = read16(address + 2);
541 return (a << 16) | b;
547 #define PLATFORM_CHECK_WRITE(a) \
548 if (address >= cfg->custom_low && address < cfg->custom_high) { \
549 switch(cfg->platform->id) { \
550 case PLATFORM_AMIGA: { \
551 if (custom_write_amiga(cfg, address, value, a) != -1) { \
560 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
561 if (handle_mapped_write(cfg, address, value, a) != -1) \
565 void m68k_write_memory_8(unsigned int address, unsigned int value) {
566 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
568 if (address == 0xbfe001) {
569 if (ovl != (value & (1 << 0)) {
570 ovl = (value & (1 << 0));
571 printf("OVL:%x\n", ovl);
575 // if (address < 0xffffff) {
577 write8((uint32_t)address, value);
584 void m68k_write_memory_16(unsigned int address, unsigned int value) {
585 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
587 // if (address < 0xffffff) {
589 write16((uint32_t)address, value);
595 void m68k_write_memory_32(unsigned int address, unsigned int value) {
596 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
598 // if (address < 0xffffff) {
600 write16(address, value >> 16);
601 write16(address + 2, value);
608 inline void write16(uint32_t address, uint32_t data) {
609 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
610 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
611 uint32_t addr_l_s = (address >> 16) << 8;
612 uint32_t addr_l_r = (~address >> 16) << 8;
613 uint32_t data_s = (data & 0x0000ffff) << 8;
614 uint32_t data_r = (~data & 0x0000ffff) << 8;
616 // asm volatile ("dmb" ::: "memory");
619 *(gpio + 1) = gpfsel1_o;
620 *(gpio + 2) = gpfsel2_o;
622 *(gpio + 7) = addr_h_s;
623 *(gpio + 10) = addr_h_r;
627 *(gpio + 7) = addr_l_s;
628 *(gpio + 10) = addr_l_r;
633 *(gpio + 7) = data_s;
634 *(gpio + 10) = data_r;
639 *(gpio + 1) = gpfsel1;
640 *(gpio + 2) = gpfsel2;
641 while ((GET_GPIO(0)))
643 // asm volatile ("dmb" ::: "memory");
646 inline void write8(uint32_t address, uint32_t data) {
647 if ((address & 1) == 0)
648 data = data + (data << 8); // EVEN, A0=0,UDS
650 data = data & 0xff; // ODD , A0=1,LDS
651 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
652 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
653 uint32_t addr_l_s = (address >> 16) << 8;
654 uint32_t addr_l_r = (~address >> 16) << 8;
655 uint32_t data_s = (data & 0x0000ffff) << 8;
656 uint32_t data_r = (~data & 0x0000ffff) << 8;
658 // asm volatile ("dmb" ::: "memory");
661 *(gpio + 1) = gpfsel1_o;
662 *(gpio + 2) = gpfsel2_o;
664 *(gpio + 7) = addr_h_s;
665 *(gpio + 10) = addr_h_r;
669 *(gpio + 7) = addr_l_s;
670 *(gpio + 10) = addr_l_r;
675 *(gpio + 7) = data_s;
676 *(gpio + 10) = data_r;
681 *(gpio + 1) = gpfsel1;
682 *(gpio + 2) = gpfsel2;
683 while ((GET_GPIO(0)))
685 // asm volatile ("dmb" ::: "memory");
688 inline uint32_t read16(uint32_t address) {
690 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
691 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
692 uint32_t addr_l_s = (address >> 16) << 8;
693 uint32_t addr_l_r = (~address >> 16) << 8;
695 // asm volatile ("dmb" ::: "memory");
698 *(gpio + 1) = gpfsel1_o;
699 *(gpio + 2) = gpfsel2_o;
701 *(gpio + 7) = addr_h_s;
702 *(gpio + 10) = addr_h_r;
706 *(gpio + 7) = addr_l_s;
707 *(gpio + 10) = addr_l_r;
713 *(gpio + 1) = gpfsel1;
714 *(gpio + 2) = gpfsel2;
716 while (!(GET_GPIO(0)))
721 // asm volatile ("dmb" ::: "memory");
722 return (val >> 8) & 0xffff;
725 inline uint32_t read8(uint32_t address) {
727 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
728 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
729 uint32_t addr_l_s = (address >> 16) << 8;
730 uint32_t addr_l_r = (~address >> 16) << 8;
732 // asm volatile ("dmb" ::: "memory");
735 *(gpio + 1) = gpfsel1_o;
736 *(gpio + 2) = gpfsel2_o;
738 *(gpio + 7) = addr_h_s;
739 *(gpio + 10) = addr_h_r;
743 *(gpio + 7) = addr_l_s;
744 *(gpio + 10) = addr_l_r;
750 *(gpio + 1) = gpfsel1;
751 *(gpio + 2) = gpfsel2;
754 while (!(GET_GPIO(0)))
759 // asm volatile ("dmb" ::: "memory");
761 val = (val >> 8) & 0xffff;
762 if ((address & 1) == 0)
763 return (val >> 8) & 0xff; // EVEN, A0=0,UDS
765 return val & 0xff; // ODD , A0=1,LDS
768 /******************************************************/
770 void write_reg(unsigned int value) {
773 *(gpio + 1) = gpfsel1_o;
774 *(gpio + 2) = gpfsel2_o;
775 *(gpio + 7) = (value & 0xffff) << 8;
776 *(gpio + 10) = (~value & 0xffff) << 8;
778 GPIO_CLR = 1 << 7; // delay
783 *(gpio + 1) = gpfsel1;
784 *(gpio + 2) = gpfsel2;
787 uint16_t read_reg(void) {
792 *(gpio + 1) = gpfsel1;
793 *(gpio + 2) = gpfsel2;
795 GPIO_CLR = 1 << 6; // delay
800 return (uint16_t)(val >> 8);
804 // Set up a memory regions to access GPIO
808 if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
809 printf("can't open /dev/mem \n");
815 NULL, // Any adddress in our space will do
816 BCM2708_PERI_SIZE, // Map length
817 PROT_READ | PROT_WRITE, // Enable reading & writting to mapped memory
818 MAP_SHARED, // Shared with other processes
819 mem_fd, // File to map
820 BCM2708_PERI_BASE // Offset to GPIO peripheral
823 close(mem_fd); // No need to keep mem_fd open after mmap
825 if (gpio_map == MAP_FAILED) {
826 printf("gpio mmap error %d\n", (int)gpio_map); // errno also set!
830 gpio = ((volatile unsigned *)gpio_map) + GPIO_ADDR / 4;
831 gpclk = ((volatile unsigned *)gpio_map) + GPCLK_ADDR / 4;