Skip to content

Commit 3d078c6

Browse files
committed
v2.5
Keyboard functionality + Lots of Bug fixes! • Added a _very_ simple keyboard interface. (details in readme) • Saving config files actually works now. • Clock speed is now more accurate (it still freaks out in some situations) • GPU now takes window header into account when calculating its size
1 parent 2569df1 commit 3d078c6

File tree

8 files changed

+159
-79
lines changed

8 files changed

+159
-79
lines changed

.DS_Store

0 Bytes
Binary file not shown.

src/Bus.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,6 @@ public static void write(short address, byte data) {
1919
} else {
2020
EaterEmulator.ram.write(address, data);
2121
}
22+
//System.out.println("Wrote "+data+" at "+address);
2223
}
2324
}

src/CPU.java

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@ public class CPU {
1919
public int cycles = 0;
2020

2121
public double ClocksPerSecond = 0;
22+
public int clockDelta = 1;
23+
public int lastClocks = 0;
24+
2225
public long startTime = 0;
26+
public long timeDelta = 1;
27+
public long lastTime = System.nanoTime();
2328

2429
public int additionalCycles = 0;
2530

@@ -348,12 +353,13 @@ void clock() {
348353
}
349354
}
350355

351-
if (((System.currentTimeMillis()-startTime)/1000) > 0)
352-
ClocksPerSecond = EaterEmulator.clocks/((System.currentTimeMillis()-startTime)/1000);
353-
354356
EaterEmulator.clocks++;
355357

356358
cycles--;
359+
360+
if (cycles < 0) {
361+
cycles = 0;
362+
}
357363
}
358364

359365
//Input Signal Handlers
@@ -386,6 +392,9 @@ void reset() {
386392

387393
void irq() {
388394
if (!getFlag('I')) {
395+
if (debug)
396+
System.out.println("Interrupted!");
397+
389398
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter>>8));
390399
stackPointer--;
391400
Bus.write((short)(0x0100+Byte.toUnsignedInt(stackPointer)), (byte)(programCounter));
@@ -403,8 +412,8 @@ void irq() {
403412
programCounter = (short)(Byte.toUnsignedInt(lo)+256*Byte.toUnsignedInt(hi));
404413

405414
cycles = 7;
406-
interruptRequested = false;
407415
}
416+
interruptRequested = false;
408417
}
409418

410419
void nmi() {

src/DisplayPanel.java

Lines changed: 91 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
import java.io.IOException;
77

88
public class DisplayPanel extends JPanel implements ActionListener, KeyListener {
9-
Timer t;
9+
Timer frameTimer = new javax.swing.Timer(16, this);;
10+
Timer clocksPerSecondCheckTimer = new Timer(150,this);
1011
int ramPage = 0;
1112
int romPage = 0;
1213

@@ -23,8 +24,8 @@ public class DisplayPanel extends JPanel implements ActionListener, KeyListener
2324
public DisplayPanel() {
2425
super(null);
2526

26-
t = new javax.swing.Timer(16, this);
27-
t.start();
27+
clocksPerSecondCheckTimer.start();
28+
frameTimer.start();
2829
setBackground(bgColor);
2930
setPreferredSize(new Dimension(1936, 966));
3031

@@ -123,12 +124,19 @@ public void paintComponent(Graphics g) {
123124
g.drawString(" IER: "+ROMLoader.padStringWithZeroes(Integer.toBinaryString(Byte.toUnsignedInt(EaterEmulator.via.IER)), 8)+" ("+ROMLoader.byteToHexString(EaterEmulator.via.IER)+")", 35, 700);
124125

125126
//Controls
126-
g.drawString("Controls:", 50, 750);
127-
g.drawString("C - Toggle Clock", 35, 780);
128-
g.drawString("Space - Pulse Clock", 35, 810);
129-
g.drawString("R - Reset", 35, 840);
130-
g.drawString("S - Toggle Slower Clock", 35, 870);
131-
g.drawString("I - Trigger VIA CA1", 35, 900);
127+
128+
if (!EaterEmulator.keyboardMode) {
129+
g.drawString("Controls:", 50, 750);
130+
g.drawString("C - Toggle Clock", 35, 780);
131+
g.drawString("Space - Pulse Clock", 35, 810);
132+
g.drawString("R - Reset", 35, 840);
133+
g.drawString("S - Toggle Slower Clock", 35, 870);
134+
g.drawString("I - Trigger VIA CA1", 35, 900);
135+
} else {
136+
g.drawString("Keyboard Mode Controls:", 50, 750);
137+
g.drawString("Typing a key will write that key code to the memory location "+EaterEmulator.options.KeyboardLocationHexLabel.getText().substring(3), 35, 780);
138+
g.drawString(" and trigger an interrupt.", 35, 810);
139+
}
132140
}
133141

134142
public static void drawString(Graphics g, String text, int x, int y) {
@@ -144,22 +152,29 @@ public void resetGraphics() {
144152

145153
@Override
146154
public void actionPerformed(ActionEvent e) {
147-
if (e.getSource().equals(t)) {
148-
EaterEmulator.running = true;
155+
if (e.getSource().equals(frameTimer)) {
149156

150-
EaterEmulator.cpu.cycles = 0;
151-
157+
EaterEmulator.running = true;
152158

153159
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
154160
EaterEmulator.ROMopenButton.setBounds(rightAlignHelper-150, 15, 125, 25);
155161
EaterEmulator.RAMopenButton.setBounds(rightAlignHelper-150, 45, 125, 25);
156162
EaterEmulator.ShowLCDButton.setBounds(rightAlignHelper-300, 15, 125, 25);
157163
EaterEmulator.ShowGPUButton.setBounds(rightAlignHelper-300, 45, 125, 25);
158-
EaterEmulator.optionsButton.setBounds(rightAlignHelper-450, 15, 125, 55);
164+
EaterEmulator.optionsButton.setBounds(rightAlignHelper-450, 15, 125, 25);
165+
EaterEmulator.keyboardButton.setBounds(rightAlignHelper-450, 45, 125, 25);
159166
this.repaint();
160167

161168
if (!EaterEmulator.options.isVisible())
162169
this.requestFocus();
170+
} else if (e.getSource().equals(clocksPerSecondCheckTimer)) {
171+
EaterEmulator.cpu.timeDelta = System.nanoTime()-EaterEmulator.cpu.lastTime;
172+
EaterEmulator.cpu.lastTime = System.nanoTime();
173+
174+
EaterEmulator.cpu.clockDelta = EaterEmulator.clocks - EaterEmulator.cpu.lastClocks;
175+
EaterEmulator.cpu.lastClocks = EaterEmulator.clocks;
176+
177+
EaterEmulator.cpu.ClocksPerSecond = Math.round(EaterEmulator.cpu.clockDelta/((double)EaterEmulator.cpu.timeDelta/1000000000.0));
163178
}
164179
}
165180

@@ -175,54 +190,70 @@ public void keyReleased(KeyEvent arg0) {
175190

176191
@Override
177192
public void keyTyped(KeyEvent arg0) {
178-
switch (arg0.getKeyChar()) {
179-
case 'l':
180-
if (romPage < 0x80) {
181-
romPage+=1;
182-
romPageString = EaterEmulator.rom.ROMString.substring(romPage*960,(romPage+1)*960);
183-
}
184-
break;
185-
case 'k':
186-
if (romPage > 0) {
187-
romPage-=1;
188-
romPageString = EaterEmulator.rom.ROMString.substring(romPage*960,(romPage+1)*960);
189-
}
190-
break;
191-
case 'j':
192-
if (ramPage < 0x80) {
193-
ramPage+=1;
194-
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
195-
}
196-
break;
197-
case 'h':
198-
if (ramPage > 0) {
199-
ramPage-=1;
193+
if (!EaterEmulator.keyboardMode) {
194+
//Control Keyboard Mode
195+
switch (arg0.getKeyChar()) {
196+
case 'l':
197+
if (romPage < 0x7f) {
198+
romPage+=1;
199+
romPageString = EaterEmulator.rom.ROMString.substring(romPage*960,(romPage+1)*960);
200+
} else {
201+
if (romPage > 0x7f) {
202+
romPage = 0x7f;
203+
romPageString = EaterEmulator.rom.ROMString.substring(romPage*960,(romPage+1)*960);
204+
}
205+
}
206+
break;
207+
case 'k':
208+
if (romPage > 0) {
209+
romPage-=1;
210+
romPageString = EaterEmulator.rom.ROMString.substring(romPage*960,(romPage+1)*960);
211+
}
212+
break;
213+
case 'j':
214+
if (ramPage < 0x7f) {
215+
ramPage+=1;
216+
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
217+
if (ramPage > 0x7f) {
218+
ramPage = 0x7f;
219+
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
220+
}
221+
}
222+
break;
223+
case 'h':
224+
if (ramPage > 0) {
225+
ramPage-=1;
226+
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
227+
}
228+
break;
229+
case 'r':
230+
EaterEmulator.cpu.reset();
231+
EaterEmulator.lcd.reset();
232+
EaterEmulator.via = new VIA();
233+
EaterEmulator.ram = new RAM();
234+
EaterEmulator.gpu.setRAM(EaterEmulator.ram);
200235
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
201-
}
202-
break;
203-
case 'r':
204-
EaterEmulator.cpu.reset();
205-
EaterEmulator.lcd.reset();
206-
EaterEmulator.via = new VIA();
207-
EaterEmulator.ram = new RAM();
208-
EaterEmulator.gpu.setRAM(EaterEmulator.ram);
209-
ramPageString = EaterEmulator.ram.RAMString.substring(ramPage*960,(ramPage+1)*960);
210236

211-
if (EaterEmulator.debug)
212-
System.out.println("Size: "+this.getWidth()+" x "+this.getHeight());
213-
break;
214-
case ' ':
215-
EaterEmulator.cpu.clock();
216-
break;
217-
case 'c':
218-
EaterEmulator.clockState = !EaterEmulator.clockState;
219-
break;
220-
case 's':
221-
EaterEmulator.slowerClock = !EaterEmulator.slowerClock;
222-
break;
223-
case 'i':
224-
EaterEmulator.via.CA1();
225-
break;
237+
if (EaterEmulator.debug)
238+
System.out.println("Size: "+this.getWidth()+" x "+this.getHeight());
239+
break;
240+
case ' ':
241+
EaterEmulator.cpu.clock();
242+
break;
243+
case 'c':
244+
EaterEmulator.clockState = !EaterEmulator.clockState;
245+
break;
246+
case 's':
247+
EaterEmulator.slowerClock = !EaterEmulator.slowerClock;
248+
break;
249+
case 'i':
250+
EaterEmulator.via.CA1();
251+
break;
252+
}
253+
} else {
254+
//Typing Keyboard Mode
255+
Bus.write((short)EaterEmulator.options.data.keyboardLocation, (byte)arg0.getKeyChar());
256+
EaterEmulator.via.CA1();
226257
}
227258
}
228259
}

src/EaterEmulator.java

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
import javax.swing.*;
99

1010
public class EaterEmulator extends JFrame implements ActionListener {
11-
public static String versionString = "2.1";
11+
public static String versionString = "2.5";
1212
public static boolean debug = false;
1313

1414
//Swing Things
@@ -22,14 +22,16 @@ public class EaterEmulator extends JFrame implements ActionListener {
2222
public static JButton ShowGPUButton = new JButton("Show GPU");
2323

2424
public static JButton optionsButton = new JButton("Options");
25-
25+
public static JButton keyboardButton= new JButton("Keyboard Mode");
26+
2627
//Clock Stuff
2728
public static Thread clockThread;
2829
public static boolean clockState = false;
2930
public static int clocks = 0;
3031
public static boolean haltFlag = true;
3132
public static boolean slowerClock = false;
3233
public static boolean running = false;
34+
public static boolean keyboardMode = false; //False = controls (default), True = keyboard
3335

3436
//Emulator Things
3537
public static EaterEmulator emu;
@@ -82,9 +84,16 @@ public EaterEmulator() {
8284
//Options Button
8385
optionsButton.setVisible(true);
8486
optionsButton.addActionListener(this);
85-
optionsButton.setBounds(getWidth()-450, 15, 125, 55);
87+
optionsButton.setBounds(getWidth()-450, 15, 125, 25);
8688
optionsButton.setBackground(Color.white);
8789
GraphicsPanel.add(optionsButton);
90+
91+
//Keyboard Mode Button
92+
keyboardButton.setVisible(true);
93+
keyboardButton.addActionListener(this);
94+
keyboardButton.setBounds(getWidth()-450, 45, 125, 25);
95+
keyboardButton.setBackground(Color.white);
96+
GraphicsPanel.add(keyboardButton);
8897

8998
//file chooser
9099
fc.setDirectory(options.data.defaultFileChooserDirectory);
@@ -107,7 +116,7 @@ public EaterEmulator() {
107116

108117
//Final Setup
109118
GraphicsPanel.setVisible(true);
110-
GraphicsPanel.t.addActionListener(this);
119+
GraphicsPanel.frameTimer.addActionListener(this);
111120
options.setVisible(false);
112121

113122
this.setTitle("6502 Emulator");
@@ -149,7 +158,7 @@ public void actionPerformed(ActionEvent e) {
149158
gpu.setVisible(!gpu.isVisible());
150159
} else if (e.getSource().equals(optionsButton)) {
151160
options.setVisible(!options.isVisible());
152-
} else if (e.getSource().equals(GraphicsPanel.t)) {
161+
} else if (e.getSource().equals(GraphicsPanel.frameTimer)) {
153162
if (!gpu.isVisible()) {
154163
ShowGPUButton.setText("Show GPU");
155164
} else {
@@ -161,6 +170,8 @@ public void actionPerformed(ActionEvent e) {
161170
} else {
162171
ShowLCDButton.setText("Hide LCD");
163172
}
173+
} else if (e.getSource().equals(keyboardButton)) {
174+
keyboardMode = !keyboardMode;
164175
}
165176

166177

src/GPU.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class GPU extends JFrame implements ActionListener {
4545
RAM vram;
4646

4747
public GPU(RAM vram,boolean isVisible) {
48-
this.setSize(GPUPixelScale*width,GPUPixelScale*height);
48+
this.setSize(GPUPixelScale*width,(GPUPixelScale*height)+30); //+30 for title bar
4949

5050
t = new Timer(16,this);
5151
t.start();

src/OptionsData.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ public class OptionsData implements Serializable {
55
//Default Options
66
String optionsFileString = "";
77
String defaultSaveDirectory = System.getProperty("os.name").toUpperCase().contains("WIN") ?
8-
System.getenv("AppData")+"/JavaEaterEmulator/" :
8+
System.getenv("AppData")+"\\JavaEaterEmulator\\" :
99
System.getProperty("user.home") + "/Library/Application Support/JavaEaterEmulator/";
1010
String defaultFileChooserDirectory = System.getProperty("user.home") + System.getProperty("file.separator")+ "Documents";
1111
int VIA_Address = 0x6000;
@@ -16,6 +16,7 @@ public class OptionsData implements Serializable {
1616
int GPUBufferBegin = 0x2000;
1717
int GPUMode = GPU.gpuMode;
1818
int GPUBitmapPixelScale = GPU.GPUPixelScale;
19+
int keyboardLocation = 0x3fff;
1920
Color bgColor = Color.blue;
2021
Color fgColor = Color.white;
2122

@@ -29,6 +30,7 @@ public String toString() {
2930
"GPU Character Dimentsions: "+GPUCols+"x"+GPURows+"\n"+
3031
"GPU Mode: "+GPUMode+"\n"+
3132
"GPU Bitmap Pixel Scale: "+GPUBitmapPixelScale+"\n"+
33+
"Keyboard Memory Location: "+keyboardLocation+"\n"+
3234
"Background Color: "+bgColor.toString()+"\n"+
3335
"Foreground Color: "+fgColor.toString()+"\n";
3436
}

0 commit comments

Comments
 (0)