2021-11-16 16:15:46 -10:00
|
|
|
package vm
|
|
|
|
|
|
|
|
|
|
import "fmt"
|
|
|
|
|
|
2021-11-18 17:53:38 -10:00
|
|
|
type Memory struct {
|
2021-11-16 16:15:46 -10:00
|
|
|
entries []uint64
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 17:53:38 -10:00
|
|
|
func NewMemory(size uint64) *Memory {
|
|
|
|
|
return &Memory{
|
2021-11-16 16:15:46 -10:00
|
|
|
entries: make([]uint64, size),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 17:53:38 -10:00
|
|
|
func (mem *Memory) ReadUnsigned(index uint64) (uint64, error) {
|
2021-11-16 16:15:46 -10:00
|
|
|
if index >= uint64(len(mem.entries)) {
|
|
|
|
|
return 0, fmt.Errorf("Invalid memory index: %016x", index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return mem.entries[index], nil
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 17:53:38 -10:00
|
|
|
func (mem *Memory) MustReadUnsigned(index uint64) uint64 {
|
|
|
|
|
value, err := mem.ReadUnsigned(index)
|
2021-11-16 16:15:46 -10:00
|
|
|
if err != nil {
|
|
|
|
|
panic(err)
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-18 17:53:38 -10:00
|
|
|
func (mem *Memory) WriteUnsigned(index uint64, value uint64) error {
|
2021-11-16 16:15:46 -10:00
|
|
|
if index >= uint64(len(mem.entries)) {
|
|
|
|
|
return fmt.Errorf("Invalid memory index: %016x", index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
mem.entries[index] = value
|
|
|
|
|
|
|
|
|
|
return nil
|
|
|
|
|
}
|