From ec2617c7ae9a03a95677691e59bf4871399c19f8 Mon Sep 17 00:00:00 2001 From: tudnai Date: Mon, 22 Jun 2020 19:23:41 -0700 Subject: [PATCH] Game mode selectable from UI --- .../xcdebugger/Expressions.xcexplist | 19 +++-- A2Mac/ViewController.swift | 85 ++++++++++++++----- src/cpu/6502.c | 1 + src/cpu/6502.h | 6 ++ src/dev/audio/speaker.c | 31 ++++--- src/dev/audio/speaker.h | 7 +- 6 files changed, 103 insertions(+), 46 deletions(-) diff --git a/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist b/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist index f10d9dd..34c828c 100644 --- a/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist +++ b/A2Mac.xcodeproj/project.xcworkspace/xcuserdata/trudnai.xcuserdatad/xcdebugger/Expressions.xcexplist @@ -569,6 +569,15 @@ + + + + + + @@ -576,19 +585,13 @@ value = "textCols"> + value = "mouseLocation"> - - - - + value = "frameCounter"> diff --git a/A2Mac/ViewController.swift b/A2Mac/ViewController.swift index ea7cba2..de7b047 100644 --- a/A2Mac/ViewController.swift +++ b/A2Mac/ViewController.swift @@ -537,7 +537,6 @@ class ViewController: NSViewController { var lastFrameTime = CACurrentMediaTime() as Double var frameCounter : UInt32 = 0 var clkCounter : Double = 0 - let fpsHalf = fps / 2 var mouseLocation = NSPoint.zero @@ -548,7 +547,7 @@ class ViewController: NSViewController { frameCnt += 1 - if ( frameCnt == fpsHalf ) { + if ( frameCnt == fps / 2 ) { ViewController.charConvTbl = ViewController.charConvTblFlashOn } else if ( frameCnt >= fps ) { @@ -789,9 +788,9 @@ class ViewController: NSViewController { m6502_Run() // video rendering -// if ( frameCounter % 5 == 0 ) { + if ( frameCounter % video_fps_divider == 0 ) { Render() -// } + } #endif @@ -853,10 +852,19 @@ class ViewController: NSViewController { // } - let upd = RepeatingTimer(timeInterval: 1/Double(fps)) + var upd = RepeatingTimer(timeInterval: 1) static var current : ViewController? = nil + func newUpdateTimer( timeInterval : Double ) { + upd.kill() + upd = RepeatingTimer(timeInterval: timeInterval) + upd.eventHandler = { + self.Update() + } + upd.resume() + } + override func viewDidLoad() { super.viewDidLoad() @@ -908,10 +916,13 @@ class ViewController: NSViewController { // self.update() // } - upd.eventHandler = { - self.Update() - } - upd.resume() +// upd.eventHandler = { +// self.Update() +// } +// upd.resume() + + newUpdateTimer( timeInterval: 1 / Double(fps) ) + // #endif } @@ -958,22 +969,50 @@ class ViewController: NSViewController { } - @IBAction func setCPUMode(_ sender: NSButton) { - switch ( sender.title ) { - case "Eco": - cpuMode = cpuMode_eco - break + + func setSimulationMode( mode : String ) { + switch ( mode ) { + case "Eco": + cpuMode = cpuMode_eco - case "Game": - cpuMode = cpuMode_game - cpuState = cpuState_running - break - - default: - cpuMode = cpuMode_normal - cpuState = cpuState_running - break + fps = DEFAULT_FPS + spkr_fps = DEFAULT_FPS + video_fps_divider = DEF_VIDEO_DIV + + break + + case "Game": + cpuMode = cpuMode_game + cpuState = cpuState_running + + fps = GAME_FPS + spkr_fps = GAME_FPS + video_fps_divider = GAME_VIDEO_DIV + + break + + default: + cpuMode = cpuMode_normal + cpuState = cpuState_running + + fps = DEFAULT_FPS + spkr_fps = DEFAULT_FPS + video_fps_divider = DEF_VIDEO_DIV + + break } + + spkr_buf_size = spkr_sample_rate * 2 / spkr_fps + newUpdateTimer( timeInterval: 1 / Double(fps) ) + setCPUClockSpeed(freq: MHz_6502) + + // TODO: Better way to deal with speaker!!! + spkr_play_timeout = 8 * video_fps_divider + } + + + @IBAction func setCPUMode(_ sender: NSButton) { + setSimulationMode(mode: sender.title ) } @IBOutlet weak var SoundGap: NSTextFieldCell! diff --git a/src/cpu/6502.c b/src/cpu/6502.c index 7ac2cd9..f9ea571 100644 --- a/src/cpu/6502.c +++ b/src/cpu/6502.c @@ -44,6 +44,7 @@ volatile cpuState_s cpuState = cpuState_unknown; const unsigned long long int iterations = G; unsigned long long int inst_cnt = 0; +unsigned int video_fps_divider = DEF_VIDEO_DIV; unsigned int fps = DEFAULT_FPS; const double default_MHz_6502 = 1.023; // 2 * M; // 4 * M; // 8 * M; // 16 * M; // 128 * M; // 256 * M; // 512 * M; diff --git a/src/cpu/6502.h b/src/cpu/6502.h index 3ee224b..28b7013 100644 --- a/src/cpu/6502.h +++ b/src/cpu/6502.h @@ -178,6 +178,12 @@ extern double mips; extern double mhz; #define DEFAULT_FPS 30U +#define DEF_VIDEO_DIV 1U + +#define GAME_FPS 600U +#define GAME_VIDEO_DIV 10U + +extern unsigned int video_fps_divider; extern unsigned int fps; extern void rom_loadFile( const char * bundlePath, const char * filename ); diff --git a/src/dev/audio/speaker.c b/src/dev/audio/speaker.c index c14851a..ee3ad50 100644 --- a/src/dev/audio/speaker.c +++ b/src/dev/audio/speaker.c @@ -56,7 +56,7 @@ ALCcontext *ctx = NULL; int spkr_level = SPKR_LEVEL_ZERO; -#define BUFFER_COUNT 64 +#define BUFFER_COUNT 256 #define SOURCES_COUNT 4 enum { @@ -74,17 +74,18 @@ ALuint spkr_disk_arm_buf = 0; ALuint spkr_disk_ioerr_buf = 0; -const int spkr_fps = 30; -const int spkr_seconds = 1; +unsigned spkr_fps = DEFAULT_FPS; +const unsigned spkr_seconds = 1; const unsigned spkr_sample_rate = 44100; const unsigned sfx_sample_rate = 22050; // original sample rate //const unsigned sfx_sample_rate = 26000; // bit higher pitch int spkr_extra_buf = 0; // 800 / spkr_fps; -const unsigned spkr_buf_size = spkr_seconds * spkr_sample_rate * 2 / spkr_fps; -int16_t spkr_samples [ spkr_buf_size * spkr_fps * BUFFER_COUNT * 2]; // stereo +const unsigned spkr_buf_alloc_size = spkr_seconds * spkr_sample_rate * 2 / DEFAULT_FPS; +unsigned spkr_buf_size = spkr_buf_alloc_size; +int16_t spkr_samples [ spkr_buf_alloc_size * DEFAULT_FPS * BUFFER_COUNT * 2]; // stereo unsigned spkr_sample_idx = 0; -const unsigned spkr_play_timeout = 8; // increase to 32 for 240 fps, normally 8 for 30 fps +unsigned spkr_play_timeout = 8; // increase to 32 for 240 fps, normally 8 for 30 fps unsigned spkr_play_time = 0; unsigned spkr_play_disk_motor_time = 0; unsigned spkr_play_disk_arm_time = 0; @@ -259,7 +260,7 @@ char spkr_state = 0; void spkr_toggle() { // TODO: This is very slow! - spkr_play_time = 0; +// spkr_play_time = 0; if ( diskAccelerator_count ) { // turn off disk acceleration immediately @@ -339,6 +340,8 @@ int spkr_unqueue( ALuint src ) { return processed; } +int playDelay = 4; + void spkr_update() { if ( spkr_play_time ) { // free up unused buffers @@ -347,9 +350,6 @@ void spkr_update() { if ( freeBuffers ) { - ALenum state; - alGetSourcei( spkr_src[SPKR_SRC_GAME_SFX], AL_SOURCE_STATE, &state ); - // al_check_error(); if ( --spkr_play_time == 0 ) { float fadeLevel = spkr_level - SPKR_LEVEL_ZERO; @@ -384,9 +384,16 @@ void spkr_update() { al_check_error(); } + ALenum state; + alGetSourcei( spkr_src[SPKR_SRC_GAME_SFX], AL_SOURCE_STATE, &state ); +// al_check_error(); + switch (state) { case AL_PAUSED: - alSourcePlay(spkr_src[SPKR_SRC_GAME_SFX]); + if ( --playDelay <= 0 ) { + alSourcePlay(spkr_src[SPKR_SRC_GAME_SFX]); + playDelay = 4; + } break; case AL_PLAYING: @@ -407,7 +414,7 @@ void spkr_update() { } // start from the beginning - spkr_sample_idx = 0; +// spkr_sample_idx = 0; } diff --git a/src/dev/audio/speaker.h b/src/dev/audio/speaker.h index 7185073..850b1f1 100644 --- a/src/dev/audio/speaker.h +++ b/src/dev/audio/speaker.h @@ -33,15 +33,16 @@ extern const unsigned spkr_sample_rate; -extern const unsigned spkr_buf_size; -extern const int spkr_fps; +extern const unsigned spkr_buf_alloc_size; +extern unsigned spkr_buf_size; +extern unsigned spkr_fps; extern int16_t spkr_samples []; extern unsigned spkr_sample_idx; extern int spkr_level; extern int freeBuffers; extern int spkr_extra_buf; -extern const unsigned spkr_play_timeout; +extern unsigned spkr_play_timeout; extern unsigned spkr_play_time;