 
 // Alternatively you can leave both of the above undefined to use hardware
 // serial, which is a bit easier on the AVR, but runs at a slightly high baud
-// rate that isn't compatible with the current version of UPURS.
+// rate that isn't compatible with UPURS.
 
 // --- Debug options ---
 

#endif

void setup() {
	// Init USB serial port with dummy speed value
	SerialUSB.begin(9600);

	// Init hardware or software serial port for comms with BBC/Electron
	pinMode(TXD_PIN, OUTPUT);
	pinMode(RXD_PIN, INPUT);
#if defined(USE_INVERTED_SOFTWARE_SERIAL) || defined(USE_SOFTWARE_SERIAL)
	SerialBBC.begin(114000);  // 114678 bps using SoftwareSerial
#else
	SerialBBC.begin(115200);  // 117647 bps using HardwareSerial
#endif

	// Configure flow control
	pinMode(CTS_PIN, INPUT);
	pinMode(RTS_PIN, OUTPUT);
	digitalWrite(RTS_PIN, HIGH);
}

static unsigned long last_activity = 0;

void loop() {
	// static int written = 0;

	while (1) {
		while (digitalRead(CTS_PIN) == HIGH && SerialUSB.available()) {
			int c = SerialUSB.read();
			SerialBBC.write(c);
			SerialBBC.flush();
#ifdef DEBUG_USB_LOOPBACK
			SerialUSB.write(c);
#endif
			last_activity = millis();
			// ++written;
		}

#ifndef IGNORE_BBC_RX
		while (SerialBBC.available()) {
			SerialUSB.write(SerialBBC.read());
			last_activity = millis();
		}
#endif

#ifdef DEBUG_REPORT_WHEN_QUIET
		if (millis() - last_activity > 1000) {
			SerialUSB.print("status: ");
			SerialUSB.print(digitalRead(CTS_PIN) == HIGH ? "cts high" : "cts low");
			SerialUSB.println();
			last_activity = millis();
		}
#endif
	}
}


// ----------------------------------------------------------------------

// This code is designed to run on an Atmel ATMEGA32U4 chip (in my case, a $3
// Pro Micro clone from Aliexpress), connected to a BBC Micro or Acorn
// Electron running Martin Barr's UPURS ROM software.

// --- Serial comms options ---
