Fixed Open/Close

This commit is contained in:
Kilian committed 2026-09-28 15:58:39 +02:00
1 parent 03518c4937
commit 566db4a550
10 files changed
+72 -33

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
#include "cpu.h"
#include "memory.h"
#include "mem.h"
#include "opcodes.h"
#define COMBINE(H, L) ((H << 8) | L)
+26 -18
View File
@@ -7,21 +7,33 @@
FILE *files[256];
int openFile(int fd, const char *path) {
files[fd] = fopen(path, "r+");
if (files[fd] == NULL) {
return errno;
if (files[fd] != NULL) {
return STATUS_READY | STATUS_ERROR;
}
return 0;
files[fd] = fopen(path, "w+");
if (files[fd] == NULL) {
return STATUS_READY | STATUS_ERROR;
}
return STATUS_READY;
}
int closeFile(int fd) {
int ret = fclose(files[fd]);
if (ret != 0) {
return errno;
FILE *f = files[fd];
if (f == NULL) {
return STATUS_READY | STATUS_ERROR;
}
return 0;
int ret = fclose(files[fd]);
if (ret != 0) {
return STATUS_READY | STATUS_ERROR;
}
files[fd] = NULL;
return STATUS_READY;
}
int readByte(const int fd, int *data) {
@@ -32,15 +44,13 @@ int readByte(const int fd, int *data) {
int error = ferror(files[fd]);
int eof = feof(files[fd]);
if (error != 0) {
status = 0x2;
status = STATUS_ERROR;
} else if (eof != 0) {
status = 0x1;
} else {
status = 0x4;
status = STATUS_EOF;
}
}
return status;
return STATUS_READY | status;
}
int writeByte(const int fd, const int data) {
@@ -51,13 +61,11 @@ int writeByte(const int fd, const int data) {
int error = ferror(files[fd]);
int eof = feof(files[fd]);
if (error != 0) {
status = 0x2;
status = STATUS_ERROR;
} else if (eof != 0) {
status = 0x1;
} else {
status = 0x4;
status = STATUS_EOF;
}
}
return status;
return STATUS_READY | status;
}
+10 -3
View File
@@ -1,4 +1,4 @@
#include "memory.h"
#include "mem.h"
uint8_t readMemory(uint16_t address, uint8_t *ram, uint8_t *rom) {
// RAM/ROM
@@ -64,10 +64,13 @@ void writeMemory(uint16_t address, uint8_t value, uint8_t *ram) {
break;
case FILE_COMMAND: {
switch (ram[IO_BASE + FILE_COMMAND]) {
switch (value) {
case COMMAND_OPEN: {
ram[IO_BASE + FILE_STATUS] = 0;
ram[IO_BASE + FILE_STATUS] = openFile(ram[IO_BASE + FILE_DESCRIPTOR], (const char *)&ram[IO_BASE + FILE_DATA]);
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]);
break;
}
case COMMAND_CLOSE: {
@@ -89,6 +92,10 @@ void writeMemory(uint16_t address, uint8_t value, uint8_t *ram) {
ram[IO_BASE + FILE_DATA] = value;
break;
}
case FILE_DATA_HIGH: {
ram[IO_BASE + FILE_DATA_HIGH] = value;
break;
}
default:
break;