Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Judging if a year is a leap year in Verilog code

Status
Not open for further replies.

xofun

Newbie level 3
Joined
May 26, 2005
Messages
4
Helped
1
Reputation
2
Reaction score
0
Trophy points
1,281
Activity points
1,348
Judge a leap year

Hi,all:
It is easy to achieve it in C code:

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

I want to transfer the C code to verilog code. Is there a function aviliable to judge a year can be divided exactly by 4,100,or 400?By right shift "year" with code "year>>1" for twice time,it is OK if i get 2 zeros.Then how about 100 and 400?

Best regards
 

Re: Judge a leap year

Here is the verilog code!

Code:
/*    leap years are all the years divisible by 4, 
      except that years divisible by 100 are not leap years, 
      but years divisible by 400 are leap years. 

Encoding the year:

      binary ? easy for divisible by 4,  
      but difficult for 100 and 400 (not powers of 2)

      BCD ? easy for 100, 
      but more difficult for 4, what about 400?

Parts:
      construct a circuit that determines if the year is divisible by 4
      construct a circuit that determines if the year is divisible by 100
      construct a circuit that determines if the year is divisible by 400
      combine the results of the previous three steps to yield the leap year flag

Reference :
       Contemporary logic design Randy H. Katz 2nd ed Chapter 5 
*/
 

module leap(/*AUTOARG*/
   // Outputs
   leap_year_flag, 
   // Inputs
   year
   );
   input [15:0] year; // years in 4 digit bcd
   output      leap_year_flag;

   wire        div_by_100, div_by_400;
   reg         div_by4;
   
   /* Divisible-by-4 circuit 
    Only need to look at low-order two digits of the year 
    all years ending in 00, 04, 08, 12, 16, 20, etc. are divisible by 4

    if tens digit is even, then divisible by 4 if ones digit is 0, 4, or 8
    if tens digit is odd, then divisible by 4 if the ones digit is 2 or 6.  
    Translates into the following Boolean expression 
    */
   always @(/*AS*/year) 
     if (year[4]) begin  //tens digit is odd
        div_by4 = (year[3:0] == 4'h2) | (year[3:0] == 4'h6);
     end else begin
        div_by4 = (year[3:0] == 4'h0) | (year[3:0] == 4'h4) | (year[3:0] == 4'h8);
     end

   /*
    Divisible-by-100 just requires checking that all bits of two low-order digits are all 0: 
    */
   assign      div_by_100 = (year[7:4] == 4'h0) & (year[3:0] == 4'h0);
   
   /*
    Divisible-by-400 combines the divisible-by-4 
    (applied to the thousands and hundreds digits) 
    and divisible-by-100 circuits 
    */
   assign      div_by_400 = div_by_100 & div_by4;

   assign      leap_year_flag = div_by4 & (~div_by_100) | div_by_400;
   
endmodule // leap

Hope this helps!
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top