The State Machine

The processing of characters sent to BBL is handled by a state machine. The incoming character is classified by a type (``token type'') that indicates if the character can be interpreted as a command or command parameter or is simply to be displayed. The state machine is a matrix with rows identified by states and columns identified by the token type of the incoming character; the elements of the matrix identify the action to be taken and resulting next state when a token of the specified type is processed when the machine is in each particular state.

In version 1 of BBL, the commands available, invoked by the incoming character stream, are:

〈FF〉
clear screen & move cursor to home

〈BS〉
move cursor back one character position (same line)

〈CR〉
move cursor to beginning of line

〈LF〉
move cursor down one line (or scroll up)

〈VT〉〈digit〉
position cursor on line 〈digit〉; e.g. 〈VT〉1 positions cursor on line 1, no change in column position

〈HT〉〈digit〉
position cursor on column 〈digit〉; e.g., 〈HT〉3 positions cursor on column 3, no change in line position

〈SO〉〈digit〉
with 〈digit〉 = [0|1|2] means set display mode to 0 (3rows x 10char/row), 1 (1x5), or 2 (1x3).

Accordingly, the state machine has four states:

        0:      waiting-for-character
        1:      VT-waiting-for-digit
        2:      HT-waiting-for-digit
        3:      SO-waiting-for-digit
and the character set (256 chars) is classified by the following types:
        0:      non-action char	--- just display (any but the following)
        1:      digit char {0..9}
        2:      move-cursor {FF, BS, CR, LF}
        3:      Hor-position {HT}
        4:      Vert-position {VT}
        5:      Set-mode {SO}
Note that the whole 256-char set has displayable characters in the 8x8 ROM, so all could be displayed except the ones used for control functions here. If you want to extend BBL to display the symbol located at the place in ROM addressed by one of these action chars, you'll need to add an escape char that can be used to prefix the control chars used above and modify the state tables accordingly.

With the definition of tokens types and states above, the following state table is used to define the actions and transitions:

Token Type char digit move ht vt so
0 1 2 3 4 5
State
0 Emit, Emit, Act, Noop, Noop, Mode,
0 0 0 2 1 3
1 beep, VPos, beep, beep, beep, beep,
0 0 0 0 0 0
2 beep, HPos, beep, beep, beep, beep,
0 0 0 0 0 0
3 beep, beep, beep, beep, beep, SetMode,
0 0 0 0 0 0

The actions include:

        Emit:    display the character coming in from the input stream
        Act:     perform the action indicated by the cursor-moving command
        beep:    error: sound bell, reset state
        HPos:    position cursor horizontally
        VPos:    position cursor vertically
        SetMode: set mode for display.

Incoming characters are categorized by direct table addressing, using the value of the character as an index (actually, the value of the character, divided by two is the index since the 4-bit token-class values are stored two nibbles per byte). The token-class table (``TC'') is built dynamically at initialization time (see below).

The state table is constructed as a matrix using a STRUC definition that gives the action procedure and next state associated with each 〈current-state,token-class〉 pair. This is a static matrix.

Using the state table makes the code easier to modify (just modify the state table and categorize the token type of any new command characters), and it makes the processing of commands in the input stream very fast (direct table addressing to find the routine to invoke and next state).