-----------------------------------------------------------------------------
-----------------------------------------------------------------------------
 82-PORTS.TXT      A Programmer's Guide to TI-82 Ports           Version 0.6
            Copyright (c) 1999 Mattias Lindqvist & Dines Justesen
-----------------------------------------------------------------------------
-----------------------------------------------------------------------------

This file tries to explain what the TI82's ports are used for, and how to use
them. Suggestions, additions and corrections are very welcome, email me at
the address below.

Dines Justesen
email: c958362@student.dtu.dk
www  : http://www.student.dtu.dk/~c958362

Most of the ports on the TI82 is used in the same way as on the TI85.
Because the TI82 has another kind of display some of the ports has been
moved to another location, but most of them are used in exactly the sameway.
Therefore most code written for the TI85 can easily be translated to the
TI82.

-----------------------------------------------------------------------------
Port 0 : Link port                                                       R/W
-----------------------------------------------------------------------------

This port is used to communicate with other calculators, computers and CBL
equipment. The port is used in the same ways as the link port on the TI85.

The port consist of two parts, the Port Configuration Register (PCR) and the
Port Data Register (PDR). PCR controls whether the PDR is used as input or as
an output (PCRx controls PDRx). PDR is used to send and receive data.

Bit  Function
--------------------------------------------------------------------
 7   PCR3
 6   PCR2
 5   PCR1
 4   PCR0
 3   PDR3
 2   PDR2
 1   PDR1
 0   PDR0

PDR bit 0 and 2 is used to control/read the voltage on the tip, while bit 1
and 3 is used for the middle. The base is always used for ground.

Each of the the data lines have the same circuit attached (shown below).

Normally PCR is set to C0 by the ROM, which sets          Vdd
PDR0/1 as inputs and PDR2/3 as outputs. Doing this        ---
means that the voltage at the tip and the middel           |
can be read as PDR1/0. PDR2/3 is then used to control      < 10 KOhms
the output (a one gives an output of app. 0V).             >
                                                           |
When two calculators are connected together the            V Diode
output will be high if neither of them has their           |
transistor on, otherwise it will be low (wired or)    out--O---------- PDR0/1
                                                           |
                                                           < 470 Ohms
                                                           >
                                                           |
                                                      NPN  \__/\/\____ PDR2/3
                                                           /  33 KOhms
                                                           |
                                                         __|___
                                                          Gnd

Write:
------

When writing to the link port the upper nibble sets the PCR, while the lower
sets the PDR. The state of the tip and the middle can be set using the
following values.

Value  Function
----------------------------------------------------
C0     White wire is positive. Red wire is positive.
D4     White wire is positive. Red wire is grounded.
E8     White wire is grounded. Red wire is positive.
FC     Whits wire is grounded. Red wire is grounded.

The above values are the values used by the ROM, except for FC which is not used
since the system never sets both wires to ground. Other values can be used, but
these gives the best results.

Read:
-----

When the a byte is read from the link port, the upper nibble contains the last
value written to the PCR. Assuming that these are set to 1100b (e.g. one of the
above mentioned values where written to PCR), the contence of the PDR can be
interpeted as follows.

Bit  State Function
------------------------------------------------------------------------
2+3        Last value written
1    1     White wire is positive.
     0     White wire is grounded.
0    1     Red wire is positive. 
     0     Red wire is grounded.

-----------------------------------------------------------------------------
Port 1 : Keyboard                                                        R/W
-----------------------------------------------------------------------------

This port is used to read the keypad.

When a byte is written to port 1, any bit set will mask out the keys listed
below.

Bit  Keys
------------------------------
7
6    Y= WINDOW ZOOM TRACE GRAPH 2ND MODE DEL
5    STO> LN LOG X^2 X^-1 MATH ALPHA
4    0 1 4 7 , SIN MATRX x,T,o
3    . 2 5 8 ( COS PRGM STAT
2    (-) 3 6 9 ) TAN VARS
1    ENTER + - * / ^ CLEAR
0    UP DOWN LEFT RIGHT

When the keypad is read,a low bit means that one of the keys listed has
been pressed.

Bit  Key
------------------------------------------------
7    ALPHA x,T,o DEL STAT
6    MATH MATRX PRGM VARS CLEAR MODE
5    X^-1 SIN COS TAN ^
4    X^2 , ( ) / Y=
3    LOG 7 8 9 * WINDOW UP
2    LN 4 5 6 - F3 ZOOM RIGTH
1    STO> 1 2 3 + TRACE LEFT
0    0 . (-) ENTER GRAPH DOWN

-----------------------------------------------------------------------------
Port 2 : Set rom page                                                    R/W
-----------------------------------------------------------------------------

This port changes/returns the current rom page used. The ROM page chosen here
is mapped into memory starting from $4000 and ending at $7FFF.

Bit  Function
--------------------------------------------------------
0-3  Number of rom page (0 to 7)
4-7  Unknown, but use 10001 otherwise the calc might crash

-----------------------------------------------------------------------------
Port 3 : Status                                                          R/W
-----------------------------------------------------------------------------

Port 3 is used check/set the status of misc hardware on the TI82.

Write:

Bit  Function
------------------------------
4-7  Not used ?
3    LCD Status + (1 = Off) ???
2    Mask timer interrupts if clear ?
1    VBI
0    Mask ON key interrupts ?

Read:

Bit  Function
------------------------------
4-7  Always 0 (not used)
3    On key status (1 = not pressed)
2-0  Not know

-----------------------------------------------------------------------------
Port 4 : Interrupt control                                               W?
-----------------------------------------------------------------------------

This register is controlles the speed of the hardware interrupts.

Bit  Function
------------------------------
5-7  Not used
4    ? (set to 1)
3    Not used
1-2  Interrupt speed (11 = normal, 00 = fastest)
0    Calc frezes if set

-----------------------------------------------------------------------------
Port 10 : Control port for the display controller                        R/W
-----------------------------------------------------------------------------

Write:

Send a command to this port to tell the controller what to do on the display,
then write the needed data to the Data port. For instructions on how to use
this port see below.

Read:

Bit  Function
------------------------------
7    Busy (clear if an instruction can be accepted)
6    Word length (Set when 8 bits is used)
5    Set when display is ON
4    Set when the display controller is in the reset state
3-2  Always 0
1-0  Direction of cursor increment 

The direction is set as follows:

Bit 1-0 Direction
-----------------
   0    Left
   1    Rigth
   2    Up
   3    Down.

-----------------------------------------------------------------------------
Port 11 : Data port for the display controller                           R/W
-----------------------------------------------------------------------------

After having written the needed control words to the control port, the data
values are read from/written to this port. For instructions on how to use
this port see below.

-----------------------------------------------------------------------------
The Display Controller
-----------------------------------------------------------------------------

The display on the TI82 is controlled by a display controller (Toshiba T6A04),
and can only be accessed using ports 10 and 11. This following paragraphs,
explains how to use the varius function of the display controller be writting
directly to the ports. For more information on the display controller see the
data sheet available from Toshibas web site.

The Z80 used in the TI82 is able to send information to the display controller
faster than it can accept it. Therefore it is necessary to include a delay
between each write operation, for the display controller to work. The ROM
includes a delay function which should be called BEFORE each read/write
operation. This delay is however longer than necessary.

Communicating with the display controller
-----------------------------------------

The display controller has two registers which are used to store the position
of the build-in cursor. Any data writen to the controller will be stored at
the current location, and a read operation will return the data stored here.
After a read or write operation the cursor position is automatically
incremented, which makes writting larger blocks of data easier.

When ever a byte is written to the command port, the command is stored in the
input register of the display controller and it is then executed. A byte written
to the data port is stored at the current location and the cursor position is
incremented.

When a byte is read from the data port the value returned is the contents of
the output register. After the value is returned the data from the current
cursor position is stored to the register and the cursor position is updated.
This means that when ever the cursor position is changed the first value read
is not the correct data, but rather the data from before the cursor position
was changed. The correct data will appear as the 2nd byte read form the port.

Set contrast
------------
Any command writen to port 10 which has the 2 MSB set will alter the contrast.
The valid values for the contast is 00 to 3F, with 3F being the darkest
and 00 the lightest. So to set the contrast to the value in a use the
following code (a has to hold a valid contrast value).

    or   0C0h         ; Set the two MSB's
    call 7F3h         ; Delay use for display commands
    out  (10h),a      ; Write command to port
    ret

Note that this function will set the contrast, but it will be returned to the
previus value if the build in functions are used. The system functions uses
contrast values 0 to 20, and the current value is stored in the system part of
the RAM. To avoid that the contrast is set to what it was before remember to
change the value in the system RAM too.

Read/set pixel
--------------

The display controller writes/reads from the position indicated by the display
cursor. The cursor can be moved by writing a command to port 10h, followed by
a number which indicates where to go. One command will change the line another
one the coloum.
                                                    20h  . . . . . . . .  2Bh
                                                  (0,0)
The drawing on the left shows the coordinates  80h  +----------------------+
used by the system and the display control-         |                      |
ler. The values in () is the system coordi-     .   | y                    |
nates, the other ones are the coordinates       .   | |                    |
used by the display controller. The system      .   | v                    |
uses another coordinate set too, when this is   .   |                      |
used the top line (line 0 before) is called 63      |   x-->               |
and the bottom line line 1. The values in the  BFh  +----------------------+
x direction are the same as before except that                        (63,95)
the last line is not used. So the pixel in the
upper rigth corner is called (63d,94d). Note that the display controller
actually uses x value from 20 to 2F, but the last bytes are not shown on the
display. 

The command to change the coloum is 20 + the number of the coloum. The valid
coloum numbers are 00 to 0F, with 00 being the coloum most to the left.
So to go to coloum 0 use the following code.

  ld    a,20h    ; Write coloum number
  call  7F3h     ; Display delay
  out   (10h),a

The command to change the line is 80h + the number of the line. Valid line
numbers are from 0 to 3Fh, with 0 being the topmost line.

So to goto the line at the bottom of the display use the following code.

  ld    a,0BFh   ; Write line number
  call  7F3h     ; Display delay
  out   (10h),a

After the cursor position has been changed the first value read from the data
portis not the byte at that position. So after each change of cursor position
you have to make one read operation before you start reading data.

Block Write
-----------

After each read/write operation the cursor position is automaticly increased.
This function can be used to read/write larger amount of data at a time.

You can use the commands 4 to 7 to choose the direction in which the cursor
moves after a write/read operation.

Command Direction
-----------------
   4    Left
   5    Rigth
   6    Up
   7    Down.

The ROM contains two functions which changes the direction,the advantages of
using them is that they do not alter the A register (see 82-ROM for
information on these rutines).

Hardware scroll
---------------

The display controller can scroll the picture it is currently showing, but
only up/down. This will not move the picture in the controllers internal
memory, but it will show the picture starting from a different address. The
command to scroll the picture is 40 + the number of lines you want the
display scrolled. So for example to scroll the display 3 lines (up) you could
use the following code.

   ld a,$43
   out ($10),a
   call $7F3

On/Off
------

The display controller can be told to start/stop sending data to the display
by sending the commands 2 (stop) and 3 (start). This does not turn off the
display even though it looks clear, you have to use port 3 to turn off the
power to the display. Stopping the display controller will not erase the
memory in it, but turning off the power will.


Word length
-----------

The word length (or width of the coloumns) can be set to either 8 bits or 6
bits (the system always uses 8). When using 6 bits the displays shows 16 columns
of 6 pixels instead of 12 columns of 8 bits. When using 6 pixel wide sprites
this can be used to make faster routines. Command for 8 bit wide columns is 1,
and 0 gives 6 bits wide coloumns.

Other functions
---------------

Besides tha above listed function  the display controller also recognizese the
commands listed below.

#      Function
----------------
08..0F Op-amp Power Control 1
10..17 Op-amp Power Control 2
18..1F Test Mode Select

These function are generally not useful in assembly programs and will there
for not be discribed here, see the datasheet for more information.

