Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow creating (empty) files. #16

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
38 changes: 37 additions & 1 deletion cpm/cpm_syscalls.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,10 @@ func SysCallReadChar(cpm *CPM) error {
}

// restore the state of the terminal to avoid mixing RAW/Cooked
term.Restore(int(os.Stdin.Fd()), oldState)
err = term.Restore(int(os.Stdin.Fd()), oldState)
if err != nil {
return fmt.Errorf("error restoring terminal state %s", err)
}

// Return the character
cpm.CPU.States.AF.Hi = b[0]
Expand Down Expand Up @@ -196,6 +199,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
Loading