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