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

Subversion Repositories risc16f84

[/] [risc16f84/] [trunk/] [risc16f84_clk2x.v] - Blame information for rev 12

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 12 jclaytons
//---------------------------------------------------------------------------
2
// RISC 16F84 "clk2x" core
3
//
4
// This file is part of the "risc_16F84" project.
5
// http://www.opencores.org/cores/risc_16F84
6
// 
7
//
8
// Description: See description below (which suffices for IP core
9
//                                     specification document.)
10
//
11
// Copyright (C) 1999 Sumio Morioka (original VHDL design version)
12
// Copyright (C) 2001 John Clayton and OPENCORES.ORG (this Verilog version)
13
//
14
// NOTE: This source code is free for educational/hobby use only.  It cannot
15
// be used for commercial purposes without the consent of Microchip
16
// Technology incorporated.
17
//
18
// This source file may be used and distributed without restriction provided
19
// that this copyright statement is not removed from the file and that any
20
// derivative work contains the original copyright notice and the associated
21
// disclaimer.
22
//
23
// This source file is free software; you can redistribute it and/or modify
24
// it under the terms of the GNU Lesser General Public License as published
25
// by the Free Software Foundation;  either version 2.1 of the License, or
26
// (at your option) any later version.
27
//
28
// This source is distributed in the hope that it will be useful, but WITHOUT
29
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 
30
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public
31
// License for more details.
32
//
33
// You should have received a copy of the GNU Lesser General Public License
34
// along with this source.
35
// If not, download it from http://www.opencores.org/lgpl.shtml
36
//
37
//---------------------------------------------------------------------------
38
//
39
// Author: John Clayton
40
// Date  : January 29, 2002
41
//
42
// (NOTE: Date formatted as day/month/year.)
43
// Update: 29/01/02 copied this file from memory_sizer.v (pared down).
44
//                  Translated the module and signal declarations.
45
//                  Transformed the instruction wires to lowercase.
46
//                  Transformed the addressing wires to lowercase.
47
// Update: 31/01/02 Translated the instruction decoder.
48
// Update:  5/02/02 Determined that stack is simply a circular buffer of
49
//                  8 locations, 13 bits per location.  Started translating
50
//                  "main_efsm" process.  Added all code from piccore.vhd
51
//                  into this file for eventual translation.  Concluded that
52
//                  "stack_full_node" is not needed.
53
// Update:  6/02/02 Translated the "ram_i_node" if/else precedural assignment.
54
// Update:  7/02/02 Changed all := to <=, changed all '0' to 0 and '1' to 1.
55
//                  Replaced all " downto " with ":".
56
//                  Finished translating QRESET state.
57
// Update: 20/02/02 Replaced all instances of Qreset with QRESET_PP.  Also
58
//                  replaced other state designations with their new names.
59
//                  Finished translating Q1, Q2 states.
60
// Update: 22/02/02 Translated section 2-4-1-1 (aluout register)
61
// Update: 27/02/02 Replaced all "or" with "||" in if statements
62
//                  Replaced all "and" with "&&" in if statements.
63
//                  Replaced all "not" with "~" in if statements.
64
//                  Finished translating Q3,Q4 states.
65
//                  Translated output signal assignments at end of code.
66
//                  Translated interrupt trigger processes.
67
// Update: 28/02/02 Finished translation of WDT and TMR0 prescaler.
68
//                  Trimmed line length to 80 characters throughout.
69
//                  Prepared to attempt initial syntax checking.
70
//                  Cleaned up some naming conventions, and verified that
71
//                  all I/O pins have _i or _o appended in the body of the
72
//                  code.
73
// Update: 03/04/02 Changed "progdata_i" to "prog_dat_i" Also changed
74
//                  "progadr_o" to "prog_adr_o"
75
// Update: 04/04/02 Created new file "risc16f84_lite.v"  This file is reduced
76
//                  and simplified from the original "risc16f84.v" file.
77
//                  Specifically, I am removing EEPROM support, and 
78
//                  consolidating porta and portb I/O pins so that they
79
//                  are bidirectional.
80
// Update: 04/04/02 Created a new file "risc16f84_small.v"  This file is
81
//                  further reduced and simplified from "risc16f84_lite.v"
82
//                  Specifically, I am removing the prescaler, TMR0 and WDT.
83
//                  Also, I am removing support for portb interrupts, leaving
84
//                  only rb0/int as an interrupt source.  This pin will be
85
//                  the only way to wake up from the SLEEP instruction...
86
//                  Obviously, the CLEARWDT instruction will no longer do
87
//                  anything.
88
// Update: 05/04/02 Removed the "powerdown_o", "startclk_o" and "clk_o" pins
89
//                  from the small design.  Also removed "rbpu_o", so if you
90
//                  want pullups, you have to add them explicitly in the
91
//                  constraints file, and option_reg[7] doesn't control them.
92
// Update: 08/04/02 Decided to modify "risc16f84_small.v" in order to try for
93
//                  more performance (only 2 states per instruction!)
94
//                  The new file is called "risc16f84_clk2x.v"  The resulting
95
//                  code was synthesized, but not tested yet.
96
// Update: 11/04/02 Decided to remove porta and portb from this unit, and add
97
//                  instead an auxiliary bus, which is intended to allow I/O
98
//                  using an indirect approach, similar to using the FSR.
99
//                  However, the aux_adr_o is 16 bits wide, so that larger
100
//                  RAM may be accessed indirectly by the processor... The use
101
//                  of FSR for this purpose proved undesirable, since any new
102
//                  page of RAM contains "holes" to accomodate the registers
103
//                  in the first 12 locations (including FSR!) so that large
104
//                  contiguous blocks of memory could not be accessed in an
105
//                  effective way.  This auxiliary bus solves that problem.
106
//                  Since this processor is implemented inside of an FPGA,
107
//                  and it is not a goal to maintain compatibility with
108
//                  existing libraries of code, there is no need to maintain
109
//                  porta and portb in the hardware.
110
//                  The aux_adr_lo and aux_adr_hi registers are located at
111
//                  88h and 89h, and the aux_dat_io location is decoded at
112
//                  08h.
113
//                  Also, changed to using "ram_we_o" instead of "readram_o"
114
//                  and "writeram_o"
115
// Update: 16/04/02 Added clock enable signal, for processor single stepping.
116
//                  "aux_dat_io" is only driven when "clk_en_i" is high...
117
// Update: 17/04/02 Removed "reset_condition" and moved "inc_pc_node" out of
118
//                  the clocking area, making it non-registered.  In fact, I
119
//                  moved everything other than the state machine out of the
120
//                  clocked logic section.  Changed "aluout_reg" to "aluout"
121
//                  since it is no longer registered.
122
// Update: 26/04/02 Fixed bug in aluout logic.  The AND and OR functions were
123
//                  coded with logical AND/OR instead of bitwise AND/OR!
124
// Update: 26/04/02 Changed location of aux_adr_lo and aux_adr_hi registers
125
//                  to 05h and 06h, respectively.  This was done to save
126
//                  code space because when using the aux data bus, no bank
127
//                  switching is necessary since they will now reside in the
128
//                  same bank.
129
// Update: 01/05/02 Fixed another bug -- the rrf and rlf instructions were
130
//                  coded incorrectly.
131
// Update: 03/05/02 Fixed another bug -- the carry bit was incorrect (the
132
//                  problem was discovered while performing SUBWF X,W where
133
//                  W contained 0 and X contained 1. (1-0).  The logic for
134
//                  the carry bit appears to have been incorrect even in
135
//                  the original VHDL code by Sumio Morioka.
136
// Update: 11/18/02 Fixed bug in PCL addressing mode (near line 791)
137
//                  Removed parameters associated with WDT.
138
// Update: 11/25/02 Re-wrote much of the main FSM.  Attempted to generate logic
139
//                  to recognize the falling edge of an interrupt, and was
140
//                  unsuccessful (not simulation, actual hardware tests.)
141
//                  Realized that falling edge interrupt can be equivalent to
142
//                  rising edge interrupt with a NOT gate on the signal.  Since
143
//                  NOTs are practically free inside of an FPGA, decided to
144
//                  abandon the negative edge aspect of recognizing interrupts.
145
//                  Therefore, removed the "option_reg" since it is no longer
146
//                  needed in this design.
147
// Update: 08/08/03 Fixed a mathematical error, which was introduced by the bug
148
//                  fix of 03/05/02.  The fix of 03/05/02 correctly generated the
149
//                  C bit for subtraction of zero, but unfortunately it introduced
150
//                  an error such that all subtraction results were off by 1.
151
//                  Obviously, this was unacceptable, and I think it has been fixed
152
//                  by the new signals "c_subtract_zero" and "c_dig_subtract_zero"
153
//
154
// Update: 23 june 2014 (Stanislav Corboot)
155
//
156
//                  - Bug fixed: interrupt handler executed one and the same
157
//                    instruction twice - before interruption and after it.
158
//                  - Bug fixed: incorrect behavior of the zero flag after LITERAL
159
//                    instructions (xxxLW) if operand was equal 0x03 and if result
160
//                    equal zero.
161
//                    Example:
162
//                      movlw 0x03
163
//                      xorlw 0x03 -> WREG=0x00, ZF=0 - ERROR!
164
//                      or
165
//                      movlw 0xFD
166
//                      addlw 0x03 -> WREG=0x00, ZF=0 - ERROR!
167
//                      or
168
//                      movlw 0x00
169
//                      andlw 0x03 -> WREG=0x00, ZF=0 - ERROR!
170
//                      etc.
171
//
172
// Description
173
//---------------------------------------------------------------------------
174
// This logic module implements a small RISC microcontroller, with functions
175
// and instruction set very similar to those of the Microchip 16F84 chip.
176
// This work is a translation (from VHDL to Verilog) of the "CQPIC" design
177
// published in 1999 by Sumio Morioka of Japan, and published in the December
178
// 1999 issue of "Transistor Gijutsu Magazine."  The translation was performed
179
// by John Clayton, without the use of any translation tools.
180
//
181
// Original version used as basis for translation:  CQPIC version 1.00b
182
//                                                  (December 10, 2000)
183
//
184
// Further revisions and re-writing have been completed on this code by John
185
// Clayton.  The interrupt mechanism has been completely re-done, and the
186
// way in which the program counter is generated is expressed in a new way.
187
//
188
// In the comments, a "cycle" is defined as a processor cycle of 2 states.
189
// Thus, passing through states Q2_PP and Q4_PP completes one cycle.
190
// The numbers "1-3" and so forth are left from the comments in the original
191
// source code used as the basis of the translation.
192
//---------------------------------------------------------------------------
193
 
194
`define STATEBIT_SIZE 2      // Size of state machine register (bits)
195
 
196
 
197
module risc16f84_clk2x (
198
  prog_dat_i,           // [13:0] ROM read data
199
  prog_adr_o,           // [12:0] ROM address
200
  ram_dat_i,            // [7:0] RAM read data
201
  ram_dat_o,            // [7:0] RAM write data
202
  ram_adr_o,            // [8:0] RAM address; ram_adr[8:7] indicates RAM-BANK
203
  ram_we_o,             // RAM write strobe (H active)
204
  aux_adr_o,            // [15:0] Auxiliary address bus
205
  aux_dat_io,           // [7:0] Auxiliary data bus (tri-state bidirectional)
206
  aux_we_o,             // Auxiliary write strobe (H active)
207
  int0_i,               // PORT-B(0) INT
208
  reset_i,              // Power-on reset (H active)
209
  clk_en_i,             // Clock enable for all clocked logic
210
  clk_i                 // Clock input
211
);
212
 
213
 
214
// You can change the following parameters as you would like
215
parameter STACK_SIZE_PP      = 8;   // Size of PC stack
216
parameter LOG2_STACK_SIZE_PP = 3;   // Log_2(stack_size)
217
 
218
// State definitions for state machine, provided as parameters to allow
219
// for redefinition of state values by the instantiator if desired.
220
parameter Q2_PP     = 2'b00;  // state Q2
221
parameter Q4_PP     = 2'b01;  // state Q4
222
parameter QINT_PP   = 2'b10;  // interrupt state (substitute for Q4)
223
parameter QSLEEP_PP = 2'b11;  // sleep state
224
 
225
 
226
// I/O declarations
227
 
228
       // program ROM data bus/address bus
229
input  [13:0] prog_dat_i;   // ROM read data
230
output [12:0] prog_adr_o;   // ROM address
231
 
232
       // data RAM data bus/address bus/control signals
233
input  [7:0] ram_dat_i;     // RAM read data
234
output [7:0] ram_dat_o;     // RAM write data
235
output [8:0] ram_adr_o;     // RAM address; ram_adr[8:7] indicates RAM-BANK
236
output ram_we_o;            // RAM write strobe (H active)
237
 
238
       // auxiliary data bus/address bus/control signals
239
output [15:0] aux_adr_o;    // AUX address bus
240
inout  [7:0]  aux_dat_io;   // AUX data bus
241
output aux_we_o;            // AUX write strobe (H active)
242
 
243
       // interrupt input
244
input  int0_i;              // INT
245
 
246
       // CPU reset
247
input  reset_i;             // Power-on reset (H active)
248
 
249
       // CPU clock
250
input  clk_en_i;            // Clock enable input
251
input  clk_i;               // Clock input
252
 
253
 
254
// Internal signal declarations
255
 
256
     // User registers
257
reg  [7:0] w_reg;            // W
258
reg  [12:0] pc_reg;          // PCH/PCL -- Address currently being fetched
259
reg  [12:0] old_pc_reg;      // Address fetched previous to this one.
260
reg  [7:0] status_reg;       // STATUS
261
reg  [7:0] fsr_reg;          // FSR
262
reg  [4:0] pclath_reg;       // PCLATH
263
reg  [7:0] intcon_reg;       // INTCON
264
reg  [7:0] aux_adr_hi_reg;   // AUX address high byte
265
reg  [7:0] aux_adr_lo_reg;   // AUX address low byte
266
 
267
     // Internal registers for controlling instruction execution
268
reg  [13:0] inst_reg;        // Holds fetched op-code/operand
269
reg  [7:0]  aluinp1_reg;     // data source (1 of 2)
270
reg  [7:0]  aluinp2_reg;     // data source (2 of 2)
271
reg  exec_stall_reg;         // if H (i.e. after GOTO etc), stall execution.
272
 
273
     // Stack
274
                             // stack (array of data-registers)
275
reg  [12:0] stack_reg [STACK_SIZE_PP-1:0];
276
                             // stack pointer
277
reg  [LOG2_STACK_SIZE_PP-1:0] stack_pnt_reg;
278
wire [12:0] stack_top;       // More compatible with sensitivity list than
279
                             // "stack_reg[stack_pnt_reg]"
280
 
281
     // Interrupt registers/nodes
282
wire int_condition;          // Indicates that an interrupt should be recognized
283
wire intrise;                // High indicates edge was detected
284
reg  intrise_reg;            // detect positive edge of PORT-B inputs
285
     // Synchronizer for interrupt
286
reg  inte_sync_reg;
287
 
288
     // State register
289
reg  [`STATEBIT_SIZE-1:0] state_reg;
290
reg  [`STATEBIT_SIZE-1:0] next_state_node;
291
 
292
     // Result of decoding instruction -- only 1 is active at a time
293
wire inst_addlw;
294
wire inst_addwf;
295
wire inst_andlw;
296
wire inst_andwf;
297
wire inst_bcf;
298
wire inst_bsf;
299
wire inst_btfsc;
300
wire inst_btfss;
301
wire inst_call;
302
wire inst_clrf;
303
wire inst_clrw;
304
wire inst_comf;
305
wire inst_decf;
306
wire inst_decfsz;
307
wire inst_goto;
308
wire inst_incf;
309
wire inst_incfsz;
310
wire inst_iorlw;
311
wire inst_iorwf;
312
wire inst_movlw;
313
wire inst_movf;
314
wire inst_movwf;
315
wire inst_retfie;
316
wire inst_retlw;
317
wire inst_ret;
318
wire inst_rlf;
319
wire inst_rrf;
320
wire inst_sleep;
321
wire inst_sublw;
322
wire inst_subwf;
323
wire inst_swapf;
324
wire inst_xorlw;
325
wire inst_xorwf;
326
 
327
     // Result of calculating RAM access address
328
wire [8:0] ram_adr_node;      // RAM access address
329
 
330
     // These wires indicate accesses to special registers... 
331
     // Only 1 is active at a time.
332
wire addr_pcl;
333
wire addr_stat;
334
wire addr_fsr;
335
wire addr_pclath;
336
wire addr_intcon;
337
wire addr_aux_adr_lo;
338
wire addr_aux_adr_hi;
339
wire addr_aux_dat;
340
wire addr_sram;
341
 
342
     // Other output registers (for removing hazards)
343
reg  ram_we_reg;          // data-sram write strobe
344
reg  aux_we_reg;          // AUX write strobe
345
 
346
 
347
     // Signals used in "main_efsm" procedure
348
     // (Intermediate nodes used for resource sharing.)
349
wire [7:0]  ram_i_node;    // result of reading RAM/Special registers
350
wire [7:0]  mask_node;     // bit mask for logical operations
351
wire [8:0]  add_node;      // result of 8bit addition
352
wire [4:0]  addlow_node;   // result of low-4bit addition
353
wire aluout_zero_node;    // H if ALUOUT = 0
354
 
355
reg  [12:0] next_pc_node;  // value of next PC
356
reg  [7:0] aluout;        // result of calculation
357
reg  writew_node;         // H if destination is W register
358
reg  writeram_node;       // H if destination is RAM/Special registers
359
reg  c_subtract_zero;     // High for special case of C bit, when subtracting zero
360
reg  c_dig_subtract_zero; // High for special case of C bit, when subtracting zero
361
 
362
wire next_exec_stall;
363
 
364
//--------------------------------------------------------------------------
365
// Instantiations
366
//--------------------------------------------------------------------------
367
 
368
 
369
//--------------------------------------------------------------------------
370
// Functions & Tasks
371
//--------------------------------------------------------------------------
372
 
373
//--------------------------------------------------------------------------
374
// Module code
375
//--------------------------------------------------------------------------
376
 
377
// This represents the instruction fetch from program memory.
378
// inst_reg[13:0] stores the instruction.  This happens at the end of Q4.
379
// So the memory access time is one processor cycle (2 clocks!) minus
380
// the setup-time of this register, and minus the delay to drive the 
381
// address out onto the prog_adr_o bus.
382
always @(posedge clk_i)
383
begin
384
  if (reset_i) inst_reg <= 0;
385
  else if (clk_en_i && (state_reg == Q4_PP)) inst_reg <= prog_dat_i;
386
end
387
 
388
// NOTE: There is an extra "15th" bit of inst_reg, which represents an
389
// interrupt execution cycle.  This is included in inst_reg so that when
390
// an interrupt instruction is executing, it effectively "pre-empts" the
391
// other instructions.
392
// The fifteenth bit, inst_reg[14], is set by the interrupt logic.
393
 
394
// Decode OPcode    (see pp.54 of PIC16F84 data sheet)
395
// only 1 signal of the following signals will be '1'
396
assign inst_call    = (inst_reg[13:11] ==  3'b100           );
397
assign inst_goto    = (inst_reg[13:11] ==  3'b101           );
398
assign inst_bcf     = (inst_reg[13:10] ==  4'b0100          );
399
assign inst_bsf     = (inst_reg[13:10] ==  4'b0101          );
400
assign inst_btfsc   = (inst_reg[13:10] ==  4'b0110          );
401
assign inst_btfss   = (inst_reg[13:10] ==  4'b0111          );
402
assign inst_movlw   = (inst_reg[13:10] ==  4'b1100          );
403
assign inst_retlw   = (inst_reg[13:10] ==  4'b1101          );
404
assign inst_sublw   = (inst_reg[13:9]  ==  5'b11110         );
405
assign inst_addlw   = (inst_reg[13:9]  ==  5'b11111         );
406
assign inst_iorlw   = (inst_reg[13:8]  ==  6'b111000        );
407
assign inst_andlw   = (inst_reg[13:8]  ==  6'b111001        );
408
assign inst_xorlw   = (inst_reg[13:8]  ==  6'b111010        );
409
assign inst_subwf   = (inst_reg[13:8]  ==  6'b000010        );
410
assign inst_decf    = (inst_reg[13:8]  ==  6'b000011        );
411
assign inst_iorwf   = (inst_reg[13:8]  ==  6'b000100        );
412
assign inst_andwf   = (inst_reg[13:8]  ==  6'b000101        );
413
assign inst_xorwf   = (inst_reg[13:8]  ==  6'b000110        );
414
assign inst_addwf   = (inst_reg[13:8]  ==  6'b000111        );
415
assign inst_movf    = (inst_reg[13:8]  ==  6'b001000        );
416
assign inst_comf    = (inst_reg[13:8]  ==  6'b001001        );
417
assign inst_incf    = (inst_reg[13:8]  ==  6'b001010        );
418
assign inst_decfsz  = (inst_reg[13:8]  ==  6'b001011        );
419
assign inst_rrf     = (inst_reg[13:8]  ==  6'b001100        );
420
assign inst_rlf     = (inst_reg[13:8]  ==  6'b001101        );
421
assign inst_swapf   = (inst_reg[13:8]  ==  6'b001110        );
422
assign inst_incfsz  = (inst_reg[13:8]  ==  6'b001111        );
423
assign inst_movwf   = (inst_reg[13:7]  ==  7'b0000001       );
424
assign inst_clrw    = (inst_reg[13:7]  ==  7'b0000010       );
425
assign inst_clrf    = (inst_reg[13:7]  ==  7'b0000011       );
426
assign inst_ret     = (inst_reg[13:0]  == 14'b00000000001000);
427
assign inst_retfie  = (inst_reg[13:0]  == 14'b00000000001001);
428
assign inst_sleep   = (inst_reg[13:0]  == 14'b00000001100011);
429
 
430
 
431
// Calculate RAM access address (see pp.19 of PIC16F84 data sheet)
432
 
433
    // if "d"=0, indirect addressing is used, so RAM address is BANK+FSR
434
    // otherwise, RAM address is BANK+"d"
435
    // (see pp.19 of PIC16F84 data sheet)
436
assign ram_adr_node = (inst_reg[6:0]==0)?{status_reg[7],fsr_reg[7:0]}:
437
                               {status_reg[6:5],inst_reg[6:0]};
438
 
439
    // check if this is an access to external RAM or not
440
assign addr_sram   = (ram_adr_node[6:0] > 7'b0001011); //0CH-7FH,8CH-FFH
441
 
442
    // check if this is an access to special register or not
443
    // only 1 signal of the following signals will be '1'
444
assign addr_pcl     = (ram_adr_node[6:0] ==  7'b0000010);    // 02H, 82H
445
assign addr_stat    = (ram_adr_node[6:0] ==  7'b0000011);    // 03H, 83H
446
assign addr_fsr     = (ram_adr_node[6:0] ==  7'b0000100);    // 04H, 84H
447
assign addr_aux_dat = (ram_adr_node[7:0] == 8'b00001000);    // 08H
448
assign addr_pclath  = (ram_adr_node[6:0] ==  7'b0001010);    // 0AH, 8AH
449
assign addr_intcon  = (ram_adr_node[6:0] ==  7'b0001011);    // 0BH, 8BH
450
assign addr_aux_adr_lo = (ram_adr_node[7:0] == 8'b00000101); // 05H
451
assign addr_aux_adr_hi = (ram_adr_node[7:0] == 8'b00000110); // 06H
452
 
453
// construct bit-mask for logical operations and bit tests
454
assign mask_node = 1 << inst_reg[9:7];
455
 
456
// Create the exec_stall signal, based on the contents of the currently
457
// executing instruction (inst_reg).  next_exec_stall reflects the state
458
// to assign to exec_stall following the conclusion of the next Q4 state.
459
// All of these instructions cause an execution stall in the next cycle
460
// because they modify the program counter, and a new value is presented
461
// for fetching during the stall cycle, during which time no instruction
462
// should be executed.
463
//
464
// The conditional instructions are given along with their conditions for
465
// execution.  If the conditions are not met, there is no stall and nothing
466
// to execute.
467
assign next_exec_stall = (
468
                             inst_goto
469
                          || inst_call
470
                          || inst_ret
471
                          || inst_retlw
472
                          || inst_retfie
473
                          || (
474
                              (inst_btfsc || inst_decfsz || inst_incfsz)
475
                              && aluout_zero_node
476
                              )
477
                          || (inst_btfss && ~aluout_zero_node)
478
                          || (addr_pcl && writeram_node)
479
                          );
480
always @(posedge clk_i)
481
begin
482
  if (reset_i) exec_stall_reg <= 0;
483
  else if (clk_en_i && (state_reg == QINT_PP)) exec_stall_reg <= 1;
484
  else if (clk_en_i && (state_reg == Q4_PP))
485
    exec_stall_reg <= (next_exec_stall && ~exec_stall_reg);
486
    // exec stall should never be generated during a stall cycle, because
487
    // a stall cycle doesn't execute anything...
488
end
489
 
490
assign stack_top = stack_reg[stack_pnt_reg];
491
// Formulate the next pc_reg value (the program counter.)
492
// During stall cycles, the pc is simply incremented...
493
always @(
494
            pc_reg
495
         or pclath_reg
496
         or aluout
497
         or stack_pnt_reg
498
         or stack_top
499
         or inst_ret
500
         or inst_retlw
501
         or inst_retfie
502
         or inst_goto
503
         or inst_call
504
         or inst_reg
505
         or writeram_node
506
         or addr_pcl
507
         or exec_stall_reg
508
         )
509
begin
510
  if (~exec_stall_reg &&(inst_ret || inst_retlw || inst_retfie))
511
    next_pc_node <= stack_top;
512
  else if (~exec_stall_reg &&(inst_goto || inst_call))
513
    next_pc_node <= {pclath_reg[4:3],inst_reg[10:0]};
514
  else if (~exec_stall_reg && (writeram_node && addr_pcl))
515
    // PCL is data-destination, but update the entire PC.
516
    next_pc_node <= {pclath_reg[4:0],aluout};
517
  else
518
    next_pc_node <= pc_reg + 1;
519
end
520
 
521
// Set the program counter
522
// If the sleep instruction is executing, then the PC is not allowed to be
523
// updated, since the processor will "freeze" and the instruction being fetched
524
// during the sleep instruction must be executed upon wakeup interrupt.
525
// Obviously, if the PC were to change at the end of the sleep instruction, then
526
// a different (incorrect) address would be fetched during the sleep time.
527
always @(posedge clk_i)
528
begin
529
  if (reset_i) begin
530
    pc_reg <= 0;
531
    old_pc_reg <= 0;
532
  end
533
  else if (clk_en_i && (state_reg == QINT_PP))
534
  begin
535
    old_pc_reg <= pc_reg;
536
    pc_reg <= 4;
537
  end
538
  else if (clk_en_i && ~inst_sleep && (state_reg == Q4_PP))
539
  begin
540
    old_pc_reg <= pc_reg;
541
    pc_reg <= next_pc_node;
542
  end
543
end
544
 
545
// 1. Intermediate nodes for resource sharing
546
 
547
// Tri-state drivers instead of a huge selector...  It produces smaller
548
// results, and runs faster.
549
assign ram_i_node = (addr_sram)       ?ram_dat_i:8'bZ;
550
assign ram_i_node = (addr_pcl)        ?pc_reg[7:0]:8'bZ;
551
assign ram_i_node = (addr_stat)       ?status_reg:8'bZ;
552
assign ram_i_node = (addr_fsr)        ?fsr_reg:8'bZ;
553
assign ram_i_node = (addr_aux_dat)    ?aux_dat_io:8'bZ;
554
assign ram_i_node = (addr_pclath)     ?{3'b0,pclath_reg}:8'bZ;
555
assign ram_i_node = (addr_intcon)     ?intcon_reg:8'bZ;
556
assign ram_i_node = (addr_aux_adr_lo) ?aux_adr_lo_reg:8'bZ;
557
assign ram_i_node = (addr_aux_adr_hi) ?aux_adr_hi_reg:8'bZ;
558
 
559
// 1-3. Adder (ALU)
560
// full 8bit-addition, with carry in/out.
561
// Note that "temp" and "dtemp" are intended to be thrown away.
562
// Also, addlow_node[3:0] are thrown away.
563
// Even though they are assigned, they should never be used.
564
assign add_node     =    {1'b0,aluinp1_reg}
565
                       + {1'b0,aluinp2_reg};
566
// lower 4bit-addition
567
assign addlow_node =    {1'b0,aluinp1_reg[3:0]}
568
                      + {1'b0,aluinp2_reg[3:0]};
569
 
570
// 1-4. Test if aluout = 0
571
assign aluout_zero_node = (aluout == 0)?1:0;
572
 
573
// 1-5. Determine destination
574
always @(
575
            inst_reg
576
         or inst_movwf
577
         or inst_bcf
578
         or inst_bsf
579
         or inst_clrf
580
         or inst_movlw
581
         or inst_addlw
582
         or inst_sublw
583
         or inst_andlw
584
         or inst_iorlw
585
         or inst_xorlw
586
         or inst_retlw
587
         or inst_clrw
588
         or inst_movf
589
         or inst_swapf
590
         or inst_addwf
591
         or inst_subwf
592
         or inst_andwf
593
         or inst_iorwf
594
         or inst_xorwf
595
         or inst_decf
596
         or inst_incf
597
         or inst_rlf
598
         or inst_rrf
599
         or inst_decfsz
600
         or inst_incfsz
601
         or inst_comf
602
         )
603
begin
604
  if (inst_movwf || inst_bcf || inst_bsf || inst_clrf)
605
  begin
606
    writew_node     <= 0;
607
    writeram_node   <= 1;
608
  end
609
  else if (   inst_movlw || inst_addlw || inst_sublw || inst_andlw
610
           || inst_iorlw || inst_xorlw || inst_retlw || inst_clrw)
611
  begin
612
    writew_node     <= 1;
613
    writeram_node   <= 0;
614
  end
615
  else if (   inst_movf   || inst_swapf || inst_addwf || inst_subwf
616
           || inst_andwf  || inst_iorwf || inst_xorwf || inst_decf
617
           || inst_incf   || inst_rlf   || inst_rrf   || inst_decfsz
618
           || inst_incfsz || inst_comf)
619
  begin
620
    writew_node     <= ~inst_reg[7];  // ("d" field of fetched instruction)
621
    writeram_node   <=  inst_reg[7];  // ("d" field of fetched instruction)
622
  end
623
  else
624
  begin
625
    writew_node     <= 0;
626
    writeram_node   <= 0;
627
  end
628
end // End of determine destination logic
629
 
630
 
631
 
632
 
633
// 2-4-1. Calculation and store result into alu-output register
634
 
635
always @(
636
            add_node
637
         or aluinp1_reg
638
         or aluinp2_reg
639
         or status_reg
640
         or inst_reg
641
         or inst_movwf
642
         or inst_bcf
643
         or inst_bsf
644
         or inst_btfsc
645
         or inst_btfss
646
         or inst_clrf
647
         or inst_addlw
648
         or inst_sublw
649
         or inst_andlw
650
         or inst_iorlw
651
         or inst_xorlw
652
         or inst_retlw
653
         or inst_clrw
654
         or inst_swapf
655
         or inst_addwf
656
         or inst_subwf
657
         or inst_andwf
658
         or inst_iorwf
659
         or inst_xorwf
660
         or inst_decf
661
         or inst_incf
662
         or inst_rlf
663
         or inst_rrf
664
         or inst_decfsz
665
         or inst_incfsz
666
         or inst_comf
667
         )
668
begin
669
  // 2-4-1-1. Set aluout register
670
          // Rotate left
671
  if      (inst_rlf)
672
          aluout <= {aluinp1_reg[6:0],status_reg[0]};
673
          // Rotate right
674
  else if (inst_rrf)
675
          aluout  <= {status_reg[0],aluinp1_reg[7:1]};
676
          // Swap nibbles
677
  else if (inst_swapf)
678
          aluout <= {aluinp1_reg[3:0],aluinp1_reg[7:4]};
679
          // Logical inversion
680
  else if (inst_comf)
681
          aluout  <= ~aluinp1_reg;
682
          // Logical AND, bit clear/bit test
683
  else if (   inst_andlw || inst_andwf || inst_bcf || inst_btfsc
684
           || inst_btfss)
685
          aluout  <= (aluinp1_reg & aluinp2_reg);
686
          // Logical OR, bit set
687
  else if (inst_bsf || inst_iorlw || inst_iorwf)
688
          aluout  <= (aluinp1_reg | aluinp2_reg);
689
          // Logical XOR
690
  else if (inst_xorlw || inst_xorwf)
691
          aluout  <= (aluinp1_reg ^ aluinp2_reg);
692
          // Addition, Subtraction, Increment, Decrement
693
  else if (  inst_addlw || inst_addwf  || inst_sublw || inst_subwf
694
           || inst_decf || inst_decfsz || inst_incf  || inst_incfsz)
695
          aluout  <= add_node[7:0];
696
          // Pass through
697
  else aluout  <= aluinp1_reg;
698
end
699
 
700
 
701
// MAIN EFSM: description of register value changes in each clock cycle
702
always @(posedge clk_i)
703
begin
704
  // Assign reset (default) values of registers
705
  if (reset_i)
706
  begin
707
    status_reg[7:5] <= 3'b0;
708
    pclath_reg      <= 0;     // 0
709
    intcon_reg[7:1] <= 7'b0;
710
    aux_adr_lo_reg  <= 0;
711
    aux_adr_hi_reg  <= 0;
712
    ram_we_reg      <= 0;
713
    status_reg[4]   <= 1;     // /T0 = 1
714
    status_reg[3]   <= 1;     // /PD = 1
715
    stack_pnt_reg   <= 0;     // Reset stack pointer
716
  end  // End of reset assignments
717
  else if (~exec_stall_reg && clk_en_i)
718
  begin   // Execution ceases during a stall cycle.
719
    if (state_reg == Q2_PP) // 2-3. Q2 cycle
720
    begin
721
     if( ~int_condition ) // Bug fixed
722
     begin
723
      // 2-3-1. Read data-RAM and store values to alu-input regs
724
      // 2-3-1-1. Set aluinp1 register (source #1)
725
      if (   inst_movf   || inst_swapf || inst_addwf || inst_subwf
726
          || inst_andwf  || inst_iorwf || inst_xorwf || inst_decf
727
          || inst_incf   || inst_rlf   || inst_rrf   || inst_bcf
728
          || inst_bsf    || inst_btfsc || inst_btfss || inst_decfsz
729
          || inst_incfsz || inst_comf)
730
 
731
          aluinp1_reg <= ram_i_node;       // RAM/Special registers
732
      else
733
      if (   inst_movlw || inst_addlw || inst_sublw || inst_andlw
734
          || inst_iorlw || inst_xorlw || inst_retlw)
735
          aluinp1_reg <= inst_reg[7:0];    // Immediate value ("k")
736
      else
737
      if (   inst_clrf  || inst_clrw) aluinp1_reg <= 0; // 0
738
      else aluinp1_reg <= w_reg;                        // W register
739
 
740
      // 2-3-1-2. Set aluinp2 register (source #2)
741
      c_subtract_zero <= 0;       // default to non-special case
742
      c_dig_subtract_zero <= 0;   // default to non-special case
743
      if      (inst_decf || inst_decfsz) aluinp2_reg <= -1; // for decr.
744
      else if (inst_incf || inst_incfsz) aluinp2_reg <=  1; // for incr.
745
              // -1 * W register (for subtract)
746
      else if (inst_sublw || inst_subwf)
747
      begin
748
        aluinp2_reg <= ~w_reg + 1;
749
        c_subtract_zero <= (w_reg == 0);            // Indicate special case
750
        c_dig_subtract_zero <= (w_reg[3:0] == 0);   // Indicate special case
751
      end
752
            // operation of BCF: AND with inverted mask ("1..101..1")
753
            // mask for BCF: value of only one position is 0
754
      else if (inst_bcf) aluinp2_reg <= ~mask_node;
755
            // operation of BSF: OR with mask_node ("0..010..0")
756
            // operation of FSC and FSS: AND with mask_node, compare to 0
757
      else if (inst_btfsc || inst_btfss || inst_bsf)
758
                                  aluinp2_reg <= mask_node;
759
      else aluinp2_reg <= w_reg; // W register
760
 
761
      // 2-3-1-3. Set stack pointer register (pop stack)
762
      if (inst_ret || inst_retlw || inst_retfie)
763
           stack_pnt_reg   <= stack_pnt_reg - 1; // cycles 3,2,1,0,7,6...
764
 
765
      // 2-4-1-3. Set data-SRAM write enable (hazard-free)
766
      // Set the write enables depending on the destination.
767
      // (These have been implemented as registers to avoid glitches?
768
      // It is not known to me (John Clayton) whether any glitches would
769
      // really occur.  It might be possible to generate these signals
770
      // using combinational logic only, without using registers!
771
      ram_we_reg <= (writeram_node && addr_sram);
772
      aux_we_reg <= (writeram_node && addr_aux_dat);
773
     end  // Bug fixed
774
    end   // End of Q2 state
775
 
776
    //---------------------------------------------------------------------
777
 
778
    else if (state_reg == QINT_PP) // Interrupt execution (instead of Q4_PP)
779
    begin
780
        // PORT-B0 INT
781
        intcon_reg[1] <= 1;                     // set INTF
782
        intcon_reg[7] <= 0;                     // clear GIE
783
        stack_reg[stack_pnt_reg] <= old_pc_reg; // Push old PC
784
        stack_pnt_reg <= stack_pnt_reg + 1;     // increment stack pointer
785
        // The old PC is pushed, so that the pre-empted instruction can be
786
        // restarted later, when the retfie is executed.
787
    end
788
 
789
    //---------------------------------------------------------------------
790
 
791
    else if (state_reg == Q4_PP)   // Execution & writing of results.
792
    begin
793
 
794
      if (inst_call)
795
      begin
796
        stack_reg[stack_pnt_reg] <= pc_reg;     // Push current PC
797
        stack_pnt_reg <= stack_pnt_reg + 1;     // increment stack pointer
798
      end
799
 
800
      if (inst_retfie) // "return from interrupt" instruction
801
      begin
802
        intcon_reg[7] <= 1;                     // Set GIE
803
      end
804
 
805
      // 2-4-1-2. Set C flag and DC flag
806
      if (inst_addlw || inst_addwf || inst_sublw || inst_subwf)
807
      begin
808
        // c_dig_subtract_zero and c_subtract_zero are used to take care of the
809
        // special case when subtracting zero, where the carry bit should be 1
810
        // (meaning no borrow).  It is explicitly set by these signals during
811
        // that condition.  See 16F84 datasheet, page 8 for further information
812
        // about the C bit.
813
        status_reg[1]   <= addlow_node[4] || c_dig_subtract_zero;  // DC flag
814
        status_reg[0]   <= add_node[8] || c_subtract_zero;         // C flag
815
      end
816
      else if (inst_rlf) status_reg[0] <= aluinp1_reg[7];  // C flag
817
      else if (inst_rrf) status_reg[0] <= aluinp1_reg[0];  // C flag
818
 
819
      // 2-5-2. Store calculation result into destination, 
820
      // 2-5-2-1. Set W register
821
 
822
      if (writew_node) w_reg   <= aluout;    // write W reg
823
 
824
      // 2-5-2-2. Set data RAM/special registers,
825
      if (writeram_node)
826
      begin
827
        if (addr_stat)
828
        begin
829
          status_reg[7:5] <= aluout[7:5];      // write IRP,RP1,RP0
830
          // status(4),status(3)...unwritable, see below (/PD,/T0 part)
831
          status_reg[1:0] <= aluout[1:0];      // write DC,C
832
        end
833
        if (addr_fsr)         fsr_reg <= aluout;       // write FSR
834
        if (addr_pclath)   pclath_reg <= aluout[4:0];  // write PCLATH
835
        if (addr_intcon) intcon_reg <= aluout;         // write INTCON
836
        if (addr_aux_adr_lo) aux_adr_lo_reg <= aluout; // write AUX low
837
        if (addr_aux_adr_hi) aux_adr_hi_reg <= aluout; // write AUX high
838
      end
839
 
840
      // 2-5-2-3. Set/clear Z flag.
841
      if (addr_stat && !writew_node) status_reg[2] <= aluout[2]; // (dest. is Z flag) // Bug fixed
842
      else if (   inst_addlw || inst_addwf || inst_andlw || inst_andwf
843
               || inst_clrf  || inst_clrw  || inst_comf  || inst_decf
844
               || inst_incf  || inst_movf  || inst_sublw || inst_subwf
845
               || inst_xorlw || inst_xorwf || inst_iorlw || inst_iorwf )
846
              status_reg[2] <= aluout_zero_node; // Z=1 if result == 0
847
 
848
      // 2-5-3. Clear RAM write enables (hazard-free)
849
      ram_we_reg <= 0;
850
      aux_we_reg <= 0;
851
 
852
    end    // End of Q4 state
853
  end // End of "if (~exec_stall_reg)"    
854
end  // End of process
855
 
856
 
857
// Calculation of next processor state.
858
// (Not including reset conditions, which are covered by the clocked logic,
859
//  which also includes a "global clock enable."
860
always @(
861
            state_reg
862
         or inst_sleep
863
         or inte_sync_reg
864
         or exec_stall_reg
865
         or int_condition
866
         )
867
begin
868
  case (state_reg)
869
  Q2_PP     : if (int_condition) next_state_node <= QINT_PP;
870
              else next_state_node <= Q4_PP;
871
  Q4_PP     : if (~exec_stall_reg && inst_sleep) next_state_node <= QSLEEP_PP;
872
              else next_state_node <= Q2_PP;
873
  QINT_PP   : next_state_node <= Q2_PP;
874
  QSLEEP_PP : if (inte_sync_reg) next_state_node <= Q2_PP;
875
              else next_state_node <= QSLEEP_PP;
876
              // Default condition provided for convention and completeness
877
              // only.  Logically, all of the conditions are already covered.
878
  default   : next_state_node <= Q2_PP;
879
  endcase
880
end
881
 
882
 
883
// Clocked state transitions, based upon dataflow (non-clocked logic) in
884
// the previous always block.
885
always @(posedge clk_i)
886
begin
887
  if (reset_i) state_reg <= Q2_PP;
888
  else if (clk_en_i) state_reg <= next_state_node;
889
end  // End of process
890
 
891
 
892
// Detect external interrupt requests
893
// You can code multiple interrupts if you wish, or use the single interrupt
894
// provided and simply have the interrupt service routine (ISR) check to find
895
// out the source of the interrupt, by or-ing together all of the interrupt
896
// sources and providing a readable register of their values at the time
897
// the interrupt occurred.
898
//
899
// When an interrupt is recognized by the processor, this is signified by
900
// entering "QINT_PP," which is treated like an executable instruction.
901
// The interrupt instruction can only be executed when not in a stall condition.
902
// It simply "pre-empts" the instruction that would have been executed during
903
// that cycle.  Then, when retfie is executed, the pre-empted instruction is
904
// re-started (the stall cycle of the retfie is when the address of the 
905
// instruction being re-started is fetched.)
906
//
907
// I was unable to obtain correct operation for capturing the negative edge,
908
// so I am discarding it.  If one really needs to generate an interrupt on the
909
// falling edge, just use an inverted version of the signal (the inversion is
910
// often "free" inside of an FPGA anyhow.)
911
//
912
// Upon further testing, I discovered that even the rising edge "trigger" was not
913
// really truly an edge detection, it was more like a "set-reset" flip flop
914
// type of behavior.  Rather than mess around with it any more, I am implementing
915
// a clocked "poor man's rising edge detector."
916
// Capture the rising edge of the interrupt input...  This part is self clearing.
917
// It also means that the interrupt must last longer than one clock cycle in
918
// order to be properly recognized.  (It is "pseudo edge triggered", not a true
919
// rising edge trigger.)
920
// When the interrupt is recognized, inte_sync_reg is cleared.
921
 
922
 
923
always @(posedge clk_i)
924
begin
925
  if (clk_en_i) intrise_reg <= int0_i;
926
end // process
927
assign intrise = (int0_i && ~intrise_reg);
928
 
929
//  The inte_sync_reg signal is used for waking up from SLEEP.
930
//  (this flip flop is also a synchronizer to minimize the
931
//   possibility of metastability due to changes at the input
932
//   occurring at the same time as the processor clock edge...)
933
//  It might be possible to eliminate this step, and issue the interrupt
934
//  directly without this intermediate synchronizer flip-flop.
935
always @(posedge clk_i)
936
begin
937
  if (reset_i || (state_reg == QINT_PP)) inte_sync_reg <= 0;
938
  else if (clk_en_i && intrise && intcon_reg[4]) inte_sync_reg <= 1;
939
end
940
 
941
// Issue an interrupt when the interrupt is present.
942
// Also, do not issue an interrupt when there is a stall cycle coming!
943
assign int_condition = (inte_sync_reg && ~exec_stall_reg && intcon_reg[7]);
944
                               // Interrupt must be pending
945
                               // Next processor cycle must not be a stall
946
                               // GIE bit must be set to issue interrupt
947
 
948
// Circuit's output signals
949
assign prog_adr_o = pc_reg;        // program ROM address
950
assign ram_adr_o  = ram_adr_node;  // data RAM address
951
assign ram_dat_o  = aluout;        // data RAM write data
952
assign ram_we_o   = ram_we_reg;    // data RAM write enable
953
 
954
assign aux_adr_o  = {aux_adr_hi_reg,aux_adr_lo_reg};
955
assign aux_dat_io = (aux_we_reg && clk_en_i)?aluout:{8{1'bZ}};
956
assign aux_we_o   = aux_we_reg;
957
 
958
endmodule
959
 
960
 
961 2 jclaytons
//`undef STATEBIT_SIZE

powered by: WebSVN 2.1.0

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