1
+ import java .util .Arrays ;
2
+
1
3
public class CPU {
2
4
public String [] opcodes ;
3
5
@@ -23,6 +25,8 @@ public class CPU {
23
25
public CPU () {
24
26
reset ();
25
27
28
+ Arrays .fill (lookup , new Instruction ("XXX" ,"IMP" ,2 ));
29
+
26
30
//ADC
27
31
lookup [0x69 ] = new Instruction ("ADC" ,"IMM" ,2 );
28
32
lookup [0x65 ] = new Instruction ("ADC" ,"ZPP" ,3 );
@@ -54,7 +58,7 @@ public CPU() {
54
58
55
59
lookup [0xF0 ] = new Instruction ("BEQ" ,"REL" ,2 );
56
60
57
- lookup [0x24 ] = new Instruction ("BIT" ,"ZP0 " ,3 );
61
+ lookup [0x24 ] = new Instruction ("BIT" ,"ZPP " ,3 );
58
62
lookup [0x2C ] = new Instruction ("BIT" ,"ABS" ,4 );
59
63
60
64
lookup [0x30 ] = new Instruction ("BMI" ,"REL" ,2 );
@@ -308,7 +312,7 @@ void clock() {
308
312
this .getClass ().getMethod (lookup [Byte .toUnsignedInt (opcode )].opcode ).invoke (this );
309
313
} catch (Exception e ) {e .printStackTrace ();}
310
314
}
311
-
315
+ EaterEmulator . clocks ++;
312
316
cycles --;
313
317
}
314
318
@@ -326,6 +330,8 @@ void reset() {
326
330
byte hi = Bus .read ((short )(addressAbsolute +1 ));
327
331
programCounter = (short )(Byte .toUnsignedInt (lo )+256 *Byte .toUnsignedInt (hi ));
328
332
333
+ EaterEmulator .clocks = 0 ;
334
+
329
335
addressRelative = 0 ;
330
336
addressAbsolute = 0 ;
331
337
fetched = 0 ;
@@ -335,16 +341,16 @@ void reset() {
335
341
336
342
void irq () {
337
343
if (!getFlag ('I' )) {
338
- Bus .write ((short )(0x0100 +stackPointer ), (byte )((programCounter >>8 )&0x00FF ));
344
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), (byte )((programCounter >>8 )&0x00FF ));
339
345
stackPointer --;
340
- Bus .write ((short )(0x0100 +stackPointer ), (byte )(programCounter &0x00FF ));
346
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), (byte )(programCounter &0x00FF ));
341
347
stackPointer --;
342
348
343
349
setFlag ('B' ,false );
344
350
setFlag ('U' ,false );
345
351
setFlag ('I' ,true );
346
352
347
- Bus .write ((short )(0x0100 +stackPointer ), flags );
353
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), flags );
348
354
stackPointer --;
349
355
350
356
addressAbsolute = (short )0xFFFE ;
@@ -357,16 +363,16 @@ void irq() {
357
363
}
358
364
359
365
void nmi () {
360
- Bus .write ((short )(0x0100 +stackPointer ), (byte )((programCounter >>8 )&0x00FF ));
366
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), (byte )((programCounter >>8 )&0x00FF ));
361
367
stackPointer --;
362
- Bus .write ((short )(0x0100 +stackPointer ), (byte )(programCounter &0x00FF ));
368
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), (byte )(programCounter &0x00FF ));
363
369
stackPointer --;
364
370
365
371
setFlag ('B' ,false );
366
372
setFlag ('U' ,false );
367
373
setFlag ('I' ,true );
368
374
369
- Bus .write ((short )(0x0100 +stackPointer ), flags );
375
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), flags );
370
376
stackPointer --;
371
377
372
378
addressAbsolute = (short )0xFFFA ;
@@ -394,7 +400,7 @@ public void IMM() {
394
400
addressAbsolute = programCounter ++;
395
401
}
396
402
397
- public void ZP0 () {
403
+ public void ZPP () {
398
404
addressAbsolute = Bus .read (programCounter );
399
405
programCounter ++;
400
406
addressAbsolute &= 0x00FF ;
@@ -604,13 +610,13 @@ public void BPL() {
604
610
public void BRK () {
605
611
programCounter ++;
606
612
setFlag ('I' ,true );
607
- Bus .write ((short )(0x0100 +stackPointer ), (byte )((programCounter >>8 )&0x00FF ));
613
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), (byte )((programCounter >>8 )&0x00FF ));
608
614
stackPointer --;
609
- Bus .write ((short )(0x0100 +stackPointer ), (byte )(programCounter &0x00FF ));
615
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), (byte )(programCounter &0x00FF ));
610
616
stackPointer --;
611
617
612
618
setFlag ('B' ,true );
613
- Bus .write ((short )(0x0100 +stackPointer ), flags );
619
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), flags );
614
620
stackPointer --;
615
621
setFlag ('B' ,false );
616
622
@@ -621,7 +627,7 @@ public void BRK() {
621
627
}
622
628
623
629
public void BVC () {
624
- if (getFlag ('V' )) {
630
+ if (! getFlag ('V' )) {
625
631
cycles ++;
626
632
addressAbsolute = (short )(programCounter +addressRelative );
627
633
@@ -633,7 +639,7 @@ public void BVC() {
633
639
}
634
640
635
641
public void BVS () {
636
- if (! getFlag ('V' )) {
642
+ if (getFlag ('V' )) {
637
643
cycles ++;
638
644
addressAbsolute = (short )(programCounter +addressRelative );
639
645
@@ -742,9 +748,9 @@ public void JMP() {
742
748
public void JSR () {
743
749
programCounter --;
744
750
745
- Bus .write ((short )(0x0100 +stackPointer ), (byte )((programCounter >>8 )&0x00FF ));
751
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), (byte )((programCounter >>8 )&0x00FF ));
746
752
stackPointer --;
747
- Bus .write ((short )(0x0100 +stackPointer ), (byte )(programCounter &0x00FF ));
753
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), (byte )(programCounter &0x00FF ));
748
754
stackPointer --;
749
755
750
756
programCounter = addressAbsolute ;
@@ -801,27 +807,27 @@ public void ORA() {
801
807
}
802
808
803
809
public void PHA () {
804
- Bus .write ((short )(0x0100 +stackPointer ), a );
810
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), a );
805
811
stackPointer --;
806
812
}
807
813
808
814
public void PHP () {
809
- Bus .write ((short )(0x0100 +stackPointer ), (byte )(flags |0b00001100));
815
+ Bus .write ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ), (byte )(flags |0b00001100));
810
816
setFlag ('B' ,false );
811
817
setFlag ('U' ,false );
812
818
stackPointer --;
813
819
}
814
820
815
821
public void PLA () {
816
822
stackPointer ++;
817
- a = Bus .read ((short )(0x0100 +stackPointer ));
823
+ a = Bus .read ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ));
818
824
setFlag ('Z' , a == 0 );
819
825
setFlag ('N' , (a & 0x80 ) == 0x80 );
820
826
}
821
827
822
828
public void PLP () {
823
829
stackPointer ++;
824
- flags = Bus .read ((short )(0x0100 +stackPointer ));
830
+ flags = Bus .read ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ));
825
831
setFlag ('U' , true );
826
832
}
827
833
@@ -853,21 +859,21 @@ public void ROR() {
853
859
854
860
public void RTI () {
855
861
stackPointer ++;
856
- flags = Bus .read ((short )(0x0100 +stackPointer ));
862
+ Bus .read ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ));
857
863
flags = (byte )(flags & ~(getFlag ('B' ) ? 0b00000100 : 0 ));
858
864
flags = (byte )(flags & ~(getFlag ('U' ) ? 0b00000100 : 0 ));
859
865
860
866
stackPointer ++;
861
- programCounter = Bus .read ((short )(0x0100 +stackPointer ));
867
+ programCounter = Bus .read ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ));
862
868
stackPointer ++;
863
- programCounter |= Bus .read ((short )(0x0100 +stackPointer )) << 8 ;
869
+ programCounter |= Bus .read ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) )) << 8 ;
864
870
}
865
871
866
872
public void RTS () {
867
873
stackPointer ++;
868
- programCounter = Bus .read ((short )(0x0100 +stackPointer ));
874
+ programCounter = Bus .read ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) ));
869
875
stackPointer ++;
870
- programCounter |= Bus .read ((short )(0x0100 +stackPointer )) << 8 ;
876
+ programCounter |= Bus .read ((short )(0x0100 +Byte . toUnsignedInt ( stackPointer ) )) << 8 ;
871
877
872
878
programCounter ++;
873
879
}
@@ -943,6 +949,6 @@ public void TYA() {
943
949
}
944
950
945
951
public void XXX () {
946
- System .out .println ("Illegal Opcode!" );
952
+ System .out .println ("Illegal Opcode! (" + ROMLoader . byteToHexString ( opcode ) + ") - " + lookup [ Byte . toUnsignedInt ( opcode )]. opcode );
947
953
}
948
954
}
0 commit comments