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