-
Notifications
You must be signed in to change notification settings - Fork 3
/
assemble_test.go
148 lines (120 loc) · 2.63 KB
/
assemble_test.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
package zog
import (
"fmt"
"strings"
"testing"
)
func TestAssembleAll(t *testing.T) {
testUtilRunAll(t, func(t *testing.T, byteForm []byte, stringForm string) {
testAssembleOne(t, stringForm)
})
}
func TestAssembleRich(t *testing.T) {
testCases := []struct {
prog string
byteFormStr string
}{
{`org 0100h : start: jp start`, "c3 00 01"},
{`ld a, (foo) : foo: defb abh`, "3a 03 00 ab"},
{`defw 1234h`, "3412"},
{`defb 10h`, "10"},
{`defs 03h`, "00 00 00"},
{"LD HL, 0x1000", "21 00 10"},
{"LD HL, 0x1000 : LD A, B : PUSH HL", "21 00 10 78 e5"},
{"LD HL, 0x1000 ; LD A, B : PUSH HL", "21 00 10"},
{" LD HL, 0x1000 ; LD A, B : PUSH HL", "21 00 10"},
{`
foo: JP foo
`, "C3 00 00"},
{`
foo:
JP foo
`, "C3 00 00"},
}
for _, tc := range testCases {
fmt.Printf("Assemble: %s\n", tc.prog)
assembly, err := Assemble(tc.prog)
if err != nil {
t.Fatalf("Failed to assemble [%s]: %s", tc.prog, err)
}
buf := Encode(assembly.Instructions())
byteFormStr := strings.ToLower(tc.byteFormStr)
byteFormStr = strings.Replace(byteFormStr, " ", "", -1)
hexBufStr := strings.ToLower(bufToHex(buf))
if hexBufStr != byteFormStr {
t.Fatalf("Encoded instructions doesn't match got [%s] expected [%s]", hexBufStr, byteFormStr)
} else {
fmt.Printf("Matched OK\n")
}
}
}
func TestAssembleBasic(t *testing.T) {
testCases := []string{
"RLC (IX+1), B",
"RLC (IX+1)",
"SRL (IX+1), B",
"SRL (IX+1)",
"SET 7, (IX+1), B",
"SET 7, (IX+1)",
"RES 7, (IX+1), B",
"RES 7, (IX+1)",
"BIT 7, (IX+1)",
"LD A, (IX+10)",
"LD A, (IX-10)",
// TODO: test hex parses
// "LD A, (IX+0x0a)",
// "LD A, (IX-0x0a)",
// "LD A, (IX+0ah)",
// "LD A, (IX-0ah)",
"OUT (0xff), A",
"IN A, (0xff)",
"OUT (c), A",
"IN A, (c)",
"EX (SP), HL",
"LD (0x1234), A",
"inc iy",
"inc iyh",
"add iy, bc",
"INC B",
"DEC B",
"LD A, B",
"LD A, 0x10",
"LD A, 0x10",
"INC DE",
// "ADD DE, HL",
"EX AF,AF'",
"RET C",
"CALL DE",
"RET C",
"RST 8",
"RST 16",
"DJNZ -10",
"CALL Z, DE",
"RL A",
"SET 4, A",
// "SLA F",
"SLA (HL)",
"LD DE, 0x1234",
"LD DE, (0x1234)",
"LD (0x1234), HL",
"LD (0x1234), H",
"LD A, (HL)",
"LD (HL), A",
}
for _, s := range testCases {
testAssembleOne(t, s)
}
}
func testAssembleOne(t *testing.T, s string) {
assembly, err := Assemble(s)
if err != nil {
t.Fatalf("Failed to assemble [%s]: %s", s, err)
}
assembledStr := ""
for _, linst := range assembly.Linsts {
assembledStr += linst.Inst.String() + "\n"
}
if !compareAssembly(assembledStr, s) {
t.Fatalf("Assembled str not equal [%s] != [%s]", assembledStr, s)
}
}