Skip to content

Commit

Permalink
1.2
Browse files Browse the repository at this point in the history
- The CPU now passes [Klaus Dormann's 6502 test suite!](https://github.com/Klaus2m5/6502_65C02_functional_tests)
- Scaling issues on smaller screens have been fixed
- Added debug mode flag in the CPU which will output an instruction log in the console
  • Loading branch information
DylanSpeiser committed Feb 28, 2021
1 parent 4e8d4ac commit d214035
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 31 deletions.
64 changes: 44 additions & 20 deletions src/CPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ public class CPU {
public byte stackPointer = 0x00;
public short programCounter = 0x0000;

public boolean debug = false;

public short addressAbsolute = 0x0000;
public short addressRelative = 0x0000;
public byte opcode = 0x00;
Expand Down Expand Up @@ -117,10 +119,10 @@ public CPU() {
lookup[0x41] = new Instruction("EOR","IZX",6);
lookup[0x51] = new Instruction("EOR","IZY",5);

lookup[0xE6] = new Instruction("INC","IMM",5);
lookup[0xF6] = new Instruction("INC","ZPP",6);
lookup[0xEE] = new Instruction("INC","ZPX",6);
lookup[0xFE] = new Instruction("INC","ABS",7);
lookup[0xE6] = new Instruction("INC","ZPP",5);
lookup[0xF6] = new Instruction("INC","ZPX",6);
lookup[0xEE] = new Instruction("INC","ABS",6);
lookup[0xFE] = new Instruction("INC","ABX",7);

lookup[0xE8] = new Instruction("INX","IMP",2);

Expand Down Expand Up @@ -148,11 +150,11 @@ public CPU() {

lookup[0xA0] = new Instruction("LDY","IMM",2);
lookup[0xA4] = new Instruction("LDY","ZPP",3);
lookup[0xB4] = new Instruction("LDY","ZPY",4);
lookup[0xB4] = new Instruction("LDY","ZPX",4);
lookup[0xAC] = new Instruction("LDY","ABS",4);
lookup[0xBC] = new Instruction("LDY","ABY",4);
lookup[0xBC] = new Instruction("LDY","ABX",4);

lookup[0x4A] = new Instruction("LSR","IMM",2);
lookup[0x4A] = new Instruction("LSR","IMP",2);
lookup[0x46] = new Instruction("LSR","ZPP",5);
lookup[0x56] = new Instruction("LSR","ZPX",6);
lookup[0x4E] = new Instruction("LSR","ABS",6);
Expand All @@ -177,17 +179,17 @@ public CPU() {

lookup[0x28] = new Instruction("PLP","IMP",4);

lookup[0x2A] = new Instruction("ROR","IMM",2);
lookup[0x26] = new Instruction("ROR","ZPP",5);
lookup[0x36] = new Instruction("ROR","ZPX",6);
lookup[0x2E] = new Instruction("ROR","ABS",6);
lookup[0x3E] = new Instruction("ROR","ABX",7);
lookup[0x2A] = new Instruction("ROL","IMP",2);
lookup[0x26] = new Instruction("ROL","ZPP",5);
lookup[0x36] = new Instruction("ROL","ZPX",6);
lookup[0x2E] = new Instruction("ROL","ABS",6);
lookup[0x3E] = new Instruction("ROL","ABX",7);

lookup[0x6A] = new Instruction("ROL","IMM",2);
lookup[0x66] = new Instruction("ROL","ZPP",5);
lookup[0x76] = new Instruction("ROL","ZPX",6);
lookup[0x6E] = new Instruction("ROL","ABS",6);
lookup[0x7E] = new Instruction("ROL","ABX",7);
lookup[0x6A] = new Instruction("ROR","IMP",2);
lookup[0x66] = new Instruction("ROR","ZPP",5);
lookup[0x76] = new Instruction("ROR","ZPX",6);
lookup[0x6E] = new Instruction("ROR","ABS",6);
lookup[0x7E] = new Instruction("ROR","ABX",7);

lookup[0x40] = new Instruction("RTI","IMP",6);

Expand Down Expand Up @@ -312,6 +314,28 @@ void clock() {
this.getClass().getMethod(lookup[Byte.toUnsignedInt(opcode)].addressMode).invoke(this);
this.getClass().getMethod(lookup[Byte.toUnsignedInt(opcode)].opcode).invoke(this);
} catch (Exception e) {e.printStackTrace();}

if (debug) {
System.out.print(Integer.toHexString(Short.toUnsignedInt(programCounter))+" "+lookup[Byte.toUnsignedInt(opcode)].opcode+" "+ROMLoader.byteToHexString(opcode)+" ");
if (!(lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("IMP") || lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("REL"))) {
if (lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("IMM")) {
System.out.print("#$"+Integer.toHexString(Byte.toUnsignedInt(fetched)));
} else if (lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("REL")) {
System.out.print("$"+Integer.toHexString(Byte.toUnsignedInt((byte)addressAbsolute)));
} else {
System.out.print("$"+Integer.toHexString(Short.toUnsignedInt(addressAbsolute)));
}
} else if (!lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("IMP")) {
System.out.print("$"+Integer.toHexString(Short.toUnsignedInt(addressRelative)));
}
if (lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("ABX") || lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("INX") || lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("ZPX")) {
System.out.print(",X");
} else if (lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("ABY") || lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("INY") || lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("ZPY")) {
System.out.print(",Y");
}
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));
System.out.println();
}
}

if (((System.currentTimeMillis()-startTime)/1000) > 0)
Expand Down Expand Up @@ -795,11 +819,11 @@ public void LDY() {
public void LSR() {
fetch();
setFlag('C',(fetched&0x0001)==0x0001);
short temp = (short)(fetched >> 1);
short temp = (short)((0x00FF&fetched) >> 1);
setFlag('Z',(temp&0x00FF)==0x0000);
setFlag('N',(temp&0x0080)==0x0080);
if (lookup[Byte.toUnsignedInt(opcode)].addressMode.equals("IMP")) {
a = (byte)(temp&0x00FF);
a = (byte)((byte)(temp)&0x00FF);
} else {
Bus.write(addressAbsolute, (byte)(temp&0x00FF));
}
Expand Down Expand Up @@ -858,7 +882,7 @@ public void ROL() {

public void ROR() {
fetch();
short temp = (short)((fetched>>1) | (getFlag('C') ? 0x80 : 0));
short temp = (short)(((0x00FF&fetched)>>1) | (short)(getFlag('C') ? 0x0080 : 0));
setFlag('C',(fetched&0x01) == 0x01);
setFlag('Z',(temp&0x00FF) == 0x0000);
setFlag('N',(temp&0x0080) == 0x0080);
Expand Down
23 changes: 15 additions & 8 deletions src/DisplayPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ public class DisplayPanel extends JPanel implements ActionListener, KeyListener
int ramPage = 0;
int romPage = 0;

int rightAlignHelper = Math.max(getWidth(), 1334);

String ramPageString = "";
String romPageString = "";

Expand Down Expand Up @@ -36,7 +38,9 @@ public void paintComponent(Graphics g) {
// g.fillRect(0, 0, EaterEmulator.getWindows()[1].getWidth(), EaterEmulator.getWindows()[1].getHeight());
// g.setColor(Color.white);
// g.drawString("Render Mode: fillRect",5,15);


rightAlignHelper = Math.max(getWidth(), 1334);

//Title
g.setFont(new Font("Calibri Bold", 50, 50));
g.drawString("Ben Eater 6502 Emulator", 40, 50);
Expand All @@ -50,23 +54,23 @@ public void paintComponent(Graphics g) {
g.drawString("Speed: "+EaterEmulator.cpu.ClocksPerSecond+" Hz"+(EaterEmulator.slowerClock ? " (Slow)" : ""), 40, 110);

//PAGE INDICATORS
g.drawString("(K) <-- "+ROMLoader.byteToHexString((byte)(romPage+0x80))+" --> (L)", 1600, 950);
g.drawString("(H) <-- "+ROMLoader.byteToHexString((byte)ramPage)+" --> (J)", 1200, 950);
g.drawString("(K) <-- "+ROMLoader.byteToHexString((byte)(romPage+0x80))+" --> (L)", rightAlignHelper-304, Math.max(getHeight()-91, 920));
g.drawString("(H) <-- "+ROMLoader.byteToHexString((byte)ramPage)+" --> (J)", rightAlignHelper-704, Math.max(getHeight()-91, 920));

//ROM
g.drawString("ROM", 1690, 130);
drawString(g,romPageString, 1525, 150);
g.drawString("ROM", rightAlignHelper-214, 130);
drawString(g,romPageString, rightAlignHelper-379, 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.fillRect(rightAlignHelper-708+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);
g.drawString("RAM", rightAlignHelper-624, 130);
drawString(g,ramPageString, rightAlignHelper-779, 150);


//CPU
Expand Down Expand Up @@ -116,6 +120,8 @@ public static void drawString(Graphics g, String text, int x, int y) {
public void actionPerformed(ActionEvent e) {
if (e.getSource().equals(t)) {
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
EaterEmulator.ROMopenButton.setBounds(rightAlignHelper-150, 15, 125, 25);
EaterEmulator.RAMopenButton.setBounds(rightAlignHelper-150, 45, 125, 25);
this.repaint();
}
}
Expand Down Expand Up @@ -163,6 +169,7 @@ public void keyTyped(KeyEvent arg0) {
EaterEmulator.via = new VIA();
EaterEmulator.ram = new RAM();
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
System.out.println("Size: "+this.getWidth()+" x "+this.getHeight());
break;
case ' ':
EaterEmulator.cpu.clock();
Expand Down
6 changes: 3 additions & 3 deletions src/EaterEmulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

public class EaterEmulator extends JFrame implements ActionListener {
public static EaterEmulator emu;
public static String versionString = "1.1";
public static String versionString = "1.2";

//Swing Things
JPanel p = new JPanel();
JPanel header = new JPanel();
JFileChooser fc = new JFileChooser();
JButton ROMopenButton = new JButton("Open ROM File");
JButton RAMopenButton = new JButton("Open RAM File");
public static JButton ROMopenButton = new JButton("Open ROM File");
public static JButton RAMopenButton = new JButton("Open RAM File");

//Clock Stuff
public static Thread clockThread;
Expand Down

0 comments on commit d214035

Please sign in to comment.