如何在不关心的情况下参数化 case 语句? [英] How to parameterize a case statement with don't cares?

查看:35
本文介绍了如何在不关心的情况下参数化 case 语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一条名为 input 的线,我想检测前导的数量我正在尝试创建一个模块,该模块使用下面的 case 语句根据前导零的数量更改输出数据.但是输入的大小是可参数化的.

I have a wire called input and I want to detect the number of leading I am trying to create a module which uses the case statement below to change the output data depending on the number of leading zeros. However the size of the input is parameterizable.

如果 X 是一个固定值 4,我会创建一个 case 语句,

If X was a fixed value of 4, I would just create a case statement,

case (input)
  4'b0001  : o_data = {i_data[0]};
  4'b001x  : o_data = {i_data[1],1'b0};
  4'b01xx  : o_data = {i_data[2],2'b0};
  4'b1xxx  : o_data = {i_data[3],3'b0};
  default  : o_data = 4'b0000;
endcase

但是对于变量 X,我如何定义所有情况?

But with variable X, how do I define all cases?

这个问题和这个类似:如何定义一个参数化使用 SystemVerilog 的多路复用器

推荐答案

你真的不能像那样参数化 case 语句,但你可以使用 for 循环相反:

You can't really parameterize a case statement like that, but you can use a for loop instead:

module lead_detector #(parameter int WIDTH = 4) (
  input logic[WIDTH - 1:0] in,
  output logic[WIDTH - 1:0] out
);
  always_comb begin
    out = '0;
    for (int i = WIDTH - 1; i >= 0; i--)
      if (in[i] == 1'b1) begin
        out[i] = 1;
        break;
      end
  end
endmodule

我看到我的设计师一直在写这种代码(尽管是用 VHDL 编写的),但它应该是可综合的.

This is the kind of code I see my designers write all the time (albeit in VHDL), but it should be synthesizable.

这篇关于如何在不关心的情况下参数化 case 语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆