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

WIP: [atari] fgetpos/fsetpos using NOTE/POINT #355

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions mos-platform/atari8-common/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,18 @@ target_link_libraries(atari8-common-crt0 PRIVATE common-asminc)
add_platform_library(atari8-common-c
putchar.c
getchar.c
update_diskpos.c

close.s
fdtab.s
fdtable.s
fdtoiocb.s
findfreeiocb.s
getfd.s
note.s
open.s
oserror.s
point.s
rwcommon.s
sysremove.s
write.s
Expand Down
42 changes: 42 additions & 0 deletions mos-platform/atari8-common/note.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
; 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.

; char __note (int fd, fpos_t *pos);

.include "atari.inc"
.include "imag.inc"

.globl __note

__note:
Copy link
Contributor

@mlund mlund Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add .section .text.__note,"ax",@progbits

jsr fdtoiocb
bmi error
tax
lda #NOTE
sta ICCOM,x
jsr CIOV
bmi error
ldy #0
lda ICAX5,x
sta (__rc2),y
iny
lda ICAX3,x
sta (__rc2),y
iny
lda ICAX4,x
sta (__rc2),y
iny
lda #0
sta (__rc2),y
rts

error:
ldy #3
lda #$ff
store_error:
sta (__rc2),y
dey
bpl store_error
rts
42 changes: 42 additions & 0 deletions mos-platform/atari8-common/point.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
; 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.

; char __point (int fd, const fpos_t *pos);

.include "atari.inc"
.include "imag.inc"

.globl __point

__point:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add .section .text.__point,"ax",@progbits

jsr fdtoiocb
bmi invalid
tax
ldy #3
lda (__rc2),y
bne invalid
lda #POINT
sta ICCOM,x
dey
lda (__rc2),y
sta ICAX4,x
dey
lda (__rc2),y
sta ICAX3,x
dey
lda (__rc2),y
sta ICAX5,x
jsr CIOV
bmi cioerr
lda #0
rts

invalid:
lda #<EINVAL
jmp __directerrno

cioerr:
tya
jmp __mappederrno
8 changes: 8 additions & 0 deletions mos-platform/atari8-common/update_diskpos.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#include <stdio.h>
#include <__stdio-internal.h>

char __note(int fd, fpos_t *pos);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Annotate function with __attribute__((leaf))


void __update_diskpos(FILE *stream) {
__note(stream->handle, &stream->diskpos);
}
22 changes: 7 additions & 15 deletions mos-platform/common/c/stdio-full.c
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdbool.h>
Copy link
Contributor

@mlund mlund Aug 17, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

stdbool.h is coming in #357.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We do already have <stdbool.h>; it just comes from clang, not the SDK.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include <__stdio-internal.h>

// Flags for representing mode (see fopen()). Note these must fit the same
// status field as the _IO?BF flags in <stdio.h> and the internal flags below.
#define FREAD (1u << 3)
Expand All @@ -37,20 +38,6 @@
// File associated with stream should be remove()d on closing (tmpfile()).
#define DELONCLOSE (1u << 14)

struct _FILE {
signed char handle; // OS file handle
char *buffer; // Pointer to buffer memory
size_t bufsize; // Size of buffer
size_t bufidx; // Index of current position in buffer
size_t bufend; // Index of last pre-read character in buffer
fpos_t pos; // Offset and multibyte parsing state
char ungetc_buf; // ungetc() buffer
bool ungetc_buf_full; // Number of ungetc()'ed characters
unsigned status; // Status flags; see above
char *filename; // Name the current stream has been opened with
FILE *next; // Pointer to next struct (internal)
};

// Buffer one-two lines; write() implementations have constant overhead, and
// buffering amortizes it.
static char serr_buf[80];
Expand Down Expand Up @@ -130,6 +117,9 @@ static unsigned filemode(const char *const mode) {
return rc;
}

__attribute__((weak)) void __update_diskpos(FILE *stream) {
}

static FILE *init_file(FILE *stream) {
FILE *rc = stream;

Expand Down Expand Up @@ -563,6 +553,8 @@ static int prep_read(FILE *stream) {
}

static int fill_buffer(FILE *stream) {
__update_diskpos(stream);

/* No need to handle buffers > INT_MAX, as PDCLib doesn't allow them */
int rc = read(stream->handle, stream->buffer, stream->bufsize);

Expand Down
24 changes: 24 additions & 0 deletions mos-platform/common/include/__stdio-internal.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef __STDIO_INTERNAL_H_
#define __STDIO_INTERNAL_H_

#include <stdbool.h>
#include <stdio.h>

struct _FILE {
signed char handle; // OS file handle
char *buffer; // Pointer to buffer memory
size_t bufsize; // Size of buffer
size_t bufidx; // Index of current position in buffer
size_t bufend; // Index of last pre-read character in buffer
fpos_t pos; // Offset and multibyte parsing state
char ungetc_buf; // ungetc() buffer
bool ungetc_buf_full; // Number of ungetc()'ed characters
unsigned status; // Status flags; see above
char *filename; // Name the current stream has been opened with
fpos_t diskpos; // Atari NOTE position
FILE *next; // Pointer to next struct (internal)
};

void __update_diskpos(FILE *stream);

#endif // __STDIO_INTERNAL_H_