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

Subversion Repositories zipcpu

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

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

powered by: WebSVN 2.1.0

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