As suspected. I repeat the repeat after me part...
So, repeat after me: loop in verilog does NOT loop temporally. loop in verilog loops spatially.
If you want something that loops in time you should use a counter.
It's a common enough mistake, so no shame there. But it does help to read replies.
What you made there is 8
always blocks that exist and execute
in parallel. I'll post the equivalent to help understand what's going on.
Your code:
Code Verilog - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
| module integers( clock, res);
input clock;
output res;
wire clock;
reg [7:0] res;
reg [7:0] buff = 0;
genvar i;
for (i = 0; i < 8 ; i = i + 1) begin: test
always@ (negedge clock)
begin
buff[i] <= 1;
res <= buff;
end
end
endmodule |
Equivalent code with the generate loop explicitely unrolled:
Code Verilog - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
| module integers( clock, res);
input clock;
output res;
wire clock;
reg [7:0] res;
reg [7:0] buff = 0;
// equivalent for i=0
always @(negedge clock)
begin
buff[0] <= 1;
res <= buff;
end
// equivalent for i=1
always @(negedge clock)
begin
buff[1] <= 1;
res <= buff;
end
// equivalents for i=2..6 go here
// equivalent for i=7
always @(negedge clock)
begin
buff[7] <= 1;
res <= buff;
end
endmodule |
And as you can imagine that is equivalent to:
Code Verilog - [expand] |
1
2
3
4
5
6
7
| ...
// equivalent with all the above always blocks lumped together
always @(negedge clock)
begin
buff <= 8'b11111111;
res <= buff;
end |
And that is why you get that all ones (8'b11111111) result after one clock cycle.
And to repeat the repeated repetition:
If you want something that loops in time you should use a counter.
Hope that clear things up.
PS: Two more minor things. First, it generally gives better results to use spaces instead of tabs when posting code. Four (4) spaces for an indent is a reasonable value IMO. And the other minor thing is that it's considered better style to use this kind of module port declaration:
Code Verilog - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
| module integers(
input clock, // you could comment this, although clock is probably understood well enough
output reg [7:0] res // putting a short description of the "res" signal here will increase code readability
);
reg [7:0] buff = 0;
always @(negedge clock)
begin
buff <= 8'b11111111;
res <= buff;
end
endmodule |
That's a bit shorter, and possibly more readable and maintainable.