14 #include <sys/types.h>
16 #include <sys/ioctl.h>
18 #include <linux/input.h>
23 #include "config_file/config_file.h"
30 struct termios term2 = term;
31 term2.c_lflag &= ~ICANON;
32 tcsetattr(0, TCSANOW, &term2);
35 ioctl(0, FIONREAD, &byteswaiting);
37 tcsetattr(0, TCSANOW, &term);
39 return byteswaiting > 0;
42 //#define BCM2708_PERI_BASE 0x20000000 //pi0-1
43 //#define BCM2708_PERI_BASE 0xFE000000 //pi4
44 #define BCM2708_PERI_BASE 0x3F000000 // pi3
45 #define BCM2708_PERI_SIZE 0x01000000
46 #define GPIO_BASE (BCM2708_PERI_BASE + 0x200000) /* GPIO controller */
47 #define GPCLK_BASE (BCM2708_PERI_BASE + 0x101000)
48 #define GPIO_ADDR 0x200000 /* GPIO controller */
49 #define GPCLK_ADDR 0x101000
50 #define CLK_PASSWD 0x5a000000
51 #define CLK_GP0_CTL 0x070
52 #define CLK_GP0_DIV 0x074
58 #define STATUSREGADDR \
59 GPIO_CLR = 1 << SA0; \
60 GPIO_CLR = 1 << SA1; \
63 GPIO_CLR = 1 << SA0; \
64 GPIO_CLR = 1 << SA1; \
67 GPIO_SET = 1 << SA0; \
68 GPIO_CLR = 1 << SA1; \
71 GPIO_CLR = 1 << SA0; \
72 GPIO_SET = 1 << SA1; \
75 GPIO_SET = 1 << SA0; \
76 GPIO_SET = 1 << SA1; \
79 #define PAGE_SIZE (4 * 1024)
80 #define BLOCK_SIZE (4 * 1024)
82 #define GPIOSET(no, ishigh) \
87 reset |= (1 << (no)); \
90 #define FASTBASE 0x07FFFFFF
91 #define FASTSIZE 0xFFFFFFF
92 #define GAYLEBASE 0xD80000 // D7FFFF
93 #define GAYLESIZE 0x6FFFF
95 #define JOY0DAT 0xDFF00A
96 #define JOY1DAT 0xDFF00C
97 #define CIAAPRA 0xBFE001
98 #define POTGOR 0xDFF016
100 int kb_hook_enabled = 0;
101 int mouse_hook_enabled = 0;
103 char mouse_dx = 0, mouse_dy = 0;
104 char mouse_buttons = 0;
106 #define KICKBASE 0xF80000
107 #define KICKSIZE 0x7FFFF
109 int mem_fd, mouse_fd = -1;
111 int gayle_emulation_enabled = 1;
115 // Configurable emulator options
116 unsigned int cpu_type = M68K_CPU_TYPE_68000;
117 unsigned int loop_cycles = 300;
118 struct emulator_config *cfg = NULL;
121 volatile unsigned int *gpio;
122 volatile unsigned int *gpclk;
123 volatile unsigned int gpfsel0;
124 volatile unsigned int gpfsel1;
125 volatile unsigned int gpfsel2;
126 volatile unsigned int gpfsel0_o;
127 volatile unsigned int gpfsel1_o;
128 volatile unsigned int gpfsel2_o;
130 // GPIO setup macros. Always use INP_GPIO(x) before using OUT_GPIO(x) or
132 #define INP_GPIO(g) *(gpio + ((g) / 10)) &= ~(7 << (((g) % 10) * 3))
133 #define OUT_GPIO(g) *(gpio + ((g) / 10)) |= (1 << (((g) % 10) * 3))
134 #define SET_GPIO_ALT(g, a) \
135 *(gpio + (((g) / 10))) |= \
136 (((a) <= 3 ? (a) + 4 : (a) == 4 ? 3 : 2) << (((g) % 10) * 3))
139 *(gpio + 7) // sets bits which are 1 ignores bits which are 0
141 *(gpio + 10) // clears bits which are 1 ignores bits which are 0
143 #define GET_GPIO(g) (*(gpio + 13) & (1 << g)) // 0 if LOW, (1<<g) if HIGH
145 #define GPIO_PULL *(gpio + 37) // Pull up/pull down
146 #define GPIO_PULLCLK0 *(gpio + 38) // Pull up/pull down clock
150 uint32_t read8(uint32_t address);
151 void write8(uint32_t address, uint32_t data);
153 uint32_t read16(uint32_t address);
154 void write16(uint32_t address, uint32_t data);
156 void write32(uint32_t address, uint32_t data);
157 uint32_t read32(uint32_t address);
159 uint16_t read_reg(void);
160 void write_reg(unsigned int value);
162 volatile uint16_t srdata;
163 volatile uint32_t srdata2;
164 volatile uint32_t srdata2_old;
166 //unsigned char g_kick[524288];
167 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
168 unsigned char toggle;
169 static volatile unsigned char ovl;
170 static volatile unsigned char maprom;
172 void sigint_handler(int sig_num) {
174 cpu_emulation_running = 0;
179 void *iplThread(void *args) {
180 printf("IPL thread running/n");
184 if (GET_GPIO(1) == 0) {
186 m68k_end_timeslice();
187 //printf("thread!/n");
196 int main(int argc, char *argv[]) {
198 const struct sched_param priority = {99};
200 // Some command line switch stuffles
201 for (g = 1; g < argc; g++) {
202 if (strcmp(argv[g], "--disable-gayle") == 0) {
203 gayle_emulation_enabled = 0;
205 else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
207 printf("%s switch found, but no CPU type specified.\n", argv[g]);
210 cpu_type = get_m68k_cpu_type(argv[g]);
213 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
215 printf("%s switch found, but no config filename specified.\n", argv[g]);
218 cfg = load_config_file(argv[g]);
224 printf("No config file specified. Trying to load default.cfg...\n");
225 cfg = load_config_file("default.cfg");
227 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
228 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
230 printf("Failed to allocate memory for emulator config!\n");
233 memset(cfg, 0x00, sizeof(struct emulator_config));
237 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
238 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
241 if (cfg->mouse_enabled) {
242 mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
243 if (mouse_fd == -1) {
244 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
245 cfg->mouse_enabled = 0;
249 sched_setscheduler(0, SCHED_FIFO, &priority);
250 mlockall(MCL_CURRENT); // lock in memory to keep us from paging out
254 signal(SIGINT, sigint_handler);
257 //goto skip_everything;
259 // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
261 printf("Enable 200MHz GPCLK0 on GPIO4\n");
263 *(gpclk + (CLK_GP0_CTL / 4)) = CLK_PASSWD | (1 << 5);
265 while ((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7))
268 *(gpclk + (CLK_GP0_DIV / 4)) =
269 CLK_PASSWD | (6 << 12); // divider , 6=200MHz on pi3
271 *(gpclk + (CLK_GP0_CTL / 4)) =
272 CLK_PASSWD | 5 | (1 << 4); // pll? 6=plld, 5=pllc
274 while (((*(gpclk + (CLK_GP0_CTL / 4))) & (1 << 7)) == 0)
278 SET_GPIO_ALT(4, 0); // gpclk0
288 // set gpio0 (aux0) and gpio1 (aux1) to input
292 // Set GPIO pins 6,7 and 8-23 to output
293 for (g = 6; g <= 23; g++) {
297 printf("Precalculate GPIO8-23 as Output\n");
298 gpfsel0_o = *(gpio); // store gpio ddr
299 printf("gpfsel0: %#x\n", gpfsel0_o);
300 gpfsel1_o = *(gpio + 1); // store gpio ddr
301 printf("gpfsel1: %#x\n", gpfsel1_o);
302 gpfsel2_o = *(gpio + 2); // store gpio ddr
303 printf("gpfsel2: %#x\n", gpfsel2_o);
305 // Set GPIO pins 8-23 to input
306 for (g = 8; g <= 23; g++) {
309 printf("Precalculate GPIO8-23 as Input\n");
310 gpfsel0 = *(gpio); // store gpio ddr
311 printf("gpfsel0: %#x\n", gpfsel0);
312 gpfsel1 = *(gpio + 1); // store gpio ddr
313 printf("gpfsel1: %#x\n", gpfsel1);
314 gpfsel2 = *(gpio + 2); // store gpio ddr
315 printf("gpfsel2: %#x\n", gpfsel2);
324 // reset cpld statemachine first
332 // load kick.rom if present
335 fd = open("kick.rom", O_RDONLY);
337 printf("Failed loading kick.rom, using motherboard kickstart\n");
340 int size = (int)lseek(fd, 0, SEEK_END);
341 if (size == 0x40000) {
342 lseek(fd, 0, SEEK_SET);
343 read(fd, &g_kick, size);
344 lseek(fd, 0, SEEK_SET);
345 read(fd, &g_kick[0x40000], size);
347 lseek(fd, 0, SEEK_SET);
348 read(fd, &g_kick, size);
350 printf("Loaded kick.rom with size %d kib\n", size / 1024);
353 // reset amiga and statemachine
357 m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
358 m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
363 m68k_set_cpu_type(cpu_type);
367 m68k_set_reg(M68K_REG_PC, 0xF80002);
369 m68k_set_reg(M68K_REG_PC, 0x0);
375 err = pthread_create(&id, NULL, &iplThread, NULL);
377 printf("\ncan't create IPL thread :[%s]", strerror(err));
379 printf("\n IPL Thread created successfully\n");
382 int cpu_emulation_running = 1;
386 if (mouse_hook_enabled) {
387 if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
388 //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
392 if (cpu_emulation_running)
393 m68k_execute(loop_cycles);
397 if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
399 printf("Keyboard hook enabled.\n");
401 else if (c == 0x1B && kb_hook_enabled) {
403 printf("Keyboard hook disabled.\n");
405 if (c == cfg->mouse_toggle_key) {
406 mouse_hook_enabled ^= 1;
407 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
408 mouse_dx = mouse_dy = mouse_buttons = 0;
411 cpu_emulation_running ^= 1;
412 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
417 printf("CPU emulation reset.\n");
420 printf("Quitting and exiting emulator.\n");
421 goto stop_cpu_emulation;
427 m68k_set_irq((srdata >> 13) & 0xff);
435 if (GET_GPIO(1) == 0) {
437 m68k_set_irq((srdata >> 13) & 0xff);
439 if (CheckIrq() == 1) {
440 write16(0xdff09c, 0x8008);
459 void cpu_pulse_reset(void) {
461 // printf("Status Reg%x\n",read_reg());
464 // printf("Status Reg%x\n",read_reg());
467 int cpu_irq_ack(int level) {
468 printf("cpu irq ack\n");
472 static unsigned int target = 0;
474 unsigned int m68k_read_memory_8(unsigned int address) {
476 int ret = handle_mapped_read(cfg, address, &target, OP_TYPE_BYTE, ovl);
480 /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
481 return g_ram[address - FASTBASE];
485 if (address > KICKBASE && address < KICKBASE + KICKSIZE) {
486 return g_kick[address - KICKBASE];
490 if (gayle_emulation_enabled) {
491 if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
492 return readGayleB(address);
497 // if (address < 0xffffff) {
498 return read8((uint32_t)address);
504 unsigned int m68k_read_memory_16(unsigned int address) {
506 int ret = handle_mapped_read(cfg, address, &target, OP_TYPE_WORD, ovl);
511 if (mouse_hook_enabled) {
512 if (address == JOY0DAT) {
513 // Forward mouse valueses to Amyga.
514 unsigned short result = (mouse_dy << 8) | (mouse_dx);
515 mouse_dx = mouse_dy = 0;
516 return (unsigned int)result;
518 if (address == CIAAPRA) {
519 unsigned short result = (unsigned int)read16((uint32_t)address);
520 if (mouse_buttons & 0x01) {
522 return (unsigned int)(result | 0x40);
525 return (unsigned int)result;
527 if (address == POTGOR) {
528 unsigned short result = (unsigned int)read16((uint32_t)address);
529 if (mouse_buttons & 0x02) {
531 return (unsigned int)(result | 0x2);
534 return (unsigned int)result;
537 /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
538 return be16toh(*(uint16_t *)&g_ram[address - FASTBASE]);
542 if (address > KICKBASE && address < KICKBASE + KICKSIZE) {
543 return be16toh(*(uint16_t *)&g_kick[address - KICKBASE]);
547 if (gayle_emulation_enabled) {
548 if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
549 return readGayle(address);
553 // if (address < 0xffffff) {
555 return (unsigned int)read16((uint32_t)address);
561 unsigned int m68k_read_memory_32(unsigned int address) {
563 int ret = handle_mapped_read(cfg, address, &target, OP_TYPE_LONGWORD, ovl);
567 /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
568 return be32toh(*(uint32_t *)&g_ram[address - FASTBASE]);
572 if (address > KICKBASE && address < KICKBASE + KICKSIZE) {
573 return be32toh(*(uint32_t *)&g_kick[address - KICKBASE]);
577 if (gayle_emulation_enabled) {
578 if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
579 return readGayleL(address);
583 // if (address < 0xffffff) {
585 uint16_t a = read16(address);
586 uint16_t b = read16(address + 2);
587 return (a << 16) | b;
593 void m68k_write_memory_8(unsigned int address, unsigned int value) {
595 int ret = handle_mapped_write(cfg, address, value, OP_TYPE_BYTE, ovl);
599 /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
600 g_ram[address - FASTBASE] = value;
604 if (gayle_emulation_enabled) {
605 if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
606 writeGayleB(address, value);
611 if (address == 0xbfe001) {
612 ovl = (value & (1 << 0));
613 printf("OVL:%x\n", ovl);
616 // if (address < 0xffffff) {
618 write8((uint32_t)address, value);
625 void m68k_write_memory_16(unsigned int address, unsigned int value) {
627 int ret = handle_mapped_write(cfg, address, value, OP_TYPE_WORD, ovl);
631 /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
632 *(uint16_t *)&g_ram[address - FASTBASE] = htobe16(value);
636 if (gayle_emulation_enabled) {
637 if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
638 writeGayle(address, value);
643 // if (address < 0xffffff) {
645 write16((uint32_t)address, value);
651 void m68k_write_memory_32(unsigned int address, unsigned int value) {
653 int ret = handle_mapped_write(cfg, address, value, OP_TYPE_LONGWORD, ovl);
657 /*if (address > FASTBASE && address < FASTBASE + FASTSIZE) {
658 *(uint32_t *)&g_ram[address - FASTBASE] = htobe32(value);
662 if (gayle_emulation_enabled) {
663 if (address > GAYLEBASE && address < GAYLEBASE + GAYLESIZE) {
664 writeGayleL(address, value);
668 // if (address < 0xffffff) {
670 write16(address, value >> 16);
671 write16(address + 2, value);
678 void write16(uint32_t address, uint32_t data) {
679 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
680 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
681 uint32_t addr_l_s = (address >> 16) << 8;
682 uint32_t addr_l_r = (~address >> 16) << 8;
683 uint32_t data_s = (data & 0x0000ffff) << 8;
684 uint32_t data_r = (~data & 0x0000ffff) << 8;
686 // asm volatile ("dmb" ::: "memory");
689 *(gpio + 1) = gpfsel1_o;
690 *(gpio + 2) = gpfsel2_o;
692 *(gpio + 7) = addr_h_s;
693 *(gpio + 10) = addr_h_r;
697 *(gpio + 7) = addr_l_s;
698 *(gpio + 10) = addr_l_r;
703 *(gpio + 7) = data_s;
704 *(gpio + 10) = data_r;
709 *(gpio + 1) = gpfsel1;
710 *(gpio + 2) = gpfsel2;
711 while ((GET_GPIO(0)))
713 // asm volatile ("dmb" ::: "memory");
716 void write8(uint32_t address, uint32_t data) {
717 if ((address & 1) == 0)
718 data = data + (data << 8); // EVEN, A0=0,UDS
720 data = data & 0xff; // ODD , A0=1,LDS
721 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
722 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
723 uint32_t addr_l_s = (address >> 16) << 8;
724 uint32_t addr_l_r = (~address >> 16) << 8;
725 uint32_t data_s = (data & 0x0000ffff) << 8;
726 uint32_t data_r = (~data & 0x0000ffff) << 8;
728 // asm volatile ("dmb" ::: "memory");
731 *(gpio + 1) = gpfsel1_o;
732 *(gpio + 2) = gpfsel2_o;
734 *(gpio + 7) = addr_h_s;
735 *(gpio + 10) = addr_h_r;
739 *(gpio + 7) = addr_l_s;
740 *(gpio + 10) = addr_l_r;
745 *(gpio + 7) = data_s;
746 *(gpio + 10) = data_r;
751 *(gpio + 1) = gpfsel1;
752 *(gpio + 2) = gpfsel2;
753 while ((GET_GPIO(0)))
755 // asm volatile ("dmb" ::: "memory");
758 uint32_t read16(uint32_t address) {
760 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
761 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
762 uint32_t addr_l_s = (address >> 16) << 8;
763 uint32_t addr_l_r = (~address >> 16) << 8;
765 // asm volatile ("dmb" ::: "memory");
768 *(gpio + 1) = gpfsel1_o;
769 *(gpio + 2) = gpfsel2_o;
771 *(gpio + 7) = addr_h_s;
772 *(gpio + 10) = addr_h_r;
776 *(gpio + 7) = addr_l_s;
777 *(gpio + 10) = addr_l_r;
783 *(gpio + 1) = gpfsel1;
784 *(gpio + 2) = gpfsel2;
786 while (!(GET_GPIO(0)))
791 // asm volatile ("dmb" ::: "memory");
792 return (val >> 8) & 0xffff;
795 uint32_t read8(uint32_t address) {
797 uint32_t addr_h_s = (address & 0x0000ffff) << 8;
798 uint32_t addr_h_r = (~address & 0x0000ffff) << 8;
799 uint32_t addr_l_s = (address >> 16) << 8;
800 uint32_t addr_l_r = (~address >> 16) << 8;
802 // asm volatile ("dmb" ::: "memory");
805 *(gpio + 1) = gpfsel1_o;
806 *(gpio + 2) = gpfsel2_o;
808 *(gpio + 7) = addr_h_s;
809 *(gpio + 10) = addr_h_r;
813 *(gpio + 7) = addr_l_s;
814 *(gpio + 10) = addr_l_r;
820 *(gpio + 1) = gpfsel1;
821 *(gpio + 2) = gpfsel2;
824 while (!(GET_GPIO(0)))
829 // asm volatile ("dmb" ::: "memory");
831 val = (val >> 8) & 0xffff;
832 if ((address & 1) == 0)
833 return (val >> 8) & 0xff; // EVEN, A0=0,UDS
835 return val & 0xff; // ODD , A0=1,LDS
838 /******************************************************/
840 void write_reg(unsigned int value) {
843 *(gpio + 1) = gpfsel1_o;
844 *(gpio + 2) = gpfsel2_o;
845 *(gpio + 7) = (value & 0xffff) << 8;
846 *(gpio + 10) = (~value & 0xffff) << 8;
848 GPIO_CLR = 1 << 7; // delay
853 *(gpio + 1) = gpfsel1;
854 *(gpio + 2) = gpfsel2;
857 uint16_t read_reg(void) {
862 *(gpio + 1) = gpfsel1;
863 *(gpio + 2) = gpfsel2;
865 GPIO_CLR = 1 << 6; // delay
870 return (uint16_t)(val >> 8);
874 // Set up a memory regions to access GPIO
878 if ((mem_fd = open("/dev/mem", O_RDWR | O_SYNC)) < 0) {
879 printf("can't open /dev/mem \n");
885 NULL, // Any adddress in our space will do
886 BCM2708_PERI_SIZE, // Map length
887 PROT_READ | PROT_WRITE, // Enable reading & writting to mapped memory
888 MAP_SHARED, // Shared with other processes
889 mem_fd, // File to map
890 BCM2708_PERI_BASE // Offset to GPIO peripheral
893 close(mem_fd); // No need to keep mem_fd open after mmap
895 if (gpio_map == MAP_FAILED) {
896 printf("gpio mmap error %d\n", (int)gpio_map); // errno also set!
900 gpio = ((volatile unsigned *)gpio_map) + GPIO_ADDR / 4;
901 gpclk = ((volatile unsigned *)gpio_map) + GPCLK_ADDR / 4;
905 int get_mouse_status(char *x, char *y, char *b) {
906 struct input_event ie;
907 if (read(mouse_fd, &ie, sizeof(struct input_event)) != -1) {
908 *b = ((char *)&ie)[0];
909 *x = ((char *)&ie)[1];
910 *y = ((char *)&ie)[2];