Verilog的种族条件

时间:2017-09-29 17:50:24

标签: verilog system-verilog

所以我正在写这个des加密算法,我遇到了障碍..无法找出我出错的地方

module initialKeyPermutation(keyOut,keyIn);
  output reg [55:0]keyOut;
  input [63:0]keyIn;
  integer pTable[0:55];
  integer i;
  
  assign pTable={57,49,41,33,25,17,9,1,58,50,42,34,26,18,10,2,59,51,43,35,27,19,11,3,60,52,44,36,63,55,47,39,31,23,15,7,62,54,46,38,30,22,14,6,61,53,45,37,29,21,13,5,28,20,12,4};
  
  always@(keyIn)
    begin
      for(i=0;i<56;i++)
        keyOut[i]=keyIn[pTable[i]-1];
    end
endmodule


module keyLeftShift(C_out,D_out,round,C_in,D_in);
  input [27:0]C_in,D_in;
  output reg [27:0]C_out,D_out;
  input integer round;
  integer shiftTable[1:16];
  integer shift;
  
  assign shiftTable={1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1};
  assign shift=shiftTable[round];
  
  always@(round)
    begin
      if(shift==1)
        begin
          C_out={C_in[26:0],C_in[27]};
          D_out={D_in[26:0],D_in[27]};
        end
      else
        begin
          C_out={C_in[25:0],C_in[27:26]};
          D_out={D_in[25:0],D_in[27:26]};
        end
      end
endmodule


module finalKeyPermutation(keyOut,keyIn);
  input [55:0]keyIn;
  output reg[47:0]keyOut;
  integer i;
  integer pTable[0:47];
  
  assign pTable={14,17,11,24,1,5,3,28,15,6,21,10,23,19,12,4,26,8,16,7,27,20,13,2,41,52,31,37,47,55,30,40,51,45,33,48,44,49,39,56,34,53,46,42,50,36,29,32};
  always@(keyIn)
    begin
      for(i=0;i<48;i++)
        keyOut[i]=keyIn[pTable[i]-1];
    end
endmodule
  

module keyGenerator(nextRoundKey,keyOut,keyIn,round);
  input[55:0]keyIn;
  input integer round;
  output [55:0]nextRoundKey;
  reg [55:0]dKey;
  output [47:0]keyOut;
  wire [27:0]C_out,D_out;
  
  keyLeftShift kLS(C_out,D_out,round,keyIn[55:28],keyIn[27:0]);
  assign nextRoundKey={C_out,D_out};
  initial
    begin
      #1 $display("%b",nextRoundKey);
    end
     
  finalKeyPermutation fkP(keyOut,{C_out,D_out});

所以这是关键的一代算法..这些是最重要的模块..

// Code your design here
module des(finalCipher,plain,key);
  output [63:0]finalCipher;
  input [63:0]plain;
  input[63:0]key;
  wire [63:0]initialPlain;
  wire [63:0]cipher[1:17];
  wire [63:0]initialKey;
  wire [55:0]nextRoundKey[1:16];
  wire [47:0]keyOut[1:16];
  
  initialPermutation iP(initialPlain,plain);
  initialKeyPermutation iKP(initialKey,key);
  
  keyGenerator kG1(nextRoundKey[1],keyOut[1],initialKey,1);
  feistelRound (cipher[1],initialPlain,keyOut[1]);
  
  /*keyGenerator kG2(nextRoundKey[2],keyOut[2],nextRoundKey[1],2);
  feistelRound (cipher[2],cipher[1],keyOut[2]);
  
  keyGenerator kG3(nextRoundKey[3],keyOut[3],nextRoundKey[2],3);
  feistelRound (cipher[3],cipher[2],keyOut[3]);
  
  keyGenerator kG4(nextRoundKey[4],keyOut[4],nextRoundKey[3],4);
  feistelRound (cipher[4],cipher[3],keyOut[4]);
  
  keyGenerator kG5(nextRoundKey[5],keyOut[5],nextRoundKey[4],5);
  feistelRound (cipher[5],cipher[4],keyOut[5]);
  
  keyGenerator kG6(nextRoundKey[6],keyOut[6],nextRoundKey[5],6);
  feistelRound (cipher[6],cipher[5],keyOut[6]);
  
  keyGenerator kG7(nextRoundKey[7],keyOut[7],nextRoundKey[6],7);
  feistelRound (cipher[7],cipher[6],keyOut[7]);
  
  keyGenerator kG8(nextRoundKey[8],keyOut[8],nextRoundKey[7],8);
  feistelRound (cipher[8],cipher[7],keyOut[8]);
  
  keyGenerator kG9(nextRoundKey[9],keyOut[9],nextRoundKey[8],9);
  feistelRound (cipher[9],cipher[8],keyOut[9]);
  
  keyGenerator kG10(nextRoundKey[10],keyOut[10],nextRoundKey[9],10);
  feistelRound (cipher[10],cipher[9],keyOut[10]);
  
  keyGenerator kG11(nextRoundKey[11],keyOut[11],nextRoundKey[10],11);
  feistelRound (cipher[11],cipher[10],keyOut[11]);
  
  keyGenerator kG12(nextRoundKey[12],keyOut[12],nextRoundKey[11],12);
  feistelRound (cipher[12],cipher[11],keyOut[12]);
  
  keyGenerator kG13(nextRoundKey[13],keyOut[13],nextRoundKey[12],13);
  feistelRound (cipher[13],cipher[12],keyOut[13]);
  
  keyGenerator kG14(nextRoundKey[14],keyOut[14],nextRoundKey[13],14);
  feistelRound (cipher[14],cipher[13],keyOut[14]);
  
  keyGenerator kG15(nextRoundKey[15],keyOut[15],nextRoundKey[14],15);
  feistelRound (cipher[15],cipher[14],keyOut[15]);
  
  keyGenerator kG16(nextRoundKey[16],keyOut[16],nextRoundKey[15],16);
  feistelRound (cipher[16],cipher[15],keyOut[16]);*/
  
  assign cipher[2]={cipher[1][31:0],cipher[1][63:32]};
  
  /*finalPermutation(finalCipher,cipher[1]);*/

  assign finalCipher=keyOut[1];
  
endmodule



/*Definition of Feistel Rounds*/  
  
module feistelRound(cipher,plain,key);
  input [63:0]plain;
  input [47:0]key;
  output [63:0]cipher;
  wire [31:0]inExp,s,out;
  wire [47:0]x,perm;
  
  assign  cipher[63:32]=plain[31:0];
  plainExpPermutation pEP0(perm,plain[31:0]);
  assign x=perm^key;
  
  s1_box s1(s[31:28],x[47:42]);
  s2_box s2(s[27:24],x[41:36]);
  s3_box s3(s[23:20],x[35:30]);
  s4_box s4(s[19:16],x[29:24]);
  s5_box s5(s[15:12],x[23:18]);
  s6_box s6(s[11:8],x[17:12]);
  s7_box s7(s[7:4],x[11:6]);
  s8_box s8(s[3:0],x[5:0]);
  plainRightPermutation pRP(out,s);
  assign cipher[31:0]=out^plain[63:32];
 
endmodule

这不是真正的算法......出于测试目的,我评论了一些部分.....这个算法显示的以下测试台的输出是000000xxxxxxxxxxxxx .......我哪里出错?< / p>

module stimulus;
  reg [63:0]plain;
  wire [63:0]cipher;
  reg [63:0]key;
  
  des i(cipher,plain,key);
  initial
    begin
      plain=64'b0000000100100011010001010110011110001001101010111100110111101111;key=64'b0001001100110100010101110111100110011011101111001101111111110001;
      #2 $display("%b ",cipher);
    end
endmodule

这些仅用于测试目的,因此变量名称可能看起来毫无意义..注意:我也使用非阻塞assignmnets

0 个答案:

没有答案