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