memory

package
v0.0.0-...-657eaca Latest Latest
Warning

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

Go to latest
Published: Oct 4, 2023 License: MIT Imports: 7 Imported by: 0

Documentation

Index

Constants

View Source
const (
	RAMBankSize = 0x2000
	ROMBankSize = 0x4000
)
View Source
const (
	ROMBanking = 0x00
	RAMBanking = 0x01
)

6000-7FFF - ROM/RAM Mode Select

View Source
const AddrDMA = 0xff46

AddrDMA is the address of DMA register in DMG address space.

View Source
const AddrExternalRAM = 0xa000
View Source
const BootAddr = 0xff50

BootAddr is the address of BOOT register in I/O RAM.

Variables

This section is empty.

Functions

This section is empty.

Types

type Addressable

type Addressable interface {
	// Contains returns true if the given address belongs to the address space.
	Contains(addr uint16) bool
	// Read returns the value stored at the given address.
	Read(addr uint16) uint8
	// Write attempts to store the given value at the given address if writable.
	Write(addr uint16, value uint8)
}

Addressable interface provides functions to read/write bytes in a given 16-bit address space.

func NewCartridge

func NewCartridge(romPath, savePath string) (cart Addressable)

NewCartridge instantiates the proper kind of adress space depending on the given ROM's header. TODO: we only handle ROM-only and MBC1 so far.

type BankedRAM

type BankedRAM struct {
	*RAM

	Bank uint // 8Kb bank to map to 0xa000-0xbfff.
}

RAM as an array of R/W bytes at addresses starting from a given offset. If dealing with a battery-backed RAM, this can also be saved to a file.

func NewBankedRAM

func NewBankedRAM(start, size uint16) *BankedRAM

NewBankedRAM instantiates a zeroed slice of the given size to represent RAM along with a bank index to map an 8Kb bank to the 0xa000-0xbfff range.

func (*BankedRAM) Read

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

Read returns the value stored at the given address in RAM, handling offsets and bank switching.

func (*BankedRAM) Write

func (r *BankedRAM) Write(addr uint16, value uint8)

Write sets stores the given value at the given address in RAM, handling offsets and bank switching.

type BankedROM

type BankedROM struct {
	*ROM

	Bank0 uint // 16Kb bank to map to 0x0000-0x3fff.
	Bank1 uint // 16Kb bank to map to 0x4000-0x7fff
}

ROM as an array of R/W bytes at addresses starting from a given offset. If dealing with a battery-backed ROM, this can also be saved to a file.

func NewBankedROM

func NewBankedROM(data []byte) *BankedROM

NewBankedROM instantiates a zeroed slice of the given size to represent ROM along with bank indexes to map 16Kb bank to the 0x0000-0x3fff and 0x4000-0x7fff ranges.

func (*BankedROM) Read

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

Read returns the value stored at the given address in ROM, handling offsets and bank switching.

func (*BankedROM) Write

func (r *BankedROM) Write(addr uint16, value uint8)

Write sets stores the given value at the given address in ROM, handling offsets and bank switching.

type Boot

type Boot struct {
	Register uint8
	ROM      ROM
	// contains filtered or unexported fields
}

Boot address space holding Boot ROM and BOOT register to disable it.

func NewBoot

func NewBoot(data []byte) *Boot

NewBoot returns a new Boot address space containing the given boot ROM.

func NewBootFromFile

func NewBootFromFile(filename string) *Boot

NewBootFromFile returns a new Boot address space containing the boot ROM in the given file.

func (*Boot) Contains

func (b *Boot) Contains(addr uint16) bool

Contains returns true if the given address belongs to the ROM or BOOT register, false otherwise.

func (*Boot) Read

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

Read returns the value stored at the given address in ROM or BOOT register.

func (*Boot) Write

func (b *Boot) Write(addr uint16, value uint8)

Write is only supported for BOOT register and disables the boot ROM.

type DMA

type DMA struct {
	DMA uint8
	MMU Addressable
	OAM Addressable
	// contains filtered or unexported fields
}

DMA address space taking care of memory transfers between main MMU and OAM memory.

func NewDMA

func NewDMA(mmu, oam Addressable) *DMA

NewDMA returns an instance of DMA managing the actual register and memory transfers. Parameter is an Addressable that must span source and destination address spaces.

func (*DMA) Contains

func (d *DMA) Contains(addr uint16) bool

Contains return true if the requested address is the DMA register.

func (*DMA) Read

func (d *DMA) Read(addr uint16) uint8

Read returns the content of the DMA register.

func (*DMA) Tick

func (d *DMA) Tick()

Tick advances DMA transfer one step if it's active. Called every 4 CPU ticks.

func (*DMA) Write

func (d *DMA) Write(addr uint16, value uint8)

Write sets the start address for memory transfer in the DMA register and initiates said transfer.

type DMAMemory

type DMAMemory struct {
	Addressable
	DMA *DMA
}

DMAMemory wraps the whole address space to forbid memory access to the CPU while a DMA transfer is taking place.

func NewDMAMemory

func NewDMAMemory(mmu, oam Addressable) *DMAMemory

NewDMA returns an instance of DMA managing the actual register and memory transfers. Parameter is an Addressable that must span source and destination address spaces.

func (*DMAMemory) Read

func (d *DMAMemory) Read(addr uint16) uint8

Read overrides the embedded Addressable method to only allow reading from high RAM if a DMA transfer is currently taking place.

func (*DMAMemory) Write

func (d *DMAMemory) Write(addr uint16, value uint8)

Write overrides the embedded Addressable method to only allow writing to high RAM if a DMA transfer is currently taking place.

type MBC1

type MBC1 struct {
	ROM *BankedROM // Complete ROM (will be addressed according to ROMBank)
	RAM *BankedRAM // Optional RAM (up to 32KB)

	RAMEnabled bool // 0000-1FFF - RAM Enable

	BankLow  uint8 // 2000-3FFF - ROM Bank Number (bits 0-4)
	BankHigh uint8 // 4000-5FFF - RAM Bank Number / ROM Bank Number (bits 5-6)

	// 6000-7FFF - ROM/RAM Mode Select
	// 00h = ROM Banking Mode (up to 8KByte RAM, 2MByte ROM) (default)
	// 01h = RAM Banking Mode (up to 32KByte RAM, 512KByte ROM)
	BankingMode uint8
	// contains filtered or unexported fields
}

MBC1 (max 2MByte ROM and/or 32KByte RAM)

func NewMBC1

func NewMBC1(rom *ROM, romBanks uint8, ramBanks uint8, battery bool, savePath string) *MBC1

NewMBC1 creates an address space emulating a cartridge with an MBC1 chip. Takes an instance of ROM because to know which kind of chip it uses, we had to read it beforehand anyway.

func (*MBC1) Contains

func (m *MBC1) Contains(addr uint16) bool

Contains returns true if the requested address is anywhere in ROM or RAM.

func (*MBC1) Read

func (m *MBC1) Read(addr uint16) uint8

Read returns the byte at requested address in current ROM or RAM bank.

func (*MBC1) Write

func (m *MBC1) Write(addr uint16, value uint8)

Write value to RAM, enable RAM or select ROM/RAM banks.

type MMU

type MMU struct {
	Spaces []Addressable
}

MMU manages an arbitrary number of ordered address spaces. It also satisfies the Addressable interface.

func NewEmptyMMU

func NewEmptyMMU() *MMU

NewEmptyMMU returns an instance of MMU with no address space.

func NewMMU

func NewMMU(spaces []Addressable) *MMU

NewMMU returns an instance of MMU initialized with existing address spaces.

func (*MMU) Add

func (m *MMU) Add(space Addressable)

Add an address space at the end of this MMU's list.

func (*MMU) Contains

func (m *MMU) Contains(addr uint16) bool

Contains returns whether one of the address spaces known to the MMU contains the given address. The first address space in the internal list containing a given address will shadow any other that may contain it.

func (*MMU) Read

func (m *MMU) Read(addr uint16) uint8

Read finds the first address space compatible with the given address and returns the value at that address. If no space contains the requested address, it returns 0xff (emulates black bar on boot).

func (*MMU) Write

func (m *MMU) Write(addr uint16, value uint8)

Write finds the first address space compatible with the given address and attempts writing the given value to that address.

type RAM

type RAM struct {
	Bytes []uint8
	Start uint16
	// contains filtered or unexported fields
}

RAM as an array of R/W bytes at addresses starting from a given offset. If dealing with a battery-backed RAM, this can also be saved to a file.

func NewRAM

func NewRAM(start, size uint16) *RAM

NewRAM instantiates a zeroed slice of the given size to represent RAM.

func (*RAM) Contains

func (r *RAM) Contains(addr uint16) bool

Contains indicates true as long as address fits in the slice. Careful not to wrap uint16 here.

func (*RAM) Load

func (r *RAM) Load(filename string) error

Load sets the current content of RAM from the given file, and stores the path to that file for subsequent saves.

func (*RAM) Read

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

Read returns the value stored at the given address in RAM, handling offsets.

func (*RAM) Save

func (r *RAM) Save() error

Save dumps the current content of RAM into the associated save file (if any).

func (*RAM) Write

func (r *RAM) Write(addr uint16, value uint8)

Write sets stores the given value at the given address in RAM, handling offsets.

type ROM

type ROM struct {
	RAM
}

ROM is a read-only special case of RAM, initialized from a binary file.

func NewROM

func NewROM(data []byte) *ROM

func NewROMFromFile

func NewROMFromFile(filename string) *ROM

NewROMFromFile instantiates a read-only chunk of memory from a binary dump. Try to support ZIP files (room for improvement there).

func (*ROM) Write

func (r *ROM) Write(addr uint16, value uint8)

Write does nothing and displays an error, for reasons I hope are obvious.

type Registers

type Registers map[uint16]*uint8

Registers represented as an address space.

func (Registers) Contains

func (r Registers) Contains(addr uint16) bool

Contains returns true if the address corresponds to a register.

func (Registers) Read

func (r Registers) Read(addr uint16) uint8

Read returns the byte at the given address in VRAM or from register.

func (Registers) Write

func (r Registers) Write(addr uint16, value uint8)

Write sets the byte at the given address in VRAM to the given value. TODO: checks

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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