PancakeNESEmu
A homebrewed NES Emulator written in C
Loading...
Searching...
No Matches
cpu.h File Reference
#include "core/mem.h"
#include "core/types.h"
#include "macros.h"
Include dependency graph for cpu.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  CPU
 This structure will be used to represent the state of the Central Processing Unit (CPU) of our emulator. This Virtual CPU emulates a "MOS Technology 6502". More...
 

Macros

#define GET_C_FLAG(x)
 
#define GET_Z_FLAG(x)
 
#define GET_I_FLAG(x)
 
#define GET_D_FLAG(x)
 
#define GET_B_FLAG(x)
 
#define GET_P_FLAG(x)
 
#define GET_V_FLAG(x)
 
#define GET_N_FLAG(x)
 
#define SET_C_FLAG(x)
 
#define SET_Z_FLAG(x)
 
#define SET_I_FLAG(x)
 
#define SET_D_FLAG(x)
 
#define SET_B_FLAG(x)
 
#define SET_P_FLAG(x)
 
#define SET_V_FLAG(x)
 
#define SET_N_FLAG(x)
 
#define SET_ALL_FLAGS(x)
 
#define CLEAR_C_FLAG(x)
 
#define CLEAR_Z_FLAG(x)
 
#define CLEAR_I_FLAG(x)
 
#define CLEAR_D_FLAG(x)
 
#define CLEAR_B_FLAG(x)
 
#define CLEAR_P_FLAG(x)
 
#define CLEAR_V_FLAG(x)
 
#define CLEAR_N_FLAG(x)
 
#define CLEAR_ALL_FLAGS(x)
 
#define indexed_indirect(cpu, addr, offset)
 Macro to access memory by indexed indirect addressing mode.
 
#define indirect_indexed(cpu, addr, offset)
 Macro to access memory by indirect indexed addressing mode.
 
#define indirect(cpu, addr)
 Macro to access memory by indirect addressing mode.
 
#define get_address(cpu)
 Macro to recreate the current address pointed by pc + 1.
 
#define is_page_crossed(addr, offset)
 Macro to know if we cross a page when doing the instruction.
 
#define LD_FLAGS_GENERIC(cpu, reg)
 Procedure that flips the correct flags when loading a value in a register.
 
#define LD_immediate_GENERIC(cpu, reg, d)
 Procedure that loads an immediate (const value) in a register.
 
#define LD_immediate_ACC(processor, d)
 Procedure that loads an immediate (const value) in the accumulator.
 
#define LD_immediate_X(processor, d)
 Procedure that loads an immediate (const value) in the X register.
 
#define LD_immediate_Y(processor, d)
 Procedure that loads an immediate (const value) in the Y register.
 
#define LD_offset_GENERIC(cpu, reg, addr, offset)
 Procedure that loads a value from memory at address addr+offset to a register.
 
#define LD_offset_ACC(processor, addr, offset)
 Procedure that loads a value from memory at address addr+offset to the X register.
 
#define LD_offset_X(processor, addr, offset)
 Procedure that loads a value from memory at address addr+offset to the X register.
 
#define LD_offset_Y(processor, addr, offset)
 Procedure that loads a value from memory at address addr+offset to the Y register.
 
#define LD_absolute_GENERIC(cpu, reg, addr)
 Procedure that loads a value from memory at the address adr to a register.
 
#define LD_absolute_ACC(processor, addr)
 Procedure that loads a value from memory at the address addr to the accumulator.
 
#define LD_absolute_X(processor, addr)
 Procedure that loads a value from memory at the address addr to the X register.
 
#define LD_absolute_Y(processor, addr)
 Procedure that loads a value from memory at the address addr to the Y register.
 
#define LD_indirect_GENERIC(cpu, reg, addr)
 Procedure that loads a value from memory at the address: mem[addr] | mem[addr+1].
 
#define LD_indirect_ACC(processor, addr)
 Procedure that lods a value from memory at the address contained in addr|addr+1. (indirection)
 
#define LD_indirect_X(processor, addr)
 Procedure that lods a value from memory at the address contained in addr|addr+1. (indirection)
 
#define LD_indirect_Y(processor, addr)
 Procedure that lods a value from memory at the address contained in addr|addr+1. (indirection)
 
#define DECINC_GENERIC(cpu, reg, op)
 Procedure that increments the register reg.
 
#define DEC_absolute(processor, addr)
 Procedure that decrement a specific memory location.
 
#define INC_absolute(processor, addr)
 Procedure that increment a specific memory location.
 
#define DEC_offset(processor, addr, offset)
 Procedure that decrement a specific memory location.
 
#define INC_offset(processor, addr, offset)
 Procedure that increment a specific memory location.
 
#define INC_X(processor)
 Procedure that increments the X register.
 
#define INC_Y(processor)
 Procedure that increments the Y register.
 
#define DEC_X(processor)
 Procedure that decrements the X register.
 
#define DEC_Y(processor)
 Procedure that decrements the Y register.
 
#define LD_indexed_indirect_ACC(processor, addr, offset)
 Procedure that loads to the accumulator using Indexed Indirect.
 
#define LD_indirect_indexed_ACC(processor, addr, offset)
 Procedure that loads to the accmulator using Inderect Indexed.
 
#define STR_absolute_GENERIC(cpu, reg, addr)
 Procedure that store the register reg value to a memory address.
 
#define STR_absolute_ACC(processor, addr)
 Procedure that store the accumulator value to a memory address.
 
#define STR_absolute_X(processor, addr)
 Procedure that store the X register value to a memory address.
 
#define STR_absolute_Y(processor, addr)
 Procedure that store the Y register value to a memory address.
 
#define STR_offset_GENERIC(cpu, reg, addr, offset)
 Procedure that store the register reg value to a memory address + offset.
 
#define STR_offset_ACC(processor, addr, offset)
 Procedure that store the accumulator value to a memory address + offset.
 
#define STR_offset_X(processor, addr, offset)
 Procedure that store the X register value to a memory address + offset.
 
#define STR_offset_Y(processor, addr, offset)
 Procedure that store the Y register value to a memory address + offset.
 
#define STR_indirect_indexed_ACC(processor, addr, offset)
 Procedure that store the accumulator value to a memory address using indirect indexed addressing mode.
 
#define STR_indexed_indirect_ACC(processor, addr, offset)
 Procedure that store the accumulator value to a memory address using indexes indirect addressing mode.
 
#define TR_GENERIC(cpu, reg1, reg2)
 Procedure that store the value in reg1 in reg2.
 
#define TR_ACC_X(processor)
 Procedure that store the value in the accumulator in the X register.
 
#define TR_ACC_Y(processor)
 Procedure that store the value in accumulator in the Y register.
 
#define TR_X_ACC(processor)
 Procedure that store the value in the X register in the accumulator.
 
#define TR_Y_ACC(processor)
 Procedure that store the value in the Y register in the accumulator.
 
#define TR_SP_X(processor)
 Procedure that store the stack pointer in the X register.
 
#define TR_X_SP(processor)
 Procedure that store the value in the X register in the stack pointer.
 
#define PUSH_GENERIC(cpu, reg)
 Procedure that push the reg value to the top of the stack.
 
#define PUSH_immediate(cpu, d)
 Procedure that push an immediate value to the top of the stack.
 
#define PUSH_ACC(processor)
 Procedure that push the accumulator value to the top of the stack.
 
#define PUSH_FLAGS(processor)
 Procedure that push the processor flags to the top of the stack.
 
#define PUSH_PC(processor)
 Procedure that push the program_counter to the top of the stack.
 
#define PULL_GENERIC(cpu, reg)
 Procedure that pop the top value of the stack and return it to the register reg.
 
#define PULL_ACC(processor)
 Procedure that pop the top value of the stack and return it to the accumulator.
 
#define PULL_FLAGS(processor)
 Procedure that pop the top value of the stack and return it to the flags register.
 
#define PULL_PC(processor)
 Procedure that pop the top value of the stack and return it to the program counter.
 
#define ADD_immediate(processor, data)
 Procedure that perform a ADD with the carry flag (A,Z,C,N=A+data+C) with an immediate.
 
#define ADD_absolute(processor, addr)
 Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with absolute addressing mode.
 
#define ADD_offset(processor, addr, offset)
 Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with absolute+offset addressing addressing.
 
#define ADD_indexed_indirect(processor, addr, offset)
 Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mode.
 
#define ADD_indirect_indexed(processor, addr, offset)
 Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.
 
#define LOGICAL_FLAGS(processor)
 Procedure that performs the test for every bitwise logic operations.
 
#define AND_immediate(processor, data)
 Procedure that performs a bitwise logic AND (A,Z,N = A & data)
 
#define AND_absolute(processor, addr)
 Procedure that performs a bitwise logic AND (A,Z,N = A&M) using absolute addressing mode.
 
#define AND_offset(processor, addr, offset)
 Procedure that performs a bitwise logic AND (A,Z,N = A&M) using offset addressing mode.
 
#define AND_indexed_indirect(processor, addr, offset)
 Procedure that perform a AND with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mode.
 
#define AND_indirect_indexed(processor, addr, offset)
 Procedure that perform a AND with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.
 
#define OR_immediate(processor, data)
 Procedure that performs a bitwise logic OR (A,Z,N = A & data)
 
#define OR_absolute(processor, addr)
 Procedure that performs a bitwise logic OR (A,Z,N = A&M) using absolute addressing mode.
 
#define OR_offset(processor, addr, offset)
 Procedure that performs a bitwise logic OR (A,Z,N = A&M) using offset addressing mode.
 
#define OR_indexed_indirect(processor, addr, offset)
 Procedure that perform a OR with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mode.
 
#define OR_indirect_indexed(processor, addr, offset)
 Procedure that perform a OR with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.
 
#define XOR_immediate(processor, data)
 Procedure that performs a bitwise logic XOR (A,Z,N = A & data)
 
#define XOR_absolute(processor, addr)
 Procedure that performs a bitwise logic XOR (A,Z,N = A&M) using absolute addressing mode.
 
#define XOR_offset(processor, addr, offset)
 Procedure that performs a bitwise logic XOR (A,Z,N = A&M) using offset addressing mode.
 
#define XOR_indexed_indirect(processor, addr, offset)
 Procedure that perform a XOR with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mode.
 
#define XOR_indirect_indexed(processor, addr, offset)
 Procedure that perform a XOR with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.
 
#define SBC_immediate(processor, data)
 Procedure that perform a substract with the carry flag (A,Z,C,N=A-data-C) with an immediate.
 
#define SBC_absolute(processor, addr)
 Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with absolute addressing mode.
 
#define SBC_offset(processor, addr, offset)
 Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with offset addressing mode.
 
#define SBC_indexed_indirect(processor, addr, offset)
 Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with indexed indirect addressing mode.
 
#define SBC_indirect_indexed(processor, addr, offset)
 Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with indirect indexed addressing mode.
 
#define NO_OPERATION()
 Procedure that litteraly does nothing.
 
#define BIT_TEST(processor, addr)
 Procedure that performs a BIT test (A & M, N = M7, V = M6)
 
#define BRANCH_GENERIC_S(processor, flag_mask, offset)
 Procedure that performs a BRANCH instruction for the FLAG.
 
#define BRANCH_GENERIC_C(processor, flag_mask, offset)
 Procedure that performs a BRANCH instruction for the FLAG.
 
#define BRANCH_If_Positive(processor, offset)
 Procedure that performs a BPL (branch if positive, if the N flag is clear)
 
#define BRANCH_Overflow_C(processor, offset)
 Procedure that performs a BVC (branch if overflow clear, if the V flag is clear)
 
#define BRANCH_Not_Equal(processor, offset)
 Procedure that performs a BNE (if the Z flag is clear)
 
#define BRANCH_Carry_C(processor, offset)
 Procedure that performs a BCC (if the C flag is clear)
 
#define BRANCH_If_Minus(processor, offset)
 Procedure that performs a BMI (branch if positive, if the N flag is clear)
 
#define BRANCH_Overflow_S(processor, offset)
 Procedure that performs a BVS (branch if positive, if the N flag is clear)
 
#define BRANCH_Equal(processor, offset)
 Procedure that performs a BEQ (branch if positive, if the N flag is clear)
 
#define BRANCH_Carry_S(processor, offset)
 Procedure that performs a BCS (branch if positive, if the N flag is clear)
 
#define SHIFT_GENERIC(processor, field, operator, flag_shift_offset, flag_mask)
 Procedure that performs a Bitshift of 1 using a specified operator and placing the overflowing bit inside of the carry flag.
 
#define SHIFT_LEFT_ACC(processor)
 Procedure that performs a bitshift to the left (ASL operation) on the accumulator and place the 7th bit inside of the Carry flag.
 
#define SHIFT_RIGHT_ACC(processor)
 Procedure that performs a bitshift to the right (LSR operation) on the accumulator and place the 0th bit inside of the Carry flag.
 
#define SHIFT_LEFT_absolute(processor, addr)
 Procedure that performs a bitshift to the left (ASL operation) at the address addr and place the 7th bit inside of the Carry flag.
 
#define SHIFT_RIGHT_absolute(processor, addr)
 Procedure that performs a bitshift to the right (LSR operation) at the address addr and place the 0th bit inside of the Carry flag.
 
#define SHIFT_LEFT_offset(processor, addr, offset)
 Procedure that performs a bitshift to the left (ASL operation) at the address (addr + offset) and place the 7th bit inside of the Carry flag.
 
#define SHIFT_RIGHT_offset(processor, addr, offset)
 Procedure that performs a bitshift to the right (LSR operation) at the address (addr + offset) and place the 0th bit inside of the Carry flag.
 
#define ROTATION_GENERIC(processor, field, operator, store_offset, pop_offset, flag_mask)
 Procedure that performs a Bitshift of 1 using a specified operator and placing the overflowing bit inside of the carry flag.
 
#define ROTATION_LEFT_ACC(processor)
 Procedure that performs a bit shift with rotation to the left (ROL operation) on the accumulator. Bit 0 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 7.
 
#define ROTATION_RIGHT_ACC(processor)
 Procedure that performs a bit shift with rotation to the right (ROR operation) on the accumulator. Bit 7 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 0.
 
#define ROTATION_LEFT_absolute(processor, addr)
 Procedure that performs a bit shift with rotation to the left (ROL operation) at the memory address addr. Bit 0 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 7.
 
#define ROTATION_RIGHT_absolute(processor, addr)
 Procedure that performs a bit shift with rotation to the right (ROR operation) at the memory address addr. Bit 7 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 0.
 
#define ROTATION_LEFT_offset(processor, addr, offset)
 Procedure that performs a bit shift with rotation to the left (ROL operation) at the memory address (addr + offset). Bit 0 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 7.
 
#define ROTATION_RIGHT_offset(processor, addr, offset)
 Procedure that performs a bit shift with rotation to the right (ROR operation) at the memory address (addr + offset). Bit 7 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 0.
 
#define JUMP_absolute(processor, addr)
 Procedure that sets the program counter to a certain address (JMP instruction)
 
#define JUMP_indirect(processor, addr)
 Procedure that sets the program counter to a certain indirect address (JMP instruction)
 
#define GENERIC_IR(processor, vector_start)
 Procedure that performs a generic IR instruction.
 
#define BREAK(processor)
 Procedure that performs a break instruction (forces an interrupt).
 
#define IRQ(processor)
 Procedure that performs an interrupt request (when an IRQ is pending).
 
#define NMI(processor)
 Procedure that performs a non maskable interrupt (when an NMI is pending).
 
#define RESET(processor)
 Procedure that performs a NES reset interrupt.
 
#define JUMP_Subroutine(processor, addr)
 Procedure that performs a Jump to a subroutine (JSR instruction) located at the address addr.
 
#define RETURN_Subroutine(processor)
 Procedure that performs a return from subroutine (RTS instruction) by pulling the top two elements of the stack as the PC (ad then, it increments the PC to prevent some errors)
 
#define RETURN_Interrupt(processor)
 Procedure that performs a return from interrupt (RTI instruction) by pulling the flags from the top of the stack and then pulling the PC.
 
#define COMPARE_GENERIC_Immediate(processor, value, register)
 Procedure that performs a CMP operation (Z,C,N = reg - M) with an immediate value.
 
#define COMPARE_Immediate_ACC(processor, value)
 Procedure that performs a CMP operation (Z,C,N = A - M) with an immediate value.
 
#define COMPARE_absolute_ACC(processor, addr)
 Procedure that performs a CMP operation (Z, C, N = A - M) with an absolute addressing mode.
 
#define COMPARE_offset_ACC(processor, addr, offset)
 Procedure that performs a CMP operation (Z, C, N = A - M) with an absolute + offset addressing mode.
 
#define COMPARE_indexed_indirect_ACC(processor, addr, offset)
 Procedure that performs a CMP operation (Z, C, N = A - M) with an indexed indirect addressing mode.
 
#define COMPARE_indirect_indexed_ACC(processor, addr, offset)
 Procedure that performs a CMP operation (Z, C, N = A - M) with an indirect indexed addressing mode.
 
#define COMPARE_Immediate_X(processor, value)
 Procedure that performs a CMP operation (Z, C, N = X - M) with a value addressing mode.
 
#define COMPARE_absolute_X(processor, addr)
 Procedure that performs a CMP operation (Z, C, N = X - M) with an indirect indexed addressing mode.
 
#define COMPARE_Immediate_Y(processor, value)
 Procedure that performs a CMP operation (Z, C, N = Y - M) with a value addressing mode.
 
#define COMPARE_absolute_Y(processor, addr)
 Procedure that performs a CMP operation (Z, C, N = Y - M) with an indirect indexed addressing mode.
 

Functions

void init_cpu (CPU *cpu, memory mem)
 Procedure that initialize the CPU to its boot/reset state.
 
void reset (CPU *processor)
 Procedure that resets the CPU by using the NES reset routine.
 
void free_cpu (CPU *cpu)
 Procedure that free the memory used by the CPU and the memory array.
 
void start_cpu (CPU *cpu)
 Procedure that acts like the reset button of the NES.
 
errcode_t step_cpu (CPU *cpu)
 Function that step a clock cycle.
 
unsigned char get_next_instruction (CPU *cpu)
 Function that get the next instruction written in the ROM.
 
errcode_t execute_instruction (CPU *cpu)
 Function that execute the current instruction in the ROM.
 
void print_cpu_state (CPU *processor)
 Procedure that displays the current state of the CPU.
 
void print_stack_state (CPU *cpu)
 Procedure that display the current state of the stack.
 
void print_stack_expected (memory stack, data start)
 Procedure that display the expected state of the stack.
 
void print_cpu_expected (data acc, data x, data y, data flags, address pc, data sp)
 Procedure that display the expected state of the CPU.
 

Macro Definition Documentation

◆ ADD_absolute

#define ADD_absolute ( processor,
addr )
Value:
ADD_immediate(processor, processor->mem[addr])
#define ADD_immediate(processor, data)
Procedure that perform a ADD with the carry flag (A,Z,C,N=A+data+C) with an immediate.
Definition cpu.h:662

Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with absolute addressing mode.

Parameters
[in,out]processorThe CPU that performs the ADD with Carry
[in]addrThe address of the data to add

Definition at line 692 of file cpu.h.

Referenced by execute_instruction().

◆ ADD_immediate

#define ADD_immediate ( processor,
data )
Value:
({ \
processor->cache = (processor->accumulator); \
processor->accumulator += (data + GET_C_FLAG(processor)); \
if ((processor->cache & BIT_7_MASK) != (processor->accumulator & BIT_7_MASK)) { \
SET_V_FLAG(processor); \
SET_C_FLAG(processor); \
CLEAR_N_FLAG(processor); \
SET_Z_FLAG(processor); \
} else if (processor->accumulator & BIT_7_MASK) { \
CLEAR_V_FLAG(processor); \
SET_N_FLAG(processor); \
CLEAR_Z_FLAG(processor); \
} else if (processor->accumulator == 0) { \
SET_Z_FLAG(processor); \
CLEAR_V_FLAG(processor); \
CLEAR_N_FLAG(processor); \
} else { \
CLEAR_V_FLAG(processor); \
CLEAR_N_FLAG(processor); \
CLEAR_Z_FLAG(processor); \
} \
})
#define GET_C_FLAG(x)
Definition cpu.h:119
#define BIT_7_MASK
Definition macros.h:20
byte data
Definition of the data format used by the CPU.
Definition types.h:11

Procedure that perform a ADD with the carry flag (A,Z,C,N=A+data+C) with an immediate.

Parameters
[in,out]processorThe CPU that performs the ADD with Carry
[in]dataThe immediate (a byte)

Definition at line 662 of file cpu.h.

662#define ADD_immediate(processor, data) \
663 ({ \
664 processor->cache = (processor->accumulator); \
665 processor->accumulator += (data + GET_C_FLAG(processor)); \
666 if ((processor->cache & BIT_7_MASK) != (processor->accumulator & BIT_7_MASK)) { \
667 SET_V_FLAG(processor); \
668 SET_C_FLAG(processor); \
669 CLEAR_N_FLAG(processor); \
670 SET_Z_FLAG(processor); \
671 } else if (processor->accumulator & BIT_7_MASK) { \
672 CLEAR_V_FLAG(processor); \
673 SET_N_FLAG(processor); \
674 CLEAR_Z_FLAG(processor); \
675 } else if (processor->accumulator == 0) { \
676 SET_Z_FLAG(processor); \
677 CLEAR_V_FLAG(processor); \
678 CLEAR_N_FLAG(processor); \
679 } else { \
680 CLEAR_V_FLAG(processor); \
681 CLEAR_N_FLAG(processor); \
682 CLEAR_Z_FLAG(processor); \
683 } \
684 })

Referenced by execute_instruction(), and main().

◆ ADD_indexed_indirect

#define ADD_indexed_indirect ( processor,
addr,
offset )
Value:
ADD_immediate(processor, indexed_indirect(processor, addr, offset))
#define indexed_indirect(cpu, addr, offset)
Macro to access memory by indexed indirect addressing mode.
Definition cpu.h:156

Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mode.

Parameters
[in,out]processorThe CPU that performs the ADD with Carry
[in]addrThe base address
[in]offsetThe offset (usually X)

Definition at line 710 of file cpu.h.

710#define ADD_indexed_indirect(processor, addr, offset) \
711 ADD_immediate(processor, indexed_indirect(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ ADD_indirect_indexed

#define ADD_indirect_indexed ( processor,
addr,
offset )
Value:
ADD_immediate(processor, indirect_indexed(processor, addr, offset))
#define indirect_indexed(cpu, addr, offset)
Macro to access memory by indirect indexed addressing mode.
Definition cpu.h:165

Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.

Parameters
[in,out]processorThe CPU that performs the ADD with Carry
[in]addrThe base address
[in]offsetThe offset (usually Y)

Definition at line 719 of file cpu.h.

719#define ADD_indirect_indexed(processor, addr, offset) \
720 ADD_immediate(processor, indirect_indexed(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ ADD_offset

#define ADD_offset ( processor,
addr,
offset )
Value:
ADD_immediate(processor, processor->mem[addr + offset])

Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with absolute+offset addressing addressing.

Parameters
[in,out]processorThe CPU that performs the ADD with Carry
[in]addrThe base address
[in]offsetThe offset from the address

Definition at line 701 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ AND_absolute

#define AND_absolute ( processor,
addr )
Value:
AND_immediate(processor, processor->mem[addr])
#define AND_immediate(processor, data)
Procedure that performs a bitwise logic AND (A,Z,N = A & data)
Definition cpu.h:744

Procedure that performs a bitwise logic AND (A,Z,N = A&M) using absolute addressing mode.

Parameters
[in,out]processorThe CPU that performs the bitwise logic AND operation
[in]addrThe addressing to the data

Definition at line 755 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ AND_immediate

#define AND_immediate ( processor,
data )
Value:
({ \
processor->accumulator &= data; \
LOGICAL_FLAGS(processor); \
})

Procedure that performs a bitwise logic AND (A,Z,N = A & data)

Parameters
[in,out]processorThe CPU that performs the bitwise logic AND operation
[in]dataThe value to bitwise AND with the accumulator

Definition at line 744 of file cpu.h.

744#define AND_immediate(processor, data) \
745 ({ \
746 processor->accumulator &= data; \
747 LOGICAL_FLAGS(processor); \
748 })

Referenced by execute_instruction(), and main().

◆ AND_indexed_indirect

#define AND_indexed_indirect ( processor,
addr,
offset )
Value:
AND_immediate(processor, indexed_indirect(processor, addr, offset))

Procedure that perform a AND with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mode.

Parameters
[in,out]processorThe CPU that performs the ADD with Carry
[in]addrThe base address
[in]offsetThe offset (usually X)

Definition at line 772 of file cpu.h.

772#define AND_indexed_indirect(processor, addr, offset) \
773 AND_immediate(processor, indexed_indirect(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ AND_indirect_indexed

#define AND_indirect_indexed ( processor,
addr,
offset )
Value:
AND_immediate(processor, indirect_indexed(processor, addr, offset))

Procedure that perform a AND with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.

Parameters
[in,out]processorThe CPU that performs the ADD with Carry
[in]addrThe base address
[in]offsetThe offset (usually Y)

Definition at line 781 of file cpu.h.

781#define AND_indirect_indexed(processor, addr, offset) \
782 AND_immediate(processor, indirect_indexed(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ AND_offset

#define AND_offset ( processor,
addr,
offset )
Value:
AND_immediate(processor, processor->mem[addr + offset])

Procedure that performs a bitwise logic AND (A,Z,N = A&M) using offset addressing mode.

Parameters
[in,out]processorThe CPU that performs the bitwise logic AND operation
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 763 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ BIT_TEST

#define BIT_TEST ( processor,
addr )
Value:
({ \
processor->cache = processor->accumulator & processor->mem[addr]; \
if (processor->cache == 0) { \
SET_Z_FLAG(processor); \
} else { \
CLEAR_Z_FLAG(processor); \
if (processor->cache & BIT_7_MASK) { \
SET_N_FLAG(processor); \
} \
if (processor->cache & BIT_6_MASK) { \
SET_V_FLAG(processor); \
} \
} \
})
#define BIT_6_MASK
Definition macros.h:21

Procedure that performs a BIT test (A & M, N = M7, V = M6)

Parameters
[in,out]processorThe CPU that performs the BIT test
[in]addrThe base address (ZP: 2 cycles, Absolute: 3 cycles)

Definition at line 951 of file cpu.h.

951#define BIT_TEST(processor, addr) \
952 ({ \
953 processor->cache = processor->accumulator & processor->mem[addr]; \
954 if (processor->cache == 0) { \
955 SET_Z_FLAG(processor); \
956 } else { \
957 CLEAR_Z_FLAG(processor); \
958 if (processor->cache & BIT_7_MASK) { \
959 SET_N_FLAG(processor); \
960 } \
961 if (processor->cache & BIT_6_MASK) { \
962 SET_V_FLAG(processor); \
963 } \
964 } \
965 })

Referenced by execute_instruction(), and main().

◆ BRANCH_Carry_C

#define BRANCH_Carry_C ( processor,
offset )
Value:
BRANCH_GENERIC_C(processor, BIT_0_MASK, offset)
#define BRANCH_GENERIC_C(processor, flag_mask, offset)
Procedure that performs a BRANCH instruction for the FLAG.
Definition cpu.h:985
#define BIT_0_MASK
Definition macros.h:27

Procedure that performs a BCC (if the C flag is clear)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]offsetThe offset (a signed byte) which is added to the PC when the condition is true

Definition at line 1018 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ BRANCH_Carry_S

#define BRANCH_Carry_S ( processor,
offset )
Value:
BRANCH_GENERIC_S(processor, BIT_0_MASK, offset)
#define BRANCH_GENERIC_S(processor, flag_mask, offset)
Procedure that performs a BRANCH instruction for the FLAG.
Definition cpu.h:973

Procedure that performs a BCS (branch if positive, if the N flag is clear)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]offsetThe offset (a signed byte) which is added to the PC when the condition is true

Definition at line 1046 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ BRANCH_Equal

#define BRANCH_Equal ( processor,
offset )
Value:
BRANCH_GENERIC_S(processor, BIT_1_MASK, offset)
#define BIT_1_MASK
Definition macros.h:26

Procedure that performs a BEQ (branch if positive, if the N flag is clear)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]offsetThe offset (a signed byte) which is added to the PC when the condition is true

Definition at line 1039 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ BRANCH_GENERIC_C

#define BRANCH_GENERIC_C ( processor,
flag_mask,
offset )
Value:
({ \
if (!(processor->flags & flag_mask)) { \
processor->program_counter += (signed char)(offset); \
} \
})

Procedure that performs a BRANCH instruction for the FLAG.

Parameters
[in,out]processorThe CPU that performs the Branch instruction
[in]flag_maskThe bitmask that isolate the correct flag
[in]offsetThe offset (a signed byte) which is added to the PC when the FLAG is CLEAR

Definition at line 985 of file cpu.h.

985#define BRANCH_GENERIC_C(processor, flag_mask, offset) \
986 ({ \
987 if (!(processor->flags & flag_mask)) { \
988 processor->program_counter += (signed char)(offset); \
989 } \
990 })

◆ BRANCH_GENERIC_S

#define BRANCH_GENERIC_S ( processor,
flag_mask,
offset )
Value:
({ \
if (processor->flags & flag_mask) { \
processor->program_counter += (signed char)(offset); \
} \
})

Procedure that performs a BRANCH instruction for the FLAG.

Parameters
[in,out]processorThe CPU that performs the Branch instruction
[in]flag_maskThe bitmask that isolate the correct flag
[in]offsetThe offset (a signed byte) which is added to the PC when the FLAG is SET

Definition at line 973 of file cpu.h.

973#define BRANCH_GENERIC_S(processor, flag_mask, offset) \
974 ({ \
975 if (processor->flags & flag_mask) { \
976 processor->program_counter += (signed char)(offset); \
977 } \
978 })

◆ BRANCH_If_Minus

#define BRANCH_If_Minus ( processor,
offset )
Value:
BRANCH_GENERIC_S(processor, BIT_7_MASK, offset)

Procedure that performs a BMI (branch if positive, if the N flag is clear)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]offsetThe offset (a signed byte) which is added to the PC when the condition is true

Definition at line 1025 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ BRANCH_If_Positive

#define BRANCH_If_Positive ( processor,
offset )
Value:
BRANCH_GENERIC_C(processor, BIT_7_MASK, offset)

Procedure that performs a BPL (branch if positive, if the N flag is clear)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]offsetThe offset (a signed byte) which is added to the PC when the condition is true

Definition at line 997 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ BRANCH_Not_Equal

#define BRANCH_Not_Equal ( processor,
offset )
Value:
BRANCH_GENERIC_C(processor, BIT_1_MASK, offset)

Procedure that performs a BNE (if the Z flag is clear)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]offsetThe offset (a signed byte) which is added to the PC when the condition is true

Definition at line 1011 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ BRANCH_Overflow_C

#define BRANCH_Overflow_C ( processor,
offset )
Value:
BRANCH_GENERIC_C(processor, BIT_6_MASK, offset)

Procedure that performs a BVC (branch if overflow clear, if the V flag is clear)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]offsetThe offset (a signed byte) which is added to the PC when the condition is true

Definition at line 1004 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ BRANCH_Overflow_S

#define BRANCH_Overflow_S ( processor,
offset )
Value:
BRANCH_GENERIC_S(processor, BIT_6_MASK, offset)

Procedure that performs a BVS (branch if positive, if the N flag is clear)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]offsetThe offset (a signed byte) which is added to the PC when the condition is true

Definition at line 1032 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ BREAK

#define BREAK ( processor)
Value:
({ \
GENERIC_IR(processor, 0xfffe); \
SET_B_FLAG(processor); \
})

Procedure that performs a break instruction (forces an interrupt).

Parameters
[in,out]processorThe CPU that performs the interrupt

Definition at line 1232 of file cpu.h.

1232#define BREAK(processor) \
1233 ({ \
1234 GENERIC_IR(processor, 0xfffe); \
1235 SET_B_FLAG(processor); \
1236 })

Referenced by execute_instruction(), and main().

◆ CLEAR_ALL_FLAGS

#define CLEAR_ALL_FLAGS ( x)
Value:
(x->flags = x->flags & 0x00)

Definition at line 148 of file cpu.h.

Referenced by main().

◆ CLEAR_B_FLAG

#define CLEAR_B_FLAG ( x)
Value:
(x->flags = x->flags & NBIT_4_MASK)
#define NBIT_4_MASK
Definition macros.h:32

Definition at line 143 of file cpu.h.

◆ CLEAR_C_FLAG

#define CLEAR_C_FLAG ( x)
Value:
(x->flags = x->flags & NBIT_0_MASK)
#define NBIT_0_MASK
Definition macros.h:36

Definition at line 139 of file cpu.h.

Referenced by execute_instruction().

◆ CLEAR_D_FLAG

#define CLEAR_D_FLAG ( x)
Value:
(x->flags = x->flags & NBIT_3_MASK)
#define NBIT_3_MASK
Definition macros.h:33

Definition at line 142 of file cpu.h.

Referenced by execute_instruction().

◆ CLEAR_I_FLAG

#define CLEAR_I_FLAG ( x)
Value:
(x->flags = x->flags & NBIT_2_MASK)
#define NBIT_2_MASK
Definition macros.h:34

Definition at line 141 of file cpu.h.

Referenced by execute_instruction().

◆ CLEAR_N_FLAG

#define CLEAR_N_FLAG ( x)
Value:
(x->flags = x->flags & NBIT_7_MASK)
#define NBIT_7_MASK
Definition macros.h:29

Definition at line 146 of file cpu.h.

Referenced by main().

◆ CLEAR_P_FLAG

#define CLEAR_P_FLAG ( x)
Value:
(x->flags = x->flags & NBIT_5_MASK)
#define NBIT_5_MASK
Definition macros.h:31

Definition at line 144 of file cpu.h.

◆ CLEAR_V_FLAG

#define CLEAR_V_FLAG ( x)
Value:
(x->flags = x->flags & NBIT_6_MASK)
#define NBIT_6_MASK
Definition macros.h:30

Definition at line 145 of file cpu.h.

Referenced by execute_instruction().

◆ CLEAR_Z_FLAG

#define CLEAR_Z_FLAG ( x)
Value:
(x->flags = x->flags & NBIT_1_MASK)
#define NBIT_1_MASK
Definition macros.h:35

Definition at line 140 of file cpu.h.

Referenced by main().

◆ COMPARE_absolute_ACC

#define COMPARE_absolute_ACC ( processor,
addr )
Value:
COMPARE_Immediate_ACC(processor, processor->mem[addr])
#define COMPARE_Immediate_ACC(processor, value)
Procedure that performs a CMP operation (Z,C,N = A - M) with an immediate value.
Definition cpu.h:1332

Procedure that performs a CMP operation (Z, C, N = A - M) with an absolute addressing mode.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address

Definition at line 1340 of file cpu.h.

Referenced by execute_instruction().

◆ COMPARE_absolute_X

#define COMPARE_absolute_X ( processor,
addr )
Value:
COMPARE_Immediate_X(processor, processor->mem[addr])
#define COMPARE_Immediate_X(processor, value)
Procedure that performs a CMP operation (Z, C, N = X - M) with a value addressing mode.
Definition cpu.h:1377

Procedure that performs a CMP operation (Z, C, N = X - M) with an indirect indexed addressing mode.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address

Definition at line 1386 of file cpu.h.

Referenced by execute_instruction().

◆ COMPARE_absolute_Y

#define COMPARE_absolute_Y ( processor,
addr )
Value:
COMPARE_Immediate_X(processor, processor->mem[addr])

Procedure that performs a CMP operation (Z, C, N = Y - M) with an indirect indexed addressing mode.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address

Definition at line 1403 of file cpu.h.

Referenced by execute_instruction().

◆ COMPARE_GENERIC_Immediate

#define COMPARE_GENERIC_Immediate ( processor,
value,
register )
Value:
({ \
if (processor->register > value) { \
SET_C_FLAG(processor); \
CLEAR_Z_FLAG(processor); \
CLEAR_N_FLAG(processor); \
} else if (processor->register == value) { \
SET_Z_FLAG(processor); \
SET_C_FLAG(processor); \
CLEAR_N_FLAG(processor); \
} else { \
CLEAR_Z_FLAG(processor); \
CLEAR_C_FLAG(processor); \
SET_N_FLAG(processor); \
} \
})

Procedure that performs a CMP operation (Z,C,N = reg - M) with an immediate value.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]valueThe current value
[in]registerThe register used for the comparison

Definition at line 1310 of file cpu.h.

1310#define COMPARE_GENERIC_Immediate(processor, value, register) \
1311 ({ \
1312 if (processor->register > value) { \
1313 SET_C_FLAG(processor); \
1314 CLEAR_Z_FLAG(processor); \
1315 CLEAR_N_FLAG(processor); \
1316 } else if (processor->register == value) { \
1317 SET_Z_FLAG(processor); \
1318 SET_C_FLAG(processor); \
1319 CLEAR_N_FLAG(processor); \
1320 } else { \
1321 CLEAR_Z_FLAG(processor); \
1322 CLEAR_C_FLAG(processor); \
1323 SET_N_FLAG(processor); \
1324 } \
1325 })

◆ COMPARE_Immediate_ACC

#define COMPARE_Immediate_ACC ( processor,
value )
Value:
COMPARE_GENERIC_Immediate(processor, value, accumulator)
#define COMPARE_GENERIC_Immediate(processor, value, register)
Procedure that performs a CMP operation (Z,C,N = reg - M) with an immediate value.
Definition cpu.h:1310

Procedure that performs a CMP operation (Z,C,N = A - M) with an immediate value.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]valueThe current value

Definition at line 1332 of file cpu.h.

1332#define COMPARE_Immediate_ACC(processor, value) \
1333 COMPARE_GENERIC_Immediate(processor, value, accumulator)

Referenced by execute_instruction().

◆ COMPARE_Immediate_X

#define COMPARE_Immediate_X ( processor,
value )
Value:
COMPARE_GENERIC_Immediate(processor, value, register_x)

Procedure that performs a CMP operation (Z, C, N = X - M) with a value addressing mode.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]valueThe immediate value

Definition at line 1377 of file cpu.h.

1377#define COMPARE_Immediate_X(processor, value) \
1378 COMPARE_GENERIC_Immediate(processor, value, register_x)

Referenced by execute_instruction().

◆ COMPARE_Immediate_Y

#define COMPARE_Immediate_Y ( processor,
value )
Value:
COMPARE_GENERIC_Immediate(processor, value, register_y)

Procedure that performs a CMP operation (Z, C, N = Y - M) with a value addressing mode.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]valueThe immediate value

Definition at line 1394 of file cpu.h.

1394#define COMPARE_Immediate_Y(processor, value) \
1395 COMPARE_GENERIC_Immediate(processor, value, register_y)

Referenced by execute_instruction().

◆ COMPARE_indexed_indirect_ACC

#define COMPARE_indexed_indirect_ACC ( processor,
addr,
offset )
Value:
COMPARE_absolute_ACC(processor, indexed_indirect(processor, addr, offset))
#define COMPARE_absolute_ACC(processor, addr)
Procedure that performs a CMP operation (Z, C, N = A - M) with an absolute addressing mode.
Definition cpu.h:1340

Procedure that performs a CMP operation (Z, C, N = A - M) with an indexed indirect addressing mode.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address
[in]offsetThe offset

Definition at line 1358 of file cpu.h.

1358#define COMPARE_indexed_indirect_ACC(processor, addr, offset) \
1359 COMPARE_absolute_ACC(processor, indexed_indirect(processor, addr, offset))

Referenced by execute_instruction().

◆ COMPARE_indirect_indexed_ACC

#define COMPARE_indirect_indexed_ACC ( processor,
addr,
offset )
Value:
COMPARE_absolute_ACC(processor, indirect_indexed(processor, addr, offset))

Procedure that performs a CMP operation (Z, C, N = A - M) with an indirect indexed addressing mode.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address
[in]offsetThe offset

Definition at line 1368 of file cpu.h.

1368#define COMPARE_indirect_indexed_ACC(processor, addr, offset) \
1369 COMPARE_absolute_ACC(processor, indirect_indexed(processor, addr, offset))

Referenced by execute_instruction().

◆ COMPARE_offset_ACC

#define COMPARE_offset_ACC ( processor,
addr,
offset )
Value:
COMPARE_absolute_ACC(processor, addr + offset)

Procedure that performs a CMP operation (Z, C, N = A - M) with an absolute + offset addressing mode.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address
[in]offsetThe offset

Definition at line 1349 of file cpu.h.

Referenced by execute_instruction().

◆ DEC_absolute

#define DEC_absolute ( processor,
addr )
Value:
DECINC_GENERIC(processor, mem[addr], --)
#define DECINC_GENERIC(cpu, reg, op)
Procedure that increments the register reg.
Definition cpu.h:357

Procedure that decrement a specific memory location.

Parameters
[in,out]processorThe CPU that performs the decrement
[in]addrThe address where we need to decrement

Definition at line 377 of file cpu.h.

Referenced by execute_instruction().

◆ DEC_offset

#define DEC_offset ( processor,
addr,
offset )
Value:
DECINC_GENERIC(processor, mem[addr + offset], --)

Procedure that decrement a specific memory location.

Parameters
[in,out]processorThe CPU that performs the decrement
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 392 of file cpu.h.

Referenced by execute_instruction().

◆ DEC_X

#define DEC_X ( processor)
Value:
DECINC_GENERIC(processor, register_x, --)

Procedure that decrements the X register.

Parameters
[in,out]processorThe CPU that contains the X register to decrement

Definition at line 418 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ DEC_Y

#define DEC_Y ( processor)
Value:
DECINC_GENERIC(processor, register_y, --)

Procedure that decrements the Y register.

Parameters
[in,out]processorThe CPU that contains the Y register to decrement

Definition at line 424 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ DECINC_GENERIC

#define DECINC_GENERIC ( cpu,
reg,
op )
Value:
({ \
op cpu->reg; \
if (cpu->reg == 0) { \
SET_Z_FLAG(cpu); \
CLEAR_N_FLAG(cpu); \
} else if (cpu->reg & BIT_7_MASK) { \
SET_N_FLAG(cpu); \
CLEAR_Z_FLAG(cpu); \
} else { \
CLEAR_N_FLAG(cpu); \
CLEAR_Z_FLAG(cpu); \
} \
})

Procedure that increments the register reg.

Parameters
[in,out]cpuThe CPU that holds the register reg
[in]regThe register to increment or decrement
[in]opThe increment or decrement operator

Definition at line 357 of file cpu.h.

357#define DECINC_GENERIC(cpu, reg, op) \
358 ({ \
359 op cpu->reg; \
360 if (cpu->reg == 0) { \
361 SET_Z_FLAG(cpu); \
362 CLEAR_N_FLAG(cpu); \
363 } else if (cpu->reg & BIT_7_MASK) { \
364 SET_N_FLAG(cpu); \
365 CLEAR_Z_FLAG(cpu); \
366 } else { \
367 CLEAR_N_FLAG(cpu); \
368 CLEAR_Z_FLAG(cpu); \
369 } \
370 })

◆ GENERIC_IR

#define GENERIC_IR ( processor,
vector_start )
Value:
({ \
PUSH_PC(processor); \
PUSH_FLAGS(processor); \
JUMP_indirect(processor, vector_start); \
})

Procedure that performs a generic IR instruction.

Parameters
[in,out]processorThe CPU that performs the interrupt
[in]vector_startThe first address of the interrupt's vector

Definition at line 1221 of file cpu.h.

1221#define GENERIC_IR(processor, vector_start) \
1222 ({ \
1223 PUSH_PC(processor); \
1224 PUSH_FLAGS(processor); \
1225 JUMP_indirect(processor, vector_start); \
1226 })

◆ get_address

#define get_address ( cpu)
Value:
(cpu->mem[cpu->program_counter + 1] | (cpu->mem[cpu->program_counter + 2] << 8))

Macro to recreate the current address pointed by pc + 1.

Parameters
[in]cpuThe CPU that holds the pc
Returns
The reconstructed address

Definition at line 180 of file cpu.h.

180#define get_address(cpu) \
181 (cpu->mem[cpu->program_counter + 1] | (cpu->mem[cpu->program_counter + 2] << 8))

Referenced by execute_instruction(), get_next_instruction(), and start_cpu().

◆ GET_B_FLAG

#define GET_B_FLAG ( x)
Value:
(x->flags & BIT_4_MASK)
#define BIT_4_MASK
Definition macros.h:23

Definition at line 123 of file cpu.h.

Referenced by print_cpu_state(), and printw_cpu_state().

◆ GET_C_FLAG

#define GET_C_FLAG ( x)
Value:
(x->flags & BIT_0_MASK)

Definition at line 119 of file cpu.h.

Referenced by get_next_instruction(), print_cpu_state(), and printw_cpu_state().

◆ GET_D_FLAG

#define GET_D_FLAG ( x)
Value:
(x->flags & BIT_3_MASK)
#define BIT_3_MASK
Definition macros.h:24

Definition at line 122 of file cpu.h.

Referenced by print_cpu_state(), and printw_cpu_state().

◆ GET_I_FLAG

#define GET_I_FLAG ( x)
Value:
(x->flags & BIT_2_MASK)
#define BIT_2_MASK
Definition macros.h:25

Definition at line 121 of file cpu.h.

Referenced by print_cpu_state(), and printw_cpu_state().

◆ GET_N_FLAG

#define GET_N_FLAG ( x)
Value:
(x->flags & BIT_7_MASK)

Definition at line 126 of file cpu.h.

Referenced by get_next_instruction(), print_cpu_state(), and printw_cpu_state().

◆ GET_P_FLAG

#define GET_P_FLAG ( x)
Value:
(x->flags & BIT_5_MASK)
#define BIT_5_MASK
Definition macros.h:22

Definition at line 124 of file cpu.h.

Referenced by print_cpu_state(), and printw_cpu_state().

◆ GET_V_FLAG

#define GET_V_FLAG ( x)
Value:
(x->flags & BIT_6_MASK)

Definition at line 125 of file cpu.h.

Referenced by get_next_instruction(), print_cpu_state(), and printw_cpu_state().

◆ GET_Z_FLAG

#define GET_Z_FLAG ( x)
Value:
(x->flags & BIT_1_MASK)

Definition at line 120 of file cpu.h.

Referenced by get_next_instruction(), print_cpu_state(), and printw_cpu_state().

◆ INC_absolute

#define INC_absolute ( processor,
addr )
Value:
DECINC_GENERIC(processor, mem[addr], ++)

Procedure that increment a specific memory location.

Parameters
[in,out]processorThe CPU that performs the increment
[in]addrThe address where we need to increment

Definition at line 384 of file cpu.h.

Referenced by execute_instruction().

◆ INC_offset

#define INC_offset ( processor,
addr,
offset )
Value:
DECINC_GENERIC(processor, mem[addr + offset], ++)

Procedure that increment a specific memory location.

Parameters
[in,out]processorThe CPU that performs the increment
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 400 of file cpu.h.

Referenced by execute_instruction().

◆ INC_X

#define INC_X ( processor)
Value:
DECINC_GENERIC(processor, register_x, ++)

Procedure that increments the X register.

Parameters
[in,out]processorThe CPU that contains the X register to increment

Definition at line 406 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ INC_Y

#define INC_Y ( processor)
Value:
DECINC_GENERIC(processor, register_y, ++)

Procedure that increments the Y register.

Parameters
[in,out]processorThe CPU that contains the Y register to increment

Definition at line 412 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ indexed_indirect

#define indexed_indirect ( cpu,
addr,
offset )
Value:
cpu->mem[(cpu->mem[(addr + offset) & 0xFF]) | (cpu->mem[(addr + offset + 1) & 0xFF] << 8)]

Macro to access memory by indexed indirect addressing mode.

Parameters
[in,out]cpuThe CPU in which the memory field is located
[in]addrThe base address (A ZERO PAGE ADDRESS ONLY)
[in]offsetThe offset (usually X)

Definition at line 156 of file cpu.h.

156#define indexed_indirect(cpu, addr, offset) \
157 cpu->mem[(cpu->mem[(addr + offset) & 0xFF]) | (cpu->mem[(addr + offset + 1) & 0xFF] << 8)]

Referenced by main().

◆ indirect

#define indirect ( cpu,
addr )
Value:
cpu->mem[((cpu->mem[(addr)]) | (cpu->mem[(addr) + 1] << 8))]

Macro to access memory by indirect addressing mode.

Parameters
[in,out]cpuThe CPU in which the memory field is located
[in]addrThe base address

Definition at line 173 of file cpu.h.

◆ indirect_indexed

#define indirect_indexed ( cpu,
addr,
offset )
Value:
cpu->mem[(cpu->mem[(addr) & 0xFF] | cpu->mem[(addr + 1) & 0xFF] << 8) + offset]

Macro to access memory by indirect indexed addressing mode.

Parameters
[in,out]cpuThe CPU in which the memory field is located
[in]addrThe base address
[in]offsetThe offset (usually Y)

Definition at line 165 of file cpu.h.

165#define indirect_indexed(cpu, addr, offset) \
166 cpu->mem[(cpu->mem[(addr) & 0xFF] | cpu->mem[(addr + 1) & 0xFF] << 8) + offset]

Referenced by get_next_instruction(), and main().

◆ IRQ

#define IRQ ( processor)
Value:
({ \
GENERIC_IR(processor, 0xfffe); \
CLEAR_B_FLAG(processor); \
})

Procedure that performs an interrupt request (when an IRQ is pending).

Parameters
[in,out]processorThe CPU that performs the interrupt

Definition at line 1242 of file cpu.h.

1242#define IRQ(processor) \
1243 ({ \
1244 GENERIC_IR(processor, 0xfffe); \
1245 CLEAR_B_FLAG(processor); \
1246 })

◆ is_page_crossed

#define is_page_crossed ( addr,
offset )
Value:
((addr & 0x00ff) && !((addr + offset) & 0x00ff))

Macro to know if we cross a page when doing the instruction.

Parameters
[in]addrBase address
[in]offsetOffset from base address
Returns
0 if we stay on the current page, 1 if not

Definition at line 189 of file cpu.h.

Referenced by get_next_instruction().

◆ JUMP_absolute

#define JUMP_absolute ( processor,
addr )
Value:
processor->program_counter = addr

Procedure that sets the program counter to a certain address (JMP instruction)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address

Definition at line 1206 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ JUMP_indirect

#define JUMP_indirect ( processor,
addr )
Value:
JUMP_absolute(processor, ((cpu->mem[(addr)]) | (cpu->mem[(addr) + 1] << 8)))
#define JUMP_absolute(processor, addr)
Procedure that sets the program counter to a certain address (JMP instruction)
Definition cpu.h:1206

Procedure that sets the program counter to a certain indirect address (JMP instruction)

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address

Definition at line 1213 of file cpu.h.

1213#define JUMP_indirect(processor, addr) \
1214 JUMP_absolute(processor, ((cpu->mem[(addr)]) | (cpu->mem[(addr) + 1] << 8)))

Referenced by execute_instruction(), and main().

◆ JUMP_Subroutine

#define JUMP_Subroutine ( processor,
addr )
Value:
({ \
processor->program_counter += 2; \
PUSH_PC(processor); \
JUMP_absolute(processor, addr); \
})

Procedure that performs a Jump to a subroutine (JSR instruction) located at the address addr.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe address of the subroutine

Definition at line 1275 of file cpu.h.

1275#define JUMP_Subroutine(processor, addr) \
1276 ({ \
1277 processor->program_counter += 2; \
1278 PUSH_PC(processor); \
1279 JUMP_absolute(processor, addr); \
1280 })

Referenced by execute_instruction(), and main().

◆ LD_absolute_ACC

#define LD_absolute_ACC ( processor,
addr )
Value:
(LD_absolute_GENERIC(processor, accumulator, addr))
#define LD_absolute_GENERIC(cpu, reg, addr)
Procedure that loads a value from memory at the address adr to a register.
Definition cpu.h:296

Procedure that loads a value from memory at the address addr to the accumulator.

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]addrThe address that holds the data to load

Definition at line 303 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ LD_absolute_GENERIC

#define LD_absolute_GENERIC ( cpu,
reg,
addr )
Value:
LD_immediate_GENERIC(cpu, reg, cpu->mem[addr])
#define LD_immediate_GENERIC(cpu, reg, d)
Procedure that loads an immediate (const value) in a register.
Definition cpu.h:217

Procedure that loads a value from memory at the address adr to a register.

Parameters
[in,out]cpuThe CPU that holds the register reg
[in]regThe register in which we need to load the data
[in]addrThe address of the data

Definition at line 296 of file cpu.h.

◆ LD_absolute_X

#define LD_absolute_X ( processor,
addr )
Value:
(LD_absolute_GENERIC(processor, register_x, addr))

Procedure that loads a value from memory at the address addr to the X register.

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]addrThe address that holds the data to load

Definition at line 310 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ LD_absolute_Y

#define LD_absolute_Y ( processor,
addr )
Value:
(LD_absolute_GENERIC(processor, register_y, addr))

Procedure that loads a value from memory at the address addr to the Y register.

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]addrThe address that holds the data to load

Definition at line 317 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ LD_FLAGS_GENERIC

#define LD_FLAGS_GENERIC ( cpu,
reg )
Value:
({ \
CLEAR_C_FLAG(cpu); \
if (cpu->reg == 0) { \
SET_Z_FLAG(cpu); \
CLEAR_N_FLAG(cpu); \
} else if (cpu->reg & BIT_7_MASK) { \
SET_N_FLAG(cpu); \
CLEAR_Z_FLAG(cpu); \
} else { \
CLEAR_N_FLAG(cpu); \
CLEAR_Z_FLAG(cpu); \
} \
})

Procedure that flips the correct flags when loading a value in a register.

Parameters
[in,out]cpuThe CPU that holds the register reg
[in]regThe register in which the data has been loaded

Definition at line 196 of file cpu.h.

196#define LD_FLAGS_GENERIC(cpu, reg) \
197 ({ \
198 CLEAR_C_FLAG(cpu); \
199 if (cpu->reg == 0) { \
200 SET_Z_FLAG(cpu); \
201 CLEAR_N_FLAG(cpu); \
202 } else if (cpu->reg & BIT_7_MASK) { \
203 SET_N_FLAG(cpu); \
204 CLEAR_Z_FLAG(cpu); \
205 } else { \
206 CLEAR_N_FLAG(cpu); \
207 CLEAR_Z_FLAG(cpu); \
208 } \
209 })

◆ LD_immediate_ACC

#define LD_immediate_ACC ( processor,
d )
Value:
(LD_immediate_GENERIC(processor, accumulator, d))

Procedure that loads an immediate (const value) in the accumulator.

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]dthe data to load

Definition at line 237 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ LD_immediate_GENERIC

#define LD_immediate_GENERIC ( cpu,
reg,
d )
Value:
({ \
CLEAR_C_FLAG(cpu); \
if ((d) == 0) { \
SET_Z_FLAG(cpu); \
CLEAR_N_FLAG(cpu); \
} else if ((d) & BIT_7_MASK) { \
CLEAR_Z_FLAG(cpu); \
SET_N_FLAG(cpu); \
} else { \
CLEAR_Z_FLAG(cpu); \
CLEAR_N_FLAG(cpu); \
} \
cpu->reg = (d); \
})

Procedure that loads an immediate (const value) in a register.

Parameters
[in,out]cpuThe CPU that holds the register reg
[in]regThe register in which we need to load the data
[in]dthe data to load

Definition at line 217 of file cpu.h.

217#define LD_immediate_GENERIC(cpu, reg, d) \
218 ({ \
219 CLEAR_C_FLAG(cpu); \
220 if ((d) == 0) { \
221 SET_Z_FLAG(cpu); \
222 CLEAR_N_FLAG(cpu); \
223 } else if ((d) & BIT_7_MASK) { \
224 CLEAR_Z_FLAG(cpu); \
225 SET_N_FLAG(cpu); \
226 } else { \
227 CLEAR_Z_FLAG(cpu); \
228 CLEAR_N_FLAG(cpu); \
229 } \
230 cpu->reg = (d); \
231 })

◆ LD_immediate_X

#define LD_immediate_X ( processor,
d )
Value:
(LD_immediate_GENERIC(processor, register_x, d))

Procedure that loads an immediate (const value) in the X register.

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]dthe data to load

Definition at line 244 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ LD_immediate_Y

#define LD_immediate_Y ( processor,
d )
Value:
(LD_immediate_GENERIC(processor, register_y, d))

Procedure that loads an immediate (const value) in the Y register.

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]dthe data to load

Definition at line 251 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ LD_indexed_indirect_ACC

#define LD_indexed_indirect_ACC ( processor,
addr,
offset )
Value:
(LD_immediate_GENERIC(processor, accumulator, indexed_indirect(processor, addr, offset)))

Procedure that loads to the accumulator using Indexed Indirect.

Parameters
[in,out]processorThe CPU that holds the accumulator
[in]addrA ZERO PAGE address only !
[in]offsetThe offset (usually, the content of X)

Definition at line 432 of file cpu.h.

432#define LD_indexed_indirect_ACC(processor, addr, offset) \
433 (LD_immediate_GENERIC(processor, accumulator, indexed_indirect(processor, addr, offset)))

Referenced by execute_instruction(), and main().

◆ LD_indirect_ACC

#define LD_indirect_ACC ( processor,
addr )
Value:
LD_indirect_GENERIC(processor, accumulator, addr)
#define LD_indirect_GENERIC(cpu, reg, addr)
Procedure that loads a value from memory at the address: mem[addr] | mem[addr+1].
Definition cpu.h:325

Procedure that lods a value from memory at the address contained in addr|addr+1. (indirection)

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]addrThe base address of the indirection

Definition at line 333 of file cpu.h.

Referenced by main().

◆ LD_indirect_GENERIC

#define LD_indirect_GENERIC ( cpu,
reg,
addr )
Value:
(LD_immediate_GENERIC(cpu, reg, indirect(cpu, addr)))
#define indirect(cpu, addr)
Macro to access memory by indirect addressing mode.
Definition cpu.h:173

Procedure that loads a value from memory at the address: mem[addr] | mem[addr+1].

Parameters
[in,out]cpuThe CPU that holds the register reg
[in]regThe register in which we need to load the data
[in]addrThe base address of the indirect addressing mode

Definition at line 325 of file cpu.h.

◆ LD_indirect_indexed_ACC

#define LD_indirect_indexed_ACC ( processor,
addr,
offset )
Value:
(LD_immediate_GENERIC(processor, accumulator, indirect_indexed(processor, addr, offset)))

Procedure that loads to the accmulator using Inderect Indexed.

Parameters
[in,out]processorThe CPU that holds the accumulator
[in]addrA ZERO PAGE address only !
[in]offsetThe offset (usually, the content of Y)

Definition at line 441 of file cpu.h.

441#define LD_indirect_indexed_ACC(processor, addr, offset) \
442 (LD_immediate_GENERIC(processor, accumulator, indirect_indexed(processor, addr, offset)))

Referenced by execute_instruction(), and main().

◆ LD_indirect_X

#define LD_indirect_X ( processor,
addr )
Value:
LD_indirect_GENERIC(processor, register_x, addr)

Procedure that lods a value from memory at the address contained in addr|addr+1. (indirection)

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]addrThe base address of the indirection

Definition at line 341 of file cpu.h.

Referenced by main().

◆ LD_indirect_Y

#define LD_indirect_Y ( processor,
addr )
Value:
LD_indirect_GENERIC(processor, register_y, addr)

Procedure that lods a value from memory at the address contained in addr|addr+1. (indirection)

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]addrThe base address of the indirection

Definition at line 349 of file cpu.h.

Referenced by main().

◆ LD_offset_ACC

#define LD_offset_ACC ( processor,
addr,
offset )
Value:
(LD_offset_GENERIC(processor, accumulator, addr, offset))
#define LD_offset_GENERIC(cpu, reg, addr, offset)
Procedure that loads a value from memory at address addr+offset to a register.
Definition cpu.h:260

Procedure that loads a value from memory at address addr+offset to the X register.

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]addrThe base address
[in]offsetThe offset from the address

Definition at line 269 of file cpu.h.

269#define LD_offset_ACC(processor, addr, offset) \
270 (LD_offset_GENERIC(processor, accumulator, addr, offset))

Referenced by execute_instruction(), and main().

◆ LD_offset_GENERIC

#define LD_offset_GENERIC ( cpu,
reg,
addr,
offset )
Value:
LD_immediate_GENERIC(cpu, reg, cpu->mem[addr + offset])

Procedure that loads a value from memory at address addr+offset to a register.

Parameters
[in,out]cpuThe CPU that holds the register reg
[in]regThe register in which we need to load the data
[in]addrThe base address
[in]offsetThe offset from the address

Definition at line 260 of file cpu.h.

260#define LD_offset_GENERIC(cpu, reg, addr, offset) \
261 LD_immediate_GENERIC(cpu, reg, cpu->mem[addr + offset])

◆ LD_offset_X

#define LD_offset_X ( processor,
addr,
offset )
Value:
(LD_offset_GENERIC(processor, register_x, addr, offset))

Procedure that loads a value from memory at address addr+offset to the X register.

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]addrThe base address
[in]offsetThe offset from the address

Definition at line 278 of file cpu.h.

278#define LD_offset_X(processor, addr, offset) \
279 (LD_offset_GENERIC(processor, register_x, addr, offset))

Referenced by execute_instruction(), and main().

◆ LD_offset_Y

#define LD_offset_Y ( processor,
addr,
offset )
Value:
(LD_offset_GENERIC(processor, register_y, addr, offset))

Procedure that loads a value from memory at address addr+offset to the Y register.

Parameters
[in,out]processorThe CPU in which we need to load the data
[in]addrThe base address
[in]offsetThe offset from the address

Definition at line 287 of file cpu.h.

287#define LD_offset_Y(processor, addr, offset) \
288 (LD_offset_GENERIC(processor, register_y, addr, offset))

Referenced by execute_instruction(), and main().

◆ LOGICAL_FLAGS

#define LOGICAL_FLAGS ( processor)
Value:
({ \
if (processor->accumulator & BIT_7_MASK) { \
CLEAR_ALL_FLAGS(processor); \
SET_N_FLAG(processor); \
} else if (processor->accumulator == 0) { \
CLEAR_ALL_FLAGS(processor); \
SET_Z_FLAG(processor); \
} else { \
CLEAR_ALL_FLAGS(processor); \
} \
})

Procedure that performs the test for every bitwise logic operations.

Parameters
[in,out]processorThe CPU that performed the bitwise operation

Definition at line 726 of file cpu.h.

726#define LOGICAL_FLAGS(processor) \
727 ({ \
728 if (processor->accumulator & BIT_7_MASK) { \
729 CLEAR_ALL_FLAGS(processor); \
730 SET_N_FLAG(processor); \
731 } else if (processor->accumulator == 0) { \
732 CLEAR_ALL_FLAGS(processor); \
733 SET_Z_FLAG(processor); \
734 } else { \
735 CLEAR_ALL_FLAGS(processor); \
736 } \
737 })

◆ NMI

#define NMI ( processor)
Value:
({ \
GENERIC_IR(processor, 0xfffa); \
CLEAR_B_FLAG(processor); \
})

Procedure that performs a non maskable interrupt (when an NMI is pending).

Parameters
[in,out]processorThe CPU that performs the interrupt

Definition at line 1252 of file cpu.h.

1252#define NMI(processor) \
1253 ({ \
1254 GENERIC_IR(processor, 0xfffa); \
1255 CLEAR_B_FLAG(processor); \
1256 })

◆ NO_OPERATION

#define NO_OPERATION ( )

Procedure that litteraly does nothing.

Definition at line 944 of file cpu.h.

◆ OR_absolute

#define OR_absolute ( processor,
addr )
Value:
OR_immediate(processor, processor->mem[addr])
#define OR_immediate(processor, data)
Procedure that performs a bitwise logic OR (A,Z,N = A & data)
Definition cpu.h:789

Procedure that performs a bitwise logic OR (A,Z,N = A&M) using absolute addressing mode.

Parameters
[in,out]processorThe CPU that performs the bitwise logic OR operation
[in]addrThe addressing to the data

Definition at line 800 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ OR_immediate

#define OR_immediate ( processor,
data )
Value:
({ \
processor->accumulator |= data; \
LOGICAL_FLAGS(processor); \
})

Procedure that performs a bitwise logic OR (A,Z,N = A & data)

Parameters
[in,out]processorThe CPU that performs the bitwise logic OR operation
[in]dataThe value to bitwise OR with the accumulator

Definition at line 789 of file cpu.h.

789#define OR_immediate(processor, data) \
790 ({ \
791 processor->accumulator |= data; \
792 LOGICAL_FLAGS(processor); \
793 })

Referenced by execute_instruction(), and main().

◆ OR_indexed_indirect

#define OR_indexed_indirect ( processor,
addr,
offset )
Value:
OR_immediate(processor, indexed_indirect(processor, addr, offset))

Procedure that perform a OR with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mode.

Parameters
[in,out]processorThe CPU that performs the OR with Carry
[in]addrThe base address
[in]offsetThe offset (usually X)

Definition at line 817 of file cpu.h.

817#define OR_indexed_indirect(processor, addr, offset) \
818 OR_immediate(processor, indexed_indirect(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ OR_indirect_indexed

#define OR_indirect_indexed ( processor,
addr,
offset )
Value:
OR_immediate(processor, indirect_indexed(processor, addr, offset))

Procedure that perform a OR with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.

Parameters
[in,out]processorThe CPU that performs the OR with Carry
[in]addrThe base address
[in]offsetThe offset (usually Y)

Definition at line 826 of file cpu.h.

826#define OR_indirect_indexed(processor, addr, offset) \
827 OR_immediate(processor, indirect_indexed(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ OR_offset

#define OR_offset ( processor,
addr,
offset )
Value:
OR_immediate(processor, processor->mem[addr + offset])

Procedure that performs a bitwise logic OR (A,Z,N = A&M) using offset addressing mode.

Parameters
[in,out]processorThe CPU that performs the bitwise logic OR operation
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 808 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ PULL_ACC

#define PULL_ACC ( processor)
Value:
PULL_GENERIC(processor, accumulator)
#define PULL_GENERIC(cpu, reg)
Procedure that pop the top value of the stack and return it to the register reg.
Definition cpu.h:626

Procedure that pop the top value of the stack and return it to the accumulator.

Parameters
[in,out]processorThe CPU holding the accumulator

Definition at line 636 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ PULL_FLAGS

#define PULL_FLAGS ( processor)
Value:
PULL_GENERIC(processor, flags)

Procedure that pop the top value of the stack and return it to the flags register.

Parameters
[in,out]processorThe CPU holding the flags register

Definition at line 642 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ PULL_GENERIC

#define PULL_GENERIC ( cpu,
reg )
Value:
({ \
++(cpu->stack_pointer); \
cpu->reg = cpu->mem[0x1100 | cpu->stack_pointer]; \
})

Procedure that pop the top value of the stack and return it to the register reg.

Parameters
[in,out]cpuThe CPU holding the register reg
[in]reg

Definition at line 626 of file cpu.h.

626#define PULL_GENERIC(cpu, reg) \
627 ({ \
628 ++(cpu->stack_pointer); \
629 cpu->reg = cpu->mem[0x1100 | cpu->stack_pointer]; \
630 })

◆ PULL_PC

#define PULL_PC ( processor)
Value:
({ \
++(processor->stack_pointer); \
processor->program_counter = ((processor->mem[STACK_END | processor->stack_pointer])); \
++(processor->stack_pointer); \
processor->program_counter = (processor->program_counter & 0x00ff) | \
(processor->mem[STACK_END | processor->stack_pointer] << 8); \
})
#define STACK_END
Definition macros.h:46

Procedure that pop the top value of the stack and return it to the program counter.

Parameters
[in,out]processorThe CPU holding the program counter

Definition at line 648 of file cpu.h.

648#define PULL_PC(processor) \
649 ({ \
650 ++(processor->stack_pointer); \
651 processor->program_counter = ((processor->mem[STACK_END | processor->stack_pointer])); \
652 ++(processor->stack_pointer); \
653 processor->program_counter = (processor->program_counter & 0x00ff) | \
654 (processor->mem[STACK_END | processor->stack_pointer] << 8); \
655 })

Referenced by main().

◆ PUSH_ACC

#define PUSH_ACC ( processor)
Value:
PUSH_GENERIC(processor, accumulator)
#define PUSH_GENERIC(cpu, reg)
Procedure that push the reg value to the top of the stack.
Definition cpu.h:582

Procedure that push the accumulator value to the top of the stack.

Parameters
[in,out]processorThe CPU holding the accumulator

Definition at line 603 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ PUSH_FLAGS

#define PUSH_FLAGS ( processor)
Value:
PUSH_GENERIC(processor, flags)

Procedure that push the processor flags to the top of the stack.

Parameters
[in,out]processorThe CPU holding the flags

Definition at line 609 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ PUSH_GENERIC

#define PUSH_GENERIC ( cpu,
reg )
Value:
({ \
cpu->mem[STACK_END | cpu->stack_pointer] = cpu->reg; \
--(cpu->stack_pointer); \
})

Procedure that push the reg value to the top of the stack.

Parameters
[in,out]cpuThe CPU holding the register reg
[in]regThe register holding the value

Definition at line 582 of file cpu.h.

582#define PUSH_GENERIC(cpu, reg) \
583 ({ \
584 cpu->mem[STACK_END | cpu->stack_pointer] = cpu->reg; \
585 --(cpu->stack_pointer); \
586 })

◆ PUSH_immediate

#define PUSH_immediate ( cpu,
d )
Value:
({ \
cpu->mem[STACK_END | cpu->stack_pointer] = d; \
--(cpu->stack_pointer); \
})

Procedure that push an immediate value to the top of the stack.

Parameters
[in,out]cpuThe CPU holding the stack_pointer
[in]dThe immediate to push

Definition at line 593 of file cpu.h.

593#define PUSH_immediate(cpu, d) \
594 ({ \
595 cpu->mem[STACK_END | cpu->stack_pointer] = d; \
596 --(cpu->stack_pointer); \
597 })

◆ PUSH_PC

#define PUSH_PC ( processor)
Value:
({ \
PUSH_immediate(processor, GET_HIGHER(processor->program_counter)); \
PUSH_immediate(processor, GET_LOWER(processor->program_counter)); \
})
#define GET_HIGHER(x)
Definition macros.h:41
#define GET_LOWER(x)
Definition macros.h:40

Procedure that push the program_counter to the top of the stack.

Parameters
[in,out]processorThe CPU holding the program counter

Definition at line 615 of file cpu.h.

615#define PUSH_PC(processor) \
616 ({ \
617 PUSH_immediate(processor, GET_HIGHER(processor->program_counter)); \
618 PUSH_immediate(processor, GET_LOWER(processor->program_counter)); \
619 })

Referenced by main().

◆ RESET

#define RESET ( processor)
Value:
({ \
processor->stack_pointer = 0xff; \
CLEAR_ALL_FLAGS(processor); \
JUMP_indirect(processor, 0xfffc); \
})

Procedure that performs a NES reset interrupt.

Parameters
[in,out]processorThe CPU that performs the interrupt

Definition at line 1262 of file cpu.h.

1262#define RESET(processor) \
1263 ({ \
1264 processor->stack_pointer = 0xff; \
1265 CLEAR_ALL_FLAGS(processor); \
1266 JUMP_indirect(processor, 0xfffc); \
1267 })

◆ RETURN_Interrupt

#define RETURN_Interrupt ( processor)
Value:
({ \
PULL_FLAGS(processor); \
PULL_PC(processor); \
})

Procedure that performs a return from interrupt (RTI instruction) by pulling the flags from the top of the stack and then pulling the PC.

Parameters
[in,out]processorThe CPU that performs the instruction

Definition at line 1298 of file cpu.h.

1298#define RETURN_Interrupt(processor) \
1299 ({ \
1300 PULL_FLAGS(processor); \
1301 PULL_PC(processor); \
1302 })

Referenced by execute_instruction(), and main().

◆ RETURN_Subroutine

#define RETURN_Subroutine ( processor)
Value:
({ \
PULL_PC(processor); \
++processor->program_counter; \
})

Procedure that performs a return from subroutine (RTS instruction) by pulling the top two elements of the stack as the PC (ad then, it increments the PC to prevent some errors)

Parameters
[in,out]processorThe CPU that performs the instruction

Definition at line 1287 of file cpu.h.

1287#define RETURN_Subroutine(processor) \
1288 ({ \
1289 PULL_PC(processor); \
1290 ++processor->program_counter; \
1291 })

Referenced by execute_instruction(), and main().

◆ ROTATION_GENERIC

#define ROTATION_GENERIC ( processor,
field,
operator,
store_offset,
pop_offset,
flag_mask )
Value:
({ \
processor->cache = ((processor->field & flag_mask) >> (store_offset)); \
processor->field = (processor->field operator(1)) | (GET_C_FLAG(processor) << pop_offset); \
CLEAR_C_FLAG(processor); \
processor->flags |= processor->cache; \
if (processor->field == 0) { \
SET_Z_FLAG(processor); \
CLEAR_N_FLAG(processor); \
} else if (processor->field & BIT_7_MASK) { \
SET_N_FLAG(processor); \
CLEAR_Z_FLAG(processor); \
} \
})

Procedure that performs a Bitshift of 1 using a specified operator and placing the overflowing bit inside of the carry flag.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]fieldThe field of the structure that is affected by the operation
[in]operatorThe left or shift operator
[in]store_offsetThe offset to place correctly the bit inside of the Carry flag
[in]pop_offsetThe offset to place correctly the bit inside of the field
[in]flag_maskThe mask to get the correct bit

Definition at line 1128 of file cpu.h.

1128#define ROTATION_GENERIC(processor, field, operator, store_offset, pop_offset, flag_mask) \
1129 ({ \
1130 processor->cache = ((processor->field & flag_mask) >> (store_offset)); \
1131 processor->field = (processor->field operator(1)) | (GET_C_FLAG(processor) << pop_offset); \
1132 CLEAR_C_FLAG(processor); \
1133 processor->flags |= processor->cache; \
1134 if (processor->field == 0) { \
1135 SET_Z_FLAG(processor); \
1136 CLEAR_N_FLAG(processor); \
1137 } else if (processor->field & BIT_7_MASK) { \
1138 SET_N_FLAG(processor); \
1139 CLEAR_Z_FLAG(processor); \
1140 } \
1141 })

◆ ROTATION_LEFT_absolute

#define ROTATION_LEFT_absolute ( processor,
addr )
Value:
ROTATION_GENERIC(processor, mem[addr], <<, 7, 0, BIT_7_MASK)
#define ROTATION_GENERIC(processor, field, operator,store_offset, pop_offset, flag_mask)
Procedure that performs a Bitshift of 1 using a specified operator and placing the overflowing bit in...
Definition cpu.h:1128

Procedure that performs a bit shift with rotation to the left (ROL operation) at the memory address addr. Bit 0 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 7.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address

Definition at line 1166 of file cpu.h.

1166#define ROTATION_LEFT_absolute(processor, addr) \
1167 ROTATION_GENERIC(processor, mem[addr], <<, 7, 0, BIT_7_MASK)

Referenced by execute_instruction(), and main().

◆ ROTATION_LEFT_ACC

#define ROTATION_LEFT_ACC ( processor)
Value:
ROTATION_GENERIC(processor, accumulator, <<, 7, 0, BIT_7_MASK)

Procedure that performs a bit shift with rotation to the left (ROL operation) on the accumulator. Bit 0 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 7.

Parameters
[in,out]processorThe CPU that performs the instruction

Definition at line 1149 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ ROTATION_LEFT_offset

#define ROTATION_LEFT_offset ( processor,
addr,
offset )
Value:
ROTATION_LEFT_absolute(processor, addr + offset)
#define ROTATION_LEFT_absolute(processor, addr)
Procedure that performs a bit shift with rotation to the left (ROL operation) at the memory address a...
Definition cpu.h:1166

Procedure that performs a bit shift with rotation to the left (ROL operation) at the memory address (addr + offset). Bit 0 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 7.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 1187 of file cpu.h.

1187#define ROTATION_LEFT_offset(processor, addr, offset) \
1188 ROTATION_LEFT_absolute(processor, addr + offset)

Referenced by execute_instruction(), and main().

◆ ROTATION_RIGHT_absolute

#define ROTATION_RIGHT_absolute ( processor,
addr )
Value:
ROTATION_GENERIC(processor, mem[addr], >>, 0, 7, BIT_0_MASK)

Procedure that performs a bit shift with rotation to the right (ROR operation) at the memory address addr. Bit 7 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 0.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address

Definition at line 1176 of file cpu.h.

1176#define ROTATION_RIGHT_absolute(processor, addr) \
1177 ROTATION_GENERIC(processor, mem[addr], >>, 0, 7, BIT_0_MASK)

Referenced by execute_instruction(), and main().

◆ ROTATION_RIGHT_ACC

#define ROTATION_RIGHT_ACC ( processor)
Value:
ROTATION_GENERIC(processor, accumulator, >>, 0, 7, BIT_0_MASK)

Procedure that performs a bit shift with rotation to the right (ROR operation) on the accumulator. Bit 7 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 0.

Parameters
[in,out]processorThe CPU that performs the instruction

Definition at line 1157 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ ROTATION_RIGHT_offset

#define ROTATION_RIGHT_offset ( processor,
addr,
offset )
Value:
ROTATION_RIGHT_absolute(processor, addr + offset)
#define ROTATION_RIGHT_absolute(processor, addr)
Procedure that performs a bit shift with rotation to the right (ROR operation) at the memory address ...
Definition cpu.h:1176

Procedure that performs a bit shift with rotation to the right (ROR operation) at the memory address (addr + offset). Bit 7 is filled with the previous state of the C flag and then the C flag is set to the current value of Bit 0.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 1198 of file cpu.h.

1198#define ROTATION_RIGHT_offset(processor, addr, offset) \
1199 ROTATION_RIGHT_absolute(processor, addr + offset)

Referenced by execute_instruction(), and main().

◆ SBC_absolute

#define SBC_absolute ( processor,
addr )
Value:
SBC_immediate(processor, processor->mem[addr])
#define SBC_immediate(processor, data)
Procedure that perform a substract with the carry flag (A,Z,C,N=A-data-C) with an immediate.
Definition cpu.h:880

Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with absolute addressing mode.

Parameters
[in,out]processorThe CPU that performs the substract with Carry
[in]addrThe address of the data to substract

Definition at line 910 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ SBC_immediate

#define SBC_immediate ( processor,
data )
Value:
({ \
processor->cache = (processor->accumulator); \
processor->accumulator -= (data + GET_C_FLAG(processor)); \
if ((processor->cache & BIT_7_MASK) != (processor->accumulator & BIT_7_MASK)) { \
SET_V_FLAG(processor); \
CLEAR_C_FLAG(processor); \
CLEAR_Z_FLAG(processor); \
SET_N_FLAG(processor); \
} else if (processor->accumulator & BIT_7_MASK) { \
CLEAR_V_FLAG(processor); \
SET_N_FLAG(processor); \
CLEAR_Z_FLAG(processor); \
} else if (processor->accumulator == 0) { \
SET_Z_FLAG(processor); \
CLEAR_V_FLAG(processor); \
CLEAR_N_FLAG(processor); \
} else { \
CLEAR_V_FLAG(processor); \
CLEAR_N_FLAG(processor); \
CLEAR_Z_FLAG(processor); \
} \
})

Procedure that perform a substract with the carry flag (A,Z,C,N=A-data-C) with an immediate.

Parameters
[in,out]processorThe CPU that performs the substract with Carry
[in]dataThe immediate (a byte)

Definition at line 880 of file cpu.h.

880#define SBC_immediate(processor, data) \
881 ({ \
882 processor->cache = (processor->accumulator); \
883 processor->accumulator -= (data + GET_C_FLAG(processor)); \
884 if ((processor->cache & BIT_7_MASK) != (processor->accumulator & BIT_7_MASK)) { \
885 SET_V_FLAG(processor); \
886 CLEAR_C_FLAG(processor); \
887 CLEAR_Z_FLAG(processor); \
888 SET_N_FLAG(processor); \
889 } else if (processor->accumulator & BIT_7_MASK) { \
890 CLEAR_V_FLAG(processor); \
891 SET_N_FLAG(processor); \
892 CLEAR_Z_FLAG(processor); \
893 } else if (processor->accumulator == 0) { \
894 SET_Z_FLAG(processor); \
895 CLEAR_V_FLAG(processor); \
896 CLEAR_N_FLAG(processor); \
897 } else { \
898 CLEAR_V_FLAG(processor); \
899 CLEAR_N_FLAG(processor); \
900 CLEAR_Z_FLAG(processor); \
901 } \
902 })

Referenced by execute_instruction(), and main().

◆ SBC_indexed_indirect

#define SBC_indexed_indirect ( processor,
addr,
offset )
Value:
SBC_immediate(processor, indexed_indirect(processor, addr, offset))

Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with indexed indirect addressing mode.

Parameters
[in,out]processorThe CPU that performs the substract with Carry
[in]addrThe base address
[in]offsetThe offset (usually X)

Definition at line 928 of file cpu.h.

928#define SBC_indexed_indirect(processor, addr, offset) \
929 SBC_immediate(processor, indexed_indirect(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ SBC_indirect_indexed

#define SBC_indirect_indexed ( processor,
addr,
offset )
Value:
SBC_immediate(processor, indirect_indexed(processor, addr, offset))

Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with indirect indexed addressing mode.

Parameters
[in,out]processorThe CPU that performs the substract with Carry
[in]addrThe base address
[in]offsetThe offset (usually Y)

Definition at line 938 of file cpu.h.

938#define SBC_indirect_indexed(processor, addr, offset) \
939 SBC_immediate(processor, indirect_indexed(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ SBC_offset

#define SBC_offset ( processor,
addr,
offset )
Value:
SBC_immediate(processor, processor->mem[addr + offset])

Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with offset addressing mode.

Parameters
[in,out]processorThe CPU that performs the substract with Carry
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 919 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ SET_ALL_FLAGS

#define SET_ALL_FLAGS ( x)
Value:
(x->flags = x->flags | 0xFF)

Definition at line 137 of file cpu.h.

◆ SET_B_FLAG

#define SET_B_FLAG ( x)
Value:
(x->flags = x->flags | BIT_4_MASK)

Definition at line 132 of file cpu.h.

◆ SET_C_FLAG

#define SET_C_FLAG ( x)
Value:
(x->flags = x->flags | BIT_0_MASK)

Definition at line 128 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ SET_D_FLAG

#define SET_D_FLAG ( x)
Value:
(x->flags = x->flags | BIT_3_MASK)

Definition at line 131 of file cpu.h.

Referenced by execute_instruction().

◆ SET_I_FLAG

#define SET_I_FLAG ( x)
Value:
(x->flags = x->flags | BIT_2_MASK)

Definition at line 130 of file cpu.h.

Referenced by execute_instruction().

◆ SET_N_FLAG

#define SET_N_FLAG ( x)
Value:
(x->flags = x->flags | BIT_7_MASK)

Definition at line 135 of file cpu.h.

◆ SET_P_FLAG

#define SET_P_FLAG ( x)
Value:
(x->flags = x->flags | BIT_5_MASK)

Definition at line 133 of file cpu.h.

◆ SET_V_FLAG

#define SET_V_FLAG ( x)
Value:
(x->flags = x->flags | BIT_6_MASK)

Definition at line 134 of file cpu.h.

Referenced by main().

◆ SET_Z_FLAG

#define SET_Z_FLAG ( x)
Value:
(x->flags = x->flags | BIT_1_MASK)

Definition at line 129 of file cpu.h.

◆ SHIFT_GENERIC

#define SHIFT_GENERIC ( processor,
field,
operator,
flag_shift_offset,
flag_mask )
Value:
({ \
CLEAR_C_FLAG(processor); \
processor->flags |= ((processor->field & flag_mask) >> (flag_shift_offset)); \
processor->field = processor->field operator(1); \
if (processor->field == 0) { \
SET_Z_FLAG(processor); \
CLEAR_N_FLAG(processor); \
} else if (processor->field & BIT_7_MASK) { \
SET_N_FLAG(processor); \
CLEAR_Z_FLAG(processor); \
} \
})

Procedure that performs a Bitshift of 1 using a specified operator and placing the overflowing bit inside of the carry flag.

Parameters
[in,out]processorThe CPU that performs the instruction
[in]fieldThe field of the structure that is affected by the operation
[in]operatorThe left or shift operator
[in]flag_shift_offsetThe offset to place correctly the bit inside of the Carry flag
[in]flag_maskThe mask to get the correct bit

Definition at line 1057 of file cpu.h.

1057#define SHIFT_GENERIC(processor, field, operator, flag_shift_offset, flag_mask) \
1058 ({ \
1059 CLEAR_C_FLAG(processor); \
1060 processor->flags |= ((processor->field & flag_mask) >> (flag_shift_offset)); \
1061 processor->field = processor->field operator(1); \
1062 if (processor->field == 0) { \
1063 SET_Z_FLAG(processor); \
1064 CLEAR_N_FLAG(processor); \
1065 } else if (processor->field & BIT_7_MASK) { \
1066 SET_N_FLAG(processor); \
1067 CLEAR_Z_FLAG(processor); \
1068 } \
1069 })

◆ SHIFT_LEFT_absolute

#define SHIFT_LEFT_absolute ( processor,
addr )
Value:
SHIFT_GENERIC(processor, mem[addr], <<, 7, BIT_7_MASK)
#define SHIFT_GENERIC(processor, field, operator,flag_shift_offset, flag_mask)
Procedure that performs a Bitshift of 1 using a specified operator and placing the overflowing bit in...
Definition cpu.h:1057

Procedure that performs a bitshift to the left (ASL operation) at the address addr and place the 7th bit inside of the Carry flag.

Parameters
[in,out]processorThe CPU that performs the operation
[in]addrThe base address

Definition at line 1091 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ SHIFT_LEFT_ACC

#define SHIFT_LEFT_ACC ( processor)
Value:
SHIFT_GENERIC(processor, accumulator, <<, 7, BIT_7_MASK)

Procedure that performs a bitshift to the left (ASL operation) on the accumulator and place the 7th bit inside of the Carry flag.

Parameters
[in,out]processorThe CPU that performs the operation

Definition at line 1076 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ SHIFT_LEFT_offset

#define SHIFT_LEFT_offset ( processor,
addr,
offset )
Value:
SHIFT_LEFT_absolute(processor, addr + offset)
#define SHIFT_LEFT_absolute(processor, addr)
Procedure that performs a bitshift to the left (ASL operation) at the address addr and place the 7th ...
Definition cpu.h:1091

Procedure that performs a bitshift to the left (ASL operation) at the address (addr + offset) and place the 7th bit inside of the Carry flag.

Parameters
[in,out]processorThe CPU that performs the operation
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 1107 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ SHIFT_RIGHT_absolute

#define SHIFT_RIGHT_absolute ( processor,
addr )
Value:
SHIFT_GENERIC(processor, mem[addr], >>, 0, BIT_0_MASK)

Procedure that performs a bitshift to the right (LSR operation) at the address addr and place the 0th bit inside of the Carry flag.

Parameters
[in,out]processorThe CPU that performs the operation
[in]addrThe base address

Definition at line 1098 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ SHIFT_RIGHT_ACC

#define SHIFT_RIGHT_ACC ( processor)
Value:
SHIFT_GENERIC(processor, accumulator, >>, 0, BIT_0_MASK)

Procedure that performs a bitshift to the right (LSR operation) on the accumulator and place the 0th bit inside of the Carry flag.

Parameters
[in,out]processorThe CPU that performs the operation

Definition at line 1083 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ SHIFT_RIGHT_offset

#define SHIFT_RIGHT_offset ( processor,
addr,
offset )
Value:
SHIFT_RIGHT_absolute(processor, addr + offset)
#define SHIFT_RIGHT_absolute(processor, addr)
Procedure that performs a bitshift to the right (LSR operation) at the address addr and place the 0th...
Definition cpu.h:1098

Procedure that performs a bitshift to the right (LSR operation) at the address (addr + offset) and place the 0th bit inside of the Carry flag.

Parameters
[in,out]processorThe CPU that performs the operation
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 1116 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ STR_absolute_ACC

#define STR_absolute_ACC ( processor,
addr )
Value:
STR_absolute_GENERIC(processor, accumulator, addr)
#define STR_absolute_GENERIC(cpu, reg, addr)
Procedure that store the register reg value to a memory address.
Definition cpu.h:450

Procedure that store the accumulator value to a memory address.

Parameters
[in,out]processorThe CPU that holds the accumulator
[in]addrThe address to the memory

Definition at line 457 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ STR_absolute_GENERIC

#define STR_absolute_GENERIC ( cpu,
reg,
addr )
Value:
({ cpu->mem[addr] = cpu->reg; })

Procedure that store the register reg value to a memory address.

Parameters
[in,out]cpuThe CPU that holds the register reg
[in]regThe register that holds the value
[in]addrThe address to the memory block

Definition at line 450 of file cpu.h.

◆ STR_absolute_X

#define STR_absolute_X ( processor,
addr )
Value:
STR_absolute_GENERIC(processor, register_x, addr)

Procedure that store the X register value to a memory address.

Parameters
[in,out]processorThe CPU that holds the X register
[in]addrThe address to the memory

Definition at line 464 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ STR_absolute_Y

#define STR_absolute_Y ( processor,
addr )
Value:
STR_absolute_GENERIC(processor, register_y, addr)

Procedure that store the Y register value to a memory address.

Parameters
[in,out]processorThe CPU that holds the Y register
[in]addrThe address to the memory

Definition at line 471 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ STR_indexed_indirect_ACC

#define STR_indexed_indirect_ACC ( processor,
addr,
offset )
Value:
({ indexed_indirect(processor, addr, offset) = processor->accumulator; })

Procedure that store the accumulator value to a memory address using indexes indirect addressing mode.

Parameters
[in,out]processorThe CPU that holds the accumulator
[in]addrA ZERO PAGE address !
[in]offsetThe offset (usually X)

Definition at line 526 of file cpu.h.

526#define STR_indexed_indirect_ACC(processor, addr, offset) \
527 ({ indexed_indirect(processor, addr, offset) = processor->accumulator; })

Referenced by execute_instruction(), and main().

◆ STR_indirect_indexed_ACC

#define STR_indirect_indexed_ACC ( processor,
addr,
offset )
Value:
({ indirect_indexed(processor, addr, offset) = processor->accumulator; })

Procedure that store the accumulator value to a memory address using indirect indexed addressing mode.

Parameters
[in,out]processorThe CPU that holds the accumulator
[in]addrA ZERO PAGE address !
[in]offsetThe offset (usually Y)

Definition at line 516 of file cpu.h.

516#define STR_indirect_indexed_ACC(processor, addr, offset) \
517 ({ indirect_indexed(processor, addr, offset) = processor->accumulator; })

Referenced by execute_instruction(), and main().

◆ STR_offset_ACC

#define STR_offset_ACC ( processor,
addr,
offset )
Value:
STR_offset_GENERIC(processor, accumulator, addr, offset)
#define STR_offset_GENERIC(cpu, reg, addr, offset)
Procedure that store the register reg value to a memory address + offset.
Definition cpu.h:480

Procedure that store the accumulator value to a memory address + offset.

Parameters
[in,out]processorThe CPU that holds the accumulator
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 488 of file cpu.h.

488#define STR_offset_ACC(processor, addr, offset) \
489 STR_offset_GENERIC(processor, accumulator, addr, offset)

Referenced by execute_instruction(), and main().

◆ STR_offset_GENERIC

#define STR_offset_GENERIC ( cpu,
reg,
addr,
offset )
Value:
STR_absolute_GENERIC(cpu, reg, addr + offset)

Procedure that store the register reg value to a memory address + offset.

Parameters
[in,out]cpuThe CPU that holds the register reg
[in]regThe register that stores the value
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 480 of file cpu.h.

◆ STR_offset_X

#define STR_offset_X ( processor,
addr,
offset )
Value:
STR_offset_GENERIC(processor, register_x, addr, offset)

Procedure that store the X register value to a memory address + offset.

Parameters
[in,out]processorThe CPU that holds the X register
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 497 of file cpu.h.

497#define STR_offset_X(processor, addr, offset) \
498 STR_offset_GENERIC(processor, register_x, addr, offset)

Referenced by execute_instruction(), and main().

◆ STR_offset_Y

#define STR_offset_Y ( processor,
addr,
offset )
Value:
STR_offset_GENERIC(processor, register_y, addr, offset)

Procedure that store the Y register value to a memory address + offset.

Parameters
[in,out]processorThe CPU that holds the Y register
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 506 of file cpu.h.

506#define STR_offset_Y(processor, addr, offset) \
507 STR_offset_GENERIC(processor, register_y, addr, offset)

Referenced by execute_instruction(), and main().

◆ TR_ACC_X

#define TR_ACC_X ( processor)
Value:
TR_GENERIC(processor, accumulator, register_x)
#define TR_GENERIC(cpu, reg1, reg2)
Procedure that store the value in reg1 in reg2.
Definition cpu.h:535

Procedure that store the value in the accumulator in the X register.

Parameters
[in,out]processorThe CPU holding both registers

Definition at line 545 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ TR_ACC_Y

#define TR_ACC_Y ( processor)
Value:
TR_GENERIC(processor, accumulator, register_y)

Procedure that store the value in accumulator in the Y register.

Parameters
[in,out]processorThe CPU holding both registers

Definition at line 551 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ TR_GENERIC

#define TR_GENERIC ( cpu,
reg1,
reg2 )
Value:
({ \
cpu->reg2 = cpu->reg1; \
LD_FLAGS_GENERIC(cpu, reg2); \
})

Procedure that store the value in reg1 in reg2.

Parameters
[in,out]cpuThe CPU that holds both registers
[in]reg1The register holding the value
[in]reg2The register in which we load the value

Definition at line 535 of file cpu.h.

535#define TR_GENERIC(cpu, reg1, reg2) \
536 ({ \
537 cpu->reg2 = cpu->reg1; \
538 LD_FLAGS_GENERIC(cpu, reg2); \
539 })

◆ TR_SP_X

#define TR_SP_X ( processor)
Value:
TR_GENERIC(processor, stack_pointer, register_x)

Procedure that store the stack pointer in the X register.

Parameters
[in,out]processorThe CPU holding both registers

Definition at line 569 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ TR_X_ACC

#define TR_X_ACC ( processor)
Value:
TR_GENERIC(processor, register_x, accumulator)

Procedure that store the value in the X register in the accumulator.

Parameters
[in,out]processorThe CPU holding both registers

Definition at line 557 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ TR_X_SP

#define TR_X_SP ( processor)
Value:
TR_GENERIC(processor, register_x, stack_pointer)

Procedure that store the value in the X register in the stack pointer.

Parameters
[in,out]processorThe CPU holding both registers

Definition at line 575 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ TR_Y_ACC

#define TR_Y_ACC ( processor)
Value:
TR_GENERIC(processor, register_y, accumulator)

Procedure that store the value in the Y register in the accumulator.

Parameters
[in,out]processorThe CPU holding both registers

Definition at line 563 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ XOR_absolute

#define XOR_absolute ( processor,
addr )
Value:
XOR_immediate(processor, processor->mem[addr])
#define XOR_immediate(processor, data)
Procedure that performs a bitwise logic XOR (A,Z,N = A & data)
Definition cpu.h:834

Procedure that performs a bitwise logic XOR (A,Z,N = A&M) using absolute addressing mode.

Parameters
[in,out]processorThe CPU that performs the bitwise logic XOR operation
[in]addrThe addressing to the data

Definition at line 845 of file cpu.h.

Referenced by execute_instruction(), and main().

◆ XOR_immediate

#define XOR_immediate ( processor,
data )
Value:
({ \
processor->accumulator ^= data; \
LOGICAL_FLAGS(processor); \
})

Procedure that performs a bitwise logic XOR (A,Z,N = A & data)

Parameters
[in,out]processorThe CPU that performs the bitwise logic OR operation
[in]dataThe value to bitwise XOR with the accumulator

Definition at line 834 of file cpu.h.

834#define XOR_immediate(processor, data) \
835 ({ \
836 processor->accumulator ^= data; \
837 LOGICAL_FLAGS(processor); \
838 })

Referenced by execute_instruction(), and main().

◆ XOR_indexed_indirect

#define XOR_indexed_indirect ( processor,
addr,
offset )
Value:
XOR_immediate(processor, indexed_indirect(processor, addr, offset))

Procedure that perform a XOR with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mode.

Parameters
[in,out]processorThe CPU that performs the XOR with Carry
[in]addrThe base address
[in]offsetThe offset (usually X)

Definition at line 862 of file cpu.h.

862#define XOR_indexed_indirect(processor, addr, offset) \
863 XOR_immediate(processor, indexed_indirect(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ XOR_indirect_indexed

#define XOR_indirect_indexed ( processor,
addr,
offset )
Value:
XOR_immediate(processor, indirect_indexed(processor, addr, offset))

Procedure that perform a XOR with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.

Parameters
[in,out]processorThe CPU that performs the XOR with Carry
[in]addrThe base address
[in]offsetThe offset (usually Y)

Definition at line 871 of file cpu.h.

871#define XOR_indirect_indexed(processor, addr, offset) \
872 XOR_immediate(processor, indirect_indexed(processor, addr, offset))

Referenced by execute_instruction(), and main().

◆ XOR_offset

#define XOR_offset ( processor,
addr,
offset )
Value:
XOR_immediate(processor, processor->mem[addr + offset])

Procedure that performs a bitwise logic XOR (A,Z,N = A&M) using offset addressing mode.

Parameters
[in,out]processorThe CPU that performs the bitwise logic XOR operation
[in]addrThe base address
[in]offsetThe offset from the base address

Definition at line 853 of file cpu.h.

Referenced by execute_instruction(), and main().

Function Documentation

◆ execute_instruction()

errcode_t execute_instruction ( CPU * cpu)

Function that execute the current instruction in the ROM.

Parameters
[in,out]cpuThe CPU that execute the current instruction
Returns
0 if everything is fine, 0xff if there's an abnormal opcode (error bubbling)

Definition at line 575 of file cpu.c.

575 {
576
577 switch (cpu->mem[cpu->program_counter]) {
578
579 case ADC_I:
580 ADD_immediate(cpu, cpu->mem[cpu->program_counter + 1]);
581 break;
582
583 case ADC_Z:
584 ADD_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
585 break;
586
587 case ADC_ZX:
588 ADD_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
589 break;
590
591 case ADC_A:
592 ADD_absolute(cpu, get_address(cpu));
593 break;
594
595 case ADC_AX:
596 ADD_offset(cpu, get_address(cpu), cpu->register_x);
597 break;
598
599 case ADC_AY:
600 ADD_offset(cpu, get_address(cpu), cpu->register_y);
601 break;
602
603 case ADC_IdX:
604 ADD_indexed_indirect(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
605 break;
606
607 case ADC_IdY:
608 ADD_indirect_indexed(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_y);
609 break;
610
611 case AND_I:
612 AND_immediate(cpu, cpu->mem[cpu->program_counter + 1]);
613 break;
614
615 case AND_Z:
616 AND_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
617 break;
618
619 case AND_ZX:
620 AND_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
621 break;
622
623 case AND_A:
624 AND_absolute(cpu, get_address(cpu));
625 break;
626
627 case AND_AX:
628 AND_offset(cpu, get_address(cpu), cpu->register_x);
629 break;
630
631 case AND_AY:
632 AND_offset(cpu, get_address(cpu), cpu->register_y);
633 break;
634
635 case AND_IdX:
636 AND_indexed_indirect(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
637 break;
638
639 case AND_IdY:
640 AND_indirect_indexed(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_y);
641 break;
642
643 case ASL_Ac:
644 SHIFT_LEFT_ACC(cpu);
645 break;
646
647 case ASL_Z:
648 SHIFT_LEFT_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
649 break;
650
651 case ASL_ZX:
652 SHIFT_LEFT_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
653 break;
654
655 case ASL_A:
657 break;
658
659 case ASL_AX:
661 break;
662
663 case BCC:
664 BRANCH_Carry_C(cpu, cpu->mem[cpu->program_counter + 1]);
665 break;
666
667 case BCS:
668 BRANCH_Carry_S(cpu, cpu->mem[cpu->program_counter + 1]);
669 break;
670
671 case BEQ:
672 BRANCH_Equal(cpu, cpu->mem[cpu->program_counter + 1]);
673 break;
674
675 case BIT_Z:
676 BIT_TEST(cpu, cpu->mem[cpu->program_counter + 1]);
677 break;
678
679 case BIT_A:
680 BIT_TEST(cpu, get_address(cpu));
681 break;
682
683 case BMI:
684 BRANCH_If_Minus(cpu, cpu->mem[cpu->program_counter + 1]);
685 break;
686
687 case BNE:
688 BRANCH_Not_Equal(cpu, cpu->mem[cpu->program_counter + 1]);
689 break;
690
691 case BPL:
692 BRANCH_If_Positive(cpu, cpu->mem[cpu->program_counter + 1]);
693 break;
694
695 case BRK:
696 BREAK(cpu);
697 break;
698
699 case BVC:
700 BRANCH_Overflow_C(cpu, cpu->mem[cpu->program_counter + 1]);
701 break;
702
703 case BVS:
704 BRANCH_Overflow_S(cpu, cpu->mem[cpu->program_counter + 1]);
705 break;
706
707 case CLC:
708 CLEAR_C_FLAG(cpu);
709 break;
710
711 case CLD:
712 CLEAR_D_FLAG(cpu);
713 break;
714
715 case CLI:
716 CLEAR_I_FLAG(cpu);
717 break;
718
719 case CLV:
720 CLEAR_V_FLAG(cpu);
721 break;
722
723 case CMP_I:
724 COMPARE_Immediate_ACC(cpu, cpu->mem[cpu->program_counter + 1]);
725 break;
726
727 case CMP_A:
729 break;
730
731 case CMP_AX:
733 break;
734
735 case CMP_AY:
737 break;
738
739 case CMP_IdX:
741 break;
742
743 case CMP_IdY:
745 break;
746
747 case CMP_Z:
748 COMPARE_absolute_ACC(cpu, cpu->mem[cpu->program_counter + 1]);
749 break;
750
751 case CMP_ZX:
752 COMPARE_offset_ACC(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
753 break;
754
755 case CPX_I:
756 COMPARE_Immediate_X(cpu, cpu->mem[cpu->program_counter + 1]);
757 break;
758
759 case CPX_Z:
760 COMPARE_absolute_X(cpu, cpu->mem[cpu->program_counter + 1]);
761 break;
762
763 case CPX_A:
765 break;
766
767 case CPY_I:
768 COMPARE_Immediate_Y(cpu, cpu->mem[cpu->program_counter + 1]);
769 break;
770
771 case CPY_Z:
772 COMPARE_absolute_Y(cpu, cpu->mem[cpu->program_counter + 1]);
773 break;
774
775 case CPY_A:
777 break;
778
779 case DEX:
780 DEC_X(cpu);
781 break;
782
783 case DEY:
784 DEC_Y(cpu);
785 break;
786
787 case DEC_Z:
788 DEC_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
789 break;
790
791 case DEC_A:
792 DEC_absolute(cpu, get_address(cpu));
793 break;
794
795 case DEC_ZX:
796 DEC_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
797 break;
798
799 case DEC_AX:
800 DEC_offset(cpu, get_address(cpu), cpu->register_x);
801 break;
802
803 case INC_Z:
804 INC_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
805 break;
806
807 case INC_A:
808 INC_absolute(cpu, get_address(cpu));
809 break;
810
811 case INC_ZX:
812 INC_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
813 break;
814
815 case INC_AX:
816 INC_offset(cpu, get_address(cpu), cpu->register_x);
817 break;
818
819 case INX:
820 INC_X(cpu);
821 break;
822
823 case INY:
824 INC_Y(cpu);
825 break;
826
827 case JMP_A:
828 JUMP_absolute(cpu, get_address(cpu));
829 break;
830
831 case JMP_Id:
832 JUMP_indirect(cpu, get_address(cpu));
833 break;
834
835 case EOR_I:
836 XOR_immediate(cpu, cpu->mem[cpu->program_counter + 1]);
837 break;
838
839 case EOR_Z:
840 XOR_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
841 break;
842
843 case EOR_ZX:
844 XOR_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
845 break;
846
847 case EOR_A:
848 XOR_absolute(cpu, get_address(cpu));
849 break;
850
851 case EOR_AX:
852 XOR_offset(cpu, get_address(cpu), cpu->register_x);
853 break;
854
855 case EOR_AY:
856 XOR_offset(cpu, get_address(cpu), cpu->register_y);
857 break;
858
859 case EOR_IdX:
861 break;
862
863 case EOR_IdY:
865 break;
866
867 case JSR:
868 JUMP_Subroutine(cpu, get_address(cpu));
869 break;
870
871 case LDA_I:
872 LD_immediate_ACC(cpu, cpu->mem[cpu->program_counter + 1]);
873 break;
874
875 case LDA_Z:
876 LD_absolute_ACC(cpu, cpu->mem[cpu->program_counter + 1]);
877 break;
878
879 case LDA_ZX:
880 LD_offset_ACC(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
881 break;
882
883 case LDA_A:
884 LD_absolute_ACC(cpu, get_address(cpu));
885 break;
886
887 case LDA_AX:
888 LD_offset_ACC(cpu, get_address(cpu), cpu->register_x);
889 break;
890
891 case LDA_AY:
892 LD_offset_ACC(cpu, get_address(cpu), cpu->register_y);
893 break;
894
895 case LDA_IdX:
897 break;
898
899 case LDA_IdY:
901 break;
902
903 case LDX_I:
904 LD_immediate_X(cpu, cpu->mem[cpu->program_counter + 1]);
905 break;
906
907 case LDX_Z:
908 LD_absolute_X(cpu, cpu->mem[cpu->program_counter + 1]);
909 break;
910
911 case LDX_ZY:
912 LD_offset_X(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_y);
913 break;
914
915 case LDX_A:
916 LD_absolute_X(cpu, get_address(cpu));
917 break;
918
919 case LDX_AY:
920 LD_offset_X(cpu, get_address(cpu), cpu->register_y);
921 break;
922
923 case LDY_I:
924 LD_immediate_Y(cpu, cpu->mem[cpu->program_counter + 1]);
925 break;
926
927 case LDY_Z:
928 LD_absolute_Y(cpu, cpu->mem[cpu->program_counter + 1]);
929 break;
930
931 case LDY_ZX:
932 LD_offset_Y(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_y);
933 break;
934
935 case LDY_A:
936 LD_absolute_Y(cpu, get_address(cpu));
937 break;
938
939 case LDY_AX:
940 LD_offset_Y(cpu, get_address(cpu), cpu->register_x);
941 break;
942
943 case LSR_Ac:
944 SHIFT_RIGHT_ACC(cpu);
945 break;
946
947 case LSR_Z:
948 SHIFT_RIGHT_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
949 break;
950
951 case LSR_ZX:
952 SHIFT_RIGHT_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
953 break;
954
955 case LSR_A:
957 break;
958
959 case LSR_AX:
961 break;
962
963 case NOP:
964 break;
965
966 case ORA_I:
967 OR_immediate(cpu, cpu->mem[cpu->program_counter + 1]);
968 break;
969
970 case ORA_Z:
971 OR_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
972 break;
973
974 case ORA_ZX:
975 OR_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
976 break;
977
978 case ORA_A:
979 OR_absolute(cpu, get_address(cpu));
980 break;
981
982 case ORA_AX:
983 OR_offset(cpu, get_address(cpu), cpu->register_x);
984 break;
985
986 case ORA_AY:
987 OR_offset(cpu, get_address(cpu), cpu->register_y);
988 break;
989
990 case ORA_IdX:
992 break;
993
994 case ORA_IdY:
996 break;
997
998 case PHA:
999 PUSH_ACC(cpu);
1000 break;
1001
1002 case PHP:
1003 PUSH_FLAGS(cpu);
1004 break;
1005
1006 case PLA:
1007 PULL_ACC(cpu);
1008 break;
1009
1010 case PLP:
1011 PULL_FLAGS(cpu);
1012 break;
1013
1014 case ROL_Ac:
1015 ROTATION_LEFT_ACC(cpu);
1016 break;
1017
1018 case ROL_Z:
1019 ROTATION_LEFT_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
1020 break;
1021
1022 case ROL_ZX:
1023 ROTATION_LEFT_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
1024 break;
1025
1026 case ROL_A:
1028 break;
1029
1030 case ROL_AX:
1032 break;
1033
1034 case ROR_Ac:
1035 ROTATION_RIGHT_ACC(cpu);
1036 break;
1037
1038 case ROR_Z:
1039 ROTATION_RIGHT_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
1040 break;
1041
1042 case ROR_ZX:
1043 ROTATION_RIGHT_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
1044 break;
1045
1046 case ROR_A:
1048 break;
1049
1050 case ROR_AX:
1052 break;
1053
1054 case RTI:
1055 RETURN_Interrupt(cpu);
1056 break;
1057
1058 case RTS:
1059 RETURN_Subroutine(cpu);
1060 break;
1061
1062 case SBC_I:
1063 SBC_immediate(cpu, cpu->mem[cpu->program_counter + 1]);
1064 break;
1065
1066 case SBC_Z:
1067 SBC_absolute(cpu, cpu->mem[cpu->program_counter + 1]);
1068 break;
1069
1070 case SBC_ZX:
1071 SBC_offset(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
1072 break;
1073
1074 case SBC_A:
1075 SBC_absolute(cpu, get_address(cpu));
1076 break;
1077
1078 case SBC_AX:
1079 SBC_offset(cpu, get_address(cpu), cpu->register_x);
1080 break;
1081
1082 case SBC_AY:
1083 SBC_offset(cpu, get_address(cpu), cpu->register_y);
1084 break;
1085
1086 case SBC_IdX:
1088 break;
1089
1090 case SBC_IdY:
1092 break;
1093
1094 case SEC:
1095 SET_C_FLAG(cpu);
1096 break;
1097
1098 case SED:
1099 SET_D_FLAG(cpu);
1100 break;
1101
1102 case SEI:
1103 SET_I_FLAG(cpu);
1104 break;
1105
1106 case STA_Z:
1107 STR_absolute_ACC(cpu, cpu->mem[cpu->program_counter + 1]);
1108 break;
1109
1110 case STA_ZX:
1111 STR_offset_ACC(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
1112 break;
1113
1114 case STA_A:
1115 STR_absolute_ACC(cpu, get_address(cpu));
1116 break;
1117
1118 case STA_AX:
1119 STR_offset_ACC(cpu, get_address(cpu), cpu->register_x);
1120 break;
1121
1122 case STA_AY:
1123 STR_offset_ACC(cpu, get_address(cpu), cpu->register_y);
1124 break;
1125
1126 case STA_IdX:
1128 break;
1129
1130 case STA_IdY:
1132 break;
1133
1134 case STX_Z:
1135 STR_absolute_X(cpu, cpu->mem[cpu->program_counter + 1]);
1136 break;
1137
1138 case STX_ZY:
1139 STR_offset_X(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_y);
1140 break;
1141
1142 case STX_A:
1143 STR_absolute_X(cpu, get_address(cpu));
1144 break;
1145
1146 case STY_Z:
1147 STR_absolute_Y(cpu, cpu->mem[cpu->program_counter + 1]);
1148 break;
1149
1150 case STY_ZX:
1151 STR_offset_Y(cpu, cpu->mem[cpu->program_counter + 1], cpu->register_x);
1152 break;
1153
1154 case STY_A:
1155 STR_absolute_Y(cpu, get_address(cpu));
1156 break;
1157
1158 case TAX:
1159 TR_ACC_X(cpu);
1160 break;
1161
1162 case TSX:
1163 TR_SP_X(cpu);
1164 break;
1165
1166 case TXA:
1167 TR_X_ACC(cpu);
1168 break;
1169
1170 case TYA:
1171 TR_Y_ACC(cpu);
1172 break;
1173
1174 case TAY:
1175 TR_ACC_Y(cpu);
1176 break;
1177
1178 case TXS:
1179 TR_X_SP(cpu);
1180 break;
1181
1182 default:
1183 return ERR_ABN_OPCODE;
1184 break;
1185 }
1186
1187 return 0;
1188}
#define ADD_indexed_indirect(processor, addr, offset)
Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mod...
Definition cpu.h:710
#define STR_absolute_X(processor, addr)
Procedure that store the X register value to a memory address.
Definition cpu.h:464
#define COMPARE_indexed_indirect_ACC(processor, addr, offset)
Procedure that performs a CMP operation (Z, C, N = A - M) with an indexed indirect addressing mode.
Definition cpu.h:1358
#define TR_ACC_X(processor)
Procedure that store the value in the accumulator in the X register.
Definition cpu.h:545
#define OR_indexed_indirect(processor, addr, offset)
Procedure that perform a OR with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mode...
Definition cpu.h:817
#define PULL_FLAGS(processor)
Procedure that pop the top value of the stack and return it to the flags register.
Definition cpu.h:642
#define COMPARE_absolute_Y(processor, addr)
Procedure that performs a CMP operation (Z, C, N = Y - M) with an indirect indexed addressing mode.
Definition cpu.h:1403
#define OR_absolute(processor, addr)
Procedure that performs a bitwise logic OR (A,Z,N = A&M) using absolute addressing mode.
Definition cpu.h:800
#define BRANCH_If_Minus(processor, offset)
Procedure that performs a BMI (branch if positive, if the N flag is clear)
Definition cpu.h:1025
#define AND_indirect_indexed(processor, addr, offset)
Procedure that perform a AND with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.
Definition cpu.h:781
#define SET_D_FLAG(x)
Definition cpu.h:131
#define TR_ACC_Y(processor)
Procedure that store the value in accumulator in the Y register.
Definition cpu.h:551
#define ROTATION_RIGHT_offset(processor, addr, offset)
Procedure that performs a bit shift with rotation to the right (ROR operation) at the memory address ...
Definition cpu.h:1198
#define ROTATION_RIGHT_ACC(processor)
Procedure that performs a bit shift with rotation to the right (ROR operation) on the accumulator....
Definition cpu.h:1157
#define TR_X_SP(processor)
Procedure that store the value in the X register in the stack pointer.
Definition cpu.h:575
#define STR_offset_ACC(processor, addr, offset)
Procedure that store the accumulator value to a memory address + offset.
Definition cpu.h:488
#define DEC_Y(processor)
Procedure that decrements the Y register.
Definition cpu.h:424
#define XOR_indexed_indirect(processor, addr, offset)
Procedure that perform a XOR with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mod...
Definition cpu.h:862
#define get_address(cpu)
Macro to recreate the current address pointed by pc + 1.
Definition cpu.h:180
#define STR_offset_X(processor, addr, offset)
Procedure that store the X register value to a memory address + offset.
Definition cpu.h:497
#define DEC_offset(processor, addr, offset)
Procedure that decrement a specific memory location.
Definition cpu.h:392
#define STR_absolute_ACC(processor, addr)
Procedure that store the accumulator value to a memory address.
Definition cpu.h:457
#define LD_indirect_indexed_ACC(processor, addr, offset)
Procedure that loads to the accmulator using Inderect Indexed.
Definition cpu.h:441
#define SET_I_FLAG(x)
Definition cpu.h:130
#define COMPARE_indirect_indexed_ACC(processor, addr, offset)
Procedure that performs a CMP operation (Z, C, N = A - M) with an indirect indexed addressing mode.
Definition cpu.h:1368
#define ADD_indirect_indexed(processor, addr, offset)
Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.
Definition cpu.h:719
#define AND_absolute(processor, addr)
Procedure that performs a bitwise logic AND (A,Z,N = A&M) using absolute addressing mode.
Definition cpu.h:755
#define BRANCH_Carry_S(processor, offset)
Procedure that performs a BCS (branch if positive, if the N flag is clear)
Definition cpu.h:1046
#define AND_indexed_indirect(processor, addr, offset)
Procedure that perform a AND with the carry flag (A,Z,C,N=A+M+C) with indexed indirect addressing mod...
Definition cpu.h:772
#define SET_C_FLAG(x)
Definition cpu.h:128
#define BRANCH_Not_Equal(processor, offset)
Procedure that performs a BNE (if the Z flag is clear)
Definition cpu.h:1011
#define BRANCH_If_Positive(processor, offset)
Procedure that performs a BPL (branch if positive, if the N flag is clear)
Definition cpu.h:997
#define STR_indirect_indexed_ACC(processor, addr, offset)
Procedure that store the accumulator value to a memory address using indirect indexed addressing mode...
Definition cpu.h:516
#define CLEAR_D_FLAG(x)
Definition cpu.h:142
#define INC_offset(processor, addr, offset)
Procedure that increment a specific memory location.
Definition cpu.h:400
#define COMPARE_offset_ACC(processor, addr, offset)
Procedure that performs a CMP operation (Z, C, N = A - M) with an absolute + offset addressing mode.
Definition cpu.h:1349
#define STR_offset_Y(processor, addr, offset)
Procedure that store the Y register value to a memory address + offset.
Definition cpu.h:506
#define DEC_X(processor)
Procedure that decrements the X register.
Definition cpu.h:418
#define BREAK(processor)
Procedure that performs a break instruction (forces an interrupt).
Definition cpu.h:1232
#define RETURN_Subroutine(processor)
Procedure that performs a return from subroutine (RTS instruction) by pulling the top two elements of...
Definition cpu.h:1287
#define LD_absolute_X(processor, addr)
Procedure that loads a value from memory at the address addr to the X register.
Definition cpu.h:310
#define LD_immediate_ACC(processor, d)
Procedure that loads an immediate (const value) in the accumulator.
Definition cpu.h:237
#define LD_immediate_X(processor, d)
Procedure that loads an immediate (const value) in the X register.
Definition cpu.h:244
#define SHIFT_LEFT_ACC(processor)
Procedure that performs a bitshift to the left (ASL operation) on the accumulator and place the 7th b...
Definition cpu.h:1076
#define BIT_TEST(processor, addr)
Procedure that performs a BIT test (A & M, N = M7, V = M6)
Definition cpu.h:951
#define ADD_offset(processor, addr, offset)
Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with absolute+offset addressing addr...
Definition cpu.h:701
#define PUSH_ACC(processor)
Procedure that push the accumulator value to the top of the stack.
Definition cpu.h:603
#define SBC_indexed_indirect(processor, addr, offset)
Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with indexed indirect addressing mod...
Definition cpu.h:928
#define INC_Y(processor)
Procedure that increments the Y register.
Definition cpu.h:412
#define XOR_absolute(processor, addr)
Procedure that performs a bitwise logic XOR (A,Z,N = A&M) using absolute addressing mode.
Definition cpu.h:845
#define LD_absolute_Y(processor, addr)
Procedure that loads a value from memory at the address addr to the Y register.
Definition cpu.h:317
#define PULL_ACC(processor)
Procedure that pop the top value of the stack and return it to the accumulator.
Definition cpu.h:636
#define SBC_offset(processor, addr, offset)
Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with offset addressing mode.
Definition cpu.h:919
#define SBC_absolute(processor, addr)
Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with absolute addressing mode.
Definition cpu.h:910
#define OR_offset(processor, addr, offset)
Procedure that performs a bitwise logic OR (A,Z,N = A&M) using offset addressing mode.
Definition cpu.h:808
#define CLEAR_C_FLAG(x)
Definition cpu.h:139
#define BRANCH_Equal(processor, offset)
Procedure that performs a BEQ (branch if positive, if the N flag is clear)
Definition cpu.h:1039
#define TR_SP_X(processor)
Procedure that store the stack pointer in the X register.
Definition cpu.h:569
#define LD_indexed_indirect_ACC(processor, addr, offset)
Procedure that loads to the accumulator using Indexed Indirect.
Definition cpu.h:432
#define LD_offset_ACC(processor, addr, offset)
Procedure that loads a value from memory at address addr+offset to the X register.
Definition cpu.h:269
#define RETURN_Interrupt(processor)
Procedure that performs a return from interrupt (RTI instruction) by pulling the flags from the top o...
Definition cpu.h:1298
#define LD_absolute_ACC(processor, addr)
Procedure that loads a value from memory at the address addr to the accumulator.
Definition cpu.h:303
#define COMPARE_Immediate_Y(processor, value)
Procedure that performs a CMP operation (Z, C, N = Y - M) with a value addressing mode.
Definition cpu.h:1394
#define DEC_absolute(processor, addr)
Procedure that decrement a specific memory location.
Definition cpu.h:377
#define XOR_offset(processor, addr, offset)
Procedure that performs a bitwise logic XOR (A,Z,N = A&M) using offset addressing mode.
Definition cpu.h:853
#define CLEAR_I_FLAG(x)
Definition cpu.h:141
#define INC_absolute(processor, addr)
Procedure that increment a specific memory location.
Definition cpu.h:384
#define AND_offset(processor, addr, offset)
Procedure that performs a bitwise logic AND (A,Z,N = A&M) using offset addressing mode.
Definition cpu.h:763
#define TR_X_ACC(processor)
Procedure that store the value in the X register in the accumulator.
Definition cpu.h:557
#define SHIFT_RIGHT_ACC(processor)
Procedure that performs a bitshift to the right (LSR operation) on the accumulator and place the 0th ...
Definition cpu.h:1083
#define OR_indirect_indexed(processor, addr, offset)
Procedure that perform a OR with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.
Definition cpu.h:826
#define XOR_indirect_indexed(processor, addr, offset)
Procedure that perform a XOR with the carry flag (A,Z,C,N=A+M+C) with indirect indexed.
Definition cpu.h:871
#define CLEAR_V_FLAG(x)
Definition cpu.h:145
#define ROTATION_LEFT_offset(processor, addr, offset)
Procedure that performs a bit shift with rotation to the left (ROL operation) at the memory address (...
Definition cpu.h:1187
#define TR_Y_ACC(processor)
Procedure that store the value in the Y register in the accumulator.
Definition cpu.h:563
#define COMPARE_absolute_X(processor, addr)
Procedure that performs a CMP operation (Z, C, N = X - M) with an indirect indexed addressing mode.
Definition cpu.h:1386
#define SBC_indirect_indexed(processor, addr, offset)
Procedure that perform a SBC with the carry flag (A,Z,C,N=A-M-C) with indirect indexed addressing mod...
Definition cpu.h:938
#define JUMP_Subroutine(processor, addr)
Procedure that performs a Jump to a subroutine (JSR instruction) located at the address addr.
Definition cpu.h:1275
#define SHIFT_RIGHT_offset(processor, addr, offset)
Procedure that performs a bitshift to the right (LSR operation) at the address (addr + offset) and pl...
Definition cpu.h:1116
#define LD_immediate_Y(processor, d)
Procedure that loads an immediate (const value) in the Y register.
Definition cpu.h:251
#define JUMP_indirect(processor, addr)
Procedure that sets the program counter to a certain indirect address (JMP instruction)
Definition cpu.h:1213
#define INC_X(processor)
Procedure that increments the X register.
Definition cpu.h:406
#define ADD_absolute(processor, addr)
Procedure that perform a ADD with the carry flag (A,Z,C,N=A+M+C) with absolute addressing mode.
Definition cpu.h:692
#define BRANCH_Overflow_S(processor, offset)
Procedure that performs a BVS (branch if positive, if the N flag is clear)
Definition cpu.h:1032
#define BRANCH_Overflow_C(processor, offset)
Procedure that performs a BVC (branch if overflow clear, if the V flag is clear)
Definition cpu.h:1004
#define PUSH_FLAGS(processor)
Procedure that push the processor flags to the top of the stack.
Definition cpu.h:609
#define LD_offset_X(processor, addr, offset)
Procedure that loads a value from memory at address addr+offset to the X register.
Definition cpu.h:278
#define BRANCH_Carry_C(processor, offset)
Procedure that performs a BCC (if the C flag is clear)
Definition cpu.h:1018
#define STR_absolute_Y(processor, addr)
Procedure that store the Y register value to a memory address.
Definition cpu.h:471
#define ROTATION_LEFT_ACC(processor)
Procedure that performs a bit shift with rotation to the left (ROL operation) on the accumulator....
Definition cpu.h:1149
#define SHIFT_LEFT_offset(processor, addr, offset)
Procedure that performs a bitshift to the left (ASL operation) at the address (addr + offset) and pla...
Definition cpu.h:1107
#define STR_indexed_indirect_ACC(processor, addr, offset)
Procedure that store the accumulator value to a memory address using indexes indirect addressing mode...
Definition cpu.h:526
#define LD_offset_Y(processor, addr, offset)
Procedure that loads a value from memory at address addr+offset to the Y register.
Definition cpu.h:287
#define ERR_ABN_OPCODE
Definition macros.h:17
@ DEC_Z
DEC Zero Page.
Definition opcodes.h:99
@ INC_AX
INC Absolute, X.
Definition opcodes.h:124
@ LSR_ZX
LSR Zero Page, X.
Definition opcodes.h:166
@ RTI
Return from the current interrupt.
Definition opcodes.h:210
@ TSX
X = sp.
Definition opcodes.h:257
@ CMP_IdX
CMP (Indirect, X)
Definition opcodes.h:85
@ BEQ
If Z = 1.
Definition opcodes.h:41
@ ROL_ZX
ROL Zero Page, X.
Definition opcodes.h:198
@ CMP_IdY
CMP (Indirect), Y.
Definition opcodes.h:86
@ ROL_Z
ROL Zero Page.
Definition opcodes.h:197
@ CMP_AX
CMP Absolute, X.
Definition opcodes.h:83
@ ASL_ZX
ASL Zero Page, X.
Definition opcodes.h:30
@ CMP_A
CMP Absolute.
Definition opcodes.h:82
@ CLI
I = 0.
Definition opcodes.h:73
@ ORA_ZX
ORA Zero Page, X.
Definition opcodes.h:176
@ LDA_AX
LDA Absolute, X.
Definition opcodes.h:144
@ CPY_Z
CPY Zero Page.
Definition opcodes.h:95
@ SBC_ZX
SBC Zero Page, X.
Definition opcodes.h:218
@ BPL
If N = 0.
Definition opcodes.h:55
@ ROL_A
ROL Absolute.
Definition opcodes.h:199
@ STY_Z
STY Zero Page.
Definition opcodes.h:249
@ TYA
ACC = Y.
Definition opcodes.h:263
@ LDX_ZY
LDX Zero Page, Y.
Definition opcodes.h:152
@ SEC
C = 1.
Definition opcodes.h:226
@ ADC_IdX
ADC (Indirect, X)
Definition opcodes.h:12
@ AND_AX
AND Absolute, X.
Definition opcodes.h:21
@ TAX
X = ACC.
Definition opcodes.h:254
@ LDY_ZX
LDY Zero Page, X.
Definition opcodes.h:159
@ ROR_Z
ROR Zero Page.
Definition opcodes.h:204
@ BCS
If C = 1.
Definition opcodes.h:38
@ AND_ZX
AND Zero Page, X.
Definition opcodes.h:19
@ INC_ZX
INC Zero Page, X.
Definition opcodes.h:122
@ LDA_A
LDA Absolute.
Definition opcodes.h:143
@ PLP
flags = pull(); sp = sp + 1;
Definition opcodes.h:193
@ CPX_A
CPX Absolute.
Definition opcodes.h:91
@ ADC_ZX
ADC Zero Page, X.
Definition opcodes.h:8
@ ADC_Z
ADC Zero Page.
Definition opcodes.h:7
@ LDA_I
LDA Immediate.
Definition opcodes.h:140
@ DEC_A
DEC Absolute.
Definition opcodes.h:101
@ CLV
V = 0.
Definition opcodes.h:76
@ BNE
If Z = 0.
Definition opcodes.h:52
@ STY_A
STY Absolute.
Definition opcodes.h:251
@ STX_Z
STX Zero Page.
Definition opcodes.h:244
@ ROL_AX
ROL Absolute, X.
Definition opcodes.h:200
@ ADC_I
ADC Immediate.
Definition opcodes.h:6
@ AND_IdX
AND (Indirect, X)
Definition opcodes.h:23
@ INC_Z
INC Zero Page.
Definition opcodes.h:121
@ BIT_Z
BIT Zero Page.
Definition opcodes.h:45
@ LDY_AX
LDY Absolute, X.
Definition opcodes.h:161
@ DEY
Y = Y - 1.
Definition opcodes.h:108
@ SEI
I = 1.
Definition opcodes.h:232
@ SBC_I
SBC Immediate.
Definition opcodes.h:216
@ AND_I
AND Immediate.
Definition opcodes.h:17
@ CPX_Z
CPX Zero Page.
Definition opcodes.h:90
@ TAY
Definition opcodes.h:269
@ ASL_AX
ASL Absolute, X.
Definition opcodes.h:32
@ INC_A
INC Absolute.
Definition opcodes.h:123
@ CMP_ZX
CMP Zero Page, X.
Definition opcodes.h:81
@ EOR_AY
EOR Absolute, Y.
Definition opcodes.h:116
@ PHP
push(flags); sp = sp - 1;
Definition opcodes.h:187
@ AND_AY
AND Absolute, Y.
Definition opcodes.h:22
@ EOR_ZX
EOR Zero Page, X.
Definition opcodes.h:113
@ BRK
Break instruction.
Definition opcodes.h:58
@ ADC_AX
ADC Absolute, X.
Definition opcodes.h:10
@ LDA_ZX
LDA Zero Page, X.
Definition opcodes.h:142
@ ROR_Ac
ROR Accumulator.
Definition opcodes.h:203
@ AND_IdY
AND (Indirect), Y.
Definition opcodes.h:24
@ BIT_A
BIT Absolute.
Definition opcodes.h:46
@ EOR_AX
EOR Absolute, X.
Definition opcodes.h:115
@ SBC_IdY
SBC (Indirect), Y.
Definition opcodes.h:223
@ ORA_AY
ORA Absolute, Y.
Definition opcodes.h:179
@ SBC_AX
SBC Absolute, X.
Definition opcodes.h:220
@ CLC
C = 0.
Definition opcodes.h:67
@ STA_Z
STA Zero Page.
Definition opcodes.h:235
@ LDA_Z
LDA Zero Page.
Definition opcodes.h:141
@ STA_A
STA Absolute.
Definition opcodes.h:237
@ TXA
ACC = X.
Definition opcodes.h:260
@ STA_AY
STA Absolute, Y.
Definition opcodes.h:239
@ ADC_IdY
ADC (Indirect), Y.
Definition opcodes.h:13
@ LDX_Z
LDX Zero Page.
Definition opcodes.h:151
@ CPX_I
CPX Immediate.
Definition opcodes.h:89
@ ROR_ZX
ROR Zero Page, X.
Definition opcodes.h:205
@ ORA_A
ORA Absolute.
Definition opcodes.h:177
@ AND_Z
AND Zero Page.
Definition opcodes.h:18
@ STA_IdY
STA (Indirect), Y.
Definition opcodes.h:241
@ CMP_Z
CMP Zero Page.
Definition opcodes.h:80
@ CPY_I
CPY Immediate.
Definition opcodes.h:94
@ ASL_Z
ASL Zero Page.
Definition opcodes.h:29
@ BCC
If C = 0.
Definition opcodes.h:35
@ CPY_A
CPY Absolute.
Definition opcodes.h:96
@ EOR_I
EOR Immediate.
Definition opcodes.h:111
@ TXS
Definition opcodes.h:266
@ SBC_IdX
SBC (Indirect, X)
Definition opcodes.h:222
@ LDX_AY
LDX Absolute, Y.
Definition opcodes.h:154
@ CMP_I
CMP Immediate.
Definition opcodes.h:79
@ STY_ZX
STY Zero Page, X.
Definition opcodes.h:250
@ STA_AX
STA Absolute, X.
Definition opcodes.h:238
@ LDX_I
LDX Immediate.
Definition opcodes.h:150
@ CLD
D = 0.
Definition opcodes.h:70
@ ROL_Ac
ROL Accumulator.
Definition opcodes.h:196
@ LDA_AY
LDA Absolute, Y.
Definition opcodes.h:145
@ ORA_IdY
ORA (Indirect), Y.
Definition opcodes.h:181
@ STA_IdX
STA (Indirect, X)
Definition opcodes.h:240
@ LSR_AX
LSR Absolute, X.
Definition opcodes.h:168
@ ROR_AX
ROR Absolute, X.
Definition opcodes.h:207
@ STX_ZY
STX Zero Page, Y.
Definition opcodes.h:245
@ PLA
ACC = pull(); sp = sp + 1;.
Definition opcodes.h:190
@ LDY_A
LDY Absolute.
Definition opcodes.h:160
@ BVC
If V = 0.
Definition opcodes.h:61
@ STA_ZX
STA Zero Page, X.
Definition opcodes.h:236
@ ASL_Ac
ASL Accumulator.
Definition opcodes.h:28
@ EOR_IdX
EOR (Indirect, X)
Definition opcodes.h:117
@ LDY_Z
LDY Zero Page.
Definition opcodes.h:158
@ LSR_A
LSR Absolute.
Definition opcodes.h:167
@ INY
Y = Y + 1.
Definition opcodes.h:130
@ DEC_ZX
DEC Zero Page, X.
Definition opcodes.h:100
@ LSR_Z
LSR Zero Page.
Definition opcodes.h:165
@ EOR_IdY
EOR (Indirect), Y.
Definition opcodes.h:118
@ AND_A
AND Absolute.
Definition opcodes.h:20
@ LDA_IdX
LDA (Indirect, X)
Definition opcodes.h:146
@ LDX_A
LDX Absolute.
Definition opcodes.h:153
@ ADC_AY
ADC Absolute, Y.
Definition opcodes.h:11
@ BMI
iF N = 1
Definition opcodes.h:49
@ LDA_IdY
LDA (Indirect), Y.
Definition opcodes.h:147
@ LSR_Ac
LSR Accumulator.
Definition opcodes.h:164
@ SBC_A
SBC Absolute.
Definition opcodes.h:219
@ DEC_AX
DEC Absolute, X.
Definition opcodes.h:102
@ PHA
push(ACC); sp = sp - 1;
Definition opcodes.h:184
@ ORA_Z
ORA Zero Page.
Definition opcodes.h:175
@ BVS
If V = 1.
Definition opcodes.h:64
@ ORA_IdX
ORA (Indirect, X)
Definition opcodes.h:180
@ INX
X = X + 1.
Definition opcodes.h:127
@ CMP_AY
CMP Absolute, Y.
Definition opcodes.h:84
@ ROR_A
ROR Absolute.
Definition opcodes.h:206
@ ADC_A
ADC Absolute.
Definition opcodes.h:9
@ SBC_Z
SBC Zero Page.
Definition opcodes.h:217
@ RTS
Return from the current subroutine.
Definition opcodes.h:213
@ JMP_A
JMP Absolute.
Definition opcodes.h:133
@ LDY_I
LDY Immediate.
Definition opcodes.h:157
@ ASL_A
ASL Absolute.
Definition opcodes.h:31
@ SED
D = 1.
Definition opcodes.h:229
@ ORA_I
ORA Immediate.
Definition opcodes.h:174
@ ORA_AX
ORA Absolute, X.
Definition opcodes.h:178
@ SBC_AY
SBC Absolute, Y.
Definition opcodes.h:221
@ STX_A
STX Absolute.
Definition opcodes.h:246
@ EOR_Z
EOR Zero Page.
Definition opcodes.h:112
@ JMP_Id
JMP Indirect.
Definition opcodes.h:134
@ NOP
No Operation.
Definition opcodes.h:171
@ DEX
X = X - 1.
Definition opcodes.h:105
@ EOR_A
EOR Abolute.
Definition opcodes.h:114
@ JSR
Jump to the subroutine.
Definition opcodes.h:137
data register_y
A 8 bit register. Commonly used to hold offset and counters.
Definition cpu.h:33
address program_counter
An address pointing to the next instruction to be executed.
Definition cpu.h:28
memory mem
A direct access to the Emulator Memory.
Definition cpu.h:39
data register_x
A 8 bit register. Commonly used to hold offset and counters.
Definition cpu.h:32

References ADC_A, ADC_AX, ADC_AY, ADC_I, ADC_IdX, ADC_IdY, ADC_Z, ADC_ZX, ADD_absolute, ADD_immediate, ADD_indexed_indirect, ADD_indirect_indexed, ADD_offset, AND_A, AND_absolute, AND_AX, AND_AY, AND_I, AND_IdX, AND_IdY, AND_immediate, AND_indexed_indirect, AND_indirect_indexed, AND_offset, AND_Z, AND_ZX, ASL_A, ASL_Ac, ASL_AX, ASL_Z, ASL_ZX, BCC, BCS, BEQ, BIT_A, BIT_TEST, BIT_Z, BMI, BNE, BPL, BRANCH_Carry_C, BRANCH_Carry_S, BRANCH_Equal, BRANCH_If_Minus, BRANCH_If_Positive, BRANCH_Not_Equal, BRANCH_Overflow_C, BRANCH_Overflow_S, BREAK, BRK, BVC, BVS, CLC, CLD, CLEAR_C_FLAG, CLEAR_D_FLAG, CLEAR_I_FLAG, CLEAR_V_FLAG, CLI, CLV, CMP_A, CMP_AX, CMP_AY, CMP_I, CMP_IdX, CMP_IdY, CMP_Z, CMP_ZX, COMPARE_absolute_ACC, COMPARE_absolute_X, COMPARE_absolute_Y, COMPARE_Immediate_ACC, COMPARE_Immediate_X, COMPARE_Immediate_Y, COMPARE_indexed_indirect_ACC, COMPARE_indirect_indexed_ACC, COMPARE_offset_ACC, CPX_A, CPX_I, CPX_Z, CPY_A, CPY_I, CPY_Z, DEC_A, DEC_absolute, DEC_AX, DEC_offset, DEC_X, DEC_Y, DEC_Z, DEC_ZX, DEX, DEY, EOR_A, EOR_AX, EOR_AY, EOR_I, EOR_IdX, EOR_IdY, EOR_Z, EOR_ZX, ERR_ABN_OPCODE, get_address, INC_A, INC_absolute, INC_AX, INC_offset, INC_X, INC_Y, INC_Z, INC_ZX, INX, INY, JMP_A, JMP_Id, JSR, JUMP_absolute, JUMP_indirect, JUMP_Subroutine, LD_absolute_ACC, LD_absolute_X, LD_absolute_Y, LD_immediate_ACC, LD_immediate_X, LD_immediate_Y, LD_indexed_indirect_ACC, LD_indirect_indexed_ACC, LD_offset_ACC, LD_offset_X, LD_offset_Y, LDA_A, LDA_AX, LDA_AY, LDA_I, LDA_IdX, LDA_IdY, LDA_Z, LDA_ZX, LDX_A, LDX_AY, LDX_I, LDX_Z, LDX_ZY, LDY_A, LDY_AX, LDY_I, LDY_Z, LDY_ZX, LSR_A, LSR_Ac, LSR_AX, LSR_Z, LSR_ZX, CPU::mem, NOP, OR_absolute, OR_immediate, OR_indexed_indirect, OR_indirect_indexed, OR_offset, ORA_A, ORA_AX, ORA_AY, ORA_I, ORA_IdX, ORA_IdY, ORA_Z, ORA_ZX, PHA, PHP, PLA, PLP, CPU::program_counter, PULL_ACC, PULL_FLAGS, PUSH_ACC, PUSH_FLAGS, CPU::register_x, CPU::register_y, RETURN_Interrupt, RETURN_Subroutine, ROL_A, ROL_Ac, ROL_AX, ROL_Z, ROL_ZX, ROR_A, ROR_Ac, ROR_AX, ROR_Z, ROR_ZX, ROTATION_LEFT_absolute, ROTATION_LEFT_ACC, ROTATION_LEFT_offset, ROTATION_RIGHT_absolute, ROTATION_RIGHT_ACC, ROTATION_RIGHT_offset, RTI, RTS, SBC_A, SBC_absolute, SBC_AX, SBC_AY, SBC_I, SBC_IdX, SBC_IdY, SBC_immediate, SBC_indexed_indirect, SBC_indirect_indexed, SBC_offset, SBC_Z, SBC_ZX, SEC, SED, SEI, SET_C_FLAG, SET_D_FLAG, SET_I_FLAG, SHIFT_LEFT_absolute, SHIFT_LEFT_ACC, SHIFT_LEFT_offset, SHIFT_RIGHT_absolute, SHIFT_RIGHT_ACC, SHIFT_RIGHT_offset, STA_A, STA_AX, STA_AY, STA_IdX, STA_IdY, STA_Z, STA_ZX, STR_absolute_ACC, STR_absolute_X, STR_absolute_Y, STR_indexed_indirect_ACC, STR_indirect_indexed_ACC, STR_offset_ACC, STR_offset_X, STR_offset_Y, STX_A, STX_Z, STX_ZY, STY_A, STY_Z, STY_ZX, TAX, TAY, TR_ACC_X, TR_ACC_Y, TR_SP_X, TR_X_ACC, TR_X_SP, TR_Y_ACC, TSX, TXA, TXS, TYA, XOR_absolute, XOR_immediate, XOR_indexed_indirect, XOR_indirect_indexed, and XOR_offset.

Referenced by step_cpu().

Here is the caller graph for this function:

◆ free_cpu()

void free_cpu ( CPU * cpu)

Procedure that free the memory used by the CPU and the memory array.

Parameters
[in,out]cpuThe CPU to free and sets the pointer to NULL

Definition at line 21 of file cpu.c.

21 {
22 free_memory(cpu->mem);
23 free(cpu);
24 cpu = ((void*)0);
25}
void free_memory(memory mem)
This procedure free the memory pointer.
Definition mem.c:25

References free_memory(), and CPU::mem.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_next_instruction()

unsigned char get_next_instruction ( CPU * cpu)

Function that get the next instruction written in the ROM.

Parameters
[in]cpuThe CPU that contains the mem
Returns
The number of cycles needed to perform the instruction

Definition at line 32 of file cpu.c.

32 {
33
34 switch (cpu->mem[cpu->program_counter]) {
35
36 case ADC_A:
37
38 case ADC_AX:
39
40 case ADC_AY:
41
42 case AND_A:
43
44 case AND_AX:
45
46 case AND_AY:
47
48 case ASL_A:
49
50 case ASL_AX:
51
52 case BIT_A:
53
54 case CMP_A:
55
56 case CMP_AX:
57
58 case CMP_AY:
59
60 case CPX_A:
61
62 case CPY_A:
63
64 case DEC_A:
65
66 case DEC_AX:
67
68 case EOR_A:
69
70 case EOR_AX:
71
72 case EOR_AY:
73
74 case INC_A:
75
76 case INC_AX:
77
78 case JMP_A:
79
80 case JMP_Id:
81
82 case JSR:
83
84 case LDA_A:
85
86 case LDA_AX:
87
88 case LDA_AY:
89
90 case LDX_A:
91
92 case LDX_AY:
93
94 case LDY_A:
95
96 case LDY_AX:
97
98 case LSR_A:
99
100 case LSR_AX:
101
102 case ORA_A:
103
104 case ORA_AX:
105
106 case ORA_AY:
107
108 case ROL_A:
109
110 case ROL_AX:
111
112 case ROR_A:
113
114 case ROR_AX:
115
116 case SBC_A:
117
118 case SBC_AX:
119
120 case SBC_AY:
121
122 case STA_A:
123
124 case STA_AX:
125
126 case STA_AY:
127
128 case STX_A:
129
130 case STY_A:
131 cpu->program_counter += 3;
132 break;
133
134 case BRK:
135
136 case CLC:
137
138 case CLD:
139
140 case CLI:
141
142 case CLV:
143
144 case DEX:
145
146 case DEY:
147
148 case INX:
149
150 case INY:
151
152 case LSR_Ac:
153
154 case NOP:
155
156 case PHP:
157
158 case PHA:
159
160 case PLP:
161
162 case PLA:
163
164 case ROR_Ac:
165
166 case ROL_Ac:
167
168 case RTI:
169
170 case RTS:
171
172 case SEC:
173
174 case SED:
175
176 case SEI:
177
178 case TAX:
179
180 case TXA:
181
182 case TAY:
183
184 case TYA:
185
186 case TSX:
187
188 case TXS:
189 cpu->program_counter += 1;
190 break;
191
192 default:
193 cpu->program_counter += 2;
194 break;
195 }
196
197 unsigned char cycles = 0;
198
199 switch (cpu->mem[cpu->program_counter]) {
200
201 case TAX:
202
203 case TAY:
204
205 case TSX:
206
207 case TXA:
208
209 case TXS:
210
211 case TYA:
212
213 case SEC:
214
215 case SED:
216
217 case SEI:
218
219 case SBC_I:
220
221 case ROR_Ac:
222
223 case ROL_Ac:
224
225 case ORA_I:
226
227 case NOP:
228
229 case LSR_Ac:
230
231 case LDY_I:
232
233 case LDA_I:
234
235 case ASL_Ac:
236
237 case AND_I:
238
239 case LDX_I:
240
241 case CLD:
242
243 case CLI:
244
245 case CLV:
246
247 case CPX_I:
248
249 case CPY_I:
250
251 case CMP_I:
252
253 case DEX:
254
255 case DEY:
256
257 case EOR_I:
258
259 case INX:
260
261 case INY:
262
263 case ADC_I:
264 cycles = 2;
265 break;
266
267 case STX_Z:
268
269 case STA_Z:
270
271 case SBC_Z:
272
273 case PHA:
274
275 case PHP:
276
277 case ORA_Z:
278
279 case LDX_Z:
280
281 case LDA_Z:
282
283 case JMP_A:
284
285 case EOR_Z:
286
287 case CPY_Z:
288
289 case CPX_Z:
290
291 case CMP_Z:
292
293 case AND_Z:
294
295 case ADC_Z:
296
297 case LDY_Z:
298 cycles = 3;
299 break;
300
301 case STX_A:
302
303 case STX_ZY:
304
305 case STA_ZX:
306
307 case STA_A:
308
309 case SBC_ZX:
310
311 case SBC_A:
312
313 case PLA:
314
315 case PLP:
316
317 case LDY_ZX:
318
319 case LDY_A:
320
321 case LDA_ZX:
322
323 case LDA_A:
324
325 case EOR_ZX:
326
327 case EOR_A:
328
329 case CPY_A:
330
331 case CPX_A:
332
333 case CMP_ZX:
334
335 case CMP_A:
336
337 case LDX_ZY:
338
339 case LDX_A:
340
341 case AND_ZX:
342
343 case ADC_ZX:
344
345 case AND_A:
346
347 case ORA_ZX:
348
349 case ORA_A:
350
351 case ADC_A:
352 cycles = 4;
353 break;
354
355 case STA_AX:
356
357 case STA_AY:
358
359 case ROR_Z:
360
361 case ROL_Z:
362
363 case LSR_Z:
364
365 case JMP_Id:
366
367 case INC_Z:
368
369 case DEC_Z:
370
371 case ASL_Z:
372 cycles = 5;
373 break;
374
375 case SBC_AX:
376
377 case LDA_AX:
378
379 case EOR_AX:
380
381 case CMP_AX:
382
383 case AND_AX:
384
385 case LDY_AX:
386
387 case ORA_AX:
388
389 case ADC_AX:
390 if (is_page_crossed(get_address(cpu), cpu->register_x)) {
391 cycles = 5;
392 } else {
393 cycles = 4;
394 }
395 break;
396
397 case SBC_AY:
398
399 case ORA_AY:
400
401 case LDA_AY:
402
403 case EOR_AY:
404
405 case CMP_AY:
406
407 case AND_AY:
408
409 case LDX_AY:
410
411 case ADC_AY:
412 if (is_page_crossed(get_address(cpu), cpu->register_y)) {
413 cycles = 5;
414 } else {
415 cycles = 4;
416 }
417 break;
418
419 case RTI:
420
421 case RTS:
422
423 case ROR_ZX:
424
425 case ROR_A:
426
427 case ROL_ZX:
428
429 case ROL_A:
430
431 case LSR_ZX:
432
433 case LSR_A:
434
435 case LDA_IdX:
436
437 case JSR:
438
439 case INC_ZX:
440
441 case INC_A:
442
443 case EOR_IdX:
444
445 case DEC_ZX:
446
447 case DEC_A:
448
449 case CMP_IdX:
450
451 case ASL_ZX:
452
453 case ASL_A:
454
455 case AND_IdX:
456
457 case ORA_IdX:
458
459 case ADC_IdX:
460
461 case STA_IdX:
462
463 case STA_IdY:
464
465 case SBC_IdX:
466 cycles = 6;
467 break;
468
469 case LDA_IdY:
470
471 case EOR_IdY:
472
473 case AND_IdY:
474
475 case CMP_IdY:
476
477 case ORA_IdY:
478
479 case SBC_IdY:
480
481 case ADC_IdY:
482 if (is_page_crossed(indirect_indexed(cpu, cpu->program_counter + 1, 0), cpu->register_y)) {
483 cycles = 6;
484 } else {
485 cycles = 5;
486 }
487 break;
488
489 case ROL_AX:
490
491 case ROR_AX:
492
493 case LSR_AX:
494
495 case INC_AX:
496
497 case DEC_AX:
498
499 case BRK:
500
501 case ASL_AX:
502 cycles = 7;
503 break;
504
505 case BCC:
506 if (!GET_C_FLAG(cpu)) {
507 cycles = 1;
508 }
509
510 branch_page_test:
511 if (is_page_crossed(cpu->program_counter, cpu->mem[cpu->program_counter + 1])) {
512 cycles += 4;
513 } else {
514 cycles += 2;
515 }
516
517 break;
518
519 case BEQ:
520 if (GET_Z_FLAG(cpu)) {
521 cycles = 1;
522 }
523 goto branch_page_test;
524 break;
525
526 case BCS:
527 if (GET_C_FLAG(cpu)) {
528 cycles = 1;
529 }
530 goto branch_page_test;
531 break;
532
533 case BMI:
534 if (GET_N_FLAG(cpu)) {
535 cycles = 1;
536 }
537 goto branch_page_test;
538 break;
539
540 case BNE:
541 if (!GET_Z_FLAG(cpu)) {
542 cycles = 1;
543 }
544 goto branch_page_test;
545 break;
546
547 case BPL:
548 if (!GET_N_FLAG(cpu)) {
549 cycles = 1;
550 }
551 goto branch_page_test;
552 break;
553
554 case BVC:
555 if (!GET_V_FLAG(cpu)) {
556 cycles = 1;
557 }
558 goto branch_page_test;
559 break;
560
561 case BVS:
562 if (GET_V_FLAG(cpu)) {
563 cycles = 1;
564 }
565 goto branch_page_test;
566 break;
567
568 default:
569 cycles = 0;
570 }
571
572 return cycles;
573}
#define GET_V_FLAG(x)
Definition cpu.h:125
#define GET_N_FLAG(x)
Definition cpu.h:126
#define is_page_crossed(addr, offset)
Macro to know if we cross a page when doing the instruction.
Definition cpu.h:189
#define GET_Z_FLAG(x)
Definition cpu.h:120

References ADC_A, ADC_AX, ADC_AY, ADC_I, ADC_IdX, ADC_IdY, ADC_Z, ADC_ZX, AND_A, AND_AX, AND_AY, AND_I, AND_IdX, AND_IdY, AND_Z, AND_ZX, ASL_A, ASL_Ac, ASL_AX, ASL_Z, ASL_ZX, BCC, BCS, BEQ, BIT_A, BMI, BNE, BPL, BRK, BVC, BVS, CLC, CLD, CLI, CLV, CMP_A, CMP_AX, CMP_AY, CMP_I, CMP_IdX, CMP_IdY, CMP_Z, CMP_ZX, CPX_A, CPX_I, CPX_Z, CPY_A, CPY_I, CPY_Z, DEC_A, DEC_AX, DEC_Z, DEC_ZX, DEX, DEY, EOR_A, EOR_AX, EOR_AY, EOR_I, EOR_IdX, EOR_IdY, EOR_Z, EOR_ZX, get_address, GET_C_FLAG, GET_N_FLAG, GET_V_FLAG, GET_Z_FLAG, INC_A, INC_AX, INC_Z, INC_ZX, indirect_indexed, INX, INY, is_page_crossed, JMP_A, JMP_Id, JSR, LDA_A, LDA_AX, LDA_AY, LDA_I, LDA_IdX, LDA_IdY, LDA_Z, LDA_ZX, LDX_A, LDX_AY, LDX_I, LDX_Z, LDX_ZY, LDY_A, LDY_AX, LDY_I, LDY_Z, LDY_ZX, LSR_A, LSR_Ac, LSR_AX, LSR_Z, LSR_ZX, CPU::mem, NOP, ORA_A, ORA_AX, ORA_AY, ORA_I, ORA_IdX, ORA_IdY, ORA_Z, ORA_ZX, PHA, PHP, PLA, PLP, CPU::program_counter, CPU::register_x, CPU::register_y, ROL_A, ROL_Ac, ROL_AX, ROL_Z, ROL_ZX, ROR_A, ROR_Ac, ROR_AX, ROR_Z, ROR_ZX, RTI, RTS, SBC_A, SBC_AX, SBC_AY, SBC_I, SBC_IdX, SBC_IdY, SBC_Z, SBC_ZX, SEC, SED, SEI, STA_A, STA_AX, STA_AY, STA_IdX, STA_IdY, STA_Z, STA_ZX, STX_A, STX_Z, STX_ZY, STY_A, TAX, TAY, TSX, TXA, TXS, and TYA.

Referenced by step_cpu().

Here is the caller graph for this function:

◆ init_cpu()

void init_cpu ( CPU * cpu,
memory mem )

Procedure that initialize the CPU to its boot/reset state.

Parameters
[in,out]cpuThe CPU to initialize
[in]memThe address to the memory array

Definition at line 9 of file cpu.c.

9 {
10 cpu->program_counter = 0xFFFC;
11 cpu->stack_pointer = 0xFF;
12 cpu->accumulator = 0x00;
13 cpu->register_x = 0x00;
14 cpu->register_y = 0x00;
15 cpu->flags = 0x00;
16 cpu->mem = mem;
17 cpu->cache = 0x00;
18 cpu->cycle_counter = 0x00;
19}
data cache
A 8 bit reserved space that I use for some operations.
Definition cpu.h:30
data accumulator
A 8 bit register used to perform operations.
Definition cpu.h:31
data flags
NVPBDIZC.
Definition cpu.h:35
unsigned char cycle_counter
The cycle counter. The counter is decremented every step.
Definition cpu.h:37
data stack_pointer
A register that holds the 8 lower bytes of the address 0x11??.
Definition cpu.h:29

References CPU::accumulator, CPU::cache, CPU::cycle_counter, CPU::flags, CPU::mem, CPU::program_counter, CPU::register_x, CPU::register_y, and CPU::stack_pointer.

Referenced by main(), and start_cpu().

Here is the caller graph for this function:

◆ print_cpu_expected()

void print_cpu_expected ( data acc,
data x,
data y,
data flags,
address pc,
data sp )

Procedure that display the expected state of the CPU.

Parameters
[in]accThe expected state of the accumulator
[in]xThe expected state of the X register
[in]yThe expected state of the Y register
[in]flagsThe expected flags
[in]pcThe expected program counter
[in]spThe expected Stack pointer

Definition at line 1235 of file cpu.c.

1235 {
1236 printf("|======== EXPECTED: Registers State ========|\n");
1237 printf("Accumulator: %i\n", acc);
1238 printf("X Register: %i\n", x);
1239 printf("Y Register: %i\n", y);
1240 printf("Program Counter: %#04x\n", pc);
1241 printf("Stack Pointer: %#02x\n", sp);
1242 printf("Flags: %#02x\n", flags);
1243
1244 printf("\n");
1245
1246 printf("|======== EXPECTED: Flags State ========|\n");
1247 printf("Negative Flag (N): %i\n", ((flags & BIT_7_MASK) >> 7));
1248 printf("Overflow Flag (V): %i\n", ((flags & BIT_6_MASK) >> 6));
1249 printf("Pause Flag (P): %i\n", ((flags & BIT_5_MASK) >> 5));
1250 printf("Break Flag (B): %i\n", ((flags & BIT_4_MASK) >> 4));
1251 printf("Decimal Flag (D): %i\n", ((flags & BIT_3_MASK) >> 3));
1252 printf("Interrupt Flag (I): %i\n", ((flags & BIT_2_MASK) >> 2));
1253 printf("Zero Flag (Z): %i\n", ((flags & BIT_1_MASK) >> 1));
1254 printf("Carry Flag (C): %i\n", (flags & BIT_0_MASK));
1255
1256 printf("\n");
1257}

References BIT_0_MASK, BIT_1_MASK, BIT_2_MASK, BIT_3_MASK, BIT_4_MASK, BIT_5_MASK, BIT_6_MASK, and BIT_7_MASK.

Referenced by main().

Here is the caller graph for this function:

◆ print_cpu_state()

void print_cpu_state ( CPU * processor)

Procedure that displays the current state of the CPU.

Parameters
[in]processorThe CPU to display

Definition at line 1210 of file cpu.c.

1210 {
1211
1212 printf("|======== Registers State ========|\n");
1213 printf("Accumulator: %i\n", processor->accumulator);
1214 printf("X Register: %i\n", processor->register_x);
1215 printf("Y Register: %i\n", processor->register_y);
1216 printf("Program Counter: %#04x\n", processor->program_counter);
1217 printf("Stack Pointer: %#02x\n", processor->stack_pointer);
1218 printf("Flags: %#02x\n", processor->flags);
1219
1220 printf("\n");
1221
1222 printf("|======== Flags State ========|\n");
1223 printf("Negative Flag (N): %i\n", (GET_N_FLAG(processor) >> 7));
1224 printf("Overflow Flag (V): %i\n", (GET_V_FLAG(processor) >> 6));
1225 printf("Pause Flag (P): %i\n", (GET_P_FLAG(processor) >> 5));
1226 printf("Break Flag (B): %i\n", (GET_B_FLAG(processor) >> 4));
1227 printf("Decimal Flag (D): %i\n", (GET_D_FLAG(processor) >> 3));
1228 printf("Interrupt Flag (I): %i\n", (GET_I_FLAG(processor) >> 2));
1229 printf("Zero Flag (Z): %i\n", (GET_Z_FLAG(processor) >> 1));
1230 printf("Carry Flag (C): %i\n", GET_C_FLAG(processor));
1231
1232 printf("\n");
1233}
#define GET_P_FLAG(x)
Definition cpu.h:124
#define GET_B_FLAG(x)
Definition cpu.h:123
#define GET_I_FLAG(x)
Definition cpu.h:121
#define GET_D_FLAG(x)
Definition cpu.h:122

References CPU::accumulator, CPU::flags, GET_B_FLAG, GET_C_FLAG, GET_D_FLAG, GET_I_FLAG, GET_N_FLAG, GET_P_FLAG, GET_V_FLAG, GET_Z_FLAG, CPU::program_counter, CPU::register_x, CPU::register_y, and CPU::stack_pointer.

Referenced by main().

Here is the caller graph for this function:

◆ print_stack_expected()

void print_stack_expected ( memory stack,
data start )

Procedure that display the expected state of the stack.

Parameters
[in]stackAn array representing the expected stack
[in]startThe start of the stack

Definition at line 1282 of file cpu.c.

1282 {
1283 printf("<===== EXPECTED: Stack State =====>\n");
1284 printf("Stack pointer: %#02x\n", start);
1285
1286 if (start == 0xFF) {
1287 printf("\n");
1288 return;
1289 }
1290
1291 printf("sp: | int |\t | hex |\n\n");
1292
1293 for (data sp = start + 1; sp < 0xFF; ++sp) {
1294 printf("%#02x: | %i |", sp, stack[sp]);
1295 printf("\t | %#02x |\n", stack[sp]);
1296 }
1297
1298 printf("%#02x: | %i |", 0xFF, stack[0xFF]);
1299 printf("\t | %#02x |\n", stack[0xFF]);
1300
1301 printf("\n");
1302}

Referenced by main().

Here is the caller graph for this function:

◆ print_stack_state()

void print_stack_state ( CPU * cpu)

Procedure that display the current state of the stack.

Parameters
[in]cpuThe current cpu holding the stack

Definition at line 1259 of file cpu.c.

1259 {
1260
1261 printf("<====== Stack State =====>\n");
1262 printf("Stack pointer: %#02x\n", cpu->stack_pointer);
1263
1264 if (cpu->stack_pointer == 0xFF) {
1265 printf("\n");
1266 return;
1267 }
1268
1269 printf("sp: | int |\t | hex |\n\n");
1270
1271 for (uint8_t sp = cpu->stack_pointer + 1; sp < 0xFF; ++sp) {
1272 printf("%#02x: | %i |", sp, cpu->mem[STACK_END | sp]);
1273 printf("\t | %#02x |\n", cpu->mem[STACK_END | sp]);
1274 }
1275
1276 printf("%#02x: | %i |", 0xFF, cpu->mem[STACK_START]);
1277 printf("\t | %#02x |\n", cpu->mem[STACK_START]);
1278
1279 printf("\n");
1280}
#define STACK_START
Definition macros.h:45

References CPU::mem, STACK_END, CPU::stack_pointer, and STACK_START.

Referenced by main().

Here is the caller graph for this function:

◆ reset()

void reset ( CPU * processor)

Procedure that resets the CPU by using the NES reset routine.

Parameters
[in,out]processorThe CPU to reset

◆ start_cpu()

void start_cpu ( CPU * cpu)

Procedure that acts like the reset button of the NES.

Parameters
[in,out]cpuThe CPU to start/restart

Definition at line 27 of file cpu.c.

27 {
28 init_cpu(cpu, cpu->mem);
29 cpu->program_counter = get_address(cpu);
30}
void init_cpu(CPU *cpu, memory mem)
Procedure that initialize the CPU to its boot/reset state.
Definition cpu.c:9

References get_address, init_cpu(), CPU::mem, and CPU::program_counter.

Here is the call graph for this function:

◆ step_cpu()

errcode_t step_cpu ( CPU * cpu)

Function that step a clock cycle.

Parameters
[in,out]cpuThe CPU to step
Returns
0 if everything is fine, 0xff if there's a problem.

Definition at line 1190 of file cpu.c.

1190 {
1191 if (cpu->cycle_counter == 0) {
1192 if (execute_instruction(cpu)) {
1193 goto error;
1194 };
1196
1197 if (cpu->cycle_counter == 0) {
1198 error:
1199 printf("Abnormal opcode: %#02x\n", cpu->mem[cpu->program_counter]);
1200 return ERR_ABN_OPCODE;
1201 }
1202 memory_mirroring(cpu->mem);
1203 return 0;
1204 }
1205
1206 --(cpu->cycle_counter);
1207 return 0;
1208}
unsigned char get_next_instruction(CPU *cpu)
Function that get the next instruction written in the ROM.
Definition cpu.c:32
errcode_t execute_instruction(CPU *cpu)
Function that execute the current instruction in the ROM.
Definition cpu.c:575
void memory_mirroring(memory mem)
This procedure make the memory mirroring.
Definition mem.c:29

References CPU::cycle_counter, ERR_ABN_OPCODE, execute_instruction(), get_next_instruction(), CPU::mem, memory_mirroring(), and CPU::program_counter.

Referenced by main().

Here is the call graph for this function:
Here is the caller graph for this function: