Class: Numeric

Inherits:
Object show all
Defined in:
lib/origen/core_ext/numeric.rb

Overview

The base class of all numbers, i.e. integers and floats

Instance Method Summary collapse

Instance Method Details

#as_units(units) ⇒ Object

[View source]

56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/origen/core_ext/numeric.rb', line 56

def as_units(units)
  if abs >= 1_000_000_000_000_000
    "#{self / 1_000_000_000_000_000.0}P#{units}"
  elsif abs >= 1_000_000_000_000
    "#{self / 1_000_000_000_000.0}T#{units}"
  elsif abs >= 1_000_000_000
    "#{self / 1_000_000_000.0}G#{units}"
  elsif abs >= 1_000_000
    "#{self / 1_000_000.0}M#{units}"
  elsif abs >= 1_000
    "#{self / 1_000.0}k#{units}"
  elsif abs >= 1
    "#{self}#{units}"
  elsif abs >= 1e-3
    "#{self * 1_000}m#{units}"
  elsif abs >= 1e-6
    "#{self * 1_000_000}u#{units}"
  elsif abs >= 1e-9
    "#{self * 1_000_000_000}n#{units}"
  elsif abs >= 1e-12
    "#{self * 1_000_000_000_000}p#{units}"
  elsif abs >= 1e-15
    "#{self * 1_000_000_000_000_000}a#{units}"
  else
    "%.3e#{units}" % self
  end
end

#hi_z?Boolean

Returns:

  • (Boolean)
[View source]

10
11
12
# File 'lib/origen/core_ext/numeric.rb', line 10

def hi_z?;
  false;
end

#ms!Object

Shorthand for tester.wait(time_in_ms: 100), e.g. 100.ms!

[View source]

202
203
204
# File 'lib/origen/core_ext/numeric.rb', line 202

def ms!
  Origen.app.tester.wait time_in_ms: self
end

#ns!Object

Shorthand for tester.wait(time_in_ns: 100), e.g. 100.ns!

[View source]

192
193
194
# File 'lib/origen/core_ext/numeric.rb', line 192

def ns!
  Origen.app.tester.wait time_in_ns: self
end

#reverse_bits(width) ⇒ Object

Reverses the bit representation of a number and returns the new value. Useful when changing register data based on bit order

[View source]

48
49
50
51
52
53
54
# File 'lib/origen/core_ext/numeric.rb', line 48

def reverse_bits(width)
  result = 0
  0.upto(width - 1) do |i|
    result += self[i] * 2**(width - 1 - i)
  end
  result
end

#s!Object

Shorthand for tester.wait(time_in_s: 100), e.g. 100.s!

[View source]

207
208
209
# File 'lib/origen/core_ext/numeric.rb', line 207

def s!
  Origen.app.tester.wait time_in_s: self
end

#to_binObject

[View source]

34
35
36
# File 'lib/origen/core_ext/numeric.rb', line 34

def to_bin
  "0b#{to_s(2)}"
end

#to_bitstring(width) ⇒ Object

Converts a number to a String representing binary number Requires width of bit string for padding. If the width is less than the number of bits required to represent the number the width argument is meaningless.

[View source]

42
43
44
# File 'lib/origen/core_ext/numeric.rb', line 42

def to_bitstring(width)
  '%0*b' % [width, self]
end

#to_hexObject

[View source]

30
31
32
# File 'lib/origen/core_ext/numeric.rb', line 30

def to_hex
  "0x#{to_s(16).upcase}"
end

#undefined?Boolean

Returns:

  • (Boolean)
[View source]

18
19
20
# File 'lib/origen/core_ext/numeric.rb', line 18

def undefined?;
  false;
end

#us!Object

Shorthand for tester.wait(time_in_us: 100), e.g. 100.us!

[View source]

197
198
199
# File 'lib/origen/core_ext/numeric.rb', line 197

def us!
  Origen.app.tester.wait time_in_us: self
end

#x?Boolean

Returns:

  • (Boolean)
[View source]

14
15
16
# File 'lib/origen/core_ext/numeric.rb', line 14

def x?;
  false;
end

#x_or_z?Boolean

Returns:

  • (Boolean)
[View source]

22
23
24
# File 'lib/origen/core_ext/numeric.rb', line 22

def x_or_z?;
  false;
end

#z?Boolean

Helpers for cases where a method may return a 0, 1 or an instance of Origen::Value::X or Origen::Value::Z

Returns:

  • (Boolean)
[View source]

6
7
8
# File 'lib/origen/core_ext/numeric.rb', line 6

def z?;
  false;
end

#z_or_x?Boolean

Returns:

  • (Boolean)
[View source]

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

def z_or_x?;
  false;
end