Skip to content

Commit 4e8d4ac

Browse files
committed
More cpu bug fixes
- Test program now gets to $F5F - Added stack pointer visual in RAM - Flags are now displayed in the correct order - Fixed some badly behaving opcodes
1 parent ce12c92 commit 4e8d4ac

File tree

2 files changed

+50
-38
lines changed

2 files changed

+50
-38
lines changed

src/CPU.java

Lines changed: 38 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ public CPU() {
217217
lookup[0x91] = new Instruction("STA","IZY",6);
218218

219219
lookup[0x86] = new Instruction("STX","ZPP",3);
220-
lookup[0x96] = new Instruction("STX","ZPX",4);
220+
lookup[0x96] = new Instruction("STX","ZPY",4);
221221
lookup[0x8E] = new Instruction("STX","ABS",4);
222222

223223
lookup[0x84] = new Instruction("STY","ZPP",3);
@@ -241,50 +241,50 @@ void setFlag(char flag, boolean condition) {
241241
flag = Character.toUpperCase(flag);
242242
switch (flag) {
243243
case 'C':
244-
flags = setBit(flags,7,condition);
244+
flags = setBit(flags,0,condition);
245245
break;
246246
case 'Z':
247-
flags = setBit(flags,6,condition);
247+
flags = setBit(flags,1,condition);
248248
break;
249249
case 'I':
250-
flags = setBit(flags,5,condition);
250+
flags = setBit(flags,2,condition);
251251
break;
252252
case 'D':
253-
flags = setBit(flags,4,condition);
253+
flags = setBit(flags,3,condition);
254254
break;
255255
case 'B':
256-
flags = setBit(flags,3,condition);
256+
flags = setBit(flags,4,condition);
257257
break;
258258
case 'U':
259-
flags = setBit(flags,2,condition);
259+
flags = setBit(flags,5,condition);
260260
break;
261261
case 'V':
262-
flags = setBit(flags,1,condition);
262+
flags = setBit(flags,6,condition);
263263
break;
264264
case 'N':
265-
flags = setBit(flags,0,condition);
265+
flags = setBit(flags,7,condition);
266266
break;
267267
}
268268
}
269269

270270
boolean getFlag(char flag) {
271271
flag = Character.toUpperCase(flag);
272272
switch (flag) {
273-
case 'C':
273+
case 'N':
274274
return ((flags&0b10000000) == 0b10000000);
275-
case 'Z':
275+
case 'V':
276276
return ((flags&0b01000000) == 0b01000000);
277-
case 'I':
277+
case 'U':
278278
return ((flags&0b00100000) == 0b00100000);
279-
case 'D':
280-
return ((flags&0b00010000) == 0b00010000);
281279
case 'B':
280+
return ((flags&0b00010000) == 0b00010000);
281+
case 'D':
282282
return ((flags&0b00001000) == 0b00001000);
283-
case 'U':
283+
case 'I':
284284
return ((flags&0b00000100) == 0b00000100);
285-
case 'V':
285+
case 'Z':
286286
return ((flags&0b00000010) == 0b00000010);
287-
case 'N':
287+
case 'C':
288288
return ((flags&0b00000001) == 0b00000001);
289289
}
290290
System.out.println("Something has gone wrong in getFlag!");
@@ -417,13 +417,13 @@ public void ZPP() {
417417
}
418418

419419
public void ZPX() {
420-
addressAbsolute = (short)(Bus.read(programCounter)+x);
420+
addressAbsolute = (short)(Byte.toUnsignedInt(Bus.read(programCounter))+Byte.toUnsignedInt(x));
421421
programCounter++;
422422
addressAbsolute &= 0x00FF;
423423
}
424424

425425
public void ZPY() {
426-
addressAbsolute = (short)(Bus.read(programCounter)+y);
426+
addressAbsolute = (short)(Byte.toUnsignedInt(Bus.read(programCounter))+Byte.toUnsignedInt(y));
427427
programCounter++;
428428
addressAbsolute &= 0x00FF;
429429
}
@@ -450,7 +450,7 @@ public void ABX() {
450450
byte hi = Bus.read(programCounter);
451451
programCounter++;
452452

453-
addressAbsolute = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi)+x);
453+
addressAbsolute = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi)+Byte.toUnsignedInt(x));
454454

455455
if ((addressAbsolute & 0xFF00) != (hi<<8))
456456
additionalCycles++;
@@ -462,7 +462,7 @@ public void ABY() {
462462
byte hi = Bus.read(programCounter);
463463
programCounter++;
464464

465-
addressAbsolute = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi)+y);
465+
addressAbsolute = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi)+Byte.toUnsignedInt(y));
466466

467467
if ((addressAbsolute & 0xFF00) != (hi<<8))
468468
additionalCycles++;
@@ -496,7 +496,7 @@ public void IZY() {
496496
byte lo = Bus.read((short)(t&0x00FF));
497497
byte hi = Bus.read((short)((t+1)&0x00FF));
498498

499-
addressAbsolute = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi)+y);
499+
addressAbsolute = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi)+Byte.toUnsignedInt(y));
500500

501501
if ((addressAbsolute & 0xFF00) != (hi<<8))
502502
additionalCycles++;
@@ -619,16 +619,18 @@ public void BPL() {
619619

620620
public void BRK() {
621621
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));
624624
stackPointer--;
625-
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter&0x00FF));
625+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter));
626626
stackPointer--;
627627

628628
setFlag('B',true);
629+
setFlag('U',true);
629630
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), flags);
630631
stackPointer--;
631-
setFlag('B',false);
632+
//setFlag('B',false);
633+
setFlag('I',true);
632634

633635
addressAbsolute = (short)0xFFFE;
634636
byte lo = Bus.read(addressAbsolute);
@@ -822,7 +824,7 @@ public void PHA() {
822824
}
823825

824826
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));
826828
setFlag('B',false);
827829
setFlag('U',false);
828830
stackPointer--;
@@ -869,21 +871,23 @@ public void ROR() {
869871

870872
public void RTI() {
871873
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));
875877

876878
stackPointer++;
877-
programCounter = Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
879+
byte lo = Bus.read((short)(0x100+Byte.toUnsignedInt(stackPointer)));
878880
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));
880883
}
881884

882885
public void RTS() {
883886
stackPointer++;
884-
programCounter = Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
887+
byte lo = Bus.read((short)(0x100+Byte.toUnsignedInt(stackPointer)));
885888
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));
887891

888892
programCounter++;
889893
}

src/DisplayPanel.java

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -57,29 +57,37 @@ public void paintComponent(Graphics g) {
5757
g.drawString("ROM", 1690, 130);
5858
drawString(g,romPageString, 1525, 150);
5959

60+
//Stack Pointer Underline
61+
if (ramPage == 1) {
62+
g.setColor(new Color(0.7f,0f,0f));
63+
g.fillRect(1196+36*(Byte.toUnsignedInt(EaterEmulator.cpu.stackPointer)%8), 156+23*((int)Byte.toUnsignedInt(EaterEmulator.cpu.stackPointer)/8), 25, 22);
64+
g.setColor(Color.white);
65+
}
66+
6067
//RAM
6168
g.drawString("RAM", 1280, 130);
6269
drawString(g,ramPageString, 1125, 150);
70+
6371

6472
//CPU
6573
g.drawString("CPU Registers:",50,140);
6674
g.drawString("A: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.cpu.a)), 8)+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.a)+")", 35, 170);
6775
g.drawString("X: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.cpu.x)), 8)+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.x)+")", 35, 200);
6876
g.drawString("Y: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.cpu.y)), 8)+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.y)+")", 35, 230);
6977
g.drawString("Stack Pointer: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.cpu.stackPointer)), 8)+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.stackPointer)+")", 35, 260);
70-
g.drawString("Program Counter: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Short.toUnsignedInt(EaterEmulator.cpu.programCounter)), 16)+" ("+ROMLoader.padStringWithZeroes(Integer.toHexString(Short.toUnsignedInt(EaterEmulator.cpu.programCounter)),4)+")", 35, 290);
71-
g.drawString("Flags: ", 35, 320);
78+
g.drawString("Program Counter: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Short.toUnsignedInt(EaterEmulator.cpu.programCounter)), 16)+" ("+ROMLoader.padStringWithZeroes(Integer.toHexString(Short.toUnsignedInt(EaterEmulator.cpu.programCounter)).toUpperCase(),4)+")", 35, 290);
79+
g.drawString("Flags: ("+ROMLoader.byteToHexString(EaterEmulator.cpu.flags)+")", 35, 320);
7280

7381
g.drawString("Absolute Address: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Short.toUnsignedInt(EaterEmulator.cpu.addressAbsolute)), 16)+" ("+ROMLoader.byteToHexString((byte)(EaterEmulator.cpu.addressAbsolute/0xFF))+ROMLoader.byteToHexString((byte)EaterEmulator.cpu.addressAbsolute)+")", 35, 350);
7482
g.drawString("Relative Address: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Short.toUnsignedInt(EaterEmulator.cpu.addressRelative)), 16)+" ("+ROMLoader.byteToHexString((byte)(EaterEmulator.cpu.addressRelative/0xFF))+ROMLoader.byteToHexString((byte)EaterEmulator.cpu.addressRelative)+")", 35, 380);
7583
g.drawString("Opcode: "+EaterEmulator.cpu.lookup[Byte.toUnsignedInt(EaterEmulator.cpu.opcode)]+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.opcode)+")", 35, 410);
7684
g.drawString("Cycles: "+EaterEmulator.cpu.cycles, 35, 440);
7785

7886
int counter = 0;
79-
String flagsString = "CZIDBUVN";
87+
String flagsString = "NVUBDIZC";
8088
for (char c : ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.cpu.flags)),8).toCharArray()) {
8189
g.setColor((c == '1') ? Color.green : Color.red);
82-
g.drawString(String.valueOf(flagsString.charAt(counter)), 120+15*counter, 320);
90+
g.drawString(String.valueOf(flagsString.charAt(counter)), 120+16*counter, 320);
8391
counter++;
8492
}
8593

0 commit comments

Comments
 (0)