当前位置:   article > 正文

verilog 学习笔记——MULtiplexers 多路复用器_41多路复用器 verilog

41多路复用器 verilog

1. 2-to-1 multiplexer

  1. module top_module(
  2. input a, b, sel,
  3. output out );
  4. assign out = (sel & b) | (~sel & a); // Mux expressed as AND and OR
  5. // Ternary operator is easier to read, especially if vectors are used:
  6. // assign out = sel ? b : a;
  7. endmodule

 2. 2-to-1 bus multiplexer

  1. module top_module(
  2. input [99:0] a, b,
  3. input sel,
  4. output [99:0] out );
  5. assign out = (sel==1)?b:a;
  6. // The following doesn't work. Why?
  7. // sel 只有一位,a和b 都是100位的
  8. // assign out = (sel & b) | (~sel & a);
  9. endmodule

3. 9-to-1 multiplexer

  1. module top_module(
  2. input [15:0] a, b, c, d, e, f, g, h, i,
  3. input [3:0] sel,
  4. output [15:0] out );
  5. always@(*)
  6. begin
  7. case(sel)
  8. 4'b0000 : out = a ;
  9. 4'b0001 : out = b ;
  10. 4'b0010 : out = c ;
  11. 4'b0011 : out = d ;
  12. 4'b0100 : out = e ;
  13. 4'b0101 : out = f ;
  14. 4'b0110 : out = g ;
  15. 4'b0111 : out = h ;
  16. 4'b1000 : out = i ;
  17. default : out = 16'b1111111111111111;
  18. endcase
  19. end
  20. endmodule

或者可以这样表达:

 

  1. module top_module (
  2. input [15:0] a,
  3. input [15:0] b,
  4. input [15:0] c,
  5. input [15:0] d,
  6. input [15:0] e,
  7. input [15:0] f,
  8. input [15:0] g,
  9. input [15:0] h,
  10. input [15:0] i,
  11. input [3:0] sel,
  12. output logic [15:0] out
  13. );
  14. // Case statements can only be used inside procedural blocks (always block)
  15. // This is a combinational circuit, so use a combinational always @(*) block.
  16. always @(*) begin
  17. out = '1; // '1 is a special literal syntax for a number with all bits set to 1.
  18. // '0, 'x, and 'z are also valid.
  19. // I prefer to assign a default value to 'out' instead of using a
  20. // default case.
  21. case (sel)
  22. 4'h0: out = a;
  23. 4'h1: out = b;
  24. 4'h2: out = c;
  25. 4'h3: out = d;
  26. 4'h4: out = e;
  27. 4'h5: out = f;
  28. 4'h6: out = g;
  29. 4'h7: out = h;
  30. 4'h8: out = i;
  31. endcase
  32. end
  33. endmodule

4. 256-to-1 multiplexer

  1. module top_module (
  2. input [255:0] in,
  3. input [7:0] sel,
  4. output out
  5. );
  6. // Select one bit from vector in[]. The bit being selected can be variable.
  7. assign out = in[sel];
  8. endmodule

 5. 256-to-1 4-bit multiplexer

  1. module top_module (
  2. input [1023:0] in,
  3. input [7:0] sel,
  4. output [3:0] out
  5. );
  6. // We can't part-select multiple bits without an error, but we can select one bit at a time,
  7. // four times, then concatenate them together.
  8. assign out = {in[sel*4+3], in[sel*4+2], in[sel*4+1], in[sel*4+0]};
  9. // Alternatively, "indexed vector part select" works better, but has an unfamiliar syntax:
  10. // assign out = in[sel*4 +: 4]; // Select starting at index "sel*4", then select a total width of 4 bits with increasing (+:) index number.
  11. // assign out = in[sel*4+3 -: 4]; // Select starting at index "sel*4+3", then select a total width of 4 bits with decreasing (-:) index number.
  12. // Note: The width (4 in this case) must be constant.
  13. endmodule

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/盐析白兔/article/detail/603085
推荐阅读
相关标签
  

闽ICP备14008679号