pub struct Constant<T: Synth> { /* private fields */ }
Expand description

The Constant wrapper can hold any Synth type and store it in a circuit for use by the HDL kernel. This is the easiest way to compute complex constants in your RustHDL constructors, and then store the results inside the circuit for later use. Unlike [Signal], Constant does not have a .next field, so you cannot assign to a Constant in the HDL kernel (blocked at compile time). Note that Constant does not impl Default. You must construct it with the appropriate value when the circuit is built.

Here is a correct usage of a Constant

 use rust_hdl::prelude::*;

#[derive(LogicBlock)]
struct AddNum {
   pub i1: Signal<In, Bits<8>>,
   pub o1: Signal<Out, Bits<8>>,
   c1: Constant<Bits<8>>,
}

impl Default for AddNum {
    fn default() -> Self {
         Self {
            i1: Default::default(),
            o1: Default::default(),
            c1: Constant::new(42.into()),
         }
    }
}

impl Logic for AddNum {
     #[hdl_gen]
     fn update(&mut self) {
        // Note that `self.c1.next` does not exist... 
        self.o1.next = self.i1.val() + self.c1.val(); 
     }
}

   let mut sim : Simulation<AddNum> = Simulation::default();
   sim.add_testbench(|mut ep: Sim<AddNum>| {
       let mut x = ep.init()?;
       x.i1.next = 13.into();
       x = ep.wait(1, x)?;
       sim_assert_eq!(ep, x.o1.val(), 55, x);
       ep.done(x)
   });
   
   let mut uut = AddNum::default(); uut.connect_all();
   sim.run(Box::new(uut), 100).unwrap();

Implementations

Create a new Constant from the given value.

Retrieve the value of the constant. Usable in HDL kernels.

Trait Implementations

Connects the internal signals of the circuit - used to initialize the circuit

Propogate changes from inputs to outputs within the circuit

Returns true if anything in the circuit has changed (outputs or internal state)

The visitor pattern - allows a circuit to be probed by a Probe struct.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.