Skip to content

Commit

Permalink
Issue #2352: Add support for fcntl
Browse files Browse the repository at this point in the history
  • Loading branch information
suborb committed Jul 16, 2023
1 parent 62610a1 commit 076dea5
Show file tree
Hide file tree
Showing 19 changed files with 601 additions and 8 deletions.
4 changes: 2 additions & 2 deletions lib/config/aquarius.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ CRT0 DESTDIR/lib/target/aquarius/classic/aquarius_crt0
# through to compiler, assembler etc as necessary
OPTIONS -O2 -SO2 -iquote. -DZ80 -DAQUARIUS -D__AQUARIUS__ -M -subtype=default -clib=default -Cc-standard-escape-chars

CLIB default -laquarius_clib -lndos -LDESTDIR/lib/clibs/z80
CLIB ansi -pragma-need=ansiterminal -D__CONIO_VT100 -laquarius_clib -lndos -LDESTDIR/lib/clibs/z80
CLIB default -laquarius_clib -LDESTDIR/lib/clibs/z80
CLIB ansi -pragma-need=ansiterminal -D__CONIO_VT100 -laquarius_clib -LDESTDIR/lib/clibs/z80

SUBTYPE none -startup=1
SUBTYPE default -Cz+aquarius -startup=1
Expand Down
47 changes: 43 additions & 4 deletions lib/target/aquarius/def/aqplus.def
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,51 @@ defc PORT_VCTRL = $e0
defc PORT_VPALSEL = $ea
defc PORT_VPALDATA = $eb


defc PORT_BANK0 = $f0
defc PORT_BANK1 = $f1
defc PORT_BANK2 = $f2
defc PORT_BANK3 = $f3





defc PORT_ESPCTRL = $f4
defc PORT_ESPDATA = $f5



defc ESPCMD_RESET = 0x01 ; Reset ESP
defc ESPCMD_OPEN = 0x10 ; Open / create file
defc ESPCMD_CLOSE = 0x11 ; Close open file
defc ESPCMD_READ = 0x12 ; Read from file
defc ESPCMD_WRITE = 0x13 ; Write to file
defc ESPCMD_SEEK = 0x14 ; Move read/write pointer
defc ESPCMD_TELL = 0x15 ; Get current read/write
defc ESPCMD_OPENDIR = 0x16 ; Open directory
defc ESPCMD_CLOSEDIR = 0x17 ; Close open directory
defc ESPCMD_READDIR = 0x18 ; Read from directory
defc ESPCMD_DELETE = 0x19 ; Remove file or directory
defc ESPCMD_RENAME = 0x1A ; Rename / move file or directory
defc ESPCMD_MKDIR = 0x1B ; Create directory
defc ESPCMD_CHDIR = 0x1C ; Change directory
defc ESPCMD_STAT = 0x1D ; Get file status
defc ESPCMD_GETCWD = 0x1E ; Get current working directory
defc ESPCMD_CLOSEALL = 0x1F ; Close any open file/directory descriptor

defc ERR_NOT_FOUND = -1 ; File / directory not found
defc ERR_TOO_MANY_OPEN = -2 ; Too many open files / directories
defc ERR_PARAM = -3 ; Invalid parameter
defc ERR_EOF = -4 ; End of file / directory
defc ERR_EXISTS = -5 ; File already exists
defc ERR_OTHER = -6 ; Other error
defc ERR_NO_DISK = -7 ; No disk
defc ERR_NOT_EMPTY = -8 ; Not empty


defc OPENF_RDONLY = 0x00 ; Open for reading only
defc OPENF_WRONLY = 0x01 ; Open for writing only
defc OPENF_RDWR = 0x02 ; Open for reading and writing
defc OPENF_ACCMODE = 0x03 ; Mask for above modes

defc OPENF_APPEND = 0x04 ; Append mode
defc OPENF_CREATE = 0x08 ; Create if non-existant
defc OPENF_TRUNC = 0x10 ; Truncate to zero length
defc OPENF_EXCL = 0x20 ; Error if already exists
1 change: 1 addition & 0 deletions libsrc/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -862,6 +862,7 @@ aquarius_clib.lib: $(TARGET_CLIB_OBJS)
@echo ''
$(call buildgeneric,aquarius,"text6 narrow")
$(MAKE) -C games TARGET=aquarius
$(MAKE) -C target/aquarius
TARGET=aquarius TYPE=z80 $(LIBLINKER) -DFORaquarius -DSTANDARDESCAPECHARS -x$(OUTPUT_DIRECTORY)/aquarius_clib.lib @$(TARGET_DIRECTORY)/aquarius/aquarius.lst

gfxaq48.lib: $(TARGET_CLIB_OBJS)
Expand Down
2 changes: 1 addition & 1 deletion libsrc/target/Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
include ../Make.config


SUBDIRS = ace c128 cpc cpm enterprise gb lm80c msx mtx nabu nc100 newbrain osca oz pps rex s1mp3 sos svi ticalc trs80 ts2068 tvc x1 z88 zx-common zx zx80 zx81 zxn
SUBDIRS = ace c128 cpc cpm enterprise gb lm80c msx mtx nabu nc100 newbrain osca oz pps rex s1mp3 sos svi ticalc trs80 ts2068 tvc x1 z88 zx-common zx zx80 zx81 zxn aquarius

CLEANDIRS = $(addsuffix -clean,$(SUBDIRS))

Expand Down
40 changes: 40 additions & 0 deletions libsrc/target/aquarius/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
include ../../Make.config

TARGET = aquarius

SUBDIRS = fcntl
CLEANDIRS = $(SUBDIRS:%=%-clean)

ASMFILES = $(wildcard *.asm)
OBJECTS = $(ASMFILES:.asm=.o)

all: dirs subdirs-all $(addprefix obj/$(TARGET)/,$(OBJECTS))


subdirs-all: $(SUBDIRS)

subdirs-clean: $(SUBDIRS_CLEAN)


clean: subdirs-clean
$(RM) -r obj
$(RM) zcc_opt.def *.err *.o
$(RM) */*.o
$(RM) */*/*.o

subdirs-clean: $(CLEANDIRS)

obj/$(TARGET)/%.o: %.asm
@$(ASSEMBLER) -DFOR$(TARGET) -I../.. -Oobj/$(TARGET) $^

dirs:
@mkdir -p obj/$(TARGET)

.PHONY: subdirs-all $(SUBDIRS) $(SUBDIRS_CLEAN)

$(SUBDIRS):
$(MAKE) -C $@ all

$(CLEANDIRS):
$(MAKE) -C $(@:%-clean=%) clean
$(RM) */*.o
12 changes: 11 additions & 1 deletion libsrc/target/aquarius/aquarius.lst
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,14 @@ target/aquarius/tape/tape_load_block_callee
@psg/ay/psg.lst
@psg/ay/vt2/psg_vt2.lst
@psg/ay/wyz/psg_wyz.lst

target/aquarius/fcntl/chdir
target/aquarius/fcntl/close
target/aquarius/fcntl/esp
target/aquarius/fcntl/mkdir
target/aquarius/fcntl/open
target/aquarius/fcntl/read
target/aquarius/fcntl/readbyte
target/aquarius/fcntl/remove
target/aquarius/fcntl/rename
target/aquarius/fcntl/write
target/aquarius/fcntl/writebyte
24 changes: 24 additions & 0 deletions libsrc/target/aquarius/fcntl/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#
# Makefile for +3 file support
#
# $Id: Makefile,v 1.9 2016-07-02 15:41:39 dom Exp $

include ../../../Make.config

CFILES = $(wildcard *.c)
AFILES = $(wildcard *.asm)
OBJECTS = $(CFILES:.c=.o) $(AFILES:.asm=.o)

all: $(OBJECTS)

%.o: %.c
$(ZCC) +aquarius $(CFLAGS) -c -o $@ $<

%.o: %.asm
$(ZCC) +aquarius $(CFLAGS) -c -o $@ $<


clean:
$(RM) *.o* zcc_opt.def $(OUTPUT_DIRECTORY)/p3.lib


31 changes: 31 additions & 0 deletions libsrc/target/aquarius/fcntl/chdir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


#include <fcntl.h>


int chdir(const char *d) __smallc __naked
{
#asm
INCLUDE "fcntl.def"
EXTERN asm_strlen

ld a,ESPCMD_CHDIR
call __esp_send_cmd
ld hl,2
add hl,sp
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
push hl
call asm_strlen
ld bc,hl
inc bc
pop hl
call __esp_send_bytes
call __esp_read_byte
ld l,a
ld h,0
ret
#endasm
}
20 changes: 20 additions & 0 deletions libsrc/target/aquarius/fcntl/close.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#include <fcntl.h>


int close(int fd) __smallc __naked
{
#asm
INCLUDE "fcntl.def"

ld a,ESPCMD_CLOSE
call __esp_send_cmd
ld hl,2
add hl,sp
ld a,(hl)
call __esp_send_byte
call __esp_read_byte
ld l,a
ld h,0
ret
#endasm
}
80 changes: 80 additions & 0 deletions libsrc/target/aquarius/fcntl/esp.asm
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@



SECTION code_clib

PUBLIC __esp_send_cmd
PUBLIC __esp_send_byte
PUBLIC __esp_send_bytes
PUBLIC __esp_read_bytes
PUBLIC __esp_read_byte

INCLUDE "target/aquarius/def/aqplus.def"

; Send byte = a
MACRO SENDBYTE
LOCAL l1
ex af,af
l1:
in a,(PORT_ESPCTRL)
and 2
jr nz,l1
ex af,af
out (PORT_ESPDATA),a
ENDM

MACRO READBYTE
LOCAL l1
ex af,af
l1:
in a,(PORT_ESPCTRL)
and 1
jr z,l1
ex af,af
in a,(PORT_ESPDATA)
ENDM

__esp_read_byte:
READBYTE
ret

__esp_send_byte:
SENDBYTE
ret

; Send a command
; a = command to send
__esp_send_cmd:
ex af,af
ld a,0x83
out (PORT_ESPCTRL),a
ex af,af
SENDBYTE
ret

; Send a block of data
; hl = block
; bc = count
__esp_send_bytes:
ld a,b
or c
ret z
ld a,(hl)
SENDBYTE
inc hl
dec bc
jr __esp_send_bytes

; Read a block of data
; hl = block
; bc = count
__esp_read_bytes:
ld a,b
or c
ret z
READBYTE
ld (hl),a
inc hl
dec bc
jr __esp_read_bytes

9 changes: 9 additions & 0 deletions libsrc/target/aquarius/fcntl/fcntl.def
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@


INCLUDE "target/aquarius/def/aqplus.def"

EXTERN __esp_send_cmd
EXTERN __esp_send_byte
EXTERN __esp_send_bytes
EXTERN __esp_read_byte
EXTERN __esp_read_bytes
31 changes: 31 additions & 0 deletions libsrc/target/aquarius/fcntl/mkdir.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@


#include <fcntl.h>
#include <sys/stat.h>

int mkdir(char *d, int mode) __smallc __naked
{
#asm
INCLUDE "fcntl.def"
EXTERN asm_strlen

ld a,ESPCMD_MKDIR
call __esp_send_cmd
ld hl,4
add hl,sp
ld a,(hl)
inc hl
ld h,(hl)
ld l,a
push hl
call asm_strlen
ld bc,hl
inc bc
pop hl
call __esp_send_bytes
call __esp_read_byte
ld l,a
ld h,0
ret
#endasm
}
Loading

0 comments on commit 076dea5

Please sign in to comment.