Skip to content

Commit

Permalink
remove division from main loop
Browse files Browse the repository at this point in the history
  • Loading branch information
minkcv committed Dec 30, 2018
1 parent 2c32b88 commit eca9030
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ ICON := icon.jpg

APP_AUTHOR := Will Smith
APP_TITLE := Vapor Spec
APP_VERSION := 0.4.1
APP_VERSION := 1.0.0

#---------------------------------------------------------------------------------
# options for code generation
Expand Down
17 changes: 8 additions & 9 deletions source/display.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

#include "constants.h"
#include "gpu.h"
#include <SDL2/SDL.h>
#include <EGL/egl.h> // EGL library
#include <EGL/eglext.h> // EGL extensions
#include <glad/glad.h> // glad library (OpenGL loader)
Expand All @@ -20,8 +19,8 @@ layout(location = 1) in vec2 a_texCoord;
out vec2 v_texCoord;
void main()
{
gl_Position = a_position;
v_texCoord = a_texCoord;
gl_Position = a_position;
v_texCoord = a_texCoord;
}
)text";

Expand All @@ -35,7 +34,7 @@ layout(location = 0) out vec4 outColor;
uniform sampler2D s_texture;
void main()
{
outColor = texture(s_texture, v_texCoord);
outColor = texture(s_texture, v_texCoord);
}
)text";

Expand All @@ -46,12 +45,12 @@ struct Display
int scale;
int pitch;
EGLDisplay s_display;
EGLContext s_context;
EGLSurface s_surface;
EGLContext s_context;
EGLSurface s_surface;
GLuint vertexShader, fragmentShader;
GLuint shaderProgram;
GLint samplerLoc;
GLuint textureId;
GLuint shaderProgram;
GLint samplerLoc;
GLuint textureId;
};

GLuint loadShaderProgram(GLenum type, const char* source);
Expand Down
15 changes: 8 additions & 7 deletions source/vm.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,9 @@ void run(VM* vm)
uint32_t cpuInstructionCount = 0;

// Enforce the instructions per second limit in sync with the display refreshes
// 500,000 instructions per second is almost the same as 8064 instructions per 16 milliseconds
uint32_t instructionsPerSecondFactor = 62;
// 500,000 instructions per second is almost the same as 8064 instructions per 16 milliseconds.
// 500,000 IPS == 8064 instructions / 0.0016 seconds
uint32_t instructionsPerFrame = 8064;

int wait = 0;
Instruction* decoded = malloc(sizeof(Instruction));
Expand All @@ -68,17 +69,17 @@ void run(VM* vm)
vm->pc++;
cpuInstructionCount++;

if (cpuInstructionCount > INSTRUCTIONS_PER_SECOND / instructionsPerSecondFactor)
if (cpuInstructionCount > instructionsPerFrame)
wait = 1;
}
if ((SDL_GetTicks() - cpuStartTime) > 1000 / instructionsPerSecondFactor)
if ((SDL_GetTicks() - cpuStartTime) > 16)
{
// 16 milliseconds have passed
// Reset the instruction count and timer
if (cpuInstructionCount < INSTRUCTIONS_PER_SECOND / instructionsPerSecondFactor)
if (cpuInstructionCount < instructionsPerFrame)
{
//printf("Running below desired instructions per second\n");
//printf("Desired: %d Actual: %d\n", INSTRUCTIONS_PER_SECOND / instructionsPerSecondFactor, cpuInstructionCount);
//printf("Running below desired instructions per frame\n");
//printf("Desired: %d Actual: %d\n", instructionsPerFrame, cpuInstructionCount);
}
cpuInstructionCount = 0;
cpuStartTime = SDL_GetTicks();
Expand Down

0 comments on commit eca9030

Please sign in to comment.