Documentation ¶
Index ¶
- Constants
- Variables
- func CpuStateString(cpu *CPU, label string) string
- func GetPaletteColour(index byte) (uint8, uint8, uint8)
- func LogMemory(gb *Gameboy, start uint16, len uint16)
- func LogOpcode(gb *Gameboy, short bool)
- func NewPalette() *cgbPalette
- func WaitForInput() uint16
- type CPU
- type DebugFlags
- type Gameboy
- func (gb *Gameboy) BGMapString() string
- func (gb *Gameboy) ExecuteNextOpcode() int
- func (gb *Gameboy) ExecuteOpcode(opcode byte)
- func (gb *Gameboy) GetScanlineCounter() int
- func (gb *Gameboy) IsCGB() bool
- func (gb *Gameboy) IsGameLoaded() bool
- func (gb *Gameboy) RequestJoypadInterrupt()
- func (gb *Gameboy) ToggleSoundChannel(channel int)
- func (gb *Gameboy) Update() int
- type GameboyOption
- type Memory
- func (mem *Memory) Init(gameboy *Gameboy)
- func (mem *Memory) LoadCart(loc string) (bool, error)
- func (mem *Memory) Read(address uint16) byte
- func (mem *Memory) ReadHighRam(address uint16) byte
- func (mem *Memory) Write(address uint16, value byte)
- func (mem *Memory) WriteHighRam(address uint16, value byte)
Constants ¶
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 )
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 )
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 ¶
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.
var CurrentPalette = PaletteBGB
CurrentPalette is the global current DMG palette.
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.
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 ¶
CpuStateString is a debug function to get a string of the state of the CPU.
func GetPaletteColour ¶
GetPaletteColour returns the colour based on the colour index and the currently selected palette.
func LogOpcode ¶
LogOpcode is a debug function to log the the current state of the gameboys CPU and next memory.
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.
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 ¶
BGMapString returns a string of the values in the background map.
func (*Gameboy) ExecuteNextOpcode ¶
ExecuteNextOpcode gets the value at the current PC address, increments the PC, updates the CPU ticks and executes the opcode.
func (*Gameboy) ExecuteOpcode ¶
ExecuteOpcode is a large switch statement containing the opcode operations.
func (*Gameboy) GetScanlineCounter ¶
GetScanlineCounter returns the current value of the scanline counter.
func (*Gameboy) IsGameLoaded ¶
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 ¶
ToggleSoundChannel toggles a sound channel for debugging.
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 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) Read ¶
Read from memory. Will go and read from cartridge memory if the requested address is mapped to that space.
func (*Memory) ReadHighRam ¶
ReadHighRam reads from 0xFF00-0xFFFF in the memory address space. The range includes both HRAM and the hardware registers.
func (*Memory) Write ¶
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 ¶
WriteHighRam writes to the range 0xFF00-0xFFFF in the memory address space. The range includes both HRAM and the hardware registers.