limit_trace_capture.v 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. `timescale 1ns/1ps
  2. // -----------------------------------------------------------------------------
  3. // limit_trace_capture
  4. //
  5. // Synchronizes the three raw limit-switch trace inputs and remembers whether a
  6. // rising/falling transition occurred since the last accepted report snapshot.
  7. // The trace firmware reports RAW electrical logic levels; it does not assume
  8. // whether the machine's limit switch is active-high or active-low.
  9. // -----------------------------------------------------------------------------
  10. module limit_trace_capture (
  11. input wire clk,
  12. input wire reset_n,
  13. input wire [2:0] limit_in,
  14. input wire clear_seen,
  15. output wire [2:0] level,
  16. output reg [2:0] rise_seen,
  17. output reg [2:0] fall_seen
  18. );
  19. reg [2:0] limit_meta;
  20. reg [2:0] limit_sync;
  21. reg [2:0] limit_sync_d;
  22. wire [2:0] rise_edge = limit_sync & ~limit_sync_d;
  23. wire [2:0] fall_edge = ~limit_sync & limit_sync_d;
  24. assign level = limit_sync;
  25. always @(posedge clk or negedge reset_n) begin
  26. if (!reset_n) begin
  27. limit_meta <= 3'b000;
  28. limit_sync <= 3'b000;
  29. limit_sync_d <= 3'b000;
  30. end else begin
  31. limit_meta <= limit_in;
  32. limit_sync <= limit_meta;
  33. limit_sync_d <= limit_sync;
  34. end
  35. end
  36. always @(posedge clk or negedge reset_n) begin
  37. if (!reset_n) begin
  38. rise_seen <= 3'b000;
  39. fall_seen <= 3'b000;
  40. end else if (clear_seen) begin
  41. // If an edge occurs in the exact clear cycle, retain it for the
  42. // following snapshot instead of losing it.
  43. rise_seen <= rise_edge;
  44. fall_seen <= fall_edge;
  45. end else begin
  46. rise_seen <= rise_seen | rise_edge;
  47. fall_seen <= fall_seen | fall_edge;
  48. end
  49. end
  50. endmodule