Verilog 中的一种热编码
one hot encoding in Verilog
我刚刚开始学习如何使用 Verilog 进行编码。谁能帮我弄清楚如何使用单热编码在 verilog 中实现以下代码
module Controller(b, x, clk, rst);
input b, clk, rst;
output x;
reg x;
parameter Off = 2'b00,
On1 = 2'b01,
On2 = 2'b10,
On3 = 2'b11;
reg [1:0] currentstate;
reg [1:0] nextstate;
//state register
always @ (posedge rst or posedge clk)
begin
if(rst==1)
currentstate <= Off;
else
currentstate <= nextstate;
end
//combinational
always @ (*)
begin
case (currentstate)
Off: begin
x <= 0;
if(b==0)
nextstate <= Off;
else
nextstate <= On1;
end
On1 : begin
x <= 1;
nextstate <= On2;
end
On2 : begin
x <= 1;
nextstate <= On3;
end
On3 : begin
x <= 1;
nextstate <= Off;
end
endcase
end
我尝试将参数更改为:
parameter Off = 4'b0001,
On1 = 4'b0010,
On2 = 4'b0100,
On3 = 4'b1000;
但是,我了解到这不是一个好的实现。
FSM 中 onehot 编码的一些优点如下:
- Low switching activity. Since only single bit is switched at a time, the power consumption is less and it is less prone to glitches.
- Simplified encoding. One can determine the state just by looking at the bit position of '1' in the current state variable.
这种技术的缺点是它需要更多的触发器。因此,如果一个 FSM 具有 10 个不同的状态,则需要 10 个触发器,而使用十进制编码时只需要 4 个触发器。
针对您的问题,更改为onehot编码的FSM很简单。需要根据currentstate
变量中1
的位置实现一条case
语句。代码片段可以实现如下:
parameter Off = 2'b00,
On1 = 2'b01,
On2 = 2'b10,
On3 = 2'b11;
//...
always @ (*)
begin
nextstate = 4'b0000;
case (1'b1)
currentstate[Off] : begin
x = 0;
if(b==0)
nextstate[Off] = 1'b1;
else
nextstate[On1] = 1'b1;
end
currentstate[On1] : begin
x = 1;
nextstate[On2] = 1'b1;
end
//...
this link and explanation is over here. Cummings paper 提供了一个简单示例,也是获取更多信息的良好来源。
编辑:正如@Greg 所指出的,这是一个copy-paste 错误。组合块必须使用 阻塞赋值 。
我刚刚开始学习如何使用 Verilog 进行编码。谁能帮我弄清楚如何使用单热编码在 verilog 中实现以下代码
module Controller(b, x, clk, rst);
input b, clk, rst;
output x;
reg x;
parameter Off = 2'b00,
On1 = 2'b01,
On2 = 2'b10,
On3 = 2'b11;
reg [1:0] currentstate;
reg [1:0] nextstate;
//state register
always @ (posedge rst or posedge clk)
begin
if(rst==1)
currentstate <= Off;
else
currentstate <= nextstate;
end
//combinational
always @ (*)
begin
case (currentstate)
Off: begin
x <= 0;
if(b==0)
nextstate <= Off;
else
nextstate <= On1;
end
On1 : begin
x <= 1;
nextstate <= On2;
end
On2 : begin
x <= 1;
nextstate <= On3;
end
On3 : begin
x <= 1;
nextstate <= Off;
end
endcase
end
我尝试将参数更改为:
parameter Off = 4'b0001,
On1 = 4'b0010,
On2 = 4'b0100,
On3 = 4'b1000;
但是,我了解到这不是一个好的实现。
FSM 中 onehot 编码的一些优点如下:
- Low switching activity. Since only single bit is switched at a time, the power consumption is less and it is less prone to glitches.
- Simplified encoding. One can determine the state just by looking at the bit position of '1' in the current state variable.
这种技术的缺点是它需要更多的触发器。因此,如果一个 FSM 具有 10 个不同的状态,则需要 10 个触发器,而使用十进制编码时只需要 4 个触发器。
针对您的问题,更改为onehot编码的FSM很简单。需要根据currentstate
变量中1
的位置实现一条case
语句。代码片段可以实现如下:
parameter Off = 2'b00,
On1 = 2'b01,
On2 = 2'b10,
On3 = 2'b11;
//...
always @ (*)
begin
nextstate = 4'b0000;
case (1'b1)
currentstate[Off] : begin
x = 0;
if(b==0)
nextstate[Off] = 1'b1;
else
nextstate[On1] = 1'b1;
end
currentstate[On1] : begin
x = 1;
nextstate[On2] = 1'b1;
end
//...
this link and explanation is over here. Cummings paper 提供了一个简单示例,也是获取更多信息的良好来源。
编辑:正如@Greg 所指出的,这是一个copy-paste 错误。组合块必须使用 阻塞赋值 。