Increase CPU cycle count per frame to smooth input

This commit is contained in:
Doyle Thai 2017-04-07 17:35:55 +10:00
parent 77346c3674
commit 4a77719d89
4 changed files with 489 additions and 463 deletions

View File

@ -290,31 +290,31 @@ FILE_SCOPE Chip8Controller dchip8_controller_map_input(PlatformInput input)
Chip8Controller result = {};
result.key[0x01] = input.key_1.isDown;
result.key[0x02] = input.key_2.isDown;
result.key[0x03] = input.key_3.isDown;
result.key[0x0C] = input.key_4.isDown;
result.key[0x01] = input.key_1.endedDown;
result.key[0x02] = input.key_2.endedDown;
result.key[0x03] = input.key_3.endedDown;
result.key[0x0C] = input.key_4.endedDown;
result.key[0x04] = input.key_q.isDown;
result.key[0x05] = input.key_w.isDown;
result.key[0x06] = input.key_e.isDown;
result.key[0x0D] = input.key_r.isDown;
result.key[0x04] = input.key_q.endedDown;
result.key[0x05] = input.key_w.endedDown;
result.key[0x06] = input.key_e.endedDown;
result.key[0x0D] = input.key_r.endedDown;
result.key[0x07] = input.key_a.isDown;
result.key[0x08] = input.key_s.isDown;
result.key[0x09] = input.key_d.isDown;
result.key[0x0E] = input.key_f.isDown;
result.key[0x07] = input.key_a.endedDown;
result.key[0x08] = input.key_s.endedDown;
result.key[0x09] = input.key_d.endedDown;
result.key[0x0E] = input.key_f.endedDown;
result.key[0x0A] = input.key_z.isDown;
result.key[0x00] = input.key_x.isDown;
result.key[0x0B] = input.key_c.isDown;
result.key[0x0F] = input.key_v.isDown;
result.key[0x0A] = input.key_z.endedDown;
result.key[0x00] = input.key_x.endedDown;
result.key[0x0B] = input.key_c.endedDown;
result.key[0x0F] = input.key_v.endedDown;
return result;
}
void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
PlatformMemory memory)
PlatformMemory memory, u32 cyclesToEmulate)
{
DQNT_ASSERT(cpu.indexRegister >= 0 && cpu.indexRegister <= 0xFFF);
DQNT_ASSERT(cpu.programCounter >= 0 && cpu.programCounter <= 0xFFF);
@ -333,7 +333,7 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
if (cpu.state == chip8state_load_file)
{
PlatformFile file = {};
if (platform_open_file(L"roms/pong", &file))
if (platform_open_file(L"roms/brix", &file))
{
DQNT_ASSERT((cpu.INIT_ADDRESS + file.size) <=
memory.permanentMemSize);
@ -359,7 +359,7 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
if (controller.key[keyVal])
{
u8 regIndex = cpu.storeKeyToRegisterIndex;
DQNT_ASSERT(keyVal >= 0 && keyVal < 0x0F);
DQNT_ASSERT(keyVal >= 0 && keyVal <= 0x0F);
cpu.registerArray[regIndex] = (u8)keyVal;
cpu.state = chip8state_running;
@ -369,10 +369,13 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
}
if (cpu.state == chip8state_running)
{
bool earlyExit = false;
for (u32 opCycle = 0; opCycle < cyclesToEmulate && !earlyExit;
opCycle++)
{
u8 opHighByte = mainMem[cpu.programCounter++];
u8 opLowByte = mainMem[cpu.programCounter++];
u8 opFirstNibble = (opHighByte & 0xF0);
switch (opFirstNibble)
{
@ -407,7 +410,8 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
{
DQNT_ASSERT(opFirstNibble == 0x20);
cpu.stack[cpu.stackPointer++] = cpu.programCounter;
DQNT_ASSERT(cpu.stackPointer < DQNT_ARRAY_COUNT(cpu.stack));
DQNT_ASSERT(cpu.stackPointer <
DQNT_ARRAY_COUNT(cpu.stack));
}
cpu.programCounter = loc;
@ -441,10 +445,12 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
case 0x50:
{
u8 firstRegNum = (0x0F & opHighByte);
DQNT_ASSERT(firstRegNum < DQNT_ARRAY_COUNT(cpu.registerArray));
DQNT_ASSERT(firstRegNum <
DQNT_ARRAY_COUNT(cpu.registerArray));
u8 secondRegNum = (0xF0 & opLowByte) >> 4;
DQNT_ASSERT(secondRegNum < DQNT_ARRAY_COUNT(cpu.registerArray));
DQNT_ASSERT(secondRegNum <
DQNT_ARRAY_COUNT(cpu.registerArray));
u8 *vx = &cpu.registerArray[firstRegNum];
u8 *vy = &cpu.registerArray[secondRegNum];
@ -478,10 +484,12 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
case 0x80:
{
u8 firstRegNum = (0x0F & opHighByte);
DQNT_ASSERT(firstRegNum < DQNT_ARRAY_COUNT(cpu.registerArray));
DQNT_ASSERT(firstRegNum <
DQNT_ARRAY_COUNT(cpu.registerArray));
u8 secondRegNum = (0xF0 & opLowByte) >> 4;
DQNT_ASSERT(secondRegNum < DQNT_ARRAY_COUNT(cpu.registerArray));
DQNT_ASSERT(secondRegNum <
DQNT_ARRAY_COUNT(cpu.registerArray));
u8 *vx = &cpu.registerArray[firstRegNum];
u8 *vy = &cpu.registerArray[secondRegNum];
@ -531,7 +539,6 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
cpu.VF = 0;
*vx = (u8)(256 + *vx - *vy);
}
}
// SHR Vx {, Vy} - 8xy6 - Set Vx = Vx SHR 1
else if (opFourthNibble == 0x06)
@ -543,7 +550,8 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
*vx >>= 1;
}
// SUBN Vx {, Vy} - 8xy7 - Set Vx = Vy - Vx, set VF = NOT borrow
// SUBN Vx {, Vy} - 8xy7 - Set Vx = Vy - Vx, set VF = NOT
// borrow
else if (opFourthNibble == 0x07)
{
if (*vy > *vx)
@ -556,7 +564,6 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
cpu.VF = 0;
*vx = (u8)(256 + *vy - *vx);
}
}
// SHL Vx {, Vy} - 8xyE - Set Vx = SHL 1
else
@ -576,10 +583,12 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
case 0x90:
{
u8 firstRegNum = (0x0F & opHighByte);
DQNT_ASSERT(firstRegNum < DQNT_ARRAY_COUNT(cpu.registerArray));
DQNT_ASSERT(firstRegNum <
DQNT_ARRAY_COUNT(cpu.registerArray));
u8 secondRegNum = (0xF0 & opLowByte) >> 4;
DQNT_ASSERT(secondRegNum < DQNT_ARRAY_COUNT(cpu.registerArray));
DQNT_ASSERT(secondRegNum <
DQNT_ARRAY_COUNT(cpu.registerArray));
u8 *vx = &cpu.registerArray[firstRegNum];
u8 *vy = &cpu.registerArray[secondRegNum];
@ -599,7 +608,8 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
// JP V0, addr - Bnnn - Jump to location (nnn + V0)
case 0xB0:
{
u16 addr = (((0x0F & opHighByte) << 8) | opLowByte) + cpu.V0;
u16 addr =
(((0x0F & opHighByte) << 8) | opLowByte) + cpu.V0;
cpu.programCounter = addr;
}
break;
@ -619,13 +629,15 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
}
break;
// DRW Vx, Vy, nibble - Dxyn - Display n-byte sprite starting at mem
// DRW Vx, Vy, nibble - Dxyn - Display n-byte sprite starting at
// mem
// location I at (Vx, Vy), set VF = collision
case 0xD0:
{
u8 xRegister = (0x0F & opHighByte);
u8 yRegister = (0xF0 & opLowByte) >> 4;
DQNT_ASSERT(xRegister < DQNT_ARRAY_COUNT(cpu.registerArray) &&
DQNT_ASSERT(
xRegister < DQNT_ARRAY_COUNT(cpu.registerArray) &&
yRegister < DQNT_ARRAY_COUNT(cpu.registerArray));
u8 initPosX = cpu.registerArray[xRegister];
@ -649,7 +661,8 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
// NOTE: Flip the Y
posY = ((u8)(renderBuffer.height - 1) - posY);
const i32 ALPHA_BYTE_INTERVAL = renderBuffer.bytesPerPixel;
const i32 ALPHA_BYTE_INTERVAL =
renderBuffer.bytesPerPixel;
const i32 BITS_IN_BYTE = 8;
i32 baseBitShift = BITS_IN_BYTE - 1;
for (i32 shift = 0; shift < BITS_IN_BYTE; shift++)
@ -660,8 +673,10 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
u32 bitmapOffset =
(posX * BYTES_PER_PIXEL) + (posY * pitch);
// NOTE: Since we are using a 4bpp bitmap, let's use the
// alpha channel to determine if a pixel is on or not.
// NOTE: Since we are using a 4bpp bitmap, let's use
// the
// alpha channel to determine if a pixel is on or
// not.
u32 *pixel = (u32 *)(&renderBitmap[bitmapOffset]);
u8 alphaBit = (*pixel >> 24);
@ -672,7 +687,8 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
bool spriteBit = ((spriteBytes >> bitShift) & 1);
bool pixelIsOn = (pixelWasOn ^ spriteBit);
// NOTE: If caused a pixel to XOR into off, then this is
// NOTE: If caused a pixel to XOR into off, then
// this is
// known as a "collision" in chip8
if (pixelWasOn && !pixelIsOn) collisionFlag = true;
@ -700,13 +716,15 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
DQNT_ASSERT(vx < DQNT_ARRAY_COUNT(controller.key));
bool skipNextInstruction = false;
// SKP Vx - Ex9E - Skip next instruction if key with the value
// SKP Vx - Ex9E - Skip next instruction if key with the
// value
// of Vx is pressed
if (opLowByte == 0x9E)
{
skipNextInstruction = controller.key[vx];
}
// SKNP Vx - ExA1 - Skip next instruction if key with the value
// SKNP Vx - ExA1 - Skip next instruction if key with the
// value
// of Vx is not pressed
else
{
@ -729,12 +747,14 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
{
*vx = cpu.delayTimer;
}
// LD Vx, K - Fx0A - Wait for a key press, store the value of
// LD Vx, K - Fx0A - Wait for a key press, store the value
// of
// the key in Vx
else if (opLowByte == 0x0A)
{
cpu.state = chip8state_await_input;
cpu.storeKeyToRegisterIndex = regNum;
earlyExit = true;
}
// LD DT, Vx - Fx15 - Set delay timer = Vx
else if (opLowByte == 0x15)
@ -764,11 +784,13 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
cpu.I = START_ADDR_OF_FONT +
(hexCharFromFontSet * BYTES_PER_FONT);
}
// LD B, Vx - Fx33 - Store BCD representations of Vx in memory
// LD B, Vx - Fx33 - Store BCD representations of Vx in
// memory
// locations I, I+1 and I+2
else if (opLowByte == 0x33)
{
DQNT_ASSERT(regNum < DQNT_ARRAY_COUNT(cpu.registerArray));
DQNT_ASSERT(regNum <
DQNT_ARRAY_COUNT(cpu.registerArray));
u8 vxVal = *vx;
const i32 NUM_DIGITS_IN_HUNDREDS = 3;
@ -777,10 +799,12 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
u8 rem = vxVal % 10;
vxVal /= 10;
mainMem[cpu.I + ((NUM_DIGITS_IN_HUNDREDS-1) - i)] = rem;
mainMem[cpu.I +
((NUM_DIGITS_IN_HUNDREDS - 1) - i)] = rem;
}
}
// LD [I], Vx - Fx55 - Store register V0 through Vx in memory
// LD [I], Vx - Fx55 - Store register V0 through Vx in
// memory
// starting at location I.
else if (opLowByte == 0x55)
{
@ -791,7 +815,8 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
cpu.registerArray[regIndex];
}
}
// LD [I], Vx - Fx65 - Read registers V0 through Vx from memory
// LD [I], Vx - Fx65 - Read registers V0 through Vx from
// memory
// starting at location I.
else
{
@ -806,7 +831,7 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
}
break;
};
}
// IMPORTANT: Timers need to be decremented at a rate of 60hz. Since we
// can run the interpreter faster than that, make sure we decrement
// timers at the fixed rate.
@ -819,9 +844,16 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
{
cpu.elapsedTime = 0;
if (cpu.delayTimer > 0) cpu.delayTimer--;
// TODO(doyle): This needs to play a buzzing sound whilst timer
// > 0
if (cpu.soundTimer > 0) cpu.soundTimer--;
if (cpu.soundTimer > 0)
{
cpu.soundTimer--;
if (cpu.soundTimer == 1)
{
// TODO(doyle): This needs to play a buzzing sound
// whilst timer > 0
}
}
}
}
else

View File

@ -4,6 +4,6 @@
#include "dchip8_platform.h"
void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
PlatformMemory memory);
PlatformMemory memory, u32 cyclesToEmulate);
#endif

View File

@ -46,8 +46,8 @@ enum Key
typedef struct KeyState
{
bool isDown;
u32 transitionCount;
bool endedDown;
u32 halfTransitionCount;
} KeyState;
typedef struct PlatformInput

View File

@ -115,14 +115,13 @@ inline FILE_SCOPE LARGE_INTEGER win32_query_perf_counter_time()
return result;
}
FILE_SCOPE inline void win32_parse_key_msg(KeyState *key, MSG msg)
FILE_SCOPE inline void win32_update_key(KeyState *key, bool isDown)
{
LPARAM lParam = msg.lParam;
bool keyIsDown = ((lParam >> 30) & 1);
bool keyTransitioned = ((lParam >> 31) & 1);
key->isDown = keyIsDown;
if (keyTransitioned) key->transitionCount++;
if (key->endedDown != isDown)
{
key->endedDown = isDown;
key->halfTransitionCount++;
}
}
FILE_SCOPE void win32_process_messages(HWND window, PlatformInput *input)
@ -137,37 +136,38 @@ FILE_SCOPE void win32_process_messages(HWND window, PlatformInput *input)
case WM_KEYDOWN:
case WM_KEYUP:
{
bool isDown = (msg.message == WM_KEYDOWN);
switch (msg.wParam)
{
case VK_UP: win32_parse_key_msg(&input->up, msg); break;
case VK_DOWN: win32_parse_key_msg(&input->down, msg); break;
case VK_LEFT: win32_parse_key_msg(&input->left, msg); break;
case VK_RIGHT: win32_parse_key_msg(&input->right, msg); break;
case VK_UP: win32_update_key(&input->up, isDown); break;
case VK_DOWN: win32_update_key(&input->down, isDown); break;
case VK_LEFT: win32_update_key(&input->left, isDown); break;
case VK_RIGHT: win32_update_key(&input->right, isDown); break;
case '1': win32_parse_key_msg(&input->key_1, msg); break;
case '2': win32_parse_key_msg(&input->key_2, msg); break;
case '3': win32_parse_key_msg(&input->key_3, msg); break;
case '4': win32_parse_key_msg(&input->key_4, msg); break;
case '1': win32_update_key(&input->key_1, isDown); break;
case '2': win32_update_key(&input->key_2, isDown); break;
case '3': win32_update_key(&input->key_3, isDown); break;
case '4': win32_update_key(&input->key_4, isDown); break;
case 'Q': win32_parse_key_msg(&input->key_q, msg); break;
case 'W': win32_parse_key_msg(&input->key_w, msg); break;
case 'E': win32_parse_key_msg(&input->key_e, msg); break;
case 'R': win32_parse_key_msg(&input->key_r, msg); break;
case 'Q': win32_update_key(&input->key_q, isDown); break;
case 'W': win32_update_key(&input->key_w, isDown); break;
case 'E': win32_update_key(&input->key_e, isDown); break;
case 'R': win32_update_key(&input->key_r, isDown); break;
case 'A': win32_parse_key_msg(&input->key_a, msg); break;
case 'S': win32_parse_key_msg(&input->key_s, msg); break;
case 'D': win32_parse_key_msg(&input->key_d, msg); break;
case 'F': win32_parse_key_msg(&input->key_f, msg); break;
case 'A': win32_update_key(&input->key_a, isDown); break;
case 'S': win32_update_key(&input->key_s, isDown); break;
case 'D': win32_update_key(&input->key_d, isDown); break;
case 'F': win32_update_key(&input->key_f, isDown); break;
case 'Z': win32_parse_key_msg(&input->key_z, msg); break;
case 'X': win32_parse_key_msg(&input->key_x, msg); break;
case 'C': win32_parse_key_msg(&input->key_c, msg); break;
case 'V': win32_parse_key_msg(&input->key_v, msg); break;
case 'Z': win32_update_key(&input->key_z, isDown); break;
case 'X': win32_update_key(&input->key_x, isDown); break;
case 'C': win32_update_key(&input->key_c, isDown); break;
case 'V': win32_update_key(&input->key_v, isDown); break;
case VK_ESCAPE:
{
win32_parse_key_msg(&input->escape, msg);
if (input->escape.isDown) globalRunning = false;
win32_update_key(&input->escape, isDown);
if (input->escape.endedDown) globalRunning = false;
}
break;
@ -279,7 +279,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
platformMemory.permanentMemSize = DQNT_ARRAY_COUNT(stackMemory);
QueryPerformanceFrequency(&globalQueryPerformanceFrequency);
const f32 TARGET_FRAMES_PER_S = 540.0f;
const f32 TARGET_FRAMES_PER_S = 30.0f;
f32 targetSecondsPerFrame = 1 / TARGET_FRAMES_PER_S;
f32 frameTimeInS = 0.0f;
globalRunning = true;
@ -300,13 +300,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
platformBuffer.height = globalRenderBitmap.height;
platformBuffer.width = globalRenderBitmap.width;
platformBuffer.bytesPerPixel = globalRenderBitmap.bytesPerPixel;
dchip8_update(platformBuffer, platformInput, platformMemory);
for (i32 i = 0; i < DQNT_ARRAY_COUNT(platformInput.key); i++)
{
if (platformInput.key[i].isDown)
OutputDebugString(L"Key is down\n");
}
dchip8_update(platformBuffer, platformInput, platformMemory, 15);
}
////////////////////////////////////////////////////////////////////////