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

Subversion Repositories risc16f84

[/] [risc16f84/] [web_uploads/] [risc16f84_clk2x.v] - Blame information for rev 9

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 9 root
//---------------------------------------------------------------------------
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
// Description
155
//---------------------------------------------------------------------------
156
// This logic module implements a small RISC microcontroller, with functions
157
// and instruction set very similar to those of the Microchip 16F84 chip.
158
// This work is a translation (from VHDL to Verilog) of the "CQPIC" design
159
// published in 1999 by Sumio Morioka of Japan, and published in the December
160
// 1999 issue of "Transistor Gijutsu Magazine."  The translation was performed
161
// by John Clayton, without the use of any translation tools.
162
//
163
// Original version used as basis for translation:  CQPIC version 1.00b
164
//                                                  (December 10, 2000)
165
//
166
// Further revisions and re-writing have been completed on this code by John
167
// Clayton.  The interrupt mechanism has been completely re-done, and the
168
// way in which the program counter is generated is expressed in a new way.
169
//
170
// In the comments, a "cycle" is defined as a processor cycle of 2 states.
171
// Thus, passing through states Q2_PP and Q4_PP completes one cycle.
172
// The numbers "1-3" and so forth are left from the comments in the original
173
// source code used as the basis of the translation.
174
//---------------------------------------------------------------------------
175
 
176
`define STATEBIT_SIZE 2      // Size of state machine register (bits)
177
 
178
 
179
module risc16f84_clk2x (
180
  prog_dat_i,           // [13:0] ROM read data
181
  prog_adr_o,           // [12:0] ROM address
182
  ram_dat_i,            // [7:0] RAM read data
183
  ram_dat_o,            // [7:0] RAM write data
184
  ram_adr_o,            // [8:0] RAM address; ram_adr[8:7] indicates RAM-BANK
185
  ram_we_o,             // RAM write strobe (H active)
186
  aux_adr_o,            // [15:0] Auxiliary address bus
187
  aux_dat_io,           // [7:0] Auxiliary data bus (tri-state bidirectional)
188
  aux_we_o,             // Auxiliary write strobe (H active)
189
  int0_i,               // PORT-B(0) INT
190
  reset_i,              // Power-on reset (H active)
191
  clk_en_i,             // Clock enable for all clocked logic
192
  clk_i                 // Clock input
193
);
194
 
195
 
196
// You can change the following parameters as you would like
197
parameter STACK_SIZE_PP      = 8;   // Size of PC stack
198
parameter LOG2_STACK_SIZE_PP = 3;   // Log_2(stack_size)
199
 
200
// State definitions for state machine, provided as parameters to allow
201
// for redefinition of state values by the instantiator if desired.
202
parameter Q2_PP     = 2'b00;  // state Q2
203
parameter Q4_PP     = 2'b01;  // state Q4
204
parameter QINT_PP   = 2'b10;  // interrupt state (substitute for Q4)
205
parameter QSLEEP_PP = 2'b11;  // sleep state
206
 
207
 
208
// I/O declarations
209
 
210
       // program ROM data bus/address bus
211
input  [13:0] prog_dat_i;   // ROM read data
212
output [12:0] prog_adr_o;   // ROM address
213
 
214
       // data RAM data bus/address bus/control signals
215
input  [7:0] ram_dat_i;     // RAM read data
216
output [7:0] ram_dat_o;     // RAM write data
217
output [8:0] ram_adr_o;     // RAM address; ram_adr[8:7] indicates RAM-BANK
218
output ram_we_o;            // RAM write strobe (H active)
219
 
220
       // auxiliary data bus/address bus/control signals
221
output [15:0] aux_adr_o;    // AUX address bus
222
inout  [7:0]  aux_dat_io;   // AUX data bus
223
output aux_we_o;            // AUX write strobe (H active)
224
 
225
       // interrupt input
226
input  int0_i;              // INT
227
 
228
       // CPU reset
229
input  reset_i;             // Power-on reset (H active)
230
 
231
       // CPU clock
232
input  clk_en_i;            // Clock enable input
233
input  clk_i;               // Clock input
234
 
235
 
236
// Internal signal declarations
237
 
238
     // User registers
239
reg  [7:0] w_reg;            // W
240
reg  [12:0] pc_reg;          // PCH/PCL -- Address currently being fetched
241
reg  [12:0] old_pc_reg;      // Address fetched previous to this one.
242
reg  [7:0] status_reg;       // STATUS
243
reg  [7:0] fsr_reg;          // FSR
244
reg  [4:0] pclath_reg;       // PCLATH
245
reg  [7:0] intcon_reg;       // INTCON
246
reg  [7:0] aux_adr_hi_reg;   // AUX address high byte
247
reg  [7:0] aux_adr_lo_reg;   // AUX address low byte
248
 
249
     // Internal registers for controlling instruction execution
250
reg  [13:0] inst_reg;        // Holds fetched op-code/operand
251
reg  [7:0]  aluinp1_reg;     // data source (1 of 2)
252
reg  [7:0]  aluinp2_reg;     // data source (2 of 2)
253
reg  exec_stall_reg;         // if H (i.e. after GOTO etc), stall execution.
254
 
255
     // Stack
256
                             // stack (array of data-registers)
257
reg  [12:0] stack_reg [STACK_SIZE_PP-1:0];
258
                             // stack pointer
259
reg  [LOG2_STACK_SIZE_PP-1:0] stack_pnt_reg;
260
wire [12:0] stack_top;       // More compatible with sensitivity list than
261
                             // "stack_reg[stack_pnt_reg]"
262
 
263
     // Interrupt registers/nodes
264
wire int_condition;          // Indicates that an interrupt should be recognized
265
wire intrise;                // High indicates edge was detected
266
reg  intrise_reg;            // detect positive edge of PORT-B inputs
267
     // Synchronizer for interrupt
268
reg  inte_sync_reg;
269
 
270
     // State register
271
reg  [`STATEBIT_SIZE-1:0] state_reg;
272
reg  [`STATEBIT_SIZE-1:0] next_state_node;
273
 
274
     // Result of decoding instruction -- only 1 is active at a time
275
wire inst_addlw;
276
wire inst_addwf;
277
wire inst_andlw;
278
wire inst_andwf;
279
wire inst_bcf;
280
wire inst_bsf;
281
wire inst_btfsc;
282
wire inst_btfss;
283
wire inst_call;
284
wire inst_clrf;
285
wire inst_clrw;
286
wire inst_comf;
287
wire inst_decf;
288
wire inst_decfsz;
289
wire inst_goto;
290
wire inst_incf;
291
wire inst_incfsz;
292
wire inst_iorlw;
293
wire inst_iorwf;
294
wire inst_movlw;
295
wire inst_movf;
296
wire inst_movwf;
297
wire inst_retfie;
298
wire inst_retlw;
299
wire inst_ret;
300
wire inst_rlf;
301
wire inst_rrf;
302
wire inst_sleep;
303
wire inst_sublw;
304
wire inst_subwf;
305
wire inst_swapf;
306
wire inst_xorlw;
307
wire inst_xorwf;
308
 
309
     // Result of calculating RAM access address
310
wire [8:0] ram_adr_node;      // RAM access address
311
 
312
     // These wires indicate accesses to special registers... 
313
     // Only 1 is active at a time.
314
wire addr_pcl;
315
wire addr_stat;
316
wire addr_fsr;
317
wire addr_pclath;
318
wire addr_intcon;
319
wire addr_aux_adr_lo;
320
wire addr_aux_adr_hi;
321
wire addr_aux_dat;
322
wire addr_sram;
323
 
324
     // Other output registers (for removing hazards)
325
reg  ram_we_reg;          // data-sram write strobe
326
reg  aux_we_reg;          // AUX write strobe
327
 
328
 
329
     // Signals used in "main_efsm" procedure
330
     // (Intermediate nodes used for resource sharing.)
331
wire [7:0]  ram_i_node;    // result of reading RAM/Special registers
332
wire [7:0]  mask_node;     // bit mask for logical operations
333
wire [8:0]  add_node;      // result of 8bit addition
334
wire [4:0]  addlow_node;   // result of low-4bit addition
335
wire aluout_zero_node;    // H if ALUOUT = 0
336
 
337
reg  [12:0] next_pc_node;  // value of next PC
338
reg  [7:0] aluout;        // result of calculation
339
reg  writew_node;         // H if destination is W register
340
reg  writeram_node;       // H if destination is RAM/Special registers
341
reg  c_subtract_zero;     // High for special case of C bit, when subtracting zero
342
reg  c_dig_subtract_zero; // High for special case of C bit, when subtracting zero
343
 
344
wire next_exec_stall;
345
 
346
//--------------------------------------------------------------------------
347
// Instantiations
348
//--------------------------------------------------------------------------
349
 
350
 
351
//--------------------------------------------------------------------------
352
// Functions & Tasks
353
//--------------------------------------------------------------------------
354
 
355
//--------------------------------------------------------------------------
356
// Module code
357
//--------------------------------------------------------------------------
358
 
359
// This represents the instruction fetch from program memory.
360
// inst_reg[13:0] stores the instruction.  This happens at the end of Q4.
361
// So the memory access time is one processor cycle (2 clocks!) minus
362
// the setup-time of this register, and minus the delay to drive the 
363
// address out onto the prog_adr_o bus.
364
always @(posedge clk_i)
365
begin
366
  if (reset_i) inst_reg <= 0;
367
  else if (clk_en_i && (state_reg == Q4_PP)) inst_reg <= prog_dat_i;
368
end
369
 
370
// NOTE: There is an extra "15th" bit of inst_reg, which represents an
371
// interrupt execution cycle.  This is included in inst_reg so that when
372
// an interrupt instruction is executing, it effectively "pre-empts" the
373
// other instructions.
374
// The fifteenth bit, inst_reg[14], is set by the interrupt logic.
375
 
376
// Decode OPcode    (see pp.54 of PIC16F84 data sheet)
377
// only 1 signal of the following signals will be '1'
378
assign inst_call    = (inst_reg[13:11] ==  3'b100           );
379
assign inst_goto    = (inst_reg[13:11] ==  3'b101           );
380
assign inst_bcf     = (inst_reg[13:10] ==  4'b0100          );
381
assign inst_bsf     = (inst_reg[13:10] ==  4'b0101          );
382
assign inst_btfsc   = (inst_reg[13:10] ==  4'b0110          );
383
assign inst_btfss   = (inst_reg[13:10] ==  4'b0111          );
384
assign inst_movlw   = (inst_reg[13:10] ==  4'b1100          );
385
assign inst_retlw   = (inst_reg[13:10] ==  4'b1101          );
386
assign inst_sublw   = (inst_reg[13:9]  ==  5'b11110         );
387
assign inst_addlw   = (inst_reg[13:9]  ==  5'b11111         );
388
assign inst_iorlw   = (inst_reg[13:8]  ==  6'b111000        );
389
assign inst_andlw   = (inst_reg[13:8]  ==  6'b111001        );
390
assign inst_xorlw   = (inst_reg[13:8]  ==  6'b111010        );
391
assign inst_subwf   = (inst_reg[13:8]  ==  6'b000010        );
392
assign inst_decf    = (inst_reg[13:8]  ==  6'b000011        );
393
assign inst_iorwf   = (inst_reg[13:8]  ==  6'b000100        );
394
assign inst_andwf   = (inst_reg[13:8]  ==  6'b000101        );
395
assign inst_xorwf   = (inst_reg[13:8]  ==  6'b000110        );
396
assign inst_addwf   = (inst_reg[13:8]  ==  6'b000111        );
397
assign inst_movf    = (inst_reg[13:8]  ==  6'b001000        );
398
assign inst_comf    = (inst_reg[13:8]  ==  6'b001001        );
399
assign inst_incf    = (inst_reg[13:8]  ==  6'b001010        );
400
assign inst_decfsz  = (inst_reg[13:8]  ==  6'b001011        );
401
assign inst_rrf     = (inst_reg[13:8]  ==  6'b001100        );
402
assign inst_rlf     = (inst_reg[13:8]  ==  6'b001101        );
403
assign inst_swapf   = (inst_reg[13:8]  ==  6'b001110        );
404
assign inst_incfsz  = (inst_reg[13:8]  ==  6'b001111        );
405
assign inst_movwf   = (inst_reg[13:7]  ==  7'b0000001       );
406
assign inst_clrw    = (inst_reg[13:7]  ==  7'b0000010       );
407
assign inst_clrf    = (inst_reg[13:7]  ==  7'b0000011       );
408
assign inst_ret     = (inst_reg[13:0]  == 14'b00000000001000);
409
assign inst_retfie  = (inst_reg[13:0]  == 14'b00000000001001);
410
assign inst_sleep   = (inst_reg[13:0]  == 14'b00000001100011);
411
 
412
 
413
// Calculate RAM access address (see pp.19 of PIC16F84 data sheet)
414
 
415
    // if "d"=0, indirect addressing is used, so RAM address is BANK+FSR
416
    // otherwise, RAM address is BANK+"d"
417
    // (see pp.19 of PIC16F84 data sheet)
418
assign ram_adr_node = (inst_reg[6:0]==0)?{status_reg[7],fsr_reg[7:0]}:
419
                               {status_reg[6:5],inst_reg[6:0]};
420
 
421
    // check if this is an access to external RAM or not
422
assign addr_sram   = (ram_adr_node[6:0] > 7'b0001011); //0CH-7FH,8CH-FFH
423
 
424
    // check if this is an access to special register or not
425
    // only 1 signal of the following signals will be '1'
426
assign addr_pcl     = (ram_adr_node[6:0] ==  7'b0000010);    // 02H, 82H
427
assign addr_stat    = (ram_adr_node[6:0] ==  7'b0000011);    // 03H, 83H
428
assign addr_fsr     = (ram_adr_node[6:0] ==  7'b0000100);    // 04H, 84H
429
assign addr_aux_dat = (ram_adr_node[7:0] == 8'b00001000);    // 08H
430
assign addr_pclath  = (ram_adr_node[6:0] ==  7'b0001010);    // 0AH, 8AH
431
assign addr_intcon  = (ram_adr_node[6:0] ==  7'b0001011);    // 0BH, 8BH
432
assign addr_aux_adr_lo = (ram_adr_node[7:0] == 8'b00000101); // 05H
433
assign addr_aux_adr_hi = (ram_adr_node[7:0] == 8'b00000110); // 06H
434
 
435
// construct bit-mask for logical operations and bit tests
436
assign mask_node = 1 << inst_reg[9:7];
437
 
438
// Create the exec_stall signal, based on the contents of the currently
439
// executing instruction (inst_reg).  next_exec_stall reflects the state
440
// to assign to exec_stall following the conclusion of the next Q4 state.
441
// All of these instructions cause an execution stall in the next cycle
442
// because they modify the program counter, and a new value is presented
443
// for fetching during the stall cycle, during which time no instruction
444
// should be executed.
445
//
446
// The conditional instructions are given along with their conditions for
447
// execution.  If the conditions are not met, there is no stall and nothing
448
// to execute.
449
assign next_exec_stall = (
450
                             inst_goto
451
                          || inst_call
452
                          || inst_ret
453
                          || inst_retlw
454
                          || inst_retfie
455
                          || (
456
                              (inst_btfsc || inst_decfsz || inst_incfsz)
457
                              && aluout_zero_node
458
                              )
459
                          || (inst_btfss && ~aluout_zero_node)
460
                          || (addr_pcl && writeram_node)
461
                          );
462
always @(posedge clk_i)
463
begin
464
  if (reset_i) exec_stall_reg <= 0;
465
  else if (clk_en_i && (state_reg == QINT_PP)) exec_stall_reg <= 1;
466
  else if (clk_en_i && (state_reg == Q4_PP))
467
    exec_stall_reg <= (next_exec_stall && ~exec_stall_reg);
468
    // exec stall should never be generated during a stall cycle, because
469
    // a stall cycle doesn't execute anything...
470
end
471
 
472
assign stack_top = stack_reg[stack_pnt_reg];
473
// Formulate the next pc_reg value (the program counter.)
474
// During stall cycles, the pc is simply incremented...
475
always @(
476
            pc_reg
477
         or pclath_reg
478
         or aluout
479
         or stack_pnt_reg
480
         or stack_top
481
         or inst_ret
482
         or inst_retlw
483
         or inst_retfie
484
         or inst_goto
485
         or inst_call
486
         or inst_reg
487
         or writeram_node
488
         or addr_pcl
489
         or exec_stall_reg
490
         )
491
begin
492
  if (~exec_stall_reg &&(inst_ret || inst_retlw || inst_retfie))
493
    next_pc_node <= stack_top;
494
  else if (~exec_stall_reg &&(inst_goto || inst_call))
495
    next_pc_node <= {pclath_reg[4:3],inst_reg[10:0]};
496
  else if (~exec_stall_reg && (writeram_node && addr_pcl))
497
    // PCL is data-destination, but update the entire PC.
498
    next_pc_node <= {pclath_reg[4:0],aluout};
499
  else
500
    next_pc_node <= pc_reg + 1;
501
end
502
 
503
// Set the program counter
504
// If the sleep instruction is executing, then the PC is not allowed to be
505
// updated, since the processor will "freeze" and the instruction being fetched
506
// during the sleep instruction must be executed upon wakeup interrupt.
507
// Obviously, if the PC were to change at the end of the sleep instruction, then
508
// a different (incorrect) address would be fetched during the sleep time.
509
always @(posedge clk_i)
510
begin
511
  if (reset_i) begin
512
    pc_reg <= 0;
513
    old_pc_reg <= 0;
514
  end
515
  else if (clk_en_i && (state_reg == QINT_PP))
516
  begin
517
    old_pc_reg <= pc_reg;
518
    pc_reg <= 4;
519
  end
520
  else if (clk_en_i && ~inst_sleep && (state_reg == Q4_PP))
521
  begin
522
    old_pc_reg <= pc_reg;
523
    pc_reg <= next_pc_node;
524
  end
525
end
526
 
527
// 1. Intermediate nodes for resource sharing
528
 
529
// Tri-state drivers instead of a huge selector...  It produces smaller
530
// results, and runs faster.
531
assign ram_i_node = (addr_sram)       ?ram_dat_i:8'bZ;
532
assign ram_i_node = (addr_pcl)        ?pc_reg[7:0]:8'bZ;
533
assign ram_i_node = (addr_stat)       ?status_reg:8'bZ;
534
assign ram_i_node = (addr_fsr)        ?fsr_reg:8'bZ;
535
assign ram_i_node = (addr_aux_dat)    ?aux_dat_io:8'bZ;
536
assign ram_i_node = (addr_pclath)     ?{3'b0,pclath_reg}:8'bZ;
537
assign ram_i_node = (addr_intcon)     ?intcon_reg:8'bZ;
538
assign ram_i_node = (addr_aux_adr_lo) ?aux_adr_lo_reg:8'bZ;
539
assign ram_i_node = (addr_aux_adr_hi) ?aux_adr_hi_reg:8'bZ;
540
 
541
// 1-3. Adder (ALU)
542
// full 8bit-addition, with carry in/out.
543
// Note that "temp" and "dtemp" are intended to be thrown away.
544
// Also, addlow_node[3:0] are thrown away.
545
// Even though they are assigned, they should never be used.
546
assign add_node     =    {1'b0,aluinp1_reg}
547
                       + {1'b0,aluinp2_reg};
548
// lower 4bit-addition
549
assign addlow_node =    {1'b0,aluinp1_reg[3:0]}
550
                      + {1'b0,aluinp2_reg[3:0]};
551
 
552
// 1-4. Test if aluout = 0
553
assign aluout_zero_node = (aluout == 0)?1:0;
554
 
555
// 1-5. Determine destination
556
always @(
557
            inst_reg
558
         or inst_movwf
559
         or inst_bcf
560
         or inst_bsf
561
         or inst_clrf
562
         or inst_movlw
563
         or inst_addlw
564
         or inst_sublw
565
         or inst_andlw
566
         or inst_iorlw
567
         or inst_xorlw
568
         or inst_retlw
569
         or inst_clrw
570
         or inst_movf
571
         or inst_swapf
572
         or inst_addwf
573
         or inst_subwf
574
         or inst_andwf
575
         or inst_iorwf
576
         or inst_xorwf
577
         or inst_decf
578
         or inst_incf
579
         or inst_rlf
580
         or inst_rrf
581
         or inst_decfsz
582
         or inst_incfsz
583
         or inst_comf
584
         )
585
begin
586
  if (inst_movwf || inst_bcf || inst_bsf || inst_clrf)
587
  begin
588
    writew_node     <= 0;
589
    writeram_node   <= 1;
590
  end
591
  else if (   inst_movlw || inst_addlw || inst_sublw || inst_andlw
592
           || inst_iorlw || inst_xorlw || inst_retlw || inst_clrw)
593
  begin
594
    writew_node     <= 1;
595
    writeram_node   <= 0;
596
  end
597
  else if (   inst_movf   || inst_swapf || inst_addwf || inst_subwf
598
           || inst_andwf  || inst_iorwf || inst_xorwf || inst_decf
599
           || inst_incf   || inst_rlf   || inst_rrf   || inst_decfsz
600
           || inst_incfsz || inst_comf)
601
  begin
602
    writew_node     <= ~inst_reg[7];  // ("d" field of fetched instruction)
603
    writeram_node   <=  inst_reg[7];  // ("d" field of fetched instruction)
604
  end
605
  else
606
  begin
607
    writew_node     <= 0;
608
    writeram_node   <= 0;
609
  end
610
end // End of determine destination logic
611
 
612
 
613
 
614
 
615
// 2-4-1. Calculation and store result into alu-output register
616
 
617
always @(
618
            add_node
619
         or aluinp1_reg
620
         or aluinp2_reg
621
         or status_reg
622
         or inst_reg
623
         or inst_movwf
624
         or inst_bcf
625
         or inst_bsf
626
         or inst_btfsc
627
         or inst_btfss
628
         or inst_clrf
629
         or inst_addlw
630
         or inst_sublw
631
         or inst_andlw
632
         or inst_iorlw
633
         or inst_xorlw
634
         or inst_retlw
635
         or inst_clrw
636
         or inst_swapf
637
         or inst_addwf
638
         or inst_subwf
639
         or inst_andwf
640
         or inst_iorwf
641
         or inst_xorwf
642
         or inst_decf
643
         or inst_incf
644
         or inst_rlf
645
         or inst_rrf
646
         or inst_decfsz
647
         or inst_incfsz
648
         or inst_comf
649
         )
650
begin
651
  // 2-4-1-1. Set aluout register
652
          // Rotate left
653
  if      (inst_rlf)
654
          aluout <= {aluinp1_reg[6:0],status_reg[0]};
655
          // Rotate right
656
  else if (inst_rrf)
657
          aluout  <= {status_reg[0],aluinp1_reg[7:1]};
658
          // Swap nibbles
659
  else if (inst_swapf)
660
          aluout <= {aluinp1_reg[3:0],aluinp1_reg[7:4]};
661
          // Logical inversion
662
  else if (inst_comf)
663
          aluout  <= ~aluinp1_reg;
664
          // Logical AND, bit clear/bit test
665
  else if (   inst_andlw || inst_andwf || inst_bcf || inst_btfsc
666
           || inst_btfss)
667
          aluout  <= (aluinp1_reg & aluinp2_reg);
668
          // Logical OR, bit set
669
  else if (inst_bsf || inst_iorlw || inst_iorwf)
670
          aluout  <= (aluinp1_reg | aluinp2_reg);
671
          // Logical XOR
672
  else if (inst_xorlw || inst_xorwf)
673
          aluout  <= (aluinp1_reg ^ aluinp2_reg);
674
          // Addition, Subtraction, Increment, Decrement
675
  else if (  inst_addlw || inst_addwf  || inst_sublw || inst_subwf
676
           || inst_decf || inst_decfsz || inst_incf  || inst_incfsz)
677
          aluout  <= add_node[7:0];
678
          // Pass through
679
  else aluout  <= aluinp1_reg;
680
end
681
 
682
 
683
// MAIN EFSM: description of register value changes in each clock cycle
684
always @(posedge clk_i)
685
begin
686
  // Assign reset (default) values of registers
687
  if (reset_i)
688
  begin
689
    status_reg[7:5] <= 3'b0;
690
    pclath_reg      <= 0;     // 0
691
    intcon_reg[7:1] <= 7'b0;
692
    aux_adr_lo_reg  <= 0;
693
    aux_adr_hi_reg  <= 0;
694
    ram_we_reg      <= 0;
695
    status_reg[4]   <= 1;     // /T0 = 1
696
    status_reg[3]   <= 1;     // /PD = 1
697
    stack_pnt_reg   <= 0;     // Reset stack pointer
698
  end  // End of reset assignments
699
  else if (~exec_stall_reg && clk_en_i)
700
  begin   // Execution ceases during a stall cycle.
701
    if (state_reg == Q2_PP) // 2-3. Q2 cycle
702
    begin
703
      // 2-3-1. Read data-RAM and store values to alu-input regs
704
      // 2-3-1-1. Set aluinp1 register (source #1)
705
      if (   inst_movf   || inst_swapf || inst_addwf || inst_subwf
706
          || inst_andwf  || inst_iorwf || inst_xorwf || inst_decf
707
          || inst_incf   || inst_rlf   || inst_rrf   || inst_bcf
708
          || inst_bsf    || inst_btfsc || inst_btfss || inst_decfsz
709
          || inst_incfsz || inst_comf)
710
 
711
          aluinp1_reg <= ram_i_node;       // RAM/Special registers
712
      else
713
      if (   inst_movlw || inst_addlw || inst_sublw || inst_andlw
714
          || inst_iorlw || inst_xorlw || inst_retlw)
715
          aluinp1_reg <= inst_reg[7:0];    // Immediate value ("k")
716
      else
717
      if (   inst_clrf  || inst_clrw) aluinp1_reg <= 0; // 0
718
      else aluinp1_reg <= w_reg;                        // W register
719
 
720
      // 2-3-1-2. Set aluinp2 register (source #2)
721
      c_subtract_zero <= 0;       // default to non-special case
722
      c_dig_subtract_zero <= 0;   // default to non-special case
723
      if      (inst_decf || inst_decfsz) aluinp2_reg <= -1; // for decr.
724
      else if (inst_incf || inst_incfsz) aluinp2_reg <=  1; // for incr.
725
              // -1 * W register (for subtract)
726
      else if (inst_sublw || inst_subwf)
727
      begin
728
        aluinp2_reg <= ~w_reg + 1;
729
        c_subtract_zero <= (w_reg == 0);            // Indicate special case
730
        c_dig_subtract_zero <= (w_reg[3:0] == 0);   // Indicate special case
731
      end
732
            // operation of BCF: AND with inverted mask ("1..101..1")
733
            // mask for BCF: value of only one position is 0
734
      else if (inst_bcf) aluinp2_reg <= ~mask_node;
735
            // operation of BSF: OR with mask_node ("0..010..0")
736
            // operation of FSC and FSS: AND with mask_node, compare to 0
737
      else if (inst_btfsc || inst_btfss || inst_bsf)
738
                                  aluinp2_reg <= mask_node;
739
      else aluinp2_reg <= w_reg; // W register
740
 
741
      // 2-3-1-3. Set stack pointer register (pop stack)
742
      if (inst_ret || inst_retlw || inst_retfie)
743
           stack_pnt_reg   <= stack_pnt_reg - 1; // cycles 3,2,1,0,7,6...
744
 
745
      // 2-4-1-3. Set data-SRAM write enable (hazard-free)
746
      // Set the write enables depending on the destination.
747
      // (These have been implemented as registers to avoid glitches?
748
      // It is not known to me (John Clayton) whether any glitches would
749
      // really occur.  It might be possible to generate these signals
750
      // using combinational logic only, without using registers!
751
      ram_we_reg <= (writeram_node && addr_sram);
752
      aux_we_reg <= (writeram_node && addr_aux_dat);
753
    end   // End of Q2 state
754
 
755
    //---------------------------------------------------------------------
756
 
757
    else if (state_reg == QINT_PP) // Interrupt execution (instead of Q4_PP)
758
    begin
759
        // PORT-B0 INT
760
        intcon_reg[1] <= 1;                     // set INTF
761
        intcon_reg[7] <= 0;                     // clear GIE
762
        stack_reg[stack_pnt_reg] <= old_pc_reg; // Push old PC
763
        stack_pnt_reg <= stack_pnt_reg + 1;     // increment stack pointer
764
        // The old PC is pushed, so that the pre-empted instruction can be
765
        // restarted later, when the retfie is executed.
766
    end
767
 
768
    //---------------------------------------------------------------------
769
 
770
    else if (state_reg == Q4_PP)   // Execution & writing of results.
771
    begin
772
 
773
      if (inst_call)
774
      begin
775
        stack_reg[stack_pnt_reg] <= pc_reg;     // Push current PC
776
        stack_pnt_reg <= stack_pnt_reg + 1;     // increment stack pointer
777
      end
778
 
779
      if (inst_retfie) // "return from interrupt" instruction
780
      begin
781
        intcon_reg[7] <= 1;                     // Set GIE
782
      end
783
 
784
      // 2-4-1-2. Set C flag and DC flag
785
      if (inst_addlw || inst_addwf || inst_sublw || inst_subwf)
786
      begin
787
        // c_dig_subtract_zero and c_subtract_zero are used to take care of the
788
        // special case when subtracting zero, where the carry bit should be 1
789
        // (meaning no borrow).  It is explicitly set by these signals during
790
        // that condition.  See 16F84 datasheet, page 8 for further information
791
        // about the C bit.
792
        status_reg[1]   <= addlow_node[4] || c_dig_subtract_zero;  // DC flag
793
        status_reg[0]   <= add_node[8] || c_subtract_zero;         // C flag
794
      end
795
      else if (inst_rlf) status_reg[0] <= aluinp1_reg[7];  // C flag
796
      else if (inst_rrf) status_reg[0] <= aluinp1_reg[0];  // C flag
797
 
798
      // 2-5-2. Store calculation result into destination, 
799
      // 2-5-2-1. Set W register
800
 
801
      if (writew_node) w_reg   <= aluout;    // write W reg
802
 
803
      // 2-5-2-2. Set data RAM/special registers,
804
      if (writeram_node)
805
      begin
806
        if (addr_stat)
807
        begin
808
          status_reg[7:5] <= aluout[7:5];      // write IRP,RP1,RP0
809
          // status(4),status(3)...unwritable, see below (/PD,/T0 part)
810
          status_reg[1:0] <= aluout[1:0];      // write DC,C
811
        end
812
        if (addr_fsr)         fsr_reg <= aluout;       // write FSR
813
        if (addr_pclath)   pclath_reg <= aluout[4:0];  // write PCLATH
814
        if (addr_intcon) intcon_reg <= aluout;         // write INTCON
815
        if (addr_aux_adr_lo) aux_adr_lo_reg <= aluout; // write AUX low
816
        if (addr_aux_adr_hi) aux_adr_hi_reg <= aluout; // write AUX high
817
      end
818
 
819
      // 2-5-2-3. Set/clear Z flag.
820
      if (addr_stat) status_reg[2] <= aluout[2]; // (dest. is Z flag)
821
      else if (   inst_addlw || inst_addwf || inst_andlw || inst_andwf
822
               || inst_clrf  || inst_clrw  || inst_comf  || inst_decf
823
               || inst_incf  || inst_movf  || inst_sublw || inst_subwf
824
               || inst_xorlw || inst_xorwf || inst_iorlw || inst_iorwf )
825
              status_reg[2] <= aluout_zero_node; // Z=1 if result == 0
826
 
827
      // 2-5-3. Clear RAM write enables (hazard-free)
828
      ram_we_reg <= 0;
829
      aux_we_reg <= 0;
830
 
831
    end    // End of Q4 state
832
  end // End of "if (~exec_stall_reg)"    
833
end  // End of process
834
 
835
 
836
// Calculation of next processor state.
837
// (Not including reset conditions, which are covered by the clocked logic,
838
//  which also includes a "global clock enable."
839
always @(
840
            state_reg
841
         or inst_sleep
842
         or inte_sync_reg
843
         or exec_stall_reg
844
         or int_condition
845
         )
846
begin
847
  case (state_reg)
848
  Q2_PP     : if (int_condition) next_state_node <= QINT_PP;
849
              else next_state_node <= Q4_PP;
850
  Q4_PP     : if (~exec_stall_reg && inst_sleep) next_state_node <= QSLEEP_PP;
851
              else next_state_node <= Q2_PP;
852
  QINT_PP   : next_state_node <= Q2_PP;
853
  QSLEEP_PP : if (inte_sync_reg) next_state_node <= Q2_PP;
854
              else next_state_node <= QSLEEP_PP;
855
              // Default condition provided for convention and completeness
856
              // only.  Logically, all of the conditions are already covered.
857
  default   : next_state_node <= Q2_PP;
858
  endcase
859
end
860
 
861
 
862
// Clocked state transitions, based upon dataflow (non-clocked logic) in
863
// the previous always block.
864
always @(posedge clk_i)
865
begin
866
  if (reset_i) state_reg <= Q2_PP;
867
  else if (clk_en_i) state_reg <= next_state_node;
868
end  // End of process
869
 
870
 
871
// Detect external interrupt requests
872
// You can code multiple interrupts if you wish, or use the single interrupt
873
// provided and simply have the interrupt service routine (ISR) check to find
874
// out the source of the interrupt, by or-ing together all of the interrupt
875
// sources and providing a readable register of their values at the time
876
// the interrupt occurred.
877
//
878
// When an interrupt is recognized by the processor, this is signified by
879
// entering "QINT_PP," which is treated like an executable instruction.
880
// The interrupt instruction can only be executed when not in a stall condition.
881
// It simply "pre-empts" the instruction that would have been executed during
882
// that cycle.  Then, when retfie is executed, the pre-empted instruction is
883
// re-started (the stall cycle of the retfie is when the address of the 
884
// instruction being re-started is fetched.)
885
//
886
// I was unable to obtain correct operation for capturing the negative edge,
887
// so I am discarding it.  If one really needs to generate an interrupt on the
888
// falling edge, just use an inverted version of the signal (the inversion is
889
// often "free" inside of an FPGA anyhow.)
890
//
891
// Upon further testing, I discovered that even the rising edge "trigger" was not
892
// really truly an edge detection, it was more like a "set-reset" flip flop
893
// type of behavior.  Rather than mess around with it any more, I am implementing
894
// a clocked "poor man's rising edge detector."
895
// Capture the rising edge of the interrupt input...  This part is self clearing.
896
// It also means that the interrupt must last longer than one clock cycle in
897
// order to be properly recognized.  (It is "pseudo edge triggered", not a true
898
// rising edge trigger.)
899
// When the interrupt is recognized, inte_sync_reg is cleared.
900
 
901
 
902
always @(posedge clk_i)
903
begin
904
  if (clk_en_i) intrise_reg <= int0_i;
905
end // process
906
assign intrise = (int0_i && ~intrise_reg);
907
 
908
//  The inte_sync_reg signal is used for waking up from SLEEP.
909
//  (this flip flop is also a synchronizer to minimize the
910
//   possibility of metastability due to changes at the input
911
//   occurring at the same time as the processor clock edge...)
912
//  It might be possible to eliminate this step, and issue the interrupt
913
//  directly without this intermediate synchronizer flip-flop.
914
always @(posedge clk_i)
915
begin
916
  if (reset_i || (state_reg == QINT_PP)) inte_sync_reg <= 0;
917
  else if (clk_en_i && intrise && intcon_reg[4]) inte_sync_reg <= 1;
918
end
919
 
920
// Issue an interrupt when the interrupt is present.
921
// Also, do not issue an interrupt when there is a stall cycle coming!
922
assign int_condition = (inte_sync_reg && ~exec_stall_reg && intcon_reg[7]);
923
                               // Interrupt must be pending
924
                               // Next processor cycle must not be a stall
925
                               // GIE bit must be set to issue interrupt
926
 
927
// Circuit's output signals
928
assign prog_adr_o = pc_reg;        // program ROM address
929
assign ram_adr_o  = ram_adr_node;  // data RAM address
930
assign ram_dat_o  = aluout;        // data RAM write data
931
assign ram_we_o   = ram_we_reg;    // data RAM write enable
932
 
933
assign aux_adr_o  = {aux_adr_hi_reg,aux_adr_lo_reg};
934
assign aux_dat_io = (aux_we_reg && clk_en_i)?aluout:{8{1'bZ}};
935
assign aux_we_o   = aux_we_reg;
936
 
937
endmodule
938
 
939
 
940
//`undef STATEBIT_SIZE

powered by: WebSVN 2.1.0

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