1////////////////////////////////////////////////////////////////////////////////
2// //
3// This software is supplied under the terms of a license agreement or //
4// nondisclosure agreement with Mitov Software and may not be copied //
5// or disclosed except in accordance with the terms of that agreement. //
6// Copyright(c) 2002-2016 Mitov Software. All Rights Reserved. //
7// //
8////////////////////////////////////////////////////////////////////////////////
910
#ifndef _MITOV_LOGIC_FLIP_FLOPS_h
11#define _MITOV_LOGIC_FLIP_FLOPS_h
1213
#include <Mitov.h>
1415
namespace Mitov
16{
17class BasicFlipFlop : public Mitov::CommonSource
18{
19typedef Mitov::CommonSource inherited;
2021
protected:
22bool FValue = false;
2324
public:
25OpenWire::SourcePin InvertedOutputPin;
2627
protected:
28void SetValue( bool AValue )
29{
30FValue = AValue;
31SendOutput();
32}
3334
void SendOutput()
35{
36OutputPin.SendValue( FValue );
37InvertedOutputPin.SendValue( !FValue );
38}
3940
protected:
41virtual void SystemStart() override
42{
43inherited::SystemStart();
44SendOutput();
45}
4647
};
48//---------------------------------------------------------------------------
49class SRFlipFlop : public BasicFlipFlop
50{
51typedef Mitov::BasicFlipFlop inherited;
5253
public:
54OpenWire::SinkPin SetInputPin;
55OpenWire::SinkPin ResetInputPin;
5657
protected:
58void DoReceiveSet( void *_Data )
59{
60bool AValue = *(bool *)_Data;
61if( AValue )
62SetValue( true );
63}
6465
void DoReceiveReset( void *_Data )
66{
67bool AValue = *(bool *)_Data;
68if( AValue )
69SetValue( false );
70}
7172
public:
73SRFlipFlop()
74{
75SetInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&SRFlipFlop::DoReceiveSet );
76ResetInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&SRFlipFlop::DoReceiveReset );
77}
78};
79//---------------------------------------------------------------------------
80class ClockableFlipFlop : public Mitov::SRFlipFlop
81{
82typedef Mitov::SRFlipFlop inherited;
8384
public:
85OpenWire::SinkPin ClockInputPin;
8687
protected:
88virtual void DoClock() = 0;
8990
protected:
91void DoClockReceive( void *_Data )
92{
93DoClock();
94}
9596
public:
97ClockableFlipFlop()
98{
99ClockInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&ClockableFlipFlop::DoClockReceive );
100}
101102
};
103//---------------------------------------------------------------------------
104class TFlipFlop : public Mitov::ClockableFlipFlop
105{
106typedef Mitov::ClockableFlipFlop inherited;
107108
public:
109OpenWire::SinkPin ToggleInputPin;
110111
protected:
112bool FToggleValue = true;
113114
protected:
115void DoReceiveToggle( void *_Data )
116{
117FToggleValue = *(bool *)_Data;
118}
119120
protected:
121virtual void DoClock() override
122{
123if( FToggleValue )
124SetValue( !FValue );
125}
126127
public:
128TFlipFlop()
129{
130ToggleInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&TFlipFlop::DoReceiveToggle );
131}
132133
};
134//---------------------------------------------------------------------------
135class DFlipFlop : public Mitov::ClockableFlipFlop
136{
137typedef Mitov::ClockableFlipFlop inherited;
138139
public:
140OpenWire::SinkPin DataInputPin;
141142
protected:
143bool FData = false;
144145
protected:
146virtual void DoClock() override
147{
148SetValue( FData );
149}
150151
protected:
152void DoReceiveData( void *_Data )
153{
154FData = *(bool *)_Data;
155}
156157
public:
158DFlipFlop()
159{
160DataInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&DFlipFlop::DoReceiveData );
161}
162163
};
164//---------------------------------------------------------------------------
165class JKFlipFlop : public Mitov::BasicFlipFlop
166{
167public:
168OpenWire::SinkPin JInputPin;
169OpenWire::SinkPin KInputPin;
170OpenWire::SinkPin ClockInputPin;
171172
protected:
173bool FJ : 1;
174bool FK : 1;
175176
protected:
177void DoReceiveJ( void *_Data )
178{
179FJ = *(bool *)_Data;
180}
181182
void DoReceiveK( void *_Data )
183{
184FK = *(bool *)_Data;
185}
186187
void DoClockReceive( void *_Data )
188{
189if( FJ && FK )
190SetValue( !FValue );
191192
else if( FJ )
193SetValue( true );
194195
else if( FK )
196SetValue( false );
197198
}
199200
public:
201JKFlipFlop() :
202FJ( false ),
203FK( false )
204{
205JInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&JKFlipFlop::DoReceiveJ );
206KInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&JKFlipFlop::DoReceiveK );
207ClockInputPin.SetCallback( this, (OpenWire::TOnPinReceive)&JKFlipFlop::DoClockReceive );
208}
209210
};
211//---------------------------------------------------------------------------
212}
213214
#endif