Documentation
¶
Overview ¶
Package cpu provides types and functions for emulating the 6502 CPU, including CPU operations and instruction execution.
Index ¶
- Constants
- func Mnemonic(mnemonic string, am AddressingModeType) string
- type AddressingMode
- type AddressingModeType
- type CPU
- func (p *CPU) Execute() (Completed, error)
- func (p *CPU) Irq()
- func (p *CPU) Memory() memory.Operations[uint16]
- func (p *CPU) NextByte() byte
- func (p *CPU) Nmi()
- func (p *CPU) Operands() []byte
- func (p *CPU) Pop() byte
- func (p *CPU) Push(b byte)
- func (p *CPU) Registers() *Registers
- func (p *CPU) Reset()
- func (p *CPU) Resume()
- func (p *CPU) Stop()
- type CPU6502
- type Completed
- type HaltExecution
- type InstructionDefinition
- type InstructionFunc
- type InstructionGetter
- type LoadAddress
- type OpCodeDef
- type Registers
- func (r *Registers) IsSet(f StatusFlag) bool
- func (r *Registers) SetCarryFlag(oldValue, result byte)
- func (r *Registers) SetNegativeFlag(result byte)
- func (r *Registers) SetOverflowFlag(m, n, result byte, isAddition bool)
- func (r *Registers) SetStatus(f StatusFlag, on bool)
- func (r *Registers) SetZeroFlag(result byte)
- type StatusFlag
- type StoreAddress
Constants ¶
const ( // ByteAddressing defines a two-character byte addressing mode representation. ByteAddressing = "nn" // WordAddressing defines a four-character word addressing mode representation. WordAddressing = "nnnn" )
const ( // CarryFlag indicates whether an arithmetic operation has resulted in a carry out of the most significant bit. CarryFlag StatusFlag = 1 << iota // Bit 1 // ZeroFlag indicates whether the result of an operation is zero. ZeroFlag = 1 << iota // Bit 2 // InterruptDisableFlag disables interrupts when set. InterruptDisableFlag = 1 << iota // Bit 3 // DecimalFlag enables Binary-Coded Decimal (BCD) mode when set. DecimalFlag = 1 << iota // Bit 4 // BreakFlag indicates a BRK instruction has been executed. BreakFlag = 1 << iota // Bit 5 // UnusedFlag is an unused flag in the 6502 status register. UnusedFlag = 1 << iota // Bit 6 // OverflowFlag indicates whether an arithmetic operation has resulted in an overflow. OverflowFlag = 1 << iota // Bit 7 // NegativeFlag indicates whether the result of an operation is negative. NegativeFlag = 1 << iota // Bit 8 )
Variables ¶
This section is empty.
Functions ¶
func Mnemonic ¶
func Mnemonic(mnemonic string, am AddressingModeType) string
Mnemonic formats the mnemonic string with the addressing mode type.
Types ¶
type AddressingMode ¶
type AddressingMode interface { // Store returns a function that stores a byte at the calculated address. // The ignoreExtraCycle parameter determines whether to ignore the extra cycle typically required for certain addressing modes. Store(cpu CPU6502, ignoreExtraCycle bool) StoreAddress // Load returns a function that loads a byte from the calculated address. // The ignoreExtraCycle parameter determines whether to ignore the extra cycle typically required for certain addressing modes. Load(cpu CPU6502, ignoreExtraCycle bool) LoadAddress // Address calculates and returns the effective address for the addressing mode. Address(cpu CPU6502) uint16 }
AddressingMode defines an interface for various addressing modes in the 6502 CPU. Each addressing mode can store and load values and calculate the effective address.
type AddressingModeType ¶
type AddressingModeType string
AddressingModeType represents a string type for addressing mode representation.
const ( // ImpliedModeStr is the implied addressing mode. ImpliedModeStr AddressingModeType = "" // AbsoluteIndirectModeStr is the absolute indirect addressing mode. AbsoluteIndirectModeStr AddressingModeType = "(nnnn)" // AbsoluteModeStr is the absolute addressing mode. AbsoluteModeStr AddressingModeType = "nnnn" // AbsoluteIndexedXModeStr is the absolute indexed X addressing mode. AbsoluteIndexedXModeStr AddressingModeType = "nnnn,X" // AbsoluteIndexedYModeStr is the absolute indexed Y addressing mode. AbsoluteIndexedYModeStr AddressingModeType = "nnnn,Y" // AccumulatorModeStr is the accumulator addressing mode. AccumulatorModeStr AddressingModeType = "A" // ZeropageModeStr is the zeropage addressing mode. ZeropageModeStr AddressingModeType = "nn" // ZeropageXModeStr is the zeropage indexed X addressing mode. ZeropageXModeStr AddressingModeType = "nn,X" // ZeropageYModeStr is the zeropage indexed Y addressing mode. ZeropageYModeStr AddressingModeType = "nn,Y" // IndexedIndirectModeStr is the indexed indirect addressing mode. IndexedIndirectModeStr AddressingModeType = "(nn,X)" // IndirectIndexedModeStr is the indirect indexed addressing mode. IndirectIndexedModeStr AddressingModeType = "(nn),Y" // ImmediateModeStr is the immediate addressing mode. ImmediateModeStr AddressingModeType = "#nn" // RelativeModeStr is the relative addressing mode. RelativeModeStr AddressingModeType = "*nn" )
type CPU ¶
type CPU struct { Reg *Registers // contains filtered or unexported fields }
CPU represents the 6502 CPU with registers, memory, and opcode definitions.
func NewCPU ¶
func NewCPU(m memory.Operations[uint16]) *CPU
NewCPU creates a new Cpu instance with the provided memory functions.
func (*CPU) Execute ¶
Execute executes the current instruction and returns whether it is completed and any error encountered.
func (*CPU) Memory ¶
func (p *CPU) Memory() memory.Operations[uint16]
Memory returns the memory functions used by the CPU.
type CPU6502 ¶
type CPU6502 interface { Stop() Resume() Execute() (Completed, error) Nmi() Irq() Reset() Push(b byte) Pop() byte Registers() *Registers Memory() memory.Operations[uint16] Operands() []byte }
CPU6502 interface defines the methods required to emulate the 6502 CPU.
type Completed ¶
type Completed bool
Completed represents whether an instruction has completed execution.
type HaltExecution ¶
type HaltExecution interface { Stop() Resume() }
HaltExecution interface defines methods to stop and resume CPU execution.
type InstructionDefinition ¶
type InstructionDefinition struct {
// contains filtered or unexported fields
}
InstructionDefinition holds the function to get the addressing mode based on the addressing mode type.
func NewInstruction ¶
func NewInstruction(am func(am AddressingModeType) AddressingMode) InstructionDefinition
NewInstruction creates a new InstructionDefinition instance.
func (InstructionDefinition) Instruction ¶
func (id InstructionDefinition) Instruction(Mnemonic string, cycles int, execGet InstructionGetter) *OpCodeDef
Instruction creates an OpCodeDef instance for the given mnemonic, cycles, and execution function getter.
type InstructionFunc ¶
InstructionFunc defines a function type for executing an instruction and returning whether it is completed and any error encountered.
type InstructionGetter ¶
type InstructionGetter func(OpCodeDef) InstructionFunc
InstructionGetter defines a function type for getting the instruction execution function based on the opcode definition.
type LoadAddress ¶
LoadAddress defines a function type that loads a byte and indicates if the operation is completed.
type OpCodeDef ¶
type OpCodeDef struct { Mnemonic string AddressingModeType AddressingModeType Bytes int Cycles int ExecGetter InstructionGetter AddressingMode AddressingMode }
OpCodeDef represents the definition of an opcode, including its mnemonic, addressing mode type, byte size, cycle count, and execution function.
type Registers ¶
Registers holds the CPU registers including the accumulator (A), index registers (X and Y), stack pointer (S), program counter (PC), and status flags.
func (*Registers) IsSet ¶
func (r *Registers) IsSet(f StatusFlag) bool
IsSet checks if the specified status flag is set.
func (*Registers) SetCarryFlag ¶
SetCarryFlag sets the carry flag based on the comparison of oldValue and result.
func (*Registers) SetNegativeFlag ¶
SetNegativeFlag sets the negative flag if the result's most significant bit is set.
func (*Registers) SetOverflowFlag ¶
SetOverflowFlag sets the overflow flag based on the result of an addition or subtraction operation.
func (*Registers) SetStatus ¶
func (r *Registers) SetStatus(f StatusFlag, on bool)
SetStatus sets or clears the specified status flag.
func (*Registers) SetZeroFlag ¶
SetZeroFlag sets the zero flag if the result is zero.
type StatusFlag ¶
type StatusFlag byte
StatusFlag represents the various status flags of the 6502 CPU.
type StoreAddress ¶
StoreAddress defines a function type that stores a byte and indicates if the operation is completed.