nes

package
v0.0.0-...-3aefdd6 Latest Latest
Warning

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

Go to latest
Published: Apr 3, 2022 License: MIT Imports: 16 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

Types

type AddressingMode

type AddressingMode int
const (
	IMP AddressingMode = iota
	IMM
	REL
	ZP0
	ZPX
	ZPY
	ABS
	ABX
	ABY
	IND
	IZX
	IZY
)

type Bus

type Bus struct {
	Cpu             *Cpu6502       // NES CPU.
	Ppu             *Ppu           // Picture processing unit.
	Ram             [8 * 1024]byte // 8KiB RAM.
	Cart            *Cartridge     // NES Cartridge.
	Controller      [2]*Controller // NES Controller.
	ControllerState [2]byte        // 8 bit shifter representing each button's state
	Disp            *Display

	ClockCount int
	// contains filtered or unexported fields
}

Main bus used by the CPU.

func NewBus

func NewBus(isDebug, isLogging bool) *Bus

func (*Bus) CheckForNestestErrors

func (b *Bus) CheckForNestestErrors()

Used for testing the emulator with nestest.

func (*Bus) Clock

func (b *Bus) Clock()

1 NES clock cycle.

func (*Bus) CpuRead

func (b *Bus) CpuRead(addr uint16) byte

Used by the CPU to read data from the main bus at a specified address.

func (*Bus) CpuWrite

func (b *Bus) CpuWrite(addr uint16, data byte)

Used by the CPU to write data to the main bus at a specified address.

func (*Bus) DrawDebugPanel

func (b *Bus) DrawDebugPanel()

TODO: move this out of Bus, and into main or something. Also, rewrite this.

func (*Bus) InsertCartridge

func (b *Bus) InsertCartridge(cart *Cartridge)

Load a cartridge to the NES. The cartridge is connected to both the CPU and PPU.

func (*Bus) Load

func (b *Bus) Load(filepath string)

Load a ROM to the NES.

func (*Bus) LoadBytes

func (b *Bus) LoadBytes(rom []byte)

Load a slice of bytes to the NES.

func (*Bus) Reset

func (b *Bus) Reset()

Reset the NES.

func (*Bus) Run

func (b *Bus) Run()

Run the NES.

type Cartridge

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

NES Cartridge. Connected to both main bus and PPU bus. The cartridges consist of program (PRG) memory and character (CHR) memory.

func NewCartridge

func NewCartridge(filepath string) *Cartridge

Creates a new NES Cartridge using the file at the given path.

type CartridgeHeader

type CartridgeHeader struct {
	Name         [4]byte // Constant "NES" followed by MS-DOS end of file
	PrgRomChunks byte    // Program memory size in 16KB chunks
	ChrRomChunks byte    // Character memory size in 8KB chunks
	Mapper1      byte    // Flags 6
	Mapper2      byte    // Flags 7
	PrgRamSize   byte    // Flags 8
	TvSystem1    byte    // Flags 9
	TvSystem2    byte    // Flags 10
	Unused       [5]byte // Unused padding
}

iNES file header reference: https://wiki.nesdev.com/w/index.php/INES

type Controller

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

func NewController

func NewController() *Controller

func (*Controller) GetState

func (c *Controller) GetState() byte

GetState returns a byte, with each bit representing the state of a button on the controller.

type Cpu6502

type Cpu6502 struct {
	Pc     uint16 // Program Counter
	Sp     byte   // Stack Pointer: low 8 bits of next free location on stack.
	A      byte   // Accumulator Register
	X      byte   // X Register
	Y      byte   // Y Register
	Status byte   // Processor Status Flags

	// Internal variables
	Cycles     byte   // Remaining cycles for current insturction
	Opcode     byte   // Opcode representing next instruction to be executed
	AddrAbs    uint16 // Set by addressing mode functions, used by instructions
	AddrRel    uint16 // Relative displacement address used for branching
	Fetched    byte   // Byte of memory used by CPU instructions
	CycleCount uint32 // Total # of cycles executed by the CPU

	// Used for printing disassembly in debug mode
	Disassembly map[uint16]string

	PrevInstructions [15]string
	PrevInstIdx      int

	InstLookup [16 * 16]Instruction // Instruction operation lookup

	AddrModeFns map[AddressingMode]func() byte // Addressing mode name -> function map

	Logger *log.Logger // CPU logging
	// contains filtered or unexported fields
}

func NewCpu6502

func NewCpu6502(isLogging bool) *Cpu6502

func (*Cpu6502) Clock

func (cpu *Cpu6502) Clock()

Cycle represents one CPU clock cycle.

func (*Cpu6502) ConnectBus

func (cpu *Cpu6502) ConnectBus(b *Bus)

Connect the CPU to a 16-bit address bus.

func (*Cpu6502) Disassemble

func (cpu *Cpu6502) Disassemble(startAddr, endAddr uint16) map[uint16]string

Disassemble the loaded 6502 program into human-readable CPU instructions mapped to their respective memory address.

Much help from https://github.com/OneLoneCoder/olcNES

func (*Cpu6502) IRQ

func (cpu *Cpu6502) IRQ()

Interrupt Request

func (*Cpu6502) NMI

func (cpu *Cpu6502) NMI()

Non-maskable Interrupt Request

func (*Cpu6502) Reset

func (cpu *Cpu6502) Reset()

type Display

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

func NewDisplay

func NewDisplay(isDebug bool) *Display

func (*Display) DrawDebugPixel

func (d *Display) DrawDebugPixel(x, y int, c color.RGBA)

func (*Display) DrawDebugRGBA

func (d *Display) DrawDebugRGBA(x, y int, img *image.RGBA)

DrawDebugRGBA draws a given image to an (x, y) offset within the debug image.

func (*Display) DrawPixel

func (d *Display) DrawPixel(x, y int, c color.RGBA)

func (*Display) UpdateScreen

func (d *Display) UpdateScreen()

UpdateScreen updates both the game display and the debug display using the display's current image.RGBA representation of each.

func (*Display) WriteControllerDebugString

func (d *Display) WriteControllerDebugString(t string)

Write a string of text to the controller status section of the debug panel.

func (*Display) WriteInstDebugString

func (d *Display) WriteInstDebugString(t string)

Write a string of text to the isntruction disassembly section of the debug panel.

func (*Display) WriteRegDebugString

func (d *Display) WriteRegDebugString(t string)

Write a string of text to the CPU register section of the debug panel.

type Instruction

type Instruction struct {
	Name     string
	Execute  func() byte
	AddrMode AddressingMode
	Cycles   byte
}

////////////////////////////////////////////////////////////// Instructions

type Mapper

type Mapper interface {
	// contains filtered or unexported methods
}

Mapper functions return whether or not the given address was successfully mapped.

type Mapper000

type Mapper000 struct {
	PrgBanks byte
	ChrBanks byte
}

func NewMapper000

func NewMapper000(prgRomChunks, chrRomChunks byte) Mapper000

type MirrorMode

type MirrorMode int

type Ppu

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

References: http://wiki.nesdev.com/w/index.php/PPU_registers https://www.youtube.com/watch?v=xdzOvpYPmGE (javidx9)

func NewPpu

func NewPpu() *Ppu

func (*Ppu) Clock

func (p *Ppu) Clock()

PPU clock cycle. 1 frame = 262 scanlines (-1 - 260) 1 scanline = 341 PPU clock cycles (0 - 340)

func (*Ppu) ConnectCartridge

func (p *Ppu) ConnectCartridge(c *Cartridge)

func (*Ppu) ConnectDisplay

func (p *Ppu) ConnectDisplay(d *Display)

func (*Ppu) GetPatternTable

func (p *Ppu) GetPatternTable(i int) *image.RGBA

Pattern tables are 16x16 grids of tiles or sprites. Each tile is 8x8 pixels and 16 bytes of memory.

type PpuLoopyReg

type PpuLoopyReg uint16

Loopy registers are 15 bit internal PPU registers used for implementing scrolling. Loopy register layout:

yyy NN YYYYY XXXXX

yyy   - fine Y scroll
NN    - nametable select
YYYYY - coarse Y scroll
XXXXX - coarse X scroll

type PpuReg

type PpuReg byte

PPU Registers

type PpuRegFlag

type PpuRegFlag byte

type SF6502

type SF6502 byte // 6502 Status Flag

////////////////////////////////////////////////////////////// Status Flags

const (
	StatusFlagC SF6502 = 1 << iota // Carry
	StatusFlagZ                    // Zero
	StatusFlagI                    // Interrupt Disable
	StatusFlagD                    // Decimal Mode (not used on NES)
	StatusFlagB                    // Break Command
	StatusFlagX                    // UNUSED
	StatusFlagV                    // Overflow
	StatusFlagN                    // Negative
)

Jump to

Keyboard shortcuts

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