logo

Verilog 그레이 카운터

그레이 코드는 한 번에 한 비트만 변경되는 일종의 이진수 시스템입니다. 오늘날 그레이 코드는 디지털 세계에서 널리 사용됩니다. 오류정정과 신호전송에 도움이 될 것입니다. 그레이 카운터는 VLSI 도메인의 설계 및 검증에도 유용합니다.

Verilog 그레이 카운터

그레이 코드는 인접한 정수의 표현이 정확히 하나의 이진 위치에서 다른 속성을 가진 비트 시퀀스로 정수를 인코딩합니다.

그레이 코드에는 균형, 이진 반사, 최대 간격 및 대척 그레이 코드와 같은 다양한 유형이 있습니다.

카운터는 지정된 출력 시퀀스를 생성하는 기본 기능을 갖고 있으며 때로는 패턴 생성기라고도 합니다.

설계

그레이 코드에서는 한 번에 한 비트만 변경됩니다. 이 설계 코드에는 클록 및 재설정 신호라는 두 개의 입력과 그레이 코드를 생성하는 하나의 4비트 출력이 있습니다.

첫째, 만약 먼저 신호가 높으면 출력은 0이 되고, 먼저 낮아지고, 상승 가장자리에서 클크 , 디자인은 4비트 그레이 코드를 생성하고 모든 상승 에지에서 계속 생성됩니다. 클크 신호.

이 디자인 코드는 업그레이드되어 이진수를 입력으로 넣을 수 있으며 이 디자인은 이진수를 그레이 코드로 변환하는 역할을 합니다.

 module gray_ctr # (parameter N = 4) ( input clk, input rstn, output reg [N-1:0] out); reg [N-1:0] q; always @ (posedge clk) begin if (!rstn) begin q <= 0; out <="0;" end else begin q + 1; `ifdef for_loop for (int i="0;" n-1; out[i] ^ q[i]; out[n-1] `else q[n-1:1] q[n-2:0]}; `endif endmodule pre> <h3>Hardware Schematic</h3> <img src="//techcodeview.com/img/verilog-tutorial/27/verilog-gray-counter-2.webp" alt="Verilog Gray Counter"> <h3>Testbench</h3> <pre> module tb; parameter N = 4; reg clk; reg rstn; wire [N-1:0] out; gray_ctr u0 ( .clk(clk), .rstn(rstn), .out(out)); always #10 clk = ~clk; initial begin {clk, rstn} <= 0; $monitor ('t="%0t" rstn="%0b" out="0x%0h&apos;," $time, rstn, out); repeat(2) @ (posedge clk); <="1;" repeat(20) $finish; end endmodule pre> <p>And it produces the following output, such as:</p> <pre> ncsim&gt; 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=0x3 T=90 rstn=1 out=0x2 T=110 rstn=1 out=0x6 T=130 rstn=1 out=0x7 T=150 rstn=1 out=0x5 T=170 rstn=1 out=0x4 T=190 rstn=1 out=0xc T=210 rstn=1 out=0xd T=230 rstn=1 out=0xf T=250 rstn=1 out=0xe T=270 rstn=1 out=0xa T=290 rstn=1 out=0xb T=310 rstn=1 out=0x9 T=330 rstn=1 out=0x8 T=350 rstn=1 out=0x0 T=370 rstn=1 out=0x1 T=390 rstn=1 out=0x3 T=410 rstn=1 out=0x2 Simulation complete via $finish(1) at time 430 NS + 0 </pre> <h3>Balanced Gray Code</h3> <p>In balanced Gray codes, the number of changes in different coordinate positions is as close as possible.</p> <p>A Gray code is <strong> <em>uniform</em> </strong> or <strong> <em>uniformly</em> </strong> balanced if its transition counts are all equal.</p> <p>Gray codes can also be <strong> <em>exponentially</em> </strong> balanced if all of their transition counts are adjacent powers of two, and such codes exist for every power of two.</p> <p>For example, a balanced 4-bit Gray code has 16 transitions, which can be evenly distributed among all four positions (four transitions per position), making it uniformly balanced.</p> <pre> 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 </pre> <h3>n-ary Gray Code</h3> <p>There are many specialized types of Gray codes other than the binary-reflected Gray code. One such type of Gray code is the n-ary Gray code, also known as a <strong> <em>non-Boolean</em> </strong> Gray code. As the name implies, this type of Gray code uses non-Boolean values in its encodings.</p> <p>For example, a 3-ary ternary Gray code would use the values {0, 1, and 2}. The (n, k)-Gray code is the n-ary Gray code with k digits. The sequence of elements in the (3, 2)-Gray code is: {00, 01, 02, 12, 11, 10, 20, 21, and 22}.</p> <p>The (n, k)-Gray code may be constructed recursively, as the BRGC, or may be constructed iteratively.</p> <h3>Monotonic Gray Codes</h3> <p>Monotonic codes are useful in interconnection networks theory, especially for minimizing dilation for linear arrays of processors.</p> <p>If we define the weight of a binary string to be the number of 1s in the string, then although we clearly cannot have a Gray code with strictly increasing weight, we may want to approximate this by having the code run through two adjacent weights before reaching the next one.</p> <h3>Beckett-Gray Code</h3> <p>Another type of Gray code, the Beckett-Gray code, is named for Irish playwright <strong> <em>Samuel Beckett</em> </strong> , who was interested in <strong> <em>symmetry</em> </strong> . His play <strong> <em>Quad</em> </strong> features four actors and is divided into sixteen time periods. Each period ends with one of the four actors entering or leaving the stage.</p> <p>The play begins with an empty stage, and Beckett wanted each subset of actors to appear on stage exactly once. A 4-bit binary Gray code can represent the set of actors currently on stage.</p> <p>However,</p> <p>Beckett placed an additional restriction on the script: he wished the actors to enter and exit so that the actor who had been on stage the longest would always be the one to exit.</p> <p>The actors could then be represented by a first-in, first-out (FIFO) queue so that the actor being dequeued is always the one who was enqueued first.</p> <p>Beckett was unable to find a Beckett-Gray code for his play, and indeed, an exhaustive listing of all possible sequences reveals that no such code exists for n = 4. It is known today that such codes do exist for n = 2, 5, 6, 7, and 8, and do not exist for n = 3 or 4.</p> <h3>Snake-in-the-box Codes</h3> <p>Snake-in-the-box codes, or snakes, are the sequences of nodes of induced paths in an n-dimensional <strong> <em>hypercube</em> </strong> graph, and coil-in-the-box codes, or coils, are the sequences of nodes of induced cycles in a hypercube.</p> <p>Viewed as Gray codes, these sequences have the property of detecting any single-bit coding error.</p> <h3>Single-track Gray Code</h3> <p>Another kind of Gray code is the single-track Gray code (STGC) developed by <strong> <em>Norman B. Spedding</em> </strong> and refined by <strong> <em>Hiltgen, Paterson</em> </strong> and <strong> <em>Brandestini</em> </strong> in &apos;Single-track Gray codes&apos; (1996).</p> <p>The STGC is a cyclical list of P unique binary encodings of length n such that two consecutive words differ in exactly one position. When the list is examined as a P &#xD7; n matrix, each column is a cyclic shift of the first column.</p> <p>The name comes from their use with rotary encoders, where many tracks are being sensed by contacts, resulting in each in an output of 0 or 1. To reduce noise due to different contacts not switching the same moment in time, one preferably sets up the tracks so that the contacts&apos; data output is in Gray code.</p> <p>To get high angular accuracy, one needs lots of contacts; to achieve at least 1-degree accuracy, one needs at least 360 distinct positions per revolution, which requires a minimum of 9 bits of data and the same number of contacts.</p> <p>If all contacts are placed at the same angular position, then 9 tracks are needed to get a standard BRGC with at least 1-degree accuracy. However, if the manufacturer moves a contact to a different angular position but at the same distance from the center shaft, then the corresponding &apos;ring pattern&apos; needs to be rotated the same angle to give the same output.</p> <h3>Two-dimensions Gray Code</h3> <p>Two-dimensional Gray codes are used in communication to minimize the number of bit errors in quadrature amplitude modulation adjacent points in the constellation.</p> <p>In a standard encoding, the horizontal and vertical adjacent constellation points differ by a single bit, and adjacent diagonal points differ by 2 bits.</p> <hr></=></pre></=>

균형 잡힌 그레이 코드

균형 그레이 코드에서는 서로 다른 좌표 위치의 변경 횟수가 최대한 가깝습니다.

그레이 코드는 제복 또는 균일하게 전환 횟수가 모두 동일하면 균형을 이룹니다.

그레이 코드도 가능합니다. 기하급수적으로 모든 전환 카운트가 2의 인접 거듭제곱이고 이러한 코드가 2의 거듭제곱마다 존재하는 경우 균형을 이룹니다.

예를 들어 균형 잡힌 4비트 그레이 코드에는 16개의 전환이 있으며, 이는 4개 위치 모두에 균등하게 분산되어(위치당 4개의 전환) 균등하게 균형을 이룰 수 있습니다.

 0 1 1 1 1 1 1 0 0 0 0 0 0 1 1 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 0 0 1 1 1 0 0 0 0 0 1 1 0 0 0 0 0 1 1 1 1 1 1 

n-ary 그레이 코드

바이너리 반영 그레이 코드 외에도 특수한 유형의 그레이 코드가 많이 있습니다. 그러한 유형의 그레이 코드 중 하나는 N-진 그레이 코드(n-ary Gray code)입니다. 부울이 아닌 그레이 코드. 이름에서 알 수 있듯이 이 유형의 그레이 코드는 인코딩에 부울이 아닌 값을 사용합니다.

예를 들어 3항 삼항 그레이 코드는 {0, 1, 2} 값을 사용합니다. (n, k)-그레이 코드는 k자리의 n진 그레이 코드입니다. (3, 2)-그레이 코드의 요소 순서는 {00, 01, 02, 12, 11, 10, 20, 21 및 22}입니다.

(n, k)-그레이 코드는 BRGC처럼 재귀적으로 구성될 수도 있고, 반복적으로 구성될 수도 있다.

단조로운 회색 코드

단조 코드는 상호 연결 네트워크 이론, 특히 프로세서 선형 배열의 팽창을 최소화하는 데 유용합니다.

이진 문자열의 가중치를 문자열의 1 개수로 정의하면 가중치가 엄격하게 증가하는 그레이 코드를 가질 수는 없지만 코드가 1에 도달하기 전에 두 개의 인접한 가중치를 통과하도록 하여 이를 근사화할 수 있습니다. 다음 것.

베켓-그레이 코드

또 다른 유형의 그레이 코드인 Beckett-Gray 코드는 아일랜드 극작가의 이름을 따서 명명되었습니다. 사무엘 베켓 , 관심이 있었던 사람 대칭 . 그의 플레이 쿼드 4명의 배우가 출연하며 16개의 기간으로 나뉩니다. 각 기간은 4명의 배우 중 한 명이 무대에 들어오거나 나가는 것으로 끝납니다.

연극은 빈 무대에서 시작되며 베켓은 각 배우가 정확히 한 번만 무대에 나타나기를 원했습니다. 4비트 바이너리 그레이 코드는 현재 무대에 있는 배우 세트를 나타낼 수 있습니다.

하지만,

Beckett은 대본에 추가적인 제한을 두었습니다. 그는 배우들이 입장하고 퇴장하여 무대에 가장 오래 있었던 배우가 항상 퇴장하기를 바랐습니다.

그런 다음 행위자는 선입선출(FIFO) 대기열로 표시될 수 있으므로 대기열에서 제거되는 행위자는 항상 대기열에 먼저 추가된 행위자가 됩니다.

Beckett은 그의 희곡에 대한 Beckett-Gray 코드를 찾을 수 없었으며 실제로 가능한 모든 시퀀스의 철저한 목록을 보면 n = 4에 대한 그러한 코드가 존재하지 않음이 드러납니다. 오늘날 그러한 코드는 n = 2, 5에 대해 존재하는 것으로 알려져 있습니다. , 6, 7 및 8이며 n = 3 또는 4인 경우에는 존재하지 않습니다.

Snake-in-the-box 코드

Snake-in-the-box 코드 또는 뱀은 n차원에서 유도된 경로의 노드 시퀀스입니다. 하이퍼큐브 그래프, 코일-인-더-박스 코드 또는 코일은 하이퍼큐브에서 유도된 주기의 노드 시퀀스입니다.

그레이 코드로 볼 때 이러한 시퀀스는 단일 비트 코딩 오류를 감지하는 속성을 가지고 있습니다.

단일 트랙 그레이 코드

그레이 코드의 또 다른 종류는 에 의해 개발된 단일 트랙 그레이 코드(STGC)입니다. 노먼 B. 스페딩 그리고 정제된 힐트겐, 패터슨 그리고 브란데스티니 '단일 트랙 그레이 코드'(1996).

STGC는 두 개의 연속 단어가 정확히 한 위치에서 달라지는 길이 n의 P개의 고유 이진 인코딩의 순환 목록입니다. 목록을 P × n 행렬로 검사하면 각 열은 첫 번째 열의 순환 이동입니다.

날짜를 문자열로 형식화

이름은 많은 트랙이 접점에 의해 감지되어 각각 0 또는 1의 출력이 되는 로터리 인코더와 함께 사용하는 데서 유래합니다. 동일한 순간에 전환하지 않는 여러 접점으로 인한 소음을 줄이려면 다음을 설정하는 것이 좋습니다. 연락처의 데이터 출력이 그레이 코드로 표시되도록 추적합니다.

높은 각도 정확도를 얻으려면 많은 접촉이 필요합니다. 최소 1도의 정확도를 달성하려면 회전당 최소 360개의 개별 위치가 필요하며, 이를 위해서는 최소 9비트의 데이터와 동일한 수의 접점이 필요합니다.

모든 접점이 동일한 각도 위치에 배치된 경우 최소 1도 정확도의 표준 BRGC를 얻으려면 9개의 트랙이 필요합니다. 그러나 제조업체가 접점을 다른 각도 위치로 이동하지만 중심 샤프트에서 동일한 거리에 있는 경우 해당 '링 패턴'은 동일한 출력을 제공하기 위해 동일한 각도로 회전해야 합니다.

2차원 그레이 코드

2차원 그레이 코드는 성상도의 인접한 지점에서 직교 진폭 변조의 비트 오류 수를 최소화하기 위해 통신에 사용됩니다.

표준 인코딩에서는 수평 및 수직 인접 성상점은 1비트씩 다르며, 인접한 대각선 지점은 2비트만큼 다릅니다.