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

Subversion Repositories xgate

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

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

powered by: WebSVN 2.1.0

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