aboutsummaryrefslogtreecommitdiff
path: root/src/tb/tb_chacha_qr.v
blob: 2c0de8e6e50f321c2c9bbae82a5c3179a4df0195 (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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
//======================================================================
//
// 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),

                .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("a      = 0x%08x, b      = 0x%08x, c      = 0x%08x, d      = 0x%08x",
               a, b, c, d);
      $display("a_prim = 0x%08x, b_prim = 0x%08x, c_prim = 0x%08x, d_prim = 0x%08x",
               a_prim, b_prim, c_prim, d_prim);
      $display("a0     = 0x%08x, a1     = 0x%08x",
               dut.qr.a0, dut.qr.a1);
      $display("b0     = 0x%08x, b1     = 0x%08x, b2     = 0x%08x, b3     = 0x%08x",
               dut.qr.b0, dut.qr.b1, dut.qr.b2, dut.qr.b3);
      $display("c0     = 0x%08x, c1     = 0x%08x",
               dut.qr.c0, dut.qr.c1);
      $display("d0     = 0x%08x, d1     = 0x%08x, d2     = 0x%08x, d3     = 0x%08x",
               dut.qr.d0, dut.qr.d1, dut.qr.d2, dut.qr.d3);
      $display("a0_reg = 0x%08x", dut.a0_reg);
//      $display("a1_reg = 0x%08x", dut.a1_reg);
//      $display("c0_reg = 0x%08x", dut.c0_reg);
      $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("");

      $display("First test:");
      a = 32'h11223344;
      b = 32'h11223344;
      c = 32'h11223344;
      d = 32'h11223344;
      #(CLK_PERIOD * 10);
      $display("");

      if (a_prim == 32'he7a34e04 && b_prim == 32'h9a971009 &&
          c_prim == 32'hd66bc95c && d_prim == 32'h6f7d62b2)
        $display("Ok: First test case correct.");
      else
        $display("Error: Expected a_prim = e7a34e04, b_prim = 9a971009, c_prim = d66bc95c, d_prim = 6f7d62b2");
      $display("");


      $display("Second test:");
      a = 32'h55555555;
      b = 32'h55555555;
      c = 32'h55555555;
      d = 32'h55555555;
      #(CLK_PERIOD * 10);

      if (a_prim == 32'haaaabaaa && b_prim == 32'h4d5d54d5 &&
          c_prim == 32'haa9aaaa9 && d_prim == 32'h55455555)
        $display("Ok: Second test case correct.");
      else
        $display("Error: Expected a_prim = aaaabaaa, b_prim = 4d5d54d5, c_prim = aa9aaaa9, d_prim = 55455555");

    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
//======================================================================