Reorganized folders, added pal-adapter io device folder

This commit is contained in:
Daniele Verducci su MatissePenguin
2021-01-02 16:33:51 +01:00
parent 55f5d62082
commit 68752e85a7
35 changed files with 327 additions and 1 deletions

View File

@ -0,0 +1,78 @@
/**
* 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.
* The Python Terminal Monitor or the Arduino IDE serial monitor is used to send
* and receive commands to/from the Z80.
*
* 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.
*/
// EN 2 // Input, Active low
// WR 11 // Input, Active low
// RS 12 // Input, low = DATA register, high = DATA_AVAILABLE register
// DATA BUS (Input/Output, active high): 3, 4, 5, 6, 7, 8, 9, 10;
byte incomingBuffer = 0; // Incoming from computer, to the Pat80
byte outgoingBuffer = 0; // Outgoing to computer, from the Pat80
byte availableBytes = 0; // Available bytes in the incoming buffer (for the DATA_AVAILABLE register)
void setup() {
DDRD = B00000010; // Port D (used arduino pins 2 (EN) and 3 to 7 (DATA)) is input. Avoid changing serial pins.
DDRB = B00000000; // Port B (used arduino pins 8 to 10 (DATA), 11 (WR) and 12 (RS) is input
Serial.begin(2000000);
Serial.println("Pat80 terminal");
attachInterrupt(digitalPinToInterrupt(2), onClk, CHANGE);
}
void loop() {
if (Serial.available() > 0) {
incomingBuffer = Serial.read();
availableBytes = 1; // TODO: Implement a 256 byte buffer and store the avail bytes number in this var
}
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("]");
}
outgoingBuffer = 0;
}
}
void onClk() {
// In any case, return to high impedance state
DDRD = B00000010;
DDRB = B00000000;
if ((PIND & B00000100) == 0) {
// EN is LOW: Clock pulse started
if ((PINB & B00001000) == B00001000) { // WR is HIGH (Pat80 wants to Read (we send data))
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
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;
}
} else {
// Pat80 wants to Write (we receive data)
outgoingBuffer = (PIND >> 3) | (PINB << 5); // Compose the final byte from the two parts
}
}
}

View File

@ -0,0 +1,153 @@
#! /usr/bin/env python3
# -*- coding: utf-8 -*-
""" @package docstring
ARDUINO PARALLEL TERMINAL EMULATOR
USAGE:
Connect the arduino to a Pat80 I/O port.
Flash /arduino/arduino_terminal firmware into the Arduino.
Connect the Arduino to a PC via USB and power on Pat80
Run this program providing the Arduino USB port
DISCLAIMER:
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
import serial
import curses
import time
import textwrap
import os
class TerminalEmulator:
SYNC_SLEEP = 0.001
def __init__(self, w, ser):
w.clear()
w.move(0,0)
while True:
# read serial port and write to curses
if ser.inWaiting():
b = ser.read(1)
if ord(b) > 31 or ord(b) == 10:
w.addch(b)
# read key and write to serial port
key = w.getch()
if key == 10 or (key > 31 and key < 256):
# Is a character
time.sleep(self.SYNC_SLEEP)
ser.write(bytes([key]))
elif int(key) == 1: # CTRL+A, enter ADB mode
# Save cursor position
(xPos, yPos) = w.getyx()
self.adbMode(w, ser)
# Restore cursor position
w.move(xPos, yPos)
w.refresh()
def adbMode(self, w, ser):
stdscr.nodelay(False)
curses.echo()
# Clear first line
w.move(0,0)
w.clrtoeol()
# Ask for file path
w.addstr(0, 0, '[ADB MODE] file to load:', curses.A_REVERSE)
path = w.getstr()
try:
header = bytearray(2)
size = os.path.getsize(path)
# Compute the two header bytes needed to declare the length of the stream
header[1] = size & 255 # get lower 8 bits
size >>= 8 # shift by 8 bits
header[0] = size & 255 # get second lower 8 bits
# Log size for debug
w.addstr("Header bytes: {}".format(header.hex()))
w.refresh()
# Send the two heading bytes (most significant first)
ser.write(header[0:1])
time.sleep(self.SYNC_SLEEP)
ser.write(header[1:2])
time.sleep(self.SYNC_SLEEP)
# Send the actual binary stream
with open(path, "rb") as f:
byte = f.read(1)
while byte:
# Check if terminal interface (Arduino) is busy
ser.write(b'\x01') # COMMAND_BUFFER
ser.read()
ser.write(byte)
byte = f.read(1)
except IOError as e:
w.move(0,0)
w.clrtoeol()
w.addstr(" {}".format(str(e)), curses.A_REVERSE)
w.refresh()
curses.noecho()
stdscr.nodelay(True)
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(
formatter_class=argparse.RawTextHelpFormatter,
epilog=textwrap.dedent('''\
Pat80 Terminal Emulator with ADB (Assembly Deploy Bridge) support.
CTRL+C Exits
CTRL+A ADB mode: sends binary file
''')
)
parser.add_argument('port', help="arduino parallel monitor USB port")
parser.add_argument('baudrate', help="arduino parallel monitor USB baudrate")
args = parser.parse_args()
# Init curses
stdscr = curses.initscr()
curses.noecho()
curses.cbreak()
stdscr.idlok(True)
stdscr.scrollok(True)
stdscr.nodelay(True)
exitMessage = None
exitSuccess = True
try:
ser = serial.Serial(args.port, args.baudrate, timeout=0)
td = TerminalEmulator(stdscr, ser)
except KeyboardInterrupt:
exitMessage = 'Bye!'
exitSuccess = True
except Exception as e:
exitMessage = str(e)
exitSuccess = False
finally:
# Close serial
ser.close()
# Stop curses
curses.nocbreak()
curses.echo()
curses.endwin()
# Print exit message
print(exitMessage)
exit(0 if exitSuccess else 1)