Advertisement
Guest User

Untitled

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