tb.v 4.04 KB
// This confidential and propriety software may be used
// only as authorized by a licensing agreement from
// Ocean Logic Pty Ltd http:/www.ocean-logic.com
// 
// In the event of publication, the following notice is
// applicable
// 
// (C) COPYRIGHT 1996-2002 Ocean Logic Pty Ltd
// ALL RIGHTS RESERVED
//
// the entire notice must be reproduced on all
// authorized copies
// 
// File : tb.v
// Author : Vincenzo Liguori
// Date : 02-07-18
// Version 1.0
// Abstract :
// Testbench for the AES decryption
// key expander core.
// Keys taken from the NIST special
// publication 800-38A, AES CBC test
// 
// Modification history :
// Date      by  Version  Change description
// -------------------------------------------------
// 02-07-18  VL   1.0     Original

`timescale 1ns/1ns

module tb;

`define tclk 10

reg clk, rstn;
reg go, en;
reg [1:0] ksize;
reg [31:0] kin;
 
wire [31:0] key;
wire [5:0] addr;
wire key_req;
wire key_last;

// Key data storage
reg [0:31] memk [0:59];
reg [0:31] memk2 [0:59];

integer i;

// Main test bench
initial begin
  clk = 0;
  rstn = 1;
  en = 1;
  go = 0;
  ksize = 0; // Set 128 bit key size
  kin = 0;
  #(`tclk/2+1);
  #`tclk rstn=0; // Reset pulse
  #`tclk rstn=1;
  #(2*`tclk);

  go = 1; // Start the core

  // Expand 128 bit key
  kin = 32'h2b7e1516;
  #`tclk;
  kin = 32'h28aed2a6;
  #`tclk;
  kin = 32'habf71588;
  #`tclk;
  kin = 32'h09cf4f3c;
  #(41*`tclk);

  // Expand 192 bit key
  ksize = 1; // Set 192 bit key size
  kin = 32'h8e73b0f7;
  #`tclk;
  kin = 32'hda0e6452;
  #`tclk;
  kin = 32'hc810f32b;
  #`tclk;
  kin = 32'h809079e5;
  #`tclk;
  kin = 32'h62f8ead2;
  #`tclk;
  kin = 32'h522c6b7b;
  #(47*`tclk);

  // Expand 256 bit key
  ksize = 2; // Set 256 bit key size
  kin = 32'h603deb10;
  #`tclk;
  kin = 32'h15ca71be;
  #`tclk;
  kin = 32'h2b73aef0;
  #`tclk;
  kin = 32'h857d7781;
  #`tclk;
  kin = 32'h1f352c07;
  #`tclk;
  kin = 32'h3b6108d7;
  #`tclk;
  kin = 32'h2d9810a3;
  #`tclk;
  kin = 32'h0914dff4;
  #`tclk;

end

// Checking process
initial @(posedge go) begin

  // Check 128 bit key expansion

  // Store the expanding key
  // according to the addr index
  for(i=0;i<44;i=i+1) begin
    @(posedge clk);
    memk[addr]= key;
  end

  // Load the reference file
  $readmemh("dk128.dat",memk2);

  // memk[] is read backward and
  // checked versus the reference
  for(i=43;i>=0;i=i-1) begin
    // $display("%x %x",memk[i],memk2[43-i]);
    if(memk[i] != memk2[43-i]) begin
      // $display("Key expansion failed");
      $finish;
    end
  end

  $display("128 bit key expanded correctly");

  // Check 192 bit key expansion

  // Store the expanding key
  // according to the addr index
  for(i=0;i<52;i=i+1) begin
    @(posedge clk);
    memk[addr]= key;
  end

  // Load the reference file
  $readmemh("dk192.dat",memk2);

  // memk[] is read backward and
  // checked versus the reference
  for(i=51;i>=0;i=i-1) begin
    // $display("%x %x",memk[i],memk2[51-i]);
    if(memk[i] != memk2[51-i]) begin
      $display("Key expansion failed");
      $finish;
    end
  end

  $display("192 bit key expanded correctly");

  // Check 256 bit key expansion

  // Store the expanding key
  // according to the addr index
  for(i=0;i<60;i=i+1) begin
    @(posedge clk);
    memk[addr]= key;
  end

  // Load the reference file
  $readmemh("dk256.dat",memk2);

  // memk[] is read backward and
  // checked versus the reference
  for(i=59;i>=0;i=i-1) begin
    // $display("%x %x",memk[i],memk2[59-i]);
    if(memk[i] != memk2[59-i]) begin
      $display("Key expansion failed");
      $finish;
    end
  end

  $display("256 bit key expanded correctly");

  $display("Test successful");
  $finish;
end

// System clock
always #(`tclk/2) clk=~clk;

// Key expander module instantiation
kexp_d u_kexp_d(
.clk(clk),
.rstn(rstn),
.en(en),
.go(go),
.ksize(ksize),
.kin(kin),
.key(key),
.addr(addr),
.key_req(key_req),
.key_last(key_last)
);

endmodule