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.



4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
# File 'lib/HDLRuby/hruby_high.rb', line 4738

def to_value
    str = self.to_s
    return nil if str[0] != "_" # Bit string are prefixed by "_"
    # Remove the "_" not needed any longer.
    str = str[1..-1]
    # Get and check the type
    type = str[0]
    if type == "0" or type == "1" or type == "z" or type == "Z" then
        # Default binary
        type = "b"
    else
        # Not a default type
        str = str[1..-1]
    end
    return nil if str.empty?
    return nil unless ["b","u","s"].include?(type)
    # 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)


4731
4732
4733
# File 'lib/HDLRuby/hruby_high.rb', line 4731

def to_value?
    return true
end