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