Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Run at 60 Hz #10

Open
tobiasvl opened this issue Oct 15, 2021 · 1 comment
Open

Run at 60 Hz #10

tobiasvl opened this issue Oct 15, 2021 · 1 comment

Comments

@tobiasvl
Copy link
Owner

No description provided.

@tobiasvl
Copy link
Owner Author

tobiasvl commented Nov 1, 2021

Others' code:

      let mut begin_cpu = time::PreciseTime::now();
      let mut begin_display = begin_cpu.clone();
      'running: loop {
            let now = time::PreciseTime::now();

            let delta_cpu = begin_cpu.to(now);
            if delta_cpu.num_milliseconds() >= (1000.0 / CPU_FREQUENCY).round() as i64 {
                  begin_cpu = now.clone();

                  if chip.running {
                        chip.cycle();
                  }
            }

            let delta_display = begin_display.to(now);
            if delta_display.num_milliseconds() >= (1000.0 / DRAW_FREQUENCY).round() as i64 {
                  begin_display = now.clone();

                  display.update(&mut chip);
                  if display.should_close() {
                        break 'running;
                  }
                  display.draw(&chip);
            }
      }




            // Delay & sound timers are decreased by 1 at a rate of 60Hz
            let now = time::precise_time_ns();
            if now - self.clock > 16666000 {
                  self.clock = now;
                  if self.delay_timer != 0 {
                        self.delay_timer -= 1;
                  }
                  if self.sound_timer != 0 {
                        self.sound_timer -= 1;
                  }
            }
extern crate time;

const BEEP_SAMPLE_RATE: u32 = 44100;
const BEEP_FREQUENCY: f32 = 1000.0;
fn generate_beep() -> Vec<i16> {
      const SAMPLES: usize = BEEP_SAMPLE_RATE as usize / 2;
      let mut data: Vec<i16> = Vec::new();
      data.resize(SAMPLES, 0);

      // generate half a second of a sine wave
      for i in 0..SAMPLES {
            let t = i as f32 / BEEP_SAMPLE_RATE as f32;
            data[i] = ((BEEP_FREQUENCY * t * 2.0 * 3.1415).sin() * 32767.0).round() as i16;
      }

      data
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant