cache

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// GetState returns the current state of the cache line at the given address.
	// Returns StateInvalid if the line is not present.
	GetState(addr uint64) State

	// SetState updates the state of the cache line at the given address.
	SetState(addr uint64, state State)

	// GetData retrieves the data stored in the cache line at the given address.
	// Returns nil if the line is invalid or not present.
	GetData(addr uint64) []byte

	// SetData updates the data stored in the cache line at the given address.
	// This may also implicitly set the state to Modified if the line exists.
	SetData(addr uint64, data []byte)

	// Invalidate marks the cache line at the given address as invalid.
	Invalidate(addr uint64)

	// IsPresent checks if a cache line exists for the given address (regardless of state).
	IsPresent(addr uint64) bool

	// SetEvictCallback sets the callback function to be called when a cache line is evicted.
	SetEvictCallback(callback EvictCallback)

	// HandleSnoop handles a snoop request from another cache/directory.
	// Protocol-agnostic: opcode interpretation depends on implementation.
	// Parameters:
	//   - snoopOpcode: Protocol-specific snoop opcode
	//   - addr: Target address
	// Returns:
	//   - *SnoopResponse: Response including data if needed
	//   - error: Any error encountered
	HandleSnoop(snoopOpcode int, addr uint64) (*SnoopResponse, error)

	// CanForward checks if this cache can forward data for the given address.
	// Used in protocols that support direct cache-to-cache transfer (DMT).
	// Returns true if the cache has the data in a state that allows forwarding.
	CanForward(addr uint64) bool
}

Cache defines the interface for cache storage and state management. This interface is protocol-agnostic and focuses purely on state/data storage.

type CacheLine

type CacheLine struct {
	Addr  uint64
	State State
	Data  []byte
}

CacheLine represents a single cache line entry.

type EvictCallback

type EvictCallback func(addr uint64, state State, data []byte)

EvictCallback is called when a cache line is evicted. Parameters: addr (address of evicted line), state (state before eviction), data (data before eviction)

type FullyAssociativeCache

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

FullyAssociativeCache implements a simple fully-associative cache with random replacement.

func NewFullyAssociativeCache

func NewFullyAssociativeCache(capacity int) *FullyAssociativeCache

NewFullyAssociativeCache creates a new fully-associative cache with the specified capacity.

func (*FullyAssociativeCache) CanForward

func (c *FullyAssociativeCache) CanForward(addr uint64) bool

CanForward implements Cache.CanForward

func (*FullyAssociativeCache) GetCapacity

func (c *FullyAssociativeCache) GetCapacity() int

GetCapacity returns the capacity of the cache.

func (*FullyAssociativeCache) GetData

func (c *FullyAssociativeCache) GetData(addr uint64) []byte

GetData retrieves the data stored in the cache line at the given address.

func (*FullyAssociativeCache) GetSize

func (c *FullyAssociativeCache) GetSize() int

GetSize returns the current number of cache lines.

func (*FullyAssociativeCache) GetState

func (c *FullyAssociativeCache) GetState(addr uint64) State

GetState returns the current state of the cache line at the given address.

func (*FullyAssociativeCache) HandleSnoop

func (c *FullyAssociativeCache) HandleSnoop(snoopOpcode int, addr uint64) (*SnoopResponse, error)

HandleSnoop implements Cache.HandleSnoop

func (*FullyAssociativeCache) Invalidate

func (c *FullyAssociativeCache) Invalidate(addr uint64)

Invalidate marks the cache line at the given address as invalid.

func (*FullyAssociativeCache) IsPresent

func (c *FullyAssociativeCache) IsPresent(addr uint64) bool

IsPresent checks if a cache line exists for the given address (regardless of state).

func (*FullyAssociativeCache) SetData

func (c *FullyAssociativeCache) SetData(addr uint64, data []byte)

SetData updates the data stored in the cache line at the given address.

func (*FullyAssociativeCache) SetEvictCallback

func (c *FullyAssociativeCache) SetEvictCallback(callback EvictCallback)

SetEvictCallback sets the callback function to be called when a cache line is evicted.

func (*FullyAssociativeCache) SetState

func (c *FullyAssociativeCache) SetState(addr uint64, state State)

SetState updates the state of the cache line at the given address.

type SnoopResponse

type SnoopResponse struct {
	ResponseOpcode int    // Protocol-specific response opcode
	Data           []byte // Data if forwarding
	HasData        bool   // Whether data is included
}

SnoopResponse represents the response to a snoop request. Used by coherence protocols (e.g., CHI, MESI, MOESI).

type State

type State string

State represents the state of a cache line. States are generic and protocol-agnostic.

const (
	StateInvalid   State = "Invalid"   // Cache line is invalid
	StateShared    State = "Shared"    // Cache line is shared (read-only)
	StateExclusive State = "Exclusive" // Cache line is exclusive (read-write, not modified)
	StateModified  State = "Modified"  // Cache line is modified (dirty)
	StateOwned     State = "Owned"     // Cache line is owned (MOESI protocol)
)

Jump to

Keyboard shortcuts

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