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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [peripherals/] [wbdmac.v] - Blame information for rev 36

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

Line No. Rev Author Line
1 36 dgisselq
////////////////////////////////////////////////////////////////////////////////
2
//
3
//
4
// Filename:    wbdmac.v
5
//
6
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
7
//
8
// Purpose:     Wishbone DMA controller
9
//
10
//      This module is controllable via the wishbone, and moves values from
11
//      one location in the wishbone address space to another.  The amount of
12
//      memory moved at any given time can be up to 4kB, or equivalently 1kW.
13
//      Four registers control this DMA controller: a control/status register,
14
//      a length register, a source WB address and a destination WB address.
15
//      These register may be read at any time, but they may only be written
16
//      to when the controller is idle.
17
//
18
//      The meanings of three of the setup registers should be self explanatory:
19
//              - The length register controls the total number of words to
20
//                      transfer.
21
//              - The source address register controls where the DMA controller
22
//                      reads from.  This address may or may not be incremented
23
//                      after each read, depending upon the setting in the
24
//                      control/status register.
25
//              - The destination address register, which controls where the DMA
26
//                      controller writes to.  This address may or may not be
27
//                      incremented after each write, also depending upon the
28
//                      setting in the control/status register.
29
//
30
//      It is the control/status register, at local address zero, that needs
31
//      more definition:
32
//
33
//      Bits:
34
//      31      R       Write protect   If this is set to one, it means the
35
//                              write protect bit is set and the controller
36
//                              is therefore idle.  This bit will be set upon
37
//                              completing any transfer.
38
//      30      R       Error.          The controller stopped mid-transfer
39
//                                      after receiving a bus error.
40
//      29      R/W     inc_s_n         If set to one, the source address
41
//                              will not increment from one read to the next.
42
//      28      R/W     inc_d_n         If set to one, the destination address
43
//                              will not increment from one write to the next.
44
//      27      R       Always 0
45
//      26..16  R       nread           Indicates how many words have been read,
46
//                              and not necessarily written (yet).  This
47
//                              combined with the cfg_len parameter should tell
48
//                              exactly where the controller is at mid-transfer.
49
//      27..16  W       WriteProtect    When a 12'h3db is written to these
50
//                              bits, the write protect bit will be cleared.
51
//                              
52
//      15      R/W     on_dev_trigger  When set to '1', the controller will
53
//                              wait for an external interrupt before starting.
54
//      14..10  R/W     device_id       This determines which external interrupt
55
//                              will trigger a transfer.
56
//      9..0    R/W     transfer_len    How many bytes to transfer at one time.
57
//                              The minimum transfer length is one, while zero
58
//                              is mapped to a transfer length of 1kW.
59
//
60
//
61
//      To use this, follow this checklist:
62
//      1. Wait for any prior DMA operation to complete
63
//              (Read address 0, wait 'till either top bit is set or cfg_len==0)
64
//      2. Write values into length, source and destination address. 
65
//              (writei(3, &vals) should be sufficient for this.)
66
//      3. Enable the DMAC interrupt in whatever interrupt controller is present
67
//              on the system.
68
//      4. Write the final start command to the setup/control/status register:
69
//              Set inc_s_n, inc_d_n, on_dev_trigger, dev_trigger,
70
//                      appropriately for your task
71
//              Write 12'h3db to the upper word.
72
//              Set the lower word to either all zeros, or a smaller transfer
73
//              length if desired.
74
//      5. wait() for the interrupt and the operation to complete.
75
//              Prior to completion, number of items successfully transferred
76
//              be read from the length register.  If the internal buffer is
77
//              being used, then you can read how much has been read into that
78
//              buffer by reading from bits 25..16 of this control/status
79
//              register.
80
//
81
// Creator:     Dan Gisselquist
82
//              Gisselquist Tecnology, LLC
83
//
84
// Copyright:   2015
85
//
86
//
87
////////////////////////////////////////////////////////////////////////////////
88
//
89
// Copyright (C) 2015, Gisselquist Technology, LLC
90
//
91
// This program is free software (firmware): you can redistribute it and/or
92
// modify it under the terms of  the GNU General Public License as published
93
// by the Free Software Foundation, either version 3 of the License, or (at
94
// your option) any later version.
95
//
96
// This program is distributed in the hope that it will be useful, but WITHOUT
97
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
98
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
99
// for more details.
100
//
101
// License:     GPL, v3, as defined and found on www.gnu.org,
102
//              http://www.gnu.org/licenses/gpl.html
103
//
104
//
105
///////////////////////////////////////////////////////////////////////////
106
//
107
//
108
module wbdmac(i_clk,
109
                i_swb_cyc, i_swb_stb, i_swb_we, i_swb_addr, i_swb_data,
110
                        o_swb_ack, o_swb_stall, o_swb_data,
111
                o_mwb_cyc, o_mwb_stb, o_mwb_we, o_mwb_addr, o_mwb_data,
112
                        i_mwb_ack, i_mwb_stall, i_mwb_data, i_mwb_err,
113
                i_dev_ints,
114
                o_interrupt,
115
                i_other_busmaster_requests_bus);
116
        parameter       LGMEMLEN = 10, DW=32, LGDV=5;
117
        input                   i_clk;
118
        // Slave/control wishbone inputs
119
        input                   i_swb_cyc, i_swb_stb, i_swb_we;
120
        input   [1:0]            i_swb_addr;
121
        input   [(DW-1):0]       i_swb_data;
122
        // Slave/control wishbone outputs
123
        output  reg             o_swb_ack;
124
        output  wire            o_swb_stall;
125
        output  reg [(DW-1):0]   o_swb_data;
126
        // Master/DMA wishbone control
127
        output  reg             o_mwb_cyc, o_mwb_stb, o_mwb_we;
128
        output  reg [(DW-1):0]   o_mwb_addr, o_mwb_data;
129
        // Master/DMA wishbone responses from the bus
130
        input                   i_mwb_ack, i_mwb_stall;
131
        input   [(DW-1):0]       i_mwb_data;
132
        input                   i_mwb_err;
133
        // The interrupt device interrupt lines
134
        input   [(DW-1):0]       i_dev_ints;
135
        // An interrupt to be set upon completion
136
        output  reg             o_interrupt;
137
        // Need to release the bus for a higher priority user
138
        input                   i_other_busmaster_requests_bus;
139
 
140
 
141
        reg                     cfg_wp; // Write protect
142
        reg                     cfg_err;
143
        reg     [(DW-1):0]       cfg_waddr, cfg_raddr, cfg_len;
144
        reg [(LGMEMLEN-1):0]     cfg_blocklen_sub_one;
145
        reg                     cfg_incs, cfg_incd;
146
        reg     [(LGDV-1):0]     cfg_dev_trigger;
147
        reg                     cfg_on_dev_trigger;
148
 
149
        // Single block operations: We'll read, then write, up to a single
150
        // memory block here.
151
 
152
        reg     [(DW-1):0]       dma_mem [0:(((1<<LGMEMLEN))-1)];
153
        reg     [(LGMEMLEN):0]   nread, nwritten, nacks;
154
        wire    [(DW-1):0]       bus_nacks;
155
        assign  bus_nacks = { {(DW-LGMEMLEN-1){1'b0}}, nacks };
156
 
157
        initial o_interrupt = 1'b0;
158
        initial o_mwb_cyc   = 1'b0;
159
        initial cfg_err     = 1'b0;
160
        initial cfg_wp      = 1'b0;
161
        initial cfg_len     = 32'h00;
162
        initial cfg_blocklen_sub_one = {(LGMEMLEN){1'b1}};
163
        initial cfg_on_dev_trigger = 1'b0;
164
        always @(posedge i_clk)
165
                if ((o_mwb_cyc)&&(o_mwb_we)) // Write cycle
166
                begin
167
                        if ((o_mwb_stb)&&(~i_mwb_stall))
168
                        begin
169
                                nwritten <= nwritten+1;
170
                                if ((nwritten == nread-1)
171
                                        ||(i_other_busmaster_requests_bus))
172
                                        // Wishbone interruptus
173
                                        o_mwb_stb <= 1'b0;
174
                                else if (cfg_incd) begin
175
                                        o_mwb_addr <= o_mwb_addr + 1;
176
                                        cfg_waddr  <= cfg_waddr  + 1;
177
                                end
178
                                // o_mwb_data <= dma_mem[nwritten + 1];
179
                        end
180
 
181
                        if (i_mwb_err)
182
                        begin
183
                                o_mwb_cyc <= 1'b0;
184
                                cfg_err <= 1'b1;
185
                                cfg_len <= 0;
186
                                nread   <= 0;
187
                        end else if (i_mwb_ack)
188
                        begin
189
                                nacks <= nacks+1;
190
                                cfg_len <= cfg_len - 1;
191
                                if ((nacks+1 == nwritten)&&(~o_mwb_stb))
192
                                begin
193
                                        o_mwb_cyc <= 1'b0;
194
                                        nread <= 0;
195
                                        o_interrupt <= (cfg_len == 1);
196
                                        // Turn write protect back on
197
                                        cfg_wp    <= 1'b1;
198
                                end
199
                        end
200
                end else if ((o_mwb_cyc)&&(~o_mwb_we)) // Read cycle
201
                begin
202
                        if ((o_mwb_stb)&&(~i_mwb_stall))
203
                        begin
204
                                nacks <= nacks+1;
205
                                if ((nacks == {1'b0, cfg_blocklen_sub_one})
206
                                        ||(bus_nacks <= cfg_len-1)
207
                                        ||(i_other_busmaster_requests_bus))
208
                                        // Wishbone interruptus
209
                                        o_mwb_stb <= 1'b0;
210
                                else if (cfg_incs) begin
211
                                        o_mwb_addr <= o_mwb_addr + 1;
212
                                end
213
                        end
214
 
215
                        if (i_mwb_err)
216
                        begin
217
                                o_mwb_cyc <= 1'b0;
218
                                cfg_err <= 1'b1;
219
                                cfg_len <= 0;
220
                                nread <= 0;
221
                        end else if (i_mwb_ack)
222
                        begin
223
                                nread <= nread+1;
224
                                if ((~o_mwb_stb)&&(nread+1 == nacks))
225
                                begin
226
                                        o_mwb_cyc <= 1'b0;
227
                                        nacks <= 0;
228
                                end
229
                                if (cfg_incs)
230
                                        cfg_raddr  <= cfg_raddr  + 1;
231
                                // dma_mem[nread[(LGMEMLEN-1):0]] <= i_mwb_data;
232
                        end
233
                end else if ((~o_mwb_cyc)&&(nread > 0)&&(~cfg_err))
234
                begin // Initiate/continue a write cycle
235
                        o_mwb_cyc  <= 1'b1;
236
                        o_mwb_stb  <= 1'b1;
237
                        o_mwb_we   <= 1'b1;
238
                        // o_mwb_data <= dma_mem[0];
239
                        o_mwb_addr <= cfg_waddr;
240
                        // nwritten  <= 0; // Can't set to zero, in case we're
241
                        // nacks     <= 0; //   continuing a cycle
242
                end else if ((~o_mwb_cyc)&&(nread == 0)&&(cfg_len>0)&&(~cfg_wp)
243
                                &&((~cfg_on_dev_trigger)
244
                                        ||(i_dev_ints[cfg_dev_trigger])))
245
                begin // Initiate a read cycle
246
                        o_mwb_cyc <= 1'b1;
247
                        o_mwb_stb <= 1'b1;
248
                        o_mwb_we  <= 1'b0;
249
                        o_mwb_addr<= cfg_raddr;
250
                        nwritten  <= 0;
251
                        nread     <= 0;
252
                        nacks     <= 0;
253
                end else begin
254
                        o_mwb_cyc  <= 1'b0;
255
                        o_mwb_stb  <= 1'b0;
256
                        o_mwb_we   <= 1'b0;
257
                        o_mwb_addr <= cfg_raddr;
258
                        o_interrupt<= 1'b0;
259
                        nwritten   <= 0;
260
                        if ((i_swb_cyc)&&(i_swb_stb)&&(i_swb_we))
261
                        begin
262
                                cfg_wp <= 1'b1;
263
                                case(i_swb_addr)
264
                                2'b00: begin
265
                                        cfg_wp    <= (i_swb_data[27:16]!=12'hfed);
266
                                        cfg_blocklen_sub_one
267
                                                <= i_swb_data[(LGMEMLEN-1):0]-1;
268
                                        cfg_dev_trigger    <= i_swb_data[14:10];
269
                                        cfg_on_dev_trigger <= i_swb_data[15];
270
                                        cfg_incs  <= ~i_swb_data[29];
271
                                        cfg_incd  <= ~i_swb_data[28];
272
                                        cfg_err   <= 1'b0;
273
                                        end
274
                                2'b01: cfg_len   <=  i_swb_data;
275
                                2'b10: cfg_raddr <=  i_swb_data;
276
                                2'b11: cfg_waddr <=  i_swb_data;
277
                                endcase
278
                        end
279
                end
280
 
281
        //
282
        // This is tricky.  In order for Vivado to consider dma_mem to be a 
283
        // proper memory, it must have a simple address fed into it.  Hence
284
        // the read_address (rdaddr) register.  The problem is that this
285
        // register must always be one greater than the address we actually
286
        // want to read from, unless we are idling.  So ... the math is touchy.
287
        //
288
        reg     [(LGMEMLEN-1):0] rdaddr;
289
        always @(posedge i_clk)
290
                if ((o_mwb_cyc)&&(o_mwb_we)&&(o_mwb_stb)&&(~i_mwb_stall))
291
                        // This would be the normal advance, save that we are
292
                        // already one ahead of nwritten
293
                        rdaddr <= rdaddr + 1; // {{(LGMEMLEN-1){1'b0}},1};
294
                else if ((~o_mwb_cyc)&&(nread > 0)&&(~cfg_err))
295
                        // Here's where we do our extra advance
296
                        rdaddr <= nwritten[(LGMEMLEN-1):0]+1;
297
                else if ((~o_mwb_cyc)||(~o_mwb_we))
298
                        rdaddr <= nwritten[(LGMEMLEN-1):0];
299
        always @(posedge i_clk)
300
                if ((~o_mwb_cyc)||((o_mwb_we)&&(o_mwb_stb)&&(~i_mwb_stall)))
301
                        o_mwb_data <= dma_mem[rdaddr];
302
        always @(posedge i_clk)
303
                if ((o_mwb_cyc)&&(~o_mwb_we)&&(i_mwb_ack))
304
                        dma_mem[nread[(LGMEMLEN-1):0]] <= i_mwb_data;
305
 
306
        always @(posedge i_clk)
307
                casez(i_swb_addr)
308
                2'b00: o_swb_data <= {  ~cfg_wp, cfg_err,
309
                                        ~cfg_incs, ~cfg_incd,
310
                                        1'b0, nread,
311
                                        cfg_on_dev_trigger, cfg_dev_trigger,
312
                                        cfg_blocklen_sub_one
313
                                        };
314
                2'b01: o_swb_data <= cfg_len;
315
                2'b10: o_swb_data <= cfg_raddr;
316
                2'b11: o_swb_data <= cfg_waddr;
317
                endcase
318
 
319
        always @(posedge i_clk)
320
                if ((i_swb_cyc)&&(i_swb_stb)) // &&(~i_swb_we))
321
                        o_swb_ack <= 1'b1;
322
                // else if ((i_swb_cyc)&&(i_swb_stb)&&(i_swb_we)&&(~o_mwb_cyc)&&(nread == 0))
323
                else
324
                        o_swb_ack <= 1'b0;
325
 
326
        assign  o_swb_stall = 1'b0;
327
 
328
endmodule
329
 

powered by: WebSVN 2.1.0

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