如何解决5 位奇校验
我正在尝试编写一个 5 位 Mealy 奇偶校验器。我编写了一些代码(复位、下一个状态逻辑和输出逻辑),除了检查 5 位输出的部分。如果 1'b1 的个数为奇数,如何检查 5 位变量?
module parity(clk,rst,w,z);
input clk,w;
reg [1:0] present,next;
output reg z;
parameter s0 = 4'b0000,s1 = 4'b0001,s2 = 4'b0010,s3 = 4'b0011,s4 = 4'b0100,s5 = 4'b0101,s6 = 4'b0110,s7 = 4'b0111,s8 = 4'b1000,s9 = 4'b1001,s10 = 4'b1010;
//Reset
always @(posedge clk,posedge rst)
begin
if(rst)
present <= s0;
else
present <= next;
end
//Next State logic
always @(present,w)
begin
case(present)
s0: if(~w) next = s1; else next = s2;
s1: if(~w) next = s3; else next = s4;
s2: if(~w) next = s4; else next = s3;
s3: if(~w) next = s5; else next = s6;
s4: if(~w) next = s6; else next = s5;
s5: if(~w) next = s7; else next = s8;
s6: if(~w) next = s8; else next = s7;
s7: if(~w) next = s9; else next = s10;
s8: if(~w) next = s10; else next = s9;
s9: if(~w) next = s0; else next = s0;
s10: if(~w) next = s0; else next = s0;
default: next = s0;
endcase
end
always @(w,present)
begin
if (present == s10 && w == 1)
z = 1'b1;
else
z = 1'b0;
end
endmodule
解决方法
如果你有一个 5 位变量,比如
reg [4:0] data;
您将通过使用一元 ^
(XOR 约简)运算符获得奇偶校验:
odd = ^data
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。