circuits 0.11.1 → 0.11.2

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 CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- ZDQ3YmZhMTQxMjJmODRhZDQ1MDA0MWU2ZTc3NTIxNTljY2E3MzA2OQ==
4
+ ZTYwYmM0YTZmMjVmMzQxZWU5M2Y0ZTUxNjAwNWVlM2U4NDVjOTA0YQ==
5
5
  data.tar.gz: !binary |-
6
- NzJhYTU4NjRhNjhmMWFmOWJjZjM5MTg5YzhkZmFhYzY4ODYyZjdhNQ==
6
+ OGQ3ZTMzMjA4ODdiMTZiOGY1YzQ4YTE5ZjUyMWY5MTZlODhiOGI5Nw==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- NDkxODk0MmRlZTk1MjdlMGRmODAzMWExZDNjMDllNWVjYWVkMTM3NTcyZjFh
10
- ZWQwYmI2MzQ5ZjZkMTZjMGZkMWE4ZTdlM2Y5NjJhZmI2N2I1ZWZhMWZmYWZk
11
- MTY4M2E2NDE3YTYxNmRjZjQ0NzVjZWVmM2NkMWMxYWY1NTMxNjk=
9
+ YTliMmUwNWJlZjg0MGZjYjFkZDlkMzdjODgwOWRjMmUxZWU5NDBmYjU4MWNi
10
+ ZjJmOWUwMWViZDVmZmY0ODhmZjg5ODVkMDE1MTcyMzM1NzY4N2FjNzRhNWU0
11
+ Nzg0YWNjOTI3ZWRjMGI1MTg0NTY1MTMwZWE2YTE2NDdjNzM3YjA=
12
12
  data.tar.gz: !binary |-
13
- N2VmZWMyZWQ5NzViMjUxNGNlNThiY2I0MzlhYzc5NjE0ZGUyOThkYjlhMmY1
14
- YzExOWQxZTlmNDg5MDZkYjc0YjMxYzcwNDRlNWQ5ZjA3MzZlNzg2ZjQyNzJl
15
- NTI4OGViNDMzNzA2OTA3NzcxZDNjMTM1MzFjM2Q0Njg0YzJjNzY=
13
+ Y2I5ZWNiM2EwMWQ1YWQxNGRhOWI4ZTQ4YjMyMjE1MTQwNjRkYzAxNDg1YWJl
14
+ ZWViZTc0OTBiNmU0ZGYwMmRkZjk1YjU3MDEyMTA5MTc4M2RmN2QzYTIxNmFh
15
+ MjljODNkZjAzZTJiMWNmZDJjM2I2NDU5NzY3NGQ2MWI5YzkxMWE=
@@ -0,0 +1,71 @@
1
+ require 'circuits/component/base'
2
+ require 'circuits/component/and'
3
+ require 'circuits/component/or'
4
+ require 'circuits/component/xor'
5
+
6
+ module Circuits
7
+ module Component
8
+ # Full 1-bit Adder
9
+ class FullAdder < Base
10
+ def initialize
11
+ sub_components = create_sub_components
12
+ super(inputs: [:a, :b, :c_in],
13
+ outputs: [:s, :c_out],
14
+ sub_components: sub_components.map { |_, v| v },
15
+ ticks: 3)
16
+ link sub_components
17
+ end
18
+
19
+ private
20
+
21
+ def create_sub_components
22
+ {
23
+ and_in: And.new,
24
+ and_carry: And.new,
25
+ or_gate: Or.new,
26
+ xor_in: Xor.new,
27
+ xor_out: Xor.new
28
+ }
29
+ end
30
+
31
+ def link(sub_components)
32
+ link_and_in sub_components
33
+ link_and_carry sub_components
34
+ link_or_gate sub_components
35
+ link_xor_in sub_components
36
+ link_xor_out sub_components
37
+ link_outputs sub_components
38
+ end
39
+
40
+ def link_and_in(sub_components)
41
+ sub_components[:and_in].a.set a
42
+ sub_components[:and_in].b.set b
43
+ end
44
+
45
+ def link_and_carry(sub_components)
46
+ sub_components[:and_carry].a.set sub_components[:xor_in].out
47
+ sub_components[:and_carry].b.set c_in
48
+ end
49
+
50
+ def link_or_gate(sub_components)
51
+ sub_components[:or_gate].a.set sub_components[:and_in].out
52
+ sub_components[:or_gate].b.set sub_components[:and_carry].out
53
+ end
54
+
55
+ def link_xor_in(sub_components)
56
+ sub_components[:xor_in].a.set a
57
+ sub_components[:xor_in].b.set b
58
+ end
59
+
60
+ def link_xor_out(sub_components)
61
+ sub_components[:xor_out].a.set sub_components[:xor_in].out
62
+ sub_components[:xor_out].b.set c_in
63
+ end
64
+
65
+ def link_outputs(sub_components)
66
+ s.set sub_components[:xor_out].out
67
+ c_out.set sub_components[:or_gate].out
68
+ end
69
+ end
70
+ end
71
+ end
@@ -0,0 +1,35 @@
1
+ require 'circuits/component/base'
2
+ require 'circuits/component/and'
3
+ require 'circuits/component/xor'
4
+
5
+ module Circuits
6
+ module Component
7
+ # Half 1-bit Adder
8
+ class HalfAdder < Base
9
+ def initialize
10
+ and_gate = And.new
11
+ xor_gate = Xor.new
12
+ super(inputs: 2,
13
+ outputs: [:s, :c],
14
+ sub_components: [and_gate, xor_gate],
15
+ ticks: 1)
16
+ link_internals and_gate, xor_gate
17
+ link_outputs and_gate, xor_gate
18
+ end
19
+
20
+ private
21
+
22
+ def link_internals(and_gate, xor_gate)
23
+ and_gate.a.set a
24
+ and_gate.b.set b
25
+ xor_gate.a.set a
26
+ xor_gate.b.set b
27
+ end
28
+
29
+ def link_outputs(and_gate, xor_gate)
30
+ s.set xor_gate.out
31
+ c.set and_gate.out
32
+ end
33
+ end
34
+ end
35
+ end
@@ -1,5 +1,5 @@
1
1
  # Circuits allows you to express logical circuits in code
2
2
  module Circuits
3
3
  # The version of the Circuits gem
4
- VERSION = '0.11.1'
4
+ VERSION = '0.11.2'
5
5
  end
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+ require 'circuits/component/full_adder'
3
+
4
+ describe Circuits::Component::FullAdder do
5
+ describe '#tick' do
6
+ subject { Circuits::Component::FullAdder.new }
7
+
8
+ context 'no overflow' do
9
+ context '0 + 0' do
10
+ it '= 0' do
11
+ subject.a.set false
12
+ subject.b.set false
13
+ subject.tick
14
+ subject.tock
15
+ expect(subject.s.get).to eq false
16
+ expect(subject.c_out.get).to eq false
17
+ end
18
+ end
19
+
20
+ context '0 + 1' do
21
+ it '= 1' do
22
+ subject.a.set false
23
+ subject.b.set true
24
+ subject.tick
25
+ subject.tock
26
+ expect(subject.s.get).to eq true
27
+ expect(subject.c_out.get).to eq false
28
+ end
29
+ end
30
+
31
+ context '1 + 0' do
32
+ it '= 1' do
33
+ subject.a.set true
34
+ subject.b.set false
35
+ subject.tick
36
+ subject.tock
37
+ expect(subject.s.get).to eq true
38
+ expect(subject.c_out.get).to eq false
39
+ end
40
+ end
41
+
42
+ context '1 + 1' do
43
+ it '= 10' do
44
+ subject.a.set true
45
+ subject.b.set true
46
+ subject.tick
47
+ subject.tock
48
+ expect(subject.s.get).to eq false
49
+ expect(subject.c_out.get).to eq true
50
+ end
51
+ end
52
+ end
53
+
54
+ context 'overflow' do
55
+ context '0 + 0 (+ 1)' do
56
+ it '= 1' do
57
+ subject.a.set false
58
+ subject.b.set false
59
+ subject.c_in.set true
60
+ subject.tick
61
+ subject.tock
62
+ expect(subject.s.get).to eq true
63
+ expect(subject.c_out.get).to eq false
64
+ end
65
+ end
66
+
67
+ context '0 + 1 (+ 1)' do
68
+ it '= 10' do
69
+ subject.a.set false
70
+ subject.b.set true
71
+ subject.c_in.set true
72
+ subject.tick
73
+ subject.tock
74
+ expect(subject.s.get).to eq false
75
+ expect(subject.c_out.get).to eq true
76
+ end
77
+ end
78
+
79
+ context '1 + 0 (+ 1)' do
80
+ it '= 10' do
81
+ subject.a.set true
82
+ subject.b.set false
83
+ subject.c_in.set true
84
+ subject.tick
85
+ subject.tock
86
+ expect(subject.s.get).to eq false
87
+ expect(subject.c_out.get).to eq true
88
+ end
89
+ end
90
+
91
+ context '1 + 1 (+ 1)' do
92
+ it '= 11' do
93
+ subject.a.set true
94
+ subject.b.set true
95
+ subject.c_in.set true
96
+ subject.tick
97
+ subject.tock
98
+ expect(subject.s.get).to eq true
99
+ expect(subject.c_out.get).to eq true
100
+ end
101
+ end
102
+ end
103
+ end
104
+ end
@@ -0,0 +1,52 @@
1
+ require 'spec_helper'
2
+ require 'circuits/component/half_adder'
3
+
4
+ describe Circuits::Component::HalfAdder do
5
+ describe '#tick' do
6
+ subject { Circuits::Component::HalfAdder.new }
7
+
8
+ context '0 + 0' do
9
+ it '= 0' do
10
+ subject.a.set false
11
+ subject.b.set false
12
+ subject.tick
13
+ subject.tock
14
+ expect(subject.s.get).to eq false
15
+ expect(subject.c.get).to eq false
16
+ end
17
+ end
18
+
19
+ context '0 + 1' do
20
+ it '= 1' do
21
+ subject.a.set false
22
+ subject.b.set true
23
+ subject.tick
24
+ subject.tock
25
+ expect(subject.s.get).to eq true
26
+ expect(subject.c.get).to eq false
27
+ end
28
+ end
29
+
30
+ context '1 + 0' do
31
+ it '= 1' do
32
+ subject.a.set true
33
+ subject.b.set false
34
+ subject.tick
35
+ subject.tock
36
+ expect(subject.s.get).to eq true
37
+ expect(subject.c.get).to eq false
38
+ end
39
+ end
40
+
41
+ context '1 + 1' do
42
+ it '= 10' do
43
+ subject.a.set true
44
+ subject.b.set true
45
+ subject.tick
46
+ subject.tock
47
+ expect(subject.s.get).to eq false
48
+ expect(subject.c.get).to eq true
49
+ end
50
+ end
51
+ end
52
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: circuits
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.1
4
+ version: 0.11.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Henry Muru Paenga
@@ -127,6 +127,8 @@ files:
127
127
  - lib/circuits/component/and.rb
128
128
  - lib/circuits/component/base.rb
129
129
  - lib/circuits/component/d.rb
130
+ - lib/circuits/component/full_adder.rb
131
+ - lib/circuits/component/half_adder.rb
130
132
  - lib/circuits/component/nand.rb
131
133
  - lib/circuits/component/nor.rb
132
134
  - lib/circuits/component/not.rb
@@ -142,6 +144,8 @@ files:
142
144
  - spec/unit/circuits/component/and_spec.rb
143
145
  - spec/unit/circuits/component/base_spec.rb
144
146
  - spec/unit/circuits/component/d_spec.rb
147
+ - spec/unit/circuits/component/full_adder_spec.rb
148
+ - spec/unit/circuits/component/half_adder_spec.rb
145
149
  - spec/unit/circuits/component/nand_spec.rb
146
150
  - spec/unit/circuits/component/nor_spec.rb
147
151
  - spec/unit/circuits/component/not_spec.rb
@@ -181,6 +185,8 @@ test_files:
181
185
  - spec/unit/circuits/component/and_spec.rb
182
186
  - spec/unit/circuits/component/base_spec.rb
183
187
  - spec/unit/circuits/component/d_spec.rb
188
+ - spec/unit/circuits/component/full_adder_spec.rb
189
+ - spec/unit/circuits/component/half_adder_spec.rb
184
190
  - spec/unit/circuits/component/nand_spec.rb
185
191
  - spec/unit/circuits/component/nor_spec.rb
186
192
  - spec/unit/circuits/component/not_spec.rb