class Origen::Value::BinStrVal
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
Attributes
Public Class Methods
Source
# File lib/origen/value/bin_str_val.rb, line 10 def initialize(value, options) @val = clean(value) if options[:size] @size = options[:size] # Trim any bits that are out of range... @val = val.split(//).last(size).join else @size = val.size end end
Public Instance Methods
Source
# 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
Returns the value of the given bit. Return nil if out of range, otherwise 0, 1 or an X
or Z
object
Source
# File lib/origen/value/bin_str_val.rb, line 21 def numeric? !!(val =~ /^[01]+$/) end
Source
# File lib/origen/value/bin_str_val.rb, line 25 def to_i if numeric? val.to_i(2) & size.bit_mask end end
Private Instance Methods
Source
# File lib/origen/value/bin_str_val.rb, line 56 def clean(val) val = val.to_s.strip.to_s[1..-1] if valid?(val) val.gsub('_', '').downcase end end
Source
# File lib/origen/value/bin_str_val.rb, line 63 def valid?(val) if val =~ /^[01_xXzZ]+$/ true else fail Origen::BinStrValError, "Binary string values can only contain: 0, 1, _, x, X, z, Z, this is invalid: #{val}" end end