Skip to content

Commit

Permalink
removed unused code
Browse files Browse the repository at this point in the history
  • Loading branch information
ntsakonas committed Dec 9, 2020
1 parent 664a28e commit a75243f
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 99 deletions.
1 change: 1 addition & 0 deletions .idea/gradle.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ public interface SystemState
{
synchronized (timerLock)
{
// System.out.println("tick "+timerTick);
if (timerTick > 0)
--timerTick;
}
Expand Down Expand Up @@ -277,10 +276,7 @@ public byte waitForKey()
{
// NOTE:: this is a blocking method.
// it will return when a key is pressed and then released
System.out.println("waiting for key...");
byte b = keyboard.waitForKey();
System.out.println("key "+b+" pressed...");
return b;
return keyboard.waitForKey();
}

@Override
Expand Down Expand Up @@ -312,7 +308,7 @@ public void setTone(byte value)
{
// check specs how tone is supposed to work
// check the existing emulator how it does it
// todo :9mplement tone
// todo :Implement tone
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,6 @@ else if (lsb == 0x00 && msb == (byte)0xE0)
int numOfPatternBytes = (Math.min(N,15));
byte posX = state.getRegister(X);
byte posY = state.getRegister(Y);
//System.out.println(String.format("write %d (%d) @ (%d,%d)",numOfPatternBytes,N,posX,posY));
boolean spriteCollisionDetected = false;
for (int i=0;i<numOfPatternBytes;i++)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,21 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class RealTimeClock
{
/*
Emulates the 60Hz tick that increments the tick counter (used in most programs for time keeping)
*/
public class RealTimeClock {
private ScheduledExecutorService rtcExecutor;
RealTimeClock()
{

RealTimeClock() {
rtcExecutor = Executors.newScheduledThreadPool(1);
}

void start(Runnable callback)
{
rtcExecutor.scheduleAtFixedRate(callback,0L,16666L, TimeUnit.MICROSECONDS);
void start(Runnable callback) {
rtcExecutor.scheduleAtFixedRate(callback, 0L, 16666L, TimeUnit.MICROSECONDS);
}

void stop()
{
void stop() {
if (!rtcExecutor.isTerminated())
rtcExecutor.shutdownNow();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,12 @@ private void simulateRealSystemTiming()
// it is no known how many instructions were executed per CHIP8 instruction
// but lets assume 20.. that means each CHIP8 instruction would last 90.8 microSec
// lets sleep for this time to emulate the original timing
// NOTE: the minimum delays obtainable in Java is 1 msec, which is fine.
// UPDATE: using 1ms the emulations is very fast. using an emulated clock of 500Hz as described here
// https://jackson-s.me/2019/07/13/Chip-8-Instruction-Scheduling-and-Frequency.html
// the emulation speed is better
try
{
Thread.sleep(1L);
Thread.sleep(2L);
} catch (InterruptedException e)
{
e.printStackTrace();
Expand Down
121 changes: 40 additions & 81 deletions src/main/java/ntsakonas/retro/chipate/ui/ChipUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.nio.file.Files;
Expand All @@ -17,8 +20,7 @@
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

class SharedVideoRam
{
class SharedVideoRam {
public byte[] videoRam;
}

Expand All @@ -27,48 +29,42 @@ public class ChipUI {

SystemDisplay systemDisplay = vram -> sharedVideoRam.videoRam = Arrays.copyOf(vram, vram.length);

private final int SIMULATOR_WINDOW_WIDTH = 400;
private final int SIMULATOR_WINDOW_HEIGHT = 380;
private final int SIMULATOR_WINDOW_WIDTH = 320;
private final int SIMULATOR_WINDOW_HEIGHT = 320;

private JFrame topLevelFrame;
private static Simulator simulator;


public ChipUI()
{
public ChipUI() {
}

public SystemDisplay getSystemDisplay()
{
public SystemDisplay getSystemDisplay() {
return systemDisplay;
}

/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private void createAndShowGUI() {
//Create and set up the window.
//JFrame.setDefaultLookAndFeelDecorated(true);
topLevelFrame = new JFrame("Chip-8 Emulator");
topLevelFrame.setResizable(false);
topLevelFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
topLevelFrame.setBackground(new Color(128, 128, 128));
topLevelFrame.setPreferredSize(new Dimension(SIMULATOR_WINDOW_WIDTH, SIMULATOR_WINDOW_HEIGHT));
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
topLevelFrame.setLocation(screenSize.width / 2 - SIMULATOR_WINDOW_WIDTH / 2, screenSize.height / 2 - SIMULATOR_WINDOW_HEIGHT /2 );
topLevelFrame.setLocation(screenSize.width / 2 - SIMULATOR_WINDOW_WIDTH / 2, screenSize.height / 2 - SIMULATOR_WINDOW_HEIGHT / 2);

//topLevelFrame.getContentPane().add(new SimulatorDisplay(sharedVideoRam), BorderLayout.CENTER);
topLevelFrame.add(new SimulatorDisplay(sharedVideoRam), BorderLayout.CENTER);
topLevelFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
topLevelFrame.addWindowListener(new WindowAdapter()
{
topLevelFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e)
{
public void windowClosing(WindowEvent e) {
simulator.terminate();
topLevelFrame.dispose();
System.out.println("exit");
System.exit(0);

}
Expand All @@ -78,45 +74,35 @@ public void windowClosing(WindowEvent e)
}



public void startSimulatorUI(String romPath) throws IOException
{
public void startSimulatorUI(String romPath) throws IOException {
final byte[] romBytes = Files.readAllBytes(Paths.get(romPath));
javax.swing.SwingUtilities.invokeLater(() -> {
createAndShowGUI();
Keyboard keyboard = new Keyboard();
connectKeyboard(keyboard);
simulator = new Simulator(keyboard,getSystemDisplay());
simulator = new Simulator(keyboard, getSystemDisplay());
// TESTING THE DEBUGGER
//simulator.attachDebugger(new CommandLineDebugger());
simulator.run(romBytes);
});
}

private void connectKeyboard(KeyboardInput keyboardInput)
{
topLevelFrame.addKeyListener(new KeyAdapter()
{
private void connectKeyboard(KeyboardInput keyboardInput) {
topLevelFrame.addKeyListener(new KeyAdapter() {
@Override
public void keyPressed(KeyEvent keyEvent)
{
//System.out.println("key pressed:"+keyEvent.getKeyChar());
public void keyPressed(KeyEvent keyEvent) {
keyboardInput.keyPressed(keyEvent.getKeyChar());
}

@Override
public void keyReleased(KeyEvent keyEvent)
{
//System.out.println("key released:"+keyEvent.getKeyChar());
public void keyReleased(KeyEvent keyEvent) {
keyboardInput.keyReleased(keyEvent.getKeyChar());
}
});
}

public static void main(String[] args) throws IOException
{
if (args.length == 0)
{
public static void main(String[] args) throws IOException {
if (args.length == 0) {
System.out.println("--- Chip-8 simulator by Nick Tsakonas (c) 2018");
System.out.println("--- usage simulator input.rom [base]");
System.out.println(" input.rom - the rom to execute (located at 0x0200)");
Expand All @@ -128,8 +114,7 @@ public static void main(String[] args) throws IOException

}

class SimulatorDisplay extends JPanel
{
class SimulatorDisplay extends JPanel {
private final int SCREEN_WIDTH_PX = 64;
private final int SCREEN_HEIGHT_PX = 32;
final int SCALE = 5;
Expand All @@ -140,61 +125,45 @@ class SimulatorDisplay extends JPanel
private final SharedVideoRam sharedVideoRam;
private BufferedImage vramImage;

public SimulatorDisplay(SharedVideoRam sharedVideoRam)
{
public SimulatorDisplay(SharedVideoRam sharedVideoRam) {
this.sharedVideoRam = sharedVideoRam;
setupDisplay();
}

private void setupDisplay()
{
private void setupDisplay() {
setPreferredSize(new Dimension(SCREEN_WIDTH_PX, SCREEN_HEIGHT_PX));
setBackground(new Color(0, 0, 0));
vramImage = new BufferedImage(displaySizeX, displaySizeY + displayYOffset, BufferedImage.TYPE_INT_RGB);
startScreenUpdate();
}

@Override
protected void paintComponent(Graphics g)
{
//super.paintComponent(g);
protected void paintComponent(Graphics g) {
g.drawImage(vramImage, 0, 0, this);
Toolkit.getDefaultToolkit().sync();
}

private void startScreenUpdate()
{
ScheduledExecutorService rtcExecutor =Executors.newScheduledThreadPool(1);
rtcExecutor.scheduleAtFixedRate(new Runnable()
{
private void startScreenUpdate() {
ScheduledExecutorService rtcExecutor = Executors.newScheduledThreadPool(1);
rtcExecutor.scheduleAtFixedRate(new Runnable() {
@Override
public void run()
{
//synchronized (sharedVideoRam)
{
if (sharedVideoRam.videoRam != null && sharedVideoRam.videoRam.length>0)
refresh(sharedVideoRam.videoRam);
}
public void run() {
if (sharedVideoRam.videoRam != null && sharedVideoRam.videoRam.length > 0)
refresh(sharedVideoRam.videoRam);
}

public void refresh(byte[] vram)
{
for (int y = 0; y < SCREEN_HEIGHT_PX; y++)
{
for (int x = 0; x < SCREEN_WIDTH_PX / 8; x++)
{
public void refresh(byte[] vram) {
for (int y = 0; y < SCREEN_HEIGHT_PX; y++) {
for (int x = 0; x < SCREEN_WIDTH_PX / 8; x++) {
int videoByte = getVideoByte(vram, y, x);
int mask = 0x80;
for (int bits = 0; bits < 8; bits++)
{
for (int bits = 0; bits < 8; bits++) {
boolean isBitOn = ((videoByte & mask) == mask);
int ypos = y * SCALE + displayYOffset;
int xpos = (x * 8 + bits) * SCALE;
if (SCALE == 1)
{
if (SCALE == 1) {
vramImage.setRGB(xpos, ypos, isBitOn ? 0xffffff : 0x000000);
} else
{
} else {
for (int yRepeat = 0; yRepeat < SCALE; ++yRepeat)
for (int xRepeat = 0; xRepeat < SCALE; ++xRepeat)
vramImage.setRGB(xpos + xRepeat, ypos + yRepeat, isBitOn ? 0xffffff : 0x000000);
Expand All @@ -205,23 +174,13 @@ public void refresh(byte[] vram)
}
repaint();
}
private int getVideoByte(byte[] vram, int y, int x)
{

private int getVideoByte(byte[] vram, int y, int x) {
return Byte.toUnsignedInt(vram[y * 8 + x]);
}


}, 0L, 5L, TimeUnit.MILLISECONDS);

}

}

/*
// http://www.baeldung.com/java-images
// https://stackoverflow.com/questions/5281262/how-to-close-the-window-in-awt
// https://www.mkyong.com/java/how-to-convert-byte-to-bufferedimage-in-java/
// https://beginnersbook.com/2015/06/java-awt-tutorial/
//
// https://www.darkcoding.net/software/non-blocking-console-io-is-not-possible/
*/
}

0 comments on commit a75243f

Please sign in to comment.