module pwm( input logic clk, reset, // Load control signal, if this is high we should load a new sample input logic load, // The audio sample to play back input logic [15:0] sample, // The audio output pin output wire pwm_pin ); // What I imagine is that the counter here can be incremented each clock. // If the counter value is less than or equal to the value in the sample buffer // then you should turn on the PWM output. Otherwise if the counter is greater // than the value in the sample buffer the output will be off. // // This means that for small sample values the output will be enabled for only // short periods of time, exactly what we want. logic [15:0] pulse_counter; // A buffer to hold the sample in. Every clock cycle you should check load // to see if you should pull the sample off the bus and store it in here. logic [15:0] sample_buffer; // A control signal for driving the PWM high or low. This gets translated into // either a 'z or a '0 later as the PWM requires. logic should_output; // NOTE: tristating the pwm pin with a 'z will output a 1 // sending a 0 will pull the pin to 0 as usual assign pwm_pin = should_output ? 'z : '0; endmodule