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

Commit

Permalink
pequenas correções gerais, melhorias em display, fonte grande
Browse files Browse the repository at this point in the history
  • Loading branch information
jbatistareis committed Jun 9, 2018
1 parent 5eeda51 commit 6e16f65
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 31 deletions.
6 changes: 2 additions & 4 deletions src/main/java/com/jbatista/batatinha/EmulatorController.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,12 +85,12 @@ public void handle(long now) {
// clear screen
canvas.getGraphicsContext2D().setFill(backgroundColor);
canvas.getGraphicsContext2D().fillRect(0, 0, canvas.getWidth(), canvas.getHeight());

// draw pixels
canvas.getGraphicsContext2D().setFill(pixelColor);
for (int iy = 0; iy < canvas.getHeight(); iy += scale) {
for (int ix = 0; ix < canvas.getWidth(); ix += scale) {
if(chip8.getDisplay()[bufferPosition++] == 1){
if (chip8.getDisplay()[bufferPosition++] == 1) {
canvas.getGraphicsContext2D().fillRect(ix, iy, scale, scale);
}
}
Expand Down Expand Up @@ -241,7 +241,6 @@ private void settings(ActionEvent event) throws Exception {
if (result.get()) {
backgroundColor = Color.web(MainApp.settings.getBackgroundColor());
pixelColor = Color.web(MainApp.settings.getPixelColor());
chip8.changeCPUSpeed(MainApp.settings.getCpuSpeed());
chip8.changeNote(MainApp.settings.getNote());

MainApp.settings.save();
Expand All @@ -267,7 +266,6 @@ private void load() throws IOException {
animationTimer.stop();
chip8 = new Chip8(
MainApp.input,
MainApp.settings.getCpuSpeed(),
MainApp.settings.getNote(),
program);
animationTimer.start();
Expand Down
35 changes: 13 additions & 22 deletions src/main/java/com/jbatista/batatinha/emulator/Chip8.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -19,7 +16,6 @@ public class Chip8 {

private File program;
private final Random random = new Random();
private short cpuSpeed;

// CPU, memory, registers, font
private char opcode;
Expand Down Expand Up @@ -87,13 +83,11 @@ public class Chip8 {

public Chip8(
Input input,
short cupSpeed,
String note,
File program) throws IOException {
this.input = input;
this.program = program;
this.cpuSpeed = cupSpeed;
this.display = new Display(Mode.CHIP8);
this.display = new Display();
this.buzzer = new Buzzer(note);

// <editor-fold defaultstate="collapsed" desc="hardcoded opcode functions, double click to expand (Netbeans)">
Expand Down Expand Up @@ -181,10 +175,6 @@ public void start() throws IOException {
fileInputStream.close();
}

public void changeCPUSpeed(short newSpeed) {
this.cpuSpeed = newSpeed;
}

public void changeNote(String note) {
this.buzzer.setNote(note);
}
Expand Down Expand Up @@ -399,20 +389,21 @@ private void rand(char opc) {
}

// DXYN
// if N = 0, it loads a 16 x 16 sprite
// if N = 0, and hi res is active, it loads a 16 x 16 sprite, else is 8 x N
private void draw(char opc) {
drawN = opc & 0x000F;
if (drawN > 0) {
for (int index = 0; index < drawN; index++) {
display.addSpriteData(memory[i + index]);
}
} else {
drawN = (((drawN = opc & 0x000F) == 0) && display.getDisplayMode().equals(Mode.HIGH_RES)) ? 16 : drawN;

if (display.getDisplayMode().equals(Mode.HIGH_RES) && (drawN == 16)) {
for (int index = 0; index < 32; index += 2) {
display.addSpriteData((char) (memory[i + index] << 8 | memory[i + index + 1]));
}
} else {
for (int index = 0; index < drawN; index++) {
display.addSpriteData(memory[i + index]);
}
}

v[0xF] = display.draw(v[(opcode & 0x0F00) >> 8], v[(opcode & 0x00F0) >> 4], (drawN == 0 ? 16 : 8));
v[0xF] = display.draw(v[(opcode & 0x0F00) >> 8], v[(opcode & 0x00F0) >> 4], (drawN == 16 ? 16 : 8));
programCounter += 2;
}

Expand Down Expand Up @@ -534,19 +525,19 @@ private void scrollLeft(char opc) {

// 00FE
private void loRes(char opc) {
display.changeDisplayMode(Mode.CHIP8);
display.changeDisplayMode(Mode.LOW_RES);
programCounter += 2;
}

// 00FF
private void hiRes(char opc) {
display.changeDisplayMode(Mode.SCHIP);
display.changeDisplayMode(Mode.HIGH_RES);
programCounter += 2;
}

// F030
private void setIToSpriteInVx10bit(char opc) {
i = (char) ((v[(opc & 0x0F00) >> 8] * 10));
i = (char) ((v[(opc & 0x0F00) >> 8] * 10 + chip8Font.length));
programCounter += 2;
}

Expand Down
17 changes: 12 additions & 5 deletions src/main/java/com/jbatista/batatinha/emulator/Display.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,24 @@ public class Display {
private int pyOffset;
private int spriteHexComparator;

private Mode mode = Mode.LOW_RES;

public enum Mode {
CHIP8, SCHIP
LOW_RES, HIGH_RES
}

public Display(Mode mode) {
public Display() {
changeDisplayMode(mode);
}

void changeDisplayMode(Mode mode) {
switch (mode) {
case CHIP8:
this.mode = mode;
switch (this.mode) {
case LOW_RES:
width = 64;
height = 32;
break;
case SCHIP:
case HIGH_RES:
width = 128;
height = 64;
break;
Expand All @@ -52,6 +55,10 @@ void changeDisplayMode(Mode mode) {
clear();
}

Mode getDisplayMode() {
return this.mode;
}

char draw(int x, int y, int spriteWidth) {
collision = 0;
spriteHexComparator = (spriteWidth == 8) ? 0x80 : 0x8000;
Expand Down

0 comments on commit 6e16f65

Please sign in to comment.