1 // SPDX-License-Identifier: MIT
5 #include "platforms/platforms.h"
6 #include "input/input.h"
9 #include "platforms/amiga/Gayle.h"
10 #include "platforms/amiga/amiga-registers.h"
11 #include "platforms/amiga/rtg/rtg.h"
12 #include "platforms/amiga/hunk-reloc.h"
13 #include "platforms/amiga/piscsi/piscsi.h"
14 #include "platforms/amiga/piscsi/piscsi-enums.h"
15 #include "platforms/amiga/net/pi-net.h"
16 #include "platforms/amiga/net/pi-net-enums.h"
17 #include "platforms/amiga/pistorm-dev/pistorm-dev.h"
18 #include "platforms/amiga/pistorm-dev/pistorm-dev-enums.h"
19 #include "gpio/ps_protocol.h"
33 #include <sys/ioctl.h>
36 #include <sys/types.h>
39 #define KEY_POLL_INTERVAL_MSEC 5000
41 unsigned char read_ranges;
42 unsigned int read_addr[8];
43 unsigned int read_upper[8];
44 unsigned char *read_data[8];
45 unsigned char write_ranges;
46 unsigned int write_addr[8];
47 unsigned int write_upper[8];
48 unsigned char *write_data[8];
49 address_translation_cache code_translation_cache = {0};
51 int kb_hook_enabled = 0;
52 int mouse_hook_enabled = 0;
53 int cpu_emulation_running = 1;
55 uint8_t mouse_dx = 0, mouse_dy = 0;
56 uint8_t mouse_buttons = 0;
57 uint8_t mouse_extra = 0;
59 extern uint8_t gayle_int;
60 extern uint8_t gayle_ide_enabled;
61 extern uint8_t gayle_emulation_enabled;
62 extern uint8_t gayle_a4k_int;
63 extern volatile unsigned int *gpio;
64 extern volatile uint16_t srdata;
65 extern uint8_t realtime_graphics_debug, emulator_exiting;
66 extern uint8_t rtg_on;
67 uint8_t realtime_disassembly, int2_enabled = 0;
68 uint32_t do_disasm = 0, old_level;
69 uint32_t last_irq = 8, last_last_irq = 8;
71 uint8_t end_signal = 0, load_new_config = 0;
73 char disasm_buf[4096];
75 #define KICKBASE 0xF80000
76 #define KICKSIZE 0x7FFFF
78 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
87 extern m68ki_cpu_core m68ki_cpu;
88 extern int m68ki_initial_cycles;
89 extern int m68ki_remaining_cycles;
91 #define M68K_SET_IRQ(i) old_level = CPU_INT_LEVEL; \
92 CPU_INT_LEVEL = (i << 8); \
93 if(old_level != 0x0700 && CPU_INT_LEVEL == 0x0700) \
94 m68ki_cpu.nmi_pending = TRUE;
95 #define M68K_END_TIMESLICE m68ki_initial_cycles = GET_CYCLES(); \
98 #define M68K_SET_IRQ m68k_set_irq
99 #define M68K_END_TIMESLICE m68k_end_timeslice()
102 #define NOP asm("nop"); asm("nop"); asm("nop"); asm("nop");
104 #define DEBUG_EMULATOR
105 #ifdef DEBUG_EMULATOR
111 // Configurable emulator options
112 unsigned int cpu_type = M68K_CPU_TYPE_68000;
113 unsigned int loop_cycles = 300, irq_status = 0;
114 struct emulator_config *cfg = NULL;
115 char keyboard_file[256] = "/dev/input/event1";
117 uint64_t trig_irq = 0, serv_irq = 0;
118 uint16_t irq_delay = 0;
119 unsigned int amiga_reset=0, amiga_reset_last=0;
120 unsigned int do_reset=0;
122 void *ipl_task(void *args) {
123 printf("IPL thread running\n");
124 uint16_t old_irq = 0;
128 value = *(gpio + 13);
130 if (!(value & (1 << PIN_IPL_ZERO))) {
155 amiga_reset=(value & (1 << PIN_RESET));
156 if(amiga_reset!=amiga_reset_last)
158 amiga_reset_last=amiga_reset;
161 printf("Amiga Reset is down...\n");
167 printf("Amiga Reset is up...\n");
172 /*if (gayle_ide_enabled) {
173 if (((gayle_int & 0x80) || gayle_a4k_int) && (get_ide(0)->drive[0].intrq || get_ide(0)->drive[1].intrq)) {
174 //get_ide(0)->drive[0].intrq = 0;
183 NOP NOP NOP NOP NOP NOP NOP NOP
184 //NOP NOP NOP NOP NOP NOP NOP NOP
185 //NOP NOP NOP NOP NOP NOP NOP NOP
186 /*NOP NOP NOP NOP NOP NOP NOP NOP
187 NOP NOP NOP NOP NOP NOP NOP NOP
188 NOP NOP NOP NOP NOP NOP NOP NOP*/
197 if (mouse_hook_enabled) {
198 get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons, &mouse_extra);
201 if (realtime_disassembly && (do_disasm || cpu_emulation_running)) {
202 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
203 printf("REGA: 0:$%.8X 1:$%.8X 2:$%.8X 3:$%.8X 4:$%.8X 5:$%.8X 6:$%.8X 7:$%.8X\n", m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1), m68k_get_reg(NULL, M68K_REG_A2), m68k_get_reg(NULL, M68K_REG_A3), \
204 m68k_get_reg(NULL, M68K_REG_A4), m68k_get_reg(NULL, M68K_REG_A5), m68k_get_reg(NULL, M68K_REG_A6), m68k_get_reg(NULL, M68K_REG_A7));
205 printf("REGD: 0:$%.8X 1:$%.8X 2:$%.8X 3:$%.8X 4:$%.8X 5:$%.8X 6:$%.8X 7:$%.8X\n", m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2), m68k_get_reg(NULL, M68K_REG_D3), \
206 m68k_get_reg(NULL, M68K_REG_D4), m68k_get_reg(NULL, M68K_REG_D5), m68k_get_reg(NULL, M68K_REG_D6), m68k_get_reg(NULL, M68K_REG_D7));
207 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
213 if (cpu_emulation_running)
214 m68k_execute(loop_cycles);
219 last_irq = ((read_reg() & 0xe000) >> 13);
220 if (last_irq != last_last_irq) {
221 last_last_irq = last_irq;
222 M68K_SET_IRQ(last_irq);
226 if (gayleirq && int2_enabled) {
227 write16(0xdff09c, 0x8000 | (1 << 3) && last_irq != 2);
228 last_last_irq = last_irq;
239 last_last_irq = last_irq;
246 usleep(1000000); // 1sec
248 // while(amiga_reset==0);
249 // printf("CPU emulation reset.\n");
252 if (mouse_hook_enabled && (mouse_extra != 0x00)) {
253 // mouse wheel events have occurred; unlike l/m/r buttons, these are queued as keypresses, so add to end of buffer
254 switch (mouse_extra) {
257 queue_keypress(0xfe, KEYPRESS_PRESS, PLATFORM_AMIGA);
261 queue_keypress(0xff, KEYPRESS_PRESS, PLATFORM_AMIGA);
265 // dampen the scroll wheel until next while loop iteration
269 if (load_new_config) {
270 printf("[CPU] Loading new config file.\n");
271 goto stop_cpu_emulation;
275 goto stop_cpu_emulation;
280 printf("[CPU] End of CPU thread\n");
284 void *keyboard_task() {
285 struct pollfd kbdpoll[1];
287 char c = 0, c_code = 0, c_type = 0;
288 char grab_message[] = "[KBD] Grabbing keyboard from input layer\n",
289 ungrab_message[] = "[KBD] Ungrabbing keyboard\n";
291 printf("[KBD] Keyboard thread started\n");
293 // because we permit the keyboard to be grabbed on startup, quickly check if we need to grab it
294 if (kb_hook_enabled && cfg->keyboard_grab) {
295 printf(grab_message);
296 grab_device(keyboard_fd);
299 kbdpoll[0].fd = keyboard_fd;
300 kbdpoll[0].events = POLLIN;
303 kpollrc = poll(kbdpoll, 1, KEY_POLL_INTERVAL_MSEC);
304 if ((kpollrc > 0) && (kbdpoll[0].revents & POLLHUP)) {
305 // in the event that a keyboard is unplugged, keyboard_task will whiz up to 100% utilisation
306 // this is undesired, so if the keyboard HUPs, end the thread without ending the emulation
307 printf("[KBD] Keyboard node returned HUP (unplugged?)\n");
311 // if kpollrc > 0 then it contains number of events to pull, also check if POLLIN is set in revents
312 if ((kpollrc <= 0) || !(kbdpoll[0].revents & POLLIN)) {
316 while (get_key_char(&c, &c_code, &c_type)) {
317 if (c && c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
319 printf("[KBD] Keyboard hook enabled.\n");
320 if (cfg->keyboard_grab) {
321 grab_device(keyboard_fd);
322 printf(grab_message);
324 } else if (kb_hook_enabled) {
325 if (c == 0x1B && c_type) {
327 printf("[KBD] Keyboard hook disabled.\n");
328 if (cfg->keyboard_grab) {
329 release_device(keyboard_fd);
330 printf(ungrab_message);
333 if (queue_keypress(c_code, c_type, cfg->platform->id) && int2_enabled && last_irq != 2) {
340 // pause pressed; trigger nmi (int level 7)
341 if (c == 0x01 && c_type) {
342 printf("[INT] Sending NMI\n");
346 if (!kb_hook_enabled && c_type) {
347 if (c && c == cfg->mouse_toggle_key) {
348 mouse_hook_enabled ^= 1;
349 printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
350 mouse_dx = mouse_dy = mouse_buttons = mouse_extra = 0;
353 cpu_emulation_running ^= 1;
354 printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
357 realtime_graphics_debug ^= 1;
358 printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
362 //m68k_pulse_reset();
363 printf("CPU emulation reset.\n");
366 printf("Quitting and exiting emulator.\n");
371 realtime_disassembly ^= 1;
373 printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
376 int r = get_mapped_item_by_address(cfg, 0x08000000);
378 printf("Dumping first 16MB of mapped range %d.\n", r);
379 FILE *dmp = fopen("./memdmp.bin", "wb+");
380 fwrite(cfg->map_data[r], 16 * SIZE_MEGA, 1, dmp);
384 if (c == 's' && realtime_disassembly) {
387 if (c == 'S' && realtime_disassembly) {
396 printf("[KBD] Keyboard thread ending\n");
397 if (cfg->keyboard_grab) {
398 printf(ungrab_message);
399 release_device(keyboard_fd);
404 void stop_cpu_emulation(uint8_t disasm_cur) {
407 m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
408 printf("REGA: 0:$%.8X 1:$%.8X 2:$%.8X 3:$%.8X 4:$%.8X 5:$%.8X 6:$%.8X 7:$%.8X\n", m68k_get_reg(NULL, M68K_REG_A0), m68k_get_reg(NULL, M68K_REG_A1), m68k_get_reg(NULL, M68K_REG_A2), m68k_get_reg(NULL, M68K_REG_A3), \
409 m68k_get_reg(NULL, M68K_REG_A4), m68k_get_reg(NULL, M68K_REG_A5), m68k_get_reg(NULL, M68K_REG_A6), m68k_get_reg(NULL, M68K_REG_A7));
410 printf("REGD: 0:$%.8X 1:$%.8X 2:$%.8X 3:$%.8X 4:$%.8X 5:$%.8X 6:$%.8X 7:$%.8X\n", m68k_get_reg(NULL, M68K_REG_D0), m68k_get_reg(NULL, M68K_REG_D1), m68k_get_reg(NULL, M68K_REG_D2), m68k_get_reg(NULL, M68K_REG_D3), \
411 m68k_get_reg(NULL, M68K_REG_D4), m68k_get_reg(NULL, M68K_REG_D5), m68k_get_reg(NULL, M68K_REG_D6), m68k_get_reg(NULL, M68K_REG_D7));
412 printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
413 realtime_disassembly = 1;
416 cpu_emulation_running = 0;
421 static volatile unsigned char maprom;
423 void sigint_handler(int sig_num) {
425 //cpu_emulation_running = 0;
428 printf("Received sigint %d, exiting.\n", sig_num);
434 if (cfg->platform->shutdown) {
435 cfg->platform->shutdown(cfg);
438 while (!emulator_exiting) {
439 emulator_exiting = 1;
443 printf("IRQs triggered: %lld\n", trig_irq);
444 printf("IRQs serviced: %lld\n", serv_irq);
449 int main(int argc, char *argv[]) {
454 //const struct sched_param priority = {99};
456 // Some command line switch stuffles
457 for (g = 1; g < argc; g++) {
458 if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
460 printf("%s switch found, but no CPU type specified.\n", argv[g]);
463 cpu_type = get_m68k_cpu_type(argv[g]);
466 else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
468 printf("%s switch found, but no config filename specified.\n", argv[g]);
471 FILE *chk = fopen(argv[g], "rb");
473 printf("Config file %s does not exist, please check that you've specified the path correctly.\n", argv[g]);
477 set_pistorm_devcfg_filename(argv[g]);
481 else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
483 printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
486 strcpy(keyboard_file, argv[g]);
494 ps_reset_state_machine();
498 if (load_new_config != 0) {
499 uint8_t config_action = load_new_config - 1;
502 free_config_file(cfg);
507 switch(config_action) {
510 cfg = load_config_file(get_pistorm_devcfg_filename());
513 cfg = load_config_file("default.cfg");
519 printf("No config file specified. Trying to load default.cfg...\n");
520 cfg = load_config_file("default.cfg");
522 printf("Couldn't load default.cfg, empty emulator config will be used.\n");
523 cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
525 printf("Failed to allocate memory for emulator config!\n");
528 memset(cfg, 0x00, sizeof(struct emulator_config));
533 if (cfg->cpu_type) cpu_type = cfg->cpu_type;
534 if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
537 cfg->platform = make_platform_config("none", "generic");
538 cfg->platform->platform_initial_setup(cfg);
541 if (cfg->mouse_enabled) {
542 mouse_fd = open(cfg->mouse_file, O_RDWR | O_NONBLOCK);
543 if (mouse_fd == -1) {
544 printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
545 cfg->mouse_enabled = 0;
548 * *-*-*-* magic numbers! *-*-*-*
549 * great, so waaaay back in the history of the pc, the ps/2 protocol set the standard for mice
550 * and in the process, the mouse sample rate was defined as a way of putting mice into vendor-specific modes.
551 * as the ancient gpm command explains, almost everything except incredibly old mice talk the IntelliMouse
552 * protocol, which reports four bytes. by default, every mouse starts in 3-byte mode (don't report wheel or
553 * additional buttons) until imps2 magic is sent. so, command $f3 is "set sample rate", followed by a byte.
555 uint8_t mouse_init[] = { 0xf4, 0xf3, 0x64 }; // enable, then set sample rate 100
556 uint8_t imps2_init[] = { 0xf3, 0xc8, 0xf3, 0x64, 0xf3, 0x50 }; // magic sequence; set sample 200, 100, 80
557 if (write(mouse_fd, mouse_init, sizeof(mouse_init)) != -1) {
558 if (write(mouse_fd, imps2_init, sizeof(imps2_init)) == -1)
559 printf("[MOUSE] Couldn't enable scroll wheel events; is this mouse from the 1980s?\n");
561 printf("[MOUSE] Mouse didn't respond to normal PS/2 init; have you plugged a brick in by mistake?\n");
565 if (cfg->keyboard_file)
566 keyboard_fd = open(cfg->keyboard_file, O_RDONLY | O_NONBLOCK);
568 keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
570 if (keyboard_fd == -1) {
571 printf("Failed to open keyboard event source.\n");
574 if (cfg->mouse_autoconnect)
575 mouse_hook_enabled = 1;
577 if (cfg->keyboard_autoconnect)
582 signal(SIGINT, sigint_handler);
584 ps_reset_state_machine();
589 printf("Setting CPU type to %d.\n", cpu_type);
590 m68k_set_cpu_type(cpu_type);
593 pthread_t ipl_tid = 0, cpu_tid, kbd_tid;
596 err = pthread_create(&ipl_tid, NULL, &ipl_task, NULL);
598 printf("[ERROR] Cannot create IPL thread: [%s]", strerror(err));
600 pthread_setname_np(ipl_tid, "pistorm: ipl");
601 printf("IPL thread created successfully\n");
605 // create keyboard task
606 err = pthread_create(&kbd_tid, NULL, &keyboard_task, NULL);
608 printf("[ERROR] Cannot create keyboard thread: [%s]", strerror(err));
610 pthread_setname_np(kbd_tid, "pistorm: kbd");
611 printf("[MAIN] Keyboard thread created successfully\n");
615 err = pthread_create(&cpu_tid, NULL, &cpu_task, NULL);
617 printf("[ERROR] Cannot create CPU thread: [%s]", strerror(err));
619 pthread_setname_np(cpu_tid, "pistorm: cpu");
620 printf("[MAIN] CPU thread created successfully\n");
623 // wait for cpu task to end before closing up and finishing
624 pthread_join(cpu_tid, NULL);
626 while (!emulator_exiting) {
627 emulator_exiting = 1;
631 if (load_new_config == 0)
632 printf("[MAIN] All threads appear to have concluded; ending process\n");
639 if (load_new_config != 0)
642 if (cfg->platform->shutdown) {
643 cfg->platform->shutdown(cfg);
649 void cpu_pulse_reset(void) {
651 if (cfg->platform->handle_reset)
652 cfg->platform->handle_reset(cfg);
654 //m68k_write_memory_16(INTENA, 0x7FFF);
656 //m68k_write_memory_8(0xbfe201, 0x0001); // AMIGA OVL
657 //m68k_write_memory_8(0xbfe001, 0x0001); // AMIGA OVL high (ROM@0x0)
662 int cpu_irq_ack(int level) {
663 printf("cpu irq ack\n");
667 static unsigned int target = 0;
668 static uint8_t send_keypress = 0;
669 static uint32_t platform_res, rres;
671 uint8_t cdtv_dmac_reg_idx_read();
672 void cdtv_dmac_reg_idx_write(uint8_t value);
673 uint32_t cdtv_dmac_read(uint32_t address, uint8_t type);
674 void cdtv_dmac_write(uint32_t address, uint32_t value, uint8_t type);
676 static inline void inline_write_16(unsigned int address, unsigned int data) {
677 *(gpio + 0) = GPFSEL0_OUTPUT;
678 *(gpio + 1) = GPFSEL1_OUTPUT;
679 *(gpio + 2) = GPFSEL2_OUTPUT;
681 *(gpio + 7) = ((data & 0xffff) << 8) | (REG_DATA << PIN_A0);
682 *(gpio + 7) = 1 << PIN_WR;
683 *(gpio + 10) = 1 << PIN_WR;
684 *(gpio + 10) = 0xffffec;
686 *(gpio + 7) = ((address & 0xffff) << 8) | (REG_ADDR_LO << PIN_A0);
687 *(gpio + 7) = 1 << PIN_WR;
688 *(gpio + 10) = 1 << PIN_WR;
689 *(gpio + 10) = 0xffffec;
691 *(gpio + 7) = ((0x0000 | (address >> 16)) << 8) | (REG_ADDR_HI << PIN_A0);
692 *(gpio + 7) = 1 << PIN_WR;
693 *(gpio + 10) = 1 << PIN_WR;
694 *(gpio + 10) = 0xffffec;
696 *(gpio + 0) = GPFSEL0_INPUT;
697 *(gpio + 1) = GPFSEL1_INPUT;
698 *(gpio + 2) = GPFSEL2_INPUT;
700 while (*(gpio + 13) & (1 << PIN_TXN_IN_PROGRESS))
704 static inline void inline_write_8(unsigned int address, unsigned int data) {
705 if ((address & 1) == 0)
706 data = data + (data << 8); // EVEN, A0=0,UDS
708 data = data & 0xff; // ODD , A0=1,LDS
710 *(gpio + 0) = GPFSEL0_OUTPUT;
711 *(gpio + 1) = GPFSEL1_OUTPUT;
712 *(gpio + 2) = GPFSEL2_OUTPUT;
714 *(gpio + 7) = ((data & 0xffff) << 8) | (REG_DATA << PIN_A0);
715 *(gpio + 7) = 1 << PIN_WR;
716 *(gpio + 10) = 1 << PIN_WR;
717 *(gpio + 10) = 0xffffec;
719 *(gpio + 7) = ((address & 0xffff) << 8) | (REG_ADDR_LO << PIN_A0);
720 *(gpio + 7) = 1 << PIN_WR;
721 *(gpio + 10) = 1 << PIN_WR;
722 *(gpio + 10) = 0xffffec;
724 *(gpio + 7) = ((0x0100 | (address >> 16)) << 8) | (REG_ADDR_HI << PIN_A0);
725 *(gpio + 7) = 1 << PIN_WR;
726 *(gpio + 10) = 1 << PIN_WR;
727 *(gpio + 10) = 0xffffec;
729 *(gpio + 0) = GPFSEL0_INPUT;
730 *(gpio + 1) = GPFSEL1_INPUT;
731 *(gpio + 2) = GPFSEL2_INPUT;
733 while (*(gpio + 13) & (1 << PIN_TXN_IN_PROGRESS))
737 static inline void inline_write_32(unsigned int address, unsigned int value) {
738 inline_write_16(address, value >> 16);
739 inline_write_16(address + 2, value);
742 static inline unsigned int inline_read_16(unsigned int address) {
743 *(gpio + 0) = GPFSEL0_OUTPUT;
744 *(gpio + 1) = GPFSEL1_OUTPUT;
745 *(gpio + 2) = GPFSEL2_OUTPUT;
747 *(gpio + 7) = ((address & 0xffff) << 8) | (REG_ADDR_LO << PIN_A0);
748 *(gpio + 7) = 1 << PIN_WR;
749 *(gpio + 10) = 1 << PIN_WR;
750 *(gpio + 10) = 0xffffec;
752 *(gpio + 7) = ((0x0200 | (address >> 16)) << 8) | (REG_ADDR_HI << PIN_A0);
753 *(gpio + 7) = 1 << PIN_WR;
754 *(gpio + 10) = 1 << PIN_WR;
755 *(gpio + 10) = 0xffffec;
757 *(gpio + 0) = GPFSEL0_INPUT;
758 *(gpio + 1) = GPFSEL1_INPUT;
759 *(gpio + 2) = GPFSEL2_INPUT;
761 *(gpio + 7) = (REG_DATA << PIN_A0);
762 *(gpio + 7) = 1 << PIN_RD;
764 unsigned int value = *(gpio + 13);
765 while ((value=*(gpio + 13)) & (1 << PIN_TXN_IN_PROGRESS))
768 *(gpio + 10) = 0xffffec;
770 return (value >> 8) & 0xffff;
773 static inline unsigned int inline_read_8(unsigned int address) {
774 *(gpio + 0) = GPFSEL0_OUTPUT;
775 *(gpio + 1) = GPFSEL1_OUTPUT;
776 *(gpio + 2) = GPFSEL2_OUTPUT;
778 *(gpio + 7) = ((address & 0xffff) << 8) | (REG_ADDR_LO << PIN_A0);
779 *(gpio + 7) = 1 << PIN_WR;
780 *(gpio + 10) = 1 << PIN_WR;
781 *(gpio + 10) = 0xffffec;
783 *(gpio + 7) = ((0x0300 | (address >> 16)) << 8) | (REG_ADDR_HI << PIN_A0);
784 *(gpio + 7) = 1 << PIN_WR;
785 *(gpio + 10) = 1 << PIN_WR;
786 *(gpio + 10) = 0xffffec;
788 *(gpio + 0) = GPFSEL0_INPUT;
789 *(gpio + 1) = GPFSEL1_INPUT;
790 *(gpio + 2) = GPFSEL2_INPUT;
792 *(gpio + 7) = (REG_DATA << PIN_A0);
793 *(gpio + 7) = 1 << PIN_RD;
795 unsigned int value = *(gpio + 13);
796 while ((value=*(gpio + 13)) & (1 << PIN_TXN_IN_PROGRESS))
799 *(gpio + 10) = 0xffffec;
801 value = (value >> 8) & 0xffff;
803 if ((address & 1) == 0)
804 return (value >> 8) & 0xff; // EVEN, A0=0,UDS
806 return value & 0xff; // ODD , A0=1,LDS
809 static inline unsigned int inline_read_32(unsigned int address) {
810 unsigned int a = inline_read_16(address);
811 unsigned int b = inline_read_16(address + 2);
812 return (a << 16) | b;
815 static inline uint32_t ps_read(uint8_t type, uint32_t addr) {
818 return inline_read_8(addr);
820 return inline_read_16(addr);
821 case OP_TYPE_LONGWORD:
822 return inline_read_32(addr);
824 // This shouldn't actually happen.
828 static inline int32_t platform_read_check(uint8_t type, uint32_t addr, uint32_t *res) {
829 switch (cfg->platform->id) {
833 if (mouse_hook_enabled && (mouse_buttons & 0x01)) {
834 rres = (uint32_t)ps_read(type, addr);
835 *res = (rres ^ 0x40);
841 if (kb_hook_enabled) {
842 rres = (uint32_t)ps_read(type, addr);
843 if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
848 if (send_keypress == 2) {
857 if (kb_hook_enabled) {
858 rres = (uint32_t)ps_read(type, addr);
859 uint8_t c = 0, t = 0;
860 pop_queued_key(&c, &t);
862 rres = ((c << 1) | t) ^ 0xFF;
870 if (mouse_hook_enabled) {
871 unsigned short result = (mouse_dy << 8) | (mouse_dx);
872 *res = (unsigned int)result;
878 if (mouse_hook_enabled) {
879 unsigned short result = (unsigned short)ps_read(type, addr);
880 // bit 1 rmb, bit 2 mmb
881 if (mouse_buttons & 0x06) {
882 *res = (unsigned int)((result ^ ((mouse_buttons & 0x02) << 9)) // move rmb to bit 10
883 & (result ^ ((mouse_buttons & 0x04) << 6))); // move mmb to bit 8
886 *res = (unsigned int)(result & 0xfffd);
895 if (addr >= cfg->custom_low && addr < cfg->custom_high) {
896 if (addr >= PISCSI_OFFSET && addr < PISCSI_UPPER) {
897 *res = handle_piscsi_read(addr, type);
900 if (addr >= PINET_OFFSET && addr < PINET_UPPER) {
901 *res = handle_pinet_read(addr, type);
904 if (addr >= PIGFX_RTG_BASE && addr < PIGFX_UPPER) {
905 *res = rtg_read((addr & 0x0FFFFFFF), type);
908 if (custom_read_amiga(cfg, addr, &target, type) != -1) {
918 if (ovl || (addr >= cfg->mapped_low && addr < cfg->mapped_high)) {
919 if (handle_mapped_read(cfg, addr, &target, type) != -1) {
928 unsigned int m68k_read_memory_8(unsigned int address) {
929 if (platform_read_check(OP_TYPE_BYTE, address, &platform_res)) {
933 if (address & 0xFF000000)
936 return (unsigned int)inline_read_8((uint32_t)address);
939 unsigned int m68k_read_memory_16(unsigned int address) {
940 if (platform_read_check(OP_TYPE_WORD, address, &platform_res)) {
944 if (address & 0xFF000000)
947 if (address & 0x01) {
948 return ((inline_read_8(address) << 8) | inline_read_8(address + 1));
950 return (unsigned int)inline_read_16((uint32_t)address);
953 unsigned int m68k_read_memory_32(unsigned int address) {
954 if (platform_read_check(OP_TYPE_LONGWORD, address, &platform_res)) {
958 if (address & 0xFF000000)
961 if (address & 0x01) {
962 uint32_t c = inline_read_8(address);
963 c |= (be16toh(inline_read_16(address+1)) << 8);
964 c |= (inline_read_8(address + 3) << 24);
967 uint16_t a = inline_read_16(address);
968 uint16_t b = inline_read_16(address + 2);
969 return (a << 16) | b;
972 static inline int32_t platform_write_check(uint8_t type, uint32_t addr, uint32_t val) {
973 switch (cfg->platform->id) {
977 if (ovl != (val & (1 << 0))) {
978 ovl = (val & (1 << 0));
979 printf("OVL:%x\n", ovl);
984 char *serdat = (char *)&val;
985 // SERDAT word. see amiga dev docs appendix a; upper byte is control codes, and bit 0 is always 1.
986 // ignore this upper byte as it's not viewable data, only display lower byte.
987 printf("%c", serdat[0]);
992 // This code is kind of strange and should probably be reworked/revoked.
993 if (!(val & 0x8000)) {
998 else if (val & 0x04) {
1007 if (addr >= cfg->custom_low && addr < cfg->custom_high) {
1008 if (addr >= PISCSI_OFFSET && addr < PISCSI_UPPER) {
1009 handle_piscsi_write(addr, val, type);
1012 if (addr >= PINET_OFFSET && addr < PINET_UPPER) {
1013 handle_pinet_write(addr, val, type);
1016 if (addr >= PIGFX_RTG_BASE && addr < PIGFX_UPPER) {
1017 rtg_write((addr & 0x0FFFFFFF), val, type);
1020 if (custom_write_amiga(cfg, addr, val, type) != -1) {
1029 if (ovl || (addr >= cfg->mapped_low && addr < cfg->mapped_high)) {
1030 if (handle_mapped_write(cfg, addr, val, type) != -1) {
1038 void m68k_write_memory_8(unsigned int address, unsigned int value) {
1039 if (platform_write_check(OP_TYPE_BYTE, address, value))
1042 if (address & 0xFF000000)
1045 inline_write_8((uint32_t)address, value);
1049 void m68k_write_memory_16(unsigned int address, unsigned int value) {
1050 if (platform_write_check(OP_TYPE_WORD, address, value))
1053 if (address & 0xFF000000)
1056 if (address & 0x01) {
1057 inline_write_8(value & 0xFF, address);
1058 inline_write_8((value >> 8) & 0xFF, address + 1);
1062 inline_write_16((uint32_t)address, value);
1066 void m68k_write_memory_32(unsigned int address, unsigned int value) {
1067 if (platform_write_check(OP_TYPE_LONGWORD, address, value))
1070 if (address & 0xFF000000)
1073 if (address & 0x01) {
1074 inline_write_8(value & 0xFF, address);
1075 inline_write_16(htobe16(((value >> 8) & 0xFFFF)), address + 1);
1076 inline_write_8((value >> 24), address + 3);
1080 inline_write_16(address, value >> 16);
1081 inline_write_16(address + 2, value);