Module: HDLRuby::High::Hmux

Included in:
HBlock, Scope, SystemT
Defined in:
lib/HDLRuby/hruby_high.rb

Overview

Module providing methods for declaring select expressions.

Instance Method Summary collapse

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.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
# File 'lib/HDLRuby/hruby_high.rb', line 234

def mux(select,*choices)
    # 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