Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <string.h>
- #include "SDL.h"
- #include "sms.h"
- #include "sound.h"
- #include "sn76489.h"
- #include "freq.h"
- #include "gaw.sn76489.h"
- static const int NTSC_LINES_PER_FRAME = 262;
- static const int PSG_DIVISOR = 16;
- static const int NTSC_FPS = 60;
- static const float NTSC_Z80_CLOCK = 3579545.0f;
- static const float NTSC_CLOCKS_PER_SAMPLE = 5.073051303855f;
- int sms_psg_enabled = 1, sms_ym2413_enabled = 0;
- // 60 fps
- int delay = 30;
- sn76489_t psg;
- uint32 psg_samples[313];
- Uint32 emu_delay() {
- static Uint32 nextTime = 0;
- Uint32 now = SDL_GetTicks();
- if(nextTime <= now) {
- nextTime = now + delay;
- return 0;
- }
- return(nextTime - now);
- }
- int main(int argc, char *argv[]) {
- int i;
- float tmp = NTSC_Z80_CLOCK / PSG_DIVISOR / NTSC_FPS / NTSC_LINES_PER_FRAME /
- NTSC_CLOCKS_PER_SAMPLE;
- SDL_Event event;
- if(SDL_Init(SDL_INIT_VIDEO) < 0) {
- return 0;
- }
- atexit(SDL_Quit);
- SDL_Window *window = SDL_CreateWindow("sms-synth", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 320, 200, 0);
- if(window == 0) {
- return 0;
- }
- /* from sms_init */
- for(i = 0; i < NTSC_LINES_PER_FRAME; ++i) {
- psg_samples[i] = (uint32) (tmp * (i + 1)) - (uint32) (tmp * i);
- }
- /* We end up generating 734 samples per frame @ 44100 Hz, 60fps, but we
- need 735. */
- psg_samples[261] += 1;
- sn76489_init(&psg, NTSC_Z80_CLOCK, 44100.0f, SN76489_NOISE_BITS_SMS, SN76489_NOISE_TAPPED_SMS);
- sound_init(2, SMS_VIDEO_NTSC);
- /* end sms_init */
- int done = 0, gaw_sample = 0, gaw_size = sizeof(gaw);
- while(!done) {
- int16 buf[882 << 1];
- int line, samples = 0;
- /* Write one sample per loop */
- if(0x80&gaw[gaw_sample]) {
- sn76489_write(&psg, gaw[gaw_sample++]);
- sn76489_write(&psg, gaw[gaw_sample++]);
- } else {
- sn76489_write(&psg, gaw[gaw_sample++]);
- }
- /* Only play ¼ of the sample for now */
- if(gaw_sample > gaw_size/4) {
- done = 1;
- }
- /* From sms_frame */
- for(line = 0; line < NTSC_LINES_PER_FRAME; line++) {
- if(sms_psg_enabled) {
- sn76489_execute_samples(&psg, buf + samples, psg_samples[line]);
- }
- while(SDL_PollEvent(&event)) {
- switch(event.type) {
- case SDL_KEYDOWN: {
- SDL_Keycode k = event.key.keysym.sym;
- if(k == SDLK_ESCAPE) {
- done = 1;
- }
- }
- break;
- case SDL_QUIT:
- done = 1;
- break;
- }
- }
- samples += psg_samples[line] << 1;
- }
- if(sms_psg_enabled) {
- sound_update_buffer(buf, samples << 1);
- sound_wait();
- }
- SDL_Delay(emu_delay());
- }
- sound_pause();
- sound_shutdown();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement