-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathedge_ff.rs
50 lines (46 loc) · 1.11 KB
/
edge_ff.rs
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
use rust_hdl_core::prelude::*;
use rust_hdl_core::timing::TimingInfo;
#[derive(Clone, Debug, LogicBlock, Default)]
pub struct EdgeDFF<T: Synth> {
pub d: Signal<In, T>,
pub q: Signal<Out, T>,
pub clk: Signal<In, Clock>,
}
impl<T: Synth> EdgeDFF<T> {
pub fn new(init: T) -> EdgeDFF<T> {
Self {
d: Signal::default(),
q: Signal::new_with_default(init),
clk: Signal::default(),
}
}
}
// TODO - make this specializable
impl<T: Synth> Logic for EdgeDFF<T> {
fn update(&mut self) {
if self.clk.pos_edge() {
self.q.next = self.d.val()
}
}
fn connect(&mut self) {
self.q.connect();
}
fn hdl(&self) -> Verilog {
Verilog::Custom(format!(
"\
initial begin
q = {:x};
end
always @(posedge clk) q <= d;",
self.q.verilog()
))
}
fn timing(&self) -> Vec<TimingInfo> {
vec![TimingInfo {
name: "edge_ff".to_string(),
clock: "clk".to_string(),
inputs: vec!["d".into()],
outputs: vec!["q".into()],
}]
}
}