Advertisement
Guest User

Untitled

a guest
May 7th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. module swfsj6(input [7:0]SW,
  2.                     input KEY1, KEY0, KEY2,
  3.                     output[6:0]HEX3, HEX2, HEX1, HEX0,
  4.                     output [9:0]LEDR);
  5.                    
  6.         wire [7:0]sum;
  7.        
  8.         assign LEDR[7:0] = sum;
  9.                
  10.         multiplier_N_bits #(4) ex(SW[7:4], SW[3:0], sum);
  11.        
  12.         decoder_hex_16 ex1(sum[7:4], HEX1);
  13.         decoder_hex_16 ex2(sum[3:0], HEX0);
  14.         decoder_hex_16 ex3(SW[7:4], HEX3);
  15.         decoder_hex_16 ex4(SW[3:0], HEX2);
  16.            
  17. endmodule
  18.  
  19. module multiplier_N_bits
  20.     #(parameter N=4)
  21.     (input [N-1:0] a,b,
  22.     output [2*N-1:0] p);
  23.    
  24.     wire [3:0] m[3:0];
  25.     wire [3:0] s[1:3];
  26.     wire cout[1:3];    
  27.     genvar i;
  28.    
  29.     generate
  30.         for(i=0; i<N; i=i+1) begin:bl1
  31.             assign m[i] = a & {N{b[i]}};
  32.         end
  33.     endgenerate
  34.    
  35.     adder_N #(N) ex1({1'b0,m[0][N-1:1]},m[1],1'b0,s[1],cout[1]);
  36.     generate
  37.         for(i=2; i<N; i=i+1) begin:bl2
  38.             adder_N #(N)
  39.             exi({cout[i-1],s[i-1][N-1:1]},m[i],1'b0,s[i],cout[i]);
  40.         end
  41.     endgenerate
  42.     assign p[0] = m[0][0];
  43.     generate
  44.         for(i=1;i<N-1;i=i+1) begin:bl3
  45.             assign p[i] = s[i][0];
  46.         end
  47.     endgenerate
  48.    
  49.     assign p[2*N-2:N-1] = s[N-1];
  50.     assign p[2*N-1] = cout[N-1];
  51. endmodule
  52.  
  53. module array_multiplier_4_bits(input [3:0] a,b, output [7:0] p);
  54.     wire    c_1_1,c_2_1,c_3_1,c_4_1,
  55.             c_2_2,c_3_2,c_4_2,c_5_2,
  56.             c_3_3, c_4_3,c_5_3,
  57.             s_2_1,s_3_1,s_4_1,s_3_2,s_4_2,s_5_2;
  58.    
  59.     assign p[0] = a[0] & b[0]; 
  60.     adder_1_bits ex_1_1(a[1]&b[0],a[0]&b[1],1'b0,p[1],c_1_1);
  61.     adder_1_bits ex_2_1(a[2]&b[0],a[1]&b[1],c_1_1,s_2_1,c_2_1);
  62.     adder_1_bits ex_3_1(a[3]&b[0],a[2]&b[1],c_2_1,s_3_1,c_3_1);
  63.     adder_1_bits ex_4_1(1'b0,a[3]&b[1],c_3_1,s_4_1,c_4_1);
  64.     adder_1_bits ex_2_2(s_2_1,a[0]&b[2],1'b0,p[2],c_2_2);
  65.     adder_1_bits ex_3_2(s_3_1,a[1]&b[2],c_2_2,s_3_2,c_3_2);
  66.     adder_1_bits ex_4_2(s_4_1,a[2]&b[2],c_3_2,s_4_2,c_4_2);
  67.     adder_1_bits ex_5_2(c_4_1,a[3]&b[2],c_4_2,s_5_2,c_5_2);
  68.     adder_1_bits ex_3_3(s_3_2,a[0]&b[3],1'b0,p[3],c_3_3);
  69.     adder_1_bits ex_4_3(s_4_2,a[1]&b[3],c_3_3,p[4],c_4_3);
  70.     adder_1_bits ex_5_3(s_5_2,a[2]&b[3],c_4_3,p[5],c_5_3);
  71.     adder_1_bits ex_6_3(c_5_2,a[3]&b[3],c_5_3,p[6],p[7]);
  72. endmodule
  73.  
  74. module add_sub_N_bits
  75.     #(N=8)
  76.     (input [N-1:0] A,
  77.     input add_sub,
  78.     input clk,aclr,
  79.     output reg [N-1:0] S,
  80.     output reg overflow,carry);
  81.     reg [N-1:0] B; 
  82.     always @(posedge clk, negedge aclr)
  83.         if (!aclr)      B <= {N{1'b0}};
  84.         else        B <= A;
  85.     always @(posedge clk, negedge aclr)
  86.         if (!aclr)      {carry,S} <= {(N+1){1'b0}};
  87.         else if (add_sub)   {carry,S} <= S + B;
  88.         else        {carry,S} <= S - B;
  89.     always @(posedge clk, negedge aclr)
  90.         if (!aclr)      overflow <= 1'b0;
  91.         else        overflow <= carry ^ S[N-1];
  92. endmodule
  93.  
  94. module accumulator_N_bits_always
  95.     #(N=8)
  96.     (input [N-1:0] A,
  97.     input clk,
  98.     output reg [N-1:0] S,
  99.     output reg overflow,carry);
  100.    
  101.     reg [N-1:0] B;
  102.    
  103.     always @(posedge clk)
  104.         B <= A;
  105.    
  106.     always @(posedge clk)
  107.         {carry,S} <= B + S;
  108.        
  109.     always @(posedge clk)
  110.         overflow <= carry ^ S[N-1];
  111. endmodule
  112.  
  113. module accumulator_N_bits_struct    #(N=8)
  114.     (input [N-1:0] A,
  115.     input clk,
  116.     output [N-1:0] S,
  117.     output ov, ca);
  118.    
  119.     wire [N-1:0] B, C /* synthesis keep */;
  120.     wire a, x /* synthesis keep */;
  121.    
  122.     register_N #(8) ex(A,clk,B);
  123.    
  124.     adder_N #(8) ex0(B,S,1'b0,C,a);
  125.     register_N #(8) ex1(C,clk,S);
  126.     FFD ex2(a,clk,ca);
  127.     assign x = a ^ C[N-1];
  128.     FFD ex3(x,clk,ov);
  129.    
  130. endmodule
  131.  
  132. module register_N
  133.     #(N=8)
  134.     (input [N-1:0] D,
  135.     input clk,
  136.     output reg [N-1:0] Q);
  137.    
  138.     always @(posedge clk)
  139.         Q <= D;
  140. endmodule
  141.  
  142. module adder_1_bits(
  143.     input a,b,cin,
  144.     output s,cout);
  145.    
  146.     assign s = a ^ b ^ cin;
  147.     assign cout = a & b & (a ^ b) & cin;
  148.    
  149. endmodule
  150.  
  151. module adder_N
  152.     #(parameter N=8)
  153.     (input [N-1:0] A,B,
  154.     input cin,
  155.     output [N-1:0] S,
  156.     output cout);
  157.    
  158.     assign {cout,S} = A + B + cin;
  159.    
  160. endmodule
  161.  
  162. module adder_ripple_carry_N_bits
  163.     #(parameter N=4)
  164.     (input [N-1:0] A, B, input CI,
  165.     output [N-1:0] S, output CO);
  166.     wire [N-1:0] c;
  167.     generate
  168.         genvar i;
  169.         for (i=0; i<N; i=i+1)
  170.         begin: ad
  171.                 case(i)
  172.                     0:  adder_1_bits x(A[i], B[i], CI, S[i], c[i]);
  173.                     N-1:    adder_1_bits x(A[i], B[i], c[i-1], S[i], CO);
  174.                     default:    adder_1_bits x(A[i], B[i], c[i-1], S[i], c[i]);
  175.                 endcase
  176.         end
  177.     endgenerate
  178. endmodule
  179.  
  180. module FFD(input D, clk,
  181.                 output reg Q);
  182.         always @(posedge clk)
  183.             Q <= D;
  184. endmodule
  185.  
  186. module decoder_hex_16(input [3:0]x, output reg [0:6]h);
  187.     always @*
  188.    
  189.     case(x)
  190.     4'b0000: h = 7'b0000001;
  191.     4'b0001: h = 7'b1001111;
  192.     4'b0010: h = 7'b0010010;
  193.     4'b0011: h = 7'b0000110;
  194.     4'b0100: h = 7'b1001100;
  195.     4'b0101: h = 7'b0100100;
  196.     4'b0110: h = 7'b0100000;
  197.     4'b0111: h = 7'b0001111;
  198.     4'b1000: h = 7'b0000000;
  199.     4'b1001: h = 7'b0000100;
  200.     4'b1010: h = 7'b0001000;
  201.     4'b1011: h = 7'b1100000;
  202.     4'b1100: h = 7'b0011000;
  203.     4'b1101: h = 7'b1000010;
  204.     4'b1110: h = 7'b0110000;
  205.     4'b1111: h = 7'b0111000;
  206.  
  207.     endcase
  208.    
  209. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement