/
move.go
170 lines (155 loc) · 4.42 KB
/
move.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
// move related operations
package iris16
import "fmt"
const (
// Move Operations
MoveOpMove = iota
MoveOpSwap
MoveOpSet
MoveOpLoad
MoveOpStore
MoveOpPush
MoveOpPop
MoveOpPeek
MoveOpLoadCode
MoveOpStoreCode
// always last
MoveOpCount
)
type MoveOp func(*Core, *DecodedInstruction) error
func (fn MoveOp) Invoke(core *Core, inst *DecodedInstruction) error {
return fn(core, inst)
}
var unimplementedMoveOp = func(_ *Core, _ *DecodedInstruction) error { return fmt.Errorf("Unimplemented move operation!") }
var moveTable = [32]MoveOp{
moveRegister,
swapRegisters,
moveOpSet,
load,
store,
moveOpPush,
moveOpPop,
moveOpPeek,
moveOpLoadCode,
moveOpStoreCode,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
unimplementedMoveOp,
}
func init() {
if MoveOpCount > 32 {
panic("Too many move operations registered! Programmer Failure!")
}
}
func moveOpStoreCode(core *Core, inst *DecodedInstruction) error {
// this one is a little odd since we have to use the contents of two registers
// build an instruction from lower and upper
return core.SetCodeMemory(core.Register(inst.Data[0]), (Instruction(core.Register(inst.Data[1])) | (Instruction(core.Register(inst.Data[2])) << 16)))
}
func moveOpLoadCode(core *Core, inst *DecodedInstruction) error {
// in this case we need to load an Instruction from memory and store it into the upper and lower
// registers. The syntax is dest, src0 are lower and upper with src1 being dest
idat := core.CodeMemory(core.Register(inst.Data[0]))
// set the lower and upper halves
lowerHalf, upperHalf := Word(idat), Word(idat>>16)
if err := core.SetRegister(inst.Data[1], lowerHalf); err != nil {
return err
} else if err := core.SetRegister(inst.Data[2], upperHalf); err != nil {
return err
} else {
return nil
}
}
func moveRegister(core *Core, inst *DecodedInstruction) error {
dest, src := inst.Data[0], core.Register(inst.Data[1])
return core.SetRegister(dest, src)
}
func swapRegisters(core *Core, inst *DecodedInstruction) error {
dest, src0 := inst.Data[0], inst.Data[1]
r0, r1 := core.Register(dest), core.Register(src0)
if err := core.SetRegister(src0, r0); err != nil {
return err
} else if err := core.SetRegister(dest, r1); err != nil {
return err
} else {
return nil
}
}
func moveOpSet(core *Core, inst *DecodedInstruction) error {
dest := inst.Data[0]
return core.SetRegister(dest, inst.Immediate())
}
func load(core *Core, inst *DecodedInstruction) error {
var val Word
dest, addr, seg := inst.Data[0], core.Register(inst.Data[1]), segment(inst.Data[2])
switch seg {
case dataSegment:
val = core.DataMemory(addr)
case microcodeSegment:
val = core.MicrocodeMemory(addr)
case stackSegment:
val = core.StackMemory(addr)
case callSegment:
val = core.CallMemory(addr)
case ioSegment:
if q, err := core.IoMemory(addr); err != nil {
return err
} else {
val = q
}
case codeSegment:
return fmt.Errorf("Can't load from the code segment!")
default:
return fmt.Errorf("Attempted to load from illegal segment %d", seg)
}
return core.SetRegister(dest, val)
}
func store(core *Core, inst *DecodedInstruction) error {
dest, src, seg := core.Register(inst.Data[0]), core.Register(inst.Data[1]), segment(inst.Data[2])
switch segment(seg) {
case dataSegment:
return core.SetDataMemory(dest, src)
case microcodeSegment:
return core.SetMicrocodeMemory(dest, src)
case stackSegment:
return core.SetStackMemory(dest, src)
case callSegment:
return core.SetCallMemory(dest, src)
case codeSegment:
return fmt.Errorf("Can't write to code memory!")
case ioSegment:
return core.SetIoMemory(dest, src)
default:
return fmt.Errorf("Attempted to write to illegal segment %d", seg)
}
}
func moveOpPush(core *Core, inst *DecodedInstruction) error {
dest := inst.Data[0]
core.Push(core.Register(dest))
return nil
}
func moveOpPop(core *Core, inst *DecodedInstruction) error {
dest := inst.Data[0]
return core.SetRegister(dest, core.Pop())
}
func moveOpPeek(core *Core, inst *DecodedInstruction) error {
dest := inst.Data[0]
return core.SetRegister(dest, core.Peek())
}
func move(core *Core, inst *DecodedInstruction) error {
return moveTable[inst.Op].Invoke(core, inst)
}