Tuesday, November 20, 2012

Boolean Logic : Activity 7

Activity 7 : Build DMUX by using (NAND, OR, AND, NOT, MUX)


/**

 * Dmultiplexor.  
 * {a,b} = {in,0} if sel==0
 *         {0,in} if sel==1
 */


CHIP DMux {
    IN in, sel;
    OUT a, b;

    PARTS:
    // Put your code here.
    Not(in=sel, out=notsel);

    And(a=in, b=notsel, out=a);
    And(a=in, b=sel, out=b);
}



/////////////////////////////////
This should be a piece of cake !!




Downloads :
 activity 7

Boolean Logic : Activity 6

Activity 6 : Build MUX by using (NAND, OR, AND, NOT)


Write truth table -> canonical form 







/**
 * Multiplexor. If sel==1 then out=b else out=a.
 */

CHIP Mux {
    IN a, b, sel;
    OUT out;

    PARTS:
    // Put your code here.
    Not(in=a, out=nota);
    Not(in=b, out=notb);
    Not(in=sel, out=notsel);


    And(a=nota, b=b, out=a1);
    And(a=a, b=b, out=a2);
    Or(a=a1, b=a2, out=o1);
    And(a=sel, b=o1, out=p1);

    And(a=a, b=notb, out=a3);
    Or(a=a2, b=a3, out=o2);
    And(a=notsel, b=o2, out=p2);


    Or(a=p1, b=p2, out=out);
}


On simplifying further :
way 2 >



/** 
 * Multiplexor. If sel==1 then out=b else out=a.
 */

CHIP Mux {
    IN a, b, sel;
    OUT out;

    PARTS:
    // Put your code here.
    Not(in=a, out=nota);
    Not(in=b, out=notb);
    Not(in=sel, out=notsel);


    And(a=a, b=b, out=a3);

    And(a=nota, b=b, out=t1);
    And(a=t1, b=sel, out=a1);

    And(a=a, b=notb, out=t2);
    And(a=t2, b=notsel, out=a2);

    Or(a=a1, b=a2, out=o1);
    Or(a=o1, b=a3, out=out);

}

Both the above solutions were built sequentially. Now its time to shorten it.. 
way 3>



/** 
 * Multiplexor. If sel==1 then out=b else out=a.
 */

CHIP Mux {
    IN a, b, sel;
    OUT out;

    PARTS:
    // Put your code here.
    Not(in=sel, out=notsel);
    And(a=a, b=notsel, out=w1);
    And(a=b, b=sel, out=w2);
    Or(a=w1, b=w2, out=out);
}





Downloads : 


Boolean Logic : Activity 5

Activity 5 : Build XOR from primitive NAND



// This file is part of the materials accompanying the book 
// "The Elements of Computing Systems" by Nisan and Schocken, 
// MIT Press. Book site: www.idc.ac.il/tecs
// File name: projects/01/Xor.hdl

/**
 *  Exclusive-or gate: out = !(a == b).
 */

CHIP Xor {

    IN a, b;
    OUT out;

    PARTS:
    // Put your code here.
    Nand(a=a, b=a, out=nota);
    Nand(a=b, b=b, out=notb);

    Nand(a=a, b=notb, out=w1);
    Nand(a=w1, b=w1, out=a1);

    Nand(a=nota, b=b, out=w2);
    Nand(a=w2, b=w2, out=a2);


    Nand(a=a1, b=a1, out=p1);
    Nand(a=a2, b=a2, out=p2);
    Nand(a=p1, b=p2, out=out);

    
}

////////////////////////////////////////////////////

hmm.. so what we do is make truth table - write canonical rep - and replace the and, or and not def with those of nand which we found out in previous activities.

truth table -> canonical form -> substitute nand def






Downloads : 



Boolean Logic : Activity 4

Activity 4 : Build OR from primitive NAND




// This file is part of the materials accompanying the book
// "The Elements of Computing Systems" by Nisan and Schocken,
// MIT Press. Book site: www.idc.ac.il/tecs
// File name: projects/01/Or.hdl


/**
 * Or gate: out = 1 if {a==1 or b==1}, 0 otherwise
 */

CHIP Or {
    IN a, b;
    OUT out;

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




//////////////////////////////////////////////////////
Well, I just made truth tables for basic gates and saw that Nand of nota and notb matched that of or.
So this solution was out of observation. Please share if this can be done logically.


Downloads :

Boolean Logic : Activity 3

Activity 3 : Build AND from primitive NAND




// This file is part of the materials accompanying the book
// "The Elements of Computing Systems" by Nisan and Schocken,
// MIT Press. Book site: www.idc.ac.il/tecs
// 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.
    //Nand(a='Nand(a=a, b=b, out=w1)', b='Nand(a=a, b=b, out=w2)', out=out);
    Nand(a=a, b=b, out=w1);
    Nand(a=w1, b=w1, out=out);
}


////////////////////////////////////////////////////////////////
Another way would be :
Nand(a=a, b=b, out=w1);
Not(in=w1, out=out);


I wonder although if we could write it as Nand(a=Nand(a=a, b=b, out=w1), b=Nand(a=a, b=b, out=w2), out=out);
Am I missing something ??




Downloads:

Boolean Logic : Activity 2 (with summary of ch-1)

DISCLAIMER: Meant to be read during coffee breaks as a summary // Author is not responsible for your frustration

chips - made of logic gates
and chips make up almost everything in computing science.

logic gates run on 0s and 1s - what we call as boolean and getting fidgetive with it called as boolean algebra.
Next, boolean functions operate on boolean i/p to give boolean o/p.

Truth table : we see all combinations of i/ps and their o/ps. From this we can form canonical representation.
This in turn proves that any boolean function can be formed out of 'And', "Or' and 'Not'.

Then we check out many boolean functions - And, Not, Constant, Equivalence, If then, Xor, Or, Nand, Nor.....
Nand function has an interesting property : And, Or and Not can be constructed from it. If you notice what we said about canonical representation, this means we can construct any boolean function from Nand alone.

Then we moved with gate symbols, interface and implementation.

Multiplexor (nick name: mux) has an extra i/p bit called selector to o/p one of the i/ps depending on the selector/selection-bit.
Demultiplexor (demux) is just the other way around.. the selection bit decides which o/p bit should give the i/p value as the o/p.

The project is meant to simplify our logic about the boolean logic :)


Activity 2 : Building NOT from primitive NAND


This goes in the .hdl file
// This file is part of the materials accompanying the book
// "The Elements of Computing Systems" by Nisan and Schocken,
// MIT Press. Book site: www.idc.ac.il/tecs
// File name: projects/01/Not.hdl

/**
 * Not gate: out = not in
 */

CHIP Not {
    IN in;
    OUT out;

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


Downloads:

Boolean Logic : Activity 1

0s and 1s can make you kneel on your knees or raise your head high !! A dive into the binary world from scratch..

For the lucky ones out there : Elements of Computing Systems (we all are lucky here ;) )

You can buy this book from here
Download TECS h/w simulator  here and read part I, II, III in this tutorial

Thanks to  Professor Shimon Schocken and Professor Noam Nisan for making everything available online and making it simple for us.


You may want to visit the Q/A forum