Skip to content

Commit

Permalink
Merge 7fa5dd2 into 9807a38
Browse files Browse the repository at this point in the history
  • Loading branch information
meringu committed Oct 27, 2015
2 parents 9807a38 + 7fa5dd2 commit 37f7c68
Show file tree
Hide file tree
Showing 15 changed files with 110 additions and 142 deletions.
2 changes: 1 addition & 1 deletion lib/circuits/component/and.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(opts = {})

# Sets the output to be the result of a logical AND of the inputs
def tick
outputs[0].set(inputs.map(&:get).inject(:&))
self[:out].set(inputs.map(&:get).inject(:&))
end
end
end
Expand Down
7 changes: 4 additions & 3 deletions lib/circuits/component/base.rb
@@ -1,4 +1,5 @@
require 'circuits/terminal'
require 'circuits/terminal/input'
require 'circuits/terminal/output'

module Circuits
# A component has a set of inputs an outputs. Every `tick` a componnent
Expand Down Expand Up @@ -68,7 +69,7 @@ def create_inputs(opts)
elsif opts[:inputs].class == Fixnum
@input_count = opts[:inputs]
end
@inputs ||= @input_count.times.collect { Circuits::Terminal.new }
@inputs ||= @input_count.times.collect { Circuits::Terminal::Input.new }
end

def create_outputs(opts)
Expand All @@ -79,7 +80,7 @@ def create_outputs(opts)
@output_count = opts[:outputs]
end
@outputs ||= @output_count.times.collect do
Circuits::Terminal.new
Circuits::Terminal::Output.new
end
end

Expand Down
2 changes: 1 addition & 1 deletion lib/circuits/component/nand.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(opts = {})

# Sets the output to be the result of a logical NAND of the inputs
def tick
outputs[0].set(!inputs.map(&:get).inject(:&))
self[:out].set(!inputs.map(&:get).inject(:&))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/circuits/component/nor.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(opts = {})

# Sets the output to be the result of a logical OR of the inputs
def tick
outputs[0].set(!inputs.map(&:get).inject(:|))
self[:out].set(!inputs.map(&:get).inject(:|))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/circuits/component/not.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(opts = {})

# Sets the output to be the result of a logical NOT of the inputs
def tick
outputs[0].set(!inputs[0].get)
self[:out].set(!self[:in].get)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/circuits/component/or.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(opts = {})

# Sets the output to be the result of a logical OR of the inputs
def tick
outputs[0].set(inputs.map(&:get).inject(:|))
self[:out].set(inputs.map(&:get).inject(:|))
end
end
end
Expand Down
21 changes: 15 additions & 6 deletions lib/circuits/component/sr_nand.rb
@@ -1,4 +1,5 @@
require 'circuits/component/base'
require 'circuits/component/nand'

module Circuits
module Component
Expand All @@ -8,9 +9,9 @@ def initialize(opts = {})
set_defaults
super opts
create_sub_components
link_inputs
link_outputs
link_sub_components
self[:q].set nand_1[:out]
self[:not_q].set nand_2[:out]
end

# Computes the outputs based on the inputs and previous state
Expand All @@ -31,11 +32,19 @@ def create_sub_components
@sub_components = [@nand_1, @nand_2]
end

def link_inputs
nand_1[:a].set self[:not_s]
nand_2[:a].set self[:not_r]
end

def link_outputs
self[:q].set nand_1[:out]
self[:not_q].set nand_2[:out]
end

def link_sub_components
nand_1[:a] = self[:not_s]
nand_2[:a] = self[:not_r]
nand_1[:b] = nand_2[:out]
nand_2[:b] = nand_1[:out]
nand_1[:b].set nand_2[:out]
nand_2[:b].set nand_1[:out]
end

def set_defaults
Expand Down
21 changes: 15 additions & 6 deletions lib/circuits/component/sr_nor.rb
@@ -1,4 +1,5 @@
require 'circuits/component/base'
require 'circuits/component/nor'

module Circuits
module Component
Expand All @@ -8,9 +9,9 @@ def initialize(opts = {})
set_defaults
super opts
create_sub_components
link_inputs
link_outputs
link_sub_components
self[:q].set nor_1[:out]
self[:not_q].set nor_2[:out]
end

# Computes the outputs based on the inputs and previous state
Expand All @@ -31,11 +32,19 @@ def create_sub_components
@sub_components = [@nor_1, @nor_2]
end

def link_inputs
nor_1[:a].set self[:r]
nor_2[:a].set self[:s]
end

def link_outputs
self[:q].set nor_1[:out]
self[:not_q].set nor_2[:out]
end

def link_sub_components
nor_1[:a] = self[:r]
nor_2[:a] = self[:s]
nor_1[:b] = nor_2[:out]
nor_2[:b] = nor_1[:out]
nor_1[:b].set nor_2[:out]
nor_2[:b].set nor_1[:out]
end

def set_defaults
Expand Down
2 changes: 1 addition & 1 deletion lib/circuits/component/xnor.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(opts = {})

# Sets the output to be the result of a logical XNOR of the inputs
def tick
outputs[0].set(!inputs.map(&:get).inject(:^))
self[:out].set(!inputs.map(&:get).inject(:^))
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/circuits/component/xor.rb
Expand Up @@ -12,7 +12,7 @@ def initialize(opts = {})

# Sets the output to be the result of a logical XOR of the inputs
def tick
outputs[0].set(inputs.map(&:get).inject(:^))
self[:out].set(inputs.map(&:get).inject(:^))
end
end
end
Expand Down
35 changes: 0 additions & 35 deletions lib/circuits/terminal.rb

This file was deleted.

32 changes: 32 additions & 0 deletions lib/circuits/terminal/input.rb
@@ -0,0 +1,32 @@
require 'circuits/terminal/output'

module Circuits
# An input or an output to read from and set
module Terminal
# Reads from a single output
class Input
# Creates the input
# @param opts [Hash] Options to create the Input with
# @option opts [Component::Output] :output The output to read from
def initialize(opts = {})
@terminal = opts[:terminal] || Output.new(state: opts[:state])
end

# Forward get to the output
# @return [Boolean] The state of the output
def get
@terminal.get
end

# Output to use or state to make a dummy output with
# @param [Output, Boolean] output The output to read from, or state
def set(output)
if [Input, Output].include? output.class
@terminal = output
else
@terminal = Output.new(state: output)
end
end
end
end
end
36 changes: 36 additions & 0 deletions lib/circuits/terminal/output.rb
@@ -0,0 +1,36 @@
module Circuits
# An input or an output to read from and set
module Terminal
# Gets set tcked then read
class Output
# Creates the output
# @param opts [Hash] Options to create the Output with
# @option opts [Boolean] :state The initial state of the Output
def initialize(opts = {})
@next_state = opts[:terminal] || opts[:state] || false
tock
end

# Gets the state of the terminal
# @return [Boolean] The state of the output
def get
@state
end

# The next state
# @param [Boolean, Terminal] terminal The terminal or state to output
def set(state)
@next_state = state
end

# Sets the state what was last set
def tock
if [Input, Output].include? @next_state.class
@state = @next_state.get
else
@state = @next_state
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/circuits/version.rb
@@ -1,5 +1,5 @@
# Circuits allows you to express logical circuits in code
module Circuits
# The version of the Circuits gem
VERSION = '0.6.0'
VERSION = '0.7.0'
end
84 changes: 0 additions & 84 deletions spec/unit/circuits/terminal_spec.rb

This file was deleted.

0 comments on commit 37f7c68

Please sign in to comment.