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

Subversion Repositories or1k_soc_on_altera_embedded_dev_kit

[/] [or1k_soc_on_altera_embedded_dev_kit/] [trunk/] [soc/] [rtl/] [adv_debug_sys/] [Hardware/] [adv_dbg_if/] [bench/] [simulated_system/] [adv_dbg_tb.v] - Blame information for rev 21

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 21 xianfeng
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  adv_dbg_tb.v                                                ////
4
////                                                              ////
5
////                                                              ////
6
////  Testbench for the SoC Advanced Debug Interface.             ////
7
////                                                              ////
8
////  Author(s):                                                  ////
9
////       Nathan Yawn (nathan.yawn@opencored.org)                ////
10
////                                                              ////
11
////                                                              ////
12
////                                                              ////
13
//////////////////////////////////////////////////////////////////////
14
////                                                              ////
15
//// Copyright (C) 2008        Authors                            ////
16
////                                                              ////
17
//// This source file may be used and distributed without         ////
18
//// restriction provided that this copyright statement is not    ////
19
//// removed from the file and that any derivative work contains  ////
20
//// the original copyright notice and the associated disclaimer. ////
21
////                                                              ////
22
//// This source file is free software; you can redistribute it   ////
23
//// and/or modify it under the terms of the GNU Lesser General   ////
24
//// Public License as published by the Free Software Foundation; ////
25
//// either version 2.1 of the License, or (at your option) any   ////
26
//// later version.                                               ////
27
////                                                              ////
28
//// This source is distributed in the hope that it will be       ////
29
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
30
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
31
//// PURPOSE.  See the GNU Lesser General Public License for more ////
32
//// details.                                                     ////
33
////                                                              ////
34
//// You should have received a copy of the GNU Lesser General    ////
35
//// Public License along with this source; if not, download it   ////
36
//// from http://www.opencores.org/lgpl.shtml                     ////
37
////                                                              ////
38
//////////////////////////////////////////////////////////////////////
39
//
40
// CVS Revision History
41
//
42
// $Log: adv_dbg_tb.v,v $
43
// Revision 1.7  2010-01-13 00:55:45  Nathan
44
// Created hi-speed mode for burst reads.  This will probably be most beneficial to the OR1K module, as GDB does a burst read of all the GPRs each time a microinstruction is single-stepped.
45
//
46
// Revision 1.2  2009/05/17 20:54:55  Nathan
47
// Changed email address to opencores.org
48
//
49
// Revision 1.1  2008/07/08 19:11:55  Nathan
50
// Added second testbench to simulate a complete system, including OR1200, wb_conbus, and onchipram.  Renamed sim-only testbench directory from verilog to simulated_system.
51
//
52
// Revision 1.11  2008/07/08 18:53:47  Nathan
53
// Fixed wrong include name.
54
//
55
// Revision 1.10  2008/06/30 20:09:19  Nathan
56
// Removed code to select top-level module as active (it served no purpose).  Re-numbered modules, requiring changes to testbench and software driver.
57
//
58
 
59
 
60
`include "tap_defines.v"
61
`include "adbg_defines.v"
62
`include "adbg_wb_defines.v"
63
`include "wb_model_defines.v"
64
 
65
// Polynomial for the CRC calculation
66
// Yes, it's backwards.  Yes, this is on purpose.
67
// To decrease logic + routing, we want to shift the CRC calculation
68
// in the same direction we use to shift the data out, LSB first.
69
`define DBG_CRC_POLY 32'hedb88320
70
 
71
// These are indicies into an array which hold values for the JTAG outputs
72
`define JTAG_TMS 0
73
`define JTAG_TCK 1
74
`define JTAG_TDO 2
75
 
76
`define JTAG_TMS_bit 3'h1
77
`define JTAG_TCK_bit 3'h2
78
`define JTAG_TDO_bit 3'h4
79
 
80
`define wait_jtag_period #50
81
 
82
 
83
module adv_debug_tb;
84
 
85
// Connections to the JTAG TAP
86
reg jtag_tck_o;
87
reg jtag_tms_o;
88
reg jtag_tdo_o;
89
wire jtag_tdi_i;
90
 
91
// Connections between TAP and debug module
92
wire capture_dr;
93
wire shift_dr;
94
wire pause_dr;
95
wire update_dr;
96
wire dbg_rst;
97
wire dbg_tdi;
98
wire dbg_tdo;
99
wire dbg_sel;
100
 
101
// Connections between the debug module and the wishbone
102
`ifdef DBG_WISHBONE_SUPPORTED
103
wire [31:0] wb_adr;
104
wire [31:0] wb_dat_m;
105
wire [31:0] wb_dat_s;
106
wire wb_cyc;
107
wire wb_stb;
108
wire [3:0] wb_sel;
109
wire wb_we;
110
wire wb_ack;
111
wire wb_err;
112
reg wb_clk_i;  // the wishbone clock
113
reg wb_rst_i;
114
`endif
115
 
116
`ifdef DBG_CPU0_SUPPORTED
117
wire cpu0_clk;
118
wire [31:0]cpu0_addr;
119
wire [31:0] cpu0_data_c;
120
wire [31:0] cpu0_data_d;
121
wire cpu0_bp;
122
wire cpu0_stall;
123
wire cpu0_stb;
124
wire cpu0_we;
125
wire cpu0_ack;
126
wire cpu0_rst;
127
`endif
128
 
129
`ifdef DBG_CPU1_SUPPORTED
130
reg cpu1_clk;
131
wire [31:0]cpu1_addr;
132
wire [31:0] cpu1_data_c;
133
wire [31:0] cpu1_data_d;
134
wire cpu1_bp;
135
wire cpu1_stall;
136
wire cpu1_stb;
137
wire cpu1_we;
138
wire cpu1_ack;
139
wire cpu1_rst;
140
`endif //  `ifdef DBG_CPU1_SUPPORTED
141
 
142
reg test_enabled;
143
 
144
// Data which will be written to the WB interface
145
reg [31:0] static_data32 [0:15];
146
reg [15:0] static_data16 [0:15];
147
reg [7:0] static_data8 [0:15];
148
 
149
// Arrays to hold data read back from the WB interface, for comparison
150
reg [31:0] input_data32 [0:15];
151
reg [15:0] input_data16 [0:15];
152
reg [7:0]  input_data8 [0:15];
153
 
154
reg [32:0] err_data;  // holds the contents of the error register from the various modules
155
 
156
reg failed;
157
integer i;
158
 
159
initial
160
begin
161
   jtag_tck_o = 1'b0;
162
   jtag_tms_o = 1'b0;
163
   jtag_tdo_o = 1'b0;
164
end
165
 
166
// Provide the wishbone clock
167
`ifdef DBG_WISHBONE_SUPPORTED
168
initial
169
begin
170
  wb_clk_i = 1'b0;
171
  forever #7 wb_clk_i = ~wb_clk_i;  // Odd frequency ratio to test the synchronization
172
end
173
`endif
174
 
175
// Provide the CPU0 clock
176
//`ifdef DBG_CPU0_SUPPORTED
177
//initial
178
//begin
179
  //cpu0_clk = 1'b0;
180
  //forever #6 cpu0_clk = ~cpu0_clk;  // Odd frequency ratio to test the synchronization
181
//end
182
//`endif
183
 
184
// Start the test (and reset the wishbone)
185
initial
186
begin
187
  test_enabled = 1'b0;
188
  wb_rst_i = 1'b0;
189
  #100;
190
  wb_rst_i = 1'b1;
191
  #100;
192
  wb_rst_i = 1'b0;
193
 
194
   // Init the memory
195
  initialize_memory(32'h0,32'h16);
196
 
197
  // Init the WB model
198
  i_wb.cycle_response(`ACK_RESPONSE, 2, 0);  // response type, wait cycles, retry_cycles
199
 
200
  #1 test_enabled<=#1 1'b1;
201
end
202
 
203
// This is the main test procedure
204
always @ (posedge test_enabled)
205
begin
206
 
207
  $display("Starting advanced debug test");
208
 
209
  reset_jtag;
210
  #1000;
211
  check_idcode;
212
  #1000;
213
 
214
  // Select the debug module in the IR
215
  set_ir(`DEBUG);
216
  #1000;
217
 
218
 
219
  ///////////////////////////////////////////////////////////////////
220
  // Test CPU0 unit
221
  ////////////////////////////////////////////////////////////////////
222
`ifdef DBG_CPU0_SUPPORTED
223
  // Select the CPU0 unit in the debug module
224
  #1000;
225
  $display("Selecting CPU0 module at time %t", $time);
226
  select_debug_module(`DBG_TOP_CPU0_DEBUG_MODULE);
227
 
228
   // Test reset, stall bits
229
   #1000;
230
   $display("Testing CPU0 intreg select at time %t", $time);
231
   select_module_internal_register(32'h1, 1);  // Really just a read, with discarded data
232
   #1000;
233
   select_module_internal_register(32'h0, 1);  // Really just a read, with discarded data
234
   #1000;
235
 
236
   // Read the stall and reset bits
237
   $display("Testing reset and stall bits at time %t", $time);
238
   read_module_internal_register(8'd2, err_data);  // We assume the register is already selected
239
   $display("Reset and stall bits are %x", err_data);
240
   #1000;
241
 
242
   //  Set rst/stall bits
243
   $display("Setting reset and stall bits at time %t", $time);
244
    write_module_internal_register(32'h0, 8'h1, 32'h3, 8'h2);  // idx, idxlen, data, datalen
245
   #1000;
246
 
247
   // Read the bits again
248
   $display("Testing reset and stall bits again at time %t", $time);
249
   read_module_internal_register(8'd2, err_data);  // We assume the register is already selected
250
   $display("Reset and stall bits are %x", err_data);
251
   #1000;
252
 
253
   // Clear the bits
254
      $display("Clearing reset and stall bits at time %t", $time);
255
    write_module_internal_register(32'h0, 8'h1, 32'h0, 8'h2);  // idx, idxlen, data, datalen
256
   #1000;
257
 
258
      // Read the bits again
259
   $display("Testing reset and stall bits again at time %t", $time);
260
   read_module_internal_register(8'd2, err_data);  // We assume the register is already selected
261
   $display("Reset and stall bits are %x", err_data);
262
   #1000;
263
 
264
   // Behavioral CPU model must be stalled in order to do SPR access
265
   //$display("Setting reset and stall bits at time %t", $time);    
266
   write_module_internal_register(32'h0, 8'h1, 32'h1, 8'h2);  // idx, idxlen, data, datalen
267
   #1000;
268
 
269
   // Test SPR bus access
270
  $display("Testing CPU0 32-bit burst write at time %t", $time);
271
  do_module_burst_write(3'h4, 16'd16, 32'h10);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
272
  #1000;
273
  $display("Testing CPU0 32-bit burst read at time %t", $time);
274
  do_module_burst_read(3'h4, 16'd16, 32'h0);
275
  #1000;
276
 
277
`endif
278
 
279
 
280
  ///////////////////////////////////////////////////////////////////
281
  // Test the Wishbone unit
282
  ////////////////////////////////////////////////////////////////////
283
 
284
`ifdef DBG_WISHBONE_SUPPORTED
285
  // Select the WB unit in the debug module
286
  #1000;
287
  $display("Selecting Wishbone module at time %t", $time);
288
  select_debug_module(`DBG_TOP_WISHBONE_DEBUG_MODULE);
289
 
290
  /*
291
  // Test error conditions
292
  #1000;
293
  $display("Testing error (size 0 WB burst write) at time %t", $time);
294
  do_module_burst_write(3'h1, 16'h0, 32'h0);  // 0-word write = error, ignored
295
  #1000;
296
  $display("Testing error (size 0 WB burst read) at time %t", $time);
297
  do_module_burst_read(3'h1, 16'h0, 32'h0);  // 0-word read = error, ignored
298
 
299
  // Test NOP (a zero in the MSB, then a NOP opcode)
300
  #1000;
301
  $display("Testing NOP at time %t", $time);
302
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
303
    write_bit(3'h0);           // capture_ir
304
    write_bit(3'h0);           // shift_ir
305
    jtag_write_stream(5'h0, 8'h5, 1);  // write data, exit_1
306
    write_bit(`JTAG_TMS_bit);  // update_dr
307
    write_bit(3'h0);           // idle
308
    #1000;
309
    */
310
 
311
    /*
312
    #1000;
313
     $display("Testing WB intreg select at time %t", $time);
314
    select_module_internal_register(32'h1, 1);  // Really just a read, with discarded data
315
    #1000;
316
     select_module_internal_register(32'h0, 1);  // Really just a read, with discarded data
317
   #1000;
318
 
319
   // Reset the error bit
320
    write_module_internal_register(32'h0, 8'h1, 32'h1, 8'h1);  // idx, idxlen, data, datalen
321
   #1000;
322
 
323
   // Read the error bit
324
   read_module_internal_register(8'd33, err_data);  // We assume the register is already selected
325
   #1000;
326
*/
327
 
328
  /////////////////////////////////
329
  // Test 8-bit WB access
330
  failed = 0;
331
  $display("Testing WB 8-bit burst write at time %t: resetting ", $time);
332
  do_module_burst_write(3'h1, 16'd16, 32'h0);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
333
  #1000;
334
  $display("Testing WB 8-bit burst read at time %t", $time);
335
  do_module_burst_read(3'h1, 16'd16, 32'h0);
336
  #1000;
337
   for(i = 0; i < 16; i = i+1) begin
338
     if(static_data8[i] != input_data8[i]) begin
339
        failed = 1;
340
        $display("32-bit data mismatch at index %d, wrote 0x%x, read 0x%x", i, static_data8[i], input_data8[i]);
341
    end
342
  end
343
  if(!failed) $display("8-bit read/write OK!");
344
 
345
   /* try it unaligned
346
  do_module_burst_write(3'h1, 16'd5, 32'h3);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
347
    #1000;
348
  do_module_burst_read(3'h1, 16'd4, 32'h4);
349
    #1000;
350
  */
351
 
352
  /////////////////////////////////
353
  // Test 16-bit WB access
354
  failed = 0;
355
  $display("Testing WB 16-bit burst write at time %t", $time);
356
  do_module_burst_write(3'h2, 16'd16, 32'h0);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
357
  #1000;
358
  $display("Testing WB 16-bit burst read at time %t", $time);
359
  do_module_burst_read(3'h2, 16'd16, 32'h0);
360
  #1000;
361
   for(i = 0; i < 16; i = i+1) begin
362
     if(static_data16[i] != input_data16[i]) begin
363
        failed = 1;
364
        $display("16-bit data mismatch at index %d, wrote 0x%x, read 0x%x", i, static_data16[i], input_data16[i]);
365
    end
366
  end
367
  if(!failed) $display("16-bit read/write OK!");
368
 
369
   /* try it unaligned
370
  do_module_burst_write(3'h2, 16'd5, 32'h2);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
371
    #1000;
372
  do_module_burst_read(3'h2, 16'd4, 32'h4);
373
    #1000;
374
  */
375
 
376
  ////////////////////////////////////
377
  // Test 32-bit WB access
378
  failed = 0;
379
  $display("Testing WB 32-bit burst write at time %t", $time);
380
  do_module_burst_write(3'h4, 16'd16, 32'h0);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
381
  #1000;
382
  $display("Testing WB 32-bit burst read at time %t", $time);
383
  do_module_burst_read(3'h4, 16'd16, 32'h0);
384
  #1000;
385
   for(i = 0; i < 16; i = i+1) begin
386
     if(static_data32[i] != input_data32[i]) begin
387
        failed = 1;
388
        $display("32-bit data mismatch at index %d, wrote 0x%x, read 0x%x", i, static_data32[i], input_data32[i]);
389
    end
390
  end
391
  if(!failed) $display("32-bit read/write OK!");
392
 
393
  /* Try another address
394
  do_module_burst_write(3'h4, 16'd16, 32'h200);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
395
    #1000;
396
  do_module_burst_read(3'h4, 16'd15, 32'h204);
397
    #1000;
398
  */
399
 
400
  ////////////////////////////////
401
  // Test error register
402
  err_data = 33'h0;
403
  // Select and reset the error register
404
  write_module_internal_register(`DBG_WB_INTREG_ERROR, `DBG_WB_REGSELECT_SIZE, 64'h1, 8'h1); // regidx,idxlen,writedata, datalen;
405
  i_wb.cycle_response(`ERR_RESPONSE, 2, 0);  // response type, wait cycles, retry_cycles
406
  do_module_burst_write(3'h4, 16'd4, 32'hdeaddead);  // 3-bit word size (bytes), 16-bit word count, 32-bit start address
407
  read_module_internal_register(8'd33, err_data);  // get the error register
408
  $display("Error bit is %d, error address is %x", err_data[0], err_data>>1);
409
 
410
`endif  // WB module supported
411
 
412
end
413
 
414
task initialize_memory;
415
  input [31:0] start_addr;
416
  input [31:0] length;
417
  integer i;
418
  reg [31:0] addr;
419
  begin
420
 
421
    for (i=0; i<length; i=i+1)
422
      begin
423
        static_data32[i] <= {i[7:0], i[7:0]+2'd1, i[7:0]+2'd2, i[7:0]+2'd3};
424
        static_data16[i] <= {i[7:0], i[7:0]+ 2'd1};
425
        static_data8[i] <= i[7:0];
426
      end
427
  end
428
endtask
429
 
430
///////////////////////////////////////////////////////////////////////////////
431
// Declaration and interconnection of components
432
 
433
// Top module
434
tap_top  i_tap (
435
                // JTAG pads
436
                .tms_pad_i(jtag_tms_o),
437
                .tck_pad_i(jtag_tck_o),
438
                .trstn_pad_i(1'b1),
439
                .tdi_pad_i(jtag_tdo_o),
440
                .tdo_pad_o(jtag_tdi_i),
441
                .tdo_padoe_o(),
442
 
443
                // TAP states
444
                                    .test_logic_reset_o(dbg_rst),
445
                                    .run_test_idle_o(),
446
                .shift_dr_o(shift_dr),
447
                .pause_dr_o(pause_dr),
448
                .update_dr_o(update_dr),
449
                .capture_dr_o(capture_dr),
450
 
451
                // Select signals for boundary scan or mbist
452
                .extest_select_o(),
453
                .sample_preload_select_o(),
454
                .mbist_select_o(),
455
                .debug_select_o(dbg_sel),
456
 
457
                // TDO signal that is connected to TDI of sub-modules.
458
                .tdi_o(dbg_tdo),
459
 
460
                // TDI signals from sub-modules
461
                .debug_tdo_i(dbg_tdi),    // from debug module
462
                .bs_chain_tdo_i(1'b0), // from Boundary Scan Chain
463
                .mbist_tdo_i(1'b0)     // from Mbist Chain
464
              );
465
 
466
 
467
// Top module
468
adbg_top i_dbg_module(
469
                // JTAG signals
470
                .tck_i(jtag_tck_o),
471
                .tdi_i(dbg_tdo),
472
                .tdo_o(dbg_tdi),
473
                .rst_i(dbg_rst),
474
 
475
                // TAP states
476
                .shift_dr_i(shift_dr),
477
                .pause_dr_i(pause_dr),
478
                .update_dr_i(update_dr),
479
                .capture_dr_i(capture_dr),
480
 
481
                // Instructions
482
                .debug_select_i(dbg_sel)
483
 
484
 
485
                `ifdef DBG_WISHBONE_SUPPORTED
486
                // WISHBONE common signals
487
                ,
488
                .wb_clk_i(wb_clk_i),
489
 
490
                // WISHBONE master interface
491
                .wb_adr_o(wb_adr),
492
                .wb_dat_o(wb_dat_m),
493
                .wb_dat_i(wb_dat_s),
494
                .wb_cyc_o(wb_cyc),
495
                .wb_stb_o(wb_stb),
496
                .wb_sel_o(wb_sel),
497
                .wb_we_o(wb_we),
498
                .wb_ack_i(wb_ack),
499
                .wb_cab_o(),
500
                .wb_err_i(wb_err),
501
                .wb_cti_o(),
502
                .wb_bte_o()
503
                `endif
504
 
505
                `ifdef DBG_CPU0_SUPPORTED
506
                // CPU signals
507
                ,
508
                .cpu0_clk_i(cpu0_clk),
509
                .cpu0_addr_o(cpu0_addr),
510
                .cpu0_data_i(cpu0_data_c),
511
                .cpu0_data_o(cpu0_data_d),
512
                .cpu0_bp_i(cpu0_bp),
513
                .cpu0_stall_o(cpu0_stall),
514
                .cpu0_stb_o(cpu0_stb),
515
                .cpu0_we_o(cpu0_we),
516
                .cpu0_ack_i(cpu0_ack),
517
                .cpu0_rst_o(cpu0_rst)
518
                `endif
519
 
520
                `ifdef DBG_CPU1_SUPPORTED
521
                // CPU signals
522
                ,
523
                .cpu1_clk_i(cpu1_clk),
524
                .cpu1_addr_o(cpu1_addr),
525
                .cpu1_data_i(cpu1_data_c),
526
                .cpu1_data_o(cpu1_data_d),
527
                .cpu1_bp_i(cpu1_bp),
528
                .cpu1_stall_o(cpu1_stall),
529
                .cpu1_stb_o(cpu1_stb),
530
                .cpu1_we_o(cpu1_we),
531
                .cpu1_ack_i(cpu1_ack),
532
                .cpu1_rst_o(cpu1_rst)
533
                `endif
534
 
535
              );
536
 
537
 
538
`ifdef DBG_WISHBONE_SUPPORTED
539
// The 'wishbone' may be just a p2p connection to a simple RAM
540
/*
541
onchip_ram_top i_ocram (
542
   .wb_clk_i(wb_clk_i),
543
   .wb_rst_i(wb_rst_i),
544
   .wb_dat_i(wb_dat_m),
545
   .wb_dat_o(wb_dat_s),
546
   .wb_adr_i(wb_adr[11:0]),
547
   .wb_sel_i(wb_sel),
548
   .wb_we_i(wb_we),
549
   .wb_cyc_i(wb_cyc),
550
   .wb_stb_i(wb_stb),
551
   .wb_ack_o(wb_ack),
552
   .wb_err_o(wb_err)
553
);
554
*/
555
 
556
wb_slave_behavioral i_wb
557
(
558
        .CLK_I(wb_clk_i),
559
        .RST_I(wb_rst_i),
560
        .ACK_O(wb_ack),
561
        .ADR_I(wb_adr),
562
        .CYC_I(wb_cyc),
563
        .DAT_O(wb_dat_s),
564
        .DAT_I(wb_dat_m),
565
        .ERR_O(wb_err),
566
        .RTY_O(),
567
        .SEL_I(wb_sel),
568
        .STB_I(wb_stb),
569
        .WE_I(wb_we),
570
        .CAB_I(1'b0)
571
);
572
`endif
573
 
574
 
575
`ifdef DBG_CPU0_SUPPORTED
576
// Instantiate a behavioral model of the CPU SPR bus
577
cpu_behavioral cpu0_i  (
578
   .cpu_rst_i(cpu0_rst),
579
   .cpu_clk_o(cpu0_clk),
580
   .cpu_addr_i(cpu0_addr),
581
   .cpu_data_o(cpu0_data_c),
582
   .cpu_data_i(cpu0_data_d),
583
   .cpu_bp_o(cpu0_bp),
584
   .cpu_stall_i(cpu0_stall),
585
   .cpu_stb_i(cpu0_stb),
586
   .cpu_we_i(cpu0_we),
587
   .cpu_ack_o(cpu0_ack),
588
   .cpu_rst_o(cpu0_rst)
589
);
590
 
591
`endif
592
 
593
 
594
`ifdef DBG_CPU1_SUPPORTED
595
// Instantiate a behavioral model of the CPU SPR bus
596
cpu_behavioral cpu1_i  (
597
                    .cpu_rst_i(cpu1_rst),
598
                    .cpu_clk_o(cpu1_clk),
599
                    .cpu_addr_i(cpu1_addr),
600
                    .cpu_data_o(cpu1_data_c),
601
                    .cpu_data_i(cpu1_data_d),
602
                    .cpu_bp_o(cpu1_bp),
603
                    .cpu_stall_i(cpu1_stall),
604
                    .cpu_stb_i(cpu1_stb),
605
                    .cpu_we_i(cpu1_we),
606
                    .cpu_ack_o(cpu1_ack),
607
                    .cpu_rst_o(cpu1_rst)
608
);
609
`endif
610
 
611
///////////////////////////////////////////////////////////////////////////
612
// Higher-level chain manipulation functions
613
 
614
// calculate the CRC, up to 32 bits at a time
615
task compute_crc;
616
    input [31:0] crc_in;
617
    input [31:0] data_in;
618
    input [5:0] length_bits;
619
    output [31:0] crc_out;
620
    integer i;
621
    reg [31:0] d;
622
    reg [31:0] c;
623
    begin
624
        crc_out = crc_in;
625
        for(i = 0; i < length_bits; i = i+1) begin
626
           d = (data_in[i]) ? 32'hffffffff : 32'h0;
627
           c = (crc_out[0]) ? 32'hffffffff : 32'h0;
628
           //crc_out = {crc_out[30:0], 1'b0};  // original
629
           crc_out = crc_out >> 1;
630
           crc_out = crc_out ^ ((d ^ c) & `DBG_CRC_POLY);
631
           //$display("CRC Itr %d, inbit = %d, crc = 0x%x", i, data_in[i], crc_out);
632
        end
633
    end
634
endtask
635
 
636
task check_idcode;
637
reg [63:0] readdata;
638
reg[31:0] idcode;
639
begin
640
    set_ir(`IDCODE);
641
 
642
    // Read the IDCODE in the DR
643
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
644
    write_bit(3'h0);           // capture_ir
645
    write_bit(3'h0);           // shift_ir
646
    jtag_read_write_stream(64'h0, 8'd32, 1, readdata);  // write data, exit_1
647
    write_bit(`JTAG_TMS_bit);  // update_ir
648
    write_bit(3'h0);           // idle
649
    idcode = readdata[31:0];
650
    $display("Got TAP IDCODE 0x%x, expected 0x%x", idcode, `IDCODE_VALUE);
651
end
652
endtask;
653
 
654
task select_debug_module;
655
input [1:0] moduleid;
656
reg validid;
657
begin
658
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
659
    write_bit(3'h0);           // capture_ir
660
    write_bit(3'h0);           // shift_ir
661
    jtag_write_stream({1'b1,moduleid}, 8'h3, 1);  // write data, exit_1
662
    write_bit(`JTAG_TMS_bit);  // update_dr
663
    write_bit(3'h0);           // idle
664
 
665
    $display("Selecting module (%0x)", moduleid);
666
 
667
    // Read back the status to make sure a valid chain is selected
668
    /* Pointless, the newly selected module would respond instead...
669
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
670
    write_bit(3'h0);           // capture_ir
671
    write_bit(3'h0);           // shift_ir
672
    read_write_bit(`JTAG_TMS_bit, validid);  // get data, exit_1
673
    write_bit(`JTAG_TMS_bit);  // update_dr
674
    write_bit(3'h0);           // idle
675
 
676
    if(validid)   $display("Selected valid module (%0x)", moduleid);
677
    else          $display("Failed to select module (%0x)", moduleid);
678
    */
679
end
680
endtask
681
 
682
 
683
task send_module_burst_command;
684
input [3:0] opcode;
685
input [31:0] address;
686
input [15:0] burstlength;
687
reg [63:0] streamdata;
688
begin
689
    streamdata = {11'h0,1'b0,opcode,address,burstlength};
690
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
691
    write_bit(3'h0);           // capture_ir
692
    write_bit(3'h0);           // shift_ir
693
    jtag_write_stream(streamdata, 8'd53, 1);  // write data, exit_1
694
    write_bit(`JTAG_TMS_bit);  // update_dr
695
    write_bit(3'h0);           // idle
696
end
697
endtask
698
 
699
task select_module_internal_register;  // Really just a read, with discarded data
700
    input [31:0] regidx;
701
    input [7:0] len;  // the length of the register index data, we assume not more than 32
702
    reg[63:0] streamdata;
703
begin
704
    streamdata = 64'h0;
705
    streamdata = streamdata | regidx;
706
    streamdata = streamdata | (`DBG_WB_CMD_IREG_SEL << len);
707
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
708
    write_bit(3'h0);           // capture_ir
709
    write_bit(3'h0);           // shift_ir
710
    jtag_write_stream(streamdata, (len+5), 1);  // write data, exit_1
711
    write_bit(`JTAG_TMS_bit);  // update_dr
712
    write_bit(3'h0);           // idle
713
end
714
endtask
715
 
716
 
717
task read_module_internal_register;  // We assume the register is already selected
718
    //input [31:0] regidx;
719
    input [7:0] len;  // the length of the data desired, we assume a max of 64 bits
720
    output [63:0] instream;
721
    reg [63:0] bitmask;
722
begin
723
    instream = 64'h0;
724
    // We shift out all 0's, which is a NOP to the debug unit
725
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
726
    write_bit(3'h0);           // capture_ir
727
    write_bit(3'h0);           // shift_ir
728
    // Shift at least 5 bits, as this is the min, for a valid NOP
729
    jtag_read_write_stream(64'h0, len+4,1,instream);  // exit_1
730
    write_bit(`JTAG_TMS_bit);  // update_dr
731
    write_bit(3'h0);           // idle
732
    bitmask = 64'hffffffffffffffff;
733
    bitmask = bitmask << len;
734
    bitmask = ~bitmask;
735
    instream = instream & bitmask;  // Cut off any unwanted excess bits
736
end
737
endtask
738
 
739
task write_module_internal_register;
740
    input [31:0] regidx; // the length of the register index data
741
    input [7:0] idxlen;
742
    input [63:0] writedata;
743
    input [7:0] datalen;  // the length of the data to write.  We assume the two length combined are 59 or less.
744
    reg[63:0] streamdata;
745
begin
746
    streamdata = 64'h0;  // This will 0 the toplevel/module select bit
747
    streamdata = streamdata | writedata;
748
    streamdata = streamdata | (regidx << datalen);
749
    streamdata = streamdata | (`DBG_WB_CMD_IREG_WR << (idxlen+datalen));
750
 
751
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
752
    write_bit(3'h0);           // capture_ir
753
    write_bit(3'h0);           // shift_ir
754
    jtag_write_stream(streamdata, (idxlen+datalen+5), 1);  // write data, exit_1
755
    write_bit(`JTAG_TMS_bit);  // update_dr
756
    write_bit(3'h0);           // idle
757
end
758
endtask
759
 
760
// This includes the sending of the burst command
761
task do_module_burst_read;
762
input [5:0] word_size_bytes;
763
input [15:0] word_count;
764
input [31:0] start_address;
765
reg [3:0] opcode;
766
reg status;
767
reg [63:0] instream;
768
integer i;
769
integer j;
770
reg [31:0] crc_calc_i;
771
reg [31:0] crc_calc_o;  // temp signal...
772
reg [31:0] crc_read;
773
reg [5:0] word_size_bits;
774
begin
775
    $display("Doing burst read, word size %d, word count %d, start address 0x%x", word_size_bytes, word_count, start_address);
776
    instream = 64'h0;
777
    word_size_bits = word_size_bytes << 3;
778
    crc_calc_i = 32'hffffffff;
779
 
780
    // Send the command
781
    case (word_size_bytes)
782
       3'h1: opcode = `DBG_WB_CMD_BREAD8;
783
       3'h2: opcode = `DBG_WB_CMD_BREAD16;
784
       3'h4: opcode = `DBG_WB_CMD_BREAD32;
785
       default:
786
          begin
787
           $display("Tried burst read with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
788
           opcode = `DBG_WB_CMD_BREAD32;
789
          end
790
   endcase
791
 
792
   send_module_burst_command(opcode,start_address, word_count);  // returns to state idle
793
 
794
   // Get us back to shift_dr mode to read a burst
795
   write_bit(`JTAG_TMS_bit);  // select_dr_scan
796
   write_bit(3'h0);           // capture_ir
797
   write_bit(3'h0);           // shift_ir
798
 
799
`ifdef ADBG_USE_HISPEED
800
      // Get 1 status bit, then word_size_bytes*8 bits
801
      status = 1'b0;
802
      j = 0;
803
      while(!status) begin
804
         read_write_bit(3'h0, status);
805
         j = j + 1;
806
      end
807
 
808
      if(j > 1) begin
809
         $display("Took %0d tries before good status bit during burst read", j);
810
      end
811
`endif
812
 
813
   // Now, repeat...
814
   for(i = 0; i < word_count; i=i+1) begin
815
 
816
`ifndef ADBG_USE_HISPEED
817
      // Get 1 status bit, then word_size_bytes*8 bits
818
      status = 1'b0;
819
      j = 0;
820
      while(!status) begin
821
         read_write_bit(3'h0, status);
822
         j = j + 1;
823
      end
824
 
825
      if(j > 1) begin
826
         $display("Took %0d tries before good status bit during burst read", j);
827
      end
828
`endif
829
 
830
     jtag_read_write_stream(64'h0, {2'h0,(word_size_bytes<<3)},0,instream);
831
     //$display("Read 0x%0x", instream[31:0]);
832
     compute_crc(crc_calc_i, instream[31:0], word_size_bits, crc_calc_o);
833
     crc_calc_i = crc_calc_o;
834
     if(word_size_bytes == 1) input_data8[i] = instream[7:0];
835
     else if(word_size_bytes == 2) input_data16[i] = instream[15:0];
836
     else input_data32[i] = instream[31:0];
837
   end
838
 
839
   // Read the data CRC from the debug module.
840
   jtag_read_write_stream(64'h0, 6'd32, 1, crc_read);
841
   if(crc_calc_o != crc_read) $display("CRC ERROR! Computed 0x%x, read CRC 0x%x", crc_calc_o, crc_read);
842
   else $display("CRC OK!");
843
 
844
   // Finally, shift out 5 0's, to make the next command a NOP
845
   // Not necessary, debug unit won't latch a new opcode at the end of a burst
846
   //jtag_write_stream(64'h0, 8'h5, 1);
847
   write_bit(`JTAG_TMS_bit);  // update_ir
848
   write_bit(3'h0);           // idle
849
end
850
endtask
851
 
852
 
853
task do_module_burst_write;
854
input [5:0] word_size_bytes;
855
input [15:0] word_count;
856
input [31:0] start_address;
857
reg [3:0] opcode;
858
reg status;
859
reg [63:0] dataword;
860
integer i;
861
integer j;
862
reg [31:0] crc_calc_i;
863
reg [31:0] crc_calc_o;
864
reg crc_match;
865
reg [5:0] word_size_bits;
866
begin
867
    $display("Doing burst write, word size %d, word count %d, start address 0x%x", word_size_bytes, word_count, start_address);
868
    word_size_bits = word_size_bytes << 3;
869
    crc_calc_i = 32'hffffffff;
870
 
871
    // Send the command
872
    case (word_size_bytes)
873
       3'h1: opcode = `DBG_WB_CMD_BWRITE8;
874
       3'h2: opcode = `DBG_WB_CMD_BWRITE16;
875
       3'h4: opcode = `DBG_WB_CMD_BWRITE32;
876
       default:
877
          begin
878
           $display("Tried burst write with invalid word size (%0x), defaulting to 4-byte words", word_size_bytes);
879
           opcode = `DBG_WB_CMD_BWRITE32;
880
          end
881
   endcase
882
 
883
   send_module_burst_command(opcode, start_address, word_count);  // returns to state idle
884
 
885
   // Get us back to shift_dr mode to write a burst
886
   write_bit(`JTAG_TMS_bit);  // select_dr_scan
887
   write_bit(3'h0);           // capture_ir
888
   write_bit(3'h0);           // shift_ir
889
 
890
 
891
   // Write a start bit (a 1) so it knows when to start counting
892
   write_bit(`JTAG_TDO_bit);
893
 
894
   // Now, repeat...
895
   for(i = 0; i < word_count; i=i+1) begin
896
      // Write word_size_bytes*8 bits, then get 1 status bit
897
      if(word_size_bytes == 4)      dataword = {32'h0, static_data32[i]};
898
      else if(word_size_bytes == 2) dataword = {48'h0, static_data16[i]};
899
      else                          dataword = {56'h0, static_data8[i]};
900
 
901
 
902
      jtag_write_stream(dataword, {2'h0,(word_size_bytes<<3)},0);
903
      compute_crc(crc_calc_i, dataword[31:0], word_size_bits, crc_calc_o);
904
      crc_calc_i = crc_calc_o;
905
 
906
 
907
`ifndef ADBG_USE_HISPEED
908
      // Check if WB bus is ready
909
      // *** THIS WILL NOT WORK IF THERE IS MORE THAN 1 DEVICE IN THE JTAG CHAIN!!!
910
      status = 1'b0;
911
      read_write_bit(3'h0, status);
912
 
913
      if(!status) begin
914
         $display("Bad status bit during burst write, index %d", i);
915
      end
916
`endif
917
 
918
     //$display("Wrote 0x%0x", dataword);
919
   end
920
 
921
   // Send the CRC we computed
922
   jtag_write_stream(crc_calc_o, 6'd32,0);
923
 
924
   // Read the 'CRC match' bit, and go to exit1_dr
925
   read_write_bit(`JTAG_TMS_bit, crc_match);
926
   if(!crc_match) $display("CRC ERROR! match bit after write is %d (computed CRC 0x%x)", crc_match, crc_calc_o);
927
   else $display("CRC OK!");
928
 
929
   // Finally, shift out 5 0's, to make the next command a NOP
930
   // Not necessary, module will not latch new opcode during burst
931
   //jtag_write_stream(64'h0, 8'h5, 1);
932
   write_bit(`JTAG_TMS_bit);  // update_ir
933
   write_bit(3'h0);           // idle
934
end
935
 
936
endtask
937
 
938
 
939
// Puts a value in the TAP IR, assuming we start in IDLE state.
940
// Returns to IDLE state when finished
941
task set_ir;
942
input [3:0] irval;
943
begin
944
    write_bit(`JTAG_TMS_bit);  // select_dr_scan
945
    write_bit(`JTAG_TMS_bit);  // select_ir_scan
946
    write_bit(3'h0);           // capture_ir
947
    write_bit(3'h0);           // shift_ir
948
    jtag_write_stream({60'h0,irval}, 8'h4, 1);  // write data, exit_1
949
    write_bit(`JTAG_TMS_bit);  // update_ir
950
    write_bit(3'h0);           // idle
951
end
952
endtask
953
 
954
// Resets the TAP and puts it into idle mode
955
task reset_jtag;
956
integer i;
957
begin
958
   for(i = 0; i < 8; i=i+1) begin
959
      write_bit(`JTAG_TMS_bit);  // 5 TMS should put us in test_logic_reset mode
960
   end
961
   write_bit(3'h0);              // idle
962
end
963
endtask
964
 
965
 
966
////////////////////////////////////////////////////////////////////////////
967
// Tasks to write or read-write a string of data
968
 
969
task jtag_write_stream;
970
input [63:0] stream;
971
input [7:0] len;
972
input set_last_bit;
973
integer i;
974
integer databit;
975
reg [2:0] bits;
976
begin
977
    for(i = 0; i < (len-1); i=i+1) begin
978
       databit = (stream >> i) & 1'h1;
979
       bits = databit << `JTAG_TDO;
980
       write_bit(bits);
981
   end
982
 
983
   databit = (stream >> i) & 1'h1;
984
   bits = databit << `JTAG_TDO;
985
   if(set_last_bit) bits = (bits | `JTAG_TMS_bit);
986
   write_bit(bits);
987
 
988
end
989
endtask
990
 
991
 
992
task jtag_read_write_stream;
993
input [63:0] stream;
994
input [7:0] len;
995
input set_last_bit;
996
output [63:0] instream;
997
integer i;
998
integer databit;
999
reg [2:0] bits;
1000
reg inbit;
1001
begin
1002
    instream = 64'h0;
1003
    for(i = 0; i < (len-1); i=i+1) begin
1004
       databit = (stream >> i) & 1'h1;
1005
       bits = databit << `JTAG_TDO;
1006
       read_write_bit(bits, inbit);
1007
       instream = (instream | (inbit << i));
1008
   end
1009
 
1010
   databit = (stream >> i) & 1'h1;
1011
   bits = databit << `JTAG_TDO;
1012
   if(set_last_bit) bits = (bits | `JTAG_TMS_bit);
1013
   read_write_bit(bits, inbit);
1014
   instream = (instream | (inbit << (len-1)));
1015
end
1016
endtask
1017
 
1018
/////////////////////////////////////////////////////////////////////////
1019
// Tasks which write or readwrite a single bit (including clocking)
1020
 
1021
task write_bit;
1022
   input [2:0] bitvals;
1023
   begin
1024
 
1025
   // Set data
1026
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
1027
   `wait_jtag_period;
1028
 
1029
   // Raise clock
1030
   jtag_out(bitvals | `JTAG_TCK_bit);
1031
   `wait_jtag_period;
1032
 
1033
   // drop clock (making output available in the SHIFT_xR states)
1034
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
1035
   `wait_jtag_period;
1036
   end
1037
endtask
1038
 
1039
task read_write_bit;
1040
   input [2:0] bitvals;
1041
   output l_tdi_val;
1042
   begin
1043
 
1044
   // read bit state
1045
   l_tdi_val <= jtag_tdi_i;
1046
 
1047
   // Set data
1048
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
1049
   `wait_jtag_period;
1050
 
1051
   // Raise clock
1052
   jtag_out(bitvals | `JTAG_TCK_bit);
1053
   `wait_jtag_period;
1054
 
1055
   // drop clock (making output available in the SHIFT_xR states)
1056
   jtag_out(bitvals & ~(`JTAG_TCK_bit));
1057
   `wait_jtag_period;
1058
   end
1059
endtask
1060
 
1061
/////////////////////////////////////////////////////////////////
1062
// Basic functions to set the state of the JTAG TAP I/F bits
1063
 
1064
task jtag_out;
1065
  input   [2:0]   bitvals;
1066
  begin
1067
 
1068
   jtag_tck_o <= bitvals[`JTAG_TCK];
1069
   jtag_tms_o <= bitvals[`JTAG_TMS];
1070
   jtag_tdo_o <= bitvals[`JTAG_TDO];
1071
   end
1072
endtask
1073
 
1074
 
1075
task jtag_inout;
1076
  input   [2:0]   bitvals;
1077
  output l_tdi_val;
1078
  begin
1079
 
1080
   jtag_tck_o <= bitvals[`JTAG_TCK];
1081
   jtag_tms_o <= bitvals[`JTAG_TMS];
1082
   jtag_tdo_o <= bitvals[`JTAG_TDO];
1083
 
1084
   l_tdi_val <= jtag_tdi_i;
1085
   end
1086
endtask
1087
 
1088
endmodule

powered by: WebSVN 2.1.0

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