Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.47 KB | None | 0 0
  1. module zadanko(
  2. input [7:0] SW,
  3. input [2:0] KEY,
  4. output [9:0] LEDR,
  5. output [0:6] HEX0,HEX1,HEX2,HEX3);
  6. wire [3:0] AH,AL,SH,SL;
  7.  
  8. assign LEDR[7:0] = SW;
  9. assign {AH,AL} = SW;
  10.  
  11. add_sub_N_bits #(8) ex(SW,KEY[2],KEY[1],KEY[0],{SH,SL},LEDR[8],LEDR[9]);
  12.  
  13. decoder_hex_16 d3(AH,HEX3);
  14. decoder_hex_16 d2(AL,HEX2);
  15. decoder_hex_16 d1(SH,HEX1);
  16. decoder_hex_16 d0(SL,HEX0);
  17. endmodule
  18.  
  19.  
  20. module register_N_bits
  21. #(N=8)
  22. (input [N-1:0] D,
  23. input clk,
  24. output reg [N-1:0] Q);
  25.  
  26. always @(posedge clk)
  27. Q <= D;
  28. endmodule
  29.  
  30. module adder_N_bits
  31. #(parameter N=8)
  32. (input [N-1:0] A,B,
  33. input cin,
  34. output [N-1:0] S,
  35. output cout);
  36.  
  37. assign {cout,S} = A + B + cin;
  38.  
  39. endmodule
  40.  
  41.  
  42. module FFD_posedge(
  43. input D,clk,
  44. output reg Q);
  45.  
  46. always @(posedge clk)
  47. Q <= D;
  48. endmodule
  49.  
  50. module accumulator_N_bits_struct
  51. #(N=8)
  52. (input [N-1:0] A,
  53. input clk,
  54. output [N-1:0] S,
  55. output overflow, carry);
  56.  
  57. (* keep *) wire [N-1:0] B,C;
  58. (* keep *) wire cout,over;
  59.  
  60. register_N_bits #(8) ex_A(A,clk,B);
  61.  
  62. adder_N_bits #(8) ex_add(B,S,1'b0,C,cout);
  63.  
  64. register_N_bits #(8) ex_S(C,clk,S);
  65. FFD_posedge ex_cout(cout,clk,carry);
  66. assign over = cout ^ C[N-1];
  67. FFD_posedge ex_over(over,clk,overflow);
  68. endmodule
  69.  
  70. module accumulator_N_bits_always_aclr
  71. #(N=8)
  72. (input [N-1:0] A,
  73. input clk,aclr,
  74. output reg [N-1:0] S,
  75. output reg overflow,carry);
  76. reg [N-1:0] B;
  77.  
  78. always @(posedge clk, negedge aclr)
  79. if (!aclr) B <= {N{1'b0}};
  80. else B <= A;
  81. always @(posedge clk, negedge aclr)
  82. if (!aclr) {carry,S} <= {(N+1){1'b0}};
  83. else {carry,S} <= B + S;
  84. always @(posedge clk, negedge aclr)
  85. if (!aclr) overflow <= 1'b0;
  86. else overflow <= carry ^ S[N-1];
  87. endmodule
  88.  
  89. // zadanie 2
  90.  
  91. module add_sub_N_bits
  92. #(N=8)
  93. (input [N-1:0] A,
  94. input add_sub,
  95. input clk,aclr,
  96. output reg [N-1:0] S,
  97. output reg overflow,carry);
  98. reg [N-1:0] B;
  99. always @(posedge clk, negedge aclr)
  100. if (aclr) B <= {N{1'b0}};
  101. else B <= A;
  102. always @(posedge clk, negedge aclr)
  103. if (aclr) {carry,S} <= {(N+1){1'b0}};
  104. else if (add_sub) {carry,S} <= S + B;
  105. else {carry,S} <= S - B;
  106. always @(posedge clk, negedge aclr)
  107. if (aclr) overflow <= 1'b0;
  108. else overflow <= carry ^ S[N-1];
  109. endmodule
  110.  
  111.  
  112. module decoder_hex_16(
  113. input [3:0] liczba,
  114. output reg [0:6] H);
  115. always @(*)
  116. case (liczba)
  117. 0: H = 7'b0000001;
  118. 1: H = 7'b1001111;
  119. 2: H = 7'b0010010;
  120. 3: H = 7'b0000110;
  121. 4: H = 7'b1001100;
  122. 5: H = 7'b0100100;
  123. 6: H = 7'b0100000;
  124. 7: H = 7'b0001111;
  125. 8: H = 7'b0000000;
  126. 9: H = 7'b0000100;
  127. 10: H = 7'b0001000;
  128. 11: H = 7'b1100000;
  129. 12: H = 7'b0110001;
  130. 13: H = 7'b1000010;
  131. 14: H = 7'b0110000;
  132. 15: H = 7'b0111000;
  133. default: H = 7'b1111111;
  134. endcase
  135. endmodule
  136.  
  137. // zadanie 3
  138.  
  139. module array_multiplier_4x4 (
  140. output [7:0] z,
  141. input [3:0] a, b);
  142.  
  143. wire [2:0] c1,c2,c3,c4;
  144. wire [2:0] s1,s2,s3;
  145. wire [3:0] P0,P1,P2,P3;
  146. // Partial Product Generation
  147. PP pp1 (P3, P2, P1, P0, a, b);
  148. // Partial Product Reduction
  149. ha HA1_2 (c1[2],s1[2],P1[2],P0[3]);
  150. ha HA1_1 (c1[1],s1[1],P1[1],P0[2]);
  151. ha HA1_0 (c1[0],s1[0],P1[0],P0[1]);
  152. fa FA2_2 (c2[2],s2[2],P2[2],P1[3],c1[2]);
  153. fa FA2_1 (c2[1],s2[1],P2[1],s1[2],c1[1]);
  154. fa FA2_0 (c2[0],s2[0],P2[0],s1[1],c1[0]);
  155. fa FA3_2 (c3[2],s3[2],P3[2],P2[3],c2[2]);
  156. fa FA3_1 (c3[1],s3[1],P3[1],s2[2],c2[1]);
  157. fa FA3_0 (c3[0],s3[0],P3[0],s2[1],c2[0]);
  158.  
  159. // Generate lower product bits
  160. assign z[0] = P0[0];
  161. assign z[1] = s1[0];
  162. assign z[2] = s2[0];
  163. assign z[3] = s3[0];
  164.  
  165. // Final Carry Propagate Addition (CPA)
  166. ha CPA1 (c4[0],z[4],c3[0],s3[1]);
  167. fa CPA2 (c4[1],z[5],c3[1],c4[0],s3[2]);
  168. fa CPA3 (z[7], z[6], c3[2], c4[1], P3[3]);
  169. endmodule
  170.  
  171. // zadanie 4
  172.  
  173. module multiplier_4_bits(
  174. input [3:0] a,b,
  175. output [7:0] p);
  176.  
  177. wire [3:0] m[3:0];
  178. wire [3:0] s[1:3];
  179. wire cout[1:3];
  180.  
  181. assign m[0] = a & {4{b[0]}};
  182. assign m[1] = a & {4{b[1]}};
  183. assign m[2] = a & {4{b[2]}};
  184. assign m[3] = a & {4{b[3]}};
  185.  
  186. adder_N_bits #(4) ex1({1'b0,m[0][3:1]},m[1],1'b0,s[1],cout[1]);
  187. adder_N_bits #(4) ex2({cout[1],s[1][3:1]},m[2],1'b0,s[2],cout[2]);
  188. adder_N_bits #(4) ex3({cout[2],s[2][3:1]},m[3],1'b0,s[3],cout[3]);
  189.  
  190. assign p[0] = m[0][0];
  191. assign p[1] = s[1][0];
  192. assign p[2] = s[2][0];
  193. assign p[6:3] = s[3];
  194. assign p[7] = cout[3];
  195. endmodule
  196.  
  197. module PP ( // Partial Product Generation
  198. output [3:0] P3, P2, P1, P0,
  199. input [3:0] a,b);
  200. and pp1(P0[3], a[3], b[0]);
  201. and pp2(P0[2], a[2], b[0]);
  202. and pp3(P0[1], a[1], b[0]);
  203. and pp4(P0[0], a[0], b[0]);
  204. and pp5(P1[3], a[3], b[1]);
  205. and pp6(P1[2], a[2], b[1]);
  206. and pp7(P1[1], a[1], b[1]);
  207. and pp8(P1[0], a[0], b[1]);
  208. and pp9(P2[3], a[3], b[2]);
  209. and pp10(P2[2], a[2], b[2]);
  210. and pp11(P2[1], a[1], b[2]);
  211. and pp12(P2[0], a[0], b[2]);
  212. and pp13(P3[3], a[3], b[3]);
  213. and pp14(P3[2], a[2], b[3]);
  214. and pp15(P3[1], a[1], b[3]);
  215. and pp16(P3[0], a[0], b[3]);
  216. endmodule
  217.  
  218. module ha(
  219. output sout,cout,
  220. input a,b
  221. );
  222.  
  223. assign sout=a^b;
  224. assign cout=(a&b);
  225. endmodule
  226.  
  227. module fa(sout,cout,a,b,cin);
  228. output sout,cout;
  229. input a,b,cin;
  230.  
  231. assign sout=(a^b^cin);
  232. assign cout=((a&b)|(a&cin)|(b&cin));
  233. endmodule
  234.  
  235. // zadanie 5
  236.  
  237. module multiply4bits(product,inp1,inp2);
  238. output [7:0]product;
  239. input [3:0]inp1;
  240. input [3:0]inp2;
  241.  
  242. assign product[0]=(inp1[0]&inp2[0]);
  243.  
  244. wire x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14,x15,x16,x17;
  245.  
  246. ha HA1(product[1],x1,(inp1[1]&inp2[0]),(inp1[0]&inp2[1]));
  247. fa FA1(x2,x3,inp1[1]&inp2[1],(inp1[0]&inp2[2]),x1);
  248. fa FA2(x4,x5,(inp1[1]&inp2[2]),(inp1[0]&inp2[3]),x3);
  249. ha HA2(x6,x7,(inp1[1]&inp2[3]),x5);
  250. ha HA3(product[2],x15,x2,(inp1[2]&inp2[0]));
  251. fa FA5(x14,x16,x4,(inp1[2]&inp2[1]),x15);
  252. fa FA4(x13,x17,x6,(inp1[2]&inp2[2]),x16);
  253. fa FA3(x9,x8,x7,(inp1[2]&inp2[3]),x17);
  254. ha HA4(product[3],x12,x14,(inp1[3]&inp2[0]));
  255. fa FA8(product[4],x11,x13,(inp1[3]&inp2[1]),x12);
  256. fa FA7(product[5],x10,x9,(inp1[3]&inp2[2]),x11);
  257. fa FA6(product[6],product[7],x8,(inp1[3]&inp2[3]),x10);
  258. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement