Skip to content

Commit

Permalink
more wpi
Browse files Browse the repository at this point in the history
  • Loading branch information
5aji committed Apr 4, 2024
1 parent 393ffa3 commit 3a271db
Showing 1 changed file with 65 additions and 63 deletions.
128 changes: 65 additions & 63 deletions src/yosys_parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::gal::Pin;
use crate::pcf::PcfFile;
use itertools::Itertools;
use log::info;
use serde::{de::Error, Deserialize, Deserializer, Serialize};
use serde_with::{serde_as, BoolFromInt};
Expand All @@ -14,6 +13,26 @@ use std::str;
#[serde(transparent)]
pub struct Net(u32);

#[derive(Debug, Serialize, Clone, Deserialize, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum NetSpecial {
N(u32),
Special(String), // constant ("1", "0") or NC ("x")
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct GalInputConnections {
#[serde(rename = "A")]
pub input: [NetSpecial; 1],
#[serde(rename = "Y")]
pub output: [NetSpecial; 1], // sop Y has exactly one output
}


#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct GalInput {
pub connections: GalInputConnections,
}

// for some reason the json outputs 00000000000111 for some numbers.
// this converts them back into binary.
fn from_binstr<'de, D>(deserializer: D) -> Result<u32, D::Error>
Expand All @@ -26,65 +45,69 @@ where

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
pub struct SoPParameters {
pub struct GalSopParameters {
#[serde(deserialize_with = "from_binstr")]
pub depth: u32,
pub table: String,
#[serde(deserialize_with = "from_binstr")]
pub width: u32,
}


#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct SoPConnections {
pub struct GalSopConnections {
#[serde(rename = "A")]
pub inputs: Vec<Net>,
pub inputs: Vec<NetSpecial>,
#[serde(rename = "Y")]
pub output: [Net; 1], // sop Y has exactly one output
pub output: [NetSpecial; 1], // sop Y has exactly one output
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct SoPCell {
pub parameters: SoPParameters,
pub connections: SoPConnections,
pub struct GalSop {
pub connections: GalSopConnections,
pub parameters: GalSopParameters,
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct DffConnections {
#[serde(rename = "C")]
pub clock: [Net; 1],
#[serde(rename = "D")]
pub input: [Net; 1],
#[serde(rename = "Q")]
pub output: [Net; 1],
}

#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct DffCell {
connections: DffConnections,
#[serde(rename_all = "UPPERCASE")]
pub struct GALOLMCParameters {
#[serde_as(as = "BoolFromInt")]
inverted: bool,
#[serde_as(as = "BoolFromInt")]
registered: bool,

}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
#[serde(rename_all = "UPPERCASE")]
pub struct NotConnections {
pub a: [Net; 1],
pub y: [Net; 1],
pub struct GalOLMCConnections {
#[serde(rename = "C")]
pub clock: [NetSpecial; 1],
#[serde(rename = "A")]
pub input: [NetSpecial; 1],
#[serde(rename = "Y")]
pub output: [NetSpecial; 1],
#[serde(rename = "E")]
pub tristate: [NetSpecial; 1],
}

#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
pub struct NotCell {
connections: NotConnections,
pub struct GalOLMC {
pub parameters: GALOLMCParameters,
pub connections: GalOLMCConnections,
}

#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
#[serde(tag = "type")]
pub enum YosysCell {
#[serde(rename = "DFF_P")]
DffCell(DffCell),
#[serde(rename = "$sop")]
SoPCell(SoPCell),
#[serde(rename = "$_NOT_")]
NotCell(NotCell),
#[serde(rename = "GAL_SOP")]
Sop(GalSop),
#[serde(rename = "GAL_INPUT")]
Input(GalInput),
#[serde(rename = "GAL_OLMC")]
OLMC(GalOLMC),
}

#[serde_as]
Expand Down Expand Up @@ -114,7 +137,7 @@ pub struct YosysDoc {
pub modules: HashMap<String, Module>,
}

impl SoPCell {
impl GalSop {
// extract the logic table using the parameters.
pub fn parse_table(self) -> Vec<Vec<Pin>> {
// get the list of inputs which is a Vec<u32>
Expand Down Expand Up @@ -250,11 +273,9 @@ type NetAdjPair = (usize, usize, Net);
/// A Node is an entry in our graph. A node has inputs and outputs.
#[derive(Debug, PartialEq, Clone)]
pub enum Node {
Input(NamedPort),
Output(NamedPort),
Sop(SoPCell),
Not(NotCell),
Dff(DffCell),
Input(GalInput),
Sop(GalSop),
Olmc(GalOLMC),
}

impl Node {
Expand All @@ -263,22 +284,18 @@ impl Node {
* internally. So get_outputs returns values on input_cells, since those are driven already.
* Likewise we reverse this for get_inputs, since the chip's outputs are our inputs.
*/
fn get_outputs(&self) -> Vec<Net> {
fn get_outputs(&self) -> Vec<NetSpecial> {
match self {
Self::Input(i) => vec![i.net.clone()],
Self::Output(_) => Vec::new(),
Self::Input(i) => i.connections.output.to_vec(),
Self::Sop(s) => s.connections.output.to_vec(),
Self::Not(n) => n.connections.y.to_vec(),
Self::Dff(d) => d.connections.output.to_vec(),
Self::Olmc(o) => o.connections.output.to_vec(),
}
}
fn get_inputs(&self) -> Vec<Net> {
fn get_inputs(&self) -> Vec<NetSpecial> {
match self {
Self::Input(_) => Vec::new(),
Self::Output(o) => vec![o.net.clone()],
Self::Sop(s) => s.connections.inputs.to_vec(),
Self::Not(n) => n.connections.a.to_vec(),
Self::Dff(d) => d.connections.input.to_vec(),
Self::Input(gi) => gi.connections.input.to_vec(),
Self::Sop(gs) => gs.connections.inputs.to_vec(),
Self::Olmc(go) => go.connections.input.to_vec(),
}
}
}
Expand Down Expand Up @@ -334,21 +351,6 @@ impl Graph {

(inputs, output.unwrap())
}
fn validate(&self) -> Result<(), String> {
let nets: Vec<&Net> = self
.adjlist
.iter()
.map(|(_, _, n)| n)
.unique()
.collect_vec();
// back-travel
// find output ports, find their drivers
// output port -> Vec<Net> -> Vec<(Net,Pin)>
// for each node that connects to output port, get the output NET of that node.
// there should only be one. Then damage that node with a pin tag.

Ok(())
}
}

impl From<YosysDoc> for Graph {
Expand Down

0 comments on commit 3a271db

Please sign in to comment.