module add_cond4_32(a,b,c,cin,co,s,ps,t,phn,h5,h4,h3,h2,h1,t4,t3,t2,t1,s0,s1,ic0,ic1);

input signed [31:0] a,b,c;
input signed [31:0] ps;
output [31:0] t, phn, h5,h4,h3,h2,h1,t4,t3,t2,t1,s0,s1,ic0,ic1;
input signed cin;
output co;
output signed [31:0] s;

wire psb,cinb;
wire [31:0] g;
wire [31:0] hs, hsn, pha, ic0, ic1, psn, gn;
wire [4:0] aa,ba;
wire [3:0] pa, s0a, s1a;


// partial sum

partialsum ixps (s[31:0], a[31:0], b[31:0]);

// 4-bit conditional sum computations

 // 0-2 selected by Cin

not ins1_0(s1[0], ps[0]); 
nand ign0 (gn[0],a[0],b[0]);
nor  it0 (t[0],a[0],b[0]);

not  ixb1(psn[1], ps[1]);
xorb ins0_1(s0[1],psn[1],gn[0]);
xorb ins1_1(s1[1],psn[1],t[0]);
nand ign1 (gn[1],a[1],b[1]);
oai  inoais0_2 (ic0[2],gn[0],psn[1],gn[1]);
oai  inoais1_2 (ic1[2],t[0],psn[1],gn[1]);

xor ins0_2(s0[2],ps[2],ic0[2]);
xor ins1_2(s1[2],ps[2],ic1[2]);

//  conditional for 31-3 selected by H

cond4l ic41 (a[5:2],b[5:2],ps[6:3],s0[6:3],s1[6:3]);
cond4l ic42 (a[9:6],b[9:6],ps[10:7],s0[10:7],s1[10:7]);
cond4l ic43 (a[13:10],b[13:10],ps[14:11],s0[14:11],s1[14:11]);
cond4l ic44 (a[17:14],b[17:14],ps[18:15],s0[18:15],s1[18:15]);
cond4l ic45 (a[21:18],b[21:18],ps[22:19],s0[22:19],s1[22:19]);
cond4l ic46 (a[25:22],b[25:22],ps[26:23],s0[26:23],s1[26:23]);
cond4l ic47 (a[29:26],b[29:26],ps[30:27],s0[30:27],s1[30:27]);

nor it30 (t[30],a[30],b[30]);
not ixb31 (psn[31],ps[31]);
xorb ins1_31 (s1[31],psn[31],t[30]);


// 1st level H# and T#

h1 ih1i0(a[0],b[0],cin,cin,h1[0]);
h1 ih1i2(a[2],b[2],a[1],b[1],h1[2]);
h1 ih1i4(a[4],b[4],a[3],b[3],h1[4]);
h1 ih1i6(a[6],b[6],a[5],b[5],h1[6]);
h1 ih1i8(a[8],b[8],a[7],b[7],h1[8]);
h1 ih1i10(a[10],b[10],a[9],b[9],h1[10]);
h1 ih1i12(a[12],b[12],a[11],b[11],h1[12]);
h1 ih1i14(a[14],b[14],a[13],b[13],h1[14]);
h1 ih1i16(a[16],b[16],a[15],b[15],h1[16]);
h1 ih1i18(a[18],b[18],a[17],b[17],h1[18]);
h1 ih1i20(a[20],b[20],a[19],b[19],h1[20]);
h1 ih1i22(a[22],b[22],a[21],b[21],h1[22]);
h1 ih1i24(a[24],b[24],a[23],b[23],h1[24]);
h1 ih1i26(a[26],b[26],a[25],b[25],h1[26]);
h1 ih1i28(a[28],b[28],a[27],b[27],h1[28]);
h1 ih1i30(a[30],b[30],a[29],b[29],h1[30]);

t1 it1i1(a[1],b[1],a[0],b[0],t1[1]);
t1 it1i3(a[3],b[3],a[2],b[2],t1[3]);
t1 it1i5(a[5],b[5],a[4],b[4],t1[5]);
t1 it1i7(a[7],b[7],a[6],b[6],t1[7]);
t1 it1i9(a[9],b[9],a[8],b[8],t1[9]);
t1 it1i11(a[11],b[11],a[10],b[10],t1[11]);
t1 it1i13(a[13],b[13],a[12],b[12],t1[13]);
t1 it1i15(a[15],b[15],a[14],b[14],t1[15]);
t1 it1i17(a[17],b[17],a[16],b[16],t1[17]);
t1 it1i19(a[19],b[19],a[18],b[18],t1[19]);
t1 it1i21(a[21],b[21],a[20],b[20],t1[21]);
t1 it1i23(a[23],b[23],a[22],b[22],t1[23]);
t1 it1i25(a[25],b[25],a[24],b[24],t1[25]);
t1 it1i27(a[27],b[27],a[26],b[26],t1[27]);
t1 it1i29(a[29],b[29],a[28],b[28],t1[29]);
t1 it1i31(a[31],b[31],a[30],b[30],t1[31]);

// 2nd level H and T

oai ih2i2(h2[2],h1[0],t1[1],h1[2]);
oai ih2i6(h2[6],h1[4],t1[5],h1[6]);
oai ih2i10(h2[10],h1[8],t1[9],h1[10]);
oai ih2i14(h2[14],h1[12],t1[13],h1[14]);
oai ih2i18(h2[18],h1[16],t1[17],h1[18]);
oai ih2i22(h2[22],h1[20],t1[21],h1[22]);
oai ih2i26(h2[26],h1[24],t1[25],h1[26]);
oai ih2i30(h2[30],h1[28],t1[29],h1[30]);

nor it2i5(t2[5],t1[3],t1[5]);
nor it2i9(t2[9],t1[7],t1[9]);
nor it2i13(t2[13],t1[11],t1[13]);
nor it2i17(t2[17],t1[15],t1[17]);
nor it2i21(t2[21],t1[19],t1[21]);
nor it2i25(t2[25],t1[23],t1[25]);
nor it2i29(t2[29],t1[27],t1[29]);

// Level 3 H# and T#

not ih3i2(h3[2],h2[2]);
aoi ih3i6(h3[6],h2[2],t2[5],h2[6]);
aoi ih3i10(h3[10],h2[6],t2[9],h2[10]);
aoi ih3i14(h3[14],h2[10],t2[13],h2[14]);
aoi ih3i18(h3[18],h2[14],t2[17],h2[18]);
aoi ih3i22(h3[22],h2[18],t2[21],h2[22]);
aoi ih3i26(h3[26],h2[22],t2[25],h2[26]);
aoi ih3i30(h3[30],h2[26],t2[29],h2[30]);

nand it3i9(t3[9],t2[5],t2[9]);
nand it3i13(t3[13],t2[9],t2[13]);
nand it3i17(t3[17],t2[13],t2[17]);
nand it3i21(t3[21],t2[17],t2[21]);
nand it3i25(t3[25],t2[21],t2[25]);
nand it3i29(t3[29],t2[25],t2[29]);

// Level 4 H and T

not ih4i2(h4[2],h3[2]);
not ih4i6(h4[6],h3[6]);
oai ih4i10(h4[10],h3[2],t3[9],h3[10]);
oai ih4i14(h4[14],h3[6],t3[13],h3[14]);
oai ih4i18(h4[18],h3[10],t3[17],h3[18]);
oai ih4i22(h4[22],h3[14],t3[21],h3[22]);
oai ih4i26(h4[26],h3[18],t3[25],h3[26]);
oai ih4i30(h4[30],h3[22],t3[29],h3[30]);

nor it4i17(t4[17],t3[9],t3[17]);
nor it4i21(t4[21],t3[13],t3[21]);
nor it4i25(t4[25],t3[17],t3[25]);
nor it4i29(t4[29],t3[21],t3[29]);

// Level 5 H# 

not ih5i2(h5[2],h4[2]);
not ih5i6(h5[6],h4[6]);
not ih5i10(h5[10],h4[10]);
not ih5i14(h5[14],h4[14]);
aoi ih5i18(h5[18],h4[2],t4[17],h4[18]);
aoi ih5i22(h5[22],h4[6],t4[21],h4[22]);
aoi ih5i26(h5[26],h4[10],t4[25],h4[26]);
aoi ih5i30(h5[30],h4[14],t4[29],h4[30]);

not ibbb0(psb,ps[0]);
not ibbb1(cinb,cin);
mux21 im0(s[0],ps[0],psb,cinb);
mux21 im1(s[1],s0[1],s1[1],cinb);
mux21 im2(s[2],s0[2],s1[2],cinb);
mux21 im3(s[3],ps[3],s1[3],h5[2]);
mux21 im4(s[4],s0[4],s1[4],h5[2]);
mux21 im5(s[5],s0[5],s1[5],h5[2]);
mux21 im6(s[6],s0[6],s1[6],h5[2]);
mux21 im7(s[7],ps[7],s1[7],h5[6]);
mux21 im8(s[8],s0[8],s1[8],h5[6]);
mux21 im9(s[9],s0[9],s1[9],h5[6]);
mux21 im10(s[10],s0[10],s1[10],h5[6]);
mux21 im11(s[11],ps[11],s1[11],h5[10]);
mux21 im12(s[12],s0[12],s1[12],h5[10]);
mux21 im13(s[13],s0[13],s1[13],h5[10]);
mux21 im14(s[14],s0[14],s1[14],h5[10]);
mux21 im15(s[15],ps[15],s1[15],h5[14]);
mux21 im16(s[16],s0[16],s1[16],h5[14]);
mux21 im17(s[17],s0[17],s1[17],h5[14]);
mux21 im18(s[18],s0[18],s1[18],h5[14]);
mux21 im19(s[19],ps[19],s1[19],h5[18]);
mux21 im20(s[20],s0[20],s1[20],h5[18]);
mux21 im21(s[21],s0[21],s1[21],h5[18]);
mux21 im22(s[22],s0[22],s1[22],h5[18]);
mux21 im23(s[23],ps[23],s1[23],h5[22]);
mux21 im24(s[24],s0[24],s1[24],h5[22]);
mux21 im25(s[25],s0[25],s1[25],h5[22]);
mux21 im26(s[26],s0[26],s1[26],h5[22]);
mux21 im27(s[27],ps[27],s1[27],h5[26]);
mux21 im28(s[28],s0[28],s1[28],h5[26]);
mux21 im29(s[29],s0[29],s1[29],h5[26]);
mux21 im30(s[30],s0[30],s1[30],h5[26]);
mux21 im31(s[31],ps[31],s1[31],h5[30]);

endmodule
