-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathInstructions.cpp
More file actions
747 lines (672 loc) · 16.1 KB
/
Instructions.cpp
File metadata and controls
747 lines (672 loc) · 16.1 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
#include "Cpu.h"
#include <cstdint>
#include <iostream>
/// Helper function to update a status registers zero and negative flags based on the input value
uint8_t update_zero_and_negative_flags(uint8_t value, uint8_t status)
{
uint8_t new_status;
if (value == 0)
{
// set the zero flag and unset the negative flag
new_status = (status | 0b00000010) & 0b01111111;
}
else if ((value >> 7) == 1)
{
// set the negative flag and unset the zero flag
new_status = (status | 0b10000000) & 0b11111101;
}
else
{
// unset both flags
new_status = status & 0b01111101;
}
return new_status;
}
// All instructions return true if they require an extra cycle (page boundary crossed)
/////////////////////////////////////////////////
/// Loading data from memory
/////////////////////////////////////////////////
/// Load Accumulator
bool Cpu::lda(AddressingMode am)
{
Fetched fetched = get_address_value(am);
a = fetched.value;
status = update_zero_and_negative_flags(a, status);
return fetched.page_boundary_crossed;
}
/// Load X register
bool Cpu::ldx(AddressingMode am)
{
Fetched fetched = get_address_value(am);
x = fetched.value;
status = update_zero_and_negative_flags(x, status);
return fetched.page_boundary_crossed;
}
/// Load Y register
bool Cpu::ldy(AddressingMode am)
{
Fetched fetched = get_address_value(am);
y = fetched.value;
set_flag(Flag::Z, y == 0x00);
set_flag(Flag::N, y & 0x80);
// status = update_zero_and_negative_flags(y, status);
return fetched.page_boundary_crossed;
}
/////////////////////////////////////////////////
/// Writing data to memory
/////////////////////////////////////////////////
/// Store Accumulator
bool Cpu::sta(AddressingMode am)
{
write_to_next_address(a, am);
return false;
}
/// Store X register
bool Cpu::stx(AddressingMode am)
{
write_to_next_address(x, am);
return false;
}
/// Store Y register
bool Cpu::sty(AddressingMode am)
{
write_to_next_address(y, am);
return false;
}
/////////////////////////////////////////////////
/// Transferring data between registers
/////////////////////////////////////////////////
/// Transfer Accumulator to X
bool Cpu::tax(AddressingMode am)
{
x = a;
status = update_zero_and_negative_flags(x, status);
return false;
}
/// Transfer X to Accumulator
bool Cpu::txa(AddressingMode am)
{
a = x;
status = update_zero_and_negative_flags(a, status);
return false;
}
/// Transfer Accumulator to Y
bool Cpu::tay(AddressingMode am)
{
y = a;
status = update_zero_and_negative_flags(y, status);
return false;
}
/// Transfer Y to Accumulator
bool Cpu::tya(AddressingMode am)
{
a = y;
status = update_zero_and_negative_flags(a, status);
return false;
}
/// Transfer Stack Pointer to X
bool Cpu::tsx(AddressingMode am)
{
x = sp;
status = update_zero_and_negative_flags(x, status);
return false;
}
/// Transfer X to Stack Pointer
bool Cpu::txs(AddressingMode am)
{
sp = x;
return false;
}
/////////////////////////////////////////////////
/// Stack operations
/////////////////////////////////////////////////
/// Push Accumulator onto the stack
bool Cpu::pha(AddressingMode am)
{
stack_push(a);
return false;
}
/// Pull Accumulator off the stack
bool Cpu::pla(AddressingMode am)
{
a = stack_pull();
status = update_zero_and_negative_flags(a, status);
return false;
}
/// Push Status register onto the stack
bool Cpu::php(AddressingMode am)
{
set_flag(Flag::B, true);
set_flag(Flag::U, true); // Turns out this flag is used apparently
stack_push(status);
set_flag(Flag::B, false);
set_flag(Flag::U, false);
return false;
}
/// Pull Status register off the stack
bool Cpu::plp(AddressingMode am)
{
status = stack_pull();
return false;
}
/////////////////////////////////////////////////
/// Arithmetic operations
/////////////////////////////////////////////////
/// Add with carry
bool Cpu::adc(AddressingMode am)
{
Fetched fetched = get_address_value(am);
uint16_t value = (uint16_t)a + (uint16_t)fetched.value;
if (get_flag(Flag::C))
{
value++;
}
set_flag(Flag::C, value > 255);
set_flag(Flag::Z, (value & 0x00FF) == 0);
set_flag(Flag::V, ((value ^ a) & ~(fetched.value ^ a)) & 0x0080);
set_flag(Flag::N, value & 0x80);
a = value & 0x00FF;
return fetched.page_boundary_crossed;
}
/// Subtract with carry
bool Cpu::sbc(AddressingMode am)
{
Fetched fetched = get_address_value(am);
uint16_t value = ((uint16_t)fetched.value ^ 0x00FF);
uint16_t sum = value + (uint16_t)a;
if (get_flag(Flag::C))
{
sum++;
}
set_flag(Flag::C, sum > 0xFF);
set_flag(Flag::Z, (sum & 0x00FF) == 0);
set_flag(Flag::V, (sum ^ (uint16_t)a) & (sum ^ value) & 0x0080);
set_flag(Flag::N, sum & 0x80);
a = sum & 0x00FF;
return fetched.page_boundary_crossed;
}
/// Logical AND
bool Cpu::and_(AddressingMode am)
{
Fetched fetched = get_address_value(am);
a = a & fetched.value;
status = update_zero_and_negative_flags(a, status);
return fetched.page_boundary_crossed;
}
/// Inclusive OR
bool Cpu::ora(AddressingMode am)
{
Fetched fetched = get_address_value(am);
a = a | fetched.value;
status = update_zero_and_negative_flags(a, status);
return fetched.page_boundary_crossed;
}
/// Exclusive OR
bool Cpu::eor(AddressingMode am)
{
Fetched fetched = get_address_value(am);
a = a ^ fetched.value;
status = update_zero_and_negative_flags(a, status);
return fetched.page_boundary_crossed;
}
/// Arithmetic shift left
bool Cpu::asl(AddressingMode am)
{
uint8_t value = get_address_value(am).value;
uint16_t new_value = (uint16_t)value << 1;
set_flag(Flag::C, (new_value & 0xFF00) > 0);
set_flag(Flag::Z, (new_value & 0x00FF) == 0);
set_flag(Flag::N, new_value & 0x80);
if (am == AddressingMode::Absolute || am == AddressingMode::AbsoluteX)
{
pc -= 2;
}
else
{
pc -= 1;
}
write_to_next_address(new_value & 0x00FF, am);
return false;
}
/// Logical shift right
bool Cpu::lsr(AddressingMode am)
{
uint8_t value = get_address_value(am).value;
set_flag(Flag::C, (value & 0x01));
uint16_t new_value = (uint16_t)value >> 1;
set_flag(Flag::Z, (new_value & 0x00FF) == 0);
set_flag(Flag::N, new_value & 0x80);
if (am == AddressingMode::Absolute || am == AddressingMode::AbsoluteX)
{
pc -= 2;
}
else
{
pc -= 1;
}
write_to_next_address(new_value & 0x00FF, am);
return false;
}
/// Rotate left
bool Cpu::rol(AddressingMode am)
{
uint8_t value = get_address_value(am).value;
uint16_t new_value = (uint16_t)value << 1 | get_flag(Flag::C);
set_flag(Flag::C, (new_value & 0xFF00) > 0);
set_flag(Flag::Z, (new_value & 0x00FF) == 0);
set_flag(Flag::N, new_value & 0x80);
if (am == AddressingMode::Absolute || am == AddressingMode::AbsoluteX)
{
pc -= 2;
}
else
{
pc -= 1;
}
write_to_next_address(new_value & 0x00FF, am);
return false;
}
/// Rotate right
bool Cpu::ror(AddressingMode am)
{
uint8_t value = get_address_value(am).value;
uint16_t new_value = ((uint16_t)value >> 1) | ((uint16_t)get_flag(Flag::C) << 7);
set_flag(Flag::C, (value & 0x01));
set_flag(Flag::Z, (new_value & 0x00FF) == 0);
set_flag(Flag::N, new_value & 0x80);
if (am == AddressingMode::Absolute || am == AddressingMode::AbsoluteX)
{
pc -= 2;
}
else
{
pc -= 1;
}
write_to_next_address(new_value & 0x00FF, am);
return false;
}
/// Bit test
bool Cpu::bit(AddressingMode am)
{
Fetched fetched = get_address_value(am);
uint8_t value = a & fetched.value;
set_flag(Flag::Z, value == 0);
set_flag(Flag::V, (fetched.value & 0b01000000) == 0b01000000);
set_flag(Flag::N, (fetched.value & 0b10000000) == 0b10000000);
return false;
}
/// Compare accumulator
bool Cpu::cmp(AddressingMode am)
{
Fetched fetched = get_address_value(am);
uint16_t value = (uint16_t)a - (uint16_t)fetched.value;
set_flag(Flag::C, a >= fetched.value);
set_flag(Flag::Z, (value & 0x00FF) == 0);
set_flag(Flag::N, (value & 0b10000000) == 0b10000000);
return fetched.page_boundary_crossed;
}
/// Compare X register
bool Cpu::cpx(AddressingMode am)
{
Fetched fetched = get_address_value(am);
uint16_t value = (uint16_t)x - (uint16_t)fetched.value;
set_flag(Flag::C, x >= fetched.value);
set_flag(Flag::Z, (value & 0x00FF) == 0);
set_flag(Flag::N, (value & 0b10000000) == 0b10000000);
return false;
}
/// Compare Y register
bool Cpu::cpy(AddressingMode am)
{
Fetched fetched = get_address_value(am);
uint16_t value = (uint16_t)y - (uint16_t)fetched.value;
set_flag(Flag::C, y >= fetched.value);
set_flag(Flag::Z, (value & 0x00FF) == 0);
set_flag(Flag::N, (value & 0b10000000) == 0b10000000);
return false;
}
/// Increment memory
bool Cpu::inc(AddressingMode am)
{
uint8_t value = get_address_value(am).value + 1;
status = update_zero_and_negative_flags(value, status);
if (am == AddressingMode::Absolute || am == AddressingMode::AbsoluteX)
{
pc -= 2;
}
else
{
pc -= 1;
}
write_to_next_address(value, am);
return false;
}
/// Decrement memory
bool Cpu::dec(AddressingMode am)
{
uint8_t value = get_address_value(am).value - 1;
status = update_zero_and_negative_flags(value, status);
if (am == AddressingMode::Absolute || am == AddressingMode::AbsoluteX)
{
pc -= 2;
}
else
{
pc -= 1;
}
write_to_next_address(value, am);
return false;
}
/// Increment X register
bool Cpu::inx(AddressingMode am)
{
x++;
status = update_zero_and_negative_flags(x, status);
return false;
}
/// Increment Y register
bool Cpu::iny(AddressingMode am)
{
y++;
status = update_zero_and_negative_flags(y, status);
return false;
}
/// Decrement X register
bool Cpu::dex(AddressingMode am)
{
x--;
status = update_zero_and_negative_flags(x, status);
return false;
}
/// Decrement Y register
bool Cpu::dey(AddressingMode am)
{
y--;
status = update_zero_and_negative_flags(y, status);
return false;
}
/////////////////////////////////////////////////
/// Setting flags
/////////////////////////////////////////////////
/// Clear carry flag
bool Cpu::clc(AddressingMode am)
{
set_flag(Flag::C, false);
return false;
}
/// Clear decimal mode flag
bool Cpu::cld(AddressingMode am)
{
set_flag(Flag::D, false);
return false;
}
/// Clear interrupt disable flag
bool Cpu::cli(AddressingMode am)
{
set_flag(Flag::I, false);
return false;
}
/// Clear overflow flag
bool Cpu::clv(AddressingMode am)
{
set_flag(Flag::V, false);
return false;
}
/// Set carry flag
bool Cpu::sec(AddressingMode am)
{
set_flag(Flag::C, true);
return false;
}
/// Set decimal mode
bool Cpu::sed(AddressingMode am)
{
set_flag(Flag::D, true);
return false;
}
/// Set interrupt disable flag
bool Cpu::sei(AddressingMode am)
{
set_flag(Flag::I, true);
return false;
}
/////////////////////////////////////////////////
/// Control flow
/////////////////////////////////////////////////
/// Jump
bool Cpu::jmp(AddressingMode am)
{
uint16_t addr;
switch (am)
{
case AddressingMode::Indirect:
addr = read_u16(pc);
if ((addr & 0x00FF) == 0xFF)
{
// Simulating a hardware bug where if the target address is on a page boundary
// the LSB will wrap. E.g. $01FF -> $0100
pc = ((u_int16_t)read(addr & 0xFF00) << 8) | read(addr);
}
else
{
pc = ((u_int16_t)read(addr + 1) << 8) | read(addr);
}
break;
case AddressingMode::Absolute:
addr = read_u16(pc);
pc = addr;
break;
default:
std::cerr << "Invalid addressing mode for JMP instruction\n";
exit(1);
}
return false;
}
/// Jump to subroutine
bool Cpu::jsr(AddressingMode am)
{
u_int16_t addr = read_u16(pc);
u_int16_t old_addr = pc + 1;
pc = addr;
stack_push(old_addr >> 8);
stack_push(old_addr & 0x00FF);
return false;
}
/// Return from subroutine
bool Cpu::rts(AddressingMode am)
{
uint8_t lsb = stack_pull();
uint8_t msb = stack_pull();
pc = ((u_int16_t)msb << 8) | lsb;
pc++;
return false;
}
/// Force interrupt
bool Cpu::brk(AddressingMode am)
{
pc++;
stack_push(pc >> 8);
stack_push(pc & 0x00FF);
stack_push(status);
pc = ((u_int16_t)read(0xFFFF) << 8) | read(0xFFFE);
set_flag(Flag::B, true);
return false;
}
/// Return from interrupt
bool Cpu::rti(AddressingMode am)
{
status = stack_pull();
uint8_t lsb = stack_pull();
uint8_t msb = stack_pull();
pc = ((u_int16_t)msb << 8) | lsb;
// pc++;
return false;
}
/// Branch if carry clear
bool Cpu::bcc(AddressingMode am)
{
if (!get_flag(Flag::C))
{
cycles++;
uint16_t offset = read(pc);
pc++;
if (offset & 0x80)
offset = offset | 0xFF00;
if (((pc + offset) & 0xFF00) != (pc & 0xFF00))
cycles++;
pc += offset;
}
else
{
pc++;
}
return false;
}
/// Branch if carry set
bool Cpu::bcs(AddressingMode am)
{
if (get_flag(Flag::C))
{
cycles++;
uint16_t offset = read(pc);
pc++;
if (offset & 0x80)
offset = offset | 0xFF00;
if (((pc + offset) & 0xFF00) != (pc & 0xFF00))
cycles++;
pc += offset;
}
else
{
pc++;
}
return false;
}
/// Branch if equal (zero flag set)
bool Cpu::beq(AddressingMode am)
{
if (get_flag(Flag::Z))
{
cycles++;
uint16_t offset = read(pc);
pc++;
if (offset & 0x80)
offset = offset | 0xFF00;
if (((pc + offset) & 0xFF00) != (pc & 0xFF00))
cycles++;
pc += offset;
}
else
{
pc++;
}
return false;
}
/// Branch if not equal (zero flag clear)
bool Cpu::bne(AddressingMode am)
{
if (!get_flag(Flag::Z))
{
cycles++;
uint16_t offset = read(pc);
pc++;
if (offset & 0x80)
offset = offset | 0xFF00;
if (((pc + offset) & 0xFF00) != (pc & 0xFF00))
cycles++;
pc += offset;
}
else
{
pc++;
}
return false;
}
/// Branch if minus (negative flag set)
bool Cpu::bmi(AddressingMode am)
{
if (get_flag(Flag::N))
{
cycles++;
uint16_t offset = read(pc);
pc++;
if (offset & 0x80)
offset = offset | 0xFF00;
if (((pc + offset) & 0xFF00) != (pc & 0xFF00))
cycles++;
pc += offset;
}
else
{
pc++;
}
return false;
}
/// Branch if positive (negative flag clear)
bool Cpu::bpl(AddressingMode am)
{
if (!get_flag(Flag::N))
{
cycles++;
uint16_t offset = read(pc);
pc++;
if (offset & 0x80)
offset = offset | 0xFF00;
if (((pc + offset) & 0xFF00) != (pc & 0xFF00))
cycles++;
pc += offset;
}
else
{
pc++;
}
return false;
}
/// Branch if overflow clear
bool Cpu::bvc(AddressingMode am)
{
if (!get_flag(Flag::V))
{
cycles++;
uint16_t offset = read(pc);
pc++;
if (offset & 0x80)
offset = offset | 0xFF00;
if (((pc + offset) & 0xFF00) != (pc & 0xFF00))
cycles++;
pc += offset;
}
else
{
pc++;
}
return false;
}
/// Branch if overflow set
bool Cpu::bvs(AddressingMode am)
{
if (get_flag(Flag::V))
{
cycles++;
uint16_t offset = read(pc);
pc++;
if (offset & 0x80)
offset = offset | 0xFF00;
if (((pc + offset) & 0xFF00) != (pc & 0xFF00))
cycles++;
pc += offset;
}
else
{
pc++;
}
return false;
}
/////////////////////////////////////////////////
/// Misc
/////////////////////////////////////////////////
/// No-operation (does nothing)
bool Cpu::nop(AddressingMode am)
{
return false;
}