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