@@ -424,6 +424,10 @@ func BdosSysCallFileOpen(cpm *CPM) error {
424424 fcbPtr .RC = fLen
425425 }
426426
427+ // Write our cache-key in the FCB
428+ fcbPtr .Al [0 ] = uint8 (ptr & 0xFF )
429+ fcbPtr .Al [1 ] = uint8 (ptr >> 8 )
430+
427431 // Update the FCB in memory.
428432 cpm .Memory .SetRange (ptr , fcbPtr .AsBytes ()... )
429433
@@ -483,6 +487,10 @@ func BdosSysCallFileOpen(cpm *CPM) error {
483487 slog .Int ("record_count" , int (fcbPtr .RC )),
484488 slog .Int64 ("file_size" , fileSize ))
485489
490+ // Write our cache-key in the FCB
491+ fcbPtr .Al [0 ] = uint8 (ptr & 0xFF )
492+ fcbPtr .Al [1 ] = uint8 (uint16 (ptr >> 8 ))
493+
486494 // Update the FCB in memory.
487495 cpm .Memory .SetRange (ptr , fcbPtr .AsBytes ()... )
488496
@@ -509,8 +517,11 @@ func BdosSysCallFileClose(cpm *CPM) error {
509517 // Create a structure with the contents
510518 fcbPtr := fcb .FromBytes (xxx )
511519
520+ // Get our cache-key from the FCB
521+ key := uint16 (uint16 (fcbPtr .Al [1 ])<< 8 + uint16 (fcbPtr .Al [0 ]))
522+
512523 // Get the file handle from our cache.
513- obj , ok := cpm .files [ptr ]
524+ obj , ok := cpm .files [key ]
514525 if ! ok {
515526 cpm .Logger .Debug ("SysCallFileClose tried to close a file that wasn't open" ,
516527 slog .Int ("fcb" , int (ptr )))
@@ -554,10 +565,12 @@ func BdosSysCallFileClose(cpm *CPM) error {
554565 }
555566
556567 // delete the entry from the cache.
557- delete (cpm .files , ptr )
568+ delete (cpm .files , key )
558569
559570 // Update the FCB in RAM
560- // cpm.Memory.SetRange(ptr, fcbPtr.AsBytes()...)
571+ fcbPtr .Al [0 ] = 0x00
572+ fcbPtr .Al [1 ] = 0x00
573+ cpm .Memory .SetRange (ptr , fcbPtr .AsBytes ()... )
561574
562575 // Record success
563576 cpm .CPU .States .AF .Hi = 0x00
@@ -787,8 +800,11 @@ func BdosSysCallRead(cpm *CPM) error {
787800 // Create a structure with the contents
788801 fcbPtr := fcb .FromBytes (xxx )
789802
803+ // Get our cache-key from the FCB
804+ key := uint16 (uint16 (fcbPtr .Al [1 ])<< 8 + uint16 (fcbPtr .Al [0 ]))
805+
790806 // Get the file handle in our cache.
791- obj , ok := cpm .files [ptr ]
807+ obj , ok := cpm .files [key ]
792808 if ! ok {
793809 cpm .Logger .Error ("SysCallRead: Attempting to read from a file that isn't open" )
794810 cpm .CPU .States .AF .Hi = 0xFF
@@ -893,8 +909,11 @@ func BdosSysCallWrite(cpm *CPM) error {
893909 // Create a structure with the contents
894910 fcbPtr := fcb .FromBytes (xxx )
895911
912+ // Get our cache-key from the FCB
913+ key := uint16 (uint16 (fcbPtr .Al [1 ])<< 8 + uint16 (fcbPtr .Al [0 ]))
914+
896915 // Get the file handle in our cache.
897- obj , ok := cpm .files [ptr ]
916+ obj , ok := cpm .files [key ]
898917 if ! ok {
899918 cpm .Logger .Error ("SysCallWrite: Attempting to write to a file that isn't open" )
900919 cpm .CPU .States .AF .Hi = 0xFF
@@ -1023,6 +1042,10 @@ func BdosSysCallMakeFile(cpm *CPM) error {
10231042 fcbPtr .RC = fLen
10241043 }
10251044
1045+ // Write our cache-key in the FCB
1046+ fcbPtr .Al [0 ] = uint8 (ptr & 0xFF )
1047+ fcbPtr .Al [1 ] = uint8 (ptr >> 8 )
1048+
10261049 // Save the file-handle
10271050 cpm .files [ptr ] = FileCache {name : fileName , handle : file }
10281051
@@ -1269,8 +1292,11 @@ func BdosSysCallReadRand(cpm *CPM) error {
12691292 // Create a structure with the contents
12701293 fcbPtr := fcb .FromBytes (xxx )
12711294
1295+ // Get our cache-key from the FCB
1296+ key := uint16 (uint16 (fcbPtr .Al [1 ])<< 8 + uint16 (fcbPtr .Al [0 ]))
1297+
12721298 // Get the file handle in our cache.
1273- obj , ok := cpm .files [ptr ]
1299+ obj , ok := cpm .files [key ]
12741300 if ! ok {
12751301 cpm .Logger .Error ("SysCallReadRand: Attempting to read from a file that isn't open" )
12761302 cpm .CPU .States .AF .Hi = 0xFF
@@ -1319,8 +1345,11 @@ func BdosSysCallWriteRand(cpm *CPM) error {
13191345 // Create a structure with the contents
13201346 fcbPtr := fcb .FromBytes (xxx )
13211347
1348+ // Get our cache-key from the FCB
1349+ key := uint16 (uint16 (fcbPtr .Al [1 ])<< 8 + uint16 (fcbPtr .Al [0 ]))
1350+
13221351 // Get the file handle in our cache.
1323- obj , ok := cpm .files [ptr ]
1352+ obj , ok := cpm .files [key ]
13241353 if ! ok {
13251354 cpm .Logger .Error ("SysCallWriteRand: Attempting to write to a file that isn't open" )
13261355 cpm .CPU .States .AF .Hi = 0xFF
0 commit comments