@@ -217,7 +217,7 @@ public CPU() {
217
217
lookup [0x91 ] = new Instruction ("STA" ,"IZY" ,6 );
218
218
219
219
lookup [0x86 ] = new Instruction ("STX" ,"ZPP" ,3 );
220
- lookup [0x96 ] = new Instruction ("STX" ,"ZPX " ,4 );
220
+ lookup [0x96 ] = new Instruction ("STX" ,"ZPY " ,4 );
221
221
lookup [0x8E ] = new Instruction ("STX" ,"ABS" ,4 );
222
222
223
223
lookup [0x84 ] = new Instruction ("STY" ,"ZPP" ,3 );
@@ -241,50 +241,50 @@ void setFlag(char flag, boolean condition) {
241
241
flag = Character .toUpperCase (flag );
242
242
switch (flag ) {
243
243
case 'C' :
244
- flags = setBit (flags ,7 ,condition );
244
+ flags = setBit (flags ,0 ,condition );
245
245
break ;
246
246
case 'Z' :
247
- flags = setBit (flags ,6 ,condition );
247
+ flags = setBit (flags ,1 ,condition );
248
248
break ;
249
249
case 'I' :
250
- flags = setBit (flags ,5 ,condition );
250
+ flags = setBit (flags ,2 ,condition );
251
251
break ;
252
252
case 'D' :
253
- flags = setBit (flags ,4 ,condition );
253
+ flags = setBit (flags ,3 ,condition );
254
254
break ;
255
255
case 'B' :
256
- flags = setBit (flags ,3 ,condition );
256
+ flags = setBit (flags ,4 ,condition );
257
257
break ;
258
258
case 'U' :
259
- flags = setBit (flags ,2 ,condition );
259
+ flags = setBit (flags ,5 ,condition );
260
260
break ;
261
261
case 'V' :
262
- flags = setBit (flags ,1 ,condition );
262
+ flags = setBit (flags ,6 ,condition );
263
263
break ;
264
264
case 'N' :
265
- flags = setBit (flags ,0 ,condition );
265
+ flags = setBit (flags ,7 ,condition );
266
266
break ;
267
267
}
268
268
}
269
269
270
270
boolean getFlag (char flag ) {
271
271
flag = Character .toUpperCase (flag );
272
272
switch (flag ) {
273
- case 'C ' :
273
+ case 'N ' :
274
274
return ((flags &0b10000000) == 0b10000000);
275
- case 'Z ' :
275
+ case 'V ' :
276
276
return ((flags &0b01000000) == 0b01000000);
277
- case 'I ' :
277
+ case 'U ' :
278
278
return ((flags &0b00100000) == 0b00100000);
279
- case 'D' :
280
- return ((flags &0b00010000) == 0b00010000);
281
279
case 'B' :
280
+ return ((flags &0b00010000) == 0b00010000);
281
+ case 'D' :
282
282
return ((flags &0b00001000) == 0b00001000);
283
- case 'U ' :
283
+ case 'I ' :
284
284
return ((flags &0b00000100) == 0b00000100);
285
- case 'V ' :
285
+ case 'Z ' :
286
286
return ((flags &0b00000010) == 0b00000010);
287
- case 'N ' :
287
+ case 'C ' :
288
288
return ((flags &0b00000001) == 0b00000001);
289
289
}
290
290
System .out .println ("Something has gone wrong in getFlag!" );
@@ -417,13 +417,13 @@ public void ZPP() {
417
417
}
418
418
419
419
public void ZPX () {
420
- addressAbsolute = (short )(Bus .read (programCounter )+ x );
420
+ addressAbsolute = (short )(Byte . toUnsignedInt ( Bus .read (programCounter ))+ Byte . toUnsignedInt ( x ) );
421
421
programCounter ++;
422
422
addressAbsolute &= 0x00FF ;
423
423
}
424
424
425
425
public void ZPY () {
426
- addressAbsolute = (short )(Bus .read (programCounter )+ y );
426
+ addressAbsolute = (short )(Byte . toUnsignedInt ( Bus .read (programCounter ))+ Byte . toUnsignedInt ( y ) );
427
427
programCounter ++;
428
428
addressAbsolute &= 0x00FF ;
429
429
}
@@ -450,7 +450,7 @@ public void ABX() {
450
450
byte hi = Bus .read (programCounter );
451
451
programCounter ++;
452
452
453
- addressAbsolute = (short )(Byte .toUnsignedInt (lo )+256 *Byte .toUnsignedInt (hi )+x );
453
+ addressAbsolute = (short )(Byte .toUnsignedInt (lo )+256 *Byte .toUnsignedInt (hi )+Byte . toUnsignedInt ( x ) );
454
454
455
455
if ((addressAbsolute & 0xFF00 ) != (hi <<8 ))
456
456
additionalCycles ++;
@@ -462,7 +462,7 @@ public void ABY() {
462
462
byte hi = Bus .read (programCounter );
463
463
programCounter ++;
464
464
465
- addressAbsolute = (short )(Byte .toUnsignedInt (lo )+256 *Byte .toUnsignedInt (hi )+y );
465
+ addressAbsolute = (short )(Byte .toUnsignedInt (lo )+256 *Byte .toUnsignedInt (hi )+Byte . toUnsignedInt ( y ) );
466
466
467
467
if ((addressAbsolute & 0xFF00 ) != (hi <<8 ))
468
468
additionalCycles ++;
@@ -496,7 +496,7 @@ public void IZY() {
496
496
byte lo = Bus .read ((short )(t &0x00FF ));
497
497
byte hi = Bus .read ((short )((t +1 )&0x00FF ));
498
498
499
- addressAbsolute = (short )(Byte .toUnsignedInt (lo )+256 *Byte .toUnsignedInt (hi )+y );
499
+ addressAbsolute = (short )(Byte .toUnsignedInt (lo )+256 *Byte .toUnsignedInt (hi )+Byte . toUnsignedInt ( y ) );
500
500
501
501
if ((addressAbsolute & 0xFF00 ) != (hi <<8 ))
502
502
additionalCycles ++;
@@ -619,16 +619,18 @@ public void BPL() {
619
619
620
620
public void BRK () {
621
621
programCounter ++;
622
- setFlag ( 'I' , true );
623
- Bus .write ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )), (byte )(( programCounter >>8 )& 0x00FF ));
622
+
623
+ Bus .write ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )), (byte )(programCounter >>8 ));
624
624
stackPointer --;
625
- Bus .write ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )), (byte )(programCounter & 0x00FF ));
625
+ Bus .write ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )), (byte )(programCounter ));
626
626
stackPointer --;
627
627
628
628
setFlag ('B' ,true );
629
+ setFlag ('U' ,true );
629
630
Bus .write ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )), flags );
630
631
stackPointer --;
631
- setFlag ('B' ,false );
632
+ //setFlag('B',false);
633
+ setFlag ('I' ,true );
632
634
633
635
addressAbsolute = (short )0xFFFE ;
634
636
byte lo = Bus .read (addressAbsolute );
@@ -822,7 +824,7 @@ public void PHA() {
822
824
}
823
825
824
826
public void PHP () {
825
- Bus .write ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )), (byte )(flags |0b00001100 ));
827
+ Bus .write ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )), (byte )(flags |0b00110000 ));
826
828
setFlag ('B' ,false );
827
829
setFlag ('U' ,false );
828
830
stackPointer --;
@@ -869,21 +871,23 @@ public void ROR() {
869
871
870
872
public void RTI () {
871
873
stackPointer ++;
872
- Bus .read ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )));
873
- flags = (byte )(flags & ~ (getFlag ('B' ) ? 0b00000100 : 0 ));
874
- flags = (byte )(flags & ~ (getFlag ('U' ) ? 0b00000100 : 0 ));
874
+ flags = Bus .read ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )));
875
+ flags = (byte )(flags & (getFlag ('B' ) ? 0b11101111 : 0 ));
876
+ flags = (byte )(flags & (getFlag ('U' ) ? 0b11011111 : 0 ));
875
877
876
878
stackPointer ++;
877
- programCounter = Bus .read ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )));
879
+ byte lo = Bus .read ((short )(0x100 +Byte .toUnsignedInt (stackPointer )));
878
880
stackPointer ++;
879
- programCounter |= Bus .read ((short )(0x0100 +Byte .toUnsignedInt (stackPointer ))) << 8 ;
881
+ byte hi = Bus .read ((short )(0x100 +Byte .toUnsignedInt (stackPointer )));
882
+ programCounter = (short )(Byte .toUnsignedInt (lo )+256 *Byte .toUnsignedInt (hi ));
880
883
}
881
884
882
885
public void RTS () {
883
886
stackPointer ++;
884
- programCounter = Bus .read ((short )(0x0100 +Byte .toUnsignedInt (stackPointer )));
887
+ byte lo = Bus .read ((short )(0x100 +Byte .toUnsignedInt (stackPointer )));
885
888
stackPointer ++;
886
- programCounter |= Bus .read ((short )(0x0100 +Byte .toUnsignedInt (stackPointer ))) << 8 ;
889
+ byte hi = Bus .read ((short )(0x100 +Byte .toUnsignedInt (stackPointer )));
890
+ programCounter = (short )(Byte .toUnsignedInt (lo )+256 *Byte .toUnsignedInt (hi ));
887
891
888
892
programCounter ++;
889
893
}
0 commit comments