Restructure code
This commit is contained in:
parent
7476eed452
commit
9192bc9684
7
lib/rcircuit.rb
Normal file
7
lib/rcircuit.rb
Normal file
@ -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"
|
@ -1,6 +1,3 @@
|
||||
require_relative "gate.rb"
|
||||
require_relative "port.rb"
|
||||
|
||||
# Represents an AND gate
|
||||
class AndGate < Gate
|
||||
|
47
lib/rcircuit/device.rb
Normal file
47
lib/rcircuit/device.rb
Normal file
@ -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
|
@ -1,5 +1,3 @@
|
||||
require_relative "port.rb"
|
||||
|
||||
# @abstract Subclass and override {#inputs_changed} to implement a gate
|
||||
## Base class for gates
|
||||
class Gate
|
@ -1,6 +1,3 @@
|
||||
require_relative "gate.rb"
|
||||
require_relative "port.rb"
|
||||
|
||||
# Represents a NOT gate
|
||||
class NotGate < Gate
|
||||
|
@ -1,6 +1,3 @@
|
||||
require_relative "gate.rb"
|
||||
require_relative "port.rb"
|
||||
|
||||
# Represents an OR gate
|
||||
class OrGate < Gate
|
||||
|
@ -12,9 +12,6 @@ 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
|
||||
def initialize(width, name="")
|
@ -1,6 +1,3 @@
|
||||
require_relative "gate.rb"
|
||||
require_relative "port.rb"
|
||||
|
||||
# Represents an XOR gate
|
||||
class XorGate < Gate
|
||||
|
@ -1,4 +1,3 @@
|
||||
require_relative "../and.rb"
|
||||
describe AndGate do
|
||||
it "should AND together all inputs" do
|
||||
table=[
|
||||
|
@ -1,6 +1,3 @@
|
||||
require_relative "../port.rb"
|
||||
require_relative "../gate.rb"
|
||||
|
||||
describe Gate do
|
||||
let(:klass) do
|
||||
Class.new(Gate) do
|
||||
|
@ -1,4 +1,3 @@
|
||||
require_relative "../not.rb"
|
||||
describe NotGate do
|
||||
it "should NOT the input" do
|
||||
table=[
|
||||
|
@ -1,4 +1,3 @@
|
||||
require_relative "../or.rb"
|
||||
describe OrGate do
|
||||
it "should OR together all inputs" do
|
||||
table=[
|
||||
|
@ -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)
|
||||
|
1
spec/spec_helper.rb
Normal file
1
spec/spec_helper.rb
Normal file
@ -0,0 +1 @@
|
||||
require_relative "../lib/rcircuit.rb"
|
@ -1,4 +1,3 @@
|
||||
require_relative "../xor.rb"
|
||||
describe XorGate do
|
||||
it "should XOR together all inputs" do
|
||||
table=[
|
||||
|
Loading…
Reference in New Issue
Block a user