Class: HDLRuby::Low::Case

Inherits:
Statement show all
Defined in:
lib/HDLRuby/hruby_low.rb,
lib/HDLRuby/hruby_viz.rb,
lib/HDLRuby/hruby_low2c.rb,
lib/HDLRuby/hruby_low2hdr.rb,
lib/HDLRuby/hruby_low2seq.rb,
lib/HDLRuby/hruby_low2vhd.rb,
lib/HDLRuby/hruby_verilog.rb,
lib/HDLRuby/hruby_low2high.rb,
lib/HDLRuby/hruby_low_cleanup.rb,
lib/HDLRuby/hruby_low_mutable.rb,
lib/HDLRuby/hruby_low_skeleton.rb,
lib/HDLRuby/hruby_low_with_var.rb,
lib/HDLRuby/hruby_low_fix_types.rb,
lib/HDLRuby/hruby_low_bool2select.rb,
lib/HDLRuby/hruby_low_without_select.rb,
lib/HDLRuby/hruby_low_without_parinseq.rb,
lib/HDLRuby/hruby_low_without_namespace.rb,
lib/HDLRuby/hruby_low_without_subsignals.rb,
lib/HDLRuby/hruby_low_casts_without_expression.rb

Overview

Describes a case statement.

Direct Known Subclasses

High::Case

Constant Summary

Constants included from Low2Symbol

Low2Symbol::Low2SymbolPrefix, Low2Symbol::Low2SymbolTable, Low2Symbol::Symbol2LowTable

Instance Attribute Summary collapse

Attributes included from Hparent

#parent

Instance Method Summary collapse

Methods inherited from Statement

#add_blocks_code, #add_make_block, #behavior, #block, #break_types!, #parent_system, #scope, #top_block, #top_scope, #with_boolean!

Methods included from Low2Symbol

#to_sym

Methods included from Hparent

#absolute_ref, #hierarchy, #no_parent!, #scope

Constructor Details

#initialize(value, default = nil, whens = []) ⇒ Case

Creates a new case statement whose excution flow is decided from +value+ with a possible cases given in +whens+ and +default

  • (can be set later)


3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
# File 'lib/HDLRuby/hruby_low.rb', line 3868

def initialize(value, default = nil, whens = [])
    # Check and set the value.
    unless value.is_a?(Expression)
        raise AnyError, "Invalid class for a value: #{value.class}"
    end
    super()
    @value = value
    # And set its parent.
    value.parent = self
    # Checks and set the default case if any.
    self.default = default if default
    # Check and add the whens.
    @whens = []
    whens.each { |w| self.add_when(w) }
end

Instance Attribute Details

#defaultObject

The default block.



3863
3864
3865
# File 'lib/HDLRuby/hruby_low.rb', line 3863

def default
  @default
end

#valueObject (readonly)

The tested value



3860
3861
3862
# File 'lib/HDLRuby/hruby_low.rb', line 3860

def value
  @value
end

Instance Method Details

#add_when(w) ⇒ Object

Adds possible when case +w+.



3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
# File 'lib/HDLRuby/hruby_low.rb', line 3921

def add_when(w)
    # Check +w+.
    unless w.is_a?(When)
        raise AnyError, "Invalid class for a when: #{w.class}"
    end
    # Add it.
    @whens << w
    # And set the parent of +w+.
    w.parent = self
end

#blocks2seq!Object

Converts the par sub blocks to seq.



160
161
162
163
164
165
166
# File 'lib/HDLRuby/hruby_low2seq.rb', line 160

def blocks2seq!
    # Recurse on the whens.
    self.each_when(&:blocks2seq!)
    # Converts the default if any.
    self.default.blocks2seq! if self.default
    return self
end

#boolean_in_assign2select!Object

Converts booleans in assignments to select operators.



136
137
138
139
140
141
142
143
144
145
# File 'lib/HDLRuby/hruby_low_bool2select.rb', line 136

def boolean_in_assign2select!
    # No need to apply on the value!
    # # Apply on the value.
    # self.set_value!(self.value.boolean_in_assign2select)
    # Apply on the whens.
    self.each_when(&:boolean_in_assign2select!)
    # Apply on the default if any.
    self.default.boolean_in_assign2select! if self.default
    return self
end

#casts_without_expression!Object

Extracts the expressions from the casts.



120
121
122
123
124
125
126
127
128
129
# File 'lib/HDLRuby/hruby_low_casts_without_expression.rb', line 120

def casts_without_expression!
    # No need to apply on the value!
    # Apply on the value.
    self.set_value!(self.value.casts_without_expression!)
    # Apply on the whens.
    self.each_when(&:casts_without_expression!)
    # Apply on the default if any.
    self.default.casts_without_expression! if self.default
    return self
end

#cloneObject

Clones the Case (deeply)



4038
4039
4040
4041
4042
4043
4044
4045
# File 'lib/HDLRuby/hruby_low.rb', line 4038

def clone
    # Clone the default if any.
    default = @default ? @default.clone : nil
    # Clone the case.
    return Case.new(@value.clone,default,(@whens.map do |w|
        w.clone
    end) )
end

#delete_related!(*names) ⇒ Object

Deletes the elements related to one of +names+: either they have one of the names or they use an element with these names. NOTE: only delete actual instantiated elements, types or systemTs are left as is.



949
950
951
952
953
954
955
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 949

def delete_related!(*names)
    # Delete the whens whose match contains a signal whoses name is
    # in names.
    @whens.delete_if { |w| w.match.use_name?(*names) }
    # Recurse on the whens.
    @whens.each { |w| w.delete_related!(*names) }
end

#delete_unless!(keep) ⇒ Object

Removes the signals and corresponding assignments whose name is not in +keep+.



158
159
160
161
162
163
# File 'lib/HDLRuby/hruby_low_cleanup.rb', line 158

def delete_unless!(keep)
    # Recurse on the whens.
    self.each_when {|w| w.delete_unless!(keep) }
    # Recurse on the default if any.
    self.default.delete_unless!(keep) if self.default
end

#delete_when!(w) ⇒ Object

Delete a when.



903
904
905
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 903

def delete_when!(w)
    @whens.delete(w)
end

#each_block(&ruby_block) ⇒ Object

Iterates over the sub blocks.



3997
3998
3999
4000
4001
4002
4003
4004
4005
# File 'lib/HDLRuby/hruby_low.rb', line 3997

def each_block(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_block) unless ruby_block
    # A ruby block?
    # Apply it on each when's block.
    self.each_when { |w| w.each_block(&ruby_block) }
    # And apply it on the default if any.
    ruby_block.call(@default) if @default
end

#each_block_deep(&ruby_block) ⇒ Object

Iterates over all the blocks contained in the current block.



4008
4009
4010
4011
4012
4013
4014
4015
4016
# File 'lib/HDLRuby/hruby_low.rb', line 4008

def each_block_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_block_deep) unless ruby_block
    # A ruby block?
    # Apply it on each when's block.
    self.each_when { |w| w.each_block_deep(&ruby_block) }
    # And apply it on the default if any.
    @default.each_block_deep(&ruby_block) if @default
end

#each_deep(&ruby_block) ⇒ Object

Iterates over each object deeply.

Returns an enumerator if no ruby block is given.



3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
# File 'lib/HDLRuby/hruby_low.rb', line 3887

def each_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_deep) unless ruby_block
    # A ruby block? First apply it to current.
    ruby_block.call(self)
    # Then apply on the value.
    self.value.each_deep(&ruby_block)
    # Then apply on the whens.
    self.each_when do |w|
        w.each_deep(&ruby_block)
    end
end

#each_node(&ruby_block) ⇒ Object

Iterates over the children (including the value).

Returns an enumerator if no ruby block is given.



3975
3976
3977
3978
3979
3980
3981
3982
# File 'lib/HDLRuby/hruby_low.rb', line 3975

def each_node(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_node) unless ruby_block
    # A ruby block? Apply it on each child.
    ruby_block.call(@value)
    @whens.each(&ruby_block)
    ruby_block.call(@default) if @default
end

#each_node_deep(&ruby_block) ⇒ Object

Iterates over the nodes deeply if any.



3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
# File 'lib/HDLRuby/hruby_low.rb', line 3985

def each_node_deep(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_node_deep) unless ruby_block
    # A ruby block? First apply it to current.
    ruby_block.call(self)
    # And recurse on the children
    @value.each_node_deep(&ruby_block)
    @whens.each { |w| w.each_node_deep(&ruby_block) }
    @default.each_node_deep(&ruby_block) if @default
end

#each_statement(&ruby_block) ⇒ Object

Iterates over each sub statement if any.

Returns an enumerator if no ruby block is given.



3952
3953
3954
3955
3956
3957
3958
3959
3960
# File 'lib/HDLRuby/hruby_low.rb', line 3952

def each_statement(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_statement) unless ruby_block
    # A ruby block?
    # Apply on each when.
    @whens.each { |w| w.each_statement(&ruby_block) }
    # And on the default if any.
    ruby_block.call(@default) if @default
end

#each_statement_deep(&ruby_block) ⇒ Object

Iterates over all the statements contained in the current statement.



4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
# File 'lib/HDLRuby/hruby_low.rb', line 4019

def each_statement_deep(&ruby_block)
    # No ruby statement? Return an enumerator.
    return to_enum(:each_statement_deep) unless ruby_block
    # A ruby block?
    # Apply it on self.
    ruby_block.call(self)
    # And apply it on each when's statement.
    self.each_when { |w| w.each_statement_deep(&ruby_block) }
    # And apply it on the default if any.
    @default.each_statement_deep(&ruby_block) if @default
end

#each_when(&ruby_block) ⇒ Object

Iterates over the match cases.

Returns an enumerator if no ruby block is given.



3965
3966
3967
3968
3969
3970
# File 'lib/HDLRuby/hruby_low.rb', line 3965

def each_when(&ruby_block)
    # No ruby block? Return an enumerator.
    return to_enum(:each_when) unless ruby_block
    # A ruby block? Apply it on each when case.
    @whens.each(&ruby_block)
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
# File 'lib/HDLRuby/hruby_low.rb', line 3901

def eql?(obj)
    return false unless obj.is_a?(Case)
    return false unless @value.eql?(obj.value)
    return false unless @whens.eql?(obj.instance_variable_get(:@whens))
    idx = 0
    obj.each_when do |w|
        return false unless @whens[idx].eql?(w)
        idx += 1
    end
    return false unless idx == @whens.size
    return false unless @default.eql?(obj.default)
    return true
end

#explicit_types!Object

Explicit the types conversions in the case.



174
175
176
177
178
179
180
181
# File 'lib/HDLRuby/hruby_low_fix_types.rb', line 174

def explicit_types!
    # Recurse on the value.
    self.set_value!(self.value.explicit_types)
    # Recurse on the whens, the match of each when must be of the
    # type of the value.
    self.each_when { |w| w.explicit_types!(self.value.type) }
    return self
end

#extract_declares!Object

Extract the declares from the scope and returns them into an array.

NOTE: do not recurse into the sub scopes or behaviors!



722
723
724
725
726
727
728
729
730
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 722

def extract_declares!
    # # Recurse on the whens.
    # return self.each_when.map(&:extract_declares!)
    # # Recurse on the default if any.
    # self.default.extract_declares! if self.default
    res = self.each_when.map(&:extract_declares!)
    res += self.default.extract_declares! if self.default
    return res
end

#extract_selects!Object

Extract the Select expressions.

Note: the default is not treated.



236
237
238
239
240
241
242
243
# File 'lib/HDLRuby/hruby_low_without_select.rb', line 236

def extract_selects!
    selects = []
    # Work on the value.
    self.set_value!(self.value.extract_selects_to!(selects))
    # Work on the whens.
    selects += self.each_when.map(&:extract_selects!).reduce(:+)
    return selects
end

#fix_scope_refnames!(scopes) ⇒ Object

Fix the references names using scopes given in +scopes + list (they are marked to be deleted).



744
745
746
747
748
749
750
751
752
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 744

def fix_scope_refnames!(scopes)
    # Fix the value.
    self.set_value!(self.value.fix_scope_refnames!(scopes))
    # Recurse on the whens.
    self.each_when {|w| w.fix_scope_refnames!(scopes) }
    # Recurse on the default.
    self.default.fix_scope_refnames!(scopes) if self.default
    return self
end

#hashObject

Hash function.



3916
3917
3918
# File 'lib/HDLRuby/hruby_low.rb', line 3916

def hash
    return [@value,@whens,@default].hash
end

#map_nodes!(&ruby_block) ⇒ Object

Maps on the children (including the value).



908
909
910
911
912
913
914
915
916
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 908

def map_nodes!(&ruby_block)
    # A block? Apply it on each child.
    @value = ruby_block.call(@value)
    map_whens!(&ruby_block)
    if @default then
        @default = ruby_block.call(@default)
        @default.parent = self unless @default.parent
    end
end

#map_whens!(&ruby_block) ⇒ Object

Maps on the whens.



894
895
896
897
898
899
900
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 894

def map_whens!(&ruby_block)
    @whens.map! do |w|
        w = ruby_block.call(w)
        w.parent = self unless w.parent
        w
    end
end

#mix?(mode = nil) ⇒ Boolean

Tell if there is a mix block. +mode+ is the mode of the upper block.

Returns:

  • (Boolean)


170
171
172
173
174
175
# File 'lib/HDLRuby/hruby_low2seq.rb', line 170

def mix?(mode = nil)
    # Recuse on the whens.
    return true if self.each_when.any? { |w| w.mix?(mode) }
    # Check the default if any.
    return self.default.mix?(mode)
end

#par_in_seq2seq!Object

Converts par blocks within seq blocks to seq blocks.



90
91
92
93
94
95
# File 'lib/HDLRuby/hruby_low_without_parinseq.rb', line 90

def par_in_seq2seq!
    self.each_when do |w|
        w.statement.par_in_seq2seq!
    end
    self.default.par_in_seq2seq! if self.default
end

#replace_expressions!(node2rep) ⇒ Object

Replaces sub expressions using +node2rep+ table indicating the node to replace and the corresponding replacement. Returns the actually replaced nodes and their corresponding replacement.

NOTE: the replacement is duplicated.



924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 924

def replace_expressions!(node2rep)
    # First recurse on the children.
    res = {}
    self.each_node do |node|
        res.merge!(node.replace_expressions!(node2rep))
    end
    # Is there a replacement to do on the value?
    rep = node2rep[self.value]
    if rep then
        # Yes, do it.
        rep = rep.clone
        node = self.value
        # node.set_parent!(nil)
        self.set_value!(rep)
        # And register the replacement.
        res[node] = rep
    end

    return res
end

#replace_names!(former, nname) ⇒ Object

Replaces recursively +former+ name by +nname+ until it is redeclared.



733
734
735
736
737
738
739
740
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 733

def replace_names!(former,nname)
    # Recurse on the value.
    self.value.replace_names!(former,nname)
    # Recurse on the whens.
    self.each_when {|w| w.replace_names!(former,nname) }
    # Recurse on the default.
    self.default.replace_names!(former,nname) if self.default
end

#set_default!(default) ⇒ Object

Sets the default.



884
885
886
887
888
889
890
891
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 884

def set_default!(default)
    # Checks and set the default case if any.
    if self.default then
        # There is a default first detach it.
        @default = nil
    end
    self.default = default
end

#set_value!(value) ⇒ Object

Sets the value.



873
874
875
876
877
878
879
880
881
# File 'lib/HDLRuby/hruby_low_mutable.rb', line 873

def set_value!(value)
    # Check and set the value.
    unless value.is_a?(Expression)
        raise AnyError, "Invalid class for a value: #{value.class}"
    end
    @value = value
    # And set its parent.
    value.parent = self
end

#signal2subs!Object

Decompose the hierarchical signals in the statements.



213
214
215
216
217
218
219
220
221
# File 'lib/HDLRuby/hruby_low_without_subsignals.rb', line 213

def signal2subs!
    # Recurse on the case value.
    self.set_value!(self.value.signal2subs!)
    # Recurse on the whens.
    self.each_when(&:signal2subs!)
    # Recurse on the default.
    self.set_default!(self.default.signal2subs!) if self.default
    return self
end

#to_c(res, level = 0) ⇒ Object

Generates the text of the equivalent HDLRuby code.

+level+ is the hierachical level of the object.

def to_c(level = 0)

def to_c(res,level = 0) # res = "" # Compute the selection value. res << " res << " " * (level+1)*3 # res << "Value value = " << self.value.to_c(level+1) << ";\n" res << "Value value = " self.value.to_c(res,level+1) res << ";\n" # Ensure the selection value is testable. res << " " * (level+1)*3 res << "if (is_defined_value(value)) {\n" # The condition is testable. # Generate the case as a succession of if statements. first = true self.each_when do |w| res << " " * (level+2)*3 if first then first = false else res << "else " end res << "if (value2integer(value) == " # res << "value2integer(" << w.match.to_c(level+2) << ")) {\n" res << "value2integer(" w.match.to_c(res,level+2) res << ")) {\n" # res << w.statement.to_c(level+3) w.statement.to_c(res,level+3) res << " " * (level+2)*3 res << "\n" end if self.default then res << " " * (level+2)*3 res << "else # res << self.default.to_c(level+3) self.default.to_c(res,level+3) res << " " * (level+2)*3 res << "\n" end # Close the case. res << " " * (level+1)*3 res << "}\n" res << " " * (level)*3 res << "}\n" # Return the resulting string. return res end Generates the text of the equivalent HDLRuby code. +level+ is the hierachical level of the object. def to_c(level = 0)



1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
# File 'lib/HDLRuby/hruby_low2c.rb', line 1789

def to_c(res,level = 0)
    # Compute the selection value.
    res << "{\n"
    self.value.to_c(res,level+1)
    res << " " * ((level+1)*3)
    res << "dup();\n"
    # Ensure the selection value is testable.
    res << " " * ((level+1)*3)
    res << "if (is_defined()) {\n"
    # The condition is testable.
    # Generate the case as a succession of if statements.
    # self.each_when do |w|
    #     res << " " * ((level+2)*3)
    #     res << "dup();\n"
    #     res << " " * ((level+2)*3)
    #     w.match.to_c(res,level+2)
    #     res << "if (to_integer() == to_integer()) { \n"
    #     w.statement.to_c(res,level+3)
    #     res << " " * (level+2)*3
    #     res << "}\n"
    # end
    # if self.default then
    #     res << " " * (level+2)*3
    #     res << "else {\n"
    #     self.default.to_c(res,level+3)
    #     res << " " * (level+2)*3
    #     res << "}\n"
    # end
    res << " " * ((level+2)*3)
    res << "int val=to_integer(), done=0;\n"
    self.each_when.with_index do |w,i|
        res << " " * ((level+2)*3)
        res << "if (!done) {\n" unless i == 0
        res << " " * ((level+2)*3)
        w.match.to_c(res,level+2)
        res << "if (val == to_integer()) {\n"
        w.statement.to_c(res,level+3)
        res << " " * (level+3)*3
        res << "done = 1;\n"
        res << " " * (level+2)*3
        res << "}" unless i == 0
        res << "}\n"
    end
    if self.default then
        res << " " * (level+2)*3
        res << "if(!done) {\n"
        self.default.to_c(res,level+3)
        res << " " * (level+2)*3
        res << "}\n"
    end

    # Close the case.
    res << " " * (level+1)*3
    # res << "pop();\n" # Remove the testing value.
    res << " " * (level+1)*3
    res << "}\n"
    res << " " * (level)*3
    res << "}\n"
    # Return the resulting string.
    return res
end

#to_ch(res) ⇒ Object

Generates the content of the h file. def to_ch



1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
# File 'lib/HDLRuby/hruby_low2c.rb', line 1853

def to_ch(res)
    # res = ""
    # Recurse on the whens.
    # self.each_when {|w| res << w.to_ch }
    self.each_when {|w| w.to_ch(res) }
    # Recurse on the default statement.
    # res << self.default.to_ch if self.default
    self.default.to_ch(res) if self.default
    return res
end

#to_hdr(level = 0) ⇒ Object

Generates the text of the equivalent hdr text. +level+ is the hierachical level of the object.



409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
# File 'lib/HDLRuby/hruby_low2hdr.rb', line 409

def to_hdr(level = 0)
    # The result string.
    res = " " * (level*3)
    # Generate the test.
    res << "hcase " << self.value.to_hdr(level) << "\n"
    # Generate the whens.
    self.each_when do |w|
        res << w.to_hdr(level)
    end
    # Generatethe default.
    if self.default then
        res << " " * (level*3)
        res << "helse do\n"
        res << self.default.to_hdr(level+1)
        res << " " * (level*3)
        res << "end\n"
    end
    # Return the resulting string.
    return res
end

#to_highObject

Creates a new high case statement.



307
308
309
310
311
312
313
314
315
# File 'lib/HDLRuby/hruby_low2high.rb', line 307

def to_high
    # Is there a default?
    if self.default then
        # Yes, create the new case statement with it.
        return HDLRuby::High::Case.new(self.value.to_high,
                                       self.default.to_high,
                             self.each_when.map { |w| w.to_high })
    end
end

#to_seq!Object

Convert the block to seq.



98
99
100
101
102
103
# File 'lib/HDLRuby/hruby_low_without_parinseq.rb', line 98

def to_seq!
    self.each_when do |w|
        w.statement.to_seq!
    end
    self.default.to_seq! if self.default
end

#to_upper_space!Object

Moves the declarations to the upper namespace.



712
713
714
715
716
717
# File 'lib/HDLRuby/hruby_low_without_namespace.rb', line 712

def to_upper_space!
    # Recurse on the whens.
    self.each_when(&:to_upper_space!)
    # Recurse on the default if any.
    self.default.to_upper_space! if self.default
end

#to_verilog(spc = 3) ⇒ Object

Converts to Verilog code, checking if variables are register or wire adding 'spc' spaces at the begining of each line.



1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
# File 'lib/HDLRuby/hruby_verilog.rb', line 1737

def to_verilog(spc = 3)

    result = " " * spc # Indented based on space_count.

    result << "case(#{self.value.to_verilog})\n"

    # n the case statement, each branch is partitioned by when. Process each time when.
    self.each_when do |whens| 
        # Reads and stores the numbers and expressions stored in when.
        result << " " * (spc+3) + "#{whens.match.to_verilog}: "

        if whens.statement.each_statement.count >= 1 then
            result << whens.statement.to_verilog(spc+3) << "\n"
        else
            result << ";\n"
        end
    end
    if self.default then
        result << " " * (spc+3) + "default: "
        if self.default.each_statement.count >= 1 then
            result << "begin\n"
            result << self.default.each_statement.map do |stmnt|
                stmnt.to_verilog(spc+6)
            end.join("\n") << "\n"
            result << " " * (spc+3) + "end\n"
        else
            result << ";\n"
        end
    end
    result << " " * spc + "endcase\n" # Conclusion.

    return result               # Return case after translation.
end

#to_vhdl(vars, level = 0) ⇒ Object

Generates the text of the equivalent HDLRuby::High code. +vars+ is the list of the variables and +level+ is the hierachical level of the object.



1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
# File 'lib/HDLRuby/hruby_low2vhd.rb', line 1015

def to_vhdl(vars,level = 0)
    # The result string.
    res = " " * (level*3)
    # Generate the test.
    res << "case " << self.value.to_vhdl(level) << " is\n"
    # Generate the whens.
    self.each_when do |w|
        res << w.to_vhdl(vars,self.value.type,level)
    end
    # Generate teh default if any.
    if self.default then
        res << " " * (level*3)
        res << "when others =>\n"
        res << self.default.to_vhdl(vars,level+1)
    else
        # NOTE: some VHDL parsers are very picky about others,
        # even though all the cases have been treated through
        # "when" statements.
        res << " " * (level*3)
        res << "when others =>\n"
    end
    # Close the case.
    res << " " * (level*3)
    res << "end case;\n"
    # Return the resulting string.
    return res
end

#to_viz_node(parent) ⇒ Object

Converts the case to a Viz flow node under +parent+.



4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
# File 'lib/HDLRuby/hruby_viz.rb', line 4654

def to_viz_node(parent)
  # node = HDLRuby::Viz::Node.new(:case,parent)
  # # Generate the value.
  # self.value.to_viz_node(node)
  # # And generate the sub statments.
  # self.each_when do |w|
  #   sub = HDLRuby::Viz::Node.new(:when,node)
  #   w.match.to_viz_node(sub)
  #   w.statement.to_viz_node(sub)
  # end
  # self.default.to_viz_node(node) if self.default
  # return node
  node = parent
  # Generate one node per possible value.
  self.each_when do |w|
    sub = HDLRuby::Viz::Node.new(:case,node)
    self.value.to_viz_node(sub) # Readd the value to compare
    w.match.to_viz_node(sub)
    w.statement.to_viz_node(sub)
    node = sub if node == parent # Set the first node
  end
  self.default.to_viz_node(node) if self.default
  return node
end

#use_name?(*names) ⇒ Boolean

Tell if the statement includes a signal whose name is one of +names+. NOTE: for the case check only the value.

Returns:

  • (Boolean)


4033
4034
4035
# File 'lib/HDLRuby/hruby_low.rb', line 4033

def use_name?(*names)
    return @value.use_name?(*name)
end

#with_var(upper = nil) ⇒ Object

Converts to a variable-compatible case where +upper+ is the upper block if any.

NOTE: the result is a new case.



325
326
327
328
329
# File 'lib/HDLRuby/hruby_low_with_var.rb', line 325

def with_var(upper = nil)
    ndefault = self.default ? self.default.clone : nil
    return Case.new(self.value.clone,ndefault,
                    self.each_when.map {|w| w.with_var(upper) })
end