nes

package
v0.0.0-...-ec4120d Latest Latest
Warning

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

Go to latest
Published: Sep 7, 2019 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const CPUFrequency = 1789773

Variables

This section is empty.

Functions

This section is empty.

Types

type APU

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

func NewAPU

func NewAPU(console *Console) *APU

func (*APU) Step

func (apu *APU) Step()

type CPU

type CPU struct {
	Console *Console
	RAM     [2048]byte

	NumCycles uint64
	PC        uint16
	SP        byte
	A         byte
	X         byte
	Y         byte
	// contains filtered or unexported fields
}

CPU emulates a NES 6502 CPU.

https://web.archive.org/web/20110320213225/http://www.obelisk.demon.co.uk/6502/

func NewCPU

func NewCPU(console *Console) *CPU

NewCPU constructs and returns a CPU for the given console.

func (*CPU) NMI

func (c *CPU) NMI() int

NMI starts a non-maskable interrupt.

func (*CPU) P

func (c *CPU) P() byte

func (*CPU) Step

func (c *CPU) Step() (uint64, error)

Step runs the CPU for one step.

Normally this is one instruction, but multiple instructions may be executed if an IRQ is handled.

Returns the total number of CPU cycles executed in the lifetime of the CPU, starting from 0.

func (*CPU) String

func (c *CPU) String() string

String returns the CPU state as a string.

type Cartridge

type Cartridge struct {
	Mirror MirrorType
	Mapper Mapper
	PRG    [][]byte // [bank][byte], 16k banks.
	CHR    [][]byte // [bank][byte], 8k banks.
	SRAM   [][]byte // [bank][byte], 8k banks.
}

A Cartridge represents a physical game cartridge.

func LoadCartridge

func LoadCartridge(filename string) (*Cartridge, error)

LoadCartridge opens and reads an iNES format ROM file.

cart, err := LoadCartridge("test.rom")

func NewCartridge

func NewCartridge(numPRGBanks int, numCHRBanks int, numSRAMBanks int) *Cartridge

NewCartridge constructs an empty Cartridge with the given memory bank sizes.

PRG banks are 16k bytes, CHR and SRAM banks are both 8k bytes. No mapper is set.

func ReadCartridge

func ReadCartridge(file io.Reader) (*Cartridge, error)

ReadCartridge reads an iNES v1.0 ROM file from file.

http://wiki.nesdev.com/w/index.php/INES

func (*Cartridge) IRQ

func (cart *Cartridge) IRQ() bool

IRQ returns true if the Cartridge has any outstanding interrupt requests (IRQs). IRQs are typically generated by Mappers performing scanline counting.

Any outstanding IRQs are reset.

func (*Cartridge) Read

func (cart *Cartridge) Read(address uint16, isPPU bool) byte

Read reads a byte from the cartridge.

address is the location to read from. Set isPPU to read from the PPU address space instead of the CPU address space.

func (*Cartridge) Write

func (cart *Cartridge) Write(address uint16, value byte, isPPU bool)

Write writes a byte to the cartridge.

address is the location to write to. Set isPPU to write to the PPU address space instead of the CPU address space.

type Console

type Console struct {
	Cart    *Cartridge
	CPU     *CPU
	PPU     *PPU
	APU     *APU
	Joypads [2]*Joypad
	// contains filtered or unexported fields
}

Console represents a NES console and its main hardware components (the cartridge, CPU, PPU, and joypads).

func NewConsole

func NewConsole(cart *Cartridge) *Console

NewConsole returns a Console initialised with cart.

func (*Console) SetAudioChannel

func (c *Console) SetAudioChannel(channel chan float32)

func (*Console) SetAudioSampleRate

func (c *Console) SetAudioSampleRate(sampleRate float64)

func (*Console) Step

func (c *Console) Step() (*image.RGBA, error)

Step runs the Console for 1 CPU instruction. The PPU runs at the same time.

Call Step() repeatedly to simulate the Console. For the majority of calls, Step() returns a nil *image.RGBA. Approximately 60 times a second the PPU emits a new image frame, and a non-nil *image.RGBA is returned. The image is 256x240px.

To regulate emulation speed, Step() may sleep when emitting an image. It sleeps to regulate the output to around 60 frames per second (as per NTSC).

type DMC

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

type Filter

type Filter interface {
	Step(x float32) float32
}

type FilterChain

type FilterChain []Filter

func (FilterChain) Step

func (fc FilterChain) Step(x float32) float32

type FirstOrderFilter

type FirstOrderFilter struct {
	B0 float32
	B1 float32
	A1 float32
	// contains filtered or unexported fields
}

func (*FirstOrderFilter) Step

func (f *FirstOrderFilter) Step(x float32) float32

type Joypad

type Joypad struct {
	A bool
	B bool

	Select bool
	Start  bool

	Up    bool
	Down  bool
	Left  bool
	Right bool
	// contains filtered or unexported fields
}

Joypad represents the state of a standard game controller.

A standard controller has the buttons: A, B, Select, Start, Up, Down, Left, and Right.

http://wiki.nesdev.com/w/index.php/Standard_controller

func NewJoypad

func NewJoypad() *Joypad

NewJoypad returns a new Joypad.

Joypad does not perform any IO. Call SetReadKeysCallback() to provide a function to update the Joypad's state from the physical input device (keyboard/USB controller/etc).

func (*Joypad) Read

func (j *Joypad) Read() byte

Read reads a byte from the joypad's output register.

func (*Joypad) SetReadKeysCallback

func (j *Joypad) SetReadKeysCallback(callback func())

SetReadKeysCallback sets a callback function used to update the Joypad's button press state.

The callback function should update j.{A,B,Select,Start,Up,Down,Left,Right} to match the physical input device.

func (*Joypad) Write

func (j *Joypad) Write(value byte)

Write writes a byte to the joypad's input register.

The ReadKeysCallback is called here to update the joypad's set of currently pressed keys.

type Mapper

type Mapper interface {
	Read(address uint16, isPPU bool) byte
	Write(address uint16, value byte, isPPU bool)
	IRQ() bool
	// contains filtered or unexported methods
}

A Mapper sits between a cartridge and the console.

Mappers provide functions such as memory bank switching (as a cartridge can contain more memory than the NES's address space allows), and scanline counting.

http://wiki.nesdev.com/w/index.php/Mapper

func NewMapper

func NewMapper(id int, cart *Cartridge) (Mapper, error)

NewMapper returns a mapper of type id for cart.

Each cartridge requires a specific mapper id, which is stated in the iNES file header.

The following mappers are currently implemented: - 0 (NROM) - 1 (MMC1) - 2 (UNROM) - 4 (MMC3)

An error is returned if the requested mapper id is not implemented.

type Mapper0

type Mapper0 struct {
	*Cartridge
	// contains filtered or unexported fields
}

Mapper0 implements the NROM mapper.

http://wiki.nesdev.com/w/index.php/NROM

func (*Mapper0) IRQ

func (m *Mapper0) IRQ() bool

func (*Mapper0) Read

func (m *Mapper0) Read(address uint16, isPPU bool) byte

func (*Mapper0) Write

func (m *Mapper0) Write(address uint16, value byte, isPPU bool)

type Mapper1

type Mapper1 struct {
	*Cartridge
	// contains filtered or unexported fields
}

Mapper1 implements the MMC1 mapper.

http://wiki.nesdev.com/w/index.php/MMC1

func (*Mapper1) IRQ

func (m *Mapper1) IRQ() bool

func (*Mapper1) Read

func (m *Mapper1) Read(address uint16, isPPU bool) byte

func (*Mapper1) Write

func (m *Mapper1) Write(address uint16, value byte, isPPU bool)

type Mapper2

type Mapper2 struct {
	*Cartridge
	// contains filtered or unexported fields
}

Mapper2 implements the UNROM mapper.

http://wiki.nesdev.com/w/index.php/UxROM

func (*Mapper2) IRQ

func (m *Mapper2) IRQ() bool

func (*Mapper2) Read

func (m *Mapper2) Read(address uint16, isPPU bool) byte

func (*Mapper2) Write

func (m *Mapper2) Write(address uint16, value byte, isPPU bool)

type Mapper4

type Mapper4 struct {
	*Cartridge
	// contains filtered or unexported fields
}

Mapper4 implements the MMC3 mapper.

The MMC3 mapper implements PRG/CHG bank switching and scanline counting.

http://wiki.nesdev.com/w/index.php/MMC3

func (*Mapper4) IRQ

func (m *Mapper4) IRQ() bool

func (*Mapper4) Read

func (m *Mapper4) Read(address uint16, isPPU bool) byte

func (*Mapper4) Write

func (m *Mapper4) Write(address uint16, value byte, isPPU bool)

type MirrorType

type MirrorType int

MirrorType determines a cartridge's nametable mirroring arrangement.

type Noise

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

type PPU

type PPU struct {
	Console *Console

	// Scanline (0-261).
	Scanline int

	// Tick (0-340).
	Tick int

	// Frame counter.
	Frame uint64
	// contains filtered or unexported fields
}

PPU implements the NES Picture Processing Unit.

func NewPPU

func NewPPU(console *Console) *PPU

NewPPU constructs and returns a PPU for the given console.

func (*PPU) ReadData

func (p *PPU) ReadData() byte

ReadData reads and returns a byte from PPU RAM. ($2007).

func (*PPU) ReadSPR

func (p *PPU) ReadSPR() byte

ReadSPR reads and returns the byte at the current sprite address.

func (*PPU) SetControlRegister

func (p *PPU) SetControlRegister(value byte)

SetControlRegister sets the value of the control register ($2000).

func (*PPU) SetMaskRegister

func (p *PPU) SetMaskRegister(value byte)

SetMaskRegister sets the value of the mask register ($2001).

func (*PPU) SetSPRAddress

func (p *PPU) SetSPRAddress(address byte)

SetSPRAddress sets the value of the sprite address register ($2003).

func (*PPU) StatusRegister

func (p *PPU) StatusRegister() byte

StatusRegister returns the value of the status register ($2002).

func (*PPU) Step

func (p *PPU) Step() (uint64, *image.RGBA)

Step runs the PPU for one cycle.

Returns the total number of cycles run in the PPU's lifetime. An image is returned when the screen is to be updated.

func (*PPU) String

func (p *PPU) String() string

String returns a description of the PPU as a string.

func (*PPU) WriteData

func (p *PPU) WriteData(value byte)

WriteData writes a byte to PPU RAM ($2007).

func (*PPU) WriteDataAddress

func (p *PPU) WriteDataAddress(value byte)

WriteDataAddress ($2006): Data address write register (two values).

func (*PPU) WriteSPR

func (p *PPU) WriteSPR(value byte)

WriteSPR writes a byte to the sprite memory at the current sprite address. The sprite address is then incremented.

func (*PPU) WriteScroll

func (p *PPU) WriteScroll(value byte)

WriteScroll ($2005): Scroll position write register (two values).

type Pulse

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

type Triangle

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

Jump to

Keyboard shortcuts

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