add pwm generator

This commit is contained in:
Elie Deletang
2026-11-29 11:24:30 +01:00
parent d2225bdb93
commit 59781bcbc0
6 changed files with 1234 additions and 0 deletions
+35
View File
@@ -0,0 +1,35 @@
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
+126
View File
@@ -0,0 +1,126 @@
<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:s="https://github.com/nturley/netlistsvg" width="676" height="246" viewBox="0 0 676 246">
<style>svg{stroke:#000;fill:none;stroke-linejoin:round}text{fill:#000;stroke:none;font-size:10px;font-weight:600;font-family:ui-monospace,"Cascadia Mono","SF Mono",Menlo,Consolas,"Courier New",monospace}.nodelabel{text-anchor:middle}.inputPortLabel{text-anchor:end}.splitjoinBody{fill:currentColor}.bus{stroke-width:1.75}.undef{color:#dc2626}.undef rect,.undef line{stroke:#dc2626}.undef text{fill:#dc2626}</style>
<g s:type="inputExt" transform="translate(40,116)" s:width="30" s:height="20" id="cell_clk">
<text x="-4" y="14" style="text-anchor:end" class="nodelabel cell_clk" s:attribute="ref">clk</text>
<path d="M0,0 L0,20 L15,20 L30,10 L15,0 Z" class="cell_clk"/>
<g s:x="30" s:y="10" s:pid="Y"/>
</g>
<g s:type="inputExt" transform="translate(40,12)" s:width="30" s:height="20" id="cell_rst">
<text x="-4" y="14" style="text-anchor:end" class="nodelabel cell_rst" s:attribute="ref">rst</text>
<path d="M0,0 L0,20 L15,20 L30,10 L15,0 Z" class="cell_rst"/>
<g s:x="30" s:y="10" s:pid="Y"/>
</g>
<g s:type="inputExt" transform="translate(40,64)" s:width="30" s:height="20" id="cell_en">
<text x="-4" y="14" style="text-anchor:end" class="nodelabel cell_en" s:attribute="ref">en</text>
<path d="M0,0 L0,20 L15,20 L30,10 L15,0 Z" class="cell_en"/>
<g s:x="30" s:y="10" s:pid="Y"/>
</g>
<g s:type="inputExt" transform="translate(40,204)" s:width="30" s:height="20" id="cell_duty">
<text x="-4" y="14" style="text-anchor:end" class="nodelabel cell_duty" s:attribute="ref">duty</text>
<path d="M0,0 L0,20 L15,20 L30,10 L15,0 Z" class="cell_duty"/>
<g s:x="30" s:y="10" s:pid="Y"/>
</g>
<g s:type="outputExt" transform="translate(588,139)" s:width="30" s:height="20" id="cell_pwm_out">
<text x="34" y="14" style="text-anchor:start" class="nodelabel cell_pwm_out" s:attribute="ref">pwm_out</text>
<path d="M30,0 L30,20 L15,20 L0,10 L15,0 Z" class="cell_pwm_out"/>
<g s:x="0" s:y="10" s:pid="A"/>
</g>
<g s:type="add" transform="translate(141,34)" s:width="26" s:height="26" id="cell_$add$top.sv:19$2">
<circle r="13" cx="13" cy="13" class="cell_$add$top.sv:19$2"/>
<text x="13" y="17.68" class="nodelabel cell_$add$top.sv:19$2" style="font-size:13px">+</text>
<line x1="0" y1="6" x2="2" y2="6" class="cell_$add$top.sv:19$2"/>
<line x1="0" y1="20" x2="2" y2="20" class="cell_$add$top.sv:19$2"/>
<g s:x="0" s:y="6" s:pid="A"/>
<g s:x="0" s:y="20" s:pid="B"/>
<g s:x="26" s:y="13" s:pid="Y"/>
</g>
<g s:type="dff" transform="translate(211,74)" s:width="48" s:height="68" id="cell_$auto$ff.cc:337:slice$23">
<text x="24" y="-4" class="nodelabel cell_$auto$ff.cc:337:slice$23" s:attribute="ref">count</text>
<rect width="48" height="68" x="0" y="0" rx="2" class="cell_$auto$ff.cc:337:slice$23"/>
<text x="5" y="13" class="cell_$auto$ff.cc:337:slice$23" style="font-size:9px;font-weight:normal">D</text>
<text x="5" y="27" class="cell_$auto$ff.cc:337:slice$23" style="font-size:9px;font-weight:normal">EN</text>
<text x="5" y="41" class="cell_$auto$ff.cc:337:slice$23" style="font-size:9px;font-weight:normal">RST</text>
<path d="M0,47 L6,52 L0,57" class="cell_$auto$ff.cc:337:slice$23"/>
<text x="43" y="13" class="cell_$auto$ff.cc:337:slice$23" style="font-size:9px;font-weight:normal;text-anchor:end">Q</text>
<g s:x="0" s:y="10" s:pid="D"/>
<g s:x="0" s:y="24" s:pid="EN"/>
<g s:x="0" s:y="38" s:pid="SRST"/>
<g s:x="0" s:y="52" s:pid="CLK"/>
<g s:x="48" s:y="10" s:pid="Q"/>
</g>
<g s:type="eq" transform="translate(344,208)" s:width="26" s:height="26" id="cell_$eq$top.sv:27$5">
<circle r="13" cx="13" cy="13" class="cell_$eq$top.sv:27$5"/>
<text x="13" y="17.68" class="nodelabel cell_$eq$top.sv:27$5" style="font-size:13px">=</text>
<line x1="0" y1="6" x2="2" y2="6" class="cell_$eq$top.sv:27$5"/>
<line x1="0" y1="20" x2="2" y2="20" class="cell_$eq$top.sv:27$5"/>
<g s:x="0" s:y="6" s:pid="A"/>
<g s:x="0" s:y="20" s:pid="B"/>
<g s:x="26" s:y="13" s:pid="Y"/>
</g>
<g s:type="lt" transform="translate(330.33,136)" s:width="26" s:height="26" id="cell_$lt$top.sv:30$6">
<circle r="13" cx="13" cy="13" class="cell_$lt$top.sv:30$6"/>
<text x="13" y="17.68" class="nodelabel cell_$lt$top.sv:30$6" style="font-size:13px">&lt;</text>
<line x1="0" y1="6" x2="2" y2="6" class="cell_$lt$top.sv:30$6"/>
<line x1="0" y1="20" x2="2" y2="20" class="cell_$lt$top.sv:30$6"/>
<g s:x="0" s:y="6" s:pid="A"/>
<g s:x="0" s:y="20" s:pid="B"/>
<g s:x="26" s:y="13" s:pid="Y"/>
</g>
<g s:type="mux" transform="translate(524,129)" s:width="20" s:height="40" id="cell_$procmux$14">
<path d="M0,0 L20,8 L20,32 L0,40 Z" class="cell_$procmux$14"/>
<text x="3" y="13" class="cell_$procmux$14" style="font-size:9px;font-weight:normal">0</text>
<text x="3" y="33" class="cell_$procmux$14" style="font-size:9px;font-weight:normal">1</text>
<line x1="10" y1="40" x2="10" y2="36" class="cell_$procmux$14"/>
<g s:x="0" s:y="10" s:pid="A"/>
<g s:x="0" s:y="30" s:pid="B"/>
<g s:x="10" s:y="40" s:pid="S"/>
<g s:x="20" s:y="20" s:pid="Y"/>
</g>
<g s:type="mux" transform="translate(437,139)" s:width="20" s:height="40" id="cell_$procmux$8">
<path d="M0,0 L20,8 L20,32 L0,40 Z" class="cell_$procmux$8"/>
<text x="3" y="13" class="cell_$procmux$8" style="font-size:9px;font-weight:normal">0</text>
<text x="3" y="33" class="cell_$procmux$8" style="font-size:9px;font-weight:normal">1</text>
<line x1="10" y1="40" x2="10" y2="36" class="cell_$procmux$8"/>
<g s:x="0" s:y="10" s:pid="A"/>
<g s:x="0" s:y="30" s:pid="B"/>
<g s:x="10" s:y="40" s:pid="S"/>
<g s:x="20" s:y="20" s:pid="Y"/>
</g>
<g class="const" s:type="constant" id="cell_1" data-value="1" transform="translate(141,54)" s:width="0" s:height="0"><line x1="0" y1="0" x2="-8" y2="0"/><rect x="-22" y="-6" width="14" height="12" rx="3"/><text x="-15" y="3.5" style="text-anchor:middle;font-weight:normal">1</text></g>
<g class="const" s:type="constant" id="cell_11111111" data-value="11111111" transform="translate(344,228)" s:width="0" s:height="0"><line x1="0" y1="0" x2="-8" y2="0"/><rect x="-40" y="-6" width="32" height="12" rx="3"/><text x="-24" y="3.5" style="text-anchor:middle;font-weight:normal">0xff</text></g>
<g class="const" s:type="constant" id="cell_0" data-value="0" transform="translate(524,139)" s:width="0" s:height="0"><line x1="0" y1="0" x2="-8" y2="0"/><rect x="-22" y="-6" width="14" height="12" rx="3"/><text x="-15" y="3.5" style="text-anchor:middle;font-weight:normal">0</text></g>
<g class="const" s:type="constant" id="cell_1" data-value="1" transform="translate(437,169)" s:width="0" s:height="0"><line x1="0" y1="0" x2="-8" y2="0"/><rect x="-22" y="-6" width="14" height="12" rx="3"/><text x="-15" y="3.5" style="text-anchor:middle;font-weight:normal">1</text></g>
<line x1="544" y1="149" x2="588" y2="149" class="net_13"/>
<line x1="259" y1="84" x2="273" y2="84" class="net_14,15,16,17,18,19,20,21 bus"/>
<line x1="104" y1="20" x2="273" y2="20" class="net_14,15,16,17,18,19,20,21 bus"/>
<line x1="104" y1="40" x2="141" y2="40" class="net_14,15,16,17,18,19,20,21 bus"/>
<line x1="273" y1="142" x2="330.33" y2="142" class="net_14,15,16,17,18,19,20,21 bus"/>
<line x1="273" y1="20" x2="273" y2="84" class="net_14,15,16,17,18,19,20,21 bus"/>
<line x1="273" y1="84" x2="273" y2="142" class="net_14,15,16,17,18,19,20,21 bus"/>
<line x1="104" y1="20" x2="104" y2="40" class="net_14,15,16,17,18,19,20,21 bus"/>
<circle cx="273" cy="84" r="2" style="fill:currentColor" class="net_14,15,16,17,18,19,20,21 bus"/>
<line x1="167" y1="47" x2="191" y2="47" class="net_22,23,24,25,26,27,28,29 bus"/>
<line x1="191" y1="84" x2="211" y2="84" class="net_22,23,24,25,26,27,28,29 bus"/>
<line x1="191" y1="47" x2="191" y2="84" class="net_22,23,24,25,26,27,28,29 bus"/>
<line x1="70" y1="74" x2="84" y2="74" class="net_4"/>
<line x1="84" y1="74" x2="181" y2="74" class="net_4"/>
<line x1="181" y1="98" x2="211" y2="98" class="net_4"/>
<line x1="84" y1="193" x2="534" y2="193" class="net_4"/>
<line x1="181" y1="74" x2="181" y2="98" class="net_4"/>
<line x1="84" y1="74" x2="84" y2="193" class="net_4"/>
<line x1="534" y1="169" x2="534" y2="193" class="net_4"/>
<circle cx="84" cy="74" r="2" style="fill:currentColor" class="net_4"/>
<line x1="70" y1="22" x2="94" y2="22" class="net_3"/>
<line x1="94" y1="112" x2="211" y2="112" class="net_3"/>
<line x1="94" y1="22" x2="94" y2="112" class="net_3"/>
<line x1="70" y1="126" x2="211" y2="126" class="net_2"/>
<line x1="70" y1="214" x2="94" y2="214" class="net_5,6,7,8,9,10,11,12 bus"/>
<line x1="94" y1="214" x2="344" y2="214" class="net_5,6,7,8,9,10,11,12 bus"/>
<line x1="94" y1="156" x2="330.33" y2="156" class="net_5,6,7,8,9,10,11,12 bus"/>
<line x1="94" y1="156" x2="94" y2="214" class="net_5,6,7,8,9,10,11,12 bus"/>
<circle cx="94" cy="214" r="2" style="fill:currentColor" class="net_5,6,7,8,9,10,11,12 bus"/>
<line x1="457" y1="159" x2="524" y2="159" class="net_32"/>
<line x1="356.33" y1="149" x2="437" y2="149" class="net_31"/>
<line x1="370" y1="221" x2="447" y2="221" class="net_30"/>
<line x1="447" y1="179" x2="447" y2="221" class="net_30"/>
<text data-rapidrtl-mark="" x="668" y="238" text-anchor="end" font-family="system-ui, -apple-system, 'Segoe UI', sans-serif" font-size="14" font-weight="700" style="fill:rgba(0,0,0,0.3) !important;stroke:none !important">Made with RapidRTL · rapidrtl.com</text></svg>

After

Width:  |  Height:  |  Size: 9.2 KiB

+74
View File
@@ -0,0 +1,74 @@
`timescale 1ns / 1ps
module tb_pwm_gen;
localparam int WIDTH = 4;
logic clk;
logic rst;
logic en;
logic [WIDTH-1:0] duty;
logic pwm_out;
// Instanciation avec surcharge du paramètre WIDTH à 4 bits
pwm_gen #(
.WIDTH(WIDTH)
) dut (
.clk(clk),
.rst(rst),
.en(en),
.duty(duty),
.pwm_out(pwm_out)
);
// Horloge 100 MHz (période 10 ns)
initial begin
clk = 1'b0;
forever #5 clk = ~clk;
end
initial begin
$dumpfile("pwm_gen.vcd");
$dumpvars(0, tb_pwm_gen);
// 1. Initialisation et Reset
rst = 1'b1;
en = 1'b0;
duty = '0;
repeat (2) @(posedge clk);
// 2. Relâchement du reset et activation du module
rst <= 1'b0;
en <= 1'b1;
// 3. Test 0 % (duty = 0) sur 1 période complète (16 cycles)
duty <= 4'd0;
repeat (16) @(posedge clk);
// 4. Test 25 % (duty = 4 / 16) sur 2 périodes
duty <= 4'd4;
repeat (32) @(posedge clk);
// 5. Test 50 % (duty = 8 / 16) sur 2 périodes
duty <= 4'd8;
repeat (32) @(posedge clk);
// 6. Test 75 % (duty = 12 / 16) sur 2 périodes
duty <= 4'd12;
repeat (32) @(posedge clk);
// 7. Test 100 % (duty = 15 = '1) sur 1 période
duty <= 4'd15;
repeat (16) @(posedge clk);
// 8. Test désactivation en cours de fonctionnement (coupure immédiate)
duty <= 4'd8;
repeat (4) @(posedge clk);
en <= 1'b0;
repeat (8) @(posedge clk);
$display("Fin de la simulation PWM.");
$finish;
end
endmodule