Preinitialise memory with preset font pack

This commit is contained in:
Doyle Thai 2017-04-05 18:31:21 +10:00
parent dc8afe349a
commit b30613e98d
3 changed files with 184 additions and 26 deletions

View File

@ -71,6 +71,141 @@ typedef struct Chip8CPU
FILE_SCOPE Chip8CPU cpu; FILE_SCOPE Chip8CPU cpu;
FILE_SCOPE RandPCGState pcgState; FILE_SCOPE RandPCGState pcgState;
FILE_SCOPE void dchip8_init_memory(u8 *memory)
{
DQNT_ASSERT(memory.permanentMemSize == 4096);
const u8 PRESET_FONTS[] =
{
// "0"
0xF0, // @@@@ ----
0x90, // @--@ ----
0x90, // @--@ ----
0x90, // @--@ ----
0xF0, // @@@@----
// "1"
0x20, // --@- ----
0x60, // -@@- ----
0x20, // --@- ----
0x20, // --@- ----
0x70, // -@@@ ----
// "2"
0xF0, // @@@@ ----
0x10, // ---@ ----
0xF0, // @@@@ ----
0x80, // @--- ----
0xF0, // @@@@ ----
// "3"
0xF0, // @@@@ ----
0x10, // ---@ ----
0xF0, // @@@@ ----
0x10, // ---@ ----
0xF0, // @@@@ ----
// "4"
0x90, // @--@ ----
0x90, // @--@ ----
0xF0, // @@@@ ----
0x10, // ---@ ----
0x10, // ---@ ----
// "5"
0xF0, // @@@@ ----
0x80, // @--- ----
0xF0, // @@@@ ----
0x10, // ---@ ----
0xF0, // @@@@ ----
// "6"
0xF0, // @@@@ ----
0x80, // @--- ----
0xF0, // @@@@ ----
0x90, // @--@ ----
0xF0, // @@@@ ----
// "7"
0xF0, // @@@@ ----
0x10, // ---@ ----
0x20, // --@- ----
0x40, // -@-- ----
0x40, // -@-- ----
// "8"
0xF0, // @@@@ ----
0x90, // @--@ ----
0xF0, // @@@@ ----
0x90, // @--@ ----
0xF0, // @@@@ ----
// "9"
0xF0, // @@@@ ----
0x90, // @--@ ----
0xF0, // @@@@ ----
0x10, // ---@ ----
0xF0, // @@@@ ----
// "A"
0xF0, // @@@@ ----
0x90, // @--@ ----
0xF0, // @@@@ ----
0x90, // @--@ ----
0x90, // @--@ ----
// "B"
0xE0, // @@@- ----
0x90, // @--@ ----
0xE0, // @@@- ----
0x90, // @--@ ----
0xE0, // @@@- ----
// "C"
0xF0, // @@@@ ----
0x80, // @--- ----
0x80, // @--- ----
0x80, // @--- ----
0xF0, // @@@@ ----
// "D"
0xE0, // @@@- ----
0x90, // @--@ ----
0x90, // @--@ ----
0x90, // @--@ ----
0xE0, // @@@- ----
// "E"
0xF0, // @@@@ ----
0x80, // @--- ----
0xF0, // @@@@ ----
0x80, // @--- ----
0xF0, // @@@@ ----
// "F"
0xF0, // @@@@ ----
0x80, // @--- ----
0xF0, // @@@@ ----
0x80, // @--- ----
0x80 // @--- ----
};
for (i32 i = 0; i < DQNT_ARRAY_COUNT(PRESET_FONTS); i++)
memory[i] = PRESET_FONTS[i];
}
FILE_SCOPE void dchip8_init_cpu(Chip8CPU *chip8CPU)
{
// NOTE: Everything before 0x200 is reserved for the actual emulator
chip8CPU->programCounter = chip8CPU->INIT_ADDRESS;
chip8CPU->I = 0;
chip8CPU->stackPointer = 0;
const u32 SEED = 0x8293A8DE;
dqnt_rnd_pcg_seed(&pcgState, SEED);
chip8CPU->state = chip8state_load_file;
}
void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input, void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
PlatformMemory memory) PlatformMemory memory)
{ {
@ -85,36 +220,46 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
{ {
// NOTE: Win32 AlphaBlend requires the RGB components to be // NOTE: Win32 AlphaBlend requires the RGB components to be
// premultiplied with alpha. // premultiplied with alpha.
f32 normA = 1.0f; if (i < numPixels * 0.5f)
f32 normR = (normA * 0.0f); {
f32 normG = (normA * 0.0f); f32 normA = 1.0f;
f32 normB = (normA * 1.0f); f32 normR = (normA * 0.0f);
f32 normG = (normA * 0.0f);
f32 normB = (normA * 1.0f);
u8 r = (u8)(normR * 255.0f); u8 r = (u8)(normR * 255.0f);
u8 g = (u8)(normG * 255.0f); u8 g = (u8)(normG * 255.0f);
u8 b = (u8)(normB * 255.0f); u8 b = (u8)(normB * 255.0f);
u8 a = (u8)(normA * 255.0f); u8 a = (u8)(normA * 255.0f);
u32 color = (a << 24) | (r << 16) | (g << 8) | (b << 0); u32 color = (a << 24) | (r << 16) | (g << 8) | (b << 0);
bitmapBuffer[i] = color; bitmapBuffer[i] = color;
}
else
{
f32 normA = 1.0f;
f32 normR = (normA * 1.0f);
f32 normG = (normA * 1.0f);
f32 normB = (normA * 1.0f);
u8 r = (u8)(normR * 255.0f);
u8 g = (u8)(normG * 255.0f);
u8 b = (u8)(normB * 255.0f);
u8 a = (u8)(normA * 255.0f);
u32 color = (a << 24) | (r << 16) | (g << 8) | (b << 0);
bitmapBuffer[i] = color;
}
} }
u8 *mainMem = (u8 *)memory.permanentMem; u8 *mainMem = (u8 *)memory.permanentMem;
if (cpu.state == chip8state_init) if (cpu.state == chip8state_init)
{ {
DQNT_ASSERT(memory.permanentMemSize == (4096 / 4)); dchip8_init_cpu(&cpu);
dchip8_init_memory(mainMem);
// NOTE: Everything before 0x200 is reserved for the actual emulator
cpu.programCounter = cpu.INIT_ADDRESS;
cpu.I = 0;
cpu.stackPointer = 0;
const u32 SEED = 0x8293A8DE;
dqnt_rnd_pcg_seed(&pcgState, SEED);
cpu.state = chip8state_load_file;
} }
#if 0
if (cpu.state == chip8state_load_file) if (cpu.state == chip8state_load_file)
{ {
PlatformFile file = {}; PlatformFile file = {};
@ -136,6 +281,7 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
platform_close_file(&file); platform_close_file(&file);
} }
} }
#endif
if (cpu.state == chip8state_running) if (cpu.state == chip8state_running)
{ {
@ -150,6 +296,10 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
// CLS - 00E0 - Clear the display // CLS - 00E0 - Clear the display
if (opLowByte == 0xE0) if (opLowByte == 0xE0)
{ {
u32 numBytesToClear = renderBuffer.width *
renderBuffer.height *
renderBuffer.bytesPerPixel;
memset(renderBuffer.memory, 0, numBytesToClear);
} }
// RET - 00EE - Return from subroutine // RET - 00EE - Return from subroutine
else if (opLowByte == 0xEE) else if (opLowByte == 0xEE)
@ -382,6 +532,14 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
case 0xD0: case 0xD0:
{ {
// TODO(doyle): Implement drawing // TODO(doyle): Implement drawing
u8 posX = (0x0F & opHighByte);
u8 posY = (0xF0 & opLowByte);
u8 readNumBytesFromMem = (0x0F & opLowByte);
for (i32 i = 0; i < readNumBytesFromMem; i++)
{
u8 *memPtr = &mainMem[cpu.indexRegister + i];
}
} }
break; break;
@ -391,15 +549,13 @@ void dchip8_update(PlatformRenderBuffer renderBuffer, PlatformInput input,
u8 checkKey = (0x0F & opHighByte); u8 checkKey = (0x0F & opHighByte);
// SKP Vx - Ex9E - Skip next instruction if key with the value // SKP Vx - Ex9E - Skip next instruction if key with the value
// of Vx // of Vx is pressed
// is pressed
bool skipNextInstruction = false; bool skipNextInstruction = false;
if (opLowByte == 0x9E) if (opLowByte == 0x9E)
{ {
} }
// SKNP Vx - ExA1 - Skip next instruction if key with the value // SKNP Vx - ExA1 - Skip next instruction if key with the value
// of // of Vx is not pressed
// Vx is not pressed
else else
{ {
DQNT_ASSERT(opLowByte == 0xA1); DQNT_ASSERT(opLowByte == 0xA1);

View File

@ -3,6 +3,8 @@
#include "dqnt.h" #include "dqnt.h"
// NOTE: Platform buffers are expected to be bottom to top! I.e. origin is
// bottom left.
typedef struct PlatformRenderBuffer typedef struct PlatformRenderBuffer
{ {
i32 width; i32 width;

View File

@ -230,7 +230,7 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
u8 stackMemory[4096] = {}; u8 stackMemory[4096] = {};
PlatformMemory platformMemory = {}; PlatformMemory platformMemory = {};
platformMemory.permanentMem = &stackMemory; platformMemory.permanentMem = &stackMemory;
platformMemory.permanentMemSize = (DQNT_ARRAY_COUNT(stackMemory) / 4); platformMemory.permanentMemSize = DQNT_ARRAY_COUNT(stackMemory);
QueryPerformanceFrequency(&globalQueryPerformanceFrequency); QueryPerformanceFrequency(&globalQueryPerformanceFrequency);
const f32 TARGET_FRAMES_PER_S = 60.0f; const f32 TARGET_FRAMES_PER_S = 60.0f;