Documentation
¶
Overview ¶
Package sim6502 provides a 6502 processor simulator
Index ¶
- Constants
- Variables
- func FormatInstruction(pc uint16, ins *instruction, b1 uint8, b2 uint8, ma uint16, mc uint8, ...) string
- func GetVector(mem Memory, vector SystemVector) uint16
- func SetVector(mem Memory, vector SystemVector, addr uint16)
- type AddressingMode
- type BreakpointHandler
- type BreakpointHandlerDisableTrace
- type BreakpointHandlerEnableTrace
- type MappableMemory
- func (m *MappableMemory) Clear()
- func (m *MappableMemory) DisableReadDebugging()
- func (m *MappableMemory) DisableWriteDebugging()
- func (m *MappableMemory) EnableReadDebugging(w io.Writer)
- func (m *MappableMemory) EnableWriteDebugging(w io.Writer)
- func (m *MappableMemory) Map(handler MappedMemoryHandler)
- func (m *MappableMemory) Read(location uint16) uint8
- func (m *MappableMemory) Write(location uint16, value uint8)
- type MappedMemoryAddressRange
- type MappedMemoryHandler
- type MappedMemoryReadHandler
- type MappedMemoryWriteHandler
- type Memory
- type Processor
- func (p *Processor) ClearBreakpoints() *Processor
- func (p *Processor) DumpState(w io.Writer)
- func (p *Processor) GetLastRunPerformance() ProcessorPerformance
- func (p *Processor) IRQ(state bool)
- func (p *Processor) Init() *Processor
- func (p *Processor) IsIRQSet() bool
- func (p *Processor) IsResetSet() bool
- func (p *Processor) Load(r io.ByteReader, start uint16) *Processor
- func (p *Processor) LoadHex(r io.Reader) *Processor
- func (p *Processor) LoadIllegalInstructions() *Processor
- func (p *Processor) Memory() Memory
- func (p *Processor) NMI(state bool) error
- func (p *Processor) Registers() *Registers
- func (p *Processor) Reset(state bool)
- func (p *Processor) RunFrom(addr uint16) (err error)
- func (p *Processor) SetBreakpoint(breakAt uint16, handler BreakpointHandler) *Processor
- func (p *Processor) SetClock(cyclesPerSecond int64) *Processor
- func (p *Processor) SetDebugWriter(w io.Writer) *Processor
- func (p *Processor) SetModel65C02() *Processor
- func (p *Processor) SetOption(opt ProcessorOption, value bool) *Processor
- func (p *Processor) SetVector(vector uint16, value uint16) *Processor
- func (p *Processor) Step() (err error, stop bool)
- func (p *Processor) Stop()
- type ProcessorModel
- type ProcessorOption
- type ProcessorPerformance
- type ProgramCounter
- type RawMemory
- func (m *RawMemory) Clear()
- func (m *RawMemory) DisableReadDebugging()
- func (m *RawMemory) DisableWriteDebugging()
- func (m *RawMemory) EnableReadDebugging(w io.Writer)
- func (m *RawMemory) EnableWriteDebugging(w io.Writer)
- func (m *RawMemory) Read(location uint16) uint8
- func (m *RawMemory) Write(location uint16, value uint8)
- type Registers
- type SEC
- type SED
- type SEI
- type SRFlag
- type STX
- type STY
- type StackPointer
- type StatusRegister
- type SystemVector
- type TAX
- type TAY
- type TSX
- type TXA
- type TXS
- type TYA
Constants ¶
const StackLocation uint16 = 0x0100 // Base of stack
StackLocation points to the base of the stack at 0x0100 N.B. Stack grows down from PTR=0xff (i.e. 0x01FF)
Variables ¶
var IllegalInstructionMagicConstant uint8 = 0x00
Because ANE is highly unstable, operation depends on a constant which may vary depending on chip series, temperature and so forth change the constant if you want ref: https://www.masswerk.at/6502/6502_instruction_set.html#ANE
var StackDebug = false
StackDebug can be set to true to output messages when stack operations occur strictly for debugging
Functions ¶
func FormatInstruction ¶
func GetVector ¶
func GetVector(mem Memory, vector SystemVector) uint16
GetVector will return the memory address in the specified vector
func SetVector ¶
func SetVector(mem Memory, vector SystemVector, addr uint16)
Types ¶
type AddressingMode ¶
type AddressingMode int
const ( A AddressingMode = iota ABS ABS_X ABS_Y IMMED IMPL IND X_IND IND_Y REL ZPG ZPG_X ZPG_Y ZPG_REL // 65C02 only IND_ABS_X // 65C02 only ZPG_IND //65C02 only )
func (AddressingMode) String ¶
func (mode AddressingMode) String() string
type BreakpointHandler ¶
type BreakpointHandler interface {
// HandleBreak will be called when the PC reaches the listed brekapoint address
// but before that instruction is executed
// The current processor state is passed to the handler for inspection and, if desired,
// modification
HandleBreak(proc *Processor) error
}
BreakpointHandler defines the interface to be implemented by the user when implementing a breakpoint
type BreakpointHandlerDisableTrace ¶
type BreakpointHandlerDisableTrace struct{}
BreakpointHandlerDisableTrace is a provided breakpoint handler that will disable trace when the breakpoint is reached
func (*BreakpointHandlerDisableTrace) HandleBreak ¶
func (b *BreakpointHandlerDisableTrace) HandleBreak(proc *Processor)
type BreakpointHandlerEnableTrace ¶
type BreakpointHandlerEnableTrace struct{}
BreakpointHandlerEnableTrace is a provided breakpoint handler that will enable trace when the breakpoint is reached
func (*BreakpointHandlerEnableTrace) HandleBreak ¶
func (b *BreakpointHandlerEnableTrace) HandleBreak(proc *Processor)
type MappableMemory ¶
type MappableMemory struct {
// contains filtered or unexported fields
}
MappableMemory is a raw 64K address space in which you may map additional handlers to provide either read or write functionality at specific addresses
func (*MappableMemory) DisableReadDebugging ¶
func (m *MappableMemory) DisableReadDebugging()
DisableReadDebugging will disable debugging of reads
func (*MappableMemory) DisableWriteDebugging ¶
func (m *MappableMemory) DisableWriteDebugging()
DisableWriteDebugging will disable debugging of writes
func (*MappableMemory) EnableReadDebugging ¶
func (m *MappableMemory) EnableReadDebugging(w io.Writer)
EnableReadDebugging will report every read to the provided writer
func (*MappableMemory) EnableWriteDebugging ¶
func (m *MappableMemory) EnableWriteDebugging(w io.Writer)
EnableWriteDebugging will report every write to the provided writer
func (*MappableMemory) Map ¶
func (m *MappableMemory) Map(handler MappedMemoryHandler)
Map will map a new handler into the memory space The MappedMemoryHandler must implement either or both of MappedMemoryReadHandler and/or MappedMemoryWriteHandler otherwise it won't do anything
func (*MappableMemory) Write ¶
func (m *MappableMemory) Write(location uint16, value uint8)
Write memory
type MappedMemoryAddressRange ¶
type MappedMemoryAddressRange struct {
Start, End uint16
}
MappedMemoryAddressRange provides an address range used for mapping memory Start must be <= End
type MappedMemoryHandler ¶
type MappedMemoryHandler interface {
// AddressRange reports the memory mapped address range supported by the device
AddressRange() []MappedMemoryAddressRange
}
MappedMemoryHandler represents an implementation of a memory mapped device
type MappedMemoryReadHandler ¶
type MappedMemoryReadHandler interface {
MappedMemoryHandler
// Read is called when the CPU reads the specified
Read(addr uint16) uint8
}
MappedMemoryReadHandler is a mapped device that receives provides data to the CPU when it's memory location(s) are read
type MappedMemoryWriteHandler ¶
type MappedMemoryWriteHandler interface {
MappedMemoryHandler
// Write is called when the CPU writes to the specified address
Write(addr uint16, val uint8)
}
MappedMemoryWriteHandler is a mapped device that received writes from the CPU to it's memory location(s)
type Memory ¶
type Memory interface {
// Read reads a byte from the specified address
Read(addr uint16) uint8
// Write writes a byte to the specified address
Write(addr uint16, value uint8)
// Clear must clear all memory to zero value
Clear()
}
Memory is the interface that must be implemented by any memory provider The memory provider should implement the entire 64K address space as it sees fit
type Processor ¶
type Processor struct {
// contains filtered or unexported fields
}
Processor represents a 6502 series processor Always create a new processor instance with NewProcessor() function
func NewProcessor ¶
NewProcessor returns a new, properly initialized 6502 processor
func (*Processor) ClearBreakpoints ¶
ClearBreakpoints will clear all previously defined breakpoints Call only when the processor is stopped
func (*Processor) DumpState ¶
DumpState can be used to output the current processor state to a writer
func (*Processor) GetLastRunPerformance ¶
func (p *Processor) GetLastRunPerformance() ProcessorPerformance
GetLastRunPerformance will return a performance report that can be used to evaluate the last run of the processor
func (*Processor) Init ¶
Init will initialite the processor, clearing memory, registers and unsetting any breakpoints
func (*Processor) IsResetSet ¶
IsResetSet returns true if the simulated reset line is pulled low
func (*Processor) Load ¶
func (p *Processor) Load(r io.ByteReader, start uint16) *Processor
Load is used to load raw binary content into the processor at the indicated start location
func (*Processor) LoadIllegalInstructions ¶
LoadIllegalInstructions will provide support for undocumented/unofficial/illegal instruction set
func (*Processor) Memory ¶
Memory can be used to retrieve a copy of the current memory implementation
func (*Processor) Registers ¶
Registers can be used to retrieve a copy of the current processor registers
func (*Processor) Reset ¶
Reset simulates the reset line being pulled low (state = true) or not (state = false) To reset the processor you have to pull it (state = true) and then set back to high after a few cycles, otherwise it will be missed
func (*Processor) RunFrom ¶
RunFrom will start the processor running at the specified address An error is returned if an unrecoverable error is encountered Typically this would be an undefined opcode, or an error returned from a breakpointer handler
func (*Processor) SetBreakpoint ¶
func (p *Processor) SetBreakpoint(breakAt uint16, handler BreakpointHandler) *Processor
SetBreakpoint is used to set a breakpoint consisting of a user provided handler The handler must return it's desired breakpoint address and a function that will be invoked when the PC reaches the breakpoint (before the instruction is executed) Call only when the processor is stopped Multiple breakpointers/handlers can be set at a given address and will be executed in the order set
func (*Processor) SetClock ¶
SetClock will set the clock speed in processor cycles per second I.E. for a 1MHZ clock set ticksPerSecond to 1,000,000 A value of zero is unrestricted (all instructions run at maximum possible speed with no regard for timing)
func (*Processor) SetDebugWriter ¶
SetDebugWriter can be used to set a writer to which debug messages will be sent when debug options are activated This defaults to STDERR
func (*Processor) SetModel65C02 ¶
SetModel65C02 will load 65C02 additional opcodes and set other specific behaviors for that processor type
func (*Processor) SetOption ¶
func (p *Processor) SetOption(opt ProcessorOption, value bool) *Processor
SetOption can be used to set one of several processor options these are generally set to true to enable debugging facilities
func (*Processor) SetVector ¶
SetVector can be used to set one of the system vectors. The value of vector should be one of:
type ProcessorModel ¶
type ProcessorModel int
ProcessorModel allows for different processor models and their associated behaviors the default model is 6502
const ( // ProcessorModel6502 represents the original 6502 ProcessorModel6502 ProcessorModel = iota // ProcessorModel65C02 represents the CMOS 65C02 // which exhibits some different behaviors with respect to the decimal flag // on BRK and interrupt ProcessorModel65C02 )
type ProcessorOption ¶
type ProcessorOption int
ProcessorOption defines options that can be used to influence the behavior of the processor Hint: setting some of these options via breakpoint handlers can be useful for debugging
const ( // ErrorOnSelfJump option, when activated, will cause an error to be generated by the processor // if a branch or JMP instruction redirects back to itself // This is useful for testing scenarios ErrorOnSelfJump ProcessorOption = iota // Trace option will cause output of a single line of debug for every instructions (pre-execution) Trace // TraceStack option will cause the stack to be dumped prior to every operation TraceStack // TraceInterrupts will cause messages to be printed when external interrupts are triggered TraceInterrupts // ErrorOnJAM will cause an error to be generated if an (illegal) JAM is enconutered // with the illegal instruction set enabled // if this is not set, then the processor will just lock up ErrorOnJAM // AutoResetIRQ will cause the IRQ line to be forcefully unasserted when the IRQ // branch is taken AutoResetIRQ // AutoRsetNMI will cause the NMI line to be forcefully unasserted when the NMI // branch is taken AutoResetNMI // Fix6502BrokenJMP will fix the incorrect behavior of indirect JUMP on 6502 (not 65C02 which is not broken) // The (implemented) broken behavior is to jump to the address stored at xxff (L), xx00 (H) instead of // xxff (L), xx+100 (H) Fix6052BrokenJMP )
type ProcessorPerformance ¶
type ProcessorPerformance struct {
// RanForNanoseconds indicates the number of nanoseconds for which the processor last ran
RanForNanoseconds int64
// RanForCycles indicates the number of cycles for which the processor last ran
RanForCycles int64
// EffectiveClock indicates the achieved clock rate of the last ran, in processor cycles per second
EffectiveClock int64
// InstructionsExecuted is the count of instructions executed
InstructionsExecuted uint64
}
ProcessorPerformance is used to report on the performance of the processor The data contained within is not valid if the processor is running, it is only valid once the processor has been stopped and reflects the stats from the last run
type ProgramCounter ¶
type ProgramCounter struct {
// contains filtered or unexported fields
}
func (*ProgramCounter) Current ¶
func (pc *ProgramCounter) Current() uint16
func (*ProgramCounter) Init ¶
func (pc *ProgramCounter) Init(p *Processor)
func (*ProgramCounter) Next ¶
func (pc *ProgramCounter) Next() uint8
func (*ProgramCounter) Set ¶
func (pc *ProgramCounter) Set(val uint16)
type RawMemory ¶
type RawMemory struct {
// contains filtered or unexported fields
}
RawMemory implements a simple R/W 64K address space with no special features
func (*RawMemory) DisableReadDebugging ¶
func (m *RawMemory) DisableReadDebugging()
DisableReadDebugging will disable debugging of reads
func (*RawMemory) DisableWriteDebugging ¶
func (m *RawMemory) DisableWriteDebugging()
DisableWriteDebugging will disable debugging of writes
func (*RawMemory) EnableReadDebugging ¶
EnableReadDebugging will report every read to the provided writer
func (*RawMemory) EnableWriteDebugging ¶
EnableWriteDebugging will report every write to the provided writer
type Registers ¶
type Registers struct {
// A is the accumulator
A uint8
// X is the X register
X uint8
// Y is the Y register
Y uint8
// SR is the status register
SR StatusRegister
// SP is the stack pointer
SP StackPointer
// PC is the program counter
PC ProgramCounter
}
Registers defines the structure containing all processor registers
type SRFlag ¶
type SRFlag uint8
const ( // SRFlagN is the bit address of the negative flag SRFlagN SRFlag = 7 // SRFlagV is the bit address of the overflow flag SRFlagV SRFlag = 6 // SRFlagU is the bit address of the unused flag SRFlagU SRFlag = 5 // SRFlagB is the bit address of the break flag SRFlagB SRFlag = 4 // SRFlagD is the bit addres of the decimal (BCD) flag SRFlagD SRFlag = 3 // SRFlagI is the bit address of the interrupt flag SRFlagI SRFlag = 2 // SRFlagZ is the bit address of the zero flag SRFlagZ SRFlag = 1 // SRFlagC is the bit address of the carry flag SRFlagC SRFlag = 0 )
type StackPointer ¶
type StackPointer struct {
// contains filtered or unexported fields
}
StackPointer is our implementation of the stack pointer
func (*StackPointer) GetStackPointer ¶
func (sp *StackPointer) GetStackPointer() uint8
GetStackPointer retrieves the current stack pointer value
func (*StackPointer) PeekStackHead ¶
func (sp *StackPointer) PeekStackHead() uint8
PeekStackHead returns the byte on the stack head without altering the stack
func (*StackPointer) Push ¶
func (sp *StackPointer) Push(value uint8)
Push pushes a byte to the stack
func (*StackPointer) SetStackPointer ¶
func (sp *StackPointer) SetStackPointer(ptr uint8)
SetStackPointer is used to explicitly set the stack pointer
func (*StackPointer) String ¶
func (sp *StackPointer) String() string
String provides a string dump of the stack
type StatusRegister ¶
type StatusRegister struct {
// contains filtered or unexported fields
}
StatusRegister defines a status register entity
func (*StatusRegister) Clear ¶
func (s *StatusRegister) Clear(flag SRFlag)
Clear will clear a specific flag in the status register
func (*StatusRegister) IsSet ¶
func (s *StatusRegister) IsSet(flag SRFlag) bool
IsSet returns true if the specified flag is set, otherwise false
func (*StatusRegister) Set ¶
func (s *StatusRegister) Set(flag SRFlag)
Set will set a specific flag in the status register
func (*StatusRegister) SetTo ¶
func (s *StatusRegister) SetTo(flag SRFlag, state bool)
SetTo will set a specific flag in the status register to either on or off depending on the provided state (true=on)
func (*StatusRegister) String ¶
func (s *StatusRegister) String() string
String provides a string representation of the status register current state
func (*StatusRegister) Value ¶
func (s *StatusRegister) Value() uint8
Value returns the raw value of the status register
type SystemVector ¶
type SystemVector uint16
SystemVector type is used for all available system vector addresses
const ( // VectorNMI is the non maskable interrupt vector address VectorNMI SystemVector = 0xFFFA // VectorReset is the reset vector address VectorReset SystemVector = 0xFFFC // VectorIRQ is the interrupt vector request VectorIRQ SystemVector = 0xFFFE )
Source Files
¶
- addrmode.go
- breakpoint.go
- doc.go
- illegalInstructions.go
- instruction.go
- instruction_65C02.go
- instruction_ADC.go
- instruction_AND.go
- instruction_ASL.go
- instruction_BCC.go
- instruction_BCS.go
- instruction_BEQ.go
- instruction_BIT.go
- instruction_BMI.go
- instruction_BNE.go
- instruction_BPL.go
- instruction_BRK.go
- instruction_BVC.go
- instruction_BVS.go
- instruction_CLC.go
- instruction_CLD.go
- instruction_CLI.go
- instruction_CLV.go
- instruction_CMP.go
- instruction_CPX.go
- instruction_CPY.go
- instruction_C_BBR.go
- instruction_C_BBS.go
- instruction_C_BRA.go
- instruction_C_PHX.go
- instruction_C_PHY.go
- instruction_C_PLX.go
- instruction_C_PLY.go
- instruction_C_RMB.go
- instruction_C_SMB.go
- instruction_C_STP.go
- instruction_C_STZ.go
- instruction_C_TRB.go
- instruction_C_TSB.go
- instruction_C_WAI.go
- instruction_DEC.go
- instruction_DEX.go
- instruction_DEY.go
- instruction_EOR.go
- instruction_INC.go
- instruction_INX.go
- instruction_INY.go
- instruction_I_ALR.go
- instruction_I_ANC.go
- instruction_I_ANE.go
- instruction_I_ARR.go
- instruction_I_DCP.go
- instruction_I_ISC.go
- instruction_I_JAM.go
- instruction_I_LAS.go
- instruction_I_LAX.go
- instruction_I_LXA.go
- instruction_I_RLA.go
- instruction_I_RRA.go
- instruction_I_SAX.go
- instruction_I_SBX.go
- instruction_I_SHA.go
- instruction_I_SHX.go
- instruction_I_SHY.go
- instruction_I_SLO.go
- instruction_I_SRE.go
- instruction_I_TAS.go
- instruction_I_USB.go
- instruction_JMP.go
- instruction_JSR.go
- instruction_LDA.go
- instruction_LDX.go
- instruction_LDY.go
- instruction_LSR.go
- instruction_NOP.go
- instruction_ORA.go
- instruction_PHA.go
- instruction_PHP.go
- instruction_PLA.go
- instruction_PLP.go
- instruction_ROL.go
- instruction_ROR.go
- instruction_RTI.go
- instruction_RTS.go
- instruction_SBC.go
- instruction_SEC.go
- instruction_SED.go
- instruction_SEI.go
- instruction_STA.go
- instruction_STX.go
- instruction_STY.go
- instruction_TAX.go
- instruction_TAY.go
- instruction_TSX.go
- instruction_TXA.go
- instruction_TXS.go
- instruction_TYA.go
- mappable_memory.go
- memory.go
- pc.go
- processor.go
- raw_memory.go
- register.go
- stack.go
- status.go
- test.go
- util.go
- variable_cycle_base.go
- vector.go
- waiter.go