Академический Документы
Профессиональный Документы
Культура Документы
module all_logic_gate_level(or_out,and_out,xor_out,not_out,nand_out,nor_out,xnor_out,x,y);
input x,y;
output or_out,and_out,xor_out,not_out,nand_out,nor_out,xnor_out;
or g1(or_out,x,y);
and g2(and_out,x,y);
xor g3(xor_out,x,y);
not g4(not_out,x);
nand g5(nand_out,x,y);
nor g6(nor_out,x,y);
xnor g7(xnor_out,x,y);
endmodule
All Logic Gates (Dataflow modelling)
module all_logic_dataflow(or_out,and_out,xor_out,not_out,nand_out,nor_out,xnor_out,x,y);
input x,y;
output or_out,and_out,xor_out,not_out,nand_out,nor_out,xnor_out;
assign or_out = x|y;
assign and_out = x&y;
assign xor_out = x^y;
assign not_out = ~x;
assign nand_out = ~(x&y);
assign nor_out = ~ (x|y);
assign xnor_out = ~(x^y);
endmodule
OR Gate (Behaviour Modelling )
module or_behav(y,a,b);
output y;
input a,b;
reg y;
always @ (a or b)
begin
if (a == 0)
begin
if (b == 0)
y = 1 'b 0;
else
y = 1 'b 1;
end
else
y = 1 'b1;
end
endmodule
2'b00:y=d[0];
2'b01:y=d[0];
2'b10:y=d[0];
2'b11:y=d[0];
default:y=1'bx;
endcase
end
endmodule
4x1 Multiplexer Behaviour (IF) Modelling
module mux_4X1_behav_if(d,sel,y);
input [3:0]d;
input [1:0]sel;
output y;
reg y;
always @(d or sel)
begin
if (sel==00)
y=d[0];
else if (sel==01)
y=d[1];
else if (sel==10)
y=d[2];
else
y=d[3];
end
endmodule
else
if (sel==00)
dout=4'b0001;
else if (sel==01)
dout=4'b0010;
else if (sel==10)
dout=4'b0100;
else
dout=4'b1000;
end
endmodule
1X4 De-Multiplexer Behaviour Level Modelling
module demux_1X4_behav_case(din, sel, dout);
input din;
input [1:0] sel;
output [3:0] dout;
reg [3:0] dout;
always @ (din or sel)
case(sel)
2'b00: dout[0]=din;
2'b01: dout[1]=din;
2'b10: dout[2]=din;
2'b11: dout[3]=din;
endcase
endmodule
1X4 De-Multiplexer Dataflow Modelling
module demux_1X4_dataflow(din, sel, dout);
input din;
input [1:0]sel;
output [3:0]dout;
assign dout[0]=(~sel[0] & ~sel[1] & din);
assign dout[1]=(sel[0] & ~sel[1] & din);
assign dout[2]=(~sel[0] & sel[1] & din);
assign dout[3]=(sel[0] & sel[1] & din);
endmodule
4X2 Encoder Behaviour (CASE) Modelling
module encoder_4x2_behav_case (y,x);
output [0:3] y;
input [0:3] x;
reg [0:1] y;
always @ (x)
case (x)
4'b0001: y = 11;
4'b0010: y = 10;
4'b0100: y = 01;
4'b1000: y = 00;
//default : y=2'bzz;
default : $display ("Invalid Input");
endcase
endmodule
4- Bit Comparator
module compare(a, b, aeqb, agtb, altb);
input [3:0] a, b;
output aeqb, agtb, altb;
reg aeqb, agtb, altb;
always @ (a or b)
begin
if (a==b)
aeqb=1'b1;
else if (a>b)
agtb=1'b1;
else
altb=1'b1;
end
endmodule
module addsub (
input [7:0] dataa,
input [7:0] datab,
input add_sub,
// if this is 1, add; else subtract
input clk,
output reg [8:0] result);
always @ (posedge clk)
begin
if (add_sub)
result <= dataa + datab;
else
result <= dataa - datab;
end
endmodule
module dff(data,clk,reset,q,qb);
input data, clk, reset ;
output q,qb;
reg q,qb;
always @ ( posedge clk or posedge reset )
begin
if (reset)
begin
q <= 1'b0;
qb <= 1'b1;
end
else
begin
q <= data;
qb<= ~data;
end
end
endmodule
module dff_behav(q,qbar,d,clk,clr);
output q,qbar;
input d,clk,clr;
reg q,qbar;
always @ (clr or posedge clk)
begin
if (~clr)
begin
q = 1'b0;
qbar = 1'b1;
end
else
begin
q = d;
assign qbar = ~q;
end
end
endmodule
module dff_gate (q,qbar,d,clk,clr);
output q,qbar;
input d,clk,clr;
wire temp1,temp2,temp3,temp4,high;
assign high = 1'b1;
nand na0 (temp1,d,clk);
nand na1 (temp2,~d,clk);
nand na2 (temp3,qbar,temp1);
nand na3 (temp4,q,temp2);
and a0 (q,temp3,clr);
and a1 (qbar,temp4,high);
endmodule
module srff_behav (q,qbar,s,r,clk,clr);
output q,qbar;
input s,r,clk,clr;
reg q,qbar;
always @ (posedge clk or clr)
if (clr)
begin
if (clk)
begin
if (s ==0)
begin
if(r ==0)
begin
q = q;
qbar = qbar;
end
else
begin
q = 0;
qbar = 1;
end
end
else
begin
if (r ==0)
begin
q = 1;
qbar = 0;
end
else
begin
end
end
end
else
begin
q = 1'bz;
qbar = 1'bz;
end
q = 1'b0;
qbar = 1'b1;
end
endmodule
begin
q = ~q;
qbar = ~qbar;
end
else
begin
q = 1'bz;
qbar = 1'bz;
end
end
endmodule
//Counter Design
module counter(clk, reset,result); //,ena
input clk, reset;
output [3:0]result;
reg [3:0] result;
always @(negedge clk or posedge reset)
begin
if (reset)
result = 0;
else
result = result + 1;
end
endmodule
if(reset)
out <=4'b0000;
else
if (out==4'b1001)
out<=0;
else
out<=out+1;
end
endmodule
begin
if(cntrl)
begin
sr[0] <= pr_in[0];
sr[1] <= pr_in[1];
sr[2] <= pr_in[2];
sr[3] <= pr_in[3];
end
else
begin
sr[0] <= pr_in[0];
sr[1] <= sr[0];
sr[2] <= sr[1];
sr[3] <= sr[2];
end
end
assign sr_out = sr[3];
endmodule