Class: Integer

Inherits:
Object show all
Includes:
Origen::IntegerExtension
Defined in:
lib/origen/core_ext/integer.rb

Class Attribute Summary collapse

Instance Method Summary collapse

Methods included from Origen::IntegerExtension

#[]

Class Attribute Details

.widthObject

Returns the value of attribute width.



26
27
28
# File 'lib/origen/core_ext/integer.rb', line 26

def width
  @width
end

Instance Method Details

#cyclesObject Also known as: cycle

Implements 10.cycles



35
36
37
38
39
40
41
42
43
44
# File 'lib/origen/core_ext/integer.rb', line 35

def cycles
  if block_given?
    times do
      yield
      Origen.app.tester.cycle
    end
  else
    Origen.app.tester.cycle(repeat: self)
  end
end

#ones_comp(num_bits) ⇒ Object Also known as: ones_complement, ones_compliment



47
48
49
# File 'lib/origen/core_ext/integer.rb', line 47

def ones_comp(num_bits)
  self ^ ((1 << num_bits) - 1)
end

#to_bit_maskObject Also known as: bit_mask

Returns a bit mask for the given number of bits:

4.to_bit_mask  # => 0x1111


56
57
58
# File 'lib/origen/core_ext/integer.rb', line 56

def to_bit_mask
  (1 << self) - 1
end

#to_boolObject



61
62
63
64
65
66
67
# File 'lib/origen/core_ext/integer.rb', line 61

def to_bool
  if self == 1
    true
  elsif self == 0
    false
  end
end

#to_spreadsheet_columnObject Also known as: to_xls_column, to_xlsx_column, to_xls_col, to_xlsx_col, to_spreadsheet_col



69
70
71
72
# File 'lib/origen/core_ext/integer.rb', line 69

def to_spreadsheet_column
  index_hash = Hash.new { |hash, key| hash[key] = hash[key - 1].next }.merge(0 => 'A')
  index_hash[self]
end

#twos_complement(width = nil) ⇒ Object Also known as: twos_comp, twos_compliment



79
80
81
82
83
84
85
86
87
88
# File 'lib/origen/core_ext/integer.rb', line 79

def twos_complement(width = nil)
  _width = width || Integer.width
  if self > 2**(_width - 1) - 1
    fail(RangeError, "Integer #{self} cannot fit into #{_width} bits with 2s complement encoding")
  elsif self < -1 * (2**(_width - 1))
    fail(RangeError, "Integer #{self} cannot fit into #{_width} bits with 2s complement encoding")
  end

  self < 0 ? ((-1 * self) ^ (2**_width - 1)) + 1 : self
end