Class: Origen::Value::BinStrVal
- Defined in:
- lib/origen/value/bin_str_val.rb
Overview
Handles a value represented by a string of bin character(s) [0, 1, x, z]
Capital X/Z will be accepted when defining the value, but they will be converted to lower case
Instance Attribute Summary collapse
-
#size ⇒ Object
readonly
Returns the value of attribute size.
-
#val ⇒ Object
readonly
Returns the value of attribute val.
Instance Method Summary collapse
-
#[](index) ⇒ Object
Returns the value of the given bit.
-
#initialize(value, options) ⇒ BinStrVal
constructor
A new instance of BinStrVal.
- #numeric? ⇒ Boolean
- #to_i ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(value, options) ⇒ BinStrVal
Returns a new instance of BinStrVal.
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/origen/value/bin_str_val.rb', line 10 def initialize(value, ) @val = clean(value) if [:size] @size = [:size] # Trim any bits that are out of range... @val = val.split(//).last(size).join else @size = val.size end end |
Instance Attribute Details
#size ⇒ Object (readonly)
Returns the value of attribute size.
8 9 10 |
# File 'lib/origen/value/bin_str_val.rb', line 8 def size @size end |
#val ⇒ Object (readonly)
Returns the value of attribute val.
8 9 10 |
# File 'lib/origen/value/bin_str_val.rb', line 8 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
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/origen/value/bin_str_val.rb', line 37 def [](index) unless index > (size - 1) if numeric? to_i[index] else char = val[val.size - 1 - index] if char == 'x' X.new elsif char == 'z' Z.new else char.to_i end end end end |
#numeric? ⇒ Boolean
21 22 23 |
# File 'lib/origen/value/bin_str_val.rb', line 21 def numeric? !!(val =~ /^[01]+$/) end |
#to_i ⇒ Object
25 26 27 28 29 |
# File 'lib/origen/value/bin_str_val.rb', line 25 def to_i if numeric? val.to_i(2) & size.bit_mask end end |
#to_s ⇒ Object
31 32 33 |
# File 'lib/origen/value/bin_str_val.rb', line 31 def to_s "b#{val}" end |