-
Notifications
You must be signed in to change notification settings - Fork 0
/
fifDirSrv.py
executable file
·1103 lines (853 loc) · 33.7 KB
/
fifDirSrv.py
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
#!/usr/bin/env python3
##
# fifsrv.py
#
# Copyright (C) David McNaughton 2023-present
#
# a utility to remotely mount disk image files (*.dsk) to the IMSAI8080esp
# using the RESTful interface provided for IO and DMA
#
# dependencies:
# python3
# requests (module) - to install, use: pip install requests
# simple_http_server (module) - to install, use: pip install simple_http_server
#
# TODO:
# - normalize the use of trk:sec vs. linear sector
# - add more error detection and return more error codes
#
# known issues:
# - TBD
#
# history:
# 13-JUL-2023 1.0 Initial release
##
import requests
import curses
import curses.textpad
import sys
import os
from stat import *
import socket
from simple_http_server import route, server, Response, BytesBody, ModelDict, logger as httpdlog
from threading import Thread
from logging import debug, info, error, warning
import logging
import json
httpdlog.set_level("ERROR")
srv = os.path.splitext(os.path.basename(sys.argv[0]))[0]
FIF_PORT = 0xFD
SRV_PORT = 3000 + FIF_PORT
SRV_PATH = srv
hosturl = 'http://imsai8080'
_srvurl = f'http://{socket.gethostname()}:{SRV_PORT}/{SRV_PATH}'
diskmap_file = 'diskmap.json'
disks = { }
disk_to_unit = { 'A': 1, 'B': 2, 'C': 4, 'D': 8, 'I': 15 }
sess = requests.Session()
TMAX = 77
win = None
def main(sc):
curses.init_pair(1, curses.COLOR_RED, curses.COLOR_BLACK)
curses.init_pair(2, curses.COLOR_GREEN, curses.COLOR_BLACK)
curses.init_pair(3, curses.COLOR_YELLOW, curses.COLOR_BLACK)
curses.init_pair(4, curses.COLOR_CYAN, curses.COLOR_BLACK)
global RED
global GREEN
global YELLOW
global CYAN
RED = curses.color_pair(1)
GREEN = curses.color_pair(2)
YELLOW = curses.color_pair(3)
CYAN = curses.color_pair(4)
global win
curses.curs_set(0)
curses.textpad.rectangle(sc, 0, 0, curses.LINES - 1, TMAX + 1)
title = f"[ Remote IMSAI FIF - {srv} ]"
sc.addstr(0, (TMAX + 2 - len(title))//2, title)
sc.refresh()
win = curses.newwin(curses.LINES - 2 , TMAX , 1, 1)
load_diskmap()
process_diskmap()
connect_to_host()
## DONT RUN THIS IN A VM OR THE HOST CAN'T BE SEEN
th = Thread(target=server.start, args=("", SRV_PORT), daemon=True)
th.start()
drive = None
while True:
key = win.getkey()
win.addstr(curses.LINES - 3, 1, f"KEY: <{key}>", CYAN)
win.clrtoeol()
win.refresh()
# info(f"KEY: {key} len={len(key)} ord={ord(key)}")
if key == chr(24): # ^X
connect_to_host()
elif key == chr(16): # ^P
info(f"PERSIST: {disks}")
with open(diskmap_file, "w") as fp:
json.dump(disks, fp) # encode disks dict into JSON
win.addstr(curses.LINES - 3, 12, f"SAVED TO {diskmap_file}")
elif key == chr(18): # ^R
info(f"RELOAD: {disks}")
load_diskmap()
process_diskmap()
elif key == chr(21): # ^U
if drive in list(disks):
info(f"UNLOAD: DSK:{drive}: {disks[drive]}")
disks.pop(drive)
process_diskmap()
elif key == chr(12): # ^L
if drive in list(disk_to_unit):
info(f"LOAD: DSK:{drive}:")
# PROMPT USER FOR image/directory NAME USING TEXTBOX
win.addstr(curses.LINES - 3, 0, f"LOAD: DSK:{drive}: = ")
win.clrtoeol()
win.refresh()
curses.curs_set(1)
txtwin = curses.newwin(1 , TMAX - 32 , curses.LINES - 2, 16)
tb = curses.textpad.Textbox(txtwin, insert_mode=True)
txt = tb.edit()
curses.curs_set(0)
txt = txt.strip()
disks[drive] = txt
process_diskmap()
elif key in list(disk_to_unit):
drive = key
win.addstr(curses.LINES - 3, 10, f"DRIVE: DSK:{drive}:")
continue
drive = None
@route(f'/{SRV_PATH}', method="PUT")
def io_out_put(p, m=ModelDict()):
port = int(p, 16)
#BODY is a little hard to get as it ends up as the first KEY in the DICT m
data = int(next(iter(m)), 16)
# info(f'{port:02X} {data:02X}')
if port == FIF_PORT:
t = fif_out(data)
if t == 1:
return Response(status_code=201)
return #normal 200 response
@route(f'/{SRV_PATH}', method="POST")
def io_out_post(p, b=BytesBody()):
port = int(p, 16)
data = bytearray(b)
info(f'{port:02X} {data}')
if port == FIF_PORT:
t = fif_with_dma(data)
if t == 1:
return Response(status_code=201)
return #normal 200 response
def connect_to_host():
sys_get = requests.delete(f'{hosturl}/io?p={FIF_PORT:02X}')
if sys_get.status_code == 200:
info(f'De-registered on {sys_get.text}')
win.addnstr(0, 0, f'De-registered on {sys_get.text}', TMAX)
try:
sys_get = requests.patch(f'{hosturl}/io?p=-{FIF_PORT:02X}&b=0x0F', data=_srvurl)
# sys_get = requests.patch(f'{hosturl}/io?p=-{FIF_PORT:02X}', data=_srvurl)
if sys_get.status_code == 200:
info(f'Listening and registered on Port {FIF_PORT:02X}h to {sys_get.text}')
win.addnstr(0, 0, f'Listening and registered on Port {FIF_PORT:02X}h to {sys_get.text}', TMAX, GREEN)
win.addnstr(1, 0, f'***You must COLD BOOT the IMSAI to recognize the remote FIF***', TMAX, YELLOW)
win.refresh()
except:
sys.exit(f"FAILED to find {hosturl} - not connected")
fdstate = 0
descno = 0
fdaddr = [0] * 16
def fif_with_dma(mem):
global descno
global fdstate
global fdaddr
res = 0
win.addstr(2, 0, 'RECV', curses.A_REVERSE + GREEN)
win.refresh()
descno = mem[9]
fdaddr[descno] = (mem[8] << 8) + mem[7]
win.addstr(1, 0, f"FIF DESC:{descno:X}")
win.clrtoeol()
win.move(2,4)
win.clrtoeol()
for i in range(16):
win.addstr((i//8) + 1, (i%8) * 7 + 12, f"{i:X}:{fdaddr[i]:04X}", curses.A_BOLD if i == descno else curses.A_NORMAL)
res = disk_io_action(mem, fdaddr[descno])
win.addstr(2, 0, ' ')
win.refresh()
return res
def fif_out(data):
global descno
global fdstate
global fdaddr
res = 0
win.addstr(2, 0, 'RECV', curses.A_REVERSE + RED)
win.refresh()
if fdstate == 0:
op = data & 0xF0
if op == 0x00:
descno = data & 0x0F
res = disk_io(fdaddr[descno])
elif op == 0x10:
descno = data & 0x0F
fdstate += 1
win.addstr(1, 0, f"FIF DESC:{descno:X}")
win.clrtoeol()
win.move(2,4)
win.clrtoeol()
for i in range(16):
win.addstr((i//8) + 1, (i%8) * 7 + 12, f"{i:X}:{fdaddr[i]:04X}", curses.A_BOLD if i == descno else curses.A_NORMAL)
elif fdstate == 1:
fdaddr[descno] = data
fdstate += 1
elif fdstate == 2:
fdaddr[descno] += data << 8
# info(f'Descriptor={descno} addr={fdaddr[descno]:04X}')
fdstate = 0
else:
error(f'Internal error fdstate={fdstate}')
fdstate = 0
win.addstr(2, 0, ' ')
win.refresh()
return res
cmd_str = [ "", "WRITE", "READ", "FORMAT", "VERIFY" ]
DEL_BYTE = 0xE5
EOF_BYTE = 0x1A
EXT_SZ = 32
SEC_SZ = 128
trans8 = [ 1,7,13,19,25,5,11,17,23,3,9,15,21,2,8,14,20,26,6,12,18,24,4,10,16,22 ]
dpb8 = {
'sectors': 26,
'blksize': 1024,
'dirsize': 64,
'disksize': 243,
'offset': 2,
'tracks': 77
}
dpbHD = {
'sectors': 128,
'blksize': 2048,
'dirsize': 1024,
'disksize': 2040,
'offset': 0,
'tracks': 255
}
dph = {
1: { 'dpb': dpb8, 'trans': trans8 },
2: { 'dpb': dpb8, 'trans': trans8 },
4: { 'dpb': dpb8, 'trans': trans8 },
8: { 'dpb': dpb8, 'trans': trans8 },
15: { 'dpb': dpbHD }
}
unit_info = { }
def load_diskmap():
global disks
try:
with open(diskmap_file, "r") as fp:
disks = json.load(fp) # Load the disks dict from the file
info(f"LOADED: {disks}")
win.addstr(2, 0, f"Loaded: {diskmap_file}")
except json.decoder.JSONDecodeError as e:
error(f'JSON error in {diskmap_file}: {e}')
sys.exit(f'JSON error in {diskmap_file}: {e}')
except FileNotFoundError:
warning(f'Diskmap file {diskmap_file} not found')
win.addstr(2, 0, f"*** Warning: no {diskmap_file} file found ***")
def process_diskmap():
win.move(5,0)
win.clrtobot()
info('DISKS:')
for i, d in enumerate(disk_to_unit):
unit_info[disk_to_unit[d]] = {}
if d in list(disks):
dstat = os.stat(disks[d])
if S_ISREG(dstat.st_mode):
info(f'DSK:{d}: = IMAGE: {disks[d]}')
unit_info[disk_to_unit[d]]['type'] = 'IMG'
unit_info[disk_to_unit[d]]['file'] = disks[d]
unit_info[disk_to_unit[d]]['dpb'] = dph[disk_to_unit[d]]['dpb']
if 'trans' in dph[disk_to_unit[d]]:
unit_info[disk_to_unit[d]]['trans'] = dph[disk_to_unit[d]]['trans']
unit_info[disk_to_unit[d]]['last'] = 0
elif S_ISDIR(dstat.st_mode):
info(f'DSK:{d}: = PATH : {disks[d]}')
unit_info[disk_to_unit[d]]['type'] = 'DIR'
unit_info[disk_to_unit[d]]['file'] = disks[d]
unit_info[disk_to_unit[d]]['dpb'] = dph[disk_to_unit[d]]['dpb']
if 'trans' in dph[disk_to_unit[d]]:
unit_info[disk_to_unit[d]]['trans'] = dph[disk_to_unit[d]]['trans']
unit_info[disk_to_unit[d]]['last'] = 0
(boot, data) = build_directory(disks[d], unit_info[disk_to_unit[d]]['dpb'])
dir = parseDir(data, 1 if dph[disk_to_unit[d]]['dpb']['disksize'] > 255 else 0)
unit_info[disk_to_unit[d]]['boot'] = boot
unit_info[disk_to_unit[d]]['dirdata'] = data
unit_info[disk_to_unit[d]]['dir'] = dir
unit_info[disk_to_unit[d]]['buffer'] = [ ]
printDir(dir, dph[disk_to_unit[d]]['dpb']['blksize'])
else:
sys.exit(f"FAILED drive {d}: file {disks[d]} - not recognized")
else:
unit_info[disk_to_unit[d]]['type'] = 'LOCAL'
unit_info[disk_to_unit[d]]['file'] = ''
unit_info[disk_to_unit[d]]['dpb'] = dph[disk_to_unit[d]]['dpb']
unit_info[disk_to_unit[d]]['last'] = 0
tscale = 0
while (unit_info[disk_to_unit[d]]['dpb']['tracks'] >> tscale) > TMAX:
tscale += 1
unit_info[disk_to_unit[d]]['scale'] = tscale
win.addstr(4*i + 3, 0, f"DSK:{d}: = {unit_info[disk_to_unit[d]]['type']}:{unit_info[disk_to_unit[d]]['file']}")
win.addstr(4*i + 3, 35, f"Login/Warm boot to reload disk", curses.A_DIM + YELLOW)
win.hline(4*i + 4, 0, '.', unit_info[disk_to_unit[d]]['dpb']['tracks'] >> tscale)
if tscale:
win.addstr(4*i + 4, (unit_info[disk_to_unit[d]]['dpb']['tracks'] >> tscale), f' [x{1 << tscale}]')
win.refresh()
# debug(unit_info)
def shorten(name, list):
tx = str.maketrans("<>.,;:=?*[]%|()/\\_", " ")
f, e = os.path.splitext(name)
file = f.upper().translate(tx).replace(' ', '')
ext = '.' + e[1:].upper().translate(tx).replace(' ', '')
tail = 0
if len(file) > 8:
tail += 1
file = f"{file[0:6]}~{tail:X}"
ext = ext[0:4]
shortname = file + ext
while tail and (shortname in list) and tail < 15:
tail += 1
shortname = f"{file[0:7]}{tail:X}{ext}"
if tail == 16:
error(f'TOO MANY FILES WITH THE SAME SHORT NAME: {shortname}')
if shortname != name:
list[list.index(name)] = shortname
return shortname
def build_directory(root, dpb):
boot = False
dirdata = [ DEL_BYTE ] * (EXT_SZ * dpb['dirsize'])
d = os.scandir(root)
dirI = 0
blkN = (EXT_SZ * dpb['dirsize']) // dpb['blksize']
for i in d:
s = i.stat().st_size
if S_ISREG(i.stat().st_mode):
if i.name == "$BOOT":
outcome = '$$$BOOT RECORD$$$'
boot = True
else:
outcome = '<IGNORED>'
info(f"{i.name:12} {s//1024:5}K {s//128:5} {outcome}")
if S_ISDIR(i.stat().st_mode):
if i.name.isnumeric() and int(i.name) in range(16):
outcome = f'USER: {i.name}'
else:
outcome = '<IGNORED>'
info(f"{i.name:12} <DIR> {outcome}")
continue
info(f"{i.name:12} <DIR> {outcome}")
files = list(os.scandir(os.path.join(root, i.name)))
names = [ f.name for f in files ]
for f in files:
size = f.stat().st_size
mode = f.stat().st_mode
name = shorten(f.name, names)
if name != f.name:
try:
os.rename(os.path.join(root, i.name, f.name), os.path.join(root, i.name, name))
warning(f'RENAMED FILE: {f.name} to {name}')
except:
error(f"FAILED TO RENAME {f.name} to {name}")
if S_ISREG(mode):
f, e = os.path.splitext(name)
cpmfile = f'{f:8}'
cpmext = f'{e[1:]:3}'
# info(f"{cpmfile}.{cpmext} {size:6} {size//1024:5} {size//128:5}")
ext = [0] * 32
ext[0] = int(i.name)
ext[1:9] = [ord(c) for c in cpmfile]
ext[9:12] = [ord(c) for c in cpmext]
# XL - extent number bits 0-4
# XH - extent number bits 5-10
xNum = 0
# BC - always ZERO for CPM22 - ignore
# RC - number of recs/secs in the extent
# round up if not a full sector (even multiple)
rc = size // SEC_SZ + ( 1 if (size % SEC_SZ) else 0)
while rc >= 0:
nextext = list(ext)
nextext[12] = xNum & 0x1F
# nextext[13] = 0
nextext[14] = xNum >> 5
nextext[15] = rc if rc <= 128 else 128
### ADD BLOCK POINTERS HERE
numRec = dpb['blksize'] // SEC_SZ
bc = (nextext[15] // numRec) + (1 if (nextext[15] % numRec) else 0)
for b in range(bc):
if dpb['disksize'] > 255:
nextext[16 + b*2] = blkN & 0xFF
nextext[17 + b*2] = blkN >> 8
else:
nextext[16 + b] = blkN
blkN += 1
# info(nextext)
for d in range(EXT_SZ):
dirdata[(dirI * EXT_SZ + d)] = nextext[d]
dirI += 1
xNum += 1
rc -= 128
return ( boot, bytearray(dirdata) )
def filename(buf, strip = True):
"""Format a filename as FILENAME.EXT
#### Parameters
- buf : the source array for characters (8+3)
- strip : strip trailing spaces from the filename and extension
"""
filename = ''
for f in range(8):
filename += chr(buf[ f ])
if strip:
filename = filename.rstrip()
filename += '.'
for e in range(3):
filename += chr(buf[ 8 + e ])
if strip:
filename = filename.rstrip()
return filename
# PARSE DIRECTORY EXTENTS INTO dir ARRAY OF DICTIONARIES ie. USER:FILE.EXT
def parseDir(dirData, blkMode):
dir = [ {} for _ in range(16) ]
for d in range(len(dirData) // EXT_SZ):
dirExt = dirData[ d * EXT_SZ : (d + 1) * EXT_SZ ]
user = dirExt[ 0 ]
filename = ''
for f in range(8):
filename += chr(dirExt[ 1 + f ])
filename += '.'
for e in range(3):
filename += chr(dirExt[ 9 + e ])
xl = dirExt[ 12 ]
bc = dirExt[ 13 ]
xh = dirExt[ 14 ]
rc = dirExt[ 15 ]
blkcount = 0
if blkMode:
blocks = []
for b in range(0, 16, 2):
bp = dirExt[ 16 + b ] | (dirExt[ 17 + b ] << 8)
if bp:
blkcount += 1
blocks.append(bp)
else:
blocks = dirExt[ 16 : 32 ]
for a in range(16):
if dirExt[ 16 + a ]:
blkcount += 1
# info(f'Entry: {d:02} User: {user:02X} Filename: {filename} Ext(xl:xh): {xl:02X}:{xh:02X} Len(bc:rc): {bc}:{rc} Bps: {list(blocks)}')
if user != DEL_BYTE:
# info(f'Entry: {d:02} User: {user:02X} Filename: {filename} Ext(xl:xh): {xl:02X}:{xh:02X} Len(bc:rc): {bc}:{rc} Bps: {list(blocks)}')
if dir[user].get(filename) == None:
dir[user][filename] = { 'blocks': 0, "recs": 0, "data": [] }
dir[user][filename]['blocks'] += blkcount
dir[user][filename]['recs'] += rc
dir[user][filename]['data'].extend(blocks)
return dir
# PRINT DIRECTORY
def printDir(dir, blksize):
for u in range(16):
if dir[u] != {}:
info('')
info(f'User {u}:')
info('')
info('Name Bytes Recs')
info('------------ ------ ------')
for f in dir[u]:
size = dir[u][f]['blocks']*(blksize//1024)
info(f"{f} {size:5}K {dir[u][f]['recs']:5}")
current_file = { 'file': '', 'mode': '', 'fd': None }
def file_start(file, mode):
if file == current_file['file'] and mode == current_file['mode']:
return current_file['fd']
if current_file['fd'] != None:
current_file['fd'].close()
current_file['file'] = file
current_file['mode'] = mode
current_file['fd'] = open(file, mode)
return current_file['fd']
def file_end():
if current_file['fd'] != None:
current_file['fd'].close()
current_file['file'] = ''
current_file['mode'] = ''
current_file['fd'] = None
def write_sector(unit, trk, sec, data):
if unit_info[unit]['type'] == 'IMG':
writeFileSector(unit, trk, sec, data)
elif unit_info[unit]['type'] == 'DIR':
writeDirSector(unit, trk, sec, data)
def dispFileSector(unit, trk, sec, mode):
dpb = unit_info[unit]['dpb']
tscale = unit_info[unit]['scale']
i = list(unit_info).index(unit)
win.addstr(4*i + 4, unit_info[unit]['last'] >> tscale, '.')
win.addstr(4*i + 5, unit_info[unit]['last'] >> tscale, ' ')
unit_info[unit]['last'] = trk
win.addstr(4*i + 4, trk >> tscale, mode)
if 'trans' in unit_info[unit]:
tsec = unit_info[unit]['trans'].index(sec)
else:
tsec = sec - 1
dtest = ((trk - dpb['offset']) * dpb['sectors'] + tsec) < (dpb['dirsize'] * EXT_SZ // SEC_SZ)
ind = 'B' if trk < dpb['offset'] else 'D' if dtest else 'E'
win.addstr(4*i + 5, trk >> tscale, ind)
win.refresh()
def writeFileSector(unit, trk, sec, data):
info(f"IMAGE WRITE: {unit}:{trk}:{sec} {unit_info[unit]['file']}")
dispFileSector(unit, trk, sec, 'W')
dpb = unit_info[unit]['dpb']
fd = open(unit_info[unit]['file'], 'r+b')
pos = (trk * dpb['sectors'] + sec - 1) * SEC_SZ
fd.seek(pos)
fd.write(data)
fd.close()
def lsec(e):
return e['lsec']
def dispDirAction(unit, desc):
i = list(unit_info).index(unit)
win.addstr(4*i + 6, 0, f"<DIR> - {desc}")
win.clrtoeol()
win.refresh()
# DO ALL THE DIRECTORY MAGIC
# - check for change to USER 0xE5 means DELETE file
# - check for change to FILENAME.EXT means RENAME file
# - check for change to xl,xh,bc,rc means ?????
# - check for changes to blockPointers (16) means WRITE new block from buffer
def check_dir_sec(unit, trk, sec, data):
root = unit_info[unit]['file']
dirdata = unit_info[unit]['dirdata']
dir = unit_info[unit]['dir']
dpb = unit_info[unit]['dpb']
numRec = dpb['blksize'] // SEC_SZ
# diff = [ 0 ] * SEC_SZ
pos = sec * SEC_SZ
secdata = dirdata[pos: pos + SEC_SZ]
ext = -1
# FIND *FIRST* EXTENT THAT HAS CHANGED - ASSUMES ONLY ONE!
for i in range(len(data)):
if data[i] != secdata[i]:
# diff[i] = ( data[i], secdata[i] )
ext = i // EXT_SZ
break
# info (ext)
if ext > -1:
lext = ext + sec * (SEC_SZ // EXT_SZ)
else:
warning("NO DIRECTORY EXTENT HAS CHANGED")
return
# info(dext)
extpos = lext * EXT_SZ
debug('BEFORE:', dirdata[extpos: extpos + EXT_SZ])
debug('AFTER :', bytearray(data[ext * EXT_SZ:(ext + 1) * EXT_SZ]))
orig = {
'user': dirdata[extpos],
'file': bytes(dirdata[extpos + 1 : extpos + 12]),
'xl': dirdata[extpos + 12],
'xh': dirdata[extpos + 14],
'xNum': ((dirdata[extpos + 14] & 0x2F) << 5) | (dirdata[extpos + 12] & 0x1F),
'rc': dirdata[extpos + 15],
# 'blocks': bytes(dirdata[extpos + 16 :extpos + 32])
}
if dpb['disksize'] > 255:
blocks = []
for b in range(0, 16, 2):
bp = dirdata[extpos + 16 + b ] | (dirdata[extpos + 17 + b] << 8)
blocks.append(bp)
orig['blocks'] = blocks
else:
orig['blocks'] = bytes(dirdata[extpos + 16 :extpos + 32])
new = {
'user': data[ext * EXT_SZ],
'file': data[ext * EXT_SZ + 1 : ext * EXT_SZ + 12],
'xl': data[ext * EXT_SZ + 12],
'xh': data[ext * EXT_SZ + 14],
'xNum': ((data[ext * EXT_SZ + 14] & 0x2F) << 5) | (data[ext * EXT_SZ + 12] & 0x1F),
'rc': data[ext * EXT_SZ + 15],
# 'blocks': data[ext * EXT_SZ + 16 :ext * EXT_SZ + 32]
}
if dpb['disksize'] > 255:
blocks = []
for b in range(0, 16, 2):
bp = data[ext * EXT_SZ + 16 + b ] | (data[ext * EXT_SZ + 17 + b] << 8)
blocks.append(bp)
new['blocks'] = blocks
else:
new['blocks'] = data[ext * EXT_SZ + 16 :ext * EXT_SZ + 32]
# info(orig)
# info(new)
# DELETE
if orig['user'] < 16 and new['user'] == DEL_BYTE:
if new['xNum'] == 0:
info(f"DELETE FILE: {filename(orig['file'])}")
dispDirAction(unit, f"DELETE FILE: {orig['user']}:{filename(orig['file'])}")
try:
os.remove(os.path.join(root, f"{orig['user']}", filename(orig['file'])))
except:
pass
else: # new['xNum'] > 0:
info(f"MARK DELETED EXTENT: {new['xNum']} for {orig['file']}")
dispDirAction(unit, f"DELETE LOGICAL EXTENT: {new['xNum']} for {orig['user']}:{filename(orig['file'])}")
# CREATE
elif orig['user'] == DEL_BYTE and new['user'] < 16:
if new['xNum'] == 0:
info(f"CREATE FILE: {filename(new['file'])}")
dispDirAction(unit, f"CREATE FILE: {new['user']}:{filename(new['file'])}")
try:
fd = file_start(os.path.join(root, f"{new['user']}", filename(new['file'])), "xb")
# file_end()
except:
pass
else: # new['xNum'] > 0:
info(f"ADD EXTENT: {new['xNum']} {new['file']}")
dispDirAction(unit, f"ADD LOGICAL EXTENT: {new['xNum']} for {new['user']}:{filename(new['file'])}")
# RENAME
elif new['file'] != orig['file']:
if new['xNum'] == 0:
info(f"RENAME FILE: {filename(orig['file'])} to {filename(new['file'])}")
dispDirAction(unit, f"RENAME FILE: {orig['user']}:{filename(orig['file'])} to {new['user']}:{filename(new['file'])}")
try:
os.rename(os.path.join(root, f"{orig['user']}", filename(orig['file'])),
os.path.join(root, f"{new['user']}", filename(new['file'])))
except:
pass
else: # new['xNum'] > 0:
info(f"RENAME EXTENT: {new['xNum']} {orig['file']} to {new['file']}")
dispDirAction(unit, f"RENAME LOGICAL EXTENT: {new['xNum']} from {orig['user']}:{filename(orig['file'])} to {new['user']}:{filename(new['file'])}")
# ADD SECTORS/BLOCKS TO AN EXTENT
else:
info(f"UPDATE EXTENT: {new['file']} {new['xNum']}")
# info(unit_info[unit]['buffer'])
unit_info[unit]['buffer'].sort(key=lsec)
fd = file_start(os.path.join(root, f"{new['user']}", filename(new['file'])), 'r+b')
for n in new['blocks']:
found = False
if n != orig['blocks'][new['blocks'].index(n)]:
for b in unit_info[unit]['buffer']:
if b['blk'] == n:
dispDirAction(unit, f"WRITE BUFFERED BLOCK TO DISK: {b['blk']} to {new['user']}:{filename(new['file'])}")
found = True
if new['xNum'] == 0: # if first extent, use first block as base
pos = (b['lsec'] - (new['blocks'][0] * numRec)) * SEC_SZ
else: # if NOT first extent, use first block in first extent as base
pos = (b['lsec'] - (dir[new['user']][filename(new['file'], False)]['data'][0] * numRec)) * SEC_SZ
# info(b['lsec'], new['xNum'], pos)
fd.seek(pos)
fd.write(b['data'])
b['blk'] = -1 # mark buffer entry as used
# DETECT IF A NEW BLOCK HAS NO DATA IN THE BUFFER
if not found and n != 0:
warning(f"BAD BLOCK REF {n} - NO DATA AVAILABLE IN BUFFER")
file_end()
# TEST TO SEE IF ANY DATA REMAINS UNUSED IN THE BUFFER
for b in unit_info[unit]['buffer']:
if b['blk'] >= 0:
warning(f"UNUSED DATA IN WRITE BUFFER blk={b['blk']} lsec={b['lsec']}")
#EMPTY THE BUFFER
unit_info[unit]['buffer'].clear()
# UPDATE IN MEMORY DIRECTORY STRUCTURES
# for i in range(EXT_SZ):
# dirdata[extpos + i ] = data[ext * EXT_SZ + i]
dirdata[extpos: extpos + EXT_SZ] = data[ext * EXT_SZ: (ext + 1) * EXT_SZ]
#unit_info[unit]['dirdata'] = dirdata # not needed as lists are by reference not copied
unit_info[unit]['dir'] = parseDir(dirdata, 1 if unit_info[unit]['dpb']['disksize'] > 255 else 0)
def dispDirSector(unit, trk, sec, mode, type, desc):
i = list(unit_info).index(unit)
tscale = unit_info[unit]['scale']
win.addstr(4*i + 4, unit_info[unit]['last'] >> tscale, '.')
win.addstr(4*i + 5, unit_info[unit]['last'] >> tscale, ' ')
unit_info[unit]['last'] = trk
win.addstr(4*i + 4, trk >> tscale, mode, curses.A_BOLD)
win.addstr(4*i + 5, trk >> tscale, type, curses.A_BOLD)
win.addstr(4*i + 6, 0, desc)
win.clrtoeol()
win.refresh()
def writeDirSector(unit, trk, sec, data):
dpb = unit_info[unit]['dpb']
root = unit_info[unit]['file']
# dirdata = unit_info[unit]['dirdata']
dir = unit_info[unit]['dir']
# BOOT TRACKS
if trk < dpb['offset']:
pos = (trk * dpb['sectors'] + sec - 1) * SEC_SZ
info(f"WRITE BOOT: {trk}:{sec} pos= {pos}")
dispDirSector(unit, trk, sec, 'W', 'B', '$BOOT')
if pos == 0:
fd = file_start(os.path.join(root, '$BOOT'), 'xb')
file_end()
fd = file_start(os.path.join(root, '$BOOT'), 'r+b')
fd.seek(pos)
fd.write(data)
file_end()
# fd.close()
return
if 'trans' in unit_info[unit]:
sec = unit_info[unit]['trans'].index(sec)
else:
sec = sec - 1
numRec = dpb['blksize'] // SEC_SZ
sec = (trk - dpb['offset']) * dpb['sectors'] + sec
blk = sec // numRec
# DIRECTORY
if sec < ((dpb['dirsize'] * EXT_SZ) // SEC_SZ):
info(f"WRITE DIR : {trk}:{sec}")
dispDirSector(unit, trk, sec, 'W', 'D', '<DIR>')
check_dir_sec(unit, trk, sec, data)
else:
for u in range(16):
for f in dir[u]:
if blk in dir[u][f]['data']:
fn = f.split('.',1)
fn[0] = fn[0].strip()
fn[1] = fn[1].strip()
fn = '.'.join(fn)
pos = (sec - (dir[u][f]['data'][0] * numRec)) * SEC_SZ
info(f"WRITE TO FILE BLOCK: {trk}:{sec} block:{blk} in file: {fn} pos: {pos}")
dispDirSector(unit, trk, sec, 'W', f"{u:X}", f"{u}: {fn}")
fd = file_start(os.path.join(root, f'{u}', fn), 'r+b')
fd.seek(pos)
fd.write(data)
# fd.close()
break
else:
continue
break
else:
info(f"WRITE TO EMPTY BLOCK: {trk}:{sec} block:{blk}")
dispDirSector(unit, trk, sec, 'W', '#', '<BUFFERING>')
unit_info[unit]['buffer'].append({ 'lsec': sec, 'blk': blk, 'data': data })
return
def read_sector(unit, trk, sec):
if unit_info[unit]['type'] == 'IMG':
return readFileSector(unit, trk, sec)
elif unit_info[unit]['type'] == 'DIR':
return readDirSector(unit, trk, sec)
def readFileSector(unit, trk, sec):
info(f"IMAGE READ: {unit}:{trk}:{sec} {unit_info[unit]['file']}")
dispFileSector(unit, trk, sec, 'R')
dpb = unit_info[unit]['dpb']
fd = open(unit_info[unit]['file'], 'rb')
pos = (trk * dpb['sectors'] + sec - 1) * SEC_SZ
fd.seek(pos)
data = fd.read(SEC_SZ)
fd.close()
return data
def readDirSector(unit, trk, sec):
empty_sec = [ DEL_BYTE ] * SEC_SZ
eof_sec = [ EOF_BYTE ] * SEC_SZ
root = unit_info[unit]['file']
boot = unit_info[unit]['boot']
dirdata = unit_info[unit]['dirdata']
dir = unit_info[unit]['dir']
dpb = unit_info[unit]['dpb']
# BOOT TRACKS
if trk < dpb['offset']:
info(f"READ BOOT: {trk}:{sec}")
dispDirSector(unit, trk, sec, 'R', 'B', '$BOOT')
if boot:
fd = file_start(os.path.join(root, '$BOOT'), 'rb')
pos = (trk * dpb['sectors'] + sec - 1) * SEC_SZ
fd.seek(pos)
data = fd.read(SEC_SZ)
# fd.close()
else:
data = bytearray(empty_sec)
return data
if 'trans' in unit_info[unit]:
sec = unit_info[unit]['trans'].index(sec)
else:
sec = sec - 1
numRec = dpb['blksize'] // SEC_SZ
sec = (trk - dpb['offset']) * dpb['sectors'] + sec
blk = sec // numRec
# DIRECTORY
if sec < ((dpb['dirsize'] * EXT_SZ) // SEC_SZ):
info(f"READ DIR : {trk}:{sec}")
dispDirSector(unit, trk, sec, 'R', 'D', '<DIR>')
file_end()
pos = sec * SEC_SZ
data = dirdata[pos: pos + SEC_SZ]
# DISK DATA
else:
for u in range(16):
for f in dir[u]:
if blk in dir[u][f]['data']: