From bfcdec1d8305356c652de835de27be75b399f2de Mon Sep 17 00:00:00 2001 From: Koray Yanik Date: Tue, 3 Oct 2023 22:31:39 +0100 Subject: [PATCH] Add a simple watchdog timeout --- sim/tb_top.sv | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/sim/tb_top.sv b/sim/tb_top.sv index d966703..1740881 100644 --- a/sim/tb_top.sv +++ b/sim/tb_top.sv @@ -102,5 +102,44 @@ assign selected_memory_implemented = hiram_sel | vram_sel | ppu_sel; current_pc, last_write_address, last_write_value) ); +// Watchdog timer +localparam HIST_SIZE = 8; +localparam MAX_COUNT = 10000; +logic [15:0] pc_history [$]; +int unsigned pc_history_count [$]; + +always @(posedge clk iff instr_valid) begin + automatic int unsigned current_pc_count = 0; + + foreach (pc_history[i]) begin + if (current_pc == pc_history[i]) begin + // History contains current PC, remove from history but keep counter + current_pc_count = pc_history_count[i]; + pc_history.delete(i); + pc_history_count.delete(i); + break; + end + end + + // Put in front of history queue + pc_history.push_front(current_pc); + pc_history_count.push_front(current_pc_count + 1); + + // Prune back of history + if (pc_history.size() > HIST_SIZE) begin + void'(pc_history.pop_back()); + void'(pc_history_count.pop_back()); + end + + assert (current_pc_count < MAX_COUNT) else begin + $display($sformatf("[%0t] Watchdog timeout", $time)); + $display("History:"); + foreach (pc_history[i]) begin + $display($sformatf("0x%X: %d", pc_history[i], pc_history_count[i])); + end + $fatal(); + end +end + `endif /* SVA_ENABLE */ endmodule : tb_top