]> git.sesse.net Git - pistorm/blob - emulator.c
Add working keyboard forwarding for Amiga
[pistorm] / emulator.c
1 #include <assert.h>
2 #include <dirent.h>
3 #include <endian.h>
4 #include <fcntl.h>
5 #include <pthread.h>
6 #include <sched.h>
7 #include <signal.h>
8 #include <stdint.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <sys/mman.h>
13 #include <sys/stat.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16 #include <sys/ioctl.h>
17 #include "m68k.h"
18 #include "main.h"
19 #include "platforms/platforms.h"
20 #include "input/input.h"
21
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"
27
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];
36
37 int kb_hook_enabled = 0;
38 int mouse_hook_enabled = 0;
39 int cpu_emulation_running = 1;
40
41 char mouse_dx = 0, mouse_dy = 0;
42 char mouse_buttons = 0;
43
44 extern volatile unsigned int *gpio;
45 extern volatile uint16_t srdata;
46 extern uint8_t realtime_graphics_debug;
47 uint8_t realtime_disassembly;
48
49 char disasm_buf[4096];
50
51 #define KICKBASE 0xF80000
52 #define KICKSIZE 0x7FFFF
53
54 int mem_fd, mouse_fd = -1, keyboard_fd = -1;
55 int mem_fd_gpclk;
56 int gayle_emulation_enabled = 1;
57 int irq;
58 int gayleirq;
59
60 void *iplThread(void *args) {
61   printf("IPL thread running\n");
62
63   while (1) {
64     if (!gpio_get_irq()) {
65       irq = 1;
66       m68k_end_timeslice();
67     }
68     else
69       irq = 0;
70
71     if (gayle_emulation_enabled) {
72       if ((gayle_int & 0x80) && get_ide(0)->drive->intrq) {
73         gayleirq = 1;
74         m68k_end_timeslice();
75       }
76       else
77         gayleirq = 0;
78     }
79     usleep(0);
80   }
81   return args;
82 }
83
84
85 // Configurable emulator options
86 unsigned int cpu_type = M68K_CPU_TYPE_68000;
87 unsigned int loop_cycles = 300;
88 struct emulator_config *cfg = NULL;
89 char keyboard_file[256] = "/dev/input/event1";
90
91 //unsigned char g_kick[524288];
92 //unsigned char g_ram[FASTSIZE + 1]; /* RAM */
93 int ovl;
94 static volatile unsigned char maprom;
95
96 void sigint_handler(int sig_num) {
97   //if (sig_num) { }
98   //cpu_emulation_running = 0;
99
100   //return;
101   printf("Received sigint %d, exiting.\n", sig_num);
102   if (mouse_fd != -1)
103     close(mouse_fd);
104   if (mem_fd)
105     close(mem_fd);
106
107   if (cfg->platform->shutdown) {
108     cfg->platform->shutdown(cfg);
109   }
110
111   exit(0);
112 }
113
114 int main(int argc, char *argv[]) {
115   int g;
116   const struct sched_param priority = {99};
117
118   // Some command line switch stuffles
119   for (g = 1; g < argc; g++) {
120     if (strcmp(argv[g], "--disable-gayle") == 0) {
121       gayle_emulation_enabled = 0;
122     }
123     else if (strcmp(argv[g], "--cpu_type") == 0 || strcmp(argv[g], "--cpu") == 0) {
124       if (g + 1 >= argc) {
125         printf("%s switch found, but no CPU type specified.\n", argv[g]);
126       } else {
127         g++;
128         cpu_type = get_m68k_cpu_type(argv[g]);
129       }
130     }
131     else if (strcmp(argv[g], "--config-file") == 0 || strcmp(argv[g], "--config") == 0) {
132       if (g + 1 >= argc) {
133         printf("%s switch found, but no config filename specified.\n", argv[g]);
134       } else {
135         g++;
136         cfg = load_config_file(argv[g]);
137       }
138     }
139     else if (strcmp(argv[g], "--keyboard-file") == 0 || strcmp(argv[g], "--kbfile") == 0) {
140       if (g + 1 >= argc) {
141         printf("%s switch found, but no keyboard device path specified.\n", argv[g]);
142       } else {
143         g++;
144         strcpy(keyboard_file, argv[g]);
145       }
146     }
147   }
148
149   if (!cfg) {
150     printf("No config file specified. Trying to load default.cfg...\n");
151     cfg = load_config_file("default.cfg");
152     if (!cfg) {
153       printf("Couldn't load default.cfg, empty emulator config will be used.\n");
154       cfg = (struct emulator_config *)calloc(1, sizeof(struct emulator_config));
155       if (!cfg) {
156         printf("Failed to allocate memory for emulator config!\n");
157         return 1;
158       }
159       memset(cfg, 0x00, sizeof(struct emulator_config));
160     }
161   }
162
163   if (cfg) {
164     if (cfg->cpu_type) cpu_type = cfg->cpu_type;
165     if (cfg->loop_cycles) loop_cycles = cfg->loop_cycles;
166
167     if (!cfg->platform)
168       cfg->platform = make_platform_config("none", "generic");
169     cfg->platform->platform_initial_setup(cfg);
170   }
171
172   if (cfg->mouse_enabled) {
173     mouse_fd = open(cfg->mouse_file, O_RDONLY | O_NONBLOCK);
174     if (mouse_fd == -1) {
175       printf("Failed to open %s, can't enable mouse hook.\n", cfg->mouse_file);
176       cfg->mouse_enabled = 0;
177     }
178   }
179
180   keyboard_fd = open(keyboard_file, O_RDONLY | O_NONBLOCK);
181   if (keyboard_fd == -1) {
182     printf("Failed to open keyboard event source.\n");
183   }
184
185   InitGayle();
186
187   signal(SIGINT, sigint_handler);
188   setup_io();
189
190   //goto skip_everything;
191
192   // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
193   // on pi model
194   printf("Enable 200MHz GPCLK0 on GPIO4\n");
195   gpio_enable_200mhz();
196
197   // reset cpld statemachine first
198
199   write_reg(0x01);
200   usleep(100);
201   usleep(1500);
202   write_reg(0x00);
203   usleep(100);
204
205   // reset amiga and statemachine
206   skip_everything:;
207
208   usleep(1500);
209
210   m68k_init();
211   printf("Setting CPU type to %d.\n", cpu_type);
212   m68k_set_cpu_type(cpu_type);
213   cpu_pulse_reset();
214
215   if (maprom == 1) {
216     m68k_set_reg(M68K_REG_PC, 0xF80002);
217   } else {
218     m68k_set_reg(M68K_REG_PC, 0x0);
219   }
220
221   char c = 0, c_code = 0, c_type = 0;
222
223   pthread_t id;
224   int err;
225   err = pthread_create(&id, NULL, &iplThread, NULL);
226   if (err != 0)
227     printf("can't create IPL thread :[%s]", strerror(err));
228   else
229     printf("IPL Thread created successfully\n");
230
231   m68k_pulse_reset();
232   while (42) {
233     if (mouse_hook_enabled) {
234       get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons);
235     }
236
237     if (cpu_emulation_running)
238       m68k_execute(loop_cycles);
239
240 disasm_run:;
241     if (realtime_disassembly) {
242       m68k_execute(1);
243       m68k_disassemble(disasm_buf, m68k_get_reg(NULL, M68K_REG_PC), cpu_type);
244       printf("%.8X (%.8X)]] %s\n", m68k_get_reg(NULL, M68K_REG_PC), (m68k_get_reg(NULL, M68K_REG_PC) & 0xFFFFFF), disasm_buf);
245     }
246     
247     if (irq) {
248       unsigned int status = read_reg();
249       m68k_set_irq((status & 0xe000) >> 13);
250     }
251     else if (gayleirq) {
252       write16(0xdff09c, 0x8000 | (1 << 3));
253       //PAULA_SET_IRQ(3); // IRQ 3 = INT2
254       m68k_set_irq(2);
255     }
256     else {
257         m68k_set_irq(0);
258     }
259
260   //usleep(0);
261     // FIXME: Rework this to use keyboard events instead.
262     while (get_key_char(&c, &c_code, &c_type)) {
263
264       if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
265         kb_hook_enabled = 1;
266         printf("Keyboard hook enabled.\n");
267       }
268       else if (kb_hook_enabled) {
269         if (c == 0x1B && c_type) {
270           kb_hook_enabled = 0;
271           printf("Keyboard hook disabled.\n");
272         }
273         else {
274           /*printf("Key code: %.2X - ", c_code);
275           switch (c_type) {
276             case 0:
277               printf("released.\n");
278               break;
279             case 1:
280               printf("pressed.\n");
281               break;
282             case 2:
283               printf("repeat.\n");
284               break;
285             default:
286               printf("unknown.\n");
287               break;
288           }*/
289           if (queue_keypress(c_code, c_type, cfg->platform->id)) {
290             m68k_set_irq(2);
291           }
292         }
293       }
294
295       if (!kb_hook_enabled && c_type) {
296         if (c == cfg->mouse_toggle_key) {
297           mouse_hook_enabled ^= 1;
298           printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
299           mouse_dx = mouse_dy = mouse_buttons = 0;
300         }
301         if (c == 'r') {
302           cpu_emulation_running ^= 1;
303           printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
304         }
305         if (c == 'g') {
306           realtime_graphics_debug ^= 1;
307           printf("Real time graphics debug is now %s\n", realtime_graphics_debug ? "on" : "off");
308         }
309         if (c == 'R') {
310           cpu_pulse_reset();
311           m68k_pulse_reset();
312           printf("CPU emulation reset.\n");
313         }
314         if (c == 'q') {
315           printf("Quitting and exiting emulator.\n");
316           goto stop_cpu_emulation;
317         }
318         if (c == 'd') {
319           realtime_disassembly ^= 1;
320           printf("Real time disassembly is now %s\n", realtime_disassembly ? "on" : "off");
321         }
322       }
323     }
324
325     if (realtime_disassembly)
326       goto disasm_run;
327
328     //gpio_handle_irq();
329     //GPIO_HANDLE_IRQ;
330   }
331
332   stop_cpu_emulation:;
333
334   if (mouse_fd != -1)
335     close(mouse_fd);
336   if (mem_fd)
337     close(mem_fd);
338
339   return 0;
340 }
341
342 void cpu_pulse_reset(void) {
343   write_reg(0x00);
344   // printf("Status Reg%x\n",read_reg());
345   usleep(100000);
346   write_reg(0x02);
347   // printf("Status Reg%x\n",read_reg());
348
349   ovl = 1;
350   m68k_write_memory_8(0xbfe201, 0x0001);  // AMIGA OVL
351   m68k_write_memory_8(0xbfe001, 0x0001);  // AMIGA OVL high (ROM@0x0)
352
353   m68k_pulse_reset();
354 }
355
356 int cpu_irq_ack(int level) {
357   printf("cpu irq ack\n");
358   return level;
359 }
360
361 static unsigned int target = 0;
362 static uint8_t send_keypress = 0;
363
364 #define PLATFORM_CHECK_READ(a) \
365   if (address >= cfg->custom_low && address < cfg->custom_high) { \
366     unsigned int target = 0; \
367     switch(cfg->platform->id) { \
368       case PLATFORM_AMIGA: { \
369         if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
370           return rtg_read((address & 0x0FFFFFFF), a); \
371         } \
372         if (custom_read_amiga(cfg, address, &target, a) != -1) { \
373           return target; \
374         } \
375         break; \
376       } \
377       default: \
378         break; \
379     } \
380   } \
381   if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
382     if (handle_mapped_read(cfg, address, &target, a) != -1) \
383       return target; \
384   }
385
386 unsigned int m68k_read_memory_8(unsigned int address) {
387   PLATFORM_CHECK_READ(OP_TYPE_BYTE);
388
389   if (mouse_hook_enabled) {
390     if (address == CIAAPRA) {
391       unsigned char result = (unsigned int)read8((uint32_t)address);
392       if (mouse_buttons & 0x01) {
393         //mouse_buttons -= 1;
394         return (unsigned int)(result ^ 0x40);
395       }
396       else
397           return (unsigned int)result;
398     }
399   }
400   if (kb_hook_enabled) {
401     unsigned char result = (unsigned int)read8((uint32_t)address);
402     if (address == CIAAICR) {
403       if (get_num_kb_queued() && (!send_keypress || send_keypress == 1)) {
404         result |= 0x08;
405         if (!send_keypress)
406           send_keypress = 1;
407       }
408       if (send_keypress == 2) {
409         result |= 0x02;
410         send_keypress = 0;
411       }
412       return result;
413     }
414     if (address == CIAADAT) {
415       if (send_keypress) {
416         uint8_t c = 0, t = 0;
417         pop_queued_key(&c, &t);
418         t ^= 0x01;
419         result = ((c << 1) | t) ^ 0xFF;
420         send_keypress = 2;
421       }
422       return result;
423     }
424   }
425
426   address &=0xFFFFFF;
427   return read8((uint32_t)address);
428 }
429
430 unsigned int m68k_read_memory_16(unsigned int address) {
431   PLATFORM_CHECK_READ(OP_TYPE_WORD);
432
433   if (mouse_hook_enabled) {
434     if (address == JOY0DAT) {
435       // Forward mouse valueses to Amyga.
436       unsigned short result = (mouse_dy << 8) | (mouse_dx);
437       return (unsigned int)result;
438     }
439     /*if (address == CIAAPRA) {
440       unsigned short result = (unsigned int)read16((uint32_t)address);
441       if (mouse_buttons & 0x01) {
442         return (unsigned int)(result | 0x40);
443       }
444       else
445           return (unsigned int)result;
446     }*/
447     if (address == POTGOR) {
448       unsigned short result = (unsigned int)read16((uint32_t)address);
449       if (mouse_buttons & 0x02) {
450         return (unsigned int)(result ^ (0x2 << 9));
451       }
452       else
453           return (unsigned int)(result & 0xFFFD);
454     }
455   }
456
457   address &=0xFFFFFF;
458   if (address & 0x01) {
459     return ((read8(address) << 8) | read8(address + 1));
460   }
461   return (unsigned int)read16((uint32_t)address);
462 }
463
464 unsigned int m68k_read_memory_32(unsigned int address) {
465   PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
466
467   address &=0xFFFFFF;
468   if (address & 0x01) {
469     uint32_t c = read8(address);
470     c |= (be16toh(read16(address+1)) << 8);
471     c |= (read8(address + 3) << 24);
472     return htobe32(c);
473   }
474   uint16_t a = read16(address);
475   uint16_t b = read16(address + 2);
476   return (a << 16) | b;
477 }
478
479 #define PLATFORM_CHECK_WRITE(a) \
480   if (address >= cfg->custom_low && address < cfg->custom_high) { \
481     switch(cfg->platform->id) { \
482       case PLATFORM_AMIGA: { \
483         if (address >= PIGFX_RTG_BASE && address < PIGFX_UPPER) { \
484           rtg_write((address & 0x0FFFFFFF), value, a); \
485           return; \
486         } \
487         if (custom_write_amiga(cfg, address, value, a) != -1) { \
488           return; \
489         } \
490         break; \
491       } \
492       default: \
493         break; \
494     } \
495   } \
496   if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
497     if (handle_mapped_write(cfg, address, value, a) != -1) \
498       return; \
499   }
500
501 void m68k_write_memory_8(unsigned int address, unsigned int value) {
502   PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
503
504   if (address == 0xbfe001) {
505     if (ovl != (value & (1 << 0))) {
506       ovl = (value & (1 << 0));
507       printf("OVL:%x\n", ovl);
508     }
509   }
510
511   address &=0xFFFFFF;
512   write8((uint32_t)address, value);
513   return;
514 }
515
516 void m68k_write_memory_16(unsigned int address, unsigned int value) {
517   PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
518
519   address &=0xFFFFFF;
520   write16((uint32_t)address, value);
521   return;
522 }
523
524 void m68k_write_memory_32(unsigned int address, unsigned int value) {
525   PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
526
527   address &=0xFFFFFF;
528   write16(address, value >> 16);
529   write16(address + 2, value);
530   return;
531 }