add uphill pips iter

This commit is contained in:
SpaceCat-Chan 2022-11-28 15:59:29 +01:00 committed by Lofty
parent afaaff6b00
commit 035247ebbf
2 changed files with 68 additions and 2 deletions

View File

@ -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);
}

View File

@ -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<Self::Item> {
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();