use crate::{
ast::{
Attributes, CodeFormatter, ComplexAttri, ComplexParseError, GroupComments, GroupFn,
Indentation, ParseScope, SimpleAttri,
},
common::table::{
TableLookUp, TableLookUp2D, TableLookUpMultiSegment, Vector3DGrpup, Vector4DGrpup,
},
expression::IdBooleanExpression,
timing::items::Mode,
ArcStr, GroupSet,
};
use core::fmt::{self, Write};
use num_traits::Zero;
#[derive(Debug, Default, Clone)]
#[derive(liberty_macros::Group)]
#[mut_set::derive::item(
sort,
macro(derive(Debug, Clone, Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct CCSNStage {
#[liberty(name)]
#[id(borrow = "&[ArcStr]")]
pub name: Vec<ArcStr>,
#[liberty(comments)]
pub comments: GroupComments<Self>,
#[liberty(attributes)]
pub attributes: Attributes,
#[liberty(simple(type = Option))]
pub load_cap_fall: Option<f64>,
#[liberty(simple(type = Option))]
pub load_cap_rise: Option<f64>,
#[liberty(simple)]
pub is_inverting: bool,
#[liberty(simple)]
pub is_needed: bool,
#[liberty(simple(type = Option))]
pub is_pass_gate: Option<bool>,
#[liberty(simple)]
pub miller_cap_fall: f64,
#[liberty(simple)]
pub miller_cap_rise: f64,
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!")]
#[liberty(simple(type = Option))]
pub related_ccb_node: Option<ArcStr>,
#[liberty(simple)]
pub stage_type: StageType,
#[id(borrow = "Option<&IdBooleanExpression>", check_fn = "mut_set::borrow_option!")]
#[liberty(simple(type = Option))]
pub when: Option<IdBooleanExpression>,
#[liberty(complex(type = Option))]
pub mode: Option<Mode>,
#[liberty(group(type = Option))]
pub dc_current: Option<TableLookUp2D>,
#[liberty(group(type = Option))]
pub output_voltage_fall: Option<Vector3DGrpup>,
#[liberty(group(type = Option))]
pub output_voltage_rise: Option<Vector3DGrpup>,
#[liberty(group(type = Option))]
pub propagated_noise_low: Option<Vector4DGrpup>,
#[liberty(group(type = Option))]
pub propagated_noise_high: Option<Vector4DGrpup>,
}
impl GroupFn for CCSNStage {
#[inline]
fn post_parse_process(&mut self, _scope: &mut ParseScope) {
if self.miller_cap_fall.is_sign_negative() {
self.miller_cap_fall.set_zero();
log::warn!("miller_cap_fall is negative!");
}
if self.miller_cap_rise.is_sign_negative() {
self.miller_cap_rise.set_zero();
log::warn!("miller_cap_rise is negative!");
}
}
}
#[derive(Debug, Clone, Copy)]
#[derive(Hash, PartialEq, Eq)]
#[derive(Ord, PartialOrd, Default)]
#[derive(strum_macros::EnumString, strum_macros::EnumIter, strum_macros::Display)]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum StageType {
#[strum(serialize = "pull_up")]
PullUp,
#[strum(serialize = "pull_down")]
PullDown,
#[strum(serialize = "both")]
#[default]
Both,
}
impl SimpleAttri for StageType {
#[inline]
fn nom_parse<'a>(
i: &'a str,
scope: &mut ParseScope,
) -> crate::ast::SimpleParseRes<'a, Self> {
crate::ast::nom_parse_from_str(i, scope)
}
}
#[derive(Debug, Default, Clone)]
#[derive(liberty_macros::Group)]
#[mut_set::derive::item(
sort,
macro(derive(Debug, Clone,Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct ReceiverCapacitance {
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!")]
#[liberty(name)]
pub name: Option<ArcStr>,
#[liberty(comments)]
pub comments: GroupComments<Self>,
#[liberty(attributes)]
pub attributes: Attributes,
#[id(borrow = "Option<&IdBooleanExpression>", check_fn = "mut_set::borrow_option!")]
#[liberty(simple(type=Option))]
pub when: Option<IdBooleanExpression>,
#[liberty(group(type=Set))]
pub receiver_capacitance_fall: GroupSet<TableLookUpMultiSegment>,
#[liberty(group(type=Set))]
pub receiver_capacitance_rise: GroupSet<TableLookUpMultiSegment>,
#[liberty(group)]
pub receiver_capacitance1_fall: Option<TableLookUp>,
#[liberty(group)]
pub receiver_capacitance1_rise: Option<TableLookUp>,
#[liberty(group)]
pub receiver_capacitance2_fall: Option<TableLookUp>,
#[liberty(group)]
pub receiver_capacitance2_rise: Option<TableLookUp>,
}
impl GroupFn for ReceiverCapacitance {}
#[derive(Debug, Clone, Default)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct PropagatingCcb {
pub input_ccb_name: ArcStr,
pub output_ccb_name: Option<ArcStr>,
}
impl ComplexAttri for PropagatingCcb {
#[inline]
fn parse<'a, I: Iterator<Item = &'a Vec<&'a str>>>(
iter: I,
_scope: &mut ParseScope,
) -> Result<Self, ComplexParseError> {
let mut i = iter.flat_map(IntoIterator::into_iter);
let input_ccb_name = match i.next() {
Some(&s) => ArcStr::from(s),
None => return Err(ComplexParseError::LengthDismatch),
};
let output_ccb_name = i.next().map(|&s| ArcStr::from(s));
if i.next().is_some() {
return Err(ComplexParseError::LengthDismatch);
}
Ok(Self { input_ccb_name, output_ccb_name })
}
#[expect(clippy::or_fun_call)]
#[inline]
fn fmt_self<T: Write, I: Indentation>(
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
self
.output_ccb_name
.as_ref()
.map_or(write!(f, "{}", self.input_ccb_name), |output_ccb_name| {
write!(f, "{}, {}", self.input_ccb_name, output_ccb_name)
})
}
}