Wednesday, 26 December 2012

one digit BCD ADDER


module bcdadd(a,b,cin,s,cout);  //Main module of one digit BCD adder
  input [3:0]a;
  input [3:0]b;
  input cin;
  output[3:0]s;
  output cout;
  wire [3:0]w;
  wire y,c0,c1,c2,c3,c4,c5;
  fulladd m1(a[0],b[0],cin,w[0],c0);
  fulladd m2(a[1],b[1],c0,w[1],c1);
  fulladd m3(a[2],b[2],c1,w[2],c2);
  fulladd m4(a[3],b[3],c2,w[3],c3);
  assign y=c3|(w[3]&(w[2]|w[1]));
  halfadd m5(w[1],y,s[1],c4);
  fulladd m6(w[2],y,c4,s[2],c5);
  halfadd m7(w[3],c5,s[3],cout);
  assign s[0]=w[0];
endmodule
                                                      
module fulladd(a,b,cin,s,cout); //submodule for fulladder
  input a,b;
  input cin;
  output s;
  output cout;
  wire w1,w2,w3;
  halfadd g1(a,b,w1,w2);
  halfadd g2(w1,cin,s,w3);
  assign cout=w3|w2;
endmodule

module halfadd(a,b,s,cout);  //submodule for halfadder
  input a,b;
  output s,cout;
assign s=a^b;
assign cout=a&b;
endmodule

FIR FILTER UNIT

// main module FIR
module filterfir(clk,rst,x,dataout);
input [7:0]x;
input clk,rst;
output [9:0]dataout;
wire [7:0]d1,d2,d3;
wire [7:0]m1,m2,m3,m4,m5;
wire [7:0]d11,d12,d13,d14;
parameter h0=3'b101;
parameter h1=3'b100;
parameter h2=3'b011;
parameter h3=3'b010;
parameter h4=3'b001;
assign m1=x>>h0;
dff u2(clk,rst,x,d11);
assign m2=d11>>h1;
assign d1=m1+m2;
dff u4(clk,rst,d11,d12);
assign m3=d12>>h2;
assign d2=d1+m3;
dff u6(clk,rst,d12,d13);
assign m4=d13>>h3;
assign d3=d2+m4;
dff u8(clk,rst,d13,d14);
assign m5=d14>>h4;
assign dataout=d3+m5;
endmodule


module dff(clk,rst,d,q);// sub module d flipflop
input clk,rst;
input [7:0]d;
output [7:0]q;
reg [7:0]q;
always@(posedge clk)
begin
if(rst==1)
begin
q=0;
end
else
begin
q=d;
end
end
endmodule

FULL ADDER


module fa(s,cout,a,b,cin);
//sub module for Full adder
input a,b,cin;
output s,cout;
wire w1,w2,w3;
ha m1(w1,w2,a,b);
ha m2(s,w3,w1,cin);
or m3(cout,w2,w3);
endmodule

module ha(s,cout,a,b);
 //sub module for Half adder
  input a,b;
  output s,cout;
  xor m1(s,a,b);
  and m2(cout,a,b);
endmodule

HALF ADDER


 module ha(s,cout,a,b);
 //sub module for Half adder
  input a,b;
  output s,cout;
  xor m1(s,a,b);
  and m2(cout,a,b);
endmodule

16 bit CARRY LOOKAHEAD ADDER


module lac16(s,cout,a,b,cin); //main module of 16 bit look ahead carry adder
  output [15:0]s;
  output cout;
  input [15:0]a;
  input [15:0]b;
  input cin;
  wire [15:0]p,g,c;
  assign p[15:0]=a[15:0]^b[15:0];
  assign g[15:0]=a[15:0]&b[15:0];
  assign c[0]=g[0]|(p[0]&cin);
  assign c[15:1]=g[15:1]|(p[15:1]&c[15:0]);
  assign s[0]=p[0]^cin;
  assign s[15:1]=p[15:1]^c[15:0];
  assign cout=c[15];
endmodule

16 bit RIPPLE CARRY ADDER


module rip(s,cout,a,b,cin);
//main module of 16 bit Ripple carry adder
input [15:0]a;
input [15:0]b;
input cin;
output cout;
output [15:0]s;
wire c4,c8,c12,cout;
rip2 m1(s[3:0],c4,a[3:0],b[3:0],cin);
rip2 m2(s[7:4],c8,a[7:4],b[7:4],c4);
rip2 m3(s[11:8],c12,a[11:8],b[11:8],c8);
rip2 m4(s[15:12],cout,a[15:12],b[15:12],c12);
endmodule

module rip2(s,cout,a,b,cin);
 //sub module for 4 bit Ripple carry adder
  input [3:0]a;
  input [3:0]b;
  input cin;
  output cout;
  output [3:0]s;
  wire c2,c3,c4,cout;
  fa m1(s[0],c2,a[0],b[0],cin);
  fa m2(s[1],c3,a[1],b[1],c2);
  fa m3(s[2],c4,a[2],b[2],c3);
  fa m4(s[3],cout,a[3],b[3],c4);
endmodule

module fa(s,cout,a,b,cin);
//sub module for Full adder
input a,b,cin;
output s,cout;
wire w1,w2,w3;
ha m1(w1,w2,a,b);
ha m2(s,w3,w1,cin);
or m3(cout,w2,w3);
endmodule

module ha(s,cout,a,b);
 //sub module for Half adder
  input a,b;
  output s,cout;
  xor m1(s,a,b);
  and m2(cout,a,b);
endmodule

8 bit SIMPLE PROCESSOR


//Main module for processor
module process(opcode,data,clk,rst,fout);
input [9:0]opcode;
input [7:0]data;
input clk,rst;
output [7:0]fout;
wire [7:0]r1,r2,r3,r4,a1,aout,bout,r11,r22,r33,r44;
wire [2:0]s,d;
wire [3:0]alf,de;
wire g;
control con(opcode,clk,alf,de,s,d,g);
muxl mu1(r11,r22,r33,r44,data,s,clk,a1);
alu al1(r1,a1,alf,clk,g,aout);
bus bu1(aout,clk,bout);
demux dd1(bout,d,clk,r1,r2,r3,r4);
regs reg1(r1,rst,clk,r11);
regs reg2(r2,rst,clk,r22);
regs reg3(r3,rst,clk,r33);
regs reg4(r4,rst,clk,r44);
assign fout=aout;
endmodule

//Submodule for control unit
module control(opcode,clk,alufn,dd,sou,des,r);
input [9:0]opcode;
input clk;
output reg[3:0]alufn,dd;
output reg[2:0]sou,des;
output reg r;
always@(posedge clk)
begin
dd=opcode[9:6];
sou=opcode[5:3];
des=opcode[2:0];
case(dd)
0:begin
r=1'b1;
alufn=4'b0000;
end
1:begin
r=1'b0;
alufn=4'b0000;
end
2:begin
r=1'b0;
alufn=4'b0001;
end
3:begin
r=1'b0;
alufn=4'b0010;
end
4:begin
r=1'b0;
alufn=4'b0011;
end
5:begin
r=1'b0;
alufn=4'b0100;
end
6:begin
r=1'b0;
alufn=4'b0101;
end
7:begin
r=1'b0;
alufn=4'b0110;
end
8:begin
r=1'b0;
alufn=4'b0111;
end
9:begin
r=1'b0;
alufn=4'b1000;
end
10:begin
r=1'b0;
alufn=4'b1001;
end
11:begin
r=1'b0;
alufn=4'b1010;
end
12:begin
r=1'b0;
alufn=4'b1011;
end
endcase
end
endmodule

//Main module for Arithmetic and Logic Unit
module alu(a,b,ch,clk,rst,out);
input [7:0]a,b;
input clk,rst;
input[3:0]ch;
output [7:0]out;
reg [7:0]out;
wire [7:0]q;
div8 s1(a,b,q);
always@(posedge clk)
begin
if(rst)
begin
out=8'b0;
end
else if(!rst)
begin
case(ch)
0 : out=a+b;
1 : out=a-b;
2 : out=a*b;
3 : out=q;
4 : out=~a;
5 : out=a&b;
6: out=a|b;
7 : out=a^b;
8 : out=b<<1;
9 : out=b>>1;
10 : out={b[0],b[7:1]};
11 : out={b[6:0],b[7]};
endcase
end
end
endmodule

//Sub module for 8-bit Divider
module div8(divs,div,q);
input [7:0]divs,div;
output [7:0]q;
wire [3:0]r1,r2,r3,r4,r5;
assign q[7:5]=0;
div4 d1(divs[7:4],div[3:0],r1[3:0],q[4]);
div4 d2({r1[2:0],divs[3]},div[3:0],r2[3:0],q[3]);
div4 d3({r2[2:0],divs[2]},div[3:0],r3[3:0],q[2]);
div4 d4({r3[2:0],divs[1]},div[3:0],r4[3:0],q[1]);
div4 d5({r4[2:0],divs[0]},div[3:0],r5[3:0],q[0]);
endmodule

//Sub module for 4-bit Division
module div4(divs,div,rem,q);
input [3:0]divs,div;
output [3:0]rem;
output q;
reg [3:0]rem;
reg q;
wire [3:0]r,c;
cas1 c1(divs[0],div[0],1'b0,r[0],c[0]);
cas1 c2(divs[1],div[1],c[0],r[1],c[1]);
cas1 c3(divs[2],div[2],c[1],r[2],c[2]);
cas1 c4(divs[3],div[3],c[2],r[3],c[3]);
always@(divs or c or r)
begin
if(c[3])
begin
q=1'b0;
rem=divs;
end
else
begin
q=1'b1;
rem=r;
end
end
endmodule

//Sub module for CAS
module cas1(a,b,c,diff,brow);
input a,b,c;
output diff,brow;
assign diff=a^b^c;
assign brow=((~a)&b)|(b&c)|((~a)&c);
endmodule

//Main module for demux
module demux(busout,des,clk,rea,reb,rec,red);
input [7:0]busout;
input clk;
input[2:0]des;
output reg[7:0]rea,reb,rec,red;
always@(posedge clk)
begin
case(des)
0:begin
rea=8'b0;
reb=8'b0;
rec=8'b0;
red=8'b0;
end
1:rea=busout;
2:reb=busout;
3:rec=busout;
4:red=busout;
endcase
end
endmodule

//Main module for MUX
module muxl(rea1,reb1,rec1,red1,exin,sou,clk,alin);
input [7:0]rea1,reb1,rec1,red1,exin;
input[2:0]sou;
input clk;
output reg [7:0]alin;
always@(posedge clk)
begin
case(sou)
0:alin=8'b0;
1:alin=rea1;
2:alin=reb1;
3:alin=rec1;
4:alin=red1;
5:alin=exin;
endcase
end
endmodule

//Main module for bus
module bus(aluout,clk,buout);
input[7:0]aluout;
input clk;
output[7:0]buout;
reg [7:0]buout;
always@(posedge clk)
begin
buout=aluout;
end
endmodule

//Main module for reg
module regs(re,rst,clk,re1);
input [7:0]re;
input rst,clk;
output [7:0]re1;
reg [7:0]re1;
always@(posedge clk)
begin
if(rst)
begin
re1=8'b0;
end
else
begin
re1=re;
end
end
endmodule

CONTROL UNIT


//Main module for control unit
module control(op,clk,rst,s,d,sd,alucon,decode);
input [9:0]op;
input clk;
output [2:0]s,d;
output [5:0]decode;
output [3:0]alucon;
output rst,sd;
reg [3:0]in;
reg [5:0]decode;
reg [9:0]r;
reg [3:0]alucon;
reg[2:0]s,d;
reg rst,sd;
initial
begin
decode=6'b100000;
end
always@(posedge clk)
begin
if(decode==6'b100000)
begin
in=op[9:6];
s=op[2:0];
r=op;
decode={1'b0,decode[5:1]};
case(in)
0 : begin//clr
rst=1'b1;
sd=1'b1;
d=3'b000;
alucon=4'b0000;
end
1 : begin//mov
rst=1'b0;
sd=1'b1;
d=op[5:3];
alucon=4'b0000;
end
2 : begin//add operation
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b0001;
end
3 : begin//subtraction operation
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b0010;
end
4 : begin//multiplication operation
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b0011;
end
5 : begin//division operation
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b0100;
end
6 : begin//and operation
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b0101;
end
7 : begin//or operation
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b0110;
end
8 : begin//xor operation
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b0111;
end
9 : begin//left shfit
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b1000;
end
10 : begin//right shift
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b1001;
end
11 : begin//rotate left
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b1010;
end
12 : begin//rotate right
rst=1'b0;
sd=1'b0;
d=3'b001;
alucon=4'b1011;
end
default: $display("invalid operation");
endcase
end
else if(decode==6'b0)
begin
if(r==op)
decode=6'b000000;
else
decode=6'b100000;
end
else
decode={1'b0,decode[5:1]};
end
endmodule

8 bit ALU UNIT


//main module for alu
module alu(a,b,op,decode,clk,out);
input [7:0]a,b;
input [5:0]decode;
input clk;
input[3:0]op;
output [7:0]out;
reg [7:0]out;
wire [7:0]quo;
div8 s1(a,b,quo);
always@(posedge clk)
begin
if(decode==6'b000100)
begin
case(op)
0 : out=a;
1 : out=a+b;
2 : out=a-b;
3 : out=a*b;
4 : out=quo;
5 : out=a&b;
6: out=a|b;
7 : out=a^b;
8 : out=b<<1;
9 : out=b>>1;
10 : out={b[0],b[7:1]};
11 : out={b[6:0],b[7]};
endcase
end
end
endmodule

//submodule
module div8(divs,divd,quo);
input [7:0]divs;
input [7:0]divd;
output [7:0]quo;
wire [3:0]r1,r2,r3,r4,r5;
assign quo[7:5]=0;
div4 d1(divs[7:4],divd[3:0],r1[3:0],quo[4]);
div4 d2({r1[2:0],divs[3]},divd[3:0],r2[3:0],
quo[3]);




div4 d3({r2[2:0],divs[2]},divd[3:0],r3[3:0],
quo[2]);
div4 d4({r3[2:0],divs[1]},divd[3:0],r4[3:0],
quo[1]);
div4 d5({r4[2:0],divs[0]},divd[3:0],r5[3:0],
quo[0]);
endmodule

//sub module
module div4(divs,divd,rem,quo);
input [3:0]divs,divd;
output [3:0]rem;
output quo;
reg [3:0]rem;
reg quo;
wire [3:0]r,c;
cas1 c1(divs[0],divd[0],1'b0,r[0],c[0]);
cas1 c2(divs[1],divd[1],c[0],r[1],c[1]);
cas1 c3(divs[2],divd[2],c[1],r[2],c[2]);
cas1 c4(divs[3],divd[3],c[2],r[3],c[3]);
always@(divs or c or r)
begin
if(c[3])
begin
quo=1'b0;
rem=divs;
end
else
begin
quo=1'b1;
rem=r;
end
end
endmodule

//sub module
module cas1(a,b,c,diff,brow);
input a,b,c;
output diff,brow;
assign diff=a^b^c;
assign brow=((~a)&b)|(b&c)|((~a)&c);
endmodule