-
Notifications
You must be signed in to change notification settings - Fork 3
/
computer.rs
1744 lines (1544 loc) · 65.3 KB
/
computer.rs
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
use std::sync::mpsc;
use std::time;
use std::thread;
mod decode;
#[derive(Clone, Debug)]
pub struct Info {
pub msg: String,
pub qty: u64,
}
const LOG_LEVEL:i16 = 0;
const OUTPUT_BTM:u16 = 0xf000;
const OUTPUT_TOP:u16 = 0xf100;
#[derive(Eq, Hash, PartialEq, Clone, Copy, Debug)]
pub enum ADRESSING_MODE {
IMMEDIATE = 0,
ZERO_PAGE = 1,
ZERO_PAGE_X = 2,
ABSOLUTE = 3,
ABSOLUTE_X = 4,
ABSOLUTE_Y = 5,
INDIRECT_X = 6,
INDIRECT_Y = 7,
INDIRECT = 8,
ZERO_PAGE_Y = 9,
ACCUMULATOR = 10,
NONE = 11,
}
pub enum ControllerMessage {
ButtonPressed(String),
UpdatedProcessorAvailable(Processor),
UpdatedDataAvailable(u16, u8),
UpdatedStackAvailable(Vec<u8>),
UpdatedOutputAvailable(Vec<u8>),
}
pub enum ComputerMessage {
ButtonPressed(String),
GetData(),
}
#[derive(Clone, Debug)]
pub struct Processor {
pub flags: u8,
pub acc: u8,
pub rx: u8,
pub ry: u8,
pub pc: u16,
pub sp: u8,
pub test: Vec<u8>,
pub info: Vec<Info>,
pub clock: u64,
pub inst: u8,
}
#[derive(Debug)]
pub struct Computer {
processor: Processor,
paused: bool,
step: bool,
start: bool,
speed: u64,
data: Vec<u8>,
tx: mpsc::Sender<ControllerMessage>,
rx: mpsc::Receiver<ComputerMessage>,
}
const FLAG_C: u8 = 1;
const FLAG_Z: u8 = 2;
const FLAG_I: u8 = 4;
const FLAG_D: u8 = 8;
const FLAG_O: u8 = 0x40;
const FLAG_N: u8 = 0x80;
impl Computer {
pub fn new(tx: mpsc::Sender<ControllerMessage>, rx: mpsc::Receiver<ComputerMessage>, data: Vec<u8>) -> Computer {
let mut computer = Computer {
data,
tx,
rx,
paused: true,
start: true,
step: false,
speed: 0,
processor: Processor {
flags: 0b00110000,
acc: 0,
rx: 0,
ry: 0,
/// Start at 0x400
pc: 0x400,
sp: 0,
test: vec![],
info: vec![],
clock: 0,
inst: 0xea,
}
};
computer
}
pub fn step(&mut self) -> bool {
while let Some(message) = self.rx.try_iter().next() {
// Handle messages arriving from the controller.
match message {
ComputerMessage::ButtonPressed(btn) => {
if btn == "faster" && self.speed > 0 {
if (self.speed >= 4) {
self.speed /= 2;
} else if self.speed >= 1 {
self.speed -= 1;
}
} else if btn == "slower" && self.speed <= 10000 {
if (self.speed >= 2) {
self.speed *= 2;
} else {
self.speed += 2;
}
} else if btn == "pause" {
self.paused = !self.paused;
} else if btn == "step" {
self.step = true;
}
},
ComputerMessage::GetData() => {
//if (self.paused && self.step) || self.start {
self.start = false;
let l = self.processor.info.len();
if l > 30 {
self.processor.info = self.processor.info[l-30..].to_vec();
}
//println!("{:?}", self.processor);
self.processor.test = self.data[0x200 as usize .. 0x220 as usize].to_vec();
self.tx.send(
ControllerMessage::UpdatedProcessorAvailable(self.processor.clone())
);
let stack = self.data[0x100 as usize..=0x1ff as usize].to_vec();
self.tx.send(
ControllerMessage::UpdatedStackAvailable(stack)
);
let output = self.data[OUTPUT_BTM as usize..OUTPUT_TOP as usize].to_vec();
self.tx.send(
ControllerMessage::UpdatedOutputAvailable(output)
);
//}
},
};
}
if self.paused && !self.step {
thread::sleep(time::Duration::from_millis(100));
return true;
}
if (self.paused && self.step) || !self.paused {
self.step = false;
let changed = self.run_instruction();
if self.speed > 0 {
thread::sleep(time::Duration::from_millis(self.speed));
}
}
true
}
fn run_instruction(&mut self) {
let inst = &self.data[(self.processor.pc) as usize];
self.processor.inst = *inst;
let opcode = decode::get_opcode_name(self.processor.inst);
//self.add_info(format!("{:#x} - running instruction {} ({:#x})", self.processor.pc, opcode, inst));
match opcode {
"ADC" => self.adc(),
"AND" => self.and(),
"ASL" => self.asl(),
"BCC" => self.bcc(),
"BCS" => self.bcs(),
"BEQ" => self.beq(),
"BIT" => self.bit(),
"BMI" => self.bmi(),
"BNE" => self.bne(),
"BPL" => self.bpl(),
"BRK" => self.brk(),
"BVC" => self.bvc(),
"BVS" => self.bvs(),
"CLC" => self.clc(),
"CLD" => self.cld(),
"CLI" => self.cli(),
"CLV" => self.clv(),
"CMP" => self.cmp(),
"CPX" => self.cpx(),
"CPY" => self.cpy(),
"DEC" => self.dec(),
"DEX" => self.dex(),
"DEY" => self.dey(),
"EOR" => self.eor(),
"INC" => self.inc(),
"INX" => self.inx(),
"INY" => self.iny(),
"JMP" => self.jmp(),
"JSR" => self.jsr(),
"LDA" => self.lda(),
"LDX" => self.ldx(),
"LDY" => self.ldy(),
"LSR" => self.lsr(),
"NOP" => self.nop(),
"ORA" => self.ora(),
"PHA" => self.pha(),
"PHP" => self.php(),
"PLA" => self.pla(),
"PLP" => self.plp(),
"ROL" => self.rol(),
"ROR" => self.ror(),
"RTI" => self.rti(),
"RTS" => self.rts(),
"SBC" => self.sbc(),
"SEC" => self.sec(),
"SED" => self.sed(),
"SEI" => self.sei(),
"STA" => self.sta(),
"STX" => self.stx(),
"STY" => self.sty(),
"TAX" => self.tax(),
"TAY" => self.tay(),
"TSX" => self.tsx(),
"TXA" => self.txa(),
"TXS" => self.txs(),
"TYA" => self.tya(),
_ => {
//// println!("Running instruction nop : {:x?}", inst);
self.nop();
},
};
}
fn add_info(&mut self, info: String) {
let len = self.processor.info.len();
if len > 0 && self.processor.info[len-1].msg == info {
let last_element = self.processor.info.pop().unwrap();
self.processor.info.push(Info {msg: info, qty: last_element.qty + 1});
self.paused = true;
} else {
self.processor.info.push(Info {msg: info, qty: 1});
}
}
fn cld(&mut self) {
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction cld: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.flags = self.processor.flags & !FLAG_D;
self.processor.clock += 2;
}
fn txs(&mut self) {
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction txs: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
self.processor.sp = self.processor.rx;
}
fn tsx(&mut self) {
self.processor.flags = Self::set_flags( self.processor.flags, self.processor.sp);
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction tsx: {:#x} val: {:#x} flags:{:#x} ", self.processor.pc, self.data[(self.processor.pc) as usize], self.processor.sp, self.processor.flags));
}
self.processor.pc += 1;
self.processor.clock += 2;
self.processor.rx = self.processor.sp;
}
fn tya(&mut self) {
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction tya: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
self.processor.acc = self.processor.ry;
self.processor.flags = Self::set_flags(self.processor.flags, self.processor.acc);
}
fn tay(&mut self) {
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction tay: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
self.processor.ry = self.processor.acc;
self.processor.flags = Self::set_flags(self.processor.flags, self.processor.ry);
}
fn tax(&mut self) {
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction tax: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
self.processor.rx = self.processor.acc;
self.processor.flags = Self::set_flags(self.processor.flags, self.processor.rx);
}
fn txa(&mut self) {
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction txa: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.flags = Self::set_flags(self.processor.flags, self.processor.rx);
self.processor.pc += 1;
self.processor.clock += 2;
self.processor.acc = self.processor.rx;
}
/// Jump to subroutine
fn jsr(&mut self) {
// Place current address on stack
let sp: u16 = (self.processor.sp as u16 + 0x100 as u16).into();
let sp1: u16 = (self.processor.sp.wrapping_sub(1) as u16 + 0x100 as u16).into();
let mut _addr = self.data.to_vec();
let this_pc = self.processor.pc + 2;
_addr[sp as usize] = ((this_pc>>8) & 0xff) as u8;
_addr[sp1 as usize] = (this_pc & 0xff) as u8;
self.data = _addr;
// Send to new address
let addr = self.get_word(self.processor.pc + 1);
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction jsr to: {:#x}", self.processor.pc, addr));
}
self.processor.sp = self.processor.sp.wrapping_sub(2);
self.processor.pc = addr;
}
fn brk(&mut self) {
let sp: u16 = (self.processor.sp as u16 + 0x100 as u16).into();
let sp1: u16 = (self.processor.sp.wrapping_sub(1) as u16 + 0x100 as u16).into();
let sp2: u16 = (self.processor.sp.wrapping_sub(2) as u16 + 0x100 as u16).into();
let mut _addr = self.data.to_vec();
let this_pc = self.processor.pc + 2;
_addr[sp as usize] = ((this_pc>>8) & 0xff) as u8;
_addr[sp1 as usize] = (this_pc & 0xff) as u8;
_addr[sp2 as usize] = (self.processor.flags) | 0x30;
self.processor.flags |= FLAG_I;
self.processor.sp = self.processor.sp.wrapping_sub(3);
self.data = _addr;
let new_addr: u16 = self.get_word(0xfffe);
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction brk ({:#x}) to: {:#x} flags: {:#b}", self.processor.pc, self.processor.inst, new_addr, self.processor.flags));
}
self.processor.pc = new_addr;
self.processor.clock += 7;
}
fn rti(&mut self) {
// Place current address on stack
let sp1: u16 = (self.processor.sp.wrapping_add(1) as u16 + 0x100 as u16).into();
let sp2: u16 = (self.processor.sp.wrapping_add(2) as u16 + 0x100 as u16).into();
let sp3: u16 = (self.processor.sp.wrapping_add(3) as u16 + 0x100 as u16).into();
let high_byte = self.data[sp3 as usize];
let low_byte = self.data[sp2 as usize];
let flags = self.data[sp1 as usize];
// Unset interrupt disabled flag
self.processor.flags = flags;
let addr: u16 = low_byte as u16 | ((high_byte as u16) << 8) as u16;
// Send to new address
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction rti to: {:#x} flags: {:#x}", self.processor.pc, addr, self.processor.flags));
}
self.processor.sp = self.processor.sp.wrapping_add(3);
self.processor.pc = addr;
self.processor.clock += 6;
}
fn rts(&mut self) {
// Place current address on stack
let sp1: u16 = (self.processor.sp.wrapping_add(1) as u16 + 0x100 as u16).into();
let sp2: u16 = (self.processor.sp.wrapping_add(2) as u16 + 0x100 as u16).into();
let low_byte = self.data[sp1 as usize];
let high_byte = self.data[sp2 as usize];
let addr: u16 = low_byte as u16 | ((high_byte as u16) << 8) as u16;
// Send to new address
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction rts to: {:#x}", self.processor.pc, addr));
}
self.processor.sp = self.processor.sp.wrapping_add(2);
self.processor.pc = addr + 1;
self.processor.clock += 6;
}
/// Clear carry flag
fn clc(&mut self) {
self.processor.flags &= !FLAG_C;
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction clc: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
}
/// Set carry flag
fn sec(&mut self) {
self.processor.flags |= FLAG_C;
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction sec: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
}
/// Set decimal flag
fn sed(&mut self) {
self.processor.flags |= FLAG_D;
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction sed: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
}
/// Clear interrupt disabled flag
fn cli(&mut self) {
self.processor.flags &= !FLAG_I;
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction cli: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
}
/// Set interrupt disabled flag
fn sei(&mut self) {
self.processor.flags |= FLAG_I;
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction sei: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
}
/// clear overflow flag
fn clv(&mut self) {
self.processor.flags &= !FLAG_O;
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction clv: {:#x}", self.processor.pc, self.data[(self.processor.pc) as usize]));
}
self.processor.pc += 1;
self.processor.clock += 2;
}
/// Push accumulator to stack
fn pha(&mut self) {
let addr: u16 = (self.processor.sp as u16 + 0x100 as u16).into();
let mut _addr = self.data.to_vec();
_addr[addr as usize] = self.processor.acc;
self.tx.send(
ControllerMessage::UpdatedDataAvailable(addr, self.processor.acc)
);
self.data = _addr;
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction pha at: {:#x} val: {:#x}", self.processor.pc, addr, self.processor.acc));
}
self.processor.sp = self.processor.sp.wrapping_sub(1);
self.processor.pc += 1;
self.processor.clock += 3;
}
/// Push flags to stack
fn php(&mut self) {
let addr: u16 = (self.processor.sp as u16 + 0x100 as u16).into();
let mut _addr = self.data.to_vec();
_addr[addr as usize] = self.processor.flags | 0x30;
self.tx.send(
ControllerMessage::UpdatedDataAvailable(addr, self.processor.flags | 0x30)
);
self.data = _addr;
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction php at: {:#x} flags: {:#x}", self.processor.pc, addr, self.processor.flags | 0x30));
}
self.processor.sp = self.processor.sp.wrapping_sub(1);
self.processor.pc += 1;
self.processor.clock += 3;
}
/// Pull stack to accumulator
fn pla(&mut self) {
self.processor.sp = self.processor.sp.wrapping_add(1);
let addr: u16 = (self.processor.sp as u16 + 0x100 as u16).into();
self.processor.acc = self.data[addr as usize];
let flags = self.processor.flags;
self.processor.flags = Self::set_flags(flags, self.processor.acc);
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction pla at: {:#x} val: {:#x}", self.processor.pc, addr, self.processor.acc));
}
self.processor.pc += 1;
self.processor.clock += 4;
}
// 0X28 Pull value from the stack into the processor registers
fn plp(&mut self) {
self.processor.sp = self.processor.sp.wrapping_add(1);
let addr: u16 = (self.processor.sp as u16 + 0x100 as u16).into();
self.processor.flags = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction plp at: {:#x} flags: {:#x}", self.processor.pc, addr, self.processor.flags));
}
self.processor.pc += 1;
self.processor.clock += 4;
}
fn get_ld_adddr(&mut self, addressing_mode: ADRESSING_MODE) -> u16 {
if LOG_LEVEL > 3 {
let inst = self.data[self.processor.pc as usize];
self.add_info(format!("{:#x} - Getting address with mode {:?} for inst {:#x}", self.processor.pc, addressing_mode, inst));
}
if addressing_mode == ADRESSING_MODE::IMMEDIATE {
return self.processor.pc + 1;
} else if addressing_mode == ADRESSING_MODE::ABSOLUTE {
//Absolute adressing
let start = self.processor.pc + 1;
let addr = self.get_word(start);
return addr;
} else if addressing_mode == ADRESSING_MODE::ABSOLUTE_X {
//Absolute adressing
let start = self.processor.pc + 1;
let start_addr = self.get_word(start);
let rx = self.processor.rx;
let addr: u16 = start_addr + (rx as u16);
if LOG_LEVEL > 2 {
self.add_info(format!("{:#x} - Getting absolute_x address from: {:#x} ry: {:#x} gives: {:#x}", self.processor.pc, start_addr, rx, addr));
}
return addr;
} else if addressing_mode == ADRESSING_MODE::ABSOLUTE_Y {
//Absolute adressing
let start = self.processor.pc + 1;
let start_addr = self.get_word(start);
let ry = self.processor.ry;
let addr: u16 = start_addr + (ry as u16);
if LOG_LEVEL > 2 {
self.add_info(format!("{:#x} - Getting absolute_y address from: {:#x} ry: {:#x} gives: {:#x}", self.processor.pc, start_addr, ry, addr));
}
return addr;
} else if addressing_mode == ADRESSING_MODE::ZERO_PAGE {
//Absolute adressing
let start = self.processor.pc + 1;
let addr: u16 = self.data[start as usize].into();
if LOG_LEVEL > 2 {
self.add_info(format!("{:#x} - Getting ZERO_PAGE address from: {:#x} gives: {:#x}", self.processor.pc, start, addr));
}
return addr;
} else if addressing_mode == ADRESSING_MODE::ZERO_PAGE_Y {
//Absolute adressing
let start = self.processor.pc + 1;
let start_addr = self.data[start as usize].wrapping_add(self.processor.ry);
let addr: u16 = start_addr.into();
if LOG_LEVEL > 2 {
self.add_info(format!("{:#x} - Getting ZERO_PAGE_Y address from: {:#x} with ry: {:#x} gives: {:#x}", self.processor.pc, start, self.processor.ry, addr));
}
return addr;
} else if addressing_mode == ADRESSING_MODE::ZERO_PAGE_X {
//Absolute adressing
let start = self.processor.pc + 1;
let start_addr = self.data[start as usize].wrapping_add(self.processor.rx);
let addr: u16 = start_addr.into();
if LOG_LEVEL > 2 {
self.add_info(format!("{:#x} - Getting ZERO_PAGE_X address from: {:#x} with rx: {:#x} gives: {:#x}", self.processor.pc, start, self.processor.rx, addr));
}
return addr;
} else if addressing_mode == ADRESSING_MODE::INDIRECT_Y {
//Absolute adressing
let start = self.processor.pc + 1;
let zp_addr = self.data[start as usize];
let base_addr = self.get_word(zp_addr.into());
let addr: u16 = base_addr + self.processor.ry as u16;
if LOG_LEVEL > 2 {
self.add_info(format!("{:#x} - Getting INDIRECT_Y address from: {:#x} with ry: {:#x} gives: {:#x}", self.processor.pc, start, self.processor.ry, addr));
}
return addr;
} else if addressing_mode == ADRESSING_MODE::INDIRECT_X {
//Absolute adressing
let start = self.processor.pc + 1;
let zp_addr = self.data[start as usize].wrapping_add(self.processor.rx);
let addr: u16 = self.get_word(zp_addr.into());
if LOG_LEVEL > 2 {
self.add_info(format!("{:#x} - Getting INDIRECT_X address from: {:#x} with ry: {:#x} gives: {:#x}", self.processor.pc, start, self.processor.ry, addr));
}
return addr;
}
return 0;
}
fn inc(&mut self) {
let addressing_mode = decode::get_adressing_mode(self.processor.inst);
let mut value: u8 = 0;
let mode = addressing_mode;
let addr = self.get_ld_adddr(mode);
if addressing_mode == ADRESSING_MODE::ZERO_PAGE || addressing_mode == ADRESSING_MODE::ZERO_PAGE_X {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction inc ZP with effective addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 2;
self.processor.clock += 5;
} else if addressing_mode == ADRESSING_MODE::ABSOLUTE || addressing_mode == ADRESSING_MODE::ABSOLUTE_X {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction inc ABS with effective addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 3;
self.processor.clock += 6;
}
let result = value.wrapping_add(1);
let mut _addr = self.data.to_vec();
_addr[addr as usize] = result;
self.data = _addr;
self.tx.send(
ControllerMessage::UpdatedDataAvailable(addr, result)
);
self.processor.flags = Self::set_flags(self.processor.flags, result);
}
fn dec(&mut self) {
let addressing_mode = decode::get_adressing_mode(self.processor.inst);
let mut value: u8 = 0;
let mode = addressing_mode;
let addr = self.get_ld_adddr(mode);
if addressing_mode == ADRESSING_MODE::ZERO_PAGE || addressing_mode == ADRESSING_MODE::ZERO_PAGE_X {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction dec ZP with effective addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 2;
self.processor.clock += 5;
} else if addressing_mode == ADRESSING_MODE::ABSOLUTE || addressing_mode == ADRESSING_MODE::ABSOLUTE_X {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction dec ABS with effective addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 3;
self.processor.clock += 6;
}
let result = value.wrapping_sub(1);
self.tx.send(
ControllerMessage::UpdatedDataAvailable(addr, result)
);
let mut _addr = self.data.to_vec();
_addr[addr as usize] = result;
self.data = _addr;
self.processor.flags = Self::set_flags(self.processor.flags, result);
}
fn ldx(&mut self) {
let addressing_mode = decode::get_adressing_mode(self.processor.inst);
let mut value: u8 = 0;
let mode = addressing_mode;
let addr = self.get_ld_adddr(mode);
if addressing_mode == ADRESSING_MODE::IMMEDIATE {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction ldx val: {:#x}", self.processor.pc, value));
}
self.processor.pc += 2;
self.processor.clock += 2;
} else if addressing_mode == ADRESSING_MODE::ABSOLUTE || addressing_mode == ADRESSING_MODE::ABSOLUTE_X || addressing_mode == ADRESSING_MODE::ABSOLUTE_Y {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction ldx absolute with addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 3;
self.processor.clock += 4;
}else if addressing_mode == ADRESSING_MODE::ZERO_PAGE || addressing_mode == ADRESSING_MODE::ZERO_PAGE_Y {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction ldx ZP with effective addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 2;
self.processor.clock += 3;
}
self.processor.rx = value;
self.processor.flags = Self::set_flags(self.processor.flags, self.processor.rx);
}
fn ldy(&mut self) {
let addressing_mode = decode::get_adressing_mode(self.processor.inst);
let mut value: u8 = 0;
let mode = addressing_mode;
let addr = self.get_ld_adddr(mode);
if addressing_mode == ADRESSING_MODE::IMMEDIATE {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction ldy val: {:#x}", self.processor.pc, value));
}
self.processor.pc += 2;
self.processor.clock += 2;
} else if addressing_mode == ADRESSING_MODE::ABSOLUTE || addressing_mode == ADRESSING_MODE::ABSOLUTE_X || addressing_mode == ADRESSING_MODE::ABSOLUTE_Y {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction ldy absolute with addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 3;
self.processor.clock += 4;
} else if addressing_mode == ADRESSING_MODE::ZERO_PAGE || addressing_mode == ADRESSING_MODE::ZERO_PAGE_X {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction ldy ZP with effective addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 2;
self.processor.clock += 3;
}
self.processor.ry = value;
self.processor.flags = Self::set_flags(self.processor.flags, self.processor.ry);
}
fn lda(&mut self) {
let addressing_mode = decode::get_adressing_mode(self.processor.inst);
let mut value: u8 = 0;
let mode = addressing_mode;
let addr = self.get_ld_adddr(mode);
if addressing_mode == ADRESSING_MODE::IMMEDIATE {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction lda val: {:#x}", self.processor.pc, value));
}
self.processor.pc += 2;
self.processor.clock += 2;
} else if addressing_mode == ADRESSING_MODE::ABSOLUTE || addressing_mode == ADRESSING_MODE::ABSOLUTE_X|| addressing_mode == ADRESSING_MODE::ABSOLUTE_Y {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction lda absolute with addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 3;
self.processor.clock += 4;
} else if addressing_mode == ADRESSING_MODE::ZERO_PAGE || addressing_mode == ADRESSING_MODE::ZERO_PAGE_X {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction lda ZP with effective addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 2;
self.processor.clock += 3;
} else if addressing_mode == ADRESSING_MODE::INDIRECT_Y || addressing_mode == ADRESSING_MODE::INDIRECT_X {
value = self.data[addr as usize];
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction lda INDIRECT with effective addr: {:#x} and val: {:#x}", self.processor.pc, addr, value));
}
self.processor.pc += 2;
self.processor.clock += 5;
} else {
panic!("This adressing mode is not implemented yet, sorry");
}
self.processor.acc = value;
self.processor.flags = Self::set_flags(self.processor.flags, value);
}
fn asl(&mut self) {
let addressing_mode = decode::get_adressing_mode(self.processor.inst);
let mode = addressing_mode;
let mut value: u8 = 0;
let addr = self.get_ld_adddr(mode);
if (mode == ADRESSING_MODE::ACCUMULATOR) {
value = self.processor.acc;
self.processor.pc += 1;
self.processor.clock += 2;
} else if mode == ADRESSING_MODE::ABSOLUTE || mode == ADRESSING_MODE::ABSOLUTE_X {
self.processor.pc += 3;
self.processor.clock += 6;
value = self.data[addr as usize];
} else {
self.processor.pc += 2;
self.processor.clock += 6;
value = self.data[addr as usize];
}
if value >> 7 & 1 == 1 {
self.processor.flags |= FLAG_C;
} else {
self.processor.flags &= !FLAG_C;
}
if value == 0 {
self.processor.flags |= FLAG_Z;
} else {
self.processor.flags &= !FLAG_Z;
}
let result = value << 1;
if result >> 7 & 1 == 1 {
self.processor.flags |= FLAG_N;
} else {
self.processor.flags &= !FLAG_N;
}
if (mode == ADRESSING_MODE::ACCUMULATOR) {
self.processor.acc = result;
} else {
self.tx.send(
ControllerMessage::UpdatedDataAvailable(addr, result)
);
let mut _addr = self.data.to_vec();
_addr[addr as usize] = result;
self.data = _addr;
}
}
fn lsr(&mut self) {
let addressing_mode = decode::get_adressing_mode(self.processor.inst);
let mode = addressing_mode;
let mut value: u8 = 0;
let addr = self.get_ld_adddr(mode);
if mode == ADRESSING_MODE::ACCUMULATOR {
value = self.processor.acc;
} else {
value = self.data[addr as usize];
}
let old_flags = self.processor.flags;
if value & 1 == 1 {
self.processor.flags |= FLAG_C;
} else {
self.processor.flags &= !FLAG_C;
}
if value == 0 {
self.processor.flags |= FLAG_Z;
} else {
self.processor.flags &= !FLAG_Z;
}
let result = value >> 1;
if result >> 7 & 1 == 1 {
self.processor.flags |= FLAG_N;
} else {
self.processor.flags &= !FLAG_N;
}
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction lsr val: {:#x} result: {:#x} flags: {:#x} old flags: {:#x}", self.processor.pc, value, result, self.processor.flags, old_flags));
}
if mode == ADRESSING_MODE::ACCUMULATOR {
self.processor.pc += 1;
self.processor.clock += 2;
self.processor.acc = result;
} else if mode == ADRESSING_MODE::ABSOLUTE || mode == ADRESSING_MODE::ABSOLUTE_X {
self.processor.pc += 3;
self.processor.clock += 6;
self.tx.send(
ControllerMessage::UpdatedDataAvailable(addr, result)
);
let mut _addr = self.data.to_vec();
_addr[addr as usize] = result;
self.data = _addr;
} else {
self.processor.pc += 2;
self.processor.clock += 5;
let mut _addr = self.data.to_vec();
self.tx.send(
ControllerMessage::UpdatedDataAvailable(addr, result)
);
_addr[addr as usize] = result;
self.data = _addr;
}
}
fn rol(&mut self) {
let addressing_mode = decode::get_adressing_mode(self.processor.inst);
let mode = addressing_mode;
let mut value: u8 = 0;
let addr = self.get_ld_adddr(mode);
if mode == ADRESSING_MODE::ACCUMULATOR {
value = self.processor.acc;
self.processor.pc += 1;
self.processor.clock += 2;
} else if mode == ADRESSING_MODE::ABSOLUTE || mode == ADRESSING_MODE::ABSOLUTE_X {
value = self.processor.acc;
self.processor.pc += 3;
self.processor.clock += 6;
} else {
self.processor.pc += 2;
self.processor.clock += 6;
value = self.data[addr as usize];
}
let old_flags = self.processor.flags;
let result = (value << 1) | (self.processor.flags & FLAG_C);
if value >> 7 & 1 == 1 {
self.processor.flags |= FLAG_C;
} else {
self.processor.flags &= !FLAG_C;
}
if result == 0 {
self.processor.flags |= FLAG_Z;
} else {
self.processor.flags &= !FLAG_Z;
}
if result >> 7 & 1 == 1 {
self.processor.flags |= FLAG_N;
} else {
self.processor.flags &= !FLAG_N;
}
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction rol val: {:#x} result: {:#x} flags: {:#x} old flags: {:#x}", self.processor.pc, value, result, self.processor.flags, old_flags));
}
if (mode == ADRESSING_MODE::ACCUMULATOR) {
self.processor.acc = result;
} else {
let mut _addr = self.data.to_vec();
self.tx.send(
ControllerMessage::UpdatedDataAvailable(addr, result)
);
_addr[addr as usize] = result;
self.data = _addr;
}
}
fn ror(&mut self) {
let addressing_mode = decode::get_adressing_mode(self.processor.inst);
let mode = addressing_mode;
let mut value: u8 = 0;
let addr = self.get_ld_adddr(mode);
if mode == ADRESSING_MODE::ACCUMULATOR {
value = self.processor.acc;
self.processor.pc += 1;
self.processor.clock += 2;
} else if mode == ADRESSING_MODE::ABSOLUTE || mode == ADRESSING_MODE::ABSOLUTE_X {
value = self.processor.acc;
self.processor.pc += 3;
self.processor.clock += 6;
} else {
self.processor.pc += 2;
self.processor.clock += 6;
value = self.data[addr as usize];
}
let old_flags = self.processor.flags;
let result = (value >> 1) | ((self.processor.flags & FLAG_C) << 7);
if value & 1 == 1 {
self.processor.flags |= FLAG_C;
} else {
self.processor.flags &= !FLAG_C;
}
if result == 0 {
self.processor.flags |= FLAG_Z;
} else {
self.processor.flags &= !FLAG_Z;
}
if result >> 7 & 1 == 1 {
self.processor.flags |= FLAG_N;
} else {
self.processor.flags &= !FLAG_N;
}
if LOG_LEVEL > 0 {
self.add_info(format!("{:#x} - Running instruction ror val: {:#x} result: {:#x} flags: {:#x} old flags: {:#x}", self.processor.pc, value, result, self.processor.flags, old_flags));
}
if mode == ADRESSING_MODE::ACCUMULATOR {
self.processor.acc = result;
} else {
let mut _addr = self.data.to_vec();
self.tx.send(
ControllerMessage::UpdatedDataAvailable(addr, result)