/[projet1]/public/pc/emulators/caloric/src/host.c
Defence Force logotype

Contents of /public/pc/emulators/caloric/src/host.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 88 - (show annotations)
Wed May 27 21:06:36 2009 UTC (10 years, 5 months ago) by Jede
File MIME type: text/plain
File size: 16101 byte(s)
Corrected a bug, if you select on the command line :

-t -d it starts an atmos

now if you do a -t -d it starts a telestrat
1 /*
2 * host.c - various stuff
3 * FF sometime in 1994-1997
4 */
5
6 /*
7 Parts of this file copyright 1994-1997 Fabrice Francès.
8 Parts of this file copyright 2000-2003 André Majorel.
9
10 This program is free software; you can redistribute it and/or modify it under
11 the terms of version 2 of the GNU General Public License as published by the
12 Free Software Foundation.
13
14 This program is distributed in the hope that it will be useful, but WITHOUT
15 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License along with
19 this program; if not, write to the Free Software Foundation, Inc., 59 Temple
20 Place, Suite 330, Boston, MA 02111-1307, USA.
21 */
22
23 #include "config.h"
24 #include <ctype.h>
25 #include <errno.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <fcntl.h>
30 #include <time.h>
31
32 #include <unistd.h>
33 #if HAVE_IOPERM
34 # include <sys/io.h>
35 #endif
36 #include <sys/types.h>
37 #include <sys/stat.h>
38
39 #include "caloric.h"
40 #include "dsp.h"
41 #include "hardware.h"
42 #include "keyb_us.h"
43 #include "traps.h"
44
45
46 static void parse_rcfile (const char *setup, FILE *setup_fp);
47
48 extern char Eprom[8192];
49 extern char Banks[8][16384];
50 extern char RomBank[8];
51 extern int Bank_point;
52 extern int Eprom_start;
53 extern char ram_pattern;
54 extern int Com_port, Com_Addr, serial_desc;
55 extern int cycles,initial_cycles;
56 extern char sound;
57 extern char joystick,joystickport,acia,rtclock,printer;
58 extern char jasmin,telestrat,disk;
59 extern int rs232;
60
61 static char *temppath="";
62 char output_name[64]="", dump_name[128]="";
63 char serial_name[64]="", printer_name[128]="";
64 static char oric1_name[128]="", atmos_name[128]="", bank_name[8][128]={"","","","","","","",""};
65 static char microdisc_name[128]="", jasmin_name[128]="";
66 char atmos=1;
67 char messages[35][41];
68 int quit_on_trap;
69 int zoom;
70 int debugger = 0;
71
72 void Load_ROM(unsigned char *rom_adr)
73 {
74 const char *basename = atmos ? atmos_name : oric1_name;
75 char *pathname;
76 FILE *rom_fp;
77
78 pathname = locate_file (basename, datapath);
79 if (pathname == NULL)
80 {
81 err ("%s: not found", basename);
82 exit (1);
83 }
84 rom_fp = fopen (pathname, "rb");
85 if (rom_fp == NULL)
86 {
87 err ("%s: %s", pathname, strerror (errno));
88 exit (1);
89 }
90 if (fread (rom_adr, 16384, 1, rom_fp) != 1)
91 {
92 err ("%s: read error", pathname);
93 exit (1);
94 }
95 fclose (rom_fp);
96 free (pathname);
97 }
98
99 void Load_EPROM()
100 {
101 const char *basename = jasmin ? jasmin_name : microdisc_name;
102 char *pathname;
103 FILE *eprom_fp;
104 int size;
105
106 pathname = locate_file (basename, datapath);
107 if (pathname == NULL)
108 {
109 err ("%s: not found", basename);
110 exit (1);
111 }
112 eprom_fp = fopen (pathname, "rb");
113 if (eprom_fp == NULL)
114 {
115 err ("%s: %s", pathname, strerror (errno));
116 exit (1);
117 }
118 size = fread (Eprom, 1, 16384, eprom_fp);
119 if (size < 1)
120 {
121 err ("%s: read error", pathname);
122 exit (1);
123 }
124 if (size < 16384)
125 memcpy (Eprom + 16384 - size, Eprom, size);
126 Eprom_start = 0x10000 - size;
127 Bank_point = Eprom_start;
128 fclose (eprom_fp);
129 free (pathname);
130 }
131
132 /*****************************************************************************/
133 void Load_Banks()
134 {
135 int i, size;
136 for (i = 1; i < 8; i++)
137 {
138 char *pathname;
139 if (bank_name[i][0]=='\0')
140 continue;
141 pathname = locate_file (bank_name[i], datapath);
142 if (pathname == NULL)
143 {
144 err ("bank #%d: %s: not found", i, pathname);
145 }
146 else
147 {
148 FILE *rom_fp = fopen (pathname, "rb");
149 if (rom_fp == NULL)
150 {
151 err ("bank #%d: %s: %s", i, pathname, strerror (errno));
152 }
153 else
154 {
155 RomBank[i] = 1;
156 size = fread (Banks[i], 1, 16384, rom_fp);
157 if (size < 16384)
158 memcpy ((char *) (Banks + i) + 16384 - size, Banks + i, size);
159 fclose (rom_fp);
160 }
161 free (pathname);
162 }
163 }
164 Bank_point=0xC000;
165 if (!RomBank[7]) {
166 err ("no bank #7");
167 err ("check your caloricrc");
168 exit(1);
169 }
170 }
171
172 /***************************************************************************/
173 char tape_name[256]="";
174 extern FILE *tape_handle;
175 extern char protected_tape;
176
177 int Open_Tape(const char *name)
178 {
179 err ("Open_Tape(%s)", name); /* FIXME DEBUG */
180 if (tape_handle) Close_Tape();
181 protected_tape=0;
182 tape_handle=fopen(name,"r+b");
183 if (tape_handle==NULL) {
184 tape_handle=fopen(name,"rb");
185 if (tape_handle==NULL) return 0;
186 protected_tape=1;
187 }
188 strcpy(tape_name,name);
189 return 1;
190 }
191
192 void protect_tape(void)
193 {
194 if (!tape_handle) return;
195 fclose(tape_handle);
196 tape_handle=NULL;
197 chmod(tape_name,0444);
198 Open_Tape(tape_name);
199 }
200
201 void unprotect_tape(void)
202 {
203 if (!tape_handle) return;
204 fclose(tape_handle);
205 tape_handle=NULL;
206 chmod(tape_name,0644);
207 Open_Tape(tape_name);
208 }
209
210 void Close_Tape(void)
211 {
212 err ("Close_Tape()"); /* FIXME DEBUG */
213 if (tape_handle) fclose(tape_handle);
214 tape_handle=NULL;
215 tape_name[0]=0;
216 }
217
218 void Create_Tape(void)
219 {
220 if (tape_handle) fclose(tape_handle);
221 tape_name[0]=0;
222 protected_tape=0;
223 tape_handle=fopen("________","w+b");
224 if (tape_handle==NULL) return;
225 strcpy(tape_name,"________");
226 }
227
228 /***************************************************************************/
229
230 /***************************************************************************/
231 extern int nbdisks;
232 extern char diskbuf[8192];
233 extern FILE *fd[4];
234 extern char write_only[4];
235 extern char drives[4];
236
237 static int current_drive;
238 static char diskid[8];
239 static int sides[4];
240 static int tracks[4];
241 static int geometry[4]; /* 1 : whole side 0 first */
242 static char disk_signature[]="MFM_DISK";
243 char disk_name[4][256]={ "", "", "", "" };
244
245 int Open_Disk(char *name,int drive)
246 {
247 Close_Disk(drive);
248 write_only[drive]=0;
249 fd[drive]=fopen(name,"r+b");
250 if (fd[drive]==NULL) {
251 fd[drive]=fopen(name,"rb");
252 if (fd[drive]==NULL) return 0;
253 write_only[drive]=1;
254 }
255 fread(diskid,8,1,fd[drive]);
256 if (strncmp(diskid,disk_signature,8)!=0) {
257 err("%s is not an Oric disk image",name);
258 return 0;
259 }
260 fread(&sides[drive],1,4,fd[drive]);
261 fread(&tracks[drive],1,4,fd[drive]);
262 fread(&geometry[drive],1,4,fd[drive]);
263 strcpy(disk_name[drive],name);
264 return 1;
265 }
266
267 void Close_Disk(int drive)
268 {
269 if (fd[drive]) fclose(fd[drive]);
270 fd[drive]=NULL;
271 disk_name[drive][0]=0;
272 }
273
274 int Create_Disk(int drive)
275 {
276 if (fd[drive]) fclose(fd[drive]);
277 disk_name[drive][0]=0;
278 write_only[drive]=0;
279 fd[drive]=fopen("________.dsk","w+b");
280 if (fd[drive]==NULL) return 0;
281 strcpy(disk_name[drive],"________.dsk");
282 return 1;
283 }
284
285 void disk_flushbuf(void)
286 {
287 if (fd[current_drive]!=NULL) {
288 fwrite(diskbuf,6400,1,fd[current_drive]);
289 fseek(fd[current_drive],-6400,SEEK_CUR);
290 }
291 }
292
293
294 void disk_read_track(int drive,int side,int track)
295 {
296 long offset=(side*tracks[drive]+track)*6400+256;
297 current_drive=drive;
298 memset(diskbuf,0,6400);
299 if (fd[drive]!=NULL) {
300 fseek(fd[drive],offset,SEEK_SET);
301 fread(diskbuf,6400,1,fd[drive]);
302 fseek(fd[drive],-6400,SEEK_CUR);
303 }
304 }
305
306 int disk_format(int drive,int side,int track)
307 {
308 long offset;
309 if (fd[drive]==NULL) {
310 if (Create_Disk(drive)==0) return 1;
311 }
312 if (write_only[drive]) return 1;
313 if (side+1>sides[drive]) sides[drive]=side+1;
314 if (track+1>tracks[drive]) tracks[drive]=track+1;
315 offset=(side*tracks[drive]+track)*6400+256;
316 fseek(fd[drive],0,SEEK_SET);
317 fwrite(disk_signature,8,1,fd[drive]);
318 fwrite(&sides[drive],4,1,fd[drive]);
319 fwrite(&tracks[drive],4,1,fd[drive]);
320 fwrite(&geometry,4,1,fd[drive]);
321 fseek(fd[drive],offset,SEEK_SET);
322 fwrite(diskbuf,6400,1,fd[drive]);
323 return 0;
324 }
325
326 /***************************************************************************/
327
328 void Save_hardcopy()
329 {
330
331 }
332
333 /***************************************************************************/
334 extern char State[64];
335 static const char dump_pathname[] = "Dump"; /* FIXME use dump_name instead ? */
336
337 void Dump(char *mem)
338 {
339 FILE *dump_fd;
340 dump_fd=fopen(dump_pathname,"wb");
341 if (dump_fd!=NULL) {
342 fwrite(mem,1,65536,dump_fd);
343 fwrite(State,1,64,dump_fd);
344 fclose(dump_fd);
345 }
346 }
347
348 void Load_Dump(unsigned char *mem)
349 {
350 FILE *dump_fd;
351 dump_fd=fopen(dump_pathname,"rb");
352 if (dump_fd==NULL) {
353 err("%s: %s",dump_pathname,strerror(errno));
354 err("Dump file %s does not exist",dump_pathname);
355 err("(Please check caloricrc)");
356 exit(1);
357 }
358 fread(mem,1,65536,dump_fd);
359 fread(State,1,64,dump_fd);
360 fclose(dump_fd);
361 }
362
363 /***************************************************************************/
364
365 static FILE *printfd=NULL;
366
367 void Start_Printer()
368 {
369 printfd=fopen(printer_name,"wb");
370 if (printfd==NULL) {
371 err("%s: %s",printer_name,strerror(errno));
372 err("Can't open printer output %s",printer_name);
373 err("(Please check it or modify caloricrc)");
374 exit(1);
375 }
376 }
377
378 void Printer(char c)
379 {
380 if (printfd) putc(c,printfd);
381 }
382
383 /***************************************************************************/
384 void Start_RS232(void)
385 {
386 if (*serial_name == '\0') {
387 serial_desc = -1;
388 }
389 else {
390 #ifdef O_NDELAY
391 serial_desc=open(serial_name,O_RDWR | O_NDELAY);
392 #else
393 #ifdef O_NONBLOCK
394 serial_desc=open(serial_name,O_RDWR | O_NONBLOCK);
395 #else
396 serial_desc = -1;
397 #endif
398 #endif
399 if (serial_desc==-1) {
400 err("%s: %s",serial_name,strerror(errno));
401 err("Can't open communication port %s",serial_name);
402 err("(Please check it or modify caloricrc)");
403 exit(1);
404 }
405 }
406 }
407 /***************************************************************************/
408
409 int init(int argc, char *argv[])
410 {
411 int speed_index,restart=0;
412 int g;
413 zoom = 1;
414 debugger=1;
415 while ((g = getopt (argc, argv, "1aA:djJpQrs:S:tX:z:g")) != EOF) {
416 if (g == '1') { telestrat=0; atmos=0; }
417 else if (g == 'a') { telestrat=0; atmos=1; }
418 else if (g == 'A') { audio_device=optarg; }
419 else if (g == 'd') { disk=1; }
420 else if (g == 'j') { telestrat=0; jasmin=1; disk=1; }
421 else if (g == 'J') { joystickport=1; }
422 else if (g == 'p') { joystick=1; disable_keypad(); }
423 else if (g == 'Q') { quit_on_trap = 1; }
424 else if (g == 'r') { restart=1; }
425 else if (g == 'g') { debugger=1; }
426 else if (g == 's') {
427 sscanf (optarg, "%d", &speed_index);
428 if (speed_index<10) {
429 err("Speed index of %d%% ? Surely you're "
430 "joking, Mr. Feynman...",speed_index);
431 exit(1);
432 }
433 initial_cycles=cycles=200*speed_index;
434 }
435 else if (g == 'S') {
436 if (strcmp (optarg, "sdl") == 0)
437 audio_method = AM_SDL;
438 else if (strcmp (optarg, "arts") == 0)
439 audio_method = AM_ARTS;
440 else if (strcmp (optarg, "auto") == 0)
441 audio_method = AM_ARTS_OSS;
442 else if (strcmp (optarg, "oss") == 0)
443 audio_method = AM_OSS;
444 else if (strcmp (optarg, "alsa") == 0)
445 audio_method = AM_ALSA;
446
447 else {
448 err ("the sound output method must be \"alsa\", \"arts\""
449 ", \"auto\" or \"oss\"");
450 exit (1);
451 }
452 }
453 else if (g == 't') { telestrat=1; disk=1; }
454 else if (g == 'z')
455 {
456 zoom = atoi(optarg);
457 if (zoom < 1)
458 {
459 err ("the zoom factor must be greater than 1");
460 exit (1);
461 }
462 }
463 else
464 exit(1);
465 }
466 {
467 int n;
468
469 for (n = optind; n < argc; n++)
470 if (Open_Disk(argv[n],nbdisks++))
471 disk=1;
472 }
473
474 if (telestrat) { disk=1; jasmin=0; acia=1; }
475 if (jasmin) { disk=1; }
476
477 if (joystick) joystickport=0;
478 if (printer) Start_Printer();
479 if (acia && *serial_name != '\0')
480 {
481 #if HAVE_IOPERM
482 if (ioperm(0x3f8, 8, 1) != 0) /* ttyS0 */
483 {
484 err("ioperm(0x3f8,8): %s, disabling 6551 emulation",
485 strerror (errno));
486 goto rs232_fini;
487 }
488 if (ioperm(0x2f8, 8, 1) != 0) /* ttyS1 */
489 {
490 err("ioperm(0x2f8,8): %s, disabling 6551 emulation",
491 strerror (errno));
492 goto rs232_fini;
493 }
494 rs232 = 1;
495 rs232_fini:
496 ;
497 #else
498 err("ioperm not available on this platform,"
499 " disabling 6551 emulation");
500 #endif
501 }
502 if (rs232) Start_RS232();
503
504
505
506 Init_Hard();
507 if (telestrat) {
508 Load_Banks();
509 } else {
510 if (disk) {
511 Load_EPROM();
512 }
513 Load_ROM(Oric_Mem+0x10000);
514 }
515 if (restart) Load_Dump(Oric_Mem);
516 Patch_ROM();
517 return restart;
518 }
519
520 void redirect_output(void)
521 {
522 sprintf(output_name,"/tmp/euphoric.lst");
523 freopen(output_name,"w",stdout);
524 rewind(stdout);
525 }
526
527 void delete_output(void)
528 {
529 unlink(output_name);
530 }
531
532 void remove_eol(char *str)
533 {
534 int i;
535 for(i=strlen(str)-1; str[i]==' ' || str[i]=='\r' || str[i]=='\n'; i--)
536 str[i]='\0';
537 }
538
539 void setup(char *env[])
540 {
541 int i;
542 const char **p;
543 int count = 0;
544 FILE *fp;
545 for (i = 0; i < 35; i++)
546 messages[i][0] = 0;
547 for(i=0;env[i]!=NULL;i++)
548 {
549 if (strncmp (env[i], "TEMP=", 5) == 0)
550 temppath = (&env[i][5]);
551 }
552
553 for (p = rcfiles; *p != NULL; p++)
554 {
555
556 char *pathname = NULL;
557
558 pathname = expand_path (*p);
559 #ifdef DEBUG_RELEASE
560 printf("Trying to find %s in %s\n",*p,pathname);
561 #endif
562
563 fp = fopen (pathname, "r");
564 if (fp == NULL)
565 {
566 if (errno != ENOENT)
567 err("warning: %s: %s", pathname, strerror (errno));
568 #ifdef DEBUG_RELEASE
569 printf("Can't found %s\n",pathname);
570 #endif
571
572
573 }
574 else
575 {
576 parse_rcfile (pathname, fp);
577 #ifdef DEBUG_RELEASE
578 printf("Reading %s\n",pathname);
579 #endif
580 count++;
581 fclose (fp);
582 }
583 free (pathname);
584 }
585 // #endif
586 if (count == 0)
587 err("warning: no config file found");
588 }
589
590 static void parse_rcfile (const char *setup, FILE *setup_fp)
591 {
592 char line[256];
593 float frequency;
594
595 while(!feof(setup_fp)) {
596 fgets(line,256,setup_fp);
597 remove_eol(line);
598 if (strncmp(line,"Computer=",9)==0) {
599 if (strcmp(line+9,"Oric1")==0)
600 ;
601 else if (strcmp(line+9,"Atmos")==0)
602 atmos=1;
603 else if (strcmp(line+9,"Telestrat")==0 || strcmp(line+9,"Stratos")==0)
604 telestrat=1;
605 } else if (strncmp(line,"DiskController=",15)==0) {
606 if (strcmp(line+15,"Microdisc")==0)
607 disk=1;
608 else if (strcmp(line+15,"Jasmin")==0)
609 jasmin=1;
610 } else if (strncmp(line,"Oric1Rom=",9)==0) {
611 strcpy(oric1_name,line+9);
612 } else if (strncmp(line,"AtmosRom=",9)==0) {
613 strcpy(atmos_name,line+9);
614 } else if (strncmp(line,"MicrodiscEprom=",15)==0) {
615 strcpy(microdisc_name,line+15);
616 } else if (strncmp(line,"JasminEprom=",12)==0) {
617 strcpy(jasmin_name,line+12);
618 } else if (strncmp(line,"Bank7=",6)==0) {
619 strcpy(bank_name[7],line+6);
620 } else if (strncmp(line,"Bank6=",6)==0) {
621 strcpy(bank_name[6],line+6);
622 } else if (strncmp(line,"Bank5=",6)==0) {
623 strcpy(bank_name[5],line+6);
624 } else if (strncmp(line,"Bank4=",6)==0) {
625 strcpy(bank_name[4],line+6);
626 } else if (strncmp(line,"Bank3=",6)==0) {
627 strcpy(bank_name[3],line+6);
628 } else if (strncmp(line,"Bank2=",6)==0) {
629 strcpy(bank_name[2],line+6);
630 } else if (strncmp(line,"Bank1=",6)==0) {
631 strcpy(bank_name[1],line+6);
632 } else if (strncmp(line,"RamPattern=",11)==0) {
633 ram_pattern=line[11]&1;
634 } else if (strncmp(line,"DriveA=",7)==0) {
635 drives[0]=(line[7]!='N' && line[7]!='n');
636 } else if (strncmp(line,"DriveB=",7)==0) {
637 drives[1]=(line[7]!='N' && line[7]!='n');
638 } else if (strncmp(line,"DriveC=",7)==0) {
639 drives[2]=(line[7]!='N' && line[7]!='n');
640 } else if (strncmp(line,"DriveD=",7)==0) {
641 drives[3]=(line[7]!='N' && line[7]!='n');
642 } else if (strncmp(line,"Clock=",6)==0) {
643 sscanf(line+6,"%f",&frequency);
644 initial_cycles=cycles=((int)(frequency*10.0))*2000;
645 } else if (strncmp(line,"AsynchronousController=",23)==0) {
646 acia=(line[23]!='N' && line[23]!='n');
647 } else if (strncmp(line,"SerialPort=",11)==0) {
648 strcpy(serial_name,line+11);
649 } else if (strncmp(line,"RealTimeClock=",14)==0) {
650 rtclock=(line[14]!='N' && line[14]!='n');
651 } else if (strncmp(line,"Joystick=",9)==0) {
652 joystick=(line[9]!='N' && line[9]!='n');
653 } else if (strncmp(line,"JoystickPort=",13)==0) {
654 joystickport=(line[13]!='N' && line[13]!='n');
655 } else if (strncmp(line,"Printer=",8)==0) {
656 printer=(line[8]!='N' && line[8]!='N');
657 } else if (strncmp(line,"PrinterOutput=",14)==0) {
658 strcpy(printer_name,line+14);
659 } else if (strncmp(line,"DumpFile=",9)==0) {
660 strcpy(dump_name,line+9);
661 } else if (line[0]>='0' && line[0]<='9' && line[1]>='0' && line[1]<='9' && line[2]=='=') {
662 int number=(line[0]-'0')*10+line[1]-'0';
663 if (number<35) {
664 strncpy(messages[number],line+3,41);
665 messages[number][40]=0;
666 }
667 }
668 }
669 }

  ViewVC Help
Powered by ViewVC 1.1.26