// racy pulse generator wave drawing
//void xprintf(const char *, ...); // needed this? no did it help? seemed to but
struct NAND {
unsigned char in1Pin, in2Pin;
unsigned char outPin;
unsigned char input1;
unsigned char input2;
unsigned char outputValue;
unsigned char initMe;
void begin(unsigned char a, unsigned char b, unsigned char c, unsigned char init) {
initMe = init;
begin(a, b, c);
digitalWrite(outPin, initMe);
};
void begin(unsigned char a, unsigned char b, unsigned char c) {
// xprintf("begin NAND %d %d %d\n", a, b, c);
pinMode(a, INPUT_PULLUP);
in1Pin = a;
pinMode(b, INPUT_PULLUP);
in2Pin = b;
pinMode(c, OUTPUT);
outPin = c;
};
void read() {
input1 = digitalRead(in1Pin);
input2 = digitalRead(in2Pin);
};
void evaluate() {
outputValue = !(input1 && input2);
};
void propogate() {
digitalWrite(outPin, outputValue);
};
unsigned char spill() {
return outputValue;
}
};
NAND SQ, RQN, OP;
void setup()
{
Serial.begin(115200);
// xprintf("synthetic MIT racy pulse generator\n");
SQ.begin(4, 3, 2, 1); // yes need one or the other or startup is odd
RQN.begin(7, 6, 5); // , 1);
OP.begin(10, 9, 8);
pinMode(A4, OUTPUT);
}
unsigned long now;
unsigned long lastTime;
void loop()
{
// xprintf("Hello World\n");
now = millis();
if (now - lastTime < 100) return;
lastTime = now;
digitalWrite(A4, digitalRead(A4) == LOW ? HIGH : LOW); // a transition at every update
SQ.read(); SQ.evaluate();
RQN.read(); RQN.evaluate();
OP.read(); OP.evaluate();
SQ.propogate();
RQN.propogate();
OP.propogate();
/*
Serial.print(digitalRead(10)); Serial.print(",");
Serial.print(OP.spill()); Serial.print(",");
Serial.print(SQ.spill()); Serial.print(",");
Serial.print(RQN.spill()); Serial.println("");
*/
// delay(1);
// static int fork = 20;
// if (!--fork) for (; ; );
}
// programmer misses printf... THX @Danois90 https://forum.arduino.cc/t/pure-c-hello-world/1042838/21
void xprintf(const char *format, ...)
{
char buffer[256];
va_list args;
va_start(args, format);
vsprintf(buffer, format, args);
va_end(args);
Serial.print(buffer);
}
ERC Warnings
flop1:CLK: Clock driven by combinatorial logic