-
Notifications
You must be signed in to change notification settings - Fork 4
/
clem_code.h
985 lines (909 loc) · 32.1 KB
/
clem_code.h
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
#include "clem_mem.h"
#include "clem_types.h"
// inlined header
#include "clem_cycle.h"
static inline void _cpu_p_flags_n_data(struct Clemens65C816 *cpu, uint8_t data) {
if (data & 0x80) {
cpu->regs.P |= kClemensCPUStatus_Negative;
} else {
cpu->regs.P &= ~kClemensCPUStatus_Negative;
}
}
static inline void _cpu_p_flags_n_data_16(struct Clemens65C816 *cpu, uint16_t data) {
if (data & 0x8000) {
cpu->regs.P |= kClemensCPUStatus_Negative;
} else {
cpu->regs.P &= ~kClemensCPUStatus_Negative;
}
}
static inline void _cpu_p_flags_z_data(struct Clemens65C816 *cpu, uint8_t data) {
if (data) {
cpu->regs.P &= ~kClemensCPUStatus_Zero;
} else {
cpu->regs.P |= kClemensCPUStatus_Zero;
}
}
static inline void _cpu_p_flags_z_data_16(struct Clemens65C816 *cpu, uint16_t data) {
if (data) {
cpu->regs.P &= ~kClemensCPUStatus_Zero;
} else {
cpu->regs.P |= kClemensCPUStatus_Zero;
}
}
static inline void _cpu_p_flags_n_z_data(struct Clemens65C816 *cpu, uint8_t data) {
_cpu_p_flags_n_data(cpu, data);
_cpu_p_flags_z_data(cpu, data);
}
static inline void _cpu_p_flags_n_z_data_16(struct Clemens65C816 *cpu, uint16_t data) {
_cpu_p_flags_n_data_16(cpu, data);
_cpu_p_flags_z_data_16(cpu, data);
}
static inline void _cpu_p_flags_n_z_data_816(struct Clemens65C816 *cpu, uint16_t data, bool is8) {
if (is8) {
_cpu_p_flags_n_z_data(cpu, (uint8_t)data);
} else {
_cpu_p_flags_n_z_data_16(cpu, data);
}
}
static inline void _cpu_p_flags_apply_m_x(struct Clemens65C816 *cpu) {
bool idx8 = (cpu->regs.P & kClemensCPUStatus_Index) != 0;
if (idx8) {
cpu->regs.X &= 0xff;
cpu->regs.Y &= 0xff;
} else {
cpu->regs.P &= ~kClemensCPUStatus_Index;
}
}
static inline void _cpu_sp_dec3(struct Clemens65C816 *cpu) {
uint16_t tmp = cpu->regs.S - 3;
if (cpu->pins.emulation) {
tmp = (cpu->regs.S & 0xff00) | (tmp & 0x00ff);
}
cpu->regs.S = tmp;
}
static inline void _cpu_sp_dec2(struct Clemens65C816 *cpu) {
uint16_t tmp = cpu->regs.S - 2;
if (cpu->pins.emulation) {
tmp = (cpu->regs.S & 0xff00) | (tmp & 0x00ff);
}
cpu->regs.S = tmp;
}
static inline void _cpu_sp_dec(struct Clemens65C816 *cpu) {
uint16_t tmp = cpu->regs.S - 1;
if (cpu->pins.emulation) {
tmp = (cpu->regs.S & 0xff00) | (tmp & 0x00ff);
}
cpu->regs.S = tmp;
}
static inline void _cpu_sp_inc3(struct Clemens65C816 *cpu) {
uint16_t tmp = cpu->regs.S + 3;
if (cpu->pins.emulation) {
tmp = (cpu->regs.S & 0xff00) | (tmp & 0x00ff);
}
cpu->regs.S = tmp;
}
static inline void _cpu_sp_inc2(struct Clemens65C816 *cpu) {
uint16_t tmp = cpu->regs.S + 2;
if (cpu->pins.emulation) {
tmp = (cpu->regs.S & 0xff00) | (tmp & 0x00ff);
}
cpu->regs.S = tmp;
}
static inline void _cpu_sp_inc(struct Clemens65C816 *cpu) {
uint16_t tmp = cpu->regs.S + 1;
if (cpu->pins.emulation) {
tmp = (cpu->regs.S & 0xff00) | (tmp & 0x00ff);
}
cpu->regs.S = tmp;
}
/* Memory Reads and Writes:
Requirements:
Handle FPI access to ROM
Handle FPI and MEGA2 fast and slow accesses to RAM
Handle Access based on the Shadow Register
*/
static inline void _clem_io_read_cycle(ClemensMachine *clem, uint16_t addr, uint16_t idx,
uint8_t bank) {
/* special rules for IO cycles called out here but decision to call
determined by the caller (to minimize conditional logic)
x = 0, crossing page boundaries on index, or write
*/
uint8_t offset = (uint8_t)(addr & 0xff);
uint8_t tmp_data;
// read from DBR,AAH.AAL+YL per WDC 65816 spec
addr = (addr & 0xff00) | (uint8_t)(offset + (idx & 0xff));
clem_read(clem, &tmp_data, addr, bank, CLEM_MEM_FLAG_BUS_IO);
}
static inline void _clem_next_dbr(ClemensMachine *clem, uint8_t *next_dbr, uint8_t dbr) {
if (!clem->cpu.pins.emulation) {
*next_dbr = dbr + 1;
} else {
*next_dbr = dbr;
}
}
static inline void _clem_write_16(ClemensMachine *clem, uint16_t data, uint16_t adr, uint8_t bank) {
uint16_t adr_1 = adr + 1;
uint16_t bank_1 = adr_1 ? bank : bank + 1;
clem_write(clem, (uint8_t)data, adr, bank, CLEM_MEM_FLAG_DATA);
clem_write(clem, (uint8_t)(data >> 8), adr_1, bank_1, CLEM_MEM_FLAG_DATA);
}
static inline void _clem_write_16_wrap(ClemensMachine *clem, uint16_t data, uint16_t adr, uint8_t bank) {
clem_write(clem, (uint8_t)data, adr, bank, CLEM_MEM_FLAG_DATA);
clem_write(clem, (uint8_t)(data >> 8), adr + 1, bank, CLEM_MEM_FLAG_DATA);
}
static inline void _clem_read_16(ClemensMachine *clem, uint16_t *data16, uint16_t adr, uint8_t bank,
uint8_t flags) {
uint16_t adr_1 = adr + 1;
uint16_t bank_1 = adr_1 ? bank : bank + 1;
uint8_t tmp_data;
clem_read(clem, &tmp_data, adr, bank, flags);
*data16 = tmp_data;
clem_read(clem, &tmp_data, adr_1, bank_1, flags);
*data16 = ((uint16_t)tmp_data << 8) | (*data16);
}
static inline void _clem_read_16_wrap(ClemensMachine *clem, uint16_t *data16, uint16_t adr, uint8_t bank,
uint8_t flags) {
uint8_t tmp_data;
clem_read(clem, &tmp_data, adr, bank, flags);
*data16 = tmp_data;
clem_read(clem, &tmp_data, adr + 1, bank, flags);
*data16 = ((uint16_t)tmp_data << 8) | (*data16);
}
static inline void _clem_read_pba(ClemensMachine *clem, uint8_t *data, uint16_t *pc) {
clem_read(clem, data, (*pc)++, clem->cpu.regs.PBR, CLEM_MEM_FLAG_PROGRAM);
}
static inline void _clem_read_pba_16(ClemensMachine *clem, uint16_t *data16, uint16_t *pc) {
uint8_t tmp_data;
_clem_read_pba(clem, &tmp_data, pc);
*data16 = tmp_data;
_clem_read_pba(clem, &tmp_data, pc);
*data16 = ((uint16_t)tmp_data << 8) | (*data16);
}
static inline void _clem_read_pba_816(ClemensMachine *clem, uint16_t *out, uint16_t *pc, bool is8) {
uint8_t tmp_data;
_clem_read_pba(clem, &tmp_data, pc);
*out = tmp_data;
if (!is8) {
_clem_read_pba(clem, &tmp_data, pc);
*out = ((uint16_t)tmp_data << 8) | *out;
}
}
static inline void _clem_read_data_816(ClemensMachine *clem, uint16_t *out, uint16_t addr,
uint8_t dbr, bool is8) {
uint8_t tmp_data;
clem_read(clem, &tmp_data, addr, dbr, CLEM_MEM_FLAG_DATA);
*out = tmp_data;
if (!is8) {
uint8_t next_dbr;
++addr;
if (!addr) {
_clem_next_dbr(clem, &next_dbr, dbr);
} else {
next_dbr = dbr;
}
clem_read(clem, &tmp_data, addr, next_dbr, CLEM_MEM_FLAG_DATA);
*out = ((uint16_t)tmp_data << 8) | *out;
}
}
static inline void _clem_read_data_indexed_816(ClemensMachine *clem, uint16_t *out, uint16_t addr,
uint16_t index, uint8_t dbr, bool is_data_8,
bool is_index_8) {
uint8_t dbr_actual;
uint16_t eff_index = is_index_8 ? (index & 0xff) : index;
uint16_t eff_addr = addr + eff_index;
if (eff_addr < addr && !clem->cpu.pins.emulation) {
_clem_next_dbr(clem, &dbr_actual, dbr);
} else {
dbr_actual = dbr;
}
if (!is_index_8 || CLEM_UTIL_CROSSED_PAGE_BOUNDARY(addr, eff_addr)) {
// indexed address crossing a page boundary adds a cycle
_clem_cycle(clem);
}
_clem_read_data_816(clem, out, eff_addr, dbr_actual, is_data_8);
}
static inline void _clem_opc_push_reg_816(ClemensMachine *clem, uint16_t data, bool is8) {
struct Clemens65C816 *cpu = &clem->cpu;
_clem_cycle(clem);
if (!is8) {
clem_write(clem, (uint8_t)(data >> 8), cpu->regs.S, 0x00, CLEM_MEM_FLAG_DATA);
_cpu_sp_dec(cpu);
}
clem_write(clem, (uint8_t)(data), cpu->regs.S, 0x00, CLEM_MEM_FLAG_DATA);
_cpu_sp_dec(cpu);
}
static inline void _clem_opc_push_reg_8(ClemensMachine *clem, uint8_t data) {
struct Clemens65C816 *cpu = &clem->cpu;
_clem_cycle(clem);
clem_write(clem, data, cpu->regs.S, 0x00, CLEM_MEM_FLAG_DATA);
_cpu_sp_dec(cpu);
}
static inline void _clem_opc_pull_reg_816(ClemensMachine *clem, uint16_t *data, bool is8) {
struct Clemens65C816 *cpu = &clem->cpu;
uint8_t data8;
_clem_cycle_2(clem);
_cpu_sp_inc(cpu);
clem_read(clem, &data8, cpu->regs.S, 0x00, CLEM_MEM_FLAG_DATA);
*data = CLEM_UTIL_set16_lo(*data, data8);
if (!is8) {
_cpu_sp_inc(cpu);
clem_read(clem, &data8, cpu->regs.S, 0x00, CLEM_MEM_FLAG_DATA);
*data = CLEM_UTIL_set16_lo((uint16_t)(data8) << 8, *data);
}
}
static inline void _clem_opc_pull_reg_8(ClemensMachine *clem, uint8_t *data) {
struct Clemens65C816 *cpu = &clem->cpu;
_clem_cycle_2(clem);
_cpu_sp_inc(cpu);
clem_read(clem, data, cpu->regs.S, 0x00, CLEM_MEM_FLAG_DATA);
}
static inline void _clem_opc_push_status(ClemensMachine *clem, bool is_irq) {
uint8_t tmp_p = clem->cpu.regs.P;
if (clem->cpu.pins.emulation) {
if (is_irq) {
tmp_p &= ~kClemensCPUStatus_EmulatedBrk;
} else {
tmp_p |= kClemensCPUStatus_Index;
}
tmp_p |= kClemensCPUStatus_MemoryAccumulator;
}
clem_write(clem, tmp_p, clem->cpu.regs.S, 0x00, CLEM_MEM_FLAG_DATA);
_cpu_sp_dec(&clem->cpu);
}
static inline void _clem_opc_pull_status(ClemensMachine *clem) {
uint8_t tmp_p;
_cpu_sp_inc(&clem->cpu);
clem_read(clem, &tmp_p, clem->cpu.regs.S, 0x00, CLEM_MEM_FLAG_DATA);
if (clem->cpu.pins.emulation) {
tmp_p |= kClemensCPUStatus_MemoryAccumulator;
tmp_p |= kClemensCPUStatus_Index;
}
clem->cpu.regs.P = tmp_p;
_cpu_p_flags_apply_m_x(&clem->cpu);
}
static inline void _clem_opc_push_pc16(ClemensMachine *clem, uint16_t pc) {
uint16_t tmp_s = clem->cpu.regs.S;
// stack receives last address of operand
clem_write(clem, (uint8_t)(pc >> 8), tmp_s, 0x00, CLEM_MEM_FLAG_DATA);
--tmp_s;
if (clem->cpu.pins.emulation) {
tmp_s = CLEM_UTIL_set16_lo(clem->cpu.regs.S, tmp_s);
}
clem_write(clem, (uint8_t)pc, tmp_s, 0x00, CLEM_MEM_FLAG_DATA);
_cpu_sp_dec2(&clem->cpu);
}
/* Handle opcode addressing modes
*/
static inline void _clem_read_pba_mode_imm_816(ClemensMachine *clem, uint16_t *imm, uint16_t *pc,
bool is8) {
_clem_read_pba_816(clem, imm, pc, is8);
}
static inline void _clem_read_pba_mode_abs(ClemensMachine *clem, uint16_t *addr, uint16_t *pc) {
_clem_read_pba_16(clem, addr, pc);
}
static inline void _clem_read_pba_mode_absl(ClemensMachine *clem, uint16_t *addr, uint8_t *dbr,
uint16_t *pc) {
_clem_read_pba_16(clem, addr, pc);
_clem_read_pba(clem, dbr, pc);
}
static inline void _clem_read_pba_mode_dp(ClemensMachine *clem, uint16_t *eff_addr, uint16_t *pc,
uint8_t *offset, uint16_t index, bool is_index_8) {
uint16_t D = clem->cpu.regs.D;
uint16_t offset_index = is_index_8 ? (index & 0xff) : index;
_clem_read_pba(clem, offset, pc);
offset_index += *offset;
if (clem->cpu.pins.emulation && !(D & 0xff)) { // page boundary wrap?
*eff_addr = (D & 0xff00) + ((D & 0xff) + offset_index) % 256;
} else {
*eff_addr = D + offset_index;
}
if (D & 0x00ff) {
_clem_cycle(clem);
}
}
static inline void _clem_read_pba_mode_dp_indirect(ClemensMachine *clem, uint16_t *eff_addr,
uint16_t *pc, uint8_t *offset, uint16_t index,
bool is_index_8) {
uint16_t tmp_addr;
_clem_read_pba_mode_dp(clem, &tmp_addr, pc, offset, index, is_index_8);
_clem_read_16(clem, eff_addr, tmp_addr, 0x00, CLEM_MEM_FLAG_DATA);
}
static inline void _clem_read_pba_mode_dp_indirectl(ClemensMachine *clem, uint16_t *eff_addr,
uint8_t *eff_bank, uint16_t *pc,
uint8_t *offset, uint16_t index,
bool is_index_8) {
/* read address located in direct page offset: lo, hi, bank ->
eff_addr, eff_bank)
*/
uint16_t tmp_addr;
_clem_read_pba_mode_dp(clem, &tmp_addr, pc, offset, index, is_index_8);
_clem_read_16(clem, eff_addr, tmp_addr, 0x00, CLEM_MEM_FLAG_DATA);
// TODO: direct page wrap? (DH, DL=255 + 1 = DH, 0)?
clem_read(clem, eff_bank, tmp_addr + 2, 0x00, CLEM_MEM_FLAG_DATA);
}
static inline void _clem_read_pba_mode_stack_rel(ClemensMachine *clem, uint16_t *addr, uint16_t *pc,
uint8_t *offset) {
_clem_read_pba(clem, offset, pc);
_clem_cycle(clem); // extra IO
*addr = clem->cpu.regs.S + *offset;
}
static inline void _clem_read_pba_mode_stack_rel_indirect(ClemensMachine *clem, uint16_t *addr,
uint16_t *pc, uint8_t *offset) {
uint16_t tmp_addr;
_clem_read_pba_mode_stack_rel(clem, &tmp_addr, pc, offset);
_clem_read_16(clem, addr, tmp_addr, 0x00, CLEM_MEM_FLAG_DATA);
_clem_cycle(clem); // extra IO
}
static inline void _cpu_adc(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
uint32_t adc;
uint8_t p;
bool carry = (cpu->regs.P & kClemensCPUStatus_Carry) != 0;
if (is8) {
value = value & 0xff;
adc = (cpu->regs.A & 0xff) + value + carry;
_cpu_p_flags_n_z_data(cpu, (uint8_t)adc);
p = cpu->regs.P;
if (((cpu->regs.A & 0xff) ^ adc) & (value ^ adc) & 0x80)
p |= kClemensCPUStatus_Overflow;
else
p &= ~kClemensCPUStatus_Overflow;
if (adc & 0x100)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.A = CLEM_UTIL_set16_lo(cpu->regs.A, adc);
} else {
adc = cpu->regs.A + value + carry;
_cpu_p_flags_n_z_data_16(cpu, (uint16_t)adc);
p = cpu->regs.P;
if ((cpu->regs.A ^ adc) & (value ^ adc) & 0x8000)
p |= kClemensCPUStatus_Overflow;
else
p &= ~kClemensCPUStatus_Overflow;
if (adc & 0x10000)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.A = (uint16_t)adc;
}
cpu->regs.P = p;
}
static inline void _cpu_adc_bcd(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
/* note, invalid BCD should still function according to specific rules. see
https://math.stackexchange.com/questions/945320/why-do-we-add-6-in-bcd-addition
*/
uint32_t adc;
uint8_t p;
bool carry = (cpu->regs.P & kClemensCPUStatus_Carry) != 0;
if (is8) {
value = value & 0xff;
adc = (cpu->regs.A & 0x0f) + (value & 0x0f) + carry;
if (adc > 0x09)
adc += 0x06;
carry = adc > 0x0f;
adc = (cpu->regs.A & 0xf0) + (value & 0xf0) + (carry << 4) + (adc & 0x0f);
p = cpu->regs.P;
if (((cpu->regs.A & 0xff) ^ adc) & (value ^ adc) & 0x80)
p |= kClemensCPUStatus_Overflow;
else
p &= ~kClemensCPUStatus_Overflow;
if (adc > 0x9f)
adc += 0x60;
_cpu_p_flags_n_z_data(cpu, (uint8_t)adc);
p = cpu->regs.P;
if (adc & 0x100)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.A = CLEM_UTIL_set16_lo(cpu->regs.A, adc);
} else {
adc = (cpu->regs.A & 0x0f) + (value & 0x0f) + carry;
if (adc > 0x09)
adc += 0x06;
carry = adc > 0x0f;
adc = (cpu->regs.A & 0xf0) + (value & 0xf0) + (carry << 4) + (adc & 0x0f);
if (adc > 0x9f)
adc += 0x60;
carry = adc > 0xff;
adc = (cpu->regs.A & 0xf00) + (value & 0xf00) + (carry << 8) + (adc & 0xff);
if (adc > 0x9ff)
adc += 0x600;
carry = adc > 0xfff;
adc = (cpu->regs.A & 0xf000) + (value & 0xf000) + (carry << 12) + (adc & 0xfff);
p = cpu->regs.P;
if ((cpu->regs.A ^ adc) & (value ^ adc) & 0x8000)
p |= kClemensCPUStatus_Overflow;
else
p &= ~kClemensCPUStatus_Overflow;
if (adc > 0x9fff)
adc += 0x6000;
_cpu_p_flags_n_z_data_16(cpu, (uint16_t)adc);
p = cpu->regs.P;
if (adc & 0x10000)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.A = (uint16_t)adc;
}
cpu->regs.P = p;
}
static inline void _cpu_sbc(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
/* inverse adc implementation a + (-b) */
uint32_t adc;
uint8_t p;
bool carry = (cpu->regs.P & kClemensCPUStatus_Carry) != 0;
if (is8) {
uint16_t a = cpu->regs.A & 0xff;
value = value & 0xff;
value = value ^ 0xff; // convert to negative
adc = a + value + carry;
_cpu_p_flags_n_z_data(cpu, (uint8_t)adc);
p = cpu->regs.P;
if ((a ^ adc) & (value ^ adc) & 0x80)
p |= kClemensCPUStatus_Overflow;
else
p &= ~kClemensCPUStatus_Overflow;
if (adc & 0x100)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.A = CLEM_UTIL_set16_lo(cpu->regs.A, adc);
} else {
value = value ^ 0xffff; // negative
adc = cpu->regs.A + value + carry;
_cpu_p_flags_n_z_data_16(cpu, (uint16_t)adc);
p = cpu->regs.P;
if ((cpu->regs.A ^ adc) & (value ^ adc) & 0x8000)
p |= kClemensCPUStatus_Overflow;
else
p &= ~kClemensCPUStatus_Overflow;
if (adc & 0x10000)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.A = (uint16_t)adc;
}
cpu->regs.P = p;
}
static inline void _cpu_sbc_bcd(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
/* note, invalid BCD should still function according to specific rules. see
https://math.stackexchange.com/questions/945320/why-do-we-add-6-in-bcd-addition
*/
uint32_t a_tmp;
uint32_t sbc;
uint32_t sbc_2comp;
uint8_t p;
bool carry = (cpu->regs.P & kClemensCPUStatus_Carry) != 0;
if (is8) {
a_tmp = (cpu->regs.A & 0x00ff);
value = value & 0xff;
sbc = (a_tmp & 0x0f) - (value & 0x0f) - !carry;
if (sbc & 0x10) {
/* borrow */
sbc = (sbc - 0x06) & 0x0f;
sbc |= ((a_tmp & 0xf0) - (value & 0xf0) - 0x10);
} else {
sbc = (sbc & 0x0f);
sbc |= ((a_tmp & 0xf0) - (value & 0xf0));
}
if (sbc & 0x100)
sbc -= 0x60;
sbc_2comp = a_tmp - value - !carry;
carry = (sbc_2comp < 0x100);
_cpu_p_flags_n_z_data(cpu, (uint8_t)(sbc_2comp & 0xff));
p = cpu->regs.P;
if (((a_tmp ^ sbc_2comp) & 0x80) && ((a_tmp ^ value) & 0x80)) {
p |= kClemensCPUStatus_Overflow;
} else {
p &= ~kClemensCPUStatus_Overflow;
}
if (carry) {
p |= kClemensCPUStatus_Carry;
} else {
p &= ~kClemensCPUStatus_Carry;
}
cpu->regs.A = CLEM_UTIL_set16_lo(cpu->regs.A, (uint16_t)(sbc & 0xff));
} else {
a_tmp = cpu->regs.A;
sbc = (a_tmp & 0x0f) - (value & 0x0f) - !carry;
if (sbc & 0x10) {
/* borrow */
sbc = (sbc - 0x06) & 0x0f;
sbc |= ((a_tmp & 0xf0) - (value & 0xf0) - 0x10);
} else {
sbc = (sbc & 0x0f);
sbc |= ((a_tmp & 0xf0) - (value & 0xf0));
}
if (sbc & 0x100) {
sbc = (sbc - 0x60) & 0xff;
sbc |= ((a_tmp & 0xf00) - (value & 0xf00) - 0x100);
} else {
sbc = (sbc & 0xff);
sbc |= ((a_tmp & 0xf00) - (value & 0xf00));
}
if (sbc & 0x1000)
sbc -= 0x600;
sbc_2comp = a_tmp - value - !carry;
carry = (sbc_2comp < 0x10000);
_cpu_p_flags_n_z_data_16(cpu, (uint16_t)sbc_2comp);
p = cpu->regs.P;
if (((a_tmp ^ sbc) & 0x8000) && ((a_tmp ^ value) & 0x8000)) {
p |= kClemensCPUStatus_Overflow;
} else {
p &= ~kClemensCPUStatus_Overflow;
}
if (carry) {
p |= kClemensCPUStatus_Carry;
} else {
p &= ~kClemensCPUStatus_Carry;
}
cpu->regs.A = (uint16_t)sbc;
}
cpu->regs.P = p;
}
static inline void _cpu_asl(struct Clemens65C816 *cpu, uint16_t *value, bool is8) {
uint8_t p = cpu->regs.P;
if (is8) {
uint8_t v = (uint8_t)(*value);
if (v & 0x80)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
v <<= 1;
cpu->regs.P = p;
_cpu_p_flags_n_z_data(cpu, v);
*value = CLEM_UTIL_set16_lo(*value, v);
} else {
if (*value & 0x8000)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
*value <<= 1;
cpu->regs.P = p;
_cpu_p_flags_n_z_data_16(cpu, *value);
}
}
static inline void _cpu_rol(struct Clemens65C816 *cpu, uint16_t *value, bool is8) {
uint8_t p = cpu->regs.P;
bool next_carry;
if (is8) {
uint8_t v = (uint8_t)(*value);
next_carry = (v & 0x80) != 0;
v <<= 1;
if (p & kClemensCPUStatus_Carry)
v |= 0x01;
else
v &= 0xfe;
if (next_carry)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.P = p;
_cpu_p_flags_n_z_data(cpu, v);
*value = CLEM_UTIL_set16_lo(*value, v);
} else {
next_carry = (*value & 0x8000) != 0;
*value <<= 1;
if (p & kClemensCPUStatus_Carry)
*value |= 0x01;
else
*value &= 0xfffe;
if (next_carry)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.P = p;
_cpu_p_flags_n_z_data_16(cpu, *value);
}
}
static inline void _cpu_lsr(struct Clemens65C816 *cpu, uint16_t *value, bool is8) {
uint8_t p = cpu->regs.P;
if (is8) {
uint8_t v = (uint8_t)(*value);
if (v & 0x01)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
v >>= 1;
cpu->regs.P = p;
_cpu_p_flags_n_z_data(cpu, v);
*value = CLEM_UTIL_set16_lo(*value, v);
} else {
if (*value & 0x0001)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
*value >>= 1;
cpu->regs.P = p;
_cpu_p_flags_n_z_data_16(cpu, *value);
}
}
static inline void _cpu_ror(struct Clemens65C816 *cpu, uint16_t *value, bool is8) {
uint8_t p = cpu->regs.P;
bool next_carry = (*value & 0x01) != 0;
if (is8) {
uint8_t v = (uint8_t)(*value);
v >>= 1;
if (p & kClemensCPUStatus_Carry)
v |= 0x80;
else
v &= 0x7f;
if (next_carry)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.P = p;
_cpu_p_flags_n_z_data(cpu, v);
*value = CLEM_UTIL_set16_lo(*value, v);
} else {
*value >>= 1;
if (p & kClemensCPUStatus_Carry)
*value |= 0x8000;
else
*value &= 0x7fff;
if (next_carry)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cpu->regs.P = p;
_cpu_p_flags_n_z_data_16(cpu, *value);
}
}
static inline void _cpu_cmp(struct Clemens65C816 *cpu, uint16_t reg, uint16_t value, bool is8) {
uint32_t cmp;
uint8_t p = cpu->regs.P;
if (is8) {
value = value & 0xff;
cmp = (reg & 0xff);
if (cmp >= value)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cmp -= value;
cpu->regs.P = p;
_cpu_p_flags_n_z_data(cpu, (uint8_t)cmp);
} else {
if (reg >= value)
p |= kClemensCPUStatus_Carry;
else
p &= ~kClemensCPUStatus_Carry;
cmp = reg;
cmp -= value;
cpu->regs.P = p;
_cpu_p_flags_n_z_data_16(cpu, (uint16_t)cmp);
}
}
static inline void _cpu_and(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
uint16_t and;
if (is8) {
value = value & 0xff;
and = (cpu->regs.A & 0xff) & value;
_cpu_p_flags_n_z_data(cpu, (uint8_t) and);
cpu->regs.A = CLEM_UTIL_set16_lo(cpu->regs.A, and);
} else {
and = cpu->regs.A & value;
_cpu_p_flags_n_z_data_16(cpu, and);
cpu->regs.A = and;
}
}
static inline void _cpu_eor(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
uint16_t eor;
if (is8) {
value = value & 0xff;
eor = (cpu->regs.A & 0xff) ^ value;
_cpu_p_flags_n_z_data(cpu, (uint8_t)eor);
cpu->regs.A = CLEM_UTIL_set16_lo(cpu->regs.A, eor);
} else {
eor = cpu->regs.A ^ value;
_cpu_p_flags_n_z_data_16(cpu, eor);
cpu->regs.A = eor;
}
}
static inline void _cpu_ora(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
uint16_t ora;
if (is8) {
value = value & 0xff;
ora = (cpu->regs.A & 0xff) | value;
_cpu_p_flags_n_z_data(cpu, (uint8_t)ora);
cpu->regs.A = CLEM_UTIL_set16_lo(cpu->regs.A, ora);
} else {
ora = cpu->regs.A | value;
_cpu_p_flags_n_z_data_16(cpu, ora);
cpu->regs.A = ora;
}
}
static inline void _cpu_bit(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
if (is8) {
uint8_t v = (uint8_t)(value);
uint8_t a = (uint8_t)(cpu->regs.A);
if (v & 0x40)
cpu->regs.P |= kClemensCPUStatus_Overflow;
else
cpu->regs.P &= ~kClemensCPUStatus_Overflow;
_cpu_p_flags_n_data(cpu, v);
_cpu_p_flags_z_data(cpu, v & a);
} else {
if (value & 0x4000)
cpu->regs.P |= kClemensCPUStatus_Overflow;
else
cpu->regs.P &= ~kClemensCPUStatus_Overflow;
_cpu_p_flags_n_data_16(cpu, value);
_cpu_p_flags_z_data_16(cpu, value & cpu->regs.A);
}
}
static inline void _cpu_bit_imm(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
// immediate mode only affects the Z flag
if (is8) {
uint8_t v = (uint8_t)(value);
uint8_t a = (uint8_t)(cpu->regs.A);
_cpu_p_flags_z_data(cpu, v & a);
} else {
_cpu_p_flags_z_data_16(cpu, value & cpu->regs.A);
}
}
static inline void _cpu_inc(struct Clemens65C816 *cpu, uint16_t *value, bool is8) {
if (is8) {
uint8_t v = (uint8_t)(*value);
++v;
_cpu_p_flags_n_z_data(cpu, v);
*value = CLEM_UTIL_set16_lo(*value, v);
} else {
*value += 1;
_cpu_p_flags_n_z_data_16(cpu, *value);
}
}
static inline void _cpu_dec(struct Clemens65C816 *cpu, uint16_t *value, bool is8) {
if (is8) {
uint8_t v = (uint8_t)(*value);
--v;
_cpu_p_flags_n_z_data(cpu, v);
*value = CLEM_UTIL_set16_lo(*value, v);
} else {
*value -= 1;
_cpu_p_flags_n_z_data_16(cpu, *value);
}
}
static inline void _cpu_lda(struct Clemens65C816 *cpu, uint16_t value, bool is8) {
if (is8) {
_cpu_p_flags_n_z_data(cpu, (uint8_t)value);
cpu->regs.A = CLEM_UTIL_set16_lo(cpu->regs.A, value);
} else {
_cpu_p_flags_n_z_data_16(cpu, value);
cpu->regs.A = value;
}
}
static inline void _cpu_ldxy(struct Clemens65C816 *cpu, uint16_t *reg, uint16_t value, bool is8) {
if (is8) {
_cpu_p_flags_n_z_data(cpu, (uint8_t)value);
*reg = CLEM_UTIL_set16_lo(*reg, value);
} else {
_cpu_p_flags_n_z_data_16(cpu, value);
*reg = value;
}
}
static inline void _cpu_trb(struct Clemens65C816 *cpu, uint16_t *value, bool is8) {
if (is8) {
uint8_t v = (uint8_t)(*value);
uint8_t a = (uint8_t)(cpu->regs.A);
_cpu_p_flags_z_data(cpu, v & a);
v &= ~a;
*value = CLEM_UTIL_set16_lo(cpu->regs.A, v);
} else {
_cpu_p_flags_z_data_16(cpu, *value & cpu->regs.A);
*value &= ~cpu->regs.A;
}
}
static inline void _cpu_tsb(struct Clemens65C816 *cpu, uint16_t *value, bool is8) {
if (is8) {
uint8_t v = (uint8_t)(*value);
uint8_t a = (uint8_t)(cpu->regs.A);
_cpu_p_flags_z_data(cpu, v & a);
v |= a;
*value = CLEM_UTIL_set16_lo(cpu->regs.A, v);
} else {
_cpu_p_flags_z_data_16(cpu, *value & cpu->regs.A);
*value |= cpu->regs.A;
}
}
static inline void _clem_write_816(ClemensMachine *clem, uint16_t value, uint16_t addr, uint8_t dbr,
bool is8) {
if (is8) {
clem_write(clem, (uint8_t)value, addr, dbr, CLEM_MEM_FLAG_DATA);
} else {
_clem_write_16(clem, value, addr, dbr);
}
}
static inline void _clem_write_indexed_816(ClemensMachine *clem, uint16_t value, uint16_t addr,
uint16_t index, uint8_t dbr, bool is_data_8,
bool is_index_8) {
uint16_t eff_index = is_index_8 ? (index & 0xff) : index;
uint16_t eff_addr = addr + eff_index;
uint8_t dbr_actual;
if (eff_addr < addr && !clem->cpu.pins.emulation) {
_clem_next_dbr(clem, &dbr_actual, dbr);
} else {
dbr_actual = dbr;
}
if (is_data_8) {
clem_write(clem, (uint8_t)value, eff_addr, dbr_actual, CLEM_MEM_FLAG_DATA);
} else {
_clem_write_16(clem, value, eff_addr, dbr_actual);
}
}
static inline void _clem_branch(ClemensMachine *clem, uint16_t *pc, int8_t offset, bool do_branch) {
if (!do_branch)
return;
uint16_t tmp_addr = *pc + offset;
if (clem->cpu.pins.emulation && CLEM_UTIL_CROSSED_PAGE_BOUNDARY(*pc, tmp_addr)) {
_clem_cycle(clem);
}
_clem_cycle(clem);
*pc = tmp_addr;
}
static inline uint16_t _clem_read_interrupt_vector(ClemensMachine *clem, uint16_t lo, uint16_t hi) {
/* Tech Note # 68
http://www.1000bit.it/support/manuali/apple/technotes/iigs/tn.iigs.068.html
Note: Interrupt vectors are always pulled from ROM regardless of
whether or not the language card soft-switches have ROM enabled,
providing that the I/O shadowing for banks $00/01 is enabled
-- which it always is when running Apple IIGS or Apple II system
software
*/
uint16_t tmp_addr;
uint8_t tmp_data;
uint8_t src_bank;
if ((*clem->mem.mmio_niolc)(&clem->mem)) {
/* this should map to RAM and override language card bank mapping */
src_bank = 0x00;
} else {
/* grab the vector from ROM always */
src_bank = 0xff;
}
clem_read(clem, &tmp_data, lo, src_bank, CLEM_MEM_FLAG_PROGRAM);
tmp_addr = tmp_data;
clem_read(clem, &tmp_data, hi, src_bank, CLEM_MEM_FLAG_PROGRAM);
return ((uint16_t)tmp_data << 8) | tmp_addr;
}
static inline void _clem_irq_brk_setup(ClemensMachine *clem, uint8_t *pbr, uint16_t *pc,
uint16_t vlo, uint16_t vhi, bool is_brk) {
/*
pass PC into function since instructions modify may modify the current
PC register.
if native push PBR
push PCH, PCL
push P
irq disable, decimal mode cleared
clear PBR
3 cycles in emulation
4 cycles in native (+PBR)
*/
struct Clemens65C816 *cpu = &clem->cpu;
if (!cpu->pins.emulation) {
_clem_opc_push_reg_8(clem, cpu->regs.PBR);
}
_clem_opc_push_reg_816(clem, *pc, false);
_clem_opc_push_status(clem, !is_brk);
// 65816 always disables decimal mode on interrupts, even in emulation
cpu->regs.P &= ~kClemensCPUStatus_Decimal;
cpu->regs.P |= kClemensCPUStatus_IRQDisable;
*pbr = 0x00;
*pc = _clem_read_interrupt_vector(clem, vlo, vhi);
}
static inline uint16_t _clem_irq_brk_return(ClemensMachine *clem) {
/* called from RTI
pop P
pop PCL, PCH
if native pull PBR
+3/4 cycles (emulation/native)
*/
uint16_t tmp_addr;
uint8_t tmp_bnk0;
struct Clemens65C816 *cpu = &clem->cpu;
_clem_opc_pull_status(clem);
_clem_opc_pull_reg_816(clem, &tmp_addr, false);
if (!cpu->pins.emulation) {
_clem_opc_pull_reg_8(clem, &tmp_bnk0);
cpu->regs.PBR = tmp_bnk0;
}
return tmp_addr;
}