首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >将Verilog Case语句与连续赋值一起使用

将Verilog Case语句与连续赋值一起使用
EN

Stack Overflow用户
提问于 2018-06-09 02:12:12
回答 1查看 4.7K关注 0票数 2

我正在尝试在Verilog中执行从一些Greycode值到一些二进制值的连续转换。也就是说,我试图从一条总线上获取一个Greycode值,然后在另一条总线上不断地将其转换为二进制值。我正尝试在Verilog中做到这一点(由于其他原因,不能使用SystemVerilog )。

我想做这样的事情:

wire [DATA_SIZE - 1:0] binary_snap;
always @(greycode_snap) begin
case (greycode_snap)
    8'b00000000 : binary_snap = 0;
    8'b00000001 : binary_snap = 1;
    8'b00000011 : binary_snap = 2;
    8'b00000111 : binary_snap = 3;
    8'b00001111 : binary_snap = 4;
    8'b00011111 : binary_snap = 5;
    8'b00111111 : binary_snap = 6;
    8'b01111111 : binary_snap = 7;
    8'b11111111 : binary_snap = 8;
    8'b11111110 : binary_snap = 9;
    8'b11111100 : binary_snap = 10;
    8'b11111000 : binary_snap = 11;
    8'b11110000 : binary_snap = 12;
    8'b11100000 : binary_snap = 13;
    8'b11000000 : binary_snap = 14;
    8'b10000000 : binary_snap = 15;
    default     : binary_snap = 0;
endcase
end

其中greycode_snap是一个wire

正如您可能已经猜到的,这不会合成并产生错误:

Procedural assignment to a non-register binary_snap is not permitted, left-hand side should be reg/integer/time/genvar

我不想将binary_snap更改为reg,所以接下来我尝试这样做:

wire [DATA_SIZE - 1:0] binary_snap;
assign binary_snap = (greycode_snap == 8'b00000001) ? 1 :
                     (greycode_snap == 8'b00000011) ? 2 :
                     (greycode_snap == 8'b00000111) ? 3 :
                     (greycode_snap == 8'b00001111) ? 4 :
                     (greycode_snap == 8'b00011111) ? 5 :
                     (greycode_snap == 8'b00111111) ? 6 :
                     (greycode_snap == 8'b01111111) ? 7 :
                     (greycode_snap == 8'b11111111) ? 8 :
                     (greycode_snap == 8'b11111110) ? 9 :
                     (greycode_snap == 8'b11111100) ? 10 :
                     (greycode_snap == 8'b11111000) ? 11 :
                     (greycode_snap == 8'b11110000) ? 12 :
                     (greycode_snap == 8'b11100000) ? 13 :
                     (greycode_snap == 8'b11000000) ? 14 :
                     (greycode_snap == 8'b10000000) ? 15 : 0;

同样,greycode_snap是一个wire

这是编译器接受的,但根据我(有限的)经验,我相信这会合成一些粗俗的东西(取决于使用的合成工具)。所以我尝试了一些优化:

wire [DATA_SIZE - 1:0] binary_snap;
assign binary_snap = (greycode_snap[0] == 0) ?
                            (greycode_snap[4] == 0) ?
                                (greycode_snap[2] == 0) ?
                                    (greycode_snap[1] == 0) ? 1 : 2
                                : //else
                                    (greycode_snap[3] == 0) ? 3 : 4
                            : // else
                                (greycode_snap[6] == 0) ?
                                    (greycode_snap[5] == 0) ? 5 : 6
                                : // else
                                    (greycode_snap[7] == 0) ? 7 : 8
                        : // else
                            (greycode_snap[4] == 1) ?
                                (greycode_snap[2] == 1) ?
                                    (greycode_snap[1] == 1) ? 9 : 10
                                : //else
                                    (greycode_snap[3] == 1) ? 11 : 12
                            : // else
                                (greycode_snap[6] == 1) ?
                                    (greycode_snap[5] == 1) ? 13 : 14
                                : // else
                                    (greycode_snap[7] == 1) ? 15 : 0;

同样,greycode_snap是一个wire

但是这段代码已经非常复杂、死板,而且不可维护。

我怎样才能以一种干净的方式来做这件事呢?将来这段代码是可扩展的,这一点可能也很重要。如果您有任何与快速greycode计数/转换方案相关的建议,也将不胜感激。

EN

回答 1

Stack Overflow用户

发布于 2018-06-09 04:21:02

如果将binary_snap的声明从wire更改为reg,则代码可以正常工作。但是,如果将其放入函数中,则可以将其保留为wire并使用case语句。

wire [DATA_SIZE - 1:0] binary_snap;
assign binary_snap = f(greycode);
function [DATA_SIZE - 1:0] f(input [7:0] reg code);
case (code)
    8'b00000000 : f = 0;
    8'b00000001 : f = 1;
    8'b00000011 : f = 2;
    8'b00000111 : f = 3;
    8'b00001111 : f = 4;
    8'b00011111 : f = 5;
    8'b00111111 : f = 6;
    8'b01111111 : f = 7;
    8'b11111111 : f = 8;
    8'b11111110 : f = 9;
    8'b11111100 : f = 10;
    8'b11111000 : f = 11;
    8'b11110000 : f = 12;
    8'b11100000 : f = 13;
    8'b11000000 : f = 14;
    8'b10000000 : f = 15;
    default     : f = 0;
endcase
endfunction
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50766295

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档