module pwm(
input clk,
input write_data[31:0],
input cs,
input write_n,
input addr,
input clr_n,
output read_data[31:0],
output pwm_out
);
reg period;
reg counter;
wire [31:0] pulse_width;
wire [31:0] off;
wire [31:0] period_en;
wire [31:0] pulse_width_en;
// Define period and pulse_width registers
always @(posedge clk or negedge clr_n)
begin
if (clr_n == 0)
begin
period <= 32'h // Requires a period of 0.03 nanosecond
pulse_width <= 32'h00000000;
end
else
begin
if (period_en)
period <=> // Same period issue
else
period <= period;=""> // Same period issue
if (pulse_width_en)
pulse_width <= write_data[31:0];
else
pulse_width <= pulse_width;
end
end
// Read access for period and pulse_width registers
if (addr == 0)
read_data = period;
else
read_data = pulse_width;
//Counter
always @(posedge clk or negedge clr_n)
begin
if (clr_n == 0)
counter <= 0;
else
if (counter >= period - 1) // count from 0 to (period-1)
counter <= 0;
else
counter <= counter="" +="" 1;// Is this proper syntax?
end
// Changes output while counter is less than pulse_width
always @(posedge clk or negedge clr_n)
begin
if (clr_n == 0)
off <= 0;
else
if (counter >= pulse_width)
off <= 1;
else
if (counter == 0)
off <= 0;
else
off <= off;
end
//Enables writing to period and pulse_width registers
assign period_en = cs & !write_n & !addr;
assign pulse_width_en = cs & !write_n & addr;
// Output
assign pwm_out = !off;
endmodule