//本题画出真值表一目了然

`timescale 1ns/1ns
module encoder_83(
   input      [7:0]       I   ,
   input                  EI  ,
   
   output wire [2:0]      Y   ,
   output wire            GS  ,
   output wire            EO    
);
assign Y[2] = EI & (I[7] | I[6] | I[5] | I[4]);
assign Y[1] = EI & (I[7] | I[6] | ~I[5]&~I[4]&I[3] | ~I[5]&~I[4]&I[2]);
assign Y[0] = EI & (I[7] | ~I[6]&I[5] | ~I[6]&~I[4]&I[3] | ~I[6]&~I[4]&~I[2]&I[1]);

assign EO = EI&~I[7]&~I[6]&~I[5]&~I[4]&~I[3]&~I[2]&~I[1]&~I[0];

assign GS = EI&(I[7] | I[6] | I[5] | I[4] | I[3] | I[2] | I[1] | I[0]);
//assign GS = EI&(| I);
         
endmodule

module encoder_164(
   input      [15:0]      A   ,
   input                  EI  ,
   
   output wire [3:0]      L   ,
   output wire            GS  ,
   output wire            EO    
);
   wire     gs0,gs1;
    wire    eo0,eo1;
    wire    [2:0] y0,y1;
    wire    y3;
    
    encoder_83 U1(A[7:0],EI,y0,gs0,eo0);            //A[7:0] 进行8-3优先编码
    encoder_83 U2(A[15:8],EI,y1,gs1,eo1);        //A[15:8]进行8-3优先编码
    
    assign    y3     = (gs1==1 && eo1 == 0)? 1'b1:1'b0;            //输出结果的最高位取决于gs1和eo1,如果gs1 == 1,eo1 == 0则表示处于高位优先状态。
    assign    L     = (gs1==1 && eo1 == 0)? {y3,y1}:{y3,y0};    
    assign     GS    = gs0 | gs1;                                                
    assign    EO     = eo0 & eo1; 
    
endmodule