// tag_alloc.sv — hands out the lowest free tag; BUG=1 injects a real-world mistake module tag_alloc #( parameter int N = 4, parameter bit BUG = 1'b0 ) ( input logic clk, input logic rst, input logic alloc_req, output logic alloc_gnt, output logic [$clog2(N)-1:0] alloc_tag, input logic free_vld, input logic [$clog2(N)-1:0] free_tag, output logic [N-1:0] free_mask ); localparam int TW = $clog2(N); // lowest free tag (tag 0 when none is free) always_comb begin alloc_tag = '0; for (int i = N - 1; i >= 0; i--) if (free_mask[i]) alloc_tag = TW'(i); end // the bug: forgetting to check that a tag is actually free assign alloc_gnt = BUG ? alloc_req : (alloc_req && (free_mask != '0)); always_ff @(posedge clk) begin if (rst) free_mask <= '1; else free_mask <= (free_mask & ~({N{alloc_gnt}} & (N'(1) << alloc_tag))) | ({N{free_vld}} & (N'(1) << free_tag)); end endmodule