| 1 |
45 |
robfinch |
// ============================================================================
|
| 2 |
|
|
// __
|
| 3 |
|
|
// \\__/ o\ (C) 2020 Robert Finch, Waterloo
|
| 4 |
|
|
// \ __ / All rights reserved.
|
| 5 |
|
|
// \/_// robfinch<remove>@finitron.ca
|
| 6 |
|
|
// ||
|
| 7 |
|
|
//
|
| 8 |
|
|
// intToPosit.sv
|
| 9 |
|
|
// - integer to posit number converter
|
| 10 |
|
|
// - parameterized width
|
| 11 |
|
|
//
|
| 12 |
|
|
//
|
| 13 |
|
|
// BSD 3-Clause License
|
| 14 |
|
|
// Redistribution and use in source and binary forms, with or without
|
| 15 |
|
|
// modification, are permitted provided that the following conditions are met:
|
| 16 |
|
|
//
|
| 17 |
|
|
// 1. Redistributions of source code must retain the above copyright notice, this
|
| 18 |
|
|
// list of conditions and the following disclaimer.
|
| 19 |
|
|
//
|
| 20 |
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice,
|
| 21 |
|
|
// this list of conditions and the following disclaimer in the documentation
|
| 22 |
|
|
// and/or other materials provided with the distribution.
|
| 23 |
|
|
//
|
| 24 |
|
|
// 3. Neither the name of the copyright holder nor the names of its
|
| 25 |
|
|
// contributors may be used to endorse or promote products derived from
|
| 26 |
|
|
// this software without specific prior written permission.
|
| 27 |
|
|
//
|
| 28 |
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
| 29 |
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
| 30 |
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
| 31 |
|
|
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
|
| 32 |
|
|
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
| 33 |
|
|
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
| 34 |
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
|
| 35 |
|
|
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
| 36 |
|
|
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
| 37 |
|
|
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
| 38 |
|
|
// ============================================================================
|
| 39 |
|
|
//
|
| 40 |
36 |
robfinch |
`timescale 1ns / 1ps
|
| 41 |
|
|
module intToPosit_tb_v;
|
| 42 |
|
|
|
| 43 |
|
|
function [31:0] log2;
|
| 44 |
|
|
input reg [31:0] value;
|
| 45 |
|
|
begin
|
| 46 |
|
|
value = value-1;
|
| 47 |
|
|
for (log2=0; value>0; log2=log2+1)
|
| 48 |
|
|
value = value>>1;
|
| 49 |
|
|
end
|
| 50 |
|
|
endfunction
|
| 51 |
|
|
|
| 52 |
|
|
parameter N=32;
|
| 53 |
|
|
parameter E=8;
|
| 54 |
|
|
parameter Bs=log2(N);
|
| 55 |
45 |
robfinch |
parameter es = 2;
|
| 56 |
36 |
robfinch |
|
| 57 |
|
|
reg clk;
|
| 58 |
46 |
robfinch |
reg [15:0] cnt;
|
| 59 |
36 |
robfinch |
|
| 60 |
45 |
robfinch |
wire [N-1:0] out, outi;
|
| 61 |
36 |
robfinch |
|
| 62 |
45 |
robfinch |
reg [N-1:0] a, a1;
|
| 63 |
36 |
robfinch |
|
| 64 |
46 |
robfinch |
reg [31:0] fa;
|
| 65 |
|
|
wire [31:0] f2po;
|
| 66 |
|
|
fpToPosit #(.FPWID(32)) ufp1 (.i(fa), .o(f2po));
|
| 67 |
|
|
|
| 68 |
|
|
wire [63:0] double = {fa[31], fa[30], {3{~fa[30]}}, fa[29:23], fa[22:0], {29{1'b0}}};
|
| 69 |
|
|
|
| 70 |
36 |
robfinch |
// Instantiate the Unit Under Test (UUT)
|
| 71 |
|
|
intToPosit #(.PSTWID(N), .es(es)) u2 (.i(a), .o(out));
|
| 72 |
46 |
robfinch |
positToInt #(.PSTWID(N), .es(es)) u3 (.i(f2po), .o(outi));
|
| 73 |
36 |
robfinch |
|
| 74 |
|
|
//FP_to_posit #(.N(32), .E(8), .es(es)) u3 (in, out3);
|
| 75 |
|
|
//Posit_to_FP #(.N(32), .E(8), .es(es)) u5 (out, out3);
|
| 76 |
|
|
|
| 77 |
|
|
|
| 78 |
|
|
initial begin
|
| 79 |
|
|
a = $urandom(1);
|
| 80 |
|
|
// Initialize Inputs
|
| 81 |
|
|
clk = 1;
|
| 82 |
|
|
cnt = 0;
|
| 83 |
|
|
// Wait 100 ns for global reset to finish
|
| 84 |
|
|
#325150
|
| 85 |
|
|
$fclose(outfile);
|
| 86 |
|
|
$finish;
|
| 87 |
|
|
end
|
| 88 |
|
|
|
| 89 |
|
|
always #5 clk=~clk;
|
| 90 |
|
|
always @(posedge clk) begin
|
| 91 |
45 |
robfinch |
a <= $urandom();
|
| 92 |
|
|
cnt <= cnt + 1;
|
| 93 |
46 |
robfinch |
if (cnt > 1000) begin
|
| 94 |
|
|
fa <= $urandom();
|
| 95 |
|
|
end
|
| 96 |
|
|
else
|
| 97 |
36 |
robfinch |
case (cnt)
|
| 98 |
46 |
robfinch |
2: fa <= 32'h3f000001; // 0.5 + 1ulp
|
| 99 |
|
|
3: fa <= 32'h3EFFFFFF; // 0.4999...
|
| 100 |
|
|
4: a <= 32'h17cf4600;
|
| 101 |
|
|
5: a <= 10;
|
| 102 |
|
|
6: a <= -1;
|
| 103 |
|
|
7: a <= -10;
|
| 104 |
|
|
8: a <= 100;
|
| 105 |
45 |
robfinch |
default: a <= $urandom();
|
| 106 |
36 |
robfinch |
endcase
|
| 107 |
|
|
end
|
| 108 |
|
|
|
| 109 |
|
|
integer outfile;
|
| 110 |
45 |
robfinch |
initial outfile = $fopen("d:/cores2020/rtf64/v2/rtl/verilog/cpu/pau/test_bench/intToPosit_tvo32.txt", "wb");
|
| 111 |
|
|
always @(posedge clk) begin
|
| 112 |
46 |
robfinch |
$fwrite(outfile, "%h\t%d\t%h\t%d\t%e\n",f2po,a,out,outi,$bitstoreal(double));
|
| 113 |
36 |
robfinch |
end
|
| 114 |
|
|
|
| 115 |
|
|
endmodule
|
| 116 |
|
|
|