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)



1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1229

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



1209
1210
1211
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1209

def base
  @base
end

#rangeObject (readonly)

The range of the vector.



1222
1223
1224
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1222

def range
  @range
end

Instance Method Details

#base?Boolean

Tells if the type has a base.

Returns:

  • (Boolean)


1217
1218
1219
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1217

def base?
  return true
end

#dirObject

Gets the direction of the range.



1306
1307
1308
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1306

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

#directionObject

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



1301
1302
1303
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1301

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.



1351
1352
1353
1354
1355
1356
1357
1358
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1351

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)


1252
1253
1254
1255
1256
1257
1258
1259
1260
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1252

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)


1334
1335
1336
1337
1338
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1334

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)


1321
1322
1323
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1321

def fixed?
  return @base.signed?
end

#float?Boolean

Tells if the type is floating point.

Returns:

  • (Boolean)


1326
1327
1328
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1326

def float?
  return @base.float?
end

#hashObject

Hash function.



1263
1264
1265
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1263

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

#maxObject

Gets the type max value if any.



1282
1283
1284
1285
1286
1287
1288
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1282

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.



1292
1293
1294
1295
1296
1297
1298
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1292

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)


1311
1312
1313
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1311

def signed?
  return @base.signed?
end

#sizeObject

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



1268
1269
1270
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1268

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

#to_cObject

Convert to C code.



1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1363

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.



1378
1379
1380
1381
1382
1383
1384
1385
1386
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1378

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.



1389
1390
1391
1392
1393
1394
1395
1396
1397
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1389

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

#unsigned?Boolean

Tells if the type is unsigned.

Returns:

  • (Boolean)


1316
1317
1318
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1316

def unsigned?
  return @base.unsigned?
end

#vector?Boolean

Tells if the type of of vector kind.

Returns:

  • (Boolean)


1212
1213
1214
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1212

def vector?
  return true
end

#widthObject

Gets the bitwidth of the type, nil for undefined.

NOTE: must be redefined for specific types.



1275
1276
1277
1278
1279
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1275

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