%external %integer maximumx=135,maximum y=130,minimumx=65,minimumy=70

%external %routine repaint(%record (array fm) %array %name grid(-frames:xrlim+frames),
                           %integer xl,yb,xr,yt )
! work variables
 
%integer j,ii,jj,xinc,yinc,k
%integer i,magic
 
! Redraw the screen from the lambda grid. not very hard really

! the lambda map is marked thus (with the addition of ! 32 for contacts)

%constant %integer poly m = 1,diff m = 2,metal m = 4,impl m = 8

! the colours are thus

%constant %integer poly = red,diff = green,metal = blue,implant = yellow
 
! by fortune the colours (except implant) are as the marks

! the main part is basically a 2 dimensional cycle-repeat drawing in as required
! The difficulty comes with implant where one has to follow it around, drawing
! a line as one goes.
!  The method chosen is, where implant is found, to check the neighbours and
! draw accordingly
! This has all bee re-writtem in machine code for speed (a 10 fold increase)
! thus the outer loop is in IMP but the inner loop is via
! the routine flash screen
! The main reason for the speed increase is that multiplication has been
! cut right down. The multiplcation came in all the acceses to grid
! since the mapping is always in increment of one in either the y or the x dir
! all that needs to be done is find out the x  increment ( the y being one)
! and effectively adding this to the pointers when an x increment is wanted.
! This is a moderately nasty trick but is, unfortuneately required
!-------------------------------------------------------------------------------
!OLD! %routine setup magic        {sets up the byte disp between to parallel x lambda}
!OLD! %byte %name i,j
!OLD! i == grid(0)_y(0)
!OLD! j == grid(1)_y(0)
!OLD! *move.l j,d0
!OLD! *sub.l i,d0
!OLD! *move.l d0,magic
!OLD! %end
!OLD!--------------------------------------------------------------------------------

%byte %name ll
%integer kk,froo

!OLD! %routine flash line
!OLD! ! a line of lambda in the Y direction
!OLD! ! must be fast -> m/c code
!OLD! ! A1 = ptr to 0th lambda in current row
!OLD! ! A0 = ptr to framestore for current lambda (1 lambda MUST = 8 pixels)
!OLD! ! A2 = ptr to colour map
!OLD! ! A3 = ptr to enable reg
!OLD! ! D0 = 16_FF pattern for filling in picture
!OLD! ! D1,D2,D3 = work
!OLD! *move.l #16_C20000,a3
!OLD! *move.l #16_C20001,a2 
!OLD! *move.l froo,a0
!OLD! *move.l ll,a1
!OLD! *move.l #-1,d0
!OLD! *move.l k,d1
!OLD! hmd:
!OLD! *move.b (a1)+,d2
!OLD! *move.l d2,d3
!OLD! *and #7,d2
!OLD! *=16_6700; *=16_22
!OLD! *move.b d2,(a2)               { 2 }
!OLD! *move.b d0,(a0)               { 2 }
!OLD! *move.b d0,128(a0)            { 4 }
!OLD! *move.b d0,256(a0)            { 4 }
!OLD! *move.b d0,384(a0)            { 4 }
!OLD! *move.b d0,512(a0)            { 4 }
!OLD! *move.b d0,640(a0)            { 4 }
!OLD! *move.b d0,768(a0)            { 4 }
!OLD! *move.b d0,896(a0)            { 4 }
!OLD! !rwt:                         { = 32 }
!OLD! ! Now draw in the implant
!OLD! *and.l #8,d3
!OLD! *=16_6700;  *=146
!OLD! *move.l #3,d3
!OLD! *move.b d3,(a3)
!OLD! *move.b d3,(a2)
!OLD! *move.b (a1),d3
!OLD! *and #8,d3
!OLD! *=16_6600;*=6
!OLD! *move.b d0,896(a0)
!OLD! l:
!OLD! *move.b -2(a1),d3
!OLD! *and #8,d3
!OLD! *=16_6600;*=4
!OLD! *move.b d0,(a0)
!OLD! l2:
!OLD! *move magic,d2
!OLD! *move.b -1(a1,d2),d3
!OLD! *and #8,d3
!OLD! *= 16_6600; *=34
!OLD! *move.l #1,d3
!OLD! *move.b d3,(a0)               { 2 }
!OLD! *move.b d3,128(a0)            { 4 }
!OLD! *move.b d3,256(a0)            { 4 }
!OLD! *move.b d3,384(a0)            { 4 }
!OLD! *move.b d3,512(a0)            { 4 }
!OLD! *move.b d3,640(a0)            { 4 }
!OLD! *move.b d3,768(a0)            { 4 }
!OLD! *move.b d3,896(a0)            { 4 }
!OLD! *neg d2
!OLD! *move.b -1(a1,d2),d3
!OLD! *and #8,d3
!OLD! *=16_6600;*=38
!OLD! *move.l #128,d3
!OLD! *move.b d3,(a0)               { 2 }
!OLD! *move.b d3,128(a0)            { 4 }
!OLD! *move.b d3,256(a0)            { 4 }
!OLD! *move.b d3,384(a0)            { 4 }
!OLD! *move.b d3,512(a0)            { 4 }
!OLD! *move.b d3,640(a0)            { 4 }
!OLD! *move.b d3,768(a0)            { 4 }
!OLD! *move.b d3,896(a0)            { 4 }
!OLD! *move.l #7,d3
!OLD! *move.b d3,(a3)
!OLD! pds:
!OLD! *move.l #-1,d0
!OLD! *lea 1024(a0),a0
!OLD! *dbra d1,hmd
!OLD! %end

{NEW}%routine flash line
{NEW}  %record %format fool hmd (%byte %array a(0:127))
{NEW}  %record %format fool again (%byte %name y %or -
{NEW}                              %record (array fm) %name x)
{NEW}
{NEW}  %byte j
{NEW}  %integer i
{NEW}  %record (fool hmd) %name screen ptr
{NEW}  %record (fool again) grid ptr
{NEW}
{NEW}  %routine paint in(%byte pattern)
{NEW}    screen ptr[0]_a(0) = pattern
{NEW}    screen ptr[1]_a(0) = pattern
{NEW}    screen ptr[2]_a(0) = pattern
{NEW}    screen ptr[3]_a(0) = pattern
{NEW}    screen ptr[4]_a(0) = pattern
{NEW}    screen ptr[5]_a(0) = pattern
{NEW}    screen ptr[6]_a(0) = pattern
{NEW}    screen ptr[7]_a(0) = pattern
{NEW}  %end
{NEW}
{NEW}  screen ptr == record(froo)
{NEW}  %for i = k,-1,0 %cycle
{NEW}    grid ptr_y == ll
{NEW}    j = ll & 7
{NEW}    %if j # 0 %start
{NEW}      colour (j)
{NEW}      paint in(16_ff)
{NEW}    %finish
{NEW}
{NEW}    %if ll & 8 # 0 %start
{NEW}      enable  (3)
{NEW}      colour(3)
{NEW}      %if grid ptr_y[1] & 8 = 0  %then screen ptr[7]_a(0) = 16_FF
{NEW}      %if grid ptr_y[-1] & 8 = 0 %then screen ptr[0]_a(0) = 16_FF
{NEW}      %if grid ptr_x[1]_y(-frames)  & 8 = 0 %then paint in(1)
{NEW}
{NEW}! alternatively
{NEW}! byteinteger(addr(grid ptr_x[..]))
{NEW}
{NEW}      %if grid ptr_x[-1]_y(-frames) & 8 = 0 %then paint in(16_80)
{NEW}      enable  (15)
{NEW}    %finish
{NEW}    screen ptr == screen ptr[8]
{NEW}    ll == ll[1]
{NEW}  %repeat
{NEW}%end
  
 

%if xl > xr %start ;i= xl;xl=xr;xr=i;%finish
%if yb > yt %start ;i= yb;yb=yt;yt=i;%finish
! thus saving unneccesary calls on the multiplication
 
%if xl < sc xl %then xl = sc xl
%if yb < sc yb %then yb = sc yb
%if xr > sc xr %then xr = sc xr
%if yt > sc yt %then yt = sc yt
colour(black)
fill(toscreenx(xl),toscreeny(yb),toscreenx(xr+1)-1,toscreeny(yt+1)-1)
 
colour(white)
xl = 0 %if xl < 0
yb = 0 %if yb < 0
xr = xrlim %if xr > xrlim
yt = ytlim %if yt > ytlim
xinc = 8
yinc = 8
enable  (15)
k = yt - yb
froo = (xl-scxl+xlat) + addr(frame) {16_c00000} + ((yb-scyb+ybat) << 10)

!OLD! setup magic
{NEW}{ Further Optimisation avaiable within this loop

%for i = xl,1,xr %cycle
  ll == grid(i)_y(yb)
  flash line          { draw in colour and implant }
  froo = froo + 1
%repeat
enable  (7)
colour(white)
j=to screen x(xrlim+1);jj=to screen y(ytlim+1)
xinc=to screenx(xr+2);yinc=to screeny(yt+2)
%if xinc > j %then xinc = j
%if yinc > jj %then yinc = jj
%if scxl <= 0 %then line(to screen x(0),toscreeny(yb),to screen x(0),yinc)
%if scxr >= xr lim %then line(j,toscreeny(yb),j,yinc)
%if scyb <= 0 %then line(toscreenx(xl),to screen y(0),xinc,to screen y(0))
%if scyt >= ytlim %then line(toscreenx(xl),jj,xinc,jj)
%if xr > maximum x %then maximum x = xr
%if yt > maximum y %then maximum y = yt
%if xl < minimum x %then minimum x = xl
%if yb < minimum y %then minimum y = yb
%end
  
%external %routine refresh(%record (array fm) %array %name grid(-frames:xrlim+frames),
                           %record (conf) %array %name con(1:200),
                           %integer con ptr)

%integer i,j,ii,jj,xinc,yinc

enable  (15)
colour(black)
fill(80,0,688,512)
repaint(grid,0,0,scxr,scyt)           ;!xrlim,ytlim)
! now the contacts

%if con ptr # 1 %start
  %for i = 1,1,con ptr - 1 %cycle
    draw contact(con(i)_t,con(i)_x,con(i)_y)
  %repeat
%finish

{@@@}%if error ptr > 0 %start
{@@@}  enable  (8)
{@@@}  colour (8)
{@@@}  %for i = 0,1,error ptr - 1 %cycle
{@@@}    ii = error x(i)
{@@@}    jj = error y(i)
{@@@}    %if scxl < ii < scxr %and scyb < jj < scyt %start
{@@@}      ii = toxpixel(ii - scxl + xlat)
{@@@}      jj = toypixel(jj - scyb + yb at)
{@@@}      fill(ii,jj,ii+8,jj+8)
{@@@}    %finish
{@@@}  %repeat
{@@@}%finish
%end
 
%endoffile
 
