1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
use rust_hdl::core::prelude::*;

#[derive(Default)]
struct PCFGenerator {
    path: NamedPath,
    namespace: NamedPath,
    pcf: Vec<String>,
}

impl Probe for PCFGenerator {
    fn visit_start_scope(&mut self, name: &str, _node: &dyn Block) {
        let _top_level = self.path.to_string();
        self.path.push(name);
        self.namespace.reset();
    }
    fn visit_start_namespace(&mut self, name: &str, _node: &dyn Block) {
        self.namespace.push(name);
    }
    fn visit_atom(&mut self, name: &str, signal: &dyn Atom) {
        if self.path.len() == 1 {
            let namespace = self.namespace.flat("$");
            let name = if namespace.is_empty() {
                name.to_owned()
            } else {
                format!("{}${}", namespace, name)
            };
            for pin in &signal.constraints() {
                match &pin.constraint {
                    Constraint::Location(l) => {
                        if signal.bits() == 1 {
                            self.pcf.push(format!("set_io {} {}", name, l))
                        } else {
                            self.pcf
                                .push(format!("set_io {}[{}] {}", name, pin.index, l))
                        }
                    }
                    Constraint::Custom(s) => self.pcf.push(s.clone()),
                    _ => {
                        panic!("Pin constraint type {:?} is unsupported!", pin.constraint)
                    }
                }
            }
        }
    }
    fn visit_end_namespace(&mut self, _name: &str, _node: &dyn Block) {
        self.namespace.pop();
    }
    fn visit_end_scope(&mut self, _name: &str, _node: &dyn Block) {
        self.path.pop();
    }
}

pub fn generate_pcf<U: Block>(uut: &U) -> String {
    let mut pcf = PCFGenerator::default();
    uut.accept("top", &mut pcf);
    pcf.pcf.join("\n") + "\n"
}