-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogram.c
1844 lines (1657 loc) · 43.3 KB
/
program.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
int _ax ;
int* local_stack ;
int local_idx ;
int local_val ;
void push_local (){
// ARGUMENTS: <GLOBAL> local_val
// RETURNS: NONE
* local_stack = local_val ;
local_stack = local_stack + 1 ;
}
void pop_local (){
// ARGUMENTS: NONE
// RETURNS: <GLOBAL> local_val: the value of the local at the top of the stack
local_stack = local_stack + 65535 ;
local_val = * local_stack ;
}
int* _local_ptr ;
void read_local (){
// ARGUMENTS: <GLOBAL> local_idx
// RETURNS: <GLOBAL> local_val: the value of the local
// local_stack points to the NEXT pointer, so offset for that
_local_ptr = ( local_stack - local_idx ) - 1 ;
local_val = * _local_ptr ;
}
void write_local (){
// ARGUMENTS:
// - <GLOBAL> local_idx
// - <GLOBAL> local_val
// RETURNS: NONE
// local_stack points to the NEXT pointer, so offset for that
_local_ptr = ( local_stack - local_idx ) - 1 ;
* _local_ptr = local_val ;
}
int _p_i ;
int* _p ;
int gs ;
int* ptr ;
int ptr_val ;
void wide_ptr_read (){
// reads a word at the specified wide pointer
// used to access outside the 16 bit pointer range
// ARGUMENTS:
// <GLOBAL> gs - the value to use for gs in the read
// <GLOBAL> ptr - the pointer to use for the offset into the segment for the read
// RETURNS: <GLOBAL> ptr_val - the read value
_ax = gs ;
// mov gs, ax
asm(" .byte 142 ; .byte 232 ; ");
// xchg bx, ax
// mov ax, word gs:[bx]
// mov word [0x1000], ax
_ax = ptr ;
asm(" .byte 147 ; .byte 101 ; .byte 139 ; .byte 7 ; .byte 163 ; .byte 0 ; .byte 16 ; ");
ptr_val = _ax ;
}
void wide_ptr_write (){
// write a word at the specified wide pointer
// used to access outside the 16 bit pointer range
_ax = gs ;
// mov gs, ax
asm(" .byte 142 ; .byte 232 ; ");
_ax = ptr ;
// push ax
asm(" .byte 80 ; ");
_ax = ptr_val ;
// pop bx
// mov word gs:[bx], ax
asm(" .byte 91 ; .byte 101 ; .byte 137 ; .byte 7 ; ");
}
int* tmp ;
int* memset_ptr ;
int memset_val ;
int memset_count ;
int* _memset_ptr_end ;
void memset (){
// sets memset_count *WORDS* of data beginning at memset_ptr to the *WORD* specified in memset_val
_memset_ptr_end = memset_ptr + memset_count ;
while( memset_ptr < _memset_ptr_end ){
* memset_ptr = memset_val ;
memset_ptr = memset_ptr + 1 ;
}
}
int* memcpy_src ;
int* memcpy_dst ;
int memcpy_count ;
int* _memcpy_end ;
void memcpy (){
// copies memcpy_count WORDS of data from memcpy_src to memcpy_dst
// the regions of memory must not overlap
_memcpy_end = memcpy_src + memcpy_count ;
while( memcpy_src < _memcpy_end ){
* memcpy_dst = * memcpy_src ;
memcpy_src = memcpy_src + 1 ;
memcpy_dst = memcpy_dst + 1 ;
}
}
void memcpy_gs_dst (){
// copies memcpy_count WORDS of data from memcpy_src to gs:memcpy_dst
// the regions of memory must not overlap
_memcpy_end = memcpy_src + memcpy_count ;
while( memcpy_src < _memcpy_end ){
ptr = memcpy_dst ;
ptr_val = * memcpy_src ;
wide_ptr_write ();
memcpy_src = memcpy_src + 1 ;
memcpy_dst = memcpy_dst + 1 ;
}
}
void memcpy_gs_src (){
// copies memcpy_count WORDS of data from gs:memcpy_src to memcpy_dst
// the regions of memory must not overlap
_memcpy_end = memcpy_src + memcpy_count ;
while( memcpy_src < _memcpy_end ){
ptr = memcpy_src ;
wide_ptr_read ();
* memcpy_dst = ptr_val ;
memcpy_src = memcpy_src + 1 ;
memcpy_dst = memcpy_dst + 1 ;
}
}
int strcmp_lhs ;
int strcmp_rhs ;
int strcmp_diff ;
int* _strcmp_lhs_ptr ;
int* _strcmp_rhs_ptr ;
void strcmp (){
// compares two null terminated strings lexicographically
// ARGUMENTS:
// <global> strcmp_lhs
// <global> strcmp_rhs
// RETURNS: <global> strcmp_diff - an integer which has a sign corresponding to the result of the comparison
// - negative if lhs is before rhs
// - zero if lhs and rhs are equal
// - positive if lhs is after rhs
_strcmp_lhs_ptr = strcmp_lhs ;
_strcmp_rhs_ptr = strcmp_rhs ;
strcmp_diff = ( * _strcmp_lhs_ptr - * _strcmp_rhs_ptr ) & 255 ;
// if the strings are different, this loop will exit
// - if the strings differ in a normal character, the difference will be non-zero, and the loop will exit
// - if one of the strings is a prefix of another ("abc", "abcd"), then the null terminator of one will be different from a character in another
// if the strings are identical, we need to make sure that we exit when both bytes are null terminators, or else this would read out of bounds
while( strcmp_diff == 0 ){
// handling identical strings
// reusing this variable as temp storage
strcmp_diff = * _strcmp_lhs_ptr & 255 ;
if( strcmp_diff == 0 ){
strcmp_diff = * _strcmp_rhs_ptr & 255 ;
if( strcmp_diff == 0 ){
// both were null terminators, return (0 is already in the return global)
return;
}
}
// read the next byte
strcmp_lhs = strcmp_lhs + 1 ;
strcmp_rhs = strcmp_rhs + 1 ;
_strcmp_lhs_ptr = strcmp_lhs ;
_strcmp_rhs_ptr = strcmp_rhs ;
strcmp_diff = ( * _strcmp_lhs_ptr - * _strcmp_rhs_ptr ) & 255 ;
}
}
int port ;
int port_val ;
void inb (){
_ax = port ;
// xchg ax, dx; in al, dx; move byte [0x1000], al
asm(" .byte 146 ; .byte 236 ; .byte 162 ; .byte 0 ; .byte 16 ; ");
port_val = _ax & 255 ;
}
void outb (){
_ax = port ;
// xchg ax, dx
asm(" .byte 146 ; ");
_ax = port_val ;
// out dx, al
asm(" .byte 238 ; ");
}
int c ;
int state ;
void read_char (){
state = 0 ;
while( state == 0 ){
// 0x3FD - status
port = 1021 ;
inb ();
state = port_val & 1 ;
}
// 0x3FD - data
port = 1016 ;
inb ();
c = port_val ;
}
void print_char (){
state = 0 ;
while( state == 0 ){
// 0x3FD - status
port = 1021 ;
inb ();
state = port_val & 32 ;
}
// 0x3FD - data
port = 1016 ;
port_val = c ;
outb ();
}
int int_div_lhs ;
int int_div_rhs ;
int int_div_quot ;
int int_div_rem ;
void int_div (){
int_div_quot = 0 ;
while( int_div_rhs < int_div_lhs ){
int_div_lhs = int_div_lhs - int_div_rhs ;
int_div_quot = int_div_quot + 1 ;
}
int_div_rem = int_div_lhs ;
}
int print_hex_val ;
int _print_hex_nibble ;
int _print_hex_count ;
void print_hex (){
// prints a value to the serial output, formatted as hex without a leading 0x
// ARGUMENTS: <global> print_hex_val - the value to print
// RETURNS: NONE
_print_hex_count = 0 ;
while( _print_hex_count < 4 ){
// 0xF000
_print_hex_nibble = ( print_hex_val & 61440 ) >> 12 ;
// adjust letters forward into the letter region
if( 9 < _print_hex_nibble ){
_print_hex_nibble = _print_hex_nibble + 7 ;
}
// shift from value into digit range
c = _print_hex_nibble + 48 ;
print_char ();
print_hex_val = print_hex_val << 4 ;
_print_hex_count = _print_hex_count + 1 ;
}
}
void println_hex (){
print_hex ();
c = 10 ;
print_char ();
}
int print_dec_val ;
int _print_dec_c ;
int _seen_zero ;
void print_dec (){
if( print_dec_val < 0 ){
// -
c = 45 ;
print_char ();
print_dec_val = 0 - print_dec_val ;
}
_print_dec_c = 0 ;
while( print_dec_val != 0 ){
int_div_lhs = print_dec_val ;
int_div_rhs = 10 ;
int_div ();
local_val = int_div_rem ;
push_local ();
print_dec_val = int_div_quot ;
_print_dec_c = _print_dec_c + 1 ;
}
while( 0 < _print_dec_c ){
pop_local ();
c = local_val + 48 ;
print_char ();
_print_dec_c = _print_dec_c - 1 ;
}
}
void print_esc (){
// ESC
c = 27 ;
print_char ();
// [
c = 91 ;
print_char ();
}
int cursor_x ;
int cursor_y ;
void set_cursor_pos (){
print_esc ();
print_dec_val = cursor_y ;
print_dec ();
// ;
c = 59 ;
print_char ();
print_dec_val = cursor_x ;
print_dec ();
// H
c = 72 ;
print_char ();
}
void clear (){
print_esc ();
// 2
c = 50 ;
print_char ();
// J
c = 74 ;
print_char ();
cursor_x = 1 ;
cursor_y = 1 ;
set_cursor_pos ();
}
int* print_str_ptr ;
int _print_str_b ;
void print_string (){
// prints a NULL terminated string to the serial output
// ARGUMENTS: <global> print_string_src - pointer to the start of the string to print
// RETURNS: NONE
_print_str_b = * print_str_ptr & 255 ;
while( _print_str_b != 0 ){
c = _print_str_b ;
print_char ();
// hack to get a 1 byte offset
_print_str_b = print_str_ptr ;
print_str_ptr = _print_str_b + 1 ;
_print_str_b = * print_str_ptr & 255 ;
}
}
int print_str_len ;
void print_string_len (){
while( 0 < print_str_len ){
_print_str_b = * print_str_ptr & 255 ;
c = _print_str_b ;
print_char ();
// hack to get a 1 byte offset
_print_str_b = print_str_ptr ;
print_str_ptr = _print_str_b + 1 ;
print_str_len = print_str_len - 1 ;
}
}
int max_head_num ;
int max_sector_num ;
int max_cylinder_num ;
void get_drive_stats (){
asm(" .byte 180 ; .byte 8 ; .byte 178 ; .byte 128 ; .byte 205 ; .byte 19 ; .byte 81 ; .byte 82 ; ");
// pop ax; move word [0x1000], ax
asm(" .byte 88 ; .byte 163 ; .byte 0 ; .byte 16 ; ");
max_head_num = _ax >> 8 ;
// pop ax; move word [0x1000], ax
asm(" .byte 88 ; .byte 163 ; .byte 0 ; .byte 16 ; ");
max_cylinder_num = ( ( _ax & 192 ) << 8 ) | ( ( _ax & 65280 ) >> 8 ) ;
max_sector_num = _ax & 63 ;
}
int mul_lhs ;
int mul_rhs ;
int mul_result ;
void int_mul (){
// multiplies two 16 bit numbers, with wrapping
mul_result = 0 ;
while( 0 < mul_rhs ){
mul_result = mul_result + mul_lhs ;
mul_rhs = mul_rhs - 1 ;
}
}
int lba_to_chs_lba ;
int lba_to_chs_c ;
int lba_to_chs_h ;
int lba_to_chs_s ;
int _lba_to_chs_tmp ;
void lba_to_chs (){
get_drive_stats ();
int_div_lhs = lba_to_chs_lba ;
int_div_rhs = max_sector_num ;
int_div ();
_lba_to_chs_tmp = int_div_quot ;
lba_to_chs_s = int_div_rem + 1 ;
int_div_lhs = _lba_to_chs_tmp ;
int_div_rhs = max_head_num + 1 ;
int_div ();
lba_to_chs_h = int_div_rem ;
lba_to_chs_c = int_div_quot ;
}
int io_lba ;
int* io_buf ;
int _io_ax ;
int _io_cx ;
int _io_dx ;
void disk_io (){
// reads or writes a sector from disk
// ARGUMENTS:
// <global> io_lba - the linear address of the sector to load
// <global> _io_ax - either 513 to read or 769 to write
// RETURNS: <global> io_buf - points to the loaded data
lba_to_chs_lba = io_lba ;
lba_to_chs ();
// 192 - 0xC0
_io_cx = ( ( lba_to_chs_c & 255 ) << 8 ) | ( lba_to_chs_s | ( ( lba_to_chs_c >> 2 ) & 192 ) ) ;
// set dx to XX80
_io_dx = ( lba_to_chs_h << 8 ) | 128 ;
// 0x6000
io_buf = 24576 ;
_ax = io_buf ;
asm(" .byte 80 ; ");
_ax = _io_dx ;
asm(" .byte 80 ; ");
_ax = _io_cx ;
asm(" .byte 80 ; ");
_ax = _io_ax ;
asm(" .byte 80 ; ");
asm(" .byte 88 ; .byte 89 ; .byte 90 ; .byte 91 ; .byte 205 ; .byte 19 ; ");
}
void read_sector (){
_io_ax = 513 ;
disk_io ();
}
void write_sector (){
_io_ax = 769 ;
disk_io ();
}
int* FAT ;
int* fat16_root_data ;
int start_sector ;
int first_data_offset ;
int root_dir_offset ;
int root_dir_entries ;
int fat1_offset ;
int fat2_offset ;
int* dirent_file_name ;
int* _find_file_name_ptr ;
int* _find_file_search_ptr ;
int find_file_idx ;
void find_file (){
// ARGUMENTS: pointer to 12 bytes that contain the null terminated name of the file
// RETURNS: pointer to the start of the fs metadata block that matched, or 0
pop_local ();
_find_file_name_ptr = local_val ;
_find_file_search_ptr = fat16_root_data ;
find_file_idx = 0 ;
while( find_file_idx < root_dir_entries ){
// search for a fs entry with the same name
strcmp_lhs = _find_file_name_ptr ;
strcmp_rhs = _find_file_search_ptr ;
strcmp ();
if( strcmp_diff == 0 ){
local_val = _find_file_search_ptr ;
push_local ();
return;
}
// increment to the next fs entry (16 bytes each)
_find_file_search_ptr = _find_file_search_ptr + 8 ;
find_file_idx = find_file_idx + 1 ;
}
// if we got here, no file found
local_val = 0 ;
push_local ();
}
int* open_file_metadata ;
int open_file_sector ;
int open_file_fat_cluster ;
int open_file_length ;
void open_file (){
// opens a a file and loads its first sector into memory
// ARGUMENTS: <global> open_file_metadata - pointer to the in memory fs metadata describing the file to load
// RETURNS: NONE
// the file starts opened at the first logical sector of the file
// this is the first cluster in the chain
open_file_sector = 0 ;
// read cluster number and load that first cluster into memory
open_file_metadata = open_file_metadata + 6 ;
open_file_fat_cluster = * open_file_metadata ;
io_lba = ( first_data_offset + open_file_fat_cluster ) - 2 ;
read_sector ();
open_file_metadata = open_file_metadata + 1 ;
open_file_length = * open_file_metadata ;
// move back to start of metadata
open_file_metadata = open_file_metadata - 7 ;
}
int next_fat_idx ;
int* next_fat_val ;
void next_fat_cluster (){
// finds the next non-occupied cluster in the FAT
// ARGUMENTS: IMPLICIT DISK STATE
// RETURNS: <global> next_fat_idx - the index of the next free cluster, or -1 if it does not exist
// entries 0 and 1 are reserved
next_fat_idx = 2 ;
// FIXME: this is wrong
// we only have 256 clusters
while( next_fat_idx < 256 ){
next_fat_val = FAT + next_fat_idx ;
next_fat_val = * next_fat_val ;
if( next_fat_val == 0 ){
return;
}
next_fat_idx = next_fat_idx + 1 ;
}
// no free cluster found
next_fat_idx = 65535 ;
}
int cluster ;
int next_cluster ;
void next_cluster_get (){
// gets the next cluster in a cluster chain
// ARGUMENTS: IMPLICIT DISK STATE
// <global> cluster - the cluster to find the next cluster for
// RETURNS: <global> next_cluster - the next cluster index
next_fat_val = FAT + cluster ;
next_cluster = * next_fat_val ;
}
void next_cluster_set (){
// sets the next cluster in a cluster chain
// ARGUMENTS: IMPLICIT DISK STATE
// <global> cluster - the cluster to set the next cluster for
// <global> next_cluster - the cluster to set
// RETURNS: NONE
next_fat_val = FAT + cluster ;
* next_fat_val = next_cluster ;
}
int seek_sector ;
int _seek_s ;
int _seek_c ;
void seek_open_file (){
// seeks to the absolute sector seek_sector in the open file
// updates open_file_sector and open_file_fat_cluster
_p = open_file_metadata + 6 ;
_seek_c = * _p ;
_seek_s = 0 ;
while( _seek_s < seek_sector ){
cluster = _seek_c ;
next_cluster_get ();
if( next_cluster == 65535 ){
// TODO: return an error probably?
return;
}
_seek_c = next_cluster ;
_seek_s = _seek_s + 1 ;
}
open_file_sector = _seek_s ;
open_file_fat_cluster = _seek_c ;
io_lba = ( first_data_offset + _seek_c ) - 2 ;
print_hex_val = io_lba ;
println_hex ();
read_sector ();
}
void allocate_new_cluster (){
// allocates a new cluster for a file
// ARGUMENTS: IMPLICIT OPEN FILE STATE
// <global> cluster - the cluster to insert the new cluster after
// RETURNS: <global> next_fat_idx - the newly allocated cluster
// <global> next_cluster - always 0xFFFF
next_fat_cluster ();
next_cluster = next_fat_idx ;
next_cluster_set ();
io_lba = ( first_data_offset + next_fat_idx ) - 2 ;
// zero the newly allocated cluster
memset_ptr = io_buf ;
memset_val = 0 ;
memset_count = 256 ;
// set the cluster after the next cluster to be EoF
cluster = next_fat_idx ;
// 0xFFFF
next_cluster = 65535 ;
next_cluster_set ();
}
int* _update_dir_ptr ;
int _update_dir_idx ;
void update_directory (){
// updates the on-disk directory from the in-memory directory data
// 0x6200
tmp = 25088 ;
memset_ptr = tmp ;
// 0x800 words
memset_count = 2048 ;
memset_val = 0 ;
memset ();
_update_dir_ptr = fat16_root_data ;
_update_dir_idx = 0 ;
while( _update_dir_idx < root_dir_entries ){
// copy filename
memcpy_src = _update_dir_ptr ;
memcpy_dst = tmp ;
memcpy_count = 6 ;
memcpy ();
// start cluster at offset 12
_update_dir_ptr = _update_dir_ptr + 6 ;
// start cluster at offset 0x1A
tmp = tmp + 13 ;
* tmp = * _update_dir_ptr ;
_update_dir_ptr = _update_dir_ptr + 1 ;
tmp = tmp + 1 ;
* tmp = * _update_dir_ptr ;
_update_dir_idx = _update_dir_idx + 1 ;
// already at offset 14 of 16
_update_dir_ptr = _update_dir_ptr + 1 ;
// already at offset 0x1C of 0x20
tmp = tmp + 2 ;
}
// 0x6200
tmp = 25088 ;
// directory table is 0x1000 bytes - 8 sectors
_update_dir_ptr = tmp + 2048 ;
io_lba = root_dir_offset ;
while( tmp < _update_dir_ptr ){
memcpy_src = tmp ;
memcpy_dst = io_buf ;
memcpy_count = 256 ;
memcpy ();
write_sector ();
tmp = tmp + 256 ;
io_lba = io_lba + 1 ;
}
}
void update_fat (){
// write the updated FAT back out to disk
memcpy_src = FAT ;
memcpy_dst = io_buf ;
memcpy_count = 256 ;
memcpy ();
io_lba = fat1_offset ;
write_sector ();
io_lba = fat2_offset ;
write_sector ();
}
void free_fat_chain (){
// frees all of the clusters in a FAT chain to the end of the file
// ARGUMENTS: <global> cluster - the cluster to start freeing from
// RETURNS: NONE
local_val = cluster ;
push_local ();
next_cluster_get ();
if( 1 < next_cluster ){
// for valid next clusters, try to free them recursively
cluster = next_cluster ;
free_fat_chain ();
}
pop_local ();
cluster = local_val ;
next_cluster = 0 ;
next_cluster_set ();
}
int file_length ;
int* _set_file_ptr ;
int _set_file_count ;
int _last_cluster ;
void file_length_set (){
// sets the length of a file to a specified value and commits it to disk
// allocates or frees FAT clusters as needed to set the size of the file
// ARGUMENTS: IMPLICIT OPEN FILE STATE
// <global> file_length - the length to set
// RETURNS: NONE
// follow the FAT chain to set it up properly
_set_file_count = 0 ;
_set_file_ptr = open_file_metadata + 6 ;
cluster = * _set_file_ptr ;
while( _set_file_count < file_length ){
_last_cluster = cluster ;
next_cluster_get ();
if( next_cluster < 0 ){
// not enough clusters are allocated, allocate as many as needed
while( _set_file_count < file_length ){
allocate_new_cluster ();
cluster = next_fat_idx ;
_set_file_count = _set_file_count + 512 ;
}
// when this drops through, allocate_new_cluster has set next_cluster to 0xFFFF
}
if( 1 < next_cluster ){
cluster = next_cluster ;
_set_file_count = _set_file_count + 512 ;
}
}
if( 1 < next_cluster ){
// if there's still clusters remaining in the chain, free them
cluster = next_cluster ;
free_fat_chain ();
// set the last used cluster to point to EoF
cluster = _last_cluster ;
next_cluster = 65535 ;
next_cluster_set ();
}
// write to the in-memory fs structure
open_file_metadata = open_file_metadata + 7 ;
* open_file_metadata = file_length ;
open_file_length = file_length ;
open_file_metadata = open_file_metadata - 7 ;
update_directory ();
update_fat ();
}
int* create_file_name ;
int* _create_file_ptr ;
void create_file (){
// creates a file with the specified name and zero length, and then opens it
// ARGUMENTS: FS STATE
// <global> create_file_name - the 8.3 null terminated file name to create
// find an open entry in the directory by looking for a file that has cluster 0
_create_file_ptr = fat16_root_data + 6 ;
while( * _create_file_ptr != 0 ){
_create_file_ptr = _create_file_ptr + 8 ;
}
// copy the name
memcpy_src = create_file_name ;
memcpy_dst = _create_file_ptr - 6 ;
memcpy_count = 6 ;
memcpy ();
next_fat_cluster ();
* _create_file_ptr = next_fat_idx ;
cluster = next_fat_idx ;
next_cluster = 65535 ;
next_cluster_set ();
// zero length
_create_file_ptr = _create_file_ptr + 1 ;
* _create_file_ptr = 0 ;
update_directory ();
update_fat ();
open_file_metadata = _create_file_ptr - 7 ;
open_file ();
}
int* write_file_buf ;
int write_file_count ;
int write_file_offset ;
int _write_file_start_sector ;
int _write_file_cluster ;
int _w_len ;
void write_file (){
// writes the contents of a buffer into the open file at a specified offset in the file
// expands the file if needed
// ARGUMENTS:
// IMPLICIT OPEN FILE STATE
// <global> write_file_buf - the buffer to write into the file
// <global> write_file_count - the number of bytes to write to the file
// <global> write_file_offset - the offset into the file to begin writing at
// save the logical sector in the file to restore after
_write_file_start_sector = open_file_sector ;
if( open_file_length < write_file_offset ){
write_file_offset = open_file_length ;
}
// calculate the end size of the file after the write is done
// reusing this variable
_write_file_cluster = write_file_offset + write_file_count ;
file_length = open_file_length ;
if( file_length < _write_file_cluster ){
file_length = _write_file_cluster ;
}
// allocates FAT clusters as needed
file_length_set ();
// bytes needed to align to a sector
_w_len = 512 - ( write_file_offset & 255 ) ;
if( write_file_count < _w_len ){
_w_len = write_file_count ;
}
// FIXME: this is wrong i think
_write_file_cluster = open_file_fat_cluster ;
// don't do this alignment write if it's not needed
if( _w_len != 512 ){
io_lba = ( first_data_offset + _write_file_cluster ) - 2 ;
read_sector ();
memcpy_src = write_file_buf ;
_p_i = io_buf ;
memcpy_dst = _p_i + ( write_file_offset & 255 ) ;
// ( len + 2 ) & 0xFFFE : aligns up to 2
memcpy_count = ( ( _w_len + 2 ) & 65534 ) >> 1 ;
memcpy ();
write_file_buf = _p_i + _w_len ;
write_file_count = write_file_count - _w_len ;
write_sector ();
cluster = _write_file_cluster ;
next_cluster_get ();
_write_file_cluster = next_cluster ;
}
while( 0 < write_file_count ){
io_lba = ( first_data_offset + _write_file_cluster ) - 2 ;
read_sector ();
memset_ptr = io_buf ;
memset_val = 0 ;
memset_count = 256 ;
memset ();
memcpy_src = write_file_buf ;
memcpy_dst = io_buf ;
// align up to 2
memcpy_count = ( ( write_file_count + 2 ) & 65534 ) >> 1 ;
// write at most one sector at a time
if( 256 < memcpy_count ){
memcpy_count = 256 ;
}
// FIXME: this is maybe right?
write_file_buf = write_file_buf + memcpy_count ;
write_file_count = write_file_count - ( memcpy_count << 1 ) ;
memcpy ();
write_sector ();
cluster = _write_file_cluster ;
next_cluster_get ();
_write_file_cluster = next_cluster ;
}
}
int buf ;
int read_byte_val ;
int* _read_byte_ptr ;
void read_byte_buf (){
_read_byte_ptr = buf ;
read_byte_val = * _read_byte_ptr ;
read_byte_val = read_byte_val & 255 ;
buf = buf + 1 ;
}
int* _read_dir_entry_ptr ;
int _read_dir_entry_count ;
int _read_dir_start_sector ;
void read_dir_entry (){
// reads a directory entry into the in-memory fs structure, assuming fat16_root_data points to an empty entry
// also sets the contents of dirent_file_name to be the name of the file in 8.3 format
// ARGUMENTS:
// - pointer to the start of the directory entry
// RETURNS:
// - 0 if file exists, 1 if entry was vacant, and 2 if the entry is the end of the directory
pop_local ();
_read_dir_entry_ptr = local_val ;
buf = _read_dir_entry_ptr ;
// the first byte of the entry is the file name, but has some special values
read_byte_buf ();
if( read_byte_val == 0 ){
// "end of directory" status
local_val = 2 ;
push_local ();
return;
}
_read_dir_entry_count = 0 ;
// copy the first 10 bytes of the filename to the name buffer
while( _read_dir_entry_count < 5 ){
* dirent_file_name = * _read_dir_entry_ptr ;
dirent_file_name = dirent_file_name + 1 ;
_read_dir_entry_ptr = _read_dir_entry_ptr + 1 ;
_read_dir_entry_count = _read_dir_entry_count + 1 ;
}
// then add the last byte and its null terminator
* dirent_file_name = * _read_dir_entry_ptr & 255 ;
// move back to the start of the name
dirent_file_name = dirent_file_name - 5 ;
// copy the filename to the fat16 tables
memcpy_src = dirent_file_name ;
memcpy_dst = fat16_root_data ;
memcpy_count = 6 ;
memcpy ();
fat16_root_data = fat16_root_data + 6 ;
// pointer is at the last byte of the filename: offset 0x0A
// get the start sector of the file (offset 0x1A)
_read_dir_entry_ptr = _read_dir_entry_ptr + 8 ;
* fat16_root_data = * _read_dir_entry_ptr ;
fat16_root_data = fat16_root_data + 1 ;
// file size
_read_dir_entry_ptr = _read_dir_entry_ptr + 1 ;
* fat16_root_data = * _read_dir_entry_ptr ;
fat16_root_data = fat16_root_data + 1 ;
// found file, return status 0
local_val = 0 ;
push_local ();
}
int _read_root_count ;
int _read_root_cluster ;
int _read_root_dirent_status ;
void init_fs (){
// reads the FAT16 data on disk into an in-memory structure
// this both loads the FAT itself and the contents of the root directory
start_sector = 1 ;
// these are all ABSOLUTE offsets from the start of the disk, not
// relative to the start of the partition
fat1_offset = 2 ;
fat2_offset = 66 ;
root_dir_offset = 130 ;
root_dir_entries = 128 ;
first_data_offset = 138 ;
// 0x8800
FAT = 34816 ;
io_lba = fat1_offset ;
read_sector ();
memcpy_src = io_buf ;
memcpy_dst = FAT ;
// 256 words = 1 sector
memcpy_count = 256 ;
memcpy ();
io_lba = root_dir_offset ;
read_sector ();
// 0x8000
fat16_root_data = 32768 ;
memset_ptr = fat16_root_data ;
memset_val = 0 ;
// 0x2000 * 2 bytes = 0x4000 bytes
memset_count = 8192 ;
memset ();
_read_root_count = 0 ;
while( _read_root_count < root_dir_entries ){
local_val = io_buf ;
push_local ();
read_dir_entry ();
pop_local ();
_read_root_dirent_status = local_val ;
// status 2 is end of directory
if( _read_root_dirent_status == 2 ){
// 0x6000
io_buf = 24576 ;
// 0x8000
fat16_root_data = 32768 ;
return;
}
// FIXME: implement status 1: vacant entry by not printing