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
+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