| `timescale 1ns / 1ps |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| module falcon_KRED_tb(); |
|
|
| |
| localparam Q = 12289; |
| localparam K = 3; |
| localparam WIDTH = 14; |
| localparam CLK_PERIOD = 10; |
| localparam PIPELINE_DEPTH = 2; |
|
|
| |
| localparam NUM_RANDOM_TESTS = 100000; |
| localparam NUM_EDGE_TESTS = 1000; |
|
|
| |
| reg clk; |
| reg [WIDTH-1:0] a, b; |
| wire [WIDTH-1:0] c_mod_q; |
|
|
| |
| |
| reg [WIDTH-1:0] a_pipe [0:PIPELINE_DEPTH]; |
| reg [WIDTH-1:0] b_pipe [0:PIPELINE_DEPTH]; |
|
|
| |
| integer pass_count; |
| integer fail_count; |
| integer total_tests; |
| integer test_phase; |
| integer pipe_idx; |
| integer skip_count; |
|
|
| |
| integer seed; |
|
|
| |
| falcon_KRED DUT ( |
| .clk(clk), |
| .a(a), |
| .b(b), |
| .c_mod_q(c_mod_q) |
| ); |
|
|
| |
| initial begin |
| clk = 0; |
| forever #(CLK_PERIOD/2) clk = ~clk; |
| end |
|
|
| |
| always @(posedge clk) begin |
| a_pipe[0] <= a; |
| b_pipe[0] <= b; |
| for (pipe_idx = 1; pipe_idx <= PIPELINE_DEPTH; pipe_idx = pipe_idx + 1) begin |
| a_pipe[pipe_idx] <= a_pipe[pipe_idx-1]; |
| b_pipe[pipe_idx] <= b_pipe[pipe_idx-1]; |
| end |
| end |
|
|
| |
| function [WIDTH-1:0] expected_result; |
| input [WIDTH-1:0] in_a; |
| input [WIDTH-1:0] in_b; |
| reg [31:0] product; |
| reg [31:0] product_k; |
| begin |
| product = in_a * in_b; |
| product_k = (K * product) % Q; |
| |
| if (product_k == 0) |
| expected_result = 0; |
| else |
| expected_result = Q - product_k; |
| end |
| endfunction |
|
|
| |
| function [WIDTH-1:0] standard_modmul; |
| input [WIDTH-1:0] in_a; |
| input [WIDTH-1:0] in_b; |
| reg [31:0] product; |
| begin |
| product = in_a * in_b; |
| standard_modmul = product % Q; |
| end |
| endfunction |
|
|
| |
| task verify_result; |
| reg [WIDTH-1:0] test_a; |
| reg [WIDTH-1:0] test_b; |
| reg [WIDTH-1:0] expected; |
| begin |
| |
| test_a = a_pipe[PIPELINE_DEPTH]; |
| test_b = b_pipe[PIPELINE_DEPTH]; |
| expected = expected_result(test_a, test_b); |
| |
| if (c_mod_q == expected) begin |
| pass_count = pass_count + 1; |
| end else begin |
| fail_count = fail_count + 1; |
| if (fail_count <= 20) begin |
| $display("FAIL: a=%d, b=%d | DUT=%d, Expected=%d (standard modmul=%d)", |
| test_a, test_b, c_mod_q, expected, standard_modmul(test_a, test_b)); |
| end |
| end |
| total_tests = total_tests + 1; |
| end |
| endtask |
|
|
| |
| task run_test_with_pipeline; |
| input [WIDTH-1:0] test_a; |
| input [WIDTH-1:0] test_b; |
| begin |
| a = test_a; |
| b = test_b; |
| @(posedge clk); |
| #1; |
| end |
| endtask |
|
|
| |
| initial begin |
| $display("========================================================"); |
| $display("Falcon K-RED Testbench"); |
| $display("q = %d = %d * 2^12 + 1", Q, K); |
| $display("Testing: output %d * a * b (mod %d)", -K, Q); |
| $display("Pipeline depth: %d cycles (3 register stages + 1 input capture)", PIPELINE_DEPTH); |
| $display("========================================================"); |
| |
| |
| a = 0; |
| b = 0; |
| pass_count = 0; |
| fail_count = 0; |
| total_tests = 0; |
| seed = 12345; |
| |
| |
| for (pipe_idx = 0; pipe_idx <= PIPELINE_DEPTH; pipe_idx = pipe_idx + 1) begin |
| a_pipe[pipe_idx] = 0; |
| b_pipe[pipe_idx] = 0; |
| end |
| |
| |
| repeat(10) @(posedge clk); |
| #1; |
| |
| |
| |
| |
| $display("\n[Phase 1] Edge Case Tests..."); |
| test_phase = 1; |
| |
| |
| $display(" Testing a=0..."); |
| a = 0; |
| skip_count = 0; |
| for (b = 0; b < NUM_EDGE_TESTS && b < Q; b = b + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| |
| $display(" Testing b=0..."); |
| b = 0; |
| skip_count = 0; |
| for (a = 0; a < NUM_EDGE_TESTS && a < Q; a = a + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| |
| $display(" Testing a=1..."); |
| a = 1; |
| skip_count = 0; |
| for (b = 0; b < NUM_EDGE_TESTS && b < Q; b = b + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| |
| $display(" Testing b=1..."); |
| b = 1; |
| skip_count = 0; |
| for (a = 0; a < NUM_EDGE_TESTS && a < Q; a = a + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| |
| $display(" Testing a=q-1=%d...", Q-1); |
| a = Q - 1; |
| skip_count = 0; |
| for (b = 0; b < NUM_EDGE_TESTS && b < Q; b = b + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| |
| $display(" Testing b=q-1=%d...", Q-1); |
| b = Q - 1; |
| skip_count = 0; |
| for (a = 0; a < NUM_EDGE_TESTS && a < Q; a = a + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| |
| $display(" Testing squares a=b..."); |
| skip_count = 0; |
| for (a = 0; a < NUM_EDGE_TESTS && a < Q; a = a + 1) begin |
| b = a; |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| $display(" Phase 1 complete: %d passed, %d failed", pass_count, fail_count); |
| |
| |
| |
| |
| $display("\n[Phase 2] Power-of-2 Boundary Tests..."); |
| test_phase = 2; |
| |
| |
| $display(" Testing around 2^12 boundary..."); |
| skip_count = 0; |
| for (a = 4090; a < 4102 && a < Q; a = a + 1) begin |
| for (b = 4090; b < 4102 && b < Q; b = b + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| |
| $display(" Testing around 2^10 boundary..."); |
| skip_count = 0; |
| for (a = 1020; a < 1028 && a < Q; a = a + 1) begin |
| for (b = 1020; b < 1028 && b < Q; b = b + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| $display(" Phase 2 complete: %d passed, %d failed", pass_count, fail_count); |
| |
| |
| |
| |
| $display("\n[Phase 3] Random Tests (%d iterations)...", NUM_RANDOM_TESTS); |
| test_phase = 3; |
| |
| skip_count = 0; |
| repeat(NUM_RANDOM_TESTS) begin |
| a = $random(seed) % Q; |
| b = $random(seed) % Q; |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| $display(" Phase 3 complete: %d passed, %d failed", pass_count, fail_count); |
| |
| |
| |
| |
| $display("\n[Phase 4] Exhaustive Test (small range 0-255)..."); |
| test_phase = 4; |
| |
| skip_count = 0; |
| for (a = 0; a < 256; a = a + 1) begin |
| for (b = 0; b < 256; b = b + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| $display(" Phase 4 complete: %d passed, %d failed", pass_count, fail_count); |
| |
| |
| |
| |
| $display("\n[Phase 5] Large Value Tests (near q)..."); |
| test_phase = 5; |
| |
| skip_count = 0; |
| for (a = Q - 256; a < Q; a = a + 1) begin |
| for (b = Q - 256; b < Q; b = b + 1) begin |
| @(posedge clk); |
| #1; |
| skip_count = skip_count + 1; |
| if (skip_count > PIPELINE_DEPTH) verify_result(); |
| end |
| end |
| |
| |
| repeat(PIPELINE_DEPTH + 2) begin |
| @(posedge clk); |
| #1; |
| verify_result(); |
| end |
| |
| $display(" Phase 5 complete: %d passed, %d failed", pass_count, fail_count); |
| |
| |
| |
| |
| $display("\n========================================================"); |
| $display("FINAL RESULTS"); |
| $display("========================================================"); |
| $display("Total Tests: %d", total_tests); |
| $display("Passed: %d", pass_count); |
| $display("Failed: %d", fail_count); |
| $display("Pass Rate: %.2f%%", 100.0 * pass_count / total_tests); |
| |
| if (fail_count == 0) begin |
| $display("\n*** ALL TESTS PASSED! ***"); |
| $display("The falcon_KRED module correctly computes (-3 * a * b) mod 12289"); |
| $display("\nNote: To get standard (a*b mod q) result, pre-multiply twiddle"); |
| $display("factors by (-3)^(-1) mod 12289 = 4096"); |
| end else begin |
| $display("\n*** SOME TESTS FAILED! ***"); |
| $display("Please review the implementation."); |
| end |
| |
| $display("========================================================\n"); |
| |
| $stop; |
| end |
|
|
| |
| initial begin |
| #500000000; |
| $display("ERROR: Testbench timeout!"); |
| $stop; |
| end |
|
|
| endmodule |
|
|