Port now supports width

This commit is contained in:
pjht 2018-07-08 17:57:51 -05:00
parent 9d9e12ea87
commit 6f3db5ae4c
2 changed files with 28 additions and 15 deletions

27
port.rb
View File

@ -7,12 +7,22 @@ class Port
# @return [Integer] the current value of the port # @return [Integer] the current value of the port
attr_reader :val attr_reader :val
# @!attribute [r] width
# @return [Integer] the width of the port
attr_reader :width
# Returns a new instance of Port # Returns a new instance of Port
# @param name [String] The string name of the port # @overload set(width)
def initialize(name="") # @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="")
@connected=[] @connected=[]
@propagating=false @propagating=false
@val=0 @val=0
@width=width
@maxval = (2**@width)-1
@strname=name @strname=name
end end
@ -22,7 +32,11 @@ class Port
def setval(val) def setval(val)
# Prevent infinite loops when the connected port calls back when propagating. # Prevent infinite loops when the connected port calls back when propagating.
if !@propagating if !@propagating
@val=val if val<=@maxval
@val=val
else
raise ArgumentError,"#{val} is over maximum of #{@maxval}"
end
@propagating=true @propagating=true
propagate() propagate()
@propagating=false @propagating=false
@ -74,10 +88,3 @@ class Port
end end
end end
a=Port.new("a")
b=Port.new("b")
c=Port.new("c")
a.connect(b)
b.connect(c)
a.setval(10)

View File

@ -2,15 +2,16 @@ require_relative "../port.rb"
describe Port do describe Port do
it "should set the port's value when we call setval" do it "should set the port's value when we call setval" do
a=Port.new a=Port.new(4)
a.setval(1) a.setval(1)
expect(a).to eq 1 expect(a).to eq 1
end end
it "should propagate values when we call setval" do it "should propagate values when we call setval" do
a=Port.new a=Port.new(4)
b=Port.new b=Port.new(4)
c=Port.new c=Port.new(4)
d=Port.new d=Port.new(4)
#connect A-B and C-D #connect A-B and C-D
a.connect(b) a.connect(b)
@ -36,4 +37,9 @@ describe Port do
expect(c).to eq 4 expect(c).to eq 4
expect(d).to eq 4 expect(d).to eq 4
end end
it "should not allow value to go over max allowed by width" do
a=Port.new(4)
expect {a.setval(16)}.to raise_error ArgumentError
end
end end