/* Module sum19cmp: Coded by Antti Karttunen, October 1, 2007. Take a circular sum of permutation, and update the current minsum and maxsum. */ // Getting a circular sum-product of elements p1 - ... to psum takes one cycle // and getting the min & max to minsum & maxsum takes another. // So we have 2-stage pipeline here: `define SUMSIZE 13 module sum19cmp(CLK,input_valid,n,nplus1, p1,p2,p3,p4,p5,p6,p7,p8,p9,p10,p11,p12,p13,p14,p15,p16,p17,p18, output_valid, minsum,maxsum,psum ); input CLK; input input_valid; // When switches from 0 to 1, we have a kind of reset. input [4:0] n; input [4:0] nplus1; input [4:0] p1; input [4:0] p2; input [4:0] p3; input [4:0] p4; input [4:0] p5; input [4:0] p6; input [4:0] p7; input [4:0] p8; input [4:0] p9; input [4:0] p10; input [4:0] p11; input [4:0] p12; input [4:0] p13; input [4:0] p14; input [4:0] p15; input [4:0] p16; input [4:0] p17; input [4:0] p18; output output_valid; output [`SUMSIZE:0] minsum; output [`SUMSIZE:0] maxsum; output [`SUMSIZE:0] psum; // For debugging. parameter NSTAGES = 2; reg [NSTAGES-1:0] pipestat = 0; assign output_valid = pipestat[NSTAGES-1]; wire psum_valid = pipestat[0]; // The sum of such permutation computed in cyclical Quet-Deutsch manner. reg [`SUMSIZE:0] psum = 0; wire [`SUMSIZE:0] wpsum; reg [`SUMSIZE:0] maxsum = 0; reg [`SUMSIZE:0] minsum = 0; // The highest non-zero term is "extra", not included among the permuted digits. perm19sum PERMSUM(wpsum, p1, (n > 1 ? p2 : (1 == n ? nplus1 : 5'b00000)), (n > 2 ? p3 : (2 == n ? nplus1 : 5'b00000)), (n > 3 ? p4 : (3 == n ? nplus1 : 5'b00000)), (n > 4 ? p5 : (4 == n ? nplus1 : 5'b00000)), (n > 5 ? p6 : (5 == n ? nplus1 : 5'b00000)), (n > 6 ? p7 : (6 == n ? nplus1 : 5'b00000)), (n > 7 ? p8 : (7 == n ? nplus1 : 5'b00000)), (n > 8 ? p9 : (8 == n ? nplus1 : 5'b00000)), (n > 9 ? p10 : (9 == n ? nplus1 : 5'b00000)), (n > 10 ? p11 : (10 == n ? nplus1 : 5'b00000)), (n > 11 ? p12 : (11 == n ? nplus1 : 5'b00000)), (n > 12 ? p13 : (12 == n ? nplus1 : 5'b00000)), (n > 13 ? p14 : (13 == n ? nplus1 : 5'b00000)), (n > 14 ? p15 : (14 == n ? nplus1 : 5'b00000)), (n > 15 ? p16 : (15 == n ? nplus1 : 5'b00000)), (n > 16 ? p17 : (16 == n ? nplus1 : 5'b00000)), (n > 17 ? p18 : (17 == n ? nplus1 : 5'b00000)), ((18 == n) ? nplus1 : 5'b00000), // p19 argument. ((18 == n) ? 5'b00000 : nplus1)); // The extra 'pn' argument. always @(posedge CLK) begin pipestat <= {pipestat,input_valid}; if(!psum_valid) begin if(input_valid) // input_valid rises, time to do "reset". begin psum <= 0; minsum <= 0; maxsum <= 0; end end else // psum_valid true. begin if(psum > maxsum) maxsum <= psum; if(~|minsum || (psum < minsum)) minsum <= psum; if(input_valid) psum <= wpsum; end end endmodule