Hazard3/hdl/arith/hazard3_priority_encode.v

47 lines
1.2 KiB
Coq
Raw Normal View History

/*****************************************************************************\
2022-06-09 07:12:01 +08:00
| Copyright (C) 2021-2022 Luke Wren |
| SPDX-License-Identifier: Apache-2.0 |
\*****************************************************************************/
2021-05-21 09:34:16 +08:00
// Really something like this should be in a utility library (or the language!),
2021-05-21 10:46:29 +08:00
// but Hazard3 is supposed to be self-contained
2021-05-21 09:34:16 +08:00
`default_nettype none
2021-05-21 10:46:29 +08:00
module hazard3_priority_encode #(
2021-05-21 09:34:16 +08:00
parameter W_REQ = 16,
parameter W_GNT = $clog2(W_REQ) // do not modify
) (
input wire [W_REQ-1:0] req,
output wire [W_GNT-1:0] gnt
);
// First do a priority-select of the input bitmap.
reg [W_REQ-1:0] gnt_onehot;
2021-05-21 09:34:16 +08:00
always @ (*) begin: smear
integer i;
for (i = 0; i < W_REQ; i = i + 1)
gnt_onehot[i] = req[i] && ~|(req & ~({W_REQ{1'b1}} << i));
2021-05-21 09:34:16 +08:00
end
// As the result is onehot, we can now just OR in the representation of each
// encoded integer.
reg [W_GNT-1:0] gnt_accum;
always @ (*) begin: encode
reg [W_GNT:0] i;
2021-05-21 09:34:16 +08:00
gnt_accum = {W_GNT{1'b0}};
for (i = 0; i < W_REQ; i = i + 1) begin
gnt_accum = gnt_accum | ({W_GNT{gnt_onehot[i]}} & i[W_GNT-1:0]);
end
end
assign gnt = gnt_accum;
2021-05-21 10:46:29 +08:00
endmodule
`default_nettype wire