File indexing completed on 2024-05-05 05:46:06

0001 /***************************************************************************
0002  *   Copyright (C) 2004 by David Saxton                                    *
0003  *   david@bluehaze.org                                                    *
0004  *                                                                         *
0005  *   This program is free software; you can redistribute it and/or modify  *
0006  *   it under the terms of the GNU General Public License as published by  *
0007  *   the Free Software Foundation; either version 2 of the License, or     *
0008  *   (at your option) any later version.                                   *
0009  ***************************************************************************/
0010 
0011 #include "fulladder.h"
0012 
0013 #include "libraryitem.h"
0014 #include "logic.h"
0015 
0016 #include <KLocalizedString>
0017 
0018 void FullAdder_inStateChanged(void *objV, bool state) { // Enable
0019     FullAdder *objT = static_cast<FullAdder*>(objV);
0020     objT->inStateChanged(state);
0021 }
0022 
0023 Item *FullAdder::construct(ItemDocument *itemDocument, bool newItem, const char *id)
0024 {
0025     return new FullAdder(static_cast<ICNDocument *>(itemDocument), newItem, id);
0026 }
0027 
0028 LibraryItem *FullAdder::libraryItem()
0029 {
0030     return new LibraryItem(QStringList(QString("ec/adder")), i18n("Adder"), i18n("Integrated Circuits"), "ic1.png", LibraryItem::lit_component, FullAdder::construct);
0031 }
0032 
0033 FullAdder::FullAdder(ICNDocument *icnDocument, bool newItem, const char *id)
0034     : Component(icnDocument, newItem, (id) ? id : "adder")
0035 {
0036     m_name = i18n("Adder");
0037 
0038     ALogic = BLogic = inLogic = nullptr;
0039     outLogic = SLogic = nullptr;
0040 
0041     QStringList pins = QString("A,B,>,,S,C").split(',', Qt::KeepEmptyParts);
0042     initDIPSymbol(pins, 48);
0043     initDIP(pins);
0044 
0045     ECNode *node;
0046 
0047     node = ecNodeWithID("S");
0048     SLogic = createLogicOut(node, false);
0049 
0050     node = ecNodeWithID("C");
0051     outLogic = createLogicOut(node, false);
0052 
0053     node = ecNodeWithID("A");
0054     ALogic = createLogicIn(node);
0055 
0056     node = ecNodeWithID("B");
0057     BLogic = createLogicIn(node);
0058 
0059     node = ecNodeWithID(">");
0060     inLogic = createLogicIn(node);
0061 
0062     //ALogic->setCallback(this, (CallbackPtr)(&FullAdder::inStateChanged));
0063     ALogic->setCallback2(FullAdder_inStateChanged, this);
0064     //BLogic->setCallback(this, (CallbackPtr)(&FullAdder::inStateChanged));
0065     BLogic->setCallback2(FullAdder_inStateChanged, this);
0066     //inLogic->setCallback(this, (CallbackPtr)(&FullAdder::inStateChanged));
0067     inLogic->setCallback2(FullAdder_inStateChanged, this);
0068 }
0069 
0070 FullAdder::~FullAdder()
0071 {
0072     ALogic->setCallback2(nullptr, nullptr);
0073     BLogic->setCallback2(nullptr, nullptr);
0074     inLogic->setCallback2(nullptr, nullptr);
0075 }
0076 
0077 void FullAdder::inStateChanged(bool /*state*/)
0078 {
0079     const bool A = ALogic->isHigh();
0080     const bool B = BLogic->isHigh();
0081     const bool in = inLogic->isHigh();
0082 
0083     const bool out = (!A && B && in) || (A && !B && in) || (A && B);
0084     const bool S = (!A && !B && in) || (!A && B && !in) || (A && !B && !in) || (A && B && in);
0085 
0086     SLogic->setHigh(S);
0087     outLogic->setHigh(out);
0088 }