Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. module TopLevel_tb;
  2.  
  3. // To DUT Inputs
  4. bit start;
  5. bit CLK;
  6.  
  7. // From DUT Outputs
  8. wire halt; // done flag
  9.  
  10. // Instantiate the Device Under Test (DUT)
  11. TopLevel DUT (
  12. start ,
  13. CLK ,
  14. halt
  15. );
  16.  
  17. logic[63:0] dividend; // fixed for pgm 1 at 64'h8000_0000_0000_0000;
  18. logic[15:0] divisor1; // divisor 1 (sole operand for 1/x) to DUT
  19. logic[63:0] quotient1; // internal wide-precision result
  20. logic[15:0] result1, // desired final result, rounded to 16 bits
  21. result1_DUT; // actual result from DUT
  22. logic ov;
  23. real quotientR; // quotient in $real format
  24.  
  25. // program 2 variables
  26. logic[15:0] div_in2; // dividend 2 to DUT
  27. logic[ 7:0] divisor2; // divisor 2 to DUT
  28. logic[23:0] result2, // desired final result, rounded to 24 bits
  29. result2_DUT; // actual result from DUT
  30.  
  31. // program 3 variables
  32. logic[15:0] dat_in3; // operand to DUT
  33. logic[ 7:0] dat_out3; // SQRT(operand) from DUT
  34. logic[47:0] square3; // internal expansion of operand
  35. logic[ 7:0] result3, // desired 8-bit rounded integer result
  36. result3_DUT; // actual result from DUT
  37. real argument, result, // reals used in test bench square root algorithm
  38. error, result_new;
  39.  
  40. always begin // clock period = 10 Verilog time units
  41. #5ns CLK = 1;
  42. #5ns CLK = 0;
  43. end
  44.  
  45. initial begin
  46. // launch program 1
  47.  
  48. start = 1;
  49.  
  50. // Initialize DUT's data memory
  51. #10ns
  52. for(int i=0; i<256; i++) begin
  53. DUT.data_mem1.core[i] = 8'h0; // clear data_mem
  54. end
  55.  
  56. // students may also pre_load desired constants into data_mem
  57. // Initialize DUT's register file
  58. for(int j=0; j<16; j++)
  59. DUT.reg_file1.registers[j] = 8'b0; // default -- clear it
  60.  
  61. DUT.reg_file1.registers[14] = 8'd17;
  62. DUT.reg_file1.registers[12] = 8'd1;
  63. DUT.reg_file1.registers[10] = 8'd255;
  64. DUT.reg_file1.registers[13] = 8'd8;
  65. DUT.reg_file1.registers[9] = 8'd9;
  66. DUT.reg_file1.registers[11] = 8'd128;
  67.  
  68. // launch program in DUT
  69. #20ns start = 0;
  70.  
  71. dividend = 64'h8000_0000_0000_0000;
  72. divisor1 = 16'd240; // *** try various values here ***
  73. div1;
  74.  
  75. DUT.data_mem1.core[8] = divisor1[15:8];
  76. DUT.data_mem1.core[9] = divisor1[ 7:0];
  77.  
  78. wait(halt);
  79.  
  80. result1_DUT[15:8] = DUT.data_mem1.core[10];
  81. result1_DUT[ 7:0] = DUT.data_mem1.core[11];
  82.  
  83. $display ("divisor = %h, quotient = %h, result1 = %h, equiv to %10.5f", divisor1, quotient1, result1, quotientR);
  84. $display ("MEM8 = %h, MEM9 = %h", DUT.data_mem1.core[8], DUT.data_mem1.core[9]);
  85.  
  86. if(result1==result1_DUT) $display("success -- match1");
  87. else $display("OOPS1! expected %h, got %h",result1,result1_DUT);
  88.  
  89. #10ns $stop;
  90.  
  91. //// preload operands and launch program 2
  92. // #10ns start = 1;
  93. //// insert dividend and divisor
  94. // div_in2 = 16'h0501; // *** try various values here ***
  95. // divisor2 = 8'h53; // *** try various values here ***
  96. //// *** change names of memory or its guts as needed ***
  97. // d1.dat_mem1.core[0] = div_in2[15:8];
  98. // d1.dat_mem1.core[1] = div_in2[ 7:0];
  99. // d1.dat_mem1.core[2] = divisor2;
  100. // div2;
  101. // #20ns start = 0;
  102. // #20ns wait(done);
  103. //// *** change names of memory or its guts as needed ***
  104. // result2_DUT[23:16] = d1.dat_mem1.core[4];
  105. // result2_DUT[15: 8] = d1.dat_mem1.core[5];
  106. // result2_DUT[ 7: 0] = d1.dat_mem1.core[6];
  107. // $display ("dividend = %h, divisor2 = %h, quotient = %h, result2 = %h, equiv to %10.5f",dividend, divisor2, quotient1, result2, quotientR);
  108. // if(result2==result2_DUT) $display("success -- match2");
  109. // else $display("OOPS2! expected %h, got %h",result2,result2_DUT);
  110. //// preload operands and launch program 3
  111. // #10ns start = 1;
  112. //// insert operand
  113. // dat_in3 = 65535; // *** try various values here ***
  114. //// *** change names of memory or its guts as needed ***
  115. // d1.dat_mem1.core[13] = dat_in3[15: 8];
  116. // d1.dat_mem1.core[14] = dat_in3[ 7: 0];
  117. // div3;
  118. // #20ns start = 0;
  119. // #20ns wait(done);
  120. //// *** change names of memory or its guts as needed ***
  121. // result3_DUT = d1.dat_mem1.core[15];
  122. // $display("operand = %h, sqrt = %h",dat_in3,dat_out3);
  123. // if(dat_out3==result3_DUT) $display("success -- match3");
  124. // else $display("OOPS3! expected %h, got %h",dat_out3,result3_DUT);
  125. // #10ns $stop;
  126. end
  127.  
  128. task automatic div1;
  129. quotient1 = dividend/divisor1;
  130. result1 = quotient1[63:48]+quotient1[47]; // half-LSB upward rounding
  131. quotientR = 1.00000/$itor(divisor1);
  132. endtask
  133.  
  134. task automatic div2;
  135. dividend = div_in2<<48;
  136. quotient1 = dividend/divisor2;
  137. result2 = quotient1[63:40]+quotient1[39]; // half-LSB upward rounding
  138. quotientR = $itor(div_in2)/$itor(divisor2);
  139. // $display ("dividend = %h, divisor2 = %h, quotient = %h, result2 = %h, equiv to %10.5f",dividend, divisor2, quotient1, result2, quotientR);
  140. endtask
  141.  
  142. task automatic div3;
  143. argument = $itor(dat_in3);
  144. // real error, result_new;
  145. result = 1.0;
  146. error = 1.0;
  147. while (error > 0.001) begin
  148. result_new = argument/2.0/result + result/2.0;
  149. error = (result_new - result)/result;
  150. if (error < 0.0) error = -error;
  151. result = result_new;
  152. $displayb(result_new,,result);
  153. end
  154. {ov,dat_out3} = $rtoi(result+0.5);
  155. if(ov) dat_out3 = 8'hff;
  156. endtask
  157.  
  158. endmodule
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement