23
23
module XC2CJTAG (
24
24
tdi, tms, tck, tdo,
25
25
config_erase,
26
- config_read_addr, config_read_data,
26
+ config_read_en, config_read_addr, config_read_data,
27
27
config_write_en, config_write_addr, config_write_data,
28
28
29
29
debug_led, debug_gpio);
@@ -51,6 +51,7 @@ module XC2CJTAG(
51
51
output reg config_erase = 0 ; // Erases all config memory
52
52
// This takes 100 ms IRL but for now we'll model it instantly
53
53
54
+ output reg config_read_en = 0 ;
54
55
output reg [ADDR_BITS- 1 :0 ] config_read_addr = 0 ; // Address for reading the bitstream (real, not gray code)
55
56
input wire [SHREG_WIDTH- 1 :0 ] config_read_data;
56
57
@@ -456,6 +457,17 @@ module XC2CJTAG(
456
457
457
458
end
458
459
460
+ // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
461
+ // Gray code decode ROM
462
+
463
+ reg [7 :0 ] gray_to_bin[255 :0 ];
464
+
465
+ integer i;
466
+ initial begin
467
+ for (i= 0 ; i< 256 ; i= i+ 1 )
468
+ gray_to_bin[i ^ (i >> 1 )] <= i;
469
+ end
470
+
459
471
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
460
472
// Config memory read
461
473
@@ -465,54 +477,58 @@ module XC2CJTAG(
465
477
wire [SHREG_WIDTH- 1 :0 ] isc_read_shreg_adv = {tdi, isc_read_shreg[SHREG_WIDTH- 1 :1 ]};
466
478
467
479
// Gray coded read address: left N bits of the shift register
468
- wire [ADDR_BITS- 1 :0 ] isc_read_addr_gray = isc_read_shreg_adv [SHREG_WIDTH- 1 : SHREG_WIDTH- ADDR_BITS];
480
+ wire [ADDR_BITS- 1 :0 ] isc_read_addr_gray = isc_read_shreg [SHREG_WIDTH- 1 : SHREG_WIDTH- ADDR_BITS];
469
481
470
- // Convert Gray code to normal
471
- reg [ADDR_BITS- 1 :0 ] isc_read_addr_normal;
472
- integer i;
482
+ // Invert the bit ordering of the address since the protocol is weird and has MSB at right
483
+ reg [ADDR_BITS- 1 :0 ] isc_read_addr_gray_flipped;
473
484
always @(* ) begin
474
- isc_read_addr_normal[ADDR_BITS- 1 ] <= isc_read_addr_gray[ADDR_BITS- 1 ];
475
- for (i= ADDR_BITS- 2 ; i>= 0 ; i= i- 1 )
476
- isc_read_addr_normal[i] <= isc_read_addr_gray[i] ^ isc_read_addr_normal[i+ 1 ];
485
+ for (i= 0 ; i< ADDR_BITS; i= i+ 1 )
486
+ isc_read_addr_gray_flipped[i] <= isc_read_addr_gray[ADDR_BITS - 1 - i];
477
487
end
478
488
489
+ reg config_read_real = 0 ;
479
490
always @(posedge tck) begin
480
491
481
- case (state)
492
+ config_read_en <= 0 ;
482
493
483
- // Load the data that we read
484
- STATE_CAPTURE_DR: begin
485
- isc_read_shreg <= config_read_data;
486
- end // end STATE_CAPTURE_DR
494
+ if (ir == INST_ISC_READ) begin
495
+ case (state)
487
496
488
- // Actual readout happens here
489
- STATE_SHIFT_DR : begin
490
- isc_read_shreg <= isc_read_shreg_adv ;
491
- end // end STATE_SHIFT_DR
497
+ // Load the data that we read
498
+ STATE_CAPTURE_DR : begin
499
+ isc_read_shreg <= config_read_data ;
500
+ end // end STATE_CAPTURE_DR
492
501
493
- // Update: save the de-Gray-ified read address
494
- STATE_UPDATE_DR : begin
495
- config_read_addr <= isc_read_addr_normal ;
496
- end // end STATE_UPDATE_DR
502
+ // Actual readout happens here
503
+ STATE_SHIFT_DR : begin
504
+ isc_read_shreg <= isc_read_shreg_adv ;
505
+ end // end STATE_SHIFT_DR
497
506
498
- endcase
507
+ // Update: save the de-Gray-ified read address
508
+ STATE_UPDATE_DR: begin
509
+ config_read_en <= 1 ;
510
+ config_read_addr <= gray_to_bin[isc_read_addr_gray_flipped];
511
+ end // end STATE_UPDATE_DR
512
+
513
+ endcase
514
+ end
499
515
500
516
end
501
517
502
518
// //////////////////////////////////////////////////////////////////////////////////////////////////////////////////
503
519
// Config memory writes
504
520
505
- reg [SHREG_WIDTH + ADDR_BITS - 1 : 0 ] isc_write_shreg = 0 ;
521
+ localparam WRITE_WIDTH = SHREG_WIDTH + ADDR_BITS;
522
+ reg [WRITE_WIDTH - 1 : 0 ] isc_write_shreg = 0 ;
506
523
507
524
// Gray coded write address: left N bits of the shift register
508
525
wire [ADDR_BITS- 1 :0 ] isc_write_addr_gray = isc_write_shreg[SHREG_WIDTH + : ADDR_BITS];
509
526
510
- // Convert Gray code to normal
511
- reg [ADDR_BITS- 1 :0 ] isc_write_addr_normal ;
527
+ // Invert the bit ordering of the address since the protocol is weird and has MSB at right
528
+ reg [ADDR_BITS- 1 :0 ] isc_write_addr_gray_flipped ;
512
529
always @(* ) begin
513
- isc_write_addr_normal[ADDR_BITS- 1 ] <= isc_write_addr_gray[ADDR_BITS- 1 ];
514
- for (i= ADDR_BITS- 2 ; i>= 0 ; i= i- 1 )
515
- isc_write_addr_normal[i] <= isc_write_addr_gray[i] ^ isc_write_addr_normal[i+ 1 ];
530
+ for (i= 0 ; i< ADDR_BITS; i= i+ 1 )
531
+ isc_write_addr_gray_flipped[i] <= isc_write_addr_gray[ADDR_BITS - 1 - i];
516
532
end
517
533
518
534
always @(posedge tck) begin
@@ -529,13 +545,13 @@ module XC2CJTAG(
529
545
530
546
// Read the new bitstream
531
547
STATE_SHIFT_DR: begin
532
- isc_write_shreg <= {tdi, isc_write_shreg[SHREG_WIDTH + ADDR_BITS - 1 : 1 ]};
548
+ isc_write_shreg <= {tdi, isc_write_shreg[WRITE_WIDTH - 1 : 1 ]};
533
549
end // end STATE_SHIFT_DR
534
550
535
551
// Update: commit the write to bitstream
536
552
STATE_UPDATE_DR: begin
537
553
config_write_en <= 1 ;
538
- config_write_addr <= isc_write_addr_normal ;
554
+ config_write_addr <= gray_to_bin[isc_write_addr_gray_flipped] ;
539
555
config_write_data <= isc_write_shreg[SHREG_WIDTH- 1 : 0 ];
540
556
end // end STATE_UPDATE_DR
541
557
0 commit comments