Click here to Skip to main content
13,354,605 members (66,535 online)
Click here to Skip to main content
Add your own
alternative version


41 bookmarked
Posted 30 Sep 2003

Logical circuits and logical gates simulator

, 30 Sep 2003
Rate this:
Please Sign up or sign in to vote.
Logical circuits and logical gates simulator. A program similar to PSpice.


This is a simple sample to simulate basic logical circuits (I hope I can develop this for FLIP-FLOPs too). There are all the  main gates in this program.

The list of Gates is :

  1. NOT
  2. AND
  3. OR
  4. NAND
  5. NOR
  6. XOR
  7. XNOR
  8. Simple left to right pip.
  9. Simple Up to down and down to up pip.
  10. Two way pip.

After drawing your gates on the board, right click on the board for TRACE menu. Select trace to see final result . You can change your trace mode from left top dialog in two modes : 1 - Fast mode. 2 - Step By Step .

Note : You can deleted any drawn gates by selecting delete tool from the left-side toolbox. And you can minimize/restore toolbox by clicking the pointer button on top left and button left side of toolbox.

Code Listing

<PRE lang=c++>// This is a function that select correc cursor for mouse pointer . BOOL CLCView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) { m_pViewWnd=pWnd; switch (m_dToolBox.CursorValue){ case AND: lhCursor=AfxGetApp()->LoadCursor(IDC_AND); SetCursor(lhCursor); ShowFlag=TRUE; break; case NAND: lhCursor=AfxGetApp()->LoadCursor(IDC_NAND); SetCursor(lhCursor); ........... } // This a function to draw selected gates on board // (after you click this gate on board) void CLCView::ShowBitmap(int x,int y,int kind) { CDocument* pDoc = GetDocument(); CClientDC dc(this); CBitmap bitmap; CDC dcMemory; BITMAP bm; switch (kind) { case AND : bitmap.LoadMappedBitmap(IDB_AND,0); break; case NAND : bitmap.LoadMappedBitmap(IDB_NAND,0); // Call main calculator function void CLCView::OnCalc() { m_dToolBox.UpdateData(TRUE); if (m_dToolBox.m_rOption==0) FastTrace=TRUE; else FastTrace=FALSE; m_dToolBox.UpdateData(FALSE); Calc(); } // This function is like a snapper grid boolean CLCView::Snaper(int x, int y,int kind) { x=(x/32)*32; y=(y/32)*32; if(ShowFlag==TRUE) { ShowBitmap(x,y,m_dToolBox.CursorValue); ScreenMap(x,y,m_dToolBox.CursorValue); } else if (kind==_DEL) { ShowBitmap(x,y,m_dToolBox.CursorValue); ..... ..... } ... } // This function save your drawes gates for calculate boolean CLCView::ScreenMap(int x, int y,int kind) { int xMem,yMem; xMem=x/32; yMem=y/32; switch (kind) { case AND : screen[xMem][yMem].Gate=AND; screen[xMem][yMem].SGate="AND"; screen[xMem][yMem].Group=1; screen[xMem][yMem].lunched=1; break; case NAND : screen[xMem][yMem].Gate=NAND ... ... // and this is calculatore of program void CLCView::Calc() { for (int i=0;i<32;i++) for (int j=0;j<24;j++) if (screen[i][j].lunched==1) { UpdateData(FALSE); switch (screen[i][j].Gate) { case AND : { switch (screen[i-1][j].Gate) { case DLR : screen[i][j].inValue1=screen[i-1][j].outValue1; ........... ............. .............. }


If you have found any bug or if you can improve the code please contact me by my email at :  or Thanks and bye.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Software Developer (Senior)
Iran (Islamic Republic of) Iran (Islamic Republic of)
No Biography provided

You may also be interested in...

Comments and Discussions

Questioncan i run the code source on 2010 ? Pin
Member 937482925-Mar-16 23:59
memberMember 937482925-Mar-16 23:59 
AnswerRe: can i run the code source on 2010 ? Pin
MH253827-Sep-16 4:44
memberMH253827-Sep-16 4:44 
QuestionGreat! Pin
Saeid Kalhor16-May-15 10:12
memberSaeid Kalhor16-May-15 10:12 
AnswerRe: Great! Pin
MH253827-Sep-16 4:40
memberMH253827-Sep-16 4:40 
QuestionHow to Run this Code Pin
Member 115706368-Apr-15 8:03
memberMember 115706368-Apr-15 8:03 
AnswerRe: How to Run this Code Pin
MH253827-Sep-16 4:41
memberMH253827-Sep-16 4:41 
Generalgreat work, but... Pin
Ezz Khayyat15-Jan-04 4:08
memberEzz Khayyat15-Jan-04 4:08 
GeneralRe: great work, but... Pin
Jacee20-Apr-04 22:06
memberJacee20-Apr-04 22:06 
GeneralCustom cursors Pin
ronpeters13-Nov-03 8:47
sussronpeters13-Nov-03 8:47 
GeneralVery impressive, but ... Pin
WREY6-Oct-03 23:47
memberWREY6-Oct-03 23:47 
GeneralNice Job but... Pin
Nice Life1-Oct-03 1:24
memberNice Life1-Oct-03 1:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 1 Oct 2003
Article Copyright 2003 by MH2538
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid