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, FullAdder, HalfAdder, 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 array of their names array of their names

Parameters:

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

    options to create the Component with

Options Hash (opts):

  • :inputs (FixNum, Array<Symbol>)

    The number of inputs or an

  • :ouputs (FixNum, Array<Symbol>)

    The number of outputs or an

  • :port_mappings (Hash)

    The port_mappings to use



18
19
20
21
22
23
24
25
# File 'lib/circuits/component/base.rb', line 18

def initialize(opts = {})
  @inputs = create_inputs opts[:inputs]
  @outputs = create_outputs opts[:outputs]
  @port_mappings = create_port_mappings opts
  @sub_components = opts[:sub_components] || []
  @ticks = opts[:ticks] || 0
  declare_ports_as_methods
end

Instance Attribute Details

#inputsObject (readonly)

the inputs of this component



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

def inputs
  @inputs
end

#outputsObject (readonly)

the outputs of this component



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

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



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/circuits/component/base.rb', line 49

def [](port)
  port_mapping = @port_mappings[port]
  return nil if port_mapping.nil?
  port_number = port_mapping[:number]
  case port_mapping[:type]
  when :input
    inputs[port_number]
  when :output
    outputs[port_number]
  end
end

#tickObject

Computes the outputs based on the inputs and previous state



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

def tick
  @ticks.times.each do
    @sub_components.each(&:tick)
    @sub_components.each(&:tock)
  end
end

#tockObject

Sets all the outputs expose what was set in #tick



42
43
44
# File 'lib/circuits/component/base.rb', line 42

def tock
  outputs.each(&:tock)
end