创建一个1位全加器_HDLBits—笔记、难点、错题(1-5)

Problem one Mux256to1

Circuit→Combinational logic→Multiplexer

遇到问题

该题目如果按照 2-to-1和9-to-1这样的思路的话,写一个由256个case的语句就会很累赘。

解答与分析

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

	// Select one bit from vector in[]. The bit being selected can be variable.
	assign out = in[sel];
        //assign out = in >> sel;
	
endmodule

这里我们可以根据题目的要求,结合选择运算符的特性实现。根据提示:选择运算符的 index 可以为变量,只要变量的位宽和向量的长度匹配即可。

所以我们直接将 sel ,这个变量,作为片选向量 in 的 index。

根据题意,也可以将输入向量 in 右移 sel 位,高位被截去,输出最低位上的 in[sel]。同样,移位的长度也可以使用变量。

Problem two Mux256to1v

Circuit→Combinational logic→Multiplexer

题干

Create a 4-bit wide, 256-to-1 multiplexer. The 256 4-bit inputs are all packed into a single 1024-bit input vector. sel=0 should select bits in[3:0], sel=1 selects bits in[7:4], sel=2 selects bitsin[11:8], etc.

遇到问题

 module top_module( 
    input [1023:0] in,
    input [7:0] sel,
    output [3:0] out );
//错误代码1
assign out[3:0] = in[(sel*4)+3 : sel*4];

//错误代码2
  /*  always @(*)begin
        for(int sel = 0;sel<256;sel++)begin
            out[3:0] = in[(sel*4)+3 : sel*4];
        end
    end*/

//正确代码
assign out = {
    in[sel*4+3], in[sel*4+2], in[sel*4+1], in[sel*4+0]};
endmodule
  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值