// A 4-to-1 multiplexer: sel picks which of the four inputs reaches y.
module mux4 (
input wire [1:0] sel,
input wire d0,
input wire d1,
input wire d2,
input wire d3,
output reg y
);
always @(*) begin
case (sel)
2'd0: y = d0;
2'd1: y = d1;
2'd2: y = d2;
default: y = d3;
endcase
end
endmodule
Description
A selector picks one of four inputs. It comes with a test that builds with Yosys and runs in the browser. Set d2 to 1, then change sel to 2 and watch y follow it.
Cite this design
@design{breadboardhub/4-to-1-mux-in-verilog,
title = {4-to-1 mux in Verilog},
author = {breadboardhub},
url = {https://www.breadboardhub.com/breadboardhub/4-to-1-mux-in-verilog},
version = {1.0.0}
}