-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlua_z80.lua
More file actions
executable file
·2450 lines (2167 loc) · 100 KB
/
lua_z80.lua
File metadata and controls
executable file
·2450 lines (2167 loc) · 100 KB
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
-- The main Z80 to Lua JIT compiling emulator.
-- (c) Copyright 2013 Rob Probin.
--
-- This program is free software; you can redistribute it and/or
-- modify it under the terms of the GNU General Public License
-- as published by the Free Software Foundation; either version 2
-- of the License, or (at your option) any later version.
--
-- This program is distributed in the hope that it will be useful,
-- but WITHOUT ANY WARRANTY; without even the implied warranty of
-- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
-- GNU General Public License for more details.
--
-- You should have received a copy of the GNU General Public License
-- along with this program; if not, write to the Free Software
-- Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
--
-- http://robprobin.com
-- https://github.com/robzed/LuaZ80
--
-- LICENSE NOTES
-- =============
-- Since this is Lua code (that generates more Lua code, so is pretty dependant
-- on a Lua interpreter), the requirement in the GPL to share does not seem
-- overly onerous/burdensome/difficult because you'll be distributing the Lua code
-- along with any product anyway. I considered MIT, ZLib, BSD, Apache licenses as
-- well but the GPL appeared to 'encourage' sharing.
--
-- I'm quite willing to consider a different license if you have a specific
-- use-case that would benefit - even if part of the (Lua or other) source
-- would be closed or licensed under a non-open source license.
--
--
-- REQUIREMENTS
-- ============
-- Requires Lua 5.2 because we use bit operations. Other changes 5.1/5.2 changes
-- might be documented as 5.2 in this file.
--
-- Also goto is used to implement branches - that might be more 'serious' for
-- backporting to Lua 5.1.
--
-- FUTURE ACTIONS
-- ==============
-- High Priority
-- @todo: Complete memory invalidation
-- @todo: test memory invalidation
--
-- Medium Priority
-- @todo: Test by booting ZX81 or Spectrum ROM and showing basic screen and key input
-- @todo: Complete instructions (make a list of those!!)
-- @todo: test instructions (make a list and test actions & flags)
--
-- Low Priority
-- @todo: defer inc/dec flag operations?
-- @todo: quicker to defer all flag operations - or not??
-- @todo: is it possible to defer other operations?
-- @todo: work out other optimisations of generated code or compiler itself
-- @todo: Check jumps into middle of instruction inside instruction
-- @todo: Add proper cycle counting
-- @todo: Fix neg flag problems? (Most emulators don't do it right...)
-- @todo: Add undocumented instructions?
-- @todo: For all CALL instructions, check if push to SP that overwrites destination address will affect current call... (zxsp assumes not)
-- @todo: Is there a way to for JP (HL) and RET not to drop out of lump?
-- @todo: Add FD CB ff nn to lua_z80_test()
-- @todo: Add IX & IY instructions to disassembler
-- @todo: Can we avoid compiling local data as instructions? (and therefore get
-- better alignement of instructions into a lump) Can we avoid
-- invalidating a block based on a write to data rather than instructions?
-- @todo: Implement interrupts in Z80 land (& I register)
-- @todo: Implement refresh register
-- @todo: Can we identify data in or immediately after code that will cause
-- lump reload, especially if it has a JR or JP immediately before
-- during compile (rather than execution because execution primatives
-- are costly)? Maybe even just stop lump compilation if JP/JR is
-- backwards and the lump doesnt have a label right after.
-- Also can we post-identify data as not self-modifying code?
-- @todo: What if the whole memory is NOPs? Does the interpreter hang?
-- @todo: Fix flag bits 5 and 3
------------------------------------------------------------------------------------
-- NOTES: code_write_check and write_allowed go hand in hand. If an instruction doesn't
-- do a write_allowed first, then code_write_check won't help
require("third_party/strict")
require("third_party/middleclass")
require("z80_ss_debug") -- only if debug enabled below... (search for 'uncomment for debug')
require("Z80_disassembler")
local Z80_SOURCE_DEBUG = true
-- bit masks for z80 flag register:
Z80_S_FLAG = 0x80 -- sign flag (duplicates bit 7 of A) ... 1 = negative
Z80_Z_FLAG = 0x40 -- zero (or equal) flag ... 1 = Zero
Z80_FLAG_6 = 0x20 -- duplicates bit 6 of A
Z80_H_FLAG = 0x10 -- half carry flag ... 1 = Carry of lower nibble
Z80_FLAG_3 = 0x08 -- duplicates bit 3 of A
Z80_P_FLAG = 0x04 -- parity/overflow flag ... 1 = Parity Even, 0=Parity Odd
Z80_V_FLAG = 0x04 -- parity/overflow flag ... 1 = Overflow between sign/unsigned
Z80_N_FLAG = 0x02 -- negate flag ... 1 = previous instruction was a substract
Z80_C_FLAG = 0x01 -- carry flag = 1 = Carry
-- Basic CPU. Note, this does not contain any memory...
--
-- References:
-- - https://en.wikipedia.org/wiki/Zilog_Z80
Z80CPU = class("Z80CPU")
function Z80CPU:initialize()
-- See http://www.cpu-world.com/Arch/Z80.html
-- The Z80 is a little endian machine
--
-- 16-bit registers
self.PC = 0
self.IX = 0 -- remember there is IXH and IXL as well
self.IY = 0 -- remember there is IYH and IYL as well
self.SP = 0xFFFF
self.I = 0
-- R refresh register..
-- 2 modes,
-- 1. random 7-bit value
-- 2. accurate emulation, involving emulation off all R updates in all instructions
self.R = 0
self._interrupt_mode = 0 -- look at IMFa/IMFb
-- main registers
self.A = 0xFF
self._F = 0xFF -- might only be updated only when requested (if nil). call :get_F()
self.H = 0
self.L = 0
self.B = 0
self.C = 0
self.D = 0
self.E = 0
-- Flags
self.Carry = self._F % 2 -- duplicate/master copy of carry flag in F register. Not a boolean, but 0 or 1.
-- Carry must always be maintained with F!!!
-- cycle counting not implemented yet. However, don't add a number on every
-- instruction, instead accumulate them up during compile and add them on
-- before a jump instruction, where we can replace 'jit.jump_count' with
-- instruction count limit.
self.cycles = 0
-- alternative 'shadow' registers
self.A_ = 0xFF
self.F_ = 0xFF -- never deferred
self.H_ = 0
self.L_ = 0
self.B_ = 0
self.C_ = 0
self.D_ = 0
self.E_ = 0
-- interrupt flags
self.IFF1 = false -- this on effects maskable interrupts
self.IFF2 = false -- this one is readible (ld a,i/ld a,r -> P/Ov)
-- interrupt mode flip flops
-- IMFa=0, IMFb=0 - Interrupt mode 0. This is an 8080-compatible interrupt mode.
-- IMFa=0, IMFb=1 - Not used.
-- IMFa=1, IMFb=0 - Interrupt mode 1. In this mode CPU jumps to location 0038h for interrupt processing.
-- IMFa=1, IMFb=1 - Interrupt mode 2. In this mode CPU jumps to location, which high-order address is taken from I register, and low order address is supplied by peripheral device.
self.IMFa = false
self.IMFb = false
-- these are used when flag setting is deferred
self._oldA = 0
self._otherOp = 0
self._newA = 0
self._subtract = false
self._inputs = {}
self._outputs = {}
self.simple_flags = {}
for A = 0,255 do
self.simple_flags[A] = bit32.band(A,0xa8) -- create the sign, bit5, bit3
if A == 0 then
self.simple_flags[A] = self.simple_flags[A] + Z80_Z_FLAG
end
end
end
function Z80CPU:register_input(address_mask, address_state, target_function, target_userdata)
local io = { mask=address_mask, state=address_state, f=target_function, ud=target_userdata }
table.insert(self._inputs, io)
end
function Z80CPU:register_output(address_mask, address_state, target_function, target_userdata)
local io = { mask=address_mask, state=address_state, f=target_function, ud=target_userdata }
table.insert(self._outputs, io)
end
-- DAA badly documented in Z80 UM & Zaks? Yep!
-- http://stackoverflow.com/questions/8119577/z80-daa-instruction
-- http://z80-heaven.wikidot.com/instructions-set:daa
-- http://datasheets.chipdb.org/Zilog/Z80/z80-documented-0.90.pdf
function Z80CPU:DAA(zflags)
-- @todo: look for more efficent ways of implementing this, e.g. table lookup
local flags = self:get_F()
local result = self.A
local half_flag_result = 0
local offset = 0
local lower = bit32.band(result, 0x0F)
if lower > 9 or bit32.btest(flags, Z80_H_FLAG) then
offset = 6
end
if result > 0x99 or self.Carry == 1 then
offset = offset + 0x60
end
-- subtract instructions
if bit32.btest(flags, Z80_N_FLAG) then
if lower < bit32.band(0xF, offset) then
half_flag_result = Z80_H_FLAG
end
result = result - offset
if result < 0 then
self.Carry=1 result=result+256
-- carry is never reset!
--else
-- self.Carry=0
end
else -- add instructions
if lower > 9 then
half_flag_result = Z80_H_FLAG
end
result = result + offset
if result > 255 then
self.Carry=1 result=result-256
-- carry is never reset!
--else
-- self.Carry=0
end
end
self._F = zflags[result] + self.Carry + half_flag_result + bit32.band(flags, Z80_N_FLAG)
self.A = result
end
-- Flags are complex
-- http://rk.nvg.ntnu.no/sinclair/faq/tech_z80.html#RREG
-- http://www.z80.info/z80sflag.htm
--
local function calc_add_overflow(oldA,op2,newA)
--if (~(ra^R)&(wml^ra)&0x80 ~= 0 then V_FLAG end
if bit32.band(bit32.bnot(bit32.bxor(oldA, op2)), bit32.bxor(newA,oldA), 0x80) ~= 0 then
return Z80_V_FLAG
end
return 0
end
Z80CPU.calc_add_overflow = calc_add_overflow
local function calc_sub_overflow(oldA,op2,newA)
--if ((ra^R)&(wml^ra)&0x80 ~= 0 then V_FLAG end
if bit32.band(bit32.bxor(oldA, op2), bit32.bxor(newA,oldA), 0x80) ~= 0 then
return Z80_V_FLAG
end
return 0
end
Z80CPU.calc_sub_overflow = calc_sub_overflow
local function calc_half_carry(oldA,op2,newA)
-- ((ra^R^wml)&H_FLAG)
--return "bit32.band(bit32.bxor(CPU.A, "..r..", result),Z80_H_FLAG)"
return bit32.band(bit32.bxor(oldA, op2, newA),Z80_H_FLAG)
end
function Z80CPU:get_F()
local F = self._F
if not F then
local oldA = self._oldA
local op2 = self._otherOp
local newA = self._newA
F = self.simple_flags[newA] + calc_half_carry(oldA,op2,newA) +
self.Carry
if self._subtract then
F = F + Z80_N_FLAG + calc_sub_overflow(oldA,op2,newA)
else
F = F + calc_add_overflow(oldA,op2,newA)
end
self._F = F -- don't calculate twice
end
return F
end
local SZV_flag_mask = Z80_S_FLAG + Z80_V_FLAG + Z80_Z_FLAG
function Z80CPU:get_F_only_SZV()
local F = self._F
if not F then
local oldA = self._oldA
local op2 = self._otherOp
local newA = self._newA
F = self.simple_flags[newA]
if self._subtract then
F = F + calc_sub_overflow(oldA,op2,newA)
else
F = F + calc_add_overflow(oldA,op2,newA)
end
end
F = bit32.band(F, SZV_flag_mask)
self._F = F
return F
end
function Z80CPU:__serialize()
return self
end
-- A lump is the container for a block of code that represents a compiled Lia set
-- of instructions for a current set of memory addresses. It also contains self-write
-- detection in order to invalidate the lump.
Z80Lump = class("Z80Lump")
function Z80Lump:initialize(start_address)
-- other
--self.code = nil -- no code to start with
--self.error = nil -- no error to start with
--self.write_detect_to_me = {}
self.start_address = start_address
--self.deleted = false
end
function Z80Lump:__serialize()
return self
end
----------------------------------------------------------------------------
local function inc_address(address)
-- maybe we should use return (address+1)%65536 ??
address = address + 1
if address >= 65536 then
address = address - 65536
end
return address
end
-- (HL) becomes lua.memory[CPU.H*256+CPU.L]
local reg_index = {[0]="CPU.B","CPU.C","CPU.D","CPU.E","CPU.H","CPU.L","memory[CPU.H*256+CPU.L]","CPU.A"}
local function _is_single_reg(reg)
return #reg == 5 -- simple, but effective
end
-- CB = rotates, shifts, bit set/reset/testing
local decode_CB_instructions = {
}
-- BIT n,r
-- *Z513*0- PV as Z, S set only if n=7 and b7 of r set
-- Behaves much like AND r,2^n, except Carry is unchanged.
local function BIT_string(bitmask, what)
return string.format("CPU._F = zflags[bit32.band(%s, %s)]+Z80_H_FLAG+CPU.Carry", bitmask, what)
end
-- populate
-- SET b, r
-- RES b, r
for reg = 0, 7 do
local reg_string = reg_index[reg] -- not speed critical
if _is_single_reg(reg_string) then
-- no memory write check for single reg write
-- RLC r ... bit 7 to carry and bit 0
decode_CB_instructions[0x00 + reg] = string.format(
"CPU:get_F_only_SZV() result=%s*2 if result>255 then result=result-255 CPU.Carry=1 else CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry %s=result",
reg_string, reg_string)
-- RRC r ... bit 0 to carry and bit 7
decode_CB_instructions[0x08 + reg] = string.format(
"CPU:get_F_only_SZV() result=%s if (result %% 2) == 1 then result=(result+255)/2 CPU.Carry=1 else result=result/2 CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry %s=result",
reg_string, reg_string)
-- RL r
decode_CB_instructions[0x10 + reg] = string.format(
"CPU:get_F_only_SZV() result=%s*2+CPU.Carry if result>255 then result=result-256 CPU.Carry=1 else CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry %s=result",
reg_string, reg_string)
-- RR r
decode_CB_instructions[0x18 + reg] = string.format(
"CPU:get_F_only_SZV() result=%s temp=result%%2 result=(result-temp)/2+CPU.Carry*128 CPU.Carry=temp CPU._F=zflags[result]+CPU.Carry %s=result",
reg_string, reg_string)
-- SLA r ... bit 7 to carry
decode_CB_instructions[0x20 + reg] = string.format(
"CPU:get_F_only_SZV() result=%s*2 if result>255 then result=result-256 CPU.Carry=1 else CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry %s=result",
reg_string, reg_string)
-- SRA r
decode_CB_instructions[0x28 + reg] = string.format(
"CPU:get_F_only_SZV() result=%s CPU.Carry=result%%2 result=((result-CPU.Carry)/2)+bit32.band(result,128) CPU._F=zflags[result]+CPU.Carry %s=result",
reg_string, reg_string)
-- SLS r ... bit 7 to carry, set bit 0
decode_CB_instructions[0x30 + reg] = string.format(
"CPU:get_F_only_SZV() result=(%s*2)+1 if result>255 then result=result-256 CPU.Carry=1 else CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry %s=result",
reg_string, reg_string)
-- SRL r
decode_CB_instructions[0x38 + reg] = string.format(
"CPU:get_F_only_SZV() result=%s CPU.Carry=result%%2 result=((result-CPU.Carry)/2) CPU._F=zflags[result]+CPU.Carry %s=result",
reg_string, reg_string)
else
-- RLC (HL) ... bit 7 to carry and bit 0
decode_CB_instructions[0x00 + reg] = function(memory, iaddr) return string.format(
[[CPU:get_F_only_SZV() addr=CPU.H*256+CPU.L;result=memory[addr]*2 if result>255 then result=result-255 CPU.Carry=1 else CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry
if jit.write_allowed[addr] then memory[addr]=result if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], iaddr), iaddr end
-- RRC (HL) ... bit 7 to carry and bit 0
decode_CB_instructions[0x08 + reg] = function(memory, iaddr) return string.format(
[[CPU:get_F_only_SZV() addr=CPU.H*256+CPU.L; result=memory[addr] if (result%%2)==1 then result=(result+255)/2 CPU.Carry=1 else result=result/2 CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry
if jit.write_allowed[addr] then memory[addr]=result if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], iaddr), iaddr end
-- RL (HL) ... bit 7 to carry and bit 0
decode_CB_instructions[0x10 + reg] = function(memory, iaddr) return string.format(
[[CPU:get_F_only_SZV() addr=CPU.H*256+CPU.L; result=memory[addr]*2+CPU.Carry if result>255 then result=result-256 CPU.Carry=1 else CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry
if jit.write_allowed[addr] then memory[addr]=result if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], iaddr), iaddr end
-- RR (HL) ... bit 0 to carry and bit 7
decode_CB_instructions[0x18 + reg] = function(memory, iaddr) return string.format(
[[CPU:get_F_only_SZV() addr=CPU.H*256+CPU.L; result=memory[addr] temp=result%%2 result=(result-temp)/2+CPU.Carry*128 CPU.Carry=temp CPU._F=zflags[result]+CPU.Carry
if jit.write_allowed[addr] then memory[addr]=result if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], iaddr), iaddr end
-- SLA (HL) ... bit 7 to carry
decode_CB_instructions[0x20 + reg] = function(memory, iaddr) return string.format(
[[CPU:get_F_only_SZV() addr=CPU.H*256+CPU.L;result=memory[addr]*2 if result>255 then result=result-256 CPU.Carry=1 else CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry
if jit.write_allowed[addr] then memory[addr]=result if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], iaddr), iaddr end
-- SRA (HL) ... bit 0 to carry and bit 7
decode_CB_instructions[0x28 + reg] = function(memory, iaddr) return string.format(
[[CPU:get_F_only_SZV() addr=CPU.H*256+CPU.L; result=memory[addr] CPU.Carry=result%%2 result=((result-CPU.Carry)/2)+bit32.band(result,128) CPU._F=zflags[result]+CPU.Carry
if jit.write_allowed[addr] then memory[addr]=result if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], iaddr), iaddr end
-- SLS (HL) ... bit 7 to carry, set bit 0
decode_CB_instructions[0x30 + reg] = function(memory, iaddr) return string.format(
[[CPU:get_F_only_SZV() addr=CPU.H*256+CPU.L;result=memory[addr]*2+1 if result>255 then result=result-256 CPU.Carry=1 else CPU.Carry=0 end CPU._F=zflags[result]+CPU.Carry
if jit.write_allowed[addr] then memory[addr]=result if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], iaddr), iaddr end
-- SRL (HL) ... bit 0 to carry and bit 7
decode_CB_instructions[0x38 + reg] = function(memory, iaddr) return string.format(
[[CPU:get_F_only_SZV() addr=CPU.H*256+CPU.L; result=memory[addr] CPU.Carry=result%%2 result=((result-CPU.Carry)/2) CPU._F=zflags[result]+CPU.Carry
if jit.write_allowed[addr] then memory[addr]=result if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], iaddr), iaddr end
end
-- bit ops
for bit = 0, 7 do
local bitmask = 2 ^ bit
local invbitmask = 255 - bitmask
if _is_single_reg(reg_string) then
-- no memory write check for single reg write
-- SET b, r
decode_CB_instructions[0xC0 + 8* bit + reg] = string.format("%s=bit32.bor(%s, %s)",reg_string, reg_string, bitmask)
-- RES b, r
decode_CB_instructions[0x80 + 8* bit + reg] = string.format("%s=bit32.band(%s, %s)",reg_string, reg_string, invbitmask)
-- BIT b, r
decode_CB_instructions[0x40 + 8* bit + reg] = BIT_string(bitmask, reg_string)
else
-- SET n,(HL)
decode_CB_instructions[0xC0 + 8* bit + reg] = function(memory, iaddr)
return string.format([[addr = CPU.H*256+CPU.L;if jit.write_allowed[addr] then memory[addr]=bit32.bor(memory[addr], %s)
if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], bitmask, iaddr), iaddr
end
-- RES n,(HL)
decode_CB_instructions[0x80 + 8* bit + reg] = function(memory, iaddr)
return string.format([[addr = CPU.H*256+CPU.L;if jit.write_allowed[addr] then memory[addr]=bit32.band(memory[addr], %s)
if jit:code_write_check(addr) then CPU.PC = 0x%x; return 'invalidate' end end]], invbitmask, iaddr), iaddr
end
-- BIT b, (HL)
decode_CB_instructions[0x40 + 8* bit + reg] = BIT_string(bitmask, reg_string)
end
end
end
local function write_2bytes_to_address_command_string(source1, source2, dest_address1, dest_address2, next_pc, pre_code)
pre_code = pre_code or ""
return string.format(
-- we only do write_checks for invalid if the write was allowed.
-- we return after both writes have been attempted.
[[%s addr=%s if jit.write_allowed[addr] then memory[addr]=%s
if jit:code_write_check(addr) then addr = nil end end
addr2=%s if jit.write_allowed[addr2] then memory[addr2]=%s
if jit:code_write_check(addr2) then addr = nil end end
if addr == nil then CPU.PC = 0x%x return 'invalidate' end ]],
pre_code, dest_address1, source1, dest_address2, source2, next_pc)
end
local function write_to_address_command_string(source, dest_address, next_pc, optional_flag_check_code)
-- e.g. dest_address = CPU.H*256+CPU.L
-- e.g. source = CPU.A
optional_flag_check_code = optional_flag_check_code or ""
return string.format(
[[ addr=%s %sif jit.write_allowed[addr] then memory[addr]=%s
if jit:code_write_check(addr) then CPU.PC = 0x%x return 'invalidate' end end]],
dest_address, optional_flag_check_code, source, next_pc)
end
local inc_flag_calc = [[ CPU._F = CPU.simple_flags[%s]+CPU.Carry
if %s %% 0x10 == 0 then CPU._F = CPU._F + Z80_H_FLAG end
if %s == 0x80 then CPU._F = CPU._F + Z80_V_FLAG end ]]
local dec_flag_calc = [[ CPU._F = CPU.simple_flags[%s] + CPU.Carry + Z80_N_FLAG
if %s %% 0x10 == 0x0F then CPU._F = CPU._F + Z80_H_FLAG end
if %s == 0x7F then CPU._F = CPU._F + Z80_V_FLAG end ]]
-- DD = IX register
local decode_DD_instructions = {
[0x21] = function(memory, iaddr) local byte1 = memory[iaddr];iaddr = inc_address(iaddr);return string.format("CPU.IX=%s", memory[iaddr]*256+byte1), inc_address(iaddr) end,
-- 22 = LD (xxxx), IX
[0x22] = function(memory, iaddr)
local addr = memory[iaddr]; iaddr = inc_address(iaddr);
addr = addr+256*memory[iaddr]; iaddr = inc_address(iaddr);
return write_2bytes_to_address_command_string("CPU.IX%256", "bit32.band(CPU.IX, 0xFF00)/256", string.format("0x%x", addr), string.format("0x%x", (addr+1)%65536), iaddr), iaddr
end,
-- "INC IX"
[0x23] = "CPU.IX = (CPU.IX+1) % 65536",
[0x26] = function(memory, iaddr) local byte1 = memory[iaddr];iaddr = inc_address(iaddr);return string.format("CPU.IX=(CPU.IX%%256)+%s", 256*byte1), iaddr end,
-- 2A = LD IX,(xxxx)
[0x2A] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr); local byte2 = memory[iaddr]; iaddr = inc_address(iaddr)
local target = byte1 + 256*byte2
return string.format(
"CPU.IX = memory[0x%x]+256*memory[0x%x]", target, (target+1)%65536 ), iaddr
end,
--"DEC IX"
[0x2B] = "CPU.IX = (CPU.IX-1) % 65536",
[0x2E] = function(memory, iaddr) local byte1 = memory[iaddr];iaddr = inc_address(iaddr);return string.format("CPU.IX=bit32.band(0xFF00, CPU.IX)+%s", byte1), iaddr end,
[0x34] = function(memory, iaddr) -- inc(IX+d)
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
return write_to_address_command_string("result", string.format("(CPU.IX+%s)%%65536", byte1),
iaddr,
"result = (memory[addr]+1)%256 " ..
string.format(inc_flag_calc, "result", "result", "result")), iaddr
end,
[0x35] = function(memory, iaddr) -- dec(IX+d)
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
return write_to_address_command_string("result", string.format("(CPU.IX+%s)%%65536", byte1),
iaddr,
"result = (memory[addr]-1)%256" ..
string.format(dec_flag_calc, "result", "result", "result")), iaddr
end,
[0x36] = --ld (IX+d), &00
function(memory, iaddr)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
local data_byte = memory[iaddr]; iaddr = inc_address(iaddr);
return write_to_address_command_string(data_byte, string.format("(CPU.IX+%s)%%65536", displacement_byte), iaddr), iaddr
end,
[0x44] = "CPU.B=bit32.band(CPU.IX, 0xFF00)/256",
[0x45] = "CPU.B=CPU.IX%256",
[0x46] = function(memory, iaddr) -- LD B,(IX+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.B=memory[(CPU.IX+%s)%%65536]", displacement_byte), iaddr
end,
[0x4C] = "CPU.C=bit32.band(CPU.IX, 0xFF00)/256",
[0x4D] = "CPU.C=CPU.IX%256",
[0x4E] = function(memory, iaddr) -- LD C,(IX+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.C=memory[(CPU.IX+%s)%%65536]", displacement_byte), iaddr
end,
[0x54] = "CPU.D=bit32.band(CPU.IX, 0xFF00)/256",
[0x55] = "CPU.D=CPU.IX%256",
[0x56] = function(memory, iaddr) -- LD D,(IX+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.D=memory[(CPU.IX+%s)%%65536]", displacement_byte), iaddr
end,
[0x5C] = "CPU.E=bit32.band(CPU.IX, 0xFF00)/256",
[0x5D] = "CPU.E=CPU.IX%256",
[0x5E] = function(memory, iaddr) -- LD E,(IX+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.E=memory[(CPU.IX+%s)%%65536]", displacement_byte), iaddr
end,
[0x60] = "CPU.IX=CPU.IX%256+CPU.B*256", -- LD IXH, B
[0x61] = "CPU.IX=CPU.IX%256+CPU.C*256", -- LD IXH, C
[0x62] = "CPU.IX=CPU.IX%256+CPU.D*256", -- LD IXH, D
[0x63] = "CPU.IX=CPU.IX%256+CPU.E*256", -- LD IXH, E
[0x64] = "", -- LD IXH, IXH
[0x65] = "CPU.IX=(CPU.IX%256)*0x101",
[0x66] = function(memory, iaddr) -- LD H,(IX+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.H=memory[(CPU.IX+%s)%%65536]", displacement_byte), iaddr
end,
[0x67] = "CPU.IX=CPU.IX%256+CPU.A*256", -- LD IXH, A
[0x6C] = "temp=bit32.band(CPU.IX, 0xFF00) CPU.IX=temp/256+temp",
[0x6D] = "", -- LD IXL, IXL
[0x6E] = function(memory, iaddr) -- LD L,(IX+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.L=memory[(CPU.IX+%s)%%65536]", displacement_byte), iaddr
end,
[0x6F] = "CPU.IX=bit32.band(CPU.IX, 0xFF00)+CPU.A", -- LD IXL, A
[0x7C] = "CPU.A=bit32.band(CPU.IX, 0xFF00)/256",
[0x7D] = "CPU.A=CPU.IX%256",
[0x7E] = function(memory, iaddr) -- LD A,(IX+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.A=memory[(CPU.IX+%s)%%65536]", displacement_byte), iaddr
end,
[0xE1] = "CPU.IX = memory[CPU.SP] + 256*memory[(CPU.SP+1)%65536] CPU.SP = (CPU.SP+2)%65536", -- POP IX
-- E3 = EX (SP), IX
[0xE3] = function(memory, iaddr)
-- seems like candidate for faster implementation! (2 extra copies, extra calculation of SP+1)
return write_2bytes_to_address_command_string("temp", "result", "CPU.SP", "(CPU.SP+1)%65536", iaddr,
"result = bit32.band(CPU.IX,0xFF00) / 256 temp = CPU.IX % 256 CPU.IX = memory[CPU.SP] + 256 * memory[(CPU.SP+1)%65536]"), iaddr
end,
[0xE5] = function(memory, iaddr) -- push IX
return write_2bytes_to_address_command_string("CPU.IX%256", "bit32.band(CPU.IX, 0xFF00)/256", "CPU.SP", "(CPU.SP+1)%65536", iaddr, "CPU.SP=(CPU.SP-2)%65536"), iaddr
end,
-- E9 = JP (IX) (see JP (HL) for notes)
[0xE9] = "do CPU.PC = CPU.IX; return 'ok' end",
}
-- FD = IY register
local decode_FD_instructions = {
[0x21] = function(memory, iaddr) local byte1 = memory[iaddr];iaddr = inc_address(iaddr);return string.format("CPU.IY=%s", memory[iaddr]*256+byte1), inc_address(iaddr) end,
-- 22 = LD (xxxx), IY
[0x22] = function(memory, iaddr)
local addr = memory[iaddr]; iaddr = inc_address(iaddr);
addr = addr+256*memory[iaddr]; iaddr = inc_address(iaddr);
return write_2bytes_to_address_command_string("CPU.IY%256", "bit32.band(CPU.IY, 0xFF00)/256", string.format("0x%x", addr), string.format("0x%x", (addr+1)%65536), iaddr), iaddr
end,
-- "INC IY"
[0x23] = "CPU.IY = (CPU.IY+1) % 65536",
[0x26] = function(memory, iaddr) local byte1 = memory[iaddr];iaddr = inc_address(iaddr);return string.format("CPU.IY=(CPU.IY%%256)+%s", 256*byte1), iaddr end,
-- 2A = LD IY,(xxxx)
[0x2A] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr); local byte2 = memory[iaddr]; iaddr = inc_address(iaddr)
local target = byte1 + 256*byte2
return string.format(
"CPU.IY = memory[0x%x]+256*memory[0x%x]", target, (target+1)%65536 ), iaddr
end,
--"DEC IY"
[0x2B] = "CPU.IY = (CPU.IY-1) % 65536",
[0x2E] = function(memory, iaddr) local byte1 = memory[iaddr];iaddr = inc_address(iaddr);return string.format("CPU.IY=bit32.band(0xFF00, CPU.IY)+%s", byte1), iaddr end,
[0x34] = function(memory, iaddr)
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
return write_to_address_command_string("result", string.format("(CPU.IY+%s)%%65536", byte1),
iaddr,
"result = (memory[addr]+1)%256 " ..
string.format(inc_flag_calc, "result", "result", "result")), iaddr
end,
[0x35] = function(memory, iaddr) -- dec(IY+d)
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
return write_to_address_command_string("result", string.format("(CPU.IY+%s)%%65536", byte1),
iaddr,
"result = (memory[addr]-1)%256" ..
string.format(dec_flag_calc, "result", "result", "result")), iaddr
end,
[0x36] = --ld (IY+d), &00
function(memory, iaddr)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
local data_byte = memory[iaddr]; iaddr = inc_address(iaddr);
return write_to_address_command_string(data_byte, string.format("(CPU.IY+%s)%%65536", displacement_byte), iaddr), iaddr
end,
[0x44] = "CPU.B=bit32.band(CPU.IY, 0xFF00)/256",
[0x45] = "CPU.B=CPU.IY%256",
[0x46] = function(memory, iaddr) -- LD B,(IY+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.B=memory[(CPU.IY+%s)%%65536]", displacement_byte), iaddr
end,
[0x4C] = "CPU.C=bit32.band(CPU.IY, 0xFF00)/256",
[0x4D] = "CPU.C=CPU.IY%256",
[0x4E] = function(memory, iaddr) -- LD C,(IY+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.C=memory[(CPU.IY+%s)%%65536]", displacement_byte), iaddr
end,
[0x54] = "CPU.D=bit32.band(CPU.IY, 0xFF00)/256",
[0x55] = "CPU.D=CPU.IY%256",
[0x56] = function(memory, iaddr) -- LD D,(IY+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.D=memory[(CPU.IY+%s)%%65536]", displacement_byte), iaddr
end,
[0x5C] = "CPU.E=bit32.band(CPU.IY, 0xFF00)/256",
[0x5D] = "CPU.E=CPU.IY%256",
[0x5E] = function(memory, iaddr) -- LD E,(IY+d)
local displacement_byte = memory[iaddr]; iaddr = inc_address(iaddr);
if displacement_byte > 127 then displacement_byte = displacement_byte-256 end
return string.format("CPU.E=memory[(CPU.IY+%s)%%65536]", displacement_byte), iaddr
end,
[0x60] = "CPU.IY=CPU.IY%256+CPU.B*256", -- LD IYH, B
[0x61] = "CPU.IY=CPU.IY%256+CPU.C*256", -- LD IYH, C
[0x62] = "CPU.IY=CPU.IY%256+CPU.D*256", -- LD IYH, D
[0x63] = "CPU.IY=CPU.IY%256+CPU.E*256", -- LD IYH, E
[0x64] = "", -- LD IYH, IYH
[0x65] = "CPU.IY=(CPU.IY%256)*0x101",
[0x67] = "CPU.IY=CPU.IY%256+CPU.A*256", -- LD IYH, A
[0x6C] = "temp=bit32.band(CPU.IY, 0xFF00) CPU.IY=temp/256+temp",
[0x6D] = "", -- LD IYL, IYL
[0x6F] = "CPU.IY=bit32.band(CPU.IY, 0xFF00)+CPU.A", -- LD IYL, A
[0x7C] = "CPU.A=bit32.band(CPU.IY, 0xFF00)/256",
[0x7D] = "CPU.A=CPU.IY%256",
[0xE1] = "CPU.IY = memory[CPU.SP] + 256*memory[(CPU.SP+1)%65536] CPU.SP = (CPU.SP+2)%65536", -- POP IY
-- E3 = EX (SP), IY
[0xE3] = function(memory, iaddr)
-- seems like candidate for faster implementation! (2 extra copies, extra calculation of SP+1)
return write_2bytes_to_address_command_string("temp", "result", "CPU.SP", "(CPU.SP+1)%65536", iaddr,
"result = bit32.band(CPU.IY,0xFF00) / 256 temp = CPU.IY % 256 CPU.IY = memory[CPU.SP] + 256 * memory[(CPU.SP+1)%65536]"), iaddr
end,
[0xE5] = function(memory, iaddr) -- push IY
return write_2bytes_to_address_command_string("CPU.IY%256", "bit32.band(CPU.IY, 0xFF00)/256", "CPU.SP", "(CPU.SP+1)%65536", iaddr, "CPU.SP=(CPU.SP-2)%65536"), iaddr
end,
-- E9 = JP (IY) (see JP (HL) for notes)
[0xE9] = "do CPU.PC = CPU.IY; return 'ok' end",
}
local function port_output_string(addr_hi, addr_lo, data)
return string.format(
-- run all output sources, not just first
-- @todo:could precalculate specific outputs on registration?
[[ for _,pd in ipairs(CPU._outputs) do
if bit32.band(pd.mask, %s*256+%s) == pd.state then
pd.f(pd.ud, %s, %s, %s)
end end]], addr_hi, addr_lo, addr_hi, addr_lo, data)
end
local function port_input_string(addr_hi, addr_lo, target_register, flags_required)
local flags_code = ""
if flags_required then
flags_code = "CPU._F=zflags[result]+CPU.Carry "
end
return string.format(
-- run first matching input source only
-- @todo:could precalculate specific input on registration?
[[ result = 0xFF for _,pd in ipairs(CPU._inputs) do
if bit32.band(pd.mask, %s*256+%s) == pd.state then
result = pd.f(pd.ud, %s, %s)
break
end end %s%s=result]], addr_hi, addr_lo, addr_hi, addr_lo, flags_code, target_register)
end
-- extended instructions
local decode_ED_instructions = {
-- ED 40 = IN B, (C) ... see below
-- ED 48 = IN C, (C) ... see below
-- ED 50 = IN D, (C) ... see below
-- ED 58 = IN E, (C) ... see below
-- ED 60 = IN H, (C) ... see below
-- ED 68 = IN L, (C) ... see below
-- ED 70 = IN F, (C) ... see below
-- ED 78 = IN A, (C) ... see below
-- ED 41 = OUT (C), B ... see below
-- ED 49 = OUT (C), C ... see below
-- ED 51 = OUT (C), D ... see below
-- ED 59 = OUT (C), E ... see below
-- ED 61 = OUT (C), H ... see below
-- ED 69 = OUT (C), L ... see below
-- ED 71 = OUT (C), F ... see below
-- ED 79 = OUT (C), A ... see below
-- ED 44 = NEG (like 0-A) ... we actually use subtract code. Might be easier to generate flags manually.
--
-- From Z80 user manual:
-- S is set if result is negative; reset otherwise
-- Z is set if result is 0; reset otherwise
-- H is set if borrow from bit 4; reset otherwise
-- P/V is set if Accumulator was 80H before operation; reset otherwise N is set
-- C is set if Accumulator was not 00H before operation; reset otherwise
--
[0x44] = [[result=-CPU.A
if result < 0 then
CPU.Carry=1 result=result+256
else
CPU.Carry=0
end
CPU._F = nil
CPU._oldA = 0
CPU._otherOp = CPU.A
CPU._newA = result
CPU._subtract = true
CPU.A = result
]],
--LD (xxxx),BC
[0x43] = function(memory, iaddr)
local addr = memory[iaddr]; iaddr = inc_address(iaddr);
addr = addr+256*memory[iaddr]; iaddr = inc_address(iaddr);
return write_2bytes_to_address_command_string("CPU.C", "CPU.B", string.format("0x%x", addr), string.format("0x%x", (addr+1)%65536), iaddr), iaddr
end,
--LD BC,(xxxx)
[0x4B] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr); local byte2 = memory[iaddr]; iaddr = inc_address(iaddr)
local target = byte1 + 256*byte2
return string.format(
"CPU.C = memory[0x%x] CPU.B = memory[0x%x]", target, (target+1)%65536 ), iaddr
end,
-- LD (xxxx),DE
[0x53] = function(memory, iaddr)
local addr = memory[iaddr]; iaddr = inc_address(iaddr);
addr = addr+256*memory[iaddr]; iaddr = inc_address(iaddr);
return write_2bytes_to_address_command_string("CPU.E", "CPU.D", string.format("0x%x", addr), string.format("0x%x", (addr+1)%65536), iaddr), iaddr
end,
--LD DE,(xxxx)
[0x5B] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr); local byte2 = memory[iaddr]; iaddr = inc_address(iaddr)
local target = byte1 + 256*byte2
return string.format(
"CPU.E = memory[0x%x] CPU.D = memory[0x%x]", target, (target+1)%65536 ), iaddr
end,
-- LD (xxxx), HL
[0x63] = function(memory, iaddr)
local addr = memory[iaddr]; iaddr = inc_address(iaddr);
addr = addr+256*memory[iaddr]; iaddr = inc_address(iaddr);
return write_2bytes_to_address_command_string("CPU.L", "CPU.H", string.format("0x%x", addr), string.format("0x%x", (addr+1)%65536), iaddr), iaddr
end,
--LD HL,(xxxx)
[0x6B] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr); local byte2 = memory[iaddr]; iaddr = inc_address(iaddr)
local target = byte1 + 256*byte2
return string.format(
"CPU.L = memory[0x%x] CPU.H = memory[0x%x]", target, (target+1)%65536 ), iaddr
end,
-- LD (xxxx),SP
[0x73] = function(memory, iaddr)
local addr = memory[iaddr]; iaddr = inc_address(iaddr);
addr = addr+256*memory[iaddr]; iaddr = inc_address(iaddr);
return write_2bytes_to_address_command_string("(CPU.SP%256)", "bit32.rshift(CPU.SP, 8)", string.format("0x%x", addr), string.format("0x%x", (addr+1)%65536), iaddr), iaddr
end,
--LD SP,(xxxx)
[0x7B] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr); local byte2 = memory[iaddr]; iaddr = inc_address(iaddr)
local target = byte1 + 256*byte2
return string.format(
"CPU.SP = memory[0x%x] + 256*memory[0x%x]", target, (target+1)%65536 ), iaddr
end,
-- Debug instruction, writes to host stdout .. ED ED
[0xED] = "io.write(string.char(CPU.A))", -- fake function for debugging
}
IN_reg_list = { "CPU.B", "CPU.C", "CPU.D", "CPU.E", "CPU.H", "CPU.L", "temp", "CPU.A" }
-- "The IN (C), F instructions is only usefull if you test bits
-- which have the same number as a flag in the F-register because
-- some older Z80 lock up otherwise." -- http://www.z80.info/z80undoc.htm
for i = 0, 7 do
local reg = IN_reg_list[i+1]
-- ED 40 = IN B, (C), ED 48 = IN C, (C) ... etc.
decode_ED_instructions[0x40+i*8] = function(memory, iaddr)
return port_input_string("CPU.B", "CPU.C", reg, true), iaddr
end
end
OUT_reg_list = { "CPU.B", "CPU.C", "CPU.D", "CPU.E", "CPU.H", "CPU.L", "CPU.F", "CPU.A" }
-- "I also have doubts about the usefullness and correctness of
-- the OUT (C),F instruction." -- http://www.z80.info/z80undoc.htm
--
-- I'm sure the OUT (C), F is broken ... it's unclear what it should do...
-- I wonder if it should be left as undefined?
for i = 0, 7 do
local reg = OUT_reg_list[i+1]
if reg ~= "CPU.F" then -- leave OUT (C), F undefined.
-- ED 41 = OUT (C), B ED 49 = OUT (C), C ... etc.
decode_ED_instructions[0x41+i*8] = function(memory, iaddr)
return port_output_string("CPU.B", "CPU.C", reg), iaddr
end
end
end
local function call_code_string(return_addr, target)
return string.format( [[
CPU.SP = (CPU.SP-1)%%65536
if jit.write_allowed[CPU.SP] then memory[CPU.SP] = 0x%x result = jit:code_write_check(CPU.SP) end
CPU.SP = (CPU.SP-1)%%65536
if jit.write_allowed[CPU.SP] then memory[CPU.SP] = 0x%x result = result or jit:code_write_check(CPU.SP) end
if result then CPU.PC = 0x%x return 'invalidate' end
if jit.jump_count==0 then CPU.PC = 0x%x; return 'ok' else jit.jump_count = jit.jump_count-1;goto l_%04x end]],
math.floor(return_addr/256), return_addr%256, target, target, target)
end
local decode_instruction
local decode_first_byte = {
-- 00 = NOP
[0] = "", -- this is the best instruction :-)
-- 08 = EX AF, AF'
-- ensure the flags are updated first!
[0x08] = [[ CPU:get_F()
result=CPU.A CPU.A=CPU.A_ CPU.A_=result
result=CPU._F CPU._F=CPU.F_ CPU.F_=result
CPU.Carry = CPU._F % 2 ]],
-- 10 = DJNZ xx
[0x10] = function(memory, iaddr)
-- safe to pre-read because a lump write in this rejoin immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
local target = iaddr+byte1
return string.format(
"CPU.B = CPU.B - 1 if CPU.B ~= 0 then if jit.jump_count==0 then CPU.PC = 0x%x; return 'ok' else jit.jump_count = jit.jump_count-1;goto l_%04x end end", target, target), iaddr, target
end,
-- 18 = JR xx
[0x18] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
local target = iaddr+byte1
return string.format(
"if jit.jump_count==0 then CPU.PC = 0x%x; return 'ok' else jit.jump_count = jit.jump_count-1;goto l_%04x end", target, target), iaddr, target
end,
-- 20 = JR NZ, xx
[0x20] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
local target = iaddr+byte1
return string.format(
"if not bit32.btest(CPU:get_F(), Z80_Z_FLAG) then if jit.jump_count==0 then CPU.PC = 0x%x; return 'ok' else jit.jump_count = jit.jump_count-1;goto l_%04x end end", target, target), iaddr, target
end,
-- 28 = JR Z, xx
[0x28] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
local target = iaddr+byte1
return string.format(
"if bit32.btest(CPU:get_F(), Z80_Z_FLAG) then if jit.jump_count==0 then CPU.PC = 0x%x; return 'ok' else jit.jump_count = jit.jump_count-1;goto l_%04x end end", target, target), iaddr, target
end,
-- 30 = JR NC, xx
[0x30] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
local target = iaddr+byte1
return string.format(
"if CPU.Carry == 0 then if jit.jump_count==0 then CPU.PC = 0x%x; return 'ok' else jit.jump_count = jit.jump_count-1;goto l_%04x end end", target, target), iaddr, target
end,
-- 38 = JR C, xx
[0x38] = function(memory, iaddr)
-- safe to pre-read because a lump write in this returns immediately invalidates lump
local byte1 = memory[iaddr]; iaddr = inc_address(iaddr)
if byte1 > 127 then byte1 = byte1-256 end
local target = iaddr+byte1
return string.format(
"if CPU.Carry == 1 then if jit.jump_count==0 then CPU.PC = 0x%x; return 'ok' else jit.jump_count = jit.jump_count-1;goto l_%04x end end", target, target), iaddr, target
end,
-- 01 = LD BC, xxxx
[0x01] = function(memory, iaddr) local byte1 = memory[iaddr];iaddr = inc_address(iaddr);return string.format("CPU.B=%s;CPU.C=%s", memory[iaddr], byte1), inc_address(iaddr) end,
-- 11 = LD DE, xxxx
[0x11] = function(memory, iaddr) local byte1 = memory[iaddr];iaddr = inc_address(iaddr);return string.format("CPU.D=%s;CPU.E=%s", memory[iaddr], byte1), inc_address(iaddr) end,
-- 21 = LD HL, xxxx
[0x21] = function(memory, iaddr) local byte1 = memory[iaddr];iaddr = inc_address(iaddr);return string.format("CPU.H=%s;CPU.L=%s", memory[iaddr], byte1), inc_address(iaddr) end,
-- 31 = LD SP, xxxx