Close

VT52 Compatible Commands on VGATonic, and Custom/Administrator Sequences

A project log for VGA Graphics Over SPI and Serial - VGATonic

640x480 (and 848x480!) Color VGA Video Card for Microcontrollers and Single Board Computers

pkPK 07/23/2015 at 16:240 Comments

VGATonic has a terminal emulator built in, and this post will detail how that works. It uses VT52 compatible commands, and also shows special sequences I added to control how VGATonic works.

For all of these, you need to hit 'escape' or send ASCII 27 first for VGATonic to respond.

VT52 Compatible Commands

To use escape codes, first hit the 'escape' key (or ASCII 27 in base 10 in your program), then hit the second key to execute the command.

	  A - Move the cursor to beginning of line above.
	  B - Move the cursor to beginning of line below.
	  C - Move the cursor right by one.
	  D - Move the cursor left by one.
	  E - Clear the screen and place the cursor in the upper left corner.
	  H - Move the cursor to the upper left corner.
	  I - Move the cursor to beginning of line above.
	  J - Erase all lines after our current line
	  K - Clear the current line from the current cursor position.
	  M - Delete the current line.
	  Y - 'Goto' Coordinate mode - first will change line number, then cursor position (both ASCII - 32)
	  b - Byte after 'b' sets new foreground color.
	  c - Byte after 'c' sets new background color.
	  d - Erase all lines above current line.
	  e - Enable the cursor.
	  f - Disable the cursor.
	  l - Erase current line line and place the cursor at the beginning of the row.
	  o - Erase the current line from the beginning to the current cursor.
	  p - Invert background and foreground color.
	  q - Invert background and foreground color.

VGATonic Framebuffer Codes

The main point of the project is, of course, being a video card... not a terminal emulator. I added some codes which are solely meant to control VGATonic, so you can choose to not use the terminal emulator while in serial mode and use it similar to how it works over SPI and write a pixel at a time. I built in 9600 bps and 38400 bps 8-N-1 asynchronous serial modes, so you'd have to be a masochist to use 640x480x8bpp mode (here are some example transfer times - i.e. 38.4k at top resolution/depth in 82.2 seconds for one frame!), but something like 80x60x1bpp or 80x60x2bpp might be bearable as more than a slideshow.

Obviously, these only work on VGATonic - if you have a VT52 somewhere, they won't work there, haha:

	  % - Administrator Mode (see next section)
	  W - 'W'rite framebuffer mode (pass through pixels from UART, 255 at a time)
	  Z - Send control character to VGATonic.  (Position or Resolution or Bit Depth change)
	  m - Become 'm'aster of CPLD (Disable external SPI writes and put Microcontroller in control)
	  r - 'r'elease CPLD (Allow external SPI writes)
	  s - 's'elect CPLD (Warn CPLD a new frame write is coming)
	  u - 'u'nselect CPLD (Inform CPLD current frame write is done)

VGATonic Administrator Mode

If you hit the '%' key after an Escape key, VGATonic puts you into administrator mode. You would come here to fine tune the PLL for your monitor, or to change the speed of asynchronous serial (it starts at 9600 bps 8-N-1, but you can set it to 38400 bps for a tiny, yet relatively large, speed bump.)

Here is a link to the LTC6903 data sheet so you can see what adding/subtracting one does with the PLL. We're only changing the second byte, and not allowing you to change the last 2 bits, so you have 2^6 options.

	  < - 9600  Baud Serial Terminal (Default)
	  > - 38400 Baud Serial Terminal
	  + - Add one to LTC6903 PLL Scaler
	  - - Subtract one from LTC6903 PLL Scaler
	  & - VGATonic default PLL Scaler, 0B01011110
	  | - Burn current PLL Scaler to EEPROM

Some Example Sequences

VGATonic starts up with the ATTiny in control. It quickly calibrates asynchronous serial (and sets it for 9600 bps 8-N-1), sets the PLL to 2x the VGA pixel clock (actually around 50.344 MHz if you don't change it), changes VGATonic's mode to 160x120x8bpp and draws a green screen to prove it is alive. It then gives up the bus. Here are a few sequences you can use to get into different modes:

To Get Into Terminal Emulator Mode:

  1. Connect to VGATonic at current speed of serial (default: 9600 bps 8-N-1)
  2. Optional: Change serial speed to 38400 bps:
    1. Escape % >
    2. Reconnect at 38400 bps 8-N-1
  3. Escape m
    1. (Become master of VGATonic)
  4. Escape s
    1. (VGATonic chip select; drives SPI low)
  5. Escape Z r
    1. (Set VGATonic to 160x120x8bpp - the only supported terminal mode, just in case it was changed)
  6. Escape E
    1. (Clears the screen - in case it was changed to something else)

VGATonic starts calibrated to 50.344 MHz, but this might not work with the PLL on all monitors. There is a way to change the PLL's setup - although, of course, you should try calibrating the monitor first before changing VGATonic's calibration! Consider this your last option, but always available (you get 100,000 EEPROM writes, however, so don't worry too much about changing it).

To Change the PLL Calibration

  1. Either:
    1. Escape % -
      1. Decrease PLL calibration
    1. Escape % +
      1. Increase PLL Calibration
  2. Then, when happy:
    1. Escape % |
      1. Burn PLL Calibration to EEPROM

Of course, it's possible you set VGATonic to a PLL calibration where you can't get any picture on your VGA monitor. Check all other connections, but if you feel the PLL setting is preventing the monitor from syncing, you can reset to factory:

To Reset PLL to Factory

  1. Escape % &
    1. Reset the PLL calibration to roughly 50.344 MHz
  2. Escape % |
    1. Burn this calibration to EEPROM

Compromises and One Improvement

I did my best with the VT52 commands - but there are a few limitations that I had to compromise on due to only having 128 bytes of RAM. Some RAM is taken up by global variables - FG/BG colors, row/column location, a buffer for ASCII characters sent over serial, etc. A bit is also reserved for local variables, such as counters and offsets (and putting functions return addresses on the stack).

The 'main' buffer is 40 (or, actually, 41 for the null character) bytes long and comprises one column in VGATonic. That means any commands that require multiple line memory we had to skip, and any time we move the cursor up or down we need to go to the 'beginning' of the line. Here is a listing of VT52 escape sequences so you can see the sorts of commands that use those constructs. We also are stuck with the 40 byte limit, so we are always overflowing to the next line instead of wrapping while typing.

Finally, we aren't supporting a few control characters - vertical tabs and bells (I mean, we don't have sound...) for example. Return/new line and delete are fully supported, though, of course!

Our main improvement is color: the VT52 supported 'high brightness' and 'low brightness' 3 bit color, which is better known now as 4 bit RGBI, where the I is an intensity bit. We support the full gamut of 256 colors, so if you use escape characters to change the BG or FG colors, whatever ASCII character you send next will use the full RRRGGGBB color set.

As for improvements?

Prior Art

VGATonic isn't the first AVR terminal emulator, of course - Martin K. Schröder released a mostly VT100 compatible emulator (with better 40x40 row x column support!) for 4k RAM and some smaller parts some time back. If you're considering a similar project, I hope you'll take a look at his code.

Discussions