-
Notifications
You must be signed in to change notification settings - Fork 3
/
encode.go
348 lines (298 loc) · 6.39 KB
/
encode.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package zog
import "fmt"
func Encode(insts []Instruction) []byte {
buf := make([]byte, 0)
for _, inst := range insts {
instBuf := inst.Encode()
buf = append(buf, instBuf...)
}
return buf
}
type loc8Info struct {
ltype locType
idxTable byte
imm8 byte
isBC bool
imm16 []byte
}
type idxInfo struct {
isPrefix bool
isIY bool // If idxPrefix is true, else IX
hasDisp bool // Redundant?
idxDisp byte
}
type InstBin8 struct {
dst Loc8
src Loc8
dstInfo loc8Info
srcInfo loc8Info
idx idxInfo
base byte
}
type locType int
const (
tableR locType = 1
Immediate = 2
tableRP = 3
tableRP2 = 4
BCDEContents = 5
ImmediateContents = 6
)
type InstU8 struct {
l Loc8
base byte
lInfo loc8Info
idx idxInfo
}
func inspectLoc8(l Loc8, info *loc8Info, idx *idxInfo) {
// indexed H and L are tableR, and set idx info
switch l {
case IXH:
info.ltype = tableR
info.idxTable = findInTableR(H)
idx.isPrefix = true
idx.isIY = false
return
case IXL:
info.ltype = tableR
info.idxTable = findInTableR(L)
idx.isPrefix = true
idx.isIY = false
return
case IYH:
info.ltype = tableR
info.idxTable = findInTableR(H)
idx.isPrefix = true
idx.isIY = true
return
case IYL:
info.ltype = tableR
info.idxTable = findInTableR(L)
idx.isPrefix = true
idx.isIY = true
return
}
iContents, ok := l.(IndexedContents)
if ok {
r16, ok := iContents.addr.(R16)
if !ok {
panic("Non-r16 addr in indexed content")
}
info.ltype = tableR
info.idxTable = findInTableR(Contents{HL})
idx.isPrefix = true
idx.isIY = r16 == IY // Else IX
idx.hasDisp = true
idx.idxDisp = byte(iContents.d)
return
}
contents, ok := l.(Contents)
if ok {
if contents.addr == HL {
info.ltype = tableR
info.idxTable = findInTableR(Contents{HL})
return
} else if contents.addr == BC || contents.addr == DE {
info.ltype = BCDEContents
info.isBC = contents.addr == BC
return
}
label, labelOK := contents.addr.(*Label)
imm16, ok := contents.addr.(Imm16)
if ok || labelOK {
if labelOK {
imm16 = label.Imm16
}
info.ltype = ImmediateContents
hi := byte(imm16 >> 8)
lo := byte(imm16 & 0xff)
info.imm16 = []byte{lo, hi}
return
}
panic("Unrecognised contents of loc8")
}
r8, ok := l.(R8)
if ok {
info.ltype = tableR
info.idxTable = findInTableR(r8)
return
}
imm8, ok := l.(Imm8)
if ok {
info.ltype = Immediate
info.imm8 = byte(imm8)
return
}
panic(fmt.Sprintf("WTF? %T", l))
}
func (u *InstU8) inspect() {
inspectLoc8(u.l, &u.lInfo, &u.idx)
}
func (u *InstU8) exec(z *Zog, f func(byte) byte) error {
v, err := u.l.Read8(z)
if err != nil {
return fmt.Errorf("%T: failed to read: %s", u, err)
}
v = f(v)
z.SetFlag(F_S, v >= 0x80)
z.SetFlag(F_Z, v == 0)
err = u.l.Write8(z, v)
if err != nil {
return fmt.Errorf("%T: failed to write: %s", u, err)
}
return nil
}
func (i *InstBin8) exec(z *Zog, f func(byte) byte) error {
v, err := i.src.Read8(z)
if err != nil {
return fmt.Errorf("LD8: failed to read: %s", err)
}
err = i.dst.Write8(z, v)
if err != nil {
return fmt.Errorf("LD8: failed to write: %s", err)
}
return nil
}
type InstU16 struct {
l Loc16
lInfo loc16Info
idx idxInfo
}
func (u *InstU16) exec(z *Zog, f func(uint16) uint16) error {
v, err := u.l.Read16(z)
if err != nil {
return fmt.Errorf("%T: failed to read: %s", u, err)
}
v = f(v)
err = u.l.Write16(z, v)
if err != nil {
return fmt.Errorf("%T: failed to write: %s", u, err)
}
return nil
}
type InstBin16 struct {
dst Loc16
src Loc16
dstInfo loc16Info
srcInfo loc16Info
idx idxInfo
}
func (i *InstBin16) exec(z *Zog, f func(uint16, uint16) uint16) error {
src, err := i.src.Read16(z)
if err != nil {
return fmt.Errorf("%T : can't read src: %s (%v)", i, i.src, err)
}
dst, err := i.dst.Read16(z)
if err != nil {
return fmt.Errorf("%T : can't read dst: %s (%v)", i, i.dst, err)
}
v := f(dst, src)
err = i.dst.Write16(z, v)
if err != nil {
return fmt.Errorf("%T : can't write dst: %s (%v)", i, i.dst, err)
}
return nil
}
type loc16Info struct {
ltype locType
idxTable byte
imm16 []byte
}
func (li *loc16Info) isHLLike() bool {
return li.ltype == tableRP && li.idxTable == HL_RP_INDEX
}
func inspectLoc16(l Loc16, info *loc16Info, idx *idxInfo, wantRP2 bool) {
if l == IX {
idx.isPrefix = true
l = HL
} else if l == IY {
idx.isPrefix = true
idx.isIY = true
l = HL
}
contents, ok := l.(Contents)
if ok {
imm16, isImm := contents.addr.(Imm16)
if isImm {
info.ltype = ImmediateContents
hi := byte(imm16 >> 8)
lo := byte(imm16 & 0xff)
info.imm16 = []byte{lo, hi}
return
}
panic("Non-immediate Loc16 contents")
}
imm16, ok := l.(Imm16)
if !ok {
var label *Label
// overwrite 'ok'
label, ok = l.(*Label)
if ok {
imm16 = label.Imm16
}
}
if ok {
info.ltype = Immediate
hi := byte(imm16 >> 8)
lo := byte(imm16 & 0xff)
info.imm16 = []byte{lo, hi}
} else {
if wantRP2 {
info.ltype = tableRP2
info.idxTable = findInTableRP2(l)
} else {
info.ltype = tableRP
info.idxTable = findInTableRP(l)
}
}
}
func (u *InstU16) inspectRP2() {
inspectLoc16(u.l, &u.lInfo, &u.idx, true)
}
func (u *InstU16) inspect() {
inspectLoc16(u.l, &u.lInfo, &u.idx, false)
}
func (b *InstBin16) inspect() {
inspectLoc16(b.dst, &b.dstInfo, &b.idx, false)
inspectLoc16(b.src, &b.srcInfo, &b.idx, false)
}
func encodeHelper(base []byte, idx idxInfo, dispFirst bool) []byte {
encoded := base
if idx.isPrefix {
idxPrefix := byte(0xdd)
if idx.isIY {
idxPrefix = 0xfd
}
if dispFirst {
// dispFirst implies idx.hasDisp
encoded = append([]byte{idxPrefix}, encoded...)
n := encoded[len(encoded)-1]
encoded[len(encoded)-1] = idx.idxDisp
encoded = append(encoded, n)
} else {
encoded = append([]byte{idxPrefix}, encoded...)
if idx.hasDisp {
encoded = append(encoded, idx.idxDisp)
}
}
}
return encoded
}
func idxEncodeHelper(base []byte, idx idxInfo) []byte {
return encodeHelper(base, idx, false)
}
func ddcbHelper(base []byte, idx idxInfo) []byte {
return encodeHelper(base, idx, true)
}
func (b *InstBin8) inspect() {
inspectLoc8(b.src, &b.srcInfo, &b.idx)
inspectLoc8(b.dst, &b.dstInfo, &b.idx)
}
// See decomposeByte in decode.go
func encodeXYZ(x, y, z byte) byte {
return x<<6 | y<<3 | z
}
func encodeXPQZ(x, p, q, z byte) byte {
y := p<<1 | q
return encodeXYZ(x, y, z)
}