Initial Commit

This commit is contained in:
Kilian committed 2026-08-26 23:09:04 +02:00
commit 1f4390a42b
25 files changed
+13462

No files matched your search

+1673
View File
File diff suppressed because it is too large. Load diff
+14
View File
@@ -0,0 +1,14 @@
#include "io.h"
#include <stdio.h>
int putChar(uint8_t c) {
int _c = putchar(c);
fflush(stdout);
return _c;
}
int getChar(void) {
clearerr(stdin);
return getchar();
}
+83
View File
@@ -0,0 +1,83 @@
#include "cpu.h"
#include "ram.h"
#include "rom.h"
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>
static struct termios orig_termios;
void restore_terminal(void) { tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios); }
void set_nonblocking_input(void) {
struct termios raw;
tcgetattr(STDIN_FILENO, &orig_termios);
atexit(restore_terminal);
raw = orig_termios;
raw.c_lflag &= ~(ICANON | ECHO); // disable line buffering and echo
raw.c_cc[VMIN] = 0; // don't block waiting for input
raw.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &raw);
// make stdin reads non-blocking too
int flags = fcntl(STDIN_FILENO, F_GETFL, 0);
fcntl(STDIN_FILENO, F_SETFL, flags | O_NONBLOCK);
}
int main(void) {
uint8_t *ram = NULL;
CPU cpu;
set_nonblocking_input();
initRom(rom);
if (createRAM(&ram) != 0) {
printf("Could not create RAM\n");
return 1;
}
/* DEBUG */
FILE *fp = fopen("/Users/Kili2/Documents/Projekte/6502pc/6502_65C02_functional_tests/6502_functional_test.bin", "rb");
if (!fp) {
perror("fopen");
return 1;
}
fread(ram, 1, 64 * 1024, fp);
if (ferror(fp)) {
perror("fread");
fclose(fp);
return 1;
}
fclose(fp);
/* DEBUG END*/
initCPU(&cpu);
resetCPU(&cpu, rom);
while (1) {
//printf("\tPC: 0x%x\tOP: 0x%x\n", cpu.ProgrammeCounter, ram[cpu.ProgrammeCounter]);
useconds_t sleep = runCycle(&cpu, ram, rom);
if (sleep == 1) {
printf("opcode missing: 0x%X\n", ram[cpu.ProgrammeCounter - 1]);
return 1;
}
fflush(stdout);
usleep(sleep);
}
/* CPU WORK */
destroyRAM(ram);
return 0;
}
+44
View File
@@ -0,0 +1,44 @@
#include "memoryMap.h"
uint8_t readMemory(uint16_t address, uint8_t *ram, uint8_t *rom) {
if (address < RAM_SIZE || address >= IO_BASE + IO_SIZE) { // FIXME: Expanded for debug
return ram[address];
} else if (address < RAM_SIZE + IO_SIZE) {
uint16_t _address = address - RAM_SIZE;
switch (_address) {
case KEYBOARD_ADDRESS: {
return getChar();
}
default:
break;
}
return 0;
} else {
return rom[address - RAM_SIZE - IO_SIZE];
}
}
void writeMemory(uint16_t address, uint8_t value, uint8_t *ram, uint8_t *rom) {
if (address < RAM_SIZE) {
ram[address] = value;
} else if (address < RAM_SIZE + IO_SIZE || address >= IO_BASE + IO_SIZE) { // FIXME: Expanded for debug
uint16_t _address = address - RAM_SIZE;
switch (_address) {
case SCREEN_ADDRESS: {
putChar(value);
break;
}
default:
break;
}
} else {
rom[address - RAM_SIZE - IO_SIZE] = value;
}
}
uint8_t readStack(uint8_t address, uint8_t *ram) { return ram[0x100 + address]; }
void writeStack(uint8_t address, uint8_t value, uint8_t *ram) { ram[0x100 + address] = value; }
+12
View File
@@ -0,0 +1,12 @@
#include "ram.h"
#include <stdlib.h>
int createRAM(uint8_t **ram) {
*ram = (uint8_t *)malloc(RAM_SIZE);
if (*ram == NULL) return -1;
return 0;
}
void destroyRAM(uint8_t *ram) { free(ram); }
+17
View File
@@ -0,0 +1,17 @@
#include "rom.h"
uint8_t rom[ROM_SIZE] = {0};
void initRom(uint8_t *rom) {
rom[0] = 0xa9, // LDA
rom[1] = 'H', // H literal
rom[2] = 0x8d, // STA
rom[3] = (uint8_t)((IO_BASE + SCREEN_ADDRESS) & 0xFF), // Lower byte of screen
rom[4] = (uint8_t)((IO_BASE + SCREEN_ADDRESS) >> 8), // Upper byte of screen
rom[6] =
/* Reset Vector */
rom[0xFFFC - ROM_BASE] = 0x00, // Lower byte base of ROM
rom[0xFFFD - ROM_BASE] = 0x04, // Upper byte of base of ROM
rom[0xFFFE - ROM_BASE] = 0x00, // Lower byte base of ROM
rom[0xFFFF - ROM_BASE] = 0x00; // Upper byte of base of ROM
}