Skip to content

Commit

Permalink
xcpld: write fuzz db to file
Browse files Browse the repository at this point in the history
  • Loading branch information
wanda-phi committed Nov 10, 2023
1 parent 55f9e89 commit 29fb3c0
Show file tree
Hide file tree
Showing 5 changed files with 122 additions and 54 deletions.
25 changes: 13 additions & 12 deletions prjcombine_xilinx_cpld/src/bits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,12 @@ use crate::types::{
use bitvec::vec::BitVec;
use enum_map::EnumMap;
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use unnamed_entity::{EntityId, EntityPartVec, EntityVec};

pub type BitPos = (u32, usize);

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct BitstreamMap {
pub main: Vec<BitPos>,
pub usercode: Option<[BitPos; 32]>,
Expand All @@ -29,7 +30,7 @@ pub struct BitstreamMap {

pub type InvBit = (usize, bool);

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct Bits {
// common
pub fbs: EntityVec<FbId, FbBits>,
Expand Down Expand Up @@ -58,7 +59,7 @@ pub struct Bits {
pub usercode: Option<[InvBit; 32]>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct FbBits {
// common
pub imux: EntityVec<ImuxId, EnumData<ImuxInput>>,
Expand All @@ -76,7 +77,7 @@ pub struct FbBits {
pub mcs: EntityVec<FbMcId, McBits>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct McBits {
// XC9500*
pub pt: Option<EnumMap<Xc9500McPt, PtData>>,
Expand Down Expand Up @@ -126,7 +127,7 @@ pub struct McBits {
pub ibuf_uim_en: Vec<InvBit>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct IPadBits {
// XPLA3
pub uim_out_en: EntityVec<FbGroupId, InvBit>,
Expand All @@ -135,13 +136,13 @@ pub struct IPadBits {
pub ibuf_mode: Option<EnumData<IBufMode>>,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct BankBits {
pub ibuf_hv: InvBit,
pub obuf_hv: InvBit,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct EnumData<K: Clone + Debug + Eq + PartialEq + Hash> {
pub bits: Vec<usize>,
pub items: HashMap<K, BitVec>,
Expand All @@ -158,32 +159,32 @@ impl<K: Clone + Debug + Eq + PartialEq + Hash> EnumData<K> {
}
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct PlaAndTerm {
pub imux: EntityVec<ImuxId, (InvBit, InvBit)>,
pub fbn: EntityVec<FbnId, InvBit>,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
pub enum PtAlloc {
OrMain,
OrExport,
Special,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
pub enum McOut {
Comb,
Reg,
}

#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Ord, PartialOrd, Serialize, Deserialize)]
pub enum IBufOut {
Pad,
Reg,
}

#[derive(Debug, Clone, Eq, PartialEq)]
#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct PtData {
pub and: EntityVec<ImuxId, (InvBit, InvBit)>,
pub hp: InvBit,
Expand Down
56 changes: 56 additions & 0 deletions prjcombine_xilinx_recpld/src/bin/xcpld_fdbprint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
use std::{error::Error, path::PathBuf};

use clap::Parser;
use prjcombine_xilinx_recpld::fuzzdb::FuzzDb;

#[derive(Parser)]
struct Args {
fdb: PathBuf,
}

pub fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();
let db = FuzzDb::from_file(args.fdb)?;
for part in db.parts {
println!("DEV {d} {p}", d = part.dev_name, p = part.pkg_name);
part.bits.print(&mut std::io::stdout())?;
let known_bits = part.bits.known_bits();
println!("BS LEN {}", part.map.main.len());
for (i, b) in part.blank.iter().enumerate() {
let (r, c) = part.map.main[i];
if let Some(s) = known_bits.get(&i) {
println!("BIT L{i} ({r}, {c}): {s}");
} else {
println!("BIT L{i} ({r}, {c}): UNK{v}", v = if *b { 1 } else { 0 });
}
}
if let Some(uc) = part.map.usercode {
for (i, (r, c)) in uc.into_iter().enumerate() {
println!(" USERCODE {i} -> ({r}, {c})");
}
}
if let Some(uc) = part.map.ues {
for (i, (r, c)) in uc.into_iter().enumerate() {
println!(" UES {i} -> ({r}, {c})");
}
}
if let Some((r, c)) = part.map.done {
println!(" DONE -> ({r}, {c})");
}

for (r, c) in part.map.rprot {
println!(" READ PROT -> ({r}, {c})");
}
for (r, c) in part.map.wprot {
println!(" WRITE PROT -> ({r}, {c})");
}

if let Some((c, r, a)) = part.map.dims {
println!(" DIMS {c}×{r}, {a}");
}
for x in part.map.transfer {
println!(" TRANSFER {x}");
}
}
Ok(())
}
59 changes: 17 additions & 42 deletions prjcombine_xilinx_recpld/src/bin/xcpld_fuzz/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use clap::Parser;
use prjcombine_toolchain::Toolchain;
use prjcombine_xilinx_recpld::{
db::Database,
fuzzdb::{FuzzDb, FuzzDbPart},
hprep6::run_hprep6,
vm6::{insert_dummy_obuf, prep_vm6},
};
Expand All @@ -19,6 +20,7 @@ use prjcombine_xilinx_recpld::{
struct Args {
tc: PathBuf,
db: PathBuf,
fdb: PathBuf,
device: Option<String>,
#[arg(short, long, action = clap::ArgAction::Count)]
debug: u8,
Expand All @@ -28,6 +30,7 @@ pub fn main() -> Result<(), Box<dyn Error>> {
let args = Args::parse();
let tc = Toolchain::from_file(args.tc)?;
let db = Database::from_file(args.db)?;
let mut parts = vec![];
for part in &db.parts {
if let Some(ref filter) = args.device {
if *filter != part.dev_name
Expand All @@ -38,57 +41,29 @@ pub fn main() -> Result<(), Box<dyn Error>> {
}
let device = &db.devices[part.device];
let package = &db.packages[part.package];
println!("DEV {d} {p}", d = part.dev_name, p = part.pkg_name);
let bits = reverse_cpld(&tc, part, device, package, args.debug);
bits.print(&mut std::io::stdout())?;
let known_bits = bits.known_bits();
println!("MAIN RE DONE {d} {p}", d = part.dev_name, p = part.pkg_name);
let mut vm6 = prep_vm6(part, &device.device, package, &part.speeds[0]);
insert_dummy_obuf(&mut vm6);
let jed = run_hprep6(&tc, &vm6, None)?;
let v2p = reverse_bitstream(
let blank = run_hprep6(&tc, &vm6, None)?;
let map = reverse_bitstream(
&tc,
device.device.kind,
&part.dev_name,
&part.pkg_name,
jed.len(),
blank.len(),
);
println!("BS RE DONE {}", v2p.main.len());
for (i, b) in jed.iter().enumerate() {
let (r, c) = v2p.main[i];
if let Some(s) = known_bits.get(&i) {
println!("BIT L{i} ({r}, {c}): {s}");
} else {
println!("BIT L{i} ({r}, {c}): UNK{v}", v = if *b { 1 } else { 0 });
}
}
if let Some(uc) = v2p.usercode {
for (i, (r, c)) in uc.into_iter().enumerate() {
println!(" USERCODE {i} -> ({r}, {c})");
}
}
if let Some(uc) = v2p.ues {
for (i, (r, c)) in uc.into_iter().enumerate() {
println!(" UES {i} -> ({r}, {c})");
}
}
if let Some((r, c)) = v2p.done {
println!(" DONE -> ({r}, {c})");
}

for (r, c) in v2p.rprot {
println!(" READ PROT -> ({r}, {c})");
}
for (r, c) in v2p.wprot {
println!(" WRITE PROT -> ({r}, {c})");
}

if let Some((c, r, a)) = v2p.dims {
println!(" DIMS {c}×{r}, {a}");
}
for x in v2p.transfer {
println!(" TRANSFER {x}");
}
println!("JED RE DONE {d} {p}", d = part.dev_name, p = part.pkg_name);
parts.push(FuzzDbPart {
dev_name: part.dev_name.clone(),
pkg_name: part.pkg_name.clone(),
bits,
map,
blank,
});
}
let fdb = FuzzDb { parts };
fdb.to_file(args.fdb)?;

Ok(())
}
35 changes: 35 additions & 0 deletions prjcombine_xilinx_recpld/src/fuzzdb.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
use std::{error::Error, fs::File, path::Path};

use bitvec::vec::BitVec;
use prjcombine_xilinx_cpld::bits::{Bits, BitstreamMap};
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct FuzzDbPart {
pub dev_name: String,
pub pkg_name: String,
pub bits: Bits,
pub map: BitstreamMap,
pub blank: BitVec,
}

#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
pub struct FuzzDb {
pub parts: Vec<FuzzDbPart>,
}

impl FuzzDb {
pub fn to_file<P: AsRef<Path>>(&self, path: P) -> Result<(), Box<dyn Error>> {
let f = File::create(path)?;
let mut cf = zstd::stream::Encoder::new(f, 9)?;
bincode::serialize_into(&mut cf, self)?;
cf.finish()?;
Ok(())
}

pub fn from_file<P: AsRef<Path>>(path: P) -> Result<Self, Box<dyn Error>> {
let f = File::open(path)?;
let cf = zstd::stream::Decoder::new(f)?;
Ok(bincode::deserialize_from(cf)?)
}
}
1 change: 1 addition & 0 deletions prjcombine_xilinx_recpld/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
pub mod bitstream;
pub mod db;
pub mod fuzzdb;
pub mod hprep6;
pub mod impact;
pub mod partgen;
Expand Down

0 comments on commit 29fb3c0

Please sign in to comment.