본문 바로가기
전자공학/디지털회로

[Verilog] More Verilog Features

by 17Hyuk 2022. 12. 20.

https://hdlbits.01xz.net/wiki/Problem_sets

 

Problem sets - HDLBits

 

hdlbits.01xz.net

 

 

Reduction

parity비트

짝수 패리티 : 1의 개수 짝수개

홀수 패리티 : 1의 개수 홀수개

따라서 XOR 이용해서 생성하기

module top_module (
    input [7:0] in,
    output parity); 
    
    //assign parity = ^in;
    assign parity = ^in[7:0];

endmodule

 

 

※ 아래 코드처럼 bit연산 가능함

module top_module( 
    input [99:0] in,
    output out_and,
    output out_or,
    output out_xor 
);

    assign out_and = ∈
    assign out_or = |in;
    assign out_xor = ^in;
    
endmodule

 

 

Vector100r

for문을 통해서 벡터를 reverse 하기

module top_module( 
    input [99:0] in,
    output [99:0] out
);

    always @(*) begin
    	// for (int i=0;i<$bits(out);i++)		// $bits()
        for(integer i=0;i<100;i++) 
        	out[i] = in[99-i];
    end
    
endmodule

 

 

Adder100i

sum은 a+b+cin이고 cout은 그때의 carry이다

full adder를 참고하면 cout = ab + bc + ca 이므로

cout[0] = a[0], b[0], cin의 캐리연산이고

cout[1] = a[1], b[1], cout[0]의 캐리연산이다

이런식으로 구하면 아래 코드처럼 작성하면 된다.

module top_module( 
    input [99:0] a, b,
    input cin,
    output [99:0] cout,
    output [99:0] sum );
    
    assign sum = a + b + cin;
    assign cout[0] = a[0]&b[0] | b[0]&cin | cin&a[0];
    
    always @(*) begin
        for(int i=1; i<$bits(a); i++)
            cout[i] = a[i]&b[i] | b[i]&cout[i-1] | cout[i-1]&a[i];
    end

endmodule

 

 

'전자공학 > 디지털회로' 카테고리의 다른 글

[Verilog] Multiplexer  (1) 2022.12.21
[Verilog] Basic Gate  (0) 2022.12.20
[Verilog] Procedures  (0) 2022.12.15
[Verilog] Module  (0) 2022.12.14
[Verilog] Vector  (0) 2022.12.13

댓글