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

Subversion Repositories openarty

[/] [openarty/] [trunk/] [rtl/] [cpu/] [zipsystem.v] - Blame information for rev 50

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
2 3 dgisselq
//
3
// Filename:    zipsystem.v
4
//
5
// Project:     Zip CPU -- a small, lightweight, RISC CPU soft core
6
//
7
// Purpose:     This portion of the ZIP CPU implements a number of soft
8
//              peripherals to the CPU nearby its CORE.  The functionality
9
//              sits on the data bus, and does not include any true
10
//              external hardware peripherals.  The peripherals included here
11
//              include:
12
//
13
//
14
//      Local interrupt controller--for any/all of the interrupts generated
15
//              here.  This would include a pin for interrupts generated
16
//              elsewhere, so this interrupt controller could be a master
17
//              handling all interrupts.  My interrupt controller would work
18
//              for this purpose.
19
//
20
//              The ZIP-CPU supports only one interrupt because, as I understand
21
//              modern systems (Linux), they tend to send all interrupts to the
22
//              same interrupt vector anyway.  Hence, that's what we do here.
23
//
24
//      Bus Error interrupts -- generates an interrupt any time the wishbone
25
//              bus produces an error on a given access, for whatever purpose
26
//              also records the address on the bus at the time of the error.
27
//
28
//      Trap instructions
29
//              Writing to this "register" will always create an interrupt.
30
//              After the interrupt, this register may be read to see what
31
//              value had been written to it.
32
//
33
//      Bit reverse register ... ?
34
//
35
//      (Potentially an eventual floating point co-processor ...)
36
//
37
//      Real-time clock
38
//
39
//      Interval timer(s) (Count down from fixed value, and either stop on
40
//              zero, or issue an interrupt and restart automatically on zero)
41
//              These can be implemented as watchdog timers if desired--the
42
//              only difference is that a watchdog timer's interrupt feeds the
43
//              reset line instead of the processor interrupt line.
44
//
45
//      Watch-dog timer: this is the same as an interval timer, only it's
46
//              interrupt/time-out line is wired to the reset line instead of
47
//              the interrupt line of the CPU.
48
//
49
//      ROM Memory map
50
//              Set a register to control this map, and a DMA will begin to
51
//              fill this memory from a slower FLASH.  Once filled, accesses
52
//              will be from this memory instead of 
53
//
54
//
55
//      Doing some market comparison, let's look at what peripherals a TI
56
//      MSP430 might offer: MSP's may have I2C ports, SPI, UART, DMA, ADC,
57
//      Comparators, 16,32-bit timers, 16x16 or 32x32 timers, AES, BSL,
58
//      brown-out-reset(s), real-time-clocks, temperature sensors, USB ports,
59
//      Spi-Bi-Wire, UART Boot-strap Loader (BSL), programmable digital I/O,
60
//      watchdog-timers,
61
//
62
// Creator:     Dan Gisselquist, Ph.D.
63
//              Gisselquist Technology, LLC
64
//
65 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
66 3 dgisselq
//
67 50 dgisselq
// Copyright (C) 2015-2017, Gisselquist Technology, LLC
68 3 dgisselq
//
69
// This program is free software (firmware): you can redistribute it and/or
70
// modify it under the terms of  the GNU General Public License as published
71
// by the Free Software Foundation, either version 3 of the License, or (at
72
// your option) any later version.
73
//
74
// This program is distributed in the hope that it will be useful, but WITHOUT
75
// ANY WARRANTY; without even the implied warranty of MERCHANTIBILITY or
76
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
77
// for more details.
78
//
79 50 dgisselq
// You should have received a copy of the GNU General Public License along
80
// with this program.  (It's in the $(ROOT)/doc directory.  Run make with no
81
// target there if the PDF file isn't present.)  If not, see
82
// <http://www.gnu.org/licenses/> for a copy.
83
//
84 3 dgisselq
// License:     GPL, v3, as defined and found on www.gnu.org,
85
//              http://www.gnu.org/licenses/gpl.html
86
//
87
//
88 50 dgisselq
////////////////////////////////////////////////////////////////////////////////
89 3 dgisselq
//
90 50 dgisselq
//
91 3 dgisselq
`include "cpudefs.v"
92
//
93
// While I hate adding delays to any bus access, this next delay is required
94
// to make timing close in my Basys-3 design.
95
`define DELAY_DBG_BUS
96
// On my previous version, I needed to add a delay to access the external
97
// bus.  Activate the define below and that delay will be put back into place.
98
// This particular version no longer needs the delay in order to run at 
99
// 100 MHz.  Timing indicates I may even run this at 250 MHz without the
100
// delay too, so we're doing better.  To get rid of this, I placed the logic
101
// determining whether or not I was accessing the local system bus one clock
102
// earlier, or into the memops.v file.  This also required my wishbone bus
103
// arbiter to maintain the bus selection as well, so that got updated ...
104
// you get the picture.  But, the bottom line is that I no longer need this
105
// delay.
106
//
107
// `define      DELAY_EXT_BUS   // Required no longer!
108
//
109
//
110
// If space is tight, you might not wish to have your performance and
111
// accounting counters, so let's make those optional here
112
//      Without this flag, Slice LUT count is 3315 (ZipSystem),2432 (ZipCPU)
113
//      When including counters, 
114
//              Slice LUTs      ZipSystem       ZipCPU
115
//      With Counters           3315            2432
116
//      Without Counters        2796            2046
117
 
118
//
119
// Now, where am I placing all of my peripherals?
120
`define PERIPHBASE      32'hc0000000
121
`define INTCTRL         5'h0    // 
122
`define WATCHDOG        5'h1    // Interrupt generates reset signal
123
`define BUSWATCHDOG     5'h2    // Sets IVEC[0]
124
`define CTRINT          5'h3    // Sets IVEC[5]
125
`define TIMER_A         5'h4    // Sets IVEC[4]
126
`define TIMER_B         5'h5    // Sets IVEC[3]
127
`define TIMER_C         5'h6    // Sets IVEC[2]
128
`define JIFFIES         5'h7    // Sets IVEC[1]
129
 
130
 
131
`ifdef  INCLUDE_ACCOUNTING_COUNTERS
132
`define MSTR_TASK_CTR   5'h08
133
`define MSTR_MSTL_CTR   5'h09
134
`define MSTR_PSTL_CTR   5'h0a
135
`define MSTR_INST_CTR   5'h0b
136
`define USER_TASK_CTR   5'h0c
137
`define USER_MSTL_CTR   5'h0d
138
`define USER_PSTL_CTR   5'h0e
139
`define USER_INST_CTR   5'h0f
140
`endif
141
 
142
// Although I have a hole at 5'h2, the DMA controller requires four wishbone
143
// addresses, therefore we place it by itself and expand our address bus
144
// width here by another bit.
145
`define DMAC            5'h10
146
 
147
// `define      RTC_CLOCK       32'hc0000008    // A global something
148
// `define      BITREV          32'hc0000003
149
//
150
//      DBGCTRL
151
//              10 HALT
152
//               9 HALT(ED)
153
//               8 STEP (W=1 steps, and returns to halted)
154
//               7 INTERRUPT-FLAG
155
//               6 RESET_FLAG
156
//              ADDRESS:
157
//               5      PERIPHERAL-BIT
158
//              [4:0]   REGISTER-ADDR
159
//      DBGDATA
160
//              read/writes internal registers
161
//
162
//
163
//
164
module  zipsystem(i_clk, i_rst,
165
                // Wishbone master interface from the CPU
166 50 dgisselq
                o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, o_wb_sel,
167 3 dgisselq
                        i_wb_ack, i_wb_stall, i_wb_data, i_wb_err,
168
                // Incoming interrupts
169
                i_ext_int,
170
                // Our one outgoing interrupt
171
                o_ext_int,
172
                // Wishbone slave interface for debugging purposes
173
                i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr, i_dbg_data,
174
                        o_dbg_ack, o_dbg_stall, o_dbg_data
175
`ifdef  DEBUG_SCOPE
176
                , o_cpu_debug
177
`endif
178
                );
179 50 dgisselq
        parameter       RESET_ADDRESS=32'h0100000, ADDRESS_WIDTH=30,
180 3 dgisselq
                        LGICACHE=10, START_HALTED=1, EXTERNAL_INTERRUPTS=1,
181
`ifdef  OPT_MULTIPLY
182
                        IMPLEMENT_MPY = `OPT_MULTIPLY,
183
`else
184
                        IMPLEMENT_MPY = 0,
185
`endif
186
`ifdef  OPT_DIVIDE
187
                        IMPLEMENT_DIVIDE=1,
188
`else
189
                        IMPLEMENT_DIVIDE=0,
190
`endif
191
`ifdef  OPT_IMPLEMENT_FPU
192
                        IMPLEMENT_FPU=1,
193
`else
194
                        IMPLEMENT_FPU=0,
195
`endif
196 50 dgisselq
                        IMPLEMENT_LOCK=1;
197
        localparam      // Derived parameters
198 3 dgisselq
                        AW=ADDRESS_WIDTH;
199
        input   i_clk, i_rst;
200
        // Wishbone master
201
        output  wire            o_wb_cyc, o_wb_stb, o_wb_we;
202
        output  wire    [(AW-1):0]       o_wb_addr;
203
        output  wire    [31:0]   o_wb_data;
204 50 dgisselq
        output  wire    [3:0]    o_wb_sel;
205 3 dgisselq
        input                   i_wb_ack, i_wb_stall;
206
        input           [31:0]   i_wb_data;
207
        input                   i_wb_err;
208
        // Incoming interrupts
209
        input           [(EXTERNAL_INTERRUPTS-1):0]      i_ext_int;
210
        // Outgoing interrupt
211
        output  wire            o_ext_int;
212
        // Wishbone slave
213
        input                   i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr;
214
        input           [31:0]   i_dbg_data;
215
        output  wire            o_dbg_ack;
216
        output  wire            o_dbg_stall;
217
        output  wire    [31:0]   o_dbg_data;
218
        //
219
`ifdef  DEBUG_SCOPE
220
        output  wire    [31:0]   o_cpu_debug;
221
`endif
222
 
223
        wire    [31:0]   ext_idata;
224
 
225
        // Handle our interrupt vector generation/coordination
226
        wire    [14:0]   main_int_vector, alt_int_vector;
227
        wire            ctri_int, tma_int, tmb_int, tmc_int, jif_int, dmac_int;
228
        wire            mtc_int, moc_int, mpc_int, mic_int,
229
                        utc_int, uoc_int, upc_int, uic_int;
230 50 dgisselq
 
231
        assign  main_int_vector[5:0] = { ctri_int, tma_int, tmb_int, tmc_int,
232
                                        jif_int, dmac_int };
233
 
234 3 dgisselq
        generate
235
        if (EXTERNAL_INTERRUPTS < 9)
236 50 dgisselq
                assign  main_int_vector[14:6] = { {(9-EXTERNAL_INTERRUPTS){1'b0}},
237
                                        i_ext_int };
238 3 dgisselq
        else
239 50 dgisselq
                assign  main_int_vector[14:6] = i_ext_int[8:0];
240 3 dgisselq
        endgenerate
241
        generate
242
        if (EXTERNAL_INTERRUPTS <= 9)
243
`ifdef  INCLUDE_ACCOUNTING_COUNTERS
244
                assign  alt_int_vector = { 7'h00,
245
                                        mtc_int, moc_int, mpc_int, mic_int,
246
                                        utc_int, uoc_int, upc_int, uic_int };
247
`else
248
                assign  alt_int_vector = { 15'h00 };
249
`endif
250
        else
251
`ifdef  INCLUDE_ACCOUNTING_COUNTERS
252
                assign  alt_int_vector = { {(7-(EXTERNAL_INTERRUPTS-9)){1'b0}},
253
                                        i_ext_int[(EXTERNAL_INTERRUPTS-1):9],
254
                                        mtc_int, moc_int, mpc_int, mic_int,
255
                                        utc_int, uoc_int, upc_int, uic_int };
256
`else
257
                assign  alt_int_vector = { {(15-(EXTERNAL_INTERRUPTS-9)){1'b0}},
258
                                        i_ext_int[(EXTERNAL_INTERRUPTS-1):9] };
259
`endif
260
        endgenerate
261
 
262 50 dgisselq
 
263 3 dgisselq
        // Delay the debug port by one clock, to meet timing requirements
264
        wire            dbg_cyc, dbg_stb, dbg_we, dbg_addr, dbg_stall;
265
        wire    [31:0]   dbg_idata, dbg_odata;
266
        reg             dbg_ack;
267
`ifdef  DELAY_DBG_BUS
268
        wire            dbg_err, no_dbg_err;
269 50 dgisselq
        wire    [3:0]    dbg_sel;
270 3 dgisselq
        assign          dbg_err = 1'b0;
271
        busdelay #(1,32) wbdelay(i_clk,
272 50 dgisselq
                i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr, i_dbg_data, 4'hf,
273 3 dgisselq
                        o_dbg_ack, o_dbg_stall, o_dbg_data, no_dbg_err,
274 50 dgisselq
                dbg_cyc, dbg_stb, dbg_we, dbg_addr, dbg_idata, dbg_sel,
275 3 dgisselq
                        dbg_ack, dbg_stall, dbg_odata, dbg_err);
276
`else
277
        assign  dbg_cyc     = i_dbg_cyc;
278
        assign  dbg_stb     = i_dbg_stb;
279
        assign  dbg_we      = i_dbg_we;
280
        assign  dbg_addr    = i_dbg_addr;
281
        assign  dbg_idata   = i_dbg_data;
282
        assign  o_dbg_ack   = dbg_ack;
283
        assign  o_dbg_stall = dbg_stall;
284
        assign  o_dbg_data  = dbg_odata;
285
`endif
286
 
287
        // 
288
        //
289
        //
290
        wire    sys_cyc, sys_stb, sys_we;
291
        wire    [4:0]    sys_addr;
292
        wire    [(AW-1):0]       cpu_addr;
293
        wire    [31:0]   sys_data;
294
        wire            sys_ack, sys_stall;
295
 
296
        //
297
        // The external debug interface
298
        //
299
        // We offer only a limited interface here, requiring a pre-register
300
        // write to set the local address.  This interface allows access to
301
        // the Zip System on a debug basis only, and not to the rest of the
302
        // wishbone bus.  Further, to access these registers, the control
303
        // register must first be accessed to both stop the CPU and to 
304
        // set the following address in question.  Hence all accesses require
305
        // two accesses: write the address to the control register (and halt
306
        // the CPU if not halted), then read/write the data from the data
307
        // register.
308
        //
309
        wire            cpu_break, dbg_cmd_write;
310
        reg             cmd_reset, cmd_halt, cmd_step, cmd_clear_pf_cache;
311
        reg     [5:0]    cmd_addr;
312
        wire    [3:0]    cpu_dbg_cc;
313
        assign  dbg_cmd_write = (dbg_cyc)&&(dbg_stb)&&(dbg_we)&&(~dbg_addr);
314
        //
315
        initial cmd_reset = 1'b1;
316
        always @(posedge i_clk)
317
                cmd_reset <= ((dbg_cmd_write)&&(dbg_idata[6]));
318
        //
319
        initial cmd_halt  = START_HALTED;
320
        always @(posedge i_clk)
321
                if (i_rst)
322
                        cmd_halt <= (START_HALTED == 1)? 1'b1 : 1'b0;
323
                else if (dbg_cmd_write)
324
                        cmd_halt <= ((dbg_idata[10])||(dbg_idata[8]));
325
                else if ((cmd_step)||(cpu_break))
326
                        cmd_halt  <= 1'b1;
327
 
328 43 dgisselq
        initial cmd_clear_pf_cache = 1'b1;
329 3 dgisselq
        always @(posedge i_clk)
330
                cmd_clear_pf_cache = (~i_rst)&&(dbg_cmd_write)
331
                                        &&((dbg_idata[11])||(dbg_idata[6]));
332
        //
333
        initial cmd_step  = 1'b0;
334
        always @(posedge i_clk)
335
                cmd_step <= (dbg_cmd_write)&&(dbg_idata[8]);
336
        //
337
        always @(posedge i_clk)
338
                if (dbg_cmd_write)
339
                        cmd_addr <= dbg_idata[5:0];
340
 
341
        wire    cpu_reset;
342
        assign  cpu_reset = (cmd_reset)||(wdt_reset)||(i_rst);
343
 
344
        wire    cpu_halt, cpu_dbg_stall;
345
        assign  cpu_halt = (i_rst)||((cmd_halt)&&(~cmd_step));
346
        wire    [31:0]   pic_data;
347
        wire    [31:0]   cmd_data;
348
        // Values:
349
        //      0x0003f -> cmd_addr mask
350
        //      0x00040 -> reset
351
        //      0x00080 -> PIC interrrupt pending
352
        //      0x00100 -> cmd_step
353
        //      0x00200 -> cmd_stall
354
        //      0x00400 -> cmd_halt
355
        //      0x00800 -> cmd_clear_pf_cache
356
        //      0x01000 -> cc.sleep
357
        //      0x02000 -> cc.gie
358
        //      0x04000 -> External (PIC) interrupt line is high
359
        //      Other external interrupts follow
360
        generate
361
        if (EXTERNAL_INTERRUPTS < 16)
362
                assign  cmd_data = { {(16-EXTERNAL_INTERRUPTS){1'b0}},
363
                                        i_ext_int,
364
                                cpu_dbg_cc,     // 4 bits
365
                                1'b0, cmd_halt, (~cpu_dbg_stall), 1'b0,
366
                                pic_data[15], cpu_reset, cmd_addr };
367
        else
368
                assign  cmd_data = { i_ext_int[15:0], cpu_dbg_cc,
369
                                1'b0, cmd_halt, (~cpu_dbg_stall), 1'b0,
370
                                pic_data[15], cpu_reset, cmd_addr };
371
        endgenerate
372
 
373
        wire    cpu_gie;
374
        assign  cpu_gie = cpu_dbg_cc[1];
375
 
376
        //
377
        // The WATCHDOG Timer
378
        //
379
        wire            wdt_ack, wdt_stall, wdt_reset;
380
        wire    [31:0]   wdt_data;
381
        ziptimer #(32,31,0)
382
                watchdog(i_clk, cpu_reset, ~cmd_halt,
383
                        sys_cyc, ((sys_stb)&&(sys_addr == `WATCHDOG)), sys_we,
384
                                sys_data,
385
                        wdt_ack, wdt_stall, wdt_data, wdt_reset);
386
 
387
        //
388
        // Position two, a second watchdog timer--this time for the wishbone
389
        // bus, in order to tell/find wishbone bus lockups.  In its current
390
        // configuration, it cannot be configured and all bus accesses must
391
        // take less than the number written to this register.
392
        //
393
        reg     wdbus_ack;
394
        reg     [(AW-1):0]       r_wdbus_data;
395
        wire    [31:0]           wdbus_data;
396
        wire    [14:0]   wdbus_ignored_data;
397
        wire    reset_wdbus_timer, wdbus_int;
398
        assign  reset_wdbus_timer = ((o_wb_cyc)&&((o_wb_stb)||(i_wb_ack)));
399
        wbwatchdog #(14) watchbus(i_clk,(cpu_reset)||(reset_wdbus_timer),
400
                        o_wb_cyc, 14'h2000, wdbus_int);
401
        initial r_wdbus_data = 0;
402
        always @(posedge i_clk)
403
                if ((wdbus_int)||(cpu_ext_err))
404
                        r_wdbus_data = o_wb_addr;
405
        assign  wdbus_data = { {(32-AW){1'b0}}, r_wdbus_data };
406
        initial wdbus_ack = 1'b0;
407
        always @(posedge i_clk)
408
                wdbus_ack <= ((sys_cyc)&&(sys_stb)&&(sys_addr == 5'h02));
409
 
410
        // Counters -- for performance measurement and accounting
411
        //
412
        // Here's the stuff we'll be counting ....
413
        //
414
        wire            cpu_op_stall, cpu_pf_stall, cpu_i_count;
415
 
416
`ifdef  INCLUDE_ACCOUNTING_COUNTERS
417
        //
418
        // The master counters will, in general, not be reset.  They'll be used
419
        // for an overall counter.
420
        //
421
        // Master task counter
422
        wire            mtc_ack, mtc_stall;
423
        wire    [31:0]   mtc_data;
424
        zipcounter      mtask_ctr(i_clk, (~cpu_halt), sys_cyc,
425
                                (sys_stb)&&(sys_addr == `MSTR_TASK_CTR),
426
                                        sys_we, sys_data,
427
                                mtc_ack, mtc_stall, mtc_data, mtc_int);
428
 
429
        // Master Operand Stall counter
430
        wire            moc_ack, moc_stall;
431
        wire    [31:0]   moc_data;
432
        zipcounter      mmstall_ctr(i_clk,(cpu_op_stall), sys_cyc,
433
                                (sys_stb)&&(sys_addr == `MSTR_MSTL_CTR),
434
                                        sys_we, sys_data,
435
                                moc_ack, moc_stall, moc_data, moc_int);
436
 
437
        // Master PreFetch-Stall counter
438
        wire            mpc_ack, mpc_stall;
439
        wire    [31:0]   mpc_data;
440
        zipcounter      mpstall_ctr(i_clk,(cpu_pf_stall), sys_cyc,
441
                                (sys_stb)&&(sys_addr == `MSTR_PSTL_CTR),
442
                                        sys_we, sys_data,
443
                                mpc_ack, mpc_stall, mpc_data, mpc_int);
444
 
445
        // Master Instruction counter
446
        wire            mic_ack, mic_stall;
447
        wire    [31:0]   mic_data;
448
        zipcounter      mins_ctr(i_clk,(cpu_i_count), sys_cyc,
449
                                (sys_stb)&&(sys_addr == `MSTR_INST_CTR),
450
                                        sys_we, sys_data,
451
                                mic_ack, mic_stall, mic_data, mic_int);
452
 
453
        //
454
        // The user counters are different from those of the master.  They will
455
        // be reset any time a task is given control of the CPU.
456
        //
457
        // User task counter
458
        wire            utc_ack, utc_stall;
459
        wire    [31:0]   utc_data;
460
        zipcounter      utask_ctr(i_clk,(~cpu_halt)&&(cpu_gie), sys_cyc,
461
                                (sys_stb)&&(sys_addr == `USER_TASK_CTR),
462
                                        sys_we, sys_data,
463
                                utc_ack, utc_stall, utc_data, utc_int);
464
 
465
        // User Op-Stall counter
466
        wire            uoc_ack, uoc_stall;
467
        wire    [31:0]   uoc_data;
468
        zipcounter      umstall_ctr(i_clk,(cpu_op_stall)&&(cpu_gie), sys_cyc,
469
                                (sys_stb)&&(sys_addr == `USER_MSTL_CTR),
470
                                        sys_we, sys_data,
471
                                uoc_ack, uoc_stall, uoc_data, uoc_int);
472
 
473
        // User PreFetch-Stall counter
474
        wire            upc_ack, upc_stall;
475
        wire    [31:0]   upc_data;
476
        zipcounter      upstall_ctr(i_clk,(cpu_pf_stall)&&(cpu_gie), sys_cyc,
477
                                (sys_stb)&&(sys_addr == `USER_PSTL_CTR),
478
                                        sys_we, sys_data,
479
                                upc_ack, upc_stall, upc_data, upc_int);
480
 
481
        // User instruction counter
482
        wire            uic_ack, uic_stall;
483
        wire    [31:0]   uic_data;
484
        zipcounter      uins_ctr(i_clk,(cpu_i_count)&&(cpu_gie), sys_cyc,
485
                                (sys_stb)&&(sys_addr == `USER_INST_CTR),
486
                                        sys_we, sys_data,
487
                                uic_ack, uic_stall, uic_data, uic_int);
488
 
489
        // A little bit of pre-cleanup (actr = accounting counters)
490
        wire            actr_ack, actr_stall;
491
        wire    [31:0]   actr_data;
492
        assign  actr_ack = ((mtc_ack | moc_ack | mpc_ack | mic_ack)
493
                                |(utc_ack | uoc_ack | upc_ack | uic_ack));
494
        assign  actr_stall = ((mtc_stall | moc_stall | mpc_stall | mic_stall)
495
                                |(utc_stall | uoc_stall | upc_stall|uic_stall));
496
        assign  actr_data = ((mtc_ack) ? mtc_data
497
                                : ((moc_ack) ? moc_data
498
                                : ((mpc_ack) ? mpc_data
499
                                : ((mic_ack) ? mic_data
500
                                : ((utc_ack) ? utc_data
501
                                : ((uoc_ack) ? uoc_data
502
                                : ((upc_ack) ? upc_data
503
                                : uic_data)))))));
504
`else //        INCLUDE_ACCOUNTING_COUNTERS
505
        reg             actr_ack;
506
        wire            actr_stall;
507
        wire    [31:0]   actr_data;
508
        assign  actr_stall = 1'b0;
509
        assign  actr_data = 32'h0000;
510
 
511
        assign  mtc_int = 1'b0;
512
        assign  moc_int = 1'b0;
513
        assign  mpc_int = 1'b0;
514
        assign  mic_int = 1'b0;
515
        assign  utc_int = 1'b0;
516
        assign  uoc_int = 1'b0;
517
        assign  upc_int = 1'b0;
518
        assign  uic_int = 1'b0;
519
 
520
        always @(posedge i_clk)
521
                actr_ack <= (sys_stb)&&(sys_addr[4:3] == 2'b01);
522
`endif  //      INCLUDE_ACCOUNTING_COUNTERS
523
 
524
        //
525
        // The DMA Controller
526
        //
527
        wire            dmac_stb, dc_err;
528
        wire    [31:0]   dmac_data;
529
        wire            dmac_ack, dmac_stall;
530
        wire            dc_cyc, dc_stb, dc_we, dc_ack, dc_stall;
531
        wire    [31:0]   dc_data;
532
        wire    [(AW-1):0]       dc_addr;
533
        wire            cpu_gbl_cyc;
534 43 dgisselq
        wire    [31:0]   dmac_int_vec;
535
        assign  dmac_int_vec = { 1'b0, alt_int_vector, 1'b0,
536
                                        main_int_vector[14:1], 1'b0 };
537 3 dgisselq
        assign  dmac_stb = (sys_stb)&&(sys_addr[4]);
538
`ifdef  INCLUDE_DMA_CONTROLLER
539
        wbdmac  #(AW) dma_controller(i_clk, cpu_reset,
540
                                sys_cyc, dmac_stb, sys_we,
541
                                        sys_addr[1:0], sys_data,
542
                                        dmac_ack, dmac_stall, dmac_data,
543
                                // Need the outgoing DMAC wishbone bus
544
                                dc_cyc, dc_stb, dc_we, dc_addr, dc_data,
545
                                        dc_ack, dc_stall, ext_idata, dc_err,
546
                                // External device interrupts
547 43 dgisselq
                                dmac_int_vec,
548 3 dgisselq
                                // DMAC interrupt, for upon completion
549
                                dmac_int);
550
`else
551
        reg     r_dmac_ack;
552
        always @(posedge i_clk)
553
                r_dmac_ack <= (sys_cyc)&&(dmac_stb);
554
        assign  dmac_ack = r_dmac_ack;
555
        assign  dmac_data = 32'h000;
556
        assign  dmac_stall = 1'b0;
557
 
558
        assign  dc_cyc  = 1'b0;
559
        assign  dc_stb  = 1'b0;
560
        assign  dc_we   = 1'b0;
561
        assign  dc_addr = { (AW) {1'b0} };
562
        assign  dc_data = 32'h00;
563
 
564
        assign  dmac_int = 1'b0;
565
`endif
566
 
567
        wire            ctri_sel, ctri_stall;
568
        reg             ctri_ack;
569
        wire    [31:0]   ctri_data;
570 32 dgisselq
        assign  ctri_sel = (sys_stb)&&(sys_addr == `CTRINT);
571 3 dgisselq
        always @(posedge i_clk)
572
                ctri_ack <= ctri_sel;
573
        assign  ctri_stall = 1'b0;
574
`ifdef  INCLUDE_ACCOUNTING_COUNTERS
575
        //
576
        // Counter Interrupt controller
577
        //
578
        generate
579
        if (EXTERNAL_INTERRUPTS <= 9)
580
        begin
581
                icontrol #(8)   ctri(i_clk, cpu_reset, (ctri_sel),
582
                                        sys_data, ctri_data, alt_int_vector[7:0],
583
                                        ctri_int);
584
        end else begin
585
                icontrol #(8+(EXTERNAL_INTERRUPTS-9))
586
                                ctri(i_clk, cpu_reset, (ctri_sel),
587
                                        sys_data, ctri_data,
588
                                        alt_int_vector[(EXTERNAL_INTERRUPTS-2):0],
589
                                        ctri_int);
590
        end endgenerate
591
 
592
`else   //      INCLUDE_ACCOUNTING_COUNTERS
593
 
594
        generate
595
        if (EXTERNAL_INTERRUPTS <= 9)
596
        begin
597
                assign  ctri_stall = 1'b0;
598
                assign  ctri_data  = 32'h0000;
599
                assign  ctri_int   = 1'b0;
600
        end else begin
601
                icontrol #(EXTERNAL_INTERRUPTS-9)
602
                                ctri(i_clk, cpu_reset, (ctri_sel),
603
                                        sys_data, ctri_data,
604
                                alt_int_vector[(EXTERNAL_INTERRUPTS-10):0],
605
                                        ctri_int);
606
        end endgenerate
607
`endif  //      INCLUDE_ACCOUNTING_COUNTERS
608
 
609
 
610
        //
611
        // Timer A
612
        //
613
        wire            tma_ack, tma_stall;
614
        wire    [31:0]   tma_data;
615
        ziptimer timer_a(i_clk, cpu_reset, ~cmd_halt,
616
                        sys_cyc, (sys_stb)&&(sys_addr == `TIMER_A), sys_we,
617
                                sys_data,
618
                        tma_ack, tma_stall, tma_data, tma_int);
619
 
620
        //
621
        // Timer B
622
        //
623
        wire            tmb_ack, tmb_stall;
624
        wire    [31:0]   tmb_data;
625
        ziptimer timer_b(i_clk, cpu_reset, ~cmd_halt,
626
                        sys_cyc, (sys_stb)&&(sys_addr == `TIMER_B), sys_we,
627
                                sys_data,
628
                        tmb_ack, tmb_stall, tmb_data, tmb_int);
629
 
630
        //
631
        // Timer C
632
        //
633
        wire            tmc_ack, tmc_stall;
634
        wire    [31:0]   tmc_data;
635
        ziptimer timer_c(i_clk, cpu_reset, ~cmd_halt,
636
                        sys_cyc, (sys_stb)&&(sys_addr == `TIMER_C), sys_we,
637
                                sys_data,
638
                        tmc_ack, tmc_stall, tmc_data, tmc_int);
639
 
640
        //
641
        // JIFFIES
642
        //
643
        wire            jif_ack, jif_stall;
644
        wire    [31:0]   jif_data;
645
        zipjiffies jiffies(i_clk, ~cmd_halt,
646
                        sys_cyc, (sys_stb)&&(sys_addr == `JIFFIES), sys_we,
647
                                sys_data,
648
                        jif_ack, jif_stall, jif_data, jif_int);
649
 
650
        //
651
        // The programmable interrupt controller peripheral
652
        //
653
        wire            pic_interrupt;
654
        generate
655
        if (EXTERNAL_INTERRUPTS < 9)
656
        begin
657
                icontrol #(6+EXTERNAL_INTERRUPTS)       pic(i_clk, cpu_reset,
658
                                        (sys_cyc)&&(sys_stb)&&(sys_we)
659
                                                &&(sys_addr==`INTCTRL),
660
                                        sys_data, pic_data,
661
                                        main_int_vector[(6+EXTERNAL_INTERRUPTS-1):0], pic_interrupt);
662
        end else begin
663
                icontrol #(15)  pic(i_clk, cpu_reset,
664
                                        (sys_cyc)&&(sys_stb)&&(sys_we)
665
                                                &&(sys_addr==`INTCTRL),
666
                                        sys_data, pic_data,
667
                                        main_int_vector[14:0], pic_interrupt);
668
        end endgenerate
669
 
670
        wire    pic_stall;
671
        assign  pic_stall = 1'b0;
672
        reg     pic_ack;
673
        always @(posedge i_clk)
674 32 dgisselq
                pic_ack <= (sys_stb)&&(sys_addr == `INTCTRL);
675 3 dgisselq
 
676
        //
677
        // The CPU itself
678
        //
679
        wire            cpu_gbl_stb, cpu_lcl_cyc, cpu_lcl_stb,
680
                        cpu_we, cpu_dbg_we;
681
        wire    [31:0]   cpu_data, wb_data;
682 50 dgisselq
        wire    [3:0]    cpu_sel;
683 3 dgisselq
        wire            cpu_ack, cpu_stall, cpu_err;
684
        wire    [31:0]   cpu_dbg_data;
685
        assign cpu_dbg_we = ((dbg_cyc)&&(dbg_stb)&&(~cmd_addr[5])
686
                                        &&(dbg_we)&&(dbg_addr));
687
        zipcpu  #(
688
                        .RESET_ADDRESS(RESET_ADDRESS),
689
                        .ADDRESS_WIDTH(ADDRESS_WIDTH),
690
                        .LGICACHE(LGICACHE),
691
                        .IMPLEMENT_MPY(IMPLEMENT_MPY),
692
                        .IMPLEMENT_DIVIDE(IMPLEMENT_DIVIDE),
693
                        .IMPLEMENT_FPU(IMPLEMENT_FPU),
694
                        .IMPLEMENT_LOCK(IMPLEMENT_LOCK)
695
                )
696
                thecpu(i_clk, cpu_reset, pic_interrupt,
697
                        cpu_halt, cmd_clear_pf_cache, cmd_addr[4:0], cpu_dbg_we,
698
                                dbg_idata, cpu_dbg_stall, cpu_dbg_data,
699
                                cpu_dbg_cc, cpu_break,
700
                        cpu_gbl_cyc, cpu_gbl_stb,
701
                                cpu_lcl_cyc, cpu_lcl_stb,
702 50 dgisselq
                                cpu_we, cpu_addr, cpu_data, cpu_sel,
703 3 dgisselq
                                cpu_ack, cpu_stall, wb_data,
704
                                cpu_err,
705
                        cpu_op_stall, cpu_pf_stall, cpu_i_count
706
`ifdef  DEBUG_SCOPE
707
                        , o_cpu_debug
708
`endif
709
                        );
710
 
711
        // Now, arbitrate the bus ... first for the local peripherals
712
        // For the debugger to have access to the local system bus, the
713
        // following must be true:
714
        //      (dbg_cyc)       The debugger must request the bus
715
        //      (~cpu_lcl_cyc)  The CPU cannot be using it (CPU gets priority)
716
        //      (dbg_addr)      The debugger must be requesting its data
717
        //                              register, not just the control register
718
        // and one of two other things.  Either
719
        //      ((cpu_halt)&&(~cpu_dbg_stall))  the CPU is completely halted,
720
        // or
721
        //      (~cmd_addr[5])          we are trying to read a CPU register
722
        //                      while in motion.  Let the user beware that,
723
        //                      by not waiting for the CPU to fully halt,
724
        //                      his results may not be what he expects.
725
        //
726
        wire    sys_dbg_cyc = ((dbg_cyc)&&(~cpu_lcl_cyc)&&(dbg_addr))
727
                                &&(((cpu_halt)&&(~cpu_dbg_stall))
728
                                        ||(~cmd_addr[5]));
729
        assign  sys_cyc = (cpu_lcl_cyc)||(sys_dbg_cyc);
730
        assign  sys_stb = (cpu_lcl_cyc)
731
                                ? (cpu_lcl_stb)
732
                                : ((dbg_stb)&&(dbg_addr)&&(cmd_addr[5]));
733
 
734
        assign  sys_we  = (cpu_lcl_cyc) ? cpu_we : dbg_we;
735
        assign  sys_addr= (cpu_lcl_cyc) ? cpu_addr[4:0] : cmd_addr[4:0];
736
        assign  sys_data= (cpu_lcl_cyc) ? cpu_data : dbg_idata;
737
 
738
        // Return debug response values
739
        assign  dbg_odata = (~dbg_addr)?cmd_data
740
                                :((~cmd_addr[5])?cpu_dbg_data : wb_data);
741
        initial dbg_ack = 1'b0;
742
        always @(posedge i_clk)
743
                dbg_ack <= (dbg_cyc)&&(dbg_stb)&&(~dbg_stall);
744
        assign  dbg_stall=(dbg_cyc)&&((~sys_dbg_cyc)||(sys_stall))&&(dbg_addr);
745
 
746
        // Now for the external wishbone bus
747
        //      Need to arbitrate between the flash cache and the CPU
748
        // The way this works, though, the CPU will stall once the flash 
749
        // cache gets access to the bus--the CPU will be stuck until the 
750
        // flash cache is finished with the bus.
751
        wire            ext_cyc, ext_stb, ext_we, ext_err;
752
        wire            cpu_ext_ack, cpu_ext_stall, ext_ack, ext_stall,
753
                                cpu_ext_err;
754
        wire    [(AW-1):0]       ext_addr;
755
        wire    [31:0]           ext_odata;
756 50 dgisselq
        wire    [3:0]            ext_sel;
757 3 dgisselq
        wbpriarbiter #(32,AW) dmacvcpu(i_clk,
758 50 dgisselq
                        cpu_gbl_cyc, cpu_gbl_stb, cpu_we, cpu_addr, cpu_data, cpu_sel,
759 3 dgisselq
                                cpu_ext_ack, cpu_ext_stall, cpu_ext_err,
760 50 dgisselq
                        dc_cyc, dc_stb, dc_we, dc_addr, dc_data, 4'hf,
761 3 dgisselq
                                        dc_ack, dc_stall, dc_err,
762 50 dgisselq
                        ext_cyc, ext_stb, ext_we, ext_addr, ext_odata, ext_sel,
763 3 dgisselq
                                ext_ack, ext_stall, ext_err);
764
 
765
`ifdef  DELAY_EXT_BUS
766
        busdelay #(AW,32) extbus(i_clk,
767
                        ext_cyc, ext_stb, ext_we, ext_addr, ext_odata,
768
                                ext_ack, ext_stall, ext_idata, ext_err,
769 50 dgisselq
                        o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data, o_wb_sel,
770 3 dgisselq
                                i_wb_ack, i_wb_stall, i_wb_data, (i_wb_err)||(wdbus_int));
771
`else
772 50 dgisselq
        assign  o_wb_cyc  = ext_cyc;
773
        assign  o_wb_stb  = ext_stb;
774
        assign  o_wb_we   = ext_we;
775
        assign  o_wb_addr = ext_addr;
776
        assign  o_wb_data = ext_odata;
777
        assign  o_wb_sel  = ext_sel;
778
        assign  ext_ack   = i_wb_ack;
779
        assign  ext_stall = i_wb_stall;
780
        assign  ext_idata = i_wb_data;
781
        assign  ext_err   = (i_wb_err)||(wdbus_int);
782 3 dgisselq
`endif
783
 
784
        wire            tmr_ack;
785
        assign  tmr_ack = (tma_ack|tmb_ack|tmc_ack|jif_ack);
786
        wire    [31:0]   tmr_data;
787
        assign  tmr_data = (tma_ack)?tma_data
788
                                :(tmb_ack ? tmb_data
789
                                :(tmc_ack ? tmc_data
790
                                :jif_data));
791
        assign  wb_data = (tmr_ack|wdt_ack)?((tmr_ack)?tmr_data:wdt_data)
792
                        :((actr_ack|dmac_ack)?((actr_ack)?actr_data:dmac_data)
793
                        :((pic_ack|ctri_ack)?((pic_ack)?pic_data:ctri_data)
794
                        :((wdbus_ack)?wdbus_data:(ext_idata))));
795
 
796
        assign  sys_stall = (tma_stall | tmb_stall | tmc_stall | jif_stall
797
                                | wdt_stall | ctri_stall | actr_stall
798 32 dgisselq
                                | pic_stall | dmac_stall); // Always 1'b0!
799 3 dgisselq
        assign  cpu_stall = (sys_stall)|(cpu_ext_stall);
800
        assign  sys_ack = (tmr_ack|wdt_ack|ctri_ack|actr_ack|pic_ack|dmac_ack|wdbus_ack);
801
        assign  cpu_ack = (sys_ack)||(cpu_ext_ack);
802
        assign  cpu_err = (cpu_ext_err)&&(cpu_gbl_cyc);
803
 
804
        assign  o_ext_int = (cmd_halt) && (~cpu_stall);
805
 
806
endmodule

powered by: WebSVN 2.1.0

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