Industriële fabricage
Industrieel internet der dingen | Industriële materialen | Onderhoud en reparatie van apparatuur | Industriële programmering |
home  MfgRobots >> Industriële fabricage >  >> Industrial programming >> Verilog

Verilog Mod-N Teller Ontwerp: Geparametriseerde Module & Testbench

Een Mod-N teller telt van 0 tot N-1 en herhaalt zich daarna. Deze geparametriseerde Verilog-implementatie ondersteunt configureerbare tellerbreedte en modulus, wat het herbruikbaar maakt voor diverse toepassingen van frequentiedeling tot adresgeneratie.

Ontwerp

module modN_ctr #(
    parameter int N     = 10,
    parameter int WIDTH = 4
)(
    input  wire                 clk,
    input  wire                 rstn,
    output logic [WIDTH-1:0]    out
);

always_ff @(posedge clk) begin
    if (!rstn)
        out <= '0;
    else if (out == N-1)
        out <= '0;
    else
        out <= out + 1'b1;
end

endmodule

Ontwerpkenmerken:

  • Parameters: N (modulus) en WIDTH (bitbreedte) staan onafhankelijke configuratie toe.
  • Actief-lage asynchrone reset: Wis de teller onmiddellijk.
  • Wrap-around logica: Wanneer out == N-1, reset naar 0; anders incrementeer.
  • SystemVerilog syntax: Gebruikt logic en always_ff voor moderne, synthetiseerbare code.

Testbench

module tb;
    parameter int N     = 10;
    parameter int WIDTH = 4;

    reg                       clk;
    reg                       rstn;
    wire [WIDTH-1:0]          out;

    modN_ctr #(.N(N), .WIDTH(WIDTH)) u0 (
        .clk  (clk),
        .rstn (rstn),
        .out  (out)
    );

    always #10 clk = ~clk;

    initial begin
        {clk, rstn} = 2'b00;
        $monitor("T=%0t rstn=%0b out=0x%0h", $time, rstn, out);
        repeat(2) @(posedge clk);
        rstn = 1'b1;
        repeat(20) @(posedge clk);
        $finish;
    end
endmodule

Simulatie Resultaten

ncsim> run
T=0   rstn=0 out=0xx
T=10  rstn=0 out=0x0
T=30  rstn=1 out=0x0
T=50  rstn=1 out=0x1
T=70  rstn=1 out=0x2
T=90  rstn=1 out=0x3
T=110 rstn=1 out=0x4
T=130 rstn=1 out=0x5
T=150 rstn=1 out=0x6
T=170 rstn=1 out=0x7
T=190 rstn=1 out=0x8
T=210 rstn=1 out=0x9
T=230 rstn=1 out=0xa
T=250 rstn=1 out=0x0  <-- Wrap-around
T=270 rstn=1 out=0x1
T=290 rstn=1 out=0x2
T=310 rstn=1 out=0x3
T=330 rstn=1 out=0x4
T=350 rstn=1 out=0x5
T=370 rstn=1 out=0x6
T=390 rstn=1 out=0x7
T=410 rstn=1 out=0x8
Simulation complete via $finish(1) at time 430 NS + 0

Analyse

De simulatie toont correcte Mod-10 gedrag:

  1. Reset (0-30ns): Uitgang initialiseert naar 0.
  2. Tellen (30-230ns): Incrementeert 0 → 1 → 2 ... → 9 (0x9).
  3. Wrap-around (230ns): Bij 9 (N-1) reset naar 0 op de volgende klokflank.
  4. Herhaling: Cyclus herhaalt zich netjes voor de overige klokcycli.

Praktische Overwegingen

  • Breedte validatie: Zorg dat WIDTH >= $clog2(N) om overflow te voorkomen. Voeg een compile-time assert toe: initial assert (WIDTH >= $clog2(N));
  • Synchrone reset: Vervang if (!rstn) door if (!rstn) binnen de always_ff voor FPGA-vriendelijke synchrone reset.
  • Enable signaal: Voeg input wire en toe en wrap de tellogica in if (en) voor teller-inhibitie.
  • Load capability: Voeg parallelle laadfunctionaliteit toe voor vooraf ingestelde startwaarden.

Deze modulaire teller is ideaal voor clock-domain crossing interfaces, PWM periodes, en state machine sequentie controle.

Verilog

  1. Verilog beheersen: Complete tutorial voor digitale schakelingontwerp
  2. Verilog concatenatie beheersen: Een praktische gids
  3. Verilog-toewijzingen begrijpen: types, syntax en best practices
  4. Verilog Blocking vs. Non-Blocking: belangrijkste verschillen uitgelegd
  5. Verilog-functies beheersen voor efficiënt RTL-ontwerp
  6. Verilog Tasks begrijpen: Functions vs Tasks, Syntax en Gebruik
  7. Verilog Clock Generator: Ontwerp & Implementatie Gids
  8. Verilog Math Functions beheersen voor efficiënt Hardware-ontwerp
  9. Verilog $timeformat beheersen: Precisie en Weergave-controle
  10. Een Verilog T Flip-Flop Ontwerpen: RTL Implementatie & Testbench
  11. Verilog Gray-code Teller Module – 4-bit Gray Code Generator