diff --git a/CMakelists.txt b/CMakelists.txt index 1130a01..5abe097 100644 --- a/CMakelists.txt +++ b/CMakelists.txt @@ -27,7 +27,7 @@ target_sources(${CMAKE_PROJECT_NAME} PRIVATE src/ram.c src/cpu.c src/rom.c - src/memory.c + src/mem.c src/file.c src/screen.c ) diff --git a/inc/file.h b/inc/file.h index ebacf7e..11c0e26 100644 --- a/inc/file.h +++ b/inc/file.h @@ -1,6 +1,10 @@ #ifndef FILE_H #define FILE_H +#define STATUS_ERROR 1 +#define STATUS_EOF 2 +#define STATUS_READY 4 + #define COMMAND_OPEN 1 #define COMMAND_CLOSE 2 #define COMMAND_SELECT_IN 3 diff --git a/inc/io.h b/inc/io.h index b3e1bf7..8ce3e15 100644 --- a/inc/io.h +++ b/inc/io.h @@ -11,7 +11,8 @@ #define ROM_BANKED_OUT 2 #define FILE_COMMAND 3 #define FILE_DESCRIPTOR 4 -#define FILE_DATA 5 // two bytes, for pointer to string +#define FILE_DATA 5 // data, low byte of string pointer +#define FILE_DATA_HIGH 6 // high byte of string pointer #define FILE_STATUS 7 #endif // IO_H \ No newline at end of file diff --git a/inc/memory.h b/inc/mem.h similarity index 88% rename from inc/memory.h rename to inc/mem.h index c5ad49c..0e18873 100644 --- a/inc/memory.h +++ b/inc/mem.h @@ -1,5 +1,5 @@ -#ifndef MEMORY_H -#define MEMORY_H +#ifndef MEM_H +#define MEM_H #include diff --git a/rom/extra.s b/rom/extra.s index 9f9b9cc..1be94ba 100644 --- a/rom/extra.s +++ b/rom/extra.s @@ -29,6 +29,7 @@ CHRIN: lda IO_KBD bmi @end jsr CHROUT ; echo + rts @file: lda IO_COMMAND_READ sta IO_FILE_COMMAND @@ -41,11 +42,14 @@ CHRIN: rts CHROUT: + pha lda CURDVC bne @file + pla sta IO_SCREEN rts @file: + pla sta IO_FILE_DATA lda IO_COMMAND_WRITE sta IO_FILE_COMMAND @@ -84,15 +88,28 @@ OPEN: lda IO_FILE_STATUS and #FILE_STATUS_READY beq @wait - lda #FILE_STATUS_READY - and #$01 + lda IO_FILE_STATUS + and #FILE_STATUS_ERROR bne @error rts @error: - jmp IQERR + jmp IQERR CLOSE: + jsr GETBYT + stx IO_FILE_DESCRIPTOR + lda #IO_COMMAND_CLOSE + sta IO_FILE_COMMAND +@wait: + lda IO_FILE_STATUS + and #FILE_STATUS_READY + beq @wait + lda IO_FILE_STATUS + and #FILE_STATUS_ERROR + bne @error rts +@error: + jmp IQERR ; Default diff --git a/rom/init.s b/rom/init.s index e294e8e..365f9be 100644 --- a/rom/init.s +++ b/rom/init.s @@ -7,6 +7,8 @@ PR_WRITTEN_BY: jsr STROUT COLD_START: + lda #$00 + sta CURDVC ldx #$FF stx CURLIN+1 ldx #$FB diff --git a/rom/zeropage.s b/rom/zeropage.s index 67c9e9d..13ebf11 100644 --- a/rom/zeropage.s +++ b/rom/zeropage.s @@ -149,11 +149,11 @@ SGNCPR = STRNG1 FACEXTENSION = STRNG1+1 STRNG2: .res 2 +CURDVC: + .res 1 +FILE_NAME_BUFFER = $0300 CHRGET: TXTPTR = <(GENERIC_TXTPTR-GENERIC_CHRGET + CHRGET) CHRGOT = <(GENERIC_CHRGOT-GENERIC_CHRGET + CHRGET) CHRGOT2 = <(GENERIC_CHRGOT2-GENERIC_CHRGET + CHRGET) -RNDSEED = <(GENERIC_RNDSEED-GENERIC_CHRGET + CHRGET) -CURDVC: - .res 1 -FILE_NAME_BUFFER = $0300 \ No newline at end of file +RNDSEED = <(GENERIC_RNDSEED-GENERIC_CHRGET + CHRGET) \ No newline at end of file diff --git a/src/cpu.c b/src/cpu.c index 5baff85..e08b998 100644 --- a/src/cpu.c +++ b/src/cpu.c @@ -1,6 +1,6 @@ #include "cpu.h" -#include "memory.h" +#include "mem.h" #include "opcodes.h" #define COMBINE(H, L) ((H << 8) | L) diff --git a/src/file.c b/src/file.c index 4c92210..9ad3a0e 100644 --- a/src/file.c +++ b/src/file.c @@ -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; } \ No newline at end of file diff --git a/src/memory.c b/src/mem.c similarity index 83% rename from src/memory.c rename to src/mem.c index a1583d2..375e1d4 100644 --- a/src/memory.c +++ b/src/mem.c @@ -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;