Advertisement
seethesatyrrise

Untitled

Jun 22nd, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module bin_cnt
  2. (
  3. input logic clk, resetn,
  4. input logic [2:0] MAX,
  5. input logic [2:0] MIN,
  6. output logic [2:0] count
  7. );
  8.  
  9. logic direction;
  10.  
  11. always_ff @ (posedge clk or negedge resetn)
  12. begin
  13. if (!resetn)
  14.     begin
  15.     //prev_mode <= mode;
  16.     count <= 0;
  17.     direction <= 1;
  18.     end
  19. else if (count>=MAX)
  20.     begin
  21.     direction <= 0;
  22.     count <= count - 1;
  23.     end
  24. else if (count<=MIN)
  25.     begin
  26.     //prev_mode <= mode;
  27.     direction <= 1;
  28.     count <= count + 1;
  29.     end
  30. else
  31.     begin
  32.     if(direction)
  33.         begin
  34.         count <= count + 1;
  35.         end
  36.     else
  37.         begin
  38.         count <= count - 1;
  39.         end
  40. end
  41. end
  42.  
  43. endmodule
  44.  
  45.  
  46.  
  47. `timescale 1ns/1ps
  48. module counter_test;
  49. integer i;
  50. logic clk, resetn;
  51. logic [2:0] cnt;
  52. logic [2:0] MAX;
  53. logic [2:0] MIN;
  54.  
  55. initial begin
  56. clk=0;
  57. MIN= 3'b010;
  58. MAX= 3'b110;
  59. forever #10 clk = ~clk;
  60. end
  61.  
  62. initial
  63. begin
  64. resetn=0;
  65. #10 resetn=1;
  66.  
  67. #400 $stop;
  68. end
  69. bin_cnt uut_inst(clk, resetn, MAX, MIN, cnt);
  70. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement