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

Instance Method Summary collapse

Methods inherited from Type

#[], #binary, #comp_operator, #constant, #define_operator, #define_operator_with_context, #each_overload, #hierarchical?, #htype?, #inner, #leaf?, #left, #name=, #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)



1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1013

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

  # Check and set the base
  unless base.is_a?(Type)
    raise AnyError,
      "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



993
994
995
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 993

def base
  @base
end

#rangeObject (readonly)

The range of the vector.



1006
1007
1008
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1006

def range
  @range
end

Instance Method Details

#base?Boolean

Tells if the type has a base.

Returns:

  • (Boolean)


1001
1002
1003
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1001

def base?
  return true
end

#dirObject

Gets the direction of the range.



1091
1092
1093
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1091

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

#directionObject

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



1086
1087
1088
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1086

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.



1136
1137
1138
1139
1140
1141
1142
1143
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1136

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)


1037
1038
1039
1040
1041
1042
1043
1044
1045
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1037

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)


1119
1120
1121
1122
1123
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1119

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)


1106
1107
1108
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1106

def fixed?
  return @base.signed?
end

#float?Boolean

Tells if the type is floating point.

Returns:

  • (Boolean)


1111
1112
1113
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1111

def float?
  return @base.float?
end

#hashObject

Hash function.



1048
1049
1050
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1048

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

#maxObject

Gets the type max value if any.



1067
1068
1069
1070
1071
1072
1073
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1067

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.



1077
1078
1079
1080
1081
1082
1083
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1077

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)


1096
1097
1098
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1096

def signed?
  return @base.signed?
end

#sizeObject

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



1053
1054
1055
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1053

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

#unsigned?Boolean

Tells if the type is unsigned.

Returns:

  • (Boolean)


1101
1102
1103
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1101

def unsigned?
  return @base.unsigned?
end

#vector?Boolean

Tells if the type of of vector kind.

Returns:

  • (Boolean)


996
997
998
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 996

def vector?
  return true
end

#widthObject

Gets the bitwidth of the type, nil for undefined.

NOTE: must be redefined for specific types.



1060
1061
1062
1063
1064
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1060

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