Skip to content

Commit d214035

Browse files
committed
1.2
- 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
1 parent 4e8d4ac commit d214035

File tree

3 files changed

+62
-31
lines changed

3 files changed

+62
-31
lines changed

src/CPU.java

Lines changed: 44 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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);

src/DisplayPanel.java

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ public class DisplayPanel extends JPanel implements ActionListener, KeyListener
88
int ramPage = 0;
99
int romPage = 0;
1010

11+
int rightAlignHelper = Math.max(getWidth(), 1334);
12+
1113
String ramPageString = "";
1214
String romPageString = "";
1315

@@ -36,7 +38,9 @@ public void paintComponent(Graphics g) {
3638
// g.fillRect(0, 0, EaterEmulator.getWindows()[1].getWidth(), EaterEmulator.getWindows()[1].getHeight());
3739
// g.setColor(Color.white);
3840
// g.drawString("Render Mode: fillRect",5,15);
39-
41+
42+
rightAlignHelper = Math.max(getWidth(), 1334);
43+
4044
//Title
4145
g.setFont(new Font("Calibri Bold", 50, 50));
4246
g.drawString("Ben Eater 6502 Emulator", 40, 50);
@@ -50,23 +54,23 @@ public void paintComponent(Graphics g) {
5054
g.drawString("Speed: "+EaterEmulator.cpu.ClocksPerSecond+" Hz"+(EaterEmulator.slowerClock ? " (Slow)" : ""), 40, 110);
5155

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

5660
//ROM
57-
g.drawString("ROM", 1690, 130);
58-
drawString(g,romPageString, 1525, 150);
61+
g.drawString("ROM", rightAlignHelper-214, 130);
62+
drawString(g,romPageString, rightAlignHelper-379, 150);
5963

6064
//Stack Pointer Underline
6165
if (ramPage == 1) {
6266
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);
67+
g.fillRect(rightAlignHelper-708+36*(Byte.toUnsignedInt(EaterEmulator.cpu.stackPointer)%8), 156+23*((int)Byte.toUnsignedInt(EaterEmulator.cpu.stackPointer)/8), 25, 22);
6468
g.setColor(Color.white);
6569
}
6670

6771
//RAM
68-
g.drawString("RAM", 1280, 130);
69-
drawString(g,ramPageString, 1125, 150);
72+
g.drawString("RAM", rightAlignHelper-624, 130);
73+
drawString(g,ramPageString, rightAlignHelper-779, 150);
7074

7175

7276
//CPU
@@ -116,6 +120,8 @@ public static void drawString(Graphics g, String text, int x, int y) {
116120
public void actionPerformed(ActionEvent e) {
117121
if (e.getSource().equals(t)) {
118122
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
123+
EaterEmulator.ROMopenButton.setBounds(rightAlignHelper-150, 15, 125, 25);
124+
EaterEmulator.RAMopenButton.setBounds(rightAlignHelper-150, 45, 125, 25);
119125
this.repaint();
120126
}
121127
}
@@ -163,6 +169,7 @@ public void keyTyped(KeyEvent arg0) {
163169
EaterEmulator.via = new VIA();
164170
EaterEmulator.ram = new RAM();
165171
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
172+
System.out.println("Size: "+this.getWidth()+" x "+this.getHeight());
166173
break;
167174
case ' ':
168175
EaterEmulator.cpu.clock();

src/EaterEmulator.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99

1010
public class EaterEmulator extends JFrame implements ActionListener {
1111
public static EaterEmulator emu;
12-
public static String versionString = "1.1";
12+
public static String versionString = "1.2";
1313

1414
//Swing Things
1515
JPanel p = new JPanel();
1616
JPanel header = new JPanel();
1717
JFileChooser fc = new JFileChooser();
18-
JButton ROMopenButton = new JButton("Open ROM File");
19-
JButton RAMopenButton = new JButton("Open RAM File");
18+
public static JButton ROMopenButton = new JButton("Open ROM File");
19+
public static JButton RAMopenButton = new JButton("Open RAM File");
2020

2121
//Clock Stuff
2222
public static Thread clockThread;

0 commit comments

Comments
 (0)