Module: HDLRuby::High::Hmux
Overview
Module providing methods for declaring select expressions.
Instance Method Summary collapse
-
#mux(select, *choices) ⇒ Object
Creates an operator selecting from +select+ one of the +choices+.
Instance Method Details
#mux(select, *choices) ⇒ Object
Creates an operator selecting from +select+ 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. * The type of the select is computed as the largest no integer-constant choice. If only constant integer choices, use the largest type of them.
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 |
# File 'lib/HDLRuby/hruby_high.rb', line 238 def mux(select,*choices) select = select ? 1 : 0 if [true,false,nil].include?(select) if select.respond_to?(:to_i) then # The mux can be evaluate straight away. Do metaprograming. return choices[select.to_i] end # Process the choices. choices = choices.flatten(1) if choices.size == 1 choices.map! { |choice| choice.to_expr } # Compute the type of the select as the largest no # integer-constant type. # If only such constants, use the largest type of them. type = choices.reduce(Bit) do |type,choice| unless choice.is_a?(Value) && choice.type == Integer then type.width >= choice.type.width ? type : choice.type else type end end unless type then type = choices.reduce(Bit) do |type,choice| type.width >= choice.type.width ? type : choice.type end end # Generate the select expression. return Select.new(type,"?",select.to_expr,*choices) end |