Search This Blog

Sunday, July 29, 2018

ChibiTerm - Vt100 implementation

Projects / ChibiTerm  Original post date: 05/23/2016

I have been avoiding this part as I read about Terminal Emulation on vt100.net. It scares me a bit as the ANSI standard is too open ended.
"However, X3.64 defines many implementation-dependent features and error conditions without defining recovery procedures."
I found Joe Smith's Ansi code article, there is a section for " Minimum requirements for VT100 emulation" at the bottom. I guess I'll use that as a starting point to nail down the basic for a milestone.
  1. To act as a passive display, implement the 4 cursor commands, the 2 erase commands, direct cursor addressing, and at least inverse characters. The software should be capable of handling strings with 16 numeric parameters with values in the range of 0 to 255.
  2. To enter data in VT100 mode, implement the 4 cursor keys and the 4 PF keys. It must be possible to enter ESC, TAB, BS, DEL, and LF from the keyboard.
  3. When doing full-screen editing on a VT100, implement directed erase, the numeric keypad in applications mode, and the limited scrolling region. The latter is needed to do insert/delete line functions without rewriting the screen.
  4. If the hardware is capable of double width/double height:
  5. If the terminal emulator is capable of insert/delete characters, insert/delete lines, insert/replace mode, and can do a full-screen dump to the printer (in text mode), then it should identify itself as a VT102.
I took the advice of writing a parser for the CSI sequence before trying to implement the commands. I wrote a simple parser based on the following from Joe's article.

General rules for interpreting a Control Sequence:
  1. It starts with CSI, the Control Sequence Introducer.
  2. It contains any number of parameter characters (0123456789:;<=>?).
  3. It terminates with an alphabetic character.
  4. Intermediate characters (if any) immediately precede the terminator.
I do not know enough about error recovery or badly coded ANSI sequence. I can't and won't expect every single piece of code out there would work.

References:
http://www.vt100.net/docs/vt100-ug/
http://aperiodic.net/phil/archives/Geekery/term-function-keys.html

"VT100" isn't too well defined as there are various implementations that do different things. Just for the irony, here is my list. I might try vttest at some point to see how bad my implementation is.
  • The hardware can only support the 7-bit ASCII table, monochrome and inverse video attribute. It is a baseline VT100 without any processor/graphic attachments nor printers, no scroll back buffer, no LED, no Bell, no fancy graphics/extra character sets/132 columns/double width etc.
  • One need to use the stty command to take advantage of the larger screen size.
        stty rows
  • Since the terminal only transmit keystrokes or small report packets, I am tempted to not implement flow control for transmit. The user can use XON/XOFF to pause/resume the data coming from the host. The firmware is surprisingly fast and doesn't requiring handshaking.
  • The keypad is under user control via Num Lock key.

Here is how I mapped the keys on a PC keyboard base on how things work on a PC.

PF1-PF4 are mapped to function keys.
[ - ] and [ , ] are mapped to [ - ] and [ + ]
User has control of numeric keypad vs application key mapping by using Num Lock key.

Note:
  • The firmware handles the keyboard LED updates and mapping, so someone could in theory redefine/reassign a different key for the Num Lock function.
  • Please ignore the cursor key labels on the keypad as the application keys might be mapped differently by host software.
Progress so far based on Joe Smith's Minimal VT100 requirement list

List 1: Implemented

  • [A Move cursor up one row, stop if a top of screen
  • [B Move cursor down one row, stop if at bottom of screen
  • [C Move cursor forward one column, stop if at right edge of screen
  • [D Move cursor backward one column, stop if at left edge of screen
  • [H Home to row 1 column 1 (also [1;1H)
  • [J Clear from current position to bottom of screen
  • [K Clear from current position to end of line
  • [24;80H Position to line 24 column 80 (any line 1 to 24, any column 1 to 132)
  • [0m Clear attributes to normal characters
  • [7m Add the inverse video attribute to succeeding characters
  • [0;7m Set character attributes to inverse video only

List 2: Implemented

  • [A Sent by the up-cursor key (alternately ESC O A)
  • [B Sent by the down-cursor key (alternately ESC O B)
  • [C Sent by the right-cursor key (alternately ESC O C)
  • [D Sent by the left-cursor key (alternately ESC O D)
  • PF1 key sends ESC O P
  • PF2 key sends ESC O Q
  • PF3 key sends ESC O R
  • PF4 key sends ESC O S
  • [c Request for the terminal to identify itself
  • [?1;0c : VT100 with memory for 24 by 80, inverse video character attribute

List 3: implemented with minor changes

  • [0J Erase from current position to bottom of screen inclusive
  • [1J Erase from top of screen to current position inclusive
  • [2J Erase entire screen (without moving the cursor)
  • [0K Erase from current position to end of line inclusive
  • [1K Erase from beginning of line to current position inclusive
  • [2K Erase entire line (without moving cursor)
  • not implemented: ESC = Set numeric keypad to applications mode
  • not implemented: ESC > Set numeric keypad to number mode
  • NumLock: Set Numeric keypad for Applications/Number mode
  • [12;24r Set scrolling region to lines 12 thru 24. If a linefeed or an INDex is received while on line 24, the former line 12 is deleted and rows 13-24 move up. If a RI (reverse Index) is received while on line 12, a blank line is inserted there as rows 12-13 move down.
    All VT100 compatible terminals (except GIGI) have this feature.

List 4 and list 5 are not under consideration.

The list is done. Testing is next.


No comments:

Post a Comment

Note: Only a member of this blog may post a comment.