`timescale 1ns / 1ps
//////////////////////////////////////////////////////////////////////////////////
// Company: 
// Engineer: 
// 
// Create Date:    12:34:47 02/15/2020 
// Design Name: 
// Module Name:    FP_sqrt 
// Project Name: 
// Target Devices: 
// Tool versions: 
// Description: 
//
// Dependencies: 
//
// Revision: 
// Revision 0.01 - File Created
// Additional Comments: 
//
//////////////////////////////////////////////////////////////////////////////////
module FP_sqrt(x,y);
input [10:0] x;
output [10:0] y;
wire [3:0] t1,t2,t3,E,t4;
wire [7:0] a,a1;
wire [6:0] q;
wire [5:0] M;
adsub4 ad1(1'b1,x[9:6],4'b0111,t1,bo);
adsub4 ad2(bo,4'b0000,t1,t2,bo1);
rsh1 shift1(t2,t3,rs);
assign a = {1'b1,x[5:0],1'b0};
vrsh1 shift2(a,a1,~rs);
square_root_Res fx_sqrt(a1,q,);
//mux6 mx(q[5:0],q[5:0],rs,M);
assign M = q[5:0];
mux4 mx1(4'b0111,x[9:6],bo,t4);
adsub4 ad3(1'b0,t4,t3,E,bo2);
assign y = {1'b0,E,M};
endmodule

module mux6(A,B,S,Y);
    input [5:0] A,B;
    output [5:0] Y;
    input S;
assign Y = (S)? B : A;
endmodule

module mux4(A,B,S,Y);
    input [3:0] A,B;
    output [3:0] Y;
    input S;
assign Y = (S)? B : A;
endmodule

module adsub4 (sel,c,a,p,bo);
  input sel;
  input [3:0] a,c;
  output [3:0] p;
  output bo;
assign {bo,p} = (sel)?(c-a):(c+a);
endmodule

module vrsh1(A,Y,S);
input [7:0] A;
output [7:0] Y;
input S;

Mux M1(A[0],A[1],S,Y[0]);
Mux M2(A[1],A[2],S,Y[1]);
Mux M3(A[2],A[3],S,Y[2]);
Mux M4(A[3],A[4],S,Y[3]);
Mux M5(A[4],A[5],S,Y[4]);
Mux M6(A[5],A[6],S,Y[5]);
Mux M7(A[6],A[7],S,Y[6]);
Mux M8(A[7],1'b0,S,Y[7]);
endmodule

module rsh1(a,b,t1);
    input [3:0] a;
    output [3:0] b;
	 output t1;
    
assign {b[3],b[2:0]}= {a[3],a[2:1]};
assign t1 = a[0];
endmodule

module Mux(a,b,s,y);
    input  a,b,s;
    output y;
   
wire sbar;
assign y = (a&sbar)|(s&b);
assign sbar = ~s;

endmodule

module square_root_Res(x,q,r);

input [7:0] x;
output [4:0] r;
output [6:0] q;

wire co,co1,co2,co3,co4,co5,co6,co7,co8,co9;

wire t1,t2,t3,t4,t5,t6,t7,t8,t9,t10;
wire q1,q2,q3,q4;

CAS1 m1(1'b0,x[7],co,q1,t2,q1);
CAS1 m2(1'b1,x[6],1'b0,q1,t1,co);

CAS1 m3(1'b0,t2,co3,q2,t6,q2);
CAS1 m4(~q1,t1,co2,q2,t5,co3);
CAS1 m5(1'b0,x[5],co1,q2,t4,co2);
CAS1 m6(1'b1,x[4],1'b0,q2,t3,co1);

CAS1 m7(1'b0,t5,co7,q3,t11,q3);
CAS1 m8(~q1,t4,co6,q3,t10,co7);
CAS1 m9(~q2,t3,co5,q3,t9,co6);
CAS1 m10(1'b0,x[3],co4,q3,t8,co5);
CAS1 m11(1'b1,x[2],1'b0,q3,t7,co4);

CAS1 m12(1'b0,t10,co12,q4,t17,q4);
CAS1 m13(~q1,t9,co11,q4,t16,co12);
CAS1 m14(~q2,t8,co10,q4,t15,co11);
CAS1 m15(~q3,t7,co9,q4,t14,co10);
CAS1 m16(1'b0,x[1],co8,q4,t13,co9);
CAS1 m17(1'b1,x[0],1'b0,q4,t12,co8);

CAS1 m18(1'b0,t16,co18,q5,t24,q5);
CAS1 m19(~q1,t15,co17,q5,t23,co18);
CAS1 m20(~q2,t14,co16,q5,t22,co17);
CAS1 m21(~q3,t13,co15,q5,t21,co16);
CAS1 m22(~q4,t12,co14,q5,t20,co15);
CAS1 m23(1'b0,1'b0,co13,q5,t19,co14);
CAS1 m24(1'b1,1'b0,1'b0,q5,t18,co13);

CAS1 m25(1'b0,t23,co25,q6,t32,q6);
CAS1 m26(~q1,t22,co24,q6,t31,co25);
CAS1 m27(~q2,t21,co23,q6,t30,co24);
CAS1 m28(~q3,t20,co22,q6,t29,co23);
CAS1 m29(~q4,t19,co21,q6,t28,co22);
CAS1 m30(~q5,t18,co20,q6,t27,co21);
CAS1 m31(1'b0,1'b0,co19,q6,t26,co20);
CAS1 m32(1'b1,1'b0,1'b0,q6,t25,co19);

CAS1 m33(1'b0,t31,co33,q7,t41,q7);
CAS1 m34(~q1,t30,co32,q7,t40,co33);
CAS1 m35(~q2,t29,co31,q7,t39,co32);
CAS1 m36(~q3,t28,co30,q7,t38,co31);
CAS1 m37(~q4,t27,co29,q7,t37,co30);
CAS1 m38(~q5,t26,co28,q7,t36,co29);
CAS1 m39(~q6,t25,co27,q7,t35,co28);
CAS1 m40(1'b0,1'b0,co26,q7,t34,co27);
CAS1 m41(1'b1,1'b0,1'b0,q7,t33,co26);

assign q = {~q1,~q2,~q3,~q4,~q5,~q6,~q7};
assign r = {t37,t36,t35,t34,t33};
endmodule

module fb(
 input a,b,bin,
  output d,bo
    );
wire a_bar;
assign a_bar = ~a;
assign d = a^b^bin;
assign bo = (b&bin)|(b&a_bar)|(a_bar&bin);
endmodule

module CAS1(d,x,bin,s,sum,bo);
input d,x,bin,s;
output sum,bo;
wire sum1;
fb m1(x,d,bin,sum1,bo);
Mux mx1(sum1,x,s,sum);
endmodule