/*
Demonstration of a D flip-flop designed using a master-slave configuration
Allows you to investigate each gate in the flip-flop.
Check out Entry 8 at https://blog.jochumzen.com/p/entries.html
Specifically, check out the schematics under Step-by-Step Construction..
Click D and CLK once to get the simulation going.
*/
//Library with basic gates
#include <ACELBG.h>
//Create an Arduino board
auto* ard = new ArduinoBoard();
//Define chips
auto* masterL = new NandLatch((char*)("Master"));
auto* slaveL = new NandLatch((char*)("Slave"));
auto* ngate1 = new NandGate((char*)("Master Upper"));
auto* ngate2 = new NandGate((char*)("Master Lower"));
auto* ngate3 = new NandGate((char*)("Slave Upper"));
auto* ngate4 = new NandGate((char*)("Slave Lower"));
auto* invD = new Inverter((char*)("Invert D"));
auto* invCLK = new Inverter((char*)("Invert CLK"));
auto* dFlipFlop = new DFlipFlop((char*)("D flip-flop"));
//Add all chips to be emulated here
Chip* chips[] = {
masterL, slaveL, ngate1, ngate2, ngate3, ngate4, invD, invCLK, dFlipFlop
};
//Add all connections here
ConnectionBase* connections[] = {
// D is on pin 2, CLK on pin 3. Final Q output is on pin 4
//Inverters
new Connection(ard, 2, invD->X),
new Connection(ard, 3, invCLK->X),
//Master
new Connection(ard, 2, ngate1->X1),
new Connection(invCLK->Y, ngate1->X2),
new Connection(invD->Y, ngate2->X1),
new Connection(invCLK->Y, ngate2->X2),
new Connection(ngate1->Y, masterL->Reset),
new Connection(ngate2->Y, masterL->Set),
//Slave
new Connection(masterL->Q, ngate3->X1),
new Connection(ard, 3, ngate3->X2),
new Connection(masterL->Q_Bar, ngate4->X1),
new Connection(ard, 3, ngate4->X2),
new Connection(ngate3->Y, slaveL->Reset),
new Connection(ngate4->Y, slaveL->Set),
new Connection(slaveL->Q, ard, 4),
//Using the ACEL D-flip-flop
new Connection(ard, 2, dFlipFlop->D),
new Connection(ard, 3, dFlipFlop->Clock),
new Connection(dFlipFlop->Q, ard, 5),
};
//Section 6: Setting up the emulation. No need to change
Emulation emulation = Emulation(ard, chips, sizeof(chips)/2,
connections, sizeof(connections)/2);
void setup() {
Serial.begin(9600); Serial.println(); Serial.println("START");
Serial.println();
emulation.makePrintPin(13, 1);
}
void loop() {
emulation.emulate();
}
D
CLK
Q
Print
Q D flip-flop