Beruflich Dokumente
Kultur Dokumente
// File Name
: encoder_using_if.sv
// Function
: Encoder using If
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module encoder_using_if(
output reg [3:0] binary_out , // 4 bit binary Output
input wire [15:0] encoder_in , // 16-bit Input
input wire
enable
// Enable for the encoder
);
//--------------Code Starts Here----------------------always_comb
begin
binary_out = 0;
if (enable) begin
if (encoder_in == 16'h0002) begin
binary_out = 1;
end if (encoder_in == 16'h0004) begin
binary_out = 2;
end if (encoder_in == 16'h0008) begin
binary_out = 3;
end if (encoder_in == 16'h0010) begin
binary_out = 4;
end if (encoder_in == 16'h0020) begin
binary_out = 5;
end if (encoder_in == 16'h0040) begin
binary_out = 6;
end if (encoder_in == 16'h0080) begin
binary_out = 7;
end if (encoder_in == 16'h0100) begin
binary_out = 8;
end if (encoder_in == 16'h0200) begin
binary_out = 9;
end if (encoder_in == 16'h0400) begin
binary_out = 10;
end if (encoder_in == 16'h0800) begin
binary_out = 11;
end if (encoder_in == 16'h1000) begin
binary_out = 12;
end if (encoder_in == 16'h2000) begin
binary_out = 13;
end if (encoder_in == 16'h4000) begin
binary_out = 14;
end if (encoder_in == 16'h8000) begin
binary_out = 15;
end
end
end
endmodule
//----------------------------------------------------// Design Name : encoder_using_case
// File Name
: encoder_using_case.sv
// Function
: Encoder using Case
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module encoder_using_case(
output reg [3:0] binary_out , // 4 bit binary Output
end else if
binary_out
end else if
binary_out
end else if
binary_out
end else if
binary_out
end else if
binary_out
end else if
binary_out
end else if
binary_out
end else if
binary_out
end
end
end
(encoder_in
= 8;
(encoder_in
= 9;
(encoder_in
= 10;
(encoder_in
= 11;
(encoder_in
= 12;
(encoder_in
= 13;
(encoder_in
= 14;
(encoder_in
= 15;
== {{7{1'bx}},1'b1,{8{1'b0}}}) begin
== {{6{1'bx}},1'b1,{9{1'b0}}}) begin
== {{5{1'bx}},1'b1,{10{1'b0}}}) begin
== {{4{1'bx}},1'b1,{11{1'b0}}}) begin
== {{3{1'bx}},1'b1,{12{1'b0}}}) begin
== {{2{1'bx}},1'b1,{13{1'b0}}}) begin
== {{1{1'bx}},1'b1,{14{1'b0}}}) begin
== {1'b1,{15{1'b0}}}) begin
endmodule
//----------------------------------------------------// Design Name : pri_encoder_using_assign
// File Name
: pri_encoder_using_assign.sv
// Function
: Pri Encoder using assign
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module pri_encoder_using_assign (
output wire [3:0] binary_out , // 4 bit binary output
input wire [15:0] encoder_in , // 16-bit input
input wire
enable
// Enable for the encoder
);
//--------------Code Starts Here----------------------assign binary_out = (!enable) ? 0 : (
(encoder_in == 16'bxxxx_xxxx_xxxx_xxx1) ? 0 :
(encoder_in == 16'bxxxx_xxxx_xxxx_xx10) ? 1 :
(encoder_in == 16'bxxxx_xxxx_xxxx_x100) ? 2 :
(encoder_in == 16'bxxxx_xxxx_xxxx_1000) ? 3 :
(encoder_in == 16'bxxxx_xxxx_xxx1_0000) ? 4 :
(encoder_in == 16'bxxxx_xxxx_xx10_0000) ? 5 :
(encoder_in == 16'bxxxx_xxxx_x100_0000) ? 6 :
(encoder_in == 16'bxxxx_xxxx_1000_0000) ? 7 :
(encoder_in == 16'bxxxx_xxx1_0000_0000) ? 8 :
(encoder_in == 16'bxxxx_xx10_0000_0000) ? 9 :
(encoder_in == 16'bxxxx_x100_0000_0000) ? 10 :
(encoder_in == 16'bxxxx_1000_0000_0000) ? 11 :
(encoder_in == 16'bxxx1_0000_0000_0000) ? 12 :
(encoder_in == 16'bxx10_0000_0000_0000) ? 13 :
(encoder_in == 16'bx100_0000_0000_0000) ? 14 : 15);
endmodule
//----------------------------------------------------// Design Name : decoder_using_case
// File Name
: decoder_using_case.sv
// Function
: decoder using case
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module decoder_using_case (
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
=
16'h0001;
16'h0002;
16'h0004;
16'h0008;
16'h0010;
16'h0020;
16'h0040;
16'h0080;
16'h0100;
16'h0200;
16'h0400;
16'h0800;
16'h1000;
16'h2000;
16'h4000;
16'h8000;
endmodule
//----------------------------------------------------// Design Name : decoder_using_assign
// File Name
: decoder_using_assign.sv
// Function
: decoder using assign
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module decoder_using_assign (
input wire [3:0] binary_in
, // 4 bit binary input
output wire [15:0] decoder_out , // 16-bit out
input wire
enable
// Enable for the decoder
);
//--------------Code Starts Here----------------------assign decoder_out = (enable) ? (1 << binary_in) : 16'b0 ;
endmodule
//----------------------------------------------------// Design Name : mux_using_assign
// File Name
: mux_using_assign.sv
// Function
: 2:1 Mux using Assign
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module mux_using_assign (
input wire din_0
, // Mux first input
input wire din_1
, // Mux Second input
input wire sel
, // Select input
output wire mux_out
// Mux output
);
//-------------Code Start----------------assign mux_out = (sel) ? din_1 : din_0;
endmodule //End Of Module mux
//----------------------------------------------------// Design Name : mux_using_if
// File Name
: mux_using_if.sv
// Function
: 2:1 Mux using If
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module mux_using_if(
input wire din_0
, // Mux first input
input wire din_1
, // Mux Second input
input wire sel
, // Select input
output reg
mux_out
// Mux output
);
//-------------Code Starts Here--------always_comb
begin : MUX
if (sel == 1'b0) begin
mux_out = din_0;
end else begin
mux_out = din_1 ;
end
end
endmodule //End Of Module mux
//----------------------------------------------------// Design Name : mux_using_case
// File Name
: mux_using_case.sv
// Function
: 2:1 Mux using Case
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module mux_using_case(
input wire din_0
, // Mux first input
input wire din_1
, // Mux Second input
input wire sel
, // Select input
output reg
mux_out
// Mux output
);
//-------------Code Starts Here--------always @ (*)
MUX : begin
case (sel)
1'b0 : mux_out = din_0;
1'b1 : mux_out = din_1;
endcase
end
endmodule //End Of Module mux
//----------------------------------------------------// Design Name : dff_async_reset
// File Name
: dff_async_reset.sv
// Function
: D flip-flop async reset
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module dff_async_reset (
input wire data , // Data Input
input wire clk
, // Clock Input
input wire reset , // Reset input
output reg q
// Q output
);
//-------------Code Starts Here--------always_ff @ ( posedge clk iff reset == 1 or negedge reset)
if (~reset) begin
q <= 1'b0;
end else begin
q <= data;
end
endmodule //End Of Module dff_async_reset
//----------------------------------------------------// Design Name : dff_sync_reset
// File Name
: dff_sync_reset.sv
// Function
: D flip-flop sync reset
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module dff_sync_reset (
input wire data , // Data Input
input wire clk
, // Clock Input
input wire reset , // Reset input
output reg q
// Q output
);
//-------------Code Starts Here--------always_ff @ ( posedge clk)
if (~reset) begin
q <= 1'b0;
end else begin
q <= data;
end
endmodule //End Of Module dff_sync_reset
//----------------------------------------------------// Design Name : tff_async_reset
// File Name
: tff_async_reset.sv
// Function
: T flip-flop async reset
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module tff_async_reset (
input
wire data , // Data Input
input
wire clk
, // Clock Input
input
wire reset , // Reset input
output reg
q
// Q output
);
//-------------Code Starts Here--------always_ff @ ( posedge clk iff reset == 1 or negedge reset)
if (~reset) begin
q <= 1'b0;
end else if (data) begin
q <= !q;
end
endmodule
Gray Counter
//----------------------------------------------------// Design Name : gray_counter
// File Name
: gray_counter.sv
// Function
: 8 bit gray counter
// Coder
: Deepak
//----------------------------------------------------module gray_counter (
output wire [7:0] out
, // counter out
input wire
enable , // enable for counter
input wire
clk
, // clock
input wire
rst
// active hight reset
);
//------------Internal Variables-------reg [7:0] count;
//-------------Code Starts Here--------always_ff @ (posedge clk)
if (rst) begin
count <= 0;
end else if (enable) begin
count <= count + 1;
end
assign out = { count[7], (count[7]
count[5]),(count[5] ^
count[3]),(count[3] ^
count[1]),(count[1] ^
^ count[6]),(count[6] ^
count[4]), (count[4] ^
count[2]), (count[2] ^
count[0]) };
endmodule
Divide by 2 Counter
//----------------------------------------------------// Design Name : clk_div
// File Name
: clk_div.sv
// Function
: Divide by two counter
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module clk_div (
input wire clk_in,
input wire enable,
input wire reset,
output reg
clk_out
);
//--------------Code Starts Here----------------------always_ff @ (posedge clk_in)
if (reset) begin
clk_out <= 1'b0;
end else if (enable) begin
clk_out <= !clk_out ;
end
endmodule
Divide By 3 Counter
//----------------------------------------------------// Design Name : divide_by_3
// File Name
: divide_by_3.sv
// Function
: Divide By 3
// Coder
: Deepak Kumar Tala
//----------------------------------------------------module divide_by_3 (
input
wire
clk_in
, //Input Clock
input
wire
reset
, // Reset Input
output wire
clk_out
// Output Clock
);
//------------Internal Variables-------reg [1:0] pos_cnt;
reg [1:0] neg_cnt;
//-------------Code Start----------------// Posedge counter
always_ff @ (posedge clk_in)
if (reset) begin
pos_cnt <= 0;
end else begin
pos_cnt <= (pos_cnt == 2) ? 0 : pos_cnt + 1;
end
// Neg edge counter
always_ff @ (negedge clk_in)
if (reset) begin
neg_cnt <= 0;
end else begin
neg_cnt <= (neg_cnt == 2) ? 0 : neg_cnt + 1;
end
assign clk_out = ((pos_cnt != 2) && (neg_cnt != 2));
endmodule
endmodule
Como foi visto no Captulo 2, a lgica combinacional pode conter perigos. A lgica de estado
prximo da Moore e mquinas Mealy simplesmente um bloco de lgica combinacional com
uma srie de entradas e uma srie de sadas. A existncia de riscos neste prximo lgica Estado
poderia fazer com que o sistema para ir para um estado incorreto. Existem duas maneiras para
evitar tal problema: ou o prximo estado lgica deve incluir a lgica redundante necessria para
suprimir o risco ou a mquina de estado devem ser concebidos de tal modo que um perigo
permitido ocorrer, mas ignorado. A primeira soluo no ideal, como o prximo lgica do
Estado mais complexo; da, a segunda abordagem utilizada. (Note-se que os sistemas
assncronos so suscetveis a riscos e no prximo lgica Estado deve evitar qualquer perigo de
ocorrer, o que uma razo pela qual os sistemas sncronos so geralmente preferidos.) Para
garantir que os sistemas seqenciais so capazes de ignorar os perigos, um relgio usado para
sincronizar dados. Quando o relgio invlido, todos os perigos que ocorrem podem ser
ignorados.
Uma tcnica simples, portanto, a lgica e um sinal de relgio com os sinais do sistema - quando
o relgio est na lgica 0, quaisquer perigos seria ignorado. O sistema , no entanto, ainda
susceptvel a riscos enquanto o relgio alta. comum, por conseguinte, ao utilizar registos que
s so sensveis a sinais de entrada, enquanto o relgio est a mudar. O clock muito curto em
comparao com o perodo do relgio. Portanto, os dados s tem de ser estvel durante a
durao da alterao do relgio, com pequenas tolerncias, antes e depois da borda do relgio.
Estes parmetros de tolerncia de tempo so conhecidos como a configurao e tempo de
espera (TSETUP, Thold), respectivamente, como mostrado na Figura 6.3. Os registros estaduais
para uma mquina de estados sncrona so, portanto, borda desencadeou-D fl ip- fl ops. Outros
tipos de fl ip- fl op pode ser usada para projetar sistemas sncronos, mas eles oferecem algumas
vantagens e no so comuns na lgica programvel.