Fix BASIC

Fix cycle waiting
Fix warnings
This commit is contained in:
Kilian committed 2026-09-27 04:48:39 +02:00
1 parent 8101cc3f62
commit a5e30a7b28
14 files changed
+114 -61

No files matched your search

+3 -3
View File
@@ -58,10 +58,10 @@ static void doSBC(CPU *cpu, uint8_t value) {
}
void initCPU(CPU *cpu) {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wmissing-field-initializers"
DIAG_PUSH();
DIAG_IGNORE("-Wmissing-field-initializers");
*cpu = (CPU){0x0000, 0xFF};
#pragma clang diagnostic pop
DIAG_POP();
}
void resetCPU(CPU *cpu, uint8_t *rom) {
+41 -38
View File
@@ -1,18 +1,23 @@
#include "cpu.h"
#include "ram.h"
#include "rom.h"
#include <fcntl.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <termios.h>
#include <time.h>
#include <unistd.h>
#define MACHINE_SPEED_HZ 1e6 // 1 MHz;
#include "cpu.h"
#include "ram.h"
#include "rom.h"
static struct termios orig_termios;
static struct timespec sleepTime;
#define MACHINE_SPEED_HZ 1000000ULL // 1 MHz;
#define SYNC_CYCLES 100
struct termios orig_termios;
struct timespec emulationStart;
uint64_t totalCycles = 0;
uint64_t lastSyncCycles = 0;
void restore_terminal(void) { tcsetattr(STDIN_FILENO, TCSANOW, &orig_termios); }
@@ -36,11 +41,13 @@ void set_nonblocking_input(void) {
}
int main(void) {
uint8_t *ram = NULL;
CPU cpu;
printf("6502PC Emulator with MS Basic\n");
printf("CTRL + G to exit\n");
uint8_t *ram = NULL;
CPU cpu;
clock_gettime(CLOCK_MONOTONIC, &emulationStart);
set_nonblocking_input();
@@ -51,44 +58,40 @@ int main(void) {
exit(EXIT_FAILURE);
}
/* DEBUG */
// FILE *fp = fopen("/Users/Kili2/Documents/Projekte/6502pc/6502_65C02_functional_tests/6502_functional_test.bin", "rb");
// FILE *fp = fopen("/Users/Kili2/Documents/Projekte/6502pc/6502_65C02_functional_tests/6502_decimal_test.bin", "rb");
// FILE *fp = fopen("/mnt/c/Users/llego/source/repos/6502PC/6502_65C02_functional_tests/6502_functional_test.bin", "rb");
// FILE *fp = fopen("/mnt/c/Users/llego/source/repos/6502PC/6502_65C02_functional_tests/6502_decimal_test.bin", "rb");
/*
if (!fp) {
perror("fopen");
exit(EXIT_FAILURE);
}
fread(ram, 1, RAM_SIZE, fp);
if (ferror(fp)) {
perror("fread");
fclose(fp);
exit(EXIT_FAILURE);
}
fclose(fp);
*/
/* DEBUG END*/
initCPU(&cpu);
resetCPU(&cpu, rom);
while (1) {
uint8_t sleep = runCycle(&cpu, ram, rom);
if (sleep == 1) {
uint8_t cycles = runCycle(&cpu, ram, rom);
totalCycles += cycles;
// No 1 cycle instruction exists
if (cycles == 1) {
printf("Illegal opcode $%02X at $%04X "
"A=$%02X X=$%02X Y=$%02X SP=$%02X\n",
ram[cpu.ProgrammeCounter - 1], cpu.ProgrammeCounter - 1, cpu.Accumulator, cpu.X, cpu.Y, cpu.StackPointer);
exit(EXIT_FAILURE);
}
sleepTime.tv_sec = 0;
sleepTime.tv_nsec = sleep * (1000 / MACHINE_SPEED_HZ);
nanosleep(&sleepTime, NULL);
// Sleep if batch is full
if (totalCycles - lastSyncCycles >= SYNC_CYCLES) {
struct timespec now;
clock_gettime(CLOCK_MONOTONIC, &now);
uint64_t elapsed_ns =
(uint64_t)(now.tv_sec - emulationStart.tv_sec) * 1000000000ULL + (uint64_t)(now.tv_nsec - emulationStart.tv_nsec);
uint64_t target_ns = totalCycles * 1000000000ULL / MACHINE_SPEED_HZ;
if (target_ns > elapsed_ns) {
uint64_t wait_ns = target_ns - elapsed_ns;
struct timespec wait = {.tv_sec = wait_ns / 1000000000ULL, .tv_nsec = wait_ns % 1000000000ULL};
nanosleep(&wait, NULL);
}
lastSyncCycles = totalCycles;
}
}
destroyRAM(ram);
+5 -2
View File
@@ -1,13 +1,16 @@
#include "memoryMap.h"
uint8_t readMemory(uint16_t address, uint8_t *ram, uint8_t *rom) {
if ((address < RAM_SIZE && address < IO_BASE) || address >= ROM_BASE) {
// RAM/ROM
if (address < IO_BASE || address >= ROM_BASE) {
if (address >= ROM_BASE && ram[IO_BASE + ROM_BANKED_OUT] == 0) {
return rom[address - ROM_BASE];
}
return ram[address];
} else {
}
// I/O
else {
uint16_t _address = address - IO_BASE;
switch (_address) {
case KEYBOARD_ADDRESS: {
+2 -2
View File
@@ -1,9 +1,9 @@
#include "ram.h"
#include "io.h"
#include <stdlib.h>
#include "io.h"
int createRAM(uint8_t **ram) {
*ram = (uint8_t *)malloc(RAM_SIZE);
+6 -7
View File
@@ -1,20 +1,23 @@
#include "rom.h"
#include "io.h"
#include <stdio.h>
#include "io.h"
uint8_t rom[ROM_SIZE] = {0};
void initRom(uint8_t *rom) {
FILE *fp = fopen("/mnt/c/Users/llego/source/repos/6502PC/msbasic/tmp/kilian.bin", "rb");
FILE *fp = fopen("msbasic/tmp/kilian.bin", "rb");
if (!fp) {
perror("fopen");
return;
}
DIAG_PUSH();
DIAG_IGNORE("-Wunused-result");
fread(rom, 1, ROM_SIZE, fp);
DIAG_POP();
if (ferror(fp)) {
perror("fread");
fclose(fp);
@@ -22,8 +25,4 @@ void initRom(uint8_t *rom) {
}
fclose(fp);
/* Reset Vector, BASIC at DF02 */
//rom[0xFFFC - ROM_BASE] = 0x02; // Lower byte base of ROM
//rom[0xFFFD - ROM_BASE] = 0xDF; // Upper byte of base of ROM
}