Search code examples
verilogdataflow

Finding the number of days in a month


I am making a program to display the no. of days in the month provided by user. I am making this program at Data Flow level. As I am new to verilog, I don't know if we can use if/else conditions or case statement in data flow level. because using if/else statement will make this program piece of cake. If not how can I implement the following idea in data flow level.

if(month==4 || month==6 || month==9|| month==11)

      days=30;
 else
    if(month==2 && leapyear==1)
        days=29;

Here is my verilog incomplete code:

 module LeapYear(year,month,leapOrNot,Days);

 input year,month;

 output leapOrNot,Days;



 //if (year % 400 == 0) || ( ( year % 100 != 0) && (year % 4 == 0 ))
 leapOrNot=((year&400)===0) && ((year % 100)!==0 || (year & 4)===0);

  Days=((month & 4)===4 ||(month & 6)===6 ||(month & 9)===9 ||(month & 11)===11 )

Solution

  • You cannot use if/else in a continuous assignment, but you can use the conditional operator, which is functionally equivalent.

    Try this:

    assign Days = (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 
                  (month == 2 && leapyear == 1) ? 29;
    

    That will produce what you put in your question. But's its not the correct answer as you are missing the conditions where Days is equal to 28 or 31.

    EDIT: Here's how to combine all the conditions into a single assign statement using the conditional operator.v

    assign Days = (month == 4 || month == 6 || month == 9 || month == 11) ? 30 : 
                  (month == 2 && leapyear == 1) ? 29 :
                  (month == 2 && leapyear == 0) ? 28 :
                  31;