use crate::{
ast::{
Attributes, BuilderScope, CodeFormatter, ComplexAttri, ComplexParseError,
GroupComments, GroupFn, GroupSet, Indentation, ParseScope, SimpleAttri,
},
common::table::{
TableLookUp, TableLookUp2D, TableLookUpMultiSegment, Vector3DGrpup, Vector4DGrpup,
},
expression::LogicBooleanExpression,
Ctx,
};
use core::fmt::{self, Write};
#[mut_set::derive::item(sort)]
#[derive(Debug, Clone)]
#[derive(liberty_macros::Group)]
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Other: serde::Serialize + serde::de::DeserializeOwned")]
pub struct CCSNStage<C: Ctx> {
#[liberty(name)]
#[size = 24]
#[id(borrow = "&[String]", with_ref = false)]
pub name: Vec<String>,
#[size = 32]
#[liberty(comments)]
comments: GroupComments,
#[size = 0]
#[liberty(extra_ctx)]
pub extra_ctx: C::Other,
#[size = 40]
#[liberty(attributes)]
pub attributes: Attributes,
#[size = 16]
#[liberty(simple(type = Option))]
pub load_cap_fall: Option<f64>,
#[size = 16]
#[liberty(simple(type = Option))]
pub load_cap_rise: Option<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<f64>,
#[size = 16]
#[liberty(simple(type = Option))]
pub miller_cap_rise: Option<f64>,
#[size = 8]
#[liberty(simple(type = Option))]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!", with_ref = false)]
pub related_ccb_node: Option<String>,
#[size = 1]
#[liberty(simple(type = Option))]
pub stage_type: Option<StageType>,
#[size = 80]
#[liberty(simple(type = Option))]
#[id(
borrow = "Option<&LogicBooleanExpression>",
check_fn = "mut_set::borrow_option!",
with_ref = false
)]
pub when: Option<LogicBooleanExpression>,
#[size = 16]
#[liberty(complex(type = Option))]
pub mode: Option<[String; 2]>,
#[size = 240]
#[liberty(group(type = Option))]
pub dc_current: Option<TableLookUp2D<C>>,
#[size = 128]
#[liberty(group(type = Option))]
pub output_voltage_fall: Option<Vector3DGrpup<C>>,
#[size = 128]
#[liberty(group(type = Option))]
pub output_voltage_rise: Option<Vector3DGrpup<C>>,
#[size = 128]
#[liberty(group(type = Option))]
pub propagated_noise_low: Option<Vector4DGrpup<C>>,
#[size = 128]
#[liberty(group(type = Option))]
pub propagated_noise_high: Option<Vector4DGrpup<C>>,
}
impl<C: Ctx> GroupFn<C> for CCSNStage<C> {
#[inline]
fn before_build(builder: &mut Self::Builder, _scope: &mut BuilderScope<C>) {
if let Some(miller_cap_fall) = builder.miller_cap_fall.as_mut() {
if miller_cap_fall.is_sign_negative() {
*miller_cap_fall = 0.0;
log::warn!("miller_cap_fall is negative!");
}
}
if let Some(miller_cap_rise) = builder.miller_cap_rise.as_mut() {
if miller_cap_rise.is_sign_negative() {
*miller_cap_rise = 0.0;
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,
}
crate::ast::impl_self_builder!(StageType);
impl<C: Ctx> SimpleAttri<C> for StageType {
#[inline]
fn nom_parse<'a>(
i: &'a str,
scope: &mut ParseScope,
) -> crate::ast::SimpleParseRes<'a, Self> {
crate::ast::nom_parse_from_str::<C, _>(i, scope)
}
}
#[mut_set::derive::item(sort)]
#[derive(Debug, Clone)]
#[derive(liberty_macros::Group)]
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Other: serde::Serialize + serde::de::DeserializeOwned")]
pub struct ReceiverCapacitance<C: Ctx> {
#[size = 8]
#[liberty(name)]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!", with_ref = false)]
pub name: Option<String>,
#[size = 32]
#[liberty(comments)]
comments: GroupComments,
#[size = 0]
#[liberty(extra_ctx)]
pub extra_ctx: C::Other,
#[size = 40]
#[liberty(attributes)]
pub attributes: Attributes,
#[size = 80]
#[liberty(simple(type=Option))]
#[id(
borrow = "Option<&LogicBooleanExpression>",
check_fn = "mut_set::borrow_option!",
with_ref = false
)]
pub when: Option<LogicBooleanExpression>,
#[size = 88]
#[liberty(group(type = Set))]
#[serde(serialize_with = "GroupSet::<TableLookUpMultiSegment<C>>::serialize_with")]
#[serde(deserialize_with = "GroupSet::<TableLookUpMultiSegment<C>>::deserialize_with")]
pub receiver_capacitance_fall: GroupSet<TableLookUpMultiSegment<C>>,
#[size = 88]
#[liberty(group(type = Set))]
#[serde(serialize_with = "GroupSet::<TableLookUpMultiSegment<C>>::serialize_with")]
#[serde(deserialize_with = "GroupSet::<TableLookUpMultiSegment<C>>::deserialize_with")]
pub receiver_capacitance_rise: GroupSet<TableLookUpMultiSegment<C>>,
#[size = 24]
#[liberty(complex)]
pub active_input_ccb: Vec<String>,
#[size = 336]
#[liberty(group)]
pub receiver_capacitance1_fall: Option<TableLookUp<C>>,
#[size = 336]
#[liberty(group)]
pub receiver_capacitance1_rise: Option<TableLookUp<C>>,
#[size = 336]
#[liberty(group)]
pub receiver_capacitance2_fall: Option<TableLookUp<C>>,
#[size = 336]
#[liberty(group)]
pub receiver_capacitance2_rise: Option<TableLookUp<C>>,
}
impl<C: Ctx> GroupFn<C> for ReceiverCapacitance<C> {}
#[derive(Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct PropagatingCcb {
pub input_ccb_name: String,
pub output_ccb_name: Option<String>,
}
crate::ast::impl_self_builder!(PropagatingCcb);
impl<C: Ctx> ComplexAttri<C> for PropagatingCcb {
#[inline]
fn parse<'a, I: Iterator<Item = &'a &'a str>>(
mut iter: I,
_scope: &mut ParseScope,
) -> Result<Self, ComplexParseError> {
let input_ccb_name = match iter.next() {
Some(&s) => String::from(s),
None => return Err(ComplexParseError::LengthDismatch),
};
let output_ccb_name = iter.next().map(|&s| String::from(s));
if iter.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)
})
}
}