Parameterize code using generate loops

Hello,

I would like to parameterize the following arbiter using `NUM_MEM_CLIENTS. Right now NUM_MEM_CLIENTS is set to four, and the arbiter RTL is hardcoded to match. I would like to be able to change NUM_MEM_CLIENTS to any number (within reason) and create the RTL using some kind of generate loop. Could someone please give me some pointers?

For those that haven’t seen it, $clog2 is a built in function that returns the log2 of a number.

Thanks!
Aaron

parameter c_select_width = $clog2(`NUM_MEM_CLIENTS);
bit [c_select_width-1 : 0] client_select, clnt_select_latch;

always @ (posedge clk or posedge reset)
if (reset == 1’b1) begin
client_select <= 0;
end else begin
case (clnt_select_latch)
0: begin
if (clnt_trans_req[1] == 1’b1) client_select <= 1;
else if (clnt_trans_req[2] == 1’b1) client_select <= 2;
else if (clnt_trans_req[3] == 1’b1) client_select <= 3;
else if (clnt_trans_req[0] == 1’b1) client_select <= 0;
end
1: begin
if (clnt_trans_req[2] == 1’b1) client_select <= 2;
else if (clnt_trans_req[3] == 1’b1) client_select <= 3;
else if (clnt_trans_req[0] == 1’b1) client_select <= 0;
else if (clnt_trans_req[1] == 1’b1) client_select <= 1;
end
2: begin
if (clnt_trans_req[3] == 1’b1) client_select <= 3;
else if (clnt_trans_req[0] == 1’b1) client_select <= 0;
else if (clnt_trans_req[1] == 1’b1) client_select <= 1;
else if (clnt_trans_req[2] == 1’b1) client_select <= 2;
end
3: begin
if (clnt_trans_req[0] == 1’b1) client_select <= 0;
else if (clnt_trans_req[1] == 1’b1) client_select <= 1;
else if (clnt_trans_req[1] == 1’b1) client_select <= 2;
else if (clnt_trans_req[2] == 1’b1) client_select <= 3;
end
default : client_select <= 0;
endcase
end // if (reset…)