Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.

Commit 050186d

Browse files
committed
Always use drive-remapping (internally)
Rather than conditionally using subdirectories as drives, we now just do that always. If drives are in use then we have amapping: * A: -> A/ * B: -> B/ etc If they are not? *A: -> . *B: -> . That means we can always use our lookup table (i.e. map) to know where to look for things and don't need to have special cases. This closes #101.
1 parent 6bda891 commit 050186d

File tree

4 files changed

+51
-80
lines changed

4 files changed

+51
-80
lines changed

cpm/cpm.go

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -147,9 +147,8 @@ type CPM struct {
147147
// be able to emulate that.
148148
CPU z80.CPU
149149

150-
// Drives specifies whether we use sub-directories for the
151-
// CP/M drives we emulate, instead of the current working directory.
152-
Drives bool
150+
// Drives specifies the local paths for each directory.
151+
drives map[string]string
153152

154153
// currentDrive contains the currently selected drive.
155154
// Valid values are 0-15, where they work in the obvious way:
@@ -420,6 +419,7 @@ func New(logger *slog.Logger, prn string, condriver string, ccp string) (*CPM, e
420419
Logger: logger,
421420
ccp: ccp,
422421
dma: 0x0080,
422+
drives: make(map[string]string),
423423
files: make(map[uint16]FileCache),
424424
input: consolein.New(),
425425
output: driver,
@@ -823,12 +823,11 @@ func (cpm *CPM) RunAutoExec() {
823823
// without doing anything.
824824
for _, name := range files {
825825

826-
path := "."
827-
if cpm.Drives {
828-
path = string(cpm.currentDrive + 'A')
829-
}
826+
// Get the local prefix.
827+
prefix := cpm.drives[string(cpm.currentDrive+'A')]
830828

831-
dst := filepath.Join(path, name)
829+
// Add the name
830+
dst := filepath.Join(prefix, name)
832831

833832
handle, err := os.OpenFile(dst, os.O_RDONLY, 0644)
834833
if err != nil {
@@ -843,9 +842,19 @@ func (cpm *CPM) RunAutoExec() {
843842
}
844843

845844
// SetDrives enables/disables the use of subdirectories upon the host system
846-
// to represent CP/M drives
845+
// to represent CP/M drives.
846+
//
847+
// We use a map to handle the drive->path mappings, and if directories are
848+
// not used we just store "." in the appropriate entry.
847849
func (cpm *CPM) SetDrives(enabled bool) {
848-
cpm.Drives = true
850+
851+
for _, c := range []string{"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P"} {
852+
if enabled {
853+
cpm.drives[c] = c
854+
} else {
855+
cpm.drives[c] = "."
856+
}
857+
}
849858
}
850859

851860
// In is called to handle the I/O reading of a Z80 port.

cpm/cpm_bdos.go

Lines changed: 24 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -267,13 +267,10 @@ func SysCallDriveAllReset(cpm *CPM) error {
267267
var ret uint8 = 0
268268

269269
// drive will default to our current drive, if the FCB drive field is 0
270-
drive := cpm.currentDrive + 'A'
270+
drive := string(cpm.currentDrive + 'A')
271271

272-
// Should we remap drives?
273-
path := "."
274-
if cpm.Drives {
275-
path = string(drive)
276-
}
272+
// Remap to the place we're supposed to use.
273+
path := cpm.drives[drive]
277274

278275
// Look for a file with $ in its name
279276
files, err := os.ReadDir(path)
@@ -354,11 +351,8 @@ func SysCallFileOpen(cpm *CPM) error {
354351
drive = fcbPtr.Drive + 'A' - 1
355352
}
356353

357-
// Should we remap drives?
358-
path := "."
359-
if cpm.Drives {
360-
path = string(drive)
361-
}
354+
// Remap to the place we're supposed to use.
355+
path := cpm.drives[string(drive)]
362356

363357
//
364358
// Ok we have a filename, but we probably have an upper-case
@@ -384,16 +378,8 @@ func SysCallFileOpen(cpm *CPM) error {
384378
slog.String("drive", string(cpm.currentDrive+'A')),
385379
slog.String("result", fileName))
386380

387-
// Should we remap drives?
388-
if cpm.Drives {
389-
before := fileName
390-
391-
fileName = filepath.Join(string(drive), fileName)
392-
393-
l.Debug("SysCallFileOpen remapped path",
394-
slog.String("before", before),
395-
slog.String("after", fileName))
396-
}
381+
// Ensure the filename is qualified
382+
fileName = filepath.Join(path, fileName)
397383

398384
// Now we open..
399385
file, err := os.OpenFile(fileName, os.O_RDWR, 0644)
@@ -531,10 +517,8 @@ func SysCallFindFirst(cpm *CPM) error {
531517
// Create a structure with the contents
532518
fcbPtr := fcb.FromBytes(xxx)
533519

534-
dir := "."
535-
if cpm.Drives {
536-
dir = string(cpm.currentDrive + 'A')
537-
}
520+
// Look in the correct location.
521+
dir := cpm.drives[string(cpm.currentDrive+'A')]
538522

539523
// Find files in the FCB.
540524
res, err := fcbPtr.GetMatches(dir)
@@ -651,10 +635,8 @@ func SysCallDeleteFile(cpm *CPM) error {
651635
drive = fcbPtr.Drive + 'A' - 1
652636
}
653637

654-
path := "."
655-
if cpm.Drives {
656-
path = string(drive)
657-
}
638+
// Remap to the place we're supposed to use.
639+
path := cpm.drives[string(drive)]
658640

659641
// Find files in the FCB.
660642
res, err := fcbPtr.GetMatches(path)
@@ -857,11 +839,8 @@ func SysCallMakeFile(cpm *CPM) error {
857839
drive = fcbPtr.Drive + 'A' - 1
858840
}
859841

860-
// Should we remap drives?
861-
path := "."
862-
if cpm.Drives {
863-
path = string(drive)
864-
}
842+
// Remap to the place we're supposed to use.
843+
path := cpm.drives[string(drive)]
865844

866845
//
867846
// Ok we have a filename, but we probably have an upper-case
@@ -887,16 +866,8 @@ func SysCallMakeFile(cpm *CPM) error {
887866
slog.String("drive", string(cpm.currentDrive+'A')),
888867
slog.String("result", fileName))
889868

890-
// Should we remap drives?
891-
if cpm.Drives {
892-
before := fileName
893-
894-
fileName = filepath.Join(string(drive), fileName)
895-
896-
l.Debug("SysCallMakeFile remapped path",
897-
slog.String("before", before),
898-
slog.String("after", fileName))
899-
}
869+
// Qualify the path
870+
fileName = filepath.Join(path, fileName)
900871

901872
// Create the file
902873
file, err := os.OpenFile(fileName, os.O_CREATE|os.O_RDWR, 0644)
@@ -967,11 +938,8 @@ func SysCallRenameFile(cpm *CPM) error {
967938
fileName += ext
968939
}
969940

970-
// Should we remap drives?
971-
path := "."
972-
if cpm.Drives {
973-
path = string(cpm.currentDrive + 'A')
974-
}
941+
// Point to the directory
942+
path := cpm.drives[string(cpm.currentDrive+'A')]
975943

976944
//
977945
// Ok we have a filename, but we probably have an upper-case
@@ -989,10 +957,8 @@ func SysCallRenameFile(cpm *CPM) error {
989957
}
990958
}
991959

992-
// Should we remap drives?
993-
if cpm.Drives {
994-
fileName = filepath.Join(string(cpm.currentDrive+'A'), fileName)
995-
}
960+
// Ensure the filename is qualified
961+
fileName = filepath.Join(path, fileName)
996962

997963
// 2. DEST
998964
// The pointer to the FCB
@@ -1011,10 +977,8 @@ func SysCallRenameFile(cpm *CPM) error {
1011977
dstName += dExt
1012978
}
1013979

1014-
// Should we remap drives?
1015-
if cpm.Drives {
1016-
dstName = filepath.Join(string(cpm.currentDrive+'A'), dstName)
1017-
}
980+
// ensure the name is qualified
981+
dstName = filepath.Join(path, dstName)
1018982

1019983
cpm.Logger.Debug("Renaming file",
1020984
slog.String("src", fileName),
@@ -1287,10 +1251,7 @@ func SysCallFileSize(cpm *CPM) error {
12871251
}
12881252

12891253
// Should we remap drives?
1290-
path := "."
1291-
if cpm.Drives {
1292-
path = string(cpm.currentDrive + 'A')
1293-
}
1254+
path := cpm.drives[string(cpm.currentDrive+'A')]
12941255

12951256
//
12961257
// Ok we have a filename, but we probably have an upper-case
@@ -1308,10 +1269,8 @@ func SysCallFileSize(cpm *CPM) error {
13081269
}
13091270
}
13101271

1311-
// Should we remap drives?
1312-
if cpm.Drives {
1313-
fileName = filepath.Join(string(cpm.currentDrive+'A'), fileName)
1314-
}
1272+
// esnure the path is qualified
1273+
fileName = filepath.Join(path, fileName)
13151274

13161275
file, err := os.OpenFile(fileName, os.O_RDONLY, 0644)
13171276
if err != nil {

cpm/cpm_bios.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ func BiosSysCallReserved1(cpm *CPM) error {
199199
//
200200
getStringFromMemory := func(addr uint16) string {
201201
str := ""
202-
c := cpm.Memory.Get(addr)
203-
for c != ' ' && c != 0x00 {
204-
str += string(c)
202+
x := cpm.Memory.Get(addr)
203+
for x != ' ' && x != 0x00 {
204+
str += string(x)
205205
addr++
206-
c = cpm.Memory.Get(addr)
206+
x = cpm.Memory.Get(addr)
207207
}
208208

209209
// Useful when the CCP has passed a string, because

main.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,13 @@ func main() {
173173
}
174174
}
175175

176+
// Default to not using subdirectories for drives
177+
obj.SetDrives(false)
178+
176179
// Are we using drives?
177180
if *useDirectories {
178181

179-
// Enable drives
182+
// Enable the use of directories.
180183
obj.SetDrives(true)
181184

182185
// Count how many drives exist - if zero show a warning

0 commit comments

Comments
 (0)