Skip to content

Commit

Permalink
More cpu bug fixes
Browse files Browse the repository at this point in the history
- 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
  • Loading branch information
DylanSpeiser committed Feb 24, 2021
1 parent ce12c92 commit 4e8d4ac
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 38 deletions.
72 changes: 38 additions & 34 deletions src/CPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ public CPU() {
lookup[0x91] = new Instruction("STA","IZY",6);

lookup[0x86] = new Instruction("STX","ZPP",3);
lookup[0x96] = new Instruction("STX","ZPX",4);
lookup[0x96] = new Instruction("STX","ZPY",4);
lookup[0x8E] = new Instruction("STX","ABS",4);

lookup[0x84] = new Instruction("STY","ZPP",3);
Expand All @@ -241,50 +241,50 @@ void setFlag(char flag, boolean condition) {
flag = Character.toUpperCase(flag);
switch (flag) {
case 'C':
flags = setBit(flags,7,condition);
flags = setBit(flags,0,condition);
break;
case 'Z':
flags = setBit(flags,6,condition);
flags = setBit(flags,1,condition);
break;
case 'I':
flags = setBit(flags,5,condition);
flags = setBit(flags,2,condition);
break;
case 'D':
flags = setBit(flags,4,condition);
flags = setBit(flags,3,condition);
break;
case 'B':
flags = setBit(flags,3,condition);
flags = setBit(flags,4,condition);
break;
case 'U':
flags = setBit(flags,2,condition);
flags = setBit(flags,5,condition);
break;
case 'V':
flags = setBit(flags,1,condition);
flags = setBit(flags,6,condition);
break;
case 'N':
flags = setBit(flags,0,condition);
flags = setBit(flags,7,condition);
break;
}
}

boolean getFlag(char flag) {
flag = Character.toUpperCase(flag);
switch (flag) {
case 'C':
case 'N':
return ((flags&0b10000000) == 0b10000000);
case 'Z':
case 'V':
return ((flags&0b01000000) == 0b01000000);
case 'I':
case 'U':
return ((flags&0b00100000) == 0b00100000);
case 'D':
return ((flags&0b00010000) == 0b00010000);
case 'B':
return ((flags&0b00010000) == 0b00010000);
case 'D':
return ((flags&0b00001000) == 0b00001000);
case 'U':
case 'I':
return ((flags&0b00000100) == 0b00000100);
case 'V':
case 'Z':
return ((flags&0b00000010) == 0b00000010);
case 'N':
case 'C':
return ((flags&0b00000001) == 0b00000001);
}
System.out.println("Something has gone wrong in getFlag!");
Expand Down Expand Up @@ -417,13 +417,13 @@ public void ZPP() {
}

public void ZPX() {
addressAbsolute = (short)(Bus.read(programCounter)+x);
addressAbsolute = (short)(Byte.toUnsignedInt(Bus.read(programCounter))+Byte.toUnsignedInt(x));
programCounter++;
addressAbsolute &= 0x00FF;
}

public void ZPY() {
addressAbsolute = (short)(Bus.read(programCounter)+y);
addressAbsolute = (short)(Byte.toUnsignedInt(Bus.read(programCounter))+Byte.toUnsignedInt(y));
programCounter++;
addressAbsolute &= 0x00FF;
}
Expand All @@ -450,7 +450,7 @@ public void ABX() {
byte hi = Bus.read(programCounter);
programCounter++;

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

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

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

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

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

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

public void BRK() {
programCounter++;
setFlag('I',true);
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)((programCounter>>8)&0x00FF));

Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter>>8));
stackPointer--;
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter&0x00FF));
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter));
stackPointer--;

setFlag('B',true);
setFlag('U',true);
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), flags);
stackPointer--;
setFlag('B',false);
//setFlag('B',false);
setFlag('I',true);

addressAbsolute = (short)0xFFFE;
byte lo = Bus.read(addressAbsolute);
Expand Down Expand Up @@ -822,7 +824,7 @@ public void PHA() {
}

public void PHP() {
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(flags|0b00001100));
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(flags|0b00110000));
setFlag('B',false);
setFlag('U',false);
stackPointer--;
Expand Down Expand Up @@ -869,21 +871,23 @@ public void ROR() {

public void RTI() {
stackPointer++;
Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
flags = (byte)(flags & ~(getFlag('B') ? 0b00000100 : 0));
flags = (byte)(flags & ~(getFlag('U') ? 0b00000100 : 0));
flags = Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
flags = (byte)(flags & (getFlag('B') ? 0b11101111 : 0));
flags = (byte)(flags & (getFlag('U') ? 0b11011111 : 0));

stackPointer++;
programCounter = Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
byte lo = Bus.read((short)(0x100+Byte.toUnsignedInt(stackPointer)));
stackPointer++;
programCounter |= Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer))) << 8;
byte hi = Bus.read((short)(0x100+Byte.toUnsignedInt(stackPointer)));
programCounter = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi));
}

public void RTS() {
stackPointer++;
programCounter = Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
byte lo = Bus.read((short)(0x100+Byte.toUnsignedInt(stackPointer)));
stackPointer++;
programCounter |= Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer))) << 8;
byte hi = Bus.read((short)(0x100+Byte.toUnsignedInt(stackPointer)));
programCounter = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi));

programCounter++;
}
Expand Down
16 changes: 12 additions & 4 deletions src/DisplayPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,29 +57,37 @@ public void paintComponent(Graphics g) {
g.drawString("ROM", 1690, 130);
drawString(g,romPageString, 1525, 150);

//Stack Pointer Underline
if (ramPage == 1) {
g.setColor(new Color(0.7f,0f,0f));
g.fillRect(1196+36*(Byte.toUnsignedInt(EaterEmulator.cpu.stackPointer)%8), 156+23*((int)Byte.toUnsignedInt(EaterEmulator.cpu.stackPointer)/8), 25, 22);
g.setColor(Color.white);
}

//RAM
g.drawString("RAM", 1280, 130);
drawString(g,ramPageString, 1125, 150);


//CPU
g.drawString("CPU Registers:",50,140);
g.drawString("A: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.cpu.a)), 8)+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.a)+")", 35, 170);
g.drawString("X: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.cpu.x)), 8)+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.x)+")", 35, 200);
g.drawString("Y: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.cpu.y)), 8)+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.y)+")", 35, 230);
g.drawString("Stack Pointer: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.cpu.stackPointer)), 8)+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.stackPointer)+")", 35, 260);
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);
g.drawString("Flags: ", 35, 320);
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);
g.drawString("Flags: ("+ROMLoader.byteToHexString(EaterEmulator.cpu.flags)+")", 35, 320);

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);
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);
g.drawString("Opcode: "+EaterEmulator.cpu.lookup[Byte.toUnsignedInt(EaterEmulator.cpu.opcode)]+" ("+ROMLoader.byteToHexString(EaterEmulator.cpu.opcode)+")", 35, 410);
g.drawString("Cycles: "+EaterEmulator.cpu.cycles, 35, 440);

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

Expand Down

0 comments on commit 4e8d4ac

Please sign in to comment.