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.
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.
-
#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, #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)
1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1118 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
#base ⇒ Object (readonly)
The base type of the vector
1098 1099 1100 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1098 def base @base end |
#range ⇒ Object (readonly)
The range of the vector.
1111 1112 1113 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1111 def range @range end |
Instance Method Details
#base? ⇒ Boolean
Tells if the type has a base.
1106 1107 1108 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1106 def base? return true end |
#dir ⇒ Object
Gets the direction of the range.
1196 1197 1198 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1196 def dir return (@range.last - @range.first) end |
#direction ⇒ Object
Get the direction of the type, little or big endian.
1191 1192 1193 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1191 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.
1241 1242 1243 1244 1245 1246 1247 1248 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1241 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.
1142 1143 1144 1145 1146 1147 1148 1149 1150 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1142 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.
1224 1225 1226 1227 1228 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1224 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.
1211 1212 1213 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1211 def fixed? return @base.signed? end |
#float? ⇒ Boolean
Tells if the type is floating point.
1216 1217 1218 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1216 def float? return @base.float? end |
#hash ⇒ Object
Hash function.
1153 1154 1155 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1153 def hash return [super,@base,@range].hash end |
#max ⇒ Object
Gets the type max value if any.
1172 1173 1174 1175 1176 1177 1178 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1172 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.
1182 1183 1184 1185 1186 1187 1188 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1182 def min if (self.signed?) then return -(2**(self.width-1)) else return 0 end end |
#signed? ⇒ Boolean
Tells if the type signed.
1201 1202 1203 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1201 def signed? return @base.signed? end |
#size ⇒ Object
Gets the size of the type in number of base elements.
1158 1159 1160 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1158 def size return (@range.first.to_i - @range.last.to_i).abs + 1 end |
#to_c ⇒ Object
Convert to C code.
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1253 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 float? then return @base.to_c else return @base + " long long" end end end |
#to_c_init ⇒ Object
Convert to C initialization code.
1268 1269 1270 1271 1272 1273 1274 1275 1276 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1268 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 |
#unsigned? ⇒ Boolean
Tells if the type is unsigned.
1206 1207 1208 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1206 def unsigned? return @base.unsigned? end |
#vector? ⇒ Boolean
Tells if the type of of vector kind.
1101 1102 1103 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1101 def vector? return true end |
#width ⇒ Object
Gets the bitwidth of the type, nil for undefined.
NOTE: must be redefined for specific types.
1165 1166 1167 1168 1169 |
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1165 def width first = @range.first.to_i last = @range.last.to_i return @base.width * ((first-last).abs + 1) end |