@@ -11,6 +11,8 @@ public class CPU {
1111 public byte stackPointer = 0x00 ;
1212 public short programCounter = 0x0000 ;
1313
14+ public boolean debug = false ;
15+
1416 public short addressAbsolute = 0x0000 ;
1517 public short addressRelative = 0x0000 ;
1618 public byte opcode = 0x00 ;
@@ -117,10 +119,10 @@ public CPU() {
117119 lookup [0x41 ] = new Instruction ("EOR" ,"IZX" ,6 );
118120 lookup [0x51 ] = new Instruction ("EOR" ,"IZY" ,5 );
119121
120- lookup [0xE6 ] = new Instruction ("INC" ,"IMM " ,5 );
121- lookup [0xF6 ] = new Instruction ("INC" ,"ZPP " ,6 );
122- lookup [0xEE ] = new Instruction ("INC" ,"ZPX " ,6 );
123- lookup [0xFE ] = new Instruction ("INC" ,"ABS " ,7 );
122+ lookup [0xE6 ] = new Instruction ("INC" ,"ZPP " ,5 );
123+ lookup [0xF6 ] = new Instruction ("INC" ,"ZPX " ,6 );
124+ lookup [0xEE ] = new Instruction ("INC" ,"ABS " ,6 );
125+ lookup [0xFE ] = new Instruction ("INC" ,"ABX " ,7 );
124126
125127 lookup [0xE8 ] = new Instruction ("INX" ,"IMP" ,2 );
126128
@@ -148,11 +150,11 @@ public CPU() {
148150
149151 lookup [0xA0 ] = new Instruction ("LDY" ,"IMM" ,2 );
150152 lookup [0xA4 ] = new Instruction ("LDY" ,"ZPP" ,3 );
151- lookup [0xB4 ] = new Instruction ("LDY" ,"ZPY " ,4 );
153+ lookup [0xB4 ] = new Instruction ("LDY" ,"ZPX " ,4 );
152154 lookup [0xAC ] = new Instruction ("LDY" ,"ABS" ,4 );
153- lookup [0xBC ] = new Instruction ("LDY" ,"ABY " ,4 );
155+ lookup [0xBC ] = new Instruction ("LDY" ,"ABX " ,4 );
154156
155- lookup [0x4A ] = new Instruction ("LSR" ,"IMM " ,2 );
157+ lookup [0x4A ] = new Instruction ("LSR" ,"IMP " ,2 );
156158 lookup [0x46 ] = new Instruction ("LSR" ,"ZPP" ,5 );
157159 lookup [0x56 ] = new Instruction ("LSR" ,"ZPX" ,6 );
158160 lookup [0x4E ] = new Instruction ("LSR" ,"ABS" ,6 );
@@ -177,17 +179,17 @@ public CPU() {
177179
178180 lookup [0x28 ] = new Instruction ("PLP" ,"IMP" ,4 );
179181
180- lookup [0x2A ] = new Instruction ("ROR " ,"IMM " ,2 );
181- lookup [0x26 ] = new Instruction ("ROR " ,"ZPP" ,5 );
182- lookup [0x36 ] = new Instruction ("ROR " ,"ZPX" ,6 );
183- lookup [0x2E ] = new Instruction ("ROR " ,"ABS" ,6 );
184- lookup [0x3E ] = new Instruction ("ROR " ,"ABX" ,7 );
182+ lookup [0x2A ] = new Instruction ("ROL " ,"IMP " ,2 );
183+ lookup [0x26 ] = new Instruction ("ROL " ,"ZPP" ,5 );
184+ lookup [0x36 ] = new Instruction ("ROL " ,"ZPX" ,6 );
185+ lookup [0x2E ] = new Instruction ("ROL " ,"ABS" ,6 );
186+ lookup [0x3E ] = new Instruction ("ROL " ,"ABX" ,7 );
185187
186- lookup [0x6A ] = new Instruction ("ROL " ,"IMM " ,2 );
187- lookup [0x66 ] = new Instruction ("ROL " ,"ZPP" ,5 );
188- lookup [0x76 ] = new Instruction ("ROL " ,"ZPX" ,6 );
189- lookup [0x6E ] = new Instruction ("ROL " ,"ABS" ,6 );
190- lookup [0x7E ] = new Instruction ("ROL " ,"ABX" ,7 );
188+ lookup [0x6A ] = new Instruction ("ROR " ,"IMP " ,2 );
189+ lookup [0x66 ] = new Instruction ("ROR " ,"ZPP" ,5 );
190+ lookup [0x76 ] = new Instruction ("ROR " ,"ZPX" ,6 );
191+ lookup [0x6E ] = new Instruction ("ROR " ,"ABS" ,6 );
192+ lookup [0x7E ] = new Instruction ("ROR " ,"ABX" ,7 );
191193
192194 lookup [0x40 ] = new Instruction ("RTI" ,"IMP" ,6 );
193195
@@ -312,6 +314,28 @@ void clock() {
312314 this .getClass ().getMethod (lookup [Byte .toUnsignedInt (opcode )].addressMode ).invoke (this );
313315 this .getClass ().getMethod (lookup [Byte .toUnsignedInt (opcode )].opcode ).invoke (this );
314316 } catch (Exception e ) {e .printStackTrace ();}
317+
318+ if (debug ) {
319+ System .out .print (Integer .toHexString (Short .toUnsignedInt (programCounter ))+" " +lookup [Byte .toUnsignedInt (opcode )].opcode +" " +ROMLoader .byteToHexString (opcode )+" " );
320+ if (!(lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("IMP" ) || lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("REL" ))) {
321+ if (lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("IMM" )) {
322+ System .out .print ("#$" +Integer .toHexString (Byte .toUnsignedInt (fetched )));
323+ } else if (lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("REL" )) {
324+ System .out .print ("$" +Integer .toHexString (Byte .toUnsignedInt ((byte )addressAbsolute )));
325+ } else {
326+ System .out .print ("$" +Integer .toHexString (Short .toUnsignedInt (addressAbsolute )));
327+ }
328+ } else if (!lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("IMP" )) {
329+ System .out .print ("$" +Integer .toHexString (Short .toUnsignedInt (addressRelative )));
330+ }
331+ if (lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("ABX" ) || lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("INX" ) || lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("ZPX" )) {
332+ System .out .print (",X" );
333+ } else if (lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("ABY" ) || lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("INY" ) || lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("ZPY" )) {
334+ System .out .print (",Y" );
335+ }
336+ System .out .print (" A:" +Integer .toHexString (Byte .toUnsignedInt (a ))+" X:" +Integer .toHexString (Byte .toUnsignedInt (x ))+" Y:" +Integer .toHexString (Byte .toUnsignedInt (y ))+" Flags:" +ROMLoader .padStringWithZeroes (Integer .toBinaryString (Byte .toUnsignedInt (flags )), 8 ));
337+ System .out .println ();
338+ }
315339 }
316340
317341 if (((System .currentTimeMillis ()-startTime )/1000 ) > 0 )
@@ -795,11 +819,11 @@ public void LDY() {
795819 public void LSR () {
796820 fetch ();
797821 setFlag ('C' ,(fetched &0x0001 )==0x0001 );
798- short temp = (short )(fetched >> 1 );
822+ short temp = (short )(( 0x00FF & fetched ) >> 1 );
799823 setFlag ('Z' ,(temp &0x00FF )==0x0000 );
800824 setFlag ('N' ,(temp &0x0080 )==0x0080 );
801825 if (lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("IMP" )) {
802- a = (byte )(temp &0x00FF );
826+ a = (byte )(( byte )( temp ) &0x00FF );
803827 } else {
804828 Bus .write (addressAbsolute , (byte )(temp &0x00FF ));
805829 }
@@ -858,7 +882,7 @@ public void ROL() {
858882
859883 public void ROR () {
860884 fetch ();
861- short temp = (short )((fetched >>1 ) | (getFlag ('C' ) ? 0x80 : 0 ));
885+ short temp = (short )((( 0x00FF & fetched ) >>1 ) | (short )( getFlag ('C' ) ? 0x0080 : 0 ));
862886 setFlag ('C' ,(fetched &0x01 ) == 0x01 );
863887 setFlag ('Z' ,(temp &0x00FF ) == 0x0000 );
864888 setFlag ('N' ,(temp &0x0080 ) == 0x0080 );
0 commit comments