| 1 | // flipflop.hxx - implementation of multiple flip flop types |
| 2 | // |
| 3 | // Written by Torsten Dreyer |
| 4 | // |
| 5 | // Copyright (C) 2010 Torsten Dreyer - Torsten (at) t3r (dot) de |
| 6 | // |
| 7 | // This program is free software; you can redistribute it and/or |
| 8 | // modify it under the terms of the GNU General Public License as |
| 9 | // published by the Free Software Foundation; either version 2 of the |
| 10 | // License, or (at your option) any later version. |
| 11 | // |
| 12 | // This program is distributed in the hope that it will be useful, but |
| 13 | // WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | // General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License |
| 18 | // along with this program; if not, write to the Free Software |
| 19 | // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | // |
| 21 | |
| 22 | #include "flipflop.hxx" |
| 23 | #include "functor.hxx" |
| 24 | #include <simgear/misc/inputvalue.hxx> |
| 25 | #include <Main/fg_props.hxx> |
| 26 | |
| 27 | using std::map; |
| 28 | using std::string; |
| 29 | using std::endl; |
| 30 | using std::cout; |
| 31 | |
| 32 | namespace FGXMLAutopilot { |
| 33 | |
| 34 | /** |
| 35 | * @brief Flip flop implementation for a RS flip flop with dominant RESET |
| 36 | * |
| 37 | * RS (reset-set) flip flops act as a fundamental latch. It has two input lines, |
| 38 | * S (set) and R (reset). Activating the set input sets the output while activating |
| 39 | * the reset input resets the output. If both inputs are activated, the output |
| 40 | * is deactivated, too. This is why the RESET line is called dominant. Use a |
| 41 | * SRFlipFlopImplementation for a dominant SET line. |
| 42 | * |
| 43 | * <table> |
| 44 | * <tr> |
| 45 | * <td colspan="3">Logictable</td> |
| 46 | * </tr> |
| 47 | * <tr> |
| 48 | * <td>S</td><td>R</td><td>Q</td> |
| 49 | * </tr> |
| 50 | * <tr> |
| 51 | * <td>false</td><td>false</td><td>unchanged</td> |
| 52 | * </tr> |
| 53 | * <tr> |
| 54 | * <td>false</td><td>true</td><td>false</td> |
| 55 | * </tr> |
| 56 | * <tr> |
| 57 | * <td>true</td><td>false</td><td>true</td> |
| 58 | * </tr> |
| 59 | * <tr> |
| 60 | * <td>true</td><td>true</td><td>false</td> |
| 61 | * </tr> |
| 62 | * </table> |
| 63 | */ |
| 64 | class RSFlipFlopImplementation : public FlipFlopImplementation { |
| 65 | protected: |
| 66 | bool _rIsDominant; |
| 67 | public: |
| 68 | RSFlipFlopImplementation( bool rIsDominant = true ) : _rIsDominant( rIsDominant ) {} |
| 69 | virtual bool getState( double dt, DigitalComponent::InputMap input, bool & q ); |
| 70 | }; |
| 71 | |
| 72 | /** |
| 73 | * @brief Flip flop implementation for a RS flip flop with dominant SET |
| 74 | * |
| 75 | * SR (set-reset) flip flops act as a fundamental latch. It has two input lines, |
| 76 | * S (set) and R (reset). Activating the set input sets the output while activating |
| 77 | * the reset input resets the output. If both inputs are activated, the output |
| 78 | * is activated, too. This is why the SET line is called dominant. Use a |
| 79 | * RSFlipFlopImplementation for a dominant RESET line. |
| 80 | * |
| 81 | * <table> |
| 82 | * <tr> |
| 83 | * <td colspan="3">Logictable</td> |
| 84 | * </tr> |
| 85 | * <tr> |
| 86 | * <td>S</td><td>R</td><td>Q</td> |
| 87 | * </tr> |
| 88 | * <tr> |
| 89 | * <td>false</td><td>false</td><td>unchanged</td> |
| 90 | * </tr> |
| 91 | * <tr> |
| 92 | * <td>false</td><td>true</td><td>false</td> |
| 93 | * </tr> |
| 94 | * <tr> |
| 95 | * <td>true</td><td>false</td><td>true</td> |
| 96 | * </tr> |
| 97 | * <tr> |
| 98 | * <td>true</td><td>true</td><td>true</td> |
| 99 | * </tr> |
| 100 | * </table> |
| 101 | */ |
| 102 | class SRFlipFlopImplementation : public RSFlipFlopImplementation { |
| 103 | public: |
| 104 | SRFlipFlopImplementation() : RSFlipFlopImplementation( false ) {} |
| 105 | }; |
| 106 | |
| 107 | /** |
| 108 | * @brief Base class for clocked flip flop implementation |
| 109 | * |
| 110 | * A clocked flip flop computes it's output on the raising edge (false/true transition) |
| 111 | * of the clock input. If such a transition is detected, the onRaisingEdge method is called |
| 112 | * by this implementation. All clocked flip flops inherit from the RS flip flop and may |
| 113 | * be set or reset by the respective set/reset lines. Note that the RS implementation |
| 114 | * ignores the clock, The output is set immediately, regardless of the state of the clock |
| 115 | * input. The "clock" input is mandatory for clocked flip flops. |
| 116 | * |
| 117 | */ |
| 118 | class ClockedFlipFlopImplementation : public RSFlipFlopImplementation { |
| 119 | private: |
| 120 | /** |
| 121 | * @brief the previous state of the clock input |
| 122 | */ |
| 123 | bool _clock; |
| 124 | protected: |
| 125 | |
| 126 | /** |
| 127 | * @brief pure virtual function to be implemented from the implementing class, gets called |
| 128 | * from the update method if the raising edge of the clock input was detected. |
| 129 | * @param input a map of named input lines |
| 130 | * @param q a reference to a boolean variable to receive the output state |
| 131 | * @return true if the state has changed, false otherwise |
| 132 | */ |
| 133 | virtual bool onRaisingEdge( DigitalComponent::InputMap input, bool & q ) = 0; |
| 134 | public: |
| 135 | |
| 136 | /** |
| 137 | * @brief constructor for a ClockedFlipFlopImplementation |
| 138 | * @param rIsDominant boolean flag to signal if RESET shall be dominant (true) or SET shall be dominant (false) |
| 139 | */ |
| 140 | ClockedFlipFlopImplementation( bool rIsDominant = true ) : RSFlipFlopImplementation( rIsDominant ), _clock(false) {} |
| 141 | |
| 142 | /** |
| 143 | * @brief evaluates the output state from the input lines. |
| 144 | * This method basically waits for a raising edge and calls onRaisingEdge |
| 145 | * @param dt the elapsed time in seconds from since the last call |
| 146 | * @param input a map of named input lines |
| 147 | * @param q a reference to a boolean variable to receive the output state |
| 148 | * @return true if the state has changed, false otherwise |
| 149 | */ |
| 150 | virtual bool getState( double dt, DigitalComponent::InputMap input, bool & q ); |
| 151 | }; |
| 152 | |
| 153 | /** |
| 154 | * @brief Implements a JK flip flop as a clocked flip flop |
| 155 | * |
| 156 | * The JK flip flop has five input lines: R, S, clock, J and K. The R and S lines work as described |
| 157 | * in the RS flip flop. Setting the J line to true sets the output to true on the next raising |
| 158 | * edge of the clock line. Setting the K line to true sets the output to false on the next raising |
| 159 | * edge of the clock line. If both, J and K are true, the output is toggled at with every raising |
| 160 | * edge of the clock line. |
| 161 | * |
| 162 | * Undefined inputs default to false. |
| 163 | * |
| 164 | * <table> |
| 165 | * <tr> |
| 166 | * <td colspan="7">Logictable</td> |
| 167 | * </tr> |
| 168 | * <tr> |
| 169 | * <td>S</td><td>R</td><td>J</td><td>K</td><td>clock</td><td>Q (previous)</td><td>Q</td> |
| 170 | * </tr> |
| 171 | * <tr> |
| 172 | * <td>false</td><td>false</td><td>false</td><td>false</td><td>any</td><td>any</td><td>unchanged</td> |
| 173 | * </tr> |
| 174 | * <tr> |
| 175 | * <td>true</td><td>false</td><td>any</td><td>any</td><td>any</td><td>any</td><td>true</td> |
| 176 | * </tr> |
| 177 | * <tr> |
| 178 | * <td>any</td><td>true</td><td>any</td><td>any</td><td>any</td><td>any</td><td>false</td> |
| 179 | * </tr> |
| 180 | * <tr> |
| 181 | * <td>false</td><td>false</td><td>true</td><td>false</td><td>^</td><td>any</td><td>true</td> |
| 182 | * </tr> |
| 183 | * <tr> |
| 184 | * <td>false</td><td>false</td><td>false</td><td>true</td><td>^</td><td>any</td><td>false</td> |
| 185 | * </tr> |
| 186 | * <tr> |
| 187 | * <td>false</td><td>false</td><td>true</td><td>true</td><td>^</td><td>false</td><td>true</td> |
| 188 | * </tr> |
| 189 | * <tr> |
| 190 | * <td>false</td><td>false</td><td>true</td><td>true</td><td>^</td><td>true</td><td>false</td> |
| 191 | * </tr> |
| 192 | * </table> |
| 193 | */ |
| 194 | class JKFlipFlopImplementation : public ClockedFlipFlopImplementation { |
| 195 | public: |
| 196 | /** |
| 197 | * @brief constructor for a JKFlipFlopImplementation |
| 198 | * @param rIsDominant boolean flag to signal if RESET shall be dominant (true) or SET shall be dominant (false) |
| 199 | */ |
| 200 | JKFlipFlopImplementation( bool rIsDominant = true ) : ClockedFlipFlopImplementation ( rIsDominant ) {} |
| 201 | |
| 202 | /** |
| 203 | * @brief compute the output state according to the logic table on the raising edge of the clock |
| 204 | * @param input a map of named input lines |
| 205 | * @param q a reference to a boolean variable to receive the output state |
| 206 | * @return true if the state has changed, false otherwise |
| 207 | */ |
| 208 | virtual bool onRaisingEdge( DigitalComponent::InputMap input, bool & q ); |
| 209 | }; |
| 210 | |
| 211 | /** |
| 212 | * @brief Implements a D (delay) flip flop. |
| 213 | * |
| 214 | */ |
| 215 | class DFlipFlopImplementation : public ClockedFlipFlopImplementation { |
| 216 | public: |
| 217 | /** |
| 218 | * @brief constructor for a DFlipFlopImplementation |
| 219 | * @param rIsDominant boolean flag to signal if RESET shall be dominant (true) or SET shall be dominant (false) |
| 220 | */ |
| 221 | DFlipFlopImplementation( bool rIsDominant = true ) : ClockedFlipFlopImplementation ( rIsDominant ) {} |
| 222 | |
| 223 | /** |
| 224 | * @brief compute the output state according to the logic table on the raising edge of the clock |
| 225 | * @param input a map of named input lines |
| 226 | * @param q a reference to a boolean variable to receive the output state |
| 227 | * @return true if the state has changed, false otherwise |
| 228 | */ |
| 229 | virtual bool onRaisingEdge( DigitalComponent::InputMap input, bool & q ) { |
| 230 | q = input.get_value("D"); |
| 231 | return true; |
| 232 | } |
| 233 | }; |
| 234 | |
| 235 | /** |
| 236 | * @brief Implements a T (toggle) flip flop. |
| 237 | * |
| 238 | */ |
| 239 | class TFlipFlopImplementation : public ClockedFlipFlopImplementation { |
| 240 | public: |
| 241 | /** |
| 242 | * @brief constructor for a TFlipFlopImplementation |
| 243 | * @param rIsDominant boolean flag to signal if RESET shall be dominant (true) or SET shall be dominant (false) |
| 244 | */ |
| 245 | TFlipFlopImplementation( bool rIsDominant = true ) : ClockedFlipFlopImplementation ( rIsDominant ) {} |
| 246 | |
| 247 | /** |
| 248 | * @brief compute the output state according to the logic table on the raising edge of the clock |
| 249 | * @param input a map of named input lines |
| 250 | * @param q a reference to a boolean variable to receive the output state |
| 251 | * @return true if the state has changed, false otherwise |
| 252 | */ |
| 253 | virtual bool onRaisingEdge( DigitalComponent::InputMap input, bool & q ) { |
| 254 | q = !q; |
| 255 | return true; |
| 256 | } |
| 257 | }; |
| 258 | |
| 259 | /** |
| 260 | * @brief Implements a monostable flip flop |
| 261 | * |
| 262 | * The stable output state is false. |
| 263 | * |
| 264 | */ |
| 265 | class MonoFlopImplementation : public JKFlipFlopImplementation { |
| 266 | protected: |
| 267 | virtual bool configure( SGPropertyNode& cfg_node, |
| 268 | const std::string& cfg_name, |
| 269 | SGPropertyNode& prop_root ); |
| 270 | simgear::ValueList _time; |
| 271 | double _t; |
| 272 | |
| 273 | bool isConfigProperty(const std::string& cfg_name) const override |
| 274 | { |
| 275 | return cfg_name == "time"; |
| 276 | } |
| 277 | |
| 278 | public: |
| 279 | /** |
| 280 | * @brief constructor for a MonoFlopImplementation |
| 281 | * @param rIsDominant boolean flag to signal if RESET shall be dominant (true) or SET shall be dominant (false) |
| 282 | */ |
| 283 | MonoFlopImplementation( bool rIsDominant = true ) : JKFlipFlopImplementation( rIsDominant ), _t(0.0) {} |
| 284 | /** |
| 285 | * @brief evaluates the output state from the input lines and returns to the stable state |
| 286 | * after expiry of the internal timer |
| 287 | * @param dt the elapsed time in seconds from since the last call |
| 288 | * @param input a map of named input lines |
| 289 | * @param q a reference to a boolean variable to receive the output state |
| 290 | * @return true if the state has changed, false otherwise |
| 291 | */ |
| 292 | virtual bool getState( double dt, DigitalComponent::InputMap input, bool & q ); |
| 293 | }; |
| 294 | |
| 295 | } // namespace |
| 296 | |
| 297 | using namespace FGXMLAutopilot; |
| 298 | |
| 299 | //------------------------------------------------------------------------------ |
| 300 | bool MonoFlopImplementation::configure( SGPropertyNode& cfg_node, |
| 301 | const std::string& cfg_name, |
| 302 | SGPropertyNode& prop_root ) |
| 303 | { |
| 304 | if( JKFlipFlopImplementation::configure(cfg_node, cfg_name, prop_root) ) |
| 305 | return true; |
| 306 | |
| 307 | if (cfg_name == "time") { |
| 308 | _time.push_back(new simgear::Value(prop_root, cfg_node)); |
| 309 | return true; |
| 310 | } |
| 311 | |
| 312 | return false; |
| 313 | } |
| 314 | |
| 315 | bool MonoFlopImplementation::getState( double dt, DigitalComponent::InputMap input, bool & q ) |
| 316 | { |
| 317 | if( JKFlipFlopImplementation::getState( dt, input, q ) ) { |
| 318 | _t = q ? _time.get_value() : 0; |
| 319 | return true; |
| 320 | } |
| 321 | |
| 322 | _t -= dt; |
| 323 | if( _t <= 0.0 ) { |
| 324 | q = 0; |
| 325 | return true; |
| 326 | } |
| 327 | |
| 328 | return false; |
| 329 | } |
| 330 | |
| 331 | |
| 332 | bool RSFlipFlopImplementation::getState( double dt, DigitalComponent::InputMap input, bool & q ) |
| 333 | { |
| 334 | bool s = input.get_value("S"); |
| 335 | bool r = input.get_value("R"); |
| 336 | |
| 337 | // s == false && q == false: no change, keep state |
| 338 | if( s || r ) { |
| 339 | if( _rIsDominant ) { // RS: reset is dominant |
| 340 | if( s ) q = true; // set |
| 341 | if( r ) q = false; // reset |
| 342 | } else { // SR: set is dominant |
| 343 | if( r ) q = false; // reset |
| 344 | if( s ) q = true; // set |
| 345 | } |
| 346 | return true; // signal state changed |
| 347 | } |
| 348 | return false; // signal state unchagned |
| 349 | } |
| 350 | |
| 351 | bool ClockedFlipFlopImplementation::getState( double dt, DigitalComponent::InputMap input, bool & q ) |
| 352 | { |
| 353 | bool c = input.get_value("clock"); |
| 354 | bool raisingEdge = c && !_clock; |
| 355 | |
| 356 | _clock = c; |
| 357 | |
| 358 | if( RSFlipFlopImplementation::getState( dt, input, q ) ) |
| 359 | return true; |
| 360 | |
| 361 | |
| 362 | if( !raisingEdge ) return false; //signal no change |
| 363 | return onRaisingEdge( input, q ); |
| 364 | } |
| 365 | |
| 366 | bool JKFlipFlopImplementation::onRaisingEdge( DigitalComponent::InputMap input, bool & q ) |
| 367 | { |
| 368 | bool j = input.get_value("J"); |
| 369 | bool k = input.get_value("K"); |
| 370 | |
| 371 | // j == false && k == false: no change, keep state |
| 372 | if( (j || k) ) { |
| 373 | if( j && k ) { |
| 374 | q = !q; // toggle |
| 375 | } else { |
| 376 | if( j ) q = true; // set |
| 377 | if( k ) q = false; // reset |
| 378 | } |
| 379 | return true; // signal state changed |
| 380 | } |
| 381 | |
| 382 | return false; // signal no change |
| 383 | } |
| 384 | |
| 385 | //------------------------------------------------------------------------------ |
| 386 | bool FlipFlopImplementation::configure( SGPropertyNode& prop_root, |
| 387 | SGPropertyNode& cfg ) |
| 388 | { |
| 389 | for( int i = 0; i < cfg.nChildren(); ++i ) |
| 390 | { |
| 391 | SGPropertyNode_ptr child = cfg.getChild(i); |
| 392 | string cname(child->getNameString()); |
| 393 | |
| 394 | if( configure(*child, cname, prop_root) ) |
| 395 | continue; |
| 396 | } |
| 397 | |
| 398 | return true; |
| 399 | } |
| 400 | |
| 401 | |
| 402 | static map<string,FunctorBase<FlipFlopImplementation> *> componentForge; |
| 403 | |
| 404 | //------------------------------------------------------------------------------ |
| 405 | bool FlipFlop::configure( SGPropertyNode& cfg_node, |
| 406 | const std::string& cfg_name, |
| 407 | SGPropertyNode& prop_root ) |
| 408 | { |
| 409 | if( componentForge.empty() ) { |
| 410 | componentForge["RS"] = new CreateAndConfigureFunctor<RSFlipFlopImplementation,FlipFlopImplementation>(); |
| 411 | componentForge["SR"] = new CreateAndConfigureFunctor<SRFlipFlopImplementation,FlipFlopImplementation>(); |
| 412 | componentForge["JK"] = new CreateAndConfigureFunctor<JKFlipFlopImplementation,FlipFlopImplementation>(); |
| 413 | componentForge["D"] = new CreateAndConfigureFunctor<DFlipFlopImplementation, FlipFlopImplementation>(); |
| 414 | componentForge["T"] = new CreateAndConfigureFunctor<TFlipFlopImplementation, FlipFlopImplementation>(); |
| 415 | componentForge["monostable"] = new CreateAndConfigureFunctor<MonoFlopImplementation, FlipFlopImplementation>(); |
| 416 | } |
| 417 | |
| 418 | if( DigitalComponent::configure(cfg_node, cfg_name, prop_root) ) |
| 419 | return true; |
| 420 | |
| 421 | if( cfg_name == "type" ) { |
| 422 | string type(cfg_node.getStringValue()); |
| 423 | if( componentForge.count(type) == 0 ) { |
| 424 | SG_LOG |
| 425 | ( |
| 426 | SG_AUTOPILOT, |
| 427 | SG_BULK, |
| 428 | "unhandled flip-flop type <" << type << ">" |
| 429 | ); |
| 430 | return true; |
| 431 | } |
| 432 | _implementation = (*componentForge[type])(prop_root, *cfg_node.getParent()); |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | if (cfg_name == "set"||cfg_name == "S") { |
| 437 | _input["S"] = sgReadCondition(&prop_root, &cfg_node); |
| 438 | return true; |
| 439 | } |
| 440 | |
| 441 | if (cfg_name == "reset" || cfg_name == "R" ) { |
| 442 | _input["R"] = sgReadCondition(&prop_root, &cfg_node); |
| 443 | return true; |
| 444 | } |
| 445 | |
| 446 | if (cfg_name == "J") { |
| 447 | _input["J"] = sgReadCondition(&prop_root, &cfg_node); |
| 448 | return true; |
| 449 | } |
| 450 | |
| 451 | if (cfg_name == "K") { |
| 452 | _input["K"] = sgReadCondition(&prop_root, &cfg_node); |
| 453 | return true; |
| 454 | } |
| 455 | |
| 456 | if (cfg_name == "D") { |
| 457 | _input["D"] = sgReadCondition(&prop_root, &cfg_node); |
| 458 | return true; |
| 459 | } |
| 460 | |
| 461 | if (cfg_name == "clock") { |
| 462 | _input["clock"] = sgReadCondition(&prop_root, &cfg_node); |
| 463 | return true; |
| 464 | } |
| 465 | |
| 466 | if (_implementation && _implementation->isConfigProperty(cfg_name)) { |
| 467 | // componentForge CreateAndConfigureFunctor<> already called configure with all the nodes |
| 468 | // on our implementation, but we need to ensure we don't return false here for such nodes |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | void FlipFlop::update( bool firstTime, double dt ) |
| 476 | { |
| 477 | if( _implementation == NULL ) { |
| 478 | SG_LOG( SG_AUTOPILOT, SG_ALERT, "No flip-flop implementation for " << subsystemId() << endl ); |
| 479 | return; |
| 480 | } |
| 481 | |
| 482 | bool q0, q; |
| 483 | |
| 484 | q0 = q = get_output(); |
| 485 | |
| 486 | if( _implementation->getState( dt, _input, q ) && q0 != q ) { |
| 487 | set_output( q ); |
| 488 | |
| 489 | if(_debug) { |
| 490 | cout << "updating flip-flop \"" << subsystemId() << "\"" << endl; |
| 491 | cout << "prev. Output:" << q0 << endl; |
| 492 | for( InputMap::const_iterator it = _input.begin(); it != _input.end(); ++it ) |
| 493 | cout << "Input \"" << (*it).first << "\":" << (*it).second->test() << endl; |
| 494 | cout << "new Output:" << q << endl; |
| 495 | } |
| 496 | } |
| 497 | } |
| 498 | |
| 499 | |
| 500 | // Register the subsystem. |
| 501 | SGSubsystemMgr::Registrant<FlipFlop> registrantFlipFlop; |