10 REM > VDUState
   20 REM Read and write VDU state
   30 ;
   40 :
   50 :
   60 REM Set palette from "palette file" VDU sequence at store%
   70 DEFPROCvdu_paletteset(store%)
   80 LOCAL A%:FOR A%=store% TO store%+95:VDU ?A%:NEXT
   90 ENDPROC
  100 :
  110 REM Get palatte as "palette file" VDU sequence to store%
  120 DEFPROCvdu_paletteget(store%)
  130 LOCAL X%,Y%,A%,col%:A%=11
  140 A%=INKEY-256:IFA%<>ASC"W":A%=11
  150 FOR col%=0 TO 15
  160   X%=store%+col%*6+1:Y%=X%DIV256:X%?-1=19:X%?0=col%
  170   IF A%=11:CALL &FFF1
  180   IF A%=ASC"W":X%?1=16:SYS "GetPaletteEntries",@hpal%,col%,1,X%+2
  190   IF A%=ASC"S":REM....
  200 NEXT col%
  210 ENDPROC
  220 :
  230 REM Set VDU non-palette state
  240 DEFPROCvdu_stateset(store%)
  250 LOCAL A%:FOR A%=store% TO store%+43:VDU ?A%:NEXT
  260 ENDPROC
  270 :
  280 REM Get the VDU state as a sequence of VDUs to restore it from
  290 REM 29,a,b,c,d, 24,a,b,c,d,e,f,g,h, 25,k,a,b,c,d, 25,k,a,b,c,d, 28,a,b,c,d, 31,a,b, 17,a, 17,128+b, 18,a,b, 18,a,128+b
  300 REM ..origin .... graphicwindow ....  previous .... current ... textwindow pos/vpos txfgd .. txbgd .. gfgd .... gbgd
  320 DEFPROCvdu_stateget(store%)
  330 LOCAL X%,Y%,A%,col%:A%=11
  340 A%=INKEY-256:IFA%<>ASC"W":A%=11
  350 IF A%<>ASC"W":ENDPROC
  420 store%?00=29      :REM Graphics origin, must do first
  430 store%!01=@vdu.o.x%
  440 store%!03=@vdu.o.y%
  450 
  360 store%?05=24      :REM Graphics window
  370 store%!06=0:REM @vdu%!60 is handle to graphics rectangle
  380 store%!08=0
  390 store%!10=0
  400 store%!12=0
  410 
  460 store%?14=25      :REM Previous graphics point
  470 store%?15=2       :REM MOVE to it
  480 store%!16=@vdu.p.x%
  490 store%!18=@vdu.p.y%
  500 
  510 store%?20=25      :REM Last graphics point
  520 store%?21=4       :REM MOVE to it
  530 store%!22=@vdu.l.x%
  540 store%!24=@vdu.l.y%
  550 
  560 store%?26=28      :REM Text window
  570 store%?27=@vdu.tl%
  580 store%?28=@vdu.tb%
  590 store%?29=@vdu.tr%
  600 store%?30=@vdu.tt%
  610 
  620 store%?31=31      :REM Text position
  630 store%?32=POS
  640 store%?33=VPOS
  650 
  660 store%?34=17      :REM Text foreground COLOUR
  670 store%?35=@vdu.t.c&
  680 store%?36=17      :REM Text background
  690 store%?37=@vdu.t.d& OR 128
  700 
  710 store%?38=18      :REM Graphics foreground GCOL
  720 store%?39=@vdu.g.a&
  730 store%?40=@vdu.g.b&
  740 store%?41=18      :REM Graphics background GCOL
  750 store%?42=@vdu.g.c&
  760 store%?43=@vdu.g.d& OR 128
  770 ENDPROC
  780 
  790 
  800 
  810 
  820 
  830 
  840 
  850 REM Read and restore VDU state by read/writing memory
  860 REM Requires X%=>5 bytes, Y%=X%DIV256
  870 :
  880 REM Save VDU state
  890 REM store% => 48 bytes
  900 DEFPROCvdu_stateread(store%)
  910 PROCmem_rd(store%+&00,&FFFF00D0,0,&06)
  920 PROCmem_rd(store%+&06,&FFFF0300,0,&1A)
  930 PROCmem_rd(store%+&20,&FFFF0357,0,&06)
  940 PROCmem_rd(store%+&26,&FFFF0366,0,&09)
  950 ENDPROC
  960 :
  970 REM Restore VDU state
  980 REM store% => 48 bytes
  990 DEFPROCvdu_statewrite(store%)
 1000 PROCmem_wr(store%+&00,&FFFF00D0,0,&06)
 1010 PROCmem_wr(store%+&06,&FFFF0300,0,&1A)
 1020 PROCmem_wr(store%+&20,&FFFF0357,0,&06)
 1030 PROCmem_wr(store%+&26,&FFFF0366,0,&09)
 1040 VDU31,store%?30-store%?14,store%?31-store%?17
 1050 ENDPROC
 1060 :
 1070 DEFPROCmem_rd(mem%,io%,rom%,num%)
 1080 A%=5:REPEAT:!X%=io%:CALL&FFF1:?mem%=X%?4:io%=io%+1:mem%=mem%+1:num%=num%-1
 1090 UNTILnum%<1:ENDPROC
 1100 :
 1110 DEFPROCmem_wr(mem%,io%,rom%,num%)
 1120 A%=6:REPEAT:!X%=io%:X%?4=?mem%:CALL&FFF1:io%=io%+1:mem%=mem%+1:num%=num%-1
 1130 UNTILnum%<1:ENDPROC
 1140 :
 1150 DEFFNfx(A%,X%):LOCALY%:=((USR&FFF4)AND&FFFF00)DIV256