Implementation of an AND chip

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

Implementation of an AND chip

markuk
This is what I've got so far:

    // 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/01/And.hdl

    /**
     * And gate:
     * out = 1 if (a == 1 and b == 1)
     *       0 otherwise
     */

    CHIP And {
        IN a, b;
        OUT out;

        PARTS:
        // Put your code here:
        Not(in=a, out=nota);
        Not(in=b, out=notb);
        And(a=a, b=b, out=out);
        Or(a=nota, b=b, out=nota);
        Or(a=a, b=notb, out=notb);
   
    }

Problem is I'm getting this error:

    ...
    at Hack.Gates.CompositeGateClass.readParts(Unknown Source)
        at Hack.Gates.CompositeGateClass.<init>(Unknown Source)
        at Hack.Gates.GateClass.readHDL(Unknown Source)
        at Hack.Gates.GateClass.getGateClass(Unknown Source)
        at Hack.Gates.CompositeGateClass.readParts(Unknown Source)
        at Hack.Gates.CompositeGateClass.<init>(Unknown Source)
        at Hack.Gates.GateClass.readHDL(Unknown Source)
    ...

And I don't know if I'm getting this error because the hardware simulator is malfunctioning or because my code is wrong and the software can't load it up.

Any help much appreciated.
Reply | Threaded
Open this post in threaded view
|

Re: Implementation of an AND chip

ivant
You are trying to implement the AND chip using... the AND chip. The official Hardware Simulator doesn't detect this and crashes with this cryptic error message.
Reply | Threaded
Open this post in threaded view
|

Re: Implementation of an AND chip

markuk
This post was updated on .
Yeah ... A guy from Stack Overflow said the same. Still struggling to comprehend the idea of logic gates. I build websites where I use boolean logic every day but when it comes to more fundamental stuff I burn like a paper.

The book says I should need to use Nand gate only to make AND chip work. But how I'm supposed to
make it work with input and output pins ??? I can't make sense of it.


This is what I've got so far:

CHIP And {
    IN a, b;
    OUT out;

    PARTS:
    // Put your code here:
    Nand(in=a, in=b, out=out);
   
}

When I hit "run" to get a feedback from the simulator it just keeps running and never seems to stop. No feedback either. I have no idea what I'm doing here.
Reply | Threaded
Open this post in threaded view
|

Re: Implementation of an AND chip

xedover
think of them like building blocks... you need bricks to build a wall, and walls to build a room, several rooms to build a house, etc. -- you can't build a house with a house, you have to first use the more fundamental parts to build it up.

Since we're treating NAND as the most fundamental element, then we can use it to build all our other parts.

Then assuming you've figured out how to build a NOT gate from NAND(s), then now you can use both NANDs and NOTs to build the next level up, the AND gate.

markuk wrote
This is what I've got so far:

CHIP And {
    IN a, b;
    OUT out;

    PARTS:
    // Put your code here:
    Nand(in=a, in=b, out=out);
   
}
What you've got so far, is a chip named AND, but it functions exactly like an NAND (because that's the only part you've used)

Think about the truth tables... an NAND is just a NOT(AND), so that makes an AND what...? a NOT(NAND), right?

Then its just a matter of chaining your outputs of one into the inputs of the other.

Hope this helps.
Reply | Threaded
Open this post in threaded view
|

Re: Implementation of an AND chip

xedover
In reply to this post by markuk
markuk wrote
Still struggling to comprehend the idea of logic gates.
Think of logic gates as switches, for example either wired together in series or parallel. The book Code, by Charles Petzold explained it really well using telegraph relay switches.


   +____./ .___./ ._____@_____-
          a      b    (light bulb)
Here two switches are in series. The light bulb will only come on when both a and b switches are closed at the same time. One or the other is not enough. This is an AND circuit.
   a   b   light
  off off   off
  off  on   off
   on off   off
   on  on    on


   +__
      |
      |__./ .___
      |    a    |
      |         |
      |__./ .___|__@___.-
           b    (light bulb)
Here two switches are in parallel. The light bulb will come on when either one or the other or both switches are closed at the same time. This is an OR circuit.
   a   b   light
   0   0     0
   0   1     1
   1   0     1
   1   1     1

Logic gates are about dealing with the true/false results of multiple inputs (and how they affect each other).
Reply | Threaded
Open this post in threaded view
|

Re: Implementation of an AND chip

Jack Draak
I'll second the recommendation for Code. I've only just begun it, but I get the feeling if I keep going I'll really get places, because it's stitched together so neatly.
Reply | Threaded
Open this post in threaded view
|

Re: Implementation of an AND chip

xedover
Jack Draak wrote
I'll second the recommendation for Code. I've only just begun it, but I get the feeling if I keep going I'll really get places, because it's stitched together so neatly.
its my all-time favorite book. I re-read it every couple of years. its amazing how he makes such a complicated subject so very simple and easy to understand