Class: RubyHDL::High::TypeVector

Inherits:
Type
  • Object
show all
Defined in:
lib/HDLRuby/std/sequencer_sw.rb

Overview

Describes a vector type.

Direct Known Subclasses

TypeFloat, TypeSigned, TypeUnsigned

Instance Attribute Summary collapse

Attributes inherited from Type

#name

Instance Method Summary collapse

Methods inherited from Type

#[], #binary, #comp_operator, #constant, #define_operator, #define_operator_with_context, #each_overload, #hierarchical?, #htype?, #inner, #input, #leaf?, #left, #output, #range?, #register, #regular?, #right, #struct?, #to_type, #to_vector, #typedef, #types?, #unary

Methods included from HDLRuby::Tprocess

#&, #*, #+, #+@, #-@, #/, #<<, #==, #abs, #lr, #make, #resolve, #slice, #~

Constructor Details

#initialize(name, base, range) ⇒ TypeVector

Creates a new vector type named +name+ from +base+ type and with +range+. NOTE: if +range+ is a positive integer it is converted to (range-1)..0, if it is a negative integer it is converted to 0..(-range-1)



1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1617

def initialize(name,base,range)
  # Initialize the type.
  super(name)

  # Check and set the base
  unless base.is_a?(Type)
    raise "Invalid class for VectorType base: #{base.class}."
  end
  @base = base

  # Check and set the range.
  if range.respond_to?(:to_i) then
    # Integer case: convert to 0..(range-1).
    range = range > 0 ? (range-1)..0 : 0..(-range-1)
  elsif
    # Other cases: assume there is a first and a last to create
    # the range.
    range = range.first..range.last
  end
  @range = range
end

Instance Attribute Details

#baseObject (readonly)

The base type of the vector



1597
1598
1599
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1597

def base
  @base
end

#rangeObject (readonly)

The range of the vector.



1610
1611
1612
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1610

def range
  @range
end

Instance Method Details

#base?Boolean

Tells if the type has a base.

Returns:

  • (Boolean)


1605
1606
1607
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1605

def base?
  return true
end

#dirObject

Gets the direction of the range.



1694
1695
1696
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1694

def dir
  return (@range.last - @range.first)
end

#directionObject

Get the direction of the type, little or big endian.



1689
1690
1691
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1689

def direction
  return @range.first < @range.last ? :big : :little
end

#each_type_deep(&ruby_block) ⇒ Object Also known as: each_deep

Iterates over the types deeply if any.



1739
1740
1741
1742
1743
1744
1745
1746
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1739

def each_type_deep(&ruby_block)
  # No ruby block? Return an enumerator.
  return to_enum(:each_type_deep) unless ruby_block
  # A ruby block? First apply it to current.
  ruby_block.call(self)
  # And recurse on the base.
  @base.each_type_deep(&ruby_block)
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


1640
1641
1642
1643
1644
1645
1646
1647
1648
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1640

def eql?(obj)
  # # General type comparison.
  # return false unless super(obj)
  # Specific comparison.
  return false unless obj.is_a?(TypeVector)
  return false unless @base.eql?(obj.base)
  return false unless @range.eql?(obj.range)
  return true
end

#equivalent?(type) ⇒ Boolean

Tell if +type+ is equivalent to current type.

NOTE: type can be compatible while not being equivalent, please refer to hruby_types.rb for type compatibility.

Returns:

  • (Boolean)


1722
1723
1724
1725
1726
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1722

def equivalent?(type)
  return (type.is_a?(TypeVector) and
          @range == type.range
  @base.equivalent?(type.base) )
end

#fixed?Boolean

Tells if the type is fixed point.

Returns:

  • (Boolean)


1709
1710
1711
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1709

def fixed?
  return @base.signed?
end

#float?Boolean

Tells if the type is floating point.

Returns:

  • (Boolean)


1714
1715
1716
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1714

def float?
  return @base.float?
end

#hashObject

Hash function.



1651
1652
1653
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1651

def hash
  return [super,@base,@range].hash
end

#maxObject

Gets the type max value if any.



1670
1671
1672
1673
1674
1675
1676
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1670

def max
  if (self.signed?) then
    return (2**(self.width-1))-1
  else
    return (2**(self.width))-1
  end
end

#minObject

Gets the type min value if any. Default: not defined.



1680
1681
1682
1683
1684
1685
1686
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1680

def min
  if (self.signed?) then
    return -(2**(self.width-1))
  else
    return 0
  end
end

#signed?Boolean

Tells if the type signed.

Returns:

  • (Boolean)


1699
1700
1701
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1699

def signed?
  return @base.signed?
end

#sizeObject

Gets the size of the type in number of base elements.



1656
1657
1658
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1656

def size
  return (@range.first.to_i - @range.last.to_i).abs + 1
end

#to_cObject

Convert to C code.



1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1751

def to_c
  if @base.is_a?(TypeVector) then
    # Array type case.
    return @base.to_c + "[#{self.size.to_i}]"
  else
    # Simple vector type case.
    if @base.float? then
      return @base.to_c
    else
      return @base.to_c + " long long"
    end
  end
end

#to_c_initObject

Convert to C initialization code.



1766
1767
1768
1769
1770
1771
1772
1773
1774
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1766

def to_c_init
  if @base.is_a?(TypeVector) then
    # Array type case.
    base_init = @base.to_c_init
    return "{" + ([base_init] * self.size.to_i).join(",") + "}"
  else
    return "0"
  end
end

#to_python_initObject

Convert to Python initialization code.



1777
1778
1779
1780
1781
1782
1783
1784
1785
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1777

def to_python_init
  if @base.is_a?(TypeVector) then
    # Array type case.
    base_init = @base.to_python_init
    return "[" + ([base_init] * self.size.to_i).join(",") + "]"
  else
    return "0"
  end
end

#to_tf_initObject

Convert to tensorflow initialization code.



1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1788

def to_tf_init
  if @base.is_a?(TypeVector) then
    # Array type case.
    base_init = @base.to_python_init
    return "tf.constant([" + 
      ([base_init] * self.size.to_i).join(",") + "])"
  else
    return "tf.constant(0)"
  end
end

#unsigned?Boolean

Tells if the type is unsigned.

Returns:

  • (Boolean)


1704
1705
1706
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1704

def unsigned?
  return @base.unsigned?
end

#vector?Boolean

Tells if the type of of vector kind.

Returns:

  • (Boolean)


1600
1601
1602
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1600

def vector?
  return true
end

#widthObject

Gets the bitwidth of the type, nil for undefined.

NOTE: must be redefined for specific types.



1663
1664
1665
1666
1667
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1663

def width
  first = @range.first.to_i
  last  = @range.last.to_i
  return @base.width * ((first-last).abs + 1)
end