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

Subversion Repositories zipcpu

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

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

powered by: WebSVN 2.1.0

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