@@ -11,6 +11,8 @@ public class CPU {
11
11
public byte stackPointer = 0x00 ;
12
12
public short programCounter = 0x0000 ;
13
13
14
+ public boolean debug = false ;
15
+
14
16
public short addressAbsolute = 0x0000 ;
15
17
public short addressRelative = 0x0000 ;
16
18
public byte opcode = 0x00 ;
@@ -117,10 +119,10 @@ public CPU() {
117
119
lookup [0x41 ] = new Instruction ("EOR" ,"IZX" ,6 );
118
120
lookup [0x51 ] = new Instruction ("EOR" ,"IZY" ,5 );
119
121
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 );
124
126
125
127
lookup [0xE8 ] = new Instruction ("INX" ,"IMP" ,2 );
126
128
@@ -148,11 +150,11 @@ public CPU() {
148
150
149
151
lookup [0xA0 ] = new Instruction ("LDY" ,"IMM" ,2 );
150
152
lookup [0xA4 ] = new Instruction ("LDY" ,"ZPP" ,3 );
151
- lookup [0xB4 ] = new Instruction ("LDY" ,"ZPY " ,4 );
153
+ lookup [0xB4 ] = new Instruction ("LDY" ,"ZPX " ,4 );
152
154
lookup [0xAC ] = new Instruction ("LDY" ,"ABS" ,4 );
153
- lookup [0xBC ] = new Instruction ("LDY" ,"ABY " ,4 );
155
+ lookup [0xBC ] = new Instruction ("LDY" ,"ABX " ,4 );
154
156
155
- lookup [0x4A ] = new Instruction ("LSR" ,"IMM " ,2 );
157
+ lookup [0x4A ] = new Instruction ("LSR" ,"IMP " ,2 );
156
158
lookup [0x46 ] = new Instruction ("LSR" ,"ZPP" ,5 );
157
159
lookup [0x56 ] = new Instruction ("LSR" ,"ZPX" ,6 );
158
160
lookup [0x4E ] = new Instruction ("LSR" ,"ABS" ,6 );
@@ -177,17 +179,17 @@ public CPU() {
177
179
178
180
lookup [0x28 ] = new Instruction ("PLP" ,"IMP" ,4 );
179
181
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 );
185
187
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 );
191
193
192
194
lookup [0x40 ] = new Instruction ("RTI" ,"IMP" ,6 );
193
195
@@ -312,6 +314,28 @@ void clock() {
312
314
this .getClass ().getMethod (lookup [Byte .toUnsignedInt (opcode )].addressMode ).invoke (this );
313
315
this .getClass ().getMethod (lookup [Byte .toUnsignedInt (opcode )].opcode ).invoke (this );
314
316
} 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
+ }
315
339
}
316
340
317
341
if (((System .currentTimeMillis ()-startTime )/1000 ) > 0 )
@@ -795,11 +819,11 @@ public void LDY() {
795
819
public void LSR () {
796
820
fetch ();
797
821
setFlag ('C' ,(fetched &0x0001 )==0x0001 );
798
- short temp = (short )(fetched >> 1 );
822
+ short temp = (short )(( 0x00FF & fetched ) >> 1 );
799
823
setFlag ('Z' ,(temp &0x00FF )==0x0000 );
800
824
setFlag ('N' ,(temp &0x0080 )==0x0080 );
801
825
if (lookup [Byte .toUnsignedInt (opcode )].addressMode .equals ("IMP" )) {
802
- a = (byte )(temp &0x00FF );
826
+ a = (byte )(( byte )( temp ) &0x00FF );
803
827
} else {
804
828
Bus .write (addressAbsolute , (byte )(temp &0x00FF ));
805
829
}
@@ -858,7 +882,7 @@ public void ROL() {
858
882
859
883
public void ROR () {
860
884
fetch ();
861
- short temp = (short )((fetched >>1 ) | (getFlag ('C' ) ? 0x80 : 0 ));
885
+ short temp = (short )((( 0x00FF & fetched ) >>1 ) | (short )( getFlag ('C' ) ? 0x0080 : 0 ));
862
886
setFlag ('C' ,(fetched &0x01 ) == 0x01 );
863
887
setFlag ('Z' ,(temp &0x00FF ) == 0x0000 );
864
888
setFlag ('N' ,(temp &0x0080 ) == 0x0080 );
0 commit comments