Guest User

Untitled

a guest
Jun 4th, 2011
804
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module sha256(/*AUTOARG*/
  2.    // Outputs
  3.    next,
  4.    // Inputs
  5.    clk, rst_b, start, block, last
  6.    );
  7.  
  8.    input clk, rst_b;
  9.    input start;
  10.    input [511:0] block;
  11.    input [255:0] last;
  12.    output reg [255:0] next;
  13.    
  14.    function [31:0] K(input [5:0] i);
  15.       case (i)
  16.     6'd0: K = 32'h428a2f98;
  17.     6'd1: K = 32'h71374491;
  18.     6'd2: K = 32'hb5c0fbcf;
  19.     6'd3: K = 32'he9b5dba5;
  20.     6'd4: K = 32'h3956c25b;
  21.     6'd5: K = 32'h59f111f1;
  22.     6'd6: K = 32'h923f82a4;
  23.     6'd7: K = 32'hab1c5ed5;
  24.     6'd8: K = 32'hd807aa98;
  25.     6'd9: K = 32'h12835b01;
  26.     6'd10: K = 32'h243185be;
  27.     6'd11: K = 32'h550c7dc3;
  28.     6'd12: K = 32'h72be5d74;
  29.     6'd13: K = 32'h80deb1fe;
  30.     6'd14: K = 32'h9bdc06a7;
  31.     6'd15: K = 32'hc19bf174;
  32.     6'd16: K = 32'he49b69c1;
  33.     6'd17: K = 32'hefbe4786;
  34.     6'd18: K = 32'h0fc19dc6;
  35.     6'd19: K = 32'h240ca1cc;
  36.     6'd20: K = 32'h2de92c6f;
  37.     6'd21: K = 32'h4a7484aa;
  38.     6'd22: K = 32'h5cb0a9dc;
  39.     6'd23: K = 32'h76f988da;
  40.     6'd24: K = 32'h983e5152;
  41.     6'd25: K = 32'ha831c66d;
  42.     6'd26: K = 32'hb00327c8;
  43.     6'd27: K = 32'hbf597fc7;
  44.     6'd28: K = 32'hc6e00bf3;
  45.     6'd29: K = 32'hd5a79147;
  46.     6'd30: K = 32'h06ca6351;
  47.     6'd31: K = 32'h14292967;
  48.     6'd32: K = 32'h27b70a85;
  49.     6'd33: K = 32'h2e1b2138;
  50.     6'd34: K = 32'h4d2c6dfc;
  51.     6'd35: K = 32'h53380d13;
  52.     6'd36: K = 32'h650a7354;
  53.     6'd37: K = 32'h766a0abb;
  54.     6'd38: K = 32'h81c2c92e;
  55.     6'd39: K = 32'h92722c85;
  56.     6'd40: K = 32'ha2bfe8a1;
  57.     6'd41: K = 32'ha81a664b;
  58.     6'd42: K = 32'hc24b8b70;
  59.     6'd43: K = 32'hc76c51a3;
  60.     6'd44: K = 32'hd192e819;
  61.     6'd45: K = 32'hd6990624;
  62.     6'd46: K = 32'hf40e3585;
  63.     6'd47: K = 32'h106aa070;
  64.     6'd48: K = 32'h19a4c116;
  65.     6'd49: K = 32'h1e376c08;
  66.     6'd50: K = 32'h2748774c;
  67.     6'd51: K = 32'h34b0bcb5;
  68.     6'd52: K = 32'h391c0cb3;
  69.     6'd53: K = 32'h4ed8aa4a;
  70.     6'd54: K = 32'h5b9cca4f;
  71.     6'd55: K = 32'h682e6ff3;
  72.     6'd56: K = 32'h748f82ee;
  73.     6'd57: K = 32'h78a5636f;
  74.     6'd58: K = 32'h84c87814;
  75.     6'd59: K = 32'h8cc70208;
  76.     6'd60: K = 32'h90befffa;
  77.     6'd61: K = 32'ha4506ceb;
  78.     6'd62: K = 32'hbef9a3f7;
  79.     6'd63: K = 32'hc67178f2;
  80.       endcase
  81.    endfunction
  82.  
  83.    function [31:0] Ch(input [31:0] x, input [31:0] y, input [31:0] z);
  84.       Ch = (x & y) ^ (~x & z);
  85.    endfunction
  86.  
  87.    function [31:0] Maj(input [31:0] x, input [31:0] y, input [31:0] z);
  88.       Maj = (x & y) ^ (x & z) ^ (y & z);
  89.    endfunction
  90.  
  91.    function [31:0] BigSig0(input [31:0] x);
  92.       BigSig0 = { x[1:0], x[31:2] } ^ { x[12:0], x[31:13] } ^ { x[21:0], x[31:22] };
  93.    endfunction
  94.  
  95.    function [31:0] BigSig1(input [31:0] x);
  96.       BigSig1 = { x[5:0], x[31:6] } ^ { x[10:0], x[31:11] } ^ { x[24:0], x[31:25] };
  97.    endfunction
  98.  
  99.    function [31:0] LittleSig0(input [31:0] x);
  100.       LittleSig0 = { x[6:0], x[31:7] } ^ { x[17:0], x[31:18] } ^ (x >> 3);
  101.    endfunction
  102.    
  103.    function [31:0] LittleSig1(input [31:0] x);
  104.       LittleSig1 = { x[16:0], x[31:17] } ^ { x[18:0], x[31:19] } ^ (x >> 10);    
  105.    endfunction
  106.  
  107.    parameter [1:0]    STATE_INIT = 2'd0, STATE_WAIT = 2'd1, STATE_LOOP = 2'd2;
  108.  
  109.    reg [1:0]          state, next_state;
  110.    reg [31:0]         a,b,c,d,e,f,g,h,next_a,next_b,next_c,next_d,next_e,next_f,next_g,next_h;
  111.    reg [31:0]         T1, T2;
  112.    reg [31:0]         W [63:0];
  113.    reg [5:0]          t, next_t;  
  114.  
  115.    // Next state
  116.    always @* begin
  117.       if (state == STATE_WAIT) begin
  118.      if (start)
  119.        next_state = STATE_INIT;
  120.      else
  121.        next_state = STATE_WAIT;
  122.       end else if (state == STATE_INIT) begin
  123.      next_state = STATE_LOOP;
  124.       end else if (state == STATE_LOOP) begin
  125.      if (next_t == 6'b0)
  126.        next_state = STATE_WAIT;
  127.      else
  128.        next_state = STATE_LOOP;
  129.       end
  130.    end
  131.  
  132.    // Registering
  133.    always @(posedge clk or negedge rst_b) begin
  134.       if (!rst_b)
  135.     state <= STATE_WAIT;
  136.       else
  137.     state <= next_state;
  138.    end
  139.  
  140.    // Datapath
  141.    always @(state) begin
  142.       if (state == STATE_INIT) begin
  143.      next_a = last[255:224];
  144.      next_b = last[223:192];
  145.      next_c = last[191:160];
  146.      next_d = last[159:128];
  147.      next_e = last[127:96];
  148.      next_f = last[95:64];
  149.      next_g = last[63:32];
  150.      next_h = last[31:0];
  151.  
  152.      next_t = 6'b0;
  153.       end else if (state == STATE_LOOP) begin      
  154.      if (t < 16)
  155.        W[t] = block >> (32 * (15 - t));
  156.      else
  157.        W[t] = LittleSig1(W[t-2]) + W[t-7] + LittleSig0(W[t-15]) + W[t-16];
  158.      T1 = h + BigSig1(e) + Ch(e, f, g) + K(t) + W[t];
  159.      T2 = BigSig0(a) + Maj(a, b, c);
  160.      
  161.      next_h = g;
  162.      next_g = f;
  163.      next_f = e;
  164.      next_e = d + T1;
  165.      next_d = c;
  166.      next_c = b;
  167.      next_b = a;
  168.      next_a = T1 + T2;
  169.  
  170.      next_t = t + 1;
  171.       end else begin
  172.      next_a = 32'bx;
  173.      next_b = 32'bx;
  174.      next_c = 32'bx;
  175.      next_d = 32'bx;
  176.      next_e = 32'bx;
  177.      next_f = 32'bx;
  178.      next_g = 32'bx;
  179.      next_h = 32'bx;
  180.      next_t = 6'b0;
  181.       end
  182.  
  183.       next = { a + last[255:224],
  184.            b + last[223:192],
  185.            c + last[191:160],
  186.            d + last[159:128],
  187.            e + last[127:96],
  188.            f + last[95:64],
  189.            g + last[63:32],
  190.            h + last[31:0] };
  191.    end
  192.  
  193.    // Registering
  194.    always @(posedge clk or negedge rst_b) begin
  195.       if (!rst_b) begin
  196.      /*AUTORESET*/
  197.      // Beginning of autoreset for uninitialized flops
  198.      a <= 32'h0;
  199.      b <= 32'h0;
  200.      c <= 32'h0;
  201.      d <= 32'h0;
  202.      e <= 32'h0;
  203.      f <= 32'h0;
  204.      g <= 32'h0;
  205.      h <= 32'h0;
  206.      t <= 6'h0;
  207.      // End of automatics
  208.       end else begin
  209.      a <= next_a;
  210.      b <= next_b;
  211.      c <= next_c;
  212.      d <= next_d;
  213.      e <= next_e;
  214.      f <= next_f;
  215.      g <= next_g;
  216.      h <= next_h;
  217.      t <= next_t;
  218.       end
  219.    end
  220. endmodule
Advertisement
Add Comment
Please, Sign In to add comment