move to ml505, import Greg\'s memory controller
[fleet.git] / ships / DDR2.ship
1 ship: DDR2
2
3 == Ports ===========================================================
4 data  in:    inAddrRead
5 data  in:    inAddrWrite
6 data  in:    inDataWrite
7
8 data  out:   out
9
10 percolate up:    DDR2_CAS_B   1
11 percolate up:    DDR2_CKE     2
12 percolate up:    DDR2_RAS_B   1
13 percolate up:    DDR2_WE_B    1
14 percolate up:    DDR2_ODT     2
15 percolate up:    DDR2_CS0_B   2
16 percolate up:    DDR2_CLK_N   2
17 percolate up:    DDR2_CLK_P   2
18 percolate up:    DDR2_A       14
19 percolate up:    DDR2_BA      3
20 percolate inout: DDR2_DQ      64
21 percolate up:    DDR2_DM      8
22 percolate inout: DDR2_DQS_N   8
23 percolate inout: DDR2_DQS_P   8
24
25 percolate inout: I2C_DDR2_SCL 1
26 percolate inout: I2C_DDR2_SDA 1
27
28 percolate down:  CLKBUF_Q1_N  1
29 percolate down:  CLKBUF_Q1_P  1
30
31 == TeX ==============================================================
32
33 == Fleeterpreter ====================================================
34     public void service() { }
35 == FleetSim ==============================================================
36
37 == FPGA ==============================================================
38
39 // Nearly all of this was copied from Greg Gibeling's work; copyright shown below:
40
41 // Everything here was copied from
42 // GateLib/Firmware/DRAM/Hardware/DDR2SDRAM/Test/FPGA_TOP_ML505_DDR2SDRAMTest.v
43
44 //==============================================================================
45 //      Section:        License
46 //==============================================================================
47 //      Copyright (c) 2005-2008, Regents of the University of California
48 //      All rights reserved.
49 //
50 //      Redistribution and use in source and binary forms, with or without modification,
51 //      are permitted provided that the following conditions are met:
52 //
53 //              - Redistributions of source code must retain the above copyright notice,
54 //                      this list of conditions and the following disclaimer.
55 //              - Redistributions in binary form must reproduce the above copyright
56 //                      notice, this list of conditions and the following disclaimer
57 //                      in the documentation and/or other materials provided with the
58 //                      distribution.
59 //              - Neither the name of the University of California, Berkeley nor the
60 //                      names of its contributors may be used to endorse or promote
61 //                      products derived from this software without specific prior
62 //                      written permission.
63 //
64 //      THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
65 //      ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
66 //      WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
67 //      DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
68 //      ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
69 //      (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
70 //      LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
71 //      ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
72 //      (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
73 //      SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
74 //==============================================================================
75
76 parameter
77     ClockFreq  = 200000000,
78     BAWidth    = 2,
79     RAWidth    = 13,
80     CAWidth    = 10,                                                     
81     DWidth     = 128,       // 128b SDR internal transfers
82     UWidth     = 8,         // This will almost ALWAYS be 8
83     BurstLen   = 2,         // 256b total burst, 2 words DWidth words at SDR, or 4 external words at DDR
84     EnableMask = 1,
85     EnableECC  = 0,
86     Board      = 0;
87
88 localparam
89     UCount   =  DWidth / UWidth,
90     // 128b/8 = 16b per mask means per-byte masking
91     MWidth   =  (EnableECC || (EnableMask == 0)) ? 0 : UCount,
92     // Unused lower address bits, the -1 is to get a proper log2
93     UAWidth  =  `log2(UCount-1),
94     TAWidth  =  CAWidth + RAWidth + BAWidth,
95     // Note that the components are in order according to where in the
96     // address they appear, the -1 is to account for DDR
97     AWidth   =  TAWidth + UAWidth - 1,
98     ECheck   =  EnableECC ? 2 : 0,
99     ECorrect =  EnableECC ? 1 : 0,
100     CWidth   =  3,
101     EHWidth  =  `max(`log2(ECheck), 1),
102     ERWidth  =  `max(`log2(ECheck), 1);
103
104 wire   Reset;
105 assign Reset = !rst;
106
107 reg     [AWidth-1:0]    CommandAddress;
108 reg     [CWidth-1:0]    Command; 
109 wire                    CommandReady;
110 reg                     CommandValid;
111
112 wire    [DWidth-1:0]    DataIn;
113 wire    [MWidth-1:0]    DataInMask;
114 reg                     DataInValid;
115 wire                    DataInReady;
116         
117 wire    [DWidth-1:0]    DataOut;
118 wire    [EHWidth-1:0]   DataOutErrorChecked;
119 wire    [ERWidth-1:0]   DataOutErrorCorrected;
120 wire                    DataOutValid;
121 reg                     DataOutReady;
122
123 wire                    Clock_IBUFG;
124 wire                    Clock, ClockD2, ClockP90;
125 wire                    Clock_DCM, ClockD2_DCM, ClockP90_DCM;
126 wire                    Locked;
127
128 reg  [`WORDWIDTH:0] out_d;
129 assign out_d_ = out_d;
130
131 assign  DDR2_BA[2]    = 1'b0;
132 assign  DDR2_CS0_B[1] = 1'b1;
133 assign  DDR2_ODT[1]   = 1'b0;
134 assign  DDR2_CKE[1]   = 1'b0;
135
136 IBUFGDS ClockIBufG(.I(CLKBUF_Q1_P), .IB(CLKBUF_Q1_N), .O(Clock_IBUFG));
137 DCM_BASE
138   #( 
139      .CLKIN_PERIOD(5.0),
140      .CLKDV_DIVIDE(2.0),
141      .DLL_FREQUENCY_MODE("HIGH"),
142      .DUTY_CYCLE_CORRECTION("TRUE"),
143      .FACTORY_JF(16'hF0F0)
144    )
145   DCMBase(
146      .CLK0(Clock_DCM),
147      .CLK180( ),
148      .CLK270( ),
149      .CLK2X( ),
150      .CLK2X180( ),
151      .CLK90(ClockP90_DCM),
152      .CLKDV(ClockD2_DCM),
153      .CLKFX( ),
154      .CLKFX180( ),
155      .LOCKED(Locked),
156      .CLKFB(Clock),
157      .CLKIN(Clock_IBUFG),
158      .RST(Reset));
159   // synthesis attribute CLKIN_PERIOD          of DCMBase is "5.0"
160   // synthesis attribute CLKDV_DIVIDE          of DCMBase is "2.0"
161   // synthesis attribute DLL_FREQUENCY_MODE    of DCMBase is "HIGH"
162   // synthesis attribute DUTY_CYCLE_CORRECTION of DCMBase is "TRUE"
163   // synthesis attribute FACTORY_JF            of DCMBase is "16'hF0F0"
164   BUFG    ClockBufG(.I(Clock_DCM), .O(Clock));
165   BUFG    ClockP90BufG(.I(ClockP90_DCM), .O(ClockP90));
166   BUFG    ClockD2BufG(.I(ClockD2_DCM), .O(ClockD2));
167
168 DDR2SDRAM DDR2SDRAM(
169       .Clock(Clock),
170       .ClockD2(ClockD2),
171       .ClockP90(ClockP90),
172       .Reset(Reset),
173       .Locked(Locked),
174       .ClockF200(Clock),
175       .Initialized( ),
176       .PoweredUp( ),
177
178       .CommandClock(clk),
179       .DataInClock(clk),
180       .DataOutClock(clk),
181       .CommandReset(Reset),
182       .DataInReset(Reset),
183       .DataOutReset(Reset),
184
185       .CommandAddress(CommandAddress),
186       .Command(Command),
187       .CommandValid(CommandValid),
188       .CommandReady(CommandReady),
189       .DataIn(DataIn),
190       .DataInMask(DataInMask),
191       .DataInValid(DataInValid),
192       .DataInReady(DataInReady),
193       .DataOut(DataOut),
194       .DataOutErrorChecked(DataOutErrorChecked),
195       .DataOutErrorCorrected(DataOutErrorCorrected),
196       .DataOutValid(DataOutValid),
197       .DataOutReady(DataOutReady),
198       .DDR2_DQ(DDR2_DQ),
199       .DDR2_A(DDR2_A),
200       .DDR2_BA(DDR2_BA[1:0]),
201       .DDR2_RAS_B(DDR2_RAS_B),
202       .DDR2_CAS_B(DDR2_CAS_B),
203       .DDR2_WE_B(DDR2_WE_B),
204       .DDR2_CS0_B(DDR2_CS0_B[0]),
205       .DDR2_ODT(DDR2_ODT[0]),
206       .DDR2_CKE(DDR2_CKE[0]),
207       .DDR2_DM(DDR2_DM),
208       .DDR2_DQS_P(DDR2_DQS_P),
209       .DDR2_DQS_N(DDR2_DQS_N),
210       .DDR2_CLK_P(DDR2_CLK_P),
211       .DDR2_CLK_N(DDR2_CLK_N));
212       defparam DDR2SDRAM.UWidth     = UWidth;
213       defparam DDR2SDRAM.BAWidth    = BAWidth;
214       defparam DDR2SDRAM.RAWidth    = RAWidth;
215       defparam DDR2SDRAM.CAWidth    = CAWidth;
216       defparam DDR2SDRAM.DWidth     = DWidth;
217       defparam DDR2SDRAM.BurstLen   = BurstLen;
218       defparam DDR2SDRAM.EnableMask = EnableMask;
219       defparam DDR2SDRAM.EnableECC  = EnableECC;
220       defparam DDR2SDRAM.Board      = Board;
221       defparam DDR2SDRAM.MultiClock = 1;
222
223   assign DataIn     = inDataWrite_d;
224   assign DataInMask = 16'b1111111111111111;
225
226   always @(posedge clk) begin
227
228     if (!rst) begin
229       `reset
230       CommandValid <= 0;
231       DataOutReady <= 0;
232     end else begin
233       `flush
234       `cleanup
235
236       CommandValid <= 0;
237       DataInValid  <= 0;
238
239       if (`out_empty) begin
240           DataOutReady <= 1;      
241       end
242
243       if (DataOutReady && DataOutValid && `out_empty) begin
244           out_d <= { 1'b0, DataOut[`WORDWIDTH-1:0] };
245           `fill_out
246           DataOutReady <= 0;
247
248       end else if (DataOutReady && CommandReady && DataInReady && `out_empty) begin
249           if (`inAddrWrite_full && `inDataWrite_full) begin
250             `drain_inDataWrite
251             `drain_inAddrWrite
252             CommandAddress <= inAddrWrite_d;
253             Command        <= 3'b000;
254             CommandValid   <= 1;
255             DataInValid    <= 1;
256             out_d <= { 1'b1, 37'b0 };
257             `fill_out
258             DataOutReady <= 0;
259           end else if (`inAddrRead_full) begin
260             `drain_inAddrRead
261             CommandAddress <= inAddrRead_d;
262             CommandValid   <= 1;
263             Command        <= 3'b001;
264             DataInValid    <= 0;
265             DataOutReady   <= 1;
266           end
267       end
268     end
269   end
270
271 == Test ==============================================================
272
273 #expect 0
274
275 #ship debug : Debug
276 #ship ddr   : DDR2
277
278 debug.in:
279   recv, deliver;
280
281 ddr.out:
282   collect;
283   set flags a=!c,b=b;
284   send to debug.in;
285   collect;
286   set flags a=!c,b=b;
287   send to debug.in;
288 ddr.inAddrWrite:
289   set word=0;
290   deliver;
291   deliver;
292 ddr.inDataWrite:
293   set word=1;
294   deliver;
295   deliver;
296
297
298
299
300 == Constants ========================================================
301
302 == Contributors =========================================================
303 Adam Megacz <megacz@cs.berkeley.edu>