From f30753a118d6124bd03fe887c64f378b5177cb82 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Magalh=C3=A3es?= Date: Mon, 2 Sep 2024 02:05:06 +0100 Subject: [PATCH] chore: support for the desynced tick mode --- frontends/web/ts/gb.ts | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/frontends/web/ts/gb.ts b/frontends/web/ts/gb.ts index 36eb6f68..89382abd 100644 --- a/frontends/web/ts/gb.ts +++ b/frontends/web/ts/gb.ts @@ -18,6 +18,8 @@ import { SaveState, SectionInfo, Size, + TickMode, + TickParams, Validation } from "emukit"; import { loadAsync } from "jszip"; @@ -191,8 +193,10 @@ export class GameboyEmulator extends EmulatorLogic implements Emulator { * - Triggers the frame event in case there's a frame to be processed. * - Triggers the audio event, allowing the deferred retrieval of the audio buffer. * - Flushes the RAM to the local storage in case the cartridge is battery backed. + * + * @params params The parameters to be used in the tick operation. */ - async tick() { + async tick(params: TickParams) { // in case the reference to the system is not set then // returns the control flow immediately (not possible to tick) if (!this.gameBoy) return; @@ -201,10 +205,22 @@ export class GameboyEmulator extends EmulatorLogic implements Emulator { // processed in the current tick operation, this value is // calculated using the logic and visual frequencies and // the current Game Boy multiplier (DMG vs CGB) - const tickCycles = Math.round( - (this.logicFrequency * (this.gameBoy?.multiplier() ?? 1)) / - this.visualFrequency - ); + let tickCycles = 0; + switch (params.mode) { + case TickMode.Synced: + tickCycles = Math.round( + (this.logicFrequency * (this.gameBoy?.multiplier() ?? 1)) / + this.visualFrequency + ); + break; + case TickMode.Desynced: + tickCycles = Math.round( + this.logicFrequency * + (this.gameBoy?.multiplier() ?? 1) * + ((params.elapsedTime ?? 1000) / 1000) + ); + break; + } // calculates the target cycles for clocking in the current // tick operation, this is the ideal value and the concrete