35 lines
592 B
Systemverilog
35 lines
592 B
Systemverilog
module pwm_gen #(
|
|
parameter int WIDTH = 8
|
|
) (
|
|
input logic clk,
|
|
input logic rst,
|
|
input logic en,
|
|
input logic [WIDTH-1:0] duty,
|
|
output logic pwm_out
|
|
);
|
|
|
|
logic [WIDTH-1:0] count;
|
|
|
|
always_ff @(posedge clk) begin
|
|
if (rst) begin
|
|
count <= '0;
|
|
end else if(en) begin
|
|
count <= count + 1'b1;
|
|
end
|
|
end
|
|
|
|
always_comb begin
|
|
|
|
if (!en) begin
|
|
pwm_out = 1'b0;
|
|
end else if(duty == '1) begin
|
|
pwm_out = 1'b1;
|
|
end else begin
|
|
pwm_out <= (count < duty);
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
endmodule |