-
Notifications
You must be signed in to change notification settings - Fork 8
/
cpu.lua
1411 lines (1235 loc) · 35.4 KB
/
cpu.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
local band, bor, bxor, bnot, lshift, rshift = bit.band, bit.bor, bit.bxor, bit.bnot, bit.lshift, bit.rshift
local map, rotatePositiveIdx, nthBitIsSet, nthBitIsSetInt =
UTILS.map,
UTILS.rotatePositiveIdx,
UTILS.nthBitIsSet,
UTILS.nthBitIsSetInt
CPU = {
RP2A03_CC = 12,
FOREVER_CLOCK = 0xffffffff
}
local CPU = CPU
local DISPATCH
CPU._mt = { __index = CPU }
do
CPU.CLK = {}
local clocks = { 1, 2, 3, 4, 5, 6, 7, 8 }
for i = 1, #clocks do
CPU.CLK[i] = clocks[i] * CPU.RP2A03_CC
end
end
CPU.UNDEFINED = {}
CPU.RAM_SIZE = 0x0800
CPU.MAINMEM_SIZE = 0x10000
CPU.NMI_VECTOR = 0xfffa
CPU.RESET_VECTOR = 0xfffc
CPU.IRQ_VECTOR = 0xfffe
CPU.IRQ_EXT = 0x01
CPU.IRQ_FRAME = 0x40
CPU.IRQ_DMC = 0x80
CPU.ClockRates = {
Ntsc = 1789773,
Pal = 1662607,
Dendy = 1773448
}
local RAM_SIZE, UNDEFINED, NMI_VECTOR, IRQ_VECTOR, FOREVER_CLOCK =
CPU.RAM_SIZE,
CPU.UNDEFINED,
CPU.NMI_VECTOR,
CPU.IRQ_VECTOR,
CPU.FOREVER_CLOCK
local UNDEFINED = CPU.UNDEFINED
local CLK = CPU.CLK
local isDefined = UTILS.isDefined
local bind = UTILS.bind
local tSetter = UTILS.tSetter
local tGetter = UTILS.tGetter
local fill = UTILS.fill
local range = UTILS.range
local printf = UTILS.printf
local nthBitIsSetInt = UTILS.nthBitIsSetInt
CPU.PokeNop = function()
end
function CPU:steal_clocks(clk)
self.clk = self.clk + clk
end
function CPU:odd_clock()
return ((self.clk_total + self.clk) % CLK[2]) ~= 0
end
function CPU:update()
self.apu:clock_dma(self.clk)
return self.clk
end
function CPU:dmc_dma(addr)
-- This is inaccurate; it must steal *up to* 4 clocks depending upon
-- whether CPU writes in this clock, but this always steals 4 clocks.
self.clk = self.clk + CLK[3]
local dma_buffer = self:fetch(addr)
self.clk = self.clk + CLK[1]
return dma_buffer
end
function CPU:next_frame_clock()
return self.clk_frame
end
function CPU:set_next_frame_clock(x)
self.clk_frame = x
self.clk_target = x < self.clk_target and x or self.clk_target
return x
end
function CPU:current_clock()
return self.clk
end
function CPU:peek_nop(addr)
return rshift(addr, 8)
end
function CPU:peek_jam_1(addr)
self._pc = band(self._pc - 1, 0xffff)
return 0xfc
end
function CPU:peek_jam_2(_addr)
return 0xff
end
function CPU:peek_ram(addr)
return self.ram[addr % CPRAM_SIZE]
end
function CPU:poke_ram(addr, data)
self.ram[addr % RAM_SIZE] = data
end
-- read an addr (8 bit)
function CPU:fetch(addr)
--UTILS.print(string.format("%04X", addr))
return self._fetch[addr](addr)
end
function CPU:store(addr, value)
return self._store[addr](addr, value)
end
-- Read 16 bits (word) from addr
function CPU:peek16(addr)
return self:fetch(addr) + lshift(self:fetch(addr + 1), 8)
end
function CPU:add_mappings(addr, peek, poke)
self.peeks[peek] = isDefined(self.peeks[peek]) and self.peeks[peek] or peek
peek = self.peeks[peek]
self.pokes[poke] = isDefined(self.pokes[poke]) and self.pokes[poke] or poke
poke = self.pokes[poke]
if type(addr) == "number" then
addr = { addr }
end
for i = addr[0] and 0 or 1, #addr do
local x = addr[i]
self._fetch[x] = peek
if poke and type(poke) ~= "table" then
self._store[x] = poke
else
self._store[x] = CPU.PokeNop
end
end
end
function CPU:reset()
self._a = 0
self._x = 0
self._y = 0
self._sp = 0xfd
self._pc = 0xfffc
self._p_nz = 1
self._p_c = 0
self._p_v = 0
self._p_i = 0x04
self._p_d = 0
fill(self.ram, 0xff)
self.clk = 0
self.clk_total = 0
local ram = self.ram
self:add_mappings(
range(0x0000, 0x07ff),
function(i)
return ram[i]
end,
tSetter(self.ram)
)
self:add_mappings(
range(0x0800, 0x1fff),
function(i)
return ram[i % RAM_SIZE]
end,
bind(self.poke_ram, self)
)
self:add_mappings(range(0x2000, 0xffff), bind(self.peek_nop, self), UNDEFINED) -- 8 PPU registers mirrored
--self:add_mappings(0xfffc, bind(self.peek_jam_1, self), UNDEFINED)
--self:add_mappings(0xfffd, bind(self.peek_jam_2, self), UNDEFINED)
end
function CPU:new(conf)
local cpu = {}
setmetatable(cpu, CPU._mt)
cpu.conf = conf or { loglevel = 0, pc = nil }
cpu.conf.debug = UTILS.print
cpu.ram = fill({}, UNDEFINED, RAM_SIZE)
cpu.ram[0] = UNDEFINED
cpu._store = fill({}, UNDEFINED, CPU.MAINMEM_SIZE)
cpu._fetch = fill({}, UNDEFINED, CPU.MAINMEM_SIZE)
cpu.peeks = {}
cpu.pokes = {}
cpu.clk_rate = CPU.ClockRates.Ntsc
cpu.clk = 0 -- the current clock
cpu.clk_frame = 0
cpu.clk_target = 0 -- the goal clock for the current CPU#run
cpu.clk_total = 0 -- the total elapsed clocks
cpu.clk_nmi = FOREVER_CLOCK -- the next NMI clock (FOREVER_CLOCK means "not scheduled")
cpu.clk_irq = FOREVER_CLOCK -- the next IRQ clock
cpu.irq_flags = 0
cpu.jammed = false
cpu:reset()
cpu.data = 0
cpu.addr = 0
cpu.opcode = nil
cpu.ppu_sync = nil
-- DUMMY APU
cpu.apu = {
clock_dma = function(clk)
end,
do_clock = function()
return CLK[1]
end
}
return cpu
end
function CPU:dmc_dma(addr)
-- This is inaccurate; it must steal *up to* 4 clocks depending upon
-- whether CPU writes in this clock, but this always steals 4 clocks.
self.clk = CLK[3] + self.clk
local dma_buffer = self:fetch(addr)
self.clk = CLK[1] + self.clk
return dma_buffer
end
function CPU:sprite_dma(addr, sp_ram)
do
local ram = self.ram
for i = 1, 256 do
sp_ram[i] = ram[addr + i - 1]
end
end
for i = 0, 63 do
local j = i * 4 + 3
sp_ram[j] = band(sp_ram[j], 0xe3)
end
end
function CPU:boot()
self.clk = CLK[7] -- clocks it takes to boot
self._pc = self.conf.pc or self:peek16(CPU.RESET_VECTOR)
end
function CPU:vsync()
if self.ppu_sync then
(self.ppu):sync(self.clk)
end
self.clk = self.clk - self.clk_frame
self.clk_total = self.clk_total + self.clk_frame
if self.clk_nmi ~= FOREVER_CLOCK then
self.clk_nmi = self.clk_nmi - self.clk_frame
end
if self.clk_irq ~= FOREVER_CLOCK then
self.clk_irq = self.clk_irq - self.clk_frame
if self.clk_irq < 0 then
self.clk_irq = 0
end
end
end
-------------------------------------------------------------------------------------------------------------------
-- interrupts
function CPU:clear_irq(line)
local old_irq_flags = band(self.irq_flags, bor(CPU.IRQ_FRAME, CPU.IRQ_DMC))
-- self.irq_flags = band(bxor(self.irq_flags, bxor(line, bor(bor(CPU.IRQ_EXT, CPU.IRQ_FRAME), CPU.IRQ_DMC))))
self.irq_flags = band(self.irq_flags, bxor(line, bor(bor(CPU.IRQ_EXT, CPU.IRQ_FRAME), CPU.IRQ_DMC)))
if self.irq_flags == 0 then
self.clk_irq = FOREVER_CLOCK
end
return old_irq_flags
end
function CPU:next_interrupt_clock(clk)
clk = clk + CLK[1] + CLK[1] / 2 -- interrupt edge
if self.clk_target > clk then
self.clk_target = clk
end
return clk
end
function CPU:do_irq(line, clk)
self.irq_flags = bor(self.irq_flags, line)
if self.clk_irq == FOREVER_CLOCK and self._p_i == 0 then
self.clk_irq = self:next_interrupt_clock(clk)
end
end
function CPU:do_nmi(clk)
if self.clk_nmi == FOREVER_CLOCK then
self.clk_nmi = self:next_interrupt_clock(clk)
end
end
function CPU:do_isr(vector)
if self.jammed then
return
end
self:push16(self._pc)
self:push8(self:flags_pack())
self._p_i = 0x04
self.clk = self.clk + CLK[7]
local addr = vector == NMI_VECTOR and NMI_VECTOR or self:fetch_irq_isr_vector()
self._pc = self:peek16(addr)
end
function CPU:fetch_irq_isr_vector()
if self.clk >= self.clk_frame then
self:fetch(0x3000)
end
if self.clk_nmi ~= FOREVER_CLOCK then
if self.clk_nmi + CLK[2] <= self.clk then
self.clk_nmi = FOREVER_CLOCK
return NMI_VECTOR
end
self.clk_nmi = self.clk + 1
end
return IRQ_VECTOR
end
------------------------------------------------------------------------------------------------------------------------
-- instruction helpers
------ P regeister ------
function CPU:flags_pack()
local nz = self._p_nz
return bor(
bor(
bor(
bor(
bor(bor(band(bor(rshift(nz, 1), nz), 0x80), (band(nz, 0xff) ~= 0 and 0 or 2)), self._p_c),
self._p_v ~= 0 and 0x40 or 0
),
self._p_i
),
self._p_d
),
0x20
)
end
function CPU:flags_unpack(f)
self._p_nz = bor(band(bnot(f), 2), lshift(band(f, 0x80), 1))
self._p_c = band(f, 0x01)
self._p_v = band(f, 0x40)
self._p_i = band(f, 0x04)
self._p_d = band(f, 0x08)
end
------ branch helper ------
function CPU:branch_true()
local tmp = self._pc + 1
local rel = self:fetch(self._pc)
self._pc = band(tmp + (rel < 128 and rel or bor(rel, 0xff00)), 0xffff)
self.clk = self.clk + (nthBitIsSetInt(tmp, 8) == nthBitIsSetInt(self._pc, 8) and CLK[3] or CLK[4])
end
function CPU:branch_false()
self._pc = self._pc + 1
self.clk = self.clk + CLK[2]
end
do
local branch_table = {}
branch_table[true] = CPU.branch_true
branch_table[false] = CPU.branch_false
function CPU:branch(cond)
branch_table[cond](self)
--[[
if cond then
local tmp = self._pc + 1
local rel = self:fetch(self._pc)
self._pc = band(tmp + (rel < 128 and rel or bor(rel, 0xff00)), 0xffff)
self.clk = self.clk + (nthBitIsSetInt(tmp, 8) == nthBitIsSetInt(self._pc, 8) and CLK[3] or CLK[4])
else
self._pc = self._pc + 1
self.clk = self.clk + CLK[2]
end
]]
end
end
--jit.off(CPU.branch)
------ storers ------
function CPU:store_mem()
self:store(self.addr, self.data)
self.clk = self.clk + CLK[1]
end
function CPU:store_zpg()
self.ram[self.addr] = self.data
end
------ stack management ------
function CPU:push8(data)
local sp = self._sp
self.ram[0x0100 + sp] = data
self._sp = band(sp - 1, 0xff)
end
function CPU:push16(data)
self:push8(rshift(data, 8))
self:push8(band(data, 0xff))
end
function CPU:pull8()
self._sp = band(self._sp + 1, 0xff)
return self.ram[0x0100 + self._sp]
end
function CPU:pull16()
local x = self:pull8()
local b = self:pull8()
return x + 256 * b
end
------------------------------------------------------------------------------------------------------------------------
-- addressing modes
-- immediate addressing (read only)
function CPU:imm(_read, _write)
self.data = self:fetch(self._pc)
self._pc = self._pc + 1
self.clk = self.clk + CLK[2]
end
-- zero-page addressing
function CPU:zpg(read, write)
self.addr = self:fetch(self._pc)
self._pc = self._pc + 1
self.clk = self.clk + CLK[3]
self.data = read and self.ram[self.addr] or self.data
self.clk = self.clk + ((read and write) and CLK[2] or 0)
--[[
if read then
self.data = self.ram[self.addr]
--self.clk = self.clk + zpg_write_clk[write]
--self.clk = self.clk + zpg_write_clk2[write and 2 or 1]
self.clk = self.clk + (write and CLK[2] or 0)
--[[
if write then
self.clk = self.clk + CLK[2]
end
--] ]
end
--]]
end
-- zero-page indexed addressing
function CPU:zpg_reg(indexed, read, write)
self.addr = band(indexed + self:fetch(self._pc), 0xff)
self._pc = self._pc + 1
self.clk = self.clk + CLK[4]
self.data = read and self.ram[self.addr] or self.data
self.clk = self.clk + ((read and write) and CLK[2] or 0)
--[[
if read then
self.data = self.ram[self.addr]
if write then
self.clk = self.clk + CLK[2]
end
end
--]]
end
function CPU:zpg_x(read, write)
return self:zpg_reg(self._x, read, write)
end
function CPU:zpg_y(read, write)
return self:zpg_reg(self._y, read, write)
end
-- absolute addressing
function CPU:abs(read, write)
self.addr = self:peek16(self._pc)
self._pc = self._pc + 2
self.clk = self.clk + CLK[3]
return self:read_write(read, write)
end
-- absolute indexed addressing
function CPU:abs_reg(indexed, read, write)
local addr = self._pc + 1
local i = indexed + self:fetch(self._pc)
self.addr = band(lshift(self:fetch(addr), 8) + i, 0xffff)
if write then
addr = band(self.addr - band(i, 0x100), 0xffff)
self:fetch(addr)
self.clk = self.clk + CLK[4]
else
self.clk = self.clk + CLK[3]
if band(i, 0x100) ~= 0 then
addr = band(self.addr - 0x100, 0xffff) -- for inlining fetch
self:fetch(addr)
self.clk = self.clk + CLK[1]
end
end
self:read_write(read, write)
self._pc = self._pc + 2
end
function CPU:abs_x(read, write)
return self:abs_reg(self._x, read, write)
end
function CPU:abs_y(read, write)
return self:abs_reg(self._y, read, write)
end
-- indexed indirect addressing
function CPU:ind_x(read, write)
local addr = self:fetch(self._pc) + self._x
self._pc = self._pc + 1
self.clk = self.clk + CLK[5]
self.addr = bor(self.ram[band(addr, 0xff)], lshift(self.ram[band(addr + 1, 0xff)], 8))
return self:read_write(read, write)
end
-- indirect indexed addressing
function CPU:ind_y(read, write)
local addr = self:fetch(self._pc)
self._pc = self._pc + 1
local ram = self.ram
local indexed = ram[addr] + self._y
self.clk = self.clk + CLK[4]
if write then
self.clk = self.clk + CLK[1]
self.addr = lshift(ram[band(addr + 1, 0xff)], 8) + indexed
addr = self.addr - band(indexed, 0x100) -- for inlining fetch
self:fetch(addr)
else
self.addr = band(lshift(ram[band(addr + 1, 0xff)], 8) + indexed, 0xffff)
if band(indexed, 0x100) ~= 0 then
addr = band(self.addr - 0x100, 0xffff) -- for inlining fetch
self:fetch(addr)
self.clk = self.clk + CLK[1]
end
end
return self:read_write(read, write)
end
function CPU:read_write(read, write)
if read then
--print(string.format("%04X",self.addr))
self.data = self:fetch(self.addr)
--print(self.clk)
self.clk = self.clk + CLK[1]
--print(self.clk)
if write then
self:store(self.addr, self.data)
--print(self.clk)
self.clk = self.clk + CLK[1]
--print(self.clk)
end
end
end
--jit.off(CPU.read_write)
--------------------------------------------------------------------------------------------------------------------
-- instructions
-- load instructions
function CPU:_lda()
self._p_nz = self.data
self._a = self.data
end
function CPU:_ldx()
self._p_nz = self.data
self._x = self.data
end
function CPU:_ldy()
self._y = self.data
self._p_nz = self.data
end
-- store instructions
function CPU:_sta()
self.data = self._a
end
function CPU:_stx()
self.data = self._x
end
function CPU:_sty()
self.data = self._y
end
-- transfer instructions
function CPU:_tax()
self.clk = self.clk + CLK[2]
local a = self._a
self._x = a
self._p_nz = a
end
function CPU:_tay()
self.clk = self.clk + CLK[2]
local a = self._a
self._y = a
self._p_nz = a
end
function CPU:_txa()
self.clk = self.clk + CLK[2]
local x = self._x
self._a = x
self._p_nz = x
end
function CPU:_tya()
self.clk = self.clk + CLK[2]
local y = self._y
self._a = y
self._p_nz = y
end
-- flow control instructions
function CPU:_jmp_a()
self._pc = self:peek16(self._pc)
self.clk = self.clk + CLK[3]
end
function CPU:_jmp_i()
local pos = self:peek16(self._pc)
local low = self:fetch(pos)
pos = bor(band(pos, 0xff00), band(pos + 1, 0x00ff))
local high = self:fetch(pos)
self._pc = high * 256 + low
self.clk = self.clk + CLK[5]
end
function CPU:_jsr()
local data = self._pc + 1
self:push16(data)
self._pc = self:peek16(self._pc)
self.clk = self.clk + CLK[6]
end
function CPU:_rts()
local x = self:pull16()
self._pc = band(x + 1, 0xffff)
self.clk = self.clk + CLK[6]
end
function CPU:_rti()
self.clk = self.clk + CLK[6]
local packed = self:pull8()
self._pc = self:pull16()
self:flags_unpack(packed)
if self.irq_flags == 0 or self._p_i ~= 0 then
self.clk_irq = FOREVER_CLOCK
else
self.clk_target = 0
self.clk_irq = 0
end
end
function CPU:_bne()
return self:branch(band(self._p_nz, 0xff) ~= 0)
end
function CPU:_beq()
return self:branch(band(self._p_nz, 0xff) == 0)
end
function CPU:_bmi()
return self:branch(band(self._p_nz, 0x180) ~= 0)
end
function CPU:_bpl()
return self:branch(band(self._p_nz, 0x180) == 0)
end
function CPU:_bcs()
return self:branch(self._p_c ~= 0)
end
function CPU:_bcc()
return self:branch(self._p_c == 0)
end
function CPU:_bvs()
return self:branch(self._p_v ~= 0)
end
function CPU:_bvc()
return self:branch(self._p_v == 0)
end
-- math operations
function CPU:_adc()
local tmp = self._a + self.data + self._p_c
self._p_v = band(bnot(bxor(self._a, self.data)), bxor(self._a, tmp), 0x80)
self._a = band(tmp, 0xff)
self._p_nz = self._a
self._p_c = nthBitIsSetInt(tmp, 8)
end
function CPU:_sbc()
local data = bxor(self.data, 0xff)
local tmp = self._a + data + self._p_c
self._p_v = band(bnot(bxor(self._a, data)), bxor(self._a, tmp), 0x80)
self._a = band(tmp, 0xff)
self._p_nz = self._a
self._p_c = nthBitIsSetInt(tmp, 8)
end
-- logical operations
function CPU:_and()
self._a = band(self._a, self.data)
self._p_nz = self._a
end
function CPU:_ora()
self._a = bor(self._a, self.data)
self._p_nz = self._a
end
function CPU:_eor()
self._a = bxor(self._a, self.data)
self._p_nz = self._a
end
function CPU:_bit()
self._p_nz = bor(band(self.data, self._a) ~= 0 and 1 or 0, lshift(band(self.data, 0x80), 1))
self._p_v = band(self.data, 0x40)
end
function CPU:_cmp()
local data = self._a - self.data
self._p_nz = band(data, 0xff)
self._p_c = 1 - nthBitIsSetInt(data, 8)
end
function CPU:_cpx()
local data = self._x - self.data
self._p_nz = band(data, 0xff)
self._p_c = 1 - nthBitIsSetInt(data, 8)
end
function CPU:_cpy()
local data = self._y - self.data
self._p_nz = band(data, 0xff)
self._p_c = 1 - nthBitIsSetInt(data, 8)
end
-- shift operations
function CPU:_asl()
self._p_c = rshift(self.data, 7)
self._p_nz = band(lshift(self.data, 1), 0xff)
self.data = self._p_nz
end
function CPU:_lsr()
self._p_c = band(self.data, 1)
self._p_nz = rshift(self.data, 1)
self.data = self._p_nz
end
function CPU:_rol()
self._p_nz = bor(band(lshift(self.data, 1), 0xff), self._p_c)
self._p_c = rshift(self.data, 7)
self.data = self._p_nz
end
function CPU:_ror()
self._p_nz = bor(rshift(self.data, 1), lshift(self._p_c, 7))
self._p_c = band(self.data, 1)
self.data = self._p_nz
end
-- increment and decrement operations
function CPU:_dec()
self._p_nz = band(self.data - 1, 0xff)
self.data = self._p_nz
end
function CPU:_inc()
self._p_nz = band(self.data + 1, 0xff)
self.data = self._p_nz
end
function CPU:_dex()
self.clk = self.clk + CLK[2]
local x = band(self._x - 1, 0xff)
self.data = x
self._p_nz = x
self._x = x
end
function CPU:_dey()
self.clk = self.clk + CLK[2]
self._y = band(self._y - 1, 0xff)
self.data = self._y
self._p_nz = self._y
end
function CPU:_inx()
self.clk = self.clk + CLK[2]
self._x = band(self._x + 1, 0xff)
self.data = self._x
self._p_nz = self._x
end
function CPU:_iny()
self.clk = self.clk + CLK[2]
self._y = band(self._y + 1, 0xff)
self.data = self._y
self._p_nz = self._y
end
-- flags instructions
function CPU:_clc()
self.clk = self.clk + CLK[2]
self._p_c = 0
end
function CPU:_sec()
self.clk = self.clk + CLK[2]
self._p_c = 1
end
function CPU:_cld()
self.clk = CLK[2] + self.clk
self._p_d = 0
end
function CPU:_sed()
self.clk = self.clk + CLK[2]
self._p_d = 8
end
function CPU:_clv()
self.clk = self.clk + CLK[2]
self._p_v = 0
end
function CPU:_sei()
self.clk = self.clk + CLK[2]
if self._p_i == 0 then
self._p_i = 0x04
self.clk_irq = FOREVER_CLOCK
if self.irq_flags ~= 0 then
self:do_isr(IRQ_VECTOR)
end
end
end
function CPU:_cli()
self.clk = self.clk + CLK[2]
if self._p_i ~= 0 then
self._p_i = 0
if self.irq_flags ~= 0 then
local clk = self.clk + 1
self.clk_irq = clk
if self.clk_target > clk then
self.clk_target = clk
end
end
end
end
-- stack operations
function CPU:_pha()
self.clk = self.clk + CLK[3]
return self:push8(self._a)
end
function CPU:_php()
self.clk = self.clk + CLK[3]
local data = bor(self:flags_pack(), 0x10)
return self:push8(data)
end
function CPU:_pla()
self.clk = self.clk + CLK[4]
self._a = self:pull8()
self._p_nz = self._a
end
function CPU:_plp()
self.clk = self.clk + CLK[4]
local i = self._p_i
self:flags_unpack(self:pull8())
if self.irq_flags ~= 0 then
if i > self._p_i then
local clk = self.clk + 1
self.clk_irq = clk
if self.clk_target > clk then
self.clk_target = clk
end
elseif i < self._p_i then
self.clk_irq = FOREVER_CLOCK
self:do_isr(IRQ_VECTOR)
end
end
end
function CPU:_tsx()
self.clk = self.clk + CLK[2]
self._p_nz = self._sp
self._x = self._sp
end
function CPU:_txs()
self.clk = self.clk + CLK[2]
self._sp = self._x
end
-- undocumented instructions, rarely used
function CPU:_anc()
self._a = band(self._a, self.data)
self._p_nz = self._a
self._p_c = rshift(self._p_nz, 7)
end
function CPU:_ane()
self._a = band(band(bor(self._a, 0xee), self._x), self.data)
self._p_nz = self._a
end
function CPU:_arr()
self._a = bor(rshift(band(self.data, self._a), 1), lshift(self._p_c, 7))
self._p_nz = self._a
self._p_c = nthBitIsSetInt(self._a, 6)
self._p_v = bxor(nthBitIsSetInt(self._a, 6), nthBitIsSetInt(self._a, 5))
end
function CPU:_asr()
self._p_c = band(band(self.data, self._a), 0x1)
self._a = rshift(band(self.data, self._a), 1)
self._p_nz = self._a
end
function CPU:_dcp()
self.data = band(self.data - 1, 0xff)
return self:_cmp()
end
function CPU:_isb()
self.data = band(self.data + 1, 0xff)
return self:_sbc()
end
function CPU:_las()
self._sp = band(self._sp, self.data)
self._x = self._sp
self._p_nz = self._x
self._a = self._x
end
function CPU:_lax()
self._x = self.data
self._p_nz = self._x
self._a = self._x
end
function CPU:_lxa()
self._x = self.data
self._p_nz = self._x
self._a = self._x
end
function CPU:_rla()
local c = self._p_c
self._p_c = rshift(self.data, 7)
self.data = bor(band(lshift(self.data, 1), 0xff), c)
self._a = band(self._a, self.data)
self._p_nz = self._a
end
function CPU:_rra()
local c = lshift(self._p_c, 7)
self._p_c = band(self.data, 1)
self.data = bor(rshift(self.data, 1), c)
return self:_adc()
end
function CPU:_sax()
self.data = band(self._a, self._x)
end
function CPU:_sbx()
self.data = band(self._a, self._x) - self.data