Path: csiph.com!x330-a1.tempe.blueboxinc.net!usenet.pasdenom.info!news.albasani.net!feeder.erje.net!border3.nntp.ams.giganews.com!Xl.tags.giganews.com!border1.nntp.ams.giganews.com!nntp.giganews.com!local2.nntp.ams.giganews.com!news.giganews.com.POSTED!not-for-mail NNTP-Posting-Date: Sat, 02 Apr 2011 16:12:41 -0500 From: Chris Hinsley Newsgroups: comp.lang.forth Date: Sat, 2 Apr 2011 22:12:41 +0100 Message-ID: <2011040222124192049-chrishinsley@gmailcom> References: <2011040219361212758-chrishinsley@gmailcom> <2011040220310050490-chrishinsley@gmailcom> <2011040220435062162-chrishinsley@gmailcom> <2011040221362339753-chrishinsley@gmailcom> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1; format=flowed Content-Transfer-Encoding: 8bit Subject: Re: Verilog macro issue User-Agent: Unison/2.1.4 Lines: 68 X-Usenet-Provider: http://www.giganews.com X-Trace: sv3-IZK+f+DDfFGp/hSWYqKfsmbCS8tB/zd4dy25djy97bXjoeScf+RnE7q/Oi4WVllPevNa8NlI+iI9ZwO!6+x9PIn9bVxiM1jmcDRnA5eqJ5F+zYeXxT7GInZZmUF1wSQFriVXuuMHMcmLrfFEq8jA1w== X-Complaints-To: abuse@giganews.com X-DMCA-Notifications: http://www.giganews.com/info/dmca.html X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 2992 Xref: x330-a1.tempe.blueboxinc.net comp.lang.forth:965 On 2011-04-02 21:36:23 +0100, Chris Hinsley said: > This works as a macro to define priority encoders, but still not the > same as the original encoder done with a case. > > `define PRI_ENCODER(NAME, BITS) \ > module NAME(i, o); \ > input [((2 ** BITS) - 1):0] i; \ > output [(BITS - 1):0] o; \ > reg [(BITS - 1):0] o; \ > integer n; \ > always @(i) \ > begin : THE_LOOP \ > o = BITS'b0; \ > for (n = 0; n < (2 ** BITS); n = n + 1) \ > begin \ > if (i[n]) \ > begin \ > o = n; \ > disable THE_LOOP; \ > end \ > end \ > end \ > endmodule > > `PRI_ENCODER(pri_enc3, 3) > `PRI_ENCODER(pri_enc4, 4) > `PRI_ENCODER(pri_enc5, 5) I've ended up with this, which does the correct job, but I really think the code sucks. There has to be a better way to do it than this ! God knows what the FPGA compiler will produce for this. :( `define ENCODER(NAME, BITS) \ module NAME(i, o); \ input [((2 ** BITS) - 1):0] i; \ output [(BITS - 1):0] o; \ reg [(BITS - 1):0] o; \ integer n, f; \ always @(i) \ begin : THE_LOOP \ o = BITS'b0; \ f = 0; \ for (n = 0; n < (2 ** BITS); n = n + 1) \ begin \ if (i[n]) \ begin \ if (f) disable THE_LOOP; \ f = 1; \ end \ end \ for (n = 0; n < (2 ** BITS); n = n + 1) \ begin \ if (i[n]) \ begin \ o = n; \ disable THE_LOOP; \ end \ end \ end \ endmodule `ENCODER(enc1, 1) `ENCODER(enc2, 2) `ENCODER(enc3, 3) `ENCODER(enc4, 4) `ENCODER(enc5, 5)