+92 332 4229 857 99ProjectIdeas@Gmail.com

Verilog Code For 8:3 Encoder Using "if-else" | Verilog Example Codes

Encoder is a digital circuit which converts a long data word into a short data word. Encoders are widely used in digital communication between different digital circuits at small scale. Encoding reduces the number of data lines required for the transmission of data. In addition encoders are also used for data encryption.
Verilog example code for 8 to 3 encoder has been implemented below. The implementation makes use of if-else statements. At the end the unconditional else branch sets the value to indeterminate, if any of the conditional statement does not comes true.
module Encoder (code, data);
output [2:0] code
input  [7:0] data;
reg    [2:0] code;

always @ (data)
if(data==8'b00000001) code=0; else
if(data==8'b00000010) code=1; else
if(data==8'b00000100) code=2; else
if(data==8'b00001000) code=3; else
if(data==8'b00010000) code=4; else
if(data==8'b00100000) code=5; else
if(data==8'b01000000) code=6; else
if(data==8'b10000000) code=7; else