diff --git a/.yardopts b/.yardopts index 873ce10..f6020b9 100644 --- a/.yardopts +++ b/.yardopts @@ -1 +1 @@ -*.rb --private --exclude old/* +--private --exclude old/* diff --git a/lib/rcircuit.rb b/lib/rcircuit.rb new file mode 100644 index 0000000..4115996 --- /dev/null +++ b/lib/rcircuit.rb @@ -0,0 +1,7 @@ +require_relative "rcircuit/port.rb" +require_relative "rcircuit/gate.rb" +require_relative "rcircuit/device.rb" +require_relative "rcircuit/not.rb" +require_relative "rcircuit/and.rb" +require_relative "rcircuit/or.rb" +require_relative "rcircuit/xor.rb" diff --git a/and.rb b/lib/rcircuit/and.rb similarity index 88% rename from and.rb rename to lib/rcircuit/and.rb index a42cfdd..34f40ae 100644 --- a/and.rb +++ b/lib/rcircuit/and.rb @@ -1,6 +1,3 @@ -require_relative "gate.rb" -require_relative "port.rb" - # Represents an AND gate class AndGate < Gate diff --git a/lib/rcircuit/device.rb b/lib/rcircuit/device.rb new file mode 100644 index 0000000..bd0018c --- /dev/null +++ b/lib/rcircuit/device.rb @@ -0,0 +1,47 @@ +# Class for devices such as adders and registers +class Device + # Add an input port + # @param name [String] Name of the port + # @param width [Integer] Width of the port + # @return [void] + def add_input(name, width=1) + self.class.class_eval{attr_reader name.to_sym} + port = Port.new(width) + instance_variable_set("@#{name}", port) + port.add_callback { |val| on_change(val) } + end + + # Add an output port + # @param name [String] Name of the port + # @param width [Integer] Width of the port + # @return [void] + def add_output(name, width=1) + self.class.class_eval{attr_reader name.to_sym} + port = Port.new(width) + instance_variable_set("@#{name}", port) + end + + # Add a subdevice + # @param name [String] Name of the subdevice + # @param device [Device] Device to add + # @return [void] + def define_device(name, device) + instance_variable_set("@#{name}", device) + self.class.class_eval{attr_reader name.to_sym} + end + + private + # Used to set the ports from the argument hash in initialize + # @param hash [Hash{String=>Port}] Hash of names to ports + # @return [void] + def init_assign(hash) + hash.each do |name, port| + #check if there is a defined method (port) that matches + if self.respond_to?(name) + method(name).call(port) + else + raise ArgumentError, "No defined input '#{name}'" + end + end + end +end diff --git a/gate.rb b/lib/rcircuit/gate.rb similarity index 98% rename from gate.rb rename to lib/rcircuit/gate.rb index a2897da..e1bc01f 100644 --- a/gate.rb +++ b/lib/rcircuit/gate.rb @@ -1,5 +1,3 @@ -require_relative "port.rb" - # @abstract Subclass and override {#inputs_changed} to implement a gate ## Base class for gates class Gate diff --git a/not.rb b/lib/rcircuit/not.rb similarity index 92% rename from not.rb rename to lib/rcircuit/not.rb index 2cfaf6a..3eb23c8 100644 --- a/not.rb +++ b/lib/rcircuit/not.rb @@ -1,6 +1,3 @@ -require_relative "gate.rb" -require_relative "port.rb" - # Represents a NOT gate class NotGate < Gate diff --git a/or.rb b/lib/rcircuit/or.rb similarity index 85% rename from or.rb rename to lib/rcircuit/or.rb index 2e1517c..ba78b4e 100644 --- a/or.rb +++ b/lib/rcircuit/or.rb @@ -1,6 +1,3 @@ -require_relative "gate.rb" -require_relative "port.rb" - # Represents an OR gate class OrGate < Gate diff --git a/port.rb b/lib/rcircuit/port.rb similarity index 91% rename from port.rb rename to lib/rcircuit/port.rb index 238dc8d..c09d68d 100644 --- a/port.rb +++ b/lib/rcircuit/port.rb @@ -12,11 +12,8 @@ class Port attr_reader :width # Returns a new instance of Port - # @overload set(width) - # @param width [Integer] The width of the port - # @overload set(width, name) - # @param width [Integer] The width of the port - # @param name [String] The name of the port + # @param width [Integer] The width of the port + # @param name [String] The name of the port def initialize(width, name="") @connected=[] @callbacks=[] diff --git a/xor.rb b/lib/rcircuit/xor.rb similarity index 86% rename from xor.rb rename to lib/rcircuit/xor.rb index 3e9728c..aeb9b1d 100644 --- a/xor.rb +++ b/lib/rcircuit/xor.rb @@ -1,6 +1,3 @@ -require_relative "gate.rb" -require_relative "port.rb" - # Represents an XOR gate class XorGate < Gate diff --git a/spec/and_spec.rb b/spec/and_spec.rb index 08291ef..62bfaad 100644 --- a/spec/and_spec.rb +++ b/spec/and_spec.rb @@ -1,4 +1,3 @@ -require_relative "../and.rb" describe AndGate do it "should AND together all inputs" do table=[ diff --git a/spec/gate_spec.rb b/spec/gate_spec.rb index 9865048..416071c 100644 --- a/spec/gate_spec.rb +++ b/spec/gate_spec.rb @@ -1,6 +1,3 @@ -require_relative "../port.rb" -require_relative "../gate.rb" - describe Gate do let(:klass) do Class.new(Gate) do diff --git a/spec/not_spec.rb b/spec/not_spec.rb index 6fd21a8..b26d63f 100644 --- a/spec/not_spec.rb +++ b/spec/not_spec.rb @@ -1,4 +1,3 @@ -require_relative "../not.rb" describe NotGate do it "should NOT the input" do table=[ diff --git a/spec/or_spec.rb b/spec/or_spec.rb index 384d121..c6996e5 100644 --- a/spec/or_spec.rb +++ b/spec/or_spec.rb @@ -1,4 +1,3 @@ -require_relative "../or.rb" describe OrGate do it "should OR together all inputs" do table=[ diff --git a/spec/port_spec.rb b/spec/port_spec.rb index 9c6b977..9ce56d7 100644 --- a/spec/port_spec.rb +++ b/spec/port_spec.rb @@ -1,5 +1,3 @@ -require_relative "../port.rb" - describe Port do it "should set the port's value when we call #setval" do a=Port.new(4) diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb new file mode 100644 index 0000000..a1b33b5 --- /dev/null +++ b/spec/spec_helper.rb @@ -0,0 +1 @@ +require_relative "../lib/rcircuit.rb" diff --git a/spec/xor_spec.rb b/spec/xor_spec.rb index ffad5db..bd9d4d7 100644 --- a/spec/xor_spec.rb +++ b/spec/xor_spec.rb @@ -1,4 +1,3 @@ -require_relative "../xor.rb" describe XorGate do it "should XOR together all inputs" do table=[