2015-06-13 131 views
0

我有一些問題的verilog,無法解決它。嘗試了不同的改變,但仍然無法解決。Verilog仿真x的輸出

代碼:

module Perpetual_Calender(); 

reg [3:0] year[277:0]; //14 different calendars can exist for 2033-1755 = 288 years 
reg [2:0] month[3:0][3:0]; //different calenders for combination of year and month 
reg [2:0] day [2:0][4:0]; //different days for combination of calender and day of month. 

reg Error; 

reg[0:3] c; reg[0:2] f, g; 

integer i,j; 

// fot Year 0 = A, 1 = B... 13 = N 

    task Show_corresponding_day; 
    input integer m; 
    input integer d; 
    input integer y; 
    begin 

     Error = 0; 

     $display("# (m/d/y) %d/%d/%d = ",m,d,y); 

     if(y<1755 || y>2033 || m<1 || m>12 || d<1 || d>31) 
     Error = 1; 

    if(!Error) begin 
    c = year[y-1755]; 
    f = month[c][m]; 

    $display("c = %d, f = %d", c, f); 

    if(d > 29 + month[c][m+1] - (f+1)%7) 
     Error = 1; 

    if(!Error) 
    g = day[f][d]; 

    $display("g = %d", g); 
    end 

    case({Error, g}) 

    4'd1: $display("Monday\n"); 
    4'd2: $display("Tuesday\n"); 
    4'd3: $display("Wednesday\n"); 
    4'd4: $display("Thrusday\n"); 
    4'd5: $display("Friday\n"); 
    4'd6: $display("Saturday\n"); 
    4'd7: $display("Sunday\n"); 
    default: $display("ERROR\n"); 

    endcase 

    end 
    endtask 

initial begin 

year[0] = 4'd2; 

for(i = 1756; i<=2033; i=i+1) begin 

    if(year[i-1756] > 6) 
    year[i-1755] = (year[i-1756]+2)%7; 
else 
    year[i-1755] = (year[i-1756]+1)%7; 

    j = i%4; 
    if(i != 1800 && i != 1900 && j == 0) 
    year[i-1755] = year[i-1756]+7; 
end 

for(i = 0; i<7; i=i+1) begin 

    month[i][1] = i; month[i][2] = (i+31)%7; 

    month[i][3] = (month[i][2]+28)%7; month[i][4] = (month[i][3]+31)%7; $display("m = %b, n = %b\n", month[i][4], (month[i][3]+31)%7); 

    month[i][5] = (month[i][4]+30)%7; month[i][6] = (month[i][5]+31)%7; 

    month[i][7] = (month[i][6]+30)%7; month[i][8] = (month[i][7]+31)%7; 

    month[i][9] = (month[i][8]+31)%7; month[i][10] = (month[i][9]+30)%7; 

    month[i][11] = (month[i][10]+31)%7; month[i][12] = (month[i][11]+30)%7; 

    month[i][13] = (month[i][12]+31)%7; // used only in checking for errors 

end 



for(i = 7; i<14; i=i+1) begin 

    month[i][1] = i%7; month[i][2] = (i+31)%7; 

    month[i][3] = (month[i][2]+29)%7; month[i][4] = (month[i][3]+31)%7; 

    month[i][5] = (month[i][4]+30)%7; month[i][6] = (month[i][5]+31)%7; 

    month[i][7] = (month[i][6]+30)%7; month[i][8] = (month[i][7]+31)%7; 

    month[i][9] = (month[i][8]+31)%7; month[i][10] = (month[i][9]+30)%7; 

    month[i][11] = (month[i][10]+31)%7; month[i][12] = (month[i][11]+30)%7; 

    month[i][13] = (month[i][12]+31)%7; // used only in checking for errors  

end 

for(i = 0; i <7; i=i+1) begin 

    for(j=1; j<32; j=j+1) begin 

    day[i][j] = (i+j-1)%7+1; 


end 

end 

Show_corresponding_day(7,31,2001); 
Show_corresponding_day(1,25,1987); 
Show_corresponding_day(4,31,2008); 
Show_corresponding_day(2,29,2005); 
Show_corresponding_day(6,30,2013); 
Show_corresponding_day(13,27,2013); 
Show_corresponding_day(11,30,1001); 
Show_corresponding_day(3,27,2012); 

end 

endmodule 

輸出部分:

# m = xxx, n = 00000000000000000000000000000110 
# 
# m = xxx, n = 00000000000000000000000000000000 
# 
# m = xxx, n = 00000000000000000000000000000001 
# 
# m = xxx, n = 00000000000000000000000000000010 
# 
# m = xxx, n = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
# 
# m = xxx, n = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 
# 
# m = xxx, n = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 

爲何或如何X的產出?模塊總計正確顯示,但寄存器的輸出僅爲x。

回答

1

的小例子來說明這個問題:

module tb; 
    reg [2:0] month[3:0][3:0]; 

    initial begin 
    month[0][2] = 3'b011; 
    month[0][3] = (month[0][2]+28)%7; 
    month[0][4] = (month[0][3]+31)%7; 

    $display("2: %b", month[0][2]); 
    $display("3: %b", month[0][3]); 
    $display("4: %b", month[0][4]); 
    end 

endmodule 

你的問題是month[0][4]當定義是reg [2:0] month[3:0][3:0];

您可以通過地址month[0][0] upto month[3][3],任何超過此範圍的內容超出範圍。

另請注意,當我運行完整的代碼時,由於此原因,我得到了許多數組索引越界警告。

您至少要定義:reg [2:0] month[6:0][13:1];它也更常見交換陣列的比特的順序,即:

reg [2:0] month[0:6][1:13]; 
+0

噢,我可能已經0之間弄亂:3,其可以meen 4位比奈其當它用作輸入或位置大小時,可以保存0到15之間的數字,當它用於表示4個不同的位置而不是16個不同的數字時。謝謝。 – mj1261829

+0

@ mj1261829它也引起了我很多時間,你指定你想使用的位置,而不是位來表示這些數字。 – Morgan