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

Subversion Repositories m65c02

[/] [m65c02/] [trunk/] [Src/] [RTL/] [M65C02.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 MichaelA
////////////////////////////////////////////////////////////////////////////////
2
//
3
//  Copyright 2012-2013 by Michael A. Morris, dba M. A. Morris & Associates
4
//
5
//  All rights reserved. The source code contained herein is publicly released
6
//  under the terms and conditions of the GNU Lesser Public License. No part of
7
//  this source code may be reproduced or transmitted in any form or by any
8
//  means, electronic or mechanical, including photocopying, recording, or any
9
//  information storage and retrieval system in violation of the license under
10
//  which the source code is released.
11
//
12
//  The source code contained herein is free; it may be redistributed and/or 
13
//  modified in accordance with the terms of the GNU Lesser General Public
14
//  License as published by the Free Software Foundation; either version 2.1 of
15
//  the GNU Lesser General Public License, or any later version.
16
//
17
//  The source code contained herein is freely released WITHOUT ANY WARRANTY;
18
//  without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
19
//  PARTICULAR PURPOSE. (Refer to the GNU Lesser General Public License for
20
//  more details.)
21
//
22
//  A copy of the GNU Lesser General Public License should have been received
23
//  along with the source code contained herein; if not, a copy can be obtained
24
//  by writing to:
25
//
26
//  Free Software Foundation, Inc.
27
//  51 Franklin Street, Fifth Floor
28
//  Boston, MA  02110-1301 USA
29
//
30
//  Further, no use of this source code is permitted in any form or means
31
//  without inclusion of this banner prominently in any derived works. 
32
//
33
//  Michael A. Morris
34
//  Huntsville, AL
35
//
36
///////////////////////////////////////////////////////////////////////////////
37
 
38
`timescale 1ns / 1ps
39
 
40
////////////////////////////////////////////////////////////////////////////////
41
// Company:         M. A. Morris & Assoc.
42
// Engineer:        Michael A. Morris
43
// 
44
// Create Date:     12:49:16 11/18/2012 
45
// Design Name:     WDC W65C02 Microprocessor Re-Implementation
46
// Module Name:     M65C02.v 
47
// Project Name:    C:\XProjects\ISE10.1i\M65C02 
48
// Target Devices:  Generic SRAM-based FPGA 
49
// Tool versions:   Xilinx ISE10.1i SP3
50
//
51
// Description:
52
//
53
//  This module provides a synthesizable implementation of a 65C02 micropro-
54
//  cessor similar to the WDC W65C02S. The original W65C02 implemented a set of
55
//  enhancements to the MOS6502 microprocessor. Two new addressing modes were
56
//  added, several existing instructions were rounded out using the new address-
57
//  ing modes, and some additional instructions were added to fill in holes pre-
58
//  in the instruction set of the MOS6502. Rockwell second sourced the W65C02, 
59
//  and in the process added 4 bit-oriented instructions using 32 opcodes. WDC
60
//  released the W65816/W65802 16-bit enhancements to the W65C02. Two of the new
61
//  instructions in these processors, WAI and STP, were combined with the four
62
//  Rockwell instructions, RMBx/SMBx and BBRx/BBSx, along with the original
63
//  W65C02's instruction set to realize the W65C02S.
64
//
65
//  The M65C02 core is a realization of the W65C02S instruction set. It is not a
66
//  cycle accurate implementation, and it does not attempt to match the idiosyn-
67
//  cratic behavior of the W65C02 or the W65C02S with respect to unused opcodes.
68
//  In the M65C02 core, all unused opcodes are realized as single byte, single
69
//  cycle NOPs.
70
//
71
//  This module demonstrates how to incorporate the M65C02_Core.v logic module
72
//  into an application-specific implementation. The core logic incorporates
73
//  most of the logic required for a microprocessor implementation: ALU, regis-
74
//  ters, address generator, and instruction decode and sequencing. Not included
75
//  in the core logic are the memory interface, the interrupt handler, the clock
76
//  generator, and any peripherals.
77
//
78
//  This module integrates the M65C02_Core.v module, an external memory inter-
79
//  face, a simple vectored interrupt controller, and a clock generator. The ob-
80
//  jective is a module that emulates the external interfaces of a 5C02 proces-
81
//  sor. The intent is not to develop a W65C02S replacement; an FPGA-based emu-
82
//  lation of a processor still in production and readily available is not eco-
83
//  nomically viable, or an objective of this project. (To be economically
84
//  viable, an FPGA-based implementation of a 65C02 system using the M65C02
85
//  (or WDC's synthesizable W65C02S) must be more than just a drop-in replace-
86
//  ment of the microprocessor; it must include on-chip peripherals and addi-
87
//  tional I/O interfaces, provide extended addressing, higher performance, etc.
88
//  In other words, it must be more than just a replacement of the inexpensive
89
//  40-pin/44-pin W65C02S microprocessor.)
90
//
91
//  The 6502 memory interface is not particularly well suited for an FPGA-based
92
//  implementation. FPGA-based implementations prefer to use a single clock, and
93
//  the 6502 memory interface uses a two phase clocking scheme. Further, for
94
//  6502-family peripherals which require a clock, the clock needs to be conti-
95
//  nuous and symmetric. The M65C02 core logic will be overclocked relative to
96
//  the 6502 memory interface. Thus, the memory interface controller can ensure
97
//  that the core logic and the output clocks are synchronized and continuous
98
//  and symmetric. However, this requires that any wait states needed by the ex-
99
//  ternal memory or peripherals must be inserted as integer multiples of the
100
//  external memory cycle length. This means that if there are four micro-cycles
101
//  per external memory cycle, then every requested wait state will add an addi-
102
//  tional 4 cycles to each microcycle.
103
//
104
//  With this configuration, the external memory's access time determines the
105
//  overall performance of the M65C02. Asynchronous memories are probably the
106
//  least expensive of any of the high-speed static RAMs currently available.
107
//  Therefore, the external memory interface provided with the M65C02 will pro-
108
//  vide support for high-speed asynchronous SRAMs and Flash EPROMs. To provide
109
//  reasonable price vs. performance, a 25ns access time will be used as the
110
//  target device speed for RAM. 
111
//
112
//  A common interface provided by embedded computers is the asynchronous serial
113
//  port. To make the interface reliable, the expectation is that the external
114
//  clock input of the M65C02 will be a provided by a "baud rate" crystal oscil-
115
//  lator. The frequency expected is the commonly available 18.432 MHz.
116
//
117
//  The Tiockp, clock to output time, of a typical FPGA capable of 100 MHz
118
//  internal operation is: 3.4ns for -5 Spartan-3AN with the IOBs configured for
119
//  LVTTL operation, 12mA drive, and fast slew rate. The input setup time with
120
//  input delay, Tiopickd, is: 3.73ns with IOB-DELAY=3. These delays, which sum
121
//  to 7.13ns, must be added to the RAM's access time to determine the external
122
//  memory cycle time: 42.13ns for a 35ns access time device.
123
//
124
//  If the external 18.432 MHz clock is multiplied by 4 and divided by 4 to set
125
//  the external memory cycle period, the resulting period would be 54.253ns,
126
//  which satisfies the 42.13ns requirement with some margin. Therefore, the
127
//  module will use a DCM whose input frequency is provided by a 18.432 MHz
128
//  oscillator. The internally the M65C02 will operate at 73.728 MHz in a
129
//  4 clock per microcycle configuration; the memory controller controls the
130
//  microcycle length of the M65C02.
131
//
132
// Dependencies:    M65C02_Core.v
133
//                      M65C02_MPCv4.v
134
//                          M65C02_uPgm_V3a.coe (M65C02_uPgm_V3a.txt)
135
//                          M65C02_Decoder_ROM.coe (M65C02_Decoder_ROM.txt)
136
//                      M65C02_AddrGen.v
137
//                      M65C02_ALU.v
138
//                          M65C02_Bin.v
139
//                          M65C02_BCD.v 
140
//
141
// Revision: 
142
//
143
//  0.00    12B18   MAM     Initial File Creation
144
//
145
//  0.01    13B16   MAM     Added DCM/DFS clock generator, refined port list,
146
//                          and refined internal reset signal generation.
147
//
148
//  1.00    13B23   MAM     Completed the integration and testing of the M65C02
149
//                          implementation as a standalone microprocessor.
150
//
151
//  2.00    13B25   MAM     Pulled into the M65C02 module the Boot ROM/RAM used
152
//                          for testing. The test program will be used during
153
//                          testing. For a final product, the ROM/RAM can be
154
//                          loaded with a Monitor or other boot program. In this
155
//                          manner, all of the block RAM of the target FPGA is
156
//                          used, but maximum performance can be achieved. i.e.
157
//                          no clock stretching is required when running from
158
//                          block RAM. To perform the internal multiplexing of
159
//                          input data bus, added a dedicated chip enable for
160
//                          this device, BootROM, that controls the multiplexer.
161
//                          Modified the clock multiplexer, now that internal
162
//                          block RAM is used for the Boot/Monitor program, so
163
//                          that SYS and ROM chip enables are used for the dy-
164
//                          namic clock stretching circuit. Added a nWP input to
165
//                          inhibit writes to the Boot/Monitor Block RAM.
166
//
167
//  2.10    13B27   MAM     Removed all unused logic, or RTL commented out.
168
//
169
//  2.20    12C02   MAM     Incorporated M65C02_Core with M65C02_MPCv4 which in-
170
//                          cludes built-in microcycle length control and 6502-
171
//                          compatible wait state generator: Phi1O and Phi2O are
172
//                          are maintained as symmetrical signals. Removed clock
173
//                          stretch logic, BUFGMUX, and added four more micro-
174
//                          cycle state decode signals: C4-C8. This required in-
175
//                          creasing the MC vector size from 2 to 3 bits.
176
//
177
//  2.21    12C03   MAM     Added C7 to the CE for the external memory data in-
178
//                          put. Also qualified DI_IFD CEwith the Rdy input sig-
179
//                          nal.
180
//
181
//  2.30    13F08   MAM     Modified for Enso's Spartan 3A board with its 60 MHz
182
//                          oscillator. Added an input for that clock, divided
183
//                          the 60 MHz input by 4, and output the 15 MHz signal.
184
//                          Expect Enso to connect the output of the divider to
185
//                          the input clock pin of the M65C02 DCM. This mini-
186
//                          mizes the amount of work needed to port the M65C02
187
//                          to his project. An accompanying change is needed to
188
//                          UCF file.
189
//
190
//  2.40    13F17   MAM     Reworked address decode to match the implementation
191
//                          of the 1004-0001 M65C02 Demo Card: two 32kB RAMs,
192
//                          one 512kB Flash, IO, and Boot ROM (internal Block
193
//                          RAM). RAM[0] is fully mapped. Only the first 16kB of
194
//                          RAM[1] is mapped. For the ROM, 12kB is mapped. The
195
//                          external IO is mapped to 2kB. The last 2kB are
196
//                          mapped to a Block RAM that serves to hold the Boot
197
//                          Program.
198
//
199
//  2.71    13F20   MAM     Corrected error in the generation of the Rst_M65C02
200
//                          internal reset signal. The reduction of the reset
201
//                          signal shift register was previously done as an AND.
202
//                          That reduction has been changed to an OR, which is
203
//                          the correct vector reduction operator to generate
204
//                          internal reset signal from the external nRst and the
205
//                          DCM_Locked signal.
206
//
207
//  2.72    13H04   MAM     Removed unused code. Changed the DI multiplexer into
208
//                          simple OR gate. The FFs feeding into the OR gate are
209
//                          forced to 0 if not selected. Changed to reset signal
210
//                          for several output strobes and input registers. On
211
//                          outputs, the RnW signal terminates synchronously
212
//                          with the rising edge of the Wr strobe. DI_IFD is rst
213
//                          if an internal data source is selected. BootIFD is
214
//                          reset if an external data source is selected.
215
//
216
//  2.73    13H17   MAM     Adapted the M16C5x Clock Generator module for use
217
//                          with M65C02. Also encapsulated the interrupt handler
218
//                          function in another module.  
219
//
220
// Additional Comments:
221
//
222
//  With regard to the W65C02S, the M65C02 microprocessor implementation differs
223
//  in a number of ways:
224
//
225
//      1)  The instruction set is emulated, but cycle accuracy was not an
226
//          objective, and the implementation provided here makes no attempt to
227
//          to provide instruction cycles times which match those of the W65C02S
228
//          microprocessor.
229
//
230
//          The M65C02 core provides pipelined execution and fetch operations.
231
//          This feature allows the M65C02 to reduce the number of memory cycles
232
//          required per instruction. In addition, additional address generation
233
//          logic for sequential operand fetch, program counter updates, and
234
//          stack pointer updates allows some complex instructions to reduce the
235
//          number of memory cycles required by one or two cycles. (Branches all
236
//          execute in two cycles regardless of whether the branch is taken or
237
//          not taken.)
238
//
239
//      2)  The W65C02S provides capabilities for wait state insertion, external
240
//          DMA control, and external falling edge-edge setting of the V flag in
241
//          the processor status word.
242
//
243
//          The M65C02 implementation provided here does not support the inser-
244
//          tion of wait states by external logic. The implementation does sup-
245
//          port the BE input signal to tri-state the signals of the processor
246
//          connecting to the bus. The BE_In input port will tri-state all of
247
//          the M65C02 processor bus signals. Since wait state insertion is not
248
//          supported, external DMA logic can not stop the M65C02 processor and
249
//          take control of the bus. The nSO port is provided to reserve a pin
250
//          for a potential future upgrade of the M65C02 to support the Set
251
//          Overflow feature found in the W65C02S. The current implementation of
252
//          the M65C02 core will have to be modified to support this function.
253
//
254
//      3)  Like the W65C02S, the M65C02 provides a Vector Pull output pin. The
255
//          pin, nVP, is asserted by the M65C02 during the two memory cycles in
256
//          which the IRQ/NMI/BRK/RST vectors are read from ROM/RAM/Registers.
257
//
258
//      4)  Unlike the W65C02S, the M65C02 provides four chip enable outputs to
259
//          simplify the selection of RAM, ROM, SYStem ROM, and I/O devices.
260
//
261
//          The current implementation provides a four chip enables: CE[3:0].
262
//
263
//          CE[0], RAM chip enable, asserts for a 48kB range: 0x0000-0xBFFF.
264
//          CE[1], ROM chip enable, asserts for a  8kB range: 0xC000-0xDFFF.
265
//          CE[2], SYS chip enable, asserts for a  4kB range: 0xE000-0xEFFF.
266
//          CE[3], IO  chip enable, asserts for a  4kB range: 0xF000-0xFFFF.
267
//          
268
//          The M65C02 also makes provisions for extended address outputs,
269
//          XA[3:0], which are intended to be used with a simple internal MMU to
270
//          allow mapping of 8kB blocks in a total address space of 4MB.
271
//
272
//      5)  The M65C02 implements a bus interface that is not exactly a standard
273
//          implementation of the two phase 6502 memory interface.
274
//
275
//          To address generation logic used to reduce the number of memory
276
//          cycles required per instruction is combinatorial and in series with
277
//          the output address lines. The M65C02 registers all I/O signals. The
278
//          consequence of this implementation detail is that the M65C02's
279
//          address output is delayed from its internal clock's rising edge by a
280
//          significant portion of the clock period. A synchronous output is
281
//          very much desired because it provides consistent clock to output 
282
//          delay times, and it eliminates the skew in the combinatorial signal
283
//          paths of the M65C02 core's address generator. Thus, the memory
284
//          address, which in a typical 6502 is output during Phi1O, is not out-
285
//          put by the M65C02 until the start of Phi2O.
286
//
287
//          The control signals and the output data are are similarly registered
288
//          and delayed to coincide with the delay in the address. This means
289
//          that the external memory and I/O devices must be able to operate
290
//          with signals which are only asserted during Phi2O. The reduced
291
//          operating margins means that RAMs and IO devices must be able to
292
//          reliably operate in a window of approximately 20ns.
293
//
294
//          To use the M65C02 core in this environment, the M65C02 processor
295
//          implementation effectively shews the 6502 memory cycle by a quarter
296
//          of the cycle. There are four states in the M65C02 core's microcycle.
297
//
298
//              C1 - Address computation cycle
299
//              C2 - Output address, data, and control signals
300
//              C3 - Deassert control, and capture input data
301
//              C4 - Execute current instruction and decode next instruction 
302
//
303
//          In this four cycle process, Phi1O is asserted during C4 and C1, and
304
//          Phi2O is asserted during C2 and C3. 
305
//          
306
//          For SRAMs, it is not difficult to meet the timing requirements im-
307
//          posed by this modified 6502 memory cycle with an inexpensive device.
308
//          For ROMs, the modified 6502 memory cycle is difficult to satisfy 
309
//          with devices suitable for use with a 6502, i.e. NOR Flash devices.
310
//          The minimum access times that NOR Flash devices provide is 45ns, and
311
//          the typical device requires access times of 55ns and/or 70ns.
312
//
313
//          One objective of the M65C02 is to provide a memory interface solu-
314
//          tion to which it is easy to connect readily available memory. It is
315
//          for this reason that the CE logic is included in the implementation.
316
//          In addition, two control signals, nOE and nWr, are provided so that
317
//          external logic is not required to combine Phi2O and RnW into output
318
//          and write enable signals.
319
//
320
//          Without implementing a wait state generator, the M65C02 requires
321
//          some means to support a slow NOR Flash memory devices. To accomplish
322
//          this without requiring external logic or extremely fast NOR Flash
323
//          devices, means that some type of internally generated clock stretch
324
//          logic is required. The M65C02 provides automatic clock stretching
325
//          logic for the IO chip enable address range. 
326
//
327
//          The FPGA clocking resources provide a glitchless clock multiplexer.
328
//          Using one of these multiplexers, the M65C02 multiplexes the internal
329
//          system clock between 73.728 MHz ClkFX clock and the 36.864 MHz Clk2X
330
//          clock. (ClkFX and Clk2X are directly related and generated in phase
331
//          from the input clock. Clk2X is used as the feedback source for the
332
//          DCM, and is in phase with ClkFX which is generated by the DFS.)
333
//
334
//          These clock changes will be glitch free, and will demonstrate the
335
//          operation of a clocking resource of the FPGA that is seldom used.
336
//          But when accessing the 4kB of IO, the frequency and duty cycle of
337
//          the Phi1O/Phi2O two phase clock will not be held at 18.432 MHz or
338
//          50%. The clock multiplexer logic stretches the clocks from a nominal
339
//          period equal to the period of the input clock, or 54.253ns, to a
340
//          period equal to 1.5x the period of the input clock, or 81.830ns. In
341
//          the clock multiplexer implemented, the Phi1O pulse width is un-
342
//          changed, 27.127ns, and the Phi2O pulse width is doubled, 54.253ns.
343
//
344
////////////////////////////////////////////////////////////////////////////////
345
 
346
module M65C02 #(
347
    parameter pStkPtr_Rst  = 8'hFF,         // SP Value after Rst
348
 
349
    parameter pIRQ_Vector = 16'hFFFE,       // IRQ Vector Addrs
350
    parameter pBRK_Vector = 16'hFFFE,       // Brk Vector Addrs
351
    parameter pRST_Vector = 16'hFFFC,       // Reset Vector Addrs
352
    parameter pNMI_Vector = 16'hFFFA,       // NMI Vector Addrs
353
 
354
    parameter pInt_Hndlr  = 9'h021,         // Microprogram Interrupt Handler
355
 
356
    parameter pBRK        = 3'b010,         // BRK #imm instruction
357
    parameter pWAI        = 3'b111,         // WAI Mode
358
 
359
    parameter pNOP        = 8'hEA,          // M65C02 Core NOP instruction
360
 
361
    parameter pROM_AddrWidth = 11,          // Boot/Monitor ROM Addres Width
362
 
363
    parameter pM65C02_uPgm  = "Src/M65C02_uPgm_V3a.coe",
364
    parameter pM65C02_IDec  = "Src/M65C02_Decoder_ROM.coe",
365
    parameter pBootROM_File = "Src/M65C02_Tst5.txt"
366
)(
367
    input   nRst,               // System Reset Input
368
    output  nRstO,              // Internal System Reset Output (OC w/ PU)
369
    input   ClkIn,              // System Clk Input
370
 
371
    output  reg Phi2O,          // Clock Phase 2 Output
372
    output  reg Phi1O,          // Clock Phase 1 Output - complement of Phi2O
373
 
374
    input   nSO,                // Set oVerflow: currently unimplemented
375
 
376
    input   nNMI,               // Non-Maskable Interrupt Request: edge sense
377
    input   nIRQ,               // Maskable Interrupt Request: level sense
378
    output  nVP,                // Vector Pull: asserted to indicate ISR taken
379
 
380
    input   BE_In,              // Bus Enable: tri-states address, data, control
381
    output  Sync,               // Synchronize: asserted during opcode fetch
382
    output  nML,                // Memory Lock: asserted during RMW instructions
383
 
384
    output  [3:0] nCE,          // Chip Enable for External RAM/ROM Memory
385
    output  RnW,                // Read/nWrite cycle control output signal
386
    output  nWr,                // External Asynchronous Bus Write Strobe
387
    output  nOE,                // External Asynchronous Bus
388
    inout   Rdy,                // Bus cycle Ready, drive low to extend cycle
389
    output  [ 3:0] XA,          // Extended Address Output for External Memory
390
    output  [15:0] A,           // External Memory Address Bus
391
    inout   [ 7:0] DB,          // External, Bidirectional Data Bus
392
 
393
    input   nWP_In,             // Internal Boot/Monitor RAM write protect
394
 
395
    output  reg nWait,          // Driven low by Wait instruction (ASIC-only)
396
 
397
    output  reg [4:0] LED,      // LED Test Register
398
 
399
    output  nSel,               // SPI I/F Chip Select
400
    output  SCk,                // SPI I/F Serial Clock
401
    output  MOSI,               // SPI I/F Master Out/Slave In Serial Data
402
    input   MISO                // SPI I/F Master In/Slave Out Serial Data
403
);
404
 
405
///////////////////////////////////////////////////////////////////////////////
406
//
407
//  Declarations
408
//
409
 
410
wire    Rst;                    // Internal reset (Clk)
411
reg     OE_nRstO;               // Internal reset output (Buf_ClkIn)
412
 
413
//wire    RE_NMI;                 // Output pulse signal from nNMI edge detector
414
//wire    CE_NMI;                 // NMI latch/register clock enable
415
//reg     NMI;                    // NMI latch/register to hold NMI until serviced
416
wire    NMI;                    // NMI latch/register to hold NMI until serviced
417
 
418
//reg     nIRQ_IFD, IRQ;          // External maskable interrupt request inputs
419
wire    IRQ;                    // External maskable interrupt request input
420
 
421
wire    Int;                    // Interrupt handler interrupt signal to M65C02
422
wire    [15:0] Vector;          // Interrupt handler interrupt vector to M65C02
423
 
424
wire    Brk;                    // Decoded M65C02 core instruction mode - BRK
425
 
426
reg     BE_IFD;                 // External Bus Enable input register (IOB)
427
wire    BE;                     // Internal Bus Enable signal
428
 
429
wire    IRQ_Msk;                // M65C02 core interrupt mask
430
wire    IntSvc;                 // M65C02 core interrupt service indicator
431
wire    ISR;                    // M65C02 core signal for signaling vector read
432
wire    Done;                   // M65C02 core instruction complete/fetch
433
wire    [2:0] Mode;             // M65C02 core instruction mode
434
wire    RMW;                    // M65C02 core Read-Modify-Write indicator
435
wire    [2:0] MC;               // M65C02 core microcycle 
436
wire    [1:0] IO_Op;            // M65C02 core I/O cycle type
437
wire    [15:0] AO;              // M65C02 core Address Output
438
wire    [ 7:0] DI;              // M65C02 core Data Input
439
wire    [ 7:0] DO;              // M65C02 core Data Output
440
 
441
wire    C1, C2, C3, C4;         // Decoded microcycle states
442
wire    C5, C6, C7, C8;         // Decoded microcycle states
443
 
444
reg     [1:0] VP;               // Vector read/pull pulse stretcher
445
 
446
reg     Sync_OFD;
447
reg     nML_OFD;
448
reg     RnW_OFD;
449
 
450
wire    BootROM;                // Internal Block RAM/ROM for Boot/Monitor
451
wire    IO, ROM;                // Address decode signals: CE[3], CE[2]
452
wire    [1:0] RAM;              // Address decode signals: CE[1], CE[0]
453
 
454
reg     [ 3:0] nCE_OFD;         // Decoded Chip Enable output (IOB registers)
455
reg     [ 3:0] XA_OFD;          // Extended Address output (IOB registers)
456
reg     [15:0] AO_OFD;          // Address Output (IOB registers)
457
 
458
reg     nOE_OFD, nWr_OFD;
459
 
460
reg     [7:0] DO_OFD;
461
reg     [7:0] DI_IFD;
462
 
463
reg     nWP;                        // Boot/Monitor RAM write protect
464
reg     WE_Boot;                    // Write Enable for the Boot/Monitor ROM
465
reg     [(pROM_AddrWidth-1):0] iAO; // Internal address pipeline register
466
reg     [7:0] iDO;                  // Internal output data pipeline register
467
reg     [7:0] Boot [((2**pROM_AddrWidth)-1):0];  // Boot ROM/RAM (2k x 8)
468
reg     [7:0] Boot_DO;              // Boot/Monitor ROM output data (absorbed)
469
reg     [7:0] Boot_IFD;             // Boot/Monitor ROM output pipeline register
470
 
471
////////////////////////////////////////////////////////////////////////////////
472
//
473
//  Implementation
474
//
475
 
476
// Instantiate the Clk and Reset Generator Module
477
 
478
M65C02_ClkGen   ClkGen (
479
                    .nRst(nRst),
480
                    .ClkIn(ClkIn),
481
 
482
                    .Clk(Clk),              // Clk      <= (M/D) x ClkIn
483
                    .Clk_UART(),            // Clk_UART <= 2x ClkIn 
484
                    .Buf_ClkIn(Buf_ClkIn),  // RefClk   <= Buffered ClkIn
485
 
486
                    .Rst(Rst)
487
                );
488
 
489
//  Generate Reset output for use by external circuits
490
 
491
always @(posedge Buf_ClkIn or posedge Rst)
492
begin
493
    if(Rst)
494
        OE_nRstO <= #1 1;
495
    else
496
        OE_nRstO <= #1 0;
497
end
498
 
499
assign nRstO = ((OE_nRstO) ? 0 : 1'bZ);
500
 
501
//
502
//  Process External NMI and maskable IRQ Interrupts
503
//
504
 
505
M65C02_IntHndlr #(
506
                    .pRST_Vector(pRST_Vector),
507
                    .pIRQ_Vector(pIRQ_Vector),
508
                    .pBRK_Vector(pBRK_Vector),
509
                    .pNMI_Vector(pNMI_Vector)
510
                ) IntHndlr (
511
                    .Rst(Rst),
512
                    .Clk(Clk),
513
 
514
                    .nNMI(nNMI),
515
                    .nIRQ(nIRQ),
516
                    .Mode(Mode),
517
 
518
                    .IRQ_Msk(IRQ_Msk),
519
                    .IntSvc(IntSvc),
520
 
521
                    .Int(Int),
522
                    .Vector(Vector),
523
 
524
                    .NMI(NMI),
525
                    .IRQ(IRQ),
526
                    .Brk(Brk)
527
                );
528
 
529
//  Synchronize BE input to Clk
530
 
531
always @(posedge Clk or posedge Rst) BE_IFD <= #1 ((Rst) ? 0 : BE_In);
532
 
533
assign BE = BE_IFD;
534
 
535
//  Instantiate M65C02 Core
536
 
537
M65C02_Core #(
538
                .pStkPtr_Rst(pStkPtr_Rst),
539
                .pInt_Hndlr(pInt_Hndlr),
540
                .pM65C02_uPgm(pM65C02_uPgm),
541
                .pM65C02_IDec(pM65C02_IDec)
542
            ) uP (
543
                .Rst(Rst),
544
                .Clk(Clk),
545
 
546
                .IRQ_Msk(IRQ_Msk),
547
                .xIRQ(IRQ),
548
                .Int(Int),
549
                .Vector(Vector),
550
                .IntSvc(IntSvc),
551
                .ISR(ISR),
552
 
553
                .Done(Done),
554
                .SC(),
555
                .Mode(Mode),
556
                .RMW(RMW),
557
 
558
                .MC(MC),
559
                .MemTyp(),
560
                .Wait(~Rdy),
561
                .Rdy(),
562
 
563
                .IO_Op(IO_Op),
564
                .AO(AO),
565
                .DI(DI),
566
                .DO(DO),
567
 
568
                .A(),
569
                .X(),
570
                .Y(),
571
                .S(),
572
                .P(),
573
                .PC(),
574
 
575
                .IR(),
576
                .OP1(),
577
                .OP2()
578
            );
579
 
580
//  Define the Memory Cycle Strobes (1 cycle in width)
581
 
582
assign C1 = (MC == 6);      // 1st cycle of microcycle
583
assign C2 = (MC == 7);      // 2nd cycle of microcycle
584
assign C3 = (MC == 5);      // 3rd cycle of microcycle
585
assign C4 = (MC == 4);      // 4th cycle of microcycle
586
//
587
assign C5 = (MC == 2);      // 5th cycle of microcycle (Wait State Sequence)
588
assign C6 = (MC == 3);      // 6th cycle of microcycle (Wait State Sequence)
589
assign C7 = (MC == 1);      // 7th cycle of microcycle (Wait State Sequence)
590
assign C8 = (MC == 0);      // 8th cycle of microcycle (Wait State Sequence)
591
 
592
//  Assign Phi1O and Phi2O
593
 
594
always @(posedge Clk or posedge Rst)
595
begin
596
    if(Rst)
597
        {Phi1O, Phi2O} <= #1 2'b01;
598
    else begin
599
        Phi1O <= #1 (C3 | C4 | C7 | C8);
600
        Phi2O <= #1 (C1 | C2 | C5 | C6);
601
    end
602
end
603
 
604
//  Generate Chip Enables
605
 
606
assign BootROM = (&AO[15:12] &  AO[11]);        // 0xF800 - 0xFFFF =  2kB (Int)
607
assign IO      = (&AO[15:12] & ~AO[11]);        // 0xF000 - 0xF7FF =  2kB (Ext)
608
assign ROM     = (&AO[15:14] & ~&AO[13:12]);    // 0xC000 - 0xEFFF = 12kB (Ext)
609
assign RAM[1]  = (AO[15] & ~AO[14]);            // 0x8000 - 0xBFFF = 16kB (Ext)
610
assign RAM[0]  = ~AO[15];                       // 0x0000 - 0x7FFF = 32kB (Ext)
611
 
612
always @(posedge Clk)
613
begin
614
    if(Rst)
615
        nCE_OFD <= #1 ~0;
616
    else if(C1)
617
        nCE_OFD <= #1 {~IO, ~ROM, ~RAM[1], ~RAM[0]};
618
end
619
 
620
assign nCE = ((BE) ? nCE_OFD : {4{1'bZ}});
621
 
622
//  Generate Address Output
623
 
624
always @(posedge Clk)
625
begin
626
    if(Rst)
627
        {XA_OFD, AO_OFD} <= #1 {{4{1'b1}}, pRST_Vector};
628
    else
629
        {XA_OFD, AO_OFD} <= #1 {{4{AO[15]}}, AO};
630
end
631
 
632
assign {XA, A} = ((BE) ? {XA_OFD, AO_OFD} : {{4{1'bZ}}, {16{1'bZ}}});
633
 
634
//  Generate Vector Pull Output
635
 
636
always @(posedge Clk)
637
begin
638
    if(Rst)
639
        VP <= #1 0;
640
    else if(C4)
641
        VP <= #1 ((ISR) ? 2'b11 : {1'b0, VP[1]});
642
end
643
 
644
assign nVP = ((BE) ? ~VP[0] : 1'bZ);
645
 
646
//  Generate nWait output; assert nRdy input when nWait asserted
647
 
648
always @(posedge Clk or posedge Rst)
649
begin
650
    if(Rst)
651
        nWait <= #1 1;
652
    else if(C1)
653
        nWait <= #1 ~(Mode == pWAI);
654
end
655
 
656
//  Generate M65C02 Memory Lock Signal
657
 
658
always @(posedge Clk)
659
begin
660
    if(Rst)
661
        nML_OFD <= #1 1;
662
    else if(C1)
663
        nML_OFD <= #1 ~RMW;
664
end
665
 
666
assign nML = ((BE) ? nML_OFD : 1'bZ);
667
 
668
//  Generate Sync Output
669
 
670
always @(posedge Clk)
671
begin
672
    if(Rst)
673
        Sync_OFD <= #1 1;
674
    else if(C1)
675
        Sync_OFD <= #1 Done;
676
end
677
 
678
assign Sync = ((BE) ? Sync_OFD : 1'bZ);
679
 
680
//  Generate M65C02 RnW output
681
 
682
always @(posedge Clk)
683
begin
684
    if(Rst | ((C3 | C7) & Rdy))
685
        RnW_OFD <= #1 1;
686
    else if(C1)
687
        RnW_OFD <= #1 ~(IO_Op == 1);
688
end
689
 
690
assign RnW = ((BE) ? RnW_OFD : 1'bZ);
691
 
692
//  Generate Asynchronous SRAM Read Strobe
693
 
694
always @(posedge Clk)
695
begin
696
    if(Rst | ((C3 | C7) & Rdy))
697
        nOE_OFD <= #1 1;
698
    else if(C1)
699
        nOE_OFD <= #1 (~IO_Op[1] | BootROM);
700
end
701
 
702
assign nOE = ((BE) ? nOE_OFD : 1'bZ);
703
 
704
//  Generate Asynchronous SRAM Write Strobe 
705
 
706
always @(posedge Clk)
707
begin
708
    if(Rst | ((C3 | C7) & Rdy))
709
        nWr_OFD <= #1 1;
710
    else if(C1)
711
        nWr_OFD <= #1 ~(IO_Op == 1);
712
end
713
 
714
assign nWr = ((BE) ? nWr_OFD : 1'bZ);
715
 
716
//  Drive DO out of M65C02 module
717
//      Feed nWR strobe back in as second output enable. Coupled with half cycle
718
//      shift in the nOE signal, these delays should make it easy to satisfy 
719
//      bus disable times when write operations follow reads and vice-versa.
720
 
721
 
722
always @(posedge Clk or posedge Rst)
723
begin
724
    if(Rst)
725
        DO_OFD <= #1 0;
726
    else if(C1)
727
        DO_OFD <= #1 DO;
728
end
729
 
730
assign DB = ((BE & ~nWr) ? DO_OFD : 8'bZ);
731
 
732
//  Capture Input Data from External Memory
733
//      Half cycle shift allows more time for RAM/peripheral device to output
734
//      data. Internal signal paths in the FPGA will operate on the data within
735
//      half a cycle of Clk. This requires tighter path controls by the map and
736
//      route tools. DI is distributed to IR, OP1, OP2, uPgm_ROM, and IDec_ROM.
737
 
738
assign CE_DI_FFs  = ((C3 | C7) & Rdy);
739
assign Rst_DI_IFD = (Rst | BootROM);
740
 
741
always @(posedge Clk)
742
begin
743
    if(Rst_DI_IFD)
744
        DI_IFD <= #1 0;
745
    else if(CE_DI_FFs)
746
        DI_IFD <= #1 DB;
747
end
748
 
749
//  Implement 2k x 8 internal Boot/Monitor ROM in Block RAM
750
//      Allow writes so that the NMI, RST, and IRQ vectors can be changed.
751
//      External active low write protect signal allows the writing to this RAM
752
//      to be inhibited.
753
 
754
//  Synchronize external Boot/Monitor ROM write protect
755
 
756
always @(posedge Clk or posedge Rst) nWP <= #1 ((Rst) ? 0 : nWP_In);
757
 
758
//  Generate a synchronous Boot/Monitor ROM write enable 
759
 
760
always @(posedge Clk or posedge Rst)
761
begin
762
    if(Rst)
763
        WE_Boot <= #1 0;
764
    else
765
        WE_Boot <= #1 (BootROM & (IO_Op == 1) & nWP & C1);
766
end
767
 
768
//  Capture the output address to break long delay path like done for output
769
//      address
770
 
771
always @(posedge Clk)
772
begin
773
    if(Rst) begin
774
        iAO <= #1 pRST_Vector;
775
        iDO <= #1 0;
776
    end else if(C1) begin
777
        iAO <= #1 AO;
778
        iDO <= #1 DO;
779
    end
780
end
781
 
782
initial
783
  $readmemh(pBootROM_File, Boot, 0, ((2**pROM_AddrWidth)-1));
784
 
785
always @(posedge Clk)
786
begin
787
    if(WE_Boot)
788
        Boot[iAO] <= #1 iDO;
789
 
790
    Boot_DO <= #1 Boot[iAO];
791
end
792
 
793
//  Add pipeline register to break circular path through Address Generator
794
 
795
always @(posedge Clk or posedge Rst)
796
begin
797
    if(Rst)
798
        Boot_IFD <= #1 0;
799
    else if(CE_DI_FFs)
800
        Boot_IFD <= #1 ((BootROM) ? Boot_DO : 0);
801
end
802
 
803
//  Multiplex the External and Internal Data sources
804
 
805
assign DI = Boot_IFD | DI_IFD;
806
 
807
//  LED Test Register
808
 
809
assign WE_LED = (IO & (iAO[10:3] == 8'hFF) & (IO_Op == 1));
810
 
811
always @(posedge Clk)
812
begin
813
    if(Rst)
814
        LED <= #1 0;
815
    else if(C3)
816
        LED <= #1 ((WE_LED) ? iDO[7:3] : LED);
817
end
818
 
819
endmodule

powered by: WebSVN 2.1.0

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