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 JOY0DAT 0xDFF00A
75 #define JOY1DAT 0xDFF00C
76 #define CIAAPRA 0xBFE001
77 #define POTGOR 0xDFF016
79 int kb_hook_enabled = 0;
80 int mouse_hook_enabled = 0;
81 int cpu_emulation_running = 1;
83 char mouse_dx = 0, mouse_dy = 0;
84 char mouse_buttons = 0;
86 #define KICKBASE 0xF80000
87 #define KICKSIZE 0x7FFFF
89 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
91 int gayle_emulation_enabled = 1;
95 // Configurable emulator options
96 unsigned int cpu_type = M68K_CPU_TYPE_68000;
97 unsigned int loop_cycles = 300;
98 struct emulator_config *cfg = NULL;
99 char keyboard_file[256] = "/dev/input/event0";
102 volatile unsigned int *gpio;
103 volatile unsigned int *gpclk;
104 volatile unsigned int gpfsel0;
105 volatile unsigned int gpfsel1;
106 volatile unsigned int gpfsel2;
107 volatile unsigned int gpfsel0_o;
108 volatile unsigned int gpfsel1_o;
109 volatile unsigned int gpfsel2_o;
111 // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or
113 #define INP_GPIO(g) *(gpio + ((g) / 10)) &= ~(7 << (((g) % 10) * 3))
114 #define OUT_GPIO(g) *(gpio + ((g) / 10)) |= (1 << (((g) % 10) * 3))
115 #define SET_GPIO_ALT(g, a) \
116 *(gpio + (((g) / 10))) |= \
117 (((a) <= 3 ? (a) + 4 : (a) == 4 ? 3 : 2) << (((g) % 10) * 3))
120 *(gpio + 7) // sets bits which are 1 ignores bits which are 0
122 *(gpio + 10) // clears bits which are 1 ignores bits which are 0
124 #define GET_GPIO(g) (*(gpio + 13) & (1 << g)) // 0 if LOW, (1<<g) if HIGH
126 #define GPIO_PULL *(gpio + 37) // Pull up/pull down
127 #define GPIO_PULLCLK0 *(gpio + 38) // Pull up/pull down clock
131 uint32_t read8(uint32_t address);
132 void write8(uint32_t address, uint32_t data);
134 uint32_t read16(uint32_t address);
135 void write16(uint32_t address, uint32_t data);
137 void write32(uint32_t address, uint32_t data);
138 uint32_t read32(uint32_t address);
140 uint16_t read_reg(void);
141 void write_reg(unsigned int value);
143 volatile uint16_t srdata;
144 volatile uint32_t srdata2;
145 volatile uint32_t srdata2_old;
147 //unsigned char g_kick[524288];
148 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
149 unsigned char toggle;
151 static volatile unsigned char maprom;
153 void sigint_handler(int sig_num) {
155 //cpu_emulation_running = 0;
158 printf("Received sigint %d, exiting.\n", sig_num);
167 void *iplThread(void *args) {
168 printf("IPL thread running/n");
172 if (GET_GPIO(1) == 0) {
174 m68k_end_timeslice();
175 //printf("thread!/n");
184 int main(int argc, char *argv[]) {
186 const struct sched_param priority = {99};
188 // Some command line switch stuffles
189 for (g = 1; g < argc; g++) {
190 if (strcmp(argv[g], "--disable-gayle") == 0) {
191 gayle_emulation_enabled = 0;
193 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
195 printf("%s switch found, but no CPU type specified.\n", argv[g]);
198 cpu_type = get_m68k_cpu_type(argv[g]);
201 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
203 printf("%s switch found, but no config filename specified.\n", argv[g]);
206 cfg = load_config_file(argv[g]);
209 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
211 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
214 strcpy(keyboard_file, argv[g]);
220 printf("No config file specified. Trying to load default.cfg...\n");
221 cfg = load_config_file("default.cfg");
223 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
224 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
226 printf("Failed to allocate memory for emulator config!\n");
229 memset(cfg, 0x00, sizeof(struct emulator_config));
234 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
235 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
238 cfg->platform = make_platform_config("none", "generic");
239 cfg->platform->platform_initial_setup(cfg);
242 if (cfg->mouse_enabled) {
243 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
244 if (mouse_fd == -1) {
245 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
246 cfg->mouse_enabled = 0;
250 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
251 if (keyboard_fd == -1) {
252 printf("Failed to open keyboard event source.\n");
255 sched_setscheduler(0, SCHED_FIFO, &priority);
256 mlockall(MCL_CURRENT); // lock in memory to keep us from paging out
260 signal(SIGINT, sigint_handler);
263 //goto skip_everything;
265 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
267 printf("Enable 200MHz GPCLK0 on GPIO4\n");
269 *(gpclk + (CLK_GP0_CTL / 4)) = CLK_PASSWD | (1 << 5);
271 while ((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7))
274 *(gpclk + (CLK_GP0_DIV / 4)) =
275 CLK_PASSWD | (6 << 12); // divider , 6=200MHz on pi3
277 *(gpclk + (CLK_GP0_CTL / 4)) =
278 CLK_PASSWD | 5 | (1 << 4); // pll? 6=plld, 5=pllc
280 while (((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7)) == 0)
284 SET_GPIO_ALT(4, 0); // gpclk0
294 // set gpio0 (aux0) and gpio1 (aux1) to input
298 // Set GPIO pins 6,7 and 8-23 to output
299 for (g = 6; g <= 23; g++) {
303 printf("Precalculate GPIO8-23 as Output\n");
304 gpfsel0_o = *(gpio); // store gpio ddr
305 printf("gpfsel0: %#x\n", gpfsel0_o);
306 gpfsel1_o = *(gpio + 1); // store gpio ddr
307 printf("gpfsel1: %#x\n", gpfsel1_o);
308 gpfsel2_o = *(gpio + 2); // store gpio ddr
309 printf("gpfsel2: %#x\n", gpfsel2_o);
311 // Set GPIO pins 8-23 to input
312 for (g = 8; g <= 23; g++) {
315 printf("Precalculate GPIO8-23 as Input\n");
316 gpfsel0 = *(gpio); // store gpio ddr
317 printf("gpfsel0: %#x\n", gpfsel0);
318 gpfsel1 = *(gpio + 1); // store gpio ddr
319 printf("gpfsel1: %#x\n", gpfsel1);
320 gpfsel2 = *(gpio + 2); // store gpio ddr
321 printf("gpfsel2: %#x\n", gpfsel2);
330 // reset cpld statemachine first
338 // reset amiga and statemachine
342 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
343 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
348 printf("Setting CPU type to %d.\n", cpu_type);
349 m68k_set_cpu_type(cpu_type);
353 m68k_set_reg(M68K_REG_PC, 0xF80002);
355 m68k_set_reg(M68K_REG_PC, 0x0);
361 err = pthread_create(&id, NULL, &iplThread, NULL);
363 printf("\ncan't create IPL thread :[%s]", strerror(err));
365 printf("\n IPL Thread created successfully\n");
371 if (mouse_hook_enabled) {
372 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
373 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
377 if (cpu_emulation_running)
378 m68k_execute(loop_cycles);
380 // FIXME: Rework this to use keyboard events instead.
381 /*while (get_key_char(&c)) {
382 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
384 printf("Keyboard hook enabled.\n");
386 else if (c == 0x1B && kb_hook_enabled) {
388 printf("Keyboard hook disabled.\n");
390 if (!kb_hook_enabled) {
391 if (c == cfg->mouse_toggle_key) {
392 mouse_hook_enabled ^= 1;
393 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
394 mouse_dx = mouse_dy = mouse_buttons = 0;
397 cpu_emulation_running ^= 1;
398 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
403 printf("CPU emulation reset.\n");
406 printf("Quitting and exiting emulator.\n");
407 goto stop_cpu_emulation;
414 m68k_set_irq((srdata >> 13) & 0xff);
422 if (GET_GPIO(1) == 0) {
424 m68k_set_irq((srdata >> 13) & 0xff);
426 if (CheckIrq() == 1) {
427 write16(0xdff09c, 0x8008);
446 void cpu_pulse_reset(void) {
448 // printf("Status Reg%x\n",read_reg());
451 // printf("Status Reg%x\n",read_reg());
454 int cpu_irq_ack(int level) {
455 printf("cpu irq ack\n");
459 static unsigned int target = 0;
461 #define PLATFORM_CHECK_READ(a) \
462 if (address >= cfg->custom_low && address < cfg->custom_high) { \
463 unsigned int target = 0; \
464 switch(cfg->platform->id) { \
465 case PLATFORM_AMIGA: { \
466 if (custom_read_amiga(cfg, address, &target, a) != -1) { \
475 if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
476 if (handle_mapped_read(cfg, address, &target, a) != -1) \
480 unsigned int m68k_read_memory_8(unsigned int address) {
481 PLATFORM_CHECK_READ(OP_TYPE_BYTE);
484 // if (address < 0xffffff) {
485 return read8((uint32_t)address);
491 unsigned int m68k_read_memory_16(unsigned int address) {
492 PLATFORM_CHECK_READ(OP_TYPE_WORD);
494 if (mouse_hook_enabled) {
495 if (address == JOY0DAT) {
496 // Forward mouse valueses to Amyga.
497 unsigned short result = (mouse_dy << 8) | (mouse_dx);
498 mouse_dx = mouse_dy = 0;
499 return (unsigned int)result;
501 if (address == CIAAPRA) {
502 unsigned short result = (unsigned int)read16((uint32_t)address);
503 if (mouse_buttons & 0x01) {
505 return (unsigned int)(result | 0x40);
508 return (unsigned int)result;
510 if (address == POTGOR) {
511 unsigned short result = (unsigned int)read16((uint32_t)address);
512 if (mouse_buttons & 0x02) {
514 return (unsigned int)(result | 0x2);
517 return (unsigned int)result;
521 // if (address < 0xffffff) {
523 return (unsigned int)read16((uint32_t)address);
529 unsigned int m68k_read_memory_32(unsigned int address) {
530 PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
532 // if (address < 0xffffff) {
534 uint16_t a = read16(address);
535 uint16_t b = read16(address + 2);
536 return (a << 16) | b;
542 #define PLATFORM_CHECK_WRITE(a) \
543 if (address >= cfg->custom_low && address < cfg->custom_high) { \
544 switch(cfg->platform->id) { \
545 case PLATFORM_AMIGA: { \
546 if (custom_write_amiga(cfg, address, value, a) != -1) { \
555 if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
556 if (handle_mapped_write(cfg, address, value, a) != -1) \
560 void m68k_write_memory_8(unsigned int address, unsigned int value) {
561 PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
563 if (address == 0xbfe001) {
564 if (ovl != (value & (1 << 0))) {
565 ovl = (value & (1 << 0));
566 printf("OVL:%x\n", ovl);
570 // if (address < 0xffffff) {
572 write8((uint32_t)address, value);
579 void m68k_write_memory_16(unsigned int address, unsigned int value) {
580 PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
582 // if (address < 0xffffff) {
584 write16((uint32_t)address, value);
590 void m68k_write_memory_32(unsigned int address, unsigned int value) {
591 PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
593 // if (address < 0xffffff) {
595 write16(address, value >> 16);
596 write16(address + 2, value);
603 inline void write16(uint32_t address, uint32_t data) {
604 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
605 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
606 uint32_t addr_l_s = (address >> 16) << 8;
607 uint32_t addr_l_r = (~address >> 16) << 8;
608 uint32_t data_s = (data & 0x0000ffff) << 8;
609 uint32_t data_r = (~data & 0x0000ffff) << 8;
611 // asm volatile ("dmb" ::: "memory");
614 *(gpio + 1) = gpfsel1_o;
615 *(gpio + 2) = gpfsel2_o;
617 *(gpio + 7) = addr_h_s;
618 *(gpio + 10) = addr_h_r;
622 *(gpio + 7) = addr_l_s;
623 *(gpio + 10) = addr_l_r;
628 *(gpio + 7) = data_s;
629 *(gpio + 10) = data_r;
634 *(gpio + 1) = gpfsel1;
635 *(gpio + 2) = gpfsel2;
636 while ((GET_GPIO(0)))
638 // asm volatile ("dmb" ::: "memory");
641 inline void write8(uint32_t address, uint32_t data) {
642 if ((address & 1) == 0)
643 data = data + (data << 8); // EVEN, A0=0,UDS
645 data = data & 0xff; // ODD , A0=1,LDS
646 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
647 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
648 uint32_t addr_l_s = (address >> 16) << 8;
649 uint32_t addr_l_r = (~address >> 16) << 8;
650 uint32_t data_s = (data & 0x0000ffff) << 8;
651 uint32_t data_r = (~data & 0x0000ffff) << 8;
653 // asm volatile ("dmb" ::: "memory");
656 *(gpio + 1) = gpfsel1_o;
657 *(gpio + 2) = gpfsel2_o;
659 *(gpio + 7) = addr_h_s;
660 *(gpio + 10) = addr_h_r;
664 *(gpio + 7) = addr_l_s;
665 *(gpio + 10) = addr_l_r;
670 *(gpio + 7) = data_s;
671 *(gpio + 10) = data_r;
676 *(gpio + 1) = gpfsel1;
677 *(gpio + 2) = gpfsel2;
678 while ((GET_GPIO(0)))
680 // asm volatile ("dmb" ::: "memory");
683 inline uint32_t read16(uint32_t address) {
685 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
686 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
687 uint32_t addr_l_s = (address >> 16) << 8;
688 uint32_t addr_l_r = (~address >> 16) << 8;
690 // asm volatile ("dmb" ::: "memory");
693 *(gpio + 1) = gpfsel1_o;
694 *(gpio + 2) = gpfsel2_o;
696 *(gpio + 7) = addr_h_s;
697 *(gpio + 10) = addr_h_r;
701 *(gpio + 7) = addr_l_s;
702 *(gpio + 10) = addr_l_r;
708 *(gpio + 1) = gpfsel1;
709 *(gpio + 2) = gpfsel2;
711 while (!(GET_GPIO(0)))
716 // asm volatile ("dmb" ::: "memory");
717 return (val >> 8) & 0xffff;
720 inline uint32_t read8(uint32_t address) {
722 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
723 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
724 uint32_t addr_l_s = (address >> 16) << 8;
725 uint32_t addr_l_r = (~address >> 16) << 8;
727 // asm volatile ("dmb" ::: "memory");
730 *(gpio + 1) = gpfsel1_o;
731 *(gpio + 2) = gpfsel2_o;
733 *(gpio + 7) = addr_h_s;
734 *(gpio + 10) = addr_h_r;
738 *(gpio + 7) = addr_l_s;
739 *(gpio + 10) = addr_l_r;
745 *(gpio + 1) = gpfsel1;
746 *(gpio + 2) = gpfsel2;
749 while (!(GET_GPIO(0)))
754 // asm volatile ("dmb" ::: "memory");
756 val = (val >> 8) & 0xffff;
757 if ((address & 1) == 0)
758 return (val >> 8) & 0xff; // EVEN, A0=0,UDS
760 return val & 0xff; // ODD , A0=1,LDS
763 /******************************************************/
765 void write_reg(unsigned int value) {
768 *(gpio + 1) = gpfsel1_o;
769 *(gpio + 2) = gpfsel2_o;
770 *(gpio + 7) = (value & 0xffff) << 8;
771 *(gpio + 10) = (~value & 0xffff) << 8;
773 GPIO_CLR = 1 << 7; // delay
778 *(gpio + 1) = gpfsel1;
779 *(gpio + 2) = gpfsel2;
782 uint16_t read_reg(void) {
787 *(gpio + 1) = gpfsel1;
788 *(gpio + 2) = gpfsel2;
790 GPIO_CLR = 1 << 6; // delay
795 return (uint16_t)(val >> 8);
799 // Set up a memory regions to access GPIO
803 if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
804 printf("can't open /dev/mem \n");
810 NULL, // Any adddress in our space will do
811 BCM2708_PERI_SIZE, // Map length
812 PROT_READ | PROT_WRITE, // Enable reading & writting to mapped memory
813 MAP_SHARED, // Shared with other processes
814 mem_fd, // File to map
815 BCM2708_PERI_BASE // Offset to GPIO peripheral
818 close(mem_fd); // No need to keep mem_fd open after mmap
820 if (gpio_map == MAP_FAILED) {
821 printf("gpio mmap error %d\n", (int)gpio_map); // errno also set!
825 gpio = ((volatile unsigned *)gpio_map) + GPIO_ADDR / 4;
826 gpclk = ((volatile unsigned *)gpio_map) + GPCLK_ADDR / 4;