-
Notifications
You must be signed in to change notification settings - Fork 0
/
machine.go
221 lines (189 loc) · 3.17 KB
/
machine.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package vm
import (
"fmt"
"log"
"os"
)
var Log *log.Logger = log.New(os.Stdout, "", 0)
const MEM_SIZE int = 1024
const (
OP_NOP byte = iota
OP_HALT byte = 0xFF
)
const (
OP_ADD byte = 0xA0 + iota
OP_SUB
)
const (
OP_STORED byte = 0xC0 + iota
OP_STOREI
OP_LOADI
OP_MOVD
OP_MOVI
OP_PRINT
)
const (
OP_JUMP byte = 0xE0 + iota
OP_JZ
OP_JNZ
)
type Word uint32
type Machine struct {
stopped bool
mem *Memory
alu *ALU
//Registers
ip uint
currentInstruction Word
reg0 byte
reg1 byte
}
func New() *Machine {
return &Machine{
mem: NewMemory(MEM_SIZE),
alu: &ALU{},
}
}
func (m *Machine) LoadProgram(prog []Word) error {
return m.mem.Set(prog)
}
func (m *Machine) halt() {
Log.Printf("execution halted at %x\n", m.ip)
m.stopped = true
}
func (m *Machine) start() {
Log.Printf("execution started at %x\n", m.ip)
m.stopped = false
}
func (m *Machine) Run() {
m.start()
var err error
for {
if m.stopped {
return
}
err = m.fetch()
if err != nil {
Log.Println(err)
break
}
err = m.exec()
if err != nil {
Log.Println(err)
break
}
m.ip++
}
m.halt()
}
func (m *Machine) fetch() error {
return m.load(m.ip, &m.currentInstruction)
}
func (m *Machine) load(addr uint, reg *Word) error {
v, err := m.mem.LoadW(addr)
if err != nil {
return err
}
*reg = v
return nil
}
func (m *Machine) exec() error {
//0x{OpCode}{b2}{b3}{b4}
opCode := byte(m.currentInstruction >> 24)
b2 := byte(m.currentInstruction >> 16)
b3 := byte(m.currentInstruction >> 8)
b4 := byte(m.currentInstruction)
switch opCode {
default:
return fmt.Errorf("unknown instruction '%x'", m.currentInstruction)
case OP_NOP:
//do literally nothing
case OP_HALT: //halt
m.halt()
case OP_PRINT:
r, err := m.findReg(b2)
if err != nil {
return err
}
fmt.Printf("0x%x = %d\n", *r, *r)
case OP_ADD:
r1, err := m.findReg(b2)
if err != nil {
return err
}
r2, err := m.findReg(b3)
if err != nil {
return err
}
m.alu.Add(*r1, *r2)
case OP_SUB:
r1, err := m.findReg(b2)
if err != nil {
return err
}
r2, err := m.findReg(b3)
if err != nil {
return err
}
m.alu.Sub(*r1, *r2)
case OP_JUMP:
m.jump(Word(b4))
case OP_JZ:
if m.alu.Flags.ZF {
m.jump(Word(b4))
}
case OP_JNZ:
if !m.alu.Flags.ZF {
m.jump(Word(b4))
}
case OP_MOVD:
r, err := m.findReg(b3)
if err != nil {
return err
}
*r = b2
case OP_MOVI:
r1, err := m.findReg(b2)
if err != nil {
return err
}
r2, err := m.findReg(b3)
if err != nil {
return err
}
*r2 = *r1
case OP_STORED:
m.mem.StoreW(uint(b3), Word(b2))
case OP_STOREI:
r, err := m.findReg(b2)
if err != nil {
return err
}
m.mem.StoreW(uint(b3), Word(*r))
case OP_LOADI:
r, err := m.findReg(b2)
if err != nil {
return err
}
data, err := m.mem.LoadW(uint(b3))
if err != nil {
return err
}
*r = byte(data)
}
return nil
}
func (m *Machine) jump(addr Word) {
m.ip = uint(addr) - 1
}
func (m *Machine) findReg(regCode byte) (*byte, error) {
switch regCode {
case 0xA:
return &m.alu.Acc, nil
case 0:
return &m.reg0, nil
case 1:
return &m.reg1, nil
}
return nil, fmt.Errorf("unknown reg '%x'", regCode)
}