%begin
   %include "level1:graphinc.imp"
   %include "inc:util.imp"
   %include "inc:maths.imp"

   %const %integer noseg = 434
   %routine origintst(%integer i)
      %own %integer j = 0, t, tl
      t = cputime+20
      %while cputime<=t %cycle; %repeat
      colour(0)
      fill(i, j, i+687, j+511)
      colour(7)
      line(i, j, i+687, j+511)
      line(i, j+511, i+687, j)
      offset(i, j)
      j = 512-j
   %end
   %routine grid(%integer rate)
      %integer i
      i = 0
      i = i-rate %while i>rate-173
      %while i<850 %cycle
         line(i, 0, i, 1023)
         i = i+rate
      %repeat
      i = 0
      i = i-rate %while i>rate-255
      %while i<767 %cycle
         line(0, i, 1023, i)
         i = i+rate
      %repeat
   %end
   %routine converge(%integer x, y)
      %integer i
      %const %byte %integer %array con(0:15) = 0, 1, 3, 2, 6, 4, 5, 7,
                                               7, 5, 4, 6, 2, 3, 1, 0
      colour(0)
      fill(x, y, x+63, y+63)
      %for i = 0, 1, 15 %cycle
         colour(con(i))
         line(x+2+i+i, y+2, x+2+i+i, y+29)
         line(x+34, y+2+i+i, x+61, y+2+i+i)
         line(x+34+i+i, y+2, x+34+i+i, y+61)
         line(x+2, y+34+i+i, x+61, y+34+i+i)
      %repeat
   %end
   %half %integer %array cm image(0:noseg+255)
   %integer c, cd, ct, i, j, oi, oj, k, l, f, s, t, z = 0
   %on %event 0 %start
      ->done
   %finish
   s = 0
   clear
   read font("level1:timesr8", f)
   { Background grid }
   colour(1)
   grid(10)
   colour(2)
   grid(50)
   colour(3)
   grid(100)
   { Corner markers }
   colour(7)
   line(0, 0, 0, 31)
   line(0, 0, 31, 0)
   line(0, 0, 31, 31)
   line(0, 511, 0, 480)
   line(0, 511, 31, 511)
   line(0, 511, 31, 480)
   line(687, 0, 687, 31)
   line(687, 0, 656, 0)
   line(687, 0, 656, 31)
   line(687, 511, 687, 480)
   line(687, 511, 656, 511)
   line(687, 511, 656, 480)
   { Big central disc }
   colour(4)
   disc(343, 255, 240)
   { Horizoltal resolution lines)
   colour(3)
   j = 500
   line(j, 320, j, 383)
   %for i = 1, 1, 16 %cycle
      j = j-i-1
      fill(j-i, 320, j, 383)
      j = j-i-1
   %repeat
   { vertical resulotion lines }
   j = 192
   line(312, j, 375, j)
   %for i = 1, 1, 11 %cycle
      j = j-i-1
      fill(312, j-i, 375, j)
      j = j-i-1
   %repeat
   { Grey scales }
   %for i = 0, 1, 31 %cycle
      colour map(i+128) = i<<10+i<<5+i
      colour map(i+160) = i<<10+(31-i)<<5
      colour map(i+192) = i<<10+(31-i)
      colour map(i+224) = i<<5+(31-i)
      colour(i+128)
      fill(121+i*14, 214, 134+i*14, 295)
      fill(36, 28+i*7, 83, 34+i*7)
      colour(i+160)
      fill(36, 484-i*7, 83, 478-i*7)
      colour(i+192)
      fill(604, 484-i*7, 651, 478-i*7)
      colour(i+224)
      fill(604, 28+i*7, 651, 34+i*7)
   %repeat
   { Convergence tests }
   converge(92, 28)
   converge(92, 420)
   converge(528, 28)
   converge(528, 420)
   colour(0)
   font(f)
   i = string width("Fred Machine")>>1
   text at(343-i, 440)
   show string("Fred Machine")
   fill(342-i, 430, 344+i, 434)
   text at(343-string width("Level 1(.5) Graphics")>>1, 400)
   show string("Level 1")
   text at(text x pos, 402)
   show symbol('(')
   text at(text x pos, 408)
   show symbol('.')
   text at(text x pos, 400)
   show symbol('5')
   text at(text x pos, 402)
   show symbol(')')
   text at(text x pos, 400)
   show string(" Graphics")
   text at(300-string width("TEST"), 170)
   show string("TEST")
   text at(388, 170)
   show string("CARD")
   %for i = 0, 5, 90 %cycle
      j = round(cos(i*pio2/90)*128)
      k = round(sin(i*pio2/90)*128)
      colour(0)
      line(300, 160, 300-j, 160-k)
      colour(7)
      line(387, 160, 387+j, 160-k)
   %repeat
   %while testsymbol<0 %cycle; %repeat
   clear
   colour(7)
   disc(512, 512, 15)
   i = 0
   j = 0
   t = cputime+20
   %while testsymbol<0 %cycle
      k = k+1
      oi = i
      oj = j
      i = (168-round(260*cos(k/50)))&\15
      j = 256+round(260*sin(k/50))
      colour(0)
      plot(oi+388, oj+256)
      offset(i, j)
      colour(7)
      plot(i+388, j+256)
      %while cputime<t %cycle; %repeat
      t = cputime+20
   %repeat
   offset(0, 0)
   colour(255)
   colour map(0) = z
   clear
   colour map(j) = 16_1F %for j = 1, 1, 255
   %for i = 0, 1, 7 %cycle
      colour map(1<<i) = 16_7FFF
      enable(1<<i)
      line(j, i<<6, j, i<<6+63) %for j = 0, 17, 544
      line(j, i<<6, j, i<<6+63) %for j = 560, 3, 605
      line(0, i<<6, 608, i<<6)
      text at(608, i<<6+24)
      show i(i, 1)
   %repeat
   %while testsymbol<0 %cycle; %repeat
   colour map(255) = z
   enable(255)
   fill(0, 0, 1023, 1023)
   colour map(j) = 16_1F %for j = 0, 1, 254
   colour(0)
   %for i = 0, 1, 7 %cycle
      colour map(255!!(1<<i)) = 16_7FFF
      enable(1<<i)
      line(j, i<<6, j, i<<6+63) %for j = 0, 17, 544
      line(j, i<<6, j, i<<6+63) %for j = 560, 3, 605
      line(0, i<<6, 608, i<<6)
      text at(608, i<<6+24)
      show i(i, 1)
   %repeat
   %while testsymbol<0 %cycle; %repeat
   clear
   colour map(i) = 16_7FFF %for i=1, 1, 254
   colour map(0) = z
   colour map(255) = z
   enable(255)
   colour(255)
   vline(i, 0, 511) %for i = 0, 2, 686
   %while testsymbol<0 %cycle; %repeat
   cm image(i) = 16_FFFF %for i = 0, 1, 255
   update colour map(cm image(0))
   cm image(0) = 0
   c = 0
   cd = 1
   ct = 0
   %for i = 1, 1, noseg+255 %cycle
      %if c=31 %start
         cd = -1
      %else %if c=0
         ct = ct!!31
         %if ct&31=0 %start
            ct = ct!!(31<<5)
            %if ct&(31<<5)=0 %start
               ct = ct!!(31<<10)
               %if ct&(31<<10)=0 %start
                  ct = 31
               %finish
            %finish
         %finish
         cd = 1
      %finish
      cm image(i) = (c+c<<5+c<<10)&ct
      c = c+cd
   %repeat
   clear
   enable(255)
   c = 0
   %for i = 0, 4, 1020 %cycle
      colour(c)
      %for j = 1022, -2, 0 %cycle
         k = i-(j>>1)
         %if -4<k<0 %start
            hline(1024+k, 1023, j+1)
            hline(1024+k, 1023, j)
            hline(0, k+3, j+1)
            hline(0, k+3, j)
         %else
            k = k+1024 %if k<0
            hline(k, k+3, j+1)
            hline(k, k+3, j)
         %finish
      %repeat
      c = rem(c+1, noseg)
   %repeat
   t = cpu time+20
   k = 1
   %while test symbol<0 %cycle
      update colour map(cm image(k))
      k = rem(k, no seg)+1
      %while cpu time<t %cycle; %repeat
      t = cpu time+20
   %repeat
   colour map(0)=z
   enable(255)
   clear
   k = 0
   l = 0
   %for i = 0, 32, 224 %cycle
      %for j = 0, 1, 31 %cycle
         colour map(l{i+j}) = (j<<10+j<<5+j)&k
         %if j&1=0 %then l = \l %else l = -l
         l = l&255
      %repeat
      k = k!!31
      %if k&31=0 %start
         k = k!!(31<<5)
         %if k&(31<<5)=0 %start
            k = k!!(31<<10)
         %finish
      %finish
   %repeat
   k = 32
   l = 16
   %for i = 0, 73, 438 %cycle
      %for j = 1, 21, 652 %cycle
         colour(l)
         fill(j, i, j+20, i+72)
         %if k&1=0 %then l = \l %else l = -l
         l = l&255
         k = k+1
      %repeat
   %repeat
   line(0, 0, 673, 0)
   line(0, 511, 673, 511)
   line(0, 0, 0, 511)
   line(673, 0, 673, 511)
   %while testsymbol<0 %cycle; %repeat
   clear
   k = 0
   %for i = 0, 32, 224 %cycle
      %for j = 0, 1, 31 %cycle
         colour map(i+j) = (j<<10+j<<5+j)&k
      %repeat
      k = k!!31
      %if k&31=0 %start
         k = k!!(31<<5)
         %if k&(31<<5)=0 %start
            k = k!!(31<<10)
         %finish
      %finish
   %repeat
   s = 0
   %cycle; !%while testsymbol<0 %cycle
      f = s
      s = (s+1)&255
      %for j = 0, 16, 496 %cycle
         k = f
         f = (f+1)&255
         %for i = 0, 16, 672 %cycle
            colour(k)
            k = (k+32)&255
            fill(i, j, i+15, j+15)
         %repeat
      %repeat
   %repeat
done:
   %for i = 0, 16, 240 %cycle
      colour map(i) = z
      colour map(i+1) = 16_1F
      colour map(i+2) = 16_3E0
      colour map(i+3) = 16_3FF
      colour map(i+4) = 16_7C00
      colour map(i+5) = 16_7C1F
      colour map(i+6) = 16_7FE0
      colour map(i+7) = 16_7FFF
      colour map(i+8) = 16_7FFF
      colour map(i+9) = 16_7FE0
      colour map(i+10) = 16_7C1F
      colour map(i+11) = 16_7C00
      colour map(i+12) = 16_3FF
      colour map(i+13) = 16_3E0
      colour map(i+14) = 16_1F
      colour map(i+15) = z
   %repeat
   clear
   print symbol(27)
   print symbol('R')
%end %of %program
