Module: Circuits::Component

Included in:
And, Nand, Nor, Not, Or, SrNand, SrNor, Xnor, Xor
Defined in:
lib/circuits/component.rb,
lib/circuits/component/or.rb,
lib/circuits/component/and.rb,
lib/circuits/component/nor.rb,
lib/circuits/component/not.rb,
lib/circuits/component/xor.rb,
lib/circuits/component/nand.rb,
lib/circuits/component/xnor.rb,
lib/circuits/component/sr_nor.rb,
lib/circuits/component/sr_nand.rb

Overview

A component has a set of inputs an outputs. Every ‘tick` a componnent computes its outputs, but the componnent will wait for the `tock` before the componnent updates its own outputs

Defined Under Namespace

Classes: And, Nand, Nor, Not, Or, SrNand, SrNor, Xnor, Xor

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#inputsObject (readonly)

the inputs of this component



59
60
61
# File 'lib/circuits/component.rb', line 59

def inputs
  @inputs
end

#outputsObject (readonly)

the outputs of this component



62
63
64
# File 'lib/circuits/component.rb', line 62

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



34
35
36
37
38
39
40
41
42
# File 'lib/circuits/component.rb', line 34

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

#[]=(port, terminal) ⇒ Input, Output

Assigns to an input or output

Parameters:

  • port (Symbol)

    The symbol that represents the terminal

  • terminal (Input, Output)

    The terminal to assign

Returns:

  • (Input, Output)

    The terminal that was passed in



48
49
50
51
52
53
54
55
56
# File 'lib/circuits/component.rb', line 48

def []=(port, terminal)
  p = @port_mappings[port]
  case p[:type]
  when :input
    @inputs[p[:number]] = terminal
  when :output
    @outputs[p[:number]] = terminal
  end
end

#initialize(opts = {}) ⇒ Object

Creates the Component with inputs and outputs

Parameters:

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

    options to create the Component with

Options Hash (opts):

  • :inputs (Array<Input>, FixNum)

    The array of inputs to use, or the number of inputs to create



13
14
15
16
17
18
19
# File 'lib/circuits/component.rb', line 13

def initialize(opts = {})
  set_defaults
  create_inputs opts
  create_outputs opts
  create_port_mappings
  setup
end

#tickObject

Does the internal computation and sets the outputs



22
23
24
# File 'lib/circuits/component.rb', line 22

def tick
  fail NotImplementedError
end

#tockObject

Sets all the outputs expose what was set in #tick



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

def tock
  outputs.each(&:tock)
end