I/O fixed and tested
This commit is contained in:
1 parent
a0cabac399
commit
e6a2af6f8a
4 files changed
+27
-22
No files matched your search
+10
-4
@@ -22,6 +22,8 @@ CHRIN:
|
||||
bne @file
|
||||
lda IO_KBD
|
||||
bmi @end
|
||||
cmp #LF
|
||||
beq @end
|
||||
jsr CHROUT ; echo
|
||||
rts
|
||||
@file:
|
||||
@@ -54,6 +56,7 @@ CHROUT:
|
||||
sta IO_FILE_DESCRIPTOR
|
||||
pla
|
||||
sta IO_FILE_DATA
|
||||
pha
|
||||
lda #IO_COMMAND_WRITE
|
||||
sta IO_FILE_COMMAND
|
||||
@wait:
|
||||
@@ -62,10 +65,12 @@ CHROUT:
|
||||
beq @wait
|
||||
lda IO_FILE_STATUS
|
||||
and #FILE_STATUS_ERROR
|
||||
beq :+
|
||||
jmp IQERR
|
||||
:
|
||||
bne @error
|
||||
pla
|
||||
rts
|
||||
@error:
|
||||
pla
|
||||
jmp IQERR
|
||||
|
||||
OPEN:
|
||||
jsr GETBYT
|
||||
@@ -132,9 +137,10 @@ CLOSE:
|
||||
lda IO_FILE_STATUS
|
||||
and #FILE_STATUS_READY
|
||||
beq @wait
|
||||
lda IO_FILE_STATUS
|
||||
lda IO_FILE_STATUS
|
||||
and #FILE_STATUS_ERROR
|
||||
bne @error
|
||||
jsr CLRCH
|
||||
rts
|
||||
@error:
|
||||
jmp IQERR
|
||||
|
||||
+2
-1
@@ -47,8 +47,9 @@ L29B9:
|
||||
bne L29DD
|
||||
|
||||
LFDO:
|
||||
lda #LF
|
||||
lda #$00
|
||||
sta POSX
|
||||
lda #LF
|
||||
jsr OUTDO
|
||||
|
||||
PRINTNULLS:
|
||||
|
||||
+13
-8
@@ -37,6 +37,10 @@ int closeFile(int fd) {
|
||||
}
|
||||
|
||||
int readByte(const int fd, uint8_t *data) {
|
||||
if (files[fd] == NULL) {
|
||||
return STATUS_READY | STATUS_ERROR;
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
int count = fread(data, 1, 1, files[fd]);
|
||||
// Error or EOF
|
||||
@@ -54,17 +58,18 @@ int readByte(const int fd, uint8_t *data) {
|
||||
}
|
||||
|
||||
int writeByte(const int fd, const uint8_t data) {
|
||||
if (files[fd] == NULL) {
|
||||
return STATUS_READY | STATUS_ERROR;
|
||||
}
|
||||
|
||||
int status = 0;
|
||||
int count = fwrite(&data, 1, 1, files[fd]);
|
||||
// Error or EOF
|
||||
if (count != 1) {
|
||||
int error = ferror(files[fd]);
|
||||
int eof = feof(files[fd]);
|
||||
if (error != 0) {
|
||||
status = STATUS_ERROR;
|
||||
} else if (eof != 0) {
|
||||
status = STATUS_EOF;
|
||||
}
|
||||
if (count != 1 && ferror(files[fd]) != 0) {
|
||||
return STATUS_READY | STATUS_ERROR;
|
||||
}
|
||||
if (fflush(files[fd]) != 0) {
|
||||
return STATUS_READY | STATUS_ERROR;
|
||||
}
|
||||
|
||||
return STATUS_READY | status;
|
||||
|
||||
@@ -64,43 +64,36 @@ void writeMemory(uint16_t address, uint8_t value, uint8_t *ram) {
|
||||
break;
|
||||
|
||||
case FILE_COMMAND: {
|
||||
ram[IO_BASE + FILE_STATUS] = 0;
|
||||
|
||||
switch (value) {
|
||||
case COMMAND_OPEN_R: {
|
||||
ram[IO_BASE + FILE_STATUS] = 0;
|
||||
|
||||
uint16_t fileAddress = ram[IO_BASE + FILE_DATA] | ((uint16_t)ram[IO_BASE + FILE_DATA_HIGH] << 8);
|
||||
|
||||
ram[IO_BASE + FILE_STATUS] = openFile(ram[IO_BASE + FILE_DESCRIPTOR], (const char *)&ram[fileAddress], "r");
|
||||
break;
|
||||
}
|
||||
case COMMAND_OPEN_W: {
|
||||
ram[IO_BASE + FILE_STATUS] = 0;
|
||||
|
||||
uint16_t fileAddress = ram[IO_BASE + FILE_DATA] | ((uint16_t)ram[IO_BASE + FILE_DATA_HIGH] << 8);
|
||||
|
||||
ram[IO_BASE + FILE_STATUS] = openFile(ram[IO_BASE + FILE_DESCRIPTOR], (const char *)&ram[fileAddress], "w+");
|
||||
break;
|
||||
}
|
||||
case COMMAND_OPEN_A: {
|
||||
ram[IO_BASE + FILE_STATUS] = 0;
|
||||
|
||||
uint16_t fileAddress = ram[IO_BASE + FILE_DATA] | ((uint16_t)ram[IO_BASE + FILE_DATA_HIGH] << 8);
|
||||
|
||||
ram[IO_BASE + FILE_STATUS] = openFile(ram[IO_BASE + FILE_DESCRIPTOR], (const char *)&ram[fileAddress], "a+");
|
||||
break;
|
||||
}
|
||||
case COMMAND_CLOSE: {
|
||||
ram[IO_BASE + FILE_STATUS] = 0;
|
||||
ram[IO_BASE + FILE_STATUS] = closeFile(ram[IO_BASE + FILE_DESCRIPTOR]);
|
||||
break;
|
||||
}
|
||||
case COMMAND_READ: {
|
||||
ram[IO_BASE + FILE_STATUS] = 0;
|
||||
ram[IO_BASE + FILE_STATUS] = readByte(ram[IO_BASE + FILE_DESCRIPTOR], &ram[IO_BASE + FILE_DATA]);
|
||||
break;
|
||||
}
|
||||
case COMMAND_WRITE: {
|
||||
ram[IO_BASE + FILE_STATUS] = 0;
|
||||
ram[IO_BASE + FILE_STATUS] = writeByte(ram[IO_BASE + FILE_DESCRIPTOR], ram[IO_BASE + FILE_DATA]);
|
||||
break;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user