OpenCores
URL https://opencores.org/ocsvn/xgate/xgate/trunk

Subversion Repositories xgate

[/] [xgate/] [trunk/] [bench/] [verilog/] [tst_bench_top.v] - Blame information for rev 62

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 rehayes
////////////////////////////////////////////////////////////////////////////////
2
//
3
//  WISHBONE revB.2 compliant Xgate Coprocessor - Test Bench
4
//
5
//  Author: Bob Hayes
6 62 rehayes
//          rehayes@opencores.org
7 2 rehayes
//
8
//  Downloaded from: http://www.opencores.org/projects/xgate.....
9
//
10
////////////////////////////////////////////////////////////////////////////////
11
// Copyright (c) 2009, Robert Hayes
12
//
13
// This source file is free software: you can redistribute it and/or modify
14
// it under the terms of the GNU Lesser General Public License as published
15
// by the Free Software Foundation, either version 3 of the License, or
16
// (at your option) any later version.
17
//
18
// Supplemental terms.
19
//     * Redistributions of source code must retain the above copyright
20 62 rehayes
//       notice, this list of conditions and the following disclaimer.
21 2 rehayes
//     * Neither the name of the <organization> nor the
22 62 rehayes
//       names of its contributors may be used to endorse or promote products
23
//       derived from this software without specific prior written permission.
24 2 rehayes
//
25
// THIS SOFTWARE IS PROVIDED BY Robert Hayes ''AS IS'' AND ANY
26
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27
// WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28
// DISCLAIMED. IN NO EVENT SHALL Robert Hayes BE LIABLE FOR ANY
29
// DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
30
// (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
31
// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
32
// ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35
//
36
// You should have received a copy of the GNU General Public License
37
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
38
////////////////////////////////////////////////////////////////////////////////
39
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
40
 
41
 
42
`include "timescale.v"
43
 
44
module tst_bench_top();
45
 
46 62 rehayes
  parameter MAX_CHANNEL = 127;    // Max XGATE Interrupt Channel Number
47 5 rehayes
  parameter STOP_ON_ERROR = 1'b0;
48 60 rehayes
  parameter MAX_VECTOR = 8000;
49 21 rehayes
 
50 41 rehayes
  parameter L_BYTE = 2'b01;
51
  parameter H_BYTE = 2'b10;
52 36 rehayes
  parameter WORD   = 2'b11;
53 21 rehayes
 
54 2 rehayes
 
55
  // Name Address Locations
56 62 rehayes
  parameter XGATE_BASE     = 24'h1000;
57 41 rehayes
  parameter XGATE_XGMCTL   = XGATE_BASE + 6'h00;
58
  parameter XGATE_XGCHID   = XGATE_BASE + 6'h02;
59
  parameter XGATE_XGISPHI  = XGATE_BASE + 6'h04;
60
  parameter XGATE_XGISPLO  = XGATE_BASE + 6'h06;
61 62 rehayes
  parameter XGATE_XGVBR    = XGATE_BASE + 6'h08;
62 41 rehayes
  parameter XGATE_XGIF_7   = XGATE_BASE + 6'h0a;
63
  parameter XGATE_XGIF_6   = XGATE_BASE + 6'h0c;
64
  parameter XGATE_XGIF_5   = XGATE_BASE + 6'h0e;
65
  parameter XGATE_XGIF_4   = XGATE_BASE + 6'h10;
66
  parameter XGATE_XGIF_3   = XGATE_BASE + 6'h12;
67
  parameter XGATE_XGIF_2   = XGATE_BASE + 6'h14;
68
  parameter XGATE_XGIF_1   = XGATE_BASE + 6'h16;
69
  parameter XGATE_XGIF_0   = XGATE_BASE + 6'h18;
70 62 rehayes
  parameter XGATE_XGSWT    = XGATE_BASE + 6'h1a;
71
  parameter XGATE_XGSEM    = XGATE_BASE + 6'h1c;
72
  parameter XGATE_RES1     = XGATE_BASE + 6'h1e;
73
  parameter XGATE_XGCCR    = XGATE_BASE + 6'h20;
74
  parameter XGATE_XGPC     = XGATE_BASE + 6'h22;
75
  parameter XGATE_RES2     = XGATE_BASE + 6'h24;
76
  parameter XGATE_XGR1     = XGATE_BASE + 6'h26;
77
  parameter XGATE_XGR2     = XGATE_BASE + 6'h28;
78
  parameter XGATE_XGR3     = XGATE_BASE + 6'h2a;
79
  parameter XGATE_XGR4     = XGATE_BASE + 6'h2c;
80
  parameter XGATE_XGR5     = XGATE_BASE + 6'h2e;
81
  parameter XGATE_XGR6     = XGATE_BASE + 6'h30;
82
  parameter XGATE_XGR7     = XGATE_BASE + 6'h32;
83 21 rehayes
 
84 11 rehayes
  // Define bits in XGATE Control Register
85 62 rehayes
  parameter XGMCTL_XGEM     = 16'h8000;
86 11 rehayes
  parameter XGMCTL_XGFRZM   = 16'h4000;
87
  parameter XGMCTL_XGDBGM   = 15'h2000;
88
  parameter XGMCTL_XGSSM    = 15'h1000;
89
  parameter XGMCTL_XGFACTM  = 15'h0800;
90
  parameter XGMCTL_XGBRKIEM = 15'h0400;
91
  parameter XGMCTL_XGSWEIFM = 15'h0200;
92
  parameter XGMCTL_XGIEM    = 15'h0100;
93 62 rehayes
  parameter XGMCTL_XGE      = 16'h0080;
94 11 rehayes
  parameter XGMCTL_XGFRZ    = 16'h0040;
95
  parameter XGMCTL_XGDBG    = 15'h0020;
96 62 rehayes
  parameter XGMCTL_XGSS     = 15'h0010;
97 11 rehayes
  parameter XGMCTL_XGFACT   = 15'h0008;
98
  parameter XGMCTL_XGBRKIE  = 15'h0004;
99
  parameter XGMCTL_XGSWEIF  = 15'h0002;
100 62 rehayes
  parameter XGMCTL_XGIE     = 15'h0001;
101 2 rehayes
 
102 5 rehayes
  parameter CHECK_POINT = 16'h8000;
103
  parameter CHANNEL_ACK = CHECK_POINT + 2;
104
  parameter CHANNEL_ERR = CHECK_POINT + 4;
105 36 rehayes
 
106 62 rehayes
  parameter SYS_RAM_BASE = 24'h00_0000;
107
 
108 36 rehayes
  //
109
  // wires && regs
110
  //
111 62 rehayes
  reg         mstr_test_clk;
112 36 rehayes
  reg  [19:0] vector;
113
  reg  [15:0] error_count;
114
  reg  [ 7:0] test_num;
115 62 rehayes
 
116 36 rehayes
  reg  [15:0] q, qq;
117 21 rehayes
 
118 62 rehayes
  reg         rstn;
119
  reg         sync_reset;
120
  reg         por_reset_b;
121
  reg         scantestmode;
122 21 rehayes
 
123 36 rehayes
  reg  [MAX_CHANNEL:0] channel_req;  // XGATE Interrupt inputs
124 62 rehayes
  wire [MAX_CHANNEL:0] xgif;          // XGATE Interrupt outputs
125
  wire         [  7:0] xgswt;         // XGATE Software Trigger outputs
126
  wire                 xg_sw_irq;    // Xgate Software Error interrupt
127 36 rehayes
 
128
 
129 62 rehayes
  wire [15:0] wbm_dat_o;  // WISHBONE Master Mode data output from XGATE
130
  wire [15:0] wbm_dat_i;  // WISHBONE Master Mode data input to XGATE
131
  wire [15:0] wbm_adr_o;  // WISHBONE Master Mode address output from XGATE
132 36 rehayes
  wire [ 1:0] wbm_sel_o;
133
 
134 62 rehayes
  reg         mem_wait_state_enable;
135 36 rehayes
 
136
  wire [15:0] tb_ram_out;
137
 
138 62 rehayes
  wire [15:0] tb_slave_dout; // WISHBONE data bus output from testbench slave module
139
  wire        error_pulse;   // Error detected output pulse from the testbench slave module
140
  wire        test_reg_ack;  // WISHBONE ack from testbench slave module
141
  wire        ack_pulse;     // Thread ack output pulse from testbench slave module
142
 
143
  wire        wbm_cyc_o;
144
  wire        wbm_stb_o;
145
  wire        wbm_we_o;
146
  wire        wbs_err_o;
147
 
148
 
149 11 rehayes
  // Registers used to mirror internal registers
150 36 rehayes
  reg  [15:0] data_xgmctl;
151
  reg  [15:0] data_xgchid;
152
  reg  [15:0] data_xgvbr;
153
  reg  [15:0] data_xgswt;
154
  reg  [15:0] data_xgsem;
155 2 rehayes
 
156 62 rehayes
  wire        sys_cyc;
157
  wire        sys_stb;
158
  wire        sys_we;
159 36 rehayes
  wire [ 1:0] sys_sel;
160 54 rehayes
  wire [23:0] sys_adr;
161 36 rehayes
  wire [15:0] sys_dout;
162 54 rehayes
  wire [15:0] sys_din;
163 62 rehayes
 
164
  wire        host_ack;
165 36 rehayes
  wire [15:0] host_dout;
166 62 rehayes
  wire        host_cyc;
167
  wire        host_stb;
168
  wire        host_we;
169 36 rehayes
  wire [ 1:0] host_sel;
170 54 rehayes
  wire [23:0] host_adr;
171 36 rehayes
  wire [15:0] host_din;
172 62 rehayes
 
173
  wire        xgate_ack;
174 36 rehayes
  wire [15:0] xgate_dout;
175 62 rehayes
  wire        xgate_cyc;
176
  wire        xgate_stb;
177
  wire        xgate_we;
178 36 rehayes
  wire [ 1:0] xgate_sel;
179
  wire [15:0] xgate_adr;
180
  wire [15:0] xgate_din;
181 62 rehayes
 
182
  wire        xgate_s_stb;
183
  wire        xgate_s_ack;
184 36 rehayes
  wire [15:0] xgate_s_dout;
185 62 rehayes
 
186
  wire        slv2_stb;
187
  wire        ram_sel;
188 36 rehayes
  wire [15:0] ram_dout;
189
 
190 2 rehayes
  // initial values and testbench setup
191
  initial
192
    begin
193
      mstr_test_clk = 0;
194
      vector = 0;
195
      test_num = 0;
196
      por_reset_b = 0;
197
      scantestmode = 0;
198 5 rehayes
      error_count = 0;
199 11 rehayes
      mem_wait_state_enable = 0;
200 2 rehayes
      // channel_req = 0;
201
 
202
      `ifdef WAVES
203 62 rehayes
           $shm_open("waves");
204
           $shm_probe("AS",tst_bench_top,"AS");
205
           $display("\nINFO: Signal dump enabled ...\n\n");
206 2 rehayes
      `endif
207
 
208
      `ifdef WAVES_V
209 62 rehayes
           $dumpfile ("xgate_wave_dump.lxt");
210
           $dumpvars (0, tst_bench_top);
211
           $dumpon;
212
           $display("\nINFO: VCD Signal dump enabled ...\n\n");
213 2 rehayes
      `endif
214
 
215
    end
216
 
217
  // generate clock
218
  always #20 mstr_test_clk = ~mstr_test_clk;
219
 
220 5 rehayes
  // Keep a count of how many clocks we've simulated
221 2 rehayes
  always @(posedge mstr_test_clk)
222 11 rehayes
    begin
223
      vector <= vector + 1;
224
      if (vector > MAX_VECTOR)
225 62 rehayes
        begin
226
          error_count <= error_count + 1;
227
          $display("\n ------ !!!!! Simulation Timeout at vector=%d\n -------", vector);
228
          wrap_up;
229
        end
230 11 rehayes
    end
231 2 rehayes
 
232 50 rehayes
  // Add up errors that come from WISHBONE read compares
233 36 rehayes
  always @host.cmp_error_detect
234 21 rehayes
    begin
235
      error_count <= error_count + 1;
236
    end
237 11 rehayes
 
238 54 rehayes
  always @(posedge error_pulse) //channel_ack_wrt
239 2 rehayes
    begin
240 54 rehayes
      #1;
241 5 rehayes
      error_count = error_count + 1;
242
      if (STOP_ON_ERROR == 1'b1)
243 62 rehayes
        wrap_up;
244 5 rehayes
    end
245
 
246 2 rehayes
  wire [ 6:0] current_active_channel = xgate.risc.xgchid;
247 54 rehayes
  always @(posedge ack_pulse) //channel_ack_wrt
248 2 rehayes
    clear_channel(current_active_channel);
249 21 rehayes
 
250 2 rehayes
 
251 62 rehayes
 
252 36 rehayes
  // Testbench RAM for Xgate program storage and Load/Store instruction tests
253
  ram p_ram
254
  (
255
    // Outputs
256
    .ram_out( ram_dout ),
257
    // inputs
258 54 rehayes
    .address( sys_adr[15:0] ),
259 36 rehayes
    .ram_in( sys_dout ),
260
    .we( sys_we ),
261 54 rehayes
    .ce( ram_sel ),
262 36 rehayes
    .stb( mstr_test_clk ),
263 54 rehayes
    .sel( sys_sel )
264 36 rehayes
  );
265
 
266
  // hookup wishbone master model
267 54 rehayes
  wb_master_model #(.dwidth(16), .awidth(24))
268 36 rehayes
    host(
269
    // Outputs
270
    .cyc( host_cyc ),
271
    .stb( host_stb ),
272
    .we( host_we ),
273
    .sel( host_sel ),
274
    .adr( host_adr ),
275
    .dout( host_dout ),
276
    // inputs
277 54 rehayes
    .din(sys_din),
278 36 rehayes
    .clk(mstr_test_clk),
279
    .ack(host_ack),
280
    .rst(rstn),
281
    .err(1'b0),
282
    .rty(1'b0)
283
  );
284
 
285
  bus_arbitration  #(.dwidth(16),
286 62 rehayes
                     .awidth(24),
287 54 rehayes
                     .ram_base(0),
288
                     .ram_size(17'h10000),
289
                     .slv1_base(XGATE_BASE),
290
                     .slv1_size(64),
291
                     .slv2_base(CHECK_POINT),
292
                     .slv2_size(8))
293 36 rehayes
    arb(
294
    // System bus I/O
295
    .sys_cyc( sys_cyc ),
296
    .sys_stb( sys_stb ),
297
    .sys_we( sys_we ),
298
    .sys_sel( sys_sel ),
299
    .sys_adr( sys_adr ),
300
    .sys_dout( sys_dout ),
301 54 rehayes
    .sys_din( sys_din ),
302 36 rehayes
    // Host bus I/O
303
    .host_ack( host_ack ),
304
    .host_dout( host_din ),
305
    .host_cyc( host_cyc ),
306
    .host_stb( host_stb ),
307
    .host_we( host_we ),
308
    .host_sel( host_sel ),
309
    .host_adr( host_adr ),
310
    .host_din( host_dout ),
311
    // Alternate Bus Master #1 Bus I/O
312
    .alt1_ack( xgate_ack ),
313
    .alt1_cyc( wbm_cyc_o ),
314
    .alt1_stb( wbm_stb_o ),
315
    .alt1_we( wbm_we_o ),
316
    .alt1_sel( wbm_sel_o ),
317 54 rehayes
    .alt1_adr( {8'h00, wbm_adr_o} ),
318 36 rehayes
    .alt1_din( wbm_dat_o ),
319 54 rehayes
    // RAM
320
    .ram_sel( ram_sel ),
321
    .ram_dout( ram_dout ),
322 36 rehayes
    // Slave #1 Bus I/O
323
    .slv1_stb( xgate_s_stb ),
324
    .slv1_ack( xgate_s_ack ),
325
    .slv1_din( xgate_s_dout ),
326
    // Slave #2 Bus I/O
327
    .slv2_stb( slv2_stb ),
328 54 rehayes
    .slv2_ack( test_reg_ack ),
329 62 rehayes
    .slv2_din( tb_slave_dout ),
330 36 rehayes
    // Miscellaneous
331
    .host_clk( mstr_test_clk ),
332
    .risc_clk( mstr_test_clk ),
333
    .rst( rstn ),  // No Connect
334
    .err( 1'b0 ),  // No Connect
335
    .rty( 1'b0 )   // No Connect
336
  );
337 62 rehayes
 
338 5 rehayes
  // hookup XGATE core - Parameters take all default values
339 54 rehayes
  xgate_top  #(.SINGLE_CYCLE(1'b0),
340 62 rehayes
               .MAX_CHANNEL(MAX_CHANNEL))    // Max XGATE Interrupt Channel Number
341
          xgate(
342
          // Wishbone slave interface
343
          .wbs_clk_i( mstr_test_clk ),
344
          .wbs_rst_i( 1'b0 ),         // sync_reset
345
          .arst_i( rstn ),            // async resetn
346
          .wbs_adr_i( sys_adr[5:1] ),
347
          .wbs_dat_i( sys_dout ),
348
          .wbs_dat_o( xgate_s_dout ),
349
          .wbs_we_i( sys_we ),
350
          .wbs_stb_i( xgate_s_stb ),
351
          .wbs_cyc_i( sys_cyc ),
352
          .wbs_sel_i( sys_sel ),
353
          .wbs_ack_o( xgate_s_ack ),
354
          .wbs_err_o( wbs_err_o ),
355 2 rehayes
 
356 62 rehayes
          // Wishbone master Signals
357
          .wbm_dat_o( wbm_dat_o ),
358
          .wbm_we_o( wbm_we_o ),
359
          .wbm_stb_o( wbm_stb_o ),
360
          .wbm_cyc_o( wbm_cyc_o ),
361
          .wbm_sel_o( wbm_sel_o ),
362
          .wbm_adr_o( wbm_adr_o ),
363
          .wbm_dat_i( sys_din ),
364
          .wbm_ack_i( xgate_ack ),
365 5 rehayes
 
366 62 rehayes
          .xgif( xgif ),             // XGATE Interrupt Flag output
367
          .xg_sw_irq( xg_sw_irq ),   // XGATE Software Error Interrupt Flag output
368
          .xgswt( xgswt ),
369
          .risc_clk( mstr_test_clk ),
370
          .chan_req_i( {channel_req[MAX_CHANNEL:40], xgswt, channel_req[31:0]} ),
371
          .debug_mode_i( 1'b0 ),
372
          .secure_mode_i( 1'b0 ),
373
          .scantestmode( scantestmode )
374 2 rehayes
  );
375
 
376 54 rehayes
  tb_slave #(.DWIDTH(16),
377 62 rehayes
             .SINGLE_CYCLE(1'b1))
378 54 rehayes
          tb_slave_regs(
379
          // wishbone interface
380
          .wb_clk_i( mstr_test_clk ),
381
          .wb_rst_i( 1'b0 ),
382
          .arst_i( rstn ),
383
          .wb_adr_i( sys_adr[3:1] ),
384
          .wb_dat_i( sys_dout ),
385 62 rehayes
          .wb_dat_o( tb_slave_dout),
386 54 rehayes
          .wb_we_i( sys_we ),
387
          .wb_stb_i( slv2_stb ),
388
          .wb_cyc_i( sys_cyc ),
389
          .wb_sel_i( sys_sel ),
390
          .wb_ack_o( test_reg_ack ),
391 2 rehayes
 
392 62 rehayes
          .ack_pulse( ack_pulse ),
393 54 rehayes
          .error_pulse( error_pulse ),
394
          .vector( vector )
395
  );
396 2 rehayes
 
397 54 rehayes
 
398
 
399 2 rehayes
////////////////////////////////////////////////////////////////////////////////
400
////////////////////////////////////////////////////////////////////////////////
401
////////////////////////////////////////////////////////////////////////////////
402
 
403 54 rehayes
// Main Test Program
404 2 rehayes
initial
405
  begin
406 36 rehayes
    $display("\nstatus at time: %t Testbench started", $time);
407 2 rehayes
 
408 36 rehayes
    // reset system
409
    rstn = 1'b1; // negate reset
410
    channel_req = 1; //
411
    repeat(1) @(posedge mstr_test_clk);
412 62 rehayes
    sync_reset = 1'b1;  // Make the sync reset 1 clock cycle long
413
    #2;          // move the async reset away from the clock edge
414 36 rehayes
    rstn = 1'b0; // assert async reset
415 62 rehayes
    #5;          // Keep the async reset pulse with less than a clock cycle
416 36 rehayes
    rstn = 1'b1; // negate async reset
417
    por_reset_b = 1'b1;
418
    channel_req = 0; //
419
    repeat(1) @(posedge mstr_test_clk);
420
    sync_reset = 1'b0;
421
    channel_req = 0; //
422 2 rehayes
 
423 36 rehayes
    $display("\nstatus at time: %t done reset", $time);
424 21 rehayes
 
425 36 rehayes
    test_inst_set;
426 21 rehayes
 
427 36 rehayes
    test_debug_mode;
428 2 rehayes
 
429 36 rehayes
    test_debug_bit;
430 21 rehayes
 
431 36 rehayes
    test_chid_debug;
432 21 rehayes
 
433 41 rehayes
    reg_test_16;
434
 
435 54 rehayes
    //host_ram;
436
 
437 36 rehayes
    // End testing
438
    wrap_up;
439 2 rehayes
  end
440
 
441 36 rehayes
////////////////////////////////////////////////////////////////////////////////
442 21 rehayes
// Test CHID Debug mode operation
443
task test_chid_debug;
444
  begin
445
    test_num = test_num + 1;
446
    $display("\nTEST #%d Starts at vector=%d, test_chid_debug", test_num, vector);
447 36 rehayes
    $readmemh("../../../bench/verilog/debug_test.v", p_ram.ram_8);
448 21 rehayes
 
449
    data_xgmctl = XGMCTL_XGBRKIEM | XGMCTL_XGBRKIE;
450 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Enable interrupt on BRK instruction
451 21 rehayes
 
452
    activate_thread_sw(3);
453
 
454
    wait_debug_set;   // Debug Status bit is set by BRK instruction
455
 
456 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h20c6, WORD);      // See Program code (BRK).
457
    host.wb_cmp(0, XGATE_XGR3,      16'h0001, WORD);      // See Program code.R3 = 1
458
    host.wb_cmp(0, XGATE_XGCHID,   16'h0003, WORD);       // Check for Correct CHID
459 21 rehayes
 
460
    channel_req[5] = 1'b1; //
461
    repeat(7) @(posedge mstr_test_clk);
462 62 rehayes
    host.wb_cmp(0, XGATE_XGCHID,   16'h0003, WORD);       // Check for Correct CHID
463 21 rehayes
 
464 62 rehayes
    host.wb_write(0, XGATE_XGCHID, 16'h000f, H_BYTE);     // Check byte select lines
465 41 rehayes
    repeat(4) @(posedge mstr_test_clk);
466 62 rehayes
    host.wb_cmp(0, XGATE_XGCHID,   16'h0003, WORD);       // Verify CHID is unchanged
467 21 rehayes
 
468 62 rehayes
    host.wb_write(0, XGATE_XGCHID, 16'h000f, L_BYTE);     // Change CHID
469
    host.wb_cmp(0, XGATE_XGCHID,   16'h000f, WORD);       // Check for Correct CHID
470 21 rehayes
 
471 62 rehayes
    host.wb_write(0, XGATE_XGCHID, 16'h0000, WORD);       // Change CHID to 00, RISC should go to IDLE state
472
 
473 21 rehayes
    repeat(1) @(posedge mstr_test_clk);
474
 
475 62 rehayes
    host.wb_write(0, XGATE_XGCHID, 16'h0004, WORD);       // Change CHID
476 21 rehayes
 
477
    repeat(8) @(posedge mstr_test_clk);
478
 
479
    data_xgmctl = XGMCTL_XGDBGM;
480 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Clear Debug Mode Control Bit
481 21 rehayes
 
482
    wait_debug_set;   // Debug Status bit is set by BRK instruction
483 62 rehayes
    host.wb_cmp(0, XGATE_XGCHID,   16'h0004, WORD);       // Check for Correct CHID
484
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Clear Debug Mode Control Bit (Excape from Break State and run)
485 21 rehayes
 
486
    wait_debug_set;   // Debug Status bit is set by BRK instruction
487 62 rehayes
    host.wb_cmp(0, XGATE_XGCHID,   16'h0005, WORD);       // Check for Correct CHID
488 21 rehayes
    activate_channel(6);
489 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Clear Debug Mode Control Bit (Excape from Break State and run)
490 21 rehayes
 
491
    wait_debug_set;   // Debug Status bit is set by BRK instruction
492 62 rehayes
    host.wb_cmp(0, XGATE_XGCHID,   16'h0006, WORD);       // Check for Correct CHID
493
    host.wb_cmp(0, XGATE_XGPC,      16'h211c, WORD);      // See Program code (BRK)
494 21 rehayes
    data_xgmctl = XGMCTL_XGSSM | XGMCTL_XGSS;
495 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step
496 21 rehayes
    repeat(8) @(posedge mstr_test_clk);
497 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h211e, WORD);      // See Program code (BRA)
498
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step
499 21 rehayes
    repeat(8) @(posedge mstr_test_clk);
500 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h2122, WORD);      // See Program code ()
501 21 rehayes
 
502
    repeat(20) @(posedge mstr_test_clk);
503
 
504
    data_xgmctl = XGMCTL_XGDBGM;
505 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Clear Debug Mode Control Bit
506 21 rehayes
 
507
    repeat(50) @(posedge mstr_test_clk);
508
 
509 36 rehayes
    p_ram.dump_ram(0);
510
 
511 41 rehayes
    read_ram_cmp(16'h0000,16'h7b55);
512
    read_ram_cmp(16'h0004,16'h7faa);
513
    read_ram_cmp(16'h0006,16'h6f55);
514
    read_ram_cmp(16'h0008,16'h00c3);
515
    read_ram_cmp(16'h000a,16'h5f66);
516
    read_ram_cmp(16'h000c,16'h0003);
517
    read_ram_cmp(16'h0022,16'hccxx);
518
    read_ram_cmp(16'h0026,16'hxx99);
519
    read_ram_cmp(16'h0032,16'h1fcc);
520
    read_ram_cmp(16'h0038,16'h2f99);
521
    read_ram_cmp(16'h0042,16'h33xx);
522
    read_ram_cmp(16'h0046,16'hxx55);
523
    read_ram_cmp(16'h0052,16'hxx66);
524
    read_ram_cmp(16'h0058,16'h99xx);
525
    read_ram_cmp(16'h0062,16'h1faa);
526
    read_ram_cmp(16'h0068,16'h2fcc);
527
 
528 21 rehayes
  end
529
endtask
530
 
531 36 rehayes
////////////////////////////////////////////////////////////////////////////////
532 11 rehayes
// Test Debug bit operation
533
task test_debug_bit;
534 2 rehayes
  begin
535 11 rehayes
    test_num = test_num + 1;
536 21 rehayes
    $display("\nTEST #%d Starts at vector=%d, test_debug_bit", test_num, vector);
537 36 rehayes
    $readmemh("../../../bench/verilog/debug_test.v", p_ram.ram_8);
538 21 rehayes
 
539 11 rehayes
    data_xgmctl = XGMCTL_XGBRKIEM | XGMCTL_XGBRKIE;
540 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Enable interrupt on BRK instruction
541 11 rehayes
 
542
    activate_thread_sw(2);
543 21 rehayes
 
544 11 rehayes
    repeat(25) @(posedge mstr_test_clk);
545
 
546
    data_xgmctl = XGMCTL_XGDBGM | XGMCTL_XGDBG;
547 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Set Debug Mode Control Bit
548 21 rehayes
    repeat(5) @(posedge mstr_test_clk);
549
 
550 36 rehayes
    host.wb_read(1, XGATE_XGR3, q, WORD);
551 21 rehayes
    data_xgmctl = XGMCTL_XGSSM | XGMCTL_XGSS;
552
    qq = q;
553
 
554
    // The Xgate test program is in an infinate loop incrementing R3
555
    while (qq == q)  // Look for change in R3 register
556
      begin
557 62 rehayes
        host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);   // Do a Single Step
558
        repeat(7) @(posedge mstr_test_clk);
559
        host.wb_read(1, XGATE_XGR3, q, WORD);
560 21 rehayes
      end
561
    if (q != (qq+1))
562
      begin
563 62 rehayes
        $display("Error! - Unexpected value of R3 at vector=%d", vector);
564
        error_count = error_count + 1;
565 21 rehayes
      end
566
 
567
 
568 62 rehayes
    host.wb_write(1, XGATE_XGPC, 16'h2094, WORD);        // Write to PC to force exit from infinite loop
569 54 rehayes
    repeat(10) @(posedge mstr_test_clk);
570 21 rehayes
 
571
    data_xgmctl = XGMCTL_XGSSM | XGMCTL_XGSS;
572 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (Load ADDL instruction)
573 54 rehayes
    repeat(10) @(posedge mstr_test_clk);
574 62 rehayes
    host.wb_cmp(0, XGATE_XGR4,      16'h0002, WORD);      // See Program code.(R4 <= R4 + 1)
575 21 rehayes
 
576 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (Load ADDL instruction)
577 54 rehayes
    repeat(10) @(posedge mstr_test_clk);
578 62 rehayes
    host.wb_cmp(0, XGATE_XGR4,      16'h0003, WORD);      // See Program code.(R4 <= R4 + 1)
579 21 rehayes
 
580
    data_xgmctl = XGMCTL_XGDBGM;
581 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Clear Debug Mode Control Bit
582
                                                 // Should be back in Run Mode
583 21 rehayes
 
584
//    data_xgmctl = XGMCTL_XGSWEIFM | XGMCTL_XGSWEIF | XGMCTL_XGBRKIEM;
585 36 rehayes
//    host.wb_write(0, XGATE_XGMCTL, data_xgmctl);   // Clear Software Interrupt and BRK Interrupt Enable Bit
586 11 rehayes
    repeat(15) @(posedge mstr_test_clk);
587 21 rehayes
 
588 2 rehayes
  end
589
endtask
590
 
591 36 rehayes
////////////////////////////////////////////////////////////////////////////////
592 11 rehayes
// Test Debug mode operation
593
task test_debug_mode;
594 2 rehayes
  begin
595 11 rehayes
    test_num = test_num + 1;
596
    $display("\nTEST #%d Starts at vector=%d, test_debug_mode", test_num, vector);
597 36 rehayes
    $readmemh("../../../bench/verilog/debug_test.v", p_ram.ram_8);
598 21 rehayes
 
599 11 rehayes
    data_xgmctl = XGMCTL_XGBRKIEM | XGMCTL_XGBRKIE;
600 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Enable interrupt on BRK instruction
601 11 rehayes
 
602
    activate_thread_sw(1);
603 21 rehayes
 
604 11 rehayes
    wait_debug_set;   // Debug Status bit is set by BRK instruction
605
 
606 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h203a, WORD);      // See Program code (BRK).
607
    host.wb_cmp(0, XGATE_XGR3,      16'h0001, WORD);      // See Program code.R3 = 1
608 11 rehayes
 
609
    data_xgmctl = XGMCTL_XGSSM | XGMCTL_XGSS;
610
 
611 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (Load ADDL instruction)
612 11 rehayes
    repeat(5) @(posedge mstr_test_clk);
613 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h203c, WORD);      // PC + 2.
614 11 rehayes
 
615 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (Load NOP instruction)
616
    repeat(5) @(posedge mstr_test_clk);                  // Execute ADDL instruction
617
    host.wb_cmp(0, XGATE_XGR3,      16'h0002, WORD);      // See Program code.(R3 <= R3 + 1)
618
    host.wb_cmp(0, XGATE_XGCCR,     16'h0000, WORD);      // See Program code.
619
    host.wb_cmp(0, XGATE_XGPC,      16'h203e, WORD);      // PC + 2.
620 11 rehayes
    repeat(5) @(posedge mstr_test_clk);
621 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h203e, WORD);      // Still no change.
622 11 rehayes
 
623 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (Load BRA instruction)
624
    repeat(9) @(posedge mstr_test_clk);                  // Execute NOP instruction
625
    host.wb_cmp(0, XGATE_XGPC,      16'h2040, WORD);      // See Program code.
626 11 rehayes
 
627
 
628 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step
629
    repeat(5) @(posedge mstr_test_clk);                  // Execute BRA instruction
630
    host.wb_cmp(0, XGATE_XGPC,      16'h2064, WORD);      // PC = Branch destination.
631
                                                         // Load ADDL instruction
632 21 rehayes
 
633 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (Load LDW R7 instruction)
634
    repeat(5) @(posedge mstr_test_clk);                  // Execute ADDL instruction
635
    host.wb_cmp(0, XGATE_XGPC,      16'h2066, WORD);      // PC + 2.
636
    host.wb_cmp(0, XGATE_XGR3,      16'h0003, WORD);      // See Program code.(R3 <= R3 + 1)
637 11 rehayes
 
638 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (LDW R7)
639 11 rehayes
    repeat(5) @(posedge mstr_test_clk);
640 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h2068, WORD);      // PC + 2.
641
    host.wb_cmp(0, XGATE_XGR7,      16'h00c3, WORD);      // See Program code
642 11 rehayes
 
643
    repeat(1) @(posedge mstr_test_clk);
644 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (BRA)
645 11 rehayes
    repeat(9) @(posedge mstr_test_clk);
646 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h2048, WORD);      // See Program code.
647 11 rehayes
 
648 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (STW R3)
649 11 rehayes
    repeat(5) @(posedge mstr_test_clk);
650 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h204a, WORD);      // PC + 2.
651
    host.wb_cmp(0, XGATE_XGR3,      16'h0003, WORD);      // See Program code.(R3 <= R3 + 1)
652 11 rehayes
 
653 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Do a Single Step (R3 <= R3 + 1)
654 11 rehayes
    repeat(5) @(posedge mstr_test_clk);
655 62 rehayes
    host.wb_cmp(0, XGATE_XGPC,      16'h204c, WORD);      // PC + 2.
656 11 rehayes
 
657
    repeat(5) @(posedge mstr_test_clk);
658
 
659
    data_xgmctl = XGMCTL_XGDBGM;
660 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Clear Debug Mode Control Bit
661
                                                         // Should be back in Run Mode
662 11 rehayes
    wait_irq_set(1);
663 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0002, WORD);
664 21 rehayes
 
665 11 rehayes
    data_xgmctl = XGMCTL_XGSWEIFM | XGMCTL_XGSWEIF | XGMCTL_XGBRKIEM;
666 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Clear Software Interrupt and BRK Interrupt Enable Bit
667 11 rehayes
    repeat(15) @(posedge mstr_test_clk);
668 21 rehayes
 
669 2 rehayes
  end
670
endtask
671
 
672 36 rehayes
////////////////////////////////////////////////////////////////////////////////
673 11 rehayes
// Test instruction set
674
task test_inst_set;
675
  begin
676 36 rehayes
    $readmemh("../../../bench/verilog/inst_test.v", p_ram.ram_8);
677 11 rehayes
    test_num = test_num + 1;
678
    $display("\nTEST #%d Starts at vector=%d, inst_test", test_num, vector);
679 21 rehayes
    repeat(1) @(posedge mstr_test_clk);
680 11 rehayes
 
681
    activate_thread_sw(1);
682
    wait_irq_set(1);
683 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0002, WORD);
684 21 rehayes
 
685 11 rehayes
    activate_thread_sw(2);
686
    wait_irq_set(2);
687 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0004, WORD);
688 11 rehayes
 
689
    activate_thread_sw(3);
690
    wait_irq_set(3);
691 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0008, WORD);
692 21 rehayes
 
693 11 rehayes
    activate_thread_sw(4);
694
    wait_irq_set(4);
695 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0010, WORD);
696 21 rehayes
 
697 11 rehayes
    activate_thread_sw(5);
698
    wait_irq_set(5);
699 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0020, WORD);
700 21 rehayes
 
701 11 rehayes
    activate_thread_sw(6);
702
    wait_irq_set(6);
703 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0040, WORD);
704 21 rehayes
 
705 11 rehayes
    activate_thread_sw(7);
706
    wait_irq_set(7);
707 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0080, WORD);
708 21 rehayes
 
709 11 rehayes
    activate_thread_sw(8);
710
    wait_irq_set(8);
711 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0100, WORD);
712 21 rehayes
 
713 11 rehayes
    activate_thread_sw(9);
714
    wait_irq_set(9);
715 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0200, WORD);
716 21 rehayes
 
717 36 rehayes
    host.wb_write(1, XGATE_XGSEM, 16'h5050, WORD);
718 62 rehayes
    host.wb_cmp(0, XGATE_XGSEM,     16'h0050, WORD);   //
719 11 rehayes
    activate_thread_sw(10);
720
    wait_irq_set(10);
721 36 rehayes
    host.wb_write(1, XGATE_XGIF_0, 16'h0400, WORD);
722 21 rehayes
 
723 36 rehayes
    host.wb_write(1, XGATE_XGSEM, 16'hff00, WORD);    // clear the old settings
724 62 rehayes
    host.wb_cmp(0, XGATE_XGSEM,     16'h0000, WORD);   //
725 36 rehayes
    host.wb_write(1, XGATE_XGSEM, 16'ha0a0, WORD);    // Verify that bits were unlocked by RISC
726 62 rehayes
    host.wb_cmp(0, XGATE_XGSEM,     16'h00a0, WORD);   // Verify bits were set
727 36 rehayes
    host.wb_write(1, XGATE_XGSEM, 16'hff08, WORD);    // Try to set the bit that was left locked by the RISC
728 62 rehayes
    host.wb_cmp(0, XGATE_XGSEM,     16'h0000, WORD);   // Verify no bits were set
729 21 rehayes
 
730 11 rehayes
    repeat(20) @(posedge mstr_test_clk);
731
 
732 36 rehayes
    p_ram.dump_ram(0);
733 62 rehayes
 
734 41 rehayes
    read_ram_cmp(16'h0000,16'haa55);
735
    read_ram_cmp(16'h0004,16'h7faa);
736
    read_ram_cmp(16'h0006,16'h6f55);
737
    read_ram_cmp(16'h000a,16'h5f66);
738
    read_ram_cmp(16'h0032,16'h1fcc);
739
    read_ram_cmp(16'h0038,16'h2f99);
740
    read_ram_cmp(16'h0062,16'h1faa);
741
    read_ram_cmp(16'h0068,16'h2fcc);
742
    read_ram_cmp(16'h0022,16'hccxx);
743
    read_ram_cmp(16'h0026,16'hxx99);
744
    read_ram_cmp(16'h0052,16'hxx66);
745
    read_ram_cmp(16'h0058,16'h99xx);
746 62 rehayes
 
747 50 rehayes
    data_xgmctl = 16'hff00;
748 62 rehayes
    host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);    // Disable XGATE
749 50 rehayes
 
750 11 rehayes
  end
751
endtask
752
 
753 36 rehayes
////////////////////////////////////////////////////////////////////////////////
754 2 rehayes
// check register bits - reset, read/write
755
task reg_test_16;
756
  begin
757
      test_num = test_num + 1;
758
      $display("TEST #%d Starts at vector=%d, reg_test_16", test_num, vector);
759 41 rehayes
 
760
      system_reset;
761 2 rehayes
 
762 62 rehayes
      host.wb_cmp(0, XGATE_XGMCTL,   16'h0000, WORD);    // verify reset
763
      host.wb_cmp(0, XGATE_XGCHID,   16'h0000, WORD);    // verify reset
764
      host.wb_cmp(0, XGATE_XGISPHI,  16'h0000, WORD);    // verify reset
765
      host.wb_cmp(0, XGATE_XGISPLO,  16'h0000, WORD);    // verify reset
766
      host.wb_cmp(0, XGATE_XGVBR,    16'hfe00, WORD);    // verify reset
767
      host.wb_cmp(0, XGATE_XGIF_7,   16'h0000, WORD);    // verify reset
768
      host.wb_cmp(0, XGATE_XGIF_6,   16'h0000, WORD);    // verify reset
769
      host.wb_cmp(0, XGATE_XGIF_5,   16'h0000, WORD);    // verify reset
770
      host.wb_cmp(0, XGATE_XGIF_4,   16'h0000, WORD);    // verify reset
771
      host.wb_cmp(0, XGATE_XGIF_3,   16'h0000, WORD);    // verify reset
772
      host.wb_cmp(0, XGATE_XGIF_2,   16'h0000, WORD);    // verify reset
773
      host.wb_cmp(0, XGATE_XGIF_1,   16'h0000, WORD);    // verify reset
774
      host.wb_cmp(0, XGATE_XGIF_0,   16'h0001, WORD);    // verify reset
775
      host.wb_cmp(0, XGATE_XGSWT,    16'h0000, WORD);    // verify reset
776
      host.wb_cmp(0, XGATE_XGSEM,    16'h0000, WORD);    // verify reset
777
      host.wb_cmp(0, XGATE_XGCCR,    16'h0000, WORD);    // verify reset
778
      host.wb_cmp(0, XGATE_XGPC,     16'h0000, WORD);    // verify reset
779
      host.wb_cmp(0, XGATE_XGR1,     16'h0000, WORD);    // verify reset
780
      host.wb_cmp(0, XGATE_XGR2,     16'h0000, WORD);    // verify reset
781
      host.wb_cmp(0, XGATE_XGR3,     16'h0000, WORD);    // verify reset
782
      host.wb_cmp(0, XGATE_XGR4,     16'h0000, WORD);    // verify reset
783
      host.wb_cmp(0, XGATE_XGR5,     16'h0000, WORD);    // verify reset
784
      host.wb_cmp(0, XGATE_XGR6,     16'h0000, WORD);    // verify reset
785
      host.wb_cmp(0, XGATE_XGR7,     16'h0000, WORD);    // verify reset
786
 
787 41 rehayes
/*
788
  parameter XGMCTL_XGDBGM   = 15'h2000;
789
  parameter XGMCTL_XGSSM    = 15'h1000;
790
  parameter XGMCTL_XGBRKIEM = 15'h0400;
791
  parameter XGMCTL_XGSWEIFM = 15'h0200;
792
  parameter XGMCTL_XGIEM    = 15'h0100;
793 2 rehayes
 
794 41 rehayes
  parameter XGMCTL_XGDBG    = 15'h0020;
795 62 rehayes
  parameter XGMCTL_XGSS     = 15'h0010;
796 41 rehayes
  parameter XGMCTL_XGBRKIE  = 15'h0004;
797
  parameter XGMCTL_XGSWEIF  = 15'h0002;
798 62 rehayes
  parameter XGMCTL_XGIE     = 15'h0001;
799 41 rehayes
*/
800
      // Test bits in the Xgate Control Register (XGMCTL)
801
      data_xgmctl = XGMCTL_XGEM | XGMCTL_XGFRZM | XGMCTL_XGFACTM | XGMCTL_XGFRZ | XGMCTL_XGFACT | XGMCTL_XGE;
802
      host.wb_write(0, XGATE_XGMCTL,   data_xgmctl, WORD);   //
803
      data_xgmctl = XGMCTL_XGFRZ | XGMCTL_XGFACT | XGMCTL_XGE;
804
      host.wb_cmp(  0, XGATE_XGMCTL, data_xgmctl, WORD);
805 2 rehayes
 
806 41 rehayes
      data_xgmctl = XGMCTL_XGEM;
807
      host.wb_write(0, XGATE_XGMCTL,   data_xgmctl, WORD);   //
808
      data_xgmctl = XGMCTL_XGFRZ | XGMCTL_XGFACT;
809
      host.wb_cmp(  0, XGATE_XGMCTL, data_xgmctl, WORD);
810 2 rehayes
 
811 41 rehayes
      data_xgmctl = XGMCTL_XGFRZM | XGMCTL_XGFACTM;
812
      host.wb_write(0, XGATE_XGMCTL,   data_xgmctl, WORD);   //
813
      data_xgmctl = 16'h0000;
814
      host.wb_cmp(  0, XGATE_XGMCTL, data_xgmctl, WORD);
815
 
816
      data_xgmctl = 16'hffff;
817
      host.wb_write(0, XGATE_XGMCTL,   data_xgmctl, H_BYTE);   //
818
      data_xgmctl = 16'h0000;
819
      host.wb_cmp(  0, XGATE_XGMCTL, data_xgmctl, WORD);
820
 
821
      data_xgmctl = 16'hffff;
822
      host.wb_write(0, XGATE_XGMCTL,   data_xgmctl, L_BYTE);   //
823
      data_xgmctl = 16'h0000;
824
      host.wb_cmp(  0, XGATE_XGMCTL, data_xgmctl, WORD);
825
 
826
      // Test the Xgate Vector Base Address Register (XGVBR)
827
      host.wb_write(0, XGATE_XGVBR,  16'h5555, WORD);
828
      host.wb_cmp(0, XGATE_XGVBR,    16'h5554, WORD);
829
 
830
      host.wb_write(0, XGATE_XGVBR,  16'hAAAA, WORD);
831
      host.wb_cmp(0, XGATE_XGVBR,    16'hAAAA, WORD);
832
 
833
      host.wb_write(0, XGATE_XGVBR,  16'hFF55, L_BYTE);
834
      host.wb_cmp(0, XGATE_XGVBR,    16'hAA54, WORD);
835 62 rehayes
 
836 41 rehayes
      host.wb_write(0, XGATE_XGVBR,  16'h55AA, H_BYTE);
837
      host.wb_cmp(0, XGATE_XGVBR,    16'h5554, WORD);
838 62 rehayes
 
839 41 rehayes
      data_xgmctl = XGMCTL_XGEM | XGMCTL_XGE;
840
      host.wb_write(0, XGATE_XGMCTL,   data_xgmctl, WORD);   //
841
      data_xgmctl = XGMCTL_XGE;
842
      host.wb_cmp(  0, XGATE_XGMCTL, data_xgmctl, WORD);
843
      host.wb_write(0, XGATE_XGVBR,  16'hFFFF, WORD);
844
      host.wb_cmp(0, XGATE_XGVBR,    16'h5554, WORD);
845
 
846
      data_xgmctl = XGMCTL_XGEM;
847
      host.wb_write(0, XGATE_XGMCTL,   data_xgmctl, WORD);   //
848
 
849
      // Test the Xgate Software Trigger Register (XGSWT)
850
      host.wb_write(0, XGATE_XGSWT,  16'hFFFF, WORD);
851
      host.wb_cmp(0, XGATE_XGSWT,    16'h00FF, WORD);
852
      host.wb_write(0, XGATE_XGSWT,  16'hFF00, WORD);
853
      host.wb_cmp(0, XGATE_XGSWT,    16'h0000, WORD);
854
 
855
      host.wb_write(0, XGATE_XGSWT,  16'hFF55, L_BYTE);
856
      host.wb_cmp(0, XGATE_XGSWT,    16'h0000, WORD);
857
      host.wb_write(0, XGATE_XGSWT,  16'hFF55, H_BYTE);
858
      host.wb_cmp(0, XGATE_XGSWT,    16'h0000, WORD);
859
 
860
      // Test the Xgate Semaphore Register (XGSEM)
861
      host.wb_write(0, XGATE_XGSEM,  16'hFFFF, WORD);
862
      host.wb_cmp(0, XGATE_XGSEM,    16'h00FF, WORD);
863
      host.wb_write(0, XGATE_XGSEM,  16'hFF00, WORD);
864
      host.wb_cmp(0, XGATE_XGSEM,    16'h0000, WORD);
865
 
866
      host.wb_write(0, XGATE_XGSEM,  16'hFFFF, L_BYTE);
867
      host.wb_cmp(0, XGATE_XGSEM,    16'h0000, WORD);
868
      host.wb_write(0, XGATE_XGSEM,  16'hFFFF, H_BYTE);
869
      host.wb_cmp(0, XGATE_XGSEM,    16'h0000, WORD);
870
 
871
      // Test the Xgate Condition Code Register (XGCCR)
872
      host.wb_write(0, XGATE_XGCCR,  16'hFFFF, L_BYTE);
873
      host.wb_cmp(0, XGATE_XGCCR,    16'h000F, WORD);
874
      host.wb_write(0, XGATE_XGCCR,  16'hFFF0, WORD);
875
      host.wb_cmp(0, XGATE_XGCCR,    16'h0000, WORD);
876
 
877
      // Test the Xgate Program Counter Register (XGPC)
878
      host.wb_write(0, XGATE_XGPC,  16'hFF55, L_BYTE);
879
      host.wb_cmp(0, XGATE_XGPC,    16'h0055, WORD);
880
      host.wb_write(0, XGATE_XGPC,  16'hAAFF, H_BYTE);
881
      host.wb_cmp(0, XGATE_XGPC,    16'hAA55, WORD);
882
      host.wb_write(0, XGATE_XGPC,  16'h9966, WORD);
883
      host.wb_cmp(0, XGATE_XGPC,    16'h9966, WORD);
884
 
885
      // Test the Xgate Register #1 (XGR1)
886
      host.wb_write(0, XGATE_XGR1,  16'hFF33, L_BYTE);
887
      host.wb_cmp(0, XGATE_XGR1,    16'h0033, WORD);
888
      host.wb_write(0, XGATE_XGR1,  16'hccFF, H_BYTE);
889
      host.wb_cmp(0, XGATE_XGR1,    16'hcc33, WORD);
890
      host.wb_write(0, XGATE_XGR1,  16'hf11f, WORD);
891
      host.wb_cmp(0, XGATE_XGR1,    16'hf11f, WORD);
892
 
893
      // Test the Xgate Register #2 (XGR2)
894
      host.wb_write(0, XGATE_XGR2,  16'hFF11, L_BYTE);
895
      host.wb_cmp(0, XGATE_XGR2,    16'h0011, WORD);
896
      host.wb_write(0, XGATE_XGR2,  16'h22FF, H_BYTE);
897
      host.wb_cmp(0, XGATE_XGR2,    16'h2211, WORD);
898
      host.wb_write(0, XGATE_XGR2,  16'hddee, WORD);
899
      host.wb_cmp(0, XGATE_XGR2,    16'hddee, WORD);
900
 
901
      // Test the Xgate Register #3 (XGR3)
902
      host.wb_write(0, XGATE_XGR3,  16'hFF43, L_BYTE);
903
      host.wb_cmp(0, XGATE_XGR3,    16'h0043, WORD);
904
      host.wb_write(0, XGATE_XGR3,  16'h54FF, H_BYTE);
905
      host.wb_cmp(0, XGATE_XGR3,    16'h5443, WORD);
906
      host.wb_write(0, XGATE_XGR3,  16'habbc, WORD);
907
      host.wb_cmp(0, XGATE_XGR3,    16'habbc, WORD);
908
 
909
      // Test the Xgate Register #4 (XGR4)
910
      host.wb_write(0, XGATE_XGR4,  16'hFF54, L_BYTE);
911
      host.wb_cmp(0, XGATE_XGR4,    16'h0054, WORD);
912
      host.wb_write(0, XGATE_XGR4,  16'h65FF, H_BYTE);
913
      host.wb_cmp(0, XGATE_XGR4,    16'h6554, WORD);
914
      host.wb_write(0, XGATE_XGR4,  16'h9aab, WORD);
915
      host.wb_cmp(0, XGATE_XGR4,    16'h9aab, WORD);
916
 
917
      // Test the Xgate Register #5 (XGR5)
918
      host.wb_write(0, XGATE_XGR5,  16'hFF65, L_BYTE);
919
      host.wb_cmp(0, XGATE_XGR5,    16'h0065, WORD);
920
      host.wb_write(0, XGATE_XGR5,  16'h76FF, H_BYTE);
921
      host.wb_cmp(0, XGATE_XGR5,    16'h7665, WORD);
922
      host.wb_write(0, XGATE_XGR5,  16'h899a, WORD);
923
      host.wb_cmp(0, XGATE_XGR5,    16'h899a, WORD);
924
 
925
      // Test the Xgate Register #6 (XGR6)
926
      host.wb_write(0, XGATE_XGR6,  16'hFF76, L_BYTE);
927
      host.wb_cmp(0, XGATE_XGR6,    16'h0076, WORD);
928
      host.wb_write(0, XGATE_XGR6,  16'h87FF, H_BYTE);
929
      host.wb_cmp(0, XGATE_XGR6,    16'h8776, WORD);
930
      host.wb_write(0, XGATE_XGR6,  16'h7889, WORD);
931
      host.wb_cmp(0, XGATE_XGR6,    16'h7889, WORD);
932
 
933
      // Test the Xgate Register #7 (XGR7)
934
      host.wb_write(0, XGATE_XGR7,  16'hFF87, L_BYTE);
935
      host.wb_cmp(0, XGATE_XGR7,    16'h0087, WORD);
936
      host.wb_write(0, XGATE_XGR7,  16'h98FF, H_BYTE);
937
      host.wb_cmp(0, XGATE_XGR7,    16'h9887, WORD);
938
      host.wb_write(0, XGATE_XGR7,  16'h6778, WORD);
939
      host.wb_cmp(0, XGATE_XGR7,    16'h6778, WORD);
940
 
941
      host.wb_cmp(0, XGATE_XGPC,    16'h9966, WORD);
942
      host.wb_cmp(0, XGATE_XGR1,    16'hf11f, WORD);
943
      host.wb_cmp(0, XGATE_XGR2,    16'hddee, WORD);
944
      host.wb_cmp(0, XGATE_XGR3,    16'habbc, WORD);
945
      host.wb_cmp(0, XGATE_XGR4,    16'h9aab, WORD);
946
      host.wb_cmp(0, XGATE_XGR5,    16'h899a, WORD);
947
      host.wb_cmp(0, XGATE_XGR6,    16'h7889, WORD);
948
      host.wb_cmp(0, XGATE_XGR7,    16'h6778, WORD);
949
 
950 2 rehayes
  end
951
endtask
952
 
953
 
954 36 rehayes
////////////////////////////////////////////////////////////////////////////////
955
// check RAM Read/Write from host
956
task host_ram;
957
  begin
958
    test_num = test_num + 1;
959
    $display("TEST #%d Starts at vector=%d, host_ram", test_num, vector);
960
 
961
    host.wb_write(1, SYS_RAM_BASE, 16'h5555, WORD);
962
    host.wb_cmp(  0, SYS_RAM_BASE, 16'h5555, WORD);
963
 
964
    repeat(5) @(posedge mstr_test_clk);
965
    p_ram.dump_ram(0);
966
 
967
  end
968
endtask
969
 
970
////////////////////////////////////////////////////////////////////////////////
971 11 rehayes
// Poll for XGATE Interrupt set
972
task wait_irq_set;
973
  input [ 6:0] chan_val;
974
  begin
975
    while(!xgif[chan_val])
976
      @(posedge mstr_test_clk); // poll it until it is set
977
    $display("XGATE Interrupt Request #%d set detected at vector =%d", chan_val, vector);
978
  end
979
endtask
980 2 rehayes
 
981 36 rehayes
////////////////////////////////////////////////////////////////////////////////
982 11 rehayes
// Poll for debug bit set
983
task wait_debug_set;
984
  begin
985 36 rehayes
    host.wb_read(1, XGATE_XGMCTL, q, WORD);
986 11 rehayes
    while(~|(q & XGMCTL_XGDBG))
987 36 rehayes
      host.wb_read(1, XGATE_XGMCTL, q, WORD); // poll it until it is set
988 11 rehayes
    $display("DEBUG Flag set detected at vector =%d", vector);
989
  end
990
endtask
991 2 rehayes
 
992 11 rehayes
 
993 36 rehayes
////////////////////////////////////////////////////////////////////////////////
994 2 rehayes
task system_reset;  // reset system
995
  begin
996
      repeat(1) @(posedge mstr_test_clk);
997
      sync_reset = 1'b1;  // Make the sync reset 1 clock cycle long
998 62 rehayes
      #2;                 // move the async reset away from the clock edge
999
      rstn = 1'b0;        // assert async reset
1000
      #5;                 // Keep the async reset pulse with less than a clock cycle
1001
      rstn = 1'b1;        // negate async reset
1002 2 rehayes
      repeat(1) @(posedge mstr_test_clk);
1003
      sync_reset = 1'b0;
1004
 
1005
      $display("\nstatus: %t System Reset Task Done", $time);
1006
      test_num = test_num + 1;
1007
 
1008
      repeat(2) @(posedge mstr_test_clk);
1009
   end
1010
endtask
1011
 
1012
 
1013 36 rehayes
////////////////////////////////////////////////////////////////////////////////
1014 2 rehayes
task activate_channel;
1015
  input [ 6:0] chan_val;
1016
  begin
1017 21 rehayes
    $display("Activating Channel %d", chan_val);
1018 2 rehayes
 
1019 21 rehayes
    channel_req[chan_val] = 1'b1; //
1020
    repeat(1) @(posedge mstr_test_clk);
1021 2 rehayes
  end
1022
endtask
1023
 
1024
 
1025 36 rehayes
////////////////////////////////////////////////////////////////////////////////
1026 2 rehayes
task clear_channel;
1027
  input [ 6:0] chan_val;
1028
  begin
1029 21 rehayes
    $display("Clearing Channel interrupt input #%d", chan_val);
1030 2 rehayes
 
1031 21 rehayes
    channel_req[chan_val] = 1'b0; //
1032
    repeat(1) @(posedge mstr_test_clk);
1033
  end
1034 2 rehayes
endtask
1035
 
1036
 
1037 36 rehayes
////////////////////////////////////////////////////////////////////////////////
1038 2 rehayes
task clear_irq_flag;
1039
  input [ 6:0] chan_val;
1040
  begin
1041
      $display("Clearing Channel interrupt flag #%d", chan_val);
1042
      if (0 < chan_val < 16)
1043 62 rehayes
        host.wb_write(1, XGATE_XGIF_0, 16'hffff, WORD);
1044 2 rehayes
      if (15 < chan_val < 32)
1045 62 rehayes
        host.wb_write(1, XGATE_XGIF_1, 16'hffff, WORD);
1046 2 rehayes
      if (31 < chan_val < 48)
1047 62 rehayes
        host.wb_write(1, XGATE_XGIF_2, 16'hffff, WORD);
1048 2 rehayes
      if (47 < chan_val < 64)
1049 62 rehayes
        host.wb_write(1, XGATE_XGIF_3, 16'hffff, WORD);
1050 2 rehayes
      if (63 < chan_val < 80)
1051 62 rehayes
        host.wb_write(1, XGATE_XGIF_4, 16'hffff, WORD);
1052 2 rehayes
      if (79 < chan_val < 96)
1053 62 rehayes
        host.wb_write(1, XGATE_XGIF_5, 16'hffff, WORD);
1054 2 rehayes
      if (95 < chan_val < 112)
1055 62 rehayes
        host.wb_write(1, XGATE_XGIF_6, 16'hffff, WORD);
1056 2 rehayes
      if (111 < chan_val < 128)
1057 62 rehayes
        host.wb_write(1, XGATE_XGIF_7, 16'hffff, WORD);
1058 2 rehayes
 
1059 21 rehayes
      channel_req[chan_val] = 1'b0; //
1060 2 rehayes
      repeat(1) @(posedge mstr_test_clk);
1061
   end
1062
endtask
1063
 
1064
 
1065 36 rehayes
////////////////////////////////////////////////////////////////////////////////
1066 2 rehayes
task activate_thread_sw;
1067
  input [ 6:0] chan_val;
1068
  begin
1069 50 rehayes
      $display("Activating Software Thread - Channel #%d", chan_val);
1070 2 rehayes
 
1071 11 rehayes
      data_xgmctl = XGMCTL_XGEM | XGMCTL_XGE;
1072 36 rehayes
      host.wb_write(0, XGATE_XGMCTL, data_xgmctl, WORD);   // Enable XGATE
1073 2 rehayes
 
1074 21 rehayes
      channel_req[chan_val] = 1'b1; //
1075 2 rehayes
      repeat(1) @(posedge mstr_test_clk);
1076
   end
1077
endtask
1078
 
1079 36 rehayes
////////////////////////////////////////////////////////////////////////////////
1080 41 rehayes
task read_ram_cmp;
1081
  input [15:0] address;
1082
  input [15:0] value;
1083 62 rehayes
  reg   [15:0] q;
1084 41 rehayes
  begin
1085
 
1086
      // BIGENDIAN
1087
      q = {p_ram.ram_8[address], p_ram.ram_8[address+1]};
1088
      // "X" compares don't work, "X" in value or q always match
1089
      if (value != q)
1090
        begin
1091 62 rehayes
          error_count = error_count + 1;
1092
          $display("RAM Data compare error at address %h. Received %h, expected %h at time %t", address, q, value, $time);
1093 41 rehayes
        end
1094
   end
1095
endtask
1096
 
1097
////////////////////////////////////////////////////////////////////////////////
1098 5 rehayes
task wrap_up;
1099
  begin
1100 21 rehayes
    test_num = test_num + 1;
1101
    repeat(10) @(posedge mstr_test_clk);
1102 5 rehayes
    $display("\nSimulation Finished!! - vector =%d", vector);
1103
    if (error_count == 0)
1104
      $display("Simulation Passed");
1105
    else
1106 21 rehayes
      $display("Simulation Failed  --- Errors =%d", error_count);
1107 5 rehayes
 
1108
    $finish;
1109
  end
1110
endtask
1111
 
1112 36 rehayes
////////////////////////////////////////////////////////////////////////////////
1113 2 rehayes
function [15:0] four_2_16;
1114
  input [3:0] vector;
1115
  begin
1116
    case (vector)
1117
      4'h0 : four_2_16 = 16'b0000_0000_0000_0001;
1118
      4'h1 : four_2_16 = 16'b0000_0000_0000_0010;
1119
      4'h2 : four_2_16 = 16'b0000_0000_0000_0100;
1120
      4'h3 : four_2_16 = 16'b0000_0000_0000_1000;
1121
      4'h4 : four_2_16 = 16'b0000_0000_0001_0000;
1122
      4'h5 : four_2_16 = 16'b0000_0000_0010_0000;
1123
      4'h6 : four_2_16 = 16'b0000_0000_0100_0000;
1124
      4'h7 : four_2_16 = 16'b0000_0000_1000_0000;
1125
      4'h8 : four_2_16 = 16'b0000_0001_0000_0000;
1126
      4'h9 : four_2_16 = 16'b0000_0010_0000_0000;
1127
      4'ha : four_2_16 = 16'b0000_0100_0000_0000;
1128
      4'hb : four_2_16 = 16'b0000_1000_0000_0000;
1129
      4'hc : four_2_16 = 16'b0001_0000_0000_0000;
1130
      4'hd : four_2_16 = 16'b0010_0000_0000_0000;
1131
      4'he : four_2_16 = 16'b0100_0000_0000_0000;
1132
      4'hf : four_2_16 = 16'b1000_0000_0000_0000;
1133
    endcase
1134
  end
1135
endfunction
1136
 
1137
endmodule  // tst_bench_top
1138
 
1139 36 rehayes
////////////////////////////////////////////////////////////////////////////////
1140
////////////////////////////////////////////////////////////////////////////////
1141
////////////////////////////////////////////////////////////////////////////////
1142 62 rehayes
module bus_arbitration  #(parameter dwidth = 16,
1143
                          parameter awidth = 24,
1144 54 rehayes
                          parameter ram_base = 0,
1145
                          parameter ram_size = 16'hffff,
1146
                          parameter slv1_base = 0,
1147
                          parameter slv1_size = 1,
1148
                          parameter slv2_base = 0,
1149
                          parameter slv2_size = 1)
1150 36 rehayes
  (
1151
  // System bus I/O
1152 62 rehayes
  output reg                 sys_cyc,
1153
  output reg                 sys_stb,
1154
  output reg                 sys_we,
1155 54 rehayes
  output reg [dwidth/8 -1:0] sys_sel,
1156
  output reg [awidth   -1:0] sys_adr,
1157
  output reg [dwidth   -1:0] sys_dout,
1158
  output     [dwidth   -1:0] sys_din,
1159 62 rehayes
 
1160 36 rehayes
  // Host bus I/O
1161 62 rehayes
  output                     host_ack,
1162 36 rehayes
  output     [dwidth   -1:0] host_dout,
1163 62 rehayes
  input                      host_cyc,
1164
  input                      host_stb,
1165
  input                      host_we,
1166
  input      [dwidth/8 -1:0] host_sel,
1167
  input      [awidth   -1:0] host_adr,
1168
  input      [dwidth   -1:0] host_din,
1169
 
1170 36 rehayes
  // Alternate Bus Master #1 Bus I/O
1171 62 rehayes
  output                     alt1_ack,
1172 36 rehayes
  output     [dwidth   -1:0] alt1_dout,
1173 62 rehayes
  input                      alt1_cyc,
1174
  input                      alt1_stb,
1175
  input                      alt1_we,
1176
  input      [dwidth/8 -1:0] alt1_sel,
1177
  input      [awidth   -1:0] alt1_adr,
1178
  input      [dwidth   -1:0] alt1_din,
1179
 
1180 54 rehayes
  // System RAM memory signals
1181 62 rehayes
  output                     ram_sel,
1182
  input      [dwidth   -1:0] ram_dout,
1183 54 rehayes
 
1184 36 rehayes
  // Slave #1 Bus I/O
1185 62 rehayes
  output                     slv1_stb,
1186
  input                      slv1_ack,
1187
  input      [dwidth   -1:0] slv1_din,
1188
 
1189 36 rehayes
  // Slave #2 Bus I/O
1190 62 rehayes
  output                     slv2_stb,
1191
  input                      slv2_ack,
1192
  input      [dwidth   -1:0] slv2_din,
1193
 
1194 36 rehayes
  // Miscellaneous
1195 62 rehayes
  input                      host_clk,
1196
  input                      risc_clk,
1197
  input                      rst,  // No Connect
1198
  input                      err,  // No Connect
1199
  input                      rty   // No Connect
1200 36 rehayes
  );
1201 62 rehayes
 
1202 60 rehayes
  // States for bus arbitration
1203 54 rehayes
  parameter [1:0] BUS_IDLE = 2'b00,
1204 62 rehayes
                  HOST_OWNS = 2'b10,
1205
                  RISC_OWNS = 2'b11;
1206
 
1207
  parameter max_bus_hold = 5;    // Max number of cycles any bus master can hold the system bus
1208 60 rehayes
  parameter ram_wait_states = 0; // Number between 0 and 15
1209 36 rehayes
  //////////////////////////////////////////////////////////////////////////////
1210
  //
1211
  // Local Wires and Registers
1212
  //
1213 62 rehayes
  wire       ram_ack;        //
1214
  wire       any_ack;        //
1215
  reg        host_wait;      // Host bus in wait state, Hold the bus till the transaction complets
1216 36 rehayes
  reg  [3:0] host_cycle_cnt; // Used to count the cycle the host and break the lock if the risc needs access
1217 62 rehayes
 
1218
  wire       risc_lock;      // RISC has the slave bus
1219
  reg        risc_wait;      // RISC bus in wait state, Hold the bus till the transaction complets
1220 36 rehayes
  reg  [3:0] risc_cycle_cnt; // Used to count the cycle the risc and break the lock if the host needs access
1221 62 rehayes
 
1222 54 rehayes
  reg  [1:0] owner_state;
1223
  reg  [1:0] owner_ns;
1224 36 rehayes
 
1225 62 rehayes
  wire       host_timeout;
1226
  wire       risc_timeout;
1227
 
1228
  wire       ram_ack_dly;    // Delayed bus ack to simulate bus wait states
1229 60 rehayes
  reg  [3:0] ack_dly_cnt;    // Counter to delay bus ack to master modules
1230 54 rehayes
 
1231
 
1232
  //
1233 36 rehayes
  always @(posedge host_clk or negedge rst)
1234
    if (!rst)
1235 54 rehayes
      owner_state <= BUS_IDLE;
1236 36 rehayes
    else
1237 54 rehayes
      owner_state <= owner_ns;
1238 62 rehayes
 
1239 54 rehayes
  //
1240
  always @*
1241
    case (owner_state)
1242
      BUS_IDLE :
1243
        begin
1244
          if (host_cyc)
1245
            owner_ns = HOST_OWNS;
1246
          else if (alt1_cyc)
1247
            owner_ns = RISC_OWNS;
1248
        end
1249
      HOST_OWNS :
1250
        begin
1251
          if (!host_cyc && !alt1_cyc)
1252
            owner_ns = BUS_IDLE;
1253
          else if (alt1_cyc && (!host_cyc || host_timeout))
1254
            owner_ns = RISC_OWNS;
1255
        end
1256
      RISC_OWNS :
1257
        begin
1258
          if (!host_cyc && !alt1_cyc)
1259
            owner_ns = BUS_IDLE;
1260
          else if (host_cyc && (!alt1_cyc || risc_timeout))
1261
            owner_ns = HOST_OWNS;
1262
        end
1263
      default : owner_ns = BUS_IDLE;
1264
    endcase
1265 36 rehayes
 
1266 62 rehayes
 
1267 60 rehayes
  assign host_timeout = (owner_state == HOST_OWNS) && (host_cycle_cnt > max_bus_hold) && any_ack;
1268
  assign risc_timeout = (owner_state == RISC_OWNS) && (risc_cycle_cnt > max_bus_hold) && any_ack;
1269 54 rehayes
 
1270
  // Start counting cycles that the host has the bus, if the risc is also requesting the bus
1271 36 rehayes
  always @(posedge host_clk or negedge rst)
1272
    if (!rst)
1273
      host_cycle_cnt <= 0;
1274 54 rehayes
    else if ((owner_state != HOST_OWNS) || !alt1_cyc)
1275
      host_cycle_cnt <= 0;
1276 62 rehayes
    else if (&host_cycle_cnt && !host_timeout)  // Don't allow rollover
1277 54 rehayes
      host_cycle_cnt <= host_cycle_cnt;
1278
    else if ((owner_state == HOST_OWNS) && alt1_cyc)
1279
      host_cycle_cnt <= host_cycle_cnt + 1'b1;
1280 36 rehayes
 
1281 54 rehayes
  // Start counting cycles that the risc has the bus, if the host is also requesting the bus
1282 36 rehayes
  always @(posedge host_clk or negedge rst)
1283
    if (!rst)
1284
      risc_cycle_cnt <= 0;
1285 54 rehayes
    else if ((owner_state != RISC_OWNS) || !host_cyc)
1286
      risc_cycle_cnt <= 0;
1287 62 rehayes
    else if (&risc_cycle_cnt && !risc_timeout)  // Don't allow rollover
1288 54 rehayes
      risc_cycle_cnt <= risc_cycle_cnt;
1289
    else if ((owner_state == RISC_OWNS) && host_cyc)
1290
      risc_cycle_cnt <= risc_cycle_cnt + 1'b1;
1291 36 rehayes
 
1292 62 rehayes
  // Aribartration Logic for System Bus access
1293 54 rehayes
  assign any_ack  = slv1_ack || slv2_ack || ram_ack;
1294
  assign host_ack = (owner_state == HOST_OWNS) && any_ack && host_cyc;
1295
  assign alt1_ack = (owner_state == RISC_OWNS) && any_ack && alt1_cyc;
1296 36 rehayes
 
1297 54 rehayes
 
1298
  // Address decoding for different Slave module instances
1299
  assign slv1_stb = sys_stb && (sys_adr >= slv1_base) && (sys_adr < (slv1_base + slv1_size));
1300
  assign slv2_stb = sys_stb && (sys_adr >= slv2_base) && (sys_adr < (slv2_base + slv2_size));
1301 62 rehayes
 
1302 36 rehayes
  // Address decoding for Testbench access to RAM
1303 54 rehayes
  assign ram_sel = sys_cyc && sys_stb && !(slv1_stb || slv2_stb) &&
1304 62 rehayes
                   (sys_adr >= ram_base) &&
1305 54 rehayes
                   (sys_adr < (ram_base + ram_size));
1306 62 rehayes
 
1307 60 rehayes
  // Throw in some wait states from the memory
1308
  always @(posedge host_clk)
1309
    if ((ack_dly_cnt == ram_wait_states) || !ram_sel)
1310
      ack_dly_cnt <= 0;
1311
    else if (ram_sel)
1312
      ack_dly_cnt <= ack_dly_cnt + 1'b1;
1313 36 rehayes
 
1314 60 rehayes
  assign ram_ack_dly = (ack_dly_cnt == ram_wait_states);
1315
  assign ram_ack = ram_sel && ram_ack_dly;
1316 36 rehayes
 
1317 60 rehayes
 
1318 54 rehayes
  // Create the System Read Data Bus from the Slave output data buses
1319
  assign sys_din = ({dwidth{slv1_stb}} & slv1_din) |
1320 62 rehayes
                   ({dwidth{slv2_stb}} & slv2_din) |
1321
                   ({dwidth{ram_sel}}  & ram_dout);
1322 36 rehayes
 
1323 54 rehayes
  // Mux for System Bus access
1324
  always @*
1325
    case (owner_state)
1326
      BUS_IDLE :
1327
        begin
1328 62 rehayes
          sys_cyc   = 0;
1329
          sys_stb   = 0;
1330
          sys_we    = 0;
1331
          sys_sel   = 0;
1332
          sys_adr   = 0;
1333
          sys_dout  = 0;
1334 54 rehayes
        end
1335
      HOST_OWNS :
1336
        begin
1337 62 rehayes
          sys_cyc   = host_cyc;
1338
          sys_stb   = host_stb;
1339
          sys_we    = host_we;
1340
          sys_sel   = host_sel;
1341
          sys_adr   = host_adr;
1342
          sys_dout  = host_din;
1343 54 rehayes
        end
1344
      RISC_OWNS :
1345
        begin
1346 62 rehayes
          sys_cyc   = alt1_cyc;
1347
          sys_stb   = alt1_stb;
1348
          sys_we    = alt1_we;
1349
          sys_sel   = alt1_sel;
1350
          sys_adr   = alt1_adr;
1351
          sys_dout  = alt1_din;
1352 54 rehayes
        end
1353
      default :
1354
        begin
1355 62 rehayes
          sys_cyc   = 0;
1356
          sys_stb   = 0;
1357
          sys_we    = 0;
1358
          sys_sel   = 0;
1359
          sys_adr   = 0;
1360
          sys_dout  = 0;
1361 54 rehayes
        end
1362
    endcase
1363 36 rehayes
 
1364 54 rehayes
endmodule   // bus_arbitration
1365 36 rehayes
 
1366 54 rehayes
////////////////////////////////////////////////////////////////////////////////
1367
////////////////////////////////////////////////////////////////////////////////
1368
////////////////////////////////////////////////////////////////////////////////
1369
module tb_slave #(parameter SINGLE_CYCLE = 1'b0,  // No bus wait state added
1370 62 rehayes
                  parameter DWIDTH = 16)          // Data bus width
1371 54 rehayes
  (
1372
  // Wishbone Signals
1373 62 rehayes
  output [DWIDTH-1:0] wb_dat_o,      // databus output
1374
  output              wb_ack_o,     // bus cycle acknowledge output
1375
  input               wb_clk_i,     // master clock input
1376
  input               wb_rst_i,     // synchronous active high reset
1377
  input               arst_i,       // asynchronous reset
1378
  input         [2:0] wb_adr_i,      // lower address bits
1379
  input  [DWIDTH-1:0] wb_dat_i,      // databus input
1380
  input               wb_we_i,      // write enable input
1381
  input               wb_stb_i,     // stobe/core select signal
1382
  input               wb_cyc_i,     // valid bus cycle input
1383
  input         [1:0] wb_sel_i,      // Select byte in word bus transaction
1384
  // Slave unique IO Signals
1385
  output reg          error_pulse,  // Error detected output pulse
1386
  output reg          ack_pulse,    // Thread ack output pulse
1387
  input        [19:0] vector
1388 54 rehayes
  );
1389 62 rehayes
 
1390
  wire                  async_rst_b;   // Asyncronous reset
1391
  wire                  sync_reset;    // Syncronous reset
1392
 
1393 54 rehayes
  // Wishbone Bus interface
1394
  // registers
1395 62 rehayes
  reg                bus_wait_state;  // Holdoff wb_ack_o for one clock to add wait state
1396 54 rehayes
  reg  [DWIDTH-1:0]  rd_data_mux;     // Pseudo Register, WISHBONE Read Data Mux
1397
  reg  [DWIDTH-1:0]  rd_data_reg;     // Latch for WISHBONE Read Data
1398 36 rehayes
 
1399 54 rehayes
  reg  [15:0] check_point_reg;
1400
  reg  [15:0] channel_ack_reg;
1401
  reg  [15:0] channel_err_reg;
1402 36 rehayes
 
1403 54 rehayes
  event check_point_wrt;
1404
  event channel_ack_wrt;
1405
  event channel_err_wrt;
1406 36 rehayes
 
1407 54 rehayes
  // Wires
1408 62 rehayes
  wire   module_sel;      // This module is selected for bus transaction
1409
  wire   wb_wacc;         // WISHBONE Write Strobe
1410
  wire   wb_racc;         // WISHBONE Read Access (Clock gating signal)
1411 36 rehayes
 
1412 54 rehayes
  //
1413
  // module body
1414
  //
1415 36 rehayes
 
1416 54 rehayes
  // generate internal resets
1417
 
1418
 
1419
  // generate wishbone signals
1420
  assign module_sel = wb_cyc_i && wb_stb_i;
1421
  assign wb_wacc    = module_sel && wb_we_i && (wb_ack_o || SINGLE_CYCLE);
1422
  assign wb_racc    = module_sel && !wb_we_i;
1423
  assign wb_ack_o   = SINGLE_CYCLE ? module_sel : bus_wait_state;
1424
  assign wb_dat_o   = SINGLE_CYCLE ? rd_data_mux : rd_data_reg;
1425
 
1426
  // generate acknowledge output signal, By using register all accesses takes two cycles.
1427
  //  Accesses in back to back clock cycles are not possable.
1428
  always @(posedge wb_clk_i or negedge arst_i)
1429
    if (!arst_i)
1430 62 rehayes
      bus_wait_state <=  1'b0;
1431 54 rehayes
    else if (wb_rst_i)
1432 62 rehayes
      bus_wait_state <=  1'b0;
1433 54 rehayes
    else
1434 62 rehayes
      bus_wait_state <=  module_sel && !bus_wait_state;
1435 54 rehayes
 
1436
  // assign data read bus -- DAT_O
1437
  always @(posedge wb_clk_i)
1438 62 rehayes
    if ( wb_racc )                     // Clock gate for power saving
1439 54 rehayes
      rd_data_reg <= rd_data_mux;
1440
 
1441
  // WISHBONE Read Data Mux
1442
  always @*
1443
    case (wb_adr_i) // synopsys parallel_case
1444
      3'b000: rd_data_mux = check_point_reg;
1445
      3'b001: rd_data_mux = channel_ack_reg;
1446
      3'b010: rd_data_mux = channel_err_reg;
1447
      3'b011: rd_data_mux = 16'b0;
1448
    endcase
1449
 
1450
  // generate wishbone write register strobes
1451
  always @(posedge wb_clk_i or negedge arst_i)
1452
    begin
1453
      if (!arst_i)
1454 62 rehayes
        begin
1455
          check_point_reg <= 0;
1456
          channel_ack_reg <= 0;
1457
          channel_err_reg <= 0;
1458
          ack_pulse       <= 0;
1459
          error_pulse     <= 0;
1460
        end
1461 54 rehayes
      else if (wb_wacc)
1462
        case (wb_adr_i) // synopsys parallel_case
1463
           3'b000 :
1464
             begin
1465 62 rehayes
               check_point_reg[ 7:0] <= wb_sel_i[0] ? wb_dat_i[ 7:0] : check_point_reg[ 7:0];
1466
               check_point_reg[15:8] <= wb_sel_i[1] ? wb_dat_i[15:8] : check_point_reg[15:8];
1467
               -> check_point_wrt;
1468 54 rehayes
             end
1469
           3'b001 :
1470
             begin
1471 62 rehayes
               channel_ack_reg[ 7:0] <= wb_sel_i[0] ? wb_dat_i[ 7:0] : channel_ack_reg[ 7:0];
1472
               channel_ack_reg[15:8] <= wb_sel_i[1] ? wb_dat_i[15:8] : channel_ack_reg[15:8];
1473 54 rehayes
               ack_pulse <= 1;
1474 62 rehayes
               -> channel_ack_wrt;
1475 54 rehayes
             end
1476
           3'b010 :
1477
             begin
1478 62 rehayes
               channel_err_reg[ 7:0] <= wb_sel_i[0] ? wb_dat_i[ 7:0] : channel_err_reg[ 7:0];
1479
               channel_err_reg[15:8] <= wb_sel_i[1] ? wb_dat_i[15:8] : channel_err_reg[15:8];
1480 54 rehayes
               error_pulse <= 1'b1;
1481 62 rehayes
               -> channel_err_wrt;
1482 54 rehayes
             end
1483
           3'b011 :
1484
             begin
1485
             end
1486
           default: ;
1487
        endcase
1488
      else
1489
        begin
1490
          ack_pulse   <= 0;
1491
          error_pulse <= 1'b0;
1492 62 rehayes
        end
1493 54 rehayes
    end
1494
 
1495
  always @check_point_wrt
1496
    begin
1497
      #1;
1498
      $display("\nSoftware Checkpoint #%h -- at vector=%d\n", check_point_reg, vector);
1499
    end
1500
 
1501
  always @channel_err_wrt
1502
    begin
1503
      #1;
1504 62 rehayes
      $display("\n ------ !!!!! Software Checkpoint Error #%d -- at vector=%d\n  -------", channel_err_reg, vector);
1505 54 rehayes
    end
1506
 
1507
 
1508
endmodule // tb_slave
1509
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.