Fix BASIC
Fix cycle waiting Fix warnings
This commit is contained in:
1 parent
8101cc3f62
commit
a5e30a7b28
14 files changed
+114
-61
No files matched your search
+41
-38
@@ -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);
|
||||
|
||||
Reference in new issue
Block a user