Verilog simulation x's in output

3.5k views Asked by At

I Have some problem verilog and cannot resolve it. Tried different changes but still no solution.

The code:

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 

Part of output:

# 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

Why or how x's in output? the modules sum displays correctly but output of register is only x.

1

There are 1 answers

2
Morgan On BEST ANSWER

The are small example to show the issue:

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

The problem you have is month[0][4] when the definition is reg [2:0] month[3:0][3:0];

You can address month[0][0] upto month[3][3], anything past this is out of range.

Also note that when I run your full code I get many array index out of bounds warnings due to this.

You at least want to define: reg [2:0] month[6:0][13:1]; it is also more common to swap the order of array bits ie:

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