OpenCores
URL https://opencores.org/ocsvn/rf6809/rf6809/trunk

Subversion Repositories rf6809

[/] [rf6809/] [trunk/] [rtl/] [noc/] [lib/] [random.sv] - Blame information for rev 22

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 19 robfinch
// ============================================================================
2
//        __
3
//   \\__/ o\    (C) 2011-2022  Robert Finch, Waterloo
4
//    \  __ /    All rights reserved.
5
//     \/_//     robfinch@finitron.ca
6
//       ||
7
//
8
// random.v
9
//     Multi-stream random number generator.
10
//
11
//
12
// This source file is free software: you can redistribute it and/or modify
13
// it under the terms of the GNU Lesser General Public License as published
14
// by the Free Software Foundation, either version 3 of the License, or
15
// (at your option) any later version.
16
//
17
// This source file is distributed in the hope that it will be useful,
18
// but WITHOUT ANY WARRANTY; without even the implied warranty of
19
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20
// GNU General Public License for more details.
21
//
22
// You should have received a copy of the GNU General Public License
23
// along with this program.  If not, see .
24
//
25
//      Reg no.
26
//      0                        read: random output bits [31:0], write: gen next number
27
//  1           random stream number
28
//  2           m_z seed setting bits [31:0]
29
//  3           m_w seed setting bits [31:0]
30
//
31
//  +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
32
//      |WISHBONE Datasheet
33
//      |WISHBONE SoC Architecture Specification, Revision B.3
34
//      |
35
//      |Description:                                           Specifications:
36
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
37
//      |General Description:                           random number generator
38
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
39
//      |Supported Cycles:                                      SLAVE,READ/WRITE
40
//      |                                                                       SLAVE,BLOCK READ/WRITE
41
//      |                                                                       SLAVE,RMW
42
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
43
//      |Data port, size:                                       16 bit
44
//      |Data port, granularity:                        16 bit
45
//      |Data port, maximum operand size:       16 bit
46
//      |Data transfer ordering:                        Undefined
47
//      |Data transfer sequencing:                      Undefined
48
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
49
//      |Clock frequency constraints:           none
50
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
51
//      |Supported signal list and                      Signal Name             WISHBONE equiv.
52
//      |cross reference to equivalent          ack_o                   ACK_O
53
//      |WISHBONE signals                                       adr_i[43:0]             ADR_I()
54
//      |                                                                       clk_i                   CLK_I
55
//      |                                   rst_i           RST_I()
56
//      |                                                                       dat_i(15:0)             DAT_I()
57
//      |                                                                       dat_o(15:0)             DAT_O()
58
//      |                                                                       cyc_i                   CYC_I
59
//      |                                                                       stb_i                   STB_I
60
//      |                                                                       we_i                    WE_I
61
//      |
62
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
63
//      |Special requirements:
64
//      +- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
65
//
66
// ============================================================================
67
//
68
// Uses George Marsaglia's multiply method
69
//
70
// m_w = ;    /* must not be zero */
71
// m_z = ;    /* must not be zero */
72
//
73
// uint get_random()
74
// {
75
//     m_z = 36969 * (m_z & 65535) + (m_z >> 16);
76
//     m_w = 18000 * (m_w & 65535) + (m_w >> 16);
77
//     return (m_z << 16) + m_w;  /* 32-bit result */
78
// }
79
//
80
`define TRUE    1'b1
81
`define FALSE   1'b0
82
 
83
module random(rst_i, clk_i, cs_i, cyc_i, stb_i, ack_o, we_i, adr_i, dat_i, dat_o);
84
input rst_i;
85
input clk_i;
86
input cs_i;
87
input cyc_i;
88
input stb_i;
89
output reg ack_o;
90
input we_i;
91
input [3:0] adr_i;
92
input [11:0] dat_i;
93
output reg [11:0] dat_o;
94
parameter pAckStyle = 1'b0;
95
 
96
reg ack;
97
reg cs;
98
reg we;
99
reg [3:0] adr;
100
reg [11:0] dat;
101
always_ff @(posedge clk_i)
102
        cs <= cs_i && cyc_i && stb_i;
103
always_ff @(posedge clk_i)
104
        we <= we_i;
105
always_ff @(posedge clk_i)
106
        adr <= adr_i;
107
always_ff @(posedge clk_i)
108
        dat <= dat_i;
109
 
110
always_ff @(posedge clk_i)
111
        ack_o <= cs & cs_i & cyc_i & stb_i;
112
//always @*
113
//      ack_o <= cs ? ack : pAckStyle;
114
 
115
reg [9:0] stream;
116
reg [35:0] next_m_z;
117
reg [35:0] next_m_w;
118
reg [35:0] out;
119
reg wrw, wrz;
120
reg [35:0] w=32'd3,z=32'd17;
121
wire [35:0] m_zs;
122
wire [35:0] m_ws;
123
wire pe_we;
124
 
125
edge_det ued1 (.rst(rst_i), .clk(clk_i), .ce(1'b1), .i(we), .pe(pe_we), .ne(), .ee());
126
rand_ram u1 (clk_i, wrw, stream, w, m_ws);
127
rand_ram u2 (clk_i, wrz, stream, z, m_zs);
128
 
129
always_comb
130
begin
131
        next_m_z = (18'h36969 * m_zs[17:0]) + m_zs[35:18];
132
        next_m_w = (18'h18000 * m_ws[17:0]) + m_ws[35:18];
133
end
134
 
135
reg [35:0] m_zsws;
136
always_comb
137
        m_zsws <= {m_zs[17:0],18'd0} + m_ws;
138
 
139
// Register read path
140
//
141
always_ff @(posedge clk_i)
142
if (cs_i)
143
        case(adr[3:0])
144
        4'd0:   dat_o <= 12'h0;
145
        4'd1: dat_o <= m_zsws[35:24];
146
        4'd2: dat_o <= m_zsws[23:12];
147
        4'd3:   dat_o <= m_zsws[11: 0];
148
        4'd5:   dat_o <= {2'h0,stream};
149
// Uncomment these for register read-back
150
//              3'd4:   dat_o <= m_z[31:16];
151
//              3'd5:   dat_o <= m_z[15: 0];
152
//              3'd6:   dat_o <= m_w[31:16];
153
//              3'd7:   dat_o <= m_w[15: 0];
154
        default:        dat_o <= 12'h000;
155
        endcase
156
else
157
        dat_o <= 12'h0;
158
 
159
// Register write path
160
//
161
always_ff @(posedge clk_i)
162
begin
163
        wrw <= `FALSE;
164
        wrz <= `FALSE;
165
        if (cs) begin
166
                if (pe_we)
167
                        case(adr[3:0])
168
                        4'd3:
169
                                begin
170
                                        z <= next_m_z;
171
                                        w <= next_m_w;
172
                                        wrw <= `TRUE;
173
                                        wrz <= `TRUE;
174
                                end
175
                        4'd5:   stream <= dat[9:0];
176
                        4'd8:   ;
177
                        4'd9:   z[35:24] <= dat;
178
                        4'd10:  z[23:12] <= dat;
179
                        4'd11:  begin z[11: 0] <= dat; wrz <= `TRUE; end
180
                        4'd12:  ;
181
                        4'd13:  w[35:24] <= dat;
182
                        4'd14:  w[23:12] <= dat;
183
                        4'd15:  begin w[11:0] <= dat; wrw <= `TRUE; end
184
                        endcase
185
        end
186
end
187
 
188
endmodule
189
 
190
 
191
// Tools were inferring a massive distributed ram so we help them out a bit by
192
// creating an explicit ram definition.
193
 
194
module rand_ram(clk, wr, ad, i, o);
195
input clk;
196
input wr;
197
input [9:0] ad;
198
input [35:0] i;
199
output [35:0] o;
200
 
201
reg [35:0] ri;
202
reg [9:0] regadr;
203
reg regwr;
204
(* RAM_STYLE="BLOCK" *)
205
reg [35:0] mem [0:1023];
206
 
207
always_ff @(posedge clk)
208
        regadr <= ad;
209
always_ff @(posedge clk)
210
        regwr <= wr;
211
always_ff @(posedge clk)
212
        ri <= i;
213
always_ff @(posedge clk)
214
        if (regwr)
215
                mem[regadr] <= ri;
216
assign o = mem[regadr];
217
 
218
endmodule

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.