Class: RubyHDL::High::Expression

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

Overview

Describes the software implementation of an expression.

Direct Known Subclasses

Binary, Ref, Ruby, Scall, Select, SignalI, Unary, Value, VerbatimExpression

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from HEnumerable

#hall?, #hany?, #hchain, #hchunk, #hchunk_while, #hcompact, #hcount, #hcycle, #hdrop, #hdrop_while, #heach_cons, #heach_entry, #heach_range, #heach_slice, #heach_with_index, #heach_with_object, #hfind, #hfind_index, #hfirst, #hflat_map, #hgrep, #hgrep_v, #hgroup_by, #hinclude?, #hinject, #hlazy, #hmap, #hmax, #hmax_by, #hmin, #hmin_by, #hminmax, #hminmax_by, #hnone?, #hone?, #hpartition, #hreduce, #hreject, #hreverse_each, #hselect, #hslice_after, #hslice_before, #hslice_when, #hsort, #hsort_by, #hsum, #htake, #htake_while, #htally, #hto_a, #hto_h, #huniq, #hzip

Constructor Details

#initialize(type) ⇒ Expression

Create a new expression with +type+ data type.



2198
2199
2200
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2198

def initialize(type)
  @type = type.to_type
end

Instance Attribute Details

#typeObject (readonly)

Returns the value of attribute type.



2196
2197
2198
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2196

def type
  @type
end

Instance Method Details

#<=(right) ⇒ Object

The <= operator which can be either a transmit or a comparison. By default set to transmit, and converted to comparison if child of operator or condition of sif/swhile statements.



2262
2263
2264
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2262

def <=(right)
  return Transmit.new(self.to_expr,right.to_expr)
end

#[](typ, rng = nil) ⇒ Object

Creates an access to elements of range +rng+ of the signal, and set the type of elements as +typ+ if given.

NOTE: +rng+ can be a single expression in which case it is an index.



2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2277

def [](typ,rng=nil)
  # Treat the number of arguments
  rng, typ = typ, nil unless rng
  # Process the range.
  if rng.is_a?(::Range) then
    first = rng.first
    if (first.is_a?(::Integer)) then
      first = self.type.size+first if first < 0
    end
    last = rng.last
    if (last.is_a?(::Integer)) then
      last = self.type.size+last if last < 0
    end
    rng = first..last
  end
  if rng.is_a?(::Integer) && rng < 0 then
    rng = self.type.size+rng
  end
  if rng.respond_to?(:to_expr) then
    # Number range: convert it to an expression.
    rng = rng.to_expr
  end 
  if rng.is_a?(Expression) then
    # Index case
    if typ then
      return RefIndex.new(typ,self.to_expr,rng)
    else
      return RefIndex.new(self.type.base,self.to_expr,rng)
    end
  else
    # Range case, ensure it is made among expression.
    first = rng.first.to_expr
    last = rng.last.to_expr
    # And create the reference.
    if typ then
      return RefRange.new(typ,
                          self.to_expr,first..last)
    else
      return RefRange.new(self.type.slice(first..last),
                          self.to_expr,first..last)
    end
  end
end

#heach(&ruby_block) ⇒ Object

HW iteration on each element. (parallel version)



2362
2363
2364
2365
2366
2367
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2362

def heach(&ruby_block)
  return self unless ruby_block
  self.type.width.each do |i|
    ruby_block.call(self[i])
  end
end

#mux(*choices) ⇒ Object

Converts to a select operator using current expression as condition for one of the +choices+.

NOTE: +choices+ can either be a list of arguments or an array. If +choices+ has only two entries (and it is not a hash), +value+ will be converted to a boolean.



2327
2328
2329
2330
2331
2332
2333
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2327

def mux(*choices)
  # Process the choices.
  choices = choices.flatten(1) if choices.size == 1
  choices.map! { |choice| choice.to_expr }
  # Generate the select expression.
  return Select.new(choices[0].type,self.to_expr,*choices)
end

#sdownto(val, &ruby_block) ⇒ Object

HW downto iteration.



2356
2357
2358
2359
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2356

def sdownto(val,&ruby_block)
  RubyHDL::High.top_sblock << 
  Siter.new(RubyHDL::High.top_sblock.sequencer,self,"downto",&ruby_block)
end

#seach(&ruby_block) ⇒ Object

HW iteration on each element.



2338
2339
2340
2341
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2338

def seach(&ruby_block)
  RubyHDL::High.top_sblock << 
  Siter.new(RubyHDL::High.top_sblock.sequencer,self,"each",&ruby_block)
end

#stimes(&ruby_block) ⇒ Object

HW times iteration.



2344
2345
2346
2347
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2344

def stimes(&ruby_block)
  RubyHDL::High.top_sblock << 
  Siter.new(RubyHDL::High.top_sblock.sequencer,self,"times",&ruby_block)
end

#supto(val, &ruby_block) ⇒ Object

HW upto iteration.



2350
2351
2352
2353
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2350

def supto(val,&ruby_block)
  RubyHDL::High.top_sblock <<
  Siter.new(RubyHDL::High.top_sblock.sequencer,self,"upto",&ruby_block)
end

#to_cObject

Convert to C code.



2220
2221
2222
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2220

def to_c
  raise "to_c not defined for class: #{self.class}."
end

#to_exprObject Also known as: to_ref

Converts to an expression.



2203
2204
2205
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2203

def to_expr
  self
end

#to_python(l = "") ⇒ Object

Convert to Python code.



2225
2226
2227
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2225

def to_python(l = "")
  raise "to_python not defined for class: #{self.class}."
end

#to_rubyObject Also known as: to_ruby_left

Convert to Ruby code.



2215
2216
2217
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2215

def to_ruby
  raise "to_ruby not defined for class: #{self.class}."
end

#to_tf(l = "") ⇒ Object

Convert to TensorFlow code.



2230
2231
2232
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2230

def to_tf(l = "")
  raise "to_python not defined for class: #{self.class}."
end

#to_valueObject

Compute the expression (convert it to a value).



2210
2211
2212
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 2210

def to_value
  raise "to_value not defined here."
end