Re: How do I combine 2 sperate bits in a 2bit input

Posted by rogerdodger91 on
URL: http://nand2tetris-questions-and-answers-forum.52.s1.nabble.com/How-do-I-combine-2-sperate-bits-in-a-2bit-input-tp3593549p3594077.html

The syntax makes perfect sense.

Remember that a byte is just an array of 8 bits.

In Hardware you cant use a 16 bit value as an input to a 1 bit gate.

However there is nothing stopping you from inputting a single bit into a multi-bit gate.

Remember that each N-bit chip you implement is just a combination of multiple more primitive versions of said chip.

Here is an example of whats legal and whats not.

CHIP Foo
{
In = a[16], b[16];
Out = low_16bit_out[8], high_16bit_out[8]; //We have split the Out=out[16] into two values of 1byte in length.  

PARTS:
And16(a=a, b=b, out=andAB);

//Remember the HDL will implicitly fill in the arrays for you if the sanity of your logic is in check.
//You can imagine this being, And16(a[0+1+2..15]=a[0..15], b[0..15]=b[0..15], out[0..15]=andAB[0..15]  
//Its just alot harder to read that way so the language does it for us.

//Now lets explore something that isnt logically sound.

And(a=a[0], b=a[6], out=Buggy_1BIT_AndAB);

//See the problem here that isnt obvious, that tends to confuse alot of people is the fact that logically //speaking, your trying to pass an individual bit from a 16 bit byte magically.

//Think of it like this, A building is on fire and you have 16 people desperately trying to fight their way //through a door that only has room for one person at a time. These people are crazy and scared, and lets //just add retarded to the mix. They arent thinking logically. So somebody (you) needs to pull them back in //the hall that they all fit in and let each person go through one by one.

And16(a=a, b=a, out[0]=person1, out[6]=person2); //ect
And(a=person1, b=person2, out=Saved_People);

//Notice how I And a with itself. I do this because i know i will get the same value back. However now im //outputting multiple 1 bit pins. The 1-5, and 7-15 bits are simply ignored and forgotten. However you //shouldnt ever have to actually use the And16 method to split up a multi bit number. There is a more //effective way that i wont reveal.

//Now to finish the tutorial.
And16(a = andAB, b[0..15] = Saved_People, low_16bit_out=sum[0..7], high_16bit_out=sum[8..15]);  
}  


Now there are a bunch of SYNTAX errors in this chip. I just went through and debugged it and posted the correct results below. (I also showed a way to convert a 1 bit value into a 16 bit value. Something that might not be so obvious).

CHIP tutorial{
IN a[16], b[16];
OUT low16bitout[8], high16bitout[8];

PARTS:
And16(a=a, b=b, out=andAB);

And(a=a[0],b=a[6],out=Buggy1BITAndAB);
 
And16(a=a, b=a, out[0]=person1, out[6]=person2);
And(a=person1, b=person2, out=SavedPeople);

Mux16(a=true, b=false, sel=SavedPeople, out=people);

 
And16(a = andAB, b = people, out[0..7]=low16bitout, out[8..15]=high16bitout);  
}

Make sure the chip name = the name of your hdl file. Or else it will yell at you. For instance, i named the chip foo originally, yet saved it as tutorial.hdl < NOTE: on windows .txt can and will most likely be hidden from your file extension leading to a bunch of headaches. Should you not know how to fix that, heres a link with an easy fix microsoft button. http://support.microsoft.com/kb/865219