timing: refactored timing constants to timing.h

This commit is contained in:
Jakub Czekański 2021-01-31 00:06:50 +01:00
parent ceb8607421
commit f8609f2eae
6 changed files with 24 additions and 9 deletions

View file

@ -7,6 +7,7 @@
#include "utils/file.h"
#include "utils/logic.h"
#include "utils/macros.h"
#include "utils/timing.h"
// For vram dump
#include <stb_image_write.h>
@ -793,7 +794,7 @@ bool GPU::emulateGpuCycles(int cycles) {
gpuDot %= 3413;
gpuLine += newLines;
if (gpuLine < LINE_VBLANK_START_NTSC - 1) {
if (gpuLine < timing::LINE_VBLANK_START_NTSC - 1) {
if (gp1_08.verticalResolution == GP1_08::VerticalResolution::r480 && gp1_08.interlace) {
odd = (frames % 2) != 0;
} else {
@ -803,7 +804,7 @@ bool GPU::emulateGpuCycles(int cycles) {
odd = false;
}
if (gpuLine == LINES_TOTAL_NTSC - 1) {
if (gpuLine == timing::LINES_TOTAL_NTSC - 1) {
gpuLine = 0;
frames++;
return true;

View file

@ -17,9 +17,6 @@ namespace gpu {
const int VRAM_WIDTH = 1024;
const int VRAM_HEIGHT = 512;
const int LINE_VBLANK_START_NTSC = 243;
const int LINES_TOTAL_NTSC = 263;
class GPU {
friend struct ::System;
friend class ::Render;

View file

@ -41,7 +41,7 @@ double limitFramerate(bool framelimiter, bool ntsc) {
double currentTime = SDL_GetPerformanceCounter() / counterFrequency;
double deltaTime = currentTime - startTime;
double frameTime = ntsc ? (1.0 / 60.0) : (1.0 / 50.0);
double frameTime = ntsc ? (1.0 / timing::NTSC_FRAMERATE) : (1.0 / 50.0);
if (framelimiter && deltaTime < frameTime) {
// If deltaTime was shorter than frameTime - spin

View file

@ -332,7 +332,7 @@ void System::singleStep() {
state = State::pause;
dma->step();
cdrom->step();
cdrom->step(3);
timer[0]->step(3);
timer[1]->step(3);
timer[2]->step(3);
@ -377,7 +377,7 @@ void System::emulateFrame() {
}
dma->step();
cdrom->step();
cdrom->step(systemCycles / 1.5f);
timer[0]->step(systemCycles);
timer[1]->step(systemCycles);
timer[2]->step(systemCycles);
@ -409,7 +409,7 @@ void System::emulateFrame() {
}
// TODO: Move this code to Timer class
if (gpu->gpuLine > gpu::LINE_VBLANK_START_NTSC) {
if (gpu->gpuLine > timing::LINE_VBLANK_START_NTSC) {
auto& t = *timer[1];
if (t.mode.syncEnabled) {
using modes = device::timer::CounterMode::SyncMode1;

View file

@ -15,6 +15,7 @@
#include "device/spu/spu.h"
#include "device/timer.h"
#include "utils/macros.h"
#include "utils/timing.h"
#include <memory>
#include <vector>

16
src/utils/timing.h Normal file
View file

@ -0,0 +1,16 @@
#pragma once
#include <cstdint>
namespace timing {
constexpr uint64_t US_IN_SECOND = 1'000'000;
constexpr uint64_t CPU_CLOCK = 33'868'800; // 44100 * 768
constexpr uint64_t GPU_CLOCK = 53'222'400;
// NTSC
const float DOTS_TOTAL = 3413.6f;
const int LINE_VBLANK_START_NTSC = 243;
const int LINES_TOTAL_NTSC = 263;
const float NTSC_FRAMERATE = (float)GPU_CLOCK / (DOTS_TOTAL * LINES_TOTAL_NTSC);
constexpr uint64_t usToCpuCycles(uint64_t us) { return us * CPU_CLOCK / US_IN_SECOND; }
} // namespace timing