Verilog Gray-code Teller Module – 4-bit Gray Code Generator
Gray-code tellers zijn essentieel in systemen waar overgangen tussen opeenvolgende waarden slechts één bit mogen veranderen – cruciaal voor klokdomeinovergang, rotary encoders en state machines om glitches en metastabiliteit te minimaliseren. Deze module implementeert een parametriseerbare N-bit Gray-teller met twee equivalente conversiemethoden.
Ontwerp
module gray_ctr #(
parameter int N = 4
)(
input wire clk,
input wire rstn,
output logic [N-1:0] out
);
logic [N-1:0] q;
always_ff @(posedge clk) begin
if (!rstn) begin
q <= '0;
out <= '0;
end else begin
q <= q + 1'b1;
// Binaire-naar-Gray conversie: G[i] = B[i] ^ B[i+1], MSB blijft gelijk
out <= {q[N-1], q[N-1:1] ^ q[N-2:0]};
// Alternatieve for-loop implementatie (voor `FOR_LOOP macro):
// for (int i = 0; i < N-1; i++) out[i] <= q[i+1] ^ q[i];
// out[N-1] <= q[N-1];
end
end
endmodule
Kernconcepten:
- Interne binaire teller (q): Telt standaard binair (0,1,2,3...).
- Binaire-naar-Gray conversie:
Gray = Binary ^ (Binary >> 1). De MSB blijft ongewijzigd; elke lagere bit is XOR van zichzelf en de bit erboven. - Twee implementaties: De concatenatie/XOR-slice methode is synthesis-vriendelijk; de for-loop variant is meer expliciet maar functioneel identiek.
- Parameter N: Ondersteunt elke bitbreedte (standaard 4-bit).
Testbench
module tb;
parameter int N = 4;
reg clk;
reg rstn;
wire [N-1:0] out;
gray_ctr #(.N(N)) 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 (0000) T=50 rstn=1 out=0x1 (0001) T=70 rstn=1 out=0x3 (0011) T=90 rstn=1 out=0x2 (0010) T=110 rstn=1 out=0x6 (0110) T=130 rstn=1 out=0x7 (0111) T=150 rstn=1 out=0x5 (0101) T=170 rstn=1 out=0x4 (0100) T=190 rstn=1 out=0xc (1100) T=210 rstn=1 out=0xd (1101) T=230 rstn=1 out=0xf (1111) T=250 rstn=1 out=0xe (1110) T=270 rstn=1 out=0xa (1010) T=290 rstn=1 out=0xb (1011) T=310 rstn=1 out=0x9 (1001) T=330 rstn=1 out=0x8 (1000) T=350 rstn=1 out=0x0 (0000) <-- Wrap-around T=370 rstn=1 out=0x1 (0001) T=390 rstn=1 out=0x3 (0011) T=410 rstn=1 out=0x2 (0010) Simulation complete via $finish(1) at time 430 NS + 0
Gray-code Volgorde Analyse
De 4-bit Gray sequentie (16 unieke waarden):
| Dec | Binair | Gray (Hex) | Gray (Bin) | Bitveranderingen |
|---|---|---|---|---|
| 0 | 0000 | 0x0 | 0000 | – |
| 1 | 0001 | 0x1 | 0001 | bit 0 |
| 2 | 0010 | 0x3 | 0011 | bit 1 |
| 3 | 0011 | 0x2 | 0010 | bit 0 |
| 4 | 0100 | 0x6 | 0110 | bit 2 |
| 5 | 0101 | 0x7 | 0111 | bit 0 |
| 6 | 0110 | 0x5 | 0101 | bit 1 |
| 7 | 0111 | 0x4 | 0100 | bit 0 |
| 8 | 1000 | 0xC | 1100 | bit 3 |
| 9 | 1001 | 0xD | 1101 | bit 0 |
| 10 | 1010 | 0xF | 1111 | bit 1 |
| 11 | 1011 | 0xE | 1110 | bit 0 |
| 12 | 1100 | 0xA | 1010 | bit 2 |
| 13 | 1101 | 0xB | 1011 | bit 0 |
| 14 | 1110 | 0x9 | 1001 | bit 1 |
| 15 | 1111 | 0x8 | 1000 | bit 0 |
Elke overgang wijzigt precies één bit – de definitie van Gray-code.
Toepassingen & Uitbreidingen
- Klokdomeinovergang (CDC): Gray-code pointers in asynchrone FIFO's voorkomen metastabiliteit.
- Rotary encoders: Mechanische encoders leveren natureel Gray-code.
- State machines: Vermindert schakelruis in kritieke paden.
- Uitbreiding: Voeg
output logic [N-1:0] bin_outtoe voor gelijktijdige binaire uitgang.
Deze module biedt een compacte, synthetiseerbare Gray-code generator geschikt voor ASIC en FPGA implementaties.
Verilog
- Verilog beheersen: Complete tutorial voor digitale schakelingontwerp
- Verilog concatenatie beheersen: Een praktische gids
- Verilog-toewijzingen begrijpen: types, syntax en best practices
- Verilog Blocking vs. Non-Blocking: belangrijkste verschillen uitgelegd
- Verilog-functies beheersen voor efficiënt RTL-ontwerp
- Verilog Tasks begrijpen: Functions vs Tasks, Syntax en Gebruik
- Verilog Clock Generator: Ontwerp & Implementatie Gids
- Verilog Math Functions beheersen voor efficiënt Hardware-ontwerp
- Verilog $timeformat beheersen: Precisie en Weergave-controle
- Een Verilog T Flip-Flop Ontwerpen: RTL Implementatie & Testbench
- Verilog Mod-N Teller Ontwerp: Geparametriseerde Module & Testbench