x393  1.0
FPGAcodeforElphelNC393camera
round_robin.v
Go to the documentation of this file.
1 
39 `timescale 1ns/1ps
40 
41 module round_robin #(
42  parameter FIXED_PRIORITY = 0, // 0 - round-robin, 1 - fixed channel priority (0 - highest)
43  parameter BITS = 2 // number of bits to encode channel number (1 << BITS) - number of inputs
44 )(
45  input clk,
46  input srst, // sync. reset - needed to reset current channel
47  input [(1 << BITS) -1:0] rq, // request vector
48  input en, // enable to grant highest priority request (should be reset by grant)
49  output reg grant, // changed to 1-cycle long (was: stays on until reset by !en)
50  output [BITS-1:0] chn,
51  output reg [(1 << BITS) -1:0] grant_chn); // 1-hot grant output per-channel, single-clock pulse
52 
53  reg [BITS-1:0] last_chn;
54  wire valid;
55  wire [BITS-1:0] next_chn;
57  reg grant_r;
58 
59  assign pre_grant_w = en && valid &&!grant_r;
60 // assign grant = grant_r;
61  assign chn = last_chn;
62 
63  assign {valid, next_chn}= func_selrr (rq, FIXED_PRIORITY?((1 << BITS) -1):last_chn);
64  always @ (posedge clk) begin
65  if (srst) last_chn <= (1 << BITS) -1;
66  else if (pre_grant_w) last_chn <= next_chn;
67 
68  if (srst || !en) grant_r <= 0;
69  else if (valid) grant_r <= 1; // grant will stay on until reset by !en
70 
72 
73  grant <= !srst && pre_grant_w;
74 
75  end
76 
77  // round-robin priority encode
78  function [BITS : 0] func_selrr; // returns {valid, chn}
79  input [(1 << BITS) -1:0] rq; // request vector
80  input [BITS-1:0] cur_chn; // current (last served) channel - lowest priority
81  reg valid; // at least one request
82  reg [BITS - 1:0] chn, sample_chn;
83  integer i;
84  begin
85  valid = 0;
86  chn = 0;
87  for (i = 0; i < (1 << BITS); i = i+1) begin
88  sample_chn = (cur_chn - i) % (1 << BITS);
89  if (rq[sample_chn]) begin
90  valid = 1;
91  chn = sample_chn;
92  end
93  end
94  func_selrr = {valid,chn};
95  end
96  endfunction
97 
98  function [(1 << BITS) -1:0] func_demux;
99  input en;
100  input [BITS - 1:0] sel;
101  integer i;
102  begin
103  for (i=0; i < (1 << BITS); i = i + 1) begin
104  func_demux[i] = en && (sel == i);
105  end
106  end
107  endfunction
108 
109 endmodule
10749FIXED_PRIORITY0
Definition: round_robin.v:42
10760next_chnwire[BITS-1:0]
Definition: round_robin.v:55
10761pre_grant_wwire
Definition: round_robin.v:56
10759validwire
Definition: round_robin.v:54
[1 << BITS -1:0] 10753rq
Definition: round_robin.v:47
10762grant_rreg
Definition: round_robin.v:57
reg [1 << BITS -1:0] 10757grant_chn
Definition: round_robin.v:51
[BITS:0] func_selrrrqcur_chn
Definition: round_robin.v:78
[BITS-1:0] 10756chn
Definition: round_robin.v:50
[(1<<BITS)-1:0] func_demuxensel
Definition: round_robin.v:98
reg 10755grant
Definition: round_robin.v:49
10758last_chnreg[BITS-1:0]
Definition: round_robin.v:53