Method: HDLRuby::High::Std#make_clock

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

#make_clock(event, times) ⇒ Object

Create a clock inverted every +times+ occurence of an +event+.



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/HDLRuby/std/clocks.rb', line 15

def make_clock(event, times)
    clock = nil # The resulting clock

    # Enters the current system
    HDLRuby::High.cur_system.open do

        # Ensures times is a value.
        times = times.to_value - 1
        if (times == 0) then
          AnyError.new("Clock multiplier must be >= 2.") 
        end

        # Create the counter.
        # Create the name of the counter.
        name = HDLRuby.uniq_name
        # Declare the counter.
        if @@__clocks_rst then
            # There is a reset, so no need to initialize.
            [times.width].inner(name)
        else
            # There is no reset, so need to initialize.
          [times.width].inner(name => times)
        end
        # Get the signal of the counter.
        counter = get_inner(name)

        # Create the clock.
        # Create the name of the clock.
        name = HDLRuby.uniq_name
        # Declares the clock.
        if @@__clocks_rst then
            # There is a reset, so no need to initialize.
            bit.inner(name)
        else
            # There is no reset, so need to initialize.
            bit.inner(name => times)
        end
        # Get the signal of the clock.
        clock = get_inner(name)
        
        # Control it.
        par(event) do
            if @@__clocks_rst then
                # There is a reset, handle it.
                hif(@@__clocks_rst) do
                  counter <= times
                    clock <= 0
                end
                helsif(counter.to_expr == 0) do
                    counter <= times 
                    clock   <= ~ clock
                end
                helse do
                    counter <= counter - 1
                end
            else
                # There is no reset.
                hif(counter == 0) do
                    counter <= times 
                    clock   <= ~ clock
                end
                helse do
                    counter <= counter - 1
                end
            end
        end
    end
    return clock
end