use crate::{
ast::{
Attributes, CodeFormatter, ComplexAttri, ComplexParseError, GroupComments, GroupFn,
GroupSet, Indentation, ParseScope, SimpleAttri,
},
common::table::{
TableLookUp, TableLookUp2D, TableLookUpMultiSegment, Vector3DGrpup, Vector4DGrpup,
},
expression::IdBooleanExpression,
timing::items::Mode,
ArcStr, NotNan,
};
use core::fmt::{self, Write};
use num_traits::Zero;
#[mut_set::derive::item(sort)]
#[derive(Debug, Default, Clone)]
#[derive(liberty_macros::Group)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct CCSNStage {
#[liberty(name)]
#[size = 24]
#[id(borrow = "&[ArcStr]")]
pub name: Vec<ArcStr>,
#[size = 288]
#[liberty(comments)]
pub comments: GroupComments<Self>,
#[size = 48]
#[liberty(attributes)]
pub attributes: Attributes,
#[size = 16]
#[liberty(simple(type = Option))]
pub load_cap_fall: Option<NotNan<f64>>,
#[size = 16]
#[liberty(simple(type = Option))]
pub load_cap_rise: Option<NotNan<f64>>,
#[size = 1]
#[liberty(simple(type = Option))]
pub is_inverting: Option<bool>,
#[size = 1]
#[liberty(simple(type = Option))]
pub is_needed: Option<bool>,
#[size = 1]
#[liberty(simple(type = Option))]
pub is_pass_gate: Option<bool>,
#[size = 16]
#[liberty(simple(type = Option))]
pub miller_cap_fall: Option<NotNan<f64>>,
#[size = 16]
#[liberty(simple(type = Option))]
pub miller_cap_rise: Option<NotNan<f64>>,
#[size = 8]
#[liberty(simple(type = Option))]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!")]
pub related_ccb_node: Option<ArcStr>,
#[size = 1]
#[liberty(simple(type = Option))]
pub stage_type: Option<StageType>,
#[size = 80]
#[liberty(simple(type = Option))]
#[id(borrow = "Option<&IdBooleanExpression>", check_fn = "mut_set::borrow_option!")]
pub when: Option<IdBooleanExpression>,
#[size = 16]
#[liberty(complex(type = Option))]
pub mode: Option<Mode>,
#[size = 240]
#[liberty(group(type = Option))]
pub dc_current: Option<TableLookUp2D>,
#[size = 128]
#[liberty(group(type = Option))]
pub output_voltage_fall: Option<Vector3DGrpup>,
#[size = 128]
#[liberty(group(type = Option))]
pub output_voltage_rise: Option<Vector3DGrpup>,
#[size = 128]
#[liberty(group(type = Option))]
pub propagated_noise_low: Option<Vector4DGrpup>,
#[size = 128]
#[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 let Some(miller_cap_fall) = self.miller_cap_fall.as_mut() {
if miller_cap_fall.is_sign_negative() {
miller_cap_fall.set_zero();
log::warn!("miller_cap_fall is negative!");
}
}
if let Some(miller_cap_rise) = self.miller_cap_rise.as_mut() {
if miller_cap_rise.is_sign_negative() {
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)
}
}
#[mut_set::derive::item(sort)]
#[derive(Debug, Default, Clone)]
#[derive(liberty_macros::Group)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct ReceiverCapacitance {
#[size = 8]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!")]
#[liberty(name)]
pub name: Option<ArcStr>,
#[size = 48]
#[liberty(comments)]
pub comments: GroupComments<Self>,
#[size = 48]
#[liberty(attributes)]
pub attributes: Attributes,
#[id(borrow = "Option<&IdBooleanExpression>", check_fn = "mut_set::borrow_option!")]
#[size = 80]
#[liberty(simple(type=Option))]
pub when: Option<IdBooleanExpression>,
#[size = 48]
#[liberty(group(type = Set))]
#[serde(serialize_with = "GroupSet::<TableLookUpMultiSegment>::serialize_with")]
#[serde(deserialize_with = "GroupSet::<TableLookUpMultiSegment>::deserialize_with")]
pub receiver_capacitance_fall: GroupSet<TableLookUpMultiSegment>,
#[size = 48]
#[liberty(group(type = Set))]
#[serde(serialize_with = "GroupSet::<TableLookUpMultiSegment>::serialize_with")]
#[serde(deserialize_with = "GroupSet::<TableLookUpMultiSegment>::deserialize_with")]
pub receiver_capacitance_rise: GroupSet<TableLookUpMultiSegment>,
#[size = 336]
#[liberty(group)]
pub receiver_capacitance1_fall: Option<TableLookUp>,
#[size = 336]
#[liberty(group)]
pub receiver_capacitance1_rise: Option<TableLookUp>,
#[size = 336]
#[liberty(group)]
pub receiver_capacitance2_fall: Option<TableLookUp>,
#[size = 336]
#[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)
})
}
}