!****************************************************************
!*                                                              *
!*      DUMPRAM:    Produce a meaningful interpretation of      *
!*                  the Canon copy controller RAM               *
!*                                                              *
!*                  Version 1.1    6 Mar 1989                   *
!*                                                              *
!****************************************************************

%begin
%include "inc:util.imp"
%string (255) infile,namefile,outfile

%integer n
%string (63) %array info(0:511)
%integer c,p,ad,len,acctno,acct,pinhi,pinlo,group,alimit,acopies,glimit,gcopies

%integerfn bcd(%integer ad)
   %integer a0,a1,a2,a3,a4,a5
   a0=byte(ad)>>4;   a1=byte(ad)&15
   a2=byte(ad+1)>>4; a3=byte(ad+1)&15
   a4=byte(ad+2)>>4; a5=byte(ad+2)&15
   %result=((((a0*10+a1)*10+a2)*10+a3)*10+a4)*10+a5
%end

%routine p2(%integer ad)
   %integer pinhi
   pinhi = byteinteger(ad) & 255
   %if pinhi=0 %start
     spaces(2)
   %finish %else %if pinhi<16 %start
     space; phex1(pinhi)
   %finish %else %start
     phex2(pinhi)
   %finish
%end

%routine get names
   %integer n,i,c
   %on 3,9 %start; ->eof; %finish
   %for n=0,1,511 %cycle; info(n)=""; %repeat
   openinput(1,namefile); selectinput(1)
   %cycle
     %for i=1,1,5 %cycle
       readsymbol(c); %exit %if c=9
     %repeat
     read(n)
     %exit %if n<0
     readline(info(n))
   %repeat
   eof:
   close input
%end

namefile="names"
get names
infile = cli param
outfile="" %unless infile -> infile.("/").outfile
printline("Can't access RAM file") %and %stop %unless exists(infile)
ad = heapget(8192)
connectfile(infile,0,ad,len)
printline("***** RAM file wrong length (".itos(len,-1).") *****") %unless len=8192
open output(2, outfile) %and selectoutput(2) %unless outfile=""

printline("Acct     PIN   Gp  Limit Copies ")
newline

acctno=0
%for p={0} 16_1DF9, -8, {16_1701} 16_1001 %cycle
   acct = ad+p
   write(acctno, 3); printstring(": ")
   pinlo = byte(acct+1)<<8+byte(acct+2)
   p2(acct); phex4(pinlo); space
   group = byte(acct+3)
   alimit = byte(acct+4)<<8+byte(acct+5)
   acopies = byte(acct+6)<<8+byte(acct+7)
   write(group, 2); write(alimit, 6); write(acopies, 6)
   spaces(3); printstring(info(acctno))
   newline
   acctno=acctno+1
%repeat
newline

acctno=0
%for p=16_1F6B, -6, 16_1E03 %cycle
   acct = ad+p
   write(acctno, 3); printstring(": ")
   glimit = bcd(acct)
   gcopies = bcd(acct+3)
   write(glimit, 6); write(gcopies, 6)
   acctno=acctno+1
   newline
%repeat
newline
close input
closeoutput
selectoutput(0)
%endofprogram
