-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
67 lines (61 loc) · 1.8 KB
/
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
58
59
60
61
62
63
64
65
66
67
package main
const (
dramBase = 0x80000000
dtbSize = 0xfe0
)
type Memory struct {
// 3GiB
Mem [3 * 1024 * 1024 * 1024]uint8
}
func NewMemory() *Memory {
return &Memory{Mem: [3 * 1024 * 1024 * 1024]uint8{}}
}
func (mem *Memory) Read(addr uint64, size int) uint64 {
index := addr - dramBase
switch size {
case byt:
return uint64(mem.Mem[index])
case halfword:
return uint64(mem.Mem[index]) | uint64(mem.Mem[index+1])<<8
case word:
return uint64(mem.Mem[index]) |
uint64(mem.Mem[index+1])<<8 |
uint64(mem.Mem[index+2])<<16 |
uint64(mem.Mem[index+3])<<24
case doubleword:
return uint64(mem.Mem[index]) |
uint64(mem.Mem[index+1])<<8 |
uint64(mem.Mem[index+2])<<16 |
uint64(mem.Mem[index+3])<<24 |
uint64(mem.Mem[index+4])<<32 |
uint64(mem.Mem[index+5])<<40 |
uint64(mem.Mem[index+6])<<48 |
uint64(mem.Mem[index+7])<<56
}
// TODO: Should throw LoadAccessFault exception
return 0
}
func (mem *Memory) Write(addr, val uint64, size int) {
index := addr - dramBase
switch size {
case byt:
mem.Mem[index] = uint8(val)
case halfword:
mem.Mem[index] = uint8(val & 0b1111_1111)
mem.Mem[index+1] = uint8((val >> 8) & 0b1111_1111)
case word:
mem.Mem[index] = uint8(val & 0b1111_1111)
mem.Mem[index+1] = uint8((val >> 8) & 0b1111_1111)
mem.Mem[index+2] = uint8((val >> 16) & 0b1111_1111)
mem.Mem[index+3] = uint8((val >> 24) & 0b1111_1111)
case doubleword:
mem.Mem[index] = uint8(val & 0b1111_1111)
mem.Mem[index+1] = uint8((val >> 8) & 0b1111_1111)
mem.Mem[index+2] = uint8((val >> 16) & 0b1111_1111)
mem.Mem[index+3] = uint8((val >> 24) & 0b1111_1111)
mem.Mem[index+4] = uint8((val >> 32) & 0b1111_1111)
mem.Mem[index+5] = uint8((val >> 40) & 0b1111_1111)
mem.Mem[index+6] = uint8((val >> 48) & 0b1111_1111)
mem.Mem[index+7] = uint8((val >> 56) & 0b1111_1111)
}
}