forked from philpem/m68emu
-
Notifications
You must be signed in to change notification settings - Fork 1
/
m68_ops.c
827 lines (672 loc) · 21.2 KB
/
m68_ops.c
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
#include <assert.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include "m68emu.h"
#include "m68_internal.h"
/****************************************************************************
* HELPER FUNCTIONS
****************************************************************************/
/**
* Carry flag calculation method selection.
*
* Instructions calculate the carry flag state in different ways:
*
* * ADC, ADD: (A7 && M7) || (M7 && !R7) || (!R7 && A7)
* * ASL, LSL, ROL: b7
* * ASR, LSR, ROR: b0
* * BRCLR, BRSET: Mn
* * CLC, MUL: 0
* * NEG: R != 0
* * CMP, CPX*, SBC: (!A7 && M7) || (M7 && R7) || (R7 && !A7)
* (* CPX: substitute X7 in place of A7)
*
* The shift and negation instructions handle carry themselves.
* This means update_flags() only needs to implement Add and Subtract carry
* logic.
*
* CARRY_ADD: Addition carry
* CARRY_SUB: Subtraction carry
* CARRY_UNDEFINED: Raise an assertion failure if this is ever encountered
*
* We could in theory have CARRY_CLEAR and CARRY_SET in here, but I think it's
* preferable to have an explicit force_flags() call for the carry instead of
* hiding carry set/clear inside this call.
*/
typedef enum {
CARRY_ADD,
CARRY_SUB,
CARRY_UNDEFINED
} M68_CARRY_TYPE;
/**
* Update the CCR flag bits after an arithmetic operation.
*
* @param ctx Emulation context
* @param ccr_bits CCR bit map (OR of M68_CCR_x constants)
* @param a Accumulator initial value (or X-register for CPX)
* @param m Operation parameter
* @param r Operation result
*/
static inline void update_flags(M68_CTX *ctx, const uint8_t ccr_bits, const uint8_t a, const uint8_t m, const uint8_t r, const M68_CARRY_TYPE carryMode)
{
// Half Carry
if (ccr_bits & M68_CCR_H) {
if (( (a & 0x08) && (m & 0x08)) ||
( (m & 0x08) && !(r & 0x08)) ||
(!(r & 0x08) && (a & 0x08))) {
ctx->reg_ccr |= M68_CCR_H;
} else {
ctx->reg_ccr &= ~M68_CCR_H;
}
}
// Negative
if (ccr_bits & M68_CCR_N) {
if (r & 0x80) {
ctx->reg_ccr |= M68_CCR_N;
} else {
ctx->reg_ccr &= ~M68_CCR_N;
}
}
// Zero
if (ccr_bits & M68_CCR_Z) {
if (r == 0) {
ctx->reg_ccr |= M68_CCR_Z;
} else {
ctx->reg_ccr &= ~M68_CCR_Z;
}
}
// Carry
// TODO: Carry is calculated in different ways depending on instruction. Implement the different modes.
if (ccr_bits & M68_CCR_C) {
bool newCarry;
switch (carryMode) {
case CARRY_ADD:
newCarry =
(( (a & 0x80) && (m & 0x80)) ||
( (m & 0x80) && !(r & 0x80)) ||
(!(r & 0x80) && (a & 0x80)));
break;
case CARRY_SUB:
newCarry =
((!(a & 0x80) && (m & 0x80)) ||
( (m & 0x80) && (r & 0x80)) ||
( (r & 0x80) && !(a & 0x80)));
break;
default:
assert(0);
}
if (newCarry) {
ctx->reg_ccr |= M68_CCR_C;
} else {
ctx->reg_ccr &= ~M68_CCR_C;
}
}
// Force CCR top 3 bits to 1
ctx->reg_ccr |= 0xE0;
}
/**
* Force one or more flag bits to a given state
*
* @param ctx Emulation context
* @param ccr_bits CCR bit map (OR of M68_CCR_x constants)
* @param state State to set -- true for set, false for clear
*/
static inline void force_flags(M68_CTX *ctx, const uint8_t ccr_bits, const bool state)
{
if (state) {
ctx->reg_ccr |= ccr_bits;
} else {
ctx->reg_ccr &= ~ccr_bits;
}
// Force CCR top 3 bits to 1
ctx->reg_ccr |= 0xE0;
}
/**
* Get the state of a CCR flag bit
*
* @param ctx Emulation context
* @param ccr_bits CCR bit (M68_CCR_x constant)
*/
static inline bool get_flag(M68_CTX *ctx, const uint8_t ccr_bit)
{
return (ctx->reg_ccr & ccr_bit) ? true : false;
}
/**
* Push a byte onto the stack
*
* @param ctx Emulation context
* @param value Byte to PUSH
*/
static inline void push_byte(M68_CTX *ctx, const uint8_t value)
{
ctx->write_mem(ctx, ctx->reg_sp, value);
ctx->reg_sp = ((ctx->reg_sp - 1) & ctx->sp_and) | ctx->sp_or;
}
/**
* Pop a byte off of the stack
*
* @param ctx Emulation context
* @return Byte popped off of the stack
*/
static inline uint8_t pop_byte(M68_CTX *ctx)
{
ctx->reg_sp = ((ctx->reg_sp + 1) & ctx->sp_and) | ctx->sp_or;
return ctx->read_mem(ctx, ctx->reg_sp);
}
/****************************************************************************
* OPCODE IMPLEMENTATION
****************************************************************************/
/* **
* Opcode implementation rules:
*
* - Branch and jump opcodes (AMODE_RELATIVE, AMODE_*_JUMP)
* - Return true to take the branch.
* - Return false to continue execution from the next instruction.
* - General opcodes
* - Return 'true' to write the new value of 'param' back to the source.
*
* When the opfunc is entered, the PC will point to the next instruction. This
* is to make sure the opfunc doesn't need to worry about correcting pushed
* return addresses -- which would require a case for every opcode.
*/
/// ADC: Add with carry
static bool m68op_ADC(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = ctx->reg_acc + *param + (ctx->reg_ccr & M68_CCR_C ? 1 : 0);
update_flags(ctx, M68_CCR_H | M68_CCR_N | M68_CCR_Z | M68_CCR_C, ctx->reg_acc, *param, result, CARRY_ADD);
ctx->reg_acc = result;
// Don't write back, affects ACC and flags only
return false;
}
/// ADD: Add
static bool m68op_ADD(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = ctx->reg_acc + *param;
update_flags(ctx, M68_CCR_H | M68_CCR_N | M68_CCR_Z | M68_CCR_C, ctx->reg_acc, *param, result, CARRY_ADD);
ctx->reg_acc = result;
// Don't write back, affects ACC and flags only
return false;
}
/// AND: Logical AND
static bool m68op_AND(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = ctx->reg_acc & *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
ctx->reg_acc = result;
// Don't write back, affects ACC and flags only
return false;
}
/// ASL: Arithmetic shift left (same as LSL)
static bool m68op_ASL(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = (*param << 1) & 0xFE;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
force_flags(ctx, M68_CCR_C, *param & 0x80);
// Result is written back to the source (in-place)
*param = result;
return true;
}
/// ASR: Arithmetic shift right
static bool m68op_ASR(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = (*param >> 1) | (*param & 0x80);
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
force_flags(ctx, M68_CCR_C, *param & 1);
// Result is written back to the source (in-place)
*param = result;
return true;
}
/// BCC: Branch carry clear (same as BHS)
static bool m68op_BCC(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (!get_flag(ctx, M68_CCR_C));
}
/// BCLR: Clear bit in memory
static bool m68op_BCLR(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// Extract the bit number from the opcode
uint8_t bitnum = (opcode & 0x0F) >> 1;
*param &= ~(1 << bitnum);
// Result is written back to the source (in-place)
return true;
}
/// BCS: Branch carry set
static bool m68op_BCS(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (get_flag(ctx, M68_CCR_C));
}
/// BEQ: Branch if equal
static bool m68op_BEQ(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (get_flag(ctx, M68_CCR_Z));
}
/// BHCC: Branch if half-carry clear
static bool m68op_BHCC(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (!get_flag(ctx, M68_CCR_H));
}
/// BHCS: Branch if half-carry set
static bool m68op_BHCS(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (get_flag(ctx, M68_CCR_H));
}
/// BHI: Branch if higher
static bool m68op_BHI(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (!get_flag(ctx, M68_CCR_C) && !get_flag(ctx, M68_CCR_Z));
}
// BHS: see BCC
/// BIH: Branch if /IRQ high
static bool m68op_BIH(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (ctx->irq);
}
/// BIL: Branch if /IRQ low
static bool m68op_BIL(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (!ctx->irq);
}
/// BIT: Bit test memory with accumulator
static bool m68op_BIT(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = ctx->reg_acc & *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
// Don't change the memory or the accumulator
return *param;
}
// BLO: see BCS
/// BLS: Branch if lower or same
static bool m68op_BLS(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (get_flag(ctx, M68_CCR_C) || get_flag(ctx, M68_CCR_Z));
}
/// BNC: Branch if interrupt mask is clear
static bool m68op_BMC(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (!get_flag(ctx, M68_CCR_I));
}
/// BMI: Branch if minus
static bool m68op_BMI(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (get_flag(ctx, M68_CCR_N));
}
/// BMS: Branch if interrupt mask is set
static bool m68op_BMS(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (get_flag(ctx, M68_CCR_I));
}
/// BNE: Branch if not equal
static bool m68op_BNE(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (!get_flag(ctx, M68_CCR_Z));
}
/// BPL: Branch if plus
static bool m68op_BPL(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return (!get_flag(ctx, M68_CCR_N));
}
/// BRA: Branch always
static bool m68op_BRA(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// Always take the branch
return true;
}
/// BRCLR: Branch if bit clear
static bool m68op_BRCLR(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// Extract the bit number from the opcode
uint8_t bitnum = (opcode & 0x0F) >> 1;
// Carry flag is set to the bit state
force_flags(ctx, M68_CCR_C, *param & (1 << bitnum) ? 1 : 0);
return !(*param & (1 << bitnum));
}
/// BRN: Branch never
static bool m68op_BRN(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// Don't take the branch
return false;
}
/// BRSET: Branch if bit set
static bool m68op_BRSET(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// Extract the bit number from the opcode
uint8_t bitnum = (opcode & 0x0F) >> 1;
// Carry flag is set to the bit state
force_flags(ctx, M68_CCR_C, *param & (1 << bitnum) ? 1 : 0);
return (*param & (1 << bitnum));
}
/// BSET: Bit set
static bool m68op_BSET(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// Extract the bit number from the opcode
uint8_t bitnum = (opcode & 0x0F) >> 1;
*param |= (1 << bitnum);
// Result is written back to the source (in-place)
return true;
}
/// BSR: Branch to subroutine
static bool m68op_BSR(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// push return address onto stack
uint16_t ra = ctx->pc_next;
push_byte(ctx, ra & 0xff);
push_byte(ctx, (ra >> 8) & 0xff);
// take the branch
return true;
}
/// CLC: Clear carry
static bool m68op_CLC(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
force_flags(ctx, M68_CCR_C, 0);
// Inherent operation, nothing to write back
return false;
}
/// CLI: Clear interrupt mask
static bool m68op_CLI(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
force_flags(ctx, M68_CCR_I, 0);
// Inherent operation, nothing to write back
return false;
}
/// CLR: Clear accumulator, X or register
static bool m68op_CLR(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
force_flags(ctx, M68_CCR_N, 0);
force_flags(ctx, M68_CCR_Z, 1);
// Result is written back to the source (in-place)
*param = 0;
return true;
}
/// CMP: Compare accumulator with memory
static bool m68op_CMP(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint8_t result = ctx->reg_acc - *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z | M68_CCR_C, ctx->reg_acc, *param, result, CARRY_SUB);
// CMP affects flags only, not the parameter or accumulator
return false;
}
/// COM: Complement
static bool m68op_COM(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint8_t result = ~*param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
force_flags(ctx, M68_CCR_C, 1);
// Result is written back to the source (in-place)
*param = result;
return true;
}
/// CPX: Compare index register with memory
static bool m68op_CPX(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint8_t result = ctx->reg_x - *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z | M68_CCR_C, ctx->reg_x, *param, result, CARRY_SUB);
// CPX affects flags only, not the parameter or accumulator
return false;
}
/// DEC: Decrement
static bool m68op_DEC(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint8_t result = *param - 1;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
// Result is written back to the source (in-place)
*param = result;
return true;
}
/// EOR: Exclusive-OR accumulator with memory
static bool m68op_EOR(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint8_t result = ctx->reg_acc ^ *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
ctx->reg_acc = result;
// Don't write back, affects ACC and flags only
return false;
}
/// INC: Increment
static bool m68op_INC(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint8_t result = *param + 1;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
// Result is written back to the source (in-place)
*param = result;
return true;
}
/// JMP: Long jump
static bool m68op_JMP(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
return true;
}
/// JSR: Jump subroutine
static bool m68op_JSR(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// push return address onto stack
uint16_t ra = ctx->pc_next;
push_byte(ctx, ra & 0xff);
push_byte(ctx, (ra >> 8) & 0xff);
// take the branch
return true;
}
/// LDA: Load accumulator
static bool m68op_LDA(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
ctx->reg_acc = *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, *param, CARRY_UNDEFINED);
// Don't write back, affects ACC and flags only
return false;
}
/// LDX: Load index
static bool m68op_LDX(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
ctx->reg_x = *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, *param, CARRY_UNDEFINED);
// Don't write back, affects X and flags only
return false;
}
/// LSR: Logical shift right
static bool m68op_LSR(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = (*param >> 1);
update_flags(ctx, M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
force_flags(ctx, M68_CCR_N, 0);
force_flags(ctx, M68_CCR_C, *param & 1);
// Result is written back to the source (in-place)
*param = result;
return true;
}
/// MUL: Multiply
static bool m68op_MUL(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = (uint16_t)ctx->reg_x * (uint16_t)ctx->reg_acc;
ctx->reg_x = (result >> 8) & 0xff;
ctx->reg_acc = result & 0xff;
force_flags(ctx, M68_CCR_H | M68_CCR_C, 0);
// Inherent operation, nothing to write back
return false;
}
/// NEG: Negate (2's complement)
static bool m68op_NEG(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = 0 - *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
force_flags(ctx, M68_CCR_C, result != 0);
ctx->reg_acc = result;
// Don't write back, affects ACC and flags only
return false;
}
/// NOP: No operation
static bool m68op_NOP(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// Do nothing
return false;
}
/// ORA: Inclusive-OR
static bool m68op_ORA(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = ctx->reg_acc | *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
ctx->reg_acc = result;
// Don't write back, affects ACC and flags only
return false;
}
/// ROL: Rotate left
static bool m68op_ROL(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = ((*param << 1) | (get_flag(ctx, M68_CCR_C) ? 1 : 0)) & 0xFF;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
force_flags(ctx, M68_CCR_C, *param & 0x80);
// Result is written back to the source (in-place)
*param = result;
return true;
}
/// ROR: Rotate right
static bool m68op_ROR(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = ((*param >> 1) | (get_flag(ctx, M68_CCR_C) ? 0x80 : 0)) & 0xFF;
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, result, CARRY_UNDEFINED);
force_flags(ctx, M68_CCR_C, *param & 1);
// Result is written back to the source (in-place)
*param = result;
return true;
}
/// RSP: Reset stack pointer
static bool m68op_RSP(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
ctx->reg_sp = 0xFF; // TODO INITIAL_SP constant?
// Inherent operation, nothing to write back
return false;
}
/// RTI: Return from interrupt
static bool m68op_RTI(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// pop CCR, ACCA, X
ctx->reg_ccr = pop_byte(ctx);
ctx->reg_acc = pop_byte(ctx);
ctx->reg_x = pop_byte(ctx);
// pop PCH:PCL
// This is split up to force instruction sequencing (ref C99, sequence points!)
uint16_t new_pc;
new_pc = (uint16_t)pop_byte(ctx) << 8;
new_pc |= pop_byte(ctx);
ctx->pc_next = new_pc & ctx->pc_and;
// Inherent operation, nothing to write back
return false;
}
/// RTS: Return from subroutine
static bool m68op_RTS(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// pop PCH:PCL
// This is split up to force instruction sequencing (ref C99, sequence points!)
uint16_t new_pc;
new_pc = (uint16_t)pop_byte(ctx) << 8;
new_pc |= pop_byte(ctx);
ctx->pc_next = new_pc & ctx->pc_and;
// Inherent operation, nothing to write back
return false;}
/// SBC: Subtract with carry
static bool m68op_SBC(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = ctx->reg_acc - *param - (ctx->reg_ccr & M68_CCR_C ? 1 : 0);
update_flags(ctx, M68_CCR_N | M68_CCR_Z | M68_CCR_C, ctx->reg_acc, *param, result, CARRY_SUB);
ctx->reg_acc = result;
// Don't write back, affects ACC and flags only
return false;
}
/// SEC: Set carry flag
static bool m68op_SEC(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
force_flags(ctx, M68_CCR_C, 1);
// Inherent operation, nothing to write back
return false;
}
/// SEI: Set interrupt mask flag (disable interrupts)
static bool m68op_SEI(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
force_flags(ctx, M68_CCR_I, 1);
// Inherent operation, nothing to write back
return false;
}
/// STA: Store accumulator
static bool m68op_STA(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_acc, *param, ctx->reg_acc, CARRY_UNDEFINED);
// Accumulator is written to memory
*param = ctx->reg_acc;
return true;
}
/// STOP: Enable IRQs, stop oscillator
static bool m68op_STOP(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
force_flags(ctx, M68_CCR_I, 0);
ctx->is_stopped = true;
// Inherent operation, nothing to write back
return false;
}
/// STX: Store X register
static bool m68op_STX(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
update_flags(ctx, M68_CCR_N | M68_CCR_Z, ctx->reg_x, *param, ctx->reg_x, CARRY_UNDEFINED);
// X register is written to memory
*param = ctx->reg_x;
return true;
}
/// SUB: Subtract
static bool m68op_SUB(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
uint16_t result = ctx->reg_acc - *param;
update_flags(ctx, M68_CCR_N | M68_CCR_Z | M68_CCR_C, ctx->reg_acc, *param, result, CARRY_SUB);
ctx->reg_acc = result;
// Don't write back, affects ACC and flags only
return false;
}
/// SWI: Software Interrupt
static bool m68op_SWI(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
// TODO Lift this code for use by IRQ?
// PC will already have been advanced by the emulation loop
push_byte(ctx, ctx->pc_next & 0xFF);
push_byte(ctx, ctx->pc_next >> 8);
push_byte(ctx, ctx->reg_x);
push_byte(ctx, ctx->reg_acc);
push_byte(ctx, ctx->reg_ccr);
// Mask further interrupts
force_flags(ctx, M68_CCR_I, 1);
// Vector fetch
uint16_t vector;
vector = (uint16_t)ctx->read_mem(ctx, 0xFFFC & ctx->pc_and) << 8;
vector |= ctx->read_mem(ctx, 0xFFFD & ctx->pc_and);
ctx->pc_next = vector;
// Inherent operation, nothing to write back
return false;
}
/// TAX: Transfer A to X
static bool m68op_TAX(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
ctx->reg_x = ctx->reg_acc;
// Inherent operation, nothing to write back
return false;
}
/// TST: Test if negative or zero
static bool m68op_TST(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
update_flags(ctx, M68_CCR_N | M68_CCR_Z, 0, 0, *param, CARRY_UNDEFINED);
// Contents of the tested register or memory location are left unaltered.
return false;
}
/// TXA: Transfer X to ACC
static bool m68op_TXA(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
ctx->reg_acc = ctx->reg_x;
// Inherent operation, nothing to write back
return false;
}
/// WAIT: Wait for interrupt. Like STOP, but leaves peripherals running.
static bool m68op_WAIT(M68_CTX *ctx, const uint8_t opcode, uint8_t *param)
{
force_flags(ctx, M68_CCR_I, 0);
ctx->is_waiting = true;
// Inherent operation, nothing to write back
return false;
}
/****************************************************************************
* OPCODE TABLES
****************************************************************************/
#include "m68_optab_hc05.h"