在我的脑海中,我想做的是接受8个1位的输入并数1,然后表示那些1。
01010111应该输出0101 (从输入有5个1)
module 8to4 (in,out,hold,clk,reset);
input [7:0] in; //1 bit inputs
reg [7:0] hold; //possible use for case statement
output [3:0] out; //Shows the count of bits
always @(clk)
begin
out = in[0] + in[1] + in[2] + in[3] + in[4] + in[5] + in[6] + in[7]; //Adds the inputs from testbench and outputs it
end
endmodule问题:
我对verilog非常陌生,所以我甚至还不想考虑一个测试平台。
发布于 2017-10-13 05:56:17
您编写它的方式可能是更好的编写它的方法,因为它使参数化位数变得更容易。但严格来说,你有一个8位的输入。
module 8to4 #(parameter WIDTH=8) (input [WIDTH-1:0] in,
output reg [3:0] out,hold,
input clk,reset);
reg [WIDTH-1:0] temp;
integer ii;
always @(clk)
begin
temp = 0;
for(ii=0; ii<WIDTH; i = i + 1)
temp = temp + in[ii];
out <= temp;
end
endmodule发布于 2017-10-14 01:23:06
从逻辑上讲,代码是正确的。
但是,您可以像下面这样改进它。
out作为reg,因为您在过程分配中使用它。reset的使用。理想情况下,任何代码都应该具有重置状态,这在您的代码中是缺失的。hold、clk和reset端口的方向(输入/输出)。parameters作为代码。https://stackoverflow.com/questions/46721548
复制相似问题