diff --git a/src/detect.rs b/src/detect.rs index 25fc795..3c7d325 100644 --- a/src/detect.rs +++ b/src/detect.rs @@ -76,6 +76,25 @@ pub struct Envelope { } impl Envelope { + /// Voltage that pairs with a frequency across this envelope. + /// + /// Voltage follows frequency linearly between the profile's floor and ceiling and is clamped + /// outside it. Boards do not run at a fixed voltage across their frequency range, so reporting + /// one voltage while the frequency moves would describe a machine that does not exist. + pub fn voltage_for(self, frequency_mhz: u16) -> u16 { + if frequency_mhz <= self.f_min_mhz { + return self.v_min_mv; + } + if frequency_mhz >= self.f_max_mhz { + return self.v_max_mv; + } + let span_f = u32::from(self.f_max_mhz - self.f_min_mhz); + let span_v = u32::from(self.v_max_mv - self.v_min_mv); + let offset = u32::from(frequency_mhz - self.f_min_mhz); + let voltage = u32::from(self.v_min_mv) + (span_v * offset) / span_f; + voltage.min(u32::from(u16::MAX)) as u16 + } + pub fn intersect(self, other: Envelope) -> Option { let v_min_mv = self.v_min_mv.max(other.v_min_mv); let v_max_mv = self.v_max_mv.min(other.v_max_mv); @@ -712,4 +731,31 @@ mod tests { let plan = run_detection(&mut backend, &db(), &request).expect("plan"); assert_eq!(plan.degraded_chains(126), vec![2]); } + + #[test] + fn voltage_follows_frequency_and_clamps_outside_the_envelope() { + let envelope = Envelope { + v_min_mv: 1100, + v_max_mv: 1500, + f_min_mhz: 300, + f_max_mhz: 700, + }; + assert_eq!(envelope.voltage_for(200), 1100); + assert_eq!(envelope.voltage_for(300), 1100); + assert_eq!(envelope.voltage_for(500), 1300); + assert_eq!(envelope.voltage_for(700), 1500); + assert_eq!(envelope.voltage_for(900), 1500); + } + + #[test] + fn a_single_frequency_envelope_reports_its_floor() { + let envelope = Envelope { + v_min_mv: 1200, + v_max_mv: 1400, + f_min_mhz: 500, + f_max_mhz: 500, + }; + assert_eq!(envelope.voltage_for(500), 1200); + assert_eq!(envelope.voltage_for(600), 1400); + } } diff --git a/src/runtime.rs b/src/runtime.rs index a207548..15acccd 100644 --- a/src/runtime.rs +++ b/src/runtime.rs @@ -67,7 +67,10 @@ impl MinerRuntime { pub fn operating_point(&self) -> OperatingPoint { OperatingPoint { - voltage_mv: self.plan.operating_point.voltage_mv, + voltage_mv: self + .plan + .envelope + .voltage_for(self.last_command.frequency_mhz), frequency_mhz: self.last_command.frequency_mhz, confirmed: self.plan.operating_point.confirmed, } @@ -271,8 +274,14 @@ mod tests { for _ in 0..10 { runtime.tick(&tick(1_500.0)); } - assert!(runtime.operating_point().frequency_mhz > 300); - assert!(!runtime.operating_point().confirmed); - assert_eq!(runtime.operating_point().voltage_mv, 1200); + let point = runtime.operating_point(); + assert!(point.frequency_mhz > 300); + assert!(!point.confirmed); + // Voltage follows the frequency across the envelope instead of staying at the boot value. + assert_eq!( + point.voltage_mv, + runtime.plan().envelope.voltage_for(point.frequency_mhz) + ); + assert!(point.voltage_mv > 1100); } }