PCem

view src/pc.c @ 151:55564c65aa15

Added configuration load/save options.
author TomW
date Sat Aug 23 21:32:36 2014 +0100
parents d08a822e6603
children d0d530adce12
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <stdarg.h>
4 #include "ibm.h"
5 #include "device.h"
7 #include "ali1429.h"
8 #include "amstrad.h"
9 #include "cdrom-ioctl.h"
10 #include "config.h"
11 #include "cpu.h"
12 #include "dma.h"
13 #include "fdc.h"
14 #include "sound_gus.h"
15 #include "ide.h"
16 #include "keyboard.h"
17 #include "mem.h"
18 #include "model.h"
19 #include "mouse.h"
20 #include "nvr.h"
21 #include "pic.h"
22 #include "pit.h"
23 #include "plat-joystick.h"
24 #include "plat-mouse.h"
25 #include "serial.h"
26 #include "sound.h"
27 #include "sound_cms.h"
28 #include "sound_opl.h"
29 #include "sound_sb.h"
30 #include "sound_ssi2001.h"
31 #include "timer.h"
32 #include "vid_voodoo.h"
33 #include "video.h"
35 int frame = 0;
37 int cdrom_enabled;
38 int CPUID;
39 int vid_resize, vid_api;
41 int cycles_lost = 0;
43 int clockrate;
44 int insc=0;
45 float mips,flops;
46 extern int mmuflush;
47 extern int readlnum,writelnum;
48 void fullspeed();
50 int framecount,fps;
51 int intcount;
53 int output;
54 int atfullspeed;
56 void saveconfig();
57 int infocus;
58 int mousecapture;
59 FILE *pclogf;
60 void pclog(const char *format, ...)
61 {
62 #ifndef RELEASE_BUILD
63 char buf[1024];
64 //return;
65 if (!pclogf)
66 pclogf=fopen("pclog.txt","wt");
67 //return;
68 va_list ap;
69 va_start(ap, format);
70 vsprintf(buf, format, ap);
71 va_end(ap);
72 fputs(buf,pclogf);
73 fflush(pclogf);
74 #endif
75 }
77 void fatal(const char *format, ...)
78 {
79 char buf[256];
80 // return;
81 if (!pclogf)
82 pclogf=fopen("pclog.txt","wt");
83 //return;
84 va_list ap;
85 va_start(ap, format);
86 vsprintf(buf, format, ap);
87 va_end(ap);
88 fputs(buf,pclogf);
89 fflush(pclogf);
90 dumppic();
91 dumpregs();
92 exit(-1);
93 }
95 uint8_t cgastat;
97 int pollmouse_delay = 2;
98 void pollmouse()
99 {
100 int x,y;
101 // return;
102 pollmouse_delay--;
103 if (pollmouse_delay) return;
104 pollmouse_delay = 2;
105 poll_mouse();
106 get_mouse_mickeys(&x,&y);
107 if (mouse_poll)
108 mouse_poll(x, y, mouse_b);
109 if (mousecapture) position_mouse(64,64);
110 }
112 /*PC1512 languages -
113 7=English
114 6=German
115 5=French
116 4=Spanish
117 3=Danish
118 2=Swedish
119 1=Italian
120 3,2,1 all cause the self test to fail for some reason
121 */
123 int cpuspeed2;
125 int clocks[3][12][4]=
126 {
127 {
128 {4772728,13920,59660,5965}, /*4.77MHz*/
129 {8000000,23333,110000,0}, /*8MHz*/
130 {10000000,29166,137500,0}, /*10MHz*/
131 {12000000,35000,165000,0}, /*12MHz*/
132 {16000000,46666,220000,0}, /*16MHz*/
133 },
134 {
135 {8000000,23333,110000,0}, /*8MHz*/
136 {12000000,35000,165000,0}, /*12MHz*/
137 {16000000,46666,220000,0}, /*16MHz*/
138 {20000000,58333,275000,0}, /*20MHz*/
139 {25000000,72916,343751,0}, /*25MHz*/
140 },
141 {
142 {16000000, 46666,220000,0}, /*16MHz*/
143 {20000000, 58333,275000,0}, /*20MHz*/
144 {25000000, 72916,343751,0}, /*25MHz*/
145 {33000000, 96000,454000,0}, /*33MHz*/
146 {40000000,116666,550000,0}, /*40MHz*/
147 {50000000, 72916*2,343751*2,0}, /*50MHz*/
148 {33000000*2, 96000*2,454000*2,0}, /*66MHz*/
149 {75000000, 72916*3,343751*3,0}, /*75MHz*/
150 {80000000,116666*2,550000*2,0}, /*80MHz*/
151 {100000000, 72916*4,343751*4,0}, /*100MHz*/
152 {120000000,116666*3,550000*3,0}, /*120MHz*/
153 {133000000, 96000*4,454000*4,0}, /*133MHz*/
154 }
155 };
157 int updatestatus;
158 int win_title_update=0;
161 void onesec()
162 {
163 fps=framecount;
164 framecount=0;
165 win_title_update=1;
166 }
168 void pc_reset()
169 {
170 resetx86();
171 cpu_set();
172 //timer_reset();
173 dma_reset();
174 fdc_reset();
175 pic_reset();
176 pit_reset();
177 serial_reset();
179 setpitclock(models[model].cpu[cpu_manufacturer].cpus[cpu].rspeed);
181 // sb_reset();
183 ali1429_reset();
184 // video_init();
185 }
187 void initpc()
188 {
189 char *p;
190 // allegro_init();
191 get_executable_name(pcempath,511);
192 pclog("executable_name = %s\n", pcempath);
193 p=get_filename(pcempath);
194 *p=0;
195 pclog("path = %s\n", pcempath);
197 keyboard_init();
198 mouse_init();
199 joystick_init();
201 loadconfig(NULL);
202 pclog("Config loaded\n");
204 cpuspeed2=(AT)?2:1;
205 // cpuspeed2=cpuspeed;
206 atfullspeed=0;
208 device_init();
210 initvideo();
211 mem_init();
212 loadbios();
213 mem_add_bios();
215 loaddisc(0,discfns[0]);
216 loaddisc(1,discfns[1]);
218 timer_reset();
219 sound_reset();
220 fdc_init();
222 //loadfont();
223 loadnvr();
224 sound_init();
225 resetide();
226 ioctl_open(cdrom_drive);
227 model_init();
228 video_init();
229 speaker_init();
230 sound_card_init(sound_card_current);
231 if (GUS)
232 device_add(&gus_device);
233 if (GAMEBLASTER)
234 device_add(&cms_device);
235 if (SSI2001)
236 device_add(&ssi2001_device);
238 pc_reset();
240 pit_reset();
241 /* if (romset==ROM_AMI386 || romset==ROM_AMI486) */fullspeed();
242 mem_updatecache();
243 ali1429_reset();
244 // CPUID=(is486 && (cpuspeed==7 || cpuspeed>=9));
245 // pclog("Init - CPUID %i %i\n",CPUID,cpuspeed);
246 shadowbios=0;
247 voodoo_init();
249 ioctl_reset();
250 }
252 void resetpc()
253 {
254 pc_reset();
255 // cpuspeed2=(AT)?2:1;
256 // atfullspeed=0;
257 ///* if (romset==ROM_AMI386 || romset==ROM_AMI486) */fullspeed();
258 shadowbios=0;
259 }
261 void resetpchard()
262 {
263 device_close_all();
264 device_init();
266 timer_reset();
267 sound_reset();
268 mem_resize();
269 fdc_init();
270 model_init();
271 video_init();
272 speaker_init();
273 sound_card_init(sound_card_current);
274 if (GUS)
275 device_add(&gus_device);
276 if (GAMEBLASTER)
277 device_add(&cms_device);
278 if (SSI2001)
279 device_add(&ssi2001_device);
281 pc_reset();
283 resetide();
285 loadnvr();
287 // cpuspeed2 = (AT)?2:1;
288 // atfullspeed = 0;
289 // setpitclock(models[model].cpu[cpu_manufacturer].cpus[cpu].rspeed);
291 shadowbios = 0;
292 ali1429_reset();
294 keyboard_at_reset();
296 // output=3;
298 ioctl_reset();
299 }
301 char romsets[17][40]={"IBM PC","IBM XT","Generic Turbo XT","Euro PC","Tandy 1000","Amstrad PC1512","Sinclair PC200","Amstrad PC1640","IBM AT","AMI 286 clone","Dell System 200","Misc 286","IBM AT 386","Misc 386","386 clone","486 clone","486 clone 2"};
302 char clockspeeds[3][12][16]=
303 {
304 {"4.77MHz","8MHz","10MHz","12MHz","16MHz"},
305 {"8MHz","12MHz","16MHz","20MHz","25MHz"},
306 {"16MHz","20MHz","25MHz","33MHz","40MHz","50MHz","66MHz","75MHz","80MHz","100MHz","120MHz","133MHz"},
307 };
308 int framecountx=0;
309 int sndcount=0;
310 int oldat70hz;
312 int sreadlnum,swritelnum,segareads,segawrites, scycles_lost;
314 int serial_fifo_read, serial_fifo_write;
316 int emu_fps = 0;
318 void runpc()
319 {
320 char s[200];
321 int done=0;
323 startblit();
324 clockrate = models[model].cpu[cpu_manufacturer].cpus[cpu].rspeed;
325 if (is386) exec386(models[model].cpu[cpu_manufacturer].cpus[cpu].rspeed / 100);
326 else if (AT) exec386(models[model].cpu[cpu_manufacturer].cpus[cpu].rspeed / 100);
327 else execx86(models[model].cpu[cpu_manufacturer].cpus[cpu].rspeed / 100);
328 keyboard_poll_host();
329 keyboard_process();
330 // checkkeys();
331 pollmouse();
332 poll_joystick();
333 endblit();
335 framecountx++;
336 framecount++;
337 if (framecountx>=100)
338 {
339 pclog("onesec\n");
340 framecountx=0;
341 mips=(float)insc/1000000.0f;
342 insc=0;
343 flops=(float)fpucount/1000000.0f;
344 fpucount=0;
345 sreadlnum=readlnum;
346 swritelnum=writelnum;
347 segareads=egareads;
348 segawrites=egawrites;
349 scycles_lost = cycles_lost;
350 updatestatus=1;
351 readlnum=writelnum=0;
352 egareads=egawrites=0;
353 cycles_lost = 0;
354 mmuflush=0;
355 intcount=0;
356 intcount=pitcount=0;
357 emu_fps = frames;
358 frames = 0;
359 }
360 if (win_title_update)
361 {
362 win_title_update=0;
363 sprintf(s, "PCem v8.1 - %s - %s - %s - %i%%", model_getname(), models[model].cpu[cpu_manufacturer].cpus[cpu].name, (!mousecapture) ? "Click to capture mouse" : "Press CTRL-END or middle button to release mouse", fps);
364 set_window_title(s);
365 }
366 done++;
367 frame++;
368 }
370 void fullspeed()
371 {
372 cpuspeed2=cpuspeed;
373 if (!atfullspeed)
374 {
375 printf("Set fullspeed - %i %i %i\n",is386,AT,cpuspeed2);
376 setpitclock(models[model].cpu[cpu_manufacturer].cpus[cpu].rspeed);
377 // if (is386) setpitclock(clocks[2][cpuspeed2][0]);
378 // else setpitclock(clocks[AT?1:0][cpuspeed2][0]);
379 }
380 atfullspeed=1;
381 nvr_recalc();
382 }
384 void speedchanged()
385 {
386 if (atfullspeed)
387 {
388 cpuspeed2=cpuspeed;
389 setpitclock(models[model].cpu[cpu_manufacturer].cpus[cpu].rspeed);
390 // if (is386) setpitclock(clocks[2][cpuspeed2][0]);
391 // else setpitclock(clocks[AT?1:0][cpuspeed2][0]);
392 }
393 mem_updatecache();
394 nvr_recalc();
395 }
397 void closepc()
398 {
399 atapi->exit();
400 // ioctl_close();
401 dumppic();
402 // output=7;
403 // setpitclock(clocks[0][0][0]);
404 // while (1) runpc();
405 savedisc(0);
406 savedisc(1);
407 dumpregs();
408 closevideo();
409 device_close_all();
410 }
412 /*int main()
413 {
414 initpc();
415 while (!key[KEY_F11])
416 {
417 runpc();
418 }
419 closepc();
420 return 0;
421 }
423 END_OF_MAIN();*/
425 int cga_comp=0;
427 void loadconfig(char *fn)
428 {
429 char s[512];
430 char *p;
432 if (!fn)
433 {
434 append_filename(config_file_default, pcempath, "pcem.cfg", 511);
436 config_load(config_file_default);
437 }
438 else
439 config_load(fn);
441 GAMEBLASTER = get_config_int(NULL, "gameblaster", 0);
442 GUS = get_config_int(NULL, "gus", 0);
443 SSI2001 = get_config_int(NULL, "ssi2001", 0);
445 model = get_config_int(NULL, "model", 14);
447 if (model >= model_count())
448 model = model_count() - 1;
450 romset = model_getromset();
451 cpu_manufacturer = get_config_int(NULL, "cpu_manufacturer", 0);
452 cpu = get_config_int(NULL, "cpu", 0);
454 gfxcard = get_config_int(NULL, "gfxcard", 0);
455 video_speed = get_config_int(NULL, "video_speed", 3);
456 sound_card_current = get_config_int(NULL, "sndcard", SB2);
458 p = (char *)get_config_string(NULL, "disc_a", "");
459 if (p) strcpy(discfns[0], p);
460 else strcpy(discfns[0], "");
462 p = (char *)get_config_string(NULL, "disc_b", "");
463 if (p) strcpy(discfns[1], p);
464 else strcpy(discfns[1], "");
466 mem_size = get_config_int(NULL, "mem_size", 4);
467 cdrom_drive = get_config_int(NULL, "cdrom_drive", 0);
468 cdrom_enabled = get_config_int(NULL, "cdrom_enabled", 0);
470 slowega = get_config_int(NULL, "slow_video", 1);
471 cache = get_config_int(NULL, "cache", 3);
472 cga_comp = get_config_int(NULL, "cga_composite", 0);
474 vid_resize = get_config_int(NULL, "vid_resize", 0);
475 vid_api = get_config_int(NULL, "vid_api", 0);
476 video_fullscreen_scale = get_config_int(NULL, "video_fullscreen_scale", 0);
477 video_fullscreen_first = get_config_int(NULL, "video_fullscreen_first", 1);
479 hdc[0].spt = get_config_int(NULL, "hdc_sectors", 0);
480 hdc[0].hpc = get_config_int(NULL, "hdc_heads", 0);
481 hdc[0].tracks = get_config_int(NULL, "hdc_cylinders", 0);
482 p = (char *)get_config_string(NULL, "hdc_fn", "");
483 if (p) strcpy(ide_fn[0], p);
484 else strcpy(ide_fn[0], "");
485 hdc[1].spt = get_config_int(NULL, "hdd_sectors", 0);
486 hdc[1].hpc = get_config_int(NULL, "hdd_heads", 0);
487 hdc[1].tracks = get_config_int(NULL, "hdd_cylinders", 0);
488 p = (char *)get_config_string(NULL, "hdd_fn", "");
489 if (p) strcpy(ide_fn[1], p);
490 else strcpy(ide_fn[1], "");
491 }
493 void saveconfig()
494 {
495 set_config_int(NULL, "gameblaster", GAMEBLASTER);
496 set_config_int(NULL, "gus", GUS);
497 set_config_int(NULL, "ssi2001", SSI2001);
499 set_config_int(NULL, "model", model);
500 set_config_int(NULL, "cpu_manufacturer", cpu_manufacturer);
501 set_config_int(NULL, "cpu", cpu);
503 set_config_int(NULL, "gfxcard", gfxcard);
504 set_config_int(NULL, "video_speed", video_speed);
505 set_config_int(NULL, "sndcard", sound_card_current);
506 set_config_int(NULL, "cpu_speed", cpuspeed);
507 set_config_int(NULL, "has_fpu", hasfpu);
508 set_config_int(NULL, "slow_video", slowega);
509 set_config_int(NULL, "cache", cache);
510 set_config_int(NULL, "cga_composite", cga_comp);
511 set_config_string(NULL, "disc_a", discfns[0]);
512 set_config_string(NULL, "disc_b", discfns[1]);
513 set_config_int(NULL, "mem_size", mem_size);
514 set_config_int(NULL, "cdrom_drive", cdrom_drive);
515 set_config_int(NULL, "cdrom_enabled", cdrom_enabled);
516 set_config_int(NULL, "vid_resize", vid_resize);
517 set_config_int(NULL, "vid_api", vid_api);
518 set_config_int(NULL, "video_fullscreen_scale", video_fullscreen_scale);
519 set_config_int(NULL, "video_fullscreen_first", video_fullscreen_first);
521 set_config_int(NULL, "hdc_sectors", hdc[0].spt);
522 set_config_int(NULL, "hdc_heads", hdc[0].hpc);
523 set_config_int(NULL, "hdc_cylinders", hdc[0].tracks);
524 set_config_string(NULL, "hdc_fn", ide_fn[0]);
525 set_config_int(NULL, "hdd_sectors", hdc[1].spt);
526 set_config_int(NULL, "hdd_heads", hdc[1].hpc);
527 set_config_int(NULL, "hdd_cylinders", hdc[1].tracks);
528 set_config_string(NULL, "hdd_fn", ide_fn[1]);
530 config_save(config_file_default);
531 }