The top one is for continuous PWM rather than just one pulse of a particular length, the second is the latter as this is technically what the system here would like.
module pwm4_basic (input wire clk,
input wire rst,
input wire [3:0] duty,
output reg pwm_out);
reg [3:0] count = 0;
always @(posedge clk) begin
count = (rst | count == duty) ? 0 : count + 1;
pwm_out <= |count;
end
endmodulemodule pwm4_basic (input wire clk,
input wire rst,
input wire [3:0] duty,
output reg pwm_out);
reg [3:0] count = 0;
always @(posedge clk) begin
count <= rst ? 0 : count + 1;
pwm_out <= rst ? 0 : count < duty;
end
endmodule