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 397

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

powered by: WebSVN 2.1.0

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