-
Notifications
You must be signed in to change notification settings - Fork 1
/
mcode.lua
2439 lines (2250 loc) · 49 KB
/
mcode.lua
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
-- Machine code compile dictionary
local decode_tree = require "z80_opcodes"
local labels = {} -- label -> address for current word
local gotos = {} -- address to be patched -> label for current word
local literal_pos -- the dictionary position just after the newest emitted literal
local literal_pos2 -- the dictionary position of the second newest literal
local call_pos -- the dictionary position just after the newest emitted Z80 call instruction
local jump_targets = {} -- addresses targeted by (forward) jumps, for detecting when tail-calls can't be used
local long_jumps = {} -- locations in source code where jumps must be long (for ELSE and THEN)
-- Z80 registers
local A = 7
local B = 0
local C = 1
local D = 2
local E = 3
local H = 4
local L = 5
local AF = 0x10
local BC = 0x20
local DE = 0x30
local HL = 0x40
local IX = 0x50
local IY = 0x60
local SP = 0x70
local BC_INDIRECT = 0x80
local DE_INDIRECT = 0x90
local HL_INDIRECT = 0xa0
-- system variables
local SCRPOS = 0x3c1c
local STKBOT = 0x3c37
local SPARE = 0x3c3b
local reg_name = {
[A] = "a",
[B] = "b",
[C] = "c",
[D] = "d",
[E] = "e",
[H] = "h",
[L] = "l",
[AF] = "af",
[BC] = "bc",
[DE] = "de",
[HL] = "hl",
[IX] = "ix",
[IY] = "iy",
[SP] = "sp",
[BC_INDIRECT] = "(bc)",
[DE_INDIRECT] = "(de)",
[HL_INDIRECT] = "(hl)",
}
local function _ld(dest, src)
list_line("ld %s,%s", reg_name[dest], reg_name[src])
if dest == BC_INDIRECT and src == A then
-- ld (bc), A
emit_byte(0x02)
elseif dest == DE_INDIRECT and src == A then
-- ld (de), A
emit_byte(0x12)
elseif dest == HL_INDIRECT then
-- ld (hl), r
assert(src >= 0 and src <= 7, "_ld: unknown src register")
emit_byte(0x70 + src)
elseif dest == A and src == BC_INDIRECT then
-- ld a, (bc)
emit_byte(0x0a)
elseif dest == A and src == DE_INDIRECT then
-- ld a, (de)
emit_byte(0x1a)
elseif src == HL_INDIRECT then
-- ld r, (de)
-- LD A,(HL) 7E
-- LD B,(HL) 46
-- LD C,(HL) 4E
-- LD D,(HL) 56
-- LD E,(HL) 5E
-- LD H,(HL) 66
-- LD L,(HL) 6E
assert(dest >= 0 and dest <= 7, "_ld: unknown dest register")
emit_byte(0x46 + dest * 8)
else
-- ld r,r
assert(dest >= 0 and dest <= 7, "_ld: unknown dest register")
assert(src >= 0 and src <= 7, "_ld: unknown src register")
emit_byte(0x40 + dest * 8 + src)
end
end
local function _ld_const(r, value)
-- LD A,n 3E n
-- LD B,n 06 n
-- LD C,n 0E n
-- LD D,n 16 n
-- LD E,n 1E n
-- LD H,n 26 n
-- LD L,n 2E n
-- LD BC,nn 01 nn nn
-- LD DE,nn 11 nn nn
-- LD HL,nn 21 nn nn
-- LD IX,nn DD 21 nn nn
-- LD IY,nn FD 21 nn nn
if r >= 0 and r <= 7 then
list_line("ld %s,$%02x", reg_name[r], value)
else
list_line("ld %s,$%04x", reg_name[r], value)
end
if r == BC then
emit_byte(0x01)
emit_short(value)
elseif r == DE then
emit_byte(0x11)
emit_short(value)
elseif r == HL then
emit_byte(0x21)
emit_short(value)
elseif r == IX then
emit_byte(0xdd)
emit_byte(0x21)
emit_short(value)
elseif r == IY then
emit_byte(0xfd)
emit_byte(0x21)
emit_short(value)
elseif r >= 0 and r <= 7 then
emit_byte(0x06 + r * 8)
emit_byte(value)
else
error("_ld_const: unknown register")
end
end
local function _ld_fetch(r, addr)
-- LD A,(nn) 3A nn nn
-- LD BC,(nn) ED 4B nn nn
-- LD DE,(nn) ED 5B nn nn
-- LD HL,(nn) 2A nn nn
-- LD IX,(nn) DD 2A nn nn
-- LD IY,(nn) FD 2A nn nn
-- LD SP,(nn) ED 7B nn nn
list_line("ld %s,($%04x)", reg_name[r], addr)
if r == A then
emit_byte(0x3a)
elseif r == BC then
emit_byte(0xed)
emit_byte(0x4b)
elseif r == DE then
emit_byte(0xed)
emit_byte(0x5b)
elseif r == HL then
emit_byte(0x2a)
elseif r == IX then
emit_byte(0xdd)
emit_byte(0x2a)
elseif r == IY then
emit_byte(0xfd)
emit_byte(0x2a)
elseif r == SP then
emit_byte(0xed)
emit_byte(0x7b)
else
error("_ld_fetch: unknown register")
end
emit_short(addr)
end
local function _ld_store(addr, r)
-- LD (nn),A 32 nn nn
-- LD (nn),BC ED 43 nn nn
-- LD (nn),DE ED 53 nn nn
-- LD (nn),HL 22 nn nn
-- LD (nn),IX DD 22 nn nn
-- LD (nn),IY FD 22 nn nn
-- LD (nn),SP ED 73 nn nn
list_line("ld ($%04x),%s", addr, reg_name[r])
if r == A then
emit_byte(0x32)
elseif r == BC then
emit_byte(0xed)
emit_byte(0x43)
elseif r == DE then
emit_byte(0xed)
emit_byte(0x53)
elseif r == HL then
emit_byte(0x22)
elseif r == IX then
emit_byte(0xdd)
emit_byte(0x22)
elseif r == IY then
emit_byte(0xfd)
emit_byte(0x22)
elseif r == SP then
emit_byte(0xed)
emit_byte(0x73)
else
error("_ld_store: unknown register")
end
emit_short(addr)
end
local function _ld_store_offset_const(r, offset, value)
-- LD (IX+OFFSET),N DD 36 o n
-- LD (IY+OFFSET),N FD 36 o n
list_line("ld (%s+$%02x),$%02x", reg_name[r], offset, value)
if r == IX then
emit_byte(0xdd)
elseif r == IY then
emit_byte(0xfd)
else
error("_ld_store_offset_const: unknown register")
end
emit_byte(0x36)
emit_byte(offset)
emit_byte(value)
end
local function _exx()
list_line("exx")
emit_byte(0xd9)
end
local function _ex_de_hl()
list_line("ex de,hl")
emit_byte(0xeb)
end
local function _ex_af_af()
list_line("ex af,af'")
emit_byte(0x08)
end
local function _inc(r)
-- INC A 3C
-- INC B 04
-- INC C 0C
-- INC D 14
-- INC E 1C
-- INC H 24
-- INC BC 03
-- INC DE 13
-- INC HL 23
-- INC (HL) 34
list_line("inc %s", reg_name[r])
if r == BC then
emit_byte(0x03)
elseif r == DE then
emit_byte(0x13)
elseif r == HL then
emit_byte(0x23)
elseif r == HL_INDIRECT then
emit_byte(0x34)
elseif r >= 0 and r <= 7 then
emit_byte(0x04 + r * 8)
else
error("_dec: unknown register")
end
end
local function _dec(r)
-- DEC A 3D
-- DEC B 05
-- DEC C 0D
-- DEC D 15
-- DEC E 1D
-- DEC H 25
-- DEC BC 0B
-- DEC DE 1B
-- DEC HL 2B
-- DEC (HL) 35
list_line("dec %s", reg_name[r])
if r == BC then
emit_byte(0x0b)
elseif r == DE then
emit_byte(0x1b)
elseif r == HL then
emit_byte(0x2b)
elseif r == HL_INDIRECT then
emit_byte(0x35)
elseif r >= 0 and r <= 7 then
emit_byte(0x05 + r * 8)
else
error("_dec: unknown register")
end
end
local function _xor(r)
assert(r >= 0 and r <= 7, "_xor: unknown register")
list_line("xor %s", reg_name[r])
emit_byte(0xa8 + r)
end
local function _xor_const(n)
list_line("xor %d", n)
emit_byte(0xee)
emit_byte(n)
end
local function _and(r)
assert(r >= 0 and r <= 7, "_and: unknown register")
list_line("and %s", reg_name[r])
emit_byte(0xa0 + r)
end
local function _and_const(n)
list_line("and %d", n)
emit_byte(0xe6)
emit_byte(n)
end
local function _or(r)
assert(r >= 0 and r <= 7, "_or: unknown register")
list_line("or %s", reg_name[r])
emit_byte(0xb0 + r)
end
local function _or_const(n)
list_line("or %d", n)
emit_byte(0xf6)
emit_byte(n)
end
local function _ccf()
list_line("ccf")
emit_byte(0x3f)
end
local function _scf()
list_line("scf")
emit_byte(0x37)
end
local function _add(dest, src)
-- ADD HL,BC 09
-- ADD HL,DE 19
-- ADD HL,HL 29
-- ADD HL,SP 39
list_line("add %s,%s", reg_name[dest], reg_name[src])
if dest == HL then
if src == BC then
emit_byte(0x09)
elseif src == DE then
emit_byte(0x19)
elseif src == HL then
emit_byte(0x29)
elseif src == SP then
emit_byte(0x39)
else
error("_add: unknown src register")
end
elseif dest == A then
assert(src >= 0 and src <= 7, "_add: unknown src register")
emit_byte(0x80 + src)
else
error("_add: unknown operands")
end
end
local function _add_const(n)
list_line("add %d", n)
emit_byte(0xc6)
emit_byte(n)
end
local function _adc(dest, src)
-- ADC HL,BC ED 4A
-- ADC HL,DE ED 5A
-- ADC HL,HL ED 6A
-- ADC HL,SP ED 7A
list_line("adc %s,%s", reg_name[dest], reg_name[src])
if dest == HL then
if src == BC then
emit_byte(0xed)
emit_byte(0x4a)
elseif src == DE then
emit_byte(0xed)
emit_byte(0x5a)
elseif src == HL then
emit_byte(0xed)
emit_byte(0x6a)
elseif src == SP then
emit_byte(0xed)
emit_byte(0x7a)
else
error("_adc: unknown src register")
end
elseif dest == A then
assert(src >= 0 and src <= 7, "_adc: unknown src register")
emit_byte(0x88 + src)
else
error("_adc: unknown operands")
end
end
local function _sub(r)
assert(r >= 0 and r <= 7, "_sub: unknown register")
list_line("sub %s", reg_name[r])
emit_byte(0x90 + r)
end
local function _sub_const(n)
list_line("sub %d", n)
emit_byte(0xd6)
emit_byte(n)
end
local function _sbc(dest, src)
-- SBC HL,BC ED 42
-- SBC HL,DE ED 52
-- SBC HL,HL ED 62
-- SBC HL,SP ED 72
list_line("sbc %s,%s", reg_name[dest], reg_name[src])
if dest == HL then
if src == BC then
emit_byte(0xed)
emit_byte(0x42)
elseif src == DE then
emit_byte(0xed)
emit_byte(0x52)
elseif src == HL then
emit_byte(0xed)
emit_byte(0x62)
elseif src == SP then
emit_byte(0xed)
emit_byte(0x72)
else
error("_sbc: unknown src register")
end
elseif dest == A then
assert(src >= 0 and src <= 7, "_sbc: unknown src register")
emit_byte(0x98 + src)
else
error("_sbc: unknown operands")
end
end
local function _cp_const(n)
list_line("cp %d", n)
emit_byte(0xfe)
emit_byte(n)
end
local function _bit(i, r)
assert(r >= 0 and r <= 7, "_bit: unknown register")
list_line("bit %s,%s", i, reg_name[r])
emit_byte(0xcb)
emit_byte(0x40 + 8 * i + r)
end
local function _sla(r)
assert(r >= 0 and r <= 7, "_sla: unknown register")
list_line("sla %s", reg_name[r])
emit_byte(0xcb)
emit_byte(0x20 + r)
end
local function _sra(r)
assert(r >= 0 and r <= 7, "_sra: unknown register")
list_line("sra %s", reg_name[r])
emit_byte(0xcb)
emit_byte(0x28 + r)
end
local function _rl(r)
assert(r >= 0 and r <= 7, "_rl: unknown register")
list_line("rl %s", reg_name[r])
emit_byte(0xcb)
emit_byte(0x10 + r)
end
local function _rr(r)
assert(r >= 0 and r <= 7, "_rr: unknown register")
list_line("rr %s", reg_name[r])
emit_byte(0xcb)
emit_byte(0x18 + r)
end
local function _rla()
list_line("rla")
emit_byte(0x17)
end
local function _ldir()
list_line("ldir")
emit_byte(0xed)
emit_byte(0xb0)
end
local function _push(r)
-- PUSH AF F5
-- PUSH BC C5
-- PUSH DE D5
-- PUSH HL E5
-- PUSH IX DD E5
-- PUSH IY FD E5
list_line("push %s", reg_name[r])
if r == AF then
emit_byte(0xf5)
elseif r == BC then
emit_byte(0xc5)
elseif r == DE then
emit_byte(0xd5)
elseif r == HL then
emit_byte(0xe5)
elseif r == IX then
emit_byte(0xdd)
emit_byte(0xe5)
elseif r == IY then
emit_byte(0xfd)
emit_byte(0xe5)
else
error("_push: unknown register")
end
end
local function _pop(r)
-- POP AF F1
-- POP BC C1
-- POP DE D1
-- POP HL E1
-- POP IX DD E1
-- POP IY FD E1
list_line("pop %s", reg_name[r])
if r == AF then
emit_byte(0xf1)
elseif r == BC then
emit_byte(0xc1)
elseif r == DE then
emit_byte(0xd1)
elseif r == HL then
emit_byte(0xe1)
elseif r == IX then
emit_byte(0xdd)
emit_byte(0xe1)
elseif r == IY then
emit_byte(0xfd)
emit_byte(0xe1)
else
error("_pop: unknown register")
end
end
local function _call(addr)
list_line("call $%04x", addr)
emit_byte(0xcd)
emit_short(addr)
call_pos = here()
end
local function _ret()
list_line("ret")
emit_byte(0xc9)
end
local function _ret_c()
list_line("ret c")
emit_byte(0xd8)
end
local function _ret_nc()
list_line("ret nc")
emit_byte(0xd0)
end
local function _jp(addr)
list_line("jp $%04x", addr)
emit_byte(0xc3)
emit_short(addr)
end
local function _jp_z(addr)
list_line("jp z,$%04x", addr)
emit_byte(0xca)
emit_short(addr)
end
local function _jp_nz(addr)
list_line("jp nz,$%04x", addr)
emit_byte(0xc2)
emit_short(addr)
end
local function _jp_c(addr)
list_line("jp c,$%04x", addr)
emit_byte(0xda)
emit_short(addr)
end
local function _jp_nc(addr)
list_line("jp nc,$%04x", addr)
emit_byte(0xd2)
emit_short(addr)
end
local function _jp_m(addr)
list_line("jp m,$%04x", addr)
emit_byte(0xfa)
emit_short(addr)
end
local function _jp_p(addr)
list_line("jp p,$%04x", addr)
emit_byte(0xf2)
emit_short(addr)
end
local function _jp_indirect(r)
assert(r == HL or r == IX or r == IY, "_jp_indirect: unknown register")
list_line("jp (%s)", reg_name[r])
if r == HL then
emit_byte(0xe9) -- jp (hl)
elseif r == IX then
emit_byte(0xdd) -- jp (ix)
emit_byte(0xe9)
elseif r == IY then
emit_byte(0xfd) -- jp (iy)
emit_byte(0xe9)
end
end
local function _di()
list_line("di")
emit_byte(0xf3)
end
local function _ei()
list_line("ei")
emit_byte(0xfb)
end
local function offset_to_absolute(offset)
if offset > 127 then offset = offset - 256 end
return here() + offset + 2
end
local function _jr(offset)
list_line("jr $%04x", offset_to_absolute(offset))
emit_byte(0x18)
emit_byte(offset)
end
local function _jr_z(offset)
list_line("jr z,$%04x", offset_to_absolute(offset))
emit_byte(0x28)
emit_byte(offset)
end
local function _jr_nz(offset)
list_line("jr nz,$%04x", offset_to_absolute(offset))
emit_byte(0x20)
emit_byte(offset)
end
local function _jr_c(offset)
list_line("jr c,$%04x", offset_to_absolute(offset))
emit_byte(0x38)
emit_byte(offset)
end
local function _jr_nc(offset)
list_line("jr nc,$%04x", offset_to_absolute(offset))
emit_byte(0x30)
emit_byte(offset)
end
local function _in(r, port)
-- IN A,(C) ED 78
-- IN B,(C) ED 40
-- IN C,(C) ED 48
-- IN D,(C) ED 50
-- IN E,(C) ED 58
-- IN H,(C) ED 60
-- IN L,(C) ED 68
-- IN F,(C) ED 70 not implemented!
assert(port == C, "_in: invalid port")
assert(r >= 0 and r <= 7, "_in: unknown register")
list_line("in %s,(%s)", reg_name[r], reg_name[port])
emit_byte(0xed)
emit_byte(0x40 + r * 8)
end
local function _in_const(r, port_addr)
assert(r == A, "_in_const: invalid register")
assert(port_addr >= 0 and port_addr <= 255, "_in_const: invalid port")
list_line("in a,($%02x)", port_addr)
emit_byte(0xdb)
emit_byte(port_addr)
end
local function _out(port, r)
-- OUT (C),A ED 79
-- OUT (C),B ED 41
-- OUT (C),C ED 49
-- OUT (C),D ED 51
-- OUT (C),E ED 59
-- OUT (C),H ED 61
-- OUT (C),L ED 69
assert(port == C, "_out: invalid port")
assert(r >= 0 and r <= 7, "_out: unknown register")
list_line("out (%s),%s", reg_name[port], reg_name[r])
emit_byte(0xed)
emit_byte(0x41 + r * 8)
end
local function _out_const(port_addr, r)
assert(r == A, "_out_const: invalid register")
assert(port_addr >= 0 and port_addr <= 255, "_out_const: invalid port")
list_line("out ($%02x),a", port_addr)
emit_byte(0xd3)
emit_byte(port_addr)
end
local function _rst(i)
assert(i >= 0 and i <= 0x38 and (i & 7) == 0, "invalid reset vector")
list_line("rst %s", i)
emit_byte(0xc7 + i)
end
-- Pushes DE on Forth stack, trashes HL.
local function stk_push_de()
_rst(16)
end
-- Pops value from Forth stack and puts it in DE register, trashes HL.
local function stk_pop_de()
_rst(24)
end
-- Pops value from Forth stack and puts it in BC register.
local function stk_pop_bc()
_call(0x084e)
end
local function stk_push_de_inline()
_ld_fetch(HL, SPARE)
_ld(HL_INDIRECT, E)
_inc(HL)
_ld(HL_INDIRECT, D)
_inc(HL)
_ld_store(SPARE, HL)
end
local function stk_pop_de_inline()
_ld_fetch(HL, SPARE)
_dec(HL)
_ld(D, HL_INDIRECT)
_dec(HL)
_ld(E, HL_INDIRECT)
_ld_store(SPARE, HL)
end
local function stk_pop_bc_inline()
_ld_fetch(HL, SPARE)
_dec(HL)
_ld(B, HL_INDIRECT)
_dec(HL)
_ld(C, HL_INDIRECT)
_ld_store(SPARE, HL)
end
local function branch_offset(jump_to_addr, instr_addr)
instr_addr = instr_addr or here()
local offset = jump_to_addr - instr_addr - 2
if offset < -128 or offset > 127 then return end -- branch too long
if offset < 0 then offset = offset + 256 end
return offset
end
-- Emits unconditional jump to <addr>.
local function jump(addr)
local offset = branch_offset(addr)
if offset then
_jr(offset)
else
_jp(addr)
end
end
-- Emits conditional jump which causes a jump to <target> if Z flag is set.
local function jump_z(addr)
local offset = branch_offset(addr)
if offset then
_jr_z(offset)
else
_jp_z(addr)
end
end
-- Emits conditional jump which causes a jump to <target> if C flag is set.
local function jump_c(addr)
local offset = branch_offset(addr)
if offset then
_jr_c(offset)
else
_jp_c(addr)
end
end
-- Emits conditional jump which causes a jump to <target> if C flag is clear.
local function jump_nc(addr)
local offset = branch_offset(addr)
if offset then
_jr_nc(offset)
else
_jp_nc(addr)
end
end
-- Patch already emitted jump instruction to jump to a new address.
-- Returns false if the jump could not be patched (branch too long).
local function patch_jump(instr_addr, jump_to_addr)
local opcode = read_byte(instr_addr)
if opcode < 0x80 then
-- relative jump
local offset = branch_offset(jump_to_addr, instr_addr)
if offset == nil then return false end
write_byte(instr_addr + 1, offset)
else
-- absolute jump
write_short(instr_addr + 1, jump_to_addr)
end
list_patch(instr_addr, "%$%x+", string.format("$%04x", jump_to_addr))
jump_targets[jump_to_addr] = true
return true
end
local function record_long_jump(ppos)
if not long_jumps[ppos] then
verbose("Deoptimizing branch to jump (%s)", ppos)
long_jumps[ppos] = true
more_work = true
end
end
local function z80_decode(code, i)
local node = decode_tree
assert(node)
local immediate
local offset
while true do
if type(node) == "string" then break end
local byte = assert(code[i])
i = i + 1
if node.n or node.nn then
if immediate == nil then
immediate = byte
else
immediate = immediate | (byte << 8)
end
node = node.n or node.nn
elseif node.o then
offset = byte
if offset > 127 then offset = offset - 256 end
node = node.o
else
node = assert(node[byte])
end
end
local instr = node
if immediate then
instr = instr:gsub("nn", string.format("$%04x", immediate))
instr = instr:gsub("n", string.format("$%02x", immediate))
end
if offset then
instr = instr:gsub("o", offset)
end
return instr, immediate, offset, i
end
-- Relocates machine code to start at new address.
-- 'code' is an array of bytes,
-- 'list' is listing lines for that code (using same indices as code!)
-- For example, list[3] contains the listing line for the instruction at code[3].
local function relocate_mcode(code, list, old_start_addr, new_start_addr)
local rel_code = {}
local rel_list = {}
local abs_jumps = {
[0xc3] = "jp nn",
[0xda] = "jp c,nn",
[0xfa] = "jp m,nn",
[0xd2] = "jp nc,nn",
[0xc2] = "jp nz,nn",
[0xf2] = "jp p,nn",
[0xea] = "jp pe,nn",
[0xe2] = "jp po,nn",
[0xca] = "jp z,nn",
}
local rel_jumps = {
[0x18] = "jr o",
[0x38] = "jr c,o",
[0x30] = "jr nc,o",
[0x20] = "jr nz,o",
[0x28] = "jr z,o",
}
local i = 1
while i <= #code do
local s = i -- start of instruction
local instr, immediate, offset, e = z80_decode(code, i)
for j = 1, e - s do
rel_code[i] = code[i]
rel_list[i] = list[i]
i = i + 1
end
local opcode = code[s] -- only valid for instructions without prefix bytes!
-- relocate absolute jumps
if abs_jumps[opcode] then
local jump_to_addr = immediate
local new_addr = jump_to_addr - old_start_addr + new_start_addr
rel_code[s + 1] = new_addr & 0xff
rel_code[s + 2] = new_addr >> 8
rel_list[s] = abs_jumps[opcode]:gsub("nn", string.format("$%04x", new_addr))
end
-- relocate relative jumps (listing file only)
if rel_jumps[opcode] then
local jump_to_addr = new_start_addr + s + offset + 1
rel_list[s] = rel_jumps[opcode]:gsub("o", string.format("$%04x", jump_to_addr))
end
-- skip over embedded strings
if opcode == 0xcd and immediate == compilation_addresses["__print"] then
local len = code[i] | (code[i + 1] << 8)
for j = 1, len + 2 do
rel_code[i] = code[i]
rel_list[i] = list[i]
i = i + 1
end
end
-- skip over embedded Forth
-- NOTE: This is pretty limited. We don't support jumping inside Forth code, for example.
-- This should be fine because the mcode compiler only generates calls to Forth words.
if opcode == 0xcd and immediate == 0x04b9 then
-- copy bytes until Forth code end marker 0x1a0e is encountered
-- every forth call address is 16-bit so we do two bytes per loop
while true do
local forth_end = (code[i] | (code[i + 1] << 8)) == 0x1a0e
rel_code[i] = code[i]
rel_list[i] = list[i]
i = i + 1
rel_code[i] = code[i]
rel_list[i] = list[i]
i = i + 1
if forth_end then break end
end
end
end
return rel_code, rel_list
end