【HDLBits刷题】Gatesv.

You are given a four-bit input vector in[3:0]. We want to know some relationships between each bit and its neighbour:

  • out_both: Each bit of this output vector should indicate whether both the corresponding input bit and its neighbour to the left (higher index) are '1'. For example, out_both[2] should indicate if in[2] and in[3] are both 1. Since in[3] has no neighbour to the left, the answer is obvious so we don't need to know out_both[3].
  • out_any: Each bit of this output vector should indicate whether any of the corresponding input bit and its neighbour to the right are '1'. For example, out_any[2] should indicate if either in[2] or in[1] are 1. Since in[0] has no neighbour to the right, the answer is obvious so we don't need to know out_any[0].
  • out_different: Each bit of this output vector should indicate whether the corresponding input bit is different from its neighbour to the left. For example, out_different[2] should indicate if in[2] is different from in[3]. For this part, treat the vector as wrapping around, so in[3]'s neighbour to the left is in[0].

1、第一种是每个位置进行计算或者说操作。代码如下: 

module top_module( 
    input [3:0] in,
    output [2:0] out_both,
    output [3:1] out_any,
    output [3:0] out_different );
    assign out_both = {in[3]&in[2],in[2]&in[1],in[0]&in[1]};
    assign out_any = {in[3]|in[2],in[2]|in[1],in[0]|in[1]};
    assign out_different = {in[3]^in[0],in[3]^in[2],in[2]^in[1],in[0]^in[1]};
endmodule

 2、第二种是将整个向量按位操作(在最后一位时,由于是进行循环检查,需要另起一行,赋值首尾检查数值以及相应位置),代码如下:

module top_module( 
    input [3:0] in,
    output [2:0] out_both,
    output [3:1] out_any,
    output [3:0] out_different );
    assign out_both = in[3:1]&in[2:0];  // {in[3]&in[2],in[2]&in[1],in[0]&in[1]};
    assign out_any = in[3:1]|in[2:0];  // {in[3]|in[2],in[2]|in[1],in[0]|in[1]};
    assign out_different[2:0] = in[3:1]^in[2:0];  // {in[3]^in[0],in[3]^in[2],in[2]^in[1],in[0]^in[1]};
    assign out_different[3] = in[3]^in[0];
endmodule

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值