1
/***************************************************************************
2
* Copyright (C) 2018 by santiago González *
3
* santigoro@gmail.com *
5
* This program is free software; you can redistribute it and/or modify *
6
* it under the terms of the GNU General Public License as published by *
7
* the Free Software Foundation; either version 3 of the License, or *
8
* (at your option) any later version. *
10
* This program is distributed in the hope that it will be useful, *
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13
* GNU General Public License for more details. *
15
* You should have received a copy of the GNU General Public License *
16
* along with this program; if not, see <http://www.gnu.org/licenses/>. *
18
***************************************************************************/
20
#include "connector.h"
21
#include "simulator.h"
25
static const char* Bus_properties[] = {
26
QT_TRANSLATE_NOOP("App::Property","Num Bits"),
27
QT_TRANSLATE_NOOP("App::Property","Start Bit")
30
Component* Bus::construct( QObject* parent, QString type, QString id )
32
return new Bus( parent, type, id );
35
LibraryItem* Bus::libraryItem()
37
return new LibraryItem(
39
tr( "Logic/Other Logic" ),
45
Bus::Bus( QObject* parent, QString type, QString id )
46
: Component( parent, type, id )
49
Q_UNUSED( Bus_properties );
51
m_busPin1 = new Pin( 270, QPoint( 0, 0 ), m_id+"-busPinI", 1, this );
52
m_busPin1->setLength( 1 );
53
m_busPin1->setFlag( QGraphicsItem::ItemStacksBehindParent, false );
56
setNumLines( 8 ); // Create Input Pins
58
m_busPin0 = new Pin( 90, QPoint( 0, 0 ), m_id+"-ePin0", 1, this );
59
m_busPin0->setLength( 1 );
60
m_busPin0->setFlag( QGraphicsItem::ItemStacksBehindParent, false );
62
m_busPin1->setIsBus( true );
63
m_busPin0->setIsBus( true );
65
m_ePin[0] = m_busPin0;
67
//m_pin[ m_numLines+1 ] = m_busPin1;
72
void Bus::setNumLines( int lines )
74
if( lines == m_numLines ) return;
75
if( lines < 1 ) return;
77
for( int i=1; i<=m_numLines; i++ )
79
if( m_pin[i]->isConnected() ) m_pin[i]->connector()->remove();
80
if( m_pin[i]->scene() ) Circuit::self()->removeItem( m_pin[i] );
85
m_pin.resize( lines+2 );
86
m_ePin.resize( lines+2 );
88
for( int i=1; i<=lines; i++ )
90
QString pinId = m_id+"-ePin"+QString::number(i);
91
Pin* pin = new Pin( 180, QPoint(-8, -8*lines+i*8 ), pinId, i, this );
93
pin->setFontSize( 4 );
94
pin->setLabelColor( QColor( 0, 0, 0 ) );
95
pin->setLabelText( " "+QString::number( m_startBit+i-1 ) );
99
m_busPin1->setPos( QPoint( 0 ,-lines*8+8 ) );
100
m_busPin1->isMoved();
101
m_busPin1->setLabelPos();
102
m_pin[ lines+1 ] = m_busPin1;
103
m_ePin[ lines+1 ] = m_busPin1;
106
m_area = QRect( -3,-m_height*8-2, 5, m_height*8+4 );
107
Circuit::self()->update();
110
void Bus::setStartBit( int bit )
112
eBus::setStartBit( bit );
114
for( int i=1; i<=m_numLines; i++ )
116
m_pin[i]->setLabelText( " "+QString::number( m_startBit+i-1 ) );
120
void Bus::initialize()
122
if( !m_busPin0->isConnected() && !m_busPin1->isConnected() ) return;
124
//qDebug() << "\nBus::initialize()"<< m_id << m_numLines;
126
eNode* busEnode = m_busPin0->getEnode();
127
if( !busEnode ) busEnode = m_busPin1->getEnode();
128
//if( !busEnode ) return;
130
for( int i=1; i<=m_numLines; i++ )
132
if( !m_pin[i]->isConnected() ) continue;
133
//eNode* enode = m_pin[i]->getEnode();
135
eNode* enode = new eNode( m_id+"eNode"+QString::number( i ) );
137
pin->registerPinsW( enode );
141
QList<ePin*> epins = enode->getEpins();
142
//qDebug() << "Registering pins line"<< i << epins;
143
busEnode->addBusPinList( epins, m_startBit+i-1 );
144
//for( ePin* epin : epins )epin->setEnode( 0l );
149
void Bus::inStateChanged( int msg )
151
//qDebug() << "Bus::inStateChanged()"<< m_id << m_numLines;
153
if( m_busPin0->isConnected() || m_busPin1->isConnected() )
155
eNode* enode = new eNode( m_id+"busNode" );
156
enode->setIsBus( true );
157
registerPins( enode );
160
if( msg == 3 ) // Called by m_busPin When disconnected
162
//qDebug() << "Bus::inStateChanged()" << m_numLines;
164
for( int i=1; i<=m_numLines; i++ )
166
if( !m_pin[i]->isConnected() ) continue;
168
eNode* enode = new eNode( m_id+"eNode"+QString::number( i ) );
170
pin->registerPinsW( enode );
175
void Bus::registerPins( eNode* enode )
177
if( !enode->isBus() ) return;
179
if( m_busPin0->isConnected() ) m_busPin0->registerPinsW( enode );
180
if( m_busPin1->isConnected() ) m_busPin1->registerPinsW( enode );
183
void Bus::paint( QPainter *p, const QStyleOptionGraphicsItem *option, QWidget *widget )
185
Component::paint( p, option, widget );
191
p->drawRect( QRect( 0, -m_height*8, 0, m_height*8 ) );
192
//QRect( -2, -m_height*8-4, 2, m_height*8+8 );
195
#include "moc_bus.cpp"