FPGA初学者__个人学习笔记(三)_选择器、多路复用器

简介

在学习FPGA时,整理的一些问题,慢慢积累,期待自己的进步。
个人邮箱: 1149025224@qq.com
欢迎交流!

2 选 1 选择器

先说什么是选择器?
好几个女同学向你求婚,你选其中一个当老婆。即多输入单输出。

好,做个题
这个模块有输入: a, b, sel,输出: out
sel 信号作为选择信号,当 sel = 1 时选择 b,sel=0 时选择 a。

```py
module top_module( 
    input a, b, sel,
    output out );
    
    assign out = (sel) ? b : a;
    
endmodule

256选1, 位宽 1

什么是位宽?
暂可以理解为一根线有一个电压,可代表1或0,
那么四根线可以有四个电压,可代表0000、0001…直到1110、1111,
那么输入信号是一根线,代表的是一位,位宽就是1,四根线就可以代表四位,位宽为4。

以上如果想深究可以百度一下啦

实现一个 256 选 1 选择器,sel 信号作为选择信号,当 sel = 0 时选择 in[0],sel = 1 时选择 in[1],以此类推。

module top_module (
	input [255:0] in,
	input [7:0] sel,
	output  out
);
	assign out = in[sel];
	
endmodule

9 选 1 选择器,位宽 4

sel 信号作为选择信号,当 sel = 0 时选择 a,sel = 1 时选择 b,以此类推。sel 信号位宽为 4bit,当 sel 大于 8 时,输出 16’hffff。

module top_module( 
    input [15:0] a, b, c, d, e, f, g, h, i,
    input [3:0] sel,
    output reg [15:0] out );
    always @(*)begin
        case(sel)
            4'd0:out = a;
            4'd1:out = b;
            4'd2:out = c;
            4'd3:out = d;
            4'd4:out = e;
            4'd5:out = f;
            4'd6:out = g;
            4'd7:out = h;
            4'd8:out = i;
            default:out=16'hffff;
        endcase
    end
endmodule

256 选 1 选择器,位宽 4

el 信号作为选择信号,当 sel = 0 时选择 in[3:0],sel = 1 时选择 in[7:4],以此类推。

module top_module (
	input [1023:0] in,
	input [7:0] sel,
	output [3:0] out
);

	// We can't part-select multiple bits without an error, but we can select one bit at a time,
	// four times, then concatenate them together.
	assign out = {in[sel*4+3], in[sel*4+2], in[sel*4+1], in[sel*4+0]};

	// Alternatively, "indexed vector part select" works better, but has an unfamiliar syntax:
	// assign out = in[sel*4 +: 4];		// Select starting at index "sel*4", then select a total width of 4 bits with increasing (+:) index number.
	// 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.
	// Note: The width (4 in this case) must be constant.

endmodule

学习总结

位宽较小时使用 assign 语句,三元表达式,case 语句;
位宽较宽时的多路选择器,需要根据需求灵活地使用位选择符 [ ] 或者位连接符{ }

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值