circuits 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- NzM3ZDQ5NWZkNzAxNDNiYTA5MTA3NzdiNzNhOWE5MjJkZjM5YjUxYg==
4
+ OTFhZjhiMjI3ODFkN2IzMmUyOGUwMGFkYjBiZTJhZmQ3YjVmMDhkOA==
5
5
  data.tar.gz: !binary |-
6
- NDYzMDY5ZmRhN2E2MTI3MGE1MGI3ZDM1ZDRiOWM0MDBjYTJjNmY3Yg==
6
+ M2RkOGE5YjkwOWNhNjA5NzcxOWM0ZTc4OTNiYTAxZDk2YTcxZmFmYg==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- ZDYxNDM1MTM3ODk1YmIzMDMwNWM0YzYzYTc0MzVmZWRkNDhlY2JiZjU0ZWEy
10
- OThjMDA4ZGQ3MWUwM2JjZDMxYjQ0ZWNkODk5ZGZhNGMyYzY0MDRmNzgzZjVi
11
- MThiZWMyNmQ5N2JmNWM4YWYwYzQ5ODNkNzgwNzZhOWE4MDg3YTc=
9
+ NWExY2I5NGI4NDEyZGM4ZDFhZmY0NjdmYzM5OWIxYWY0Y2IwYWNlMTg5NjRj
10
+ YjJlYzFiMDYxZmFlZjdmMzIwY2I1ZWQ0MmJmZGJkMDZlYTFkNTZmMjk2YjVk
11
+ NmRmNGNhZDU2Y2YzYTE0Y2JhNGE2MTgxNjIwODgzY2Q3ZTIxYzk=
12
12
  data.tar.gz: !binary |-
13
- NGJlYTEyYTlhMjU4ZThjYTgxNGY0M2U4ZjBjODJkY2U0MGYwMTFlMjE5MWNm
14
- YmU5ZDA5NDY2ZmFmNDc3NjExZjE3ZTBkMDllOTY3NGVmYTY1NjJmY2IwNDE1
15
- MjAyMDBhMWQ3NWY0NmQ5MDJkYTlmMWM1MGQwYmNjYmMwYWE4MTI=
13
+ N2E3YjI0MjkyYjBjN2I0YjA0NTZjYjVkMmM3ZGRhMWNlODNkOTIwNzJlMTdm
14
+ OTgwMGJjNDBjYWMyZjhhOTIxZDcwNDNjNDM1YzkyY2U5NmJhNTJiNTI4ZmEw
15
+ YzExMTc5MTM5MWRmNDQzYzhkMTA3NTA4YTk2OTcwZTc5YjE4NGI=
@@ -5,117 +5,117 @@ 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
9
- # Creates the Component with inputs and outputs
10
- # @param opts [Hash] options to create the Component with
11
- # @option opts [Array<Input>, FixNum] :inputs The array of inputs to use, or
12
- # the number of inputs to create
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>, FixNum] :inputs The array of inputs to use, or
12
+ # the number of inputs to create
13
13
  def initialize(opts = {})
14
14
  set_defaults
15
15
  create_inputs opts
16
16
  create_outputs opts
17
17
  create_port_mappings
18
18
  setup
19
- end
20
-
21
- # Does the internal computation and sets the outputs
22
- def tick
23
- fail NotImplementedError
24
- end
25
-
26
- # Sets all the outputs expose what was set in #tick
27
- def tock
28
- outputs.each(&:tock)
29
- end
30
-
31
- # Gets the teminal assigned to the port
32
- # @param port [Symbol] The symbol that represents the terminal
33
- # @return [Input, Output] The terminal
34
- def [](port)
35
- p = @port_mappings[port]
36
- case p[:type]
37
- when :input
38
- @inputs[p[:number]]
39
- when :output
40
- @outputs[p[:number]]
41
- end
42
- end
43
-
44
- # Assigns to an input or output
45
- # @param port [Symbol] The symbol that represents the terminal
46
- # @param terminal [Input, Output] The terminal to assign
47
- # @return [Input, Output] The terminal that was passed in
48
- def []=(port, terminal)
49
- p = @port_mappings[port]
50
- case p[:type]
51
- when :input
52
- @inputs[p[:number]] = terminal
53
- when :output
54
- @outputs[p[:number]] = terminal
55
- end
56
- end
57
-
58
- # the inputs of this component
59
- attr_reader :inputs
60
-
61
- # the outputs of this component
62
- attr_reader :outputs
63
-
64
- private
65
-
66
- def create_inputs(opts)
67
- if opts[:inputs].class == Array
68
- @inputs = opts[:inputs]
69
- @input_count = @inputs.length
70
- elsif opts[:inputs].class == Fixnum
71
- @input_count = opts[:inputs]
72
- end
73
- @inputs ||= @input_count.times.collect { Circuits::Terminal::Input.new }
74
- end
75
-
76
- def create_outputs(opts)
77
- if opts[:outputs].class == Array
78
- @outputs = opts[:outputs]
79
- @output_count = @outputs.length
80
- elsif opts[:outputs].class == Fixnum
81
- @output_count = opts[:outputs]
82
- end
83
- @outputs ||= @output_count.times.collect do
84
- Circuits::Terminal::Output.new
85
- end
86
- end
87
-
88
- def create_port_mappings
89
- return @port_mappings unless @port_mappings.nil?
90
- @port_mappings = {}
91
- input_mappings.each { |x| @port_mappings.merge!(x) }
92
- output_mappings.each { |x| @port_mappings.merge!(x) }
93
- @port_mappings
94
- end
95
-
96
- def input_mappings
97
- return [{ in: { type: :input, number: 0 } }] if @input_count == 1
98
- @input_count.times.collect do |i|
99
- { num_to_port(i) => { type: :input, number: i } }
100
- end
101
- end
102
-
103
- def output_mappings
104
- return[{ out: { type: :output, number: 0 } }] if @output_count == 1
105
- @output_count.times.collect do |i|
106
- { num_to_port(i + @input_count) => { type: :output, number: i } }
107
- end
108
- end
109
-
110
- def num_to_port(i)
111
- (i + 'a'.ord).chr.to_sym
112
- end
113
-
114
- def set_defaults
115
- fail NotImplementedError
116
- end
117
-
19
+ end
20
+
21
+ # Does the internal computation and sets the outputs
22
+ def tick
23
+ fail NotImplementedError
24
+ end
25
+
26
+ # Sets all the outputs expose what was set in #tick
27
+ def tock
28
+ outputs.each(&:tock)
29
+ end
30
+
31
+ # Gets the teminal assigned to the port
32
+ # @param port [Symbol] The symbol that represents the terminal
33
+ # @return [Input, Output] The terminal
34
+ def [](port)
35
+ p = @port_mappings[port]
36
+ case p[:type]
37
+ when :input
38
+ @inputs[p[:number]]
39
+ when :output
40
+ @outputs[p[:number]]
41
+ end
42
+ end
43
+
44
+ # Assigns to an input or output
45
+ # @param port [Symbol] The symbol that represents the terminal
46
+ # @param terminal [Input, Output] The terminal to assign
47
+ # @return [Input, Output] The terminal that was passed in
48
+ def []=(port, terminal)
49
+ p = @port_mappings[port]
50
+ case p[:type]
51
+ when :input
52
+ @inputs[p[:number]] = terminal
53
+ when :output
54
+ @outputs[p[:number]] = terminal
55
+ end
56
+ end
57
+
58
+ # the inputs of this component
59
+ attr_reader :inputs
60
+
61
+ # the outputs of this component
62
+ attr_reader :outputs
63
+
64
+ private
65
+
66
+ def create_inputs(opts)
67
+ if opts[:inputs].class == Array
68
+ @inputs = opts[:inputs]
69
+ @input_count = @inputs.length
70
+ elsif opts[:inputs].class == Fixnum
71
+ @input_count = opts[:inputs]
72
+ end
73
+ @inputs ||= @input_count.times.collect { Circuits::Terminal::Input.new }
74
+ end
75
+
76
+ def create_outputs(opts)
77
+ if opts[:outputs].class == Array
78
+ @outputs = opts[:outputs]
79
+ @output_count = @outputs.length
80
+ elsif opts[:outputs].class == Fixnum
81
+ @output_count = opts[:outputs]
82
+ end
83
+ @outputs ||= @output_count.times.collect do
84
+ Circuits::Terminal::Output.new
85
+ end
86
+ end
87
+
88
+ def create_port_mappings
89
+ return @port_mappings unless @port_mappings.nil?
90
+ @port_mappings = {}
91
+ input_mappings.each { |x| @port_mappings.merge!(x) }
92
+ output_mappings.each { |x| @port_mappings.merge!(x) }
93
+ @port_mappings
94
+ end
95
+
96
+ def input_mappings
97
+ return [{ in: { type: :input, number: 0 } }] if @input_count == 1
98
+ @input_count.times.collect do |i|
99
+ { num_to_port(i) => { type: :input, number: i } }
100
+ end
101
+ end
102
+
103
+ def output_mappings
104
+ return[{ out: { type: :output, number: 0 } }] if @output_count == 1
105
+ @output_count.times.collect do |i|
106
+ { num_to_port(i + @input_count) => { type: :output, number: i } }
107
+ end
108
+ end
109
+
110
+ def num_to_port(i)
111
+ (i + 'a'.ord).chr.to_sym
112
+ end
113
+
114
+ def set_defaults
115
+ fail NotImplementedError
116
+ end
117
+
118
118
  def setup
119
- end
119
+ end
120
120
  end
121
121
  end
@@ -5,14 +5,14 @@ module Circuits
5
5
  # Logical NOT Operator
6
6
  class Not
7
7
  include Component
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
-
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
+
16
16
  def set_defaults
17
17
  @input_count = 1
18
18
  @output_count = 1
@@ -8,10 +8,7 @@ module Circuits
8
8
 
9
9
  # Computes the outputs based on the inputs and previous state
10
10
  def tick
11
- 2.times.each do
12
- sub_components.each(&:tick)
13
- sub_components.each(&:tock)
14
- end
11
+ update_internal_components
15
12
  self[:q].set nand_1[:out].get
16
13
  self[:not_q].set nand_2[:out].get
17
14
  end
@@ -20,6 +17,19 @@ module Circuits
20
17
 
21
18
  attr_reader :nand_1, :nand_2, :sub_components
22
19
 
20
+ def create_internal_components
21
+ @nand_1 = Nand.new
22
+ @nand_2 = Nand.new
23
+ @sub_components = [@nand_1, @nand_2]
24
+ end
25
+
26
+ def link_internal_components
27
+ nand_1[:a] = self[:not_s]
28
+ nand_2[:a] = self[:not_r]
29
+ nand_1[:b] = nand_2[:out]
30
+ nand_2[:b] = nand_1[:out]
31
+ end
32
+
23
33
  def set_defaults
24
34
  @input_count = 2
25
35
  @output_count = 2
@@ -32,13 +42,15 @@ module Circuits
32
42
  end
33
43
 
34
44
  def setup
35
- @nand_1 = Nand.new
36
- @nand_2 = Nand.new
37
- @sub_components = [@nand_1, @nand_2]
38
- nand_1[:a] = self[:not_s]
39
- nand_2[:a] = self[:not_r]
40
- nand_1[:b] = nand_2[:out]
41
- nand_2[:b] = nand_1[:out]
45
+ create_internal_components
46
+ link_internal_components
47
+ end
48
+
49
+ def update_internal_components
50
+ 2.times.each do
51
+ sub_components.each(&:tick)
52
+ sub_components.each(&:tock)
53
+ end
42
54
  end
43
55
  end
44
56
  end
@@ -8,10 +8,7 @@ module Circuits
8
8
 
9
9
  # Computes the outputs based on the inputs and previous state
10
10
  def tick
11
- 2.times.each do
12
- sub_components.each(&:tick)
13
- sub_components.each(&:tock)
14
- end
11
+ update_internal_components
15
12
  self[:q].set nor_1[:out].get
16
13
  self[:not_q].set nor_2[:out].get
17
14
  end
@@ -20,6 +17,19 @@ module Circuits
20
17
 
21
18
  attr_reader :nor_1, :nor_2, :sub_components
22
19
 
20
+ def create_internal_components
21
+ @nor_1 = Nor.new
22
+ @nor_2 = Nor.new
23
+ @sub_components = [@nor_1, @nor_2]
24
+ end
25
+
26
+ def link_internal_components
27
+ nor_1[:a] = self[:r]
28
+ nor_2[:a] = self[:s]
29
+ nor_1[:b] = nor_2[:out]
30
+ nor_2[:b] = nor_1[:out]
31
+ end
32
+
23
33
  def set_defaults
24
34
  @input_count = 2
25
35
  @output_count = 2
@@ -32,13 +42,15 @@ module Circuits
32
42
  end
33
43
 
34
44
  def setup
35
- @nor_1 = Nor.new
36
- @nor_2 = Nor.new
37
- @sub_components = [@nor_1, @nor_2]
38
- nor_1[:a] = self[:r]
39
- nor_2[:a] = self[:s]
40
- nor_1[:b] = nor_2[:out]
41
- nor_2[:b] = nor_1[:out]
45
+ create_internal_components
46
+ link_internal_components
47
+ end
48
+
49
+ def update_internal_components
50
+ 2.times.each do
51
+ sub_components.each(&:tick)
52
+ sub_components.each(&:tock)
53
+ end
42
54
  end
43
55
  end
44
56
  end
@@ -1,5 +1,5 @@
1
- module Circuits
2
- # A terminal holds a logical state, it can be attatched to components
3
- module Terminal
4
- end
5
- end
1
+ module Circuits
2
+ # A terminal holds a logical state, it can be attatched to components
3
+ module Terminal
4
+ end
5
+ 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.4.2'
4
+ VERSION = '0.4.3'
5
5
  end
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.4.2
4
+ version: 0.4.3
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-26 00:00:00.000000000 Z
11
+ date: 2015-10-27 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler