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