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

Subversion Repositories s6soc

[/] [s6soc/] [trunk/] [rtl/] [busmaster.v] - Blame information for rev 4

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

Line No. Rev Author Line
1 4 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 2 dgisselq
//
3 4 dgisselq
// Filename:    busmaster.v
4 2 dgisselq
//
5 4 dgisselq
// Project:     CMod S6 System on a Chip, ZipCPU demonstration project
6 2 dgisselq
//
7 4 dgisselq
// Purpose:     
8 2 dgisselq
//
9 4 dgisselq
// Creator:     Dan Gisselquist, Ph.D.
10 2 dgisselq
//              Gisselquist Technology, LLC
11
//
12 4 dgisselq
////////////////////////////////////////////////////////////////////////////////
13 2 dgisselq
//
14 4 dgisselq
// Copyright (C) 2015-2016, Gisselquist Technology, LLC
15 2 dgisselq
//
16 4 dgisselq
// This program is free software (firmware): you can redistribute it and/or
17
// modify it under the terms of  the GNU General Public License as published
18
// by the Free Software Foundation, either version 3 of the License, or (at
19
// your option) any later version.
20
//
21
// This program is distributed in the hope that it will be useful, but WITHOUT
22
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
23
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
24
// for more details.
25
//
26
// You should have received a copy of the GNU General Public License along
27
// with this program.  (It's in the $(ROOT)/doc directory, run make with no
28
// target there if the PDF file isn't present.)  If not, see
29
// <http://www.gnu.org/licenses/> for a copy.
30
//
31
// License:     GPL, v3, as defined and found on www.gnu.org,
32
//              http://www.gnu.org/licenses/gpl.html
33
//
34
//
35
////////////////////////////////////////////////////////////////////////////////
36
//
37
//
38
//
39 2 dgisselq
`include "builddate.v"
40
//
41
`define INCLUDE_ZIPPY
42 4 dgisselq
`define IMPLEMENT_ONCHIP_RAM    // 2804 w/o after synthesis
43 2 dgisselq
`ifndef VERILATOR
44
`define FANCY_ICAP_ACCESS
45
`endif
46
`define FLASH_ACCESS
47 4 dgisselq
// `define      CFG_SCOPE       // About 204 LUTs, at 2^6 addresses
48
`define INCLUDE_RTC     // About 90 LUTs
49 2 dgisselq
module  busmaster(i_clk, i_rst,
50
                i_rx_stb, i_rx_data, o_tx_stb, o_tx_data, i_tx_busy,
51 4 dgisselq
                        o_uart_rts,
52 2 dgisselq
                // The SPI Flash lines
53
                o_qspi_cs_n, o_qspi_sck, o_qspi_dat, i_qspi_dat, o_qspi_mod,
54
                // The board I/O
55
                i_btn, o_led, o_pwm, o_pwm_aux,
56
                // Keypad connections
57
                i_kp_row, o_kp_col,
58
                // UART control
59
                o_uart_setup,
60
                // GPIO lines
61
                i_gpio, o_gpio);
62
        parameter       ZIP_ADDRESS_WIDTH=23, ZA=ZIP_ADDRESS_WIDTH,
63
                        CMOD_ZIPCPU_RESET_ADDRESS=23'h400100,
64
                        BUS_ADDRESS_WIDTH=23, BAW=23; // 24bits->2,258,23b->2181
65
        input                   i_clk, i_rst;
66
        // The bus commander, via an external JTAG port
67
        input                   i_rx_stb;
68
        input           [7:0]    i_rx_data;
69
        output  reg             o_tx_stb;
70
        output  reg     [7:0]    o_tx_data;
71
        input                   i_tx_busy;
72 4 dgisselq
        output  wire            o_uart_rts;
73 2 dgisselq
        // SPI flash control
74
        output  wire            o_qspi_cs_n, o_qspi_sck;
75
        output  wire    [3:0]    o_qspi_dat;
76
        input           [3:0]    i_qspi_dat;
77
        output  wire    [1:0]    o_qspi_mod;
78
        // Board I/O
79
        input           [1:0]    i_btn;
80
        output  wire    [3:0]    o_led;
81
        output  wire            o_pwm;
82
        output  wire    [1:0]    o_pwm_aux;
83
        // Keypad
84
        input           [3:0]    i_kp_row;
85
        output  wire    [3:0]    o_kp_col;
86
        // UART control
87
        output  wire    [29:0]   o_uart_setup;
88
        // GPIO liines
89
        input           [15:0]   i_gpio;
90
        output  wire    [15:0]   o_gpio;
91
 
92
 
93
        //
94
        //
95
        // Master wishbone wires
96
        //
97
        //
98
        wire            wb_cyc, wb_stb, wb_we, wb_stall, wb_ack, wb_err;
99
        wire    [31:0]   wb_data, wb_idata;
100
        wire    [(BAW-1):0]      wb_addr;
101
        wire    [5:0]            io_addr;
102
        assign  io_addr = {
103
                        wb_addr[22],    // Flash
104
                        wb_addr[13],    // RAM
105
                        wb_addr[11],    // RTC
106
                        wb_addr[10],    // CFG
107
                        wb_addr[ 9],    // SCOPE
108
                        wb_addr[ 8] };  // I/O
109
 
110
        // Wires going to devices
111
        // And then headed back home
112
        wire    w_interrupt;
113
        // Oh, and the debug control for the ZIP CPU
114
        wire            zip_dbg_ack, zip_dbg_stall;
115
        wire    [31:0]   zip_dbg_data;
116
 
117
 
118
        //
119
        //
120
        // The BUS master (source): The ZipCPU
121
        //
122
        //
123
        wire            zip_cyc, zip_stb, zip_we, zip_cpu_int;
124
        wire    [(ZA-1):0]       w_zip_addr;
125
        wire    [(BAW-1):0]      zip_addr;
126
        wire    [31:0]           zip_data;
127
        // and then coming from devices
128
        wire            zip_ack, zip_stall, zip_err;
129
        wire    dwb_we, dwb_stb, dwb_cyc, dwb_ack, dwb_stall, dwb_err;
130
        wire    [(BAW-1):0]      dwb_addr;
131
        wire    [31:0]           dwb_odata;
132
 
133
        // wire [31:0]  zip_debug;
134
//
135
// We'll define our RESET_ADDRESS to be halfway through our flash memory.
136
//      `define CMOD_ZIPCPU_RESET_ADDRESS       23'h600000
137
//
138
// Ahm, No.  We can actually do much better than that.  Our toplevel *.bit file
139
// only takes up only 335kB.  Let's give it some room to grow to 1024 kB.  Then
140
// 23 can start our ROM at 23'h400100
141
//
142
// Not so fast.  In hindsight, we really want to be  able to adjust the load and
143
// the program separately.  So, instead, let's place our RESET address at the
144
// second flash erase block.  That way, we can change our program code found
145
// in the flash without needing to change our FPGA load and vice versa.
146
//
147
// 23'h404000
148
        zipbones #(CMOD_ZIPCPU_RESET_ADDRESS,ZA,6)
149
                thecpu(i_clk, 1'b0,
150
                        // Zippys wishbone interface
151
                        wb_cyc, wb_stb, wb_we, w_zip_addr, wb_data,
152
                                wb_ack, wb_stall, wb_idata, wb_err,
153
                        w_interrupt, zip_cpu_int,
154
                        // Debug wishbone interface
155
                        1'b0, 1'b0,1'b0, 1'b0, 32'h00,
156
                                zip_dbg_ack, zip_dbg_stall, zip_dbg_data);
157
        generate
158
        if (ZA < BAW)
159
                assign  wb_addr = { {(BAW-ZA){1'b0}}, w_zip_addr };
160
        else
161
                assign  wb_addr = w_zip_addr;
162
        endgenerate
163
 
164
        wire    io_sel, flash_sel, flctl_sel, scop_sel, cfg_sel, mem_sel,
165
                        rtc_sel, none_sel, many_sel;
166
        wire    flash_ack, scop_ack, cfg_ack, mem_ack;
167
        wire    rtc_ack, rtc_stall;
168
`ifdef  INCLUDE_RTC
169
        assign  rtc_stall = 1'b0;
170
`endif
171
        wire    io_stall, flash_stall, scop_stall, cfg_stall, mem_stall;
172 4 dgisselq
        reg     io_ack;
173 2 dgisselq
 
174
        wire    [31:0]   flash_data, scop_data, cfg_data, mem_data, pwm_data,
175
                        spio_data, gpio_data, uart_data;
176
        reg     [31:0]   io_data;
177
        reg     [(BAW-1):0]      bus_err_addr;
178
 
179
        assign  wb_ack = (wb_cyc)&&((io_ack)||(scop_ack)||(cfg_ack)
180
`ifdef  INCLUDE_RTC
181
                                ||(rtc_ack)
182
`endif
183
                                ||(mem_ack)||(flash_ack)||((none_sel)&&(1'b1)));
184
        assign  wb_stall = ((io_sel)&&(io_stall))
185
                        ||((scop_sel)&&(scop_stall))
186
                        ||((cfg_sel)&&(cfg_stall))
187
                        ||((mem_sel)&&(mem_stall))
188
`ifdef  INCLUDE_RTC
189
                        ||((rtc_sel)&&(rtc_stall))
190
`endif
191
                        ||((flash_sel||flctl_sel)&&(flash_stall));
192
                        // (none_sel)&&(1'b0)
193
 
194
        /*
195
        assign  wb_idata = (io_ack)?io_data
196
                        : ((scop_ack)?scop_data
197
                        : ((cfg_ack)?cfg_data
198
                        : ((mem_ack)?mem_data
199
                        : ((flash_ack)?flash_data
200
                        : 32'h00))));
201
        */
202
        assign  wb_idata =  (io_ack|scop_ack)?((io_ack )? io_data  : scop_data)
203
                        : ((mem_ack|rtc_ack)?((mem_ack)?mem_data:rtc_data)
204 4 dgisselq
                        : ((cfg_ack) ? cfg_data : flash_data));//if (flash_ack)
205 2 dgisselq
        assign  wb_err = ((wb_cyc)&&(wb_stb)&&(none_sel || many_sel)) || many_ack;
206
 
207
        // Addresses ...
208
        //      0000 xxxx       configuration/control registers
209
        //      1 xxxx xxxx xxxx xxxx xxxx      Up-sampler taps
210
        assign  io_sel   =((wb_cyc)&&(io_addr[5:0]==6'h1));
211
        assign  flctl_sel= 1'b0; // ((wb_cyc)&&(io_addr[5:1]==5'h1));
212
        assign  scop_sel =((wb_cyc)&&(io_addr[5:1]==5'h1));
213
        assign  cfg_sel  =((wb_cyc)&&(io_addr[5:2]==4'h1));
214
        // zip_sel is not on the bus at this point
215
`ifdef  INCLUDE_RTC
216
        assign  rtc_sel  =((wb_cyc)&&(io_addr[5:3]==3'h1));
217
`endif
218
        assign  mem_sel  =((wb_cyc)&&(io_addr[5:4]==2'h1));
219
        assign  flash_sel=((wb_cyc)&&(io_addr[5]));
220
 
221
        assign  none_sel =((wb_cyc)&&(wb_stb)&&(io_addr==6'h0));
222
        /*
223
        assign  many_sel =((wb_cyc)&&(wb_stb)&&(
224
                         {3'h0, io_sel}
225
                        +{3'h0, flctl_sel}
226
                        // +{3'h0, scop_sel}
227
                        +{3'h0, cfg_sel}
228
                        +{3'h0, mem_sel}
229
                        +{3'h0, flash_sel} > 1));
230
        */
231
        assign  many_sel = 1'b0;
232
 
233
        wire    many_ack;
234
        assign  many_ack =((wb_cyc)&&(
235
                         {3'h0, io_ack}
236
                        +{3'h0, scop_ack}
237
                        +{3'h0, cfg_ack}
238
`ifdef  INCLUDE_RTC
239
                        +{3'h0, rtc_ack}
240
`endif
241
                        +{3'h0, mem_ack}
242
                        +{3'h0, flash_ack} > 1));
243
 
244
        wire            flash_interrupt, scop_interrupt, tmra_int, tmrb_int,
245
                        rtc_interrupt, gpio_int, pwm_int, keypad_int,button_int;
246
 
247
 
248
        //
249
        //
250
        //
251
        reg             rx_rdy;
252
        wire    [10:0]   int_vector;
253
        assign  int_vector = { gpio_int, pwm_int, keypad_int,
254
                                ~i_tx_busy, rx_rdy, tmrb_int, tmra_int,
255
                                rtc_interrupt, scop_interrupt,
256
                                wb_err, button_int };
257
 
258
        wire    [31:0]   pic_data;
259 4 dgisselq
        icontrol #(11)  pic(i_clk, 1'b0, (wb_stb)&&(io_sel)
260 2 dgisselq
                                        &&(wb_addr[3:0]==4'h0)&&(wb_we),
261
                        wb_data, pic_data, int_vector, w_interrupt);
262
 
263
        initial bus_err_addr = `DATESTAMP;
264
        always @(posedge i_clk)
265
                if (wb_err)
266
                        bus_err_addr <= wb_addr;
267
 
268
        wire            zta_ack, zta_stall, ztb_ack, ztb_stall;
269
        wire    [31:0]   timer_a, timer_b;
270 4 dgisselq
        ziptimer        #(32,20)
271
                zipt_a(i_clk, 1'b0, 1'b1, wb_cyc,
272 2 dgisselq
                                (wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h2),
273
                                wb_we, wb_data, zta_ack, zta_stall, timer_a,
274
                                tmra_int);
275 4 dgisselq
        ziptimer        #(32,20)
276
                zipt_b(i_clk, 1'b0, 1'b1, wb_cyc,
277 2 dgisselq
                                (wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h3),
278
                                wb_we, wb_data, ztb_ack, ztb_stall, timer_b,
279
                                tmrb_int);
280
 
281
        wire    [31:0]   rtc_data;
282
`ifdef  INCLUDE_RTC
283
        wire    rtcd_ack, rtcd_stall, ppd;
284
        // rtcdate      thedate(i_clk, ppd, wb_cyc, (wb_stb)&&(io_sel), wb_we,
285
                        // wb_data, rtcd_ack, rtcd_stall, date_data);
286
        reg     r_rtc_ack;
287
        initial r_rtc_ack = 1'b0;
288
        always @(posedge i_clk)
289
                r_rtc_ack <= ((wb_stb)&&(rtc_sel));
290
        assign  rtc_ack = r_rtc_ack;
291
 
292
        rtclight
293 4 dgisselq
                #(32'h35afe5,23)        // 80 MHz clock
294 2 dgisselq
                thetime(i_clk, wb_cyc,
295
                        ((wb_stb)&&(rtc_sel)), wb_we,
296
                        { 1'b0, wb_addr[1:0] }, wb_data, rtc_data,
297
                        rtc_interrupt, ppd);
298
`else
299
        assign  rtc_interrupt = 1'b0;
300
        assign  rtc_data = 32'h00;
301
        assign  rtc_ack  = 1'b0;
302
`endif
303
 
304
        always @(posedge i_clk)
305
                case(wb_addr[3:0])
306
                        4'h0: io_data <= pic_data;
307
                        4'h1: io_data <= { {(32-BAW){1'b0}}, bus_err_addr };
308
                        4'h2: io_data <= timer_a;
309
                        4'h3: io_data <= timer_b;
310
                        4'h4: io_data <= pwm_data;
311
                        4'h5: io_data <= spio_data;
312
                        4'h6: io_data <= gpio_data;
313
                        4'h7: io_data <= uart_data;
314
                        default: io_data <= `DATESTAMP;
315
                        // 4'h8: io_data <= `DATESTAMP;
316
                endcase
317
        always @(posedge i_clk)
318
                io_ack <= (wb_cyc)&&(wb_stb)&&(io_sel);
319
        assign  io_stall = 1'b0;
320
 
321
        wire    pwm_ack, pwm_stall;
322
        wbpwmaudio      theaudio(i_clk, wb_cyc,
323
                                ((wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h4)), wb_we,
324
                                1'b0, wb_data,
325
                                pwm_ack, pwm_stall, pwm_data, o_pwm, o_pwm_aux,
326
                                pwm_int);
327
 
328
        //
329
        // Special Purpose I/O: Keypad, button, LED status and control
330
        //
331
        spio    thespio(i_clk, wb_cyc,(wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h5),wb_we,
332
                        wb_data, spio_data, o_kp_col, i_kp_row, i_btn, o_led,
333
                        keypad_int, button_int);
334
 
335
        //
336
        // General purpose (sort of) I/O:  (Bottom two bits robbed in each
337
        // direction for an I2C link at the toplevel.v design)
338
        //
339
        wbgpio  #(16,16,16'hffff) thegpio(i_clk, wb_cyc,
340
                        (wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h6), wb_we,
341
                        wb_data, gpio_data, i_gpio, o_gpio, gpio_int);
342
 
343
        //
344
        //
345
        //      Rudimentary serial port control
346
        //
347
        reg     [7:0]    r_rx_data;
348
        // Baud rate is set by clock rate / baud rate.
349
        // Thus, 80MHz / 115200MBau
350
        //      = 694.4, or about 0x2b6. 
351
        // although the CPU might struggle to keep up at this speed without a
352
        // hardware buffer.
353
        //
354
        // We'll add the flag for two stop bits.
355
        assign  o_uart_setup = 30'h080002b6; // 115200 MBaud @ an 80MHz clock
356
 
357
        initial o_tx_stb = 1'b0;
358
        initial o_tx_data = 8'h00;
359
        always @(posedge i_clk)
360 4 dgisselq
                if ((wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h7)&&(wb_we))
361 2 dgisselq
                begin
362
                        o_tx_data <= wb_data[7:0];
363
                        o_tx_stb <= 1'b1;
364
                end
365
                else if ((o_tx_stb)&&(~i_tx_busy))
366
                        o_tx_stb <= 1'b0;
367
        initial rx_rdy = 1'b0;
368
        always @(posedge i_clk)
369
                if (i_rx_stb)
370
                        r_rx_data <= i_rx_data;
371
        always @(posedge i_clk)
372
        begin
373 4 dgisselq
                if((wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h7)&&(~wb_we))
374 2 dgisselq
                        rx_rdy <= i_rx_stb;
375
                else if (i_rx_stb)
376
                        rx_rdy <= (rx_rdy | i_rx_stb);
377
        end
378 4 dgisselq
        assign  o_uart_rts = (~rx_rdy);
379 2 dgisselq
        assign  uart_data = { 23'h0, ~rx_rdy, r_rx_data };
380 4 dgisselq
        //
381
        // uart_ack gets returned as part of io_ack, since that happens when
382
        // io_sel and wb_stb are defined
383
        //
384
        // always @(posedge i_clk)
385
                // uart_ack<= ((wb_stb)&&(io_sel)&&(wb_addr[3:0]==4'h7));
386 2 dgisselq
 
387
 
388
 
389
        //
390
        //      FLASH MEMORY CONFIGURATION ACCESS
391
        //
392
        wire    flash_cs_n, flash_sck, flash_mosi;
393
        wbqspiflash #(24)       flashmem(i_clk,
394
                wb_cyc,(wb_stb&&flash_sel),(wb_stb)&&(flctl_sel),wb_we,
395 4 dgisselq
                        wb_addr[(24-3):0], wb_data,
396 2 dgisselq
                flash_ack, flash_stall, flash_data,
397
                o_qspi_sck, o_qspi_cs_n, o_qspi_mod, o_qspi_dat, i_qspi_dat,
398
                flash_interrupt);
399
 
400
        //
401
        //      MULTIBOOT/ICAPE2 CONFIGURATION ACCESS
402
        //
403
        wire    [31:0]   cfg_scope;
404
`ifdef  FANCY_ICAP_ACCESS
405
        wbicape6        fpga_cfg(i_clk, wb_cyc,(cfg_sel)&&(wb_stb), wb_we,
406
                                wb_addr[5:0], wb_data,
407
                                cfg_ack, cfg_stall, cfg_data,
408
                                cfg_scope);
409
`else
410
        reg     r_cfg_ack;
411
        always @(posedge i_clk)
412
                r_cfg_ack <= (wb_cyc)&&(cfg_sel)&&(wb_stb);
413
        assign  cfg_ack   = r_cfg_ack;
414
        assign  cfg_stall = 1'b0;
415
        assign  cfg_data  = 32'h00;
416
        assign  cfg_scope = 32'h00;
417
`endif
418
 
419
 
420
        //
421
        //      ON-CHIP RAM MEMORY ACCESS
422
        //
423 4 dgisselq
`ifdef  IMPLEMENT_ONCHIP_RAM
424 2 dgisselq
        memdev  #(12) ram(i_clk, wb_cyc, (wb_stb)&&(mem_sel), wb_we,
425
                        wb_addr[11:0], wb_data, mem_ack, mem_stall, mem_data);
426 4 dgisselq
`else
427
        assign  mem_data = 32'h00;
428
        assign  mem_stall = 1'b0;
429
        reg     r_mem_ack;
430
        always @(posedge i_clk)
431
                r_mem_ack <= (wb_cyc)&&(wb_stb)&&(mem_sel);
432
        assign  mem_ack = r_mem_ack;
433
`endif
434 2 dgisselq
 
435
        //
436
        //
437
        //      WISHBONE SCOPE
438
        //
439
        //
440
        //
441
        //
442
        wire    [31:0]   scop_cfg_data;
443
        wire            scop_cfg_ack, scop_cfg_stall, scop_cfg_interrupt;
444
`ifdef  CFG_SCOPE
445
        wire            scop_cfg_trigger;
446
        assign  scop_cfg_trigger = (wb_cyc)&&(wb_stb)&&(cfg_sel);
447 4 dgisselq
        wbscope #(5'h6) wbcfgscope(i_clk, 1'b1, scop_cfg_trigger, cfg_scope,
448 2 dgisselq
                // Wishbone interface
449 4 dgisselq
                i_clk, wb_cyc, (wb_stb)&&(scop_sel),
450 2 dgisselq
                                wb_we, wb_addr[0], wb_data,
451
                        scop_cfg_ack, scop_cfg_stall, scop_cfg_data,
452
                scop_cfg_interrupt);
453 4 dgisselq
`else
454
        reg     r_scop_cfg_ack;
455
        always @(posedge i_clk)
456
                r_scop_cfg_ack <= (wb_cyc)&&(wb_stb)&&(scop_sel);
457
        assign  scop_cfg_ack = r_scop_cfg_ack;
458
        assign  scop_cfg_data = 32'h000;
459
        assign  scop_cfg_stall= 1'b0;
460 2 dgisselq
`endif
461
 
462
        assign  scop_interrupt = scop_cfg_interrupt;
463
        assign  scop_ack   = scop_cfg_ack;
464
        assign  scop_stall = scop_cfg_stall;
465
        assign  scop_data  = scop_cfg_data;
466
 
467
endmodule
468
 

powered by: WebSVN 2.1.0

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