fixed keyboard buffer error

This commit is contained in:
Julian Hinxlage 2020-01-07 11:40:47 +01:00
parent 50b24b5644
commit 90948c9edb
4 changed files with 12 additions and 21 deletions

View File

@ -21,7 +21,7 @@ namespace vkvm {
setForegroundColor(white); setForegroundColor(white);
setRedrawInterval(500); setRedrawInterval(500);
setTimerInterruptInterval(1000); setTimerInterruptInterval(1000);
setFont(FontType::FuturisticBlack); setFont(FontType::ProFontIIX);
} }
impl.localMemoryWarn = true; impl.localMemoryWarn = true;
} }
@ -96,10 +96,7 @@ namespace vkvm {
auto buttonPressed(KeyCode keyCode) -> void { auto buttonPressed(KeyCode keyCode) -> void {
lockSharedMemory(); lockSharedMemory();
auto reg = getRegisters(); auto reg = getRegisters();
reg->keyboardBuffer[reg->keyboardBuffer_index_write++] = keyCode; reg->keyboardBuffer[(reg->keyboardBuffer_index_write) % keyboardBufferSize] = keyCode;
if (reg->keyboardBuffer_index_write >= keyboardBufferSize) {
reg->keyboardBuffer_index_write = 0;
}
unlockSharedMemory(); unlockSharedMemory();
} }

View File

@ -319,21 +319,15 @@ namespace vkvm {
lockSharedMemory(); lockSharedMemory();
auto keyCode = KeyCode(0); auto keyCode = KeyCode(0);
auto reg = getRegisters(); auto reg = getRegisters();
if (reg->keyboardBuffer_index_read != reg->keyboardBuffer_index_write) {
auto code = reg->keyboardBuffer[reg->keyboardBuffer_index_read++]; if(reg->keyboardBuffer_index_read >= reg->keyboardBuffer_index_write) {
auto code = reg->keyboardBuffer[(reg->keyboardBuffer_index_read) % keyboardBufferSize];
keyCode = KeyCode(code); keyCode = KeyCode(code);
if (reg->keyboardBuffer_index_read >= keyboardBufferSize) {
reg->keyboardBuffer_index_read = 0;
}
}else{ }else{
if(reg->keyboardBuffer_index_read == 0){ auto code = reg->keyboardBuffer[(reg->keyboardBuffer_index_read) % keyboardBufferSize];
auto code = reg->keyboardBuffer[keyboardBufferSize - 1]; keyCode = KeyCode(code);
keyCode = KeyCode(code);
}else{
auto code = reg->keyboardBuffer[reg->keyboardBuffer_index_read - 1];
keyCode = KeyCode(code);
}
} }
unlockSharedMemory(); unlockSharedMemory();
return keyCode; return keyCode;
} }

View File

@ -12,8 +12,8 @@ TEST_CASE("Keyboard Buffer") {
} }
SECTION("read") { SECTION("read") {
CHECK(vkvm::getLastPressedKey() == vkvm::A); CHECK(vkvm::getLastPressedKey() == vkvm::C);
CHECK(vkvm::getLastPressedKey() == vkvm::B); CHECK(vkvm::getLastPressedKey() == vkvm::C);
CHECK(vkvm::getLastPressedKey() == vkvm::C); CHECK(vkvm::getLastPressedKey() == vkvm::C);
} }
} }

View File

@ -17,7 +17,7 @@ TEST_CASE("Default values") {
REQUIRE(vkvm::getForegroundColor() == vkvm::white); REQUIRE(vkvm::getForegroundColor() == vkvm::white);
REQUIRE(vkvm::getRedrawInterval() == 500); REQUIRE(vkvm::getRedrawInterval() == 500);
REQUIRE(vkvm::getTimerInterruptInterval() == 1000); REQUIRE(vkvm::getTimerInterruptInterval() == 1000);
REQUIRE(vkvm::getFont() == vkvm::FuturisticBlack); REQUIRE(vkvm::getFont() == vkvm::ProFontIIX);
} }
SECTION("change") { SECTION("change") {
@ -63,7 +63,7 @@ TEST_CASE("Default values") {
REQUIRE(vkvm::getForegroundColor() == vkvm::white); REQUIRE(vkvm::getForegroundColor() == vkvm::white);
REQUIRE(vkvm::getRedrawInterval() == 500); REQUIRE(vkvm::getRedrawInterval() == 500);
REQUIRE(vkvm::getTimerInterruptInterval() == 1000); REQUIRE(vkvm::getTimerInterruptInterval() == 1000);
REQUIRE(vkvm::getFont() == vkvm::FuturisticBlack); REQUIRE(vkvm::getFont() == vkvm::ProFontIIX);
} }