Skip to content

Commit

Permalink
Allowing the Aztec C-compiler to work - SysCallWriteRand was broken (#48
Browse files Browse the repository at this point in the history
)

* Work towards allowing the Aztec C-compiler to work.

This pull request, once complete, will close #38 by resolving the
problem that keeps the AS.com assembler from assembling a working
output file (.O).

The problem seems to be related to file-based I/O, but I've not yet
worked out out.

So far this pull request:

* Adds more debug-output
* Switches to using fcb.SIZE rather than 36 to avoid mistakes.
* Updates to use the real slice offsets for data-copies.
* Corrects a bug where the DMA area was hardcoded to be 0x0080 for FindFirst
  * This didn't help, but was obviously wrong.

* Added simple write-sequential test-program.

* Added simple read record helper.  Seems to work.

* FIXED The bug

I was incorrectly adding padding.
  • Loading branch information
skx committed Apr 24, 2024
1 parent 5237513 commit 9365f43
Show file tree
Hide file tree
Showing 9 changed files with 329 additions and 18 deletions.
6 changes: 3 additions & 3 deletions cpm/cpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,7 @@ func (cpm *CPM) LoadCCP() {
var ccpEntrypoint uint16 = 0xDE00

// Load it into memory
cpm.Memory.SetRange(ccpEntrypoint, data[:]...)
cpm.Memory.SetRange(ccpEntrypoint, data...)

// DMA area / CLI Args are going to be unset.
cpm.Memory.Set(0x0080, 0x00)
Expand Down Expand Up @@ -383,13 +383,13 @@ func (cpm *CPM) Execute(args []string) error {
// Setup FCB1 if we have a first argument
if len(args) > 0 {
x := fcb.FromString(args[0])
cpm.Memory.SetRange(0x005C, x.AsBytes()[:]...)
cpm.Memory.SetRange(0x005C, x.AsBytes()...)
}

// Setup FCB2 if we have a second argument
if len(args) > 1 {
x := fcb.FromString(args[1])
cpm.Memory.SetRange(0x006C, x.AsBytes()[:]...)
cpm.Memory.SetRange(0x006C, x.AsBytes()...)
}

// Poke in the CLI argument as a Pascal string.
Expand Down
68 changes: 54 additions & 14 deletions cpm/cpm_syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func SysCallFileOpen(cpm *CPM) error {
ptr := cpm.CPU.States.DE.U16()

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

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)
Expand Down Expand Up @@ -365,7 +365,7 @@ func SysCallFindFirst(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)
xxx := cpm.Memory.GetRange(ptr, fcb.SIZE)

// Previous results are now invalidated
cpm.findFirstResults = []string{}
Expand Down Expand Up @@ -407,6 +407,9 @@ func SysCallFindFirst(cpm *CPM) error {
cpm.Memory.SetRange(cpm.dma, data...)

// Return 0x00 to point to the first entry in the DMA area.
cpm.CPU.States.HL.Hi = 0x00
cpm.CPU.States.HL.Lo = 0x00
cpm.CPU.States.BC.Hi = 0x00
cpm.CPU.States.AF.Hi = 0x00

return nil
Expand All @@ -429,9 +432,12 @@ func SysCallFindNext(cpm *CPM) error {
// Create a new FCB and store it in the DMA entry
x := fcb.FromString(res)
data := x.AsBytes()
cpm.Memory.SetRange(0x80, data...)
cpm.Memory.SetRange(cpm.dma, data...)

// Return 0x00 to point to the first entry in the DMA area.
cpm.CPU.States.HL.Hi = 0x00
cpm.CPU.States.HL.Lo = 0x00
cpm.CPU.States.BC.Hi = 0x00
cpm.CPU.States.AF.Hi = 0x00

return nil
Expand All @@ -442,7 +448,7 @@ func SysCallDeleteFile(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)
xxx := cpm.Memory.GetRange(ptr, fcb.SIZE)

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)
Expand Down Expand Up @@ -506,7 +512,7 @@ func SysCallRead(cpm *CPM) error {
ptr := cpm.CPU.States.DE.U16()

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

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)
Expand Down Expand Up @@ -541,8 +547,15 @@ func SysCallRead(cpm *CPM) error {
return fmt.Errorf("error reading file %s", err)
}

// Add logging of the result and details.
cpm.Logger.Debug("SysCallRead",
slog.Int("dma", int(cpm.dma)),
slog.Int("fcb", int(ptr)),
slog.Int("handle", int(obj.handle.Fd())),
slog.Int("offset", int(offset)))

// Copy the data to the DMA area
cpm.Memory.SetRange(cpm.dma, data[:]...)
cpm.Memory.SetRange(cpm.dma, data...)

// Update the next read position
fcbPtr.IncreaseSequentialOffset()
Expand All @@ -567,7 +580,7 @@ func SysCallWrite(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)
xxx := cpm.Memory.GetRange(ptr, fcb.SIZE)

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)
Expand All @@ -583,6 +596,13 @@ func SysCallWrite(cpm *CPM) error {
// Get the next write position
offset := fcbPtr.GetSequentialOffset()

// Add logging of the result and details.
cpm.Logger.Debug("SysCallWrite",
slog.Int("dma", int(cpm.dma)),
slog.Int("fcb", int(ptr)),
slog.Int("handle", int(obj.handle.Fd())),
slog.Int("offset", int(offset)))

// Get the data range from the DMA area
data := cpm.Memory.GetRange(cpm.dma, 128)

Expand Down Expand Up @@ -616,7 +636,7 @@ func SysCallMakeFile(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)
xxx := cpm.Memory.GetRange(ptr, fcb.SIZE)

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)
Expand Down Expand Up @@ -718,7 +738,7 @@ func SysCallRenameFile(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)
xxx := cpm.Memory.GetRange(ptr, fcb.SIZE)

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)
Expand Down Expand Up @@ -762,7 +782,7 @@ func SysCallRenameFile(cpm *CPM) error {

// 2. DEST
// The pointer to the FCB
xxx2 := cpm.Memory.GetRange(ptr+16, 36)
xxx2 := cpm.Memory.GetRange(ptr+16, fcb.SIZE)

// Create a structure with the contents
dstPtr := fcb.FromBytes(xxx2)
Expand Down Expand Up @@ -891,15 +911,15 @@ func SysCallReadRand(cpm *CPM) error {
}
}

cpm.Memory.SetRange(cpm.dma, data[:]...)
cpm.Memory.SetRange(cpm.dma, data...)
return 0
}

// 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)
xxx := cpm.Memory.GetRange(ptr, fcb.SIZE)

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)
Expand All @@ -921,6 +941,16 @@ func SysCallReadRand(cpm *CPM) error {
// Read the data
res := sysRead(obj.handle, fpos)

// Add logging of the result and details.
cpm.Logger.Debug("SysCallReadRand",
slog.Int("dma", int(cpm.dma)),
slog.Int("fcb", int(ptr)),
slog.Int("handle", int(obj.handle.Fd())),
slog.Int("record_count", int(fcbPtr.RC)),
slog.Int("record", record),
slog.Int64("fpos", fpos),
slog.Int("result", res))

// Update the FCB in memory
cpm.Memory.SetRange(ptr, fcbPtr.AsBytes()...)
cpm.CPU.States.AF.Hi = uint8(res)
Expand All @@ -934,7 +964,7 @@ func SysCallWriteRand(cpm *CPM) error {
ptr := cpm.CPU.States.DE.U16()

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

// Create a structure with the contents
fcbPtr := fcb.FromBytes(xxx)
Expand Down Expand Up @@ -965,7 +995,17 @@ func SysCallWriteRand(cpm *CPM) error {

// If the offset we're writing to is bigger than the file size then
// we need to add an appropriate amount of padding.
padding := fileSize - fpos
padding := fpos - fileSize

// Add logging of the result and details.
cpm.Logger.Debug("SysCallWriteRand",
slog.Int("dma", int(cpm.dma)),
slog.Int("fcb", int(ptr)),
slog.Int("padding", int(padding)),
slog.Int("handle", int(obj.handle.Fd())),
slog.Int("record_count", int(fcbPtr.RC)),
slog.Int("record", record),
slog.Int64("fpos", fpos))

for padding > 0 {
_, er := obj.handle.Write([]byte{0x00})
Expand Down
3 changes: 3 additions & 0 deletions fcb/fcb.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ import (
"strings"
)

// SIZE contains the size of the FCB structure
var SIZE = 36

// FCB is a structure which is used to hold details about file entries, although
// later versions of CP/M support directories we do not.
//
Expand Down
4 changes: 4 additions & 0 deletions fcb/fcb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func TestCopy(t *testing.T) {
f1.R0 = 'R'
f1.R1 = '0'
f1.R2 = '1'
f1.Cr = '*'
b := f1.AsBytes()

f2 := FromBytes(b)
Expand All @@ -37,6 +38,9 @@ func TestCopy(t *testing.T) {
if f2.S1 != 'S' {
t.Fatalf("copy failed")
}
if f2.Cr != '*' {
t.Fatalf("copy failed")
}
if f2.S2 != '?' {
t.Fatalf("copy failed")
}
Expand Down
2 changes: 1 addition & 1 deletion samples/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#
# The files we wish to generate.
#
all: cli-args.com create.com delete.com drive.com find.com ret.com user-num.com version.com
all: cli-args.com create.com delete.com drive.com find.com read.com ret.com user-num.com version.com write.com


#
Expand Down
Binary file added samples/read.com
Binary file not shown.

0 comments on commit 9365f43

Please sign in to comment.