| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417 |
- module floating_add #(parameter N=16, M=4)(input_1, input_2, sum, diff, clk, reset);
- input logic [N-1:0] input_1, input_2;
- input logic clk, reset;
- output logic [N-1:0] sum;
- output logic [M:0] diff;
- logic flag_a;
- logic flag_b;
- logic [M:0] abs;
- logic [N-3-M:0] res;
-
- logic [N-1:0] D0 [7:0];
- logic [N-1:0] Q0 [7:0];
- logic [N-1:0] Q1 [7:0];
- logic [N-1:0] Q2 [7:0];
-
- // sign_x = x[N-1]
- // exponent_x = x[N-2:N-2-M]
- // mantissa_x = x[N-3-M:0]
-
- //First pipeline stage
- always_comb
- begin
- D0[0] = input_1;
- D0[1] = input_2;
- D0[2] = 0; // sum
- D0[3] = 0; // diff
- D0[4] = 0; // flag_a
- D0[5] = 0; // flag_b
- D0[6] = 0; // abs
- D0[7] = 0; // res
- end
- pipe#(.N(N-1), .K(7)) pipe0(.clk(clk), .reset(reset), .D(D0), .Q(Q0));
-
-
- always_comb
- begin
- if (Q0[0][N-2:N-2-M] > Q0[1][N-2:N-2-M]) // If input 1 has the bigger exponent
- begin
- // Flags input a as larger and calculates the absolute difference
- Q0[4] = 1;
- Q0[5] = 0;
- Q0[6] = Q0[0][N-2:N-2-M] - Q0[1][N-2:N-2-M];
- // ASsigning overall sign of the output
- Q0[2][N-1] = Q0[0][N-1];
- // Sets output to have the same exponent
- Q0[2][N-2:N-2-M] = Q0[0][N-2:N-2-M];
- end
- else if (Q0[1][N-2:N-2-M] > Q0[0][N-2:N-2-M]) // If input 2 has the bigger exponent
- begin
- // Similarly flags input b as larger and calculates the absolute difference
- Q0[4] = 0;
- Q0[5] = 1;
- Q0[6] = Q0[1][N-2:N-2-M] - Q0[0][N-2:N-2-M];
- // ASsigning overall sign of the output
- Q0[2][N-1] = Q0[1][N-1];
- // Sets ouput to have the same exponent
- Q0[2][N-2:N-2-M] = Q0[1][N-2:N-2-M];
- end
- else
- begin
- // THe condition that both inputs have the same exponent
- Q0[4] = 1;
- Q0[5] = 1;
- Q0[6] = 0;
- // ASsigning overall sign of the output based on size of the mantissa
- if (Q0[0][N-3-M:0] >= Q0[1][N-3-M:0]) Q0[2][N-1] = Q0[0][N-1];
- else Q0[2][N-1] = Q0[1][N-1];
- Q0[2][N-2:N-2-M] = Q0[0][N-2:N-2-M];
- end
- Q0[3] = Q0[6];
- end
-
- //Second pipeline stage 1
- pipe#(.N(N-1), .K(7)) pipe1(.clk(clk), .reset(reset), .D(Q0), .Q(Q1));
-
- always_comb
- begin
- // Condition for overflow is that it sets the output to the larger input
- if (Q1[6] > N-2-M) // Because size of mantissa is 10 bits and shifting by 10 would give 0
- begin
- if (Q1[4] & ~Q1[5]) Q1[2] = Q1[0]; // input 1 is larger and is translated to output
- else if (~Q1[4] & Q1[5]) Q1[2] = Q1[1]; // input 2 is larger and is translated to output
- else // exponents are the same
- begin
- if (Q1[6][N-3-M:0] >= Q1[1][N-3-M:0]) Q1[2] = Q1[0];// input 1 has the bigger mantissa
- else Q1[2] = Q1[1]; // input 2 has the bigger mantissa
- end
- end
- else
- begin
- // Shifts the smaller input's mantissa to the right based on abs
- if (Q1[4] & ~Q1[5])// If input 1 has the larger exponent
- begin
- // If the signs of both inputs are the same you add, otherwise you subtract
- if (Q1[0][N-1] == Q1[1][N-1])
- begin
- Q1[7] = Q1[0][N-3-M:0] + (Q1[1][N-3-M:0] >> Q1[6]-1); // Sum the mantissa
- Q1[2][N-3-M:0] = Q1[7];
- end
- else
- begin
- Q1[7] = Q1[0][N-3-M:0] - (Q1[1][N-3-M:0] >> Q1[6]-1); // Subtract the mantissas
- sum[N-3-M:0] = res;
- end
- end
- else if (~Q1[4] & Q1[5])
- begin
- // If the signs of both inputs are the same you add, otherwise you subtract
- if (Q1[0][N-1] == Q1[1][N-1])
- begin
- Q1[7] = (Q1[0][N-3-M:0] >> Q1[6]-1) + Q1[1][N-3-M:0]; // Sum the mantissa
- Q1[2][N-3-M:0] = Q1[7];
- end
- else
- begin
- Q1[7] = Q1[1][N-3-M:0] - (Q1[0][N-3-M:0] >> Q1[6]-1); // Subtract the mantissas
- Q1[2][N-3-M:0] = Q1[7];
- end
- end
- else
- begin
- if (Q1[0][N-1] == Q1[1][N-1]) // If exponents and signs equal
- begin
- Q1[7] = Q1[0][N-3-M:0] + Q1[1][N-3-M:0]; // Sum the mantissa
- Q1[2][N-3-M:0] = Q1[7];
- end
- else // In this case it will be a subtraction
- begin
- if (Q1[0][N-3-M:0] > Q1[1][N-3-M:0]) // Which has the larger mantissa
- begin
- Q1[7] = Q1[0][N-3-M:0] - Q1[1][N-3-M:0]; // Subtract the mantissa
- Q1[2][N-3-M:0] = Q1[7];
- end
- else if (Q1[0][N-3-M:0] < Q1[1][N-3-M:0])
- begin
- Q1[7] = Q1[1][N-3-M:0] - Q1[0][N-3-M:0]; // Subtract the mantissa
- Q1[2][N-3-M:0] = Q1[7];
- end
- else Q1[7] = 0; // Both the exponent and the mantissa are equal so subtraction leads to 0
- Q1[2][N-3-M:0] = Q1[7];
- end
- end
- end
- end
-
- // Final pipeline stage
- pipe#(.N(N-1), .K(7)) pipe2(.clk(clk), .reset(reset), .D(Q1), .Q(Q2));
- assign sum = Q2[2];
- assign diff = Q2[3];
- endmodule : floating_add
- module floating_product #(parameter N=16, M=4)(input_1, input_2, product, clk, reset);
- input logic [N-1:0] input_1, input_2;
- input logic clk, reset;
- output logic [N-1:0] product;
-
- logic [2*N-1:0] D0 [7:0];
- logic [2*N-1:0] Q0 [7:0];
- logic [2*N-1:0] Q1 [7:0];
- logic [2*N-1:0] Q2 [7:0];
-
- always_comb
- begin
- D0[0][N-1:0] = input_1;
- D0[1][N-1:0] = input_2;
- D0[2] = 0; // product (output)
- D0[3] = 0; // multiple for multiplying the mantissa
- D0[4] = 0; // flag for return NaN
- D0[5] = 0; // flag for return infinity
- D0[6] = 0; // flag for return zero
- D0[7][N-3-M:0] = input_1[N-3-M:0]; // for storing the mantissa of input 1
- end
- // sign_x = x[N-1]
- // exponent_x = x[N-2:N-2-M]
- // mantissa_x = x[N-3-M:0]
-
- pipe #(.N(2*N-1), .K(7)) pipe0(.clk(clk), .reset(reset), .D(D0), .Q(Q0));
-
- always_comb
- begin
- // if input_1 or input_2 is NaN then return NaN
- if ((Q0[0][N-2:N-2-M] == (1<<M) && Q0[0][N-3-M:0] != 0) || (Q1[0][N-2:N-2-M] == (1<<M) && Q1[0][N-3-M:0] != 0))
- begin
- Q0[2][N-1] = 1;
- Q0[2][N-2:N-2-M] = (1 << (M+1)) - 1;
- Q0[2][N-3-M] = 1;
- Q0[2][N-4-M:0] = 0;
- Q0[4][0] = 1;
- end
- // if input 1 is infinity then return infinity
- else if (Q0[0][N-2:N-2-M] == (1<<M))
- begin
- Q0[2][N-1] = Q0[0][N-1] ^ Q0[1][N-1];
- Q0[2][N-2:N-2-M] = (1 << (M+1)) - 1;
- Q0[2][N-3-M:0] = 0;
- Q0[5] = 1;
- // if input 2 is zero then return NaN
- if (($signed(Q0[1][N-2:N-2-M]) == (-1*((1<<M)-1))) && (Q0[1][N-3-M:0] == 0))
- begin
- Q0[2][N-1] = 1;
- Q0[2][N-2:N-2-M] = (1 << (M+1)) - 1;
- Q0[2][N-3-M] = 1;
- Q0[2][N-4-M:0] = 0;
- Q0[4][0] = 1;
- end
- end
- // if input 2 is infinity then return infinity
- else if (Q0[1][N-2:N-2-M] == (1<<M))
- begin
- Q0[2][N-1] = Q0[0][N-1] ^ Q0[1][N-1];
- Q0[2][N-2:N-2-M] = (1 << (M+1)) - 1;
- Q0[2][N-3-M:0] = 0;
- Q0[5][0] = 1;
- // if input 1 is zero then return NaN
- if (($signed(Q0[0][N-2:N-2-M]) == (-1*((1<<M)-1))) && (Q0[0][N-3-M:0] == 0))
- begin
- Q0[2][N-1] = 1;
- Q0[2][N-2:N-2-M] = (1 << (M+1)) - 1;
- Q0[2][N-3-M] = 1;
- Q0[2][N-4-M:0] = 0;
- Q0[4][0] = 1;
- end
- end
- // if input 1 is zero then return zero
- else if (($signed(Q0[0][N-2:N-2-M]) == (-1*((1<<M)-1))) && (Q0[0][N-3-M:0] == 0))
- begin
- Q0[2][N-1] = Q0[0][N-1] ^ Q0[1][N-1];
- Q0[2][N-2:N-2-M] = 0;
- Q0[2][N-3-M:0] = 0;
- Q0[6][0] = 1;
- end
- // if input 2 is zero then return zero
- else if (($signed(Q0[1][N-2:N-2-M]) == (-1*((1<<M)-1))) && (Q0[1][N-3-M:0] == 0))
- begin
- Q0[2][N-1] = Q0[0][N-1] ^ Q0[1][N-1];
- Q0[2][N-2:N-2-M] = 0;
- Q0[2][N-3-M:0] = 0;
- Q0[6][0] = 1;
- end
- end
-
- pipe #(.N(2*N-1), .K(7)) pipe1(.clk(clk), .reset(reset), .D(Q0), .Q(Q1));
-
- always_comb
- begin
- // If none of the return flags have been triggered
- if ((Q1[4][0] && Q1[5][0] && Q1[6][0]) != 1)
- begin
- // if msb of input 1 mantissa is not 1 then shift left by 1 and reduce exponent by 1
- if (Q1[0][N-3-M] != 1)
- begin
- Q1[0][N-3-M:0] = Q1[0][N-3-M:0] << 1;
- Q1[0][N-2:N-2-M] = Q1[0][N-2:N-2-M] - 1;
- end
- // if msb of input 2 mantissa is not 1 then shift left by 1 and reduce exponent by 1
- if (Q1[1][N-3-M] != 1)
- begin
- Q1[1][N-3-M:0] = Q1[1][N-3-M:0] << 1;
- Q1[1][N-2:N-2-M] = Q1[1][N-2:N-2-M] - 1;
- end
- Q1[2][N-1] = Q1[2][N-1] ^ Q1[2][N-1]; // ouput sign = input_1 sign xor input_2 sign
- Q1[2][N-2:N-2-M] = Q1[0][N-2:N-2-M] + Q1[1][N-2:N-2-M] - (1<<M); // out exp = in1 exp + in2 exp - 2**M
- // multiplying mantissa
- for (int i = 0; i<N-2-M; i++)
- begin
- // multiplying each digit of input 2 by all of input 1 shifting the bits left each time and adding result to the array
- if (Q1[1][i] == 1)
- begin
- Q1[3] = Q1[3] +(Q1[7]<<i);
- end
- else
- begin
- Q1[3] = Q1[3] + 0;
- end
- end
- // Assigning the top set of bits to the mantissa of the output
- Q1[2][N-3-M:0] = Q1[3][2*N-1:2*N-1-N-3-M];
- end
- end
-
- pipe #(.N(2*N-1), .K(7)) pipe2(.clk(clk), .reset(reset), .D(Q1), .Q(Q2));
- assign product = Q2[2][N-1:0];
-
- endmodule : floating_product
- module pipe #(parameter N, K)(clk, reset, Q, D);
- input logic clk, reset;
- input logic [N:0] D [K:0];
- output reg [N:0] Q [K:0];
- reg [N:0] in_pipe [K:0];
-
- always_ff @(posedge clk or negedge reset)
- begin
- if(reset)
- begin
- in_pipe <= '{default:0};
- Q <= '{default:0};
- end
- else
- begin
- in_pipe <= D;
- Q <= in_pipe;
- end
- end
- endmodule : pipe
- module floating_tb;
- reg reset, clk;
- logic [15:0] input_a, input_b, result_add, result_mult;
- logic [4:0] diff;
- logic [15:0] expected_add, expected_mult;
- floating_add adder1(.input_1(input_a), .input_2(input_b), .sum(result_add), .diff(diff), .clk(clk), .reset(reset));
- floating_product multiplier1(.input_1(input_a), .input_2(input_b), .product(result_mult), .clk(clk), .reset(reset));
-
- initial forever #5 clk = ~clk;
- localparam PIPELINES = 3;
- reg [15:0] test_mem [29:0][3:0];
- initial $readmemh("scripts/fp16_test.hex", test_mem);
- initial begin
- static int num_err = 0;
- static int num_tests = $size(test_mem) * 2;
- clk = 0;
- reset = 1;
-
- #15;
- reset = 0;
- expected_add = 0;
- expected_mult = 0;
- for (int i=0; i < $size(test_mem)+PIPELINES; i++) begin
- if(i >= PIPELINES) begin
- expected_add = test_mem[i-PIPELINES][2];
- expected_mult = test_mem[i-PIPELINES][3];
- end
- input_a = test_mem[i][0];
- input_b = test_mem[i][1];
- #10;
- if(result_add != test_mem[i][2]) begin
- if(num_err < 20)
- $display("FAIL ADD: %H + %H = %H, expected %H", input_a, input_b, result_add, test_mem[i][2]);
- num_err = num_err + 1;
- end
- if(result_mult != test_mem[i][3]) begin
- if(num_err < 20)
- $display("FAIL MULTIPLY: %H + %H = %H, expected %H", input_a, input_b, result_mult, test_mem[i][3]);
- num_err = num_err + 1;
- end
- end
- expected_add = 0;
- expected_mult = 0;
- #50;
- $display("Passed %d of %d tests", num_tests-num_err, num_tests);
- $finish();
- end
- endmodule : floating_tb
- module floating32_tb;
- reg reset, clk;
- logic [31:0] input_a, input_b, result_add, result_mult;
- floating_add#(.N(32), .M(8)) add0(
- .input_1(input_a), .input_2(input_b), .sum(result_add), .diff()
- );
- floating_product#(.N(32), .M(8)) mult0(
- .input_1(input_a), .input_2(input_b), .product(result_mult)
- );
- reg [31:0] test_mem [29:0][3:0];
- initial $readmemh("scripts/fp32_test.hex", test_mem);
- initial begin
- static int num_err = 0;
- static int num_tests = $size(test_mem) * 2;
- for (int i=0; i < $size(test_mem); i++) begin
- input_a = test_mem[i][0];
- input_b = test_mem[i][1];
- #10;
- if(result_add != test_mem[i][2]) begin
- if(num_err < 20)
- $display("FAIL ADD: %H + %H = %H, expected %H", input_a, input_b, result_add, test_mem[i][2]);
- num_err = num_err + 1;
- end
- if(result_mult != test_mem[i][3]) begin
- if(num_err < 20)
- $display("FAIL MULTIPLY: %H + %H = %H, expected %H", input_a, input_b, result_mult, test_mem[i][3]);
- num_err = num_err + 1;
- end
- end
- $display("Passed %d of %d tests", num_tests-num_err, num_tests);
- $finish();
- end
- endmodule : floating32_tb
|