Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ apply plugin: 'com.github.johnrengelman.shadow'
mainClassName = 'com.chip8java.emulator.runner.Runner'

group = 'com.chip8java'
version = '1.0'
version = '1.0.1'

description = 'A Chip 8 emulator'

Expand Down
32 changes: 23 additions & 9 deletions src/main/java/com/chip8java/emulator/components/Emulator.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,10 @@ public class Emulator
private static final String DEFAULT_FONT = "VeraMono.ttf";

// Whether the Emulator is in trace mode
public boolean inTraceMode;
public volatile boolean inTraceMode;

// Whether the Emulator is in step mode
public boolean inStepMode;
public volatile boolean inStepMode;

// Emulator window and frame elements
private JCheckBoxMenuItem traceMenuItem;
Expand All @@ -64,11 +64,11 @@ public class Emulator
private Font overlayFont;

// The current state of the emulator and associated tasks
private EmulatorState state;
private volatile EmulatorState state;
private int cpuCycleTime;
private Timer timer;
private TimerTask timerTask;
private boolean doSingleStep;
private volatile boolean doSingleStep;

/**
* Convenience constructor that sets the emulator running with a 1x
Expand All @@ -91,8 +91,6 @@ public Emulator(int scale, int cycleTime, String rom, boolean traceMode, boolean
memory = new Memory(Memory.MEMORY_4K);
screen = new Screen(scale);
cpu = new CentralProcessingUnit(memory, keyboard, screen);
inTraceMode = traceMode || stepMode;
inStepMode = stepMode;
doSingleStep = false;

// Load the font file into memory
Expand All @@ -104,22 +102,25 @@ public Emulator(int scale, int cycleTime, String rom, boolean traceMode, boolean
IO.closeStream(fontFileStream);

// Attempt to load specified ROM file
state = EmulatorState.PAUSED;
setPaused();
if (rom != null) {
InputStream romFileStream = IO.openInputStream(rom);
if (!memory.loadStreamIntoMemory(romFileStream,
CentralProcessingUnit.PROGRAM_COUNTER_START)) {
LOGGER.severe("Could not load ROM file [" + rom + "]");
} else {
state = EmulatorState.RUNNING;
setRunning();
}
IO.closeStream(romFileStream);
}

// Initialize the screen, keyboard listeners, and overlayScreen information
initEmulatorJFrame();
initializeOverlay();
setTrace(traceMode || stepMode);
setStep(stepMode);
cpuCycleTime = cycleTime;
start();
}

/**
Expand Down Expand Up @@ -347,7 +348,6 @@ public void setStep(boolean step) {
if (step) {
setTrace(true);
}
state = EmulatorState.STEP;
}

/**
Expand Down Expand Up @@ -399,4 +399,18 @@ public void kill() {
public void dispose() {
container.dispose();
}

/**
* Sets the emulator running.
*/
public void setRunning() {
state = EmulatorState.RUNNING;
}

/**
* Pauses the emulator.
*/
public void setPaused() {
state = EmulatorState.PAUSED;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,5 @@

public enum EmulatorState
{
PAUSED, TRACE, STEP, RUNNING, KILLED
PAUSED, RUNNING, KILLED
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ public void openROMFileDialog() {
if (!memory.loadStreamIntoMemory(inputStream, CentralProcessingUnit.PROGRAM_COUNTER_START)) {
JOptionPane.showMessageDialog(container, "Error reading file.", "File Read Problem",
JOptionPane.ERROR_MESSAGE);
emulator.setPaused();
return;
}
cpu.reset();
emulator.setRunning();
}
}

Expand Down
1 change: 0 additions & 1 deletion src/test/java/com/chip8java/emulator/common/IOTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@

public class IOTest
{
private String testStreamFileBytes = "This is a test";
private static final String GOOD_STREAM_FILE = "test_stream_file.bin";

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.*;

/**
* Tests for the StepMenuItemListener.
Expand All @@ -28,7 +28,7 @@ public class StepMenuItemListenerTest

@Before
public void setUp() {
emulator = new Emulator();
emulator = mock(Emulator.class);
traceMenuItemListener = new StepMenuItemListener(emulator);
ButtonModel buttonModel = mock(ButtonModel.class);
Mockito.when(buttonModel.isSelected()).thenReturn(true).thenReturn(false);
Expand All @@ -38,38 +38,26 @@ public void setUp() {
Mockito.when(mockItemEvent.getSource()).thenReturn(button);
}

@After
public void tearDown() {
emulator.dispose();
}

@Test
public void testCPUInStepModeWhenItemListenerTriggered() {
traceMenuItemListener.itemStateChanged(mockItemEvent);
assertTrue(emulator.inStepMode);
verify(emulator, times(1)).setStep(true);
}

@Test
public void testCPUNotInStepModeWhenItemListenerTriggeredTwice() {
traceMenuItemListener.itemStateChanged(mockItemEvent);
traceMenuItemListener.itemStateChanged(mockItemEvent);
assertFalse(emulator.inStepMode);
}

@Test
public void testCPUStaysInTraceModeWhenStepModeTriggered() {
emulator.setTrace(true);
traceMenuItemListener.itemStateChanged(mockItemEvent);
assertTrue(emulator.inTraceMode);
assertTrue(emulator.inStepMode);
verify(emulator, times(1)).setStep(true);
verify(emulator, times(1)).setStep(false);
}

@Test
public void testCPUStaysInTraceModeWhenStepModeStopped() {
emulator.setTrace(true);
traceMenuItemListener.itemStateChanged(mockItemEvent);
traceMenuItemListener.itemStateChanged(mockItemEvent);
assertTrue(emulator.inTraceMode);
assertFalse(emulator.inStepMode);
verify(emulator, times(1)).setStep(true);
verify(emulator, times(1)).setStep(false);
verify(emulator, times(1)).setTrace(true);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package com.chip8java.emulator.listeners;

import com.chip8java.emulator.components.Emulator;
import jdk.nashorn.internal.ir.annotations.Ignore;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
Expand All @@ -16,6 +17,7 @@
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;

/**
* Tests for the TraceMenuItemListenerTest.
Expand All @@ -28,7 +30,7 @@ public class TraceMenuItemListenerTest

@Before
public void setUp() {
emulator = new Emulator();
emulator = mock(Emulator.class);
traceMenuItemListener = new TraceMenuItemListener(emulator);
ButtonModel buttonModel = mock(ButtonModel.class);
Mockito.when(buttonModel.isSelected()).thenReturn(true).thenReturn(false);
Expand All @@ -38,21 +40,19 @@ public void setUp() {
Mockito.when(mockItemEvent.getSource()).thenReturn(button);
}

@After
public void tearDown() {
emulator.dispose();
}

@Test
@Ignore
public void testCPUInTraceModeWhenItemListenerTriggered() {
traceMenuItemListener.itemStateChanged(mockItemEvent);
assertTrue(emulator.inTraceMode);
Mockito.verify(emulator, times(1)).setTrace(true);
}

@Test
@Ignore
public void testCPUNotInTraceModeWhenItemListenerTriggeredTwice() {
traceMenuItemListener.itemStateChanged(mockItemEvent);
traceMenuItemListener.itemStateChanged(mockItemEvent);
assertFalse(emulator.inTraceMode);
Mockito.verify(emulator, times(1)).setTrace(true);
Mockito.verify(emulator, times(1)).setTrace(false);
}
}