Skip to content

Commit 1eeab97

Browse files
committed
bug fixes!
- Added total clocks display - Unused opcodes no longer cause Java errors. - Fixed Zero Page addressing - Fixed stack pointer arithmetic - Fixed BVC and BVS
1 parent f79e1a4 commit 1eeab97

File tree

3 files changed

+37
-26
lines changed

3 files changed

+37
-26
lines changed

src/CPU.java

Lines changed: 32 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import java.util.Arrays;
2+
13
public class CPU {
24
public String[] opcodes;
35

@@ -23,6 +25,8 @@ public class CPU {
2325
public CPU() {
2426
reset();
2527

28+
Arrays.fill(lookup, new Instruction("XXX","IMP",2));
29+
2630
//ADC
2731
lookup[0x69] = new Instruction("ADC","IMM",2);
2832
lookup[0x65] = new Instruction("ADC","ZPP",3);
@@ -54,7 +58,7 @@ public CPU() {
5458

5559
lookup[0xF0] = new Instruction("BEQ","REL",2);
5660

57-
lookup[0x24] = new Instruction("BIT","ZP0",3);
61+
lookup[0x24] = new Instruction("BIT","ZPP",3);
5862
lookup[0x2C] = new Instruction("BIT","ABS",4);
5963

6064
lookup[0x30] = new Instruction("BMI","REL",2);
@@ -308,7 +312,7 @@ void clock() {
308312
this.getClass().getMethod(lookup[Byte.toUnsignedInt(opcode)].opcode).invoke(this);
309313
} catch (Exception e) {e.printStackTrace();}
310314
}
311-
315+
EaterEmulator.clocks++;
312316
cycles--;
313317
}
314318

@@ -326,6 +330,8 @@ void reset() {
326330
byte hi = Bus.read((short)(addressAbsolute+1));
327331
programCounter = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi));
328332

333+
EaterEmulator.clocks = 0;
334+
329335
addressRelative = 0;
330336
addressAbsolute = 0;
331337
fetched = 0;
@@ -335,16 +341,16 @@ void reset() {
335341

336342
void irq() {
337343
if (!getFlag('I')) {
338-
Bus.write((short)(0x0100+stackPointer), (byte)((programCounter>>8)&0x00FF));
344+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)((programCounter>>8)&0x00FF));
339345
stackPointer--;
340-
Bus.write((short)(0x0100+stackPointer), (byte)(programCounter&0x00FF));
346+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter&0x00FF));
341347
stackPointer--;
342348

343349
setFlag('B',false);
344350
setFlag('U',false);
345351
setFlag('I',true);
346352

347-
Bus.write((short)(0x0100+stackPointer), flags);
353+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), flags);
348354
stackPointer--;
349355

350356
addressAbsolute = (short)0xFFFE;
@@ -357,16 +363,16 @@ void irq() {
357363
}
358364

359365
void nmi() {
360-
Bus.write((short)(0x0100+stackPointer), (byte)((programCounter>>8)&0x00FF));
366+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)((programCounter>>8)&0x00FF));
361367
stackPointer--;
362-
Bus.write((short)(0x0100+stackPointer), (byte)(programCounter&0x00FF));
368+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter&0x00FF));
363369
stackPointer--;
364370

365371
setFlag('B',false);
366372
setFlag('U',false);
367373
setFlag('I',true);
368374

369-
Bus.write((short)(0x0100+stackPointer), flags);
375+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), flags);
370376
stackPointer--;
371377

372378
addressAbsolute = (short)0xFFFA;
@@ -394,7 +400,7 @@ public void IMM() {
394400
addressAbsolute = programCounter++;
395401
}
396402

397-
public void ZP0() {
403+
public void ZPP() {
398404
addressAbsolute = Bus.read(programCounter);
399405
programCounter++;
400406
addressAbsolute &= 0x00FF;
@@ -604,13 +610,13 @@ public void BPL() {
604610
public void BRK() {
605611
programCounter++;
606612
setFlag('I',true);
607-
Bus.write((short)(0x0100+stackPointer), (byte)((programCounter>>8)&0x00FF));
613+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)((programCounter>>8)&0x00FF));
608614
stackPointer--;
609-
Bus.write((short)(0x0100+stackPointer), (byte)(programCounter&0x00FF));
615+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter&0x00FF));
610616
stackPointer--;
611617

612618
setFlag('B',true);
613-
Bus.write((short)(0x0100+stackPointer), flags);
619+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), flags);
614620
stackPointer--;
615621
setFlag('B',false);
616622

@@ -621,7 +627,7 @@ public void BRK() {
621627
}
622628

623629
public void BVC() {
624-
if (getFlag('V')) {
630+
if (!getFlag('V')) {
625631
cycles++;
626632
addressAbsolute = (short)(programCounter+addressRelative);
627633

@@ -633,7 +639,7 @@ public void BVC() {
633639
}
634640

635641
public void BVS() {
636-
if (!getFlag('V')) {
642+
if (getFlag('V')) {
637643
cycles++;
638644
addressAbsolute = (short)(programCounter+addressRelative);
639645

@@ -742,9 +748,9 @@ public void JMP() {
742748
public void JSR() {
743749
programCounter--;
744750

745-
Bus.write((short)(0x0100+stackPointer), (byte)((programCounter>>8)&0x00FF));
751+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)((programCounter>>8)&0x00FF));
746752
stackPointer--;
747-
Bus.write((short)(0x0100+stackPointer), (byte)(programCounter&0x00FF));
753+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter&0x00FF));
748754
stackPointer--;
749755

750756
programCounter = addressAbsolute;
@@ -801,27 +807,27 @@ public void ORA() {
801807
}
802808

803809
public void PHA() {
804-
Bus.write((short)(0x0100+stackPointer), a);
810+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), a);
805811
stackPointer--;
806812
}
807813

808814
public void PHP() {
809-
Bus.write((short)(0x0100+stackPointer), (byte)(flags|0b00001100));
815+
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(flags|0b00001100));
810816
setFlag('B',false);
811817
setFlag('U',false);
812818
stackPointer--;
813819
}
814820

815821
public void PLA() {
816822
stackPointer++;
817-
a = Bus.read((short)(0x0100+stackPointer));
823+
a = Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
818824
setFlag('Z', a == 0);
819825
setFlag('N', (a & 0x80) == 0x80);
820826
}
821827

822828
public void PLP() {
823829
stackPointer++;
824-
flags = Bus.read((short)(0x0100+stackPointer));
830+
flags = Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
825831
setFlag('U', true);
826832
}
827833

@@ -853,21 +859,21 @@ public void ROR() {
853859

854860
public void RTI() {
855861
stackPointer++;
856-
flags = Bus.read((short)(0x0100+stackPointer));
862+
Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
857863
flags = (byte)(flags & ~(getFlag('B') ? 0b00000100 : 0));
858864
flags = (byte)(flags & ~(getFlag('U') ? 0b00000100 : 0));
859865

860866
stackPointer++;
861-
programCounter = Bus.read((short)(0x0100+stackPointer));
867+
programCounter = Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
862868
stackPointer++;
863-
programCounter |= Bus.read((short)(0x0100+stackPointer)) << 8;
869+
programCounter |= Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer))) << 8;
864870
}
865871

866872
public void RTS() {
867873
stackPointer++;
868-
programCounter = Bus.read((short)(0x0100+stackPointer));
874+
programCounter = Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer)));
869875
stackPointer++;
870-
programCounter |= Bus.read((short)(0x0100+stackPointer)) << 8;
876+
programCounter |= Bus.read((short)(0x0100+Byte.toUnsignedInt(stackPointer))) << 8;
871877

872878
programCounter++;
873879
}
@@ -943,6 +949,6 @@ public void TYA() {
943949
}
944950

945951
public void XXX() {
946-
System.out.println("Illegal Opcode!");
952+
System.out.println("Illegal Opcode! (" + ROMLoader.byteToHexString(opcode) +") - "+lookup[Byte.toUnsignedInt(opcode)].opcode);
947953
}
948954
}

src/DisplayPanel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@ public void paintComponent(Graphics g) {
3232
g.setFont(new Font("Calibri Bold", 50, 50));
3333
g.drawString("Ben Eater 6502 Emulator", 40, 50);
3434

35+
//Clocks
36+
g.setFont(new Font("Courier New Bold",20,20));
37+
g.drawString("Clocks: "+EaterEmulator.clocks, 40, 80);
38+
3539
//Mouse Position
3640
// g.setFont(new Font("Arial",10,10));
3741
// g.drawString(mousePos.toString(), 10, 10);

src/EaterEmulator.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ public class EaterEmulator extends JFrame implements ActionListener {
1818
public DisplayPanel GraphicsPanel = new DisplayPanel();
1919

2020
public Timer clock;
21+
public static int clocks = 0;
2122
public static boolean haltFlag = true;
2223

2324
//Emulator Things

0 commit comments

Comments
 (0)