Class: Origen::Value::BinStrVal

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

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, 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

Instance Attribute Details

#sizeObject (readonly)

Returns the value of attribute size



8
9
10
# File 'lib/origen/value/bin_str_val.rb', line 8

def size
  @size
end

#valObject (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

Returns:

  • (Boolean)


21
22
23
# File 'lib/origen/value/bin_str_val.rb', line 21

def numeric?
  !!(val =~ /^[01]+$/)
end

#to_iObject



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_sObject



31
32
33
# File 'lib/origen/value/bin_str_val.rb', line 31

def to_s
  "b#{val}"
end