]> git.sesse.net Git - pistorm/blob - emulator.c
Add dumb framebuffer RTG
[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   sched_setscheduler(0, SCHED_FIFO, &priority);
146   mlockall(MCL_CURRENT);  // lock in memory to keep us from paging out
147
148   InitGayle();
149
150   signal(SIGINT, sigint_handler);
151   setup_io();
152
153   //goto skip_everything;
154
155   // Enable 200MHz CLK output on GPIO4, adjust divider and pll source depending
156   // on pi model
157   printf("Enable 200MHz GPCLK0 on GPIO4\n");
158   gpio_enable_200mhz();
159
160   // reset cpld statemachine first
161
162   write_reg(0x01);
163   usleep(100);
164   usleep(1500);
165   write_reg(0x00);
166   usleep(100);
167
168   // reset amiga and statemachine
169   skip_everything:;
170   cpu_pulse_reset();
171   ovl = 1;
172   m68k_write_memory_8(0xbfe201, 0x0001);  // AMIGA OVL
173   m68k_write_memory_8(0xbfe001, 0x0001);  // AMIGA OVL high (ROM@0x0)
174
175   usleep(1500);
176
177   m68k_init();
178   printf("Setting CPU type to %d.\n", cpu_type);
179   m68k_set_cpu_type(cpu_type);
180   m68k_pulse_reset();
181
182   if (maprom == 1) {
183     m68k_set_reg(M68K_REG_PC, 0xF80002);
184   } else {
185     m68k_set_reg(M68K_REG_PC, 0x0);
186   }
187
188   char c = 0;
189
190   m68k_pulse_reset();
191   while (42) {
192     if (mouse_hook_enabled) {
193       if (get_mouse_status(&mouse_dx, &mouse_dy, &mouse_buttons)) {
194         //printf("Maus: %d (%.2X), %d (%.2X), B:%.2X\n", mouse_dx, mouse_dx, mouse_dy, mouse_dy, mouse_buttons);
195       }
196     }
197
198     if (cpu_emulation_running)
199       m68k_execute(loop_cycles);
200     
201     // FIXME: Rework this to use keyboard events instead.
202     /*while (get_key_char(&c)) {
203       if (c == cfg->keyboard_toggle_key && !kb_hook_enabled) {
204         kb_hook_enabled = 1;
205         printf("Keyboard hook enabled.\n");
206       }
207       else if (c == 0x1B && kb_hook_enabled) {
208         kb_hook_enabled = 0;
209         printf("Keyboard hook disabled.\n");
210       }
211       if (!kb_hook_enabled) {
212         if (c == cfg->mouse_toggle_key) {
213           mouse_hook_enabled ^= 1;
214           printf("Mouse hook %s.\n", mouse_hook_enabled ? "enabled" : "disabled");
215           mouse_dx = mouse_dy = mouse_buttons = 0;
216         }
217         if (c == 'r') {
218           cpu_emulation_running ^= 1;
219           printf("CPU emulation is now %s\n", cpu_emulation_running ? "running" : "stopped");
220         }
221         if (c == 'R') {
222           cpu_pulse_reset();
223           m68k_pulse_reset();
224           printf("CPU emulation reset.\n");
225         }
226         if (c == 'q') {
227           printf("Quitting and exiting emulator.\n");
228           goto stop_cpu_emulation;
229         }
230       }
231     }*/
232
233     //gpio_handle_irq();
234     GPIO_HANDLE_IRQ;
235   }
236
237   stop_cpu_emulation:;
238
239   if (mouse_fd != -1)
240     close(mouse_fd);
241   if (mem_fd)
242     close(mem_fd);
243
244   return 0;
245 }
246
247 void cpu_pulse_reset(void) {
248   write_reg(0x00);
249   // printf("Status Reg%x\n",read_reg());
250   usleep(100000);
251   write_reg(0x02);
252   // printf("Status Reg%x\n",read_reg());
253 }
254
255 int cpu_irq_ack(int level) {
256   printf("cpu irq ack\n");
257   return level;
258 }
259
260 static unsigned int target = 0;
261
262 #define PLATFORM_CHECK_READ(a) \
263   if (address >= cfg->custom_low && address < cfg->custom_high) { \
264     unsigned int target = 0; \
265     switch(cfg->platform->id) { \
266       case PLATFORM_AMIGA: { \
267         if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
268           return rtg_read((address & 0x0FFFFFFF), a); \
269         } \
270         if (custom_read_amiga(cfg, address, &target, a) != -1) { \
271           return target; \
272         } \
273         break; \
274       } \
275       default: \
276         break; \
277     } \
278   } \
279   if (ovl || (address >= cfg->mapped_low && address < cfg->mapped_high)) { \
280     if (handle_mapped_read(cfg, address, &target, a) != -1) \
281       return target; \
282   }
283
284 unsigned int m68k_read_memory_8(unsigned int address) {
285   PLATFORM_CHECK_READ(OP_TYPE_BYTE);
286
287     address &=0xFFFFFF;
288     return read8((uint32_t)address);
289 }
290
291 unsigned int m68k_read_memory_16(unsigned int address) {
292   PLATFORM_CHECK_READ(OP_TYPE_WORD);
293
294   if (mouse_hook_enabled) {
295     if (address == JOY0DAT) {
296       // Forward mouse valueses to Amyga.
297       unsigned short result = (mouse_dy << 8) | (mouse_dx);
298       mouse_dx = mouse_dy = 0;
299       return (unsigned int)result;
300     }
301     if (address == CIAAPRA) {
302       unsigned short result = (unsigned int)read16((uint32_t)address);
303       if (mouse_buttons & 0x01) {
304         mouse_buttons -= 1;
305         return (unsigned int)(result | 0x40);
306       }
307       else
308           return (unsigned int)result;
309     }
310     if (address == POTGOR) {
311       unsigned short result = (unsigned int)read16((uint32_t)address);
312       if (mouse_buttons & 0x02) {
313         mouse_buttons -= 2;
314         return (unsigned int)(result | 0x2);
315       }
316       else
317           return (unsigned int)result;
318     }
319   }
320
321
322   address &=0xFFFFFF;
323   return (unsigned int)read16((uint32_t)address);
324 }
325
326 unsigned int m68k_read_memory_32(unsigned int address) {
327   PLATFORM_CHECK_READ(OP_TYPE_LONGWORD);
328
329   address &=0xFFFFFF;
330   uint16_t a = read16(address);
331   uint16_t b = read16(address + 2);
332   return (a << 16) | b;
333 }
334
335 #define PLATFORM_CHECK_WRITE(a) \
336   if (address >= cfg->custom_low && address < cfg->custom_high) { \
337     switch(cfg->platform->id) { \
338       case PLATFORM_AMIGA: { \
339         if (address >= PIGFX_RTG_BASE && address < PIGFX_RTG_BASE + PIGFX_RTG_SIZE) { \
340           rtg_write((address & 0x0FFFFFFF), value, a); \
341           return; \
342         } \
343         if (custom_write_amiga(cfg, address, value, a) != -1) { \
344           return; \
345         } \
346         break; \
347       } \
348       default: \
349         break; \
350     } \
351   } \
352   if (address >= cfg->mapped_low && address < cfg->mapped_high) { \
353     if (handle_mapped_write(cfg, address, value, a) != -1) \
354       return; \
355   }
356
357 void m68k_write_memory_8(unsigned int address, unsigned int value) {
358   PLATFORM_CHECK_WRITE(OP_TYPE_BYTE);
359
360   if (address == 0xbfe001) {
361     if (ovl != (value & (1 << 0))) {
362       ovl = (value & (1 << 0));
363       printf("OVL:%x\n", ovl);
364     }
365   }
366
367   address &=0xFFFFFF;
368   write8((uint32_t)address, value);
369   return;
370 }
371
372 void m68k_write_memory_16(unsigned int address, unsigned int value) {
373   PLATFORM_CHECK_WRITE(OP_TYPE_WORD);
374
375   address &=0xFFFFFF;
376   write16((uint32_t)address, value);
377   return;
378 }
379
380 void m68k_write_memory_32(unsigned int address, unsigned int value) {
381   PLATFORM_CHECK_WRITE(OP_TYPE_LONGWORD);
382
383   address &=0xFFFFFF;
384   write16(address, value >> 16);
385   write16(address + 2, value);
386   return;
387 }