2020-11-22 22:06:45 +01:00
|
|
|
/**
|
|
|
|
* Terminal interface.
|
|
|
|
* This sketch allow an Arduino to be used as a terminal to log into Pat80.
|
|
|
|
* The Arduino is connected to the Pat80 I/O bus and to the terminal computer via USB.
|
2020-12-16 08:49:28 +01:00
|
|
|
* The Python Terminal Monitor or the Arduino IDE serial monitor is used to send
|
|
|
|
* and receive commands to/from the Z80.
|
|
|
|
*
|
2021-01-01 23:09:42 +01:00
|
|
|
* Seen from the PC, the terminal receives two bytes: a command byte and a value byte.
|
|
|
|
* Commands:
|
|
|
|
* 0x00 WRITE: The next byte is sent as-is to Pat80. If the terminal interface buffer
|
|
|
|
* is not empty, the new byte will replace the older one.
|
|
|
|
* 0x01 BUFFER: The terminal interface returns the number of bytes waiting to be sent.
|
|
|
|
*
|
2020-12-16 08:49:28 +01:00
|
|
|
* Seen from the Pat80, the terminal interface has two registers:
|
|
|
|
* DATA Register at addr 0x00 (\RS) contains the last received byte from the pc
|
|
|
|
* DATA_AVAILABLE Register at addr 0x01 (RS) contains the number of bytes in the buffer,
|
|
|
|
* waiting to be read from the Pat80. A READ operation on DATA register removes the
|
|
|
|
* byte from the buffer and decrements DATA_AVAILABLE.
|
2020-11-22 22:06:45 +01:00
|
|
|
*/
|
|
|
|
|
2020-12-06 15:51:26 +01:00
|
|
|
// EN 2 // Input, Active low
|
|
|
|
// WR 11 // Input, Active low
|
2020-12-16 08:49:28 +01:00
|
|
|
// RS 12 // Input, low = DATA register, high = DATA_AVAILABLE register
|
2020-12-06 15:51:26 +01:00
|
|
|
// DATA BUS (Input/Output, active high): 3, 4, 5, 6, 7, 8, 9, 10;
|
2020-11-22 22:06:45 +01:00
|
|
|
|
2021-01-01 23:09:42 +01:00
|
|
|
const byte COMMAND_WRITE = 0x00;
|
|
|
|
const byte COMMAND_BUFFER = 0x01;
|
|
|
|
|
2020-11-29 12:19:55 +01:00
|
|
|
byte incomingBuffer = 0; // Incoming from computer, to the Pat80
|
|
|
|
byte outgoingBuffer = 0; // Outgoing to computer, from the Pat80
|
2020-12-16 08:49:28 +01:00
|
|
|
byte availableBytes = 0; // Available bytes in the incoming buffer (for the DATA_AVAILABLE register)
|
2020-11-29 12:19:55 +01:00
|
|
|
|
2020-12-06 15:51:26 +01:00
|
|
|
void setup() {
|
2020-11-29 12:19:55 +01:00
|
|
|
DDRD = B00000010; // Port D (used arduino pins 2 (EN) and 3 to 7 (DATA)) is input. Avoid changing serial pins.
|
2020-12-16 08:49:28 +01:00
|
|
|
DDRB = B00000000; // Port B (used arduino pins 8 to 10 (DATA), 11 (WR) and 12 (RS) is input
|
2020-12-06 15:51:26 +01:00
|
|
|
|
|
|
|
Serial.begin(2000000);
|
|
|
|
Serial.println("Pat80 terminal");
|
|
|
|
|
2020-11-29 12:19:55 +01:00
|
|
|
attachInterrupt(digitalPinToInterrupt(2), onClk, CHANGE);
|
2020-11-22 22:06:45 +01:00
|
|
|
}
|
|
|
|
|
2020-11-29 12:19:55 +01:00
|
|
|
void loop() {
|
2021-01-02 07:56:11 +01:00
|
|
|
if (Serial.available() > 1) {
|
2021-01-01 23:09:42 +01:00
|
|
|
switch (Serial.read()) {
|
|
|
|
case COMMAND_WRITE:
|
2021-01-02 07:56:11 +01:00
|
|
|
while (Serial.available() < 1) {} // Waits for the second byte
|
2021-01-01 23:09:42 +01:00
|
|
|
incomingBuffer = Serial.read();
|
|
|
|
availableBytes = 1; // TODO: Implement a 256 byte buffer and store the avail bytes number in this var
|
|
|
|
break;
|
|
|
|
case COMMAND_BUFFER:
|
|
|
|
Serial.write(availableBytes);
|
|
|
|
break;
|
|
|
|
}
|
2020-11-29 12:19:55 +01:00
|
|
|
}
|
2021-01-01 23:09:42 +01:00
|
|
|
|
2020-11-29 12:19:55 +01:00
|
|
|
if (outgoingBuffer != 0) {
|
|
|
|
if ((outgoingBuffer >= 8 && outgoingBuffer <= 13) || (outgoingBuffer >= 32 && outgoingBuffer <= 127)) {
|
|
|
|
// Printable character
|
|
|
|
Serial.print((char)outgoingBuffer);
|
|
|
|
} else {
|
|
|
|
// Non-printable character
|
|
|
|
Serial.print("[0x");
|
|
|
|
Serial.print(outgoingBuffer, HEX);
|
|
|
|
Serial.print("]");
|
|
|
|
}
|
2020-12-06 15:51:26 +01:00
|
|
|
outgoingBuffer = 0;
|
2020-11-29 12:19:55 +01:00
|
|
|
}
|
|
|
|
}
|
2020-11-22 22:06:45 +01:00
|
|
|
|
|
|
|
void onClk() {
|
2020-11-29 12:29:54 +01:00
|
|
|
// In any case, return to high impedance state
|
|
|
|
DDRD = B00000010;
|
|
|
|
DDRB = B00000000;
|
2020-12-06 15:51:26 +01:00
|
|
|
if ((PIND & B00000100) == 0) {
|
2020-11-29 12:19:55 +01:00
|
|
|
// EN is LOW: Clock pulse started
|
2020-12-06 15:51:26 +01:00
|
|
|
if ((PINB & B00001000) == B00001000) { // WR is HIGH (Pat80 wants to Read (we send data))
|
2020-11-22 22:06:45 +01:00
|
|
|
DDRD = DDRD | B11111000; // Port D (arduino pins 3 to 7) is output. In or to preserve serial pins and interrupt pin
|
|
|
|
DDRB = B00000111; // Port B (0,1,2) = pins 8,9,10 output
|
2020-12-16 08:49:28 +01:00
|
|
|
if ((PINB & B00010000) == B00010000) { // RS is HIGH: we send number of bytes available in buffer
|
|
|
|
// Split byte to two parts
|
|
|
|
PORTD = availableBytes << 3;
|
|
|
|
PORTB = availableBytes >> 5;
|
|
|
|
} else {
|
|
|
|
// Split byte to two parts
|
|
|
|
PORTD = incomingBuffer << 3;
|
|
|
|
PORTB = incomingBuffer >> 5;
|
|
|
|
incomingBuffer = 0;
|
|
|
|
availableBytes = 0;
|
|
|
|
}
|
2020-11-22 22:06:45 +01:00
|
|
|
} else {
|
|
|
|
// Pat80 wants to Write (we receive data)
|
2020-11-29 12:19:55 +01:00
|
|
|
outgoingBuffer = (PIND >> 3) | (PINB << 5); // Compose the final byte from the two parts
|
2020-11-22 22:06:45 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|