Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.74 KB | None | 0 0
  1. // Stump ALU
  2. // Implement your Stump ALU here
  3. //
  4. // Created by Paul W Nutter, Feb 2015
  5. //
  6. // This an Stump ALU verilog implementation by Mircea Peia
  7. // I've implemented every instruction in a case in a single always block
  8. //
  9.  
  10. `include "src/Stump/Stump_definitions.v"
  11.  
  12. // 'include' definitions of function codes etc.
  13. // e.g. can use "`ADD" instead of "'h0" to aid readability
  14. // Substitute your own definitions if you prefer by
  15. // modifying Stump_definitions.v
  16.  
  17. /*----------------------------------------------------------------------------*/
  18.  
  19. module Stump_ALU (input wire [15:0] operand_A, // First operand
  20. input wire [15:0] operand_B, // Second operand
  21. input wire [ 2:0] func, // Function specifier
  22. input wire c_in, // Carry input
  23. input wire csh, // Carry from shifter
  24. output reg [15:0] result, // ALU output
  25. output reg [ 3:0] flags_out); // Flags {N, Z, V, C}
  26.  
  27. // func is the function code
  28. // operand_A is the first operand(the one we shift)
  29. // operand_B is the seocnd operand
  30. //c_in is the carry in may be 1 or 0
  31. //csh carry from shifter
  32. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  33. /* Declarations of any internal signals and buses used */
  34. reg [16:0] intmResult;
  35. reg [15:0] invOpB;
  36. assign invC_in = ~ c_in;
  37. reg [ 3:0] intmFlags;
  38.  
  39.  
  40.  
  41.  
  42. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  43.  
  44. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  45. /* Verilog code */
  46.  
  47. always @ (*)
  48. begin
  49.  
  50. //inverting operand B for SUB/SBC
  51. invOpB[0] = ~ operand_B[0];
  52. invOpB[1] = ~ operand_B[1];
  53. invOpB[2] = ~ operand_B[2];
  54. invOpB[3] = ~ operand_B[3];
  55. invOpB[4] = ~ operand_B[4];
  56. invOpB[5] = ~ operand_B[5];
  57. invOpB[6] = ~ operand_B[6];
  58. invOpB[7] = ~ operand_B[7];
  59. invOpB[8] = ~ operand_B[8];
  60. invOpB[9] = ~ operand_B[9];
  61. invOpB[10] = ~ operand_B[10];
  62. invOpB[11] = ~ operand_B[11];
  63. invOpB[12] = ~ operand_B[12];
  64. invOpB[13] = ~ operand_B[13];
  65. invOpB[14] = ~ operand_B[14];
  66. invOpB[15] = ~ operand_B[15];
  67.  
  68. //determining what instruction to do thorugh case
  69. case (func)
  70.  
  71. 3'b000: //Addition
  72. begin
  73. //calculte the sum
  74. intmResult = operand_A + operand_B;
  75. //set the corresponding flags
  76. intmFlags[3] = intmResult[15];
  77. intmFlags[2] = (intmResult[15:0] == 0);
  78. intmFlags[1] = (operand_A[15] == operand_B[15] && operand_A[15] != intmResult[15]);
  79. intmFlags[0] = intmResult[16];
  80. end
  81.  
  82. 3'b001: //Addition w. carry_in
  83. begin
  84. //calculte the sum
  85. intmResult = operand_A + operand_B;
  86. intmResult = intmResult + c_in;
  87. //set the corresponding flags
  88. intmFlags[3] = intmResult[15];
  89. intmFlags[2] =(intmResult[15:0] == 0);
  90. intmFlags[1] = (operand_A[15] == operand_B[15] && operand_A[15] != intmResult[15]);
  91. intmFlags[0] = intmResult[16];
  92. end
  93.  
  94. 3'b010: //Subtraction
  95. begin
  96. //perform the calculation
  97. intmResult = operand_A + invOpB;
  98. intmResult = intmResult + 17'b1;
  99. //set the corresponding flags
  100. intmFlags[3] = intmResult[15];
  101. intmFlags[2] =(intmResult[15:0] == 0);
  102. intmFlags[1] = (operand_A[15] != operand_B[15] && operand_A[15] != intmResult[15]);
  103. intmFlags[0] = ~intmResult[16];
  104. end
  105.  
  106. 3'b011: //Subtraction w. carry_in
  107. begin
  108. //perform the calculation
  109. intmResult = operand_A + invOpB;
  110. intmResult = intmResult + invC_in;
  111. //set the corresponding flags
  112. intmFlags[3] = intmResult[15];
  113. intmFlags[2] = (intmResult[15:0] == 0);
  114. intmFlags[1] = (operand_A[15] != operand_B[15] && operand_A[15] != intmResult[15]);
  115. intmFlags[0] = ~intmResult[16];
  116. end
  117.  
  118. 3'b100: //BitWise AND
  119. begin
  120. //perform the logical function
  121. intmResult[15:0] = operand_A & operand_B;
  122. //set bit to don't care so no warnings appear
  123. intmResult[16]= 1'bX;
  124. //set the corresponding flags
  125. intmFlags[3] = intmResult[15];
  126. intmFlags[2] = (intmResult[15:0] == 0);
  127. intmFlags[1] = 0;
  128. intmFlags[0] = csh;
  129. end
  130.  
  131. 3'b101: //BitWise OR
  132. begin
  133. //perform the logical function
  134. intmResult[15:0] = operand_A | operand_B;
  135. //set bit to don't care so no warnings appear
  136. intmResult[16]= 1'bX;
  137. //set the corresponding flags
  138. intmFlags[3] = intmResult[15];
  139. intmFlags[2] =(intmResult[15:0] == 0);
  140. intmFlags[1] = 0;
  141. intmFlags[0] = csh;
  142. end
  143.  
  144. 3'b110: //LD or ST
  145. begin
  146. //perform the logical function
  147. intmResult = operand_A + operand_B;
  148. intmFlags[3] = 1'bX;
  149. intmFlags[2] = 1'bX;
  150. intmFlags[1] = 1'bX;
  151. intmFlags[0] = 1'bX;
  152. end
  153.  
  154. 3'b111: //Branch
  155. begin
  156. //perform the logical function
  157. intmResult = operand_A + operand_B;
  158. intmFlags[3] = 1'bX;
  159. intmFlags[2] = 1'bX;
  160. intmFlags[1] = 1'bX;
  161. intmFlags[0] = 1'bX;
  162. end
  163.  
  164. default: // Does nothing
  165. begin
  166. intmResult = 17'bX;
  167. intmFlags[3] = 1'bX;
  168. intmFlags[2] = 1'bX;
  169. intmFlags[1] = 1'bX;
  170. intmFlags[0] = 1'bX;
  171. end
  172.  
  173. endcase
  174. //set actual result from the internal variable
  175. result = intmResult[15:0];
  176. flags_out = intmFlags;
  177.  
  178. end
  179.  
  180.  
  181.  
  182. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -*/
  183.  
  184. /*----------------------------------------------------------------------------*/
  185.  
  186.  
  187.  
  188. endmodule
  189.  
  190. /*============================================================================*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement