Skip to content
This repository has been archived by the owner on May 25, 2020. It is now read-only.

Commit

Permalink
limpeza, novo áudio, cpu agora usando somente a thread do javafx
Browse files Browse the repository at this point in the history
  • Loading branch information
jbatistareis committed Jun 8, 2018
1 parent d8156c5 commit 54f7fd2
Show file tree
Hide file tree
Showing 5 changed files with 16 additions and 109 deletions.
9 changes: 5 additions & 4 deletions src/main/java/com/jbatista/batatinha/EmulatorController.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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(),
Expand Down
7 changes: 0 additions & 7 deletions src/main/java/com/jbatista/batatinha/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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();
}
Expand Down
18 changes: 9 additions & 9 deletions src/main/java/com/jbatista/batatinha/emulator/Buzzer.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.");
Expand Down
31 changes: 2 additions & 29 deletions src/main/java/com/jbatista/batatinha/emulator/Chip8.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<Character, Consumer<Character>> 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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand All @@ -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);

Expand All @@ -247,7 +220,7 @@ private void cpuTick() {
}

// 60Hz
private void timerTick() {
public void timerTick() {
if (soundTimer > 0) {
if ((--soundTimer == 0) && beep) {
buzzer.beep();
Expand Down
60 changes: 0 additions & 60 deletions src/main/java/com/jbatista/batatinha/emulator/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

public class Display {

// private WritableImage image;
// private BufferedImage image;
// private Color backgroundColor;
// private Color pixelColor;
private final List<Character> sprite = new ArrayList<>();
private char[] buffer;
private char[] tempBuffer;
Expand All @@ -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;
Expand All @@ -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;
}

Expand All @@ -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();
}

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 54f7fd2

Please sign in to comment.