munch

package module
v0.0.0-...-d76f600 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2022 License: GPL-3.0 Imports: 3 Imported by: 0

README

munch

A 6502 emulator.

Usage

package main

import (
	"github.com/noddy76/munch"
)

var rom = []uint8{
	0xa9, 0x01, //       LDA #$01
	0x8d, 0x00, 0x02, // STA $0200
	0xa9, 0x05, //       LDA #$05
	0x8d, 0x01, 0x02, // STA $0201
	0xa9, 0x08, //       LDA #$08
	0x8d, 0x02, 0x02, // STA $0202
}

func main() {
	bus := munch.NewBus()
	bus.Addressable(0x0000, 0x3fff, munch.NewRam(0x40000))
	bus.Addressable(0x8000, 0x8fff, munch.NewRom(rom))
	bus.Addressable(0xfffa, 0xffff, munch.NewRom([]uint8{0x00, 0x00, 0x00, 0x80, 0x00, 0x00}))

	cpu := munch.NewCpu6502(bus)

	cpu.Debug = true

	for cpu.PC != uint16(0x8000+len(rom)) {
		bus.Tick()
	}
}

When run (because debug output from the CPU was turned on) you should see.

8000    LDA #$01                  PC:8002 A:01 X:00 Y:00 SP:fd nv‑BdIzc
8002    STA $0200                 PC:8005 A:01 X:00 Y:00 SP:fd nv‑BdIzc
8005    LDA #$05                  PC:8007 A:05 X:00 Y:00 SP:fd nv‑BdIzc
8007    STA $0201                 PC:800a A:05 X:00 Y:00 SP:fd nv‑BdIzc
800a    LDA #$08                  PC:800c A:08 X:00 Y:00 SP:fd nv‑BdIzc
800c    STA $0202                 PC:800f A:08 X:00 Y:00 SP:fd nv‑BdIzc

The central element is the bus. You can attach Addressables to the bus sunch as the Ram and Rom objects in this package. When the Cpu6502 is created registers itself as a Ticker with the bus. From then on every time Tick() is called on the Bus the call is propogated to every registered Ticker object.

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addressable

type Addressable interface {
	Read(addr uint16) uint8
	Write(addr uint16, v uint8)
}

type Bus

type Bus struct {
	// contains filtered or unexported fields
}

func NewBus

func NewBus() *Bus

func (*Bus) Addressable

func (b *Bus) Addressable(start uint16, end uint16, device Addressable)

func (*Bus) Read

func (b *Bus) Read(addr uint16) uint8

func (*Bus) Tick

func (b *Bus) Tick() error

One tick of the clock

func (*Bus) TickCount

func (b *Bus) TickCount() uint64

func (*Bus) Ticker

func (b *Bus) Ticker(t Ticker)

func (*Bus) Write

func (b *Bus) Write(addr uint16, v uint8)

type Cpu6502

type Cpu6502 struct {
	A  uint8  // Accumulator
	X  uint8  // X index register
	Y  uint8  // Y index register
	SP uint8  // Stack pointer
	P  uint8  // Status register
	PC uint16 // PC register

	Debug          bool
	DisableDecimal bool
	// contains filtered or unexported fields
}

func NewCpu6502

func NewCpu6502(bus *Bus) *Cpu6502

func (*Cpu6502) ClearFlag

func (cpu *Cpu6502) ClearFlag(flag Flag)

func (*Cpu6502) Disassemble

func (cpu *Cpu6502) Disassemble(addr uint16) (string, uint16)

func (*Cpu6502) FlagSet

func (cpu *Cpu6502) FlagSet(flag Flag) bool

func (*Cpu6502) Irq

func (cpu *Cpu6502) Irq()

func (*Cpu6502) Nmi

func (cpu *Cpu6502) Nmi()

func (*Cpu6502) Reset

func (cpu *Cpu6502) Reset()

func (*Cpu6502) SetFlag

func (cpu *Cpu6502) SetFlag(flag Flag)

func (*Cpu6502) SetFlagValue

func (cpu *Cpu6502) SetFlagValue(flag Flag, v bool)

func (*Cpu6502) StatusString

func (cpu *Cpu6502) StatusString() string

func (*Cpu6502) Tick

func (cpu *Cpu6502) Tick() error

func (*Cpu6502) Waiting

func (cpu *Cpu6502) Waiting() bool

type Flag

type Flag uint8
const (
	P_CARRY Flag = 1 << iota
	P_ZERO
	P_DISABLE_IRQ
	P_DECIMAL_MODE
	P_BRK_COMMAND
	P_UNUSED
	P_OVERFLOW
	P_NEGATIVE
)

type NullDevice

type NullDevice struct{}

func (*NullDevice) Read

func (*NullDevice) Read(uint16) uint8

func (*NullDevice) Write

func (*NullDevice) Write(uint16, uint8)

type Ram

type Ram struct {
	// contains filtered or unexported fields
}

func NewRam

func NewRam(size uint) *Ram

func (*Ram) Read

func (r *Ram) Read(addr uint16) uint8

func (*Ram) Write

func (r *Ram) Write(addr uint16, v uint8)

type Rom

type Rom struct {
	// contains filtered or unexported fields
}

func NewRom

func NewRom(bytes []uint8) *Rom

func (*Rom) Read

func (r *Rom) Read(addr uint16) uint8

func (*Rom) Write

func (r *Rom) Write(addr uint16, v uint8)

type Ticker

type Ticker interface {
	Tick() error
}

Jump to

Keyboard shortcuts

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