1
0
mirror of https://github.com/jborza/emu6502.git synced 2024-11-25 03:34:40 +00:00
emu6502/emu6502.c

62 lines
1.5 KiB
C
Raw Normal View History

2019-04-13 20:00:37 +00:00
// emu6502.cpp : This file contains the 'main' function. Program execution begins and ends there.
//
#include <stdio.h>
2019-04-14 08:57:25 +00:00
#include <stdlib.h>
2019-04-14 10:39:56 +00:00
#include <memory.h>
2019-04-13 20:00:37 +00:00
#include "state.h"
#include "cpu.h"
2019-04-13 23:51:50 +00:00
#include "disassembler.h"
2019-04-14 08:57:25 +00:00
#include "opcodes.h"
2019-04-14 10:39:56 +00:00
#include "test6502.h"
2019-04-29 04:37:56 +00:00
#include <errno.h>
#include <direct.h>
2019-04-13 23:51:50 +00:00
2019-04-29 04:37:56 +00:00
#define NESTEST_SIZE 0x4000
#define NESTEST_DST 0xC000
#define MEMORY_SIZE 0xFFFF
byte* read_nestest() {
FILE* file = fopen("nestest\\nestest.bin", "rb");
if (!file) {
int err = errno;
exit(1);
}
2019-05-04 12:11:39 +00:00
static byte buffer[NESTEST_SIZE];
2019-04-29 04:37:56 +00:00
int read = fread(&buffer, sizeof(byte), NESTEST_SIZE, file);
2019-04-13 23:51:50 +00:00
fclose(file);
return buffer;
}
2019-05-04 12:11:39 +00:00
byte debug_flags_as_byte(State6502* state) {
byte flags_value = 0;
memcpy(&flags_value, &state->flags, sizeof(Flags));
return flags_value;
}
2019-04-29 04:37:56 +00:00
void run_nestest() {
2019-04-19 09:38:52 +00:00
State6502 state;
clear_state(&state);
2019-04-29 04:37:56 +00:00
state.memory = malloc(MEMORY_SIZE);
memset(state.memory, 0, MEMORY_SIZE);
byte* bin = read_nestest();
//const word TARGET = 0xC000;
memcpy(state.memory + NESTEST_DST, bin, NESTEST_SIZE);
memcpy(state.memory + 0x8000, bin, NESTEST_SIZE);
state.pc = NESTEST_DST;
2019-05-04 12:11:39 +00:00
//a little cheat to simulate probably a JSR and SEI at the beginning
state.sp = 0xfd;
state.flags.i = 1;
2019-04-29 04:37:56 +00:00
do{
2019-05-01 05:21:36 +00:00
char* dasm = disassemble_6502_to_string(state.memory, state.pc);
2019-05-04 12:11:39 +00:00
printf("%-50s A:%02X X:%02X Y:%02X P:%02X SP:%02X\n", dasm, state.a, state.x, state.y, debug_flags_as_byte(&state), state.sp);
2019-04-29 04:37:56 +00:00
emulate_6502_op(&state);
} while (state.flags.b != 1);
2019-04-19 09:38:52 +00:00
}
2019-04-13 20:00:37 +00:00
int main()
2019-04-19 09:38:52 +00:00
{
2019-05-04 12:11:39 +00:00
run_nestest();
//run_tests();
2019-04-14 11:24:02 +00:00
return 0;
2019-05-01 05:21:36 +00:00
}