package ram import ( "errors" "fmt" "git.elyanpoujol.fr/elyan/central-arch/pkg/events" "git.elyanpoujol.fr/elyan/central-arch/pkg/simcontext" "git.elyanpoujol.fr/elyan/central-arch/pkg/simstate" "git.elyanpoujol.fr/elyan/central-arch/pkg/simstep" ) type RAM struct { state simstate.RamState stepper simstep.SimStepper eventLogger events.EventLogger } const alignment = 4 func New(context simcontext.SimContext, bytes uint) (*RAM, error) { if bytes < alignment { return nil, errors.New("memory size should be at least 4 bytes") } return &RAM{ state: simstate.RamState{Data: make([]uint32, bytes/alignment)}, stepper: context.Stepper, eventLogger: context.EventLogger, }, nil } func (r *RAM) Read(addr uint32) (uint32, error) { if addr%alignment != 0 { return 0, fmt.Errorf("unaligned memory address: %08x", addr) } return r.state.Data[addr/alignment], nil } func (r *RAM) Write(addr uint32, value uint32) error { if addr%alignment != 0 { return fmt.Errorf("unaligned memory address: %08x", addr) } r.state.Data[addr/alignment] = value return nil } func (m *RAM) Fetch(addr uint32) (uint32, error) { return m.Read(addr) }