【发布时间】:2018-06-08 18:12:12
【问题描述】:
我正在尝试在 Verilog 中执行从某些灰码值到某些二进制值的连续转换。也就是说,我正在尝试获取一个总线上的格雷码值,并不断地将其转换为另一总线上的二进制值。我正在尝试在 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。
但是这段代码已经非常复杂、死板且不可维护。
我怎样才能以干净的方式做到这一点?将来,此代码可扩展也可能很重要。如果您对快速格雷码计数/翻译方案有任何建议,我们也将不胜感激。
【问题讨论】:
标签: verilog hardware synthesis