/
decode.go
44 lines (39 loc) · 1.33 KB
/
decode.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
package vm
import "github.com/dnsge/orange/arch"
func (v *VirtualMachine) fetchNextInstruction() arch.Instruction {
i := v.memory.Read(v.programCounter, 32) // read word for instruction
return arch.Instruction(i)
}
func (v *VirtualMachine) executeInstruction(instruction arch.Instruction) {
opcode := arch.GetOpcode(instruction)
iType := arch.GetInstructionType(opcode)
switch iType {
case arch.IType_A:
i := arch.DecodeATypeInstruction(instruction, opcode)
v.executeATypeInstruction(i)
case arch.IType_AI:
i := arch.DecodeATypeImmInstruction(instruction, opcode)
v.executeATypeImmInstruction(i)
case arch.IType_M:
i := arch.DecodeMTypeInstruction(instruction, opcode)
v.executeMTypeInstruction(i)
case arch.IType_E:
i := arch.DecodeETypeInstruction(instruction, opcode)
v.executeETypeInstruction(i)
case arch.IType_BI:
i := arch.DecodeBTypeImmInstruction(instruction, opcode)
v.executeBTypeImmInstruction(i)
case arch.IType_B:
i := arch.DecodeBTypeInstruction(instruction, opcode)
v.executeBTypeInstruction(i)
case arch.IType_R:
i := arch.DecodeRTypeInstruction(instruction, opcode)
v.executeRTypeInstruction(i)
case arch.IType_O:
i := arch.DecodeOTypeInstruction(instruction, opcode)
v.executeOTypeInstruction(i)
default:
panic("invalid instruction type")
}
v.programCounter += 4 // advance by word
}