Skip to content

Commit

Permalink
Merge pull request #7 from johanlindfors/dev/java-file-open
Browse files Browse the repository at this point in the history
Dev/java file open
  • Loading branch information
johanlindfors authored May 26, 2024
2 parents 584bb21 + d52efb2 commit 73fca5c
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
**/zig-out/*
**/build/*
**/target/*
**/dependency-reduced-pom.xml
**/.DS_Store
17 changes: 15 additions & 2 deletions java/src/main/java/se/programmeramera/chip8/CPU.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class CPU {
private Random random = new Random();
private Audio audio;

public CPU(Display display, Keyboard keyboard, Memory memory, Audio audio) {
public CPU(Display display, Keyboard keyboard, Audio audio) {
super();
this.delayTimer = 0;
this.soundTimer = 0;
Expand All @@ -29,16 +29,29 @@ public CPU(Display display, Keyboard keyboard, Memory memory, Audio audio) {
this.i = 0x0000;
this.display = display;
this.keyboard = keyboard;
this.audio = audio;
}

public void attachMemory(Memory memory) {
this.display.clear();
this.delayTimer = 0;
this.soundTimer = 0;
this.registers = new int[16];
this.stack = new Stack<>();
this.pc = 0x0200;
this.i = 0x0000;
this.memory = memory;
this.audio = audio;
}

public static int getBitValue(int value, int bitIndex) {
return value & (0x80 >> bitIndex);
}

public void tick() {
if(this.memory == null) {
return;
}

if(this.delayTimer > 0) {
this.delayTimer--;
}
Expand Down
52 changes: 41 additions & 11 deletions java/src/main/java/se/programmeramera/chip8/Emulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
import java.nio.file.Files;
import java.nio.file.Paths;

import javax.swing.AbstractAction;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import java.awt.event.KeyEvent;
import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.awt.event.ActionEvent;

public class Emulator {

Expand Down Expand Up @@ -38,22 +48,42 @@ private void createAndShowUI(String filename) {
//this.addKeyBindings();
this.setupGameLoop();

JMenuBar menuBar = new JMenuBar();
JMenu menuFile = new JMenu("File");
JMenuItem menuFileOpen = new JMenuItem(new AbstractAction("Open") {
public void actionPerformed(ActionEvent ev) {
JFileChooser fileChooser = new JFileChooser(); // create filechooser
int retVal = fileChooser.showOpenDialog(frame); // open the open dialog
if (retVal == JFileChooser.APPROVE_OPTION) { // check for approval
try {
String filename = fileChooser.getSelectedFile().getAbsolutePath();
Memory memory = new Memory();
byte[] rom = Files.readAllBytes(Paths.get(filename));
memory.loadData(rom);
cpu.attachMemory(memory);
} catch (Exception e) {
e.printStackTrace();
}
}
}
});

menuFile.setMnemonic(KeyEvent.VK_F);
menuFileOpen.setMnemonic(KeyEvent.VK_O);

menuBar.add(menuFile);
menuFile.add(menuFileOpen);
frame.setJMenuBar(menuBar);

frame.add(display);
frame.pack();
frame.setVisible(true);

try {
Memory memory = new Memory();
byte[] rom = Files.readAllBytes(Paths.get(filename));
memory.loadData(rom);
this.cpu = new CPU(display, keyboard, memory, new AudioDevice());
this.cpu = new CPU(display, keyboard, new AudioDevice());

// after setting the frame visible we start the game loop, this could be done in a button or wherever you want
this.isRunning = true;
this.gameLoop.start();
} catch (Exception e) {
e.printStackTrace();
}
// after setting the frame visible we start the game loop, this could be done in a button or wherever you want
this.isRunning = true;
this.gameLoop.start();
}

/**
Expand Down

0 comments on commit 73fca5c

Please sign in to comment.