CO Lab9 Task
CO Lab9 Task
UNIVERSITY OF ENGINEERING
AND TECHNOLOGY, TAXILA
GROUP:
B2
2011-EE-197
31-05-2013
Lab-09
TASK: 1
A) Write Gate Level Code For The Given Diagram
2011-EE-197
31-05-2013
Lab-09
2011-EE-197
31-05-2013
Lab-09
endmodule
/*__________4-BIT ADDER__________*/
module FBA(s,c,a,b,cin,v);//Define 4-bit adder
output [3:0]s;output c,v;//Declare I/O ports
input [3:0]a,b;input cin;
wire c1,c2,c3;//Declare internal nets
FA X0(s[0],c1,a[0],b[0],cin);//Instantiate four 1-bit full adders
FA X1(s[1],c2,a[1],b[1],c1);
FA X2(s[2],c3,a[2],b[2],c2);
FA X3(s[3],c,a[3],b[3],c3);
xor X4(v,c,c3);
endmodule
/*__________ARITHMETIC UNIT__________*/
module AU(G,c,x,y,S,cin,ov);//Define module for arithmetic unit
output [3:0]G;output c,ov;//Declare I/O ports
input [3:0]x,y;input [2:1]S;input cin;
wire [11:0]w;wire [3:0]v;//Declare internal nets
not X0(w[0],y[3]);//Instantiate logic gate primitives
not X1(w[1],y[2]);
not X2(w[2],y[1]);
not X3(w[3],y[0]);
and X4(w[4],y[3],S[1]);
and X5(w[5],w[0],S[2]);
and X6(w[6],y[2],S[1]);
and X7(w[7],w[1],S[2]);
and X8(w[8],y[1],S[1]);
and X9(w[9],w[2],S[2]);
and X10(w[10],y[0],S[1]);
and X11(w[11],w[3],S[2]);
2011-EE-197
31-05-2013
Lab-09
or X12(v[3],w[4],w[5]);
or X13(v[2],w[6],w[7]);
or X14(v[1],w[8],w[9]);
or X15(v[0],w[10],w[11]);
FBA X16(G,c,x,v,cin,ov);//Instantiate 4-bit full adder
endmodule
/*__________LOGIC UNIT__________*/
module LU(g,s,x,y);//Define module for logic unit
output g;//Declare I/O ports
input x,y;input [1:0]s;
not X0(w0,x);//Instantiate logic gate primitives
xor X1(w1,x,y);
or X2(w2,x,y);
and X3(w3,x,y);
MUX_4x1 X5(g,w3,w2,w1,w0,s);//Instantiate 4x1 Mux
endmodule
/*__________4-BIT LOGIC UNIT__________*/
module LU_4b(g,s,x,y);//Define module for 4-bit logic circuit
output [3:0]g;//Declare I/O ports
input [3:0]x,y;input [1:0]s;
LU X0(g[0],s,x[0],y[0]);//Instantiate four 1-bit logic circuits
LU X1(g[1],s,x[1],y[1]);
LU X2(g[2],s,x[2],y[2]);
LU X3(g[3],s,x[3],y[3]);
endmodule
/*__________ALU (COMBINING AU & LU)__________*/
module ALU(G,C,X,Y,S,V);//Define ALU module
output [3:0]G;output C,V;//Declare I/O ports
input [3:0]X,Y,S;
2011-EE-197
31-05-2013
Lab-09
2011-EE-197
31-05-2013
Lab-09
TASK: 2
A) Combine Register File & Function Unit
/*Subject: Computer Organization
Name: Ameer Hamza
Registration Number: 2011-EE-197
Group: B2
Lab Number: 9
2011-EE-197
31-05-2013
Lab-09
2011-EE-197
31-05-2013
Lab-09
endmodule
/*__________QUAD 4x1 MUX__________*/
module Q_MUX_4x1(Q,I0,I1,I2,I3,S);//Define module for 4x1 Quad Mux
output [3:0]Q;//Declare I/O ports
input [3:0]I0,I1,I2,I3;input [1:0]S;
MUX_4x1 X0(Q[0],I0[0],I1[0],I2[0],I3[0],S);//Instantiate 4x1 Mux four times
MUX_4x1 X1(Q[1],I0[1],I1[1],I2[1],I3[1],S);
MUX_4x1 X2(Q[2],I0[2],I1[2],I2[2],I3[2],S);
MUX_4x1 X3(Q[3],I0[3],I1[3],I2[3],I3[3],S);
endmodule
/*__________2x4 DECODER__________*/
module DEC_2x4(D,I);//Define module for 2x4 Decoder
output [3:0]D;//Declare I/O ports
input [1:0]I;
and X0(D[0],~I[0],~I[1]);//Instantiate logic gate primitives
and X1(D[1],I[0],~I[1]);
and X2(D[2],~I[0],I[1]);
and X3(D[3],I[0],I[1]);
endmodule
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||DESIGNING THE REGISTER FILE||||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*__________D-FLIP FLOP DESIGN__________*/
/*Using Behavorial Modeling for D-Flip Flop Design*/
module DFF(OUT,IN,RST,CLK);
parameter n=4;
output [n-1:0]OUT;
input [n-1:0]IN;
input RST;
2011-EE-197
31-05-2013
Lab-09
2011-EE-197
input CLK;
reg [n-1:0]OUT;
always @(posedge CLK)
begin
if (RST)
OUT=0;
else
OUT=IN;
end
endmodule
/*__________REGISTER__________*/
module REG(A,IN,LD,CLK,RST);//Define module for register
output [3:0]A;//Declare I/O ports
input [3:0]IN;input LD,CLK,RST;
wire [3:0]W;//Declare internal nets
Q_MUX_2x1 X0(W,LD,A,IN);//Instantiate 2x1 Quad Mux
DFF X1(A,W,RST,CLK);//Instantiate D-Flip Flop
endmodule
/*__________REGISTER HIERARCHY__________*/
module REG_HIER(R0_OUT,R1_OUT,R2_OUT,R3_OUT,DATA,LD,CLK,RST);//Define module
for registers hierarchy
output [3:0]R0_OUT,R1_OUT,R2_OUT,R3_OUT;//Declare I/O ports
input [3:0]DATA,LD;input CLK,RST;
REG X0(R0_OUT,DATA,LD[0],CLK,RST);//Instantiate register four times
REG X1(R1_OUT,DATA,LD[1],CLK,RST);
REG X2(R2_OUT,DATA,LD[2],CLK,RST);
REG X3(R3_OUT,DATA,LD[3],CLK,RST);
endmodule
/*__________REGISTER FILE__________*/
//Define module for Register File
31-05-2013
Lab-09
2011-EE-197
module REG_FILE(A_DATA,B_DATA,AS,BS,DS,LD_EN,D_DATA,CLK,RST);
output [3:0]A_DATA,B_DATA;//Declare I/O ports
input [1:0]AS,BS,DS;input LD_EN,CLK,RST;input [3:0]D_DATA;
wire [3:0]DEC_OUT,LOAD,R0_OUT,R1_OUT,R2_OUT,R3_OUT;//Declare internal nets
DEC_2x4 X0(DEC_OUT,DS);//Instantiate 2x4 Decoder
and X1(LOAD[0],LD_EN,DEC_OUT[0]);//Instantiate logic gate primitives
and X2(LOAD[1],LD_EN,DEC_OUT[1]);
and X3(LOAD[2],LD_EN,DEC_OUT[2]);
and X4(LOAD[3],LD_EN,DEC_OUT[3]);
//Instantiate register hierarchy
REG_HIER X5(R0_OUT,R1_OUT,R2_OUT,R3_OUT,D_DATA,LOAD,CLK,RST);
//Instantiate 4x1 Quad Mux
Q_MUX_4x1 X6(A_DATA,R0_OUT,R1_OUT,R2_OUT,R3_OUT,AS);
Q_MUX_4x1 X7(B_DATA,R0_OUT,R1_OUT,R2_OUT,R3_OUT,BS);
endmodule
/*||||||||||||||||||||||||||||||||||||||||||||||||||||||||
||||||||||DESIGNING THE FUNCTION UNIT|||||||||
||||||||||||||||||||||||||||||||||||||||||||||||||||||||*/
/*__________HALF ADDER__________*/
module HA(s,c,a,b);//Define half adder
output s,c;//Declare I/O ports
input a,b;
xor X0(s,a,b);//Instantiate logic gate primitives
and X1(c,a,b);
endmodule
/*__________FULL ADDER__________*/
module FA(s,c,a,b,cin);//Define full adder
output s,c;//Declare I/O ports
input a,b,cin;
31-05-2013
Lab-09
2011-EE-197
31-05-2013
Lab-09
and X8(w[8],y[1],S[1]);
and X9(w[9],w[2],S[2]);
and X10(w[10],y[0],S[1]);
and X11(w[11],w[3],S[2]);
or X12(v[3],w[4],w[5]);
or X13(v[2],w[6],w[7]);
or X14(v[1],w[8],w[9]);
or X15(v[0],w[10],w[11]);
FBA X16(G,c,x,v,cin,ov);//Instantiate 4-bit full adder
endmodule
/*__________LOGIC UNIT__________*/
module LU(g,s,x,y);//Define module for logic unit
output g;//Declare I/O ports
input x,y;input [1:0]s;
not X0(w0,x);//Instantiate logic gate primitives
xor X1(w1,x,y);
or X2(w2,x,y);
and X3(w3,x,y);
MUX_4x1 X5(g,w3,w2,w1,w0,s);//Instantiate 4x1 Mux
endmodule
/*__________4-BIT LOGIC UNIT__________*/
module LU_4b(g,s,x,y);//Define module for 4-bit logic circuit
output [3:0]g;//Declare I/O ports
input [3:0]x,y;input [1:0]s;
LU X0(g[0],s,x[0],y[0]);//Instantiate four 1-bit logic circuits
LU X1(g[1],s,x[1],y[1]);
LU X2(g[2],s,x[2],y[2]);
LU X3(g[3],s,x[3],y[3]);
endmodule
2011-EE-197
31-05-2013
Lab-09
2011-EE-197
31-05-2013
Lab-09
2011-EE-197
31-05-2013
Lab-09
2011-EE-197
31-05-2013
Wave:
Lab-09
2011-EE-197