Skip to content

Commit

Permalink
Allow creating (empty) files.
Browse files Browse the repository at this point in the history
This pull-request closes #11 by allowing files to be created.
  • Loading branch information
skx committed Apr 14, 2024
1 parent eb49bce commit 9d0ac51
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 1 deletion.
4 changes: 4 additions & 0 deletions cpm/cpm.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,10 @@ func New(filename string, logger *slog.Logger) *CPM {
Desc: "F_SNEXT",
Handler: SysCallFindNext,
}
sys[22] = CPMHandler{
Desc: "F_MAKE",
Handler: SysCallMakeFile,
}
sys[25] = CPMHandler{
Desc: "DRV_GET",
Handler: SysCallDriveGet,
Expand Down
33 changes: 33 additions & 0 deletions cpm/cpm_syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,39 @@ func SysCallFindNext(cpm *CPM) error {
return nil
}

// SysCallMakeFile creates the file named in the FCB given in DE
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)

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

// Get the name
name := fcbPtr.GetName()
ext := fcbPtr.GetType()

fileName := name
if ext != "" && ext != " " {
fileName += "."
fileName += ext
}

// Create the file
file, err := os.OpenFile(fileName, os.O_RDONLY|os.O_CREATE, 0644)
if err != nil {
return err
}
err = file.Close()
if err != nil {
return err
}

return nil
}

// SysCallDriveGet returns the number of the active drive.
func SysCallDriveGet(cpm *CPM) error {
cpm.CPU.States.AF.Hi = cpm.currentDrive
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 drive.com find.com ret.com user-num.com
all: cli-args.com create.com drive.com find.com ret.com user-num.com


#
Expand Down
Binary file added samples/create.com
Binary file not shown.
52 changes: 52 additions & 0 deletions samples/create.z80
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
;; create.asm - Create the named file
;;

FCB1: EQU 0x5C

BDOS_ENTRY_POINT: EQU 5

BDOS_OUTPUT_STRING: EQU 9
BDOS_MAKE_FILE: EQU 22

;;
;; CP/M programs start at 0x100.
;;
ORG 100H

;; The FCB will be populated with the pattern/first argument,
;; if the first character of that region is a space-character
;; then we've got nothing to search for.
ld a, (FCB1 + 1)
cp 0x20 ; 0x20 = 32 == SPACE
jp nz, got_argument ; Not a space, so we can proceed

;;
;; No argument, so show the error and exit
;;
ld de, usage_message
ld c, BDOS_OUTPUT_STRING
call BDOS_ENTRY_POINT
jr exit_fn

got_argument:
LD DE, FCB1
LD C, 22
CALL BDOS_ENTRY_POINT

exit_fn:
;; exit
LD C,0x00
CALL BDOS_ENTRY_POINT


;;;
;;; The message displayed if no command-line argument was present.
;;;
usage_message:
db "Usage: CREATE FILENAME.EXT"

;; note fall-through here :)
newline:
db 0xa, 0xd, "$"

END

0 comments on commit 9d0ac51

Please sign in to comment.