Skip to content

Commit

Permalink
pass rubocop
Browse files Browse the repository at this point in the history
  • Loading branch information
meringu committed Nov 1, 2015
1 parent b5cf851 commit 362a967
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 22 deletions.
65 changes: 45 additions & 20 deletions lib/circuits/component/full_adder.rb
Expand Up @@ -8,38 +8,63 @@ module Component
# Full 1-bit Adder
class FullAdder < Base
def initialize
and_in = And.new
and_carry = And.new
or_gate = Or.new
xor_in = Xor.new
xor_out = Xor.new
sub_components = create_sub_components
super(inputs: [:a, :b, :c_in],
outputs: [:s, :c_out],
sub_components: [and_in, and_carry, or_gate, xor_in, xor_out],
sub_components: sub_components.map { |_, v| v },
ticks: 3)
link and_in, and_carry, or_gate, xor_in, xor_out
link sub_components
end

private

def link(and_in, and_carry, or_gate, xor_in, xor_out)
and_in.a.set a
and_in.b.set b
def create_sub_components
{
and_in: And.new,
and_carry: And.new,
or_gate: Or.new,
xor_in: Xor.new,
xor_out: Xor.new
}
end

and_carry.a.set xor_in.out
and_carry.b.set c_in
def link(sub_components)
link_and_in sub_components
link_and_carry sub_components
link_or_gate sub_components
link_xor_in sub_components
link_xor_out sub_components
link_outputs sub_components
end

or_gate.a.set and_in.out
or_gate.b.set and_carry.out
def link_and_in(sub_components)
sub_components[:and_in].a.set a
sub_components[:and_in].b.set b
end

xor_in.a.set a
xor_in.b.set b
def link_and_carry(sub_components)
sub_components[:and_carry].a.set sub_components[:xor_in].out
sub_components[:and_carry].b.set c_in
end

xor_out.a.set xor_in.out
xor_out.b.set c_in
def link_or_gate(sub_components)
sub_components[:or_gate].a.set sub_components[:and_in].out
sub_components[:or_gate].b.set sub_components[:and_carry].out
end

def link_xor_in(sub_components)
sub_components[:xor_in].a.set a
sub_components[:xor_in].b.set b
end

def link_xor_out(sub_components)
sub_components[:xor_out].a.set sub_components[:xor_in].out
sub_components[:xor_out].b.set c_in
end

s.set xor_out.out
c_out.set or_gate.out
def link_outputs(sub_components)
s.set sub_components[:xor_out].out
c_out.set sub_components[:or_gate].out
end
end
end
Expand Down
8 changes: 6 additions & 2 deletions lib/circuits/component/half_adder.rb
Expand Up @@ -13,16 +13,20 @@ def initialize
outputs: [:s, :c],
sub_components: [and_gate, xor_gate],
ticks: 1)
link and_gate, xor_gate
link_internals and_gate, xor_gate
link_outputs and_gate, xor_gate
end

private

def link(and_gate, xor_gate)
def link_internals(and_gate, xor_gate)
and_gate.a.set a
and_gate.b.set b
xor_gate.a.set a
xor_gate.b.set b
end

def link_outputs(and_gate, xor_gate)
s.set xor_gate.out
c.set and_gate.out
end
Expand Down

0 comments on commit 362a967

Please sign in to comment.