Q010FreeSystemVerilog
Optimize nonadjacent bank activation
Interview prompt
Question
Choose a maximum-benefit subset of static random-access memory (SRAM) banks when adjacent banks may not be active in the same maintenance interval. Return the total score and exact selection mask.
Candidate starting point
Implementation scaffold
module adjacent_bank_optimizer (
input logic clk,
input logic rst_n,
input logic in_valid,
input logic in_last,
input logic [7:0] benefit,
input logic result_ready,
output logic in_ready,
output logic result_valid,
output logic [11:0] max_score,
output logic [15:0] select_mask,
output logic [4:0] bank_count
);
typedef enum logic [1:0] {S_INPUT, S_SEARCH, S_RESULT} state_t;
state_t state_q;
logic [7:0] benefit_q [0:15];
logic [4:0] write_idx_q;
logic [16:0] enum_q, limit_q;
logic [11:0] best_score_q, result_score_q;
logic [15:0] best_mask_q, result_mask_q;
logic [4:0] best_count_q, result_count_q;
logic cand_legal, cand_better, lower_index_preferred;
logic [11:0] cand_score;
logic [4:0] cand_count;
logic first_difference;
always_comb begin : evaluate_candidate
// TODO: Implement evaluate_candidate using the supplied state and interface.
end
assign in_ready = (state_q == S_INPUT);
assign result_valid = (state_q == S_RESULT);
assign max_score = result_score_q;
assign select_mask = result_mask_q;
assign bank_count = result_count_q;
always_ff @(posedge clk) begin : load_search_and_publish
// TODO: Implement load_search_and_publish using the supplied state and interface.
end
endmoduleReviewed example
Trace one case
Input
benefit by bank = [5, 1, 4, 9, 2]Expected output
max_score=14; select_mask=16'h0009; bank_count=2Banks 0 and 3 are nonadjacent and total 14, which exceeds every other legal subset.
What to cover
Requirements
- Accept a frame of 1 to 16 unsigned eight-bit benefits; the first and last banks are not adjacent.
- Select no adjacent pair and maximize the total benefit.
- On a score tie, prefer fewer selected banks; if still tied, prefer the mask that selects the first differing lower-index bank.
- Clear mask bits above the frame length and return the selected-bank popcount.
- Hold the final score, mask, and count stable while stalled.
