-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcpm.c
2744 lines (2557 loc) · 75.8 KB
/
cpm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***********************************************************************/
/* CP/M-80 V2.2 emulator for WIN32 v0.4 */
/* cpm.exe */
/* Copyright (C) 2004-2012 by Keiji Murakami */
/***********************************************************************/
/* 2005/09 FCB rc 8bits->7bits (for PL/I-80 Linker) K.M */
/* 2005/12 Ctrl-C trap K.M */
/* 2007/01 delete CR on stdout K.M */
/* 2008/12 modify FCB reuse method (for TURBO PASCAL) K.M */
/* 2012/02 screen clear char change \0 to ' ' (for wineconsole) K.M */
/* 2012/02 BDOS 36 (set random record) bug-fix (for UNARC) K.M */
/* 2012/03 DEL code, debug message bug-fix N.F */
/* 2012/03 adapt to VC++ N.F, K.M */
/* 2012/03 add option -C (args to uppercase) (for ZLINK) N.F, K.M */
/* 2012/03 FCB terminate on [=:;<>] N.F, K.M */
/* 2012/03 Fix screen control function N.F, K.M */
/***********************************************************************/
/* for VC++ 2008/2010 */
#ifdef _MSC_VER
#pragma warning(disable:4996)
#endif
#ifdef __linux__
#include <ctype.h>
#include <libgen.h>
#define CH_SLASH '/'
#define _GNU_SOURCE
#define FOREGROUND_INTENSITY 0x0008 // text color is intensified.
#define COMMON_LVB_REVERSE_VIDEO 0x4000 // DBCS: Reverse fore/back ground attribute.
#define COMMON_LVB_UNDERSCORE 0x8000 // DBCS: Underscore.
typedef unsigned int UINT;
typedef unsigned char UCHAR;
typedef unsigned char BYTE;
typedef unsigned short USHORT;
typedef short SHORT;
typedef unsigned short WORD;
typedef unsigned long ULONG;
typedef unsigned long DWORD;
typedef struct _COORD {
SHORT X;
SHORT Y;
} COORD, *PCOORD;
#define APPEXT ""
#define APPTARGET "LINUX"
#else
#include <windows.h>
#define CH_SLASH '\\'
#define F_OK 0
#define APPEXT ".EXE"
#define APPTARGET "WIN32"
#endif
#define APPVERSION "0.5"
#define CON_BUF_EMPTY -1
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/stat.h>
#ifdef __linux__
#include <signal.h>
#include <termios.h>
#include <unistd.h>
#include <dirent.h>
#include <sys/statvfs.h>
#include <sys/ioctl.h>
#include <fnmatch.h>
#include "ansiesc.h"
#include "conio.h"
#else
#include <io.h>
#include <conio.h>
#define PATH_MAX MAX_PATH
#endif
#include <fcntl.h>
#define TRUE 1
#define FALSE 0
#ifdef EM180
#include "em180.h"
#else
#define EMST_STOP 0
#define EMST_HALT 1
#define EMST_FUNC 0x100
#define EMST_UNKOWN 0x200
/* from RunCPM/globals.h */
typedef signed char int8;
typedef signed short int16;
typedef signed int int32;
typedef unsigned char uint8;
typedef unsigned short uint16;
typedef unsigned int uint32;
#define byte uint8
#define word uint16
uint8 mem1024[ 0x100000];
uint8 guard[ 0x10];
uint8* mem=mem1024; /* pointer to current 64k page start { Orion-128 port F9 } */
uint8* mem16=mem1024; /* pointer to current 16k page start { Orion-128 port FB[3..0] & ![7] } */
int dispatcher=FALSE;
int fullram=FALSE;
int int50hz=FALSE;
int orion128=FALSE;
int ordfile=0;
int ordaddr=0;
#include "ram.h"
#include "cpu.h"
#endif
#define MAXFCB 64
int debug_flag;
#define DEBUGOUT if (debug_flag) fprintf
#define DPH_XLT 0 /* DW xlt ;Address of sector translation table */
#define DPH_WS1 1 /* DW 0,0,0 ;Used as workspace by CP/M */
#define DPH_WS2 2
#define DPH_WS3 3
#define DPH_BUF 4 /* DW dirbuf ;Address of a 128-byte sector buffer; this is the same for all DPHs in the system. */
#define DPH_DPB 5 /* DW dpb ;Address of the DPB giving the format of this drive. */
#define DPH_CSV 6 /* DW csv ;Address of the directory checksum vector for this drive. (set=0) */
#define DPH_ALV 7 /* DW alv ;Address of the allocation vector for this drive. */
#define EMST_BDOS EMST_FUNC
#define EMST_BIOS_BASE (EMST_FUNC+1)
#define RETEM 0xed,0xfd,
#define CALLN(v) 0xed,0xed,(v),
#define JP(x) 0xc3,(byte)(x),(byte)((x)>>8),
#define RET 0xc9,
#define HALT 0x76
#define ROM_ORG 0xf800
#define ROM_CNT 22
#define BDOS_ORG 0xff00 // 0xfe00
#define BIOS_CNT 17
#define BIOS_ORG 0x10000-(3*BIOS_CNT)-3 // 0xff00
#define BIOS_DPH BIOS_ORG-16 // 16=sizeof(TDPH)
#define DMY_DPB (BDOS_ORG + 16)
#define DMY_DPB_SFT (DMY_DPB + 2)
#define DMY_DPB_MSK (DMY_DPB + 3)
#define DMY_DPB_MAX (DMY_DPB + 5)
#define DMY_ALLOC (BDOS_ORG + 32)
#define MAXBLK (128*8) // 32 bytes ALLOC
#define setword( p, x) { (p) = (byte)(x); (p) = (byte)((x)>>8); }
/* ============ Configuration variable ============ */
byte kbwait; /* wait on console status check */
int pause_flag; /* pause before exit */
int retcode_flag; /* exit code type: 0..none 1:HI-TECH C 2:BDSC */
int no_auto_assign; /* disable auto drive assign */
int uppercase_flag; /* force to uppercase */
int NoKOI=FALSE; /* prevent AltToKIO8 decoding */
int R1715=FALSE; /* Robotron1715 terminal support */
int adm3a=FALSE; /* Kaypro termial support */
int inverted=FALSE;
enum { RC_HITECH = 1, RC_BDSC};
/* ============ I/O callback ============ */
int io_input( int add) {
DEBUGOUT( stderr, "ERROR: i/o read port:%04x\n", add);
exit( -1);
return 0;
}
void io_output( int add, int data) {
int p,i;
DEBUGOUT( stderr, "ERROR: i/o write port:%04x <- %02x\n", add, data);
switch (add) {
case 0xf9: mem=&mem1024[(data & 0x0f) * 0x10000];
goto ori128;
case 0xfb: if (data & 0x80) {
dispatcher=TRUE;
mem16=&mem1024[((int)data & 0x0f)<<6];
}
else mem16=mem;
fullram=data & 0x20;
int50hz=data & 0x40;
case 0xf8:
case 0xfa:
ori128: if (orion128==FALSE) { /* Switch to Orion128 mode and init F800 ROM area */
orion128=TRUE;
p = ROM_ORG;
for ( i = 0; i < ROM_CNT; i++) {
mem1024[ p++] = 0xC3;
setword( mem1024[ p++], 3*ROM_CNT+ROM_ORG); /* JP MMMM */
}
mem1024[ p++] = 0xED; mem1024[ p++] = 0xED; /* SPECIAL OPCODE */
mem1024[ p++] = 0xC9; /* RET */
}
break;
default: exit( -1);
}
}
/* ================== CP/M BDOS emulation ================== */
#define MAXDRV 16
byte cpm_usr_no = 0;
byte cpm_disk_no = 'B'-'A';
word cpm_dma_addr = 0x80;
word cpm_version = 0x22;
word cpm_disk_vct = 0;
char *cpm_drive[ MAXDRV];
char *cpmdrv0=NULL;
int abort_submit;
FILE *punfp, *rdrfp, *lstfp;
char StartDir[PATH_MAX+1];
char filename[ 1024];
char filename2[ 1024];
#define freedrv if(cpmdrv0){free(cpmdrv0);cpmdrv0=NULL;}
#define CPMPATH "CPMPATH"
#define ORDPATH "ORDPATH"
#ifdef __linux__
static unsigned short ruspseudo[]={
0xd090, 0xd091, 0xd092, 0xd093, 0xd094, 0xd095, 0xd096, 0xd097, /* %c%c */
0xd098, 0xd099, 0xd09a, 0xd09b, 0xd09c, 0xd09d, 0xd09e, 0xd09f, /* %c%c */
0xd0a0, 0xd0a1, 0xd0a2, 0xd0a3, 0xd0a4, 0xd0a5, 0xd0a6, 0xd0a7, /* %c%c */
0xd0a8, 0xd0a9, 0xd0aa, 0xd0ab, 0xd0ac, 0xd0ad, 0xd0ae, 0xd0af, /* %c%c */
0xd0b0, 0xd0b1, 0xd0b2, 0xd0b3, 0xd0b4, 0xd0b5, 0xd0b6, 0xd0b7, /* %c%c */
0xd0b8, 0xd0b9, 0xd0ba, 0xd0bb, 0xd0bc, 0xd0bd, 0xd0be, 0xd0bf, /* %c%c */
0x9691, 0x9692, 0x9693, 0x9482, 0x94a4, 0x95a1, 0x95a2, 0x9596, /* \xe2%c%c */
0x9595, 0x95a3, 0x9591, 0x9597, 0x959d, 0x959c, 0x959b, 0x9490, /* \xe2%c%c */
0x9494, 0x94b4, 0x94ac, 0x949c, 0x9480, 0x94bc, 0x959e, 0x959f, /* \xe2%c%c */
0x959a, 0x9594, 0x95a9, 0x95a6, 0x95a0, 0x9590, 0x95ac, 0x95a7, /* \xe2%c%c */
0x95a8, 0x95a4, 0x95a5, 0x9599, 0x9598, 0x9592, 0x9593, 0x95ab, /* \xe2%c%c */
0x95aa, 0x9498, 0x948c, 0x9688, 0x9684, 0x968b, 0x9690, 0x9680, /* \xe2%c%c */
0xd180, 0xd181, 0xd182, 0xd183, 0xd184, 0xd185, 0xd186, 0xd187, /* %c%c */
0xd188, 0xd189, 0xd18a, 0xd18b, 0xd18c, 0xd18d, 0xd18e, 0xd18f, /* %c%c */
0xd081, 0xd191, 0xd084, 0xd194, 0xd087, 0xd197, 0xd08e, 0xd19e, /* %c%c */
0xd089, 0xd199, 0xd08a, 0xd19a, 0xd08b, 0xd19b, 0xd08f, 0xd19f};/* %c%c */
#else
enum Colors {
RESET_COLOR,
RED_TXT,
GREEN_TXT,
YELLOW_TXT,
BLUE_TXT,
MAGENTA_TXT,
CYAN_TXT,
WHITE_TXT
};
#endif
static unsigned char oriKoi[]={
0xC0, 0xC1, 0xC2, 0xC3, 0xC4, 0xC5, 0xC6, 0xC7, 0xC8, 0xC9, 0xCA, 0xCB, 0xCC, 0xCD, 0xCE, 0xCF,
0xD0, 0xD1, 0xD2, 0xD3, 0xD4, 0xD5, 0xD6, 0xD7, 0xD8, 0xD9, 0xDA, 0xDB, 0xDC, 0xDD, 0xDE, 0xDF,
0xF0, 0xF1, 0xF2, 0xF3, 0xF4, 0xF5, 0xF6, 0xF7, 0xF8, 0xF9, 0xFA, 0xFB, 0xFC, 0xFD, 0xFE, 0xFF,
0xB0, 0xB1, 0xB2, 0xB3, 0xB4, 0xB5, 0xB6, 0xB7, 0xB8, 0xB9, 0xBA, 0xBB, 0xBC, 0xBD, 0xBE, 0xBF,
0xEE, 0xA0, 0xA1, 0xE6, 0xA4, 0xA5, 0xE4, 0xA3, 0xE5, 0xA8, 0xA9, 0xAA, 0xAB, 0xAC, 0xAD, 0xAE,
0xAF, 0xEF, 0xE0, 0xE1, 0xE2, 0xE3, 0xA6, 0xA2, 0xEC, 0xEB, 0xA7, 0xE8, 0xED, 0xE9, 0xE7, 0xEA,
0x9E, 0x80, 0x81, 0x96, 0x84, 0x85, 0x94, 0x83, 0x95, 0x88, 0x89, 0x8A, 0x8B, 0x8C, 0x8D, 0x8E,
0x8F, 0x9F, 0x90, 0x91, 0x92, 0x93, 0x86, 0x82, 0x9C, 0x9B, 0x87, 0x98, 0x9D, 0x99, 0x97, 0x9A};
char *auto_drive_assign( char **arg)
{
#ifdef __linux__
struct stat statbuf;
#endif
DWORD att;
char *p;
static char argbuf[ 256];
static char *argp = argbuf;
static int n = 2;
if ( !no_auto_assign && ( p = strrchr( *arg, CH_SLASH)) != NULL) {
*p = '\0';
#ifdef __linux__
att = stat( *arg, &statbuf);
#else
att = GetFileAttributes( *arg);
#endif
*p = CH_SLASH;
if ( *arg == p ||
#ifdef __linux__
( !att && ( statbuf.st_mode & S_IFDIR ))) {
#else
( att != (DWORD)-1 && ( att & FILE_ATTRIBUTE_DIRECTORY))) {
#endif
cpm_drive[ n] = *arg;
*arg = argp;
argp += sprintf( argp, "%c:%.124s", n++ + 'A', ++p) + 1;
*p = '\0';
}
}
return *arg;
}
void setup_disk_vct( void)
{
int i;
char envname[ 8];
cpm_disk_vct = 3;
for ( i = 2; i < MAXDRV; i++) {
if ( cpm_drive[ i]) continue;
sprintf( envname, "CPM%c", i + 'A');
if (( cpm_drive[ i] = getenv( envname)) != NULL) {
cpm_disk_vct |= (word)( 1 << i);
}
}
}
char* CharUpperX(char* st)
{
char* ss=st;
if (st==NULL)
return st;
while (*ss) {
*ss=toupper(*ss);
ss++;
}
return st;
}
#ifdef __linux__
/* Constants for MSC pathname functions */
#define _MAX_PATH 260
#define _MAX_DRIVE 3
#define _MAX_DIR 256
#define _MAX_FNAME 256
#define _MAX_EXT 256
#define stricmp strucmp
void _splitpath(char *path, char *drive, char *dir, char *name, char *ext)
{
char* cc, sz;
void* pp;
*dir=0; *name=0; *ext=0;
pp=malloc((sz=strlen(path))+1);
strncpy(pp,path,sz); /* because dirname flushes `path` */
strncpy(name, (cc=basename(path)) ? cc: "", _MAX_FNAME);
strncpy(dir, (cc=dirname(path)) ? cc: "", _MAX_DIR);
if ((strcmp(dir,".")==0)&&(*path!='.'))
*dir=0;
if ((cc=strstr(name, "."))!=NULL) {
strncpy(ext, &cc[1], _MAX_EXT);
*cc='\0';
}
strncpy(path,pp,sz);
free(pp);
}
char *buildname(char* dirnm, char* filenm)
{
char *cp;
static char buf[_MAX_DIR];
if ((dirnm == NULL) || (*dirnm == '\0'))
return filenm;
if (filenm == NULL)
return dirnm;
if ((cp = strrchr(filenm, '/')) != NULL)
filenm = cp + 1;
strcpy(buf, dirnm);
strcat(buf, "/");
strcat(buf, filenm);
return buf;
}
void _makepath(char *path, char *drive, char *dir, char *name, char *ext)
{
char *cc;
if (path) {
*path=0;
if ((name==NULL)&&(ext==NULL))
strncpy(path, dir, _MAX_DIR);
else {
cc=buildname(dir,name);
if (cc) strncpy(path, cc, _MAX_DIR);
}
if (ext) {
if (*ext!='.')
strcat(path, ".");
strncat(path, ext, 4); /* CPM have 3-char ext + dot */
}
}
}
/* Find file in pathes:
* 1. /name or ./name or ../name is already qualified names
* 2. else search in all pathes described in env var CPMPATH
* (if this var is not exist, StartDir is used)
* 3. else search in current directory
* 4. else return NULL
*/
char* findPath(char* path, char* env)
{
char *p, *envp;
static char name[_MAX_PATH+1];
if (*path == '/' || /* qualified name */
*path == '.')
return path;
if ((envp = getenv(env)) != NULL) /* lookup all pathes */
while (*envp) {
p = name;
while (*envp && (*p = *envp++) != ':') {
if ((uint)(p - name) >= sizeof(name))
break;
++p;
}
if (*--p != '/')
*++p = '/';
++p;
if ((p - name) + strlen(path) >= sizeof(name))
break;
strcpy(p, path);
if (access(name, F_OK) == 0)
return name;
}
/* search within startdir */
p=buildname(StartDir, path);
if (access(p, F_OK) == 0) /* file exist in current dir */
return p;
return NULL;
}
char* _searchenv(char *file, char *varname, char *buf)
{
char* cc;
return strncpy(buf, (cc=findPath(file,varname)) ? cc : "", _MAX_DIR);
}
/* CASE insensitive string compare */
int strucmp(char *d, char *s)
{
register char c1, *s1 = (char *)d, *s2 = (char *)s, c2;
while ( ( c1 = ((c1=*s1++) > 0x60 ? c1 & 0x5F : c1 )) == ( c2 = ((c2=*s2++) > 0x60 ? c2 & 0x5F : c2 )) && c1)
;
return c1 - c2;
}
#endif
char scpm[5]="cpm";
char scom[5]="com";
int load_program( char *pfile)
{
FILE *fp;
int ordsize;
#ifdef __linux__
char* drv="";
#else
char drv[ _MAX_DRIVE];
#endif
char dir[ _MAX_DIR], fname[ _MAX_FNAME], ext[ _MAX_EXT];
_splitpath( pfile, drv, dir, fname, ext);
#ifdef __linux__
if ((access(pfile, F_OK) != 0) && uppercase_flag) {
CharUpperX(fname);
CharUpperX(ext);
CharUpperX(scpm);
CharUpperX(scom);
}
#endif
if (orion128) {
if (stricmp(ext,"ORD")==0) ordfile=1;
else if (stricmp(ext,"BRU")==0) ordfile=1;
else if (stricmp(ext,"RKO")==0) ordfile=2;
}
if ( drv[ 0] == '\0' && dir[ 0] == '\0') {
if ( ext[ 0] == '\0') {
_makepath( filename2, drv, dir, fname, scpm);
_searchenv( filename2, CPMPATH, filename);
if ( filename[ 0] == '\0') {
_makepath( filename2, drv, dir, fname, scom);
_searchenv( filename2, CPMPATH, filename);
}
} else {
_makepath( filename2, drv, dir, fname, ext);
if (ordfile) _searchenv( filename2, ORDPATH, filename);
else _searchenv( filename2, CPMPATH, filename);
}
if ( filename[ 0] == '\0') return FALSE;
if (( fp = fopen( filename, "rb")) == NULL) return FALSE;
_splitpath( filename, drv, dir, fname, ext);
} else if ( ext[ 0] == '\0'){
_makepath( filename, drv, dir, fname, scpm);
if (( fp = fopen( filename, "rb")) == NULL) {
_makepath( filename, drv, dir, fname, scom);
if (( fp = fopen( filename, "rb")) == NULL) return FALSE;
}
_splitpath( filename, drv, dir, fname, ext);
} else {
if (( fp = fopen( pfile, "rb")) == NULL) return FALSE;
}
if (ordfile) {
if (ordfile==2) /* ftRko */
fread( &(mem[0x100]), 1, 77, fp); /* skip RKO header */
ordsize=fread( &(mem[0x100]), 1, 16, fp);
if (ordsize==16) { /* read ordos header */
ordsize=*((word*)&mem[0x100+10]);
ordaddr=*((word*)&mem[0x100+8]);
if (ordsize+ordaddr>=0xf400)
ordsize=0xf400-ordaddr;
if (ordsize>0)
fread( &mem[ordaddr], 1, ordsize, fp);
}
else {
fclose( fp);
return FALSE;
}
}
else fread( &(mem[0x100]), 1, BDOS_ORG-0x100, fp);
fclose( fp);
if ( stricmp( ext, "COM") == 0)
cpm_version = 0x122;
cpmdrv0 = cpm_drive[ 0] = (char *)malloc( strlen( drv) + strlen( dir) + 1);
#ifdef __linux__
strcpy( cpm_drive[ 0], dir);
#else
_makepath( cpm_drive[ 0], drv, dir, NULL, NULL);
#endif
return TRUE;
}
void mkFCB( byte *p, char *s)
{
int i, c;
*p = 0;
if ( s[ 1] == ':') {
if (( c = *s) >= 'a') c &= 0x5f;
*p = (byte)( c - 'A' + 1); s += 2;
}
p++;
for ( i = 0; i < 8; i++) {
c = ( *s && *s != '.') ? *s++ : ' ';
// if ( c >= 'a' && c <= 'z') c &= 0x5f;
*p++ = (byte)c;
}
while ( *s && *s != '.') s++;
if ( *s) s++;
for ( i = 0; i < 3; i++) {
c = ( *s) ? *s++ : ' ';
// if ( c >= 'a' && c <= 'z') c &= 0x5f;
*p++ = (byte)c;
}
}
char *mk_filename( char *s, byte *fcb)
{
int i, j;
char *dir;
#ifdef __linux__
char *dot=NULL;
#endif
i = fcb[ 0];
if ( i == '?' || i == 0) i = cpm_disk_no;
else i--;
if ( i >= MAXDRV) return NULL;
if ( cpm_drive[ i] == NULL) {
if ( cpm_disk_vct == 0) setup_disk_vct();
if ( cpm_drive[ i] == NULL) return NULL;
}
strcpy( s, cpm_drive[ i]);
s += i = strlen( s);
if ( i && s[ -1] != CH_SLASH && s[ -1] != '/' && s[ -1] != ':') *s++ = '/';
dir = s;
for ( j = 8; j >= 1 && fcb[ j] == '?'; j--);
j++;
for ( i = 1; i < j; i++) {
if ( fcb[ i] == ' ') break;
*s++ = fcb[ i];
}
if ( i == j && i <= 8) *s++ = '*';
#ifdef __linux__
dot=s;
#endif
*s++ = '.';
for ( j = 11; j >= 9 && fcb[ j] == '?'; j--);
j++;
for ( i = 9; i < j; i++) {
if ( fcb[ i] == ' ') break;
*s++ = fcb[ i];
#ifdef __linux__
dot=NULL;
#endif
}
if ( i == j && i <= 11) *s++ = '*';
#ifdef __linux__
if (dot) s=dot;
#endif
*s = '\0';
return dir;
}
struct {
FILE *fp;
unsigned pos;
byte *addr;
byte wr, mod, dmy1, dmy2;
} fcbs[ MAXFCB];
struct FCB {
byte dr, f[ 8], t[ 3];
byte ex, s1, s2, rc, d[ 16];
byte cr, r0, r1, r2;
};
#ifdef __linux__
#define HANDLE void*
#define INVALID_HANDLE_VALUE (void*)0
#define FILE_ATTRIBUTE_DIRECTORY S_IFDIR
#define FILE_ATTRIBUTE_HIDDEN 2
#define chsize ftruncate
#endif
HANDLE hFindFile = INVALID_HANDLE_VALUE;
#ifdef __linux__
typedef struct {
DWORD dwFileAttributes;
DWORD nFileSizeHigh;
DWORD nFileSizeLow;
char cFileName[ _MAX_PATH ];
char cAlternateFileName[ 14 ];
} WIN32_FIND_DATA;
char ffName[_MAX_PATH];
char ffDir[_MAX_PATH];
DIR *FindFirstFile(char* lpFileName, // pointer to name of file to search for
WIN32_FIND_DATA *lpFindFileData) // pointer to returned information
{
DIR *pDir;
struct dirent *pDirent;
struct stat statbuf;
char *cDir, *cName=lpFileName;
int szDir;
cDir=strrchr(lpFileName, '|');
if (cDir==NULL)
cDir=strrchr(lpFileName, '/');
if ((cDir)!=NULL) {
cName=cDir+1;
cDir=lpFileName;
szDir=cName-cDir-1;
strncpy(ffDir, cDir, szDir);
ffDir[szDir]=0;
}
else {
strncpy(ffDir, cpm_drive[ cpm_disk_no], sizeof(ffDir));
}
strncpy(ffName, cName, sizeof(ffName)-1);
if (strncmp(ffName,"*.*",4)==0) {
*ffName='*';
ffName[1]=0;
}
CharUpperX(ffName);
pDir = opendir(ffDir);
if (pDir == NULL)
return hFindFile=INVALID_HANDLE_VALUE;
while ((pDirent = readdir(pDir)) != NULL) {
strncpy(lpFindFileData->cFileName, pDirent->d_name, _MAX_PATH-1);
if (fnmatch(ffName, CharUpperX(pDirent->d_name), FNM_CASEFOLD)==0) {
if (stat(buildname(ffDir,lpFindFileData->cFileName), &statbuf)!=0) {
closedir(pDir);
DEBUGOUT(stderr, "FindFirst ERROR: failed while stat file `%s`\n",lpFindFileData->cFileName);
return hFindFile=INVALID_HANDLE_VALUE;
}
if (!(statbuf.st_mode & S_IFDIR)) {
lpFindFileData->cAlternateFileName[0]=0;
lpFindFileData->dwFileAttributes=(*(lpFindFileData->cFileName)=='.' ? FILE_ATTRIBUTE_HIDDEN : 0);
lpFindFileData->nFileSizeHigh=0;
lpFindFileData->nFileSizeLow=(DWORD)statbuf.st_size;
return pDir;
}
}
}
closedir(pDir);
return hFindFile=INVALID_HANDLE_VALUE;
}
int FindNextFile(
DIR *hFndFile, // handle to search
WIN32_FIND_DATA *lpFindFileData) // pointer to structure for data on found file
{
struct dirent *pDirent;
struct stat statbuf;
if (hFndFile==INVALID_HANDLE_VALUE)
return 0;
while ((pDirent = readdir(hFndFile)) != NULL) {
strncpy(lpFindFileData->cFileName, pDirent->d_name, _MAX_PATH-1);
if (fnmatch(ffName, CharUpperX(pDirent->d_name), FNM_CASEFOLD)==0) {
if (stat(buildname(ffDir,lpFindFileData->cFileName), &statbuf)!=0) {
DEBUGOUT(stderr, "FindNext ERROR: failed while stat file `%s`\n",lpFindFileData->cFileName);
break;
}
if (!(statbuf.st_mode & S_IFDIR)) {
lpFindFileData->cAlternateFileName[0]=0;
lpFindFileData->dwFileAttributes=(*(lpFindFileData->cFileName)=='.' ? FILE_ATTRIBUTE_HIDDEN : 0);
lpFindFileData->nFileSizeHigh=0;
lpFindFileData->nFileSizeLow=(DWORD)statbuf.st_size;
return 1;
}
}
}
// if (hFndFile!=INVALID_HANDLE_VALUE) closedir(hFndFile);
// hFindFile=INVALID_HANDLE_VALUE;
return 0;
}
int FindClose(
DIR *hFndFile // file search handle
)
{
int res;
if (hFndFile && (hFndFile!=INVALID_HANDLE_VALUE)) {
res=closedir(hFndFile);
hFindFile=INVALID_HANDLE_VALUE;
return res==0;
}
return TRUE;
}
int GetDiskFreeSpace(
char* lpRootPathName, // address of root path
DWORD *lpSectorsPerCluster, // address of sectors per cluster
DWORD *lpBytesPerSector, // address of bytes per sector
DWORD *lpNumberOfFreeClusters, // address of number of free clusters
DWORD *lpTotalNumberOfClusters // address of total number of clusters
)
{
struct statvfs stat;
if (statvfs(lpRootPathName, &stat) != 0) {
// error happens, just quits here
return 0;
}
*lpTotalNumberOfClusters=stat.f_blocks;
*lpNumberOfFreeClusters=stat.f_bavail;
*lpBytesPerSector=stat.f_bsize;
*lpSectorsPerCluster=stat.f_frsize/stat.f_bsize;
return 1;
}
#endif
byte cpm_findnext( void)
{
WIN32_FIND_DATA aFindData;
char *p;
struct FCB *fcb = (struct FCB *)(mem + cpm_dma_addr);
if ( hFindFile == INVALID_HANDLE_VALUE) return 0xff;
while ( FindNextFile( hFindFile, &aFindData)) {
p = aFindData.cAlternateFileName;
if ( !*p) p = aFindData.cFileName;
if (( aFindData.dwFileAttributes &
(FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN)) == 0) {
DWORD l = aFindData.nFileSizeLow;
if ( aFindData.nFileSizeHigh || l > 0x7fff80) l = 0x7fff80;
l = (l + 0x7f) >> 7;
fcb->ex = (byte)(l >> 7);
fcb->rc = (byte)(l & 0x7f);
mkFCB( mem + cpm_dma_addr, p);
return 0;
}
}
FindClose( hFindFile);
hFindFile = INVALID_HANDLE_VALUE;
return 0xff;
}
byte cpm_findfirst( byte *fcbaddr)
{
WIN32_FIND_DATA aFindData;
char *p;
struct FCB *fcb = (struct FCB *)(mem + cpm_dma_addr);
DWORD l;
if ( hFindFile != INVALID_HANDLE_VALUE) FindClose( hFindFile);
if ( !mk_filename( filename, fcbaddr)) return 0xff;
hFindFile = FindFirstFile( filename, &aFindData);
if ( hFindFile == INVALID_HANDLE_VALUE) return 0xff;
p = aFindData.cAlternateFileName;
if ( !*p) p = aFindData.cFileName;
if ( aFindData.dwFileAttributes &
(FILE_ATTRIBUTE_DIRECTORY | FILE_ATTRIBUTE_HIDDEN)) {
return cpm_findnext();
}
l = aFindData.nFileSizeLow;
if ( aFindData.nFileSizeHigh || l > 0x7fff80) l = 0x7fff80;
l = (l + 0x7f) >> 7;
fcb->ex = (byte)(l >> 7);
fcb->rc = (byte)(l & 0x7f);
mkFCB( mem + cpm_dma_addr, p);
return 0;
}
#define CFRW_SEQ 0
#define CFRW_RND 1
#define CFRW_RD 0
#define CFRW_WR 1
#define CF_OPEN 0
#define CF_WROPEN 1
#define CF_CREATE 2
byte cpm_file_open( byte *fcbaddr, int cr)
{
int i;
int d = 0;
struct FCB *fcb = (struct FCB *)fcbaddr;
#ifdef __linux__
DIR *hFndFile;
WIN32_FIND_DATA aFindData;
char dir[ _MAX_DIR], fname[ _MAX_FNAME], ext[ _MAX_EXT];
#endif
if (( i = fcb->d[ 0]) > 0 && --i < MAXFCB &&
fcbs[ i].addr == fcbaddr && fcbs[ i].fp) {
DEBUGOUT( stderr, "REOPEN %d - ", i);
fclose( fcbs[ i].fp); fcbs[ i].fp = NULL;
} else { // 2008.12 FCBðNA·éAvÉÎ
for ( i = 0; i < MAXFCB && fcbs[ i].fp; i++) {
if ( fcbs[ i].addr == fcbaddr) {
fclose( fcbs[ i].fp); fcbs[ i].fp = NULL; break;
}
}
}
if ( i >= MAXFCB || !mk_filename( filename, fcbaddr)) return 0xff;
if ( cr == CF_CREATE) {
if ( access( filename, F_OK) == 0) return 0xff;
if (( fcbs[ i].fp = fopen( filename, "w+b")) == NULL) return 0xff;
fcbs[ i].mod = fcbs[ i].wr = TRUE;
} else {
if (( fcbs[ i].fp = fopen( filename, cr ? "r+b" : "rb")) == NULL)
{
#ifdef __linux__
if ((hFndFile=FindFirstFile( filename, &aFindData))==INVALID_HANDLE_VALUE)
return 0xff;
_splitpath( filename, NULL, dir, fname, ext);
fcbs[ i].fp = fopen( buildname(dir, aFindData.cFileName), cr ? "r+b" : "rb");
FindClose( hFndFile);
if (fcbs[ i].fp == NULL)
return 0xff;
#else
return 0xff;
#endif
}
fcbs[ i].mod = fcbs[ i].wr = FALSE;
fseek( fcbs[ i].fp, 0, SEEK_END);
d = ftell( fcbs[ i].fp);
fseek( fcbs[ i].fp, 0, SEEK_SET);
d = ( d + 0x7f) >> 7;
}
fcbs[ i].pos = 0;
fcbs[ i].addr = fcbaddr;
memset( fcbaddr + 12, 0, 33-12);
fcb->d[0] = (byte)(i + 1);
fcb->d[1] = (byte)d;
fcb->d[2] = (byte)(d >> 8);
fcb->d[3] = (byte)(d >> 16);
fcb->rc = (byte)(( d >= 127) ? 127 : d);
return 0;
}
byte cpm_file_close( byte *fcbaddr)
{
int i;
struct FCB *fcb = (struct FCB *)fcbaddr;
if (( i = fcb->d[ 0]) > 0 && --i < MAXFCB) {
DEBUGOUT( stderr, "CLOSE %d - ", i);
if ( fcbs[ i].fp) fclose( fcbs[ i].fp);
fcbs[ i].fp = 0;
fcb->d[ 0] = 0;
return 0;
}
if ( i == 0) return 0;
return 0xff;
}
void cpm_change_filesize( byte *fcbaddr)
{
int i, sz;
struct FCB *fcb = (struct FCB *)fcbaddr;
sz = (fcb->d[ 3] << 16) + (fcb->d[2] << 8) + fcb->d[1];
if (( sz >> 7) == fcb->ex && (sz & 0x7f) > fcb->rc &&
mk_filename( filename, fcbaddr) &&
( i = open( filename, O_RDWR)) != -1) {
chsize( i, (( fcb->ex << 7) + fcb->rc) << 7);
close( i);
DEBUGOUT( stderr, "Change file size : %s\n", filename);
}
}
byte cpm_file_rw( byte *fcbaddr, byte wr, int rnd)
{
int i, n, p, max;
struct FCB *fcb = (struct FCB *)fcbaddr;
if ( rnd) {
if ( fcb->r2) return 6;
p = fcb->r0 + ( fcb->r1 << 8);
fcb->cr = (byte)(p & 0x7f); fcb->ex = (byte)(p >> 7);
} else {
p = (fcb->cr & 0x7f) + (fcb->ex << 7);
}
max = fcb->d[1] + (fcb->d[2] << 8) + (fcb->d[3] << 16);
DEBUGOUT( stderr, "BDOS: %s%s file '%11.11s'. @%d",
rnd ? "random ": "", wr ? "write" : "read", fcbaddr + 1, p);
if (( i = fcb->d[ 0]) == 0 || --i >= MAXFCB || fcbs[ i].fp == NULL) {
if ( !wr && p >= max) return 1;
if ( cpm_file_open( fcbaddr, wr ? CF_WROPEN : CF_OPEN)) return 0xff;
fcb->cr = (byte)(p & 0x7f); fcb->ex = (byte)(p >> 7); /* 2012.02 bugfix 8->7 */
i = fcb->d[ 0] - 1;
} else if ( !fcbs[ i].mod && wr) {
fclose( fcbs[ i].fp);
if ( !mk_filename( filename, fcbaddr) ||
( fcbs[ i].fp = fopen( filename, "r+b")) == NULL) {
cpm_file_close( fcbaddr);
return 0xff;
}
fcbs[ i].pos = 0;
}
if ((unsigned)(n = p << 7) != fcbs[ i].pos || fcbs[ i].wr != wr) {
if (( !wr && p > max) ||
fseek( fcbs[ i].fp, n, SEEK_SET)) {
cpm_file_close( fcbaddr);
return 1;
}
fcbs[ i].pos = n;
}
if ( wr) {
fcbs[ i].mod = fcbs[ i].wr = TRUE;
if ( fwrite( mem + cpm_dma_addr, 128, 1, fcbs[ i].fp) != 1)
return 1;
n = 128;
} else {
fcbs[ i].wr = FALSE;
if (( n = fread( mem + cpm_dma_addr, 1, 128, fcbs[ i].fp)) <= 0) {
if ( !fcbs[ i].mod) cpm_file_close( fcbaddr);
return 1;
}
if ( n < 128) memset( mem + cpm_dma_addr + n, 0x1a, 128 - n);
}
fcbs[ i].pos += n;
++p;
if ( !rnd) { fcb->cr = (byte)(p & 0x7f); fcb->ex = (byte)(p >> 7); }
if ( p >= max && !fcbs[ i].mod) cpm_file_close( fcbaddr);
if ( p > max) {
fcb->d[ 1] = (byte)p;
fcb->d[ 2] = (byte)(p >> 8);
fcb->d[ 3] = (byte)(p >> 16);
fcb->rc = (byte)(p & 0x7f);
}
return 0;
}
byte cpm_file_size( byte *fcbaddr)
{
struct FCB *fcb = (struct FCB *)fcbaddr;
DWORD d;
if ( cpm_file_open( fcbaddr, CF_OPEN)) return 0xff;
d = fcb->d[1] + (fcb->d[2] << 8) + (fcb->d[3] << 16);
fcb->r0 = (byte)d;
fcb->r1 = (byte)(d >> 8);
fcb->r2 = (byte)(d >> 16);
cpm_file_close( fcbaddr);
return 0;
}
byte cpm_file_delete( byte *fcbaddr)
{
struct FCB *fcb = (struct FCB *)fcbaddr;
int i;
HANDLE hFndFile;