Exemple #1
0
int FlippedRight(CPU_t *cpu, int on) {
	link_t* link = cpu->pio.link;
	AUDIO_t* audio = &link->audio;
	if (!audio->enabled) return 1;
	if (on == 1) {
		audio->LastFlipRight = tc_elapsed(cpu->timer_c);
	} else if (on == 0) {
		audio->HighLengRight += (tc_elapsed(cpu->timer_c) - audio->LastFlipRight);
	}
	audio->RightOn = on;
	return 0;
}
void SaveTIMER(SAVESTATE_t *save, timerc *time) {
	if (!time) return;
	CHUNK_t* chunk = NewChunk(save,TIMER_tag);
	WriteLong(chunk, time->tstates);
	WriteLong(chunk, time->freq);
	WriteDouble(chunk, tc_elapsed(time));
	WriteDouble(chunk, time->lasttime);
}
Exemple #3
0
static void port3(CPU_t *cpu, device_t *dev) {
	STDINT_t * stdint = (STDINT_t *) dev->aux;
	
	if (cpu->input) {
		unsigned char result = 0;
		if ((tc_elapsed(cpu->timer_c) - stdint->lastchk1) > stdint->timermax1) result += 4;
		if (cpu->pio.lcd->active) result += 2;
		if (stdint->on_latch) result += 1;
		else result += 8;
		
		cpu->bus = result;
		cpu->input = FALSE;
	} else if (cpu->output) {
		if (cpu->bus & 0x08) {
			cpu->pio.lcd->active = TRUE;		//I'm worried about this
		} else {
			cpu->pio.lcd->active = FALSE;
		}
		
		if ((cpu->bus & 0x01) == 0)
			stdint->on_latch = FALSE;
		
		stdint->intactive = cpu->bus;
		cpu->output = FALSE;
	}
	
	if (!(stdint->intactive & 0x04) && cpu->pio.lcd->active == TRUE) {
		if ((tc_elapsed(cpu->timer_c) - stdint->lastchk1) > stdint->timermax1) {
			cpu->interrupt = TRUE;
			while ((tc_elapsed(cpu->timer_c) - stdint->lastchk1) > stdint->timermax1)
				stdint->lastchk1 += stdint->timermax1;
		}
	}

	if ((stdint->intactive & 0x01) && (cpu->pio.keypad->on_pressed & KEY_VALUE_MASK) && (stdint->on_backup & KEY_VALUE_MASK) == 0)  {
		stdint->on_latch = TRUE;
	}
	stdint->on_backup = cpu->pio.keypad->on_pressed;
	if (stdint->on_latch)
		cpu->interrupt = TRUE;
}
Exemple #4
0
int playsound(AUDIO_t *audio) {
	if (audio->init == 0) {
		soundinit(audio);
	} else {
		int i,b;
		audio->PlayTime = tc_elapsed(audio->timer_c) - (BankTime * ((float)BufferBanks));
		audio->PlayPnt = (audio->CurPnt-(PreferedSamples * BufferBanks)) % BufferSamples;
		for(b = 0; b < BufferBanks; b++) {
			for(i = 0; i < PreferedSamples; i++) {
				audio->playbuf[b][i].left = 0x80;
				audio->playbuf[b][i].right = 0x80;
			}
		}
		waveOutRestart(audio->hWaveOut);
		audio->enabled = 1;
	}
	return 0;
}
Exemple #5
0
static void port4(CPU_t *cpu, device_t *dev) {
	if (cpu->input) {
		cpu->bus = 1;
		cpu->input = FALSE;
	} else if (cpu->output) {
		dev->aux = (void *) cpu->bus;
		int freq = (cpu->bus >> 1) & 0x3;
		cpu->pio.stdint->timermax1 = cpu->pio.stdint->freq[freq];
		cpu->pio.stdint->lastchk1 = tc_elapsed(cpu->timer_c);
		int lcd_mode = (cpu->bus >> 3) & 0x3;
		if (lcd_mode == 0) {
			cpu->pio.lcd->width = 80;
		} else {
			cpu->pio.lcd->width = 32 * lcd_mode + 64;
		}

		
		cpu->output = FALSE;
	}
Exemple #6
0
int soundinit(AUDIO_t *audio) {
	int i,b;
	
	puts("Sound intial");
	
	audio->init		= 0;
	audio->enabled	= 0;


	for(i = 0; i < BufferSamples; i++) {
		audio->buffer[i].left = 0x80;
		audio->buffer[i].right = 0x80;
	}


	for(b = 0; b < BufferBanks; b++) {
		for(i = 0;i<PreferedSamples; i++) {
			audio->playbuf[b][i].left = 0x80;
			audio->playbuf[b][i].right = 0x80;
		}
	}


	audio->PlayPnt			= 0;
	audio->CurPnt			= BufferBanks*PreferedSamples;

	audio->PlayTime			= tc_elapsed(audio->timer_c)-(((float) BufferBanks) * ((float) PreferedSamples)) / ((float) SampleRate);
	audio->LastFlipLeft		= tc_elapsed(audio->timer_c);
	audio->HighLengLeft		= 0;
	audio->LastFlipRight	= tc_elapsed(audio->timer_c);
	audio->HighLengRight	= 0;
	audio->LastSample		= tc_elapsed(audio->timer_c);
	audio->LeftOn			= 0;
	audio->RightOn			= 0;
	
	audio->volume			= 0.33f;


	audio->wfx.nSamplesPerSec	= SampleRate;
	audio->wfx.wBitsPerSample	= SampleSizeBits;
	audio->wfx.nChannels		= Channels;
	audio->wfx.cbSize			= 0;
	audio->wfx.wFormatTag		= WAVE_FORMAT_PCM;
	audio->wfx.nBlockAlign		= (audio->wfx.wBitsPerSample >> 3) * audio->wfx.nChannels;
	audio->wfx.nAvgBytesPerSec	= audio->wfx.nBlockAlign * audio->wfx.nSamplesPerSec;



	if( waveOutOpen(	&audio->hWaveOut, 
						WAVE_MAPPER, 
						&audio->wfx, 
						(DWORD_PTR)FillSoundBuffer, 
						(DWORD) audio, 
						CALLBACK_FUNCTION
					) != MMSYSERR_NOERROR ) {

		audio->enabled		= 0;
	    MessageBox(NULL, _T("Unable to open audio device."), _T("Error"), MB_OK);
	    return 1;
	}
	
	audio->init =1;
	audio->enabled =1;

	for(i = 0; i < BufferBanks; i++) {	
		audio->waveheader[i].lpData				= (char *) audio->playbuf[i];
		audio->waveheader[i].dwBufferLength		= BankSize;
		audio->waveheader[i].dwFlags			= 0;
		waveOutPrepareHeader(audio->hWaveOut, &audio->waveheader[i], sizeof(WAVEHDR));
		waveOutWrite(audio->hWaveOut, &audio->waveheader[i], sizeof(WAVEHDR));
	}


	return 0;
}
Exemple #7
0
int nextsample(CPU_t *cpu) {
	link_t* link = cpu->pio.link;
	AUDIO_t* audio = &link->audio;
	double tmp;
	double max		=	255.0f * audio->volume;
	double lower	=	(255.0f - max) / 2.0f;
	
	unsigned char left;
	unsigned char right;
	if (!audio->enabled) return 1;
	
	if (tc_elapsed(cpu->timer_c) < (audio->LastSample + SampleLength)) return 0;
	
	if (audio->RightOn == 1) {
		if ((audio->LastSample+SampleLength) > audio->LastFlipRight) {
			audio->HighLengRight += ((audio->LastSample + SampleLength) - audio->LastFlipRight);
			audio->LastFlipRight = audio->LastSample+SampleLength;
		} 			
	}
	
	if (audio->LeftOn == 1) {
		if ((audio->LastSample + SampleLength) > audio->LastFlipLeft) {
			audio->HighLengLeft += ((audio->LastSample+SampleLength) - audio->LastFlipLeft);
			audio->LastFlipLeft = audio->LastSample+SampleLength;
		}
	}

	if (audio->HighLengLeft < 0) {
		puts("Left less than 0");
		audio->HighLengLeft = 0;
	}
	if (audio->HighLengLeft > SampleLength) {
//		printf("Left %Lf > %Lf \n",(double)audio->HighLengLeft,(double)SampleLength);
		audio->HighLengLeft = SampleLength;
	}

	if (audio->HighLengRight < 0) {
		puts("right less than 0");
		audio->HighLengRight = 0;
	}
	if (audio->HighLengRight > SampleLength) {
		audio->HighLengRight =SampleLength;
//		puts("right greater than Sample length");
	}

	tmp = (audio->HighLengLeft*max*SampleRate)+lower;
	if (tmp < 0) {
		puts("Left less than 0");
		tmp=0;
	}
	if (tmp > 255) {
		puts("Left greater than 255");
		tmp=255;
	}
	left = (unsigned char) tmp;
	
	tmp = (audio->HighLengRight*max*SampleRate)+lower;
	if (tmp < 0) {
		puts("Right less than 0");
		tmp = 0;
	}
	if (tmp > 255) {
		puts("Right greater than 255");
		tmp=255;
	}
	right = (unsigned char) tmp;

	audio->buffer[audio->CurPnt].left		=left;
	audio->buffer[audio->CurPnt].right		=right;

	audio->CurPnt		=  (audio->CurPnt+1)%BufferSamples;

	audio->HighLengRight	=	0;
	audio->HighLengLeft		=	0;
	audio->LastSample		+=	SampleLength;

	if ( (audio->LastSample+(SampleLength*2.0f)) < tc_elapsed(cpu->timer_c)) {
		puts("Last sample out of sync");
		audio->LastSample = tc_elapsed(cpu->timer_c);
	}

	return 0;
}
Exemple #8
0
static void CALLBACK FillSoundBuffer(HWAVEOUT hWaveOut,
									 UINT uMsg,
									 DWORD dwInstance,
									 DWORD dwParam1,
									 DWORD dwParam2 ) {

	WAVEHDR* waveheader = (WAVEHDR*) dwParam1;
	//LPCALC lpCalc = (LPCALC) dwInstance;
	AUDIO_t *audio = (AUDIO_t *) dwInstance;
	int i;

	switch(uMsg) {
		case WOM_DONE:
		{

			waveOutUnprepareHeader(audio->hWaveOut, waveheader, sizeof(WAVEHDR));

			if ((!audio->enabled) /*|| (lpCalc->running)*/ ) {
				if (audio->init) {
					memset(waveheader->lpData,0x80,BankSize);
					waveOutPrepareHeader(audio->hWaveOut,waveheader,sizeof(WAVEHDR));
					waveOutWrite(audio->hWaveOut,waveheader,sizeof(WAVEHDR));
				} else audio->endsnd++;
			} else {

				if ((audio->PlayTime + (BankTime * 1.5f)) < (tc_elapsed(audio->timer_c))) {

					if ((audio->PlayTime+(BankTime * ((float) (BufferBanks * 2)))) < tc_elapsed(audio->timer_c)) {

						audio->PlayTime = tc_elapsed(audio->timer_c) - (BankTime * ((float) BufferBanks));
						audio->PlayPnt = (audio->CurPnt - (PreferedSamples * BufferBanks)) % BufferSamples;
					}
					unsigned char* dataout	= (unsigned char *) &audio->buffer[audio->PlayPnt];
					unsigned char* datain	= (unsigned char *) waveheader->lpData;
					unsigned char* dataend	= (unsigned char *) &audio->buffer[BufferSamples];
					for(i = 0; i < BankSize; i++) {
						if (dataout >= dataend) {
							dataout = (unsigned char *) &audio->buffer[0];
						}
						datain[i] = dataout[0];
						dataout++;
					}
					waveheader->dwFlags = 0;
					waveOutPrepareHeader(audio->hWaveOut,waveheader,sizeof(WAVEHDR));
					waveOutWrite(audio->hWaveOut,waveheader,sizeof(WAVEHDR));
					audio->PlayPnt = (audio->PlayPnt + PreferedSamples) % BufferSamples;
					audio->PlayTime += BankTime;
					if (gif_write_state == GIF_FRAME) {
						//WriteAVIAudioFrame(datain, BankSize);
					}
				} else {

					memset(waveheader->lpData, 0x80, BankSize);
					waveOutPrepareHeader(audio->hWaveOut, waveheader, sizeof(WAVEHDR));
					waveOutWrite(audio->hWaveOut, waveheader, sizeof(WAVEHDR));
				}
		
		    }
		    break;
		}
		case WOM_OPEN:
		{
			puts("WOM_OPEN");
			break;
		}
		case WOM_CLOSE:
		{
			puts("WOM_CLOSE");
			audio->endsnd = 100;
			break;
		}
		default:
		{
			puts("sound callback msg unknown");
			break;
		}
	}
	return;
}
Exemple #9
0
int calc_run_all(void) {
	int i, j, active_calc = -1;
	BOOL calc_waiting = FALSE;

	for (i = 0; i < FRAME_SUBDIVISIONS; i++) {
		link_hub[MAX_CALCS]->host = 0;
		for (j = 0; j < MAX_CALCS; j++) {
			char hostVal = 0;
			for (int k = 0; k < MAX_CALCS; k++) {
				if (link_hub[k] != NULL && link_hub[k]->host) {
					hostVal |= link_hub[k]->host;
					calc_waiting |= link_hub[k]->hasChanged;
				}
			}
			if (hostVal != link_hub[MAX_CALCS]->host) {
				link_hub[MAX_CALCS]->host = hostVal;
				calc_waiting = TRUE;
				for (int k = 0; k < MAX_CALCS; k++) {
					if (link_hub[k]) {
						link_hub[k]->hasChanged = TRUE;
						link_hub[k]->changedTime = calcs[k].timer_c.tstates;
					}
				}
			}
			if (calcs[j].active) {
				/*if (link_hub[j] != NULL && (!link_hub[MAX_CALCS]->host || link_hub[MAX_CALCS]->host != link_hub[j]->host)
					&& calcs[j].cpu.is_link_instruction || ((int) (calcs[j].cpu.linking_time - calcs[j].cpu.timer_c->tstates) >= 100000)) {
					calcs[j].cpu.is_link_instruction = FALSE;
					calcs[j].cpu.linking_time = 0;
					CPU_step(&calcs[j].cpu);
				}*/
				if (calcs[j].cpu.is_link_instruction && calcs[j].cpu.pio.link->changedTime - calcs[j].timer_c.tstates >= 100000) {
					calcs[j].cpu.is_link_instruction = FALSE;
					calcs[j].cpu.pio.link->changedTime = 0;
					calcs[j].cpu.pio.link->hasChanged = FALSE;
					CPU_step(&calcs[j].cpu);
				}
				active_calc = j;
				int time = (int)((int64_t) calcs[j].speed * calcs[j].timer_c.freq / FPS / 100) / FRAME_SUBDIVISIONS;
				if (!calcs[j].cpu.is_link_instruction || !calc_waiting || calcs[j].cpu.pio.link->hasChanged == TRUE) {
					calc_run_tstates(&calcs[j], time);
				} /*else {
					calcs[j].cpu.linking_time += time;
				}*/
			}
		}

		if (link_hub_count > 1 && calc_waiting_link >= link_hub_count) {
			for (int k = 0; k < MAX_CALCS; k++) {
				if (calcs[k].cpu.is_link_instruction) {
					calcs[k].cpu.is_link_instruction = FALSE;
					CPU_step(&calcs[k].cpu);
				}
			}
			calc_waiting_link = 0;
		}

		//this code handles screenshoting if were actually taking screenshots right now
		if (active_calc >= 0 && !calc_waiting_link && calcs[active_calc].cpu.timer_c != NULL && calcs[active_calc].cpu.pio.lcd != NULL &&
				((tc_elapsed(calcs[active_calc].cpu.timer_c) - calcs[active_calc].cpu.pio.lcd->lastgifframe) >= 0.01)) {
			handle_screenshot();
			calcs[active_calc].cpu.pio.lcd->lastgifframe += 0.01;
		}
	}

	return 0;
}