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