From bc7fbac203f80d408437672286da29904286b6c7 Mon Sep 17 00:00:00 2001 From: pjht Date: Thu, 12 Jul 2018 06:55:33 -0500 Subject: [PATCH] Add adder device --- Gemfile | 1 + Gemfile.lock | 6 ++++++ console | 6 ++++++ lib/rcircuit.rb | 1 + lib/rcircuit/adder.rb | 13 +++++++++++++ lib/rcircuit/device.rb | 3 +-- 6 files changed, 28 insertions(+), 2 deletions(-) create mode 100755 console create mode 100644 lib/rcircuit/adder.rb diff --git a/Gemfile b/Gemfile index 896d28a..00c7933 100644 --- a/Gemfile +++ b/Gemfile @@ -1,2 +1,3 @@ source "https://rubygems.org" gem "yard" +gem "pry" diff --git a/Gemfile.lock b/Gemfile.lock index b3289ce..eb0675d 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/console b/console new file mode 100755 index 0000000..fa64462 --- /dev/null +++ b/console @@ -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() diff --git a/lib/rcircuit.rb b/lib/rcircuit.rb index 4115996..6c47c09 100644 --- a/lib/rcircuit.rb +++ b/lib/rcircuit.rb @@ -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" diff --git a/lib/rcircuit/adder.rb b/lib/rcircuit/adder.rb new file mode 100644 index 0000000..0ab0fc8 --- /dev/null +++ b/lib/rcircuit/adder.rb @@ -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 diff --git a/lib/rcircuit/device.rb b/lib/rcircuit/device.rb index bd0018c..5ef7826 100644 --- a/lib/rcircuit/device.rb +++ b/lib/rcircuit/device.rb @@ -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