rmoronsv

Untitled

Dec 6th, 2019
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.40 KB | None | 0 0
  1. //This uses the 9's compliment, 10's compliment (9's compliment + 1), Binary Coded Decimal, Binary Coded Decimal addition (if >10 add +6), and ripple-carry adder connections
  2.  
  3. //For example
  4.  
  5. //Sum = 22 - 11
  6. //X >= Y
  7. //Sum = 22 + (99 - 11) + 1 -100
  8. //Sum = 11
  9.  
  10. //Sum = 57 - 99
  11. //X < Y
  12. //Sum = 100 - (57 + (99 - 99) + 1)
  13.  
  14. //Comments show what code is related to what type of operands and overflow and producing the 9's compliment of Y.
  15. //nY1 is like Y1 but the n is there for 9. (9 - Y1 = nY1)
  16. //NX1 is like X1 but the capital N is there for negative number - negative number
  17. //pmp means positive minus positive
  18. //S2 is sum bit 2. three BCD are needed for X1X0 - Y1Y0
  19. //Negative number - negative number is done by swapping X and Y in the first always block and then adding a capital N to instantiations on line 41-45
  20.  
  21. module BCD_Subtractor_ndigits(input [3:0]X0, input [3:0]Y0, input [3:0]X1, input [3:0]Y1, input SignofX, input SignofY, output reg [3:0]result_S0, output reg [3:0]result_S1, output reg [3:0]result_S2, output reg Signbit, output reg overflow);
  22. reg [3:0]nY1;
  23. reg [3:0]nY0;
  24. reg [3:0]NnY1;
  25. reg [3:0]NnY0;
  26. reg [3:0]NX1;
  27. reg [3:0]NX0;
  28.  
  29. always@(*) //overflow and 99 - Y1Y0 (9's compliment)
  30. begin
  31. overflow = X1 > 9 | X0 > 9 | Y1 > 9 | Y0 > 9;
  32. nY1 = 9 - Y1; //9 - Y
  33. nY0 = 9 - Y0; //9 - Y
  34.  
  35. NnY1 = 9 - X1; //flipped X and Y
  36. NnY0 = 9 - X0;
  37. NX1 = Y1;
  38. NX0 = Y0;
  39. end
  40.  
  41. //Positive number - positive number
  42. BCD_Adder b1 (nY0, 1, 0, nY0_plus1, carry_to_nY1); //+1 RCA
  43. BCD_Adder b1_2 (nY1, 0, carry_to_nY1, nY1_plus1, S2); //+1 RCA
  44. BCD_Adder b1_3 (nY0_plus1, X0, 0, result_pmp0, carry_to_nY1_plus1_plusX1); //+A RCA
  45. BCD_Adder b1_4 (nY1_plus1, X1, carry_to_nY1_plus1_plusX1, result_pmp1, carry_to_S2); //+A RCA
  46. BCD_Adder b1_5 (S2, 0, carry_to_S2, result_pmp2, useless1); //+A RCA
  47. //Negative number - negative number
  48. BCD_Adder b2 (NnY0, 0, 1'b1, NnY0_plus1, Ncarry_to_nY1); //+1 RCA
  49. BCD_Adder b2_2 (NnY1, 0, Ncarry_to_nY1, NnY1_plus1, NS2); //+1 RCA
  50. BCD_Adder b2_3 (NnY0_plus1, NX0, 0, Nresult_pmp0, Ncarry_to_nY1_plus1_plusX1); //+A RCA
  51. BCD_Adder b2_4 (NnY1_plus1, NX1, Ncarry_to_nY1_plus1_plusX1, Nresult_pmp1, Ncarry_to_S2); //+A RCA
  52. BCD_Adder b2_5 (NS2, 0, Ncarry_to_S2, Nresult_pmp2, Nuseless1); //+A RCA
  53. //Pos - Neg and Neg - Pos
  54. BCD_Adder b3 (Y0, X0, 0, Y0_plusX0, carry_to_S1);
  55. BCD_Adder b3_2 (Y1, X1, carry_to_S1, Y1_plusX1, carry_to_s2);
  56. BCD_Adder b3_3 (0, 0, carry_to_s2, result_s2, useless2);
  57.  
  58. always@(*)
  59. begin
  60. if (~SignofX & ~SignofY) //Pos - Pos
  61. begin
  62. if ((X1 > Y1) | (X1 == Y1) & (X0 > Y0) | (X1 == Y1 & X0 == Y0)) //X >= Y
  63. begin
  64. result_S0 = result_pmp0;
  65. result_S1 = result_pmp1;
  66. result_S2 = result_pmp2 - 1;
  67. Signbit = 1'b0;
  68. end
  69. else if ((X1 < Y1) | (X1 == Y1) & (X0 < Y0)) //X < Y
  70. begin
  71. result_S0 = (100 - ((result_pmp1 * 10) + result_pmp0)) % 10;
  72. result_S1 = ((100 - ((result_pmp1 * 10) + result_pmp0)) - (100 - ((result_pmp1 * 10) + result_pmp0)) % 10) / 10;
  73. result_S2 = 0;
  74. Signbit = 1'b1;
  75. end
  76. end
  77. else if (SignofX & SignofY) //Neg - Neg
  78. begin
  79. if ((X1 > Y1) | (X1 == Y1) & (X0 > Y0) | (X1 == Y1 & X0 == Y0)) //X >= Y
  80. begin
  81. result_S0 = Nresult_pmp0;
  82. result_S1 = Nresult_pmp1;
  83. result_S2 = Nresult_pmp2 - 1;
  84. Signbit = 1'b0;
  85. end
  86. else if ((X1 < Y1) | (X1 == Y1) & (X0 < Y0)) //X < Y
  87. begin
  88. result_S0 = (100 - ((Nresult_pmp1 * 10) + Nresult_pmp0)) % 10;
  89. result_S1 = ((100 - ((Nresult_pmp1 * 10) + Nresult_pmp0)) - (100 - ((Nresult_pmp1 * 10) + Nresult_pmp0)) % 10) / 10;
  90. result_S2 = 0;
  91. Signbit = 1'b1;
  92. end
  93. end
  94. else if (~SignofX & SignofY) //Pos - Neg
  95. begin
  96. result_S0 = Y0_plusX0;
  97. result_S1 = Y1_plusX1;
  98. result_S2 = result_s2;
  99. Signbit = 1'b0;
  100. end
  101. else if (SignofX & ~SignofY) //Neg - Pos
  102. begin
  103. result_S0 = Y0_plusX0;
  104. result_S1 = Y1_plusX1;
  105. result_S2 = result_s2;
  106. Signbit = 1'b1;
  107. end
  108. end
  109. endmodule
  110. module BCD_Adder(input [3:0]X, input [3:0]Y, input carryin, output wire [3:0]S, output wire carry);
  111. wire [4:0]result;
  112. assign result = X + Y + carryin;
  113. assign carry = (result >= 10);
  114. assign S = result % 10;
  115. endmodule
  116.  
  117.  
  118. //Alternate BCD_Adder RCA format I used and did not fix the code
  119. // //Positive number - positive number
  120. // BCD_Adder b1 (nY0, 1, nY0_plus1, carry_to_nY1); //+1 RCA
  121. // BCD_Adder b1_2 (nY1, carry_to_nY1, nY1_plus1, S2); //+1 RCA
  122. // BCD_Adder b1_3 (nY0_plus1, X0, result_pmp0, carry_to_nY1_plus1_plusX1); //+A RCA
  123. // BCD_Adder b1_4 (nY1_plus1, X1 + carry_to_nY1_plus1_plusX1, result_pmp1, carry_to_S2); //+A RCA
  124. // BCD_Adder b1_5 (S2, carry_to_S2, result_pmp2, useless1); //+A RCA
  125. // //Negative number - negative number
  126. // BCD_Adder b2 (NnY0, 1, NnY0_plus1, Ncarry_to_nY1); //+1 RCA
  127. // BCD_Adder b2_2 (NnY1, Ncarry_to_nY1, NnY1_plus1, NS2); //+1 RCA
  128. // BCD_Adder b2_3 (NnY0_plus1, NX0, Nresult_pmp0, Ncarry_to_nY1_plus1_plusX1); //+A RCA
  129. // BCD_Adder b2_4 (NnY1_plus1, NX1 + Ncarry_to_nY1_plus1_plusX1, Nresult_pmp1, Ncarry_to_S2); //+A RCA
  130. // BCD_Adder b2_5 (NS2, Ncarry_to_S2, Nresult_pmp2, Nuseless1); //+A RCA
  131. // //Pos - Neg and Neg - Pos
  132. // BCD_Adder b3 (Y0, X0, Y0_plusX0, carry_to_S1); //+A RCA
  133. // BCD_Adder b3_2 (Y1, X1 + carry_to_S1, Y1_plusX1, carry_to_s2); //+A RCA
  134. // BCD_Adder b3_3 (0, carry_to_s2, result_s2, useless2); //+A RCA
Advertisement
Add Comment
Please, Sign In to add comment