Skip to content

Commit

Permalink
Merge pull request #29 from skx/27-files
Browse files Browse the repository at this point in the history
Prepare towards allowing multiple files to be opened.
  • Loading branch information
skx authored Apr 16, 2024
2 parents f73d20a + 74c18ba commit d0cb7ff
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 7 deletions.
4 changes: 4 additions & 0 deletions cpm/cpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func New(filename string, logger *slog.Logger) *CPM {
Desc: "DRV_GET",
Handler: SysCallDriveGet,
}
sys[26] = CPMHandler{
Desc: "F_DMAOFF",
Handler: SysCallSetDMA,
}
sys[31] = CPMHandler{
Desc: "DRV_DPB",
Handler: SysCallGetDriveDPB,
Expand Down
36 changes: 29 additions & 7 deletions cpm/cpm_syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -179,11 +179,8 @@ func SysCallFileOpen(cpm *CPM) error {
// handle in our CPM struct. Record it
cpm.fileIsOpen = true

// No error, so we can proceed with the rest of the
// steps.
fcbPtr.S1 = 0x00
fcbPtr.S2 |= 0x80 // not modified
fcbPtr.RC = 0x00
// Reset the values
fcbPtr.Al[0] = 'S'

// Get file size, in bytes
fi, err := cpm.file.Stat()
Expand Down Expand Up @@ -218,6 +215,19 @@ func SysCallFileOpen(cpm *CPM) error {
// SysCallFileClose closes the filename that matches the pattern on the FCB supplied in DE
func SysCallFileClose(cpm *CPM) error {

// The pointer to the FCB
ptr := cpm.CPU.States.DE.U16()

// Get the bytes which make up the FCB entry.
xxx := cpm.Memory.GetRange(ptr, 36)

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)

if fcbPtr.Al[0] != 'S' {
return fmt.Errorf("S1 has not maintained state - in FileClose")
}

// Close the handle, if we have one
if cpm.fileIsOpen {
cpm.fileIsOpen = false
Expand Down Expand Up @@ -379,6 +389,10 @@ func SysCallRead(cpm *CPM) error {
// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)

if fcbPtr.Al[0] != 'S' {
return fmt.Errorf("S1 has not maintained state - in SysCallRead")
}

// Get the next read position
offset := fcbPtr.GetSequentialOffset()

Expand All @@ -397,8 +411,8 @@ func SysCallRead(cpm *CPM) error {

// Read from the file, now we're in the right place
_, err = cpm.file.Read(data)
if err != nil {
return fmt.Errorf("error writing to file %s", err)
if err != nil && err != io.EOF {
return fmt.Errorf("error reading file %s", err)
}

// Copy the data to the DMA area
Expand Down Expand Up @@ -434,6 +448,10 @@ func SysCallWrite(cpm *CPM) error {
// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)

if fcbPtr.Al[0] != 'S' {
return fmt.Errorf("S1 has not maintained state - in SysCallWrite")
}

// Get the next write position
offset := fcbPtr.GetSequentialOffset()

Expand Down Expand Up @@ -566,6 +584,10 @@ func SysCallReadRand(cpm *CPM) error {
// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)

if fcbPtr.Al[0] != 'S' {
return fmt.Errorf("S1 has not maintained state - in SysCallReadRand")
}

if !cpm.fileIsOpen {
return fmt.Errorf("ReadRand called against a non-open file")
}
Expand Down

0 comments on commit d0cb7ff

Please sign in to comment.