Embedded Systems
Spring 2024
Lab 6 2's complement adder/subtractor.
Name: Mason
Brady Email:
mrbrady1@fortlewis.edu
2's Complement Adder / Subtractor
Introduction:
This lab was to learn how to implement more complex circuits with all
the techniques we've learned Materials
GVIM, Vivado, Basys 3 Methods / Results:
1) Add/ Subtract 2's Complement
on LEDs and SSD
I combined part 1 and 2 of the lab by having two output wires that I
set equal to eachother from my module. This allows me to test both the
SSD and LED tests we were asked to do at the same time. It also helped
me debug the SSDs.
The trickiest part of this section is handling the positive and
negative numbers on the SSD because if the sum >= d8 then the
logic is different. I did a check on x[3] (MSB) to see if it is above
this threshold and therefore negative. The actual adder logic is fairly
simple and I just programmed the logic we discussed in class using
behavioural programming which I found to be a simpler approach than
structural where you would have to do a lot more to get the same
results. an <= 4'b1110;
From this the code was fairly straightforward. Some of the parameters
can be ignored for this part since I was just future proofing it for
step two so I wouldn't have to rewrite anything (rh is the rushour
enable). The code is fairly simple I think there is a better way of
using the step count instead of breaking it up every loop but this
worked out fine. Basically if the clock has reached the loop limit (2
for simulation and 50000000 for the Basys) then it increments the step
in the loop and resets the timer. The step count is then used for the
logic expressions above by just breaking it into it's individual bits
and using the aformentioned logic. The simulation results are as
expected. I found out that you can rearrange signals in the viewer
which was nice to know especially for this project it made it much
easier to read.
The next step was implementing a serial in parallel out so that only 5
switches are needed instead of 7 (You can get away with 6 and 4 for
continuous calculation like abovce but an extra switch is needed to
trigger calculation). There are three clock switches needed for this,
one for each SIPO and the computation trigger. All three of these
switches should be run through debounce to help mitigate unwanted
triggering (sometimes my clocks on the SIPO will still trigger twice if
I'm not careful). The following modules were added to the code.
always @(posedge clk) begin
tmp = {si, tmp[2:1]};
end
assign po1 = tmp;
assign po2 = tmp;
endmodule
From these two modules and the one above we have everything we need for
this to work. One thing to note is I also added a second output to my
SIPO so that the number appears on the LEDs. I did this becuase I
thought my code was wrong since I was getting incorrect numbers but
after debugging with the LEDs it seems like it will sometimes trigger
twice (shift the sw[1] or sw[3] into the register twice instead of
once) even with the debounce. I think it's simply because the clock is
really fast and even with the debounce it can still be stuck in a weird
state for slightly too long. The bench module to implement all of this
can be seen below and is almost identical to what I described above.
Discussion: This
lab was ok no real complaints just some things were buggier than I
would've hoped. Sometimes with HDL I write code and it doesn't work so
I start over from scratch and do almost the exact same thing and it
works fine so I think my code is just slightly too sloppy still and I
encounter unwanted errors because of it.