Skip to content

Commit

Permalink
opcode - waiting for key pressed - implementation fixed, simplified a…
Browse files Browse the repository at this point in the history
…nd fully tested
  • Loading branch information
tbielaszewski committed Aug 9, 2018
1 parent 3d003d2 commit d06ee5c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ public boolean accept(short code) {
public void execute(short code, VirtualMachine vm) {
short x = (short) ((code & 0x0F00) >>> 8);

while (!isKeyPressed(vm.keyboard)) {
if(isKeyPressed(vm.keyboard)) {
vm.V[x] = getKeyValue(vm.keyboard);
} else {
vm.PC -= 2;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,33 @@ public void shouldAcceptCode() throws Exception {
assertTrue(opcode.accept(code));
}

@Test(timeout = 1000)
public void shouldWaitForKeystroke() throws Exception {
@Test
public void shouldDecrementProgramCounterWhenKeyIsNotPressed() throws Exception {
short code = (short) 0xFA0A;
byte vxBefore = 0xf;
vm.keyboard = 0;
vm.V[0xA] = vxBefore;
boolean[] isKeypressed = {false};

ExecutorService executor = Executors.newSingleThreadExecutor();

executor.execute(() -> {
sleep(500);

isKeypressed[0] = true;
vm.keyboard = 0b1 << 5;
});
vm.PC = 100;

opcode.execute(code, vm);

if (!isKeypressed[0]) fail();
assertTrue(vm.V[0xA] == 6);
assertTrue(vm.V[0xA] == vxBefore);
assertTrue(vm.PC == 98);
}

@SneakyThrows
private void sleep(long time) {
Thread.sleep(time);
@Test
public void shouldSetRegisterToKeyCodeWhenKey_x_IsPressed() throws Exception {
for (int i = 0; i < 16; i++) {
short code = (short) 0xFA0A;
byte vxBefore = 0xf;
vm.keyboard = (short) ((0b1 << i) & 0xFFFF);
vm.V[0xA] = vxBefore;
vm.PC = 100;

opcode.execute(code, vm);

assertTrue(vm.V[0xA] == i + 1);
assertTrue(vm.PC == 100);
}
}
}

0 comments on commit d06ee5c

Please sign in to comment.