2017-09-29 84 views
0

我想分配一个二维数组(特定的位[代码[我] [K])的值。这是一种网络类型。但未分配的值reg [3:0]代码[0:3]的值未知逻辑值为'X'。将值分配给二维解压数组中的特定位[系统 - verilog]

这里是代码片段

  for(k=0;k<len;k++) begin 
       if (tc[k] == 1'b0) begin 
        code[i][k]= 1'b0;//----> value is not assigning as expected 
       end else begin 
        code[i][k]= 1'b1;// ---> value is not assigning as expected 
       end 
      end 
      codeLen[i] = len; 

这for循环属于总是 block.Here,代码codeLen是输出类型。

output [3:0] code[0:3]; 
output [3:0] codeLen[0:3]; 
reg [3:0] code[0:3]; 
reg [3:0] codeLen[0:3]; 

codeLen [I]正确但不是代码[I] [K]分配。我试图分配第i个字节的第k位。

详细信息 我已经创建了一个需要6个输入并返回两个2维数组作为输出的模块。

这里是模块:

`timescale 1ns/1ps 
module generate_code(CLK,nRST,nodes,nodeCount,characters,charCount,code,codeLen); 
input CLK; 
input nRST; 
input integer nodeCount;//Total nodes in huffman tree 
input integer charCount;//Total unique characters 
input [6:0] characters[0:3]; 
input [23:0] nodes[0:6]; // total characters 

output [3:0] code[0:3]; //[2:0] max code length <= total characters 
output [3:0] codeLen[0:3]; 

reg [3:0] code[0:3]; 
reg [3:0] codeLen[0:3]; 

reg[3:0] tc;//temprary code reg. Holds a single bit in each byte 
integer len=0;//code length 
reg [23:0] tNode; 

function void FindRoot; 
    reg [23:0] aNode;//local 
    integer i; 
    begin 
    for (i=0; i<nodeCount;i++) begin // For all nodes 
     aNode= nodes[i]; // aNode is current node 
     if (tNode[23:16] == aNode[14:7]) begin 
      tc[len]= tNode[15];//15th bit of nodes is codebit 
      len++; 
     //aNode is parent of tNode. Is it root? 
      if(aNode[23:16]==8'b0000_0000) begin//or frequency==nodeCount or node_id = 8'b1111_1111 
       return; 
      end else begin 
       tNode=aNode; 
       FindRoot(); 
      end 
     end 
    end 
    end 
endfunction 

[email protected](posedge CLK or negedge nRST) 
begin 
    if(!nRST) begin 
    // init 
    end 
    else begin 
     // Do code generation 
     integer i,j,k; 
     for(i= 0;i < charCount;i++) begin // For all character we are going to find codeword 
      for(j=0; j<nodeCount; j++) begin 
       tNode= nodes[j];//current node 
       if (characters[i] == tNode[6:0]) begin 
       // Got the character. tNode is a leaf nodes. Lets back track to root. 
        break; 
       end 
      end 
      len=0; 
      FindRoot(); 
      for(k=0;k<len;k++) begin 
       if (tc[k] == 1'b0) begin 
        code[i][k]= 1'b0; 
       end else begin 
        code[i][k]= 1'b1; 
       end 
      end 
      //code[i]=2; 
      codeLen[i]= len; 
     end 
    end 
end 
endmodule 

当我分配值进行编码[] [],预期下面的循环被执行。虽然不是代码[] []的所有位都将被设置。在调试过程中,当我开始分配时,我发现该值没有被分配(代码[i] [k] = 1或0)。它获得未知逻辑值X.

   for(k=0;k<len;k++) begin 
       if (tc[k] == 1'b0) begin 
        code[i][k]= 1'b0; 
       end else begin 
        code[i][k]= 1'b1; 
       end 
      end 

测试平台:

`timescale 1ns/1ps 
module generate_code_test; 

// Inputs 
reg CLK; 
reg nRST; 
integer nodeCount=7;//Total nodes in huffman tree 
integer charCount=4;//Total unique characters 
reg [6:0] characters[0:3]; 
reg [23:0] nodes[0:6]; // total characters 

// Outputs 
wire [3:0] code[0:3]; //[2:0] max code length <= total characters 
wire [3:0] codeLen[0:3]; 

generate_code uut (
    .CLK(CLK), 
    .nRST(nRST), 
    .nodes(nodes), 
    .nodeCount(nodeCount), 
    .characters(characters), 
    .charCount(charCount), 
    .code(code), 
    .codeLen(codeLen) 
); 

initial begin 
    // Initialize Inputs 
    CLK = 0; 
    nRST = 0; 
    nodeCount= 7; 
    charCount= 4; 
    characters[0]= 7'b110_0001; 
    characters[1]= 7'b110_0010; 
    characters[2]= 7'b110_0011; 
    characters[3]= 7'b110_0100; 

    nodes[0] = 24'b0000_0011_0_0000_0001_110_0001; 
    nodes[1] = 24'b0000_0011_1_0000_0010_110_0011; 
    nodes[2] = 24'b0000_0101_1_0000_0011_111_1111; 
    nodes[3] = 24'b0000_0101_0_0000_0100_110_0010; 
    nodes[4] = 24'b1111_1111_1_0000_0101_111_1111; 
    nodes[5] = 24'b1111_1111_0_0000_0110_110_0100; 
    nodes[6] = 24'b0000_0000_0_1111_1111_111_1111; 

    // Wait 10 ns for global reset to finish 
    #10; 
    nRST = 1; 

end 
parameter DELAY = 1; 
always 
    #DELAY CLK = ~CLK; 

endmodule 

的代码已经在ModelSim的2016 编译我刚开始学习的Verilog。所以我会很感激你的帮助来表明我的错误。 此致敬礼。

+0

欢迎来到Stack Overflow。我怀疑这种行为是由你没有发布的代码造成的。例如,什么是'k'和'​​len' - 它可能是你的循环被执行了零次?请发布[MCVE](https://stackoverflow.com/help/mcve) - 一小段可编译代码 - 以便其他人可以重现您的问题。 –

+0

@MatthewTaylor,谢谢你的回应。我已经上传了可编译模块以及测试平台。我相信,当我分配代码[] []时,循环被执行。 k和len在当时应该有必要的价值。虽然我没有设置代码[] []的所有位。当我调试模块来到那个任务时,我发现行被执行,但是代码[] []没有被赋值。 – rakibdana

+1

您的代码无法编译 - 请参阅[http://www.edaplayground.com/x/4uk4](http://www.edaplayground.com/x/4uk4)。我修好了,然后模拟没有停止。你可以(a)编译它(MCVE中的“C”)和(b)拿出一些额外的东西,只留下问题(MCVE中的“M”)? –

回答

0

我为我的问题得到了解决。并非代码[] []的所有位都已设置。即使在设置该位之后,这会导致代码[] []中的未知逻辑值。在总是块中初始化代码[] []的所有位后,它会得到解决。

相关问题