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

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [orpsocv2/] [bench/] [verilog/] [vpi/] [verilog/] [vpi_debug_module.v] - Blame information for rev 46

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

Line No. Rev Author Line
1 40 julius
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
////  ORPSoC Testbench                                            ////
4
////                                                              ////
5
////  Description                                                 ////
6
////  ORPSoC VPI Debugging Testbench file                         ////
7
////                                                              ////
8
////  To Do:                                                      ////
9
////                                                              ////
10
////                                                              ////
11
////  Author(s):                                                  ////
12
////      - jb, jb@orsoc.se                                       ////
13
////                                                              ////
14
////                                                              ////
15
//////////////////////////////////////////////////////////////////////
16
////                                                              ////
17
//// Copyright (C) 2009 Authors and OPENCORES.ORG                 ////
18
////                                                              ////
19
//// This source file may be used and distributed without         ////
20
//// restriction provided that this copyright statement is not    ////
21
//// removed from the file and that any derivative work contains  ////
22
//// the original copyright notice and the associated disclaimer. ////
23
////                                                              ////
24
//// This source file is free software; you can redistribute it   ////
25
//// and/or modify it under the terms of the GNU Lesser General   ////
26
//// Public License as published by the Free Software Foundation; ////
27
//// either version 2.1 of the License, or (at your option) any   ////
28
//// later version.                                               ////
29
////                                                              ////
30
//// This source is distributed in the hope that it will be       ////
31
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
32
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
33
//// PURPOSE.  See the GNU Lesser General Public License for more ////
34
//// details.                                                     ////
35
////                                                              ////
36
//// You should have received a copy of the GNU Lesser General    ////
37
//// Public License along with this source; if not, download it   ////
38
//// from http://www.opencores.org/lgpl.shtml                     ////
39
////                                                              ////
40
//////////////////////////////////////////////////////////////////////
41
`timescale 1ns/10ps
42
 
43
`include "vpi_debug_defines.v"
44
 
45
// uncomment the following line to get more debug output for this module 
46
//`define DEBUG_INFO
47
//`define VPI_DEBUG_INFO
48
 
49
module vpi_debug_module(tms, tck, tdi, tdo);
50
 
51
   output               tms;
52
   output               tck;
53
   output               tdi;
54
   input                tdo;
55
 
56
   reg                  tms;
57
   reg                  tck;
58
   reg                  tdi;
59
 
60
   reg [31:0]            in_data_le, in_data_be;
61
   reg                  err;
62
   integer              i;
63
 
64
   reg [31:0]            id;
65
   reg [31:0]            npc;
66
   reg [31:0]            ppc;
67
   reg [31:0]            r1;
68
   reg [31:0]            insn;
69
   reg [31:0]            result;
70
   reg [31:0]            tmp;
71
 
72
   reg [31:0]            crc_out;
73
   reg [31:0]            crc_in;
74
   wire                 crc_match_in;
75
 
76
   reg [`DBG_TOP_STATUS_LEN -1:0] status;
77
   reg [`DBG_WB_STATUS_LEN -1:0]  status_wb;
78
   reg [`DBG_CPU_STATUS_LEN -1:0] status_cpu;
79
 
80
   // Text used for easier debugging
81
   reg [199:0]                     test_text;
82
   reg [`DBG_WB_CMD_LEN -1:0]      last_wb_cmd;
83
   reg [`DBG_CPU_CMD_LEN -1:0]     last_cpu_cmd;
84
   reg [199:0]                     last_wb_cmd_text;
85
   reg [199:0]                     last_cpu_cmd_text;
86
 
87
   reg [31:0]                      data_storage [0:4095];   // Data storage (for write and read operations). 
88
   reg [18:0]                      length_global;
89
 
90
   parameter                      Tp    = 1;
91
//parameter Tck   = 25;   // Clock half period (Clok period = 50 ns => 20 MHz)
92
   parameter                      Tck   = 50;   // Clock half period (Clok period = 100 ns => 10 MHz)
93
 
94
   integer cmd;
95
   integer block_cmd_length;
96
   integer jtag_instn_val;
97
   integer set_chain_val;
98
 
99
   reg [1:0] cpu_ctrl_val; // two important bits for the ctrl reg
100
   reg [31:0] cmd_adr;
101 46 julius
   reg [31:0]  cmd_size;
102 40 julius
   reg [31:0] cmd_data;
103
 
104
 
105
   initial
106
     begin
107
        $display("JTAG debug module with VPI interface enabled\n");
108
        tck    <=#Tp 1'b0;
109
        tdi    <=#Tp 1'bz;
110
        tms    <=#Tp 1'b0;
111
 
112
        // Insert a #delay here because we need to
113
        // wait until the PC isn't pointing to flash anymore
114
        // (this is around 20k ns if the flash_crash boot code
115
        // is being booted from, else much bigger, around 10mil ns)
116
 
117
        #200_000 main;
118
 
119
     end
120
 
121
   task main;
122
      begin
123
 
124
         id     <=#Tp 32'h00;
125
         npc    <=#Tp 32'h00;
126
         ppc    <=#Tp 32'h00;
127
         insn   <=#Tp 32'h00;
128
         result <=#Tp 32'h00;
129
         err    <=#Tp 1'b0;
130
         tmp    <=#Tp 32'h00;
131
 
132
        // execute some cycles
133
         #50000;
134
 
135
         reset_tap;
136
         goto_run_test_idle;
137
 
138
         //$init_rsp_server();
139
 
140
         while (1) begin
141
 
142
           // Check for incoming command
143
 
144
           // wait until a command is sent
145
           // poll with a delay here
146
           cmd = -1;
147
 
148
           while (cmd == -1)
149
             begin
150
                #1000 $check_for_command(cmd);
151
             end
152
 
153
            // now switch on the command
154
            case (cmd)
155
 
156
              `CMD_RESET : // reset
157
                begin
158
 
159
                   // call reset task
160
                   reset_tap;
161
                   // and put TAP into run_test_idle state
162
                   goto_run_test_idle;
163
 
164
               end
165
 
166
              `CMD_JTAG_SET_IR : // set jtag instruction register
167
                begin
168
 
169
                   $get_command_data(jtag_instn_val);
170
 
171
                   set_instruction(jtag_instn_val);
172
 
173
               end
174
              `CMD_SET_DEBUG_CHAIN : // set debug chain
175
               begin
176
 
177
                  $get_command_data(set_chain_val);
178
 
179
                  module_select(set_chain_val, 1'b0);   // {chain, gen_crc_err}
180
 
181
               end
182
             `CMD_CPU_CTRL_WR : // cpu CTRL write
183
               begin
184
 
185
                  $get_command_data(cpu_ctrl_val);
186
 
187
                  debug_cpu_wr_ctrl(cpu_ctrl_val, "");
188
 
189
               end
190
 
191
             `CMD_CPU_CTRL_RD : // cpu CTRL read
192
               begin
193
 
194
                  debug_cpu_rd_ctrl(cpu_ctrl_val);
195
 
196
                  $return_command_data(cpu_ctrl_val);
197
 
198
               end
199
 
200
             `CMD_CPU_WR_REG :
201
               begin
202
 
203
                  $get_command_address(cmd_adr);
204
 
205
                  $get_command_data(cmd_data);
206
 
207
`ifdef VPI_DEBUG_INFO
208
                  $display("CPU reg write. adr: 0x%x (reg group: %d reg#: %d), val: 0x%x",
209
                           cmd_adr,cmd_adr[15:11], cmd_adr[10:0], cmd_data);
210
`endif
211
 
212
                  cpu_write_32(cmd_data, cmd_adr,16'h3);
213
 
214
               end
215
 
216
             `CMD_CPU_RD_REG :
217
               begin
218
 
219
                  $get_command_address(cmd_adr);
220
 
221
                  cpu_read_32(cmd_data, cmd_adr, 16'h3);
222
 
223
`ifdef VPI_DEBUG_INFO
224
                  $display("CPU reg read. adr: 0x%x (reg group: %d reg#: %d), val: 0x%x",
225
                           cmd_adr,cmd_adr[15:11], cmd_adr[10:0], cmd_data);
226
`endif
227
 
228
                  $return_command_data(cmd_data);
229
 
230
               end
231
 
232 46 julius
             `CMD_WB_WR :
233 40 julius
               begin
234
 
235
                  $get_command_address(cmd_adr);
236
 
237 46 julius
                  $get_command_data(cmd_size);
238
 
239 40 julius
                  $get_command_data(cmd_data);
240
 
241 46 julius
                  case (cmd_size)
242
                    4 :
243
                      begin
244
                         wb_write_32(cmd_data, cmd_adr, 16'h3);
245
                      end
246
                    2 :
247
                      begin
248
                         wb_write_16(cmd_data[15:0], cmd_adr, 16'h1);
249
                      end
250
                    1 :
251
                      begin
252
                         wb_write_8(cmd_data[7:0], cmd_adr, 16'h0);
253
                      end
254
                    default:
255
                      begin
256
                         $display("* vpi_debug_module: CMD_WB_WR size incorrect: %d\n", cmd_size);
257
                      end
258
                  endcase // case (cmd_size)
259
 
260 40 julius
               end
261
 
262
             `CMD_WB_RD32 :
263
               begin
264
 
265
                  $get_command_address(cmd_adr);
266
 
267
                  wb_read_32(cmd_data, cmd_adr, 16'h3);
268
 
269
                  $return_command_data(cmd_data);
270
 
271
               end
272
 
273
             `CMD_WB_BLOCK_WR32 :
274
               begin
275
 
276
                  $get_command_address(cmd_adr);
277
 
278
                  $get_command_data(block_cmd_length);
279
 
280
                  $get_command_block_data(block_cmd_length, data_storage);
281
 
282
                  wb_block_write_32(cmd_adr ,block_cmd_length);
283
 
284
               end
285
 
286
              `CMD_WB_BLOCK_RD32 :
287
                begin
288
 
289
                   $get_command_address(cmd_adr);
290
 
291
                   $get_command_data(block_cmd_length);
292
 
293
                   wb_block_read_32(cmd_adr, block_cmd_length);
294
 
295
                   $return_command_block_data(block_cmd_length, data_storage);
296
 
297
                end
298
 
299
              `CMD_READ_JTAG_ID :
300
                begin
301
 
302
                   read_id_code(id);
303
 
304
                   $return_command_data(id);
305
 
306
                end
307
 
308
              `CMD_GDB_DETACH :
309
                begin
310
 
311
                   $display("Debugging client disconnected. Finishing simulation");
312
 
313
 
314
                   $finish();
315
 
316
                end
317
 
318
              default:
319
                begin
320
                   $display("Somehow got to the default case in the command case statement.");
321
                   $display("Command was: %x", cmd);
322
                   $display("Exiting...");
323
 
324
                   $finish();//shouldn't be here
325
                end
326
 
327
            endcase // case (cmd)
328
 
329
            // send back response, which is currently nothing
330
            // but could be used to signal something
331
            $return_response();
332
 
333
         end // while (1)
334
      end
335
 
336
   endtask // main
337
 
338
 
339
 
340
   // Receiving data and calculating input crc
341
   always @(posedge tck)
342
     begin
343
        in_data_be[31:1] <= #1 in_data_be[30:0];
344
        in_data_be[0]    <= #1 tdo;
345
 
346
        in_data_le[31]   <= #1 tdo;
347
        in_data_le[30:0] <= #1 in_data_le[31:1];
348
     end
349
 
350
   // Generation of the TCK signal
351
   task gen_clk;
352
      input [31:0] number;
353
      integer      i;
354
      begin
355
         for(i=0; i<number; i=i+1)
356
           begin
357
              #Tck tck<=1;
358
              #Tck tck<=0;
359
           end
360
      end
361
   endtask
362
 
363
   // TAP reset
364
   task reset_tap;
365
 
366
      begin
367
`ifdef DEBUG_INFO
368
         $display("(%0t) Task reset_tap", $time);
369
`endif
370
         tms<=#1 1'b1;
371
         gen_clk(5);
372
      end
373
   endtask
374
 
375
 
376
   // Goes to RunTestIdle state
377
   task goto_run_test_idle;
378
      begin
379
`ifdef DEBUG_INFO
380
         $display("(%0t) Task goto_run_test_idle", $time);
381
`endif
382
         tms<=#1 1'b0;
383
         gen_clk(1);
384
      end
385
   endtask
386
 
387
   // sets the instruction to the IR register and goes to the RunTestIdle state
388
   task set_instruction;
389
      input [3:0] instr;
390
      integer     i;
391
 
392
      begin
393
`ifdef DEBUG_INFO
394
         case (instr)
395
           `EXTEST          : $display("(%0t) Task set_instruction (EXTEST)", $time);
396
           `SAMPLE_PRELOAD  : $display("(%0t) Task set_instruction (SAMPLE_PRELOAD)", $time);
397
           `IDCODE          : $display("(%0t) Task set_instruction (IDCODE)", $time);
398
           `DEBUG           : $display("(%0t) Task set_instruction (DEBUG)", $time);
399
           `MBIST           : $display("(%0t) Task set_instruction (MBIST)", $time);
400
           `BYPASS          : $display("(%0t) Task set_instruction (BYPASS)", $time);
401
           default
402
             begin
403
                $display("(%0t) Task set_instruction (Unsupported instruction !!!)", $time);
404
                $display("\tERROR: Unsupported instruction !!!", $time);
405
                $stop;
406
             end
407
         endcase
408
`endif
409
 
410
         tms<=#1 1;
411
         gen_clk(2);
412
         tms<=#1 0;
413
         gen_clk(2);  // we are in shiftIR
414
 
415
         for(i=0; i<`IR_LENGTH-1; i=i+1)
416
           begin
417
              tdi<=#1 instr[i];
418
              gen_clk(1);
419
           end
420
 
421
         tdi<=#1 instr[i]; // last shift
422
         tms<=#1 1;        // going out of shiftIR
423
         gen_clk(1);
424
         tdi<=#1 'hz;    // tri-state
425
         gen_clk(1);
426
         tms<=#1 0;
427
         gen_clk(1);       // we are in RunTestIdle
428
      end
429
   endtask
430
 
431
 
432
   // send 32 bits through the device
433
   task test_bypass;
434
      input  [31:0] in;
435
      output [31:0] out;
436
      integer       i;
437
 
438
      reg [31:0]    out;
439
 
440
      begin
441
         tms<=#Tp 1;
442
         gen_clk(1);
443
         tms<=#Tp 0;
444
         gen_clk(2);             // we are in shiftDR
445
 
446
         for(i=31; i>=0; i=i-1)
447
           begin
448
              tdi<=#Tp in[i];
449
              gen_clk(1);
450
           end
451
 
452
         tms<=#Tp 1;             // going out of shiftDR
453
         gen_clk(1);
454
 
455
         out <=#Tp in_data_be;
456
         tdi<=#Tp 'hz;
457
 
458
         gen_clk(1);
459
         tms<=#Tp 0;
460
         gen_clk(1);             // we are in RunTestIdle
461
      end
462
   endtask
463
 
464
   // Reads the ID code
465
   task read_id_code;
466
      output [31:0] code;
467
      reg [31:0]    code;
468
      begin
469
`ifdef DEBUG_INFO
470
         $display("(%0t) Task read_id_code", $time);
471
`endif
472
 
473
         tms<=#1 1;
474
         gen_clk(1);
475
         tms<=#1 0;
476
         gen_clk(2);  // we are in shiftDR
477
 
478
         tdi<=#1 0;
479
         gen_clk(31);
480
 
481
         tms<=#1 1;        // going out of shiftIR
482
         gen_clk(1);
483
 
484
         code = in_data_le;
485
 
486
         tdi<=#1 'hz; // tri-state
487
         gen_clk(1);
488
         tms<=#1 0;
489
         gen_clk(1);       // we are in RunTestIdle
490
      end
491
   endtask
492
 
493
   // test bundary scan chain
494
   task test_bs;
495
      input  [31:0] in;
496
      output [31:0] out;
497
      integer       i;
498
 
499
      reg [31:0]    out;
500
 
501
      begin
502
         tms<=#Tp 1;
503
         gen_clk(1);
504
         tms<=#Tp 0;
505
         gen_clk(2);             // we are in shiftDR
506
 
507
         for(i=31; i>=0; i=i-1)
508
           begin
509
              tdi<=#Tp in[i];
510
              gen_clk(1);
511
           end
512
 
513
         gen_clk(`BS_CELL_NB-1);
514
         tms<=#Tp 1;             // going out of shiftDR
515
         gen_clk(1);
516
 
517
         out <=#Tp in_data_be;
518
 
519
         gen_clk(1);
520
         tms<=#Tp 0;
521
         gen_clk(1);             // we are in RunTestIdle
522
      end
523
   endtask
524
 
525
 
526
 
527
   // sets the selected scan chain and goes to the RunTestIdle state
528
   task module_select;
529
      input [`DBG_TOP_MODULE_ID_LENGTH -1:0]  data;
530
      input                                   gen_crc_err;
531
      integer                                 i;
532
 
533
      begin
534
`ifdef DEBUG_INFO
535
         case (data)
536
           `DBG_TOP_CPU1_DEBUG_MODULE : $display("(%0t) Task module_select (DBG_TOP_CPU1_DEBUG_MODULE, gen_crc_err=%0d)", $time, gen_crc_err);
537
           `DBG_TOP_CPU0_DEBUG_MODULE : $display("(%0t) Task module_select (DBG_TOP_CPU0_DEBUG_MODULE, gen_crc_err=%0d)", $time, gen_crc_err);
538
           `DBG_TOP_WISHBONE_DEBUG_MODULE : $display("(%0t) Task module_select (DBG_TOP_WISHBONE_DEBUG_MODULE gen_crc_err=%0d)", $time, gen_crc_err);
539
           default               : $display("(%0t) Task module_select (ERROR!!! Unknown module selected)", $time);
540
         endcase
541
`endif
542
 
543
         tms<=#1 1'b1;
544
         gen_clk(1);
545
         tms<=#1 1'b0;
546
         gen_clk(2);  // we are in shiftDR
547
 
548
         status = {`DBG_TOP_STATUS_LEN{1'b0}};    // Initialize status to all 0's
549
         crc_out = {`DBG_TOP_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff    
550
         tdi<=#1 1'b1; // module_select bit
551
         calculate_crc(1'b1);
552
         gen_clk(1);
553
 
554
         for(i=`DBG_TOP_MODULE_ID_LENGTH -1; i>=0; i=i-1) // Shifting module ID
555
           begin
556
              tdi<=#1 data[i];
557
              calculate_crc(data[i]);
558
              gen_clk(1);
559
           end
560
 
561
         for(i=`DBG_TOP_CRC_LEN -1; i>=0; i=i-1)
562
           begin
563
              if (gen_crc_err & (i==0))  // Generate crc error at last crc bit
564
                tdi<=#1 ~crc_out[i];   // error crc
565
              else
566
                tdi<=#1 crc_out[i];    // ok crc
567
 
568
              gen_clk(1);
569
           end
570
 
571
         tdi<=#1 1'hz;  // tri-state
572
 
573
         crc_in = {`DBG_TOP_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
574
 
575
         for(i=`DBG_TOP_STATUS_LEN -1; i>=0; i=i-1)
576
           begin
577
              gen_clk(1);     // Generating 1 clock to read out a status bit.
578
              status[i] = tdo;
579
           end
580
 
581
         for(i=0; i<`DBG_TOP_CRC_LEN -1; i=i+1)
582
           gen_clk(1);
583
 
584
         tms<=#1 1'b1;
585
         gen_clk(1);         // to exit1_dr
586
 
587
         if (~crc_match_in)
588
           begin
589
              $display("(%0t) Incoming CRC failed !!!", $time);
590
`ifdef DEBUG_INFO
591
              $stop;
592
`endif
593
           end
594
 
595
         tms<=#1 1'b1;
596
         gen_clk(1);         // to update_dr
597
         tms<=#1 1'b0;
598
         gen_clk(1);         // to run_test_idle
599
 
600
         if (|status)
601
           begin
602
              $write("(*E) (%0t) Module select error: ", $time);
603
              casex (status)
604
                4'b1xxx : $display("CRC error !!!\n\n", $time);
605
                4'bx1xx : $display("Non-existing module selected !!!\n\n", $time);
606
                4'bxx1x : $display("Status[1] should be 1'b0 !!!\n\n", $time);
607
                4'bxxx1 : $display("Status[0] should be 1'b0 !!!\n\n", $time);
608
              endcase
609
`ifdef DEBUG_INFO
610
              $stop;
611
`endif
612
           end
613
      end
614
   endtask   // module_select
615
 
616
 
617
 
618
   // 32-bit write to the wishbone
619
   task wb_write_32;
620
      input [31:0] data;
621
      input [`DBG_WB_ADR_LEN -1:0] addr;
622
      input [`DBG_WB_LEN_LEN -1:0] length;
623
 
624
      begin
625
         data_storage[0] = data;
626
         debug_wishbone_wr_comm(`DBG_WB_WRITE32, addr, length, 1'b0);
627
         last_wb_cmd = `DBG_WB_WRITE32;  last_wb_cmd_text = "DBG_WB_WRITE32";
628
         length_global = length + 1;
629
         debug_wishbone_go(1'b0, 1'b0);
630
         //debug_wishbone_go(1'b1, 1'b0); // maybe causes underrun/overrun error when wait for WB ready?
631
         if (length>3)
632
           $display("WARNING: Only first data word is stored for writting ( See module %m)");
633
      end
634
   endtask
635
 
636
   // block 32-bit write to the wishbone
637
   // presumes data is already in data_storage[]
638
   task wb_block_write_32;
639
 
640
      input [`DBG_WB_ADR_LEN -1:0] addr;
641
      input [`DBG_WB_LEN_LEN -1:0] length;
642
 
643
      begin
644
 
645
         debug_wishbone_wr_comm(`DBG_WB_WRITE32, addr, length-1, 1'b0);
646
 
647
         last_wb_cmd = `DBG_WB_WRITE32;  last_wb_cmd_text = "DBG_WB_WRITE32";
648
 
649
         length_global = length; // number of bytes!
650
 
651
         debug_wishbone_go(1'b0, 1'b0);
652
 
653
         //debug_wishbone_go(1'b1, 1'b0); // maybe causes underrun/overrun error when wait for WB ready?
654
 
655
      end
656
   endtask
657
 
658
   // 32-bit read from the wishbone
659
   task wb_read_32;
660
 
661
    output [31:0] data;
662
 
663
    input [`DBG_WB_ADR_LEN -1:0] addr;
664
    input [`DBG_WB_LEN_LEN -1:0] length;
665
 
666
      begin
667
         debug_wishbone_wr_comm(`DBG_WB_READ32, addr, length, 1'b0);
668
         last_wb_cmd = `DBG_WB_READ32;  last_wb_cmd_text = "DBG_WB_READ32";
669
         length_global = length + 1;
670
         //debug_wishbone_go(1'b0, 1'b0);
671
         debug_wishbone_go(1'b1, 1'b0);
672
         data = data_storage[0];
673
         if (length>3)
674
           $display("WARNING: Only first data word is stored for writting ( See module %m)");
675
      end
676
   endtask
677
 
678
 
679
   // block 32-bit read from the wishbone
680
   // assumes data will be stored into data_storage[]
681
   task wb_block_read_32;
682
 
683
    input [`DBG_WB_ADR_LEN -1:0] addr;
684
    input [`DBG_WB_LEN_LEN -1:0] length;
685
 
686
      begin
687
         debug_wishbone_wr_comm(`DBG_WB_READ32, addr, length-1, 1'b0);
688
 
689
         last_wb_cmd = `DBG_WB_READ32;  last_wb_cmd_text = "DBG_WB_READ32";
690
 
691
         length_global = length;
692
 
693
         //debug_wishbone_go(1'b0, 1'b0);
694
         debug_wishbone_go(1'b1, 1'b0);
695
 
696
      end
697
   endtask
698
 
699
 
700
   // 16-bit write to the wishbone
701
   task wb_write_16;
702
      input [15:0] data;
703
      input [`DBG_WB_ADR_LEN -1:0] addr;
704
      input [`DBG_WB_LEN_LEN -1:0] length;
705
 
706
      begin
707
         data_storage[0] = {data, 16'h0};
708
         debug_wishbone_wr_comm(`DBG_WB_WRITE16, addr, length, 1'b0);
709
         last_wb_cmd = `DBG_WB_WRITE16;  last_wb_cmd_text = "DBG_WB_WRITE16";
710
         length_global = length + 1;
711
         debug_wishbone_go(1'b0, 1'b0);
712
         if (length>1)
713
           $display("WARNING: Only first data half is stored for writting ( See module %m)");
714
      end
715
   endtask
716
 
717
 
718
 
719
   // 8-bit write to the wishbone
720
   task wb_write_8;
721
      input [7:0] data;
722
      input [`DBG_WB_ADR_LEN -1:0] addr;
723
      input [`DBG_WB_LEN_LEN -1:0] length;
724
 
725
      begin
726
         data_storage[0] = {data, 24'h0};
727
         debug_wishbone_wr_comm(`DBG_WB_WRITE8, addr, length, 1'b0);
728
         last_wb_cmd = `DBG_WB_WRITE8;  last_wb_cmd_text = "DBG_WB_WRITE8";
729
         length_global = length + 1;
730
         debug_wishbone_go(1'b0, 1'b0);
731
         if (length>0)
732
           $display("WARNING: Only first data byte is stored for writting ( See module %m)");
733
      end
734
   endtask
735
 
736
 
737
 
738
   task debug_wishbone_wr_comm;
739
      input [`DBG_WB_ACC_TYPE_LEN -1:0]   acc_type;
740
      input [`DBG_WB_ADR_LEN -1:0]         addr;
741
      input [`DBG_WB_LEN_LEN -1:0]         length;
742
      input                               gen_crc_err;
743
      integer                             i;
744
      reg [`DBG_WB_CMD_LEN -1:0]           command;
745
 
746
      begin
747
`ifdef DEBUG_INFO
748
         $display("(%0t) Task debug_wishbone_wr_comm: ", $time);
749
`endif
750
 
751
         command = `DBG_WB_WR_COMM;
752
         tms<=#1 1'b1;
753
         gen_clk(1);
754
         tms<=#1 1'b0;
755
         gen_clk(2);  // we are in shiftDR
756
 
757
         crc_out = {`DBG_WB_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
758
 
759
         tdi<=#1 1'b0; // module_select bit = 0
760
         calculate_crc(1'b0);
761
         gen_clk(1);
762
 
763
         for(i=`DBG_WB_CMD_LEN -1; i>=0; i=i-1)
764
           begin
765
              tdi<=#1 command[i]; // command
766
              calculate_crc(command[i]);
767
              gen_clk(1);
768
           end
769
 
770
         for(i=`DBG_WB_ACC_TYPE_LEN -1; i>=0; i=i-1)
771
           begin
772
              tdi<=#1 acc_type[i]; // command
773
              calculate_crc(acc_type[i]);
774
              gen_clk(1);
775
           end
776
 
777
         for(i=`DBG_WB_ADR_LEN -1; i>=0; i=i-1)       // address
778
           begin
779
              tdi<=#1 addr[i];
780
              calculate_crc(addr[i]);
781
              gen_clk(1);
782
           end
783
 
784
         for(i=`DBG_WB_LEN_LEN -1; i>=0; i=i-1)       // length
785
           begin
786
              tdi<=#1 length[i];
787
              calculate_crc(length[i]);
788
              gen_clk(1);
789
           end
790
 
791
         for(i=`DBG_WB_CRC_LEN -1; i>=0; i=i-1)
792
           begin
793
              if (gen_crc_err & (i==0))  // Generate crc error at last crc bit
794
                tdi<=#1 ~crc_out[i];   // error crc
795
              else
796
                tdi<=#1 crc_out[i];    // ok crc
797
 
798
              gen_clk(1);
799
           end
800
 
801
         tdi<=#1 1'hz;
802
 
803
         crc_in = {`DBG_WB_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
804
 
805
         for(i=`DBG_WB_STATUS_LEN -1; i>=0; i=i-1)
806
           begin
807
              gen_clk(1);     // Generating clock to read out a status bit.
808
              status_wb[i] = tdo;
809
           end
810
 
811
         if (|status_wb)
812
           begin
813
              $write("(*E) (%0t) debug_wishbone_wr_comm error: ", $time);
814
              casex (status_wb)
815
                4'b1xxx : $display("CRC error !!!\n\n", $time);
816
                4'bx1xx : $display("Unknown command !!!\n\n", $time);
817
                4'bxx1x : $display("WISHBONE error !!!\n\n", $time);
818
                4'bxxx1 : $display("Overrun/Underrun !!!\n\n", $time);
819
              endcase
820
`ifdef DEBUG_INFO
821
              $stop;
822
`endif
823
           end
824
 
825
 
826
         for(i=0; i<`DBG_WB_CRC_LEN -1; i=i+1)  // Getting in the CRC
827
           begin
828
              gen_clk(1);
829
           end
830
 
831
         tms<=#1 1'b1;
832
         gen_clk(1);         // to exit1_dr
833
 
834
         if (~crc_match_in)
835
           begin
836
              $display("(%0t) Incoming CRC failed !!!", $time);
837
`ifdef DEBUG_INFO
838
              $stop;
839
`endif
840
           end
841
 
842
         tms<=#1 1'b1;
843
         gen_clk(1);         // to update_dr
844
         tms<=#1 1'b0;
845
         gen_clk(1);         // to run_test_idle
846
      end
847
   endtask       // debug_wishbone_wr_comm
848
 
849
 
850
 
851
   task debug_wishbone_go;
852
 
853
      input         wait_for_wb_ready;
854
      input         gen_crc_err;
855
      integer       i;
856
      reg [4:0]     bit_pointer;
857
      integer       word_pointer;
858
      reg [31:0]    tmp_data;
859
      reg [`DBG_WB_CMD_LEN -1:0] command;
860
 
861
 
862
      begin
863
 
864
`ifdef DEBUG_INFO
865
         $display("(%0t) Task debug_wishbone_go (previous command was %0s): ", $time, last_wb_cmd_text);
866
`endif
867
 
868
         command = `DBG_WB_GO;
869
         word_pointer = 0;
870
 
871
         tms<=#1 1'b1;
872
         gen_clk(1);
873
         tms<=#1 1'b0;
874
         gen_clk(2);  // we are in shiftDR
875
 
876
         crc_out = {`DBG_WB_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
877
 
878
         tdi<=#1 1'b0; // module_select bit = 0
879
         calculate_crc(1'b0);
880
         gen_clk(1);
881
 
882
         for(i=`DBG_WB_CMD_LEN -1; i>=0; i=i-1)
883
           begin
884
              tdi<=#1 command[i]; // command
885
              calculate_crc(command[i]);
886
              gen_clk(1);
887
           end
888
 
889
         // W R I T E
890
         if (
891
             (last_wb_cmd == `DBG_WB_WRITE8) | (last_wb_cmd == `DBG_WB_WRITE16) |
892
             (last_wb_cmd == `DBG_WB_WRITE32)
893
             )  // When WB_WRITEx was previously activated, data needs to be shifted.
894
           begin
895
              for (i=0; i<((length_global) << 3); i=i+1)
896
                begin
897
 
898
                   if ((!(i%32)) && (i>0))
899
                     begin
900
                        word_pointer = word_pointer + 1;
901
                     end
902
 
903
                   tmp_data = data_storage[word_pointer];
904
 
905
                   bit_pointer = 31-i[4:0];
906
 
907
                   tdi<=#1 tmp_data[bit_pointer];
908
 
909
                   calculate_crc(tmp_data[bit_pointer]);
910
 
911
                   gen_clk(1);
912
 
913
                end
914
           end
915
 
916
         for(i=`DBG_WB_CRC_LEN -1; i>=1; i=i-1)
917
           begin
918
              tdi<=#1 crc_out[i];
919
              gen_clk(1);
920
           end
921
 
922
         if (gen_crc_err)  // Generate crc error at last crc bit
923
           tdi<=#1 ~crc_out[0];   // error crc
924
         else
925
           tdi<=#1 crc_out[0];    // ok crc
926
 
927
         if (wait_for_wb_ready)
928
           begin
929
              tms<=#1 1'b1;
930
              gen_clk(1);       // to exit1_dr. Last CRC is shifted on this clk
931
              tms<=#1 1'b0;
932
              gen_clk(1);       // to pause_dr
933
 
934
              #2;             // wait a bit for tdo to activate
935
              while (tdo)     // waiting for wb to send "ready"
936
                begin
937
                   gen_clk(1);       // staying in pause_dr
938
                end
939
 
940
              tms<=#1 1'b1;
941
              gen_clk(1);       // to exit2_dr
942
              tms<=#1 1'b0;
943
              gen_clk(1);       // to shift_dr
944
           end
945
         else
946
           begin
947
              gen_clk(1);       // Last CRC is shifted on this clk
948
           end
949
 
950
 
951
         tdi<=#1 1'hz;
952
 
953
         // R E A D
954
 
955
         crc_in = {`DBG_WB_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
956
 
957
         if (
958
             (last_wb_cmd == `DBG_WB_READ8) | (last_wb_cmd == `DBG_WB_READ16) |
959
             (last_wb_cmd == `DBG_WB_READ32)
960
             )  // When WB_READx was previously activated, data needs to be shifted.
961
           begin
962
 
963
`ifdef DEBUG_INFO
964
              $display("\t\tGenerating %0d clocks to read %0d data bytes.", length_global<<3, length_global);
965
`endif
966
              word_pointer = 0; // Reset pointer
967
 
968
              for (i=0; i<(length_global<<3); i=i+1)
969
                begin
970
 
971
                   gen_clk(1);
972
 
973
                   if (i[4:0] == 31)   // Latching data
974
                     begin
975
 
976
                        data_storage[word_pointer] = in_data_be;
977
`ifdef DEBUG_INFO
978
                        $display("\t\tin_data_be = 0x%x", in_data_be);
979
`endif
980
                        word_pointer = word_pointer + 1;
981
 
982
                     end
983
                end
984
           end
985
 
986
         for(i=`DBG_WB_STATUS_LEN -1; i>=0; i=i-1)
987
           begin
988
 
989
              gen_clk(1);     // Generating clock to read out a status bit.
990
              status_wb[i] = tdo;
991
 
992
           end
993
 
994
         if (|status_wb)
995
           begin
996
              $write("(*E) (%0t) debug_wishbone_go error: ", $time);
997
              casex (status_wb)
998
                4'b1xxx : $display("CRC error !!!\n\n", $time);
999
                4'bx1xx : $display("Unknown command !!!\n\n", $time);
1000
                4'bxx1x : $display("WISHBONE error !!!\n\n", $time);
1001
                4'bxxx1 : $display("Overrun/Underrun !!!\n\n", $time);
1002
              endcase
1003
`ifdef DEBUG_INFO
1004
              $stop;
1005
`endif
1006
           end
1007
 
1008
 
1009
         for(i=0; i<`DBG_WB_CRC_LEN -1; i=i+1)  // Getting in the CRC
1010
           begin
1011
              gen_clk(1);
1012
           end
1013
 
1014
         tms<=#1 1'b1;
1015
         gen_clk(1);         // to exit1_dr
1016
 
1017
         if (~crc_match_in)
1018
           begin
1019
              $display("(%0t) Incoming CRC failed !!!", $time);
1020
`ifdef DEBUG_INFO
1021
              $stop;
1022
`endif
1023
           end
1024
 
1025
         tms<=#1 1'b1;
1026
         gen_clk(1);         // to update_dr
1027
         tms<=#1 1'b0;
1028
         gen_clk(1);         // to run_test_idle
1029
      end
1030
   endtask       // debug_wishbone_go
1031
 
1032
 
1033
 
1034
   task debug_cpu_wr_ctrl;
1035
      input [`DBG_CPU_DR_LEN -1:0]  data;
1036
      input [99:0]                   text;
1037
      integer                       i;
1038
      reg [`DBG_CPU_CMD_LEN -1:0]   command;
1039
 
1040
      begin
1041
         test_text = text;
1042
 
1043
`ifdef DEBUG_INFO
1044
         $display("(%0t) Task debug_cpu_wr_ctrl (data=0x%0x (%0s))", $time, data, text);
1045
`endif
1046
 
1047
         command = `DBG_CPU_WR_CTRL;
1048
         tms<=#1 1'b1;
1049
         gen_clk(1);
1050
         tms<=#1 1'b0;
1051
         gen_clk(2);  // we are in shiftDR
1052
 
1053
         crc_out = {`DBG_CPU_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
1054
 
1055
         tdi<=#1 1'b0; // module_select bit = 0
1056
         calculate_crc(1'b0);
1057
         gen_clk(1);
1058
 
1059
         for(i=`DBG_CPU_CMD_LEN -1; i>=0; i=i-1)
1060
           begin
1061
              tdi<=#1 command[i]; // command
1062
              calculate_crc(command[i]);
1063
              gen_clk(1);
1064
           end
1065
 
1066
 
1067
         for(i=`DBG_CPU_CTRL_LEN -1; i>=0; i=i-1)
1068
           begin
1069
              tdi<=#1 data[i];                                    // data (used cotrol bits
1070
              calculate_crc(data[i]);
1071
              gen_clk(1);
1072
           end
1073
 
1074
         for(i=`DBG_CPU_DR_LEN - `DBG_CPU_CTRL_LEN -1; i>=0; i=i-1)  // unused control bits
1075
           begin
1076
              tdi<=#1 1'b0;
1077
              calculate_crc(1'b0);
1078
              gen_clk(1);
1079
           end
1080
 
1081
 
1082
         for(i=`DBG_CPU_CRC_LEN -1; i>=0; i=i-1)
1083
           begin
1084
              tdi<=#1 crc_out[i];    // ok crc
1085
              gen_clk(1);
1086
           end
1087
 
1088
         tdi<=#1 1'hz;
1089
 
1090
         crc_in = {`DBG_CPU_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
1091
 
1092
         for(i=`DBG_CPU_STATUS_LEN -1; i>=0; i=i-1)
1093
           begin
1094
              gen_clk(1);     // Generating clock to read out a status bit.
1095
              status_cpu[i] = tdo;
1096
           end
1097
 
1098
         if (|status_cpu)
1099
           begin
1100
              $write("(*E) (%0t) debug_cpu_wr_ctrl error: ", $time);
1101
              casex (status_cpu)
1102
                4'b1xxx : $display("CRC error !!!\n\n", $time);
1103
                4'bx1xx : $display("??? error !!!\n\n", $time);
1104
                4'bxx1x : $display("??? error !!!\n\n", $time);
1105
                4'bxxx1 : $display("??? error !!!\n\n", $time);
1106
              endcase
1107
`ifdef DEBUG_INFO
1108
              $stop;
1109
`endif
1110
           end
1111
 
1112
 
1113
         for(i=0; i<`DBG_CPU_CRC_LEN -1; i=i+1)  // Getting in the CRC
1114
           begin
1115
              gen_clk(1);
1116
           end
1117
 
1118
         tms<=#1 1'b1;
1119
         gen_clk(1);         // to exit1_dr
1120
 
1121
         if (~crc_match_in)
1122
           begin
1123
              $display("(%0t) Incoming CRC failed !!!", $time);
1124
`ifdef DEBUG_INFO
1125
              $stop;
1126
`endif
1127
           end
1128
 
1129
         tms<=#1 1'b1;
1130
         gen_clk(1);         // to update_dr
1131
         tms<=#1 1'b0;
1132
         gen_clk(1);         // to run_test_idle
1133
      end
1134
   endtask       // debug_cpu_wr_ctrl
1135
 
1136
   task debug_cpu_rd_ctrl;
1137
      output [`DBG_CPU_DR_LEN -1:0]  data;
1138
      //input [99:0]                 text;
1139
      integer                        i;
1140
      reg [`DBG_CPU_CMD_LEN -1:0]    command;
1141
 
1142
      begin
1143
 
1144
 
1145
`ifdef DEBUG_INFO
1146
         $display("(%0t) Task debug_cpu_rd_ctrl", $time);
1147
`endif
1148
 
1149
         command = `DBG_CPU_RD_CTRL;
1150
         tms<=#1 1'b1;
1151
         gen_clk(1);
1152
         tms<=#1 1'b0;
1153
         gen_clk(2);  // we are in shiftDR
1154
 
1155
         crc_out = {`DBG_CPU_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
1156
 
1157
         tdi<=#1 1'b0; // module_select bit = 0
1158
         calculate_crc(1'b0);
1159
         gen_clk(1);
1160
 
1161
         for(i=`DBG_CPU_CMD_LEN -1; i>=0; i=i-1)
1162
           begin
1163
              tdi<=#1 command[i]; // command
1164
              calculate_crc(command[i]);
1165
              gen_clk(1);
1166
           end
1167
 
1168
         for(i=`DBG_CPU_CRC_LEN -1; i>=0; i=i-1)
1169
           begin
1170
              tdi<=#1 crc_out[i];    // ok crc
1171
              gen_clk(1);
1172
           end
1173
 
1174
         tdi<=#1 1'hz;
1175
 
1176
         crc_in = {`DBG_CPU_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
1177
 
1178
         // Read incoming debug ctrl data (52 bits)
1179
         //cpu_ctrl_val[1:0];
1180
         gen_clk(1);
1181
         //cpu_ctrl_val[0]  <= #1 tdo; // cpu reset bit
1182
         data[0]  <= #1 tdo; // cpu reset bit
1183
         gen_clk(1);
1184
         //cpu_ctrl_val[1]  <= #1 tdo; // cpu stall bit
1185
         data[1]  <= #1 tdo; // cpu stall bit
1186
 
1187
         for(i=`DBG_CPU_DR_LEN - `DBG_CPU_CTRL_LEN -1; i>=0; i=i-1)  // unused control bits
1188
           begin
1189
              gen_clk(1);
1190
           end
1191
 
1192
         for(i=`DBG_CPU_STATUS_LEN -1; i>=0; i=i-1)
1193
           begin
1194
              gen_clk(1);     // Generating clock to read out a status bit.
1195
              status_cpu[i] = tdo;
1196
           end
1197
 
1198
         if (|status_cpu)
1199
           begin
1200
              $write("(*E) (%0t) debug_cpu_wr_ctrl error: ", $time);
1201
              casex (status_cpu)
1202
                4'b1xxx : $display("CRC error !!!\n\n", $time);
1203
                4'bx1xx : $display("??? error !!!\n\n", $time);
1204
                4'bxx1x : $display("??? error !!!\n\n", $time);
1205
                4'bxxx1 : $display("??? error !!!\n\n", $time);
1206
              endcase // casex (status_cpu)
1207
 
1208
           end
1209
 
1210
 
1211
         for(i=0; i<`DBG_CPU_CRC_LEN -1; i=i+1)  // Getting in the CRC
1212
           begin
1213
              gen_clk(1);
1214
           end
1215
 
1216
         tms<=#1 1'b1;
1217
         gen_clk(1);         // to exit1_dr
1218
 
1219
         if (~crc_match_in)
1220
           begin
1221
              $display("(%0t) Incoming CRC failed !!!", $time);
1222
 
1223
           end
1224
 
1225
         tms<=#1 1'b1;
1226
         gen_clk(1);         // to update_dr
1227
         tms<=#1 1'b0;
1228
         gen_clk(1);         // to run_test_idle
1229
      end
1230
   endtask       // debug_cpu_rd_ctrl
1231
 
1232
 
1233
   // 32-bit read from cpu
1234
   task cpu_read_32;
1235
      output [31:0] data;
1236
      input [`DBG_WB_ADR_LEN -1:0] addr;
1237
      input [`DBG_WB_LEN_LEN -1:0] length;
1238
 
1239
      reg [31:0]                    tmp;
1240
 
1241
      begin
1242
         debug_cpu_wr_comm(`DBG_CPU_READ, addr, length, 1'b0);
1243
 
1244
         last_cpu_cmd = `DBG_CPU_READ;  last_cpu_cmd_text = "DBG_CPU_READ";
1245
 
1246
         length_global = length + 1;
1247
 
1248
         debug_cpu_go(1'b0, 1'b0);
1249
 
1250
         data = data_storage[0];
1251
 
1252
         if (length>3)
1253
           $display("WARNING: Only first data word is returned( See module %m.)");
1254
 
1255
      end
1256
   endtask
1257
 
1258
 
1259
 
1260
   // 32-bit write to cpu
1261
   task cpu_write_32;
1262
      input [31:0] data;
1263
      input [`DBG_WB_ADR_LEN -1:0] addr;
1264
      input [`DBG_WB_LEN_LEN -1:0] length;
1265
 
1266
      reg [31:0]                    tmp;
1267
 
1268
      begin
1269
         debug_cpu_wr_comm(`DBG_CPU_WRITE, addr, length, 1'b0);
1270
         last_cpu_cmd = `DBG_CPU_WRITE;  last_cpu_cmd_text = "DBG_CPU_WRITE";
1271
         length_global = length + 1;
1272
         data_storage[0] = data;
1273
         debug_cpu_go(1'b0, 1'b0);
1274
         if (length>3)
1275
           $display("WARNING: Only first data word is stored for writting ( See module %m)");
1276
      end
1277
   endtask
1278
 
1279
 
1280
 
1281
   task debug_cpu_wr_comm;
1282
      input [`DBG_CPU_ACC_TYPE_LEN -1:0]  acc_type;
1283
      input [`DBG_CPU_ADR_LEN -1:0]        addr;
1284
      input [`DBG_CPU_LEN_LEN -1:0]        length;
1285
      input                               gen_crc_err;
1286
      integer                             i;
1287
      reg [`DBG_CPU_CMD_LEN -1:0]          command;
1288
 
1289
      begin
1290
`ifdef DEBUG_INFO
1291
         $display("(%0t) Task debug_cpu_wr_comm: ", $time);
1292
`endif
1293
 
1294
         command = `DBG_CPU_WR_COMM;
1295
         tms<=#1 1'b1;
1296
         gen_clk(1);
1297
         tms<=#1 1'b0;
1298
         gen_clk(2);  // we are in shiftDR
1299
 
1300
         crc_out = {`DBG_CPU_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
1301
 
1302
         tdi<=#1 1'b0; // module_select bit = 0
1303
         calculate_crc(1'b0);
1304
         gen_clk(1);
1305
 
1306
         for(i=`DBG_CPU_CMD_LEN -1; i>=0; i=i-1)
1307
           begin
1308
              tdi<=#1 command[i]; // command
1309
              calculate_crc(command[i]);
1310
              gen_clk(1);
1311
           end
1312
 
1313
         for(i=`DBG_CPU_ACC_TYPE_LEN -1; i>=0; i=i-1)
1314
           begin
1315
              tdi<=#1 acc_type[i]; // command
1316
              calculate_crc(acc_type[i]);
1317
              gen_clk(1);
1318
           end
1319
 
1320
         for(i=`DBG_CPU_ADR_LEN -1; i>=0; i=i-1)       // address
1321
           begin
1322
              tdi<=#1 addr[i];
1323
              calculate_crc(addr[i]);
1324
              gen_clk(1);
1325
           end
1326
 
1327
         for(i=`DBG_CPU_LEN_LEN -1; i>=0; i=i-1)       // length
1328
           begin
1329
              tdi<=#1 length[i];
1330
              calculate_crc(length[i]);
1331
              gen_clk(1);
1332
           end
1333
 
1334
         for(i=`DBG_CPU_CRC_LEN -1; i>=0; i=i-1)
1335
           begin
1336
              if (gen_crc_err & (i==0))      // Generate crc error at last crc bit
1337
                tdi<=#1 ~crc_out[i];   // error crc
1338
              else
1339
                tdi<=#1 crc_out[i];    // ok crc
1340
 
1341
              gen_clk(1);
1342
           end
1343
 
1344
         tdi<=#1 1'hz;
1345
 
1346
         crc_in = {`DBG_CPU_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
1347
 
1348
         for(i=`DBG_CPU_STATUS_LEN -1; i>=0; i=i-1)
1349
           begin
1350
              gen_clk(1);     // Generating clock to read out a status bit.
1351
              status_cpu[i] = tdo;
1352
           end
1353
 
1354
         if (|status_cpu)
1355
           begin
1356
              $write("(*E) (%0t) debug_cpu_wr_comm error: ", $time);
1357
              casex (status_cpu)
1358
                4'b1xxx : $display("CRC error !!!\n\n", $time);
1359
                4'bx1xx : $display("Unknown command !!!\n\n", $time);
1360
                4'bxx1x : $display("??? error !!!\n\n", $time);
1361
                4'bxxx1 : $display("Overrun/Underrun !!!\n\n", $time);
1362
              endcase
1363
`ifdef DEBUG_INFO
1364
              $stop;
1365
`endif
1366
           end
1367
 
1368
 
1369
         for(i=0; i<`DBG_CPU_CRC_LEN -1; i=i+1)  // Getting in the CRC
1370
           begin
1371
              gen_clk(1);
1372
           end
1373
 
1374
         tms<=#1 1'b1;
1375
         gen_clk(1);         // to exit1_dr
1376
 
1377
         if (~crc_match_in)
1378
           begin
1379
              $display("(%0t) Incoming CRC failed !!!", $time);
1380
`ifdef DEBUG_INFO
1381
              $stop;
1382
`endif
1383
           end
1384
 
1385
         tms<=#1 1'b1;
1386
         gen_clk(1);         // to update_dr
1387
         tms<=#1 1'b0;
1388
         gen_clk(1);         // to run_test_idle
1389
      end
1390
   endtask       // debug_cpu_wr_comm
1391
 
1392
 
1393
 
1394
   task debug_cpu_go;
1395
      input         wait_for_cpu_ready;
1396
      input         gen_crc_err;
1397
      integer       i;
1398
      reg [4:0]     bit_pointer;
1399
      integer       word_pointer;
1400
      reg [31:0]    tmp_data;
1401
      reg [`DBG_CPU_CMD_LEN -1:0] command;
1402
 
1403
 
1404
      begin
1405
`ifdef DEBUG_INFO
1406
         $display("(%0t) Task debug_cpu_go (previous command was %0s): ", $time, last_cpu_cmd_text);
1407
`endif
1408
         command = `DBG_CPU_GO;
1409
         word_pointer = 0;
1410
 
1411
         tms<=#1 1'b1;
1412
         gen_clk(1);
1413
         tms<=#1 1'b0;
1414
         gen_clk(2);  // we are in shiftDR
1415
 
1416
         crc_out = {`DBG_CPU_CRC_LEN{1'b1}}; // Initialize outgoing CRC to all ff
1417
 
1418
         tdi<=#1 1'b0; // module_select bit = 0
1419
         calculate_crc(1'b0);
1420
         gen_clk(1);
1421
 
1422
         for(i=`DBG_CPU_CMD_LEN -1; i>=0; i=i-1)
1423
           begin
1424
              tdi<=#1 command[i]; // command
1425
              calculate_crc(command[i]);
1426
              gen_clk(1);
1427
           end
1428
 
1429
 
1430
         if (last_cpu_cmd == `DBG_CPU_WRITE)  // When DBG_CPU_WRITE was previously activated, data needs to be shifted.
1431
           begin
1432
              for (i=0; i<((length_global) << 3); i=i+1)
1433
                begin
1434
                   tmp_data = data_storage[word_pointer];
1435
                   if ((!(i%32)) && (i>0))
1436
                     begin
1437
                        word_pointer = word_pointer + 1;
1438
                     end
1439
                   bit_pointer = 31-i[4:0];
1440
                   tdi<=#1 tmp_data[bit_pointer];
1441
                   calculate_crc(tmp_data[bit_pointer]);
1442
                   gen_clk(1);
1443
 
1444
                end
1445
           end
1446
 
1447
         for(i=`DBG_CPU_CRC_LEN -1; i>=1; i=i-1)
1448
           begin
1449
              tdi<=#1 crc_out[i];
1450
              gen_clk(1);
1451
           end
1452
 
1453
         if (gen_crc_err)  // Generate crc error at last crc bit
1454
           tdi<=#1 ~crc_out[0];   // error crc
1455
         else
1456
           tdi<=#1 crc_out[0];    // ok crc
1457
 
1458
         if (wait_for_cpu_ready)
1459
           begin
1460
              tms<=#1 1'b1;
1461
              gen_clk(1);       // to exit1_dr. Last CRC is shifted on this clk
1462
              tms<=#1 1'b0;
1463
              gen_clk(1);       // to pause_dr
1464
 
1465
              #2;             // wait a bit for tdo to activate
1466
              while (tdo)     // waiting for wb to send "ready"
1467
                begin
1468
                   gen_clk(1);       // staying in pause_dr
1469
                end
1470
 
1471
              tms<=#1 1'b1;
1472
              gen_clk(1);       // to exit2_dr
1473
              tms<=#1 1'b0;
1474
              gen_clk(1);       // to shift_dr
1475
           end
1476
         else
1477
           begin
1478
              gen_clk(1);       // Last CRC is shifted on this clk
1479
           end
1480
 
1481
 
1482
         tdi<=#1 1'hz;
1483
         crc_in = {`DBG_CPU_CRC_LEN{1'b1}};  // Initialize incoming CRC to all ff
1484
 
1485
         if (last_cpu_cmd == `DBG_CPU_READ)  // When DBG_CPU_READ was previously activated, data needs to be shifted.
1486
           begin
1487
`ifdef DEBUG_INFO
1488
              $display("\t\tGenerating %0d clocks to read %0d data bytes.", length_global<<3, length_global);
1489
`endif
1490
              word_pointer = 0; // Reset pointer
1491
              for (i=0; i<(length_global<<3); i=i+1)
1492
                begin
1493
 
1494
                   gen_clk(1);
1495
 
1496
                   if (i[4:0] == 31)   // Latching data
1497
                     begin
1498
                        data_storage[word_pointer] = in_data_be;
1499
`ifdef DEBUG_INFO
1500
                        $display("\t\tin_data_be = 0x%x", in_data_be);
1501
`endif
1502
                        word_pointer = word_pointer + 1;
1503
                     end
1504
 
1505
                end
1506
           end
1507
 
1508
 
1509
         for(i=`DBG_CPU_STATUS_LEN -1; i>=0; i=i-1)
1510
           begin
1511
              gen_clk(1);     // Generating clock to read out a status bit.
1512
              status_cpu[i] = tdo;
1513
           end
1514
 
1515
         if (|status_cpu)
1516
           begin
1517
              $write("(*E) (%0t) debug_cpu_go error: ", $time);
1518
              casex (status_cpu)
1519
                4'b1xxx : $display("CRC error !!!\n\n", $time);
1520
                4'bx1xx : $display("Unknown command !!!\n\n", $time);
1521
                4'bxx1x : $display("??? error !!!\n\n", $time);
1522
                4'bxxx1 : $display("Overrun/Underrun !!!\n\n", $time);
1523
              endcase
1524
              $stop;
1525
           end
1526
 
1527
 
1528
         for(i=0; i<`DBG_CPU_CRC_LEN -1; i=i+1)  // Getting in the CRC
1529
           begin
1530
              gen_clk(1);
1531
           end
1532
 
1533
         tms<=#1 1'b1;
1534
         gen_clk(1);         // to exit1_dr
1535
 
1536
         if (~crc_match_in)
1537
           begin
1538
              $display("(%0t) Incoming CRC failed !!!", $time);
1539
              $stop;
1540
           end
1541
 
1542
         tms<=#1 1'b1;
1543
         gen_clk(1);         // to update_dr
1544
         tms<=#1 1'b0;
1545
         gen_clk(1);         // to run_test_idle
1546
      end
1547
   endtask       // debug_cpu_go
1548
 
1549
 
1550
 
1551
 
1552
   // Calculating outgoing CRC
1553
   task calculate_crc;
1554
      input data;
1555
 
1556
      begin
1557
         crc_out[0]  <= #1 data          ^ crc_out[31];
1558
         crc_out[1]  <= #1 data          ^ crc_out[0]  ^ crc_out[31];
1559
         crc_out[2]  <= #1 data          ^ crc_out[1]  ^ crc_out[31];
1560
         crc_out[3]  <= #1 crc_out[2];
1561
         crc_out[4]  <= #1 data          ^ crc_out[3]  ^ crc_out[31];
1562
         crc_out[5]  <= #1 data          ^ crc_out[4]  ^ crc_out[31];
1563
         crc_out[6]  <= #1 crc_out[5];
1564
         crc_out[7]  <= #1 data          ^ crc_out[6]  ^ crc_out[31];
1565
         crc_out[8]  <= #1 data          ^ crc_out[7]  ^ crc_out[31];
1566
         crc_out[9]  <= #1 crc_out[8];
1567
         crc_out[10] <= #1 data         ^ crc_out[9]  ^ crc_out[31];
1568
         crc_out[11] <= #1 data         ^ crc_out[10] ^ crc_out[31];
1569
         crc_out[12] <= #1 data         ^ crc_out[11] ^ crc_out[31];
1570
         crc_out[13] <= #1 crc_out[12];
1571
         crc_out[14] <= #1 crc_out[13];
1572
         crc_out[15] <= #1 crc_out[14];
1573
         crc_out[16] <= #1 data         ^ crc_out[15] ^ crc_out[31];
1574
         crc_out[17] <= #1 crc_out[16];
1575
         crc_out[18] <= #1 crc_out[17];
1576
         crc_out[19] <= #1 crc_out[18];
1577
         crc_out[20] <= #1 crc_out[19];
1578
         crc_out[21] <= #1 crc_out[20];
1579
         crc_out[22] <= #1 data         ^ crc_out[21] ^ crc_out[31];
1580
         crc_out[23] <= #1 data         ^ crc_out[22] ^ crc_out[31];
1581
         crc_out[24] <= #1 crc_out[23];
1582
         crc_out[25] <= #1 crc_out[24];
1583
         crc_out[26] <= #1 data         ^ crc_out[25] ^ crc_out[31];
1584
         crc_out[27] <= #1 crc_out[26];
1585
         crc_out[28] <= #1 crc_out[27];
1586
         crc_out[29] <= #1 crc_out[28];
1587
         crc_out[30] <= #1 crc_out[29];
1588
         crc_out[31] <= #1 crc_out[30];
1589
      end
1590
   endtask // calculate_crc
1591
 
1592
 
1593
   // Calculating and checking input CRC
1594
   always @(posedge tck)
1595
     begin
1596
        crc_in[0]  <= #1 tdo           ^ crc_in[31];
1597
        crc_in[1]  <= #1 tdo           ^ crc_in[0]  ^ crc_in[31];
1598
        crc_in[2]  <= #1 tdo           ^ crc_in[1]  ^ crc_in[31];
1599
        crc_in[3]  <= #1 crc_in[2];
1600
        crc_in[4]  <= #1 tdo           ^ crc_in[3]  ^ crc_in[31];
1601
        crc_in[5]  <= #1 tdo           ^ crc_in[4]  ^ crc_in[31];
1602
        crc_in[6]  <= #1 crc_in[5];
1603
        crc_in[7]  <= #1 tdo           ^ crc_in[6]  ^ crc_in[31];
1604
        crc_in[8]  <= #1 tdo           ^ crc_in[7]  ^ crc_in[31];
1605
        crc_in[9]  <= #1 crc_in[8];
1606
        crc_in[10] <= #1 tdo          ^ crc_in[9]  ^ crc_in[31];
1607
        crc_in[11] <= #1 tdo          ^ crc_in[10] ^ crc_in[31];
1608
        crc_in[12] <= #1 tdo          ^ crc_in[11] ^ crc_in[31];
1609
        crc_in[13] <= #1 crc_in[12];
1610
        crc_in[14] <= #1 crc_in[13];
1611
        crc_in[15] <= #1 crc_in[14];
1612
        crc_in[16] <= #1 tdo          ^ crc_in[15] ^ crc_in[31];
1613
        crc_in[17] <= #1 crc_in[16];
1614
        crc_in[18] <= #1 crc_in[17];
1615
        crc_in[19] <= #1 crc_in[18];
1616
        crc_in[20] <= #1 crc_in[19];
1617
        crc_in[21] <= #1 crc_in[20];
1618
        crc_in[22] <= #1 tdo          ^ crc_in[21] ^ crc_in[31];
1619
        crc_in[23] <= #1 tdo          ^ crc_in[22] ^ crc_in[31];
1620
        crc_in[24] <= #1 crc_in[23];
1621
        crc_in[25] <= #1 crc_in[24];
1622
        crc_in[26] <= #1 tdo          ^ crc_in[25] ^ crc_in[31];
1623
        crc_in[27] <= #1 crc_in[26];
1624
        crc_in[28] <= #1 crc_in[27];
1625
        crc_in[29] <= #1 crc_in[28];
1626
        crc_in[30] <= #1 crc_in[29];
1627
        crc_in[31] <= #1 crc_in[30];
1628
     end
1629
 
1630
   assign crc_match_in = crc_in == 32'h0;
1631
 
1632
 
1633
endmodule // vpi_debug_module

powered by: WebSVN 2.1.0

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