Skip to content

Commit

Permalink
Port atari open and close from cc65
Browse files Browse the repository at this point in the history
  • Loading branch information
mysterymath committed May 26, 2024
1 parent 721efaa commit 97022fb
Show file tree
Hide file tree
Showing 12 changed files with 803 additions and 33 deletions.
11 changes: 10 additions & 1 deletion mos-platform/atari8-common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,14 @@ target_link_libraries(atari8-common-crt0 PRIVATE common-asminc)
add_platform_library(atari8-common-c
putchar.c
getchar.c

close.s
fdtab.s
fdtable.s
findfreeiocb.s
getfd.s
open.s
oserror.s
)
target_include_directories(atari8-common-c SYSTEM BEFORE PUBLIC .)
target_include_directories(atari8-common-c BEFORE PUBLIC .)
target_link_libraries(atari8-common-c PRIVATE common-asminc)
38 changes: 38 additions & 0 deletions mos-platform/atari8-common/close.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
; Copyright 2024 LLVM-MOS Project
; Licensed under the Apache License, Version 2.0 with LLVM Exceptions.
; See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license
; information.

; Originally from cc65. Modified from original version.

;
; Christian Groessler, May-2000
;
; int __fastcall__ close(int fd);
;

.include "atari.inc"
.include "errno.inc"

.globl close

close:
jsr fdtoiocb_down ; get iocb index into X and decr. usage count
bmi inverr
bne ok ; not last one -> don't close yet
lda #CLOSE
sta ICCOM,x
jsr CIOV
bmi closerr
ok: ldx #0
stx __oserror ; clear system specific error code
txa
rts

inverr: lda #<EINVAL
jmp __directerrno

closerr:
tya
jmp __mappederrno

27 changes: 27 additions & 0 deletions mos-platform/atari8-common/fd.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
; Copyright 2024 LLVM-MOS Project
; Licensed under the Apache License, Version 2.0 with LLVM Exceptions.
; See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license
; information.

; Originally from cc65. Modified from original version.

; Christian Groessler, Oct-2000
;
; offsets and defines for fdtable (internal use only)
;

MAX_FD_INDEX = 12 ; max. # of open fds
MAX_FD_VAL = 8 ; we have 8 IOCBs

ft_entrylen = 4 ; length of table entry (it's not sufficient to change here!
; the code sometimes does two bit shifts to multiply/divide by
; this length)

ft_usa = 0 ; usage counter
ft_iocb = 1 ; iocb index (0,$10,$20,etc.), $ff for empty entry
ft_dev = 2 ; device of open iocb (0 - device not remembered, eg. filename specified)
ft_flag = 3 ; flags
; lower 3 bits: device number (for R: and D:)
; bit 3 - seeking supported by DOS/disk combination
; bit 4 - indicates a fd opened by the program as apposed to the
; inherited ones from program start (fd 0 to fd 2)
34 changes: 34 additions & 0 deletions mos-platform/atari8-common/fdtab.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
; Copyright 2024 LLVM-MOS Project
; Licensed under the Apache License, Version 2.0 with LLVM Exceptions.
; See https://github.com/llvm-mos/llvm-mos-sdk/blob/main/LICENSE for license
; information.

; Originally from cc65. Modified from original version.

;
; Christian Groessler, Oct-2000
; Daniel Serpell, Dec-2009
;
; the fdtable itself is defined here
;

.include "fd.inc"

.globl fd_table,fd_index

.data

fd_index: ; fd number is index into this table, entry's value specifies the fd_table entry
.byte 0,0,0 ; at start, three first files are stdin/stdout/stderr.
.space MAX_FD_INDEX-3,$ff

fd_table: ; each entry represents an open iocb
.byte 3,0,'E',0 ; system console, app starts with opened iocb #0 for E:
.byte 0,$ff,0,0
.byte 0,$ff,0,0
.byte 0,$ff,0,0
.byte 0,$ff,0,0
.byte 0,$ff,0,0
.byte 0,$ff,0,0
.byte 0,$ff,0,0

Loading

0 comments on commit 97022fb

Please sign in to comment.