適用於DE2-70
//------------------------------------------------
// 16-bit Comparator using 4-bit-comparator task &
// 4-bit-comparator function
// filename: compare16.v
//------------------------------------------------
module comp_16_t_tf(SW, LEDR, LEDG , CLOCK_27 ,KEY ,HEX0 ,HEX1 ,HEX2,HEX3 ,HEX4 );
input [17:0] SW; // toggle switches
input [3:0] KEY; // Push bottom
input CLOCK_27; //Clock 27MHz , 50Mhz
output [17:0] LEDR; // red LEDS
output [7:0] LEDG; // green LEDs
output [6:0] HEX0,HEX1,HEX2,HEX3,HEX4; //7-segment display
// set original program input , output
// ( A, B, Yout );
// input [15:0] A ; //16-bit input A and B
// input [15:0] B ;
// output [2:0] Yout;
// Yout[2]=1'b1 -->Great than,
// Yout[1]=1'b1 --> Equal, and
//Yout[0]=1'b1 -->Less than
reg [15:0] A,B ; //16-bit input A and B
//set SW17=1 then A input , SW=0 Then B input , Enable is KEY[0]
reg [7:0] A8bit; // 4-bit data in input A
reg [7:0] B8bit; // 4-bit data in input B
reg [2:0] Yout ;
reg [6:0] HEX0,HEX1,HEX2,HEX3,HEX4;
assign LEDR=SW;
assign LEDG[3]=KEY[3];
always @( negedge KEY[3] ) //Setting A or B value
begin
if (SW[17])
A<=SW[15:0]; //SW17=1 then Setting A value
else
B<=SW[15:0]; //SW17=0 then Setting B value
end
always @( A or B )
begin
A8bit = A[15:8] ;
B8bit = B[15:8] ;
compare8( A8bit , B8bit , Yout );
if( Yout == 3'b010 ) // A[15:12] == B[15:12]
begin
A8bit = A[7:0] ;
B8bit = B[7:0] ;
compare8( A8bit , B8bit , Yout ) ;
end
end
//=================================
always @( Yout )
begin
case (Yout)
3'b010 :
begin
HEX4=7'b0000110; //e
HEX3=7'b0011000; //q
HEX2=7'b1100011; //u
HEX1=7'b0000110; //e
HEX0=7'b1000111; //l
end
3'b100 :
begin
HEX4=7'b0000010; //g
HEX3=7'b0001101; //r
HEX2=7'b0000110; //e
HEX1=7'b0001000; //a
HEX0=7'b0000111; //t
end
3'b001 :
begin
HEX4=7'b1111111; //
HEX3=7'b1000111; //L
HEX2=7'b0000110; //e
HEX1=7'b0010010; //s
HEX0=7'b0010010; //s
end
default :
begin
HEX4=7'b1111111; //
HEX3=7'b1111111; //
HEX2=7'b1111111; //
HEX1=7'b1111111; //
HEX0=7'b1111111; //
end
endcase
end
//=================================
// Task for a 8-bit comprator
task compare8 ;
input [7:0] I_A ; //4-bit input I_A and I_B
input [7:0] I_B ;
output [2:0] S ; //S[2]=1'b1 -->Great than, S[1]=1'b1 --> Equal, and
//S[0]=1'b1 -->Less than
reg [3:0] A4bit;
reg [3:0] B4bit;
begin
A4bit=I_A[7:4];
B4bit=I_B[7:4];
compare4_T(A4bit,B4bit,S); //Call Task
if( S == 3'b010) // Equal )
begin
A4bit=I_A[3:0];
B4bit=I_B[3:0];
S=compare4_F(A4bit,B4bit); //Call Function
end
end
endtask
//
//===================================
// Task for a 4-bit comprator
//===================================
task compare4_T ;
input [3:0] I_A ; //4-bit input I_A and I_B
input [3:0] I_B ;
output [2:0] S ; //S[2]=1'b1 -->Great than, S[1]=1'b1 --> Equal, and
//S[0]=1'b1 -->Less than
begin
if( I_A > I_B )
S = 3'b100 ; // Great than
else if( I_A == I_B )
S = 3'b010 ; // Equal
else
S = 3'b001 ; // Less than
end
endtask
//===================================
// Function for a 4-bit comprator
//===================================
function [2:0] compare4_F;
input [3:0] I_A ; //4-bit input I_A and I_B
input [3:0] I_B ;
// output [2:0] S ; //S[2]=1'b1 -->Great than, S[1]=1'b1 --> Equal, and
//S[0]=1'b1 -->Less than
begin
if( I_A > I_B )
compare4_F = 3'b100 ; // Great than
else if( I_A == I_B )
compare4_F = 3'b010 ; // Equal
else
compare4_F = 3'b001 ; // Less than
end
endfunction
//
endmodule
沒有留言:
張貼留言