Class: Origen::Value::HexStrVal

Inherits:
Object
  • Object
show all
Defined in:
lib/origen/value/hex_str_val.rb

Overview

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

This is

  • x when all the bits in a nibble are x

  • X when some of the bits in a nibble are x, though the exact bit-level values are not known

  • z when all the bits in a nibble are z

  • Z when some of the bits in a nibble are z, though the exact bit-level values are not known

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, options) ⇒ HexStrVal

Returns a new instance of HexStrVal.



17
18
19
20
21
22
23
24
25
26
# 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

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size.



15
16
17
# File 'lib/origen/value/hex_str_val.rb', line 15

def size
  @size
end

#valObject (readonly)

Returns the value of attribute val.



15
16
17
# File 'lib/origen/value/hex_str_val.rb', line 15

def val
  @val
end

Instance Method Details

#[](index) ⇒ Object

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# 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?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'lib/origen/value/hex_str_val.rb', line 28

def numeric?
  !!(val =~ /^[0-9a-f]+$/)
end

#to_iObject



32
33
34
35
36
# 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_sObject



38
39
40
# File 'lib/origen/value/hex_str_val.rb', line 38

def to_s
  "h#{val}"
end