ALU Help

classic Classic list List threaded Threaded
29 messages Options
12
Reply | Threaded
Open this post in threaded view
|

ALU Help

snowkel
I'm having difficulties trying to understand the ALU and writing the hdl for it. I've tried filling out this chart  but I know I'm doing something wrong due to my lack of understanding.

http://nand2tetris-questions-and-answers-forum.32033.n3.nabble.com/file/n95834/alu_worksheet.pdf

Here is my attempt at trying to fill in the first box.
http://imgur.com/a/c5oRr

Can someone point me in the right direction?
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

ybakos
You're on the right track. There's just a mistake in your addition, which is causing the confusion.

   1111
+ 1111
  ------
   1110

Now negate that, and what do you get?
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

snowkel
Thanks for clearing up the addition for me. That certainly helps a bit!

The -1 output was simple enough to figure out, but I'm having a little trouble on the x output though. Here's my thought process:
zx = 0, this means that x (0100) is not zeroed.
nx = 0, x is not negated
zy = 1, y is zeroed (0000)
ny = 1, y is negated (1111)
x+y=0, x (0100) and y (1111) are not added

So does this mean I simply ignore y? Leaving me with the desired output x (0100).
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

ybakos
Review the ALU spec. What does the last f bit mean when it is 1 or 0?

You can do it!
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

snowkel
When f is 1, then out = x + y. When f is 0, then out = x & y. In the book, it says that this means it's a bit-wise And. Not entirely sure I understand that concept. Would this mean that the out is both x (0100) and -1 (1111)? But the resulting out on the chart shows that it's just x?
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

ybakos
When f is 0, then take x AND y ("bitwise AND" is a logical AND, it is the AND you know).
When f is 1, then take x plus y (arithmetic sum).

snowkel wrote
zx = 0, this means that x (0100) is not zeroed.
nx = 0, x is not negated
zy = 1, y is zeroed (0000)
ny = 1, y is negated (1111)
What is x now? 0100
What is y now? 1111

f = 0, so x AND y

0100 AND 1111 = ?

Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

snowkel
So I understand that it's using the logic gate AND but I'm a bit lost on what exactly 0100 AND 1111 mean. Should I be thinking about it like this?
http://imgur.com/a/TinNm
If so, I still don't understand how 0100 AND 1111 gives us the output of just x. Apologies if I'm not seeing the obvious.
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

ybakos
This is where the "bitwise" comes in.
If you know what 0 AND 1 are, what is 00 AND 11? What about 01 AND 10?
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

snowkel
I seem to be lacking some basic understanding. I don't know what 0 AND 1 are.
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

ybakos
I bet you do.

Consider an AND gate. Do you know how it behaves? Consult a truth table.

0 AND 1 are the same thing, where 0 is an "off" input and 1 is "on" input into an AND gate.
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

snowkel
This post was updated on .
I drew up a truth table for the AND gate and I think I'm starting to get what you mean by 0 AND 1 (or at least I hope I do). If one of the inputs is 0 and the other 1, then the out is 0.
http://imgur.com/a/Zb0O7

Therefore meaning that 1 AND 0 = 0? I'm not sure how to expand upon this logic to reach 00 AND 11. But since it would require both of the inputs to be "on" for 00 AND 11 = 11, I'm guessing that 00 (off) AND 11 (on) = 00 ?

Edit: I tried searching through the book for any more info on the bit-wise AND but the book doesn't give any additional details other than the fact that this chip has already been built. I was able to fill out the rest of the worksheet except for the sections where the f bit is 0.
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

cadet1620
Administrator
"Bit-wise" means one bit at a time, just like the And16.hdl chip that you made in project 1.
Use the 1-bit AND operation for each bit position in the inputs to generate the corresponding output bit.

11011010 AND 10010111 = 10010010:
    1 1 0 1 1 0 1 0
AND 1 0 0 1 0 1 1 1
    ---------------
    1 0 0 1 0 0 1 0
--Mark
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

snowkel
Hey Mark, I think I finally got it. The way you formatted the numbers really helped!

0 AND 1 = 0
00 AND 11 = 00
01 AND 10 = 00

Also, I'm having a little trouble converting negative binary numbers to their decimal value in a 4 bit system. I know that 0101 = (1*2^0)+(0*2^1)+(1*2^2)+(0*2^3) = 5 but this method does not work for binary numbers larger than 7.
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

cadet1620
Administrator
snowkel wrote
4 bit system ... this method does not work for binary numbers larger than 7.
What you have just learned is the maximum representable signed 4-bit number.
The range for 4-bit signed numbers is -8 – 7.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

snowkel
I'm starting to have a better grasp of the the ALU conceptually after filling out the worksheet but how do I apply these concepts into writing the actual hdl code? I'm a bit lost as to where to go from here.

My first thought was that since there are 8 inputs (x[16], y[16], zx, nx, yz, ny, f, no) it would use multiple mux chips? But I'm almost certain this is incorrect and am scratching my head trying to figure how to build upon this.
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

cadet1620
Administrator
snowkel wrote
I'm starting to have a better grasp of the the ALU conceptually after filling out the worksheet but how do I apply these concepts into writing the actual hdl code? I'm a bit lost as to where to go from here.
See this post, Getting started with ALU.

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

snowkel
I'm still having trouble wrapping my head around this. I'm lost at syntax usage, what chips to use, and how to even start. My first instinct is to use mux for the if-then statements but I don't know how to incorporate all of the inputs into my code. I also have no idea what the zr and ng outputs mean in context of this whole thing other than zr being True if out=0 and ng being true if out < 0.
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

cadet1620
Administrator
These posts might be helpful.
  Getting started with ALU
  ALU Trouble in starting

To begin with ignore the 'zr' and 'ng' status outputs. Get your ALU to pass the ALU-nostat.tst.
Once your ALU is properly computing the 'out' value, then you can concentrate on 'zr' and ng'.

Think about how to break the ALU into smaller conceptual blocks. For instance, 'x', 'zx' and 'nx' are used to generate one of the inputs to the And16 and Add16. 'y', 'zy', and 'ny' similarly generate the other input to the And and Add. Function processing and output processing can also be thought about as individual blocks.


For 'zr' think about what the bits need to be for a 16-bit number to be 0 and how you might detect that. It might be easier to to think about how you can detect that a number is not 0.

For 'ng', what nakes a 2's-complement number negative?

--Mark
Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

ybakos
In reply to this post by snowkel
snowkel, I have a video for you:
https://vimeo.com/139964683

Email me for the password.

Reply | Threaded
Open this post in threaded view
|

Re: ALU Help

snowkel
This post was updated on .
In reply to this post by cadet1620
Thanks for the links, they were helpful. I tried thinking about it as blocks and tried writing my hdl step by step like how the worksheet had certain steps to it. I emailed you a copy of my hdl file, I hope that's okay. I'm not sure about the 'zr' part but as for the 'ng' bit, I believe that that you'd have to flip the bits and add one to it.

Edit: Just saw your post ybakos. Messaged.

Edit2: Just got my code to finally pass the ALU.tst! I want to thank ybakos and Mark for going to great lengths to help me understand this stuff. You guys have no idea how much of a difference it made.
12