data/full_repos/permissive/204486675/hdl/pwm.v
data/verilator_xmls/06c68727-8f87-47d2-8db6-e9545de5b79c.xml
module pwm (
rst,
clkin,
clkout,
onperiod,
offperiod
);

localparam RESOLUTION = 16;
localparam  _RESOLUTION = RESOLUTION -1;

input rst;
input clkin;
output reg clkout;
input [_RESOLUTION:0] onperiod;
input [_RESOLUTION:0] offperiod;

reg [_RESOLUTION:0] count;
reg [_RESOLUTION:0] lastonperiod;
reg [_RESOLUTION:0] lastoffperiod;

always @ (posedge clkin or posedge rst)
if (rst)
begin
	count <= 16'b0000000000000000;
	clkout <= 1'b0;
end
else
begin
	if (onperiod != lastonperiod)
		count <= 16'b0;
	if (offperiod != lastoffperiod)
		count <= 16'b0;

	lastonperiod = onperiod;
	lastoffperiod = offperiod;

	if (clkout == 0)
	begin
		if (count == offperiod)
		begin
			count <= 16'b0000000000000000;
			clkout <= 1;
		end
		else
		begin
			count <= count + 1'b1;
		end
	end
	else
	begin
		if (count == onperiod)
		begin
			count <= 16'b0000000000000000;
			clkout <= 0;
		end
		else
		begin
			count <= count + 1'b1;
		end
	end

end

endmodule
module pwm (
rst,
clkin,
clkout,
onperiod,
offperiod
);


localparam RESOLUTION = 16;
localparam  _RESOLUTION = RESOLUTION -1;

input rst;
input clkin;
output reg clkout;
input [_RESOLUTION:0] onperiod;
input [_RESOLUTION:0] offperiod;

reg [_RESOLUTION:0] count;
reg [_RESOLUTION:0] lastonperiod;
reg [_RESOLUTION:0] lastoffperiod;

always @ (posedge clkin or posedge rst)
if (rst)
begin
	count <= 16'b0000000000000000;
	clkout <= 1'b0;
end
else
begin
	if (onperiod != lastonperiod)
		count <= 16'b0;
	if (offperiod != lastoffperiod)
		count <= 16'b0;

	lastonperiod = onperiod;
	lastoffperiod = offperiod;

	if (clkout == 0)
	begin
		if (count == offperiod)
		begin
			count <= 16'b0000000000000000;
			clkout <= 1;
		end
		else
		begin
			count <= count + 1'b1;
		end
	end
	else
	begin
		if (count == onperiod)
		begin
			count <= 16'b0000000000000000;
			clkout <= 0;
		end
		else
		begin
			count <= count + 1'b1;
		end
	end

end

endmodule