diff --git a/src/main/java/com/jbatista/batatinha/EmulatorController.java b/src/main/java/com/jbatista/batatinha/EmulatorController.java index 51e2073..5ea54b2 100644 --- a/src/main/java/com/jbatista/batatinha/EmulatorController.java +++ b/src/main/java/com/jbatista/batatinha/EmulatorController.java @@ -74,6 +74,11 @@ public EmulatorController() { animationTimer = new AnimationTimer() { @Override public void handle(long now) { + chip8.timerTick(); + for (int i = 0; i < (MainApp.settings.getCpuSpeed() * 0.016); i++) { + chip8.cpuTick(); + } + bufferPosition = 0; scale = (chip8.getDisplay().length == 2048) ? 8 : 4; @@ -256,11 +261,7 @@ private void load() throws IOException { MainApp.settings.save(); animationTimer.stop(); - if (chip8 != null) { - chip8.shutdown(); - } chip8 = new Chip8( - MainApp.executor, MainApp.input, MainApp.settings.getCpuSpeed(), MainApp.settings.getNote(), diff --git a/src/main/java/com/jbatista/batatinha/MainApp.java b/src/main/java/com/jbatista/batatinha/MainApp.java index bb0f00b..9a23e69 100644 --- a/src/main/java/com/jbatista/batatinha/MainApp.java +++ b/src/main/java/com/jbatista/batatinha/MainApp.java @@ -6,8 +6,6 @@ import com.jbatista.batatinha.emulator.Settings; import java.io.File; import java.io.IOException; -import java.util.concurrent.Executors; -import java.util.concurrent.ScheduledExecutorService; import javafx.application.Application; import static javafx.application.Application.launch; import javafx.fxml.FXMLLoader; @@ -17,8 +15,6 @@ public class MainApp extends Application { - public static final ScheduledExecutorService executor = Executors.newScheduledThreadPool(2); - public static final ObjectMapper objectMapper = new ObjectMapper().configure(SerializationFeature.INDENT_OUTPUT, true); public static final File settingsFile = new File("settings.json"); public static Settings settings; @@ -215,9 +211,6 @@ public void start(Stage stage) throws Exception { stage.setTitle("Batatinha"); stage.setScene(scene); - stage.setOnCloseRequest((event) -> { - executor.shutdownNow(); - }); stage.setResizable(false); stage.show(); } diff --git a/src/main/java/com/jbatista/batatinha/emulator/Buzzer.java b/src/main/java/com/jbatista/batatinha/emulator/Buzzer.java index 7d19d04..2799da0 100644 --- a/src/main/java/com/jbatista/batatinha/emulator/Buzzer.java +++ b/src/main/java/com/jbatista/batatinha/emulator/Buzzer.java @@ -23,11 +23,11 @@ public void beep() { } private byte[] sineWave(int frequency, int amplitude) { - final byte[] output = new byte[1500]; + final byte[] output = new byte[3000]; final double f = (double) frequency / 22050; for (int i = 0; i < output.length; i++) { - output[i] = (byte) (((i > 1449) ? --amplitude : amplitude) * Math.sin(pi2 * f * i)); + output[i] = (byte) (((i > 2949) ? --amplitude : amplitude) * Math.sin(pi2 * f * i)); } return output; @@ -45,25 +45,25 @@ public void setNote(String note) { switch (this.note) { case "A": - clip.open(audioFormat, sineWave(440, 50), 0, 1500); + clip.open(audioFormat, sineWave(440, 50), 0, 3000); break; case "B": - clip.open(audioFormat, sineWave(493, 50), 0, 1500); + clip.open(audioFormat, sineWave(493, 50), 0, 3000); break; case "C": - clip.open(audioFormat, sineWave(523, 50), 0, 1500); + clip.open(audioFormat, sineWave(523, 50), 0, 3000); break; case "D": - clip.open(audioFormat, sineWave(587, 50), 0, 1500); + clip.open(audioFormat, sineWave(587, 50), 0, 3000); break; case "E": - clip.open(audioFormat, sineWave(659, 50), 0, 1500); + clip.open(audioFormat, sineWave(659, 50), 0, 3000); break; case "F": - clip.open(audioFormat, sineWave(698, 50), 0, 1500); + clip.open(audioFormat, sineWave(698, 50), 0, 3000); break; case "G": - clip.open(audioFormat, sineWave(783, 50), 0, 1500); + clip.open(audioFormat, sineWave(783, 50), 0, 3000); break; default: new RuntimeException("Sound note '" + note + "' not recognized."); diff --git a/src/main/java/com/jbatista/batatinha/emulator/Chip8.java b/src/main/java/com/jbatista/batatinha/emulator/Chip8.java index 04f8441..2d12463 100644 --- a/src/main/java/com/jbatista/batatinha/emulator/Chip8.java +++ b/src/main/java/com/jbatista/batatinha/emulator/Chip8.java @@ -79,22 +79,17 @@ public class Chip8 { private final Input input; private final Display display; private final Buzzer buzzer; - private final ScheduledExecutorService executor; private final Map> opcodesMap = new HashMap<>(); - private ScheduledFuture timer60Hz; - private ScheduledFuture timerCPU; private boolean beep; private char decodedOpcode; private char tempResult; private int drawN; public Chip8( - ScheduledExecutorService executor, Input input, short cupSpeed, String note, File program) throws IOException { - this.executor = executor; this.input = input; this.program = program; this.cpuSpeed = cupSpeed; @@ -157,13 +152,6 @@ public Chip8( } public void start() throws IOException { - if (timer60Hz != null) { - timer60Hz.cancel(true); - } - if (timerCPU != null) { - timerCPU.cancel(true); - } - Arrays.fill(v, (char) 0); Arrays.fill(stack, (char) 0); Arrays.fill(memory, (char) 0); @@ -191,21 +179,6 @@ public void start() throws IOException { memory[index++ + 512] = (char) data; } fileInputStream.close(); - - // 60Hz timer - timer60Hz = executor.scheduleWithFixedDelay(this::timerTick, 0, 16666, TimeUnit.MICROSECONDS); - - // CPU timer - timerCPU = executor.scheduleWithFixedDelay(() -> { - for (int i = 0; i < (cpuSpeed * 0.016); i++) { - cpuTick(); - } - }, 0, 16666, TimeUnit.MICROSECONDS); - } - - public void shutdown() { - timer60Hz.cancel(true); - timerCPU.cancel(true); } public void changeCPUSpeed(short newSpeed) { @@ -221,7 +194,7 @@ public char[] getDisplay() { } // into main loop - private void cpuTick() { + public void cpuTick() { opcode = (char) (memory[programCounter] << 8 | memory[programCounter + 1]); decodedOpcode = (char) (opcode & 0xF000); @@ -247,7 +220,7 @@ private void cpuTick() { } // 60Hz - private void timerTick() { + public void timerTick() { if (soundTimer > 0) { if ((--soundTimer == 0) && beep) { buzzer.beep(); diff --git a/src/main/java/com/jbatista/batatinha/emulator/Display.java b/src/main/java/com/jbatista/batatinha/emulator/Display.java index d4cb5e4..bd1fc1f 100644 --- a/src/main/java/com/jbatista/batatinha/emulator/Display.java +++ b/src/main/java/com/jbatista/batatinha/emulator/Display.java @@ -6,10 +6,6 @@ public class Display { - // private WritableImage image; - // private BufferedImage image; - // private Color backgroundColor; - // private Color pixelColor; private final List sprite = new ArrayList<>(); private char[] buffer; private char[] tempBuffer; @@ -21,13 +17,9 @@ public class Display { private int reducedHeight; private int width; private int height; - // private int imgX; - // private int imgY; private int xPos; private int yPos; private int pixel; - // private int bufferPosition; - // private int scale; private int pyOffset; private int spriteHexComparator; @@ -45,12 +37,10 @@ void changeDisplayMode(Mode mode) { case CHIP8: width = 64; height = 32; - // scale = 6; break; case SCHIP: width = 128; height = 64; - // scale = 3; break; } @@ -59,11 +49,6 @@ void changeDisplayMode(Mode mode) { tempBuffer = new char[buffer.length]; xLine = new char[width]; yLine = new char[height]; - /* - backgroundColor = Color.web(MainApp.settings.getBackgroundColor()); - pixelColor = Color.web(MainApp.settings.getPixelColor()); - image = new WritableImage(width * scale, height * scale); - */ clear(); } @@ -175,51 +160,6 @@ void addSpriteData(char data) { sprite.add(data); } - /* - public BufferedImage getImage() { - bufferPosition = 0; - - for (int iy = 0; iy < image.getHeight(); iy += scale) { - for (int ix = 0; ix < image.getWidth(); ix += scale) { - image.getGraphics().setColor((buffer[bufferPosition++] == 0) ? backgroundColor : pixelColor); - image.getGraphics().drawRect(ix, iy, scale, scale); - } - } - - /* - imgX = 0; - imgY = 0; - - for (int i = 0; i < buffer.length; i++) { - for (int ix = 0; ix < scale; ix++) { - for (int iy = 0; iy < scale; iy++) { - image.getPixelWriter().setColor( - (int) ((imgX + ix) % image.getWidth()), - (int) ((imgY + iy) % image.getHeight()), - (buffer[i] == 0) ? backgroundColor : pixelColor); - } - } - - imgX = (imgX > image.getWidth() - scale - 1) ? 0 : (imgX + scale); - imgY = (imgY > image.getHeight() - 1) ? 0 : (imgX == 0) ? (imgY + scale) : imgY; - } - * - - return image; - } - - public void changeBackgroundColor(String backgroundColor) { - this.backgroundColor = Color.decode(backgroundColor); - } - - public void changePixelColor(String pixelColor) { - this.pixelColor = Color.decode(pixelColor); - } - - public void changeScale(int ratio) { - this.scale *= ratio; - } - */ public char[] getBuffer() { return this.buffer; }