Add adder device

This commit is contained in:
pjht 2018-07-12 06:55:33 -05:00
parent 9192bc9684
commit bc7fbac203
6 changed files with 28 additions and 2 deletions

View File

@ -1,2 +1,3 @@
source "https://rubygems.org"
gem "yard"
gem "pry"

View File

@ -1,12 +1,18 @@
GEM
remote: https://rubygems.org/
specs:
coderay (1.1.2)
method_source (0.9.0)
pry (0.11.3)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
yard (0.9.14)
PLATFORMS
ruby
DEPENDENCIES
pry
yard
BUNDLED WITH

6
console Executable file
View File

@ -0,0 +1,6 @@
#!/usr/bin/env ruby
require "pry"
require_relative "lib/rcircuit.rb"
a=Port.new(8)
b=Port.new(8)
Pry.start()

View File

@ -5,3 +5,4 @@ require_relative "rcircuit/not.rb"
require_relative "rcircuit/and.rb"
require_relative "rcircuit/or.rb"
require_relative "rcircuit/xor.rb"
require_relative "rcircuit/adder.rb"

13
lib/rcircuit/adder.rb Normal file
View File

@ -0,0 +1,13 @@
class Adder < Device
def initialize(width, init_args)
add_input("a", width)
add_input("b", width)
add_output("out", width)
init_assign(init_args)
@mask=(2**width)-1
end
def on_change(data_val)
out.setval((a.val+b.val)&@mask)
end
end

View File

@ -5,7 +5,6 @@ class Device
# @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) }
@ -38,7 +37,7 @@ class Device
hash.each do |name, port|
#check if there is a defined method (port) that matches
if self.respond_to?(name)
method(name).call(port)
instance_variable_get("@#{name}").connect(port)
else
raise ArgumentError, "No defined input '#{name}'"
end