2012年10月25日 星期四

P6-70 2digit BCD counter verilog + Modelsim simulation

 //2digit BCD counter verilog + Modelsim simulation

 //top-level module for 2-digit BCD counter 00-->99
// 
module bcdcounter(clk,reset,one_out,ten_out);
  input clk,reset;
  output [3:0] one_out,ten_out;
  
  wire nreset,ones_en,tens_en,tc_ones;  //tc_ones --> 1's carry out
  wire [3:0] one_out , ten_out;
  

  // module cnt_10(ce,clk,clr,tc,qout);
  cnt_10 _1s (.ce(ones_en),
             .clk(clk),
             .clr(nreset),
             .tc(tc_ones),    //1's carry out
             .qout(one_out));
  
  cnt_10 _10s (.ce(tens_en),
             .clk(clk),
             .clr(nreset),
             .tc(),    //10's carry out  ignore
             .qout(ten_out));
            
            
  assign tens_en=tc_ones;
  assign ones_en =1'b1;
  assign nreset=~reset;
            
endmodule

//===========================================


//BCD counter counts 0 to 9 and then rolls over
module cnt_10(ce,clk,clr,tc,qout);
  input ce,clk,clr;
  output tc;  //carry out
  output [3:0] qout;
  
  reg [3:0] count;
  always @(posedge clk or posedge clr)
  begin
    if (clr)  //clear 
        count <=4'b0;
    else 
    if (ce) 
      if (count==4'h9)
          count <=4'h0;
      else    
          count <= count +1;
  end        
  
  assign qout = count ;
  assign tc=(count==4'h9);   //if count=9 then carry out=1
endmodule

//============================================

module testbench();
  //inputs 
  reg clk,reset;
  
  //outputs
  wire [3:0] one_out ,ten_out;
  
  //Instantiate UUT
  bcdcounter UUT (.clk(clk),
                  .reset(reset),
                  .one_out(one_out),
                  .ten_out(ten_out));
                  
  //initial inputs
  initial 
  $monitor ($time,"clk=%b, reset=%b ,ten_out=%d , onr_out=%d",clk,reset,ten_out,one_out);
  
  initial 
  begin 
    clk=0;
    reset=0;
    #35 reset=1;
  end 
  
  initial 
  begin
    forever #10 clk=~clk;
  end
  
  initial 
  #400 $finish;
  
endmodule
  //============================================


沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...