Add16

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

Add16

n00b2Tetris
This post was updated on .
Can you guys tell what I'm doing wrong?

Removed...

It breaks on the last line of the test script...
Reply | Threaded
Open this post in threaded view
|

Re: Add16

cadet1620
Administrator
This code passes the test for me.

Please edit your post to remove the working HDL.

Things to try to find the problem.
   1) Look at the test output and confirm that your Add16 is computing the wrong value.
   2) If it's failing, make a subdirectory under projects/02 and copy only Add16.* into it.  Then test that Add16.  This will eliminate any of your HDL other than Add16 being used.  If it fails, there must be something wrong with your Add16.tst or Add16.cmp.
   3) If it's not failing, the problem is with your HalfAdder or FullAdder.  Copy HalfAdder into the subdirectory and retest.  If it fails, something's subtly wrong with HalfAdder.  Do the same thing with FullAdder.

If you find that your HalfAdder and FullAdder both past their tests, but cause Add16 to fail, please email them to me so that I can improve the tests to catch your problem.

--Mark


My Add16.cmp:
|        a         |        b         |       out        |
| 0000000000000000 | 0000000000000000 | 0000000000000000 |
| 0000000000000000 | 1111111111111111 | 1111111111111111 |
| 1111111111111111 | 1111111111111111 | 1111111111111110 |
| 1010101010101010 | 0101010101010101 | 1111111111111111 |
| 0011110011000011 | 0000111111110000 | 0100110010110011 |
| 0001001000110100 | 1001100001110110 | 1010101010101010 |
My Add16.tst
// This file is part of www.nand2tetris.org
// and the book "The Elements of Computing Systems"
// by Nisan and Schocken, MIT Press.
// File name: projects/02/Add16.tst

load Add16.hdl,
output-file Add16.out,
compare-to Add16.cmp,
output-list a%B1.16.1 b%B1.16.1 out%B1.16.1;

set a %B0000000000000000,
set b %B0000000000000000,
eval,
output;

set a %B0000000000000000,
set b %B1111111111111111,
eval,
output;

set a %B1111111111111111,
set b %B1111111111111111,
eval,
output;

set a %B1010101010101010,
set b %B0101010101010101,
eval,
output;

set a %B0011110011000011,
set b %B0000111111110000,
eval,
output;

set a %B0001001000110100,
set b %B1001100001110110,
eval,
output;
Reply | Threaded
Open this post in threaded view
|

Re: Add16

n00b2Tetris
Thanks for the reply.

It still failed in the same way when I copied it a sub directory. Can I send you my files?
Reply | Threaded
Open this post in threaded view
|

Re: Add16

cadet1620
Administrator
Yes, email them to me.  Might be easiest to zip or rar your whole 02 directory and send that.

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

Re: Add16

n00b2Tetris
This post was updated on .
Ok, I got it to work...

I had tried to make the full adder without using half adders.

Thanks for the suggestions!
Reply | Threaded
Open this post in threaded view
|

Re: Add16

cadet1620
Administrator
You can use a FullAdder as a Half adder by setting the carry-in to zero:
    FullAdder (a=a[0], b=b[0], c=false, sum=out[0], carry=c0);
    ...
Common real world adder parts have a pin for the bit 0 carry-in.  This makes them chainable to make even wider adders.  Given an Add16c chip:
    IN a[16], b[16], c;
    OUT out[16], carry;
PARTS:
    FullAdder (a=a[0], b=b[0], c=c, sum=out[0], carry=c0);
    ...
    FullAdder (a=a[15], b=b[15], c=c14, sum=out[0], carry=carry);
You could use it to make a 32-bit adder:
    Add16c (a=a[0..15],  b=b[0..15],  c=false, out=out[0..15], carry=c15); 
    Add16c (a=a[16..31], b=b[16..31], c=c15,   out=out[16..31]); 
(Note that 32-bit parts are not supported by the simulator.)


Here's something to think about.  What operation does this chip implement, and how does it work?
CHIP Something16 {
    IN  a[16], b[16];
    OUT out[16];
PARTS:
    Not16 (in=b, out=notB);
    Add16c (a=a, b=notB, c=true, out=out);
}

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

Re: Add16

n00b2Tetris
This post was updated on .
I'm trying to understand what you're trying to get me to understand:)

If you add say 0000000000000000 to 0000000000000000 you get 0000000000000001?

0000000000000000 to 1111111111111111 (255) you get 1111111111111110 (254)??

255 to 255 just gives you 255?!


...hope my binary math, or lack there of isn't laughable:P
Reply | Threaded
Open this post in threaded view
|

Re: Add16

cadet1620
Administrator
Not sure which part of the message you're referring to.

If the first part, the 32-bit adder, the first Add16c has its carry in bit tied to 0.

If the second part, the Something16 chip is not Addition and is not something that you need to know.  I just intended it to show another use of a multi-bit adder with carry-in.

Something16 is a math operation related to addition.

For arithmetic on the Hack computer all numbers need to be considered as signed numbers, so for a = 16 and b = -5
  0000 0000 0001 0000  (16)
+ 1111 1111 1111 1011  (-5)
  -------------------
  0000 0000 0000 1011  (11)
For Something16 with a = 16 and b = -5, because of the Not part before the Add16c and because c = true, the result is
                    1
  0000 0000 0001 0000  (16)
+ 0000 0000 0000 0100  (4)
  -------------------
  0000 0000 0001 0101  (21)
Something16 is a Sub16 chip.

Sorry for the confusion.

--Mark