summaryrefslogtreecommitdiffstats
path: root/src/electronics/components/fulladder.cpp
blob: ad5e40c5220ca46f91b09310177831d193f88240 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/***************************************************************************
 *   Copyright (C) 2004 by David Saxton                                    *
 *   [email protected]                                                    *
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 ***************************************************************************/

#include "fulladder.h"

#include "logic.h"
#include "libraryitem.h"

#include <kiconloader.h>
#include <klocale.h>


Item* FullAdder::construct( ItemDocument *itemDocument, bool newItem, const char *id )
{
	return new FullAdder( (ICNDocument*)itemDocument, newItem, id );
}

LibraryItem* FullAdder::libraryItem()
{
	return new LibraryItem(
		QString("ec/adder"),
		i18n("Adder"),
		i18n("Integrated Circuits"),
		"ic1.png",
		LibraryItem::lit_component,
		FullAdder::construct
			);
}

FullAdder::FullAdder( ICNDocument *icnDocument, bool newItem, const char *id )
	: Component( icnDocument, newItem, (id) ? id : "adder" )
{
	m_name = i18n("Adder");
// 	m_desc = i18n("Insert missing adder help here.");

	ALogic = BLogic = inLogic = 0l;
	outLogic = SLogic = 0l;

	QStringList pins = QStringList::split( ',', "A,B,>,,S,C", true );
	initDIPSymbol( pins, 48 );
	initDIP(pins);
	
	ECNode *node;
	
	node =  ecNodeWithID("S");
	SLogic = createLogicOut( node, false );
	
	node = ecNodeWithID("C");
	outLogic = createLogicOut( node, false );
	
	node = ecNodeWithID("A");
	ALogic = createLogicIn(node);
	
	node = ecNodeWithID("B");
	BLogic = createLogicIn(node);
	
	node = ecNodeWithID(">");
	inLogic = createLogicIn(node);
	
	
	ALogic->setCallback( this, (CallbackPtr)(&FullAdder::inStateChanged) );
	BLogic->setCallback( this, (CallbackPtr)(&FullAdder::inStateChanged) );
	inLogic->setCallback( this, (CallbackPtr)(&FullAdder::inStateChanged) );
}

FullAdder::~FullAdder()
{
}


void FullAdder::inStateChanged( bool /*state*/ )
{
	const bool A = ALogic->isHigh();
	const bool B = BLogic->isHigh();
	const bool in = inLogic->isHigh();
	
	const bool out = (!A && B && in) || (A && !B && in) || (A && B);
	const bool S = (!A && !B && in) || (!A && B && !in) || (A && !B && !in) || (A && B && in);
	
	SLogic->setHigh(S);
	outLogic->setHigh(out);
}