-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopcode_helper.go
79 lines (70 loc) · 1.46 KB
/
opcode_helper.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
package go_gb
import (
"encoding/json"
"fmt"
"os"
"strconv"
"strings"
)
type operand struct {
Name string `json:"name"`
Immediate bool `json:"immediate"`
}
func (o operand) String() string {
if o.Immediate {
return o.Name
}
return fmt.Sprintf("(%s)", o.Name)
}
type opInfo struct {
Name string `json:"mnemonic"`
Operands []operand `json:"operands"`
cachedString string
}
func (o *opInfo) String() string {
if o.cachedString == "" {
var sb strings.Builder
sb.WriteString(o.Name)
for i, operand := range o.Operands {
sb.WriteRune(' ')
sb.WriteString(operand.String())
if i != len(o.Operands)-1 {
sb.WriteString(",")
}
}
o.cachedString = sb.String()
}
return o.cachedString
}
var Unprefixed = map[byte]*opInfo{}
var Prefixed = map[byte]*opInfo{}
func InitInstructions() {
ops, err := os.Open("opcodes.json")
if err != nil {
panic(err)
}
defer ops.Close()
var document struct {
Unprefixed map[string]opInfo `json:"unprefixed"`
Prefixed map[string]opInfo `json:"cbprefixed"`
}
if err = json.NewDecoder(ops).Decode(&document); err != nil {
panic(err)
}
for opcode, op := range document.Unprefixed {
op := op
id, err := strconv.ParseUint(opcode[2:], 16, 64)
if err != nil {
panic(err)
}
Unprefixed[byte(id)] = &op
}
for opcode, op := range document.Prefixed {
op := op
id, err := strconv.ParseUint(opcode[2:], 16, 64)
if err != nil {
panic(err)
}
Prefixed[byte(id)] = &op
}
}