aboutsummaryrefslogtreecommitdiff
path: root/src/tb/tb_sha1.v
diff options
context:
space:
mode:
Diffstat (limited to 'src/tb/tb_sha1.v')
-rw-r--r--src/tb/tb_sha1.v164
1 files changed, 79 insertions, 85 deletions
diff --git a/src/tb/tb_sha1.v b/src/tb/tb_sha1.v
index 9c339a0..f454034 100644
--- a/src/tb/tb_sha1.v
+++ b/src/tb/tb_sha1.v
@@ -8,7 +8,7 @@
// Author: Joachim Strombergson
// Copyright (c) 2014, NORDUnet A/S
// All rights reserved.
-//
+//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
@@ -37,13 +37,8 @@
//
//======================================================================
-//------------------------------------------------------------------
-// Simulator directives.
-//------------------------------------------------------------------
-`timescale 1ns/10ps
-
module tb_sha1();
-
+
//----------------------------------------------------------------
// Internal constant and parameter definitions.
//----------------------------------------------------------------
@@ -52,7 +47,7 @@ module tb_sha1();
parameter CLK_HALF_PERIOD = 1;
parameter CLK_PERIOD = CLK_HALF_PERIOD * 2;
-
+
parameter ADDR_NAME0 = 8'h00;
parameter ADDR_NAME1 = 8'h01;
parameter ADDR_VERSION = 8'h02;
@@ -66,7 +61,7 @@ module tb_sha1();
parameter ADDR_STATUS = 8'h09;
parameter STATUS_READY_BIT = 0;
parameter STATUS_VALID_BIT = 1;
-
+
parameter ADDR_BLOCK0 = 8'h10;
parameter ADDR_BLOCK1 = 8'h11;
parameter ADDR_BLOCK2 = 8'h12;
@@ -83,14 +78,14 @@ module tb_sha1();
parameter ADDR_BLOCK13 = 8'h1d;
parameter ADDR_BLOCK14 = 8'h1e;
parameter ADDR_BLOCK15 = 8'h1f;
-
+
parameter ADDR_DIGEST0 = 8'h20;
parameter ADDR_DIGEST1 = 8'h21;
parameter ADDR_DIGEST2 = 8'h22;
parameter ADDR_DIGEST3 = 8'h23;
parameter ADDR_DIGEST4 = 8'h24;
-
+
//----------------------------------------------------------------
// Register and Wire declarations.
//----------------------------------------------------------------
@@ -108,35 +103,35 @@ module tb_sha1();
reg [31 : 0] read_data;
reg [159 : 0] digest_data;
-
-
+
+
//----------------------------------------------------------------
// Device Under Test.
//----------------------------------------------------------------
sha1 dut(
.clk(tb_clk),
.reset_n(tb_reset_n),
-
+
.cs(tb_cs),
.we(tb_write_read),
-
+
.address(tb_address),
.write_data(tb_data_in),
.read_data(tb_data_out),
- .error(tb_error)
+ .error(tb_error)
);
-
+
//----------------------------------------------------------------
// clk_gen
//
- // Clock generator process.
+ // Clock generator process.
//----------------------------------------------------------------
- always
+ always
begin : clk_gen
#CLK_HALF_PERIOD tb_clk = !tb_clk;
end // clk_gen
-
+
//----------------------------------------------------------------
// sys_monitor
@@ -147,7 +142,7 @@ module tb_sha1();
begin
dump_core_state();
end
-
+
if (DEBUG_TOP)
begin
dump_top_state();
@@ -157,13 +152,13 @@ module tb_sha1();
cycle_ctr = cycle_ctr + 1;
end
-
+
//----------------------------------------------------------------
// dump_top_state()
//
// Dump state of the the top of the dut.
//----------------------------------------------------------------
- task dump_top_state();
+ task dump_top_state;
begin
$display("State of top");
$display("-------------");
@@ -172,24 +167,21 @@ module tb_sha1();
$display("address = 0x%02x, write_data = 0x%08x", dut.address, dut.write_data);
$display("error = 0x%01x, read_data = 0x%08x", dut.error, dut.read_data);
$display("");
-
+
$display("Control and status flags:");
- $display("init = 0x%01x, next = 0x%01x, ready = 0x%01x",
+ $display("init = 0x%01x, next = 0x%01x, ready = 0x%01x",
dut.init_reg, dut.next_reg, dut.ready_reg);
$display("");
$display("block registers:");
- $display("block0_reg = 0x%08x, block1_reg = 0x%08x, block2_reg = 0x%08x, block3_reg = 0x%08x",
- dut.block0_reg, dut.block1_reg, dut.block2_reg, dut.block3_reg);
-
- $display("block4_reg = 0x%08x, block5_reg = 0x%08x, block6_reg = 0x%08x, block7_reg = 0x%08x",
- dut.block4_reg, dut.block5_reg, dut.block6_reg, dut.block7_reg);
-
- $display("block8_reg = 0x%08x, block9_reg = 0x%08x, block10_reg = 0x%08x, block11_reg = 0x%08x",
- dut.block8_reg, dut.block9_reg, dut.block10_reg, dut.block11_reg);
-
- $display("block12_reg = 0x%08x, block13_reg = 0x%08x, block14_reg = 0x%08x, block15_reg = 0x%08x",
- dut.block12_reg, dut.block13_reg, dut.block14_reg, dut.block15_reg);
+ $display("block0 = 0x%08x, block1 = 0x%08x, block2 = 0x%08x, block3 = 0x%08x",
+ dut.block_reg[00], dut.block_reg[01], dut.block_reg[02], dut.block_reg[03]);
+ $display("block4 = 0x%08x, block5 = 0x%08x, block6 = 0x%08x, block7 = 0x%08x",
+ dut.block_reg[04], dut.block_reg[05], dut.block_reg[06], dut.block_reg[07]);
+ $display("block8 = 0x%08x, block9 = 0x%08x, block10 = 0x%08x, block11 = 0x%08x",
+ dut.block_reg[08], dut.block_reg[09], dut.block_reg[10], dut.block_reg[11]);
+ $display("block12 = 0x%08x, block13 = 0x%08x, block14 = 0x%08x, block15 = 0x%08x",
+ dut.block_reg[12], dut.block_reg[13], dut.block_reg[14], dut.block_reg[15]);
$display("");
$display("Digest registers:");
@@ -198,59 +190,59 @@ module tb_sha1();
end
endtask // dump_top_state
-
+
//----------------------------------------------------------------
// dump_core_state()
//
// Dump the state of the core inside the dut.
//----------------------------------------------------------------
- task dump_core_state();
+ task dump_core_state;
begin
$display("State of core");
$display("-------------");
$display("Inputs and outputs:");
- $display("init = 0x%01x, next = 0x%01x",
+ $display("init = 0x%01x, next = 0x%01x",
dut.core.init, dut.core.next);
$display("block = 0x%0128x", dut.core.block);
- $display("ready = 0x%01x, valid = 0x%01x",
+ $display("ready = 0x%01x, valid = 0x%01x",
dut.core.ready, dut.core.digest_valid);
$display("digest = 0x%040x", dut.core.digest);
- $display("H0_reg = 0x%08x, H1_reg = 0x%08x, H2_reg = 0x%08x, H3_reg = 0x%08x, H4_reg = 0x%08x",
+ $display("H0_reg = 0x%08x, H1_reg = 0x%08x, H2_reg = 0x%08x, H3_reg = 0x%08x, H4_reg = 0x%08x",
dut.core.H0_reg, dut.core.H1_reg, dut.core.H2_reg, dut.core.H3_reg, dut.core.H4_reg);
$display("");
-
+
$display("Control signals and counter:");
$display("sha1_ctrl_reg = 0x%01x", dut.core.sha1_ctrl_reg);
- $display("digest_init = 0x%01x, digest_update = 0x%01x",
+ $display("digest_init = 0x%01x, digest_update = 0x%01x",
dut.core.digest_init, dut.core.digest_update);
- $display("state_init = 0x%01x, state_update = 0x%01x",
+ $display("state_init = 0x%01x, state_update = 0x%01x",
dut.core.state_init, dut.core.state_update);
- $display("first_block = 0x%01x, ready_flag = 0x%01x, w_init = 0x%01x",
+ $display("first_block = 0x%01x, ready_flag = 0x%01x, w_init = 0x%01x",
dut.core.first_block, dut.core.ready_flag, dut.core.w_init);
- $display("round_ctr_inc = 0x%01x, round_ctr_rst = 0x%01x, round_ctr_reg = 0x%02x",
+ $display("round_ctr_inc = 0x%01x, round_ctr_rst = 0x%01x, round_ctr_reg = 0x%02x",
dut.core.round_ctr_inc, dut.core.round_ctr_rst, dut.core.round_ctr_reg);
$display("");
$display("State registers:");
- $display("a_reg = 0x%08x, b_reg = 0x%08x, c_reg = 0x%08x, d_reg = 0x%08x, e_reg = 0x%08x",
+ $display("a_reg = 0x%08x, b_reg = 0x%08x, c_reg = 0x%08x, d_reg = 0x%08x, e_reg = 0x%08x",
dut.core.a_reg, dut.core.b_reg, dut.core.c_reg, dut.core.d_reg, dut.core.e_reg);
- $display("a_new = 0x%08x, b_new = 0x%08x, c_new = 0x%08x, d_new = 0x%08x, e_new = 0x%08x",
+ $display("a_new = 0x%08x, b_new = 0x%08x, c_new = 0x%08x, d_new = 0x%08x, e_new = 0x%08x",
dut.core.a_new, dut.core.b_new, dut.core.c_new, dut.core.d_new, dut.core.e_new);
$display("");
$display("State update values:");
- $display("f = 0x%08x, k = 0x%08x, t = 0x%08x, w = 0x%08x,",
+ $display("f = 0x%08x, k = 0x%08x, t = 0x%08x, w = 0x%08x,",
dut.core.state_logic.f, dut.core.state_logic.k, dut.core.state_logic.t, dut.core.w);
$display("");
end
endtask // dump_core_state
-
-
+
+
//----------------------------------------------------------------
// reset_dut()
//----------------------------------------------------------------
- task reset_dut();
+ task reset_dut;
begin
$display("*** Toggle reset.");
tb_reset_n = 0;
@@ -259,19 +251,19 @@ module tb_sha1();
end
endtask // reset_dut
-
+
//----------------------------------------------------------------
// init_sim()
//
// Initialize all counters and testbed functionality as well
// as setting the DUT inputs to defined values.
//----------------------------------------------------------------
- task init_sim();
+ task init_sim;
begin
cycle_ctr = 32'h00000000;
error_ctr = 32'h00000000;
tc_ctr = 32'h00000000;
-
+
tb_clk = 0;
tb_reset_n = 0;
tb_cs = 0;
@@ -281,13 +273,13 @@ module tb_sha1();
end
endtask // init_dut
-
+
//----------------------------------------------------------------
// display_test_result()
//
// Display the accumulated test results.
//----------------------------------------------------------------
- task display_test_result();
+ task display_test_result;
begin
if (error_ctr == 0)
begin
@@ -300,8 +292,8 @@ module tb_sha1();
end
end
endtask // display_test_result
-
-
+
+
//----------------------------------------------------------------
// wait_ready()
//
@@ -312,17 +304,17 @@ module tb_sha1();
// when the dut is actively processing and will in fact at some
// point set the flag.
//----------------------------------------------------------------
- task wait_ready();
+ task wait_ready;
begin
read_data = 0;
-
+
while (read_data == 0)
begin
read_word(ADDR_STATUS);
end
end
endtask // wait_ready
-
+
//----------------------------------------------------------------
// read_word()
@@ -347,7 +339,7 @@ module tb_sha1();
end
end
endtask // read_word
-
+
//----------------------------------------------------------------
// write_word()
@@ -362,7 +354,7 @@ module tb_sha1();
$display("*** Writing 0x%08x to 0x%02x.", word, address);
$display("");
end
-
+
tb_address = address;
tb_data_in = word;
tb_cs = 1;
@@ -373,7 +365,7 @@ module tb_sha1();
end
endtask // write_word
-
+
//----------------------------------------------------------------
// write_block()
//
@@ -400,13 +392,13 @@ module tb_sha1();
end
endtask // write_block
-
+
//----------------------------------------------------------------
// check_name_version()
//
// Read the name and version from the DUT.
//----------------------------------------------------------------
- task check_name_version();
+ task check_name_version;
reg [31 : 0] name0;
reg [31 : 0] name1;
reg [31 : 0] version;
@@ -426,7 +418,7 @@ module tb_sha1();
version[31 : 24], version[23 : 16], version[15 : 8], version[7 : 0]);
end
endtask // check_name_version
-
+
//----------------------------------------------------------------
// read_digest()
@@ -434,7 +426,7 @@ module tb_sha1();
// Read the digest in the dut. The resulting digest will be
// available in the global variable digest_data.
//----------------------------------------------------------------
- task read_digest();
+ task read_digest;
begin
read_word(ADDR_DIGEST0);
digest_data[159 : 128] = read_data;
@@ -448,8 +440,8 @@ module tb_sha1();
digest_data[31 : 0] = read_data;
end
endtask // read_digest
-
-
+
+
//----------------------------------------------------------------
// single_block_test()
//
@@ -460,8 +452,8 @@ module tb_sha1();
input [159 : 0] expected
);
begin
- $display("*** TC%01d - Single block test started.", tc_ctr);
-
+ $display("*** TC%01d - Single block test started.", tc_ctr);
+
write_block(block);
write_word(ADDR_CTRL, CTRL_INIT_VALUE);
#(CLK_PERIOD);
@@ -479,12 +471,12 @@ module tb_sha1();
$display("TC%01d: Got: 0x%040x", tc_ctr, digest_data);
error_ctr = error_ctr + 1;
end
- $display("*** TC%01d - Single block test done.", tc_ctr);
+ $display("*** TC%01d - Single block test done.", tc_ctr);
tc_ctr = tc_ctr + 1;
end
endtask // single_block_test
-
-
+
+
//----------------------------------------------------------------
// double_block_test()
//
@@ -498,7 +490,7 @@ module tb_sha1();
input [159 : 0] expected1
);
begin
- $display("*** TC%01d - Double block test started.", tc_ctr);
+ $display("*** TC%01d - Double block test started.", tc_ctr);
// First block
write_block(block0);
@@ -525,7 +517,7 @@ module tb_sha1();
#(CLK_PERIOD);
wait_ready();
read_digest();
-
+
if (digest_data == expected1)
begin
$display("TC%01d final block: OK.", tc_ctr);
@@ -538,15 +530,18 @@ module tb_sha1();
error_ctr = error_ctr + 1;
end
- $display("*** TC%01d - Double block test done.", tc_ctr);
+ $display("*** TC%01d - Double block test done.", tc_ctr);
tc_ctr = tc_ctr + 1;
end
endtask // double_block_test
-
+
//----------------------------------------------------------------
// sha1_test
- // The main test functionality.
+ // The main test functionality.
+ //
+ // Test cases taken from:
+ // http://csrc.nist.gov/groups/ST/toolkit/documents/Examples/SHA_All.pdf
//----------------------------------------------------------------
initial
begin : sha1_test
@@ -563,7 +558,7 @@ module tb_sha1();
init_sim();
reset_dut();
check_name_version();
-
+
// TC1: Single block message: "abc".
tc1 = 512'h61626380000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000018;
res1 = 160'ha9993e364706816aba3e25717850c26c9cd0d89d;
@@ -573,11 +568,11 @@ module tb_sha1();
// "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq"
tc2_1 = 512'h6162636462636465636465666465666765666768666768696768696A68696A6B696A6B6C6A6B6C6D6B6C6D6E6C6D6E6F6D6E6F706E6F70718000000000000000;
res2_1 = 160'hf4286818c37b27ae0408f581846771484a566572;
-
+
tc2_2 = 512'h000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001C0;
res2_2 = 160'h84983e441c3bd26ebaae4aa1f95129e5e54670f1;
double_block_test(tc2_1, res2_1, tc2_2, res2_2);
-
+
display_test_result();
$display("*** Simulation done. ***");
$finish;
@@ -587,4 +582,3 @@ endmodule // tb_sha1
//======================================================================
// EOF tb_sha1.v
//======================================================================
-