* QL WORLD DIY TOOLKIT - USE procedure
* Ver. 0.2, Copyright 1988,90 Simon N Goodwin.
*
* Effect:
*
* USE #n%   Makes the specified channel the
* default, to be used in PRINT, INPUT and other
* commands that assume a default channel number
* of #1. Subsequent implicit or explicit references
* to #1 will in fact address #n%.
*
* USE or USE #1 re-sets the default to normal.
*
* N.B. The default for LIST is #2, so USE will
* not affect LIST. Similarly, INKEY$ defaults to
* #0. Various 'SuperToolkit' commands expect
* defaults of #0, #1, #2 or even #3, and 
* therefore not all of them are affected by USE.
* USE works with the following commands:
*
* ARC, ARC_R, AT, BLOCK, BORDER, CIRCLE, CIRCLE_R,
* CLS, CSIZE, CURSOR, DIR, FILL, FLASH, FORMAT,
* INPUT, LINE, LINE_R, OVER, PAN, PAPER, POINT,
* POINT_R, PRINT, RECOL, SCALE, SCROLL, STRIP,
* UNDER, WIDTH, WINDOW.
*
* It also works with these TURBO TOOLKIT extensions:
*
* LIST_TASKS, CURSOR_ON, CURSOR_OFF, EDIT$, EDIT%,
* EDITF, SET_FONT.
*
* And these SPEEDSCREEN extensions:
*
* _FOUNT, _XSTEP, _YSTEP. 
*
* And these TONY TEBBY ones:
*
* CHAR_INC, CHAR_USE, CURSEN, CURDIS, DLIST,
* EXTRAS, JOBS, PRINT_USING, STAT, VIEW, WDIR, WSTAT
*
* Note: beware when using turtle or line graphics
* with USE. The co-ordinates in the channel you're
* re-directing are NOT updated while you USE it, so
* you must not assume their values when you come to
* USE that channel again. As long as you start with
* absolute co-ordinates (or angles) when returning
* to a channel you've previously USEd, all is well.
*
* USE       Clears graphics and turtle co-ordinates,
* USE #1    raises pen and re-sets #1 as the default. 
*
* Action:
*
* IF no parameter is specified, sets SuperBASICs
* channel #1 area to its start-up state: ID 1,1,
* CH.WIDTH 80, all other values zero.
*
* Otherwise, reads a channel index and loads the
* SuperBASIC details of that channel into slot #1;
* error if the channel is closed or undefined.
*
start      lea.l    define,a1
           move.w   $110,a2     BP.INIT vector
           jmp      (a2)
*
define     dc.w     1           One procedure
           dc.w     ch_use-*
           dc.b     3,'USE'     [ channel% ]
           ds.w     0
           dc.w     0,0,0       No functions
*
ch_use     cmpa.l   a3,a5       Any parameters?
           bne.s    read_ch	  Yes, work 'em out
chan_1	  move.l   $30(a6),a1  A1:=CHTAB offset
			  lea.l    40(a1),a1	  Set channel 1's ID
			  move.l   #65537,0(a1,a6.l)
			  moveq	  #4,d0		  Clear (4+1) longs
advance	  addq.l   #4,a1
			  clr.l	  0(a1,a6.l)
           dbra     d0,advance
           moveq    #80,d0		  POS:=0, WIDTH:=80
			  move.l   d0,4(a1,a6.l)
			  bra.s    no_error
*
* Read the channel number into D0
*
read_ch    lea.l    8(a3),a0   Predict end of table
			  cmpa.l	  a0,a5
			  bne.s	  bad_param  Allow one parameter
           move.w   $112,a2    Vector to get integers
           jsr      (a2)
           bne.s    bad_exit
           move.w   0(a1,a6.l),d0
			  cmpi.w   #1,d0		 USE #1 means reset
			  beq.s    chan_1
*
* Track down the data in the channel table
*
			  move.l		$30(a6),a1 A1:=CHTAB offset
			  moveq     #40,d1     D1:=CHTAB entry size
*
* Check channel number in D0 and convert to offset
*
chan_sel   mulu       d1,d0      Channel table size
           add.l      a1,d0      Add base offset
           cmp.l      $34(a6),d0 
           bge.s      what_chan  Past end of table?
           move.l     0(a6,d0.l),d5
           bmi.s      what_chan  Closed if negative
           add.l      a1,d1
*
* Now D1 is the offset of #1 and D0 offsets to #n%
*
			  moveq		 #9,d2		Copy (10-1) longs
copy_ch	  move.l	    0(a6,d0.l),0(a6,d1.l)
           addq.l     #4,d0
			  addq.l     #4,d1
           dbra       d2,copy_ch
*
* Error handlers
*
no_error	  moveq      #0,d0
           rts
what_chan  moveq    #-6,d0       CHANNEL NOT OPEN error
           rts
bad_param  moveq    #-15,d0      BAD PARAMETER error
bad_exit   rts
           end
