Game mode selectable from UI

This commit is contained in:
tudnai 2020-06-22 19:23:41 -07:00
parent 15b4b3252e
commit ec2617c7ae
6 changed files with 103 additions and 46 deletions

View File

@ -569,6 +569,15 @@
<ContextState
contextName = "ViewController.Update():ViewController.swift">
<PersistentStrings>
<PersistentString
value = "clkfrm">
</PersistentString>
<PersistentString
value = "textLines * (textCols+1) + textCols">
</PersistentString>
<PersistentString
value = "txtArr">
</PersistentString>
<PersistentString
value = "y * (textCols + lineEndChars) + x ">
</PersistentString>
@ -576,19 +585,13 @@
value = "textCols">
</PersistentString>
<PersistentString
value = "textLines * (textCols+1) + textCols">
value = "mouseLocation">
</PersistentString>
<PersistentString
value = "textLines">
</PersistentString>
<PersistentString
value = "mouseLocation">
</PersistentString>
<PersistentString
value = "clkfrm">
</PersistentString>
<PersistentString
value = "txtArr">
value = "frameCounter">
</PersistentString>
</PersistentStrings>
</ContextState>

View File

@ -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!

View File

@ -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;

View File

@ -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 );

View File

@ -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;
}

View File

@ -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;