\ Terminal program for driving the RS-232 port vandys
only extensions also drivers definitions
variable comport
: com ( n -- ) 1- 1 swap - $100 * $2F0 + comport ! ;
: defport ( off -- ) create , does> @ comport @ + ;
$B defport LINE $8 defport LOWBAUD $9 defport HIBAUD
$D defport STAT $8 defport DATA $2 defport IER
: baud! ( baud -- ) LINE inb swap $80 LINE outb
16 * 1843200 swap / dup LOWBAUD outb 8 rshift HIBAUD outb
LINE outb ;
: cominit ( baud -- ) baud! 3 LINE outb $C1 IER outb ;
: comput? ( -- <bool> ) STAT inb $20 and 0<> ;
: comput ( c -- ) DATA outb ;
: comget? ( -- <bool> ) STAT inb $1 and 0<> ;
: comget ( -- c ) DATA inb $7F and ;
: term ( -- ) begin comget? if comget emit then
key? comput? and if key dup 26 ( ^Z ) = if drop exit then
comput then again ;
\ Support for running a CLI on a COM port--buffering of typing vandys
128 constant COMBUF
create com_typing COMBUF allot variable com_ntyped
: com_typing_deq ( -- c ) com_typing c@ ( c )
com_ntyped @ 1- dup com_ntyped ! ( c u )
?dup if com_typing dup 1+ swap rot move then ;
\ Terminal operations implemented by emitting ANSI escape sequences vandys
\ Note ansi_put_scr only works for system standard console geometry (80x25)
: esc ( -- ) 27 emit ;
: ansi_put_scr ( a -- ) CONS_ROWS 0 do
esc [char] [ emit i 1+ 1 u.r ." ;1H"
esc ." [K" dup CONS_COLS -trailing type
CONS_COLS + loop drop ;
: ansi_cons_op ( ... op -- ... op F | T )
dup 3 = if drop
esc ." [m" esc ." [7h" esc ." [2J" esc ." [H"
true exit then
dup 4 = if drop swap
esc [char] [ emit 1+ 1 u.r [char] ; emit
1+ 1 u.r [char] H emit true exit then
dup 5 = if drop esc ." [K" true exit then
dup 6 = if drop esc [char] [ emit
if [char] 1 emit then [char] m emit true exit then
dup 8 = if drop ansi_put_scr true exit then
false ;
\ 'ttyops for console operation on a COM port vandys
: com_watcher ( -- ) begin begin pause comget? until comget
com_ntyped @ dup COMBUF = if 2drop else ( c u )
com_typing + c! com_ntyped inc then
again ;
: com_cons_op ( op -- ... ) pause dup 2 = if drop
com_ntyped @ if com_typing_deq true else false then
exit then
dup 1 = if drop begin comput? 0= while pause repeat
DATA outb exit then
ansi_cons_op if exit then
1 abort" Bad com_cons_op" ;
\ Launch CLI on com port vandys
also os
: >'ttyops ( a-user -- a-'ttyops ) [ 'ttyops up @ - ] literal + ;
\ Initialize the desired COM port before invoking this
: fork_com ( -- )
fork ?dup 0= if com_watcher then setrun
fork ?dup 0= if quit then
['] com_cons_op over >'ttyops ! 0 over >ttchan ! setrun ;
Put this all in "drivers", even the utility parts
Base port for currently selected port
: com Set COM 1/2, adjusting to base port address
: defport Make it easy to access the various RS-232 ports by name,
reflecting currently selected I/O port base
: baud! Calculate divisor values for high/low baud, set baud
: cominit Set baud, then set 8N1, then enable FIFO
: comput? Tell if there's room to put another char
: comput Put out another char (assumes you've checked comput?)
: comget? Tell if there's a char available
: comget Return next char (you've checked comget?, of course)
: term Terminal interactive loop, exits on ^Z input
# chars of input we buffer from the COM port
COM port input buffer, and current count
: com_typing_deq Pull a char off the input FIFO
Decrement count of buffered typing
Shuffle down the queue of chars if there are any more
: esc Emit an escape
: ansi_put_scr Bulk put of screen contents; not so friendly on an actual
RS-232 TTY. If we really cared, should do update optimization ala curses,
but for now just paint the screen top to bottom.
: ansi_cons_op Common code for ANSI-driven terminals, factored out
so that, for instance, Avram can hook to it from the telnetd code.
Opcode 3: Clear screen, cancel standout, home cursor
Opcode 4: cursor addressing
Opcode 5: clear to end of line ("blot")
Opcode 6: set/clear "standout" display enhancement
Opcode 8: Bulk put to screen
: com_watcher Thread which pulls typing from COM port and queues it
for consumption via 'ttyops/getc
: com_cons_op TTY operations for COM port TTYs
Opcode 2: Get char
Opcode 1: Put char wait for UART ready to accept
Put the data out
Stuff handled by ANSI escape sequences
Anything else--very bad
: >'ttyops Given user pointer, return pointer to 'ttyops field in it
: fork_com Launch thread serving CLI for COM port. Also launch thread
to wait for data on port and queueing it.