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

Subversion Repositories m16c5x

[/] [m16c5x/] [trunk/] [RTL/] [Src/] [M16C5x.v] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 MichaelA
////////////////////////////////////////////////////////////////////////////////
2
//
3
//  Copyright 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 & Associates 
42
// Engineer:        Michael A. Morris 
43
// 
44
// Create Date:     19:30:58 06/15/2013 
45
// Design Name:     Microcomputer Implementation using P16C5x Processor Core
46
// Module Name:     M16C5x.v 
47
// Project Name:    C;\XProjects\ISE10.1i\M16C5x
48
// Target Devices:  RAM-based FPGA 
49
// Tool versions:   Xilinx ISE 10.1i SP3
50
//
51
// Description:
52
//
53
//  This module is a microcomputer implementation using an FPGA-based processor
54
//  core based on the P16C5x module. The P16C5x is derived from the released
55
//  PIC16C5x core found on GitHUB. The P16C5x differs from that core in that the
56
//  TRISA..TRISC registers, and the IO Ports A..C have been removed and replaced
57
//  by a number of WE and RE strobes and an IO data bus.
58
//
59
//  This modification has been done to demonstrate how the PIC16C5x core can be
60
//  adapted to interface to a UART or an SPI Master. By using the P16C5x core,
61
//  a microcomputer implementation can be generated for a small FPGA that pro-
62
//  vides significant processing capabilities. By using a core like the P16C5x,
63
//  standard programming languages and support tools can be used to ease the 
64
//  development of sophisticated FPGA-based products.
65
//
66
// Dependencies:    M16C5x_ClkGen.v
67
//                      ClkGen.xaw
68
//                      fedet.v
69
//                  P16C5x.v
70
//                      P16C5x_IDec.v
71
//                      P16C5x_ALU.v
72
//                  M16C5x_SPI.v
73
//                      DPSFmnCE.v
74
//                      SPIxIF.v
75
//                  M16C5x_UART.v
76
//                      SSPx_Slv.v
77
//                      SSP_UART.v
78
//                          re1ce.v
79
//                          DPSFmnCE.v
80
//                          UART_BRG.v
81
//                          UART_TXSM.v
82
//                          UART_RXSM.v
83
//                          UART_RTO.v
84
//                          UART_INT.v
85
//                              redet.v
86
//                              fedet.v
87
//
88
// Revision:
89
//
90
//  0.01    13F15   MAM     Initial creation of the M16C5x module.
91
//
92
//  2.20    13G14   MAM     Updated all of the module instantiations and the top
93
//                          module to support the parameterization of the soft-
94
//                          core microcontroller from the top level: M16C5x. Up-
95
//                          dated Dependencies section, and set revision to
96
//                          match the release number on GitHUB.
97
//
98
//  2.30    13G21   MAM     Changed UART Clk to operate from the Clk2x output of
99
//                          DCM. Gives a fixed value for the UART Clk regardless
100
//                          of the ClkFX output frequency. Adjusted default PS,
101
//                          Div values to produce 9600 bps as the default.
102
//
103
//  2.40    13J18   MAM     Remove CE TFF, and force single cycle operation.
104
//                          Changed clock edge for internal block RAM from the
105
//                          rising edge to the falling edge. This allows M16C5x
106
//                          to function as a single cycle core when operating 
107
//                          from internal block RAM.
108
//
109
//  2.41    13J19   MAM     Modified the UART FIFOs to be 64 elements deep.
110
// 
111
// Additional Comments: 
112
//
113
////////////////////////////////////////////////////////////////////////////////
114
 
115
module M16C5x #(
116
    // P16C5x Module Parameter Settings
117
 
118
    parameter pWDT_Size  = 20,              // 20 - synthesis; 10 - Simulation
119
    parameter pRstVector = 12'h7FF,         // Reset Vector Location (PIC16F59)
120
    parameter pUserProg  = "Src/M16C5x_Tst4.coe",   // Tst Pgm file: 4096 x 12
121
    parameter pRAMA_Init = "Src/RAMA.coe",  // RAM A initial value file ( 8x8)
122
    parameter pRAMB_Init = "Src/RAMB.coe",  // RAM B initial value file (64x8)
123
 
124
    // M16C5x_SPI Module Parameter Settings
125
 
126
    parameter pSPI_CR_Default = 8'b0_110_00_0_0,    // SPI Interface Defaults
127
    parameter pSPI_TF_Depth   = 4,          // Tx FIFO Depth: 2**pTF_Depth
128
    parameter pSPI_RF_Depth   = 4,          // Rx FIFO Depth: 2**pRF_Depth
129
    parameter pSPI_TF_Init    = "Src/TF_Init.coe",  // Tx FIFO Memory Init
130
    parameter pSPI_RF_Init    = "Src/RF_Init.coe",  // Rx FIFO Memory Init
131
 
132
    // SSP_UART Module Parameter Settings
133
 
134
    parameter pPS_Default    = 4'h0,        // see baud rate tables SSP_UART
135
    parameter pDiv_Default   = 8'hBF,       // BR = 9600 @UART_Clk = 29.4912 MHz
136
    parameter pRTOChrDlyCnt  = 3,           // Rcv Time Out Character Dly Count
137
    parameter pUART_TF_Depth = 2,           // Tx FIFO Depth: 2**(pTF_Depth + 4)
138
    parameter pUART_RF_Depth = 2,           // Rx FIFO Depth: 2**(pRF_Depth + 4)
139
    parameter pUART_TF_Init  = "Src/UART_TF.coe",   // Tx FIFO Memory Init
140
    parameter pUART_RF_Init  = "Src/UART_RF.coe"    // Rx FIFO Memory Init
141
)(
142
    input   ClkIn,                      // External Clk - drives 4x DCM
143
 
144
    input   nMCLR,                      // Master Clear Input
145
    input   nT0CKI,                     // Timer 0 Clk Input
146
    input   nWDTE,                      // Watch Dog Timer Enable
147
 
148
    input   PROM_WE,                    // Temporary Signal to Force Block RAM
149
 
150
    output  TD,                         // UART TD Output
151
    input   RD,                         // UART RD Input
152
    output  nRTS,                       // UART Request To Send (active low) Out
153
    input   nCTS,                       // UART Clear to Send (active low) Input
154
    output  DE,                         // UART RS-485 Driver Enable
155
 
156
    output  [2:0] nCS,                  // SPI Chip Select (active low) Output
157
    output  SCK,                        // SPI Serial Clock
158
    output  MOSI,                       // SPI Master Out/Slave In Output
159
    input   MISO,                       // SPI Master In/Slave Out Input
160
 
161
    //  Test Signals
162
 
163
    output  [2:0] nCSO,
164
    output  nWait
165
);
166
 
167
////////////////////////////////////////////////////////////////////////////////
168
//
169
//  Declarations
170
//
171
 
172
wire    ClkEn;
173
 
174
reg     [11:0] PROM [4095:0];           // User Program ROM (3x Block RAMs)
175
wire    [11:0] PROM_Addrs;              // Program Counter from CPU
176
reg     [11:0] PROM_DO;                 // Instruction Register to CPU
177
 
178
reg     nWDTE_IFD, nT0CKI_IFD;          // IOB FFs for external inputs
179
wire    WDTE, T0CKI;
180
 
181
wire    [7:0] IO_DO;                    // IO Data Output bus
182
reg     [7:0] IO_DI;                    // IO Data Input bus
183
 
184
reg     [7:0] TRISA, TRISB;             // IO Ports
185
reg     [7:0] PORTA, PORTB;
186
 
187
wire    [1:0] CS;                       // Chip select outputs of the SPI Mstr
188
wire    SPI_SCK;                        // SPI SCK for internal components
189
wire    SPI_MOSI, SPI_MISO;
190
 
191
wire    [7:0] SPI_DO;                   // Output Data Bus of SPI Master module
192
wire    TF_EF, TF_FF, RF_EF, RF_FF;     // SPI Module Status Signals
193
 
194
wire    SSP_MISO;                       // SSP UART MISO signal
195
wire    RTS, CTS;                       // SSP UART Modem Control Signals
196
wire    IRQ;                            // SSP UART Interrupt Request Signal
197
 
198
////////////////////////////////////////////////////////////////////////////////
199
//
200
//  Implementation
201
//
202
 
203
// Instantiate the Clk and Reset Generator Module
204
 
205
M16C5x_ClkGen   ClkGen (
206
                    .nRst(nMCLR),
207
                    .ClkIn(ClkIn),
208
 
209
                    .Clk(Clk),              // Clk      <= (M/D) x ClkIn
210
                    .Clk_UART(Clk_UART),    // Clk_UART <= 2x ClkIn 
211
                    .BufClkIn(),            // RefClk   <= Buffered ClkIn
212
 
213
                    .Rst(Rst)
214
                );
215
 
216
//  Force Single Cycle Operation
217
 
218
assign ClkEn = 1;
219
 
220
//  Register Inputs and connect to CPU
221
 
222
always @(posedge Clk)
223
begin
224
    if(Rst) begin
225
        nWDTE_IFD  <= #1 1;
226
        nT0CKI_IFD <= #1 1;
227
    end else if(ClkEn) begin
228
        nWDTE_IFD  <= #1 nWDTE;
229
        nT0CKI_IFD <= #1 nT0CKI;
230
    end
231
end
232
 
233
assign WDTE  = ~nWDTE_IFD;
234
assign T0CKI = ~nT0CKI_IFD;
235
 
236
// Instantiate the P16C5x module
237
 
238
P16C5x  #(
239
            .pRstVector(pRstVector),
240
            .pWDT_Size(pWDT_Size),
241
            .pRAMA_Init(pRAMA_Init),
242
            .pRAMB_Init(pRAMB_Init)
243
        ) CPU (
244
            .POR(Rst),
245
            .Clk(Clk),
246
            .ClkEn(ClkEn),
247
 
248
            .MCLR(Rst),
249
            .T0CKI(T0CKI),
250
            .WDTE(WDTE),
251
 
252
            .PC(PROM_Addrs),
253
            .ROM(PROM_DO),
254
 
255
            .WE_TRISA(WE_TRISA),
256
            .WE_TRISB(WE_TRISB),
257
            .WE_TRISC(WE_TRISC),
258
            .WE_PORTA(WE_PORTA),
259
            .WE_PORTB(WE_PORTB),
260
            .WE_PORTC(WE_PORTC),
261
            .RE_PORTA(RE_PORTA),
262
            .RE_PORTB(RE_PORTB),
263
            .RE_PORTC(RE_PORTC),
264
 
265
            .IO_DO(IO_DO),
266
            .IO_DI(IO_DI),
267
 
268
            .Rst(),
269
 
270
            .OPTION(),
271
            .IR(),
272
            .dIR(),
273
            .ALU_Op(),
274
            .KI(),
275
            .Msk(),
276
            .Err(),
277
            .Skip(),
278
            .TOS(),
279
            .NOS(),
280
            .W(),
281
            .FA(),
282
            .DO(),
283
            .DI(),
284
            .TMR0(),
285
            .FSR(),
286
            .STATUS(),
287
            .T0CKI_Pls(),
288
            .WDTClr(),
289
            .WDT(),
290
            .WDT_TC(),
291
            .WDT_TO(),
292
            .PSCntr(),
293
            .PSC_Pls()
294
        );
295
 
296
////////////////////////////////////////////////////////////////////////////////
297
//
298
//  User Program ROM
299
//
300
 
301
initial
302
  $readmemh(pUserProg, PROM, 0, 4095);
303
 
304
assign WE_PROM = ClkEn & WE_PORTA & PROM_WE;
305
 
306
always @(negedge Clk)
307
begin
308
    if(Rst)
309
        PROM_DO <= #1 0;
310
    else if(WE_PROM)
311
        PROM[{PORTB[7:0], TRISB[7:4]}] <= #1 {TRISB[3:0], TRISA[7:0]};
312
    else
313
        PROM_DO <= #1 PROM[PROM_Addrs];
314
end
315
 
316
////////////////////////////////////////////////////////////////////////////////
317
//
318
//  M16C5x I/O
319
//
320
 
321
always @(posedge Clk)
322
begin
323
    if(Rst) begin
324
        TRISA <= #1 ~0;
325
        TRISB <= #1 ~0;
326
        //
327
        PORTA <= #1 ~0;
328
        PORTB <= #1 ~0;
329
    end else if(ClkEn) begin
330
        TRISA <= #1 ((WE_TRISA) ? IO_DO : TRISA);
331
        TRISB <= #1 ((WE_TRISB) ? IO_DO : TRISB);
332
        //
333
        PORTA <= #1 ((WE_PORTA) ? IO_DO : PORTA);
334
        PORTB <= #1 ((WE_PORTB) ? IO_DO : PORTB);
335
    end
336
end
337
 
338
always @(*)
339
begin
340
    casex({RE_PORTA, RE_PORTB, RE_PORTC})
341
        3'b1xx  : IO_DI <= {IRQ, CTS, RTS, DE, RF_FF, RF_EF, TF_FF, TF_EF};
342
        3'b01x  : IO_DI <= PORTB;
343
        3'b001  : IO_DI <= SPI_DO;
344
        default : IO_DI <= 0;
345
    endcase
346
end
347
 
348
//assign TD      = ~PORTA[7];
349
//assign nRTS    = ~PORTA[6];
350
assign nCSO[2] = 1;
351
assign nCSO[1] = 1;
352
assign nCSO[0] = ~CS[0];
353
assign nWait   = ~CS[1];
354
 
355
// Instantiate the M16C5x SPI Interface module
356
 
357
assign SPI_MISO = ((CS[1]) ? SSP_MISO : MISO);
358
 
359
M16C5x_SPI  #(
360
                .pCR_Default(pSPI_CR_Default),
361
                .pTF_Depth(pSPI_TF_Depth),
362
                .pRF_Depth(pSPI_RF_Depth),
363
                .pTF_Init(pSPI_TF_Init),
364
                .pRF_Init(pSPI_RF_Init)
365
            ) SPI (
366
                .Rst(Rst),
367
                .Clk(Clk),
368
 
369
                .ClkEn(ClkEn),
370
 
371
                .WE_CR(WE_TRISC),
372
                .WE_TF(WE_PORTC),
373
                .RE_RF(RE_PORTC),
374
                .DI(IO_DO),
375
                .DO(SPI_DO),
376
 
377
                .CS(CS[1:0]),
378
                .SCK(SCK),
379
                .MOSI(SPI_MOSI),
380
                .MISO(SPI_MISO),
381
 
382
                .SS(SS),
383
                .TF_FF(TF_FF),
384
                .TF_EF(TF_EF),
385
                .RF_FF(RF_FF),
386
                .RF_EF(RF_EF)
387
            );
388
 
389
assign nCS[0] = ~CS[0];
390
assign nCS[1] = ~CS[1];
391
assign nCS[2] = 1'b1;
392
 
393
assign MOSI = SPI_MOSI;
394
 
395
//  Instantiate Global Clock Buffer for driving the SPI Clock to internal nodes
396
 
397
BUFG    BUF1 (
398
            .I(SCK),
399
            .O(SPI_SCK)
400
        );
401
 
402
//  Instantiate UART with an NXP LPC213x/LPC214x SSP-compatible interface
403
 
404
assign CTS = ~nCTS;
405
 
406
M16C5x_UART #(
407
                .pPS_Default(pPS_Default),
408
                .pDiv_Default(pDiv_Default),
409
                .pRTOChrDlyCnt(pRTOChrDlyCnt),
410
                .pTF_Depth(pUART_TF_Depth),
411
                .pRF_Depth(pUART_RF_Depth),
412
                .pTF_Init(pUART_TF_Init),
413
                .pRF_Init(pUART_RF_Init)
414
            ) UART (
415
                .Rst(Rst),
416
 
417
                .Clk_UART(Clk_UART),
418
 
419
                .SSEL(CS[1]),
420
                .SCK(SPI_SCK),
421
                .MOSI(SPI_MOSI),
422
                .MISO(SSP_MISO),
423
 
424
                .TxD(TD),
425
                .RTS(RTS),
426
                .RxD(RD),
427
                .CTS(CTS),
428
 
429
                .DE(DE),
430
 
431
                .IRQ(IRQ)
432
            );
433
 
434
assign nRTS = ~RTS;
435
 
436
endmodule

powered by: WebSVN 2.1.0

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