 sttl Table Initialization
 pag

 if DEBUG&DBG_SYS
00 fcc $d,'Init CLISTs, Return = $',0
01 fcc $d,'Init TTYs, Return = $',0
02 fcc $d,'Init Tasks, Return = $',0
03 fcc $d,'STBinit done, Return = $',0
 endif
*
* stbinit - Initialize System Memory Tables
*
stbinit ldx #sys_vars clear all variables
 ldd #0
10 std ,x++
 cmpx #end_vars done yet?
 blo 10b
 if DEBUG&DBG_SYS
 ldx #00b
 jsr DB_pdata
 ldd 0,s
 jsr DB_phex2
 endif
* Initialize Terminal Interrupt Queue
 ldx #TI_Q set queue empty
 stx TI_Q_ptr
 ldx #int_buf reset message in pointer
 stx int_ptr
 bsr clinit initialize Clists
 if DEBUG&DBG_SYS
 ldx #01b
 jsr DB_pdata
 ldd 0,s
 jsr DB_phex2
 endif
 bsr ttyinit initialize TTY tables
 if DEBUG&DBG_SYS
 ldx #02b
 jsr DB_pdata
 ldd 0,s
 jsr DB_phex2
 endif
 lbsr tskinit initialize tasks
 if DEBUG&DBG_SYS
 ldx #03b
 jsr DB_pdata
 ldd 0,s
 jsr DB_phex2
 endif
 rts

*
* clinit
*
* Initialize character buffer lists.  This routine
* is only called once at startup time.
*

clinit ldx #CLISTS point to clist
 ldb #MAX_CL set count
 stx cfreel set head pointer
clini2 leay CBSIZE,x
 sty 0,x set fwd link
 tfr y,x advance to next
 decb
 bne clini2
 lda #MAX_CL-2 get buffer count
 sta lcbuf set max count
 clra make d=0
 std -CBSIZE,x clear last link
 clr cbufct
 rts all done!

*
* ttyinit
*
* Init the tty tables and queues.
*

ttyinit ldy #TTYQS point to q tables
 clr ,-s terminal # counter
 ldb #MAX_TRM get terminal count
 pshs b
 ldx #TTYTABS initialize TTY tables
 stx ttytab point to tty table
 ldu #TTY_ports port addresses
 pshs u not enough registers
 ldu #OBUFRS output buffer
ttini2 sty tqin,x set input q
 leay QH_SIZE,y get next q
 sty tqproc,x set pr q
 leay QH_SIZE,y set next q
 sty tqout,x set output q
 stu oq_buf,y ** Init output Q **
 stu oq_get,y
 stu oq_put,y
 leau OBUF_SIZE,u
 stu oq_end,y
 leay OQH_SIZE,y get next q
 lda #%101<<2 8 bits, no parity, 1 stop bit
 sta tbaud,x set conf word
 pshs u save register
 ldu 2,s get table address
 ldd ,u++ get port address
 std taddr,x initialize port address
 ldd ,u high/low part of switch to read?
 lda [,u++] fetch switch value
 bitb #1
 bne 10f jump for odd side
 lsra get high nibble
 lsra
 lsra
 lsra
 bra 15f
10 anda #$0F get low nibble
15 ora #$10 set up baud rate
 sta tbaud2,x
 stu 2,s update register on stack
 puls u restore other pointer - whew!
 lda 3,s get terminal #
 sta tdevic,x set in table
 inc 3,s bump terminal #
 leax TTYSIZ,x next tty entry
 dec 2,s dec the count
 bne ttini2 repeat?
 puls d,u,pc clean up stack & return
*
TTY_ports fdb PORT1,SWITCH1+1 Terminal 0
      fdb PORT2,SWITCH1 Terminal 1
      fdb PORT3,SWITCH2+1 Terminal 2
NUM_TTY equ (*-TTY_ports)/4 Number of Terminals

*
* tskinit - Initialize all tasks
*
tskinit ldx #tsktab
 stx utask
 ldb #MAX_TSK # tasks in system
 pshs b
10
 if DEBUG&DBG_SYS
 pshs d,x
 ldx #90f
 lbsr DB_pdata
 ldd 2,s
 lbsr DB_phex2
 puls d,x
 endif
 lda #TFREE mark all tasks free
 cmpx #tsktab is this the system task?
 bne 15f
 lda #TSYS the system task is never free
15 sta tsstat,x
 lda #$FF not associated with a terminal
 sta tsdev,x
 leau TSKSIZ,x initialize stack
 ldd #IO_han
 cmpx #tsktab+TSKSIZ task 1?
 bne 20f no - normal I/O handling task
 ldd #TI_HAN yes - special terminal interrupt handling task
20 pshu d
 stu usp,x
 stu umark0,x
 leax TSKSIZ,x
 dec 0,s done?
 bne 10b
 leas 1,s clean up stack
 ldd #0 nothing running or sleeping
 std runlst
 std slplst
 std slplst+tsslnk
 ldx #tsktab+TSKSIZ start interrupt handling task
 jsr makrdy
 rts
 if DEBUG&DBG_SYS
90 fcc $d,'... Init task $',0
 endif
