-
Notifications
You must be signed in to change notification settings - Fork 3
/
instruction.go
2337 lines (2136 loc) · 42.6 KB
/
instruction.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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package zog
import (
"errors"
"fmt"
"strings"
)
type Instruction interface {
String() string
Encode() []byte
Resolve(a *Assembly) error
Execute(z *Zog) error
TStates(z *Zog) int
}
type LabelHolder struct{}
func (lh *LabelHolder) String() string { return "" }
func (lh *LabelHolder) Encode() []byte { return make([]byte, 0) }
func (lh *LabelHolder) Resolve(a *Assembly) error { return nil }
func (lh *LabelHolder) Execute(z *Zog) error { panic("Attempt to execute labelholder") }
func (lh *LabelHolder) TStates(z *Zog) int { panic("Attempt to get t-states of labelholder") }
type Data struct {
data []byte
}
func NewData(data []byte) *Data {
return &Data{data: data}
}
func (d *Data) String() string {
return bufToHex(d.data)
}
func (d *Data) TStates(z *Zog) int {
panic("Error - trying to get t-states for dummy data instruction")
}
func (d *Data) Encode() []byte {
return d.data
}
func (d *Data) Resolve(a *Assembly) error {
return nil
}
func (d *Data) Execute(z *Zog) error {
return errors.New("Error - trying to execute dummy data instruction")
}
type LD8 struct {
InstBin8
}
func NewLD8(dst Loc8, src Loc8) *LD8 {
return &LD8{InstBin8{dst: dst, src: src}}
}
func (i *LD8) TStates(z *Zog) int {
switch i.dst.(type) {
case R8:
r8 := i.dst.(R8)
switch i.src.(type) {
case R8:
r8s := i.src.(R8)
if r8 >= A && r8 <= L && r8 != F {
if r8s >= A && r8s <= L && r8s != F {
return 4
}
}
if r8 == I {
if r8s == A {
return 9
}
}
if r8 == IXH || r8 == IXL || r8 == IYH || r8 == IYL {
return 8
}
if r8 == A && r8s == R {
return 9
}
if r8 == R && r8s == A {
return 9
}
case Imm8:
if r8 >= A && r8 <= L && r8 != F {
return 7
} else if r8 == IXH || r8 == IXL || r8 == IYH || r8 == IYL {
return 11
}
case Contents:
c := i.src.(Contents)
switch c.addr.(type) {
case R16:
r16, ok := c.addr.(R16)
if ok {
if r16 == HL {
return 7
}
if r8 == A {
if r16 == BC || r16 == DE {
return 7
}
}
}
case Imm16:
if r8 == A {
return 13
}
}
case IndexedContents:
return 19
}
case Contents:
c := i.dst.(Contents)
switch c.addr.(type) {
case R16:
r16 := c.addr.(R16)
if r16 == HL {
switch i.src.(type) {
case R8:
r8 := i.src.(R8)
if r8 >= A && r8 <= L && r8 != F {
return 7
}
case Imm8:
return 10
}
} else if r16 == BC || r16 == DE {
r8, ok := i.src.(R8)
if ok {
if r8 == A {
return 7
}
}
}
case Imm16:
r8, ok := i.src.(R8)
if ok {
if r8 == A {
return 13
}
}
}
case IndexedContents:
return 19
}
fmt.Printf("%v\n", i)
fmt.Printf("i.src.(R8): %d\n", i.src.(R8))
panic("Invalid load instruction\n")
}
func (l *LD8) String() string {
return fmt.Sprintf("LD %s, %s", l.dst, l.src)
}
func (l *LD8) Encode() []byte {
// ED special cases
switch true {
case l.dst == I && l.src == A:
return []byte{0xed, 0x47}
case l.dst == A && l.src == I:
return []byte{0xed, 0x57}
case l.dst == R && l.src == A:
return []byte{0xed, 0x4f}
case l.dst == A && l.src == R:
return []byte{0xed, 0x5f}
}
l.inspect()
switch l.dstInfo.ltype {
case BCDEContents:
// LD (BC), A or LD (DE), A
p := byte(1)
if l.dstInfo.isBC {
p = 0
}
buf := []byte{encodeXPQZ(0, p, 0, 2)}
return buf
case ImmediateContents:
// LD (nn), A
buf := []byte{encodeXPQZ(0, 3, 0, 2)}
buf = append(buf, l.dstInfo.imm16...)
return buf
}
if l.dstInfo.ltype != tableR {
panic("Non-tableR dst in LD8")
}
switch l.srcInfo.ltype {
case tableR:
b := encodeXYZ(1, l.dstInfo.idxTable, l.srcInfo.idxTable)
return idxEncodeHelper([]byte{b}, l.idx)
case Immediate:
buf := []byte{encodeXYZ(0, l.dstInfo.idxTable, 6)}
buf = idxEncodeHelper(buf, l.idx)
buf = append(buf, l.srcInfo.imm8)
return buf
case BCDEContents:
// LD A, (BC) or LD A, (DE)
p := byte(1)
if l.srcInfo.isBC {
p = 0
}
b := encodeXPQZ(0, p, 1, 2)
return []byte{b}
case ImmediateContents:
// LD A, (nn)
buf := []byte{encodeXPQZ(0, 3, 1, 2)}
buf = append(buf, l.srcInfo.imm16...)
return buf
default:
panic("Unknown src type in LD8")
}
}
func (l *LD8) Execute(z *Zog) error {
// Flags are unchanged for LD
f, err := F.Read8(z)
if err != nil {
return err
}
err = l.exec(z, func(v byte) byte { return v })
if err != nil {
return err
}
/*
Whem a Load Register A with Register I (LD A, I) instruction or a Load Register A with Register
R (LD A, R) instruction is executed, the state of IFF2 is copied to the parity flag, where it
can be tested or stored.
*/
z.SetFlag(F_PV, z.is.IFF2)
return F.Write8(z, f)
}
type INC8 struct {
InstU8
}
func NewINC8(l Loc8) *INC8 {
return &INC8{InstU8{l: l}}
}
func (i *INC8) String() string {
return fmt.Sprintf("INC %s", i.l)
}
func (i *INC8) TStates(z *Zog) int {
switch i.l.(type) {
case R8:
return 4
case Contents:
return 11
case IndexedContents:
return 23
default:
panic(fmt.Sprintf("Unknown inc location type: %T", i.l))
}
}
func (i *INC8) Encode() []byte {
i.inspect()
if i.lInfo.ltype != tableR {
panic("Non-tableR INC8")
}
b := encodeXYZ(0, i.lInfo.idxTable, 4)
return idxEncodeHelper([]byte{b}, i.idx)
}
func (i *INC8) Execute(z *Zog) error {
err := i.exec(z, func(v byte) byte {
z.SetFlag(F_H, v&0xf == 0x0f)
z.SetFlag(F_PV, v == 0x7f)
z.SetFlag(F_N, false)
return v + 1
})
return err
}
type DEC8 struct {
InstU8
}
func NewDEC8(l Loc8) *DEC8 {
return &DEC8{InstU8{l: l}}
}
func (d *DEC8) String() string {
return fmt.Sprintf("DEC %s", d.l)
}
func (d *DEC8) TStates(z *Zog) int {
switch d.l.(type) {
case R8:
return 4
case Contents:
return 11
case IndexedContents:
return 23
default:
panic(fmt.Sprintf("Unknown dec location type: %T", d.l))
}
}
func (d *DEC8) Encode() []byte {
d.inspect()
if d.lInfo.ltype != tableR {
panic("Non-tableR DEC8")
}
b := encodeXYZ(0, d.lInfo.idxTable, 5)
return idxEncodeHelper([]byte{b}, d.idx)
}
func (d *DEC8) Execute(z *Zog) error {
err := d.exec(z, func(v byte) byte {
z.SetFlag(F_H, v&0x0f == 0x00)
z.SetFlag(F_PV, v == 0x80)
z.SetFlag(F_N, true)
return v - 1
})
return err
}
type LD16 struct {
InstBin16
}
func NewLD16(dst, src Loc16) *LD16 {
return &LD16{InstBin16: InstBin16{dst: dst, src: src}}
}
func (i *LD16) TStates(z *Zog) int {
switch i.dst.(type) {
case R16:
r16 := i.dst.(R16)
switch i.src.(type) {
case Imm16:
if r16 == IX || r16 == IY {
return 14
}
return 10
case Contents:
c := i.src.(Contents)
switch c.addr.(type) {
case Imm16:
if r16 == HL {
return 16
}
return 20
}
case R16:
r16s := i.src.(R16)
if r16 == SP {
if r16s == HL {
return 6
} else if r16s == IX || r16s == IY {
return 10
}
}
}
case Contents:
c := i.dst.(Contents)
switch c.addr.(type) {
case Imm16:
r16, ok := i.src.(R16)
if ok {
if r16 == HL {
return 16
}
return 20
}
}
}
panic("Invalid load instruction\n")
}
func (l *LD16) String() string {
return fmt.Sprintf("LD %s, %s", l.dst, l.src)
}
func (l *LD16) Encode() []byte {
l.inspect()
switch l.dstInfo.ltype {
case ImmediateContents:
// LD (nn), HL has multiple encodings, we choose the non-ED one
if l.srcInfo.isHLLike() {
buf := []byte{encodeXPQZ(0, 2, 0, 2)}
buf = append(buf, l.dstInfo.imm16...)
return idxEncodeHelper(buf, l.idx)
} else {
if l.srcInfo.ltype != tableRP {
panic("Non-tableRP src in LD16 (NN), src")
}
buf := []byte{0xed, encodeXPQZ(1, l.srcInfo.idxTable, 0, 3)}
buf = append(buf, l.dstInfo.imm16...)
return buf
}
}
if l.dstInfo.ltype != tableRP {
panic("Non-tableRP dst in LD16")
}
switch l.srcInfo.ltype {
case Immediate:
buf := []byte{encodeXPQZ(0, l.dstInfo.idxTable, 0, 1)}
buf = append(buf, l.srcInfo.imm16...)
return idxEncodeHelper(buf, l.idx)
case ImmediateContents:
// LD HL, (nn) has multiple encodings
if l.dstInfo.isHLLike() {
buf := []byte{encodeXPQZ(0, 2, 1, 2)}
buf = append(buf, l.srcInfo.imm16...)
return idxEncodeHelper(buf, l.idx)
} else {
if l.dstInfo.ltype != tableRP {
panic("Non-tableRP src in LD16 (NN), src")
}
buf := []byte{0xed, encodeXPQZ(1, l.dstInfo.idxTable, 1, 3)}
buf = append(buf, l.srcInfo.imm16...)
return buf
}
case tableRP:
if l.srcInfo.isHLLike() {
if l.dst != SP {
panic("HL-like load to non-SP")
}
buf := []byte{encodeXPQZ(3, 3, 1, 1)}
return idxEncodeHelper(buf, l.idx)
} else {
panic("Non-HL like load to something")
}
default:
panic("Unknown src type in LD16")
}
}
func (l *LD16) Execute(z *Zog) error {
nn, err := l.src.Read16(z)
if err != nil {
return fmt.Errorf("LD16: failed to read: %s", err)
}
err = l.dst.Write16(z, nn)
if err != nil {
return fmt.Errorf("LD16: failed to write: %s", err)
}
return nil
}
type ADD16 struct {
InstBin16
}
func NewADD16(dst, src Loc16) *ADD16 {
return &ADD16{InstBin16: InstBin16{dst: dst, src: src}}
}
func (a *ADD16) TStates(z *Zog) int {
if a.dst == HL {
return 11
} else {
return 15
}
}
func (a *ADD16) String() string {
return fmt.Sprintf("ADD %s, %s", a.dst, a.src)
}
func (a *ADD16) Encode() []byte {
a.inspect()
if a.dstInfo.ltype != tableRP {
panic("Non-tableRP dst in ADD16")
}
if a.srcInfo.ltype != tableRP {
panic("Non-tableRP src in ADD16")
}
if !a.dstInfo.isHLLike() {
panic("Non-HL dst in ADD16")
}
switch a.srcInfo.ltype {
case tableRP:
buf := []byte{encodeXPQZ(0, a.srcInfo.idxTable, 1, 1)}
return idxEncodeHelper(buf, a.idx)
default:
panic("Unknown src type in ADD16")
}
}
func (a *ADD16) Execute(z *Zog) error {
return a.exec(z, func(a, b uint16) uint16 {
v := a + b
z.SetFlag(F_H, ((a&0x0fff)+(b&0x0fff))&0x1000 != 0)
z.SetFlag(F_N, false)
z.SetFlag(F_C, int(a)+int(b) > 0xffff)
return v
})
}
type ADC16 struct {
InstBin16
}
func NewADC16(dst, src Loc16) *ADC16 {
return &ADC16{InstBin16: InstBin16{dst: dst, src: src}}
}
func (a *ADC16) TStates(z *Zog) int {
return 15
}
func (a *ADC16) String() string {
return fmt.Sprintf("ADC %s, %s", a.dst, a.src)
}
func (a *ADC16) Encode() []byte {
a.inspect()
if a.srcInfo.ltype != tableRP {
panic("Non-tableRP src in ADC16")
}
buf := []byte{0xed, encodeXPQZ(1, a.srcInfo.idxTable, 1, 2)}
return idxEncodeHelper(buf, a.idx)
}
func (a *ADC16) Execute(z *Zog) error {
return a.exec(z, func(a, b uint16) uint16 {
c := uint16(0)
if z.GetFlag(F_C) {
c = 1
}
v32 := uint32(a) + uint32(b) + uint32(c)
v := uint16(v32)
z.SetFlag(F_S, v >= 0x8000)
z.SetFlag(F_Z, v == 0)
z.SetFlag(F_H, ((a&0x0fff)+(b&0x0fff)+c)&0x1000 != 0)
overflow := (isPos16(a) && isPos16(b) && !isPos16(v)) || (!isPos16(a) && !isPos16(b) && isPos16(v))
z.SetFlag(F_PV, overflow)
z.SetFlag(F_N, false)
z.SetFlag(F_C, v32 >= 0x10000)
// fmt.Fprintf(os.Stderr, "JB ADC16 a %04x b %04x v %04x (carry %d)\n", a, b, v, c)
return v
})
}
type SBC16 struct {
InstBin16
}
func NewSBC16(dst, src Loc16) *SBC16 {
return &SBC16{InstBin16: InstBin16{dst: dst, src: src}}
}
func (s *SBC16) TStates(z *Zog) int {
return 15
}
func (s *SBC16) String() string {
return fmt.Sprintf("SBC %s, %s", s.dst, s.src)
}
func (s *SBC16) Encode() []byte {
s.inspect()
if s.srcInfo.ltype != tableRP {
panic("Non-tableRP src in SBC16")
}
buf := []byte{0xed, encodeXPQZ(1, s.srcInfo.idxTable, 0, 2)}
return idxEncodeHelper(buf, s.idx)
}
func isPos16(v uint16) bool {
return v&0x8000 == 0
}
func (s *SBC16) Execute(z *Zog) error {
return s.exec(z, func(a, b uint16) uint16 {
c := uint16(0)
if z.GetFlag(F_C) {
c = 1
}
v := a - b - c
z.SetFlag(F_S, !isPos16(v))
z.SetFlag(F_Z, v == 0)
z.SetFlag(F_H, ((a&0x0fff)-(b&0x0fff)-c)&0x1000 != 0)
vSigned := int32(int16(a)) - int32(int16(b)) - int32(c)
z.SetFlag(F_PV, vSigned >= 0x8000 || vSigned < -0x8000)
z.SetFlag(F_N, true)
z.SetFlag(F_C, int(a)-int(b)-int(c) < 0)
return v
})
}
type INC16 struct {
InstU16
}
func NewINC16(l Loc16) *INC16 {
return &INC16{InstU16{l: l}}
}
func (i *INC16) String() string {
return fmt.Sprintf("INC %s", i.l)
}
func (i *INC16) TStates(z *Zog) int {
if _, ok := i.l.(IndexedContents); ok {
return 10
} else {
return 6
}
}
func (i *INC16) Encode() []byte {
i.inspect()
if i.lInfo.ltype != tableRP {
panic("Non-tableRP INC16")
}
b := encodeXPQZ(0, i.lInfo.idxTable, 0, 3)
return idxEncodeHelper([]byte{b}, i.idx)
}
func (i *INC16) Execute(z *Zog) error {
err := i.exec(z, func(v uint16) uint16 {
return v + 1
})
return err
}
type DEC16 struct {
InstU16
}
func NewDEC16(l Loc16) *DEC16 {
return &DEC16{InstU16{l: l}}
}
func (d *DEC16) String() string {
return fmt.Sprintf("DEC %s", d.l)
}
func (d *DEC16) TStates(z *Zog) int {
if _, ok := d.l.(IndexedContents); ok {
return 10
} else {
return 6
}
}
func (d *DEC16) Encode() []byte {
d.inspect()
if d.lInfo.ltype != tableRP {
panic("Non-tableRP DEC16")
}
b := encodeXPQZ(0, d.lInfo.idxTable, 1, 3)
return idxEncodeHelper([]byte{b}, d.idx)
}
func (d *DEC16) Execute(z *Zog) error {
err := d.exec(z, func(v uint16) uint16 {
return v - 1
})
return err
}
type EX struct {
InstBin16
}
func NewEX(dst, src Loc16) *EX {
return &EX{InstBin16: InstBin16{dst: dst, src: src}}
}
func (ex *EX) TStates(z *Zog) int {
switch ex.src {
case AF:
return 4
case DE:
return 4
default:
if _, ok := ex.dst.(IndexedContents); ok {
return 23
} else {
return 19
}
}
}
func (ex *EX) String() string {
return fmt.Sprintf("EX %s, %s", ex.dst, ex.src)
}
func (ex *EX) Encode() []byte {
if ex.dst == AF && ex.src == AF_PRIME {
return []byte{0x08}
} else if ex.dst.String() == (Contents{SP}).String() {
var info loc16Info
var idx idxInfo
inspectLoc16(ex.src, &info, &idx, false)
buf := []byte{encodeXYZ(3, 4, 3)}
return idxEncodeHelper(buf, idx)
} else if ex.dst == DE && ex.src == HL {
// EX DE,HL is an excpetion to the IX/IY rule
return []byte{encodeXYZ(3, 5, 3)}
}
panic("Unrecognised EX instruction")
}
func (ex *EX) Execute(z *Zog) error {
a, err := ex.src.Read16(z)
if err != nil {
return fmt.Errorf("%s : can't read src: %s (%v)", ex, ex.src, err)
}
b, err := ex.dst.Read16(z)
if err != nil {
return fmt.Errorf("%s : can't read dst: %s (%v)", ex, ex.dst, err)
}
err = ex.dst.Write16(z, a)
if err != nil {
return fmt.Errorf("%s : can't write dst: %s (%v)", ex, ex.dst, err)
}
err = ex.src.Write16(z, b)
if err != nil {
return fmt.Errorf("%s : can't write dst: %s (%v)", ex, ex.dst, err)
}
return nil
}
type DJNZ struct {
d Disp
}
func (d *DJNZ) String() string {
return fmt.Sprintf("DJNZ %s", d.d)
}
func (d *DJNZ) TStates(z *Zog) int {
if z.reg.B == 0 {
return 13
} else {
return 8
}
}
func (d *DJNZ) Encode() []byte {
b := encodeXYZ(0, 2, 0)
return []byte{b, byte(d.d)}
}
func (d *DJNZ) Resolve(a *Assembly) error {
return nil
}
func (d *DJNZ) Execute(z *Zog) error {
bReg, err := B.Read8(z)
if err != nil {
return fmt.Errorf("Can't read B: %s", err)
}
bReg--
err = B.Write8(z, bReg)
if err != nil {
return fmt.Errorf("Can't write B: %s", err)
}
zero := bReg == 0
if !zero {
z.jr(int8(d.d))
}
return nil
}
type JR struct {
c Conditional
d Disp
}
func (j *JR) String() string {
if j.c == True || j.c == nil {
return fmt.Sprintf("JR %s", j.d)
} else {
return fmt.Sprintf("JR %s, %s", j.c, j.d)
}
}
func (j *JR) TStates(z *Zog) int {
takeJump := j.c.IsTrue(z)
if takeJump {
return 12
} else {
return 7
}
}
func (j *JR) Encode() []byte {
var y byte
if j.c == True || j.c == nil {
y = 3
} else {
y = findInTableCC(j.c)
y += 4
}
b := encodeXYZ(0, y, 0)
return []byte{b, byte(j.d)}
}
func (j *JR) Resolve(a *Assembly) error {
return nil
}
func (j *JR) Execute(z *Zog) error {
takeJump := j.c.IsTrue(z)
if takeJump {
z.jr(int8(j.d))
}
return nil
}
type JP struct {
InstU16
c Conditional
}
func NewJP(c Conditional, l Loc16) *JP {
return &JP{InstU16: InstU16{l: l}, c: c}
}
func (jp *JP) String() string {
if jp.c == True || jp.c == nil {
return fmt.Sprintf("JP %s", jp.l)
} else {
return fmt.Sprintf("JP %s, %s", jp.c, jp.l)
}
}
func (jp *JP) TStates(z *Zog) int {
switch jp.l.(type) {
case Imm16:
return 10
case R16:
return 4
case IndexedContents:
return 8
default:
panic(fmt.Sprintf("Unknown jp location type: %T [%s]", jp.l, jp))
}
}
func (jp *JP) Encode() []byte {
jp.inspect()
if jp.c == True || jp.c == nil {
if jp.lInfo.isHLLike() {
buf := []byte{encodeXPQZ(3, 2, 1, 1)}
return idxEncodeHelper(buf, jp.idx)
}
}
if jp.lInfo.ltype != Immediate {
panic("Non-immediate (or direct HL-like) JP")
}
var buf []byte
if jp.c == True || jp.c == nil {
buf = []byte{encodeXYZ(3, 0, 3)}
} else {
y := findInTableCC(jp.c)
buf = []byte{encodeXYZ(3, y, 2)}
}
buf = append(buf, jp.lInfo.imm16...)
return buf
}
func (jp *JP) Execute(z *Zog) error {
takeJump := jp.c.IsTrue(z)
if takeJump {
addr, err := jp.l.Read16(z)
if err != nil {
return err
}
z.jp(addr)
}
return nil
}
type CALL struct {
InstU16
c Conditional
}
func NewCALL(c Conditional, l Loc16) *CALL {
return &CALL{InstU16: InstU16{l: l}, c: c}
}
func (c *CALL) String() string {
if c.c == True || c.c == nil {
return fmt.Sprintf("CALL %s", c.l)
} else {
return fmt.Sprintf("CALL %s, %s", c.c, c.l)
}
}
func (c *CALL) TStates(z *Zog) int {
takeJump := c.c.IsTrue(z)
if takeJump {
return 17
} else {
return 10
}
}
func (c *CALL) Encode() []byte {
c.inspect()
var buf []byte
if c.c == nil || c.c == True {
buf = []byte{encodeXPQZ(3, 0, 1, 5)}
} else {
y := findInTableCC(c.c)
buf = []byte{encodeXYZ(3, y, 4)}
}
buf = append(buf, c.lInfo.imm16...)
return buf
}
func (c *CALL) Execute(z *Zog) error {
takeJump := c.c.IsTrue(z)
if takeJump {
addr, err := c.l.Read16(z)
if err != nil {
return err
}
z.push(z.reg.PC)
z.jp(addr)
}
return nil
}
type OUT struct {
port Loc8
value Loc8
}
func (o *OUT) TStates(z *Zog) int {
if o.value == A {
return 11
} else {
return 12
}
}
func (o *OUT) String() string {
return fmt.Sprintf("OUT (%s), %s", o.port, o.value)
}
func (o *OUT) Encode() []byte {
if o.port == C {
var info loc8Info
var idx idxInfo
inspectLoc8(o.value, &info, &idx)
if info.ltype != tableR {
panic("Non-tableR value in OUT")
}
// (HL)? IX?
buf := []byte{0xed, encodeXYZ(1, info.idxTable, 1)}
return idxEncodeHelper(buf, idx)
} else {
imm8 := o.port.(Imm8)
return []byte{encodeXYZ(3, 2, 3), byte(imm8)}
}
}
func (o *OUT) Resolve(a *Assembly) error {
return nil
}
func (o *OUT) Execute(z *Zog) error {
/*
In the IN A and OUT n, A instructions, the I/O device’s n address appears in the lower half
of the address bus (A7–A0), while the Accumulator content is transferred in the upper half
of the address bus. In all Register Indirect input output instructions, including block I/O
transfers, the contents of the C Register are transferred to the lower half of the address bus
(device address) while the contents of Register B are transferred to the upper half of the
address bus.
*/
var addr uint16
port, err := o.port.Read8(z)
if err != nil {
return err
}
if o.port == C {
addr = z.reg.Read16(BC)
} else {
addr = uint16(port&0xFF) | (uint16(z.reg.A) << 8)
}
v, err := o.value.Read8(z)
if err != nil {
return err
}
z.out(addr, v)
return nil
}
type IN struct {
dst Loc8
port Loc8
}
func (i *IN) TStates(z *Zog) int {
if i.dst == A {
return 11
} else {
return 12
}
}
func (i *IN) String() string {
return fmt.Sprintf("IN %s, (%s)", i.dst, i.port)
}
func (i *IN) Encode() []byte {
if i.port == C {
var y byte
var info loc8Info
var idx idxInfo
if i.dst == F {
y = 6
} else {
inspectLoc8(i.dst, &info, &idx)
if info.ltype != tableR {
panic("Non-tableR dst in IN")
}
y = info.idxTable
}
buf := []byte{0xed, encodeXYZ(1, y, 0)}
return idxEncodeHelper(buf, idx)
} else {
imm8 := i.port.(Imm8)
return []byte{encodeXYZ(3, 3, 3), byte(imm8)}
}
}
func (i *IN) Resolve(a *Assembly) error {
return nil
}
func (i *IN) Execute(z *Zog) error {
// See spec comment in OUT