Issues in a simple comparator

Don’t use a fall time of 0. This is unphysical and will lead to errors. Give it a small fall time instead.

Don’t run a comparator at its edge of the detection window. Choose a robust value in the middle of the ramp instead.

There is an integrated code model for a PWM, which I have used in a Regulated boost converter, see More simulation examples for KiCad/Eeschema/ngspice .