Start on FILE i/O

This commit is contained in:
Kilian committed 2026-09-28 14:44:11 +02:00
1 parent 7033104799
commit 03518c4937
22 files changed
+330 -127

No files matched your search

+1 -1
View File
@@ -1,6 +1,6 @@
#include "cpu.h"
#include "memoryMap.h"
#include "memory.h"
#include "opcodes.h"
#define COMBINE(H, L) ((H << 8) | L)
+63
View File
@@ -0,0 +1,63 @@
#include "file.h"
#include <errno.h>
#include <stdio.h>
// Up to 256 files can be open
FILE *files[256];
int openFile(int fd, const char *path) {
files[fd] = fopen(path, "r+");
if (files[fd] == NULL) {
return errno;
}
return 0;
}
int closeFile(int fd) {
int ret = fclose(files[fd]);
if (ret != 0) {
return errno;
}
return 0;
}
int readByte(const int fd, int *data) {
int status = 0;
int count = fread(&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 = 0x2;
} else if (eof != 0) {
status = 0x1;
} else {
status = 0x4;
}
}
return status;
}
int writeByte(const int fd, const int data) {
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 = 0x2;
} else if (eof != 0) {
status = 0x1;
} else {
status = 0x4;
}
}
return status;
}
+1 -1
View File
@@ -46,7 +46,7 @@ int main(void) {
printf("6502PC Emulator with MS Basic\n");
printf("CTRL + G to exit\n");
printf("WOZMON available, BASIC at DF6A\n");
printf("WOZMON available, BASIC at E032\n");
clock_gettime(CLOCK_MONOTONIC, &emulationStart);
+36 -1
View File
@@ -1,4 +1,4 @@
#include "memoryMap.h"
#include "memory.h"
uint8_t readMemory(uint16_t address, uint8_t *ram, uint8_t *rom) {
// RAM/ROM
@@ -16,10 +16,18 @@ uint8_t readMemory(uint16_t address, uint8_t *ram, uint8_t *rom) {
case KEYBOARD_ADDRESS: {
return getChar();
}
case ROM_BANKED_OUT: {
return ram[IO_BASE + ROM_BANKED_OUT];
}
case FILE_DATA: {
return ram[IO_BASE + FILE_DATA];
}
case FILE_STATUS: {
return ram[IO_BASE + FILE_STATUS];
}
default:
break;
}
@@ -55,6 +63,33 @@ void writeMemory(uint16_t address, uint8_t value, uint8_t *ram) {
ram[IO_BASE + ROM_BANKED_OUT] = value;
break;
case FILE_COMMAND: {
switch (ram[IO_BASE + FILE_COMMAND]) {
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]);
break;
}
case COMMAND_CLOSE: {
ram[IO_BASE + FILE_STATUS] = 0;
ram[IO_BASE + FILE_STATUS] = closeFile(ram[IO_BASE + FILE_DESCRIPTOR]);
break;
}
default:
break;
}
break;
}
case FILE_DESCRIPTOR: {
ram[IO_BASE + FILE_DESCRIPTOR] = value;
break;
}
case FILE_DATA: {
ram[IO_BASE + FILE_DATA] = value;
break;
}
default:
break;
}
+37 -37
View File
@@ -1,38 +1,38 @@
#include "io.h"
#include <stdio.h>
#include <stdlib.h>
int putChar(uint8_t c) {
int _c = putchar(c);
fflush(stdout);
return _c;
}
int getChar(void) {
clearerr(stdin);
int c = getchar();
// Specifically send FF for EOF to trigger echo cancellation
if (c == EOF) {
return 0xFF;
}
// BASIC expects CR always
if (c == '\n') {
return '\r';
}
// BASIC is uppercase only
if (c >= 'a' && c <= 'z') {
c = c - 'a' + 'A';
}
if (c == 0x07) {
exit(EXIT_SUCCESS);
}
return c;
#include "screen.h"
#include <stdio.h>
#include <stdlib.h>
int putChar(uint8_t c) {
int _c = putchar(c);
fflush(stdout);
return _c;
}
int getChar(void) {
clearerr(stdin);
int c = getchar();
// Specifically send FF for EOF to trigger echo cancellation
if (c == EOF) {
return 0xFF;
}
// BASIC expects CR always
if (c == '\n') {
return '\r';
}
// BASIC is uppercase only
if (c >= 'a' && c <= 'z') {
c = c - 'a' + 'A';
}
if (c == 0x07) {
exit(EXIT_SUCCESS);
}
return c;
}