104 lines
2.3 KiB
C
104 lines
2.3 KiB
C
#include "../include/cpu.h"
|
|
#include "cpu.h"
|
|
#include "memory.h"
|
|
|
|
/*
|
|
* =====================================================================================
|
|
*
|
|
* Filename: cpu.c
|
|
*
|
|
* Description: 6502 CPU emulator
|
|
*
|
|
* Version: 1.0
|
|
* Created: 2023-09-21 10:10:26 PM
|
|
* Revision: none
|
|
* Compiler: gcc
|
|
*
|
|
* Author: William Nolin,
|
|
* Organization:
|
|
*
|
|
* =====================================================================================
|
|
*/
|
|
|
|
CpuRegisters registers;
|
|
Mapper mapper;
|
|
|
|
void cpu_init() {
|
|
registers.program_counter = 0x0000;
|
|
registers.stack_pointer = 0xff;
|
|
registers.accumulator = 0x00;
|
|
registers.x = 0x00;
|
|
registers.y = 0x00;
|
|
registers.status = 0x00;
|
|
}
|
|
|
|
// === Registers ===
|
|
CpuRegisters* cpu_get_registers() {
|
|
return ®isters;
|
|
}
|
|
|
|
byte cpu_get_flag(byte mask) {
|
|
return registers.status & mask;
|
|
}
|
|
|
|
void cpu_set_flag(bool set, byte mask) {
|
|
if (set) {
|
|
registers.status |= mask;
|
|
} else {
|
|
registers.status &= ~mask;
|
|
}
|
|
}
|
|
|
|
// === Memory ===
|
|
byte cpu_get_next_byte() {
|
|
byte next_byte = mem_get_byte(&mapper, registers.program_counter);
|
|
registers.program_counter++;
|
|
return next_byte;
|
|
}
|
|
|
|
word cpu_get_next_word() {
|
|
word next_word = mem_get_word(&mapper, registers.program_counter);
|
|
registers.program_counter += 2;
|
|
return next_word;
|
|
}
|
|
|
|
byte cpu_peek_byte(address addr) {
|
|
return mem_get_byte(&mapper, addr);
|
|
}
|
|
|
|
word cpu_peek_word(address addr) {
|
|
return mem_get_word(&mapper, addr);
|
|
}
|
|
|
|
void cpu_push_byte(byte value, address addr) {
|
|
mem_set_byte(&mapper, addr, value);
|
|
}
|
|
|
|
// === Stack ===
|
|
void cpu_stack_push(byte value) {
|
|
address mem_addr = CPU_STACK_ADDR | registers.stack_pointer;
|
|
cpu_push_byte(value, mem_addr);
|
|
registers.stack_pointer--;
|
|
}
|
|
|
|
byte cpu_stack_pop() {
|
|
address mem_addr = CPU_STACK_ADDR | registers.stack_pointer;
|
|
byte value = cpu_peek_byte(mem_addr);
|
|
registers.stack_pointer++;
|
|
return value;
|
|
}
|
|
|
|
void cpu_stack_push_context() {
|
|
cpu_stack_push(registers.program_counter >> 8);
|
|
cpu_stack_push(registers.program_counter & 0xff);
|
|
cpu_stack_push(registers.status);
|
|
}
|
|
|
|
void cpu_stack_pop_context() {
|
|
registers.status = cpu_stack_pop();
|
|
|
|
byte lo = cpu_stack_pop();
|
|
address pc = cpu_stack_pop() << 8;
|
|
pc += lo;
|
|
registers.program_counter = pc;
|
|
} |