Friday, November 22, 2013

Simple Logic gate Simulator project in Turbo C Compiler

#include<stdio.h>
#include<conio.h>
#include<graphics.h>
#include<dos.h>

void textstyle(int t)
{
settextstyle(DEFAULT_FONT,HORIZ_DIR,t);
}

void NOTGATE()//Function to simulate NOT gate
{
int c;
textstyle(2);
outtextxy(250,50,"NOT GATE");
textstyle(1);
outtextxy(100,70,"The logic gate in which output is complement of input.");
outtextxy(100,80,"is called NOT gate.");
outtextxy(100,90,"Simulation:");
outtextxy(100,100,"Press 0 to make input low.");
outtextxy(100,110,"Press 1 to make input high.");
outtextxy(100,120,"Enter 2 when done.");
outtextxy(100,130,"Yellow color indicates logic high.");
outtextxy(100,140,"Absence of yellow color indicates logic low.");
line(250,200,250,250);
line(250,200,300,225);
line(250,250,300,225);
line(300,225,320,225);
line(250,225,230,225);
outtextxy(225,200,"A");
outtextxy(325,200,"Y=A'");
circle(225,225,5);
circle(325,225,5);
while(1)
{
if(kbhit())
  {
  c=getch();
  if(c==48)
  {
  setfillstyle(SOLID_FILL,BLUE);
  floodfill(225,225,WHITE);
  setfillstyle(SOLID_FILL,YELLOW);
  floodfill(325,225,WHITE);

  }
  else if(c==49)
  {
  setfillstyle(SOLID_FILL,YELLOW);
  floodfill(225,225,WHITE);
  setfillstyle(SOLID_FILL,BLUE);
  floodfill(325,225,WHITE);
  }
  else if(c==50)
  {
  break;
  }
  }
 }
outtextxy(100,300,"One can obtain following truth table after simulation");
outtextxy(100,310,"Truth Table:");
outtextxy(100,320,"Input");
outtextxy(150,320,"Output");
outtextxy(105,330,"(A)");
outtextxy(155,330,"(Y=A')");
outtextxy(113,340,"0");
outtextxy(163,340,"1");
outtextxy(113,350,"1");
outtextxy(163,350,"0");
}

void ORGATE()//Function to simulate OR gate
{
int c,d;
textstyle(2);
outtextxy(250,50,"OR GATE");
textstyle(1);
outtextxy(40,70,"The logic gate in which output is high when either one or");
outtextxy(40,80,"both inputs are high is called OR gate.");
outtextxy(40,90,"Simulation:");
outtextxy(40,100,"Press 0 to make input A high.It is low if other key is pressed.");
outtextxy(40,110,"Press 1 to make input B high.It is low if other key is pressed.");
outtextxy(40,120,"Enter 2 twice when done.");
outtextxy(40,130,"Yellow color indicates logic high.");
outtextxy(40,140,"Absence of yellow color indicates logic low.");
arc(250,200,-45,45,20);//draws arc
line(265,186,285,200);//draws line
line(265,214,285,200);
line(240,190,267,190);//line corresponding to input A
line(240,210,267,210);//line corresponding to input B
line(285,200,300,200);//Line corresponding to output Y
circle(305,200,5);//output terminal
circle(235,190,5);//terminal A
circle(235,210,5);//terminal B
outtextxy(220,187,"A");
outtextxy(220,207,"B");
outtextxy(320,197,"Y=A+B");
outtextxy(40,150,"Input A and B");
while(1)
 {
  if(kbhit())
  {
  c=getch();
  d=getch();
  if(c==48&&d==49)
  {
  setfillstyle(SOLID_FILL,YELLOW);
  floodfill(235,190,WHITE);
  floodfill(235,210,WHITE);
  floodfill(305,200,WHITE);
  }
  else if(c==50&&d==50)
  break;
  else if(c!=48&&d==49)
  {
  setfillstyle(SOLID_FILL,BLUE);
  floodfill(235,190,WHITE);
  setfillstyle(SOLID_FILL,YELLOW);
  floodfill(235,210,WHITE);
  floodfill(305,200,WHITE);
  }
  else if(c==48&&d!=49)
  {
  setfillstyle(SOLID_FILL,YELLOW);
  floodfill(235,190,WHITE);
  floodfill(305,200,WHITE);
  setfillstyle(SOLID_FILL,BLUE);
  floodfill(235,210,WHITE);
  }
  else if(c!=48&&d!=49)
  {
  setfillstyle(SOLID_FILL,BLUE);
  floodfill(235,190,WHITE);
  floodfill(235,210,WHITE);
  floodfill(305,200,WHITE);
  }
  }
 }
outtextxy(40,300,"One can obtain following truth table after simulation");
outtextxy(40,310,"Truth Table:");
outtextxy(40,320,"Inputs");
outtextxy(150,320,"Output");
outtextxy(40,330,"A");
outtextxy(60,330,"B");
outtextxy(40,340,"0");
outtextxy(40,350,"0");
outtextxy(40,360,"1");
outtextxy(40,370,"1");
outtextxy(60,340,"0");
outtextxy(60,350,"1");
outtextxy(60,360,"0");
outtextxy(60,370,"1");
outtextxy(150,330,"Y=A+B");
outtextxy(150,340,"0");
outtextxy(150,350,"1");
outtextxy(150,360,"1");
outtextxy(150,370,"1");

}

void ANDGATE()//Function to simulate AND gate
{
int c,d;
textstyle(2);
outtextxy(250,50,"AND GATE");
textstyle(1);
outtextxy(40,70,"The logic gate in which output is high only when");
outtextxy(40,80,"both inputs are high is called AND gate.");
outtextxy(40,90,"Simulation:");
outtextxy(40,100,"Press 0 to make input A high.It is low if other key is pressed.");
outtextxy(40,110,"Press 1 to make input B high.It is low if other key is pressed.");
outtextxy(40,120,"Enter 2 twice when done.");
outtextxy(40,130,"Yellow color indicates logic high.");
outtextxy(40,140,"Absence of yellow color indicates logic low.");
line(265,186,265,214);//draws line
arc(265,200,-90,90,15);//draws arc
line(280,200,300,200);//draws output line
line(265,190,245,190);//line for A
line(265,210,245,210);//line for B
circle(240,190,5);//terminal for A
circle(240,210,5);//terminal for B
circle(305,200,5);//terminal for output
outtextxy(220,187,"A");
outtextxy(220,207,"B");
outtextxy(320,197,"Y=A.B");
outtextxy(40,150,"Input A and B");
while(1)
 {
  if(kbhit())
  {
  c=getch();
  d=getch();
  if(c==48&&d==49)
  {
  setfillstyle(SOLID_FILL,YELLOW);
  floodfill(240,190,WHITE);
  floodfill(240,210,WHITE);
  floodfill(305,200,WHITE);
  }
  else if(c==50&&d==50)
  break;
  else if(c!=48&&d==49)
  {
  setfillstyle(SOLID_FILL,BLUE);
  floodfill(240,190,WHITE);
  floodfill(305,200,WHITE);
  setfillstyle(SOLID_FILL,YELLOW);
  floodfill(240,210,WHITE);
  }
  else if(c==48&&d!=49)
  {
  setfillstyle(SOLID_FILL,YELLOW);
  floodfill(240,190,WHITE);
  setfillstyle(SOLID_FILL,BLUE);
  floodfill(240,210,WHITE);
  floodfill(305,200,WHITE);
  }
  else if(c!=48&&d!=49)
  {
  setfillstyle(SOLID_FILL,BLUE);
  floodfill(240,190,WHITE);
  floodfill(240,210,WHITE);
  floodfill(305,200,WHITE);
  }
  }
 }
outtextxy(40,300,"One can obtain following truth table after simulation");
outtextxy(40,310,"Truth Table:");
outtextxy(40,320,"Inputs");
outtextxy(150,320,"Output");
outtextxy(40,330,"A");
outtextxy(60,330,"B");
outtextxy(40,340,"0");
outtextxy(40,350,"0");
outtextxy(40,360,"1");
outtextxy(40,370,"1");
outtextxy(60,340,"0");
outtextxy(60,350,"1");
outtextxy(60,360,"0");
outtextxy(60,370,"1");
outtextxy(150,330,"Y=A.B");
outtextxy(150,340,"0");
outtextxy(150,350,"0");
outtextxy(150,360,"0");
outtextxy(150,370,"1");
}

void welcome()//start screen function
{
textstyle(2);
outtextxy(20,50,"Welcome to this Logic Gates Simulator");
textstyle(1);
outtextxy(20,80,"In this simulator you will be able to simulate");
outtextxy(20,100,"basic logic gates.");
outtextxy(600,450,"BKL");
delay(5000);
textstyle(2);
while(!kbhit())
{
outtextxy(200,250,"PRESS ANY KEY");
delay(500);
cleardevice();
delay(500);
}
}

void gates()
{
int c;
textstyle(2);
outtextxy(10,100,"Which gate would you like to simulate?");
outtextxy(10,120,"a.Enter 1 to simulate NOT gate");
outtextxy(10,140,"b.Enter 2 to simulate OR gate.");
outtextxy(10,160,"c.Enter 3 to simulate AND gate.");
while(1)
{
if(kbhit())
  {
  c=getch();
  if(c==49)
  {
  cleardevice();
  NOTGATE();
  break;
  }
  else if(c==50)
  {
  cleardevice();
  ORGATE();
  break;
  }
  else if(c==51)
  {
  cleardevice();
  ANDGATE();
  break;
  }
 }
 }
 }

 void bye()
 {
 textstyle(3);
 cleardevice();
 outtextxy(250,250,"BYE-BYE!");
 }

int main()
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc\\bgi");
setbkcolor(BLUE);
welcome();
gates();
getch();
bye();
getch();
return 0;
}

0 comments:

Post a Comment