-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
57 lines (45 loc) · 925 Bytes
/
memory.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package vm
import (
"errors"
"fmt"
)
type Memory struct {
mem []Word
}
func NewMemory(size int) *Memory {
if size%4 != 0 {
size += 4 - size%4
}
return &Memory{
mem: make([]Word, size),
}
}
func (m *Memory) addrOK(addr uint) bool {
return int(addr) >= len(m.mem)
}
func (m *Memory) Set(data []Word) error {
if len(data) > len(m.mem) {
return fmt.Errorf("data size exceeds memory size")
}
nb := copy(m.mem, data)
Log.Printf("loaded %d words (%d bytes) in memory\n", nb, nb*4)
return nil
}
func (m *Memory) Dump() []Word {
var snapshot []Word
copy(snapshot, m.mem)
return snapshot
}
func (m *Memory) LoadW(addr uint) (Word, error) {
if m.addrOK(addr) {
return 0, errors.New("load: addr is out of range")
}
return m.mem[addr], nil
}
func (m *Memory) StoreW(addr uint, v Word) error {
if m.addrOK(addr) {
return errors.New("store: addr is out of range")
}
m.mem[addr] = v
return nil
}