circuits 0.2.3 → 0.2.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +8 -8
- data/README.md +1 -0
- data/lib/circuits/component/and.rb +7 -4
- data/lib/circuits/component/nand.rb +7 -4
- data/lib/circuits/component/nor.rb +7 -4
- data/lib/circuits/component/not.rb +9 -6
- data/lib/circuits/component/or.rb +7 -4
- data/lib/circuits/component/sr_nand.rb +9 -8
- data/lib/circuits/component/sr_nor.rb +9 -8
- data/lib/circuits/component/xnor.rb +7 -4
- data/lib/circuits/component/xor.rb +7 -4
- data/lib/circuits/component.rb +21 -14
- data/lib/circuits/terminal/input.rb +9 -0
- data/lib/circuits/terminal/output.rb +8 -1
- data/lib/circuits/terminal.rb +6 -0
- data/lib/circuits/version.rb +2 -1
- data/spec/unit/circuits/component_spec.rb +0 -20
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NDA3ZjgwZWU3MWRjMjU5NjE5ZjFkNGI5ZTUxOWY5NzNhYzgxOGZmNg==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
MmJjNDdlMmRmNDE0Y2RkZWJiOTBkMGMzYzAyODU0OTkwZjk2OWRlZA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
NWZjODc5OGQwYjFhNzgxNTM5OTY5OGFjMjMwNmExZTUzNGRiMjU2OGI2MmQ1
|
10
|
+
MDY5NDI2NDM4NzYxMTU1ZjIxYzRmMTk4NDE4MWRhZjU5YjdjMzI4ODZhYmZl
|
11
|
+
Njc5YjJmOThmZmFjNGQ3MDE1ZmMyNTM0M2I5YzViMDE2MDk4MzI=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
MzIwZmVkZmQ2YjhkYjIxMTM4MGEwZWE5ZGQ2OWYzZjhlMzFmMjBkMjgzM2Y3
|
14
|
+
MGQyMDFjY2FkM2YxZjMzODMyYmZiYzQ4YTYyMWU1YzBkZjE1ZDYwMzNhMjdh
|
15
|
+
ODU4M2JhNjZhNjFjOGY1NGI5ZDUzMzljMTE2MGMyMmZkMTlhNTY=
|
data/README.md
CHANGED
@@ -1,4 +1,5 @@
|
|
1
1
|
[](https://travis-ci.org/meringu/circuits)
|
2
|
+
[](https://badge.fury.io/rb/circuits)
|
2
3
|
[](https://coveralls.io/github/meringu/circuits?branch=master)
|
3
4
|
[](https://codeclimate.com/github/meringu/circuits)
|
4
5
|
|
@@ -6,6 +6,13 @@ module Circuits
|
|
6
6
|
class And
|
7
7
|
include Component
|
8
8
|
|
9
|
+
# Sets the output to be the result of a logical AND of the inputs
|
10
|
+
def tick
|
11
|
+
outputs[0].set(inputs.map(&:get).inject(:&))
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
9
16
|
def input_count
|
10
17
|
2
|
11
18
|
end
|
@@ -13,10 +20,6 @@ module Circuits
|
|
13
20
|
def output_count
|
14
21
|
1
|
15
22
|
end
|
16
|
-
|
17
|
-
def tick
|
18
|
-
outputs[0].set(inputs.map(&:get).inject(:&))
|
19
|
-
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
@@ -6,6 +6,13 @@ module Circuits
|
|
6
6
|
class Nand
|
7
7
|
include Component
|
8
8
|
|
9
|
+
# Sets the output to be the result of a logical NAND of the inputs
|
10
|
+
def tick
|
11
|
+
outputs[0].set(!inputs.map(&:get).inject(:&))
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
9
16
|
def input_count
|
10
17
|
2
|
11
18
|
end
|
@@ -13,10 +20,6 @@ module Circuits
|
|
13
20
|
def output_count
|
14
21
|
1
|
15
22
|
end
|
16
|
-
|
17
|
-
def tick
|
18
|
-
outputs[0].set(!inputs.map(&:get).inject(:&))
|
19
|
-
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
@@ -6,6 +6,13 @@ module Circuits
|
|
6
6
|
class Nor
|
7
7
|
include Component
|
8
8
|
|
9
|
+
# Sets the output to be the result of a logical OR of the inputs
|
10
|
+
def tick
|
11
|
+
outputs[0].set(!inputs.map(&:get).inject(:|))
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
9
16
|
def input_count
|
10
17
|
2
|
11
18
|
end
|
@@ -13,10 +20,6 @@ module Circuits
|
|
13
20
|
def output_count
|
14
21
|
1
|
15
22
|
end
|
16
|
-
|
17
|
-
def tick
|
18
|
-
outputs[0].set(!inputs.map(&:get).inject(:|))
|
19
|
-
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
@@ -5,18 +5,21 @@ module Circuits
|
|
5
5
|
# Logical NOT Operator
|
6
6
|
class Not
|
7
7
|
include Component
|
8
|
-
|
8
|
+
|
9
|
+
# Sets the output to be the result of a logical NOT of the inputs
|
10
|
+
def tick
|
11
|
+
outputs[0].set(!inputs[0].get)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
9
16
|
def input_count
|
10
17
|
1
|
11
18
|
end
|
12
19
|
|
13
20
|
def output_count
|
14
21
|
1
|
15
|
-
end
|
16
|
-
|
17
|
-
def tick
|
18
|
-
outputs[0].set(!inputs[0].get)
|
19
|
-
end
|
22
|
+
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
@@ -6,6 +6,13 @@ module Circuits
|
|
6
6
|
class Or
|
7
7
|
include Component
|
8
8
|
|
9
|
+
# Sets the output to be the result of a logical OR of the inputs
|
10
|
+
def tick
|
11
|
+
outputs[0].set(inputs.map(&:get).inject(:|))
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
9
16
|
def input_count
|
10
17
|
2
|
11
18
|
end
|
@@ -13,10 +20,6 @@ module Circuits
|
|
13
20
|
def output_count
|
14
21
|
1
|
15
22
|
end
|
16
|
-
|
17
|
-
def tick
|
18
|
-
outputs[0].set(inputs.map(&:get).inject(:|))
|
19
|
-
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
@@ -6,14 +6,7 @@ module Circuits
|
|
6
6
|
class SrNand
|
7
7
|
include Component
|
8
8
|
|
9
|
-
|
10
|
-
2
|
11
|
-
end
|
12
|
-
|
13
|
-
def output_count
|
14
|
-
2
|
15
|
-
end
|
16
|
-
|
9
|
+
# Computes the outputs based on the inputs and previous state
|
17
10
|
def tick
|
18
11
|
2.times.each do
|
19
12
|
sub_components.each(&:tick)
|
@@ -27,6 +20,14 @@ module Circuits
|
|
27
20
|
|
28
21
|
attr_reader :nor_1, :nor_2, :sub_components
|
29
22
|
|
23
|
+
def input_count
|
24
|
+
2
|
25
|
+
end
|
26
|
+
|
27
|
+
def output_count
|
28
|
+
2
|
29
|
+
end
|
30
|
+
|
30
31
|
def setup
|
31
32
|
@nor_1 = Nand.new(inputs: [inputs[0]])
|
32
33
|
@nor_2 = Nand.new(inputs: [inputs[1]])
|
@@ -6,14 +6,7 @@ module Circuits
|
|
6
6
|
class SrNor
|
7
7
|
include Component
|
8
8
|
|
9
|
-
|
10
|
-
2
|
11
|
-
end
|
12
|
-
|
13
|
-
def output_count
|
14
|
-
2
|
15
|
-
end
|
16
|
-
|
9
|
+
# Computes the outputs based on the inputs and previous state
|
17
10
|
def tick
|
18
11
|
2.times.each do
|
19
12
|
sub_components.each(&:tick)
|
@@ -27,6 +20,14 @@ module Circuits
|
|
27
20
|
|
28
21
|
attr_reader :nor_1, :nor_2, :sub_components
|
29
22
|
|
23
|
+
def input_count
|
24
|
+
2
|
25
|
+
end
|
26
|
+
|
27
|
+
def output_count
|
28
|
+
2
|
29
|
+
end
|
30
|
+
|
30
31
|
def setup
|
31
32
|
@nor_1 = Nor.new(inputs: [inputs[0]])
|
32
33
|
@nor_2 = Nor.new(inputs: [inputs[1]])
|
@@ -6,6 +6,13 @@ module Circuits
|
|
6
6
|
class Xnor
|
7
7
|
include Component
|
8
8
|
|
9
|
+
# Sets the output to be the result of a logical XNOR of the inputs
|
10
|
+
def tick
|
11
|
+
outputs[0].set(inputs[0].get == inputs[1].get)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
9
16
|
def input_count
|
10
17
|
2
|
11
18
|
end
|
@@ -13,10 +20,6 @@ module Circuits
|
|
13
20
|
def output_count
|
14
21
|
1
|
15
22
|
end
|
16
|
-
|
17
|
-
def tick
|
18
|
-
outputs[0].set(inputs[0].get == inputs[1].get)
|
19
|
-
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
@@ -6,6 +6,13 @@ module Circuits
|
|
6
6
|
class Xor
|
7
7
|
include Component
|
8
8
|
|
9
|
+
# Sets the output to be the result of a logical XOR of the inputs
|
10
|
+
def tick
|
11
|
+
outputs[0].set(inputs[0].get != inputs[1].get)
|
12
|
+
end
|
13
|
+
|
14
|
+
private
|
15
|
+
|
9
16
|
def input_count
|
10
17
|
2
|
11
18
|
end
|
@@ -13,10 +20,6 @@ module Circuits
|
|
13
20
|
def output_count
|
14
21
|
1
|
15
22
|
end
|
16
|
-
|
17
|
-
def tick
|
18
|
-
outputs[0].set(inputs[0].get != inputs[1].get)
|
19
|
-
end
|
20
23
|
end
|
21
24
|
end
|
22
25
|
end
|
data/lib/circuits/component.rb
CHANGED
@@ -5,33 +5,40 @@ module Circuits
|
|
5
5
|
# A component has a set of inputs an outputs. Every `tick` a componnent
|
6
6
|
# computes its outputs, but the componnent will wait for the `tock` before the
|
7
7
|
# componnent updates its own outputs
|
8
|
-
module Component
|
8
|
+
module Component
|
9
|
+
# Creates the Component with inputs and outputs
|
10
|
+
# @param opts [Hash] options to create the Component with
|
11
|
+
# @option opts [Array<Input>] :inputs The array of inputs to use
|
9
12
|
def initialize(opts = {})
|
10
13
|
@inputs = opts[:inputs] ||
|
11
14
|
input_count.times.collect { Circuits::Terminal::Input.new }
|
12
15
|
@outputs = output_count.times.collect { Circuits::Terminal::Output.new }
|
13
16
|
setup
|
14
|
-
end
|
17
|
+
end
|
18
|
+
|
19
|
+
# Does the internal computation and sets the outputs
|
20
|
+
def tick
|
21
|
+
fail NotImplementedError
|
22
|
+
end
|
23
|
+
|
24
|
+
# Sets all the outputs expose what was set in #tick
|
25
|
+
def tock
|
26
|
+
outputs.each(&:tock)
|
27
|
+
end
|
15
28
|
|
16
29
|
attr_reader :inputs, :outputs
|
17
|
-
|
30
|
+
|
31
|
+
private
|
32
|
+
|
18
33
|
def input_count
|
19
34
|
fail NotImplementedError
|
20
35
|
end
|
21
|
-
|
36
|
+
|
22
37
|
def output_count
|
23
38
|
fail NotImplementedError
|
24
39
|
end
|
25
|
-
|
40
|
+
|
26
41
|
def setup
|
27
|
-
end
|
28
|
-
|
29
|
-
def tick
|
30
|
-
fail NotImplementedError
|
31
|
-
end
|
32
|
-
|
33
|
-
def tock
|
34
|
-
outputs.each(&:tock)
|
35
|
-
end
|
42
|
+
end
|
36
43
|
end
|
37
44
|
end
|
@@ -1,17 +1,26 @@
|
|
1
|
+
require 'circuits/terminal'
|
2
|
+
|
1
3
|
module Circuits
|
2
4
|
module Terminal
|
3
5
|
# Reads from a single output, only assigned to one component
|
4
6
|
class Input
|
7
|
+
# Creates the input
|
8
|
+
# @param opts [Hash] Options to create the Input with
|
9
|
+
# @option opts [Component::Output] :output The output to read from
|
5
10
|
def initialize(opts = {})
|
6
11
|
@output = opts[:output] || Circuits::Terminal::Output.new
|
7
12
|
end
|
8
13
|
|
9
14
|
attr_accessor :output
|
10
15
|
|
16
|
+
# Forward get to the output
|
17
|
+
# @return [TrueClass, FalseClass] The state of the output
|
11
18
|
def get
|
12
19
|
output.get
|
13
20
|
end
|
14
21
|
|
22
|
+
# Create a new output to use
|
23
|
+
# @param [TrueClass, FalseClass] The state of the output to create
|
15
24
|
def set(s)
|
16
25
|
@output = Circuits::Terminal::Output.new(state: s)
|
17
26
|
end
|
@@ -1,20 +1,27 @@
|
|
1
|
+
require 'circuits/terminal'
|
2
|
+
|
1
3
|
module Circuits
|
2
4
|
module Terminal
|
3
|
-
#
|
5
|
+
# Belongs to a single component, gets set for reading by inputs
|
4
6
|
class Output
|
5
7
|
def initialize(opts = {})
|
6
8
|
@next_state = opts[:state] || false
|
7
9
|
@state = opts[:state] || false
|
8
10
|
end
|
9
11
|
|
12
|
+
# Gets the state of the output
|
13
|
+
# @return [TrueClass, FalseClass] The state of the output
|
10
14
|
def get
|
11
15
|
state
|
12
16
|
end
|
13
17
|
|
18
|
+
# Saves the state
|
19
|
+
# @param [TrueClass, FalseClass] The next desired state of the output
|
14
20
|
def set(s)
|
15
21
|
@next_state = s
|
16
22
|
end
|
17
23
|
|
24
|
+
# Sets the state to be the last state passed by #set
|
18
25
|
def tock
|
19
26
|
@state = next_state
|
20
27
|
end
|
data/lib/circuits/version.rb
CHANGED
@@ -11,26 +11,6 @@ class MockComponent
|
|
11
11
|
end
|
12
12
|
|
13
13
|
describe Circuits::Component do
|
14
|
-
subject { MockComponent.new }
|
15
|
-
|
16
|
-
describe '#input_count' do
|
17
|
-
it 'is not implemented' do
|
18
|
-
expect { subject.input_count }.to raise_error(NotImplementedError)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
|
22
|
-
describe '#output_count' do
|
23
|
-
it 'is not implemented' do
|
24
|
-
expect { subject.output_count }.to raise_error(NotImplementedError)
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
describe '#tick' do
|
29
|
-
it 'is not implemented' do
|
30
|
-
expect { subject.tick }.to raise_error(NotImplementedError)
|
31
|
-
end
|
32
|
-
end
|
33
|
-
|
34
14
|
describe '#tock' do
|
35
15
|
let(:outputs) { [double('output')] }
|
36
16
|
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: circuits
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Henry Muru Paenga
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-10-
|
11
|
+
date: 2015-10-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -92,6 +92,7 @@ files:
|
|
92
92
|
- lib/circuits/component/sr_nor.rb
|
93
93
|
- lib/circuits/component/xnor.rb
|
94
94
|
- lib/circuits/component/xor.rb
|
95
|
+
- lib/circuits/terminal.rb
|
95
96
|
- lib/circuits/terminal/input.rb
|
96
97
|
- lib/circuits/terminal/output.rb
|
97
98
|
- lib/circuits/version.rb
|