Class: Origen::Value

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

Overview

This class wraps various different class which handle number representation in various formats.

The user should never instantiate those directly and should always instantiate an Origen::Value instance, thereby ensuring a common API regardless of the internal representation and handling of the value

Defined Under Namespace

Classes: BinStrVal, HexStrVal, X, Z

Instance Method Summary collapse

Constructor Details

#initialize(val, options = {}) ⇒ Value

Returns a new instance of Value.



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/origen/value.rb', line 48

def initialize(val, options = {})
  if val.is_a?(Integer)
    @val = val
  else
    val = val.to_s
    case val[0].downcase
    when 'b'
      @val = BinStrVal.new(val, options)
    when 'h'
      @val = HexStrVal.new(val, options)
    when 'd'
      @val = val.to_s[1..-1].to_i
    else
      if val =~ /^[0-9]+$/
        @val = val.to_i
      else
        fail 'Unsupported value syntax'
      end
    end
  end
end

Instance Method Details

#[](index) ⇒ Object



106
107
108
109
110
111
112
# File 'lib/origen/value.rb', line 106

def [](index)
  if index.is_a?(Range)
    fail 'Currently, only single bit extraction from a Value object is supported'
  end

  val[index]
end

#bin_str_val?Boolean Also known as: bin_str_value?

Returns:

  • (Boolean)


101
102
103
# File 'lib/origen/value.rb', line 101

def bin_str_val?
  val.is_a?(BinStrVal)
end

#hex_str_val?Boolean Also known as: hex_str_value?

Returns:

  • (Boolean)


96
97
98
# File 'lib/origen/value.rb', line 96

def hex_str_val?
  val.is_a?(HexStrVal)
end

#numeric?Boolean

Returns true if all bits have a numeric value - i.e. no X or Z

Returns:

  • (Boolean)


71
72
73
# File 'lib/origen/value.rb', line 71

def numeric?
  val.numeric?
end

#sizeObject Also known as: bits, number_of_bits

Returns the size of the value in bits



90
91
92
# File 'lib/origen/value.rb', line 90

def size
  val.size
end

#to_iObject

Converts to an integer, returns nil if the value contains non-numeric bits



80
81
82
# File 'lib/origen/value.rb', line 80

def to_i
  val.to_i
end

#to_sObject

Converts to a string, the format of it depends on the underlying value type



85
86
87
# File 'lib/origen/value.rb', line 85

def to_s
  val.to_s
end

#value?Boolean

Returns:

  • (Boolean)


75
76
77
# File 'lib/origen/value.rb', line 75

def value?
  true
end