-
Notifications
You must be signed in to change notification settings - Fork 0
/
memory.go
71 lines (58 loc) · 1.44 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
68
69
70
71
package main
import (
"encoding/hex"
"fmt"
)
var data []byte
var current []byte
// 4096 max size of program memory.
// Both data memory and program memory inherit
// from here.
type Memory [8192]byte
// Use Fetch() for grabbing 2 bytes from program memory.
// Increments the program counter.
func (mem *Memory) Fetch() {
current = mem[cpu.pc:(cpu.pc + 2)]
cpu.pc += 2
}
// Use Read() for reading a single byte from data memory
func (mem *Memory) Read(loc int) byte {
return mem[loc]
}
// Loads the executable stuff into program memory.
func (mem *Memory) LoadProgram(data []byte) {
for i, b := range data {
//fmt.Println(i)
mem[i] = b
}
}
func (mem *Memory) Dump() string {
return hex.Dump(mem[0:])
}
// Here but unused.
func (mem *Memory) Store(i uint16, b byte) {
mem[i] = b
}
func (mem *Memory) printRegs() {
var regs []string
for i := 0; i < 32; i++ {
regs = append(regs, fmt.Sprintf("r%d[%.2x]", i, mem[i]))
}
fmt.Println("Registers:")
fmt.Printf("%v\n", regs[0:13])
fmt.Printf("%v\n", regs[13:26])
fmt.Printf("X:\t%v\t%v\n", regs[26], regs[27])
fmt.Printf("Y:\t%v\t%v\n", regs[28], regs[29])
fmt.Printf("Z:\t%v\t%v\n", regs[30], regs[31])
}
func (mem *Memory) printStack() {
var stack []string
se := b2u16little(stackEnd) + 1
if cpu.sp.current() != 0 && cpu.sp.current() < se {
for _, v := range mem[(cpu.sp.current() + 1):se] {
stack = append(stack, fmt.Sprintf("%.2x ", v))
}
}
fmt.Println("Stack:")
fmt.Println(stack)
}