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

Subversion Repositories xulalx25soc

[/] [xulalx25soc/] [trunk/] [rtl/] [cpu/] [wbdmac.v] - Blame information for rev 118

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 21 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 Technology, LLC
83
//
84
////////////////////////////////////////////////////////////////////////////////
85
//
86 69 dgisselq
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
87 21 dgisselq
//
88
// This program is free software (firmware): you can redistribute it and/or
89
// modify it under the terms of  the GNU General Public License as published
90
// by the Free Software Foundation, either version 3 of the License, or (at
91
// your option) any later version.
92
//
93
// This program is distributed in the hope that it will be useful, but WITHOUT
94
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
95
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
96
// for more details.
97
//
98
// License:     GPL, v3, as defined and found on www.gnu.org,
99
//              http://www.gnu.org/licenses/gpl.html
100
//
101
//
102
///////////////////////////////////////////////////////////////////////////
103
//
104
//
105 69 dgisselq
`define DMA_IDLE        3'b000
106
`define DMA_WAIT        3'b001
107
`define DMA_READ_REQ    3'b010
108
`define DMA_READ_ACK    3'b011
109
`define DMA_PRE_WRITE   3'b100
110
`define DMA_WRITE_REQ   3'b101
111
`define DMA_WRITE_ACK   3'b110
112
 
113
module wbdmac(i_clk, i_rst,
114 21 dgisselq
                i_swb_cyc, i_swb_stb, i_swb_we, i_swb_addr, i_swb_data,
115
                        o_swb_ack, o_swb_stall, o_swb_data,
116
                o_mwb_cyc, o_mwb_stb, o_mwb_we, o_mwb_addr, o_mwb_data,
117
                        i_mwb_ack, i_mwb_stall, i_mwb_data, i_mwb_err,
118
                i_dev_ints,
119
                o_interrupt);
120
        parameter       ADDRESS_WIDTH=32, LGMEMLEN = 10,
121
                        DW=32, LGDV=5,AW=ADDRESS_WIDTH;
122 69 dgisselq
        input                   i_clk, i_rst;
123 21 dgisselq
        // Slave/control wishbone inputs
124
        input                   i_swb_cyc, i_swb_stb, i_swb_we;
125
        input   [1:0]            i_swb_addr;
126
        input   [(DW-1):0]       i_swb_data;
127
        // Slave/control wishbone outputs
128
        output  reg             o_swb_ack;
129
        output  wire            o_swb_stall;
130
        output  reg [(DW-1):0]   o_swb_data;
131
        // Master/DMA wishbone control
132 69 dgisselq
        output  wire            o_mwb_cyc, o_mwb_stb, o_mwb_we;
133 21 dgisselq
        output  reg [(AW-1):0]   o_mwb_addr;
134
        output  reg [(DW-1):0]   o_mwb_data;
135
        // Master/DMA wishbone responses from the bus
136
        input                   i_mwb_ack, i_mwb_stall;
137
        input   [(DW-1):0]       i_mwb_data;
138
        input                   i_mwb_err;
139
        // The interrupt device interrupt lines
140
        input   [(DW-1):0]       i_dev_ints;
141
        // An interrupt to be set upon completion
142
        output  reg             o_interrupt;
143
        // Need to release the bus for a higher priority user
144
        //      This logic had lots of problems, so it is being
145
        //      removed.  If you want to make sure the bus is available
146
        //      for a higher priority user, adjust the transfer length
147
        //      accordingly.
148
        //
149
        // input                        i_other_busmaster_requests_bus;
150
        //
151
 
152
 
153 69 dgisselq
        reg     [2:0]            dma_state;
154
        reg                     cfg_err, cfg_len_nonzero;
155 21 dgisselq
        reg     [(AW-1):0]       cfg_waddr, cfg_raddr, cfg_len;
156
        reg [(LGMEMLEN-1):0]     cfg_blocklen_sub_one;
157
        reg                     cfg_incs, cfg_incd;
158
        reg     [(LGDV-1):0]     cfg_dev_trigger;
159
        reg                     cfg_on_dev_trigger;
160
 
161
        // Single block operations: We'll read, then write, up to a single
162
        // memory block here.
163
 
164
        reg     [(DW-1):0]       dma_mem [0:(((1<<LGMEMLEN))-1)];
165 69 dgisselq
        reg     [(LGMEMLEN):0]   nread, nwritten, nwacks, nracks;
166
        wire    [(AW-1):0]       bus_nracks;
167
        assign  bus_nracks = { {(AW-LGMEMLEN-1){1'b0}}, nracks };
168 21 dgisselq
 
169 69 dgisselq
        reg     last_read_request, last_read_ack,
170
                last_write_request, last_write_ack;
171 118 dgisselq
        reg     trigger, abort, user_halt;
172 69 dgisselq
 
173
        initial dma_state = `DMA_IDLE;
174 21 dgisselq
        initial o_interrupt = 1'b0;
175
        initial cfg_len     = {(AW){1'b0}};
176
        initial cfg_blocklen_sub_one = {(LGMEMLEN){1'b1}};
177
        initial cfg_on_dev_trigger = 1'b0;
178 69 dgisselq
        initial cfg_len_nonzero = 1'b0;
179 21 dgisselq
        always @(posedge i_clk)
180 69 dgisselq
        case(dma_state)
181
        `DMA_IDLE: begin
182
                o_mwb_addr <= cfg_raddr;
183
                nwritten   <= 0;
184
                nread      <= 0;
185
                nracks     <= 0;
186
                nwacks     <= 0;
187
                cfg_len_nonzero <= (|cfg_len);
188
 
189
                // When the slave wishbone writes, and we are in this 
190
                // (ready) configuration, then allow the DMA to be controlled
191
                // and thus to start.
192 117 dgisselq
                if ((i_swb_stb)&&(i_swb_we))
193 21 dgisselq
                begin
194 69 dgisselq
                        case(i_swb_addr)
195
                        2'b00: begin
196 118 dgisselq
                                if ((i_swb_data[31:16] == 16'h0fed)
197 69 dgisselq
                                                &&(cfg_len_nonzero))
198
                                        dma_state <= `DMA_WAIT;
199
                                cfg_blocklen_sub_one
200
                                        <= i_swb_data[(LGMEMLEN-1):0]
201
                                        + {(LGMEMLEN){1'b1}};
202
                                        // i.e. -1;
203
                                cfg_dev_trigger    <= i_swb_data[14:10];
204
                                cfg_on_dev_trigger <= i_swb_data[15];
205
                                cfg_incs  <= ~i_swb_data[29];
206
                                cfg_incd  <= ~i_swb_data[28];
207 21 dgisselq
                                end
208 69 dgisselq
                        2'b01: begin
209
                                cfg_len   <=  i_swb_data[(AW-1):0];
210
                                cfg_len_nonzero <= (|i_swb_data[(AW-1):0]);
211
                                end
212
                        2'b10: cfg_raddr <=  i_swb_data[(AW-1):0];
213
                        2'b11: cfg_waddr <=  i_swb_data[(AW-1):0];
214
                        endcase
215
                end end
216
        `DMA_WAIT: begin
217
                o_mwb_addr <= cfg_raddr;
218
                nracks     <= 0;
219
                nwacks     <= 0;
220
                nwritten   <= 0;
221
                nread      <= 0;
222
                if (abort)
223
                        dma_state <= `DMA_IDLE;
224 118 dgisselq
                else if (user_halt)
225
                        dma_state <= `DMA_IDLE;
226 69 dgisselq
                else if (trigger)
227
                        dma_state <= `DMA_READ_REQ;
228
                end
229
        `DMA_READ_REQ: begin
230
                nwritten  <= 0;
231 21 dgisselq
 
232 69 dgisselq
                if (~i_mwb_stall)
233 21 dgisselq
                begin
234 69 dgisselq
                        // Number of read acknowledgements needed
235
                        nracks <= nracks+1;
236
                        if (last_read_request)
237
        //((nracks == {1'b0, cfg_blocklen_sub_one})||(bus_nracks == cfg_len-1))
238
                                // Wishbone interruptus
239
                                dma_state <= `DMA_READ_ACK;
240
                        if (cfg_incs)
241
                                o_mwb_addr <= o_mwb_addr
242
                                                + {{(AW-1){1'b0}},1'b1};
243
                end
244
 
245 118 dgisselq
                if (user_halt)
246
                        dma_state <= `DMA_READ_ACK;
247 69 dgisselq
                if (i_mwb_err)
248
                begin
249
                        cfg_len <= 0;
250
                        dma_state <= `DMA_IDLE;
251
                end
252 118 dgisselq
 
253 69 dgisselq
                if (abort)
254
                        dma_state <= `DMA_IDLE;
255
                if (i_mwb_ack)
256
                begin
257
                        nread <= nread+1;
258
                        if (cfg_incs)
259
                                cfg_raddr  <= cfg_raddr
260
                                                + {{(AW-1){1'b0}},1'b1};
261
                end end
262
        `DMA_READ_ACK: begin
263
                nwritten  <= 0;
264
 
265
                if (i_mwb_err)
266
                begin
267
                        cfg_len <= 0;
268
                        dma_state <= `DMA_IDLE;
269
                end else if (i_mwb_ack)
270
                begin
271
                        nread <= nread+1;
272
                        if (last_read_ack) // (nread+1 == nracks)
273
                                dma_state  <= `DMA_PRE_WRITE;
274 118 dgisselq
                        if (user_halt)
275
                                dma_state <= `DMA_IDLE;
276 69 dgisselq
                        if (cfg_incs)
277
                                cfg_raddr  <= cfg_raddr
278
                                                + {{(AW-1){1'b0}},1'b1};
279
                end
280
                if (abort)
281
                        dma_state <= `DMA_IDLE;
282
                end
283
        `DMA_PRE_WRITE: begin
284
                o_mwb_addr <= cfg_waddr;
285
                dma_state <= (abort)?`DMA_IDLE:`DMA_WRITE_REQ;
286
                end
287
        `DMA_WRITE_REQ: begin
288
                if (~i_mwb_stall)
289
                begin
290
                        nwritten <= nwritten+1;
291
                        if (last_write_request) // (nwritten == nread-1)
292
                                // Wishbone interruptus
293
                                dma_state <= `DMA_WRITE_ACK;
294
                        if (cfg_incd)
295 21 dgisselq
                        begin
296 69 dgisselq
                                o_mwb_addr <= o_mwb_addr
297
                                                + {{(AW-1){1'b0}},1'b1};
298
                                cfg_waddr  <= cfg_waddr
299
                                                + {{(AW-1){1'b0}},1'b1};
300 21 dgisselq
                        end
301 69 dgisselq
                end
302 21 dgisselq
 
303 69 dgisselq
                if (i_mwb_err)
304
                begin
305
                        cfg_len  <= 0;
306
                        dma_state <= `DMA_IDLE;
307
                end
308
                if (i_mwb_ack)
309
                begin
310
                        nwacks <= nwacks+1;
311
                        cfg_len <= cfg_len +{(AW){1'b1}}; // -1
312
                end
313 118 dgisselq
                if (user_halt)
314
                        dma_state <= `DMA_WRITE_ACK;
315 69 dgisselq
                if (abort)
316
                        dma_state <= `DMA_IDLE;
317
                end
318
        `DMA_WRITE_ACK: begin
319
                if (i_mwb_err)
320
                begin
321
                        cfg_len  <= 0;
322
                        nread    <= 0;
323
                        dma_state <= `DMA_IDLE;
324
                end else if (i_mwb_ack)
325
                begin
326
                        nwacks <= nwacks+1;
327
                        cfg_len <= cfg_len +{(AW){1'b1}};//cfg_len -= 1;
328
                        if (last_write_ack) // (nwacks+1 == nwritten)
329 21 dgisselq
                        begin
330
                                nread <= 0;
331 69 dgisselq
                                dma_state <= (cfg_len == 1)?`DMA_IDLE:`DMA_WAIT;
332 21 dgisselq
                        end
333
                end
334
 
335 69 dgisselq
                if (abort)
336
                        dma_state <= `DMA_IDLE;
337
                end
338
        default:
339
                dma_state <= `DMA_IDLE;
340
        endcase
341
 
342
        initial o_interrupt = 1'b0;
343
        always @(posedge i_clk)
344
                o_interrupt <= (dma_state == `DMA_WRITE_ACK)&&(i_mwb_ack)
345
                                &&(last_write_ack)
346
                                &&(cfg_len == {{(AW-1){1'b0}},1'b1});
347
 
348
        initial cfg_err = 1'b0;
349
        always @(posedge i_clk)
350
                if (dma_state == `DMA_IDLE)
351
                begin
352 117 dgisselq
                        if ((i_swb_stb)&&(i_swb_we)&&(i_swb_addr==2'b00))
353 69 dgisselq
                                cfg_err <= 1'b0;
354
                end else if (((i_mwb_err)&&(o_mwb_cyc))||(abort))
355
                        cfg_err <= 1'b1;
356
 
357
        initial last_read_request = 1'b0;
358
        always @(posedge i_clk)
359
                if ((dma_state == `DMA_WAIT)||(dma_state == `DMA_READ_REQ))
360
                begin
361
                        if ((~i_mwb_stall)&&(dma_state == `DMA_READ_REQ))
362
                        begin
363
                                last_read_request <=
364
                                (nracks + 1 == { 1'b0, cfg_blocklen_sub_one})
365
                                        ||(bus_nracks == cfg_len-2);
366
                        end else
367
                                last_read_request <=
368
                                        (nracks== { 1'b0, cfg_blocklen_sub_one})
369
                                        ||(bus_nracks == cfg_len-1);
370
                end else
371
                        last_read_request <= 1'b0;
372
 
373
        initial last_read_ack = 1'b0;
374
        always @(posedge i_clk)
375
                if ((dma_state == `DMA_READ_REQ)||(dma_state == `DMA_READ_ACK))
376
                begin
377 117 dgisselq
                        if ((i_mwb_ack)&&((~o_mwb_stb)||(i_mwb_stall)))
378 69 dgisselq
                                last_read_ack <= (nread+2 == nracks);
379
                        else
380
                                last_read_ack <= (nread+1 == nracks);
381
                end else
382
                        last_read_ack <= 1'b0;
383
 
384
        initial last_write_request = 1'b0;
385
        always @(posedge i_clk)
386
                if (dma_state == `DMA_PRE_WRITE)
387
                        last_write_request <= (nread <= 1);
388
                else if (dma_state == `DMA_WRITE_REQ)
389
                begin
390
                        if (i_mwb_stall)
391
                                last_write_request <= (nwritten >= nread-1);
392
                        else
393
                                last_write_request <= (nwritten >= nread-2);
394
                end else
395
                        last_write_request <= 1'b0;
396
 
397
        initial last_write_ack = 1'b0;
398
        always @(posedge i_clk)
399
                if((dma_state == `DMA_WRITE_REQ)||(dma_state == `DMA_WRITE_ACK))
400
                begin
401 117 dgisselq
                        if ((i_mwb_ack)&&((~o_mwb_stb)||(i_mwb_stall)))
402 69 dgisselq
                                last_write_ack <= (nwacks+2 == nwritten);
403
                        else
404
                                last_write_ack <= (nwacks+1 == nwritten);
405
                end else
406
                        last_write_ack <= 1'b0;
407
 
408
        assign  o_mwb_cyc = (dma_state == `DMA_READ_REQ)
409
                        ||(dma_state == `DMA_READ_ACK)
410
                        ||(dma_state == `DMA_WRITE_REQ)
411
                        ||(dma_state == `DMA_WRITE_ACK);
412
 
413
        assign  o_mwb_stb = (dma_state == `DMA_READ_REQ)
414
                        ||(dma_state == `DMA_WRITE_REQ);
415
 
416
        assign  o_mwb_we = (dma_state == `DMA_PRE_WRITE)
417
                        ||(dma_state == `DMA_WRITE_REQ)
418
                        ||(dma_state == `DMA_WRITE_ACK);
419
 
420 21 dgisselq
        //
421
        // This is tricky.  In order for Vivado to consider dma_mem to be a 
422
        // proper memory, it must have a simple address fed into it.  Hence
423
        // the read_address (rdaddr) register.  The problem is that this
424
        // register must always be one greater than the address we actually
425
        // want to read from, unless we are idling.  So ... the math is touchy.
426
        //
427
        reg     [(LGMEMLEN-1):0] rdaddr;
428
        always @(posedge i_clk)
429 69 dgisselq
                if((dma_state == `DMA_IDLE)||(dma_state == `DMA_WAIT)
430
                                ||(dma_state == `DMA_WRITE_ACK))
431
                        rdaddr <= 0;
432
                else if ((dma_state == `DMA_PRE_WRITE)
433
                                ||((dma_state==`DMA_WRITE_REQ)&&(~i_mwb_stall)))
434
                        rdaddr <= rdaddr + {{(LGMEMLEN-1){1'b0}},1'b1};
435 21 dgisselq
        always @(posedge i_clk)
436 69 dgisselq
                if ((dma_state != `DMA_WRITE_REQ)||(~i_mwb_stall))
437 21 dgisselq
                        o_mwb_data <= dma_mem[rdaddr];
438
        always @(posedge i_clk)
439 69 dgisselq
                if((dma_state == `DMA_READ_REQ)||(dma_state == `DMA_READ_ACK))
440 21 dgisselq
                        dma_mem[nread[(LGMEMLEN-1):0]] <= i_mwb_data;
441
 
442
        always @(posedge i_clk)
443
                casez(i_swb_addr)
444 69 dgisselq
                2'b00: o_swb_data <= {  (dma_state != `DMA_IDLE), cfg_err,
445 21 dgisselq
                                        ~cfg_incs, ~cfg_incd,
446
                                        1'b0, nread,
447
                                        cfg_on_dev_trigger, cfg_dev_trigger,
448
                                        cfg_blocklen_sub_one
449
                                        };
450
                2'b01: o_swb_data <= { {(DW-AW){1'b0}}, cfg_len  };
451
                2'b10: o_swb_data <= { {(DW-AW){1'b0}}, cfg_raddr};
452
                2'b11: o_swb_data <= { {(DW-AW){1'b0}}, cfg_waddr};
453
                endcase
454
 
455 69 dgisselq
        // This causes us to wait a minimum of two clocks before starting: One
456
        // to go into the wait state, and then one while in the wait state to
457
        // develop the trigger.
458
        initial trigger = 1'b0;
459 21 dgisselq
        always @(posedge i_clk)
460 69 dgisselq
                trigger <=  (dma_state == `DMA_WAIT)
461
                                &&((~cfg_on_dev_trigger)
462
                                        ||(i_dev_ints[cfg_dev_trigger]));
463 21 dgisselq
 
464 69 dgisselq
        // Ack any access.  We'll quietly ignore any access where we are busy,
465
        // but ack it anyway.  In other words, before writing to the device,
466
        // double check that it isn't busy, and then write.
467
        always @(posedge i_clk)
468 117 dgisselq
                o_swb_ack <= (i_swb_stb);
469 69 dgisselq
 
470 21 dgisselq
        assign  o_swb_stall = 1'b0;
471
 
472 69 dgisselq
        initial abort = 1'b0;
473
        always @(posedge i_clk)
474 117 dgisselq
                abort <= (i_rst)||((i_swb_stb)&&(i_swb_we)
475 69 dgisselq
                        &&(i_swb_addr == 2'b00)
476
                        &&(i_swb_data == 32'hffed0000));
477
 
478 118 dgisselq
        initial user_halt = 1'b0;
479
        always @(posedge i_clk)
480
                user_halt <= ((user_halt)&&(dma_state != `DMA_IDLE))
481
                        ||((i_swb_stb)&&(i_swb_we)&&(dma_state != `DMA_IDLE)
482
                                &&(i_swb_addr == 2'b00)
483
                                &&(i_swb_data == 32'hafed0000));
484
 
485 21 dgisselq
endmodule
486
 

powered by: WebSVN 2.1.0

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