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

Subversion Repositories eco32

[/] [eco32/] [trunk/] [fpga/] [mc/] [src/] [ram/] [sdr/] [ram.v] - Blame information for rev 312

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

Line No. Rev Author Line
1 119 hellwig
//
2 290 hellwig
// ram.v -- external RAM interface, using SDRAM
3
//          8M x 32 bit = 32 MB
4 119 hellwig
//
5
 
6
 
7 290 hellwig
`timescale 1ns/10ps
8
`default_nettype none
9
 
10
 
11
module ram(clk, clk_ok, rst,
12
           stb, we, addr,
13
           data_in, data_out, ack,
14 27 hellwig
           sdram_cke, sdram_cs_n,
15
           sdram_ras_n, sdram_cas_n,
16 126 hellwig
           sdram_we_n, sdram_ba, sdram_a,
17
           sdram_udqm, sdram_ldqm, sdram_dq);
18 119 hellwig
    // internal interface signals
19 27 hellwig
    input clk;
20
    input clk_ok;
21 290 hellwig
    input rst;
22
    input stb;
23
    input we;
24
    input [24:2] addr;
25 27 hellwig
    input [31:0] data_in;
26
    output reg [31:0] data_out;
27 290 hellwig
    output reg ack;
28 119 hellwig
    // SDRAM interface signals
29 27 hellwig
    output sdram_cke;
30
    output sdram_cs_n;
31
    output sdram_ras_n;
32
    output sdram_cas_n;
33
    output sdram_we_n;
34
    output [1:0] sdram_ba;
35
    output [12:0] sdram_a;
36 126 hellwig
    output sdram_udqm;
37
    output sdram_ldqm;
38 27 hellwig
    inout [15:0] sdram_dq;
39
 
40 290 hellwig
  reg [2:0] state;
41 27 hellwig
  reg a0;
42
  reg cntl_read;
43
  reg cntl_write;
44
  wire cntl_done;
45
  wire [23:0] cntl_addr;
46
  reg [15:0] cntl_din;
47
  wire [15:0] cntl_dout;
48
 
49
  wire sd_out_en;
50
  wire [15:0] sd_out;
51
 
52
//--------------------------------------------------------------
53
 
54 290 hellwig
  sdramCntl sdramCntl_1(
55 27 hellwig
    // clock
56
    .clk(clk),
57
    .clk_ok(clk_ok),
58
    // host side
59
    .rd(cntl_read & ~cntl_done),
60
    .wr(cntl_write & ~cntl_done),
61
    .done(cntl_done),
62
    .hAddr(cntl_addr),
63
    .hDIn(cntl_din),
64
    .hDOut(cntl_dout),
65
    // SDRAM side
66
    .cke(sdram_cke),
67
    .ce_n(sdram_cs_n),
68
    .ras_n(sdram_ras_n),
69
    .cas_n(sdram_cas_n),
70
    .we_n(sdram_we_n),
71
    .ba(sdram_ba),
72
    .sAddr(sdram_a),
73
    .sDIn(sdram_dq),
74
    .sDOut(sd_out),
75
    .sDOutEn(sd_out_en),
76
    .dqmh(sdram_udqm),
77
    .dqml(sdram_ldqm)
78
  );
79
 
80
  assign sdram_dq = (sd_out_en == 1) ? sd_out : 16'hzzzz;
81
 
82
//--------------------------------------------------------------
83
 
84
  // the SDRAM is organized in 16-bit halfwords
85
  // address line 0 is controlled by the state machine
86
  assign cntl_addr[23:1] = addr[24:2];
87
  assign cntl_addr[0] = a0;
88
 
89
  // state machine for SDRAM access
90
  always @(posedge clk) begin
91 290 hellwig
    if (rst) begin
92
      state <= 3'b000;
93
      ack <= 0;
94 27 hellwig
    end else begin
95
      case (state)
96 290 hellwig
        3'b000:
97 27 hellwig
          // wait for access
98
          begin
99 290 hellwig
            if (stb) begin
100 27 hellwig
              // access
101 290 hellwig
              if (we) begin
102 27 hellwig
                // write
103 290 hellwig
                state <= 3'b001;
104 27 hellwig
              end else begin
105
                // read
106 290 hellwig
                state <= 3'b011;
107 27 hellwig
              end
108
            end
109
          end
110 290 hellwig
        3'b001:
111 27 hellwig
          // write word, upper 16 bits
112
          begin
113 290 hellwig
            if (cntl_done) begin
114
              state <= 3'b010;
115 27 hellwig
            end
116
          end
117 290 hellwig
        3'b010:
118 27 hellwig
          // write word, lower 16 bits
119
          begin
120 290 hellwig
            if (cntl_done) begin
121
              state <= 3'b111;
122
              ack <= 1;
123 27 hellwig
            end
124
          end
125 290 hellwig
        3'b011:
126 27 hellwig
          // read word, upper 16 bits
127
          begin
128 290 hellwig
            if (cntl_done) begin
129
              state <= 3'b100;
130 27 hellwig
              data_out[31:16] <= cntl_dout;
131
            end
132
          end
133 290 hellwig
        3'b100:
134 27 hellwig
          // read word, lower 16 bits
135
          begin
136 290 hellwig
            if (cntl_done) begin
137
              state <= 3'b111;
138 27 hellwig
              data_out[15:0] <= cntl_dout;
139 290 hellwig
              ack <= 1;
140 27 hellwig
            end
141
          end
142 290 hellwig
        3'b111:
143 27 hellwig
          // end of bus cycle
144
          begin
145 290 hellwig
            state <= 3'b000;
146
            ack <= 0;
147 27 hellwig
          end
148
        default:
149
          // all other states: reset
150
          begin
151 290 hellwig
            state <= 3'b000;
152
            ack <= 0;
153 27 hellwig
          end
154
      endcase
155
    end
156
  end
157
 
158
  // output of state machine
159
  always @(*) begin
160
    case (state)
161 290 hellwig
      3'b000:
162 27 hellwig
        // wait for access
163
        begin
164
          a0 = 1'bx;
165
          cntl_read = 0;
166
          cntl_write = 0;
167
          cntl_din = 16'hxxxx;
168
        end
169 290 hellwig
      3'b001:
170 27 hellwig
        // write word, upper 16 bits
171
        begin
172
          a0 = 1'b0;
173
          cntl_read = 0;
174
          cntl_write = 1;
175
          cntl_din = data_in[31:16];
176
        end
177 290 hellwig
      3'b010:
178 27 hellwig
        // write word, lower 16 bits
179
        begin
180
          a0 = 1'b1;
181
          cntl_read = 0;
182
          cntl_write = 1;
183
          cntl_din = data_in[15:0];
184
        end
185 290 hellwig
      3'b011:
186 27 hellwig
        // read word, upper 16 bits
187
        begin
188
          a0 = 1'b0;
189
          cntl_read = 1;
190
          cntl_write = 0;
191
          cntl_din = 16'hxxxx;
192
        end
193 290 hellwig
      3'b100:
194 27 hellwig
        // read word, lower 16 bits
195
        begin
196
          a0 = 1'b1;
197
          cntl_read = 1;
198
          cntl_write = 0;
199
          cntl_din = 16'hxxxx;
200
        end
201 290 hellwig
      3'b111:
202 27 hellwig
        // end of bus cycle
203
        begin
204
          a0 = 1'bx;
205
          cntl_read = 0;
206
          cntl_write = 0;
207
          cntl_din = 16'hxxxx;
208
        end
209
      default:
210
        // all other states: reset
211
        begin
212
          a0 = 1'bx;
213
          cntl_read = 0;
214
          cntl_write = 0;
215
          cntl_din = 16'hxxxx;
216
        end
217
    endcase
218
  end
219
 
220
endmodule

powered by: WebSVN 2.1.0

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