class Origen::Value::HexStrVal

Handles a value represented by a string of hex character(s) [0-9, a-f, x, X, z, Z]

This is

Capital hex numbers will be accepted when defining the value, but they will be converted to lower case

Attributes

size[R]
val[R]

Public Class Methods

new(value, options) click to toggle source
# File lib/origen/value/hex_str_val.rb, line 17
def initialize(value, options)
  @val = clean(value)
  if options[:size]
    @size = options[:size]
    # Trim any nibbles that are out of range...
    @val = val.split(//).last(size_in_nibbles).join
  else
    @size = (val.size * 4)
  end
end

Public Instance Methods

[](index) click to toggle source

Returns the value of the given bit. Return nil if out of range, otherwise 0, 1 or an X or Z object

# File lib/origen/value/hex_str_val.rb, line 44
def [](index)
  unless index > (size - 1)
    if numeric?
      to_i[index]
    else
      # Get the nibble in question and re-align the index, if the bit falls in a numeric
      # part of the string we can still resolve to an integer
      nibble = nibble_of(index)
      nibble = val[val.size - 1 - nibble]
      if nibble.downcase == 'x'
        X.new
      elsif nibble.downcase == 'z'
        Z.new
      else
        nibble.to_i[index % 4]
      end
    end
  end
end
numeric?() click to toggle source
# File lib/origen/value/hex_str_val.rb, line 28
def numeric?
  !!(val =~ /^[0-9a-f]+$/)
end
to_i() click to toggle source
# File lib/origen/value/hex_str_val.rb, line 32
def to_i
  if numeric?
    val.to_i(16) & size.bit_mask
  end
end
to_s() click to toggle source
# File lib/origen/value/hex_str_val.rb, line 38
def to_s
  "h#{val}"
end

Private Instance Methods

clean(val) click to toggle source
# File lib/origen/value/hex_str_val.rb, line 76
def clean(val)
  val = val.to_s.strip.to_s[1..-1]
  if valid?(val)
    if val =~ /[A-F]/
      val = val.gsub('A', 'a')
      val = val.gsub('B', 'b')
      val = val.gsub('C', 'c')
      val = val.gsub('D', 'd')
      val = val.gsub('E', 'e')
      val = val.gsub('F', 'f')
    end
    val.gsub('_', '')
  end
end
nibble_of(bit_number) click to toggle source
# File lib/origen/value/hex_str_val.rb, line 66
def nibble_of(bit_number)
  bit_number / 4
end
size_in_nibbles() click to toggle source

Rounds up to the nearest whole nibble

# File lib/origen/value/hex_str_val.rb, line 71
def size_in_nibbles
  adder = size % 4 == 0 ? 0 : 1
  (size / 4) + adder
end
valid?(val) click to toggle source
# File lib/origen/value/hex_str_val.rb, line 91
def valid?(val)
  if val =~ /^[0-9a-fA-F_xXzZ]+$/
    true
  else
    fail Origen::HexStrValError, "Hex string values can only contain: 0-9, a-f, A-F, _, x, X, z, Z, this is invalid: #{val}"
  end
end