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