bmc_ascii_snapshot_tb.v 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. `timescale 1ns/1ps
  2. module bmc_ascii_snapshot_tb;
  3. reg clk = 0;
  4. reg rst_n = 0;
  5. reg tick = 0;
  6. reg rx_valid = 0;
  7. reg [7:0] rx_data = 0;
  8. reg framing = 0;
  9. wire commit;
  10. wire error;
  11. wire [15:0] epoch;
  12. wire [15:0] age;
  13. wire stale;
  14. wire active_bank;
  15. wire [23:0] valid_mask;
  16. reg control_bank;
  17. reg [4:0] control_index;
  18. wire [15:0] control_raw;
  19. wire control_valid;
  20. reg snapshot_bank;
  21. reg [4:0] snapshot_index;
  22. wire [15:0] snapshot_raw;
  23. wire snapshot_valid;
  24. reg saw_commit = 0;
  25. reg saw_error = 0;
  26. integer i;
  27. always #5 clk = ~clk;
  28. always @(posedge clk) begin
  29. if (commit) saw_commit <= 1;
  30. if (error) saw_error <= 1;
  31. end
  32. bmc_ascii_snapshot #(.STALE_SECONDS(2)) dut (
  33. .clk(clk), .rst_n(rst_n), .tick_1s(tick), .rx_valid(rx_valid),
  34. .rx_data(rx_data), .rx_framing_error(framing), .snapshot_lock(1'b0),
  35. .snapshot_lock_bank(1'b0), .snapshot_commit(commit),
  36. .parser_error(error), .epoch(epoch), .age_seconds(age), .stale(stale),
  37. .active_bank(active_bank), .active_valid_mask(valid_mask),
  38. .control_bank(control_bank), .control_index(control_index), .control_raw(control_raw),
  39. .control_valid(control_valid), .snapshot_bank(snapshot_bank),
  40. .snapshot_index(snapshot_index), .snapshot_raw(snapshot_raw), .snapshot_valid(snapshot_valid)
  41. );
  42. task send_char;
  43. input [7:0] value;
  44. begin
  45. @(negedge clk); rx_data = value; rx_valid = 1;
  46. @(negedge clk); rx_valid = 0;
  47. end
  48. endtask
  49. function [7:0] hex_char;
  50. input [3:0] value;
  51. input lower;
  52. begin
  53. if (value < 10) hex_char = "0" + value;
  54. else if (lower) hex_char = "a" + value - 10;
  55. else hex_char = "A" + value - 10;
  56. end
  57. endfunction
  58. task send_byte;
  59. input [7:0] value;
  60. input lower;
  61. begin
  62. send_char(hex_char(value[7:4], lower));
  63. send_char(hex_char(value[3:0], lower));
  64. send_char(" ");
  65. end
  66. endtask
  67. initial begin
  68. control_bank = 0;
  69. control_index = 0;
  70. snapshot_bank = 0;
  71. snapshot_index = 0;
  72. repeat (3) @(negedge clk);
  73. rst_n = 1;
  74. repeat (26) @(negedge clk);
  75. for (i = 0; i < 23; i = i + 1) begin
  76. send_byte(8'd25 + i, i[0]);
  77. send_byte(8'd50, !i[0]);
  78. end
  79. send_byte(8'hff, 1);
  80. send_byte(8'hff, 0);
  81. send_char(8'h0d);
  82. repeat (2) @(negedge clk);
  83. if (epoch !== 1 || !saw_commit || valid_mask !== 24'h7fffff) begin
  84. $display("commit mismatch epoch=%0d valid=%h", epoch, valid_mask);
  85. $fatal;
  86. end
  87. control_bank = active_bank;
  88. control_index = 0;
  89. @(posedge clk); #1;
  90. if (!control_valid || control_raw !== 16'h1932) $fatal;
  91. control_index = 23;
  92. @(posedge clk); #1;
  93. if (control_valid || control_raw !== 16'hffff) $fatal;
  94. send_byte(8'd30, 0);
  95. send_byte(8'd100, 0);
  96. for (i = 1; i < 24; i = i + 1) begin send_byte(8'd30, 0); send_byte(8'd0, 0); end
  97. send_char(8'h0a);
  98. repeat (2) @(negedge clk);
  99. if (epoch !== 1 || !saw_error) $fatal;
  100. tick = 1; @(negedge clk); tick = 0;
  101. tick = 1; @(negedge clk); tick = 0;
  102. if (!stale) $fatal;
  103. $display("PASS: bmc_ascii_snapshot_tb");
  104. $finish;
  105. end
  106. endmodule