gb

package
v0.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 26, 2018 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ClockSpeed is the number of cycles the GameBoy CPU performs each second.
	ClockSpeed = 4194304
	// FramesSecond is the target number of frames for each frame of GameBoy output.
	FramesSecond = 60
	// CyclesFrame is the number of CPU cycles in each frame.
	CyclesFrame = ClockSpeed / FramesSecond
)
View Source
const (
	// DIV is the divider register which is incremented periodically by
	// the Gameboy.
	DIV = 0xFF04
	// TIMA is the timer counter register which is incremented by a clock
	// frequency specified in the TAC register.
	TIMA = 0xFF05
	// TMA is the timer modulo register. When the TIMA overflows, this data
	// will be loaded into the TIMA register.
	TMA = 0xFF06
	// TAC is the timer control register. Writing to this register will
	// start and stop the timer, and select the clock speed for the timer.
	TAC = 0xFF07
)
View Source
const (
	// PaletteGreyscale is the default greyscale gameboy colour palette.
	PaletteGreyscale = byte(iota)
	// PaletteOriginal is more authentic looking green tinted gameboy
	// colour palette  as it would have been on the GameBoy
	PaletteOriginal
	// PaletteBGB used by default in the BGB emulator.
	PaletteBGB
)

Variables

View Source
var CBOpcodeCycles = []int{} //0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

/* 256 elements not displayed */

CBOpcodeCycles is the number of cpu cycles for each CB opcode.

View Source
var CurrentPalette = PaletteBGB

CurrentPalette is the global current DMG palette.

View Source
var OpcodeCycles = []int{} //0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f

/* 256 elements not displayed */

OpcodeCycles is the number of cpu cycles for each normal opcode.

View Source
var Palettes = [][][]byte{

	{
		{0xFF, 0xFF, 0xFF},
		{0xCC, 0xCC, 0xCC},
		{0x77, 0x77, 0x77},
		{0x00, 0x00, 0x00},
	},

	{
		{0x9B, 0xBC, 0x0F},
		{0x8B, 0xAC, 0x0F},
		{0x30, 0x62, 0x30},
		{0x0F, 0x38, 0x0F},
	},

	{
		{0xE0, 0xF8, 0xD0},
		{0x88, 0xC0, 0x70},
		{0x34, 0x68, 0x56},
		{0x08, 0x18, 0x20},
	},
}

Palettes is an mapping from colour palettes to their colour values to be used by the emulator.

Functions

func CpuStateString

func CpuStateString(cpu *CPU, label string) string

CpuStateString is a debug function to get a string of the state of the CPU.

func GetPaletteColour

func GetPaletteColour(index byte) (uint8, uint8, uint8)

GetPaletteColour returns the colour based on the colour index and the currently selected palette.

func LogMemory

func LogMemory(gb *Gameboy, start uint16, len uint16)

LogMemory is a debug function to log some arbitrary memory.

func LogOpcode

func LogOpcode(gb *Gameboy, short bool)

LogOpcode is a debug function to log the the current state of the gameboys CPU and next memory.

func NewPalette

func NewPalette() *cgbPalette

NewPalette makes a new CGB colour palette.

func WaitForInput

func WaitForInput() uint16

WaitForInput is a debug function which blocks and waits for some input before continuing.

Types

type CPU

type CPU struct {
	AF register
	BC register
	DE register
	HL register

	PC uint16
	SP register

	Divider int
}

CPU contains the registers used for program execution and provides methods for setting flags.

func (*CPU) C

func (cpu *CPU) C() bool

C gets the value of the C flag.

func (*CPU) H

func (cpu *CPU) H() bool

H gets the value of the H flag.

func (*CPU) Init

func (cpu *CPU) Init(cgb bool)

Init CPU and its registers to the initial values.

func (*CPU) N

func (cpu *CPU) N() bool

N gets the value of the N flag.

func (*CPU) SetC

func (cpu *CPU) SetC(on bool)

SetC sets the value of the C flag.

func (*CPU) SetH

func (cpu *CPU) SetH(on bool)

SetH sets the value of the H flag.

func (*CPU) SetN

func (cpu *CPU) SetN(on bool)

SetN sets the value of the N flag.

func (*CPU) SetZ

func (cpu *CPU) SetZ(on bool)

SetZ sets the value of the Z flag.

func (*CPU) Z

func (cpu *CPU) Z() bool

Z gets the value of the Z flag.

type DebugFlags

type DebugFlags struct {
	// HideSprites turns off rendering of sprites to the display.
	HideSprites bool
	// HideBackground turns off rendering of background tiles to the display.
	HideBackground bool
	// OutputOpcodes will log the current opcode to the console on each tick.
	// This will slow down execution massively so is only used for debugging
	// issues with the emulation.
	OutputOpcodes bool
}

DebugFlags are flags which can be set to alter the execution of the Gameboy.

type Gameboy

type Gameboy struct {
	Memory *Memory
	CPU    *CPU
	Sound  *apu.APU

	Debug           DebugFlags
	ExecutionPaused bool

	// PreparedData is a matrix of screen pixel data for a single frame which has
	// been fully rendered.
	PreparedData [160][144][3]uint8

	InputMask byte

	BGPalette     *cgbPalette
	SpritePalette *cgbPalette
	// contains filtered or unexported fields
}

Gameboy is the master struct which contains all of the sub components for running the Gameboy emulator.

func NewGameboy

func NewGameboy(romFile string, opts ...GameboyOption) (*Gameboy, error)

NewGameboy returns a new Gameboy instance.

func (*Gameboy) BGMapString

func (gb *Gameboy) BGMapString() string

BGMapString returns a string of the values in the background map.

func (*Gameboy) ExecuteNextOpcode

func (gb *Gameboy) ExecuteNextOpcode() int

ExecuteNextOpcode gets the value at the current PC address, increments the PC, updates the CPU ticks and executes the opcode.

func (*Gameboy) ExecuteOpcode

func (gb *Gameboy) ExecuteOpcode(opcode byte)

ExecuteOpcode is a large switch statement containing the opcode operations.

func (*Gameboy) GetScanlineCounter

func (gb *Gameboy) GetScanlineCounter() int

GetScanlineCounter returns the current value of the scanline counter.

func (*Gameboy) IsCGB

func (gb *Gameboy) IsCGB() bool

IsCGB returns if we are using CGB features

func (*Gameboy) IsGameLoaded

func (gb *Gameboy) IsGameLoaded() bool

IsGameLoaded returns if there is a game loaded in the gameboy or not.

func (*Gameboy) RequestJoypadInterrupt

func (gb *Gameboy) RequestJoypadInterrupt()

RequestJoypadInterrupt triggers a joypad interrupt. To be called by the io binding implementation.

func (*Gameboy) ToggleSoundChannel

func (gb *Gameboy) ToggleSoundChannel(channel int)

ToggleSoundChannel toggles a sound channel for debugging.

func (*Gameboy) Update

func (gb *Gameboy) Update() int

Update update the state of the gameboy by a single frame.

type GameboyOption

type GameboyOption func(o *gameboyOptions)

GameboyOption is an option for the Gameboy execution.

func WithCGBEnabled

func WithCGBEnabled() GameboyOption

WithCGBEnabled runs the Gameboy with cgb mode enabled.

func WithSound

func WithSound() GameboyOption

WithSound runs the Gameboy with sound output.

func WithTransferFunction

func WithTransferFunction(transfer func(byte)) GameboyOption

WithTransferFunction provides a function to callback on when the serial transfer address is written to.

type Memory

type Memory struct {
	Cart    *cart.Cart
	HighRAM [0x100]byte
	// VRAM bank 1-2 data
	VRAM [0x4000]byte
	// Index of the current VRAM bank
	VRAMBank byte

	// WRAM bank 0-7 data
	WRAM [0x9000]byte
	// Index of the current WRAM bank
	WRAMBank byte

	OAM [0x100]byte
	// contains filtered or unexported fields
}

Memory stores gameboy ROM, RAM and cartridge data. It manages the banking of these data banks.

func (*Memory) Init

func (mem *Memory) Init(gameboy *Gameboy)

Init the gb memory to the post-boot values.

func (*Memory) LoadCart

func (mem *Memory) LoadCart(loc string) (bool, error)

LoadCart load a cart rom into memory.

func (*Memory) Read

func (mem *Memory) Read(address uint16) byte

Read from memory. Will go and read from cartridge memory if the requested address is mapped to that space.

func (*Memory) ReadHighRam

func (mem *Memory) ReadHighRam(address uint16) byte

ReadHighRam reads from 0xFF00-0xFFFF in the memory address space. The range includes both HRAM and the hardware registers.

func (*Memory) Write

func (mem *Memory) Write(address uint16, value byte)

Write a value at an address to the relevant location based on the current state of the gameboy. This handles banking and side effects of writing to certain addresses.

func (*Memory) WriteHighRam

func (mem *Memory) WriteHighRam(address uint16, value byte)

WriteHighRam writes to the range 0xFF00-0xFFFF in the memory address space. The range includes both HRAM and the hardware registers.

Jump to

Keyboard shortcuts

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