임베디드랜드

Count zeros-Combinational version. source.
제로를 카운트하는 소스



출처 : http://www.soe.ucsc.edu/classes/cmpe225/Fall01/synver.pdf

the circuit must determine two things:

  • The presence of a value containg exactly one sequence of Zeroes.
    값이 정확하게 하나의 '0'의 연속으로 나와야 한다.
  • The number of zeroes in the sequence.
    연속적인 '0'의 개수.

The circuit must complete this computation in a single clock cycle.
그 회로는 반드시 한클럭 사이클안에 계산이 끝나야 한다.
The input to the circuit is an 8-bit value.
입력은 8비트 값을 가짐.
The two outputs the circuit produces are the number of zeroes found and a error indication.
두 출력은 '0'의 수와 에러를 표시한다.

A valid value contains only one series of Zeroes. If more than one series of zeroes appears,
유효값은 '0'의 하나의 시리즈에만 나타난다. '0'이 하나 이상의 시리즈로 나타나면,
the value is invalid.
그값은 무효값이 된다.
A value consisting of all ones is a valid value. If a value is invalid, the count of zeroes is set to zero.
모두 '1'인 값은 유효값이 된다. 만약, 값이 무효하다면, '0'의 카운터는 '0'로 세팅된다.

ex> 000000 is valid. -> the count is 8 zeroes.
11000111 is valid. -> the count is three zeroes.
00111110 is invalid. '0'의 시리즈가 하나이상이어서 무효값.

소스

module count_zeros(in, out, error);
  input [7:0] in;
  output [3:0] out;
  output error;
  function legal;
  input [7:0] x;
  reg seenZero, seenTrailing;
  integer i;
  begin : _legal_block
    legal = 1,seenZero = 0; seenTrailing = 0;
    for(i =0;i<=7;i=i+1)
      if(seenTrailing && (x[i] == 1'b0)) begin
        legal = 0;
        disable _legal_block;
        end;
      else if(seenZero && (x[i] == 1'b1))
        seenTrailing = 1;
      else if(x[i] == 1'b0)
        seenZero = 1;
    end
  endfunction

function [3:0] zeros;
input [7:0] x;
reg [3:0] count;
integer i;

begin
  count = 0;
  for(i=0;i<=7;i=i+1)
    if(x[i] == 1'b0) count = count +1;
    zeros = count;
  end
endfunction
wire is_legal = legal(in);
assign error = ! is_legal;
assign out = is_lega  zeros(in) : 1'b0;
endmodule



이 글은 스프링노트에서 작성되었습니다.

Posted by suvisor