-
Notifications
You must be signed in to change notification settings - Fork 1
/
ntvcm.cxx
3298 lines (2940 loc) · 122 KB
/
ntvcm.cxx
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
// NT Virtual CP/M Machine
// This app runs CP/M 2.2 .com programs on Windows, MacOS, Linux, and DOS (real-mode 8086 using WATCOM compiler)
// Written by David Lee in late 2022
// Notes: -- Only the subset of CP/M 2.2 required to run the apps I tested is implemented.
// -- asm.com, load.com, and Turbo Pascal 1.00 & 3.01A are rested.
// -- Also tested with Wordstar Release 4 and mbasic.com BASIC-80 Ref 5.21.
// -- Also tested with Aztec C 1.06. compiler, assembler, linker, and generated apps work.
// -- Also tested with MBasic.
// -- Use tinst.com to configure Turbo Pascal 1.00 for vT100 and 3.01A for ANSI to get the screen to work.
// -- pip.com runs for simple file copies. Not tested for other modes, which probably fail.
// -- Tested with AlgolM, Janus ADA, CB80, Turbo Modula 2, Multiplan, Microsoft Fortran, MT Pascal, JRT Pascal, and more.
// -- Can be run in 8080 or Z80 modes (the latter is required for Turbo Pascal).
// -- Optionally detects if an ESC character is output, and switches to 80,24 mode.
// -- Uses x80.?xx for 8080 and Z80 emulation
// -- Not tested for other CP/M apps, which means they probably don't work. If they fail, it's probably
// not the CPU emulation (that's pretty well tested). It's probably the CP/M emulation in this file.
// To build on Windows debug and release:
// cl /nologo /openmp ntvcm.cxx x80.cxx /Oti2 /Ob2 /Qpar /Fa /EHac /Zi /DDEBUG /D_AMD64_ /link user32.lib ntdll.lib /OPT:REF
// cl /nologo /openmp ntvcm.cxx x80.cxx /Oti2 /Ob2 /Qpar /Fa /EHac /Zi /DNDEBUG /D_AMD64_ /link user32.lib ntdll.lib /OPT:REF
// To build on Linux debug and release:
// g++ -ggdb -Ofast -fopenmp -fno-builtin -D DEBUG -I . ntvcm.cxx x80.cxx -lssl -lcrypto -o ntvcm
// g++ -ggdb -Ofast -fopenmp -fno-builtin -D NDEBUG -I . ntvcm.cxx x80.cxx -lssl -lcrypto -o ntvcm
// To build on Windows with mingw64 & g++: (performance is >10% faster than the Microsoft compiler)
// g++ -Ofast -ggdb -fopenmp -D _MSC_VER ntvcm.cxx x80.cxx -I ../djl -D DEBUG -o ntvcm.exe -static -lwininet
// g++ -Ofast -ggdb -fopenmp -D _MSC_VER ntvcm.cxx x80.cxx -I ../djl -D NDEBUG -o ntvcm.exe -static -lwininet
// To build on Windows targeting DOS:
// I used Open Watcom C/C++ x86 16-bit Compile and Link Utility Version 2.0 beta Oct 9 2023 02:19:55 (64-bit)
// https://github.com/open-watcom/open-watcom-v2/releases/tag/Current-build
// wcl -q -zp=1 -ml -obmr -oh -ei -oi -s -0 -xs -j -oe=128 -ol+ -ot ntvcm.cxx x80.cxx -bcl=DOS -k8192 /I. /DWATCOM /DNDEBUG
// helpful: https://open-watcom.github.io/open-watcom-v2-wikidocs/clib.html
// Note: openmp, wininet, ssl, crypto, and djl_rssrdr.hxx are only required for RSS support.
//
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <cstring>
#include <assert.h>
#include <ctype.h>
#include <sys/stat.h>
#include <djl_os.hxx>
#include <djltrace.hxx>
#include <djl_con.hxx>
#include <djl_cycle.hxx>
// On non-Windows platforms djl_rssrdr.hxx has a dependency on:
// httplib.h from https://github.com/yhirose/cpp-httplib
// openssl headers and libraries
#ifdef NTVCM_RSS_SUPPORT
#include <djl_rssrdr.hxx>
CRssFeed g_rssFeed;
#endif //NTVCM_RSS_SUPPORT
#include "x80.hxx"
#include "ntvcm.h" // get bdos extensions
// CP/M constants for memory addresses where OS-global state is stored
#define FCB_ARG1_OFFSET 0x5c
#define FCB_ARG2_OFFSET 0x6c
#define COMMAND_TAIL_LEN_OFFSET 0x80
#define COMMAND_TAIL_OFFSET 0x81
#define DEFAULT_DMA_OFFSET 0x80 // read arguments before doing I/O because it's the same address
// The BDOS invocation function that ships with M80 incorrectly assumes the BIOS_JUMP_TABLE low byte (minus 3) is 0.
// The BDOS_ENTRY address must be one byte beyond free memory for the app.
// So 260 bytes must be reserved: 256 for page-aligned BIOS and 4 bytes for the BDOS stub.
const uint8_t BDOS_ENTRY_LO = 0xfc;
const uint8_t BDOS_ENTRY_HI = 0xfe;
const uint16_t BDOS_ENTRY = ( ( BDOS_ENTRY_HI << 8 ) | BDOS_ENTRY_LO );
const uint8_t BIOS_JUMP_TABLE_LO = 0x00;
const uint8_t BIOS_JUMP_TABLE_HI = 0xff;
const uint16_t BIOS_JUMP_TABLE = ( ( BIOS_JUMP_TABLE_HI << 8 ) | BIOS_JUMP_TABLE_LO );
const uint16_t BIOS_FUNCTIONS = 0xff40; // 17 1-byte hooks
const uint16_t BIOS_FUNCTION_COUNT = 17;
const uint8_t DPB_OFFSET_LO = 0x60; // lo part of DPB
const uint8_t DPB_OFFSET_HI = 0xff; // where the Disk Parameter Block resides for BDOS 31.
const uint16_t DPB_OFFSET = ( ( DPB_OFFSET_HI << 8 ) | DPB_OFFSET_LO );
#define CPM_FILENAME_LEN ( 8 + 3 + 1 + 1 ) // name + type + dot + null
// cr = current record = ( file pointer % 16k ) / 128
// ex = current extent = ( file pointer % 512k ) / 16k
// s2 = extent high = ( file pointer / 512k )
struct FCB
{
uint8_t dr; // 00 0 = default drive, 1 = A ... 16 = P
uint8_t f[8]; // 01 file name. uppercase ascii or spaces
uint8_t t[3]; // 09 file type. uppercase ascii or spaces
uint8_t ex; // 12 extent 0..31 during I/O
uint8_t s1; // 13 reserved for CP/M
uint8_t s2; // 14 reserved for CP/M. extent high byte
uint8_t rc; // 15 record count for extent ex. 0..127
uint8_t dImage[16]; // 16 second half of directory entry OR rename new name
uint8_t cr; // 32 current record to read or write in sequential file operations
uint8_t r0; // 33 low byte of random I/O offset. Also for Compute File Size.
uint8_t r1; // 34 high byte of random I/O offset. Also for Compute File Size.
uint8_t r2; // 35 overflow of r0 and r1 (unused in CP/M 2.2)
// r0 and r1 are a 16-bit count of 128 byte records
uint16_t GetRandomIOOffset() { return ( (uint16_t) this->r1 << 8 ) | this->r0; }
void SetRandomIOOffset( uint16_t o )
{
this->r0 = ( 0xff & o );
this->r1 = ( ( o & 0xff00 ) >> 8 );
// unused in cp/m 2.2 this->r2 = 0;
} //SetRandomIOOffset
void SetRecordCount( FILE * fp )
{
// set rc to file size in 128 byte records if < 16k, else 128
uint32_t file_size = portable_filelen( fp );
if ( file_size >= ( 16 * 1024 ) ) // CP/M 2.2 does this and Whitesmith C's A80.COM and LNK.COM depend on it
this->rc = 128;
else
{
uint32_t tail_size = ( file_size % ( 16 * 1024 ) ); // won't matter because of 16k check above
this->rc = (uint8_t) ( tail_size / 128 );
if ( 0 != ( tail_size % 128 ) )
this->rc++;
}
} //SetRecordCount
void UpdateSequentialOffset( uint32_t offset )
{
cr = (uint8_t) ( ( offset % ( (uint32_t) 16 * 1024 ) ) / (uint32_t) 128 );
ex = (uint8_t) ( ( offset % ( (uint32_t) 512 * 1024 ) ) / ( (uint32_t) 16 * 1024 ) );
s2 = (uint8_t) ( offset / ( (uint32_t) 512 * 1024 ) );
#ifdef WATCOM
tracer.Trace( " new offset: %u, s2 %u, ex %u, cr %u\n", (uint16_t) offset, (uint16_t) s2, (uint16_t) ex, (uint16_t) cr );
#else
tracer.Trace( " new offset: %u, s2 %u, ex %u, cr %u\n", offset, s2, ex, cr );
#endif
} //UpdateSequentialOffset
uint32_t GetSequentialOffset()
{
uint32_t curr = (uint32_t) cr * 128;
curr += ( (uint32_t) ex * ( (uint32_t) 16 * 1024 ) );
curr += ( (uint32_t) s2 * ( (uint32_t) 512 * 1024 ) );
return curr;
} //GetSequentialOffset
void Trace( bool justArg = false ) // justArg is the first 16 bytes at app startup
{
tracer.Trace( " FCB at address %04x:\n", (uint32_t) ( (uint8_t * ) this - memory ) );
tracer.Trace( " drive: %#x == %c\n", dr, ( 0 == dr ) ? 'A' : 'A' + dr - 1 );
tracer.Trace( " filename: '%c%c%c%c%c%c%c%c'\n", 0x7f & f[0], 0x7f & f[1], 0x7f & f[2], 0x7f & f[3],
0x7f & f[4], 0x7f & f[5], 0x7f & f[6], 0x7f & f[7] );
tracer.Trace( " filetype: '%c%c%c'\n", 0x7f & t[0], 0x7f & t[1], 0x7f & t[2] );
tracer.Trace( " R S A: %d %d %d\n", 0 != ( 0x80 & t[0] ), 0 != ( 0x80 & t[1] ), 0 != ( 0x80 & t[2] ) );
tracer.Trace( " ex: %d\n", ex );
tracer.Trace( " s1: %u\n", s1 );
tracer.Trace( " s2: %u\n", s2 );
tracer.Trace( " rc: %u\n", rc );
if ( !justArg )
{
tracer.Trace( " cr: %u\n", cr );
tracer.Trace( " r0: %u\n", r0 );
tracer.Trace( " r1: %u\n", r1 );
tracer.Trace( " r2: %u\n", r2 );
}
} //Trace
};
// this struct is used to cache FILE * objects to avoid open/close for each file access
struct FileEntry
{
char acName[ CPM_FILENAME_LEN ];
FILE * fp;
};
struct CPMTime // non-standard time structure
{
uint16_t hour;
uint16_t minute;
uint16_t second;
uint16_t millisecond;
};
#pragma pack( push, 1 )
struct DiskParameterBlock // for BDOS 31. https://www.seasip.info/Cpm/format22.html
{
uint16_t spt; // Number of 128-byte records per track
uint8_t bsh; // Block shift. 3 => 1k, 4 => 2k, 5 => 4k....
uint8_t blm; // Block mask. 7 => 1k, 0Fh => 2k, 1Fh => 4k...
uint8_t exm; // Extent mask, see later
uint16_t dsm; // (no. of blocks on the disc)-1
uint16_t drm; // (no. of directory entries)-1
uint8_t al0; // Directory allocation bitmap, first byte
uint8_t al1; // Directory allocation bitmap, second byte
uint16_t cks; // Checksum vector size, 0 for a fixed disc
uint16_t off; // Offset, number of reserved tracks
};
#pragma pack(pop)
CDJLTrace tracer;
ConsoleConfiguration g_consoleConfig;
static bool g_haltExecuted = false;
static bool g_emulationEnded = false;
static bool g_exitCodeSet = false;
static uint16_t g_exitCode = 0;
static uint8_t * g_DMA = memory + DEFAULT_DMA_OFFSET;
static vector<FileEntry> g_fileEntries;
static bool g_forceConsole = false;
static bool g_forceLowercase = false;
static bool g_backspaceToDel = false;
static bool g_kayproToCP437 = false;
static size_t g_fileInputOffset = 0;
static vector<char> g_fileInputText;
static bool g_sleepOnKbdLoop = true;
enum terminal_escape { termVT100, termVT52, termKayproII };
static terminal_escape g_termEscape = termVT100;
void dump_memory( const char * pname = "ntvcm.dmp" )
{
FILE * fp = fopen( pname, "wb" );
if ( !fp )
{
printf( "can't create dump file, error %d = %s\n", errno, strerror( errno ) );
return;
}
for ( uint32_t i = 0; i < 65536; i += 128 ) // write in small blocks so it works on DOS too
fwrite( & memory[ i ], 1, 128, fp );
fclose( fp );
} //dump_memory
#ifdef WATCOM
#include <dos.h>
uint32_t DosTimeInMS()
{
struct dostime_t tNow;
_dos_gettime( &tNow );
uint32_t t = (uint32_t) tNow.hour * 60 * 60 * 100;
t += (uint32_t) tNow.minute * 60 * 100;
t += (uint32_t) tNow.second * 100;
t += (uint32_t) tNow.hsecond;
return t * 10;
} //DosTimeInMS
#endif //WATCOM
int ends_with( const char * str, const char * end )
{
size_t len = strlen( str );
size_t lenend = strlen( end );
if ( len < lenend )
return false;
return ( 0 == _stricmp( str + len - lenend, end ) );
} //ends_with
bool ValidCPMFilename( char * pc )
{
if ( !strcmp( pc, "." ) )
return false;
if ( !strcmp( pc, ".." ) )
return false;
const char * pcinvalid = "<>,;:=?[]%|()/\\";
for ( size_t i = 0; i < strlen( pcinvalid ); i++ )
if ( strchr( pc, pcinvalid[i] ) )
return false;
size_t len = strlen( pc );
if ( len > 12 )
return false;
char * pcdot = strchr( pc, '.' );
if ( !pcdot && ( len > 8 ) )
return false;
if ( pcdot && ( ( pcdot - pc ) > 8 ) )
return false;
return true;
} //ValidCPMFilename
#ifdef _MSC_VER
static HANDLE g_hFindFirst = INVALID_HANDLE_VALUE;
void CloseFindFirst()
{
if ( INVALID_HANDLE_VALUE != g_hFindFirst )
{
FindClose( g_hFindFirst );
g_hFindFirst = INVALID_HANDLE_VALUE;
}
} //CloseFindFirst
#elif defined( WATCOM )
static bool g_FindActive = false;
static struct find_t g_FindFirst;
void CloseFindFirst()
{
if ( g_FindActive )
{
g_FindActive = false;
_dos_findclose( & g_FindFirst );
}
} //CloseFindFirst
#else
#include <dirent.h>
static DIR * g_FindFirst = 0;
struct LINUX_FIND_DATA
{
char cFileName[ MAX_PATH ];
};
void CloseFindFirst()
{
if ( 0 != g_FindFirst )
{
closedir( g_FindFirst );
g_FindFirst = 0;
}
} //CloseFindFirst
bool FindNextFileLinux( const char * pattern, DIR * pdir, LINUX_FIND_DATA & fd )
{
do
{
struct dirent * pent = readdir( pdir );
if ( 0 == pent )
return false;
// ignore files CP/M just wouldn't understand
if ( !ValidCPMFilename( pent->d_name ) )
continue;
tracer.Trace( " FindNextFileLinux is matching '%s' with '%s'\n", pattern, pent->d_name );
// if the first character is ?, match any filename per CP/M rules
if ( '?' != pattern[ 0 ] )
{
// If the pattern isn't a question mark, look for a literal match
if ( strcmp( pattern, pent->d_name ) )
continue;
}
else
{
char const * pdot = strchr( pattern, '.' );
if ( pdot )
{
if ( '?' != pdot[ 1 ] )
{
const char * pentdot = strchr( pent->d_name, '.' );
if ( !pentdot || strcmp( pentdot + 1, pdot + 1 ) )
continue;
}
}
}
strcpy( fd.cFileName, pent->d_name );
return true;
} while ( true );
return false;
} //FindNextFileLinux
DIR * FindFirstFileLinux( const char * pattern, LINUX_FIND_DATA & fd )
{
DIR * pdir = opendir( "." );
tracer.Trace( " opendir returned %p, errno %d = %s\n", pdir, errno, strerror( errno ) );
if ( 0 == pdir )
return 0;
bool found = FindNextFileLinux( pattern, pdir, fd );
if ( !found )
{
closedir( pdir );
return 0;
}
return pdir;
} //FindFirstFileLinux
#endif
void ParseFoundFile( char * pfile )
{
tracer.Trace( " ParseFoundFile '%s'\n", pfile );
memset( g_DMA, 0, 128 );
memset( g_DMA + 1, ' ', 11 );
size_t len = strlen( pfile );
assert( len <= 12 ); // 8.3 only
_strupr( pfile );
for ( size_t i = 0; i < len; i++ )
{
if ( '.' == pfile[ i ] )
{
for ( size_t e = 0; e < 3; e++ )
{
if ( 0 == pfile[ i + 1 + e ] )
break;
g_DMA[ 8 + e + 1 ] = pfile[ i + 1 + e ];
}
break;
}
g_DMA[ i + 1 ] = pfile[ i ];
}
tracer.Trace( " search for first/next found '%c%c%c%c%c%c%c%c%c%c%c'\n",
g_DMA[1], g_DMA[2], g_DMA[3], g_DMA[4], g_DMA[5], g_DMA[6], g_DMA[7], g_DMA[8],
g_DMA[9], g_DMA[10], g_DMA[11] );
} //ParseFoundFile
bool parse_FCB_Filename( FCB * pfcb, char * pcFilename )
{
char * orig = pcFilename;
// note: the high bits are used for file attributes. Mask them away
for ( int i = 0; i < 8; i++ )
{
if ( ' ' == ( 0x7f & pfcb->f[ i ] ) )
break;
*pcFilename++ = ( 0x7f & pfcb->f[ i ] );
}
if ( ' ' != pfcb->t[0] )
{
*pcFilename++ = '.';
for ( int i = 0; i < 3; i++ )
{
if ( ' ' == ( 0x7f & pfcb->t[ i ] ) )
break;
*pcFilename++ = ( 0x7f & pfcb->t[ i ] );
}
}
*pcFilename = 0;
// CP/M assumes all filenames are uppercase. Linux users generally use all lowercase filenames
if ( g_forceLowercase )
_strlwr( orig );
return ( pcFilename != orig );
} //parse_FCB_Filename
const char * low_address_names[] =
{
"warm boot",
"warm boot (bios) low",
"warm boot (bios) high",
"tty",
"default drive",
"bdos call",
"bdos low",
"bdos high",
};
void x80_hard_exit( const char * pcerror, uint8_t arg1, uint8_t arg2 )
{
//dump_memory( "ntvcm_hard_exit.dmp" );
g_consoleConfig.RestoreConsole( false );
tracer.Trace( pcerror, arg1, arg2 );
printf( pcerror, arg1, arg2 );
tracer.Trace( " %s\n", build_string() );
printf( " %s\n", build_string() );
exit( 1 );
} //x80_hard_exit
#pragma warning(disable: 4100)
void x80_invoke_out( uint8_t x)
{
} //x80_invoke_out
void x80_invoke_in( uint8_t x)
{
} //x80_invoke_in
void x80_invoke_halt()
{
tracer.Trace( "cpu_halt\n" );
g_haltExecuted = true;
g_emulationEnded = true;
} //x80_invoke_halt
FILE * RemoveFileEntry( char * name )
{
for ( size_t i = 0; i < g_fileEntries.size(); i++ )
{
if ( !strcmp( name, g_fileEntries[ i ].acName ) )
{
FILE * fp = g_fileEntries[ i ].fp;
tracer.Trace( " removing file entry '%s'\n", name );
g_fileEntries.erase( g_fileEntries.begin() + i );
return fp;
}
}
tracer.Trace( "ERROR: could not remove file entry for '%s'\n", name );
return 0;
} //RemoveFileEntry
FILE * FindFileEntry( char * name )
{
for ( size_t i = 0; i < g_fileEntries.size(); i++ )
{
if ( !strcmp( name, g_fileEntries[ i ].acName ) )
{
//tracer.Trace( " found file entry '%s': %p\n", name, g_fileEntries[ i ].fp );
tracer.Trace( " found file entry '%s'\n", name );
return g_fileEntries[ i ].fp;
}
}
tracer.Trace( " could not find an open file entry for '%s'; that might be OK\n", name );
return 0;
} //FindFileEntry
const char * bdos_functions[] =
{
"system reset",
"console input",
"console output",
"reader input",
"punch output",
"list output",
"direct console i/o",
"get i/o byte",
"set i/o byte",
"print string",
"read console buffer",
"get console status",
"return version number",
"reset disk system",
"select disk",
"open file",
"close file",
"search for first",
"search for next",
"delete file",
"read sequential",
"write sequential",
"make file",
"rename file",
"return login vector",
"return current disk",
"set dma address",
"get addr(alloc)",
"write protect disk",
"get read-only vector",
"set file attributes",
"get addr(disk parms)",
"get/set user code",
"read random",
"write random",
"compute file size",
"set random record",
"reset drive",
"write random with zero fill",
};
const char * get_bdos_function( uint8_t id )
{
if ( id < _countof( bdos_functions ) )
return bdos_functions[ id ];
if ( BDOS_GET_TIME == id )
return "get time";
if ( BDOS_SLEEP == id )
return "sleep";
if ( BDOS_INITIALIZE_RSS_FEED == id )
return "initialize rss feed";
if ( BDOS_FETCH_RSS_ITEM == id )
return "fetch rss item";
if ( BDOS_RAND == id )
return "rand";
if ( BDOS_ENABLE_INSTRUCTION_TRACING == id )
return "enable/disable instruction tracing";
if ( BDOS_GET_PUT_PROGRAM_RETURN_CODE == id )
return "get/put program return code";
if ( 45 == id )
return "non - cp/m 2.2: set action on hardware error";
return "unknown";
} //get_bdos_function
const char * get_bios_function( uint16_t id )
{
// the ids are shifted by 3 since I put code start at 0, not -3
id *= 3;
if ( 0 == id )
return "cold start";
if ( 3 == id )
return "warm boot (reload command processor)";
if ( 6 == id )
return "console status";
if ( 9 == id )
return "console input";
if ( 12 == id )
return "console output";
if ( 15 == id )
return "list: printer output";
if ( 18 == id )
return "punch: paper tape punch output";
if ( 21 == id )
return "reader: paper tape reader input";
if ( 24 == id )
return "home: move dic head to track 0";
if ( 27 == id )
return "seldsk: select disc drive";
if ( 30 == id )
return "settrk: set track number";
if ( 33 == id )
return "setsec: set sector number";
if ( 36 == id )
return "setdma: set dma address";
if ( 39 == id )
return "read: read a sector";
if ( 42 == id )
return "write: write a sector";
if ( 45 == id )
return "listst: status of list device";
if ( 48 == id )
return "sectran: sector translation for skewing";
return "unknown";
} //get_bios_function
char kaypro_to_cp437( uint8_t c )
{
#if defined( _MSC_VER ) || defined( WATCOM )
#pragma warning(disable: 4310)
// these are mostly box-drawing characters
if ( 0xb0 == c )
return (char) 0xcd;
if ( 0xd0 == c )
return (char) 0xc9;
if ( 0xd5 == c )
return (char) 0xba;
if ( 0xdf == c )
return (char) 0xbb;
if ( 0x85 == c )
return (char) 0xc8;
if ( 0x8c == c )
return (char) 0xcd;
if ( 0x8a == c )
return (char) 0xbc;
if ( 0xbc == c )
return (char) 0xdf;
if ( 0xbf == c )
return (char) 0xdb;
#else // for linux, use ascii-art
if ( 0xb0 == c || 0x8c == c )
return '-';
if ( 0xd0 == c || 0xdf == c || 0x85 == c || 0x8a == c )
return '+';
if ( 0xd5 == c )
return '|';
if ( 0xbc == c || 0xbf == c )
return '*';
#endif
if ( c >= 0x80 )
tracer.Trace( "untranslated kaypro high character %u == %02x\n", c, c );
return c;
} //kaypro_to_cp437
#ifdef WATCOM
void append( char * pc, size_t len, char c )
{
pc[ len ] = c;
pc[ len + 1 ] = 0;
} //append
void match_vt100( char * pc, size_t len )
{
static bool text_positive = true;
assert( 0x1b == pc[ 0 ] );
if ( len > 1 )
{
char last = pc[ len - 1 ];
if ( '[' == pc[ 1 ] )
{
if ( ( 'H' == last ) && ( strchr( pc, ';' ) ) ) // set cursor position
{
// rows and columns: vt-100 is 1-based, WATCOM library functions are 1-based. DOS is 0-based.
uint8_t row = atoi( pc + 2 );
char * pcol = strchr( pc, ';' ) + 1;
uint8_t col = atoi( pcol );
if ( 0 == row )
row = 1;
if ( 0 == col )
col = 1;
//tracer.Trace( " vt100: setting text position to %u, %u\n", row, col );
_settextposition( row, col );
}
else if ( 'm' == last ) // other display attributes
{
char * pnext = pc + 2;
while ( pnext && ( 'm' != *pnext ) )
{
uint8_t val = atoi( pnext );
if ( 0 == val ) // reset all attributes
{
//tracer.Trace( "vt100 reset text attributes\n" );
_settextcolor( 7 );
_setbkcolor( 0 );
text_positive = true;
}
else if ( 1 == val ) // bright / bold
{
//tracer.Trace( "vt100 text bright\n" );
grcolor tc = _gettextcolor();
if ( tc < 8 )
{
tc += 8;
_settextcolor( tc );
}
}
else if ( 2 == val ) // dim
{
//tracer.Trace( "vt100 text dim\n" );
grcolor tc = _gettextcolor();
if ( tc >= 8 )
{
tc -= 8;
_settextcolor( tc );
}
}
else if ( 3 == val || 23 == val ) // italic on/off
{
// no such feature
}
else if ( 4 == val || 24 == val ) // underline on/off
{
// no such feature
}
else if ( 5 == val || 25 == val ) // blink / unblink cursor
{
// yuck
}
else if ( 7 == val ) // reverse
{
//tracer.Trace( "vt100 text reverse\n" );
if ( text_positive )
{
long bk = _getbkcolor();
grcolor tc = _gettextcolor();
_settextcolor( bk );
_setbkcolor( tc );
text_positive = false;
//tracer.Trace( " reverse colors bk %u text %u\n", tc, bk );
}
}
else if ( 22 == val ) // turn off bold and dim/faint
{
//tracer.Trace( "vt100 normal text\n" );
grcolor tc = _gettextcolor();
_setbkcolor( 0 );
if ( tc >= 8 )
{
tc -= 8;
_setbkcolor( tc );
}
}
else if ( 27 == val ) // positive
{
//tracer.Trace( "vt100 text positive\n" );
if ( !text_positive )
{
long bk = _getbkcolor();
grcolor tc = _gettextcolor();
_settextcolor( bk );
_setbkcolor( tc );
text_positive = true;
//tracer.Trace( " positive colors bk %u text %u\n", tc, bk );
}
}
else if ( 31 == val ) // red
_settextcolor( 4 );
else if ( 32 == val ) // green
_settextcolor( 2 );
else if ( 33 == val ) // yellow
_settextcolor( 6 );
else if ( 34 == val ) // blue
_settextcolor( 1 );
else if ( 35 == val ) // magenta
_settextcolor( 5 );
else if ( 36 == val ) // cyan
_settextcolor( 3 );
else if ( 37 == val ) // white
_settextcolor( 7 );
else
tracer.Trace( " vt100 ignoring display attribute ^[%um\n", val );
pnext = strchr( pnext, ';' );
if ( pnext )
pnext++;
}
}
else if ( 'M' == last ) // delete n lines from the buffer at the current line (scroll up what's above)
{
short n = atoi( pc + 2 );
if ( n > 0 )
{
short x, y, dx, dy;
_gettextwindow( &x, &y, &dx, &dy );
//tracer.Trace( "original text window: %u %u %u %d\n", x, y, dx, dy );
struct rccoord pos = _gettextposition();
//tracer.Trace( "current row, col %u, %u\n", pos.row, pos.col );
//tracer.Trace( "deleting line by scrolling up %d rows at row %d\n", n, pos.row );
_settextwindow( pos.row, 1, 24, 80 ); // temporary view for the scroll
_scrolltextwindow( n );
_settextwindow( x, y, dx, dy ); // restore to the whole window
_settextposition( pos.row, pos.col ); // restore as the scroll moves this
}
}
else if ( !strcmp( pc + 1, "[H" ) ) // home cursor
{
//tracer.Trace( " vt100: home cursor\n" );
_settextposition( 1, 1 );
}
else if ( !strcmp( pc + 1, "[0K" ) || !strcmp( pc + 1, "[K" ) ) // clear line from cursor to right
{
struct rccoord pos = _gettextposition();
int to_clear = 81 - pos.col;
//tracer.Trace( " vt100: clear line from cursor to right, position %u, %u, to_clear %u\n", pos.row, pos.col, to_clear );
char ac[ 81 ];
memset( ac, ' ', to_clear );
ac[ to_clear ] = 0;
_outtext( ac );
_settextposition( pos.row, pos.col ); // restore the cursor position
}
else if ( !strcmp( pc + 1, "[2J" ) ) // erase screen and home cursor
{
//tracer.Trace( " vt100: erase and home cursor\n" );
_clearscreen( _GCLEARSCREEN );
_settextposition( 1, 1 );
}
else if ( !strcmp( pc + 1, "[J" ) ) // erase from current line down to bottom of screen
{
struct rccoord pos = _gettextposition();
short x, y, dx, dy;
_gettextwindow( &x, &y, &dx, &dy );
_settextwindow( pos.row, 1, 24, 80 ); // temporary view for the clear
_clearscreen( _GWINDOW );
_settextwindow( x, y, dx, dy ); // restore to the whole window
_settextposition( pos.row, pos.col ); // restore cursor position
}
else if ( 'L' == last ) // insert n lines at the current line (scroll down what's below)
{
short n = atoi( pc + 2 );
if ( n > 0 )
{
short x, y, dx, dy;
_gettextwindow( &x, &y, &dx, &dy );
struct rccoord pos = _gettextposition();
//tracer.Trace( "current row, col %u, %u\n", pos.row, pos.col );
//tracer.Trace( "inserting line by scrolling down %d rows at row %d\n", n, pos.row );
_settextwindow( pos.row, 1, 24, 80 ); // temporary view for the scroll
_scrolltextwindow( -n );
_settextwindow( x, y, dx, dy ); // restore to the whole window
_settextposition( pos.row, pos.col ); // restore as the scroll moves this
}
}
else if ( 'k' == last )
{
tracer.Trace( " vt100: unhandled K, full string '%s'\n", pc + 1 );
}
else if ( '?' == pc[ 2 ] && 'h' == last ) // cursor commands
{
uint8_t cmd = atoi( pc + 3 );
if ( 7 == cmd ) // enable text wrap mode
_wrapon( _GWRAPON );
else if ( 25 == cmd ) // show cursor
_settextcursor( 0x607 );
else
tracer.Trace( " vt100: unhandled h cursor command %u\n", cmd );
}
else if ( '?' == pc[ 2 ] && 'l' == last ) // cursor commands
{
uint8_t cmd = atoi( pc + 3 );
if ( 7 == cmd ) // reset text wrap mode
_wrapon( _GWRAPOFF );
else if ( 25 == cmd ) // hide cursor
_settextcursor( 0x2000 );
else
tracer.Trace( " vt100: unhandled l cursor command %u\n", cmd );
}
else if ( tolower( last ) >= 'a' && tolower( last ) <= 'z' )
{
tracer.Trace( " vt100: unhandled termination char %c, full string '%s'\n", last, pc + 1 );
_outtext( pc );
}
else
{
//tracer.Trace( " no vt100 match as of yet; waiting for more characters\n" );
return;
}
}
else if ( '<' == pc[ 1 ] ) // enter ANSI mode
{
// (already there)
}
else if ( 'A' == pc[ 1 ] ) // cursor up
{
rccoord rc = _gettextposition();
if ( rc.row > 1 )
_settextposition( rc.row - 1, rc.col );
}
else if ( 'B' == pc[ 1 ] ) // cursor down
{
rccoord rc = _gettextposition();
if ( rc.row < 24 )
_settextposition( rc.row + 1, rc.col );
}
else if ( 'C' == pc[ 1 ] ) // cursor right
{
rccoord rc = _gettextposition();
if ( rc.col < 80 )
_settextposition( rc.row, rc.col + 1 );
}
else if ( 'D' == pc[ 1 ] ) // cursor left
{
rccoord rc = _gettextposition();
if ( rc.col > 1 )
_settextposition( rc.row, rc.col - 1 );
}
else if ( 'H' == pc[ 1 ] ) // cursor home
_settextposition( 1, 1 );
else if ( 'c' == pc[ 1 ] ) // reset terminal
{
_clearscreen( 0 );
_settextposition( 1, 1 );
_settextcolor( 7 );
_setbkcolor( 0 );
_settextcursor( 0x607 );
text_positive = true;
}
else
{
tracer.Trace( " vt100: output unhandled escape sequence doesn't start with a [: '%s'\n", pc + 1 );
_outtext( pc );
}
pc[ 0 ] = 0;
}
} //match_vt100
#endif // WATCOM
// https://en.wikipedia.org/wiki/ANSI_escape_code vt-100 in 1978, it existed at the same time as CP/M
// https://en.wikipedia.org/wiki/VT52 VT52
// https://mdfs.net/Archive/info-cpm/1985/01/19/053100.htm Kaypro II / Lear-Siegler ADM-3A
void output_character( uint8_t c )
{
// for terminal emulation, I only implement translations for actual sequences apps use.
// if the output character is ESC, assume the app wants 80x24.
if ( 0x1b == c && !g_forceConsole && !g_consoleConfig.IsOutputEstablished() )
{