/* DEGAPLUS.C  VER.1.0 (AC45TST9Xb) Includes Test Code 11/89  */

/* CHANGES Log... */
/* C-42b3: w/newcode download routine                               */
/* C-43a1:'adeidflg' chg..removal of'npalwidar'adjustments (NOT yet)*/
/* C-43b0: TEST-TEMP Code Installation...                           */
/* C-43b1: Chges based on TEST-TEMP results..Klick                  */
/* C-44a0: First Debug of dwnload..WORK DE->Work PLUS only turned on*/
/* C-44a1: .......Dwnload code activated                            */
/*      (1) -1 loadflg=> -10; (2) Error in outerloop dwnload chged  */
/*      (1a) -1 chged at B200-1b2b, B200-2a2b                       */
/* C-44b0: Error... chged lsr.r ' D6,' to '#1,' in hdwnload routine */
/*    added bra code to hdwnload                                    */
/* C-44b2: 'lastpal' code adjusted for 1,1-9 not 0-9 sequence       */
/* C-44b3: now adda.l #32000-320,A3 in hdownload                    */
/* C-44B4: ERROR OF SLASH IN hdownload                              */
/*    added... (a1) w/(a0) pallete border #s, with #196 last tail   */
/* C-45a0: First working version was 44b4: this first WORKING halfcolor     */
/* C-45a1:Avoid rgbprint code in mouseroutine;add dead pal to A0,A1 downld  */
/* C-45a2:
     1. Version 1.02 detect and x/y packet addrs adjusted
     2. Now a NEW DEid chgd each initialization('thedeid')
    3. 'hcpypalno/hcpypalflg' routines copysingle subpal up to scrn
*/
/* C-45a5,6,7:
    1. Chg subpalno tst single pal move in hdownload.
    2. NEW.. DEBUGvertical,DEBUGhorizontal routines
    3. ROM12flg/rom1.0 determined in setup
    4. Activated download routine in EDITPlus->new workscrn w/mouseklik
    5. a7:"    ""    "   "    in B200,I.B.2.d; Work(DE) to Other

 C-45a8:
    1. DEBUG:'DE'Trap stored vector: found and saved/restored each hbl/mouse
       if on work screen....stops retuurn vector corruption
 C-45a9:
     1. DEBUG:'DE' Newcode routine installed if DE CODE FOUND; 45a8 removed
*/

/* 1B-9T9Lc PalNo chged;#15blackedonnew pals;#3=new bounds no,clr0flg */
/* 1B=toggleSAT#1081 */
/* 1C-#up11%eventcounter->2222 */
/* 1C-#280,266-7 xysw togglesmouseintrptshift */
/* 1D-#286,299->SR2700; halt ints| #136,209-216->$4A2 new biostore area */
/* 1D2,#340:chgfrm 6 to 200:avoid vbl int. */
/* 1E-#284,291,420->restart from"spurious int#24" */

#include <obdefs.h>
#include <gemdefs.h>
#include <osbind.h>


extern int     gl_apid;
int     menu_id;

int     contrl[12],intin[128],ptsin[128],intout[128],ptsout[128];

/* --------- DE find stuff ---------- */
char  no_DEGAS[]= "[1][  |DEGAS Elite is not resident!|  ][ Sorry ]";
char  yes_DEGAS[]="[1][  |  Installing DEGA+......|  ][ oooh,all those colors
]";
char  off_DEGAS[]="[1][  |    Removing DEGA+......|  ][ reset to reinstall ]";
char reset_DEGAS[]="[1][  |WARNING...not re-enterable|  ][ reset to reinstall
]";
char cpyrt_DEGAS[]="[1][  |Copyright 1989 by B.Duggan|  ][ Press Ret.! ]";


int  DE_id, mgin[8],mgout[8];
int  forever=1, slots, dum, event;
/*  int  screens;  OLD WAY  */
long  screens[15], *s_ptrs;      /* C-26:new array for DEscreen ptr storage */

int  startscrn;                  /* C-28:Present screen(mult*4=offset)  */
long start1scrn;                 /* C-28: Store addr from offsedt */

/* C-ac1dtstb */
int          installflg;
/* --------- DE find stuff -end declarations */

/* XXXXXXXXXXXXXXXXXX GLOBAL DECLARATIONS END XXXXXXXXXXXXXXXXXXXXXXXXXXXXX */



/* XXXXXXXXXXXXXXXXXXXXXXXXXX START STATIC DECL. XXXXXXXXXXXXXXXXXXXXXXXXXX */
install()
{
register long *tempstor=0;
static int  lastpal=9,lastcpy,copypalsw;     /* C-P8 */
static int  tpalwidth,palwidth=19,palwidno=10,back1grd;     /* C-R0a */

static long  lowvbvec=0x70,tpaladdr,base4,vbvec=0x070,hbvec=0x120;
static long  keyvec=0x118,traptable14=0;
static int   hold1,hold2,hold3,hold4,resolution;

static int  editsw,subpalno,chgclrsw,red,grn,blu,xysw,newclrsw;
static long     editscrn,workscrn,subpal0,subpal1,subpal2;

static int      clrsum,flipword,download,upload,newclr=2,degasmode,dirtypal;


/* *********************** New Code 23a   12/31/88 ************************ */

/* PARAMETERS: */
static int  numofpal=0;    /*  offset into array.... BYTE siZed             */
static int  prgmid=0xBDDC; /* C-41c(toC)14 bits,2 reserved for Upgrade ID's */
static int  presentid=0;   /* ...........??? see below                      */
static int  scrnid=0;           /* MUST be kept integer size */
static int  newscrnid=0;   /* C-07 replace presentid,like newscrnaddr       */

/*  FLAGS: */
static int  editcycle=0;     /* C-302:Prevents scrnbox at less than x cycle */
static int  dirtyflg=0;      /*  In mouse routine w/"wasdegasflg"           */
static int  floppyflg=0;
static int  flopwaitflg=0; /* In 2.a adjustable wait for DE prgm process aftr
flop */
static int  wasdegasflg=0; /* Set in mouseroutine if DEGASMODE set(PAL click)
*/
static int loadflg=0; /* Combines:'uploadflg'= + checked in VBL,reset in VBL*/
                      /* 'dwnloadflg= -      "    "   ,  "    " HBL  */
static int  didloadflg=0;   /* Stops rentrance of NEW#5,reloads systemaddr  */
static int  rgboff=0;
static int  scrnidflg=0;
static int  prgmidflg=0;
static int  dirtysw=0;

/* CHANGES: C-45a2+3+4 */
static int  hcpypalflg=0xfafa;
static int  hcpypalno= 0xfbfb;  /* C-45a2:single pal transfer->newPLUS scrn */

/* CHANGES: C-45a5,6 */
static int  rom12flg;
static int  hitoloflg;         /* C-45a6:Hi to lower singl pal move is made */
static long saveA2,saveA3;     /* C-45a6, Temp. save vectors */

/* CHANGES: C-45a8,9 DEBUG  */
static long workvector, vectoraddr, editvector, vectorcode;
static int  devectorflg;       /* Restore corrupted returnaddr in DE prg   */
static int  gotvectflg;        /* Set in mouse routine,after code plug in  */

/* NEW ADDRESS PAR.: */
static long presentscrn,oldscrn; /* C-28D0:In setup,prsnt=current being chged
from */

static long  work1scrn; /* =screens[1];AddrofFirstWorkScreen from DEGASquery
*/
static long  newscrnaddr;            /* Obtained from VidFlgAddress...    */
/* (long) int Xvidclrs  basearray pointerC-28see printout...600byte=16 pals */

/* *** C-10/11: New DE Addition to NewCode10/11 :  ****** ----------------- */
static long  detemp0=0x000faded;
static long  detemp1=0xfadea;
static long  cyclesum;                  /* TEMP...check no of bras made... */
static long  depaladdr;    /* Addr internal to DEprgm itself=Palstore:chges */

static long  thedeid=0xFFBDFFDE; /* Escreenid(now chges)lower tworasters+Palno
*/

/* C-28: All chged to 'LONG'  */
static long  vidloadflg;  /* Used in'vmovevidclrs'=+ upload; =- dwnload; =0
check */
static long  newdeflg;  /* =0 when NO array base store addr yet found     */
static long  adeidflg;  /* Used in 'vcheckids:'=found DEscreenid          */
static long  tempde2=0xfade;
static long  depalno;    /* From'0-15'= CURRENT palno, not necess. last used*/
static long  newdepalno; /* From'vcheckids'....new id from nextscrnaddr     */
static long  lastdepal;  /* The last used pal (0-15)recycles in 'vwriteDEid'*/

/* From ScreenBox Routine: */
static long  numofscrn;        /* DE_08:Gives offset into DEscreenptr array */
static long  scrnboxflg;       /* DE_08:Causes Download of Boxclicked Screen*/

/* TEST CODE... test43a0=ABCDEF00 start point...TEMP */
static long test43a0,test43a1,test43a2,test43a3, test43a4, test43a5,test43a6;
static long test43a7,test43a8,test43a9, test43aa, test43ab,test43ac,test43ad;
static long test43ae,test43af;

static long test43b0,test43b1,test43b2,test43b3;  /* C-43B0:TEST-TEMP */


/* XXXXXXXXXXXXXXXXXXXXXX END 23A/26XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX */

/* lowres */
static long     prtredaddr=27297, prtgrnaddr=27305,mousepack;
static long     prtbluaddr=27313, prtbegaddr=27328,prtpaladdr=30216;
static     int  redborder=0x8000,redshift=2,grnborder=0x4000,grnshift=1;
static     int  bluborder=0x2000,blushift=1,xsum,ysum,tst,rgb_update;

/* Original array sizes=290:  */
static int  palette0[290];
static int  palette1[596];    /* C-1D, Upper600bytes->$4A2 (BIOS store area)*/
static int  palette2[596];  /* C-24: Upper [600]->storage for 16'vidclrs'  */

/* Number & Screen Write LOOK UP Table Array:   */
static int noarray[] = {
     0x3c,0x66,0x6E,0x76,0x66,0x66,0x3c,0x0,
     0x18,0x38,0x18,0x18,0x18,0x18,0x7E,0x0,
     0x3c,0x66,0x06,0x0c,0x18,0x30,0x7E,0x0,
     0x7E,0x0c,0x18,0x0c,0x06,0x66,0x3c,0x0,
     0x0c,0x1c,0x3c,0x6c,0x7F,0x0c,0x0c,0x0,
     0x7E,0x60,0x7C,0x0c,0x06,0x66,0x3c,0x0,
     0x3c,0x60,0x60,0x7C,0x66,0x66,0x3c,0x0,
     0x7E,0x06,0x0c,0x18,0x30,0x30,0x30,0x0,
     0x3c,0x66,0x66,0x3C,0x66,0x66,0x3c,0x0,
     0x3c,0x66,0x66,0x3E,0x06,0x0c,0x38,0x0,
     0x63c,0xE66,0x66E,0x676,0x666,0x666,0x63C,0x0,
     0x618,0xE38,0x618,0x618,0x618,0x618,0x618,0x0,
     0x63c,0xE66,0x606,0x60c,0x618,0x630,0x67E,0x0,
     0x67E,0xE0c,0x618,0x60c,0x606,0x666,0x63C,0x0,
     0x60c,0xE1c,0x63c,0x66c,0x67F,0x60c,0x60c,0x0 };

static int  *paladdr0= &palette0[0],
               *paladdr1= &palette1[0],
               *paladdr2= &palette2[0];

static int     *numarray= &noarray[0];

static int  *hupal0= &palette0[177],
               *hupal1= &palette1[177],
               *hupal2= &palette2[177];     /* Store HUE color palettes */

static int  *satpal0= &palette0[193],
               *satpal1= &palette1[193],
               *satpal2= &palette2[193];     /* Store SAT palettes */

static int  *ourpal0=  &palette0[0];     /* C-41d, A new paladdr0 get!! */

static int  *nplwidar= &palette0[230];
static  int *biostore= &palette1[296];  /* C-1D, biostore area substitute */

static int *vidclrs= &palette2[296]; /* C-24:600bytes or32*16clr storedpals */
static long screenbase;             /* C-28:arraybase of DEscrnptrs IN setup*/

/* SAT/HUE C-Ts */
static int     satsw,huesw,oldclr,osubpalno,doithue;
static int     anewhue,priorclr,ulastclr,clr0flag;
     palette1[0]=  0x0777;
     palette1[16]=19;
     palette1[32]=38;
     palette1[48]=57;
     palette1[64]=76;
     palette1[80]=95;
     palette1[96]=114;
     palette1[112]=133;
     palette1[128]=152;
     palette1[144]=171;
     palette1[160]=196;
     palette1[176]= 0x0fa;          /* Bottom 'border' */

     palette0[0]=  0x0777;
     palette0[16]=19;
     palette0[32]=38;
     palette0[48]=57;
     palette0[64]=76;
     palette0[80]=95;
     palette0[96]=114;
     palette0[112]=133;
     palette0[128]=152;
     palette0[144]=171;
     palette0[160]=196;
     palette0[176]= 0x0fa;

/* palwidth array at palette0,230th: called 'nplwidar' */
     palette0[230]= 0x19;
     palette0[231]= 0xFF;     /* 1st= 1 pal */
     palette0[232]= 0x62;
     palette0[233]= 0x42;
     palette0[234]= 0x32;
     palette0[235]= 0x28;
     palette0[236]= 0x21;
     palette0[237]= 0x1C;
     palette0[238]= 0x19;
     palette0[239]= 0x16;
     palette0[240]= 0x13;     /* 10th =10 palettes */
/* -----------END DECLARATIONS -------------------------------------------- */


/* XXXXXXXXXXXXXXXXXXXXXXXXXXXX END STATIC DECL. XXXXXXXXXXXXXXXXXXXXXXXXXX */
/* XXXXXXXXXXXXXXXXXXXXXXX START OF ACTUAL ASS. PROGRAM XXXXXXXXXXXXXXXXXXX */

 asm {
     lea.l     setup,A0
     move.l  A0,-(A7)
     move.w     #38,-(A7)
     trap#14
     addq.l     #6,A7
     bra      thatsall

setup:
     move     #4,-(A7)
     trap     #14
     adda.l     #2,A7
     move     D0,resolution(A4)

     tst.b     D0
     beq          snotmedres
     move     #6,redshift(A4)     /* allshift 1 toomany */
     move     #5,grnshift(A4)
     move     #4,blushift(A4)
     move     #8,redborder(A4)
     move     #4,grnborder(A4)
     move     #2,bluborder(A4)

     move.l     #27301,prtredaddr(A4)
     move.l     #27305,prtgrnaddr(A4)
     move.l     #27309,prtbluaddr(A4)
     move.l     #27316,prtbegaddr(A4)
     move.l     #30200,prtpaladdr(A4)
snotmedres:

     move.l     biostore(A4),A0   /* C-1D,enlarge biostore area,pal1 array  */
     move.l     #0x93E,A1         /* "E"=longaddr possiblethere */
     move.l     0x4A2, A2
 biosmove:
     move     -(A1),-(A0)
     cmpa.l     A2,A1
     bhi          biosmove
     move.l     A0,0x4A2          /* save new store address=500byte room */

/* C-30 --start-------------- */
      move     #0x0,newdeflg(A4)
     move.l    s_ptrs(A4),A0      /* C-28:Has original ptr in DEprgm      */
     move.l    A0,screenbase(A4)   /* Make DE array now ours...           */
     move      startscrn(A4),D1    /* Int. quantity from DEinquiry        */
     lsl       #2,D1               /* Multx4=long word offset             */
     move.l    0(A0,D1.w),start1scrn(A4)   /* Get startscrn addr          */
     move.l  4(A0), work1scrn(A4)          /* C-41: added                 */

     move.l     ourpal0(A4),D0            /* C-41d: A paladdr0 get-around */
     move.l     D0, paladdr0(A4)          /* C-41d: ....                  */


 /* -END C-30---- */

     lea.l     put00main,A0               /* TEST-TEMP  ...... */
     move.l    nplwidar(A4),(A0)
     move.w     slots(A4),6(A0)
     move.l     s_ptrs(A4),12(A0)
     move.l  vidclrs(A4),18(A0)
     move.l  paladdr0(A4),24(A0)
     move.l     paladdr1(A4),30(A0)
     move.l     paladdr2(A4),36(A0)

     bra          test1exit
     bra          put00main              /* dummycode jump */
     nop
     nop
     nop
     nop
put00main:
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
     nop
test1exit:

     move.l #0x11111111,test43a1(A4)     /* ALL TEMP  Code here.... */
     move.l #0xABCDEF00,test43a0(A4)     /* STORES debug code from  */
     move.l #0x22222222,test43a2(A4)     /* running program here    */
     move.l #0x33333333,test43a3(A4)     /* Look at with Memfile20  */
     move.l #0x44444444,test43a4(A4)
     move.l #0x55555555,test43a5(A4)
     move.l #0x66666666,test43a6(A4)
     move.l #0x77777777,test43a7(A4)
     move.l #0x88888888,test43a8(A4)
     move.l #0x99999999,test43a9(A4)
     move.l #0xAAAAAAAA,test43aa(A4)
     move.l #0xBBBBBBBB,test43ab(A4)
     move.l #0xCCCCCCCC,test43ac(A4)
     move.l #0xDDDDDDDD,test43ad(A4)
     move.l #0xEEEEEEEE,test43ae(A4)
     move.l #0xFFFFFFFF,test43af(A4)



/* -----C-29---------------- */
move.l  0x044E,presentscrn(A4)     /* C-29: Get currentscreen start up      */
/* C-45a2/C-45a5: NEW ROM detect and adjust: */
clr     rom12flg(A4)          /* C-45a5: DEBUG rom switch; 0=oldroms        */
move     #0x0102,D0
cmp.w     0xfc0002,D0
bne          oldroms100
move     #1,rom12flg(A4)     /* C-45a5: DEBUG rom switch; 1=v.1.02 roms     */
lea.l     doittoit14,A0      /* Mouse14 packet addr                         */
      move.w     #0x2847,2(A0)
       lea.l     xvector,A0
      move.w     #0x2740,2(A0)
       lea.l     yvector,A0
      move.w     #0x2742,2(A0)

       lea.l     xvector2,A0               /* Hueroutine */
      move.w     #0x2740,2(A0)
       lea.l     yvector2,A0
      move.w     #0x2742,4(A0)           /* Note: 4 byte offset for addr  */

      lea.l     editfirstpass,A0     /* Yaddr in editfirstpass routine */
     move.l     #0x2742,2(A0)

 oldroms100:

/* C-45a2:New 'thedeid' at initialization...no longer same(FFBD,FFDE) */
     move.l     #0xFF00FF00,D0          /* solves loading"unknown" DE pal */
     or.b     0x04BC,D0
     swap     D0
     or.b     0x04BD,D0
     lea.l     thedeid(A4),A0
     move.l     D0,(A0)
/* ---END...C-45a2------- */

/* C-45a8,9 *****DEBUG FIND THE TRAP RETURN WORKVECTOR: **** */
     clr.l     workvector(A4)    /* Ensure=0...tested for in Hbl/mouse14  */
     lea.l     256(PC),A0
     move.w     #0x4e41,D0                     /* = 'trap 1' opcode       */
     move.w     #0x2f39,D1                    /* = 'move.l' opcode        */
     move.l     0x0436, D2                    /* 'memtop'                 */
     sub.l     #256,D2         /* End address of search for D0-1 seq.     */
     move.l     D2,A1

cloop:   /* Actual detect loop for above code sequence in DE prgm          */
       move     (A0)+,D2               /* loops 10 times then repeats      */
       cmp          D0,D2
       beq.s        dcheck

       move     (A0)+,D2
       cmp          D0,D2
       beq.s        dcheck

       move     (A0)+,D2
       cmp          D0,D2
       beq.s        dcheck

       move     (A0)+,D2
       cmp          D0,D2
       beq.s        dcheck

       move     (A0)+,D2
       cmp          D0,D2
       beq.s        dcheck

       move     (A0)+,D2
       cmp          D0,D2
       beq.s        dcheck

       move     (A0)+,D2
       cmp          D0,D2
       beq.s        dcheck

       move     (A0)+,D2
       cmp          D0,D2
       beq.s        dcheck

       move     (A0)+,D2
       cmp          D0,D2
       beq.s        dcheck

       move     (A0)+,D2
       cmp          D0,D2
       beq.s        dcheck

     cmpa.l     A0,A1
     bcc.s     cloop
     clr      devectorflg(A4)               /* NOT Found so clear flag..   */
     bra.s   donevloop

   dcheck:  /* Check for Second Op code */
    cmp          (A0),D1
    bne.s     cloop
    move.l     2(A0),D2          /* Address of DE Vector store area     */
    cmp.l     -6(A0),D2     /* If TRUE= found vector(DE version 1.0 only)   */
    bne.s     cloop         /* Else, continue search          */
    move.l     D2,vectoraddr(A4)   /* Save found DE vector store addr     */
    move.l     D2,A1
    move.l     (A1),editvector(A4)      /* Save editscrn return
vector...Needed??  */

/* C-45a9      ************ DEBUG... get first code addr          */
     move.l    A0,D2
     sub.l     #8,D2               /* to store JSR jump to NewCODE          */
     move.l    D2,vectorcode(A4)   /* Get DE Code address into 'vectorcode',*/
     lea.l     newcode0,A0         /* ... for use by startup subroutine     */
     move.l  0x44e,2(A0)        /* Get editscreen addr into NewCode routine */
     move.l    A0,0x90          /* Setup TRAP 4 jump address                */
     lea.l     nousecode,A0     /* Get DE Vector store addr into routine    */
     move.l  vectoraddr(A4),2(A0)
     lea.l     newcode4, A0     /* Gives TrapIV a detect Degasmode state    */
     move.l    A4,2(A0)
/* END C-45a9 **********     */

     move    #1,devectorflg(A4)    /* Set flag for hbl/mouse debug routines */
     bra          donevloop
/* ---------------------------- */

/* NewCode RoutineC-45a9 tests for'b'trap 1 call,if in work mode=subst.addr */
 newcode0:          /* ***TRAP 4 routine***  */
     cmpi.l     #0xabcdef00,0x44E     /* Is in work mode ?                  */
     beq        backtousr             /* NO: bypass                         */
  newcode4:          /* A4 base register put in init. */
     movea.l #0xabcdef04,A1
     tst       degasmode(A1)          /* If in Degasmode...bypass           */
     bne       backtousr
  newcode1:         /* YES...N.B. startup gets work vector in here */
     move.l    #0xabcdef01,(A0)  /* Replace RTS vector w/stored 'workvector'*/
backtousr:
     rte                         /* Back to USER Mode                       */

newcode:  /* Installed with JSR into DE by start-up routine else not used.. */
     cmpi     #0x0B,4(A7)               /* (ONE addr up):Is it Constat call?
*/
     bne          nousecode               /* NO: bypass
*/
     move.l  A7,A0                    /* Get USR stack address
*/
     trap     #4                         /* Calls itself in SUPERVISOR MODE
*/
nousecode:
     move.l     (A7)+,0xabcdef02     /* Put return vector in DE store addr
*/
 newcode3:
      jmp          0xabcdef03               /* Return to DE Code(put in on
startup)          */

/* END:NewCode RoutineC-45a9 .... */

 donevloop:
/* *****END C-45a8,9 DEBUG: ***************************                */

     lea.l   flipw,A1
     lea.l   flipword(A4),A0
     move.l  A0,2(A1)
     lea.l   flipreset,A1
     move.l  A0,2(A1)
     move.w  #0x0,flipword(A4)

     move.l  A4,base4(A4)
     lea.l   basereg4,A1
     move.l  A4,2(A1)

     lea.l     regbas15,A1
     move.l     A4,2(A1)

     lea.l     regbas16,A1          /* C */
     move.l     A4,2(A1)

     lea.l   palad0,A1
     lea.l     paladdr0(A4),A0
     move.l  A0,2(A1)

     lea.l   palad1,A1
     lea.l     paladdr1(A4),A0
     move.l  A0, 2(A1)

     lea.l   tpal1,A1
     lea.l     tpaladdr(A4),A0
     move.l  A0,2(A1)

     lea.l   tpal2,A1
     move.l  A0,2(A1)

     lea.l   tpal3,A1
     move.l  A0,2(A1)

     lea.l      editwind,A0
     move.l  0x44e,2(A0)
     move.l  0x44e,editscrn(A4)

     move     #34,-(A7)
     trap     #14
     addq.l     #2,A7
     move.l  D0,A0
     lea.l     here14,A1
     move.l     16(A0),2(A1)
     lea.l     doittoit14,A1
     move.l     A1,16(A0)

     move.b      0xfffa07,hold1(A4)
     move.b      0xfffa09,hold2(A4)
     move.b      0xfffa13,hold3(A4)
     move.l  0x0120,  hold4(A4)

     move.l     keyvec(A4),A0
     lea.l  keysave,A1
     move.l  (A0),2(A1)
     lea.l  vbsave,A1
     move.l  0x070,(A1)

     lea.l      mykey, A0
     move.l     A0,0x0118
     lea.l     xysw(A4),A1      /* 1-c */
     move.l  A1,2(A0)

     lea.l      int24,A0     /* C-1E spuriousinterrupt#24 */
     move.l  A0,0x060

     andi.b     #0xfe,0xfffa07
     lea.l      hblank,A0
     move.l  A0,0x120
     ori.b     #0x01,0xfffa07
     ori.b     #0x01,0xfffa13
     lea.l      vblank,A0
     move.l  A0,0x70
     rts

int24: adda.l     #6,A7     /* C-1E strips SR,oldaddr spurious int redo */
     bra          vblfrmint24          /* restarts at vbl after   */

mykey:
     tst          0x12345          /* 1C-showxy switch  */
     bne          knochange

     cmpi.b #6,0xfffa21     /* C frm10 */
     bls          kchange
     move #0x2700,SR          /* 1D-halt int */
  bra     knochange  /* TEMPadd */
     cmpi.b #0x88,0x0E42
     bcs          knochange
     cmpi.b #70,0xfffa23
     bhi          knochange
     kchange:
     move #0x2500,SR
     knochange:     nop
     keysave:  jmp  0x12345

/* ********************************* HBLANK Code: ************ */

hblank:
     move #0x2700,SR                              /* C-1D halt int */
     movem.l D0-D7/A0-A6,-(A7)               /* C-41: Make all A6 */

  regbas16: move.l     #0x123456,A4          /* C-43a1: For hdownloadsw... */
       move     loadflg(A4),D0                    /* C-44a1 from 'didloadflg'
*/
     cmpi     #-10,D0                              /* C-44a1  */
       beq          hdownload                         /* C-44a1  */

  tpal1: move.l  0xfaded,A3     /*  LINE# Addr */
     movem.l (A3)+,D3-D7/A0-A2

 hbpwidth:     move.w  #0x0002,D0               /* C-41b: From 0004 */


 hbdelay:
     nop
     dbf     D0,hbdelay
     clr     D0
     move.b     1(A3),D0
     sub.b     -31(A3),D0
     clr          D2
     move.b     33(A3),D2
     sub.b     1(A3),D2
     bra     tpal2                    /* ?? why? */

 tpal2:  move.l  A3,0xfaded
     and.l     #0x0777,D3
 backgrdclr: or.l  #0x12345,D3
     movem.l D3-D7/A0-A2,0xff8240

/*  regbas17: move.l     #0x12345,A4    C-41:moved up ahead....as Regbas16a */
     move.b     -31(A3),D1
     cmpi.b     #196,D1
     bcs          hbnotend
     move     #200,D0                /* C-1D2,chgfrm 6 to 200:avoid vbl int.
*/
     bra          hbfrmbottom
 hbnotend:
     cmp          tpalwidth(A4),D0
     bne          hbchanged
     beq          hbexit
 hbchanged:
     subq     #1,D0
     move     D0,tpalwidth(A4)
 hbfrmbottom:
     clr.b     0xfffa1b
     movea.l     #0xfffa21,A2
  hbverify:
     move.b  D0,(A2)
     cmp.b     (A2),D0
     bne    hbverify
     move.b #0x08,0xfffa1b

hbexit:
/* *****DEBUG: HORIZONTAL Routines: C-45a5 all **** */
     tst          rom12flg(A4)
     bne          hdebug12
     move.l     #0x00FCA4A0,0x6c82          /* 'RTS'Rom vector,sometimes
Corrupts      */
     bra      endhdebug
  hdebug12:
     bra          endhdebug          /* TEST-TEMP:KEEP until code found in ROM
v.1.2 */
     move.l     #0x00000000,0x0000
endhdebug:
/* *****END: DEBUGHORIZ ROUTINES ******** */

     bclr   #0,0xfffa0f
     movem.l (A7)+,D0-D7/A0-A6          /* C-41 now to A6 */
     rte
/* ******END HBL******************************************************** */



/* *********************************************************************** */
/* ************************* START NEWCODE ******************************* */
/*          1 of 20               New Code 23a      12/31/88               */
/* N.B. (Effects on other routines):
  1. HBL Routine: Checks for DEGASMODE,exits if dwnload(loadflg='-',
                  then updates 'oldwrkscreen'

  2. Mouse  "  ": Sets dirtyflg; but first checks for DEGASMODE, if set
                  clears DEGASMODE,sets WASDEGASFLG and clears DIRTYFLG.   */

/* ***************** New Code Starts Here ********************************* */
/* Exit and Entrance Jumps used in New Code:NEEDED HERE UNTIL IMPLEMENTED */
     bra     tonewcode     /* C-28's */
/* vstartplus: */
     nop
     nop
/*     bra     hdownload C-41 */
/*     bra     hupload  C-41  */
     nop
     bra     vsavevidclrs
/* ------------- END JUMP TO AND FROM INTO CALLS ---------------- */

/* 0.0 On Startup....(C-28:The DEpalAddr save area got in Setup):  once only!
*/
tonewcode:
  tst     newdeflg(A4)        /* Check if addr already gotten               */
  bne     vgotdepaladdr       /* This gets vid pal addr in DEprgm upon      */

/* 0.01 *****DEBUG TRAP RETURN WORKVECTOR: C-45a9 **** */
    bra          hnovectors   /* ***************** TEST-TEMP ********* */

    tst          devectorflg(A4)
    beq          hnovectors      /* Not found...so dont install NewCode     */
    move.l     workvector(A4),D0  /* Already got addr on start up?          */
    bne          hnovectors      /* Yup, we got it already,so bypass        */

    movea.l editscrn(A4),A0               /* In Work Mode?
*/
    cmpa.l  0x044E,A0
    beq           hnovectors                    /* Need work mode: Bypass
*/

    movea.l vectoraddr(A4),A0          /* DE temp store address
*/
    move.l     (A0),D0
    move.l     D0,workvector(A4)          /* Got Work vector...
*/
     lea.l     newcode1,A0
     move.l     D0,2(A0)                    /* Store Work vector in NewCode
Routine */
     lea.l     newcode,A0                    /* Get NewCode start address
*/
     movea.l vectorcode(A4),A1          /* Get DE code startaddress
*/
     move     #0x4EF9,(A1)+               /* Put JMP op code     in DE program
code     */
     move.l     A0,(A1)+                    /* Store NewCode strt. addr in DE
Code  */
     lea.l     newcode3,A0                    /* Get NewCode return point
*/
     move.l     A1,2(A0)                    /* Store DE code return address
*/

 hnovectors:
     /* *****END:DEBUG TRAP C-45a9 ****    */

  /* 0.02 Continue Startup routine: */
  tst     0x045A                    /*  TESTY1 */
  beq       vblout                       /*  TESTY1 */
  movea.l editscrn(A4),A0          /*  TESTY4: NEEDED A0 for firstpass */
  cmpa.l  0x044E,A0                    /*  TESTY4: INSURE in editmode
*/
  bne          vblout                    /*    "  ": ..NO...so waitfor it
*/
  move.l  0x045A,depaladdr(A4)     /*  TESTY1: And save in 'DEpaladdr' */

  bra       Anewscrnbx             /* C-28A To ScreenBox to Get Paras.& Return
*/
backfrmbox:
  move.l  start1scrn(A4),A0   /* 1st pass startup active scrn
*/
  move    #1,newdeflg(A4)     /* Turn off this subroutine                   */
  bra     vfrmstart           /* C-26: Bra to flop @ 2.a
*/
vgotdepaladdr:                /* End: Start Up                              */

  bra          vavoid     /* C-300:TEMP BYPASS CHANGE XXXXXXXXXX  ???  */

/* 1. Is the Floppy Active? 'flock'==1 ************************************ */
  tst     0x043E
  bne     vfloppyon         /* ON:Test if Degasave prgm in use:bra to  1.a  */

/* 2. OFF: But is 'floppyflag' ALREADY set? ******************************* */
  tst     floppyflg(A4)
  bne     vwasfloppy        /* YES: Bra to 2.a WAS FLOPPY)                  */

/* 3. NO, then clear 'floppyflag' and ready for checks ******************** */
vnofloppy:                         /* From: 'vyesprogram' end of 2.a        */
  clr     floppyflg(A4)


/* 4. Check 'wasdegasflg': ************************************************ */
/* Set w/dirtyflg at check for DEGASMODE in Mouseroutine by clicked RGBbxes */
vavoid:          /* C-41b */
  tst     wasdegasflg(A4)
  bne     vyeswasdegas        /* YES: branch to subroutine 4.a .....        */

/* 4.1 Branch to 'Alt. Screen Box'Routine: *************************
*/

     movea.l editscrn(A4),A0          /* Get editscrn addr
*/
     cmpa.l     0x44E,A0
     beq     screenAbox           /* Only if in Editscrn Mode
*/
     clr          editcycle(A4)          /* C-302:Clearsin workmode=prevents
scrnbox */
vfrmAscrnbx:                         /*  C-27.... and returnsfrm Alt.ScrnBox
*/
    bra     vischgtowrk5        /*  Continue on to major routine.(#5)
*/


/* ************************************************************************ */
/* ********************** SubRoutines 1.a-4.a ***************************** */

/* 1.a IS NEW SCREEN BEING DWNLOADED/UPLOADED ? ******** */
vfloppyon:              /* ITS ON:Test Degasave prgm use while in workmode  */
  move.l  0x044E,D4           /* Get logical screen address being used now  */
  cmp.l   editscrn(A4),D4     /* Is present screen same as Edit screen?     */
  bne     vnoteditmode        /* NO:...Degasave is being used so...bra      */
  move    #1,floppyflg(A4)    /* YES:Set 'floppyflag'                       */
  move.b  #0x0, 0xFFFa1b      /* Turn off HBL timer                         */
  bra     vblfrmint24         /* (EXIT) VBL exit avoids VBL, resets timers  */

  vnoteditmode:               /* Degasave is in Use:                        */
  clr     floppyflg(A4)       /* Then....ignore vbl,and turn off hbl        */
  move.b  #0x0,0xFFFa1b       /* Turn off HBL timer                         */
  bra     vblfrmint24         /* (EXIT)   avoids VBL and resets timers      */

/* 2.a But HAS 'floppyflag' ALREADY been set? *********** */
vwasfloppy:            /* Need a wait period for DE activity....  */
  btst    #2,flopwaitflg(A4)  /* =4 cycles of VBL wait until rest is done   */
  bge     vwas1flopon         /* ...lets DE do internal process= adjustable */
  move    #1,floppyflg(A4)    /* ensure on                                  */
  addq    #1,flopwaitflg(A4)  /* ....adjust to fit!                         */
  move.b  #0x0,0xFFFa1b      /* Off the HBL timer                          */
  bra     vblfrmint24         /*   (EXIT) Until wait is over                */


     /*  After Floppy Activity+ Wait period (4+screen refreshes):           */
vwas1flopon:
  clr     flopwaitflg(A4)     /* Reset flg for next Floppy.....             */
  clr     dirtyflg(A4)       /* C-03 Prevent prior activity stopping DEmode */
  clr     dirtysw(A4)         /* (These two stick together)....RESET        */
  movea.l work1scrn(A4),A0    /* C-06....needs screen to check in A0        */
vfrmstart:                /* C-26, From startup code,A0='startscrn' */
  clr     floppyflg(A4)
  bsr     vcheckids           /* YES: So Check screen1 for 'prgmid'         */
  tst     prgmidflg(A4)
  bne     vyesprogram         /* Its a PLUS screen......                    */

  move    #1,degasmode(A4)    /* NO: Reset to ---DEGASMODE---               */
  move    #1,rgboff(A4)       /*     Turn off HUE+Colors+CPY                */



/* ...Floppy DEscrn1 adjust: A0=scrnaddr, D3=screen depalno:Frm'vcheckids' */
  tst     adeidflg(A4)                  /* Is it already a DE screen ?      */
  beq     vunknownde                   /* NO....Make it one then, branch   */

  cmp     depalno(A4),D3               /* YESDE: But are DEid's the same?  */
  bne     vunknownde                   /* NOT old scrn,also dwnload newpal */

  movea.l vidclrs(A4),A1               /* Maybe yet,chk more:Get arraybase */
  movea.l depaladdr(A4),A2             /* Get DElite palette addr          */
  lsl     #5,D3                        /* Offset from array base'0'(32*No) */
  move.l  (A2)+,D0                     /* Get Colors 0-1                   */
  cmp.l   0(A1,D3.w),D0                /* The Same?                        */
  bne     vunknownde                   /* NO:Then Download Newly Lded Clrs */

/* --------- Old Screen= a Floppy Save ------------- */
  move.l  (A2),D1                      /* Get Colors 2-3                   */
  cmp.l   4(A1,D3.w),D1                /* The Same?tst0-3 max(medres max)  */
  bne     vunknownde                   /* NO:Then Download Newly Lded Clrs */
  move.b  #0x0,0xFFFa1b                /* Was floppy SAVE! off HBL Timer.. */
  bra     vblfrmint24         /* (EXIT):  avoids VBL and resets timers     */

/* --------- New DE Screen= Floppy Load (1)--------- */
vunknownde:     /* Write/assign thedeid,palno; A0=work1scrn,=a floppy LOAD
*/
  bsr     vwritedeid                   /* Assign id and palno's            */
  move    #-1,vidloadflg(A4)           /* C-13: ensures dwnload frm DEprgm */
  bsr     vmovevidclrs                 /* Download vidcolors               */
  move.b  #0x0,0xFFFa1b                /* This exit turns off HBL timer    */
  bra     vblfrmint24         /* (EXIT): avoids VBL and resets timers      */

/* --------- New PLUS Screen=Floppy Load (2)-------- */
 vyesprogram:                 /* *YES*: A Plus picture is there..scrn1     */
  move    D0,scrnid(A4)       /* D0=new 'scrnid' from rts: store new id    */
  move    #-1,loadflg(A4)     /* Set download palettes flag for hbl        */

  move.l  A0,newscrnaddr(A4)  /* C-26 to END:Prepare for dwnload pals      */
  move       #1,scrnboxflg(A4)       /* Prevent scrn addr upload to system
*/
  clr       didloadflg(A4)       /*  "   "  same
*/
  clr       degasmode(A4)            /* Now in Plusmode
*/
  clr       rgboff(A4)            /* Startup needs this
*/
/* >>>>>>> Set HBL for 2-3 rasters then exit/ (only turns on HBL) <<<<<<<  */
  moveq   #2,D0               /* C-28:Get No. of rasters into D0
*/
  bra     vstartplus          /* Continue on .......bra to VBL routine     */
/* *************** END: Subroutine Floppy Upload/Dwnload ***************** */




/* ************************************************************************ */
/*  Subroutine 4.a CHANGING DEGAS TO PLUSMODE (while on Editscreen)******** */
/* ************************************************************************ */
vyeswasdegas:          /*  YES:It was 'wasdegasflg',start change: */
  movea.l depaladdr(A4),A3             /* Copy videocolors to palette0      */
  movem.l (A3)+,D0-D7
  move.l  paladdr0(A4),A0              /* Get subpal0 addresses(0-2)        */
  move.l  paladdr1(A4),A1
  move.l  paladdr2(A4),A2
  movem.l D0-D7,(A0)                   /* Move them into pals0 and 1        */
  movem.l D0-D7,(A1)
  lsl.l   #1,D0                        /* Here Pal2= 2x other palettes      */
  move.l  D0,(A2)+                 /* Now Shift all by '1',i.e. double 'em: */
  moveq   #1,D0              /* Register shift number                       */
  lsl.l   D0,D1              /* Copy sums to subpal0,pal2                   */
  lsl.l   D0,D2              /* add.l to itself saves no cycles here        */
  lsl.l   D0,D3
  lsl.l   D0,D4
  lsl.l   D0,D5
  lsl.l   D0,D6
  lsl.l   D0,D7
  movem.l D1-D7,(A2)         /* double/sum is in Palette2                   */

  move       #196,D0                /* C-41d:Move 196 to start of Subpal1's */
  move.w  D0, 32(A0)
  move.w  D0, 32(A1)
  move.w  D0, 28(A2)           /*             4 less due to postincrement */
  move       #0xFA,D0                /* Move $FA to start of Subpal2's */
  move.w  D0, 64(A0)
  move.w  D0, 64(A1)
  move.w  D0, 60(A2)           /* C-41dEnd: 4 less due to postincrement */

/* ???????????         Background color to Subpal1?                         */
/* ---------------- SET PARAMETERS: (To return to Plusmode) --------------- */
/* ........ with ONE fullscrn pallette,                                     */
  clr     rgboff(A4)          /* Back to normal mode(onepal only)           */
  move    #1,lastpal(A4)      /* C-44b2:Set LastPal index to '1':(1,1to9)   */
  clr     subpalno(A4)        /* Set'0'(present)'subpalno'+(old)'osubpalno' */
  clr     osubpalno(A4)

  move.w  0x04BC,scrnid(A4)       /* C-41: New id from lo word of system clock
*/
  clr       dirtysw(A4)            /* C-41: reset parameters
*/
  clr       dirtyflg(A4)            /* C-41:   ditto
*/

  move    #1,palwidno(A4)     /* Set present PalWidthNo index to '1':(1-10) */
  move    #196,palwidth(A4)   /* Reset Palwidth to FullScreen,i.e. 196(max) */
  clr     wasdegasflg(A4)     /* Now out of DEGASMODE into Plusmode         */
  clr     degasmode(A4)       /* Clear flag ?;
*/

  bra       hupload1                   /* C-41b: Direct bypass to upload to
workscrn     */
/* ***********************END SUBROUTINES 1.a-4.a************************** */
/* ************************************************************************ */



/* **********************************************************************   */
/* ****** (5) CHANGE to Screen on NEXT Vert. Blank? OR Exit? ************   */
/* **********************************************************************   */
/* PARAMETERS: */
/* int   prgmid, presentid, scrnid; */

/* FLAGS:      */
/* int   dirtyflg, dirtysw, floppyflg, wasdegasflg; */
/* int   loadflg;                 * Now  an upload=+, a dwnload=-        */
/* int   didloadflg;              * Stops rentrance of #5,reloads systemaddr
*/

/* VARIABLES:  */
/* long  presentscrn, oldscrn:  *C-29:new chge detect... */
/* long  workscreen, editscrn; */
/* long  newscrnaddr;              * Stores new vid screen addr frm 0x045E  */
/* ------------------------------------------------------------------------ */

vischgtowrk5:
  move.l  0x044E,D0                /* C-29:(DEL#)Load 'logical'system variable
*/
  cmp.l     presentscrn(A4),D0       /* C-29:Chge to Edit or NEW Work scrn?
*/
  bne     vyeschgscrn                    /* C-29:YES....so branch */


/* *************** These are all SAME Screen EXITS ************************ */

/* 5.a  Is Screen Box Clicked On?  */
  tst     scrnboxflg(A4)      /* DE-08:Screen Box clicked=chg to scrn? DEAD */
/* C-42b1:following.changed from  'beq' to 'bra', forces continuous bypass. */
  bra     vaintnone           /* No change pending....bra to none      DEAD */
  tst     didloadflg(A4)      /* Is secondgoaround with set scrnboxflg?DEAD */
  bne     vloadedlastpass     /* YUP: So exit now                      DEAD */
  move.l  newscrnaddr(A4),D0  /* Change: So move addr to D0            DEAD */
  bra     v1yeschgscrn        /*    ....and branch, for 1st run throughDEAD */

/* 5.b  NO: But chge Done on LAST Pass? */
vaintnone:
  tst     didloadflg(A4)      /* Test 'didloadflg', done last pass?         */
  bne     vloadedlastpass
  tst     degasmode(A4)       /* YES: But Now In Degasmode?                 */
  beq     vblroutine          /* NO->  THE STANDARD PLUS DIRECT PASS THRU   */
  move.b  #0x0,0xFFFa1b       /*          (offs HBL timer)                  */
  bra     vblfrmint24         /* ->->  THE STANDARD DEGAS DIRECT PASS THRU  */

/* 5.c  WAS Changed so Reset:          */
vloadedlastpass:              /* Yes*:Reset 'didloadflg',that caused it     */
  clr     didloadflg(A4)

  tst     scrnboxflg(A4)      /* DE_08:Chged because of Screen Box Klick..  */
  bne     vnonewaddr          /* So dont load sys with new addr...bra       */
  move.l  newscrnaddr(A4),0x045E        /* Restore new 'videoaddr'          */
 vnonewaddr:    /* Bra also frm 'block buffer detect'at 6.0  */
  clr     scrnboxflg(A4)      /* Reset                                      */
  tst     degasmode(A4)       /* In Degasmode?                              */
  beq     vblroutine          /* NO: BRA to VBL and normal process          */
  move.b  #0x0,0xFFFa1b       /* YES:This exit turns off HBL timer          */
  bra     vblfrmint24         /* also avoids VBL and resets timers (EXIT)   */
/* ****************** END: MISC EXITS ************************************* */
/* ************************************************************************ */



/* ************************************************************************ */
/* ********** 6. Change Pending: But From/To WHAT?..(2 paths) ************* */
/* ************************************************************************ */
 vyeschgscrn:                 /* **YES: Screen change next VBL! wow         */
  clr     scrnboxflg(A4)      /* A cleanup precaution                       */
  cmp.l   newscrnaddr(A4),D0
/*  beq     vloadedlastpass TEMP.C-42b1 Catches mkliks while active scrnboxflg
*/
  movea.l screenbase(A4),A1   /* (C-28chged->scrnbase)Get pointer arraybase */
  cmp.l   12(A1),D0           /* If move to block scrn buffer bypass        */
  beq     vnonewaddr
  cmp.l   16(A1),D0           /* If move to block mask buffer bypass        */
  beq     vnonewaddr          /* Check latter for removal of this BRA ????? */
v1yeschgscrn:                 /* ENDC-23: screenbox+klick to new scrn mixup */

  clr     didloadflg(A4)      /* Clear 'didloadflg' and continue...         */
  cmp.l   editscrn(A4),D0     /* Compare 'editscrn' with 'newscrnaddr'in D0 */
  bne     vtruechange         /* Change to WORKSCREEN....bra                */

  move.l  D0,presentscrn(A4)  /* C-29: Store "accepted" editscreen
*/
  tst     degasmode(A4)       /* Change to EDITSCREEN....but In Degasmode?  */
/* ******************************************************************* */
/* 1.EDITSCREEN:  TO Editmode:............ BRA Exit to normal VBL      */
/* ******************************************************************* */
  beq     vblroutine          /* PLUSMODE  Exit                        */
  move.b  #0x0,0xFFFa1b       /* DEGASMODE Exit (turns off HBL timer)  */
  bra     vblfrmint24         /*         resets timers                 */
/* ************************************************************************ */
/* 2.WORKSCREEN:   TO Workmode next VBL:.....a true chge, continue on       */
/* ************************************************************************ */
/*   Either:  I. Workscrn to Wrkscrn, or  II.Editscrn to Wrkscrn            */

vtruechange:           /* HUE Bypass Routine:/ also bra here from SCREENBOX */
  tst     huesw(A4)           /* Is HUE/SAT Mode?:                          */
  beq     vnothuemode         /*    NO: branch....                          */
        /*  YES: Do nothing,allows switchingto aother screensw while in HUE
*/
        /*       mode to see colors or otherimages w/o chge to other PALS:  */
  clr     didloadflg(A4)      /* Ensure no leftoverflg....                  */
  clr     dirtyflg(A4)        /* Ensure no upload of palettes               */
  clr     dirtysw(A4)         /*  See screenbox routine                     */
  bra     vblroutine          /* End HUE Bypass: BRA to VBL Standard EXIT   */
/* ---------- */

vnothuemode:   /* Bypass avoided.....ACCEPT new 'newscrnaddr' */
  move.l  D0,newscrnaddr(A4)  /*  Store new 'logicalscrn' in 'newscrnaddr'  */
  move.l  presentscrn(A4),0x045E /* C-29"CLEAR"newlyloaded Screen Back->Prsnt
*/
  move.l  D0,A0               /* Get screen ids,                            */
  bsr     vcheckids

/* C-43B0:----------------- TEST-TEMP CODE INItialization ----- */
  move.l     #0xB1000000,test43b1(A4)          /* Clear all C-43b0 */
  move.l     #0xB2000000,test43b2(A4)
  move.l     #0xB3000000,test43b3(A4)
/* --------------- */

/* TYPE I:  WORK TO WORK?...........( 6 options).... */
  move.l     #0xB1000010,test43b1(A4)          /* C-43b0: TEST-TEMP */

  move.l  presentscrn(A4),D4  /* C-29:Get logical scrnaddress being used now
*/
  cmp.l   editscrn(A4),D4     /* Is present screen same as Edit screen?     */
  bne     vworktowork         /* NO: So branch to 'Type I-WORK TO WORK'     */

/*  clr       hcpypalflg(A4)   * C-45a2:TEMP REMOVAL Prevent/clear plus copypal
*/

/* TYPE IIa:   EDIT(Plus) TO WORK?...(3 options).... */
  move.l     #0xB10002A0,test43b1(A4)          /* C-43b0: TEST-TEMP ****** */
  tst     degasmode(A4)       /* SAME=Edit scrn: But are we in Plusmode?    */
  beq     veditowork          /* YES:..bra, handle as normal Plus           */
  clr     editsw(A4)          /* C-23: All from Edit, so clean its flag     */


/* TYPE IIb.1: EDIT(DEgas) TO (NonDE) WORK?......... */
  move.l     #0xB10002B1,test43b1(A4)          /* C-43b0: TEST-TEMP ****** */
  tst     adeidflg(A4)         /* Is new screen now a DE screen?
*/
  beq     vnotdenext          /* YES: Next either PLUS orVirgin             */


/* TYPE IIb.2: EDIT(DEgas) TO (Diff)DEGAS WORK?..... */
  move.l     #0xB10002B2,test43b1(A4)          /* C-43b0: TEST-TEMP ****** */
  cmp     depalno(A4),D3      /* Next is DE, but is it different screen?    */
  bne     vdetode             /* YES: Bra...change to new DE screen         */


/* TYPE IIb.3: EDIT(DEgas) TO (Same)DEGAS WORK?..... */
  move.l     #0xB1002B30,test43b1(A4)          /* C-43b0: TEST-TEMP ****** */
  move    #-1,vidloadflg(A4)  /* SAME Screen:UPDATE stored palette(finally) */
  bsr     vmovevidclrs        /* Dwnloads present pal from DE into storepal */
  move    #1,rgboff(A4)       /* Insure turn off                            */

  tst     scrnboxflg(A4)               /* C-28C:From Ascreenbox Routine?
*/
  bne     v2b3yes
  move.l     #0xB1002B3A,test43b1(A4)          /* C-43b0: TEST-TEMP ***** */
  move.l  newscrnaddr(A4),D0               /* C-29: */
  move.l     D0,presentscrn(A4)               /* C-29: Update stored current
scrnaddr          */
v2b3yes:
  move.l  presentscrn(A4),0x044E  /* C-29: Restore log. sys. video update   */
  move.l     #0x0,0x045E               /* C-29: Stop imposed CLEAR from
occurring     */

  clr     scrnboxflg(A4)               /* C-28END:Reset to normal null
*/
  move     #1,degasmode(A4)          /* C-28C: Ensure DEmode ON
*/

  move.b  #0x0,0xFFFa1b       /* Turn off HBL timer                         */
  bra     vblfrmint24         /*      (EXIT) Avoids VBL and resets timer    */

/* ************************************************************************ */
/* ******** Type I.  WORKSCREEN to WORKSCREEN  - (A0=newscrnaddr)---------- */
/* ************************************************************************ */
/* 6 permutations: A. PLUS to +,   PLUS to DEGAS,  PLUS to  VIRGIN          */
/*                 B. DEGAS to + , DEGAS to DEGAS, DEGAS to VIRGIN          */
/* ************************************************************************ */
vworktowork:
  tst     degasmode(A4)                /* DEGASmode or PLUSmode?            */
  bne     vdegastowhat                 /* Bra to'I.B. WORK(DEgas) to ???'   */

/* ***************************************************** */
/* *************** I.A  WORK(Plus) to ??? ************** */
/* ***************************************************** */
  tst       prgmidflg(A4)                 /* PLUS to PLUS?
*/
  bne     vptoplus
vfrmeditplus:                           /* (A bra from 'EDIT to PLUS')      */
  clr     editsw(A4)                    /* Takes care of branch to here     */
  tst     adeidflg(A4)                  /* PLUS to DEGAS?                   */
  bne     vptode
  bra     vpdeother                     /* PLUS to VIRGIN                   */

/* I.A.1  WORK(Plus) TO DEGAS: (DE screen is next)............... */
vptode:
  move.l     #0xB2001A10,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  move    newdepalno(A4),D0            /*  */
  move    D0,depalno(A4)               /* Store new palno                   */
  move    #1,vidloadflg(A4)            /* Upload new pal to DE progam pal   */
  bsr     vmovevidclrs                 /* store area                        */
  move.l  depaladdr(A4),0x045A         /* Set systemptr toload vidregisters */

  tst     scrnboxflg(A4)     /* XXXXXXXXX  C-43b1:From Ascreenbox Routine? XXXX
*/
  bne     v1a1yes                              /* ......bra dont chge screen..
*/
  move.l     #0xB2001A1a,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  move.l  newscrnaddr(A4),D0            /* REset video stored scrn
*/
  move.l     D0,presentscrn(A4)          /* To new value...
*/
v1a1yes:
  move.l     presentscrn(A4),0x044E     /* Restore present scrn
*/
  clr.l     0x045E                           /*  Abort forced Chg back to
present */
  clr     scrnboxflg(A4)          /* XXXXXXXXXX C-43b1END:Reset to normal null
*/

  move    #1,rgboff(A4)                /* Ensure its off                    */
  move    #1,degasmode(A4)             /*  " "   Degasmode flg set          */
  move.b  #0x0,0xFFFa1b                /*  (EXIT) Turns off HBL timer       */
  bra     vblfrmint24                  /* also avoids VBL and resets timers */

/* I.A.2  WORK(Plus) TO PLUS:............................  */
vptoplus:
  move.l     #0xB2001A20,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  clr     degasmode(A4)                /* YES:Equals a PLUS Screen          */
  clr     rgboff(A4)                   /*                                   */

  tst     scrnboxflg(A4)                    /* C-43b1:From Ascreenbox Routine?
*/
  bne     v1a2yes                              /* ......bra dont chge screen..
*/
  move.l     #0xB2001A2b,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  move.l  newscrnaddr(A4),D0            /* REset video stored scrn
*/
  move.l     D0,presentscrn(A4)          /* To new value...
*/
v1a2yes:
  move.l     presentscrn(A4),0x044E     /* Restore present scrn
*/
  clr.l       0x045E                           /*  Abort forced Chg back to
present */
  clr       scrnboxflg(A4)                    /* C-43b1END:Reset to normal null
*/

  tst     scrnidflg(A4)                /* Is it the same screen?            */
  beq     vpdiffscrnid                 /* NO: Bra,its a new screen....      */
  bra     vblroutine                   /* BRA to VBL Standard process       */

 vpdiffscrnid:    /* .....Which requires a dwnload     */
  move.l     #0xB2001A2A,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
/*  bra     vblroutine               * ***** C-44a1: TEST-TEMP ***** */
  move    #-10,loadflg(A4)      /* C-44a1:-1 to -10: the PLUSdwnld(-)option */
  moveq   #2,D0                        /* Get No. of rasters into D0        */
  bra     vblroutine               /* C-43a1..... TES-TEMP */

 /* bra     vstartplus                   * In VBL near'tpal3:' and exits
*/
      /* N.B.* VBL CHECKS FOR 'loadflg' BEING SET:  IF SO THEN COUNTER */
      /* RESETS TO 2 OR 3 RASTERS FOR LONG WRITE...(only turns on HBL) */

/* I.A.3  WORK(Plus) To a VIRGIN Screen: ................ */
vpdeother:             /* Not DE,or PLUS; = VIRGIN? Make a NEW DE screen   */
  move.l     #0xB2001A30,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  bsr     vwritedeid                   /* Put DEid on scrn/GetNew depalno  */

  movea.l     depaladdr(A4),A2   /* C-45a2:ALLOWS PLUS->DE PAL VIRGIN TRANSFER
*/
  movea.l   subpal0(A4),A1               /* DE program pal store area/w pal
addr */
  tst          subpalno(A4)               /* C-45a3: Subpal1-9 are 2 bytes
higher */
  beq          vitsubpal0                    /* C-45a3 */
  subq.l     #2,A1                         /* C-45a3 */

vitsubpal0:                                   /* C-45a3 */
  movem.l     4(A1),D2-D7/A3               /* Load in 14 colors only...
*/
  move.l     back1grd(A4),D1               /* Get background color into upper
word     */
  move.w     2(A1),D1                    /* 1st Subpal color into low word
*/
  movem.l   D1-D7/A3,0(A2)         /* END C-45A2:Upload into DE program
*/

  move    #-1,vidloadflg(A4)           /* Download prsnt clrs into new pal */
  bsr     vmovevidclrs                 /* Move vidclrs into pal            */
  move    #1,rgboff(A4)                /* Ensure PLUS is OFF               */
  move.l  depaladdr(A4),0x045A         /* Set 'systemptr'to load new Pal   */
  move    #1,degasmode(A4)             /* C-13:Reset degasmode to ON       */

  tst     scrnboxflg(A4)                    /* C-43b1:From Ascreenbox Routine?
*/
  bne     v1A3yes                              /* ......bra dont chge screen..
*/
  move.l     #0xB2001a3a,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  move.l  newscrnaddr(A4),D0            /* REset video stored scrn
*/
  move.l     D0,presentscrn(A4)          /* To new value...
*/
v1A3yes:
  move.l     presentscrn(A4),0x044E     /* Restore present scrn
*/
  clr.l       0x045E                           /*  Abort forced Chg back to
present */
  clr       scrnboxflg(A4)                    /* C-43b1END:Reset to normal null
*/

  move.b  #0x0,0xFFFa1b                /* (EXIT) turns off HBL timer       */
  bra     vblfrmint24                  /* avoids VBL and resets timers     */

/* ************************************************************************ */
/* ************************ I.B WORK(DEgas) to ??? ************************ */
/* ************************************************************************ */
/* A0=newscreenaddr: from'vcheckids';D3='newdepalno'; 'degasmode'is set     */

vdegastowhat:
  tst     adeidflg(A4)                  /* Is it a DE screen?               */
  beq     vnotdenext                   /* NO: branch                        */
  cmp     depalno(A4),D3               /* YES DE: Same DE? D3='newDEpalno'  */
  bne     vdetode                      /* NOTSAME:Degas, so branch......    */

/* I.B.0 WORK(DEGAS) TO (same) DEGAS:.......  */
  move.l     #0xB2001B00,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */

  tst     scrnboxflg(A4)                    /* C-43b1:From Ascreenbox Routine?
*/
  bne     v1b0yes                              /* ......bra dont chge screen..
*/
  move.l     #0xB2001B01,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  move.l  newscrnaddr(A4),D0            /* REset video stored scrn
*/
  move.l     D0,presentscrn(A4)          /* To new value...
*/
v1b0yes:
  move.l     presentscrn(A4),0x044E     /* Restore present scrn
*/
  clr.l     0x045E                           /*  Abort forced Chg back to
present */
  clr     scrnboxflg(A4)                    /* C-43b1END:Reset to normal null
*/

  move    #1,rgboff(A4)                     /* Ensure its off
*/
  move.b  #0x0,0xFFFa1b       /* This exit turns off HBL timer              */
  bra     vblfrmint24         /* also avoids VBL and resets timers (EXIT)   */

/* I.B.1 WORK(DEGAS) TO (Diff) DEGAS:.......  */
vdetode:
  move.l  #0xB2001B10,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  move    newdepalno(A4),D0            /*  */
  move    D0,depalno(A4)               /* Store new palno                   */
  move    #1,vidloadflg(A4)            /* Upload new pal to DE progam palv  */
  bsr     vmovevidclrs                 /* store area                        */
  move.l  depaladdr(A4),0x045A         /* Set sysclrptr toload vidregisters */
  move    #1,rgboff(A4)                /* Ensure its off                    */

  tst     scrnboxflg(A4)               /* C-28C:From Ascreenbox Routine?    */
  bne     v1b1yes                      /* ......bra dont chge screen..      */
  move.l  #0xB2001B1A,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */

  move.l  newscrnaddr(A4),D0            /* C-29:REset video stored scrn    */
  move.l  D0,presentscrn(A4)               /* To new value...              */
v1b1yes:
  move.l     presentscrn(A4),0x044E     /* C-29:Restore present scrn        */
  clr.l     0x045E                  /* C-29: Abort forced Chge backtopresent*/
  clr     scrnboxflg(A4)               /* C-28CEND:Reset to normal null     */

  move     #1,degasmode(A4)            /* C-28C: Ensure DEmode on           */
  move.b  #0x0,0xFFFa1b      /* This exit turns off HBL timer               */
  bra     vblfrmint24         /* also avoids VBL and resets timers (EXIT)   */



/* I.B.2 WORK(DEGAS) TO PLUS/OTHER:........(Bra also from 'Edit(DEGAS)' )  */
 vnotdenext:
  move.l     #0xB2001B20,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  tst     prgmidflg(A4)                     /* Is it a Degas to PLUS?       */
  beq     vdeother                          /* No: bra .....->to virgin     */
  clr     degasmode(A4)                     /* YES: =  a PLUS Screen        */
  clr     rgboff(A4)                        /*                              */
  clr.l       0x045E                        /* Abort forced Chge            */

  tst          scrnboxflg(A4)          /* C-43a1: START */
  bne          v1b2yes                 /* Editmode box klick...bra->        */
  move.l     #0xB2001B2A,test43b2(A4)     /* ***** C-43b0: TEST-TEMP *****  */
  move.l       newscrnaddr(A4),D0         /* PLUS MODE: REset stored screen */
  move.l     D0,presentscrn(A4)           /* To new value...                */
  move.l     D0,0x044E                    /* Restore present scrn           */
  clr     editsw(A4)                      /* Reset Para.                    */
  clr     loadflg(A4)
  tst     scrnidflg(A4)
  bne     vblroutine                 /* Same screen=no download-->EXIT      */

  move.l   #0xB2001B2B,test43b2(A4)     /* ******** C-43b0: TEST-TEMP ***** */
  move  #-10,loadflg(A4)            /* C-44a1:-1to-10:PLUSMODE dwnload      */
  bra   vstartplus              /* EXIT:C-45a6: CHGD from 'vblroutine'      */

v1b2yes: /* C-43a1: Still in edit mode; chg only if 'dirtyflg'NOT set       */
  move.l #0xB2001B2C,test43b2(A4)     /* ***** C-43b0: TEST-TEMP *****      */
  move.l presentscrn(A4),0x044E          /* Restore "original" present scrn */
  clr    scrnboxflg(A4)                  /* Reset to normal null            */
  move   #1,editsw(A4)                   /* Restore editsw                  */
  move.l #0x11114444, test43aa(A4)
  tst    dirtyflg(A4)                    /* Is pallete upload transfer?     */
  bne     vblroutine                     /* YES: Cont. normally no download */
/* -- */
  move.l     #0xB2001B2D,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
/*  bra     vblroutine  * C-45a7 removed ***** C-43b0: TEST-TEMP ***** */

  move    #-10,loadflg(A4)              /* C-45a7:Set for PLUS download     */
  clr     editsw(A4)                              /* C-45a7:Reset Para.
*/
/* >>>>>>> Set HBL for 2-3 rasters then exit/ (only turns on HBL) <<<<<<<   */
  bra     vstartplus                   /* In VBL near'tpal3:' and exits     */
                         /* C-43a1 END */

/* I.B.3 WORK(DEGAS) TO VIRGIN:.......................  */
vdeother:   /* Not DE, Not PLUS, therefore= VIRGIN? Make a NEW DE screen    */
  move.l     #0xB2001B30,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */

  bsr     vwritedeid                   /* Put DEid on scrn/GetNew depalno   */
  move    #-1,vidloadflg(A4)           /* Dwnload prsnt DEclrs into new pal */
  bsr     vmovevidclrs                 /* Move vidclrs into pal             */
  move    #1,rgboff(A4)                /* Ensure its OFF                    */
  move.l  depaladdr(A4),0x045A         /* Set 'system  to load new Pal      */

  tst     scrnboxflg(A4)                          /* C-28C:From Ascreenbox
Routine?       */
  bne     v1bxyes                                    /* ......bra dont chge
screen. */
  move.l     #0xB2001B3A,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  move.l  newscrnaddr(A4),D0            /* C-29:REset video stored scrn
*/
  move.l     D0,presentscrn(A4)          /* To new value... */
v1bxyes:
  move.l     presentscrn(A4),0x044E     /* C-29:Restore present scrn
*/
  clr.l     0x045E                           /* C-29: Abort forced Chge back to
present */
  clr     scrnboxflg(A4)                /* C-28CEND:Reset to normal null
*/

  move     #1,degasmode(A4)     /* C-28C: Ensure DEmode on
*/
  move.b  #0x0,0xFFFa1b      /* This EXIT turns off HBL timer               */
  bra     vblfrmint24         /* also avoids VBL and resets timers          */



/* ************************************************************************ */
/* ********* II. Editscreen(PLUS) to Workscreen   (D0=newscrnaddr) ******** */
/* ************************************************************************ */
veditowork:                  /*  (Edit to Work screen routine) */
/* ----Now Compare new w/old workscreen: */
  tst     scrnidflg(A4)
  beq     vnewscreen         /*  NOTSAME: Must do something..branch         */

/* 1. EDIT(Plus) to SAME WORKSCREEN: ................. */
/* I.Check 'dirtyflg':(So switching to other scrn */
/* ignores prior chgs and cancels themout,ie orig colors not chg'd.==UNDO.  */
/* Therefore,to NOT chg use HUE to look @ work screen(only!)                */

  move.l     #0xB2002A10,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */

  tst     scrnboxflg(A4)      /* #### C-43b1:Ascreenbox KLICK Routine? ########
*/
  bne     vplusyes                         /* ......bra dont chge screen.. */
  move.l  newscrnaddr(A4),D0            /* REset video stored scrn      */
  move.l  D0,presentscrn(A4)          /* To new value...                    */
vplusyes:
  move.l     presentscrn(A4),0x044E     /* Restore present scrn             */
  clr.l     0x045E                           /* Abort Chg back to present   */
  clr     scrnboxflg(A4)     /* ##### C-43b1END:Reset to normal null####### */

  tst     dirtyflg(A4)                       /* C-42b..moved here           */
  bne     vchgedpals                /* C-42b:Changed PALS.an upload needed  */
  move.l     #0xB3002A1a,test43b3(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  bra     vblroutine        /* EXIT: No change== standard run through VBL   */

 vchgedpals:
  move.l    #0xB3002A1b,test43b3(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  move    #1,loadflg(A4)                  /* Set UPLOAD flg=rts return      */
  bsr       hupload1                /* C-42b: uploads during VBL interrupt  */
  move.l    #0xB3002A1c,test43b3(A4)      /* ***** C-43b0: TEST-TEMP *****  */
  bra     vblroutine           /* OFF to normal 1stpal after upload routine */
/* Formerly required an activation of the hbl routine thru normal VBLEdit   */


/* IIA.2. EDIT(Plus) to DIFFERENT WORKSCREEN:.................. */
vnewscreen:   /* NOTSAME:Screen */
  move.l     #0xB2002A20,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  tst     dirtyflg(A4)                  /* Is it a simple upload?
*/
  beq     vnotanupload                  /* NO...bra
*/
  move.l     #0xB3002A2a,test43b3(A4)     /* ***** C-43b0: TEST-TEMP ***** */

  tst     scrnboxflg(A4)      /* #### C-43b1:Ascreenbox KLICK ?
################ */
  bne     v2A20yes                         /* ......bra dont chge screen.. */
  move.l  #0xB3002A2b,test43b3(A4)  /* ***** C-43b0: TEST-TEMP **** */
  move.l  newscrnaddr(A4),D0            /* REset video stored scrn      */
  move.l     D0,presentscrn(A4)          /* To new value...
*/
v2A20yes:
  move.l     presentscrn(A4),0x044E     /* Restore present scrn
*/
  clr.l     0x045E                      /* Abort forced Chg back to present */
  clr     scrnboxflg(A4)     /* ##### C-43b1END:Reset to normal null
######### */


  move    #1,loadflg(A4)      /* Set UPLOAD flg for rts return              */
  bsr       hupload1                 /* C-42b: ...uploads during VBL now
*/
  move.l     #0xB3002A2c,test43b3(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  bra     vblroutine          /* OFF to normal 1stpal after upload routine  */

/* IIa.2.a  */
vnotanupload:    /* Tis a mere shift to other screen:          */
  move.l     #0xB2002A2a,test43b2(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  tst     prgmidflg(A4)                 /* Is it a return to DEGASMODE??    */
  beq     vyesbacktodeg                 /* YES; Bra .....                   */

  tst    scrnboxflg(A4)    /* #### C-43b1:Ascreenbox KLICK Routine? ####### */
  bne       v2a2ayes                     /* ......bra dont chge screen..    */
  move.l    #0xB3002A2b,test43b3(A4)  /* ***** C-43b0: TEST-TEMP **** */
  move.l    newscrnaddr(A4),D0            /* REset video stored scrn        */
  move.l    D0,presentscrn(A4)          /* To new value...                  */
v2a2ayes:
  move.l     presentscrn(A4),0x044E     /* Restore present scrn
*/
  clr.l          0x045E                      /* Abort forced Chg back to
present */
  clr          scrnboxflg(A4)     /* ##### C-43b1END:Reset to normal null
######### */

  clr          editsw(A4)                      /* C-43a1: ensure-> hbl
routine...           */
/*  bra          vblroutine               * ***** C-43b0: TEST-TEMP ***** */
  move         #-10,loadflg(A4)     /* C-44a1:-1to-10:Sets the DOWNLOADFLG
*/

  moveq          #2,D0               /* BEG C-45a5: activate download
routine.... */
  bra          vstartplus               /* C-45a5: bypasses normal entry.
*/

/*  bra     vblroutine         * OFF to normal 1stpal then download routine */
/* This still requires activation of the hbl routine thru normal VBL Edit   */


/* IIa.3. EDIT(Plus) to DEGAS WORKSCREEN: ...............*/
  vyesbacktodeg:              /* So back to DEGASMODE....                   */
  move.l     #0xB3002A30,test43b3(A4)     /* ***** C-43b0: TEST-TEMP ***** */
  clr     editsw(A4)          /*                                            */
  bra     vfrmeditplus        /* Bra to 'I.A Plusmode to ???'........       */
/* ***************** END MAJOR ADDITION TO VBL ROUTINE  ******************* */
/* ************************************************************************ */



/* VARIOUS ROUTINES: (not in vbl portion) */

/* ************************************************************************ */
/* ***************** HDOWNLOAD:  C-16 DE_06 12/21/88 ********************** */
/* ENTRY: loadflg=-1,newscrnaddr, paladdr0,paladdr1,paladdr2,A4=basereg ref */
/* CHGES: D0-D7,A0-A3,A5                                                    */
/* NEW Parameters chgd: 'scrnid',palwidno, lastpal                          */
/* palwidno->Byte#-160;prgmid->#-128 & -96;scrnid->#-64 & -32
*/
/* ************************************************************************ */

hdownload:          /* C-26:From'updwnloadsw' */
/* 1. Setup parameters: ****************************      */
  move.b  #0x0,0xFFFa1b      /* This turns off HBL timer; C-17              */
  clr     loadflg(A4)                    /* C-43a1:Reset flg                */

  move.l  newscrnaddr(A4),A3           /* Get screen addr into A3           */
  clr.l   D0
  clr     D1
  move.b  32000-160(A3),D0             /* Get Palwidno (1-10) from screen   */

  beq     hupit10                      /* A mistake if '0'                  */
  cmpi    #10,D0                     /* C-42a:The maximum No. of pals allowable
*/
  bls     hokno0                       /* C-41d(was bcc) Its acceptable,bra */
  move    #10,D0                      /* C-42a:Too many...reduce to '10'    */
  bra     hokno0
 hupit10:
  move    #1,D0                            /* C-42a:Well....=1 the minimum */
 hokno0:
  move.l  D0,A5                        /* Make it outer loop (# pals) index */
  move    D0,palwidno(A4)              /* END C-23:Reset store to newnumber */

  move    D0,D4                        /* BEGC-44b2:'lastpal'equals oneless */
  subq    #1,D4                        /* than'palwidno'except always 1 Min */
  bne     hitsnot0
  moveq     #1,D4
 hitsnot0:
  move    D4,lastpal(A4)                   /* END C-44b2  */

  move     D0,D1
  sub   subpalno(A4),D0    /* BEG C-45a2:Copy singlepal routine************ */
  clr   hitoloflg(A4)          /* C-45a6 */
  cmp   subpalno(A4),D1        /* C-45a5If no. less=OK,else =or grter-f>adj */

  bhi    hinbounds             /* (to bhi frm bpl)Here#1=last pal,i.e.bottom*/
  move     #1,hitoloflg(A4)          /* C-45a6 */
  moveq     #1,D0                        /* Must always be ONE pal          */
 hinbounds:
  tst     hcpypalflg(A4)                   /* Flag set? */
  bne     hokcpypal
  clr     D0                             /* NO, so null it=bypass
*/
 hokcpypal:
  move     D0,hcpypalno(A4)              /* Store adj. pal #                */
  clr     hcpypalflg(A4)  /* END:C-45a2, Reset flag....***************      */

  move.w  32000-64(A3),D0              /* Get top byte                      */
  move.b  32000-32(A3),D0              /* Get bottom byte                   */
  move    scrnid(A4),test43a8(A4)    /* TEST-TEMP C-44b0 Codd...REMOVE..>>> */
  move    D0,scrnid(A4)                /* Get new'scrnid' into storage      */

  move.l paladdr0(A4),A0                  /* C-41:Get paladdrs for download */
  move.l paladdr1(A4),A1
  move.l paladdr2(A4),A2
  adda.l #32000-320,A3               /* C-44B3: start address for move.     */

/* C-43A START TEST-TEMP....DO REMOVE!! >>>>>>>>>>>>>>>>>>>>>>>> */
  move      scrnid(A4),test43a1(A4)
  move      palwidno(A4),test43a2(A4)
  move.l A3,test43a3(A4)
  move.l 0x44E,test43a4(A4)
  move.l A0,test43a5(A4)
  move.l A1,test43a6(A4)
  move.l A2,test43a7(A4)
/* C-43A END TEST-TEMP....DO REMOVE!! >>>>>>>>>>>>>>>>>>>>>>>> */

 /* New Code Register Constants Setup: C-42b3  */
  clr.l   D3                           /*   D3= counter                     */
  move.l  #0x01110111,D4               /*   D4= 'AND' mask for halfclr bits */
  move.l  #0x00100010,D5               /*   D5= 'green'mask                 */
  move.l  #0x00010001,D6               /*   D6= 'blu'  mask also #1 for LSR */
  move.l  #20,D7                       /*   D7= bit test 20=bit21           */

  move    (A3)+,D0                     /* 1st color of Pal0=onlyeven(bckgrd */
  and.l   #0x0EEE,D0                   /* Make sure its even....            */
  move    D0,(A2)+                     /* Put it in sum palette             */
  lsr     #1,D0                        /* Half the color sum                */
  move    D0,(A0)+                     /* An even never half color backgrd. */
  move    D0,(A1)+                     /* Both pals have same no.           */

/* NOTE: Now offset by ONE word from start each Pal0,1,2                    */
  move.l     A5,D0          /* BEG:C-45a2 Init. D0 in first pass for cpypal */

hdown0:  /*  OUTER LOOP rentry point for next pal download ****             */
  moveq        #7,D3           /* (C-42b3:Must Bypass first download always)*/
  cmp     hcpypalno(A4),D0     /* = so is pallete to replace downloads pal  */
  bne.s   hentry               /* else bypass....hcpypalno=0 always bypasses*/
  tst     hitoloflg(A4)          /* C-45a6 */
  beq.s   hnormal1               /* C-45a6:NOT from a "higher" pal          */
  move.l  A2,saveA2(A4)          /* C-45a6:Save old indexes                 */
  move.l  A3,saveA3(A4)          /* Ditto                                   */
  move.l  subpal2(A4),A2          /* C-45a6: Fetch last sum subpal          */
 hnormal1:
  movem.l    (A2)+,D0-D2/D7     /* Get next 8 words/colors but from subpal */
  movem.l    D0-D2/D7,(A3)      /* Upload 8 subpal sum clrs onto scrn pal  */
  movem.l    (A2)+,D0-D2/D7     /* Again,next                              */
  movem.l    D0-D2,16(A3)       /* Only next 6, =14 total                  */
  swap       D7
  move.w     D7,28(A3)          /* Last color uploaded                      */
  move.l     #20,D7             /* Restore 'constant' for normal routine    */
  move.l     #32,D0             /* Offset adj for pal addresses             */

  tst       hitoloflg(A4)      /* BEG C-45a6 ***********                    */
  beq.s     hnormal2               /* not from higher pal? then bra         */
  move.l    saveA2(A4),A2          /* Restore old parameters                */
  move.l     saveA3(A4),A3
  bra.s          hentry
 hnormal2:                          /* END C-45a6:now dwnload to get
intonormalpal */

  adda.l  D0,A0                         /* Addrs. for next pal-1st clr for
outerloop */
  adda.l  D0,A1                         /* A2 already upped
*/
  adda.l  D0,A3
/*  clr  hcpypalno(A4)  * TEMP REMOVAL Reset to Null,i.e.the bypass pal No. */
  bra     henddwn0      /* END:C-45a2, Bypass normal download routine->     */

/* NOTE:  The 'bra.s hentry' Avoids extra long download                     */
/* One less Dbf routine-resets inner loop */
/* NOTE: 8X=16 words,ie.last is redundant and is corrected in outer loop... */

/* 2. Actual INNER LOOP Start Point: ************************** */
hdown1:
  move.l  D0,(A0)+
  move.l  D1,(A1)+            /* Puts in 2 words (colors) at one time       */

hentry:   /* Bypass at first pass */
  move.l  (A3)+,D0            /* Strts one word after 1stclr(offset 1 word) */
  move.l  D0,(A2)+            /* Store Pal sum in Pal2                      */

  move.l  D0,D2               /* D0=(red/blue pal),D1=(grn or blue),D2=work */
  and.l   D4,D2               /* D2= ones, or half colors                   */
  sub.l   D2,D0               /* Remove ones from sum                       */
  lsr.l   #1,D0               /* C-44b0:1/2 the sum    ********* */
  move.l  D0,D1               /* Duplicate for Pal#1                        */

/* 3. Process For Left/Right Green Determination:  */
  bclr    #4,D2               /* Bit #5 is Right Green if there             */
  bne     hrightgrn           /* Upper color has green bit set...Bra        */
  bclr    D7,D2               /* Bit #21 =Left  Green if there              */
  bne     hleftgrn            /* Lower color green bit set.......Bra        */

/* 3.a Fall thru= no green half color      */
  add.l   D2,D0               /* No green, so add all ones into both        */
  and.l   D6,D2               /* Blu mask off red+blu sum in D2             */
  sub.l   D2,D0               /* Remove blu bits from Pal0                  */
  add.l   D2,D1               /* Add in blu bits to Pal1                    */
  dbf     D3,hdown1
  bra.s   henddwnload

/* 4. Green in Upper Color(or Both): */
hrightgrn:
  bclr    D7,D2               /* Check for and remove(if there) LeftGrnbit  */
  bne     hbothgrn            /* Both Upper+lower green bits set...bra      */
  add.l   D2,D0               /* Put Both Red and Blue Bits in Pal0 first   */
  and.l   D6,D2               /* Blumask off red bits in D2                 */
  clr.w   D2                  /* Only Left Blu to be shifted to Pal1        */
  sub.l   D2,D0               /* Remove Left blue bit from Pal0             */
  add.l   D2,D1               /* Put left blu in Pal1                       */
  add.w   D5,D1               /* Add in Right Green Bit...#5                */
  dbf     D3,hdown1
  bra.s   henddwnload

/* 5. Green in Lower Color Only (Left Word): */
hleftgrn:
  add.l   D2,D0               /* Add in both blu/red half colors to Pal0    */
  add.l   D5,D1               /* First add both grns                        */
  sub.w   D5,D1               /* Then remove right green                    */
  and.w   D6,D2               /* Blu mask off red bits                      */
  sub.w   D2,D0               /* Remove right blu                           */
  add.w   D2,D1               /* Add in right blu to Pal1                   */
  dbf     D3,hdown1
  bra.s   henddwnload

/* 6. Both Green Bits Set: */
hbothgrn:
  add.l   D2,D0               /* All blu+red half colors in Pal 0           */
  add.l   D5,D1               /* Add both greens to Pal1                    */
  dbf     D3,hdown1
  bra.s   henddwnload

/* ******************* End New Code of each 15 word Pal dwnload *********** */

/* ***** Outer Loop:  (Also word adjusts) ****** */
henddwnload:                  /* Starts with a one word offset              */

  move.l  D0,(A0)+            /* C-42b3 */
  move.l  D1,(A1)+            /* C-42b3: Last run was NOT downloaded        */
 henddwn0:   /* C-45a2: From hcpypal..bypass afterupload frm subpal */

  move    -2(A3),D0           /* put in 1st word with Palwidth in next Pal  */
  andi    #0x00ff,D0          /* ensure no Id's are dwnloaded               */
  move    D0,-2(A0)           /* redo each subpal first word:(not subpal0)  */
  move    D0,-2(A1)           /* C-44b4: ALSO, now pal 1! */
  move    D0,-2(A2)                 /* C-42a: Only pals 0 & 2
*/
  move    A5,D0
  subq.w  #1,D0               /* A5=# of pals(i.e.'Palwidno'):OUTERLOOP TST */
  movea   D0,A5               /* restore in A5                              */
  bne     hdown0              /*  ***** OUTER LOOP RECYCLE ***** */
/* ---- End Both Outer/Inner Loop Dwnloads --- */

/* 5. ******** Restore Parameters to New Vidscreen Quantities: ************ */
  clr.l   D0
  clr     D1
  move     #196,-2(A0)            /* C-44B2: End No. bottom line
*/
  move     #196,-2(A1)            /* C-44B4: also End No. bottom line
*/

  clr     rgboff(A4)          /* Back to normal mode                        */
  clr     subpalno(A4)        /* Set'0'(present)'subpalno'+(old)'osubpalno' */
  clr     osubpalno(A4)
  clr     dirtysw(A4)
  clr     dirtyflg(A4)
  clr     didloadflg(A4)
  clr     loadflg(A4)
  clr     scrnboxflg(A4)

  move.l  newscrnaddr(A4),A3       /* C-41: Restore base address of scrn
*/
  move.b  32000-320+33(A3),D0      /* C-42b: Get only byte
*/
  move.w  D0,palwidth(A4)              /* Reset Palwidth to firstpal
*/

  clr     wasdegasflg(A4)     /* Now out of DEGASMODE into Plusmode         */
  clr     degasmode(A4)       /* Clear flag ?;                              */

/* 6.******* Put 'Background' color in DEAD bottom Pal: ******************* */
  move.l  paladdr0(A4),A5     /* C-41 Fetch base address of Pal0
*/
  move.l  (A5),D0             /* Get background color                       */
  move    (A5),D0             /* Both words = background color0 from Pal0   */
  move.l  D0,D1               /* Prepare for dwnload to Pal0/1 Dead Pals    */
  move.l  D0,D2
  move.l  D0,D3
  lea.l       32(A0),A0                 /* C-41: 16clrs moved  */
  lea.l   32(A1),A1

  movem.l D0-D3,-(A0)          /* No need for Pal2 sum palette here         */
  movem.l D0-D3,-(A1)
  movem.l D0-D3,-(A0)
  movem.l D0-D3,-(A1)                /* C-44a1: Now 4 bytes below 1st clr
*/

  move    #0x0Fa,34(A0)
  move    #0x0Fa,34(A1)       /* Turn off # in next phantom pal slot        */

  nop          /* TEST-TEMP .................. */
  nop
  nop
  bra     hbexit          /* C-44b0: 2d loop back */
  nop          /* TEST-TEMP .................. */
  nop

/* XXXXXXXXXXXXXXXX C-43A TEST TEST TEST....REMOVE WHEN DONE XXXXXXXXXXXXXX */
 bra   testbypass     /* Remove....when needed  */
 move     #0,degasmode(A4)     /* reset to degasmode */
 move     #0,rgboff(A4)          /* reset */
 bra   regbas16
 movem.l  (A7)+,D0-D7/A0-A6
 rte
testbypass:   /* Remove when needed */
/* XXXXXXXXXXXXXXXXXXXXXXX END TEST END TEST ....REMOVE XXXXXXXXXXXXXXXXXXX */



/* 7. Store'OnScreen Guard Clr15/3 Line' on vidscreen (ensure its there!)   */

  move.l  #0xFFFFffff,D1               /* Color No.15 (or No.3 in med res)  */
  cmp.l   32000-324(A3),D1             /* C-41First do scrn checkIs there?  */
  bne     hputonguard                  /*  NO: So do it....                 */
  cmp.l   32000-328(A3),D1
  bne     hputonguard
  cmp.l   32000-468(A3),D1             /* C-41:2d check...Is There ?        */
  bne     hputonguard
  cmp.l   32000-472(A3),D1
  beq     hguardok                     /* Its There!...bypass no need
*/

 hputonguard:
  move.l  D1,D2                        /* 8 longs */
  move.l  D1,D3
  move.l  D1,D4
  move.l  D1,D5
  move.l  D1,D6
  move.l  D1,D7
  move.l  D1,A1

  moveq   #4,D0                        /* Dbf=4,or 5 cycles(8*4*5=160bytes) */
  adda.l   #32000-320,A3                /* End of line addr, 3 lines back   */
 hguardline:
  movem.l D1-D7/A1,-(A3)               /* Use decrement move                */
  dbf     D0,hguardline                /* .....yawn, do it again            */

 hguardok:                             /* By pass from start of guardline   */
 movem.l  (A7)+,D0-D7/A0-A6                    /* C-41 A6  */
 rte


/* **********************END C-16 DE_06************************************ */
/* ************************************************************************ */

/* Question remains....palwidno, lastpal ?...which =no. of pals ????? */

/* ************************************************************************ */
/* *********            HUPLOAD: DE_07, C-17 12/21/88          ************ */
/* ************************************************************************ */
/* ENTRY: loadflg=+,paladdr2,newscrnaddr,palwidno?,dirtyflg=1,A4=basereg    */
/* CHGS:  D0-7,A0,2,3;scrnid,dirtyflg,loadflg,didloadflg                    */
/* ************************************************************************ */
hupload:         /* C-26: From 'updwnloadsw'/C-42b2:From editplustoworkplus */
/*  move   #1,did0loadflg(A4)    TEMP REMOVALC-42b * Reset didloadflg       */
  clr      loadflg(A4)                       /* C-42b: TEMP...see above     */

hupload1:    /* C-41b: From 'vyeswasdegas'/C-42b:From II.1 'vchgedpals'     */
  move.b  #0x0,0xFFFa1b                /* This turns off HBL timer          */

/* 1. Get Screen Addresses, parameters:    */
  movea.l  newscrnaddr(A4),A3          /* Get workscreen addr               */
  move.l  A3,A0                        /* Duplicate it                      */
  move.l  paladdr2(A4),A2              /*  doublesum palette2 address       */
  adda.l  #32000-320,A3                /* Start of on screen palettes       */
  clr.l     320-12(A3)                 /* C-44B3: clear DE on-scrn pal/ids  */
  clr.l     320-8(A3)
  clr.l     320-4(A3)
  move    palwidno(A4),D0              /* Total of Pals used (1-10)         */
  subq    #1,D0                        /* Dbf adjustment                    */
  bmi     hupit0                       /* Amistake if below '0'             */
  cmpi    #9,D0                        /* The maximum No. of pals allowable */
  bls     hokno                        /* C-41d(was bcc) Its acceptable,bra */
  move    #9,D0                        /* Too many...reduce to '9'          */
  bra     hokno
 hupit0:
  clr     D0                           /* Well....'0'=1 the minimum         */
 hokno:
  nop


/* 2. The Upload Loop: (D0=counter; A3=vidscreen; A2=stored plus palettes */
huploadloop:
  movem.l (A2)+,D1-D7/A1               /* From prgm pal to registers        */
  movem.l D1-D7/A1,(A3)                /* Upload onto vidscrn 16 words/clrs */
  adda.l  #32,A3                       /* Update screen addr by 32 bytes    */
  dbf     D0,huploadloop               /* When = No. of pals then stops     */

/* 3. Restore On Screen Parameters in uploaded pals:  */
  move    palwidno(A4),D1
  move.b  D1,32000-160(A0)             /* Restore pal no.; A0=vidscrn start */

  move    prgmid(A4),D1
  move.b  prgmid(A4),32000-128(A0)     /* ....programid restored on screen  */
  move.b  D1,32000-96(A0)              /* C-41d(from A4) Lower byte of id   */

  move.w  0x04BC,D0                    /* New scrnid from sys 200Hz clock   */
  move    D0,scrnid(A4)                /* New scrnid from sys 200Hz clock   */
  move.b  scrnid(A4),32000-64(A0)      /* Upper byte on screen              */
  move.b  D0,32000-32(A0)              /* Lower byte ....to screen          */

  clr     dirtyflg(A4)                 /* Turn off palupload token          */
  clr     dirtysw(A4)                  /* Reset Screenbox loop flg          */
  clr     rgboff(A4)                   /* Turn off Degasmode flgs           */
  clr     degasmode(A4)                /*  "    "    "    "  bits           */

/* 4. Store'OnScreen Guard Clr15/3 Line' on vidscreen ********************  */
  move.l  #0xFFFFffff,D1               /* Color No.15 (or No.3 in med res)  */

  move.l  D1,D2                        /* 8 longs ready to upload           */
  move.l  D1,D3
  move.l  D1,D4
  move.l  D1,D5
  move.l  D1,D6
  move.l  D1,D7
  move.l  D1,A1

  moveq   #4,D0                        /* Dbf=4,or 5 cycles(8*4*5=160bytes) */
  movea.l newscrnaddr(A4),A0           /* Get vidscreen start addr          */
  adda.l  #32000-320,A0                /* End of line addr, 3 lines back    */

 h0guardline:
  movem.l D1-D7/A1,-(A0)               /* Use decrement move                */
  dbf     D0,h0guardline               /* .....yawn, do it again            */

  tst     loadflg(A4)               /* C-42b: NEW, bra back to call routine */
  beq     hwas1stpass               /* C-42b: to cont. with normal vbl      */
  clr     loadflg(A4)               /* .... to prevent vblroutine hbl chge  */
  rts
hwas1stpass:     /* C-42b */

  movem.l (A7)+,D0-D7/A0-A6                    /* C-41b A6 */
  rte

/* ********************* END DE_07 C-17 *********************************** */



/* ************************************************************************ */
/* *********    (ALT.)  SCREENBOX: DE_08a, C-27  1/05/89       ************ */
/* ************************************************************************ */
/* ENTRY:   A0=editscrn */
/* NEW PARAMETERS:'numofscrn'=divby4if>2sub4==actualno,'scrnboxflg'dirtysw' */
/* CHGS:  D1-D5,A0-3,5;numofscrn(=offset into array),scrnboxflg,newscrnaddr */
/*        editcycle */
screenAbox:                      /* From 4.1 routine  */

/* 1. Setup of parameters: *************************  */
 cmpi     #3,editcycle(A4)     /* C-302:Reset in 4.1.                       */
 bcc   voldbox0               /* C-302: Assures min. of editcycles run      */
 addi  #1,editcycle(A4)     /* C-302: To max of above                       */
 bra   vfrmAscrnbx               /* C-302: and bypass to 4.1                */

/* 2. First,Check If Old Box Still Active:  ---------- */
voldbox0:
   move  #27256,D1              /* Get Old box Screenline offset(byte)      */
   mov    0(A0,D1.w),D2
voffset0:
   btst   #0x5,D2            /* Get offset into word,and test top'on'bit    */
   bne    Ascrnbxout               /* C-300 ON: Still oldscrn box, bra->OUT */

voldbox1:
  move     #0x31256,D1            /* Get old box 2d screenline offset(byte) */
  move     0(A0,D1.w),D2
voffset1:
  btst     #0x9,D2              /* Get offset into word,test bottom 'on'bit */
  bne      Ascrnbxout


/* 3. A New Box...But is it Lawful pass?---(C-42a:MOVED TO 5.A AT END)----- */

Anewscrnbx:    /* C-28A:FROM internal AND from 1st pass routine A0=editscrn */
    tst     resolution(A4)           /* Is it med res?                      */
    bne     Aboxhires       /* YES: So branch here,since no adj possible    */


/* 4.a Test For LOWRes Box.... find new screen:  --------------  */

Ascreen1box: /* 170*160=27200(topline offset A0),195*160=31200(bot line)    */
     move     27200+56(A0),D1     /* Upper bit test: Get scrn byte offset   */
     btst     #5,D1                         /* BIT Offset into 1st byte     */
     beq      Ascreen2box                /* Not Set..... try next scrnbox   */
     move     31200+56(A0),D2     /* Bottom bit test                        */
     btst     #9,D2               /* BIT Offset into 2d byte...             */
     beq     Ascreen2box          /* Something is wrong here,BUT cont, on   */

     move   #27200+56,D1    /* Gotcha: Load newparameters for'oldbx'routine */
     move     #31200+56,D2     /* Second byte offset..                      */
     move     #5,D3            /* First bit offset...                       */
     move     #9,D4            /* 2d bit .........                          */
     move     #4,D5            /* Offset into Screenbase array=addr         */
     bra    Ascrnbxfound       /* Have got new box=new screen               */
Ascreen2box:
     move     27200+64(A0),D1
     btst     #7,D1
     beq          Ascreen3box
     move     31200+64(A0),D2
     btst     #11,D2
     beq          Ascreen3box

     move     #27200+64,D1
     move     #31200+64,D2
     move     #7,D3
     move     #11,D4
     move     #8,D5
     bra          Ascrnbxfound

Ascreen3box:
     move     27200+72(A0),D1
     btst     #10, D1
     beq          Ascreen4box
     move     31200+72(A0),D2
     btst     #13,D2
     beq          Ascreen4box

     move     #27200+72,D1
     move     #31200+72,D2
     move     #10,D3
     move     #13,D4
     move     #28,D5
     bra          Ascrnbxfound
Ascreen4box:
     move     27200+80(A0),D1
     btst     #11,D1
     beq          Ascreen5box
     move     31200+80(A0),D2
     btst     #14,D2
     beq          Ascreen5box

     move     #27200+80,D1
     move     #31200+80,D2
     move     #11,D3
     move     #14,D4
     move     #32,D5
     bra          Ascrnbxfound
Ascreen5box:
     move     27200+80(A0),D1
     btst     #0,D1
     beq          Ascreen6box
     move     31200+80(A0),D2
     btst     #3,D2
     beq          Ascreen6box

     move     #27200+80,D1
     move     #31200+80,D2
     move     #0,D3
     move     #3,D4
     move     #36,D5
     bra          Ascrnbxfound
Ascreen6box:
     move     27200+88(A0),D1
     btst     #6,D1
     beq          Ascreen7box
     move     31200+88(A0),D2
     btst     #8,D2
     beq          Ascreen7box

     move     #27200+88,D1
     move     #31200+88,D2
     move     #6,D3
     move     #8,D4
     move     #40,D5
     bra          Ascrnbxfound
Ascreen7box:
     move     27200+96(A0),D1
     btst     #12,D1
     beq          Ascreen8box
     move     31200+96(A0),D2
     btst     #15,D2
     beq     Ascreen8box          /* NOT found...bypass and keep the same.. */

     move     #27200+96,D1
     move     #31200+96,D2
     move     #12,D3
     move     #15,D4
     move     #44,D5
     bra      Ascrnbxfound     /* Scrn #8 is at 48 offset: NOT usedhere.... */

 Ascreen8box:     /* Only Partly Functional now, keep for future if needed */
/*     move     27200+0(A0),D1                                   */
/*     move.w  #0xFFFF,27200+0(A0)    TEST C-300 XXXXXXXXXXXXXXX */
/*     btst     #11,D1                                           */
/*     beq          Ascrnbxout                                   */
/*     move     31200+0(A0),D2                                   */
/*     move.w  #0xFFFF,31200+96(A0)   TEST C-300 XXXXXXXXXXXXXXX */
/*     btst     #11,D2                                           */
/*     beq          Ascrnbxout                                   */

     move     #27200+0,D1       /* This part is functional..stops #7 lockup */
     move     #31200+0,D2       /* ..... at left screen edge..              */
     move     #11,D3
     move     #11,D4
     move     #48,D5
     bra     Ascrnbxfound       /* Scrn #8 is at 48 offset:  used here.... */


/* 4.b Test for HIGHRes Box... New screen................------------------ */
Aboxhires:  /* 170x160=27200(topline offset A0),195x160=31200(bot line)     */
     move     27200+60(A0),D1       /* Upper bit test: Get scrn byte offset */
     btst     #6,D1                 /* BIT Offset into 1st byte             */
     beq     Hscreen2box            /* Not Set..... try next scrnbox        */
     move     31200+60(A0),D2               /* Bottom bit test              */
     btst     #10,D2                         /* BIT Offset into 2d byte...  */
     beq    Hscreen2box             /* Something is wrong here,BUT cont, on */

     move    #27200+60,D1   /* Gotcha: Load newparameters for'oldbx routine */
     move     #31200+60,D2     /* Second byte offset..                      */
     move     #6,D3            /* First bit offset...                       */
     move     #10,D4           /* 2d bit .........                          */
     move     #4,D5            /* Offset into Screenbase array=addr         */
     bra    Ascrnbxfound               /* Have got new box=new screen       */
Hscreen2box:
     move     27200+68(A0),D1
     btst     #15,D1
     beq       Hscreen3box
     move     31200+64(A0),D2
     btst     #3,D2
     beq       Hscreen3box          /* Something is wrong here,BUT cont, on */

     move     #27200+68,D1
     move     #31200+64,D2
     move     #15,D3
     move     #3,D4
     move     #8,D5
     bra          Ascrnbxfound
Hscreen3box:
     move     27200+72(A0),D1
     btst     #3,D1
     beq          Hscreen4box
     move     31200+72(A0),D2
     btst     #10,D2
     beq          Hscreen4box

     move     #27200+72,D1
     move     #31200+72,D2
     move     #3,D3
     move     #10,D4
     move     #28,D5
     bra          Ascrnbxfound
Hscreen4box:
     move     27200+80(A0),D1
     btst     #2,D1
     beq          Hscreen5box
     move     31200+80(A0),D2
     btst     #8,D2
     beq          Hscreen5box

     move     #27200+80,D1
     move     #31200+80,D2
     move     #2,D3
     move     #8,D4
     move     #32,D5
     bra          Ascrnbxfound
Hscreen5box:
     move     27200+88(A0),D1
     btst     #12,D1
     beq          Hscreen6box
     move     31200+88(A0),D2
     btst     #15,D2
     beq          Hscreen6box

     move     #27200+88,D1
     move     #31200+88,D2
     move     #12,D3
     move     #15,D4
     move     #36,D5
     bra          Ascrnbxfound        /* Have got new box=new screen       */
Hscreen6box:
     move     27200+92(A0),D1
     btst     #10,D1
     beq          Hscreen7box
     move     31200+92(A0),D2
     btst     #13,D2
     beq          Hscreen7box

     move     #27200+92,D1
     move     #31200+92,D2
     move     #10,D3
     move     #13,D4
     move     #40,D5
     bra          Ascrnbxfound  /* Have got new box=new screen             */
Hscreen7box:
     move     27200+96(A0),D1
     btst     #10,D1
     beq          Hscreen8box     /* NO Match...keep kool and bypass!       */
     move     31200+96(A0),D2
     btst     #14,D2
     beq          Hscreen8box          /* ditto */

     move     #27200+96,D1
     move     #31200+96,D2
     move     #10,D3
     move     #14,D4
     move     #44,D5
     bra          Ascrnbxfound     /* Have got new box=new screen           */

Hscreen8box:   /* Only Partly Functional now.... keep for future if needed  */
/*     move     27200+0(A0),D1          */
/*     btst     #11,D1                  */
/*     beq          Ascrnbxout          */
/*     move     31200+0(A0),D2          */
/*     btst     #11,D2                  */
/*     beq          Ascrnbxout          */

     move     #27200+0,D1          /* This IS functional...stops #7 lockup  */
     move     #31200+0,D2
     move     #11,D3
     move     #11,D4
     move     #48,D5
     bra        Ascrnbxfound     /* Scrn #8 is at 48 offset:  used here.... */

/* ----------------------------------- */

/* 5. ScreenBox is Found:  */
Ascrnbxfound:
  movea.l screenbase(A4),A1   /* C-26:Added'base':Get DEscrnptr arraybase   */
  move.l  0(A1,D5.w),D0            /* Get screenaddr from array+offset      */
  beq     Ascrnbxout               /* 0= invalid screen change...abort      */

  move.l  D0,newscrnaddr(A4)     /* OK:So Upadate newa screen addr...       */
  move    #1,scrnboxflg(A4)      /* Set the flag                            */
  move    D5,numofscrn(A4)       /* Store screen No.(offset into array)     */

     lea.l     voldbox0,A1               /* Renew these old stodgy.numbers..*/
     lea.l     voldbox1,A2
     lea.l     voffset0,A3
     lea.l     voffset1,A5
     move          D1,2(A1)
     move          D2,2(A2)
     move          D3,2(A3)
     move          D4,2(A5)
     movea.l   D0,A0       /* Prepare for 'vfrmstart' w/D0=A0='newscrnaddr' */

/* 5.A New Box...But is it Lawful Plus download?---(C-42a:MOVED FROM 3.0)-- */
     tst          degasmode(A4)     /* C-45a3 */
     beq          sokmode               /* C-45a3: In PLUS mode? bra...
*/
    clr  hcpypalflg(A4)     /* C-45a3 Clear stops transfer->Plus single pal */
  sokmode:                         /* C-45a3 */
    tst  dirtyflg(A4)                 /* IF pal chgd:dont downloadon click */
    beq  sAgoodtry                    /* Normal....branch                  */

/* 'Dirtyflg/hcpypalflg'reset only when flg=1,sw=1:where flg=set,sw=cleared */
    clr  dirtyflg(A4)           /* C-45a3...clear flags to normal so that.. */
    move #1,hcpypalflg(A4)   /* C-45a2:Only set here for single palcpy      */
    bra  vblroutine              /* C-45a4:->EXIT:abort download.           */
/*     bset #0,dirtysw(A4)  * On 1st pass:sw=1,flg=0;->BYTE basis NOT WORD  */
/*     beq     vblroutine    * ->EXIT:abort download. On 2d pass,sw=1,flg=1 */
/*     clr     dirtyflg(A4)  * ...clear flags to normal so that....         */
/*     clr     hcpypalflg(A4)* C-45a2 Clear transfer Plus single pal        */
/*     clr   dirtysw(A4)   * Allows'PLUS'upload with 1st chg,dwnload then on*/
/* Note: 'dirtysw' only set HERE;'dirtyflg'only set in Plus color chge      */
/* N.B.'Dirtysw'is reset where dirtyflg is cleared:'hupload',vtruechange',  */
/*                                                 'vgotdepaladdr'          */
sAgoodtry:          /* Time to test for First Pass  */

  tst   newdeflg(A4)   /* C-28A:Return->first pass routine?(w/Para.set)  */
  bne   vtruechange    /* C-302: NORMAL..so go chge color palettes       */

  clr   scrnboxflg(A4)    /* C-28A: Reset flag.....                      */
  bra   backfrmbox        /* C-28A: And continue the First Passroutine   */

/* 6. Screen Box bypass: ------------------------ */
Ascrnbxout:
     clr   scrnboxflg(A4)
     bra   vfrmAscrnbx          /*......continue normal process....(EXIT)   */

/* ********** END THIS a lousy routine..*********************************** */


/* ********* SUBROUTINE TO SAVE VIDEO COLORS *********** */
vsavevidclrs:
  move.l  #0xFF8240,A3        /* Get VIDEOPAL addr       */
  movem.l (A3)+,D0-D7         /*  "    colors            */
  movea.l vidclrs(A4),A2      /* Get array base,a long   */
  movem.l D0-D7, 0(A2)        /* Move into static array  */
  rts   /* Clrs 15+16 are reversed and 15 should be put  */
        /* in 1stWord of subpallete,thru'swap'etc.       */
/* *************** orig to here pre version 10 ********* */


/* *********************************************************************** */
/* **** C-10/11 'DE_1' SUBROUTINE-'vwritedeid' TO SCREEN+ASSIGN PALNO **** */
/* *********************************************************************** */
/* ENTER: A0=screenaddr, deid== 'FFFF, FFFF, FFFF, FFFF' @32000-320-(4*2)  */
/* x= palnos 0-15 ??=at initial.'FFFF, FF??, FF??, FF0x' @32000-8,-6,& -4  */
/* CHGS:  D2, updates depalno, installs id+new # on lower corner+sets flg  */
/* EXIT:  A0=scrn address, D2=new lastdepal ('0xFF0x'form),'vidloadflg'= - */
/*        'depalno'= The current palette now in use */

vwritedeid:
  move.l  #0xFFFFFFFF,D2               /* Make upper 'FF...' boundary       */
  move.l  D2,32000-160-8(A0)           /* Upper start addr                  */
  move.l  D2,32000-160-4(A0)           /* 2d upprboundary= 4 words all res. */
  move.w  D2,32000-8(A0)               /* Lower protective boundary         */
  move.l  thedeid(A4),32000-6(A0)      /* Install DEprgm id                 */
  move    lastdepal(A4),D2             /* Gets last Pal used:in form'000x'  */
  add.w   #1,D2                        /* Update to next no.                */
  and.w   #0x000F,D2                   /* Ensure recycles 0 to 15           */
  move    D2,lastdepal(A4)             /* Update last Palette used flg      */
  move    D2,depalno(A4)               /* Store new current 'depalno'       */

  ori.w   #0xff00,D2                   /* Put in protective FF bits         */
  move    D2,32000-2(A0)               /* Store on screen                   */
  rts                     /* To Caller: A0=screenaddr,vidloadflg set to -   */
/* *****END C-10/11 'DE-1' SUBROUTINE ************************************* */


/* ************************************************************************ */
/* **** C-10 'DE-2' SUBROUTINE-'VMOVEVIDCLRS' TO/FROM DELITEPR0GRAM ******* */
/* ************************************************************************ */
/* ENTER:'vidloadflg= + then upload, =- then download, =0 Palno inquiry     */
/*       'depalno'=   '0x0x', the x=0 to 15 (actual DEpal Array in use)     */
/*       'vidclrs'=   storage of array'0'address...16 sixteen word arrays   */
/*       'depaladdr'= storage of DE program pal store area                  */
/* CHGS:  D0-D7,A1-A3                                                       */
/* EXIT: 'vidloadflg= cleared, if was '0' then D0 = depalno returned (0-15) */
/* ************************************************************************ */
vmovevidclrs:
  move    depalno(A4),D0               /* Get 0x000x pal array no           */
  and     #0x0F,D0                     /* Mask off nonsignif. left 12 bits  */
  tst     vidloadflg(A4)               /* EXIT:only if depalno inquiry...   */
  beq     voutvidsub                   /* ....with D0= depalno (0-15)       */
  movea.l vidclrs(A4),A1               /* Get array base '0' address        */
  movea.l depaladdr(A4),A2           /* Also DEstore area(no chge to Z,etc) */
  bmi     vdwnvidclrs         /* TO DOWNLOAD: into DEpal arrays (branch) */
                              /* UPLOAD:into DEprgm,set system para.     */
  lsl     #5,D0                        /* Multiply by 32 bytes for ea.array */
  movem.l 0(A1,D0.w),D1-D7/A3          /* A1/D0=base+shifted Palnos.        */
  movem.l D1-D7/A3,0(A2)               /* A2=DE pal store area...           */
  move.l  A2,0x045A                    /* Set system vidaddr update flg     */
  bra     voutvidsub                   /* UPLOAD,sys set end= return        */

 vdwnvidclrs:                 /* DOWNLOAD: Get from DE its latest colors    */
  lsl     #5,D0               /* Multiply by 32 bytes for ea.array          */
  movem.l 0(A2),D1-D7/A3      /* A2=DEpal store area                        */
  movem.l D1-D7/A3,0(A1,D0.w) /* New clr paletted in appropriate array      */
                              /* N.B. Max of 16 used beforerecycling occurs */
 voutvidsub:
  clr     vidloadflg(A4)      /* Return to caller with D0= 0-15 depalno     */
  rts                         /*    or =offset into vidarray                */
/* ********* END C-10 'DE_2' SUBROUTINE *********************************** */



/* ************************************************************************ */
/* **** C-06 SUBROUTINE TO CHECK SCREEN IDS: Needs A0=scrnaddr ************ */
/* ************************************************************************ */
/*      EXIT:scrnidflg=1=same screen,prgmidflg=1= PLUS screen               */
/*           D3=scrn depalno:D1=oldscrnid:D0=new scrnid                     */
/*           'newscrnid' if Plus                                            */
/*  -32/-64 = 'scrnid', -96/-128 = 'prgmid', -160 = 'widpalno' from +32000  */

vcheckids:                    /*PLUS is checked first........       */
  move    32000-128(A0),D0    /* prgmid test...upper 8bits          */
  move.b  32000-96(A0), D0    /* lower 8                            */
  and.w   #0xfffc,D0          /* C-23:last two bits kept for future ids  */
  cmp.w   prgmid(A4),D0       /*                                    */
  beq     vokisprgm           /*                                    */
  clr     prgmidflg(A4)       /* Not a PLUS prgm screen...          */
  clr     scrnidflg(A4)       /* .....not the same                  */
  move    scrnid(A4),D1       /* old scrnid mvd to D1 for cmpatibility     */
  clr     newscrnid(A4)       /* C-07 DEGASmode has no id                  */
  move.l  32000-6(A0),D2      /* C10/11 Get DEid frm scrn corner(if there) */
  cmp.l   thedeid(A4),D2      /* Is it a deid #'d screen already??         */
  beq     vyesdeid            /* YES: Branch then...                       */
  clr     adeidflg(A4)        /* NO:  Ensure its off                       */
  rts                         /* to caller: NO DEid                        */

  vyesdeid:
  move    #1,adeidflg(A4)     /* Set DE flag to 'on'                       */
  move    32000-2(A0),D3      /* Fetch screen Palno and                    */
  andi.w  #0x000F,D3          /* Mask it off                               */
  move    D3,newdepalno(A4)   /* Update to new number                      */
  rts                      /* ******END C-10/11****** */

vokisprgm:
  move    #1,prgmidflg(A4)    /* C-43a1: PLUS flgis checked first........   */
  clr     adeidflg(A4)        /* C-43a1: Ensure its off
*/

  move    32000-64(A0),D0     /* Get NEW scrnid                             */
  move.b  32000-32(A0),D0     /* ...lower 8 bits                            */
  move    scrnid(A4),D1       /* Now is "old" scrn id                       */
  move    #1,scrnidflg(A4)    /*                                            */
  cmp.w   D0,D1               /* Compare screen ids                         */
  beq     vsamescrnid         /* Its the same screen....                    */
  clr     scrnidflg(A4)       /* .....not the same                          */
  move    D0,newscrnid(A4)    /* C-07 New scrn id replaced                  */
  rts                         /* N.B. 'scrnid' is NOT updated here          */
 vsamescrnid:                 /* perhaps in Dwn/up load hbl routine         */
  rts                         /* ....with scrnidflg set                     */

/* ********* C-06 END SUBROUTINE vcheckids ** END: NEW23a 12/32/88***** */
/* ************************************************************************ */
     bra     vblank        /* ALL Dummy code here ..... */
     bra vblroutine
     bra     basereg4
     bra vblroutine
     bra flipw

/* --- MASTER ENTRY POINT FOR VERTICAL BLANK SYS. JUMP */
vblank:
     movem.l D0-D7/A0-A6,-(A7)          /* C-27 A6 */

 basereg4:
     move.l #0x52656734,A4

/* *****DEBUGVERTICAL Routines: C-45a5 all **** */
     tst          rom12flg(A4)
     bne          vdebug12
     move.l     #0x00FCA4A0,0x6c82          /* 'RTS'Rom vector,sometimes
Corrupts      */
     bra      endvdebug
  vdebug12:
     bra          endvdebug                    /* TEST-TEMP:KEEP until code
foundinRom */
     move.l     #0x00000000,0x0000
endvdebug:
/* ****END: DEBUGVERT ROUTINES ******** */

     bra      tonewcode                         /* C-28 Entry point of newcode
*/
vblroutine:
      nop

 flipw:      move.w     0x12345,D0
     bchg     #0,D0
     bne     palad1
palad0:     move.l      0x12345,A0
     bra     flipreset
palad1:     move.l  0x12345,A0

flipreset:
     move.w     D0,0x12345678
     move.w     D0,D2
     move.l  0x44e,A3
  editwind: cmpa.l #0x12345,A3
     beq editscreen

     move.l     A3,workscrn(A4)
     move.w  #0,editsw(A4)

     lea.l   32(A0),A1

     clr     D0     /* C */
     move.b     1(A1),D0
     move     0(A0),back1grd(A4)

     move.l     0(A0),D2
     and.l     #0x07770000,D2
     lea.l     backgrdclr,A3
     move.l     D2,2(A3)
     movem.l     (A0),D1-D7/A2          /* C-R */
     movem.l     D1-D7/A2,0xff8240

tpal3: move.l #0x0faded,A0
      move.l   A1,(A0)
/* C */
vstartplus:                              /* C-28: Possible Entry newcode
routines ... */
     move     loadflg(A4),D1          /* C-44a1: up/dwnload next Hbl?
*/
     cmpi     #-10,D1                    /* C-44a1: Enable downloads...
*/
     bne          vnormalhbl               /* C-44a1 No. bypass then...
*/
     moveq     #2,D0                    /* C-41,YES...set at 2 rasters width
*/
  vnormalhbl:

     lea.l     putin,A1
     move     D0,tpalwidth(A4)
     move.w     D0,2(A1)
     move.b     #0x0,0xfffa1b
   verify:
     move.b  D0,0xfffa21
  putin: cmpi.b #0x13,0xfffa21
     bne verify
     move.b #0x08,0xfffa1b
vblfrmint24:          /* C-1E int24 routine restart */

     andi.b     #0xdf,0xfffa09
     move.l     #0xfffa23,A0
     andi.b     #0x8f,0xfffa1d
     move.w     #0xd3,D0     /* C-1C frm $C0 to $D3,upped11% */
  vredo: move.b      D0,(A0)
     cmp.b     (A0),D0
     bne     vredo
     ori.b     #0x51,0xfffa1d
     move.w     #0x2222,0xE42          /* C-1C frm2222 */
      bclr     #5,0xfffa11
     ori.b     #0x20,0xfffa09

 vblout:
     movem.l (A7)+,D0-D7/A0-A6     /* C-27 */
     dc.w 0x4ef9
    vbsave: dc.w  0x1111
     dc.w  0x1111

editscreen:   /* EDITSCREEN ROUTINE ************************************* */

     move.b  #0x0,0xfffa1b
     bclr    #5,0xfffa11
     ori.b     #0x20,0xfffa09
     tst          editsw(A4)
     beq          editfirstpass

     tst          degasmode(A4)     /* P-stop flash */
     bne          vdegas_on     /* allow degas */
     clr.l     0x45a
 vdegas_on:

     move.l     #0xff8240,A2
     move.l  subpal0(A4),A1
     tst.w     D2
     beq     thissubpal
     move.l  subpal1(A4),A1
thissubpal:
     movem.l (A1),D0-D7
     tst     subpalno(A4)
     beq     e1stpal
     swap     D7
     movem.l     D0-D6,2(A2)
     move.w     D7,0xff825E     /* C */
     bra vanewhue
 e1stpal: movem.l D0-D7,(A2)

 vanewhue:     /* CT9e */
     move     newclr(A4),D0
     cmpi     #1,D0               /* allnewhues from No.1clr click */
     bne     vblout
     tst     huesw(A4)
     beq     vblout
     tst     satsw(A4)
     bne     vblout
     tst          priorclr(A4)          /* if priorclr was=0 then no new hues
*/
     beq      vblout
     move     #1,anewhue(A4)
     bra      uwason
     vbackfhue:
     clr          anewhue(A4)
     bra      vblout

editfirstpass:
      move.w     0x000026e2,D0               /* C-45a2: Chges with ROMS102 */

     cmpi     #195,D0
     bcs       ve0
     move     #195,D0
     ve0:
      move.l     paladdr0(A4),A0
     move.l     A0,subpal0(A4)
     move.l     paladdr1(A4),subpal1(A4)
     move.l     paladdr2(A4),subpal2(A4)
     move     #1,rgb_update(A4)
     clr.l     D1

     tst          huesw(A4)
     bne          huevblank

     cmp.b     33(A0),D0
     bcc       subpal02
/* subpal0: */
     move.l  A0,0x045a
     move     #0,subpalno(A4)
     bsr          changeclr
     bra       vblout2

subpal02:
     cmp.b     65(A0),D0
     bcc       subpal03
     move.w     #34,D1
     moveq     #1,D2
     bra       vblout3
 subpal03:
     cmp.b     97(A0),D0
     bcc          subpal04
     move.w     #66,D1
     moveq     #2,D2
     bra       vblout3
 subpal04:
     cmp.b     129(A0),D0
     bcc          subpal05
     move.w     #98,D1
     moveq     #3,D2
     bra       vblout3
 subpal05:
     cmp.b     161(A0),D0
     bcc          subpal06
     move.w     #130,D1
     moveq     #4,D2
     bra       vblout3
 subpal06:
     cmp.b     193(A0),D0
     bcc      subpal07
     move.w     #162,D1
     moveq     #5,D2
     bra       vblout3
 subpal07:
     cmp.b     225(A0),D0
     bcc      subpal08
     move.w     #194,D1
     moveq     #6,D2
     bra       vblout3
 subpal08:
     cmp.b     257(A0),D0
     bcc      subpal09
     move.w     #226,D1
     moveq     #7,D2
     bra      vblout3
 subpal09:
     cmp.b     289(A0),D0
     bcc      subpal10
     move.w     #258,D1
     moveq     #8,D2
     bra      vblout3
 subpal10:
     move.w     #290,D1
     moveq     #9,D2
     bra      vblout3

vblout3:
     add.l     D1,subpal0(A4)
     add.l     D1,subpal1(A4)
     add.l     D1,subpal2(A4)
     move     D2,subpalno(A4)

     move.l     subpal0(A4),A2
     movem.l     (A2),D0-D7
     swap     D7
     movem.l     D0-D6,0xff8242
     move.w     D7,0xff825E
     bsr          changeclr      /* newclr? */

vblout2:
     move    #1,editsw(A4)
     bra          vblout
/* END VBLANK */

/* Moustrap14 Code */

doittoit14:
     tst.b     0x27e7
     bne          here14

     movem.l     D0-D7/A0-A6,-(A7)               /* C-41b To A6 from A7
*/
regbas15: move.l #0x12345678,A4

  tst     newdeflg(A4)  /* TESTY3:Check if address gotten
*/
  beq     leave15       /* This waits for vidpaladdr in DEprgm on startup
*/

/* *****DEBUG RESTORE TRAP RETURN WORKVECTOR: C-45a8 **** */
 bra  mnotwrkscrn    /* ****************TEMP-TEST  BYPASS ***************** */
      move.l     editscrn(A4),D0               /* Test for work mode
*/
      cmp.l     0x44e,D0
      beq        mnotwrkscrn
    tst          devectorflg(A4)
    beq        mnotwrkscrn                    /* Not found...so dont refresh,
bypass     */
    movea.l vectoraddr(A4),A1          /* DE temp store address
*/
    move.l     workvector(A4),D0          /* Insure got addr on start up
*/
    bne          mhavevector                    /* Yup, we got it already
*/
    move.l     (A1),D0
    move.l     D0,workvector(A4)          /* Get Work vector...
*/
  mhavevector:
    move.l     D0,(A1)                         /* Plug it back in DE temp store
addr     */
 mnotwrkscrn:
/* *****END:DEBUG TRAP C-45a8 **** */

/* 0.01 *****DEBUG TRAP RETURN WORKVECTOR: C-45a9 **** */
/*      bra          mnovectors    ***************** TEST-TEMP ********* */

     tst          gotvectflg(A4)               /* Turns off this routine, set
below      */
     bne          mnovectors                    /* Ok,got it... */
    tst          devectorflg(A4)               /* SET in init...only v.1.0 now
*/
    beq          mnovectors                    /* Not found...so dont install
NewCode     */
    move.l     workvector(A4),D0          /* Already got addr on start up?
*/
    bne          mnovectors                    /* Yup, we got it already,so
bypass          */

    movea.l editscrn(A4),A0               /* In Work Mode?
*/
    cmpa.l  0x044E,A0
    beq           mnovectors                    /* Need work mode: Bypass
*/

    movea.l vectoraddr(A4),A0          /* DE temp store address
*/
    move.l     (A0),D0
    move.l     D0,workvector(A4)          /* Got Work vector...
*/
     lea.l     newcode1,A0
     move.l     D0,2(A0)                    /* Store Work vector in NewCode
Routine */
     lea.l     newcode,A0                    /* Get NewCode start address
*/
     movea.l vectorcode(A4),A1          /* Get DE code startaddress
*/
     move     #0x4EF9,(A1)+               /* Put JMP op code     in DE program
code     */
     move.l     A0,(A1)+                    /* Plug NewCode start addr in DE
Code       */
     lea.l     newcode3,A0                    /* Get NewCode return point
*/
     move.l     A1,2(A0)                    /* Plug DE code return address
*/
     move     #1,gotvectflg(A4)          /* Turns off this routine
*/

 mnovectors:
/* *****END:DEBUG TRAP C-45a9 ****    */

     clr.l     D0
     move.b     2(A0),D0     /* A0 has mouse packet addr */
   ext.w    D0
 yvector:                    /* C-45a2: provides for ROM102 x chge */
     add.w     0x26e2,D0     /* ysum saved */
     bmi          leave15
     move     D0,ysum(A4)

     clr.l     D3          /* xsum saved */
     move.b     1(A0), D3
   ext.w     D3
 xvector:                    /* C-45a2: provides for ROM102 x chge */
     add.w     0x26e0,D3
     bmi          leave15
     move     D3,xsum(A4)
     move.l     A0,mousepack(A4)          /* m-addr saved  */

     tst          degasmode(A4)          /* C-300: Allow xyshowin DE mode....
*/
     bne          showifdegas

      tst     editsw(A4)
     bne          buttontst
  showifdegas:                           /* C-300 */
     tst          xysw(A4)     /* showxyvalues */
     beq       frmxyshow
     bra          xyshow
  frmxyshow:

/* MBottom  C-T9E */
     tst          huesw(A4)
     beq     mbnot
     btst.b  #1,(A0)
     beq     mbnot
     move     ysum(A4),D0
     cmpi     #190,D0
     bcs     mbnot
     move     #1,doithue(A4)
     addi     #1,newclr(A4)
     move     newclr(A4),D0
     cmp          ulastclr(A4),D0
     bls     mbok
     move     #2,newclr(A4)
 mbok:     bra     changeclr
      doitfrmchg:
     clr          doithue(A4)
     move     #0xF9,D0
     and.b     (A0),D0
     move.b     D0,(A0)
 mbnot:
/* - */
     tst          degasmode(A4)          /* C-300: Bypass bottomblck if in DE
mode   */
     bne          buttontst               /* C-300 */

     move     ysum(A4),D0
     cmp.w     #197,D0                    /* C-42b: Chged from 196 */
     bcs          leave15          /*  there?no,...out */

/*     move.w     0x26E2,D0   * If used must chg with 102 ROMS *
     cmp.w     #196,D0          * C-TL removes ability to go below198(chged to
196) *
     bhi          leave15
*/
     clr.b     2(A0)
     bra          leave15

buttontst:                    /* C-9X */
     btst.b  #0,(A0)
     beq          bntst1
     clr          rgb_update(A4)
     clr          copypalsw(A4)
     bra          leave15

 bntst1:          /* C-9X */
     btst.b  #1,(A0)
     beq          leave14

     bra   mousecode14

leave14:
     tst          rgb_update(A4)          /* prints */
     beq          leave15

     tst          copypalsw(A4)     /* C-P9 'copypal'tst prevents #update */
     beq          mnocpypal
     cmpi     #167,ysum(A4)
     bcs          mnocpypal
     cmpi     #489,xsum(A4)
     bhi          leave15
     tst          resolution(A4)     /* isitlowres? */
     bne          mnocpypal
     cmpi     #290,xsum(A4)
     bhi          leave15

mnocpypal:
     clr          copypalsw(A4)
     clr          lastcpy(A4)               /* Cend */

     move.l     editscrn(A4),D0          /* C-45a1: Print avoided if workscrn
*/
     cmp.l     0x44E,D0               /* C-45a1
*/
     bne          mrgbnodo               /* C-45a1
*/

     move     #1,chgclrsw(A4)
     bsr          print3rgb
  mrgbnodo:                              /* C-45a1 */
     clr          chgclrsw(A4)
     clr          rgb_update(A4)

vprintpal:  /* fr 'palroutine,copypal', BEG-->D3  */
     move.l  editscrn(A4),D3          /* C-45a1
*/
     cmp.l     0x44E,D3               /* C-45a1: Avoid if on workscrn */
     bne          leave15                    /* C-45a1
*/

     clr          D3               /*  P1: */
     clr          D5
     move     subpalno(A4),D5
     tst          huesw(A4)
     beq          mnohue
     move     osubpalno(A4),D5
  mnohue:
     tst          D5
     bne           vnotfirst
     move     palwidno(A4),D3               /* 1to10 */
     bra          vfirstpal
 vnotfirst:
     move.l     subpal0(A4),A2
     move.b     -1(A2),D3          /* D3 has0or# C-T5 */
     addi     #1,D3          /* C-R0A,d ADJ */
 vfirstpal:     move.l     prtbegaddr(A4),D1
     clr.w     D4
     bsr          print3char

 vprtpalno:          /* Cp9X */
     tst          lastcpy(A4)          /* C-P8 */
     beq          vnotcpy
     move     lastcpy(A4),D5
 vnotcpy:                         /* End P8 */

     move     D5,D3          /*  P1:PAL=>D5 */
     move.l     prtpaladdr(A4),D1
     clr.w     D4
     bsr          print1char

leave15:
     clr.w  newclrsw(A4)
     movem.l (A7)+,D0-D7/A0-A6          /* C-41b: To A6 from A7 */

here14:     jmp  0x12345678
/* EXIT -->system mouse handler */


/* ------XY SHOW------------ */
xyshow:

     move     xsum(A4),D3
     move.l     #0,D1
     clr.l     D2
     clr.l     D4
     bsr          print3char           /* X */

     move.l     #0,D3
     move.w     ysum(A4),D3
     clr.l     D2
     clr.l     D4
     bsr          print3char          /* Y */
xyexit: bra frmxyshow

/* -- */

mousecode14:
     move     ysum(A4),D0
     cmpi.w      #169,D0
     bcc          rgbhuecopy

     tst          degasmode(A4)          /* C-300:Bumpout if in DEmode  */
     bne      leave15                    /* C-300 */

     cmpi.w      #29,D0
     bhi          leave15          /* allto15 */

     cmpi.w      #14,D0
     bcs     leave15
     bra     colorbox


/*  RGB-HUE-CPY-PALBEG Tree */
rgbhuecopy:
     move     xsum(A4),D1          /* X,Y */
     move     ysum(A4),D0
     tst          resolution(A4)
     bne          rmedres

     add          #204,D1          /* lowX upped 204 */

rmedres:
/* --- C-300 Chge Start: allows xytoggle/show in DEmode------------ */
     tst          degasmode(A4)          /*  Bumpout if in NOT DEmode
*/
     beq      rnormalrun               /*  ...bypass to normal run
*/

     cmpi.w     #457,D1                    /* C-41b:Turn off Degasmode switch
*/
     bcs          leave15                    /* C-41b */
     move.l     editscrn(A4),D2
     cmp.l     0x44E,D2               /*  Extra tst now needed
*/
     bne          leave15                    /*  If NOT Editscrn=then getout...
*/
     cmpi.w     #490,D1
     bcc          rtstxychge               /* C-41b */
     move     #1,wasdegasflg(A4)  /* C-41b Turns on Plus mode switch   */
     bra          leave15                    /* C-41b and then exits...
*/
 rtstxychge:                        /* C-41b */

     cmpi.w     #178,D0
     bcs      leave15
     cmpi.w     #187,D0
     bcc      leave15                    /* C-300: OK now allow xysw toggle!
*/
     move     xysw(A4),D0
     bchg      #0,D0
     move     D0,xysw(A4)
     bra          leave15
/* --END of C-300 chg----------- */

  rnormalrun:   /* C-300 */
     cmpi.w     #403,D1
     bcs          leave14


     cmpi.w     #457,D1
     bcs          rgbroutine

     cmpi.w     #490,D1
     bcs          palroutine

     cmpi.w     #178,D0
     bcs      copyroutine

     cmpi.w     #187,D0
     bcc      hueroutine

     move     xysw(A4),D0
     bchg      #0,D0
     move     D0,xysw(A4)

     bra          leave14

/* PAL-BEG C-P3 */
palroutine:
     tst           huesw(A4)
     bne          leave15
     move     #1,dirtyflg(A4)          /* C-45a3: Now set for upload */
     clr     D1
     clr     D2
     move.l     subpal0(A4),A1
     move.b     -1(A1), D1          /* C */
     move     subpalno(A4),D3
     tst          D3
     bne          palnot0                              /* TEST: CHECK THESE
BRANCHES.... */

     beq          pchgwidth     /* new move T9J */

     move     palwidth(A4),D1  /* old  C-Th */
palnot0:
     move     ysum(A4),D0          /* Y */
     cmpi.w     #187,D0
     bcs          raise_beg
     addi     #2,D1
raise_beg:
     subi     #1,D1

     cmp     #196,D1          /* C-R */
     bhi     palexit
     tst     D3
     beq     pchgwidth

     move.b     -33(A1),D0          /* subpal line#s */
     move.b      31(A1),D2
     cmpi.b     #1,D3
     bne          palnot1          /* #1=adjfor'0' */
     move.w     #0,D0
 palnot1:
     addi     #3,D0          /* low/upperbound=+2:-2: chgd to 3 */
     cmp.b     D0,D1
     bcs          palexit
     subi     #3,D2
     cmp.b     D1,D2
     bcs          palexit

     move.b     D1,-1(A1)
     move.l     subpal1(A4),A1
     move.b     D1,-1(A1)
     move.l     subpal2(A4),A1
     move.b     D1,-1(A1)
     bra          vprintpal

palexit:   bra leave14

pchgwidth:     /* bra          notnumber      TEMP */

/*  ----  */
     move          palwidno(A4),D1
     move          ysum(A4),D0          /* Y */
     cmpi.w     #187,D0
     bcc          plower_it
     addi          #2,D1
plower_it:
     subi     #1,D1
     bne          pc0not                    /* =0 ? */
     move     #10,D1                         /* recycle to 10 */
 pc0not:
      cmpi     #10,D1
      bls          pcinrange                    /* also catches neg */
      move     #1,D1                         /* recycle to 1 */
 pcinrange:          /* between 1-10 */
      move          D1,palwidno(A4)
     move.l     nplwidar(A4),A0          /* array is pallette0,230th */
     lsl          #1,D1                    /* dble for word size */
     move           0(A0,D1.w),D0               /* Get width from array indexed
*/
     move           D0,D1
/* --- */

     cmpi          #19,D1                    /* insure not too low */
     bcc          pcminwidth
     move          #19,D1                    /* TEST: WHY #19 AS A MINIMUM
HERE??? */
 pcminwidth:
     cmpi          #195,D1
     bcs          pcmaxwidth
     move          #196,D1
 pcmaxwidth:               /* width in bounds:19-195 */
     move          D1,palwidth(A4)
     move          D1,D3
     move.l     #1,D0                /* start'lastpal'index */

     move          lastpal(A4),D4          /* C-9TK */
     move          #33,D2
     move.l     paladdr0(A4),A0
     move.l     paladdr1(A4),A1
     move.l     paladdr2(A4),A2
 ploopal:
     move.b     D3,0(A0,D2.w)
     move.b     D3,0(A1,D2.w)
     move.b     D3,0(A2,D2.w)

     cmp          D0,D4               /*  C-T9KA/L putblack in#1-15new pal */
     bne          pcnotnew               /* N.B.D2 is odd so.... */
     move.l     A0,A3
     bsr          pcblack
     move.l     A1,A3
     bsr          pcblack
     move.l     A2,A3
     bsr          pcblack
     bra          pcnotnew
 pcblack:     /* oddis even here! */
     clr.w     29(A3,D2.w)
     clr.l     25(A3,D2.w)
     clr.l     21(A3,D2.w)
     clr.l     17(A3,D2.w)
     clr.l     13(A3,D2.w)
     clr.l     9(A3,D2.w)
     clr.l     5(A3,D2.w)
     clr.l     1(A3,D2.w)
     rts
pcnotnew:          /* C-9tKa end */

     cmpi     #196,D3
     bcs          pnotyet196
     beq          pyes196
  p10thpal:
     move     #196,D3
     bra          ploopal
  pnotyet196:
      add     D1,D3
     add     #32,D2
     cmpi     #9,D0
     beq     p10thpal
     add     #1,D0
     bra     ploopal

 pyes196:
     move     D0,lastpal(A4)
     addi     #1,D2
     move     #7,D4          /* loop No. */
     move.l     back1grd(A4),D0
     move     back1grd(A4),D0

  pchglast:          /* fill with backgrdclr */
       move.l     D0,0(A0,D2.w)
     move.l     D0,0(A1,D2.w)
     move.l     D0,0(A2,D2.w)
     addi     #4,D2
     dbf          D4,pchglast

     clr     D4
     bra     vprintpal

/* COPY Pals */
copyroutine:
     tst     huesw(A4)
     beq     cphh
     bra     satroutine

     cphh:
     move.l     #0,A0
     move     subpalno(A4),D0
     bne     cnoneed2
     move.l     #2,A0               /* '0'adj */
  cnoneed2:
     move     #1,copypalsw(A4)
     cmp          lastcpy(A4),D0
     bhi          cnew
     move     lastcpy(A4),D0
 cnew:
     cmp     lastpal(A4),D0
     bcs     cpyok
     clr     lastcpy(A4)
     clr     copypalsw(A4)
     bra     leave15
 cpyok:
     move      D0,lastcpy(A4)
      cmpi      #1,D0
     bhi          ccheck5
     beq          ccheck1

     bsr     c0
     bsr     to01
     bsr     c1
     bsr     to01
     bsr     c2
     bsr     to01
     bra     cpexit
ccheck1:
     bsr     c0
     bsr     to02
     bsr     c1
     bsr     to02
     bsr     c2
     bsr     to02
     bra     cpexit
ccheck2:
     bsr     c0
     bsr     to03
     bsr     c1
     bsr     to03
     bsr     c2
     bsr     to03
     bra     cpexit
ccheck3:
     cmpi     #3,D0
     blt     ccheck2
     bgt     ccheck4

     bsr     c0
     bsr     to04
     bsr     c1
     bsr     to04
     bsr     c2
     bsr     to04
     bra     cpexit
ccheck4:
     bsr     c0
     bsr     to05
     bsr     c1
     bsr     to05
     bsr     c2
     bsr     to05
     bra     cpexit
ccheck5:
     cmpi     #5,D0
     bhi     ccheck7
     bcs     ccheck3

     bsr     c0
     bsr     to06
     bsr     c1
     bsr     to06
     bsr     c2
     bsr     to06
     bra     cpexit
ccheck7: cmpi     #7,D0
     bgt     ccheck8
     beq     ccheck7a

     bsr     c0
     bsr     to07
     bsr     c1
     bsr     to07
     bsr     c2
     bsr     to07
     bra     cpexit
ccheck7a:
     bsr     c0
     bsr     to08
     bsr     c1
     bsr     to08
     bsr     c2
     bsr     to08
     bra     cpexit
ccheck8:
     bsr     c0
     bsr     to09
     bsr     c1
     bsr     to09
     bsr     c2
     bsr     to09
     bra     cpexit

c0:     move.l     paladdr0(A4),A2
     move.l     subpal0(A4),A1
     bra          ca0
c1:     move.l     paladdr1(A4),A2
     move.l     subpal1(A4),A1
     bra          ca0
c2:     move.l     paladdr2(A4),A2
     move.l     subpal2(A4),A1

ca0:
     adda.l     A0,A1
     movem.l (A1),D0-D7
     swap     D7
     rts

to01:
     movem.l     D0-D6,34(A2)
     move     D7,62(A2)
     rts
to02:
     movem.l     D0-D6,66(A2)
     move     D7,94(A2)
     rts
to03:
     movem.l     D0-D6,98(A2)
     move     D7,126(A2)
     rts
to04:
     movem.l     D0-D6,130(A2)
     move     D7,158(A2)
     rts
to05:
     movem.l     D0-D6,162(A2)
     move     D7,190(A2)
     rts
to06:
     movem.l     D0-D6,194(A2)
     move     D7,222(A2)
     rts
to07:
     movem.l     D0-D6,226(A2)
     move     D7,254(A2)
     rts
to08:
     movem.l     D0-D6,258(A2)
     move     D7,286(A2)
     rts
to09:
     movem.l     D0-D6,290(A2)
     move     D7,318(A2)
     rts

cpexit:
     addi     #1,lastcpy(A4)
     move     lastcpy(A4),D0
     cmp     lastpal(A4),D0
     bcs     ckeepon
     clr     lastcpy(A4)
     clr     copypalsw(A4)
     bra     leave15
 ckeepon:
     move     #1,rgb_update(A4)
     bra     vprtpalno

/* ENDCOPYsglpal */

/* SATROUTINE:C-T9 */
satroutine:
     tst          satsw(A4)
     bne          hueroutine
     move     #1,satsw(A4)
     move     #12,subpalno(A4)
     move.l     satpal0(A4),A0
     move.l     satpal1(A4),A1
     move.l     satpal2(A4),A2
     move     #0x01,D0          /* 0->1 */
     bra          ufromsat
 satfrmhue:     /* rgbsinD1-3,D4smallest */
     move     #1,newclr(A4)
     clr     D6
     clr     D7
     bsr     uchangeclr          /* showselectedclr */
     bsr     ugetsmall

/* getlargest->D6 */
     move     D1,D6
     cmp          D2,D1
     bcc          s1larger
     move     D2,D6
 s1larger:
     cmp     D3,D2
     bcc     slargest
     cmp     D3,D1
     bcc     slargest
     move     D3,D6
 slargest:     /* D6=largest */

/* getMiddleVAL->D5 */
     move     D1,D5
     add     D2,D5
     add     D3,D5
     sub     D4,D5
     sub     D6,D5

/* Top compare #->D0 */
     cmp          D4,D5
     bne          snotequals
     move     D6,D0
     bra          satadj
 snotequals:
      move     D5,D0


/* AdjustD1-D3;if=top,okelse->1 */
satadj:
     cmp          D0,D1
     bcc          satok1
     move     #1,D1
 satok1: cmp     D0,D2
     bcc          satok2
     move     #1,D2
 satok2: cmp     D0,D3
     bcc          satok3
     move     #1,D3
 satok3:     /* D1-3areadj,D0=top */

/* SATloop:usesA0-2; */
     subi     #1,D0
     move     D0,D5      /* topcutoff */
     subi     #1,D0     /* loop# */
     bpl     stopok
     clr     D0
  stopok:
     move     #2,newclr(A4)
 satloop:
     clr     D6
     clr     D7
      bsr     uchangeclr
     cmp     D5,D1
     bcc     s0
     addi     #1,D1
  s0: cmp     D5,D2
     bcc          s1
     addi     #1,D2
  s1: cmp     D5,D3
     bcc          s2
     addi     #1,D3
  s2: addi     #1,newclr(A4)

     dbf          D0,satloop
     bsr          unewdone
     move     oldclr(A4),newclr(A4)
     bra          leave15
/* End */

/* HUEROUTINE */
hueroutine:
/*     tst          clr0flag(A4)     * C-T9L AVOID allclr0 */
/*     bne     leave15               * nonselective of 1or2 in flag */

     tst          satsw(A4)
     bne          uwason

     tst          huesw(A4)
     bne          urestore

     move     #1,huesw(A4)
     move     newclr(A4),oldclr(A4)
     move     subpalno(A4),osubpalno(A4)

 uwason:
     move     #11,subpalno(A4)
     move.l     hupal0(A4),A0
     move.l     hupal1(A4),A1
     move.l     hupal2(A4),A2
     move     #0xB000,D0          /* 0->neg */
     tst          satsw(A4)
     beq          ufromsat
     move     #0xf000,satsw(A4)
     bra          u2

 ufromsat:     /* C-T9A */
     clr.l     D1
     clr.l     D2
     clr.l     D3
     move     red(A4),D1
     bne          u0
     move     D0,D1
 u0:
     move     grn(A4),D2
     bne          u1
     move     D0,D2
 u1:
     move     blu(A4),D3
     bne          u2
     move     D0,D3
 u2:
     move.l     subpal0(A4),A3     /* &restores clrfrmSat */
     move.b     -1(A3),-1(A0)
     move.l     A0,subpal0(A4)
     move.l     A1,subpal1(A4)
     move.l     A2,subpal2(A4)
     tst     satsw(A4)
     bmi     u2dgo
     bne     satfrmhue          /* rtrn to'SAT',CT9 */

     bsr     ugetsmall
     bsr     unewhues

     tst     anewhue(A4)          /* CT9F */
     bne     vbackfhue

 u2dgo:     move     oldclr(A4),newclr(A4)
     clr     satsw(A4)
     bra     leave15

 urestore:
      clr          huesw(A4)
     move     #1,rgb_update(A4)
     move     osubpalno(A4),subpalno(A4)

     move     oldclr(A4),D0     /* C-T9Cforcecheckmove */
     move     D0,newclr(A4)
     move     D0,D1
     move.l     mousepack(A4),A0
     move.b     #4,1(A0)
     move.b     #4,2(A0)
     lsl     #2,D0
     lsl     #4,D1
     add     D0,D1
     addi     #6,D1
 xvector2:                         /* C-45a2:Provide for ROM102 x chge */
     move     D1,0x26E0
 yvector2:                         /* C-45a2:Provide for ROM102 y chge */
     move     #22,0x26E2

     move     osubpalno(A4),D0
     lsl          #5,D0
     addi     #2,D0               /* C-T5 */

     tst          osubpalno(A4)
     bne          unotsub0
     subi     #2,D0          /* C-T9LC adj left for pal0 */
     unotsub0:

     move.l     paladdr0(A4),A0
     adda.l     D0,A0
     move.l     A0,subpal0(A4)
     move.l     paladdr1(A4),A0
     adda.l     D0,A0
     move.l     A0,subpal1(A4)
     move.l     paladdr2(A4),A0
     adda.l     D0,A0
     move.l     A0,subpal2(A4)

     bra          leave15

/* GETSMALLEST:rgbsD1-3,sm=D4;'0's=-15 */
ugetsmall:
     move     D1,D4
     cmp          D1,D2
     bcc          u1smaller
     move     D2,D4
 u1smaller: cmp     D2,D3
     bcc     usmallest
     cmp     D1,D3
     bcc     usmallest
     move     D3,D4
 usmallest: /* inD4 */
     rts

/* GET NEWHUES:D1-3=RGBs;D4=smallest */
unewhues:
     move     #15,D5
     clr     D6
     clr     D7
unewhues1:
     move     D4,D0
     subi     #1,D0
     move     #1,newclr(A4)
     bsr          uchangeclr
     move     #2,newclr(A4)
     sub     D0,D1
     sub     D0,D2
     sub     D0,D3
     clr.l     D4
  unewloop0:
     clr     D6
     clr     D7

     addi     #1,D1
     bmi     u1is0
     cmp     D5,D1
     bge     unewdone
     u1is0:
     addi     #1,D2
     bmi     u2is0
     cmp     D5,D2
     bge     unewdone
     u2is0:
     addi     #1,D3
     bmi     u3is0
     cmp     D5,D3     /* C-T6B */
     bge     unewdone
     u3is0:
     bsr          uchangeclr
     addq     #1,newclr(A4)
     bra          unewloop0

uchangeclr:
     move     D1,D4
      bmi     ugrn
     lsr          #1,D4
     lsl          #8,D4
     or          D4,D7
     btst     #0,D1
     beq          urednohalf
     add          #0x0100,D4
  urednohalf:
     or     D4,D6     /* C-T4 */
ugrn:
     move     D2,D4
      bmi     ublu
     lsr     #1,D4
     lsl     #4,D4
     or     D4,D6
     btst     #0,D2
     beq     ugnothalf
     add     #0x0010,D4
  ugnothalf:
       or     D4,D7
ublu:
     move     D3,D4
      bmi     unoblu
     lsr     #1,D4
     or     D4,D6
     or     D4,D7
     btst     #0,D3
     beq          ubnothalfclr
     btst     #0,D2
     bne          ubyesgrn
     addi     #1,D7
     bra          ubnothalfclr
  ubyesgrn:     addi     #1,D6
  ubnothalfclr: nop
  unoblu:     move      D6,(A0)+
     move      D7,(A1)+
     add          D6,D7
     move     D7,(A2)+
     rts

unewdone:      /* clearunused clrs;subpals set */
     move     newclr(A4),D1
     move     D1,ulastclr(A4)
     cmpi     #15,D1
     bcc          unewexit
     clr          D0

 uclearclr:
     clr.l     0(A0,D0.w)
     clr.l     0(A1,D0.w)
     clr.l     0(A2,D0.w)
     cmpi     #14,D1
     bcc          unewexit
     addq     #4,D0
     addq     #2,D1
     bra          uclearclr
 unewexit:
     rts

/* VBlank addon */
huevblank:
     move     #354,D1          /* 12thpal */
     move     #11,D2
     tst  satsw(A4)
     beq          vblout3
     move     #386,D1          /* 13thpal */
     move     #12,D2
     bra          vblout3


/* RGB ROUTINES D1=X,D0=Y */

rgbroutine:
     move     #1,dirtyflg(A4)          /* C-42b: Set upload flg for chged color
*/
     clr          dirtysw(A4)               /* C-42b: Reset screenbox routine
flg    */

     move     newclr(A4), D2
     lsl.w     #1,D2

     tst          subpalno(A4)
     beq          rgbtis0          /* adjleftallsubpal1-9 */
     tst          newclr(A4)
     beq          leave14          /* EXIT:clr0 only subpal0 */
     sub          #2,D2
rgbtis0:
     move.l     subpal0(A4),A0     /* getindexedaddrforrgbs */
     lea.l     0(A0,D2.w),A0
     move.l     subpal1(A4),A1
     lea.l     0(A1,D2.w),A1
     move.l     subpal2(A4),A2
     lea.l     0(A2,D2.w),A2

     cmpi.w     #187,D0
     bcs          raise_rgb
     bhi          lower_rgb
     bra          leave14

lower_rgb:
     cmpi.w      #419,D1
     bhi          lower_gb
     lea.l     red(A4),A3
     clr          D4
     bra          lower_all
 lower_gb:
     cmpi.w     #437,D1
     bhi          lowerb
      lea.l     grn(A4),A3
     move     #1,D4
     bra          lower_all
 lowerb:
     lea.l     blu(A4),A3
     move     #2,D4
lower_all:
     move.w     (A3),D3
     subq.w     #1,D3
     bpl          oklow          /* frm bge */
     move     #14,D3          /* was clr */
      oklow:      move.w     D3,(A3)
      cmpi.b     #1,D4
     bhi          blutopal
     beq          grntopal
     bra          redtopal

raise_rgb:
     cmpi.w      #419,D1
     bhi          raise_gb
     lea.l     red(A4),A3
     clr          D4
     bra          raise_all
 raise_gb:
     cmpi.w     #437,D1
     bhi          raise_b
     lea.l     grn(A4),A3
     move     #1,D4
     bra          raise_all
 raise_b:
     lea.l     blu(A4),A3
     move     #2,D4
raise_all:
     move.w     (A3),D3
     addq.w     #1,D3
     cmpi.b     #14,D3
     bls          okthis
     move     #0,D3
     okthis:      move.w     D3,(A3)
      cmpi.b     #1,D4
     bhi     blutopal
     beq     grntopal
     bra     redtopal

/* P-chg end */

/*  COLORS toindexedSubpals A0-2;D3=chg */
redtopal:
     move     red(A4),D3
     move     D3,D1          /*  allrgbs */
     lsr     #1,D3
     lsl     #8,D3

     move     (A1), D0
     and     #0x077,D0
     or     D3,D0
     move      D0,(A1)
     btst     #0,D1
     beq     rnothalf
     add     #0x0100,D3
  rnothalf: move     (A0),D0
     and          #0x077,D0
     or          D3,D0
     move      D0,(A0)
     add          (A1),D0
     move      D0,(A2)
     bra          printred

grntopal:
     move     grn(A4),D3
     move     D3,D1
     lsr          #1,D3
     lsl          #4,D3

     move     (A0), D0
     and          #0x0707,D0
     or          D3,D0
     move      D0,(A0)
     btst     #0,D1
     beq     gnothalf
     add          #0x0010,D3
  gnothalf: move     (A1),D0
     and     #0x0707,D0
     or     D3,D0
     move      D0,(A1)
     add     (A0),D0
     move     D0,(A2)
     bra     printgrn

blutopal:
     move     blu(A4),D3
     move     D3,D1
     lsr          #1,D3

     move     (A0), D0
     and     #0x0770,D0
     or     D3,D0
     btst     #0,D1
     beq     bnothalfclr
     move     red(A4),D1
     btst     #0,D1
     bne     byesred
     move     grn(A4),D1
     btst     #0,D1
     beq     byesred
     addq.w     #1,D0

     bra     bnothalfclr
  byesred:     addq     #1,D3
  bnothalfclr: move     (A1),D2
     and     #0x0770,D2
     or     D3,D2
     move      D2,(A1)
     move      D0,(A0)
     add          D2,D0
     move     D0,(A2)
     bra     printblu
/* - */

print3rgb: nop     /* from chgclr; mnocpypal */
printred:
     move     red(A4),D0
     move.l     prtredaddr(A4),D1
     move     redborder(A4),D2
     move     redshift(A4),D4
     bsr     printnumber

     tst          chgclrsw(A4)
     bne     printgrn
     bra     leave14
printgrn:
     move     grn(A4),D0
     move.l     prtgrnaddr(A4),D1
     move     grnborder(A4),D2
     move     grnshift(A4),D4
     bsr     printnumber

     tst          chgclrsw(A4)
     bne     printblu
     bra     leave14
printblu:
     move     blu(A4),D0
     move.l     prtbluaddr(A4),D1
     move     bluborder(A4),D2
     move     blushift(A4),D4
     bsr     printnumber

     tst          chgclrsw(A4)
     beq     leave14
     rts          /* EXIT->colorbox, */

/* PRINTsgle/dble */
printnumber:
     movem.l     D0-D5/A0-A1,-(A7)
     lea.l     numarray(A4),A0
     move     D2,D5
     move     D0,D3
     lsl          #4,D3
     lea.l     16(A0,D3.w),A1
     move.l     0x44E,A0
     adda.l     D1,A0
     move     #7,D2
     tst          D4
     beq     prt8aligned
     sub          #1,D4
     cmpi.b     #9,D0
     bhi          prt8offset
     add          #2,D4
prt8offset:     /* dble"0-14"LowRes */
     clr.l     D0
     move     (A1)+,D0
     lsl.l     D4,D0
     add          D5,D0
     move.l     D0,D1
     lsr          #8,D0

     move.b     D0,0(A0)
     move.b     D0,2(A0)
     tst      resolution(A4)
     bne     prtmoffset
     move.b     D0,4(A0)
     move.b     D0,6(A0)
     move.b     D1,7(A0)
     move.b     D1,9(A0)
     move.b     D1,11(A0)
     move.b     D1,13(A0)

     adda.l     #160,A0
     dbf          D2,prt8offset
     bra     printout
prtmoffset:
     move.b     D1,3(A0)
     move.b     D1,5(A0)

     adda.l     #160,A0
     dbf          D2,prt8offset
     bra     printout

prt8aligned:  /* single"0-9" */
     move.w     (A1)+,D0
     move.b     D0,0(A0)
     move.b     D0,2(A0)
     tst      resolution(A4)
     bne     ppmedres2
     move.b     D0,4(A0)
     move.b     D0,6(A0)
 ppmedres2:
     adda.l     #160,A0
     dbf     D2,prt8aligned

printout:
     movem.l     (A7)+,D0-D5/A0-A1
     rts

/* END */


/* ***********************************************************************  */
/* ******************** PRINTCHAR 0-999 SCREEN ROUTINE *******************  */
/* ***********************************************************************  */
print3char:
     move.l #0,D0
  ploop3:
     subi     #100,D3
     bcs     pendloop3
     addq     #1,D0
     bra     ploop3
  pendloop3:
     add.w      #100,D3
     bsr     printnumber

     btst     #0,D1                    /* adj byt2 location */
     beq     peven3
     addq     #2,D1
     tst          resolution(A4)
     bne     peven3
     addq     #4,D1
 peven3:
     addq     #1,D1

print2char:          /*  10's */
     move.l  #0,D0
  ploop2:     sub #10,D3
     bcs          pendloop2
     addq.l     #1,D0
     bra     ploop2
  pendloop2: add.w #10,D3
     bsr      printnumber

     btst     #0,D1
     beq     peven2
     addq     #2,D1
     tst          resolution(A4)
     bne     peven2
     addq     #4,D1
 peven2:
     addq     #1,D1

print1char:
     move.l     #0,D0
 ploop1:     sub #1,D3
     bcs     pendloop1
     addq.l     #1,D0
     bra     ploop1
  pendloop1:
     bsr     printnumber

     tst     resolution(A4)
     bne     peven0
     add     #4,D1
 peven0:
     add     #4,D1

     rts

/* COLOR BOX:D1=xsum,D2=addbx no,D3=foundbxNo */
colorbox:
     move.w     xsum(A4), D1
     tst          resolution(A4)
     beq     bx_lowres
     cmpi.b     #164,D1      /* Med=4clrs */
     bcc     boxexit
     lsr          #1,D1          /* 1/2adj */
bx_lowres:
     clr          D2
     cmpi     #156,D1          /* Combinesinto1/2 */
     bcs     bxlower8
     subi     #151,D1
     move     #8,D2          /* 8addon */
  bxlower8:
     cmpi     #82,D1
     bcc     bxupper4

/* lower 4 */
     cmpi.b     #44,D1
     bcs     bxlo2

     cmpi.b     #63,D1
     bcc     bx3
     move     #2,D3
     bra     bxgot
   bx3:           move  #3,D3
     bra     bxgot

 bxlo2:          cmpi.b #26,D1
      bcc     bx1
      move     #0,D3
      bra     bxgot
   bx1:          move  #1,D3
     bra     bxgot

/* Top4 ofcollapsedhalf */
bxupper4:
     cmpi     #121,D1
     bcs     bxhilo2

     cmpi     #138,D1
     bcc     bx7
     move     #6,D3
     bra          bxgot
   bx7:     move     #7,D3
     bra          bxgot

 bxhilo2:  cmpi      #101,D1
      bcc     bx5
      move     #4,D3
      bra     bxgot
   bx5:          move #5,D3
     bra     bxgot

bxgot:      /* addin extra 8 */
     add          D2,D3                 /* now0-7,or8-15 */
     move     D3,newclr(A4)
     bsr     changeclr               /* getnew clrsums */

/* print */
     move     #1,chgclrsw(A4)
     bsr     print3rgb
     clr          chgclrsw(A4)
boxexit:
     bra     leave15          /* exitmouscode14 */

/* END */

/* CHANGECLR */

changeclr:
     move     newclr(A4),D3
     clr          clr0flag(A4)          /* C-9TL clear0flag for HUE/SAT */
     move     D3,priorclr(A4)          /* keepold colorif=0 then no newhues */

     lsl.w     #1,D3

     tst          subpalno(A4)
     beq          ch_is0                    /*  adjif notPal0 */
     subi     #2,D3
     tst          D3          /* C-T3 */
     bpl          ch_is0                    /* C-1b from bne...#1now ok */
     rts                                   /* Exit if 0clr is not pal0 */
  ch_is0:

     tst          D3                    /* C-9TL setclr0 flg=2 for HUE/SATin
pal0  */
     bne          ch_0ok
     move     #2,clr0flag(A4)
 ch_0ok:

     move.l     subpal0(A4),A0          /* back to 0+1=sum */
     move     0(A0,D3.w),D1
     move.l     subpal1(A4),A1
     move     0(A1,D3.w),D2
     move     D1,D5               /* CT5 */
     add          D2,D5
     move     D5,clrsum(A4)

     tst          huesw(A4)     /* CT9 HUEin */
     beq     ch_nohue
     tst          newclr(A4)     /* CT9LC exit if clr0 */
     beq     ch_twas0
     move     D1,(A0)
     move     D2,(A1)
     move.l     subpal2(A4),A2
     move     D5,(A2)
     tst          satsw(A4)
     beq     ch_s
     move     D1,-32(A0)
     move     D2,-32(A1)
     move     D5,-32(A2)
 ch_s:     move     subpalno(A4),D0          /* CT9 was11 */
     sub          osubpalno(A4),D0
     lsl          #5,D0
     clr          D3
     sub          D0,D3
     move     oldclr(A4),D0
     lsl          #1,D0
     subi     #2,D0
     add          D0,D3
     move     D1,0(A0,D3.w)
     move     D2,0(A1,D3.w)
     move     D5,0(A2,D3.w)
     ch_twas0:
     tst          doithue(A4)
     bne     doitfrmchg

/* each sum: */
 ch_nohue:
     move      D5,D3
     andi.w     #0x000F,D3          /* all=>F */
     move     D3,blu(A4)

     move     D5,D3
     andi.w     #0x00F0,D3
     lsr.w     #4,D3
     move     D3,grn(A4)

     andi.w     #0x0F00,D5
     lsr.w     #8,D5
     move     D5,red(A4)
     rts


/* ******************* End 'downloadpal' ********** */

thatsall:
     }
}

/* ********************* End assm  ********************************* */


/* ********************ac1d02******* MAIN C PROG ************************* */
/* Init accessory, store accessory title in system's menu bar */
/* then wait for a message using evnt_mesag().  When we get   */
/* an open (message type 40), we ask DEGAS Elite for various  */
/* information, then do our thing.  If DEGAS Elite is not     */
/* running, the accessory goes to sleep until called again    */
/* by waiting for another evnt_mesag.                         */
/**************************************************************/

main()
{
appl_init();

menu_id =menu_register(gl_apid," AC1DA05cut4 ");

while(forever)
 {
 evnt_mesag(mgin);
 if(mgin[0]==40 && mgin[4]==menu_id)
  {
  DE_id=appl_find("DEGELITE");
  if(DE_id<0)
   form_alert(1,no_DEGAS);
  else
   {

/* Make sure Elite's REALLY there */

   mgout[0]=0xDE00;
   mgout[1]=gl_apid;
   mgout[2]=0;
   appl_write(DE_id,16,mgout);

   is_there:
   event=evnt_multi(0x0030,
              -1,-1,-1,
              0,0,0,0,0,
              0,0,0,0,0,
              mgin,
              2000,0,     /* 2000 ms */
              &dum,&dum,&dum,&dum,&dum,&dum);

/* If timer event, DEGAS Elite isn't there, otherwise, look for reply msg */

   if(event & 0x0020)
    form_alert(1,no_DEGAS);     /* Timer timed out -- no Elite! */
   else
    {
    if(mgin[0]==0xDE80)
     do_test();          /* Got reply -- Elite's there! */
    else
     goto is_there;     /* Keep waiting until timer event happens */
    }
   }
  }
 }
}

do_test()
{
register int ix;


/******************** screen addresses ********************/

/* "Requesting Screen Addresses" */

mgout[0]=0xDE00;
mgout[1]=gl_apid;
mgout[2]=0;
appl_write(DE_id,16,mgout);

/* Wait for reply message */

do
 {
 evnt_mesag(mgin);
 }
 while(mgin[0] != 0xDE80);


/* get buffer address results */

s_ptrs=(long *)(((long)mgin[3]<<16) | (((long)mgin[4]) & 0x0000ffffL));
for (ix=0; ix<15; screens[ix++]=0L);   /* C-26:Clear storage array for ptrs */

/* Got Screen Pointers, now number of slots... */
slots=mgin[5];

/*
for(ix=0; ix<slots; ix++)  /* C-27:Note change: ++ix ->ix++ */
 {
 screens[ix]= s_ptrs[ix];
 }

/* ***************** Screen buffer number ***************** */

/* lprint("Requesting Screen Number"); */
mgout[0]=0xDE01;
mgout[1]=gl_apid;
mgout[2]=0;
appl_write(DE_id,16,mgout);


/* Wait for reply message */

do
 {
 evnt_mesag(mgin);
 }
 while(mgin[0] != 0xDE81);

/* Print out results */

/*
lprint("Got Screen Number:");
prt_i(mgin[3]);
*/
startscrn= mgin[3];          /* C-23: Offset from array start=prsntscrn */

/* ***************** Get block parameters ******************/

/* lprint("Requesting Block Parameters"); */
/* mgout[0]=0xDE02; */

/* ***************** Set block parameters **************** * */
/*
lprint("Altering Block Parameters");
mgout[0]=0xDE03;
*/

/******************** Set color palette ***************** */

/*  "Altering Colors" /
mgout[0]=0xDE04;
mgout[1]=gl_apid;
mgout[2]=32;                           * NOTE: Extra bytes in msg! *
mgout[3]=1;                              * Use palette for picture   *
appl_write(DE_id,16,mgout);          * Write part 1              *
appl_write(DE_id,32,colors);     * Write part 2              *
*/

/* No reply! */

putinout();

}

/* ****** start of************************************  C-ac1dtstb */
putinout()
{
static long  Slowvbvec=0x70, Svbvec=0x070, Shbvec=0x120;
static long  Stpaladdr, Skeyvec=0x118;
static int   Shold1,Shold2,Shold3;

static long           Shold4;  /* C-1dtstb ..was an int should be a long!11/18
*/
static long       Smousehand,Skeysave,Svbsave,
                    Spurint24;

 if (installflg== 0x4321)
      {
     form_alert(1,reset_DEGAS);     /* rset-Elite! warning message  */
     }
 else if (installflg== 0x1234)
     {
     form_alert(1,off_DEGAS);     /* already installed so remove it... */
      installflg= 0x4321;               /* reset flag to prevent re-entry */

     asm {
/*          movem.l D0-D7/A0-A4,-(A7)     */
          lea.l     restorem,A0
          move.l  A0,-(A7)
          move.w     #38,-(A7)
          trap#14

          addq.l     #6,A7
/*          movem.l (A7)+,D0-D7/A0-A4 */
          bra      endrestorem

       restorem:               /*  restore  code with these: */
             move     #34,-(A7)
           trap     #14          /* mousehandler addr @ SmousehandL */
           addq.l     #2,A7
           move.l  D0,A0
         move.l     Smousehand(A4),16(A0)

          move.b      Shold1(A4),0xfffa07
          move.b      Shold2(A4),0xfffa09
          move.b      Shold3(A4),0xfffa13
          move.l  Shold4(A4),  0x0120

          move.l     Skeyvec(A4),A0         /* at 0x118 */
          move.l  Skeysave(A4),(A0)
          move.l  Svbsave(A4),0x070

          move.l  Spurint24(A4),0x060      /* Spurious interrupt#24 */

      rts

          endrestorem:
          }
     }
 else
     {

     form_alert(1,yes_DEGAS);     /* - yes-Elite! install message  */
   asm {
/*     movem.l D0-D7/A0-A4,-(A7) */
     lea.l     savem,A0
     move.l  A0,-(A7)
     move.w     #38,-(A7)
     trap#14

     addq.l     #6,A7
/*     movem.l (A7)+,D0-D7/A0-A4 */
     bra      endsavem

    savem:
/*  save and then replace  code tfor these: */

   move     #34,-(A7)
     trap     #14          /* mousehandler addr @ SmousehandL */
     addq.l     #2,A7
     move.l  D0,A0
   move.l     16(A0),Smousehand(A4)

     move.b      0xfffa07,Shold1(A4)
     move.b      0xfffa09,Shold2(A4)
     move.b      0xfffa13,Shold3(A4)
     move.l  0x0120,  Shold4(A4)

     move.l     Skeyvec(A4),A0     /* at 0x118 */
     move.l  (A0), Skeysave(A4)
     move.l  0x070,Svbsave(A4)

     move.l  0x060,Spurint24(A4)     /* Spurious interrupt#24 */

     rts

     endsavem:
     }


      install();
     form_alert(1,cpyrt_DEGAS);            /* C-23:Force redraw=>DEpal addr get
*/
      installflg= 0x1234;                         /* set install flag */
     }

/* ********************************  C-ac1dtstb END chg */

}     /*  returns to reloop....... */


/* ***************-------- END: DE find stuff ------************************
*/
