stimulus_atomic_bank_tb.v 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. `timescale 1ns/1ps
  2. module stimulus_atomic_bank_tb;
  3. reg clk = 0;
  4. reg rst_n = 0;
  5. reg write_enable = 0;
  6. reg [4:0] write_address = 0;
  7. reg [7:0] write_data = 0;
  8. reg stimulus_commit = 0;
  9. wire commit_applied;
  10. wire active_bank;
  11. reg read_bank = 0;
  12. reg [4:0] read_address = 0;
  13. wire [7:0] read_data;
  14. integer i;
  15. always #5 clk = ~clk;
  16. stimulus_atomic_bank dut (
  17. .clk(clk), .rst_n(rst_n), .write_enable(write_enable),
  18. .write_address(write_address), .write_data(write_data),
  19. .stimulus_commit(stimulus_commit), .commit_applied(commit_applied),
  20. .active_bank(active_bank),
  21. .read_bank(read_bank), .read_address(read_address), .read_data(read_data)
  22. );
  23. task write_inactive;
  24. input [4:0] address;
  25. input [7:0] data;
  26. begin
  27. @(negedge clk);
  28. write_enable = 1;
  29. write_address = address;
  30. write_data = data;
  31. @(negedge clk);
  32. write_enable = 0;
  33. end
  34. endtask
  35. task commit_bank;
  36. begin
  37. @(negedge clk);
  38. stimulus_commit = 1;
  39. @(negedge clk);
  40. stimulus_commit = 0;
  41. end
  42. endtask
  43. task expect_active;
  44. input [7:0] base;
  45. begin
  46. read_bank = active_bank;
  47. for (i = 0; i < 32; i = i + 1) begin
  48. read_address = i[4:0];
  49. #1;
  50. if (read_data !== base + i[7:0]) begin
  51. $display("FAIL: active[%0d]=%0d expected=%0d", i, read_data, base + i);
  52. $fatal(1);
  53. end
  54. end
  55. end
  56. endtask
  57. initial begin
  58. repeat (2) @(negedge clk);
  59. rst_n = 1;
  60. // A complete legal command stages vector A without exposing a byte.
  61. for (i = 0; i < 32; i = i + 1)
  62. write_inactive(i[4:0], 8'd10 + i[7:0]);
  63. if (active_bank !== 0)
  64. $fatal(1, "inactive staging changed pointer before commit");
  65. commit_bank();
  66. if (active_bank !== 1) $fatal(1, "first commit did not flip pointer");
  67. expect_active(8'd10);
  68. // An invalid/partial command may dirty the inactive bank, but without
  69. // stimulus_commit it cannot alter either the pointer or active data.
  70. for (i = 0; i < 12; i = i + 1)
  71. write_inactive(i[4:0], 8'd80 + i[7:0]);
  72. // A raw commit after a partial command is rejected inside the bank.
  73. commit_bank();
  74. if (active_bank !== 1) $fatal(1, "partial command changed pointer");
  75. expect_active(8'd10);
  76. // The next complete legal command overwrites all 32 inactive bytes;
  77. // one commit exposes the entire B vector atomically.
  78. for (i = 0; i < 32; i = i + 1)
  79. write_inactive(i[4:0], 8'd40 + i[7:0]);
  80. expect_active(8'd10);
  81. commit_bank();
  82. if (active_bank !== 0) $fatal(1, "second commit did not flip pointer");
  83. expect_active(8'd40);
  84. $display("PASS: stimulus atomic bank inactive staging and pointer commit");
  85. $finish;
  86. end
  87. endmodule