[Hardware] RC5 verilog code generator

Martin Klingensmith martin at nnytech.net
Wed Mar 14 11:44:39 EDT 2007


There are some errors with this. I thought I had made them in this code 
but I suppose I have not. The main thing is that it should use non 
blocking assignments. It will require tweaking to get the correct 
[syntax] output anyway.
--
Martin K

Martin Klingensmith wrote:
> For those who may be interested, here is my Verilog pipeline generator 
> that I believe produces code that correctly computes any of the RC5 
> tests. It does NO pipeline retiming which is what I'm working on fixing 
> right now. This was adapted from my C program which I will post. This 
> program was not adapted from John Bass's work, but probably wouldn't be 
> possible without his assistance over the past year or two. I'm posting 
> this mainly in case anyone else has interest in it in the future.
> --
> Martin Klingensmith
>
> ---
> start builder_vlg.c
> ///
> // Verilog Pipeline Builder code
> // No pipeline retiming (work in progress)
> // Martin Klingensmith: martin at nnytech.net
>
> // References: Schneier, Bruce, Applied Cryptography
> // John Bass
>
> #include <stdio.h>
> #include <math.h>
>
> int main()
> {
>         int w=32;
>         int r=12;
>         int b=5;
>         int c=2; // b/4 if remainder add 1
>
>         int x=0, i=0, j=0, y=0, oldi=0, oldj=0;
>         int An=0, Bn=0, Ln=0, Sn=0;
>         long int S[6*r+6],P,Q;
>         int ln[4];
>         char n=20,oldn=20,oldoldn=20,tempn=20;
>         ln[0] = 0;
>         ln[1] = 0;
>         ln[2] = 0;
>         for(x=0;x<2*r+2;x++) S[x] = 0;
>
>         printf("\nmodule pipe(A,B,key0,key1,key2,clock,clr);");
>         printf("\n\toutput [31:0]A,B;");
>         printf("\n\tinput [31:0]key0,key1,key2;");
>         printf("\n\tinput clock,clr;");
>
>         printf("\n// RC5 - %d/%d/%d (%d bit word size / %d rounds / %d 
> byte key)\n\n",w,r,b,w,r,b);
>
>         printf("\treg[32:0]\n");
>         printf("A, B, L0, L1, L2, S0, S1,f,g");
>
>         // determine how many times to do the main loop:
>         y = 3*c;
>         if((6*r+6)>y) y = (6*r+6);
>
>
>         for(x=0;x<=c;x++) {
>                 for(An=0;An<(y/2);An++) {
>                         printf(",L%d_%d",x,An); // initialize Ln variables
>                 }
>         }
>         for(x=0;x<=2*r+1;x++){
>                 
> printf(",S%da,S%db,S%dc",x,x,x);                                // 
> output Sa/b/c vars
>                 if(!(x%5)) printf("\n");
>         }
>         for(x=0;x<=r;x++){                                      // 
> output A/B vars
>                 printf(", A%d, B%d",x,x);
>         }
>
>     printf(";\n\nfunction [31:0]ROTL;\ninput [31:0]x;\ninput 
> [31:0]n;\nbegin");
>         printf("\n\tROTL = (((x) << (n[4:0])) | ((x) >> 
> (32-(n[4:0]))));\n\tend\nendfunction");
>         printf("\nfunction [31:0]ROTL3;\n\tinput [31:0]x;\n\tROTL3 = 
> (((x) << 3) | ((x) >> (32-3)));\nendfunction");
>
>
>         printf("\n\nalways @(posedge 
> clock)\nbegin\n\tif(clr==1)\n\tbegin\n\nA=0; B=0");
>         for(x=0;x<=c;x++) {
>                 for(An=0;An<(y/2);An++) {
>                         printf(";L%d_%d=0",x,An);       // initialize Ln 
> variables
>                 }
>         }
>         for(x=0;x<=2*r+1;x++){
>                 printf(";S%da=0;S%db=0;S%dc=0",x,x,x);
>                 if(!(x%5)) printf("\n");
>         }
>         for(x=0;x<=r;x++){                                              
> // initialize A/B vars
>                 printf("; A%d=0; B%d=0",x,x);
>         }
>         printf(";\n\nend else\nbegin\n");
>         printf("\n\n// Key Setup + Encryption");
>         printf("\nL2_0 = 32'h85;");
>         printf("\nL1_0 = 32'hfee1d4c0;");
>         printf("\nL0_0 = 32'h53030cc9;");
>         printf("\nA = 32'h3f3ca653;");
>         printf("\nB = 32'h2ff17af3;");
>
>         P = 0xb7e15163;
>         Q = 0x9e3779b9;
>         S[0] = P;
>         for(x=1;x<2*r+2;x++) S[x] = ( S[x-1] + Q ) & 0xFFFFFFFF;
>
>
>         An=0;
>         Bn=0;
>
>
>         for(x=0;x<y;x++){
>                 if(x<(2*r+2)) {
>                         if(n != 'a') oldn=n;
>                         n = 'a';
>                 } else {
>                         if(x<(4*r+4)) {
>                                 if(n != 'b') oldn=n;
>                                 n = 'b';
>                         } else {
>                                 if(x<(6*r+6)) {
>                                         if(n != 'c') oldn=n;
>                                         n = 'c';
>                                 }
>                         }
>                 }
>                 printf("\n\n// loopn=%d, third=%c, oldn=%c",x+1,n,oldn);
>
>                 if(x==0){
>                         ln[j]++;
>                         printf("\nS%d%c = ROTL(32'h%X, 
> 3);",i,n,S[x]);                                  // case 0
>                         printf("\nL%d_%d = ROTL(L%d_%d + S%d%c, 
> S%d%c);",j,ln[j],j,ln[j]-1,i,n,i,n);            // case 0
>                 } else {
>                         if(x<=(2*r+1)){
>                                 printf("\nS%d%c = ROTL(32'h%X + S%d%c + 
> L%d_%d, 3);",i,n,S[x],oldi,n,oldj,ln[oldj]); // first third
>                         } else {
>                                 if(oldn!=oldoldn) {
>                                         oldoldn=oldn;
>                                         printf(" *");
>                                         tempn = oldn;
>                                 } else {
>                                         tempn = n;
>                                 }
>                                 printf("\nS%d%c = ROTL(S%d%c + S%d%c + 
> L%d_%d, 3);",i,n,i,oldn,oldi,tempn,oldj,ln[oldj]);       // last third
>                         }
>                         ln[j]++; // first+last third
>                         printf("\nL%d_%d = ROTL(L%d_%d + S%d%c + L%d_%d, 
> S%d%c+L%d_%d);",j,ln[j],j,ln[j]-1,i,n,oldj,ln[oldj],i,n,oldj,ln[oldj]);
>                 }
>                 //printf("\nprintf(\"\\n%d.\tf=%%x\tg=%%x\",f,g);",x+1);
>
>                 if(i==0 && (x)>=(4*r+4))        { // if i==0
>                         printf("\nA0 = A + S0%c;",n);
>                 } else {
>                         if(i==1 && (x)>=(4*r+4)) { // if i==1
>                                 printf("\nB0 = B + S1%c;",n);
>                         } else {
>                                 if(i%2 && (x)>=(4*r+4)) { // if i==odd
>                                         Bn++;
>                                         printf("\nB%d = ROTL(B%d^A%d, 
> A%d) + S%d%c;",Bn,Bn-1,An,An,i,n);
>                                 }
>                                 if((i+1)%2 && (x)>=(4*r+4)) { // if i==even
>                                         An++;
>                                         printf("\nA%d = ROTL(A%d^B%d, 
> B%d) + S%d%c;",An,An-1,Bn,Bn,i,n);
>                                 }
>                         }
>                 }
>                 oldi = i;
>                 oldj = j;
>                 i = (i + 1) % (2*(r+1));
>                 j = (j + 1) % c;
>         }
>
>         printf("\n\nend\nend\nendmodule");
>
> }
>
>
> _______________________________________________
> Hardware mailing list
> Hardware at lists.distributed.net
> http://lists.distributed.net/mailman/listinfo/hardware
>
>   



More information about the Hardware mailing list