-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoxMuller.bsv
More file actions
133 lines (102 loc) · 3.4 KB
/
BoxMuller.bsv
File metadata and controls
133 lines (102 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
package BoxMuller;
// Bluespec standard packages.
import StmtFSM::*;
// My packages.
import FixedPoint::*;
import FIFO::*;
import RandomNumberGenerator::*;
import BoxMullerInterface::*;
import LogTableFxdP::*;
import InterfaceLogTableFxdP::*;
import WellPRNG::*;
import Complex::*;
import AirblueCommon::*;
import AirblueTypes::*;
import SquareRoot::*;
import ClientServer::*;
import FIFOF::*;
import GetPut::*;
import UnitAppendList :: * ;
import BUtils::*;
import Assert::*;
//`define isDebug True // uncomment this line to display error
`define cordicIters 16 // no. iterations cordic performs before giving result
`define cordicItersPerStage 8 // no. pipeline iters per stages
typedef UInt#(64) SqrtT;
typedef FixedPoint#(32,32) SqrtTFx;
typedef FixedPoint#(33,32) SqrtTFx33;
typedef Tuple4#(Int32WORD, Int32WORD, SqrtTFx, SqrtTFx) TupleUInt32;
(* synthesize *)
module mkBoxMuller (IfcBoxMullerInterface#(Int32WORD, TupleUInt32, SqrtTFx));
IfcRandomNumberGenerator#(Int32WORD, Int32WORD) rgn1 <- mkWellPRNG();
IfcRandomNumberGenerator#(Int32WORD, Int32WORD) rgn2 <- mkWellPRNG();
Reg#(Int32WORD) v1 <- mkReg(0);
Reg#(Int32WORD) v2 <- mkReg(0);
Reg#(SqrtTFx) valsqrIn <- mkReg(1024);
Reg#(SqrtTFx) valsqrOut <- mkReg(1);
Reg#(TupleUInt32) randtuple <- mkRegU;
Server#(SqrtTFx,Tuple2#(SqrtTFx,Bool)) sqrtfxm <- mkFixedPointSquareRooter(1);
InterfaceLogTableFxdP#(SqrtTFx33,SqrtTFx33) mLUT <- mkLogTableFxdP();
Reg#(SqrtTFx33) r1 <- mkReg(0);
Reg#(SqrtTFx33) r2 <- mkReg(0);
Reg#(SqrtTFx33) x_1 <- mkReg(0);
Reg#(SqrtTFx33) x_2 <- mkReg(0);
FIFOF#(SqrtTFx) fCheck <- mkLFIFOF;
Reg#(int) cycle <- mkReg(0);
Reg#(Bool) flag <- mkReg(True);
function Action putSqrt();
action
sqrtfxm.request.put(valsqrIn);
endaction
endfunction
function Action getSqrt();
action
Tuple2#(SqrtTFx,Bool) retval <- sqrtfxm.response.get();
valsqrOut <= tpl_1(retval);
endaction
endfunction
Stmt test =
seq
putSqrt();
getSqrt();
endseq;
FSM testFSM <- mkFSM(test);
method Action initialize (Int32WORD s1, Int32WORD s2);
rgn1.initialize(s1);
rgn2.initialize(s2);
endmethod:initialize
method Action run (SqrtTFx nfx );
if(flag)
action
let vt1 <- rgn1.get;
let vt2 <- rgn2.get;
r1 <= (0.000000000232830643653869628906)* fromUInt(unpack(vt1));
r2 <= (0.000000000232830643653869628906)* fromUInt(unpack(vt2));
SqrtTFx33 x1 = r1*(2.0) -1.0;
SqrtTFx33 x2 = r2*(2.0) -1.0;
x_1 <= x1;
x_2 <= x2;
let w = x1*x1 + x2*x2;
mLUT.run(w);
let sw <- mLUT.get();
let sw2 = 2.0*(sw);
let sw3 = sw2/w;
SqrtTFx tva;
tva.i = nfx.i;
tva.f = nfx.f;
valsqrIn <= tva;
testFSM.start;
cycle <= cycle + 1;
flag <= False;
endaction
else
action
flag <= True;
endaction
endmethod:run
method ActionValue#(TupleUInt32) get ( );
let tpl4 = tuple4(v1,v2, valsqrOut, valsqrOut);
return tpl4;
endmethod:get
endmodule: mkBoxMuller
endpackage: BoxMuller