Advertisement
Guest User

Untitled

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