题解 | #用优先编码器①实现键盘编码电路#
用优先编码器①实现键盘编码电路
https://www.nowcoder.com/practice/03b8c5837d7f406797b4a57358057ef7
`timescale 1ns/1ns module encoder_0( input [8:0] I_n , output reg [3:0] Y_n ); always @(*)begin casex(I_n) 9'b111111111 : Y_n = 4'b1111; 9'b0xxxxxxxx : Y_n = 4'b0110; 9'b10xxxxxxx : Y_n = 4'b0111; 9'b110xxxxxx : Y_n = 4'b1000; 9'b1110xxxxx : Y_n = 4'b1001; 9'b11110xxxx : Y_n = 4'b1010; 9'b111110xxx : Y_n = 4'b1011; 9'b1111110xx : Y_n = 4'b1100; 9'b11111110x : Y_n = 4'b1101; 9'b111111110 : Y_n = 4'b1110; default : Y_n = 4'b1111; endcase end endmodule module key_encoder( input [9:0] S_n , output wire[3:0] L , output wire GS ); wire [3:0] L1; encoder_0 e( . I_n(S_n[9:1]) , . Y_n(L1) ); reg GS1; reg [3:0] LL; always@(*) begin if ((&S_n[9:1])&S_n[0]) GS1=0; else begin GS1=1; if(L1==4'b0110) LL=4'd9; else if(L1==4'b0111) LL=4'd8; else if(L1==4'b1000) LL=4'd7; else if(L1==4'b1001) LL=4'd6; else if(L1==4'b1010) LL=4'd5; else if(L1==4'b1011) LL=4'd4; else if(L1==4'b1100) LL=4'd3; else if(L1==4'b1101) LL=4'd2; else if(L1==4'b1110) LL=4'd1; else if(S_n[0]==0) LL=4'd0; end end assign L=LL; assign GS=GS1; endmodule