Skip to content

Commit

Permalink
Moved Component module into a class
Browse files Browse the repository at this point in the history
  • Loading branch information
meringu committed Oct 27, 2015
1 parent 142c852 commit dc79220
Show file tree
Hide file tree
Showing 12 changed files with 70 additions and 316 deletions.
121 changes: 0 additions & 121 deletions lib/circuits/component.rb

This file was deleted.

17 changes: 7 additions & 10 deletions lib/circuits/component/and.rb
@@ -1,22 +1,19 @@
require 'circuits/component'
require 'circuits/component/base'

module Circuits
module Component
# Logical AND Operator
class And
include Component
class And < Base
def initialize(opts = {})
@input_count = 2
@output_count = 1
super opts
end

# Sets the output to be the result of a logical AND of the inputs
def tick
outputs[0].set(inputs.map(&:get).inject(:&))
end

private

def set_defaults
@input_count = 2
@output_count = 1
end
end
end
end
17 changes: 7 additions & 10 deletions lib/circuits/component/nand.rb
@@ -1,22 +1,19 @@
require 'circuits/component'
require 'circuits/component/base'

module Circuits
module Component
# Logical NAND Operator
class Nand
include Component
class Nand < Base
def initialize(opts = {})
@input_count = 2
@output_count = 1
super opts
end

# Sets the output to be the result of a logical NAND of the inputs
def tick
outputs[0].set(!inputs.map(&:get).inject(:&))
end

private

def set_defaults
@input_count = 2
@output_count = 1
end
end
end
end
17 changes: 7 additions & 10 deletions lib/circuits/component/nor.rb
@@ -1,22 +1,19 @@
require 'circuits/component'
require 'circuits/component/base'

module Circuits
module Component
# Logical NOR Operator
class Nor
include Component
class Nor < Base
def initialize(opts = {})
@input_count = 2
@output_count = 1
super opts
end

# Sets the output to be the result of a logical OR of the inputs
def tick
outputs[0].set(!inputs.map(&:get).inject(:|))
end

private

def set_defaults
@input_count = 2
@output_count = 1
end
end
end
end
17 changes: 7 additions & 10 deletions lib/circuits/component/not.rb
@@ -1,22 +1,19 @@
require 'circuits/component'
require 'circuits/component/base'

module Circuits
module Component
# Logical NOT Operator
class Not
include Component
class Not < Base
def initialize(opts = {})
@input_count = 1
@output_count = 1
super opts
end

# Sets the output to be the result of a logical NOT of the inputs
def tick
outputs[0].set(!inputs[0].get)
end

private

def set_defaults
@input_count = 1
@output_count = 1
end
end
end
end
17 changes: 7 additions & 10 deletions lib/circuits/component/or.rb
@@ -1,22 +1,19 @@
require 'circuits/component'
require 'circuits/component/base'

module Circuits
module Component
# Logical OR Operator
class Or
include Component
class Or < Base
def initialize(opts = {})
@input_count = 2
@output_count = 1
super opts
end

# Sets the output to be the result of a logical OR of the inputs
def tick
outputs[0].set(inputs.map(&:get).inject(:|))
end

private

def set_defaults
@input_count = 2
@output_count = 1
end
end
end
end
19 changes: 12 additions & 7 deletions lib/circuits/component/sr_nand.rb
@@ -1,14 +1,19 @@
require 'circuits/component'
require 'circuits/component/base'

module Circuits
module Component
# SR NAND Latch
class SrNand
include Component
class SrNand < Base
def initialize(opts = {})
set_defaults
super opts
create_sub_components
link_sub_components
end

# Computes the outputs based on the inputs and previous state
def tick
update_internal_components
update_sub_components
self[:q].set nand_1[:out].get
self[:not_q].set nand_2[:out].get
end
Expand All @@ -17,13 +22,13 @@ def tick

attr_reader :nand_1, :nand_2, :sub_components

def create_internal_components
def create_sub_components
@nand_1 = Nand.new
@nand_2 = Nand.new
@sub_components = [@nand_1, @nand_2]
end

def link_internal_components
def link_sub_components
nand_1[:a] = self[:not_s]
nand_2[:a] = self[:not_r]
nand_1[:b] = nand_2[:out]
Expand All @@ -46,7 +51,7 @@ def setup
link_internal_components
end

def update_internal_components
def update_sub_components
2.times.each do
sub_components.each(&:tick)
sub_components.each(&:tock)
Expand Down
11 changes: 8 additions & 3 deletions lib/circuits/component/sr_nor.rb
@@ -1,10 +1,15 @@
require 'circuits/component'
require 'circuits/component/base'

module Circuits
module Component
# SR NOR Latch
class SrNor
include Component
class SrNor < Base
def initialize(opts = {})
set_defaults
super opts
create_internal_components
link_internal_components
end

# Computes the outputs based on the inputs and previous state
def tick
Expand Down
17 changes: 7 additions & 10 deletions lib/circuits/component/xnor.rb
@@ -1,22 +1,19 @@
require 'circuits/component'
require 'circuits/component/base'

module Circuits
module Component
# Logical XNOR Operator
class Xnor
include Component
class Xnor < Base
def initialize(opts = {})
@input_count = 2
@output_count = 1
super opts
end

# Sets the output to be the result of a logical XNOR of the inputs
def tick
outputs[0].set(!inputs.map(&:get).inject(:^))
end

private

def set_defaults
@input_count = 2
@output_count = 1
end
end
end
end
17 changes: 7 additions & 10 deletions lib/circuits/component/xor.rb
@@ -1,22 +1,19 @@
require 'circuits/component'
require 'circuits/component/base'

module Circuits
module Component
# Logical XOR Operator
class Xor
include Component
class Xor < Base
def initialize(opts = {})
@input_count = 2
@output_count = 1
super opts
end

# Sets the output to be the result of a logical XOR of the inputs
def tick
outputs[0].set(inputs.map(&:get).inject(:^))
end

private

def set_defaults
@input_count = 2
@output_count = 1
end
end
end
end

0 comments on commit dc79220

Please sign in to comment.