Class: RubyHDL::High::Select

Inherits:
Expression show all
Defined in:
lib/HDLRuby/std/sequencer_sw.rb

Overview

Describes the software implementation of an select operation.

Instance Attribute Summary

Attributes inherited from Expression

#type

Instance Method Summary collapse

Methods inherited from Expression

#<=, #[], #mux, #sdownto, #seach, #stimes, #supto, #to_expr, #to_value

Constructor Details

#initialize(type, operator, sel, *choices) ⇒ Select

Create a new select operation with +type+ data type, selection operand +sel+ and possible choices +choices+.



1973
1974
1975
1976
1977
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1973

def initialize(type,operator,sel,*choices)
  super(type)
  @sel = sel.to_expr
  @choices = choices.map(&:to_expr)
end

Instance Method Details

#to_cObject

Convert to C code.



1988
1989
1990
1991
1992
1993
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1988

def to_c
  return "switch(#{@sel.to_c}) {\n" +
    @choices.map.with_index do |choice,i|
      "case #{i}:\n#{choice.to_c}\nbreak;"
    end.join("\n") + "\n}"
end

#to_rubyObject

Convert to Ruby code.



1980
1981
1982
1983
1984
1985
# File 'lib/HDLRuby/std/sequencer_sw.rb', line 1980

def to_ruby
  return "case(#{@sel.to_}) ; " +
    @choices.map.with_index do |choice,i|
      "when #{i} ; #{choice.to_ruby} ; "
    end.join + "end"
end