cpu

package
v0.0.0-...-c42c46a Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 4, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Overview

Package cpu provides types and functions for emulating the 6502 CPU, including CPU operations and instruction execution.

Index

Constants

View Source
const (
	// ByteAddressing defines a two-character byte addressing mode representation.
	ByteAddressing = "nn"

	// WordAddressing defines a four-character word addressing mode representation.
	WordAddressing = "nnnn"
)
View Source
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

func (p *CPU) Execute() (Completed, error)

Execute executes the current instruction and returns whether it is completed and any error encountered.

func (*CPU) Irq

func (p *CPU) Irq()

Irq triggers an interrupt request.

func (*CPU) Memory

func (p *CPU) Memory() memory.Operations[uint16]

Memory returns the memory functions used by the CPU.

func (*CPU) NextByte

func (p *CPU) NextByte() byte

NextByte reads the next byte from memory and increments the program counter.

func (*CPU) Nmi

func (p *CPU) Nmi()

Nmi triggers a non-maskable interrupt.

func (*CPU) Operands

func (p *CPU) Operands() []byte

Operands returns the operands for the current instruction.

func (*CPU) Pop

func (p *CPU) Pop() byte

Pop pops a byte from the stack.

func (*CPU) Push

func (p *CPU) Push(b byte)

Push pushes a byte onto the stack.

func (*CPU) Registers

func (p *CPU) Registers() *Registers

Registers returns the CPU's registers.

func (*CPU) Reset

func (p *CPU) Reset()

Reset resets the CPU to its initial state.

func (*CPU) Resume

func (p *CPU) Resume()

Resume resumes the CPU's execution.

func (*CPU) Stop

func (p *CPU) Stop()

Stop halts the CPU's execution.

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

type InstructionFunc = func() (Completed, error)

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

type LoadAddress func() (byte, Completed)

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.

func OpCodes

func OpCodes(p *CPU) []*OpCodeDef

OpCodes returns a map of the actual 6502 opcode numbers to OpCodeDef instances.

type Registers

type Registers struct {
	A, X, Y, S byte
	PC         uint16
	Status     byte
}

Registers holds the CPU registers including the accumulator (A), index registers (X and Y), stack pointer (S), program counter (PC), and status flags.

func NewRegisters

func NewRegisters() *Registers

NewRegisters creates a new Registers instance.

func (*Registers) IsSet

func (r *Registers) IsSet(f StatusFlag) bool

IsSet checks if the specified status flag is set.

func (*Registers) SetCarryFlag

func (r *Registers) SetCarryFlag(oldValue, result byte)

SetCarryFlag sets the carry flag based on the comparison of oldValue and result.

func (*Registers) SetNegativeFlag

func (r *Registers) SetNegativeFlag(result byte)

SetNegativeFlag sets the negative flag if the result's most significant bit is set.

func (*Registers) SetOverflowFlag

func (r *Registers) SetOverflowFlag(m, n, result byte, isAddition bool)

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

func (r *Registers) SetZeroFlag(result byte)

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

type StoreAddress func(b byte) Completed

StoreAddress defines a function type that stores a byte and indicates if the operation is completed.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL