2020年4月1日 星期三

4 bit Binary to Gray Code in Verilog (4位元 二進制轉格雷碼)

4 bit Binary to Gray Code in Verilog (4位元 二進制轉格雷碼)


1) Gate Level

//gate level 
module Bin_Gray(B,G);
input [3:0]B;
output [3:0]G;

buf u1(G[3],B[3]);
xor u2(G[2],B[3],B[2]);
xor u3(G[1],B[2],B[1]);
xor u4(G[0],B[1],B[0]);

endmodule

//===========================
// 時間單位 100ns, 時間精確度100 ps
`timescale 100ns/100ps    
module T;
/*
module Bin_Gray(B,G);
input [3:0]B;
output [3:0]G;
*/
// Inputs
reg [3:0]B=4'b0000;
// Outputs
wire [3:0]G;
integer i;

// Instantiate the Unit Under Test (UUT)
 Bin_Gray UUT(B,G);

initial begin
  $monitor (B,G);
  for(i=1; i<=18; i=i+1)
begin
#20 B=B+1;
end

end
endmodule

2) Dataflow Level
//Dataflow level 
module Bin_Gray(B,G);
input [3:0]B;
output [3:0]G;

assign G[3]=B[3];
assign G[2]= B[3] ^ B[2];
assign G[1]= B[2] ^ B[1];
assign G[0]= B[1] ^ B[0];

/*
buf u1(G[3],B[3]);
xor u2(G[2],B[3],B[2]);
xor u3(G[1],B[2],B[1]);
xor u4(G[0],B[1],B[0]);
*/
endmodule

3) Behavior Level if

//Behavior Level
module Bin_Gray(B,G);
input [3:0]B;
output reg [3:0]G;

always @ (*)
begin
if (B==0) G=4'b0000;
else if (B==1) G=4'b0001;
else if (B==2) G=4'b0011;
else if (B==3) G=4'b0010;
else if (B==4) G=4'b0110;
else if (B==5) G=4'b0111;
else if (B==6) G=4'b0101;
else if (B==7) G=4'b0100;
else if (B==8)  G=4'b1100;
else if (B==9)  G=4'b1101;
else if (B==10) G=4'b1111;
else if (B==11) G=4'b1110;
else if (B==12) G=4'b1010;
else if (B==13) G=4'b1011;
else if (B==14) G=4'b1001;
else  G=4'b1000;
end

endmodule

4) Behavior Level Case

//Behavior level  Case
module Bin_Gray(B,G);
input [3:0]B;
output reg [3:0]G;

always @ (*) begin
case (B)
  0 :  G=4'b0000;
  1 :  G=4'b0001;
  2 :  G=4'b0011;
  3 :  G=4'b0010;
  4 :  G=4'b0110;
  5 :  G=4'b0111;
  6 :  G=4'b0101;
  7 :  G=4'b0100;
  8 :  G=4'b1100;
  9 :  G=4'b1101;
  10 : G=4'b1111;
  11 : G=4'b1110;
  12 : G=4'b1010;
  13 : G=4'b1011;
  14 : G=4'b1001;
  15 : G=4'b1000;
endcase
end

endmodule




沒有留言:

張貼留言

2024產專班 作業2 (純模擬)

2024產專班 作業2  (純模擬) 1) LED ON,OFF,TIMER,FLASH 模擬 (switch 控制) 2)RFID卡號模擬 (buttom  模擬RFID UID(不從ESP32) Node-Red 程式 [{"id":"d8886...