Class: Symbol

Inherits:
Object
  • Object
show all
Defined in:
lib/HDLRuby/hruby_high.rb,
lib/HDLRuby/hruby_low2sym.rb

Overview

Extends the Symbol class with of equivalent HDLRuby object.

Constant Summary collapse

High =
HDLRuby::High

Instance Method Summary collapse

Instance Method Details

#to_hdrObject

Convert to the equivalent HDLRuby object if any, returns nil if not.



49
50
51
# File 'lib/HDLRuby/hruby_low2sym.rb', line 49

def to_hdr
    return Low2Symbol::Symbol2LowTable[self]
end

#to_valueObject Also known as: to_expr

Converts to a new value.

Returns nil if no value can be obtained from it.



5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
# File 'lib/HDLRuby/hruby_high.rb', line 5067

def to_value
    str = self.to_s
    return nil if str[0] != "_" # Bit string are prefixed by "_"
    # Get and check the type
    # type = str[0]
    type = str[1]
    if ["0", "1", "z", "Z", "o", "d", "h"].include?(type) then
        # Default binary
        type = "b"
    else
        # Not a default type
        # str = str[1..-1]
        str = str[2..-1]
    end
    return nil unless ["b","u","s"].include?(type)
    # Remove the "_"
    str = str.delete("_")
    return nil if str.empty?
    # Get the width if any.
    if str[0].match(/[0-9]/) then
        width = str.scan(/[0-9]*/)[0]
    else
        width = nil
    end
    # puts "width=#{width}"
    old_str = str # Save the string it this state since its first chars
                  # can be erroneously considered as giving the width
    str = str[width.size..-1] if width
    # Get the base and the value
    base = str[0]
    # puts "base=#{base}\n"
    unless ["b", "o", "d", "h"].include?(base) then
        # No base found, default is bit
        base = "b"
        # And the width was actually a part of the value.
        value = old_str
        width = nil
    else
        # Get the value.
        value = str[1..-1]
    end
    # puts "value=#{value}"
    # Compute the bit width and the value
    case base
    when "b" then
        # base 2, compute the width
        width = width ? width.to_i : value.size
        # Check the value
        return nil unless value.match(/^[0-1zxZX]+$/)
    when "o" then
        # base 8, compute the width
        width = width ? width.to_i : value.size * 3
        # Check the value
        if value.match(/^[0-7xXzZ]+$/) then
            # 4-state value, conpute the correspondig bit string.
            value = value.each_char.map do |c|
                c = c.upcase
                if c == "X" or c.upcase == "Z" then
                    c * 3
                else
                    c.to_i(8).to_s(2).rjust(3,"0")
                end
            end.join
        else
            # Invalid value
            return nil
        end
    when "d" then
        # base 10, compute the width 
        width = width ? width.to_i : value.to_i.to_s(2).size + 1
        # Check the value
        return nil unless value.match(/^[0-9]+$/)
        # Compute it (base 10 values cannot be 4-state!)
        value = value.to_i.to_s(2)
    when "h" then
        # base 16, compute the width
        width = width ? width.to_i : value.size * 4
        # Check the value
        if value.match(/^[0-9a-fA-FxXzZ]+$/) then
            # 4-state value, conpute the correspondig bit string.
            value = value.each_char.map do |c|
                c = c.upcase
                if c == "X" or c.upcase == "Z" then
                    c * 4
                else
                    c.to_i(16).to_s(2).rjust(4,"0")
                end
            end.join
        else
            # Invalid value
            return nil
        end
    else
        # Unknown base
        return nil
    end
    # Compute the type.
    case type
    when "b" then
        type = bit[width]
    when "u" then
        type = unsigned[width]
    when "s" then
        type = signed[width]
    else
        # Unknown type
        return nil
    end
    # puts "type.width=#{type.width}, value=#{value}"
    # Create and return the value.
    return Value.new(type,value)
end

#to_value?Boolean

Tell if the expression can be converted to a value.

Returns:

  • (Boolean)


5060
5061
5062
# File 'lib/HDLRuby/hruby_high.rb', line 5060

def to_value?
    return true
end