forked from robhagemans/pcbasic
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtechref.html
More file actions
2749 lines (2727 loc) · 108 KB
/
Copy pathtechref.html
File metadata and controls
2749 lines (2727 loc) · 108 KB
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
<!--
PC-BASIC documentation
Copyright (c) 2014-2022 Rob Hagemans
This work is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
http://creativecommons.org/licenses/by-sa/4.0/legalcode
-->
<article>
<h2 id="technical">Technical reference</h2>
<section>
<h3 id="tokenised-file-format">Tokenised file format</h3>
<p>
A tokenised program file on a disk device has the following format.
</p>
<dl>
<dt>
Magic byte
</dt>
<dd>
<code>FF</code>
</dd>
<dt>
Program lines
</dt>
<dd>
Each line is stored as follows:
<div class="scrollable">
<table>
<tr>
<th>
Bytes
</th>
<th>
Format
</th>
<th>
Meaning
</th>
</tr>
<tr>
<td>
2
</td>
<td>
Unsigned 16-bit little-endian integer.
</td>
<td>
Memory location of the line following the current one. This is used internally by
GW-BASIC but ignored when a program is loaded.
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Unsigned 16-bit little-endian integer.
</td>
<td>
The line number.
</td>
</tr>
<tr>
<td>
Variable
</td>
<td>
Tokenised BASIC, see below.
</td>
<td>
The contents of the line.
</td>
</tr>
<tr>
<td>
1
</td>
<td>
<code>00</code> (<code><i>NUL</i></code> byte)
</td>
<td>
End of line marker.
</td>
</tr>
</table>
</div>
</dd>
<dt>
End of file marker
</dt>
<dd>
An <code>1A</code> is written to mark the end of file. This is optional;
the file will be read without problems if it is omitted.
</dd>
</dl>
<h4 id="tokens">Tokenised BASIC</h4>
<p>
The printable ASCII characters in the range <code>20</code>—<code>7E</code>
are used for string literals, comments, variable names,
and elements of statement syntax that are not reserved words.
Reserved words are represented by their <a href="#reserved-words">reserved word tokens</a> and
numeric literals are represented by numeric token sequences.
</p>
<h5>Numeric token sequences</h5>
<p>
Numeric literals are stored in tokenised programs
according to the following representation. All numbers are positive; negative numbers are stored
simply by preceding the number with <code>EA</code>, the token for <code>-</code>.
</p>
<div class="scrollable">
<table>
<tr>
<th style="width:10em">
Class
</th>
<th>
Bytes
</th>
<th>
Format
</th>
</tr>
<tr>
<td>
Indirect line numbers
</td>
<td>
3
</td>
<td>
<code>0E</code> followed by an unsigned 16-bit little-endian integer.
</td>
</tr>
<tr>
<td>
Octal integers
</td>
<td>
3
</td>
<td>
<code>0B</code> followed by an unsigned 16-bit little-endian integer.
</td>
</tr>
<tr>
<td>
Hexadecimal integers
</td>
<td>
3
</td>
<td>
<code>0C</code> followed by an unsigned 16-bit little-endian integer.
</td>
</tr>
<tr>
<td>
Positive decimal integers less than 11
</td>
<td>
1
</td>
<td>
Tokens <code>11</code>—<code>1B</code> represent 0—10.
</td>
</tr>
<tr>
<td>
Positive decimal integers less than 256
</td>
<td>
2
</td>
<td>
<code>0F</code> followed by an unsigned 8-bit integer.
</td>
</tr>
<tr>
<td>
Other decimal integers
</td>
<td>
3
</td>
<td>
<code>1C</code> followed by a two's complement signed 16-bit little-endian integer.
GW-BASIC will recognise a negative number encountered this way but it will not store
negative numbers itself using the two's complement, but rather by preceding the positive
number with <code>EA</code>.
</td>
</tr>
<tr>
<td>
Single precision floating-point number
</td>
<td>
5
</td>
<td>
<code>1D</code> followed by a four-byte single in <a href="#mbf">Microsoft Binary Format</a>.
</td>
</tr>
<tr>
<td>
Double precision floating-point number
</td>
<td>
9
</td>
<td>
<code>1F</code> followed by an eight-byte double in <a href="#mbf">Microsoft Binary Format</a>.
</td>
</tr>
</table>
</div>
<h5 id="reserved-words">Keyword tokens</h5>
<p>
Most keywords in PC-BASIC are <dfn>reserved words</dfn>. Reserved words are represented
in a tokenised program by a single- or double-byte token. The complete list is below.
</p>
<p>
All function names and operators are reserved words and all statements start with a reserved word
(which in the case of <code>LET</code> is optional). However, the converse is not true:
not all reserved words are statements, functions, or operators.
For example, <code>TO</code> and <code>SPC(</code> only occur as part of a statement syntax.
Furthermore, some keywords that form part of statement syntax are not reserved words:
examples are <code>AS</code>, <code>BASE</code>, and <code>ACCESS</code>.
</p>
<p>
Keywords that are not reserved words are spelt out in full text in the tokenised source.
</p>
<p>
A variable or user-defined function name must not be identical to a reserved word. The list below
is an exhaustive list of reserved words that can be used to determine whether a
name is legal.
</p>
<ul class="compact10">
<li><code>81</code> <code>END</code></li>
<li><code>82</code> <code>FOR</code></li>
<li><code>83</code> <code>NEXT</code></li>
<li><code>84</code> <code>DATA</code></li>
<li><code>85</code> <code>INPUT</code></li>
<li><code>86</code> <code>DIM</code></li>
<li><code>87</code> <code>READ</code></li>
<li><code>88</code> <code>LET</code></li>
<li><code>89</code> <code>GOTO</code></li>
<li><code>8A</code> <code>RUN</code></li>
<li><code>8B</code> <code>IF</code></li>
<li><code>8C</code> <code>RESTORE</code></li>
<li><code>8D</code> <code>GOSUB</code></li>
<li><code>8E</code> <code>RETURN</code></li>
<li><code>8F</code> <code>REM</code></li>
<li><code>90</code> <code>STOP</code></li>
<li><code>91</code> <code>PRINT</code></li>
<li><code>92</code> <code>CLEAR</code></li>
<li><code>93</code> <code>LIST</code></li>
<li><code>94</code> <code>NEW</code></li>
<li><code>95</code> <code>ON</code></li>
<li><code>96</code> <code>WAIT</code></li>
<li><code>97</code> <code>DEF</code></li>
<li><code>98</code> <code>POKE</code></li>
<li><code>99</code> <code>CONT</code></li>
<li><code>9C</code> <code>OUT</code></li>
<li><code>9D</code> <code>LPRINT</code></li>
<li><code>9E</code> <code>LLIST</code></li>
<li><code>A0</code> <code>WIDTH</code></li>
<li><code>A1</code> <code>ELSE</code></li>
<li><code>A2</code> <code>TRON</code></li>
<li><code>A3</code> <code>TROFF</code></li>
<li><code>A4</code> <code>SWAP</code></li>
<li><code>A5</code> <code>ERASE</code></li>
<li><code>A6</code> <code>EDIT</code></li>
<li><code>A7</code> <code>ERROR</code></li>
<li><code>A8</code> <code>RESUME</code></li>
<li><code>A9</code> <code>DELETE</code></li>
<li><code>AA</code> <code>AUTO</code></li>
<li><code>AB</code> <code>RENUM</code></li>
<li><code>AC</code> <code>DEFSTR</code></li>
<li><code>AD</code> <code>DEFINT</code></li>
<li><code>AE</code> <code>DEFSNG</code></li>
<li><code>AF</code> <code>DEFDBL</code></li>
<li><code>B0</code> <code>LINE</code></li>
<li><code>B1</code> <code>WHILE</code></li>
<li><code>B2</code> <code>WEND</code></li>
<li><code>B3</code> <code>CALL</code></li>
<li><code>B7</code> <code>WRITE</code></li>
<li><code>B8</code> <code>OPTION</code></li>
<li><code>B9</code> <code>RANDOMIZE</code></li>
<li><code>BA</code> <code>OPEN</code></li>
<li><code>BB</code> <code>CLOSE</code></li>
<li><code>BC</code> <code>LOAD</code></li>
<li><code>BD</code> <code>MERGE</code></li>
<li><code>BE</code> <code>SAVE</code></li>
<li><code>BF</code> <code>COLOR</code></li>
<li><code>C0</code> <code>CLS</code></li>
<li><code>C1</code> <code>MOTOR</code></li>
<li><code>C2</code> <code>BSAVE</code></li>
<li><code>C3</code> <code>BLOAD</code></li>
<li><code>C4</code> <code>SOUND</code></li>
<li><code>C5</code> <code>BEEP</code></li>
<li><code>C6</code> <code>PSET</code></li>
<li><code>C7</code> <code>PRESET</code></li>
<li><code>C8</code> <code>SCREEN</code></li>
<li><code>C9</code> <code>KEY</code></li>
<li><code>CA</code> <code>LOCATE</code></li>
<li><code>CC</code> <code>TO</code></li>
<li><code>CD</code> <code>THEN</code></li>
<li><code>CE</code> <code>TAB(</code></li>
<li><code>CF</code> <code>STEP</code></li>
<li><code>D0</code> <code>USR</code></li>
<li><code>D1</code> <code>FN</code></li>
<li><code>D2</code> <code>SPC(</code></li>
<li><code>D3</code> <code>NOT</code></li>
<li><code>D4</code> <code>ERL</code></li>
<li><code>D5</code> <code>ERR</code></li>
<li><code>D6</code> <code>STRING$</code></li>
<li><code>D7</code> <code>USING</code></li>
<li><code>D8</code> <code>INSTR</code></li>
<li><code>D9</code> <code>'</code></li>
<li><code>DA</code> <code>VARPTR</code></li>
<li><code>DB</code> <code>CSRLIN</code></li>
<li><code>DC</code> <code>POINT</code></li>
<li><code>DD</code> <code>OFF</code></li>
<li><code>DE</code> <code>INKEY$</code></li>
<li><code>E6</code> <code>></code></li>
<li><code>E7</code> <code>=</code></li>
<li><code>E8</code> <code><</code></li>
<li><code>E9</code> <code>+</code></li>
<li><code>EA</code> <code>-</code></li>
<li><code>EB</code> <code>*</code></li>
<li><code>EC</code> <code>/</code></li>
<li><code>ED</code> <code>^</code></li>
<li><code>EE</code> <code>AND</code></li>
<li><code>EF</code> <code>OR</code></li>
<li><code>F0</code> <code>XOR</code></li>
<li><code>F1</code> <code>EQV</code></li>
<li><code>F2</code> <code>IMP</code></li>
<li><code>F3</code> <code>MOD</code></li>
<li><code>F4</code> <code>\</code></li>
<li><code>FD81</code> <code>CVI</code></li>
<li><code>FD82</code> <code>CVS</code></li>
<li><code>FD83</code> <code>CVD</code></li>
<li><code>FD84</code> <code>MKI$</code></li>
<li><code>FD85</code> <code>MKS$</code></li>
<li><code>FD86</code> <code>MKD$</code></li>
<li><code>FD8B</code> <code>EXTERR</code></li>
<li><code>FE81</code> <code>FILES</code></li>
<li><code>FE82</code> <code>FIELD</code></li>
<li><code>FE83</code> <code>SYSTEM</code></li>
<li><code>FE84</code> <code>NAME</code></li>
<li><code>FE85</code> <code>LSET</code></li>
<li><code>FE86</code> <code>RSET</code></li>
<li><code>FE87</code> <code>KILL</code></li>
<li><code>FE88</code> <code>PUT</code></li>
<li><code>FE89</code> <code>GET</code></li>
<li><code>FE8A</code> <code>RESET</code></li>
<li><code>FE8B</code> <code>COMMON</code></li>
<li><code>FE8C</code> <code>CHAIN</code></li>
<li><code>FE8D</code> <code>DATE$</code></li>
<li><code>FE8E</code> <code>TIME$</code></li>
<li><code>FE8F</code> <code>PAINT</code></li>
<li><code>FE90</code> <code>COM</code></li>
<li><code>FE91</code> <code>CIRCLE</code></li>
<li><code>FE92</code> <code>DRAW</code></li>
<li><code>FE93</code> <code>PLAY</code></li>
<li><code>FE94</code> <code>TIMER</code></li>
<li><code>FE95</code> <code>ERDEV</code></li>
<li><code>FE96</code> <code>IOCTL</code></li>
<li><code>FE97</code> <code>CHDIR</code></li>
<li><code>FE98</code> <code>MKDIR</code></li>
<li><code>FE99</code> <code>RMDIR</code></li>
<li><code>FE9A</code> <code>SHELL</code></li>
<li><code>FE9B</code> <code>ENVIRON</code></li>
<li><code>FE9C</code> <code>VIEW</code></li>
<li><code>FE9D</code> <code>WINDOW</code></li>
<li><code>FE9E</code> <code>PMAP</code></li>
<li><code>FE9F</code> <code>PALETTE</code></li>
<li><code>FEA0</code> <code>LCOPY</code></li>
<li><code>FEA1</code> <code>CALLS</code></li>
<li><code>FEA5</code> <code>PCOPY</code></li>
<li><code>FEA7</code> <code>LOCK</code></li>
<li><code>FEA8</code> <code>UNLOCK</code></li>
<li><code>FF81</code> <code>LEFT$</code></li>
<li><code>FF82</code> <code>RIGHT$</code></li>
<li><code>FF83</code> <code>MID$</code></li>
<li><code>FF84</code> <code>SGN</code></li>
<li><code>FF85</code> <code>INT</code></li>
<li><code>FF86</code> <code>ABS</code></li>
<li><code>FF87</code> <code>SQR</code></li>
<li><code>FF88</code> <code>RND</code></li>
<li><code>FF89</code> <code>SIN</code></li>
<li><code>FF8A</code> <code>LOG</code></li>
<li><code>FF8B</code> <code>EXP</code></li>
<li><code>FF8C</code> <code>COS</code></li>
<li><code>FF8D</code> <code>TAN</code></li>
<li><code>FF8E</code> <code>ATN</code></li>
<li><code>FF8F</code> <code>FRE</code></li>
<li><code>FF90</code> <code>INP</code></li>
<li><code>FF91</code> <code>POS</code></li>
<li><code>FF92</code> <code>LEN</code></li>
<li><code>FF93</code> <code>STR$</code></li>
<li><code>FF94</code> <code>VAL</code></li>
<li><code>FF95</code> <code>ASC</code></li>
<li><code>FF96</code> <code>CHR$</code></li>
<li><code>FF97</code> <code>PEEK</code></li>
<li><code>FF98</code> <code>SPACE$</code></li>
<li><code>FF99</code> <code>OCT$</code></li>
<li><code>FF9B</code> <code>LPOS</code></li>
<li><code>FF9A</code> <code>HEX$</code></li>
<li><code>FF9C</code> <code>CINT</code></li>
<li><code>FF9D</code> <code>CSNG</code></li>
<li><code>FF9E</code> <code>CDBL</code></li>
<li><code>FF9F</code> <code>FIX</code></li>
<li><code>FFA0</code> <code>PEN</code></li>
<li><code>FFA1</code> <code>STICK</code></li>
<li><code>FFA2</code> <code>STRIG</code></li>
<li><code>FFA3</code> <code>EOF</code></li>
<li><code>FFA4</code> <code>LOC</code></li>
<li><code>FFA5</code> <code>LOF</code></li>
</ul>
<p>
The following additional reserved words are activated by the option
<code><a href="#--syntax">syntax</a>={pcjr|tandy}</code>.
</p>
<ul class="compact10">
<li><code>FEA4</code> <code>NOISE</code></li>
<li><code>FEA6</code> <code>TERM</code></li>
</ul>
<h5 id="internal-use-tokens">Internal use tokens</h5>
<p>
The tokens <code>10</code>, <code>1E</code> and <code>0D</code> are
known to be used internally by GW-BASIC. They should not appear in a
correctly stored tokenised program file.
</p>
<h4 id="mbf">Microsoft Binary Format</h3>
<p>
Floating point numbers in GW-BASIC and PC-BASIC are represented in <dfn>Microsoft Binary Format
(MBF)</dfn>, which differs from the IEEE 754 standard used by practically all modern software and hardware.
Consequently, binary files generated by either BASIC are
fully compatible with each other and with some applications contemporary to
GW-BASIC, but not easily interchanged with other software. QBASIC, for example, uses IEEE
floats.
</p>
<p>
MBF differs from IEEE in the position of the sign bit and in using
only 8 bits for the exponent, both in single- and in double-precision.
This makes the range of allowable numbers in an MBF double-precision number smaller,
but their precision higher, than for an IEEE double:
an MBF single has 23 bits of precision,
while an MBF double has 55 bits of precision. Both have the same range.
</p>
<p>
Unlike IEEE, the Microsoft Binary Format does not support signed zeroes, subnormal numbers, infinities or not-a-number values.
</p>
<p>
MBF floating point numbers are represented in bytes as follows:
</p>
<dl class="compact">
<dt>
Single
</dt>
<dd>
<var>M<sub>3</sub></var> <var>M<sub>2</sub></var> <var>M<sub>1</sub></var> <var>E<sub>0</sub></var>
</dd>
<dt>
Double
</dt>
<dd>
<var>M<sub>7</sub></var> <var>M<sub>6</sub></var> <var>M<sub>5</sub></var> <var>M<sub>4</sub></var> <var>M<sub>3</sub></var> <var>M<sub>2</sub></var> <var>M<sub>1</sub></var> <var>E<sub>0</sub></var>
</dd>
</dl>
<p>
Here, <var>E<sub>0</sub></var> is the <dfn>exponent byte</dfn> and the
other bytes form the <dfn>mantissa</dfn>, in little-endian order so that
<var>M<sub>1</sub></var> is the most significant byte.
The most significant bit of <var>M<sub>1</sub></var> is the <dfn>sign bit</dfn>, followed
by the most significant bits of the mantissa:
<var>M<sub>1</sub></var> = <var>s<sub>0</sub> f<sub>1</sub> f<sub>2</sub> f<sub>3</sub> f<sub>4</sub> f<sub>5</sub> f<sub>6</sub> f<sub>7</sub></var>.
The other bytes contain the less-significant mantissa bits:
<var>M<sub>2</sub></var> = <var>f<sub>8</sub> f<sub>9</sub> f<sub>A</sub> f<sub>B</sub> f<sub>C</sub> f<sub>D</sub> f<sub>E</sub> f<sub>F</sub></var>, and so on.
</p>
<p>
The value of the floating-point number is <var>v</var> = 0 if <var>E<sub>0</sub></var> = 0 and
<var>v</var> = (-1) <sup><var>s<sub>0</sub></var></sup> × <var>mantissa</var> × 2 <sup><var>E<sub>0</sub></var> - 128</sup> otherwise,
where
the mantissa is formed as a binary fraction
<var>mantissa</var> = 0 . 1 <var>f<sub>1</sub></var> <var>f<sub>2</sub></var> <var>f<sub>3</sub></var> ...
</p>
</section>
<hr />
<section>
<h3 id="protected-file-format">Protected file format</h3>
<p>
The protected format is an encrypted form of the tokenised format.
GW-BASIC would refuse to show the source code of such files.
This protection scheme could easily be circumvented by changing a flag in memory.
Deprotection programs have circulated widely for decades and the decryption
algorithm and keys were published in a mathematical magazine.
</p>
<p>
A protected program file on a disk device has the following format.
</p>
<dl>
<dt>
Magic byte
</dt>
<dd>
<code>FE</code>
</dd>
<dt>
Payload
</dt>
<dd>
Encrypted content of a tokenised program file, including its
end of file marker but excluding its magic byte. The encription cipher
rotates through an 11-byte and a 13-byte key so that the resulting
transformation is the same after 143 bytes. For each byte,
<ul>
<li>
Subtract the corresponding byte from the 11-byte sequence<br />
<code>0B 0A 09 08 07 06 05 04 03 02 01</code>
</li>
<li>
Exclusive-or with the corresponding byte from the 11-byte key<br />
<code>1E 1D C4 77 26 97 E0 74 59 88 7C</code>
</li>
<li>
Exclusive-or with the corresponding byte from the 13-byte key<br />
<code>A9 84 8D CD 75 83 43 63 24 83 19 F7 9A</code>
</li>
<li>
Add the corresponding byte from the 13-byte sequence<br />
<code>0D 0C 0B 0A 09 08 07 06 05 04 03 02 01</code>
</li>
</ul>
</dd>
<dt>
End of file marker
</dt>
<dd>
An <code>1A</code> is written to mark the end of file. This is optional;
the file will be read without problems if it is omitted. Since the
end-of-file marker of the tokenised program is included in the encrypted
content, a protected file is usually one byte longer than its
unprotected equivalent.
</dd>
</dl>
</section>
<hr />
<section>
<h3 id="bsave-file-format"><code>BSAVE</code> file format</h3>
<p>
A memory-dump file on a disk device has the following format.
</p>
<dl>
<dt>
Magic byte
</dt>
<dd>
<code>FD</code>
</dd>
<dt>
Header
</dt>
<dd>
<div class="scrollable">
<table>
<tr>
<th>
Bytes
</th>
<th>
Format
</th>
<th>
Meaning
</th>
</tr>
<tr>
<td>
2
</td>
<td>
Unsigned 16-bit little-endian integer.
</td>
<td>
Segment of the memory block.
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Unsigned 16-bit little-endian integer.
</td>
<td>
Offset of the first byte of the memory block.
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Unsigned 16-bit little-endian integer.
</td>
<td>
Length of the memory block in bytes.
</td>
</tr>
</table>
</div>
</dd>
<dt>
Payload
</dt>
<dd>
The bytes of the memory block.
</dd>
<dt>
Footer
</dt>
<dd>
On Tandy only, the magic byte and the six bytes of the header are repeated here.
This is optional;
the file will be read without problems if it is omitted.
</dd>
<dt>
End of file marker
</dt>
<dd>
An <code>1A</code> is written to mark the end of file. This is optional;
the file will be read without problems if it is omitted.
</dd>
</dl>
</section>
<hr />
<section>
<h3 id="tape-file-format">Cassette file format</h3>
<p>
Files on cassette are stored as frequency-modulated sound. The payload
format of files on cassette is the same as for files on disk device, but
the headers are different and the files may be split in chunks.
</p>
<h5 id="tape-modulation">Modulation</h5>
<p>
A 1-bit is represented by a single 1 ms wave period (1000 Hz).
A 0-bit is represented by a single 0.5 ms wave period (2000 Hz).
</p>
<h5 id="tape-byte">Byte format</h5>
<p>
A byte is sent as 8 bits, most significant first. There are no start- or stopbits.
</p>
<h5 id="tape-record">Record format</h5>
<p>
A file is made up of two or more records. Each record has the following format:
<div class="scrollable">
<table>
<tr>
<th>
Length
</th>
<th>
Format
</th>
<th>
Meaning
</th>
</tr>
<tr>
<td>
256 bytes
</td>
<td>
All <code>FF</code>
</td>
<td>
2048 ms pilot wave at 1000 Hz, used for calibration.
</td>
</tr>
<tr>
<td>
1 bit
</td>
<td>
<code>0</code>
</td>
<td>
Synchronisation bit.
</td>
</tr>
<tr>
<td>
1 byte
</td>
<td>
<code>16</code> (<code><i>SYN</i></code>)
</td>
<td>
Synchronisation byte.
</td>
</tr>
<tr>
<td>
256 bytes
</td>
<td>
</td>
<td>
Data block.
</td>
</tr>
<tr>
<td>
2 bytes
</td>
<td>
Unsigned 16-bit <em>big-endian</em> integer
</td>
<td>
CRC-16-CCITT checksum.
</td>
</tr>
<tr>
<td>
31 bits
</td>
<td>
30 1s followed by a 0.
</td>
<td>
End of record marker.
</td>
</tr>
</table>
</div>
<p>
Tokenised, protected and <code>BSAVE</code> files consist of a header record
followed by a single record which may contain multiple 256-byte data blocks, each followed by the 2 CRC bytes.
Plain text program files and data files consist of a header record followed by multiple single-block records.
</p>
<h5 id="tape-header">Header block format</h5>
<div class="scrollable">
<table>
<tr>
<th>
Bytes
</th>
<th>
Format
</th>
<th>
Meaning
</th>
</tr>
<tr>
<td>
1
</td>
<td>
<code>A5</code>
</td>
<td>
Header record magic byte
</td>
</tr>
<tr>
<td>
8
</td>
<td>
8 characters
</td>
<td>
Filename.
</td>
</tr>
<tr>
<td>
1
</td>
<td>
</td>
<td>
File type. <code>00</code> for data file,
<code>01</code> for memory dump,
<code>20</code> or <code>A0</code> for protected,
<code>40</code> for plain text program,
<code>80</code> for tokenised program.
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Unsigned 16-bit little-endian integer
</td>
<td>
Length of next data record, in bytes.
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Unsigned 16-bit little-endian integer
</td>
<td>
Segment of memory location.
</td>
</tr>
<tr>
<td>
2
</td>
<td>
Unsigned 16-bit little-endian integer
</td>
<td>
Offset of memory location.
</td>
</tr>
<tr>
<td>
1
</td>
<td>
<code>00</code>
</td>
<td>
End of header data
</td>
</tr>
<tr>
<td>
239
</td>
<td>
All <code>01</code>
</td>
<td>
Filler
</td>
</tr>
</table>
</div>
<h5 id="block">Data block format</h5>
<div class="scrollable">
<table>
<tr>
<th>
Bytes
</th>
<th>
Format
</th>
<th>
Meaning
</th>
</tr>
<tr>
<td>
1
</td>
<td>
8-bit unsigned integer
</td>
<td>
Number of payload bytes in last record, plus one.
If zero, the next record is not the last record.
</td>
</tr>
<tr>
<td>
255
</td>
<td>
</td>
<td>
Payload data. If this is the last record, any unused bytes are filled
by repeating the last payload byte.
</td>
</tr>
</table>
</div>
</p>
</section>
<hr />
<section>
<h3 id="emulator-file-formats">Emulator file formats</h3>
<p>
PC-BASIC uses a number of file formats to support its emulation of
legacy hardware, which are documented in this section.
These file formats are not used by GW-BASIC or
contemporary software.
</p>
<h4 id="hex-file-format">HEX font file format</h4>
<p>
The HEX file format for bitfonts was developed by Roman Czyborra for the
GNU Unifont package. PC-BASIC uses an extended version of this file format to store its fonts.
</p>
<p>
A HEX file is an ASCII text file, consisting of lines terminated by <code><i>LF</i></code>.
Each line of this file is one of the following:
</p>
<ul>
<li>
Empty
</li>
<li>
A comment, starting with a <code>#</code> character.
</li>
<li>
One or more 4 or 6-character hexadecimal Unicode code points, separated by commas,
followed by a colon,
followed by a hexadecimal number representing the glyph.
A 64-hexdigit or longer number represents a fullwidth (16xN) glyph, with
each row of 16 pixels represented by four hexadecimal digits.
A shorter number represents a halfwidth (8xN) glyph, with
each row of 8 pixels represented by two hexadecimal digits.
</li>
</ul>
<h4 id="ucp-file-format">UCP code page file format</h4>
<p>
Unicode-codepage mappings are stored in UCP files.
</p>
<p>
A UCP file is an ASCII text file, consisting of lines terminated by <code><i>LF</i></code>.
Each line of this file is one of the following:
</p>
<ul>
<li>
Empty
</li>
<li>
A comment, starting with a <code>#</code> character.
</li>
<li>
A 2- or 4-character hexadecimal codepage point, followed by a colon, followed
by a comma-separated list of 4- or 6-character hexadecimal Unicode code points.
If more than one Unicode code point is provided for a codepage point, the code points
combine into a single glyph.
</li>
</ul>
<h4 id="cas-file-format">CAS tape file format</h4>
<p>
A CAS file is a bit-level representation of cassette data introduced by the PCE emulator.
CAS-files produced by PC-BASIC start with the characters
<code>PC-BASIC tape<i>EOF</i></code>. This sequence is followed by seven 0 bits,
followed by the tape contents. The seven zero bits are intended to
ensure that the tape contents are byte-aligned; the one bit is made up
by the synchronisation bit following the pilot wave.
</p>
<p>
Note that PC-BASIC does not require the introductory sequence to read
a CAS-file correctly, nor does it require the contents of a CAS-file
to be byte-aligned. However, new files produced by PC-BASIC follow
this convention.
</p>
</section>
<hr />
<section>
<h3 id="character-codes">Character codes</h3>
<p>
Depending on context, PC-BASIC will treat a code point in
the control characters range as a control character or as a
glyph defined by the active <a href="#codepages">codepage</a> which by
default is <a href="#cp437">codepage 437</a>. Code points of
<code>&h80</code> or higher are always interpreted as a
codepage glyph.
</p>
<h4 id="ascii">ASCII</h4>
<p>
This is a list of the American Standard Code for Information Interchange (ASCII).
ASCII only covers 128 characters and defines the code point ranges
<code>&h00</code>–<code>&h1F</code> and <code>&h7F</code>
as <dfn>control characters</dfn> which do not have a printable glyph assigned
to them. This includes such values as the <i>Carriage Return</i> (<code><i>CR</i></code>)
character that ends a program line.
</p>
<p>
In the context of this documentation, character <code>&h1A</code> (<code><i>SUB</i></code>)
will usually be indicated as <code><i>EOF</i></code> since it plays the role of end-of-file marker in DOS.
</p>