Method: HDLRuby::High::Std#with_counter

Defined in:
lib/HDLRuby/std/counters.rb

#with_counter(init, rst = $rst, clk = $clk, &code) ⇒ Object

Sets a counter to +init+ when +rst+ is 1 that is decreased according to +clk+. +code+ will be applied on this counter. When not within a block, a behavior will be created which is activated on the rising edge of +clk+.



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
# File 'lib/HDLRuby/std/counters.rb', line 13

def with_counter(init, rst = $rst, clk = $clk, &code)
    # Are we in a block?
    if HDLRuby::High.top_user.is_a?(HDLRuby::High::SystemT) then
        # No, create a behavior.
        behavior(clk.posedge) do
            with_counter(init,rst,clk,&code)
        end
    else
        # Ensure init is a value.
        init = init.to_value
        # Creates the counter
        # counter = HDLRuby::High::SignalI.new(HDLRuby.uniq_name,
        #                           TypeVector.new(:"",bit,init.width),
        #                           :inner)
        # Create the name of the counter.
        name = HDLRuby.uniq_name
        # Declare the counter.
        [init.width].inner(name)
        # Get the signal of the counter.
        counter = HDLRuby::High.cur_block.get_inner(name)
        # Apply the code on the counter.
        # code.call(counter)
        instance_exec(counter,&code)
    end
end