The following code you wrote:
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
31
32
33
34
35
36
37
38
| always @ (posedge clk or negedge rst) begin
if (!rst) begin
clk_out <= 1'bz;
count <= 8'b0;;
end else begin
count <= count + 1; // count is incremented here with begin end block and is controlled by clock //
end
if (count > 0 && count < m) begin
// is this if statement not controlled by clock???? why after incrementing count
above this is not getting executed???? and about multiple drivers for clock_out
there must be 4 comparators generating clk_out signal thats why multiple drivers
is my understanding right???? because if
ststements are sequential. because once count is incremented will it not checking
all if sequentially???? because i am thinking that way...i dont know c
programming so please bear with my doubts....why are begin - end blocks
mainly used...in books it is mentioned if u have multiple statements use
begin - end blocks//
clk_out <=1'b0;
end else if (count >l && count < n) begin
clk_out <=1'b1;
end
if (count > o) begin
count <= 8'b0;
end else begin
count <= count;
end
end
assign baud_clk = clk_out;
endmodule |
Is equivalent to the following:
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
31
32
| // code of first if is independent of the other parallel if statements
always @ (posedge clk or negedge rst) begin
if (!rst) begin
clk_out <= 1'bz;
count <= 8'b0;;
end else begin
count <= count + 1; // count is incremented here with begin end block and is controlled by clock //
end
end
// second if statement is in parallel with first if statement, but this one conflicts
// with the previous one as it shares an output with the other if (multiple driver error).
always @ (posedge clk or negedge rst) begin
if (count > 0 && count < m) begin
clk_out <=1'b0;
end else if (count > l && count < n) begin
clk_out <=1'b1;
end
end
// third if that is in parallel with previous two if statements.
// this also shares a output with the first if statement and will also result in
// a multiple driver error
always @ (posedge clk or negedge rst) begin
if (count > o) begin
count <= 8'b0;
end else begin
count <= count;
end
end
endmodule |
You seem to be trying to think of Verilog scheduling and execution of code as an imperative program. As Verilog represents a description of hardware: registers, logic gates, memories, and interconnect (wires), it isn't an imperative programming language. All your if statements in your edited version are executed in parallel on each positive edge of the clk or the negative edge of rst, but as you are attempting to set the value of both count and clk_out from different processes it has a multiple driver issue and doesn't simulate correctly.
To simplify your thinking only have one output for any always block and that always block should only have a single statement, which means you don't need begin-end on the always block.
e.g.
Code Verilog - [expand] |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
| always @ (posedge clk or negedge rst)
// priority encoded if statement rst has highest priority, and count < 30 has the lowest,
// resetting count back to 0 is the default
if (!rst) begin // poor name choice should make it obvious that it is active-low e.g. rstb, rstn, rst_n, rst_b, etc
count <= 0;
end else if (count == 5) begin // if count is 5 then it jumps to 7
count <= 7;
end else if (count < 12) begin // as long as count is less than 12 but not 5 it increments by 1
count <= count + 1;
end else if (count < 30) begin // as long as count is less than 30 not equal to 5 but more than 12 it increments by 2
count <= count + 2;
end else begin // once count reaches 30 or more it will reset back to 0 and start over again.
count <= 0;
end |
As you can see all of the assignments for count are in a single always block not distributed across multiple if statements.
What you were thinking was that the statements would follow the imperative programming paradigm where any instance of an assignment to count following the first if statement would be immediately executed in sequential order after the first if statement was executed, that doesn't represent any kind of hardware.
You should be thinking of a hardware counter as represented (described) by a register (flip-flops) that holds the current count value, which is fed by a logic cone that computes the next count value from the current count value. Which is exactly what the count description I wrote does: count is a register with a large logic cone (the priority encoded if statement) feeding the count.
Regards