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

Subversion Repositories zipcpu

[/] [zipcpu/] [trunk/] [rtl/] [zipsystem.v] - Blame information for rev 66

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

Line No. Rev Author Line
1 2 dgisselq
///////////////////////////////////////////////////////////////////////////
2
//
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 Tecnology, LLC
64
//
65
///////////////////////////////////////////////////////////////////////////
66
//
67
// Copyright (C) 2015, Gisselquist Technology, LLC
68
//
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
// License:     GPL, v3, as defined and found on www.gnu.org,
80
//              http://www.gnu.org/licenses/gpl.html
81
//
82
//
83
///////////////////////////////////////////////////////////////////////////
84
//
85 66 dgisselq
`include "cpudefs.v"
86
//
87 36 dgisselq
// While I hate adding delays to any bus access, this next delay is required
88 3 dgisselq
// to make timing close in my Basys-3 design.
89
`define DELAY_DBG_BUS
90 36 dgisselq
// On my previous version, I needed to add a delay to access the external
91
// bus.  Activate the define below and that delay will be put back into place.
92
// This particular version no longer needs the delay in order to run at 
93
// 100 MHz.  Timing indicates I may even run this at 250 MHz without the
94
// delay too, so we're doing better.  To get rid of this, I placed the logic
95
// determining whether or not I was accessing the local system bus one clock
96
// earlier, or into the memops.v file.  This also required my wishbone bus
97
// arbiter to maintain the bus selection as well, so that got updated ...
98
// you get the picture.  But, the bottom line is that I no longer need this
99
// delay.
100 3 dgisselq
//
101 56 dgisselq
// `define      DELAY_EXT_BUS   // Required no longer!
102 3 dgisselq
//
103 36 dgisselq
//
104
// If space is tight, you might not wish to have your performance and
105
// accounting counters, so let's make those optional here
106
//      Without this flag, Slice LUT count is 3315 (ZipSystem),2432 (ZipCPU)
107
//      When including counters, 
108
//              Slice LUTs      ZipSystem       ZipCPU
109
//      With Counters           3315            2432
110
//      Without Counters        2796            2046
111
`define INCLUDE_ACCOUNTING_COUNTERS
112
 
113
//
114 3 dgisselq
// Now, where am I placing all of my peripherals?
115 2 dgisselq
`define PERIPHBASE      32'hc0000000
116 36 dgisselq
`define INTCTRL         5'h0    // 
117
`define WATCHDOG        5'h1    // Interrupt generates reset signal
118 56 dgisselq
`define BUSWATCHDOG     5'h2    // Sets IVEC[0]
119 36 dgisselq
`define CTRINT          5'h3    // Sets IVEC[5]
120
`define TIMER_A         5'h4    // Sets IVEC[4]
121
`define TIMER_B         5'h5    // Sets IVEC[3]
122
`define TIMER_C         5'h6    // Sets IVEC[2]
123
`define JIFFIES         5'h7    // Sets IVEC[1]
124 2 dgisselq
 
125
 
126 36 dgisselq
`ifdef  INCLUDE_ACCOUNTING_COUNTERS
127
`define MSTR_TASK_CTR   5'h08
128
`define MSTR_MSTL_CTR   5'h09
129
`define MSTR_PSTL_CTR   5'h0a
130
`define MSTR_INST_CTR   5'h0b
131
`define USER_TASK_CTR   5'h0c
132
`define USER_MSTL_CTR   5'h0d
133
`define USER_PSTL_CTR   5'h0e
134
`define USER_INST_CTR   5'h0f
135
`endif
136
 
137
// Although I have a hole at 5'h2, the DMA controller requires four wishbone
138
// addresses, therefore we place it by itself and expand our address bus
139
// width here by another bit.
140
`define DMAC            5'h10
141
 
142 2 dgisselq
// `define      RTC_CLOCK       32'hc0000008    // A global something
143
// `define      BITREV          32'hc0000003
144
//
145
//      DBGCTRL
146
//              10 HALT
147
//               9 HALT(ED)
148
//               8 STEP (W=1 steps, and returns to halted)
149
//               7 INTERRUPT-FLAG
150
//               6 RESET_FLAG
151
//              ADDRESS:
152
//               5      PERIPHERAL-BIT
153
//              [4:0]   REGISTER-ADDR
154
//      DBGDATA
155
//              read/writes internal registers
156 66 dgisselq
//
157
//
158
//
159 2 dgisselq
module  zipsystem(i_clk, i_rst,
160
                // Wishbone master interface from the CPU
161
                o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data,
162 36 dgisselq
                        i_wb_ack, i_wb_stall, i_wb_data, i_wb_err,
163 2 dgisselq
                // Incoming interrupts
164
                i_ext_int,
165 18 dgisselq
                // Our one outgoing interrupt
166
                o_ext_int,
167 2 dgisselq
                // Wishbone slave interface for debugging purposes
168
                i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr, i_dbg_data,
169 66 dgisselq
                        o_dbg_ack, o_dbg_stall, o_dbg_data
170
`ifdef  DEBUG_SCOPE
171
                , o_cpu_debug
172
`endif
173
                );
174 48 dgisselq
        parameter       RESET_ADDRESS=24'h0100000, ADDRESS_WIDTH=24,
175 66 dgisselq
                        LGICACHE=12, START_HALTED=1, EXTERNAL_INTERRUPTS=1,
176 48 dgisselq
                        // Derived parameters
177
                        AW=ADDRESS_WIDTH;
178 2 dgisselq
        input   i_clk, i_rst;
179
        // Wishbone master
180
        output  wire            o_wb_cyc, o_wb_stb, o_wb_we;
181 48 dgisselq
        output  wire    [(AW-1):0]       o_wb_addr;
182 2 dgisselq
        output  wire    [31:0]   o_wb_data;
183
        input                   i_wb_ack, i_wb_stall;
184
        input           [31:0]   i_wb_data;
185 36 dgisselq
        input                   i_wb_err;
186 2 dgisselq
        // Incoming interrupts
187 34 dgisselq
        input           [(EXTERNAL_INTERRUPTS-1):0]      i_ext_int;
188 18 dgisselq
        // Outgoing interrupt
189
        output  wire            o_ext_int;
190 2 dgisselq
        // Wishbone slave
191
        input                   i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr;
192
        input           [31:0]   i_dbg_data;
193
        output  wire            o_dbg_ack;
194
        output  wire            o_dbg_stall;
195
        output  wire    [31:0]   o_dbg_data;
196 56 dgisselq
        //
197 66 dgisselq
`ifdef  DEBUG_SCOPE
198 56 dgisselq
        output  wire    [31:0]   o_cpu_debug;
199 66 dgisselq
`endif
200 2 dgisselq
 
201
        wire    [31:0]   ext_idata;
202
 
203
        // Delay the debug port by one clock, to meet timing requirements
204
        wire            dbg_cyc, dbg_stb, dbg_we, dbg_addr, dbg_stall;
205
        wire    [31:0]   dbg_idata, dbg_odata;
206
        reg             dbg_ack;
207 3 dgisselq
`ifdef  DELAY_DBG_BUS
208 36 dgisselq
        wire            dbg_err, no_dbg_err;
209
        assign          dbg_err = 1'b0;
210 2 dgisselq
        busdelay #(1,32) wbdelay(i_clk,
211
                i_dbg_cyc, i_dbg_stb, i_dbg_we, i_dbg_addr, i_dbg_data,
212 36 dgisselq
                        o_dbg_ack, o_dbg_stall, o_dbg_data, no_dbg_err,
213 2 dgisselq
                dbg_cyc, dbg_stb, dbg_we, dbg_addr, dbg_idata,
214 36 dgisselq
                        dbg_ack, dbg_stall, dbg_odata, dbg_err);
215 3 dgisselq
`else
216
        assign  dbg_cyc     = i_dbg_cyc;
217
        assign  dbg_stb     = i_dbg_stb;
218
        assign  dbg_we      = i_dbg_we;
219
        assign  dbg_addr    = i_dbg_addr;
220
        assign  dbg_idata   = i_dbg_data;
221
        assign  o_dbg_ack   = dbg_ack;
222
        assign  o_dbg_stall = dbg_stall;
223
        assign  o_dbg_data  = dbg_odata;
224
`endif
225 2 dgisselq
 
226
        // 
227
        //
228
        //
229
        wire    sys_cyc, sys_stb, sys_we;
230 36 dgisselq
        wire    [4:0]    sys_addr;
231 48 dgisselq
        wire    [(AW-1):0]       cpu_addr;
232 2 dgisselq
        wire    [31:0]   sys_data;
233 36 dgisselq
        wire            sys_ack, sys_stall;
234 2 dgisselq
 
235
        //
236
        // The external debug interface
237
        //
238
        // We offer only a limited interface here, requiring a pre-register
239
        // write to set the local address.  This interface allows access to
240
        // the Zip System on a debug basis only, and not to the rest of the
241
        // wishbone bus.  Further, to access these registers, the control
242
        // register must first be accessed to both stop the CPU and to 
243
        // set the following address in question.  Hence all accesses require
244
        // two accesses: write the address to the control register (and halt
245
        // the CPU if not halted), then read/write the data from the data
246
        // register.
247
        //
248 9 dgisselq
        wire            cpu_break, dbg_cmd_write;
249 18 dgisselq
        reg             cmd_reset, cmd_halt, cmd_step, cmd_clear_pf_cache;
250 2 dgisselq
        reg     [5:0]    cmd_addr;
251 56 dgisselq
        wire    [3:0]    cpu_dbg_cc;
252 9 dgisselq
        assign  dbg_cmd_write = (dbg_cyc)&&(dbg_stb)&&(dbg_we)&&(~dbg_addr);
253
        //
254 2 dgisselq
        initial cmd_reset = 1'b1;
255 9 dgisselq
        always @(posedge i_clk)
256
                cmd_reset <= ((dbg_cmd_write)&&(dbg_idata[6]));
257
        //
258 2 dgisselq
        initial cmd_halt  = 1'b1;
259
        always @(posedge i_clk)
260
                if (i_rst)
261 34 dgisselq
                        cmd_halt <= (START_HALTED == 1)? 1'b1 : 1'b0;
262 9 dgisselq
                else if (dbg_cmd_write)
263 36 dgisselq
                        cmd_halt <= ((dbg_idata[10])||(dbg_idata[8]));
264 9 dgisselq
                else if ((cmd_step)||(cpu_break))
265
                        cmd_halt  <= 1'b1;
266 18 dgisselq
 
267
        always @(posedge i_clk)
268 56 dgisselq
                cmd_clear_pf_cache = (~i_rst)&&(dbg_cmd_write)
269
                                        &&((dbg_idata[11])||(dbg_idata[6]));
270 9 dgisselq
        //
271
        initial cmd_step  = 1'b0;
272
        always @(posedge i_clk)
273
                cmd_step <= (dbg_cmd_write)&&(dbg_idata[8]);
274
        //
275
        always @(posedge i_clk)
276
                if (dbg_cmd_write)
277 2 dgisselq
                        cmd_addr <= dbg_idata[5:0];
278 9 dgisselq
 
279 2 dgisselq
        wire    cpu_reset;
280 36 dgisselq
        assign  cpu_reset = (cmd_reset)||(wdt_reset)||(i_rst);
281 2 dgisselq
 
282
        wire    cpu_halt, cpu_dbg_stall;
283 34 dgisselq
        assign  cpu_halt = (i_rst)||((cmd_halt)&&(~cmd_step));
284 2 dgisselq
        wire    [31:0]   pic_data;
285
        wire    [31:0]   cmd_data;
286 18 dgisselq
        // Values:
287
        //      0x0003f -> cmd_addr mask
288
        //      0x00040 -> reset
289 34 dgisselq
        //      0x00080 -> PIC interrrupts enabled
290 18 dgisselq
        //      0x00100 -> cmd_step
291
        //      0x00200 -> cmd_stall
292
        //      0x00400 -> cmd_halt
293
        //      0x00800 -> cmd_clear_pf_cache
294
        //      0x01000 -> cc.sleep
295
        //      0x02000 -> cc.gie
296
        //      0x10000 -> External interrupt line is high
297 34 dgisselq
        assign  cmd_data = { 7'h00, {(9-EXTERNAL_INTERRUPTS){1'b0}}, i_ext_int,
298 56 dgisselq
                        cpu_dbg_cc,
299 18 dgisselq
                        1'b0, cmd_halt, (~cpu_dbg_stall), 1'b0,
300
                        pic_data[15], cpu_reset, cmd_addr };
301 38 dgisselq
        wire    cpu_gie;
302
        assign  cpu_gie = cpu_dbg_cc[1];
303 2 dgisselq
 
304
`ifdef  USE_TRAP
305
        //
306
        // The TRAP peripheral
307
        //
308
        wire            trap_ack, trap_stall, trap_int;
309
        wire    [31:0]   trap_data;
310
        ziptrap trapp(i_clk,
311
                        sys_cyc, (sys_stb)&&(sys_addr == `TRAP_ADDR), sys_we,
312
                                sys_data,
313
                                trap_ack, trap_stall, trap_data, trap_int);
314
`endif
315
 
316
        //
317
        // The WATCHDOG Timer
318
        //
319
        wire            wdt_ack, wdt_stall, wdt_reset;
320
        wire    [31:0]   wdt_data;
321
        ziptimer watchdog(i_clk, cpu_reset, ~cmd_halt,
322
                        sys_cyc, ((sys_stb)&&(sys_addr == `WATCHDOG)), sys_we,
323
                                sys_data,
324
                        wdt_ack, wdt_stall, wdt_data, wdt_reset);
325
 
326
        //
327 56 dgisselq
        // Position two, a second watchdog timer--this time for the wishbone
328
        // bus, in order to tell/find wishbone bus lockups.  In its current
329
        // configuration, it cannot be configured and all bus accesses must
330
        // take less than the number written to this register.
331 2 dgisselq
        //
332 56 dgisselq
        reg     wdbus_ack;
333
        reg     [(AW-1):0]       r_wdbus_data;
334
        wire    [31:0]           wdbus_data;
335
        wire    [14:0]   wdbus_ignored_data;
336
        wire    reset_wdbus_timer, wdbus_int, wdbus_ack_ignored, wdbus_stall;
337
        assign  reset_wdbus_timer = ((o_wb_cyc)&&((o_wb_stb)||(i_wb_ack)));
338
        //      o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data,
339
        //              i_wb_ack, i_wb_stall, i_wb_data, i_wb_err,
340
        ziptimer #(15) watchbus(i_clk, (cpu_reset), o_wb_cyc,
341
                        reset_wdbus_timer, reset_wdbus_timer, 1'b1, 15'h2000,
342
                        wdbus_ack_ignored, wdbus_stall, wdbus_ignored_data,
343
                        wdbus_int);
344
        initial r_wdbus_data = 0;
345 36 dgisselq
        always @(posedge i_clk)
346 56 dgisselq
                if (wdbus_int)
347
                        r_wdbus_data = o_wb_addr;
348
        assign  wdbus_data = { {(32-AW){1'b0}}, r_wdbus_data };
349
        initial wdbus_ack = 1'b0;
350
        always @(posedge i_clk)
351
                wdbus_ack <= ((sys_cyc)&&(sys_stb)&&(sys_addr == 5'h02));
352
 
353 2 dgisselq
        // Counters -- for performance measurement and accounting
354
        //
355
        // Here's the stuff we'll be counting ....
356
        //
357 9 dgisselq
        wire            cpu_op_stall, cpu_pf_stall, cpu_i_count;
358 2 dgisselq
 
359 36 dgisselq
`ifdef  INCLUDE_ACCOUNTING_COUNTERS
360 2 dgisselq
        //
361
        // The master counters will, in general, not be reset.  They'll be used
362
        // for an overall counter.
363
        //
364
        // Master task counter
365
        wire            mtc_ack, mtc_stall, mtc_int;
366
        wire    [31:0]   mtc_data;
367 36 dgisselq
        zipcounter      mtask_ctr(i_clk, (~cpu_halt), sys_cyc,
368 2 dgisselq
                                (sys_stb)&&(sys_addr == `MSTR_TASK_CTR),
369
                                        sys_we, sys_data,
370
                                mtc_ack, mtc_stall, mtc_data, mtc_int);
371
 
372 9 dgisselq
        // Master Operand Stall counter
373
        wire            moc_ack, moc_stall, moc_int;
374
        wire    [31:0]   moc_data;
375
        zipcounter      mmstall_ctr(i_clk,(cpu_op_stall), sys_cyc,
376 2 dgisselq
                                (sys_stb)&&(sys_addr == `MSTR_MSTL_CTR),
377
                                        sys_we, sys_data,
378 9 dgisselq
                                moc_ack, moc_stall, moc_data, moc_int);
379 2 dgisselq
 
380
        // Master PreFetch-Stall counter
381
        wire            mpc_ack, mpc_stall, mpc_int;
382
        wire    [31:0]   mpc_data;
383 9 dgisselq
        zipcounter      mpstall_ctr(i_clk,(cpu_pf_stall), sys_cyc,
384 2 dgisselq
                                (sys_stb)&&(sys_addr == `MSTR_PSTL_CTR),
385
                                        sys_we, sys_data,
386
                                mpc_ack, mpc_stall, mpc_data, mpc_int);
387
 
388 9 dgisselq
        // Master Instruction counter
389
        wire            mic_ack, mic_stall, mic_int;
390
        wire    [31:0]   mic_data;
391
        zipcounter      mins_ctr(i_clk,(cpu_i_count), sys_cyc,
392 25 dgisselq
                                (sys_stb)&&(sys_addr == `MSTR_INST_CTR),
393 2 dgisselq
                                        sys_we, sys_data,
394 9 dgisselq
                                mic_ack, mic_stall, mic_data, mic_int);
395 2 dgisselq
 
396
        //
397
        // The user counters are different from those of the master.  They will
398
        // be reset any time a task is given control of the CPU.
399
        //
400
        // User task counter
401
        wire            utc_ack, utc_stall, utc_int;
402
        wire    [31:0]   utc_data;
403 38 dgisselq
        zipcounter      utask_ctr(i_clk,(~cpu_halt)&&(cpu_gie), sys_cyc,
404 2 dgisselq
                                (sys_stb)&&(sys_addr == `USER_TASK_CTR),
405
                                        sys_we, sys_data,
406
                                utc_ack, utc_stall, utc_data, utc_int);
407
 
408 9 dgisselq
        // User Op-Stall counter
409
        wire            uoc_ack, uoc_stall, uoc_int;
410
        wire    [31:0]   uoc_data;
411 38 dgisselq
        zipcounter      umstall_ctr(i_clk,(cpu_op_stall)&&(cpu_gie), sys_cyc,
412 2 dgisselq
                                (sys_stb)&&(sys_addr == `USER_MSTL_CTR),
413
                                        sys_we, sys_data,
414 9 dgisselq
                                uoc_ack, uoc_stall, uoc_data, uoc_int);
415 2 dgisselq
 
416
        // User PreFetch-Stall counter
417
        wire            upc_ack, upc_stall, upc_int;
418
        wire    [31:0]   upc_data;
419 38 dgisselq
        zipcounter      upstall_ctr(i_clk,(cpu_pf_stall)&&(cpu_gie), sys_cyc,
420 2 dgisselq
                                (sys_stb)&&(sys_addr == `USER_PSTL_CTR),
421
                                        sys_we, sys_data,
422
                                upc_ack, upc_stall, upc_data, upc_int);
423
 
424 9 dgisselq
        // User instruction counter
425
        wire            uic_ack, uic_stall, uic_int;
426
        wire    [31:0]   uic_data;
427 38 dgisselq
        zipcounter      uins_ctr(i_clk,(cpu_i_count)&&(cpu_gie), sys_cyc,
428 25 dgisselq
                                (sys_stb)&&(sys_addr == `USER_INST_CTR),
429 2 dgisselq
                                        sys_we, sys_data,
430 9 dgisselq
                                uic_ack, uic_stall, uic_data, uic_int);
431 2 dgisselq
 
432
        // A little bit of pre-cleanup (actr = accounting counters)
433
        wire            actr_ack, actr_stall;
434
        wire    [31:0]   actr_data;
435 9 dgisselq
        assign  actr_ack = ((mtc_ack | moc_ack | mpc_ack | mic_ack)
436
                                |(utc_ack | uoc_ack | upc_ack | uic_ack));
437
        assign  actr_stall = ((mtc_stall | moc_stall | mpc_stall | mic_stall)
438
                                |(utc_stall | uoc_stall | upc_stall|uic_stall));
439 2 dgisselq
        assign  actr_data = ((mtc_ack) ? mtc_data
440 9 dgisselq
                                : ((moc_ack) ? moc_data
441 2 dgisselq
                                : ((mpc_ack) ? mpc_data
442 9 dgisselq
                                : ((mic_ack) ? mic_data
443 2 dgisselq
                                : ((utc_ack) ? utc_data
444 9 dgisselq
                                : ((uoc_ack) ? uoc_data
445 2 dgisselq
                                : ((upc_ack) ? upc_data
446 9 dgisselq
                                : uic_data)))))));
447 36 dgisselq
`else //        INCLUDE_ACCOUNTING_COUNTERS
448
        reg             actr_ack;
449
        wire            actr_stall;
450
        wire    [31:0]   actr_data;
451
        assign  actr_stall = 1'b0;
452
        assign  actr_data = 32'h0000;
453 2 dgisselq
 
454 36 dgisselq
        wire    utc_int, uoc_int, upc_int, uic_int;
455
        wire    mtc_int, moc_int, mpc_int, mic_int;
456
        assign  mtc_int = 1'b0;
457
        assign  moc_int = 1'b0;
458
        assign  mpc_int = 1'b0;
459
        assign  mic_int = 1'b0;
460
        assign  utc_int = 1'b0;
461
        assign  uoc_int = 1'b0;
462
        assign  upc_int = 1'b0;
463
        assign  uic_int = 1'b0;
464
 
465
        always @(posedge i_clk)
466
                actr_ack <= (sys_stb)&&(sys_addr[4:3] == 2'b01);
467
`endif  //      INCLUDE_ACCOUNTING_COUNTERS
468
 
469
        //
470
        // The DMA Controller
471
        //
472
        wire            dmac_int, dmac_stb, dc_err;
473
        wire    [31:0]   dmac_data;
474
        wire            dmac_ack, dmac_stall;
475
        wire            dc_cyc, dc_stb, dc_we, dc_ack, dc_stall;
476 48 dgisselq
        wire    [31:0]   dc_data;
477
        wire    [(AW-1):0]       dc_addr;
478 36 dgisselq
        wire            cpu_gbl_cyc;
479
        assign  dmac_stb = (sys_stb)&&(sys_addr[4]);
480 66 dgisselq
`define INCLUDE_DMA_CONTROLLER
481 56 dgisselq
`ifdef  INCLUDE_DMA_CONTROLLER
482 48 dgisselq
        wbdmac  #(AW) dma_controller(i_clk,
483 36 dgisselq
                                sys_cyc, dmac_stb, sys_we,
484
                                        sys_addr[1:0], sys_data,
485
                                        dmac_ack, dmac_stall, dmac_data,
486
                                // Need the outgoing DMAC wishbone bus
487
                                dc_cyc, dc_stb, dc_we, dc_addr, dc_data,
488
                                        dc_ack, dc_stall, ext_idata, dc_err,
489
                                // External device interrupts
490
                                { {(32-EXTERNAL_INTERRUPTS){1'b0}}, i_ext_int },
491
                                // DMAC interrupt, for upon completion
492
                                dmac_int,
493
                                // Whether or not the CPU wants the bus
494
                                cpu_gbl_cyc);
495 56 dgisselq
`else
496
        reg     r_dmac_ack;
497
        always @(posedge i_clk)
498
                r_dmac_ack <= (sys_cyc)&&(dmac_stb);
499
        assign  dmac_ack = r_dmac_ack;
500
        assign  dmac_data = 32'h000;
501
        assign  dmac_stall = 1'b0;
502 2 dgisselq
 
503 56 dgisselq
        assign  dc_cyc  = 1'b0;
504
        assign  dc_stb  = 1'b0;
505
        assign  dc_we   = 1'b0;
506
        assign  dc_addr = { (AW) {1'b0} };
507
        assign  dc_data = 32'h00;
508
 
509
        assign  dmac_int = 1'b0;
510
`endif
511
 
512
 
513 36 dgisselq
`ifdef  INCLUDE_ACCOUNTING_COUNTERS
514 2 dgisselq
        //
515
        // Counter Interrupt controller
516
        //
517
        reg             ctri_ack;
518
        wire            ctri_stall, ctri_int, ctri_sel;
519
        wire    [7:0]    ctri_vector;
520
        wire    [31:0]   ctri_data;
521
        assign  ctri_sel = (sys_cyc)&&(sys_stb)&&(sys_addr == `CTRINT);
522 9 dgisselq
        assign  ctri_vector = { mtc_int, moc_int, mpc_int, mic_int,
523
                                        utc_int, uoc_int, upc_int, uic_int };
524 2 dgisselq
        icontrol #(8)   ctri(i_clk, cpu_reset, (ctri_sel)&&(sys_addr==`CTRINT),
525
                                sys_data, ctri_data, ctri_vector, ctri_int);
526
        always @(posedge i_clk)
527
                ctri_ack <= ctri_sel;
528 36 dgisselq
        assign  ctri_stall = 1'b0;
529
`else   //      INCLUDE_ACCOUNTING_COUNTERS
530
        reg     ctri_ack;
531
        wire    ctri_stall, ctri_int;
532
        wire    [31:0]   ctri_data;
533
        assign  ctri_stall = 1'b0;
534
        assign  ctri_data  = 32'h0000;
535
        assign  ctri_int   = 1'b0;
536 2 dgisselq
 
537 36 dgisselq
        always @(posedge i_clk)
538
                ctri_ack <= (sys_cyc)&&(sys_stb)&&(sys_addr == `CTRINT);
539
`endif  //      INCLUDE_ACCOUNTING_COUNTERS
540 2 dgisselq
 
541 36 dgisselq
 
542 2 dgisselq
        //
543
        // Timer A
544
        //
545
        wire            tma_ack, tma_stall, tma_int;
546
        wire    [31:0]   tma_data;
547
        ziptimer timer_a(i_clk, cpu_reset, ~cmd_halt,
548
                        sys_cyc, (sys_stb)&&(sys_addr == `TIMER_A), sys_we,
549
                                sys_data,
550
                        tma_ack, tma_stall, tma_data, tma_int);
551
 
552
        //
553
        // Timer B
554
        //
555
        wire            tmb_ack, tmb_stall, tmb_int;
556
        wire    [31:0]   tmb_data;
557
        ziptimer timer_b(i_clk, cpu_reset, ~cmd_halt,
558
                        sys_cyc, (sys_stb)&&(sys_addr == `TIMER_B), sys_we,
559
                                sys_data,
560
                        tmb_ack, tmb_stall, tmb_data, tmb_int);
561
 
562
        //
563
        // Timer C
564
        //
565
        wire            tmc_ack, tmc_stall, tmc_int;
566
        wire    [31:0]   tmc_data;
567
        ziptimer timer_c(i_clk, cpu_reset, ~cmd_halt,
568
                        sys_cyc, (sys_stb)&&(sys_addr == `TIMER_C), sys_we,
569
                                sys_data,
570
                        tmc_ack, tmc_stall, tmc_data, tmc_int);
571
 
572
        //
573
        // JIFFIES
574
        //
575
        wire            jif_ack, jif_stall, jif_int;
576
        wire    [31:0]   jif_data;
577
        zipjiffies jiffies(i_clk, ~cmd_halt,
578
                        sys_cyc, (sys_stb)&&(sys_addr == `JIFFIES), sys_we,
579
                                sys_data,
580
                        jif_ack, jif_stall, jif_data, jif_int);
581
 
582
        //
583
        // The programmable interrupt controller peripheral
584
        //
585
        wire            pic_interrupt;
586 34 dgisselq
        wire    [(5+EXTERNAL_INTERRUPTS):0]      int_vector;
587 2 dgisselq
        assign  int_vector = { i_ext_int, ctri_int, tma_int, tmb_int, tmc_int,
588 36 dgisselq
                                        jif_int, dmac_int };
589 34 dgisselq
        icontrol #(6+EXTERNAL_INTERRUPTS)       pic(i_clk, cpu_reset,
590 2 dgisselq
                                (sys_cyc)&&(sys_stb)&&(sys_we)
591
                                        &&(sys_addr==`INTCTRL),
592
                                sys_data, pic_data,
593
                                int_vector, pic_interrupt);
594 36 dgisselq
        wire    pic_stall;
595
        assign  pic_stall = 1'b0;
596 2 dgisselq
        reg     pic_ack;
597
        always @(posedge i_clk)
598
                pic_ack <= (sys_cyc)&&(sys_stb)&&(sys_addr == `INTCTRL);
599
 
600
        //
601
        // The CPU itself
602
        //
603 36 dgisselq
        wire            cpu_gbl_stb, cpu_lcl_cyc, cpu_lcl_stb,
604
                        cpu_we, cpu_dbg_we;
605 2 dgisselq
        wire    [31:0]   cpu_data, wb_data;
606 36 dgisselq
        wire            cpu_ack, cpu_stall, cpu_err;
607 2 dgisselq
        wire    [31:0]   cpu_dbg_data;
608
        assign cpu_dbg_we = ((dbg_cyc)&&(dbg_stb)&&(~cmd_addr[5])
609
                                        &&(dbg_we)&&(dbg_addr));
610 48 dgisselq
        zipcpu  #(RESET_ADDRESS,ADDRESS_WIDTH,LGICACHE)
611
                thecpu(i_clk, cpu_reset, pic_interrupt,
612 18 dgisselq
                        cpu_halt, cmd_clear_pf_cache, cmd_addr[4:0], cpu_dbg_we,
613 2 dgisselq
                                dbg_idata, cpu_dbg_stall, cpu_dbg_data,
614 18 dgisselq
                                cpu_dbg_cc, cpu_break,
615 36 dgisselq
                        cpu_gbl_cyc, cpu_gbl_stb,
616
                                cpu_lcl_cyc, cpu_lcl_stb,
617
                                cpu_we, cpu_addr, cpu_data,
618 2 dgisselq
                                cpu_ack, cpu_stall, wb_data,
619 36 dgisselq
                                cpu_err,
620 66 dgisselq
                        cpu_op_stall, cpu_pf_stall, cpu_i_count
621
`ifdef  DEBUG_SCOPE
622
                        , o_cpu_debug
623
`endif
624
                        );
625 2 dgisselq
 
626
        // Now, arbitrate the bus ... first for the local peripherals
627 36 dgisselq
        // For the debugger to have access to the local system bus, the
628
        // following must be true:
629
        //      (dbg_cyc)       The debugger must request the bus
630
        //      (~cpu_lcl_cyc)  The CPU cannot be using it (CPU gets priority)
631
        //      (dbg_addr)      The debugger must be requesting its data
632
        //                              register, not just the control register
633
        // and one of two other things.  Either
634
        //      ((cpu_halt)&&(~cpu_dbg_stall))  the CPU is completely halted,
635
        // or
636
        //      (~cmd_addr[5])          we are trying to read a CPU register
637
        //                      while in motion.  Let the user beware that,
638
        //                      by not waiting for the CPU to fully halt,
639
        //                      his results may not be what he expects.
640
        //
641
        wire    sys_dbg_cyc = ((dbg_cyc)&&(~cpu_lcl_cyc)&&(dbg_addr))
642
                                &&(((cpu_halt)&&(~cpu_dbg_stall))
643
                                        ||(~cmd_addr[5]));
644
        assign  sys_cyc = (cpu_lcl_cyc)||(sys_dbg_cyc);
645
        assign  sys_stb = (cpu_lcl_cyc)
646
                                ? (cpu_lcl_stb)
647 2 dgisselq
                                : ((dbg_stb)&&(dbg_addr)&&(cmd_addr[5]));
648
 
649 36 dgisselq
        assign  sys_we  = (cpu_lcl_cyc) ? cpu_we : dbg_we;
650
        assign  sys_addr= (cpu_lcl_cyc) ? cpu_addr[4:0] : cmd_addr[4:0];
651
        assign  sys_data= (cpu_lcl_cyc) ? cpu_data : dbg_idata;
652 2 dgisselq
 
653
        // Return debug response values
654
        assign  dbg_odata = (~dbg_addr)?cmd_data
655
                                :((~cmd_addr[5])?cpu_dbg_data : wb_data);
656
        initial dbg_ack = 1'b0;
657
        always @(posedge i_clk)
658 36 dgisselq
                dbg_ack <= (dbg_cyc)&&(~dbg_stall);
659
        assign  dbg_stall=(dbg_cyc)&&((~sys_dbg_cyc)||(sys_stall))&&(dbg_addr);
660 2 dgisselq
 
661
        // Now for the external wishbone bus
662
        //      Need to arbitrate between the flash cache and the CPU
663
        // The way this works, though, the CPU will stall once the flash 
664
        // cache gets access to the bus--the CPU will be stuck until the 
665
        // flash cache is finished with the bus.
666 36 dgisselq
        wire            ext_cyc, ext_stb, ext_we, ext_err;
667
        wire            cpu_ext_ack, cpu_ext_stall, ext_ack, ext_stall,
668
                                cpu_ext_err;
669 48 dgisselq
        wire    [(AW-1):0]       ext_addr;
670
        wire    [31:0]           ext_odata;
671 56 dgisselq
        wbpriarbiter #(32,AW) dmacvcpu(i_clk,
672 36 dgisselq
                        cpu_gbl_cyc, cpu_gbl_stb, cpu_we, cpu_addr, cpu_data,
673
                                cpu_ext_ack, cpu_ext_stall, cpu_ext_err,
674
                        dc_cyc, dc_stb, dc_we, dc_addr, dc_data,
675
                                        dc_ack, dc_stall, dc_err,
676
                        ext_cyc, ext_stb, ext_we, ext_addr, ext_odata,
677
                                ext_ack, ext_stall, ext_err);
678 2 dgisselq
 
679 3 dgisselq
`ifdef  DELAY_EXT_BUS
680 48 dgisselq
        busdelay #(AW,32) extbus(i_clk,
681 2 dgisselq
                        ext_cyc, ext_stb, ext_we, ext_addr, ext_odata,
682 36 dgisselq
                                ext_ack, ext_stall, ext_idata, ext_err,
683 2 dgisselq
                        o_wb_cyc, o_wb_stb, o_wb_we, o_wb_addr, o_wb_data,
684 56 dgisselq
                                i_wb_ack, i_wb_stall, i_wb_data, (i_wb_err)||(wdbus_int));
685 3 dgisselq
`else
686
        assign  o_wb_cyc   = ext_cyc;
687
        assign  o_wb_stb   = ext_stb;
688
        assign  o_wb_we    = ext_we;
689
        assign  o_wb_addr  = ext_addr;
690
        assign  o_wb_data  = ext_odata;
691
        assign  ext_ack    = i_wb_ack;
692
        assign  ext_stall  = i_wb_stall;
693
        assign  ext_idata  = i_wb_data;
694 56 dgisselq
        assign  ext_err    = (i_wb_err)||(wdbus_int);
695 3 dgisselq
`endif
696 2 dgisselq
 
697
        wire            tmr_ack;
698
        assign  tmr_ack = (tma_ack|tmb_ack|tmc_ack|jif_ack);
699
        wire    [31:0]   tmr_data;
700
        assign  tmr_data = (tma_ack)?tma_data
701
                                :(tmb_ack ? tmb_data
702
                                :(tmc_ack ? tmc_data
703
                                :jif_data));
704
        assign  wb_data = (tmr_ack|wdt_ack)?((tmr_ack)?tmr_data:wdt_data)
705 36 dgisselq
                        :((actr_ack|dmac_ack)?((actr_ack)?actr_data:dmac_data)
706 2 dgisselq
                        :((pic_ack|ctri_ack)?((pic_ack)?pic_data:ctri_data)
707 56 dgisselq
                        :((wdbus_ack)?wdbus_data:(ext_idata))));
708 2 dgisselq
 
709 36 dgisselq
        assign  sys_stall = (tma_stall | tmb_stall | tmc_stall | jif_stall
710
                                | wdt_stall | ctri_stall | actr_stall
711 56 dgisselq
                                | pic_stall | dmac_stall | wdbus_stall);
712 36 dgisselq
        assign  cpu_stall = (sys_stall)|(cpu_ext_stall);
713 56 dgisselq
        assign  sys_ack = (tmr_ack|wdt_ack|ctri_ack|actr_ack|pic_ack|dmac_ack|wdbus_ack);
714 36 dgisselq
        assign  cpu_ack = (sys_ack)||(cpu_ext_ack);
715
        assign  cpu_err = (cpu_ext_err)&&(cpu_gbl_cyc);
716 18 dgisselq
 
717
        assign  o_ext_int = (cmd_halt) && (~cpu_stall);
718
 
719 2 dgisselq
endmodule

powered by: WebSVN 2.1.0

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