led_blink_mod.v 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. // vim: ts=4 sw=4 noexpandtab
  2. /*
  3. * LED blinker
  4. *
  5. * Copyright (c) 2019 Michael Buesch <m@bues.ch>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. */
  21. `ifndef LED_BLINK_MOD_V_
  22. `define LED_BLINK_MOD_V_
  23. module led_blink #(
  24. parameter BLINK_ON_CLKS = 1024,
  25. parameter BLINK_OFF_CLKS = 1024,
  26. ) (
  27. input clk,
  28. input n_reset,
  29. input enable,
  30. output reg led,
  31. );
  32. reg [31:0] led_on_count;
  33. reg [31:0] led_off_count;
  34. initial begin
  35. led <= 0;
  36. led_on_count <= 0;
  37. led_off_count <= 0;
  38. end
  39. always @(posedge clk) begin
  40. if (n_reset) begin
  41. if (led) begin
  42. if (led_on_count == 0) begin
  43. led <= 0;
  44. led_off_count <= BLINK_OFF_CLKS;
  45. end else begin
  46. led_on_count <= led_on_count - 1;
  47. end
  48. end else begin
  49. if (led_off_count == 0) begin
  50. if (enable) begin
  51. led <= 1;
  52. led_on_count <= BLINK_ON_CLKS;
  53. end
  54. end else begin
  55. led_off_count <= led_off_count - 1;
  56. end
  57. end
  58. end else begin
  59. led <= 0;
  60. led_on_count <= 0;
  61. led_off_count <= 0;
  62. end
  63. end
  64. endmodule
  65. `endif /* LED_BLINK_MOD_V_ */