From 035247ebbfb74df7a0f7fb88fae39b9a2f5ae38d Mon Sep 17 00:00:00 2001 From: SpaceCat-Chan <49094338+SpaceCat-Chan@users.noreply.github.com> Date: Mon, 28 Nov 2022 15:59:29 +0100 Subject: [PATCH] add uphill pips iter --- common/route/awooter.cc | 27 +++++++++++++++-- common/route/awooter/rust/src/npnr.rs | 43 +++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/common/route/awooter.cc b/common/route/awooter.cc index a7164d0f..7ae89642 100644 --- a/common/route/awooter.cc +++ b/common/route/awooter.cc @@ -60,6 +60,14 @@ struct DownhillIterWrapper { DownhillIterWrapper(DownhillIter begin, DownhillIter end) : current(begin), end(end) {} }; +using UphillIter = decltype(Context(ArchArgs()).getPipsUphill(WireId()).begin()); + +struct UphillIterWrapper { + UphillIter current; + UphillIter end; + + UphillIterWrapper(UphillIter begin, UphillIter end) : current(begin), end(end) {} +}; extern "C" { USING_NEXTPNR_NAMESPACE; @@ -244,6 +252,14 @@ extern "C" { void npnr_delete_downhill_iter(DownhillIterWrapper *iter) { delete iter; } + UphillIterWrapper *npnr_context_get_pips_uphill(Context *ctx, uint64_t wire_id) { + auto wire = unwrap_wire(wire_id); + auto range = ctx->getPipsUphill(wire); + return new UphillIterWrapper(range.begin(), range.end()); + } + void npnr_delete_uphill_iter(UphillIterWrapper *iter) { + delete iter; + } PortRef* npnr_netinfo_driver(NetInfo *const net) { if (net == nullptr) { @@ -280,14 +296,21 @@ extern "C" { void npnr_inc_downhill_iter(DownhillIterWrapper *iter) { ++iter->current; } - uint64_t npnr_deref_downhill_iter(DownhillIterWrapper *iter) { return wrap(*iter->current); } - bool npnr_is_downhill_iter_done(DownhillIterWrapper *iter) { return !(iter->current != iter->end); } + void npnr_inc_uphill_iter(UphillIterWrapper *iter) { + ++iter->current; + } + uint64_t npnr_deref_uphill_iter(UphillIterWrapper *iter) { + return wrap(*iter->current); + } + bool npnr_is_uphill_iter_done(UphillIterWrapper *iter) { + return !(iter->current != iter->end); + } extern bool npnr_router_awooter(Context *ctx); } diff --git a/common/route/awooter/rust/src/npnr.rs b/common/route/awooter/rust/src/npnr.rs index cd7c281c..0bcd5252 100644 --- a/common/route/awooter/rust/src/npnr.rs +++ b/common/route/awooter/rust/src/npnr.rs @@ -245,6 +245,14 @@ impl Context { } } + pub fn get_uphill_pips(&self, wire: WireId) -> UphillPipsIter { + let iter = unsafe { npnr_context_get_pips_uphill(self, wire) }; + UphillPipsIter { + iter, + phantom_data: Default::default(), + } + } + pub fn pip_location(&self, pip: PipId) -> Loc { unsafe { npnr_context_get_pip_location(self, pip) } } @@ -340,6 +348,8 @@ extern "C" { ) -> u32; fn npnr_context_get_pips_downhill(ctx: *const Context, wire: WireId) -> *mut RawDownhillIter; fn npnr_delete_downhill_iter(iter: *mut RawDownhillIter); + fn npnr_context_get_pips_uphill(ctx: *const Context, wire: WireId) -> *mut RawUphillIter; + fn npnr_delete_uphill_iter(iter: *mut RawUphillIter); fn npnr_netinfo_driver(net: *mut NetInfo) -> *mut PortRef; fn npnr_netinfo_users_leak(net: *mut NetInfo, users: *mut *mut *mut PortRef) -> u32; @@ -352,6 +362,9 @@ extern "C" { fn npnr_inc_downhill_iter(iter: *mut RawDownhillIter); fn npnr_deref_downhill_iter(iter: *mut RawDownhillIter) -> PipId; fn npnr_is_downhill_iter_done(iter: *mut RawDownhillIter) -> bool; + fn npnr_inc_uphill_iter(iter: *mut RawUphillIter); + fn npnr_deref_uphill_iter(iter: *mut RawUphillIter) -> PipId; + fn npnr_is_uphill_iter_done(iter: *mut RawUphillIter) -> bool; } /// Store for the nets of a context. @@ -473,6 +486,36 @@ impl<'a> Drop for DownhillPipsIter<'a> { } } +#[repr(C)] +struct RawUphillIter { + content: [u8; 0], +} + +pub struct UphillPipsIter<'a> { + iter: *mut RawUphillIter, + phantom_data: std::marker::PhantomData<&'a PipId>, +} + +impl<'a> Iterator for UphillPipsIter<'a> { + type Item = PipId; + + fn next(&mut self) -> Option { + if unsafe { npnr_is_uphill_iter_done(self.iter) } { + None + } else { + let pip = unsafe { npnr_deref_uphill_iter(self.iter) }; + unsafe { npnr_inc_uphill_iter(self.iter) }; + Some(pip) + } + } +} + +impl<'a> Drop for UphillPipsIter<'a> { + fn drop(&mut self) { + unsafe { npnr_delete_uphill_iter(self.iter) }; + } +} + macro_rules! log_info { ($($t:tt)*) => { let s = std::ffi::CString::new(format!($($t)*)).unwrap();