Advertisement
Guest User

Untitled

a guest
Mar 18th, 2017
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. `timescale 1ns/1ps
  2.  
  3. module counter_test;
  4.  
  5. int start;
  6. logic clk,rst;
  7. logic [3:0] result;
  8.  
  9. initial begin                                              
  10.     clk=0;                 
  11.     forever #10 clk = ~clk;
  12. end
  13.  
  14.    
  15.        
  16. initial
  17. begin
  18.     start = 6;
  19.     rst=0;     
  20.     #10 rst=1;     
  21.     repeat(16) #20 $strobe("Counter value: %d", result);
  22.     #100 rst=0;    
  23.     #10 rst=1;     
  24.     #100 $stop;
  25. end
  26.  
  27. bin_cnt test_dev(clk, rst,start,  result);
  28.  
  29. endmodule
  30.  
  31.  
  32.  
  33.  
  34. module bin_cnt
  35. (
  36.     input logic clk, resetn,
  37.     int start,
  38.     output logic [3:0] count
  39. );
  40.  
  41. int it;
  42. logic cond;
  43.  
  44.     always @ (posedge clk or negedge resetn)
  45.     begin
  46.        
  47.         if (!resetn)
  48.             begin
  49.                 count <= 0;
  50.                 it = 0;
  51.                 cond = 1;
  52.             end
  53.         else
  54.             if(cond)
  55.             if (count==(start-it))
  56.                 begin
  57.                     if((start-it)<=(it+1))
  58.                         begin
  59.                             cond = 0;
  60.                             count <= count - 1;
  61.                         end
  62.                     else
  63.                     count <= it++;
  64.                     //it++;
  65.                 end
  66.             else
  67.                 begin
  68.                     count <= count + 1;
  69.                 end
  70.     end
  71.  
  72. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement