Class: Circuits::Component::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/circuits/component/base.rb

Overview

Base class to extend

Direct Known Subclasses

And, D, Nand, Nor, Not, Or, SrNand, SrNor, Xnor, Xor

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Base

Creates the Component with inputs and outputs

Parameters:

  • opts (Hash) (defaults to: {})

    options to create the Component with

Options Hash (opts):

  • :input_count (FixNum)

    The number of inputs

  • :ouput_count (FixNum)

    The number of outputs

  • :port_mappings (Hash)

    The port_mappings to use



16
17
18
19
20
21
22
# File 'lib/circuits/component/base.rb', line 16

def initialize(opts = {})
  input_count = opts[:input_count] || default_input_count
  output_count = opts[:output_count] || default_output_count
  @inputs = input_count.times.collect { Circuits::Terminal::Input.new }
  @outputs = output_count.times.collect { Circuits::Terminal::Output.new }
  @port_mappings = opts[:port_mappings] || default_port_mappings
end

Instance Attribute Details

#inputsObject (readonly)

the inputs of this component



25
26
27
# File 'lib/circuits/component/base.rb', line 25

def inputs
  @inputs
end

#outputsObject (readonly)

the outputs of this component



28
29
30
# File 'lib/circuits/component/base.rb', line 28

def outputs
  @outputs
end

Instance Method Details

#[](port) ⇒ Input, Output

Gets the teminal assigned to the port

Parameters:

  • port (Symbol)

    The symbol that represents the terminal

Returns:

  • (Input, Output)

    The terminal



38
39
40
41
42
43
44
45
46
47
# File 'lib/circuits/component/base.rb', line 38

def [](port)
  port_mapping = port_mappings[port]
  port_number = port_mapping[:number]
  case port_mapping[:type]
  when :input
    inputs[port_number]
  when :output
    outputs[port_number]
  end
end

#tockObject

Sets all the outputs expose what was set in #tick



31
32
33
# File 'lib/circuits/component/base.rb', line 31

def tock
  outputs.each(&:tock)
end