aboutsummaryrefslogtreecommitdiff
path: root/src/tb/tb_chacha_qr.v
blob: 66a790cfda4bf076a333e5b2c7b05317c05dec23 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
//======================================================================
//
// tb_chacha_qr.v
// --------------
// Testbench for the Chacha stream cipher quarerround (QR) module.
//
//
// Author: Joachim Strombergson
// Copyright (c) 2018, 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:
// - Redistributions of source code must retain the above copyright notice,
//   this list of conditions and the following disclaimer.
//
// - Redistributions in binary form must reproduce the above copyright
//   notice, this list of conditions and the following disclaimer in the
//   documentation and/or other materials provided with the distribution.
//
// - Neither the name of the NORDUnet nor the names of its contributors may
//   be used to endorse or promote products derived from this software
//   without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
// IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
// TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
// PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
// TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//======================================================================

module tb_chacha_qr();

  //----------------------------------------------------------------
  // Internal constant and parameter definitions.
  //----------------------------------------------------------------
  parameter CLK_HALF_PERIOD = 2;
  parameter CLK_PERIOD = 2 * CLK_HALF_PERIOD;


  //----------------------------------------------------------------
  // Register and Wire declarations.
  //----------------------------------------------------------------
  reg [31 : 0] cycle_ctr;
  reg [31 : 0] error_ctr;
  reg [31 : 0] tc_ctr;

  reg tb_clk;
  reg tb_reset_n;

  reg [31 : 0] a;
  reg [31 : 0] b;
  reg [31 : 0] c;
  reg [31 : 0] d;

  wire [31 : 0] a_prim;
  wire [31 : 0] b_prim;
  wire [31 : 0] c_prim;
  wire [31 : 0] d_prim;

  reg            display_cycle_ctr;
  reg            display_ctrl_and_ctrs;
  reg            display_qround;
  reg            display_state;


  //----------------------------------------------------------------
  // chacha_core device under test.
  //----------------------------------------------------------------
  chacha_qr dut(
                .clk(tb_clk),
                .reset_n(tb_reset_n),

                .a(a),
                .b(b),
                .c(c),
                .d(d),

                .a_prim(a_prim),
                .b_prim(b_prim),
                .c_prim(c_prim),
                .d_prim(d_prim)
               );


  //----------------------------------------------------------------
  // clk_gen
  //
  // Clock generator process.
  //----------------------------------------------------------------
  always
    begin : clk_gen
      #CLK_HALF_PERIOD tb_clk = !tb_clk;
    end // clk_gen


  //--------------------------------------------------------------------
  // dut_monitor
  //
  // Monitor that displays different types of information
  // every cycle depending on what flags test cases enable.
  //
  // The monitor includes a cycle counter for the testbench.
  //--------------------------------------------------------------------
  always @ (posedge tb_clk)
    begin : dut_monitor
      cycle_ctr = cycle_ctr + 1;

      $display("cycle = %08x:", cycle_ctr);
      $display("");

      $display("a      = %08x, b      = %08x, c      = %08x, d      = %08x",
               a, b, c, d);
      $display("a_prim = %08x, b_prim = %08x, c_prim = %08x, d_prim = %08x",
               a_prim, b_prim, c_prim, d_prim);
      $display("");
    end // dut_monitor


  //----------------------------------------------------------------
  // cycle_reset()
  //
  // Cycles the reset signal on the dut.
  //----------------------------------------------------------------
  task cycle_reset;
    begin
      tb_reset_n = 0;
      #(CLK_PERIOD);

      @(negedge tb_clk)

      tb_reset_n = 1;
      #(CLK_PERIOD);
    end
  endtask // cycle_reset


  //----------------------------------------------------------------
  // init_sim()
  //
  // Set the input to the DUT to defined values.
  //----------------------------------------------------------------
  task init_sim;
    begin
      cycle_ctr         = 0;
      tb_clk            = 0;
      tb_reset_n        = 0;
      error_ctr         = 0;
    end
  endtask // init_dut


  //----------------------------------------------------------------
  // qr_tests()
  //
  // Run some simple test on the qr logic.
  // Note: Not self testing. No expected value used.
  //----------------------------------------------------------------
  task qr_tests;
    begin
      $display("*** Test of Quarterround:");
      $display("");
      a = 32'h11223344;
      b = 32'h11223344;
      c = 32'h11223344;
      d = 32'h11223344;

      #(CLK_PERIOD * 10);

      a = 32'h55555555;
      b = 32'h55555555;
      c = 32'h55555555;
      d = 32'h55555555;

      #(CLK_PERIOD * 10);
    end
  endtask // qr_tests


  //----------------------------------------------------------------
  // chacha_qr_test
  //
  // The main test functionality.
  //----------------------------------------------------------------
  initial
    begin : chacha_qr_test
      $display("   -- Testbench for chacha qr started --");
      $display("");

      init_sim();
      #(CLK_PERIOD * 10);
      cycle_reset();
      #(CLK_PERIOD * 10);

      qr_tests();

      // Finish in style.
      $display("   -- Testbench for chacha qr completed --");
      $finish;
    end // chacha_core_test

endmodule // tb_chacha_qr

//======================================================================
// EOF tb_chacha_qr.v
//======================================================================