class HDLRuby::Low::TypeVector
Describes a vector data type.
Describes a vector type.
Extends the TypeVector
class with generation of C text.
Extends the TypeVector
class with generation of hdr text.
Add the conversion to high.
Extends the TypeVector
class with generation of HDLRuby::High
text.
Describes a vector type.
Extends the TypeVector
class with functionality for breaking hierarchical types.
Used to indicate the number of bits. Enhance TypeVector
with generation of verilog code.
Attributes
The base type of the vector
The range of the vector.
Public Class Methods
Creates a new type vector named name
from a base
type and with range
# File lib/HDLRuby/hruby_db.rb, line 137 def initialize(name,base,range) # Ensure base si a HDLRuby::Low type. base = Type.get(base) # Create the type. super(name,base,range) end
Public Instance Methods
Tells if the type has a base.
# File lib/HDLRuby/hruby_low.rb, line 1567 def base? return true end
Breaks the hierarchical types into sequences of type definitions. Assumes to_upper_space! has been called before. types
include the resulting types.
# File lib/HDLRuby/hruby_low_without_namespace.rb, line 297 def break_types!(types) if self.base.is_a?(TypeVector) || self.base.is_a?(TypeTuple) || self.base.is_a?(TypeStruct) then # Need to break # First recurse on the base. nbase = self.base.break_types!(types) # # Maybe such a type already exists. # ndef = types[nbase] # if ndef then # # Yes, use it. # self.set_base!(ndef.clone) # else # # No change it to a type definition # ndef = TypeDef.new(HDLRuby.uniq_name,nbase) # self.set_base!(ndef) # # And add it to the types by structure. # types[nbase] = ndef # end # Sets the base. self.set_base!(nbase) # And create a new type from current type. # Maybe the new type already exists. ndef = types[self] return ndef if ndef # Yes, already exists. # No, create and register a new typedef. ndef = TypeDef.new(HDLRuby.uniq_name,self) types[self] = ndef return ndef end return self end
Gets the direction of the range.
# File lib/HDLRuby/hruby_low.rb, line 1657 def dir return (@range.last - @range.first) end
Get the direction of the type, little or big endian.
# File lib/HDLRuby/hruby_low.rb, line 1652 def direction return @range.first < @range.last ? :big : :little end
Iterates over the types deeply if any.
# File lib/HDLRuby/hruby_low.rb, line 1702 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
Comparison for hash: structural comparison.
# File lib/HDLRuby/hruby_low.rb, line 1603 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
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.
# File lib/HDLRuby/hruby_low.rb, line 1685 def equivalent?(type) return (type.is_a?(TypeVector) and @range == type.range @base.equivalent?(type.base) ) end
Tells if the type is fixed point.
# File lib/HDLRuby/hruby_low.rb, line 1672 def fixed? return @base.signed? end
Tells if the type is floating point.
# File lib/HDLRuby/hruby_low.rb, line 1677 def float? return @base.float? end
Hash
function.
# File lib/HDLRuby/hruby_low.rb, line 1614 def hash return [super,@base,@range].hash end
Gets the type max value if any.
# File lib/HDLRuby/hruby_low.rb, line 1633 def max if (self.signed?) then return (2**(self.width-1))-1 else return (2**(self.width))-1 end end
Gets the type min value if any. Default: not defined.
# File lib/HDLRuby/hruby_low.rb, line 1643 def min if (self.signed?) then return -(2**(self.width-1)) else return 0 end end
Sets the base
type.
# File lib/HDLRuby/hruby_low_mutable.rb, line 299 def set_base!(type) # Check and set the base unless type.is_a?(Type) raise AnyError, "Invalid class for VectorType base: #{base.class}." end @base = type end
Sets the range
.
# File lib/HDLRuby/hruby_low_mutable.rb, line 309 def set_range!(ranage) # Check and set the range. if range.respond_to?(:to_i) then # Integer case: convert to 0..(range-1). range = (range-1)..0 elsif # Other cases: assume there is a first and a last to create # the range. range = range.first..range.last end @range = range end
Tells if the type signed.
# File lib/HDLRuby/hruby_low.rb, line 1662 def signed? return @base.signed? end
Gets the size of the type in number of base elements.
# File lib/HDLRuby/hruby_low.rb, line 1619 def size return (@range.first.to_i - @range.last.to_i).abs + 1 end
Generates the C text of the equivalent HDLRuby
code. level
is the hierachical level of the object.
# File lib/HDLRuby/hruby_low2c.rb, line 544 def to_c(level = 0) # The resulting string. return "get_type_vector(#{self.base.to_c(level+1)}," + "#{self.size})" end
Generates the text of the equivalent hdr text. level
is the hierachical level of the object.
# File lib/HDLRuby/hruby_low2hdr.rb, line 195 def to_hdr(level = 0) # The resulting string. res = "" # Generate the base. res << self.base.to_hdr(level) # Generate the range. res << "[" << self.range.first.to_hdr(level) << ".." << self.range.last.to_hdr(level) << "]" # Return the result. return res end
Creates a new high type vector.
# File lib/HDLRuby/hruby_low2high.rb, line 79 def to_high return HDLRuby::High::TypeVector.new(self.name, self.base.to_high, self.range) end
Converts the system to Verilog
code.
# File lib/HDLRuby/hruby_verilog.rb, line 1406 def to_verilog # if self.base.name.to_s != "bit" if VERILOG_BASE_TYPES.include?(self.base.name.to_s) return " #{self.base.name.to_s}[#{self.range.first}:#{self.range.last}]" end return " [#{self.range.first}:#{self.range.last}]" end
Generates the text of the equivalent HDLRuby::High
code. level
is the hierachical level of the object.
# File lib/HDLRuby/hruby_low2vhd.rb, line 652 def to_vhdl(level = 0) # The resulting string. res = "" # Depending on the base. if self.base.class < Type then # The base is not a leaf, therefore the type is a VHDL array. # NOTE: array are always valid if used in type definition, # it is assumed that break_types! from # hruby_low_without_namespace.rb is used. res << "array (" res << self.range.first.to_vhdl(level) if self.range.first >= self.range.last then res << " downto " else res << " to " end res << self.range.last.to_vhdl(level) res << ") of " # Now generate the base. res << base.to_vhdl(level+1) else # The base is a leaf, therefore the type is VHDL vector. # Depending on the base name. case(base.name) when :bit # std_logic_vector. res << "std_logic_vector" when :signed res << "signed" when :unsigned res << "unsigned" else res << Low2VHDL.vhdl_name(self.base.name) end # Now the range res << "(" res << self.range.first.to_vhdl(level) left = self.range.first right = self.range.last left = left.content if left.is_a?(Value) right = right.content if right.is_a?(Value) if left >= right then res << " downto " else res << " to " end res << self.range.last.to_vhdl(level) res << ")" end # Return the result. return res end
Tells if the type is unsigned.
# File lib/HDLRuby/hruby_low.rb, line 1667 def unsigned? return @base.unsigned? end
Tells if the type of of vector kind.
# File lib/HDLRuby/hruby_low.rb, line 1562 def vector? return true end
Gets the bitwidth of the type, nil for undefined.
NOTE: must be redefined for specific types.
# File lib/HDLRuby/hruby_low.rb, line 1626 def width first = @range.first.to_i last = @range.last.to_i return @base.width * ((first-last).abs + 1) end