Advertisement
wrandall

verilog

Mar 15th, 2020
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module clock_divider
  2. {
  3.     //fill out the i/o
  4.     //inputs
  5.     input hw_clk, // hardware clock
  6.     input [9:0] tp, // toggle period // max 1000
  7.     input rst, // reset signal
  8.     //outputs
  9.     output reg clk_out; // clock output
  10. };
  11.  
  12. parameter FHP = 0;
  13. parameter SHP = 1;
  14.  
  15. //declare a counter signal to count elapsed hardware clock periods (assume that the counter signal is initialized to 0)
  16.  
  17. reg state = FHP;
  18. reg [10:0] counter = 0; // max 2000
  19.  
  20. always @ (posedge hw_clk or posedge rst)
  21. begin
  22.     //write code to increment the counter
  23.     if(rst == 1'b1)
  24.     begin
  25.         counter <= 1;
  26.     end
  27.    
  28.     else if(rst == 1'b0)
  29.     begin
  30.         counter <= counter + 1;
  31.     end
  32. end
  33.  
  34. always @ (posedge hw_clk or posedge rst)
  35. begin
  36.     //write the state machine
  37.     if(rst == 1'b1)
  38.     begin
  39.         state <= FHP;
  40.     end
  41.    
  42.     else if(rst == 1'b0)
  43.     begin
  44.         case (state)
  45.         begin
  46.             FHP:
  47.             begin
  48.                 if(counter == tp)
  49.                 begin
  50.                     state <= SHP
  51.                 end
  52.                 clk_out <= 1b'0;
  53.             end
  54.  
  55.             SHP:
  56.             begin
  57.                 if(counter == (tp << 2)) // twice tp
  58.                 begin
  59.                     counter <= 0;
  60.                     state <= FHP;
  61.                 end
  62.                 clk_out <= 1b'1;
  63.             end
  64.         end
  65.     end
  66. end
  67.  
  68. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement