Class: RubyHDL::High::TypeTuple

Inherits:
Type
  • Object
show all
Defined in:
lib/HDLRuby/std/sequencer_sw.rb

Overview

Describes a tuple type.

Instance Method Summary collapse

Methods inherited from Type

#[], #binary, #comp_operator, #constant, #define_operator, #define_operator_with_context, #each_overload, #fixed?, #float?, #hierarchical?, #htype?, #inner, #leaf?, #left, #max, #min, #name=, #range?, #register, #right, #signed?, #struct?, #to_type, #to_vector, #typedef, #unary, #unsigned?, #vector?

Methods included from HDLRuby::Tprocess

#&, #*, #+, #+@, #-@, #/, #<<, #==, #abs, #lr, #make, #resolve, #slice, #~

Constructor Details

#initialize(name, direction, *content) ⇒ TypeTuple

Creates a new tuple type named +name+ width +direction+ and whose sub types are given by +content+.



1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1200

def initialize(name,direction,*content)
  # Initialize the type.
  super(name)

  # Set the direction.
  @direction = direction.to_sym
  unless [:little, :big].include?(@direction)
    raise AnyError, "Invalid direction for a type: #{direction}"
  end

  # Check and set the content.
  content.each do |sub|
    unless sub.is_a?(Type) then
      raise AnyError, "Invalid class for a type: #{sub.class}"
    end
  end
  @types = content
end

Instance Method Details

#add_type(type) ⇒ Object

Adds a sub +type+.



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

def add_type(type)
  unless type.is_a?(Type) then
    raise AnyError, 
      "Invalid class for a type: #{type.class} (#{type})"
  end
  @types << type
end

#baseObject

Gets the base type.

NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)



1342
1343
1344
1345
1346
1347
1348
1349
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1342

def base
  if regular? then
    # Regular tuple, return the type of its first element.
    return @types[0]
  else
    raise AnyError, "No base type for type #{self}"
  end
end

#base?Boolean

Tells if the type has a base.

NOTE: only if the tuple is regular (i.e., all its sub types are identical)

Returns:

  • (Boolean)


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

def base?
  return regular?
end

#directionObject

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



1313
1314
1315
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1313

def direction
  return @direction
end

#each(&ruby_block) ⇒ Object

Iterates over the sub name/type pair.

Returns an enumerator if no ruby block is given.



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

def each(&ruby_block)
  # No ruby block? Return an enumerator.
  return to_enum(:each) unless ruby_block
  # A ruby block? Apply it on each sub name/type pair.
  @types.each(&ruby_block)
end

#each_type(&ruby_block) ⇒ Object

Iterates over the sub types.

Returns an enumerator if no ruby block is given.



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

def each_type(&ruby_block)
  # No ruby block? Return an enumerator.
  return to_enum(:each_type) unless ruby_block
  # A ruby block? Apply it on each sub type.
  @types.each(&ruby_block)
end

#each_type_deep(&ruby_block) ⇒ Object Also known as: each_deep

Iterates over the types deeply if any.



1284
1285
1286
1287
1288
1289
1290
1291
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1284

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 sub types.
  @types.each { |type| type.each_type_deep(&ruby_block) }
end

#eql?(obj) ⇒ Boolean

Comparison for hash: structural comparison.

Returns:

  • (Boolean)


1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1220

def eql?(obj)
  # # General type comparison.
  # return false unless super(obj)
  return false unless obj.is_a?(TypeTuple)
  # Specific comparison.
  idx = 0
  obj.each_type do |type|
    return false unless @types[idx].eql?(type)
    idx += 1
  end
  return false unless idx == @types.size
  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)


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

def equivalent?(type)
  return (type.is_a?(TypeTuple) and
          !@types.zip(type.types).index {|t0,t1| !t0.equivalent?(t1) })
end

#get_all_typesObject

Gets an array containing all the syb types.



1245
1246
1247
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1245

def get_all_types
  return @types.clone
end

#get_type(index) ⇒ Object

Gets a sub type by +index+.



1250
1251
1252
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1250

def get_type(index)
  return @types[index.to_i]
end

#hashObject

Hash function.



1235
1236
1237
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1235

def hash
  return [super,@types].hash
end

#rangeObject

Gets the range of the type.

NOTE: only valid if the tuple is regular (i.e., all its sub types are identical)



1321
1322
1323
1324
1325
1326
1327
1328
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1321

def range
  if regular? then
    # Regular tuple, return its range as if it was an array.
    return 0..@types.size-1
  else
    raise AnyError, "No range for type #{self}"
  end
end

#regular?Boolean

Tell if the tuple is regular, i.e., all its sub types are equivalent.

NOTE: empty tuples are assumed not to be regular.

Returns:

  • (Boolean)


1298
1299
1300
1301
1302
1303
1304
1305
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1298

def regular?
  return false if @types.empty?
  t0 = @types[0]
  @types[1..-1].each do |type|
    return false unless t0.equivalent?(type)
  end
  return true
end

#types?Boolean

Tells if the type has sub types.

Returns:

  • (Boolean)


1240
1241
1242
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1240

def types?
  return true
end

#widthObject

Gets the bitwidth.



1308
1309
1310
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1308

def width
  return @types.reduce(0) { |sum,type| sum + type.width }
end