Skip to content

Commit

Permalink
more finish refector
Browse files Browse the repository at this point in the history
  • Loading branch information
wanda-phi committed Jan 26, 2024
1 parent c2d648f commit b882015
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 61 deletions.
1 change: 1 addition & 0 deletions prjcombine_types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ version.workspace = true

[dependencies]
serde.workspace = true
serde_json.workspace = true
bitvec.workspace = true
itertools.workspace = true
unnamed_entity.workspace = true
Expand Down
24 changes: 24 additions & 0 deletions prjcombine_types/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use std::collections::{btree_map, BTreeMap};
use bitvec::vec::BitVec;
use itertools::*;
use serde::{Deserialize, Serialize};
use serde_json::json;
use unnamed_entity::entity_id;

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
Expand Down Expand Up @@ -55,6 +56,29 @@ impl<T: Debug + Copy + Eq + Ord> Tile<T> {
}
}
}

pub fn to_json(&self, bit_to_json: impl Fn(T) -> serde_json::Value) -> serde_json::Value {
serde_json::Map::from_iter(self.items.iter().map(|(k, v)| {
(
k.clone(),
match &v.kind {
TileItemKind::Enum { values } => json!({
"bits": Vec::from_iter(v.bits.iter().copied().map(&bit_to_json)),
"values": serde_json::Map::from_iter(
values.iter().map(|(vk, vv)| {
(vk.clone(), Vec::from_iter(vv.iter().map(|x| *x)).into())
})
),
}),
TileItemKind::BitVec { invert } => json!({
"bits": Vec::from_iter(v.bits.iter().copied().map(&bit_to_json)),
"invert": *invert,
}),
},
)
}))
.into()
}
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
Expand Down
36 changes: 5 additions & 31 deletions prjcombine_xilinx_recpld/src/bin/xcpld_finish_xc9500.rs
Original file line number Diff line number Diff line change
Expand Up @@ -749,32 +749,6 @@ fn validate_pterm(device: &Device, fpart: &FuzzDbPart) {
}
}

fn tile_to_json<T: Copy>(
tile: &Tile<T>,
bit_to_json: impl Fn(T) -> serde_json::Value,
) -> serde_json::Value {
serde_json::Map::from_iter(tile.items.iter().map(|(k, v)| {
(
k.clone(),
match &v.kind {
TileItemKind::Enum { values } => json!({
"bits": Vec::from_iter(v.bits.iter().copied().map(&bit_to_json)),
"values": serde_json::Map::from_iter(
values.iter().map(|(vk, vv)| {
(vk.clone(), Vec::from_iter(vv.iter().map(|x| *x)).into())
})
),
}),
TileItemKind::BitVec { invert } => json!({
"bits": Vec::from_iter(v.bits.iter().copied().map(&bit_to_json)),
"invert": invert,
}),
},
)
}))
.into()
}

fn fb_bit_to_json(crd: FbBitCoord) -> serde_json::Value {
json!([crd.row, crd.bit, crd.column])
}
Expand Down Expand Up @@ -1043,9 +1017,9 @@ pub fn main() -> Result<(), Box<dyn Error>> {
"banks": device.banks,
"tdo_bank": device.tdo_bank,
"io_special": device.io_special,
"imux_bits": tile_to_json(&device.imux_bits, fb_bit_to_json),
"imux_bits": device.imux_bits.to_json(fb_bit_to_json),
"uim_ibuf_bits": if let Some(ref bits) = device.uim_ibuf_bits {
tile_to_json(bits, global_bit_to_json)
bits.to_json(global_bit_to_json)
} else {
serde_json::Value::Null
},
Expand Down Expand Up @@ -1074,9 +1048,9 @@ pub fn main() -> Result<(), Box<dyn Error>> {
),
"speeds": &database.speeds,
"parts": &database.parts,
"mc_bits": tile_to_json(&database.mc_bits, |bit| bit.into()),
"fb_bits": tile_to_json(&database.fb_bits, fb_bit_to_json),
"global_bits": tile_to_json(&database.global_bits, global_bit_to_json),
"mc_bits": database.mc_bits.to_json(|bit| bit.into()),
"fb_bits": database.fb_bits.to_json(fb_bit_to_json),
"global_bits": database.global_bits.to_json(global_bit_to_json),
});
std::fs::write(args.json, json.to_string())?;

Expand Down
34 changes: 4 additions & 30 deletions prjcombine_xilinx_recpld/src/bin/xcpld_finish_xpla3.rs
Original file line number Diff line number Diff line change
Expand Up @@ -724,32 +724,6 @@ fn verify_jed(
assert_eq!(pos, fpart.map.main.len());
}

fn tile_to_json<T: Copy>(
tile: &Tile<T>,
bit_to_json: impl Fn(T) -> serde_json::Value,
) -> serde_json::Value {
serde_json::Map::from_iter(tile.items.iter().map(|(k, v)| {
(
k.clone(),
match &v.kind {
TileItemKind::Enum { values } => json!({
"bits": Vec::from_iter(v.bits.iter().copied().map(&bit_to_json)),
"values": serde_json::Map::from_iter(
values.iter().map(|(vk, vv)| {
(vk.clone(), Vec::from_iter(vv.iter().map(|x| *x)).into())
})
),
}),
TileItemKind::BitVec { invert } => json!({
"bits": Vec::from_iter(v.bits.iter().copied().map(&bit_to_json)),
"invert": *invert,
}),
},
)
}))
.into()
}

fn bit_to_json(crd: xpla3::BitCoord) -> serde_json::Value {
json!([crd.row, crd.plane, crd.column])
}
Expand Down Expand Up @@ -1032,9 +1006,9 @@ pub fn main() -> Result<(), Box<dyn Error>> {
"fb_cols": device.fb_cols,
"io_mcs": device.io_mcs,
"io_special": device.io_special,
"global_bits": tile_to_json(&device.global_bits, bit_to_json),
"global_bits": device.global_bits.to_json(bit_to_json),
"jed_global_bits": device.jed_global_bits,
"imux_bits": tile_to_json(&device.imux_bits, bit_to_json),
"imux_bits": device.imux_bits.to_json(bit_to_json),
}))),
"bonds": Vec::from_iter(
database.bonds.values().map(|bond| json!({
Expand All @@ -1055,8 +1029,8 @@ pub fn main() -> Result<(), Box<dyn Error>> {
),
"speeds": &database.speeds,
"parts": &database.parts,
"mc_bits": tile_to_json(&database.mc_bits, bit_to_json),
"fb_bits": tile_to_json(&database.fb_bits, bit_to_json),
"mc_bits": database.mc_bits.to_json(bit_to_json),
"fb_bits": database.fb_bits.to_json(bit_to_json),
"jed_mc_bits_iob": &database.jed_mc_bits_iob,
"jed_mc_bits_buried": &database.jed_mc_bits_buried,
"jed_fb_bits": &database.jed_fb_bits,
Expand Down

0 comments on commit b882015

Please sign in to comment.