Minor fixes to clock code

This commit is contained in:
transistor 2023-04-23 16:17:07 -07:00
parent 07a675fab5
commit cd5336bc23
7 changed files with 40 additions and 36 deletions

View File

@ -105,13 +105,19 @@ impl ClockDuration {
}
#[inline]
pub fn checked_add(self, rhs: Self) -> Option<Self> {
self.femtos.checked_add(rhs.femtos).map(Self::from_femtos)
pub const fn checked_add(self, rhs: Self) -> Option<Self> {
match self.femtos.checked_add(rhs.femtos) {
Some(femtos) => Some(Self::from_femtos(femtos)),
None => None,
}
}
#[inline]
pub fn checked_sub(self, rhs: Self) -> Option<Self> {
self.femtos.checked_sub(rhs.femtos).map(Self::from_femtos)
pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
match self.femtos.checked_sub(rhs.femtos) {
Some(femtos) => Some(Self::from_femtos(femtos)),
None => None,
}
}
}
@ -119,13 +125,13 @@ impl Add for ClockDuration {
type Output = Self;
fn add(self, rhs: Self) -> Self::Output {
self.checked_add(rhs).expect("clock duration overflow")
self.checked_add(rhs).expect("clock duration overflow during addition")
}
}
impl AddAssign for ClockDuration {
fn add_assign(&mut self, rhs: Self) {
*self = self.checked_add(rhs).expect("clock duration overflow");
*self = *self + rhs;
}
}
@ -133,13 +139,13 @@ impl Sub for ClockDuration {
type Output = Self;
fn sub(self, rhs: Self) -> Self::Output {
self.checked_sub(rhs).expect("clock duration overflow")
self.checked_sub(rhs).expect("clock duration overflow during subtraction")
}
}
impl SubAssign for ClockDuration {
fn sub_assign(&mut self, rhs: Self) {
*self = self.checked_sub(rhs).expect("clock duration overflow");
*self = *self - rhs;
}
}
@ -191,13 +197,23 @@ pub struct ClockTime(ClockDuration);
impl ClockTime {
pub const START: Self = Self(ClockDuration::ZERO);
#[inline]
pub const fn as_duration(self) -> ClockDuration {
self.0
}
#[inline]
pub fn duration_since(self, other: Self) -> ClockDuration {
self.0 - other.0
}
#[inline]
pub const fn checked_add(self, duration: ClockDuration) -> Option<Self> {
match self.0.checked_add(duration) {
Some(duration) => Some(Self(duration)),
None => None,
}
}
}
impl Add<ClockDuration> for ClockTime {

View File

@ -1,6 +1,5 @@
use crate::error::Error;
use crate::devices::TransmutableBox;
pub struct InterruptController {

View File

@ -95,7 +95,7 @@ impl System {
self.clock = event_device.next_clock;
let result = match event_device.device.borrow_mut().as_steppable().unwrap().step(self) {
Ok(diff) => {
event_device.next_clock = self.clock + diff;
event_device.next_clock = self.clock.checked_add(diff).unwrap();
Ok(())
},
Err(err) => Err(err),

View File

@ -264,16 +264,16 @@ impl MiniFrontend {
while window.is_open() && !window.is_key_down(Key::Escape) {
let frame_time = update_timer.elapsed();
update_timer = Instant::now();
println!("new frame after {:?}us", frame_time.as_micros());
//println!("new frame after {:?}us", frame_time.as_micros());
let run_timer = Instant::now();
//let run_timer = Instant::now();
if let Some(system) = system.as_mut() {
//system.run_for(nanoseconds_per_frame).unwrap();
system.run_for(ClockDuration::from_nanos((frame_time.as_nanos() as f32 * speed) as u64)).unwrap();
//system.run_until_break().unwrap();
}
let sim_time = run_timer.elapsed().as_micros();
println!("ran simulation for {:?}us in {:?}us (avg: {:?}us)", frame_time.as_micros(), sim_time, frame_time.as_micros() as f64 / sim_time as f64);
//let sim_time = run_timer.elapsed().as_micros();
//println!("ran simulation for {:?}us in {:?}us (avg: {:?}us)", frame_time.as_micros(), sim_time, frame_time.as_micros() as f64 / sim_time as f64);
if let Some(keys) = window.get_keys_pressed(minifb::KeyRepeat::No) {
for key in keys {

View File

@ -133,6 +133,7 @@ enum EnvelopeState {
#[derive(Clone)]
struct EnvelopeGenerator {
#[allow(dead_code)]
debug_name: String,
total_level: u16,
sustain_level: u16,
@ -245,6 +246,7 @@ enum OperatorAlgorithm {
#[derive(Clone)]
struct Operator {
#[allow(dead_code)]
debug_name: String,
wave: SineWave,
frequency: f32,
@ -267,10 +269,6 @@ impl Operator {
self.frequency = frequency;
}
fn reset(&mut self) {
self.wave.reset();
}
fn set_multiplier(&mut self, _frequency: f32, multiplier: f32) {
self.multiplier = multiplier;
}
@ -305,6 +303,7 @@ impl Operator {
#[derive(Clone)]
struct Channel {
#[allow(dead_code)]
debug_name: String,
operators: Vec<Operator>,
on_state: u8,
@ -332,12 +331,6 @@ impl Channel {
}
}
fn reset(&mut self) {
for operator in self.operators.iter_mut() {
operator.reset();
}
}
fn get_sample(&mut self, envelope_clock: EnvelopeClock) -> f32 {
if self.on_state != self.next_on_state {
self.on_state = self.next_on_state;
@ -468,7 +461,7 @@ impl Ym2612 {
selected_reg_1: None,
clock_frequency,
envelope_clock_period: clock_frequency.period_duration() * 351, //3 * 144 * 1_000_000_000 / clock_frequency as ClockDuration,
envelope_clock_period: clock_frequency.period_duration() * 351,
channels: (0..CHANNELS).map(|i| Channel::new(format!("ch {}", i), sample_rate)).collect(),
channel_frequencies: [(0, 0); CHANNELS],
@ -590,8 +583,7 @@ impl Ym2612 {
|| is_reg_range(reg, 0x80)
|| is_reg_range(reg, 0x90)
=> {
let (ch, op) = get_ch_op(bank, reg);
self.update_rates(ch, op);
self.update_rates(bank, reg & 0x0F);
},
reg if (0xA0..=0xA2).contains(&reg) => {
@ -630,8 +622,9 @@ impl Ym2612 {
}
}
fn update_rates(&mut self, ch: usize, op: usize) {
let index = get_index(ch, op);
fn update_rates(&mut self, bank: u8, reg: u8) {
let index = bank as usize * 256 + reg as usize;
let (ch, op) = get_ch_op(bank, reg);
let keycode = self.registers[0xA0 + get_ch_index(ch)] >> 1;
let rate_scaling = self.registers[0x50 + index] & 0xC0 >> 6;
let attack_rate = self.registers[0x50 + index] & 0x1F;
@ -683,12 +676,6 @@ fn get_ch_op(bank: u8, reg: u8) -> (usize, usize) {
(ch, op)
}
#[inline]
fn get_index(ch: usize, op: usize) -> usize {
let (bank, ch_l) = if ch < 3 { (0, ch) } else { (1, ch - 3) };
(bank << 8) | op << 2 | ch
}
#[inline]
fn get_ch(bank: u8, reg: u8) -> usize {
((reg as usize) & 0x07) + ((bank as usize) * 3)

View File

@ -1,6 +1,6 @@
use moa_core::{warn, info};
use moa_core::{System, Error, ClockTime, ClockDuration, Address, Addressable, Steppable, Transmutable};
use moa_core::{System, Error, ClockDuration, Address, Addressable, Steppable, Transmutable};
use moa_core::host::{Host, ControllerUpdater, HostData, ControllerDevice, ControllerEvent};

View File

@ -276,6 +276,7 @@ enum ColourMode {
Highlight,
}
/*
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
enum Scroll {
ScrollA,
@ -290,6 +291,7 @@ enum Priority {
ScrollB,
Background,
}
*/
struct Ym7101State {
status: u16,