Class: RubyHDL::High::TypeVector
- Defined in:
- lib/HDLRuby/std/sequencer_sw.rb
Overview
Describes a vector type.
Direct Known Subclasses
Instance Attribute Summary collapse
-
#base ⇒ Object
readonly
The base type of the vector.
-
#range ⇒ Object
readonly
The range of the vector.
Attributes inherited from Type
Instance Method Summary collapse
-
#base? ⇒ Boolean
Tells if the type has a base.
-
#dir ⇒ Object
Gets the direction of the range.
-
#direction ⇒ Object
Get the direction of the type, little or big endian.
-
#each_type_deep(&ruby_block) ⇒ Object
(also: #each_deep)
Iterates over the types deeply if any.
-
#eql?(obj) ⇒ Boolean
Comparison for hash: structural comparison.
-
#equivalent?(type) ⇒ Boolean
Tell if +type+ is equivalent to current type.
-
#fixed? ⇒ Boolean
Tells if the type is fixed point.
-
#float? ⇒ Boolean
Tells if the type is floating point.
-
#hash ⇒ Object
Hash function.
-
#initialize(name, base, range) ⇒ TypeVector
constructor
Creates a new vector type named +name+ from +base+ type and with +range+.
-
#max ⇒ Object
Gets the type max value if any.
-
#min ⇒ Object
Gets the type min value if any.
-
#signed? ⇒ Boolean
Tells if the type signed.
-
#size ⇒ Object
Gets the size of the type in number of base elements.
-
#to_c ⇒ Object
Convert to C code.
-
#to_c_init ⇒ Object
Convert to C initialization code.
-
#to_python_init ⇒ Object
Convert to Python initialization code.
-
#to_tf_init ⇒ Object
Convert to tensorflow initialization code.
-
#unsigned? ⇒ Boolean
Tells if the type is unsigned.
-
#vector? ⇒ Boolean
Tells if the type of of vector kind.
-
#width ⇒ Object
Gets the bitwidth of the type, nil for undefined.
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
#base ⇒ Object (readonly)
The base type of the vector
1597 1598 1599 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1597 def base @base end |
#range ⇒ Object (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.
1605 1606 1607 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1605 def base? return true end |
#dir ⇒ Object
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 |
#direction ⇒ Object
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.
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.
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.
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.
1714 1715 1716 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1714 def float? return @base.float? end |
#hash ⇒ Object
Hash function.
1651 1652 1653 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1651 def hash return [super,@base,@range].hash end |
#max ⇒ Object
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 |
#min ⇒ Object
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.
1699 1700 1701 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1699 def signed? return @base.signed? end |
#size ⇒ Object
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_c ⇒ Object
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_init ⇒ Object
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_init ⇒ Object
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_init ⇒ Object
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.
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.
1600 1601 1602 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1600 def vector? return true end |
#width ⇒ Object
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 |