tmr.v 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. `include "counter.v"
  2. `include "injector.v"
  3. module tmr #(parameter CYCLES_SEC = 100_000_000) (
  4. input clk,
  5. input reset_n,
  6. input [2 : 0] apply,
  7. input [2 : 0] inject,
  8. input [2 : 0] mode,
  9. output select,
  10. output fault
  11. );
  12. // save toggled state for each counter
  13. (* keep="true" *) wire [2 : 0] toggled;
  14. (* keep="true" *) wire [2 : 0] tampered;
  15. // triple modular redundancy: at least two out of three bits set
  16. assign select = (tampered[1] & tampered[0]) | (tampered[2] & tampered[1]) | (tampered[2] & tampered[0]);
  17. // if one bit is different, we have a fault
  18. // detects both kinds of faults, but stuck at only during high phases
  19. // triggers reconfigurator
  20. assign fault = ^tampered == 0;
  21. injector injector_inst (
  22. .clk(clk),
  23. .reset_n(reset_n),
  24. .apply(apply),
  25. .inject(inject),
  26. .mode(mode),
  27. .original(toggled),
  28. .tampered(tampered)
  29. );
  30. counter #(.CYCLES_SEC (CYCLES_SEC)) counter_inst_0 (
  31. .clk(clk),
  32. .reset_n(reset_n),
  33. .toggled(toggled[0])
  34. );
  35. counter #(.CYCLES_SEC (CYCLES_SEC)) counter_inst_1 (
  36. .clk(clk),
  37. .reset_n(reset_n),
  38. .toggled(toggled[1])
  39. );
  40. counter #(.CYCLES_SEC (CYCLES_SEC)) counter_inst_2 (
  41. .clk(clk),
  42. .reset_n(reset_n),
  43. .toggled(toggled[2])
  44. );
  45. endmodule