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

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [core/] [rtl/] [verilog/] [omsp_frontend.v] - Blame information for rev 106

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 olivier.gi
//----------------------------------------------------------------------------
2
// Copyright (C) 2001 Authors
3
//
4
// This source file may be used and distributed without restriction provided
5
// that this copyright statement is not removed from the file and that any
6
// derivative work contains the original copyright notice and the associated
7
// disclaimer.
8
//
9
// This source file is free software; you can redistribute it and/or modify
10
// it under the terms of the GNU Lesser General Public License as published
11
// by the Free Software Foundation; either version 2.1 of the License, or
12
// (at your option) any later version.
13
//
14
// This source is distributed in the hope that it will be useful, but WITHOUT
15
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
16
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
17
// License for more details.
18
//
19
// You should have received a copy of the GNU Lesser General Public License
20
// along with this source; if not, write to the Free Software Foundation,
21
// Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
22
//
23
//----------------------------------------------------------------------------
24
//
25 34 olivier.gi
// *File Name: omsp_frontend.v
26 2 olivier.gi
// 
27
// *Module Description:
28
//                       openMSP430 Instruction fetch and decode unit
29
//
30
// *Author(s):
31
//              - Olivier Girard,    olgirard@gmail.com
32
//
33
//----------------------------------------------------------------------------
34 17 olivier.gi
// $Rev: 106 $
35
// $LastChangedBy: olivier.girard $
36
// $LastChangedDate: 2011-03-25 23:01:03 +0100 (Fri, 25 Mar 2011) $
37
//----------------------------------------------------------------------------
38 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
39
`else
40 23 olivier.gi
`include "openMSP430_defines.v"
41 103 olivier.gi
`endif
42 2 olivier.gi
 
43 34 olivier.gi
module  omsp_frontend (
44 2 olivier.gi
 
45
// OUTPUTs
46
    dbg_halt_st,                   // Halt/Run status from CPU
47 53 olivier.gi
    decode_noirq,                  // Frontend decode instruction
48 2 olivier.gi
    e_state,                       // Execution state
49
    exec_done,                     // Execution completed
50
    inst_ad,                       // Decoded Inst: destination addressing mode
51
    inst_as,                       // Decoded Inst: source addressing mode
52
    inst_alu,                      // ALU control signals
53
    inst_bw,                       // Decoded Inst: byte width
54
    inst_dest,                     // Decoded Inst: destination (one hot)
55
    inst_dext,                     // Decoded Inst: destination extended instruction word
56
    inst_irq_rst,                  // Decoded Inst: Reset interrupt
57
    inst_jmp,                      // Decoded Inst: Conditional jump
58 105 olivier.gi
    inst_mov,                      // Decoded Inst: mov instruction
59 2 olivier.gi
    inst_sext,                     // Decoded Inst: source extended instruction word
60
    inst_so,                       // Decoded Inst: Single-operand arithmetic
61
    inst_src,                      // Decoded Inst: source (one hot)
62
    inst_type,                     // Decoded Instruction type
63
    irq_acc,                       // Interrupt request accepted (one-hot signal)
64
    mab,                           // Frontend Memory address bus
65
    mb_en,                         // Frontend Memory bus enable
66
    nmi_acc,                       // Non-Maskable interrupt request accepted
67
    pc,                            // Program counter
68
    pc_nxt,                        // Next PC value (for CALL & IRQ)
69
 
70
// INPUTs
71 106 olivier.gi
    cpu_en_s,                      // Enable CPU code execution (synchronous)
72 2 olivier.gi
    cpuoff,                        // Turns off the CPU
73
    dbg_halt_cmd,                  // Halt CPU command
74
    dbg_reg_sel,                   // Debug selected register for rd/wr access
75 33 olivier.gi
    fe_pmem_wait,                  // Frontend wait for Instruction fetch
76 2 olivier.gi
    gie,                           // General interrupt enable
77
    irq,                           // Maskable interrupts
78
    mclk,                          // Main system clock
79
    mdb_in,                        // Frontend Memory data bus input
80
    nmi_evt,                       // Non-maskable interrupt event
81
    pc_sw,                         // Program counter software value
82
    pc_sw_wr,                      // Program counter software write
83
    puc,                           // Main system reset
84
    wdt_irq                        // Watchdog-timer interrupt
85
);
86
 
87
// OUTPUTs
88
//=========
89
output              dbg_halt_st;   // Halt/Run status from CPU
90 53 olivier.gi
output              decode_noirq;  // Frontend decode instruction
91 2 olivier.gi
output        [3:0] e_state;       // Execution state
92
output              exec_done;     // Execution completed
93
output        [7:0] inst_ad;       // Decoded Inst: destination addressing mode
94
output        [7:0] inst_as;       // Decoded Inst: source addressing mode
95
output       [11:0] inst_alu;      // ALU control signals
96
output              inst_bw;       // Decoded Inst: byte width
97
output       [15:0] inst_dest;     // Decoded Inst: destination (one hot)
98
output       [15:0] inst_dext;     // Decoded Inst: destination extended instruction word
99
output              inst_irq_rst;  // Decoded Inst: Reset interrupt
100
output        [7:0] inst_jmp;      // Decoded Inst: Conditional jump
101 105 olivier.gi
output              inst_mov;      // Decoded Inst: mov instruction
102 2 olivier.gi
output       [15:0] inst_sext;     // Decoded Inst: source extended instruction word
103
output        [7:0] inst_so;       // Decoded Inst: Single-operand arithmetic
104
output       [15:0] inst_src;      // Decoded Inst: source (one hot)
105
output        [2:0] inst_type;     // Decoded Instruction type
106
output       [13:0] irq_acc;       // Interrupt request accepted (one-hot signal)
107
output       [15:0] mab;           // Frontend Memory address bus
108
output              mb_en;         // Frontend Memory bus enable
109
output              nmi_acc;       // Non-Maskable interrupt request accepted
110
output       [15:0] pc;            // Program counter
111
output       [15:0] pc_nxt;        // Next PC value (for CALL & IRQ)
112
 
113
// INPUTs
114
//=========
115 106 olivier.gi
input               cpu_en_s;      // Enable CPU code execution (synchronous)
116 2 olivier.gi
input               cpuoff;        // Turns off the CPU
117
input               dbg_halt_cmd;  // Halt CPU command
118
input         [3:0] dbg_reg_sel;   // Debug selected register for rd/wr access
119 33 olivier.gi
input               fe_pmem_wait;  // Frontend wait for Instruction fetch
120 2 olivier.gi
input               gie;           // General interrupt enable
121
input        [13:0] irq;           // Maskable interrupts
122
input               mclk;          // Main system clock
123
input        [15:0] mdb_in;        // Frontend Memory data bus input
124
input               nmi_evt;       // Non-maskable interrupt event
125
input        [15:0] pc_sw;         // Program counter software value
126
input               pc_sw_wr;      // Program counter software write
127
input               puc;           // Main system reset
128
input               wdt_irq;       // Watchdog-timer interrupt
129
 
130
 
131
//=============================================================================
132 85 olivier.gi
// 0)  UTILITY FUNCTIONS
133
//=============================================================================
134
 
135
// 16 bits one-hot decoder
136
function [15:0] one_hot16;
137
   input  [3:0] binary;
138
   begin
139
      one_hot16         = 16'h0000;
140
      one_hot16[binary] =  1'b1;
141
   end
142
endfunction
143
 
144
// 8 bits one-hot decoder
145
function [7:0] one_hot8;
146
   input  [2:0] binary;
147
   begin
148
      one_hot8         = 8'h00;
149
      one_hot8[binary] = 1'b1;
150
   end
151
endfunction
152
 
153
 
154
//=============================================================================
155 2 olivier.gi
// 1)  FRONTEND STATE MACHINE
156
//=============================================================================
157
 
158
// The wire "conv" is used as state bits to calculate the next response
159
reg  [2:0] i_state;
160
reg  [2:0] i_state_nxt;
161
 
162
reg  [1:0] inst_sz;
163
wire [1:0] inst_sz_nxt;
164
wire       irq_detect;
165
wire [2:0] inst_type_nxt;
166
wire       is_const;
167
reg [15:0] sconst_nxt;
168
reg  [3:0] e_state_nxt;
169
 
170
// State machine definitons
171
parameter I_IRQ_FETCH = 3'h0;
172
parameter I_IRQ_DONE  = 3'h1;
173
parameter I_DEC       = 3'h2; // New instruction ready for decode
174
parameter I_EXT1      = 3'h3; // 1st Extension word
175
parameter I_EXT2      = 3'h4; // 2nd Extension word
176
parameter I_IDLE      = 3'h5; // CPU is in IDLE mode
177
 
178 106 olivier.gi
// CPU on/off through the debug interface or cpu_en port
179
wire   cpu_halt_cmd = dbg_halt_cmd | ~cpu_en_s;
180
 
181 2 olivier.gi
// States Transitions
182
always @(i_state   or inst_sz    or inst_sz_nxt or pc_sw_wr     or exec_done or
183 106 olivier.gi
         exec_done or irq_detect or cpuoff      or cpu_halt_cmd or e_state)
184 2 olivier.gi
    case(i_state)
185 106 olivier.gi
      I_IDLE     : i_state_nxt = (irq_detect & ~cpu_halt_cmd) ? I_IRQ_FETCH :
186
                                 (~cpuoff    & ~cpu_halt_cmd) ? I_DEC       : I_IDLE;
187 2 olivier.gi
      I_IRQ_FETCH: i_state_nxt =  I_IRQ_DONE;
188
      I_IRQ_DONE : i_state_nxt =  I_DEC;
189
      I_DEC      : i_state_nxt =  irq_detect                  ? I_IRQ_FETCH :
190 106 olivier.gi
                          (cpuoff | cpu_halt_cmd) & exec_done ? I_IDLE      :
191
                            cpu_halt_cmd & (e_state==`E_IDLE) ? I_IDLE      :
192 2 olivier.gi
                                  pc_sw_wr                    ? I_DEC       :
193
                             ~exec_done & ~(e_state==`E_IDLE) ? I_DEC       :        // Wait in decode state
194
                                  (inst_sz_nxt!=2'b00)        ? I_EXT1      : I_DEC; // until execution is completed
195
      I_EXT1     : i_state_nxt =  irq_detect                  ? I_IRQ_FETCH :
196
                                  pc_sw_wr                    ? I_DEC       :
197
                                  (inst_sz!=2'b01)            ? I_EXT2      : I_DEC;
198
      I_EXT2     : i_state_nxt =  irq_detect                  ? I_IRQ_FETCH : I_DEC;
199
      default    : i_state_nxt =  I_IRQ_FETCH;
200
    endcase
201
 
202
// State machine
203
always @(posedge mclk or posedge puc)
204
  if (puc) i_state  <= I_IRQ_FETCH;
205
  else     i_state  <= i_state_nxt;
206
 
207
// Utility signals
208 53 olivier.gi
wire   decode_noirq =  ((i_state==I_DEC) &  (exec_done | (e_state==`E_IDLE)));
209
wire   decode       =  decode_noirq | irq_detect;
210
wire   fetch        = ~((i_state==I_DEC) & ~(exec_done | (e_state==`E_IDLE))) & ~(e_state_nxt==`E_IDLE);
211 2 olivier.gi
 
212
// Debug interface cpu status
213
reg    dbg_halt_st;
214
always @(posedge mclk or posedge puc)
215
  if (puc)  dbg_halt_st <= 1'b0;
216 106 olivier.gi
  else      dbg_halt_st <= cpu_halt_cmd & (i_state_nxt==I_IDLE);
217 2 olivier.gi
 
218
 
219
//=============================================================================
220
// 2)  INTERRUPT HANDLING
221
//=============================================================================
222
 
223
// Detect nmi interrupt
224
reg         inst_nmi;
225
always @(posedge mclk or posedge puc)
226
  if (puc)                      inst_nmi <= 1'b0;
227
  else if (nmi_evt)             inst_nmi <= 1'b1;
228
  else if (i_state==I_IRQ_DONE) inst_nmi <= 1'b0;
229
 
230
 
231
// Detect reset interrupt
232
reg         inst_irq_rst;
233
always @(posedge mclk or posedge puc)
234
  if (puc)                      inst_irq_rst <= 1'b1;
235
  else if (exec_done)           inst_irq_rst <= 1'b0;
236
 
237
//  Detect other interrupts
238 106 olivier.gi
assign  irq_detect = (inst_nmi | ((|irq | wdt_irq) & gie)) & ~cpu_halt_cmd & ~dbg_halt_st & (exec_done | (i_state==I_IDLE));
239 2 olivier.gi
 
240
// Select interrupt vector
241
reg  [3:0] irq_num;
242
always @(posedge mclk or posedge puc)
243
  if (puc)             irq_num <= 4'hf;
244
  else if (irq_detect) irq_num <= inst_nmi           ?  4'he :
245
                                  irq[13]            ?  4'hd :
246
                                  irq[12]            ?  4'hc :
247
                                  irq[11]            ?  4'hb :
248
                                 (irq[10] | wdt_irq) ?  4'ha :
249
                                  irq[9]             ?  4'h9 :
250
                                  irq[8]             ?  4'h8 :
251
                                  irq[7]             ?  4'h7 :
252
                                  irq[6]             ?  4'h6 :
253
                                  irq[5]             ?  4'h5 :
254
                                  irq[4]             ?  4'h4 :
255
                                  irq[3]             ?  4'h3 :
256
                                  irq[2]             ?  4'h2 :
257
                                  irq[1]             ?  4'h1 :
258
                                  irq[0]             ?  4'h0 : 4'hf;
259
 
260
wire [15:0] irq_addr    = {11'h7ff, irq_num, 1'b0};
261
 
262
// Interrupt request accepted
263 85 olivier.gi
wire [15:0] irq_acc_all = one_hot16(irq_num) & {16{(i_state==I_IRQ_FETCH)}};
264 2 olivier.gi
wire [13:0] irq_acc     = irq_acc_all[13:0];
265
wire        nmi_acc     = irq_acc_all[14];
266
 
267
 
268
//=============================================================================
269
// 3)  FETCH INSTRUCTION
270
//=============================================================================
271
 
272
//
273
// 3.1) PROGRAM COUNTER & MEMORY INTERFACE
274
//-----------------------------------------
275
 
276
// Program counter
277
reg  [15:0] pc;
278
 
279 60 olivier.gi
// Compute next PC value
280
wire [15:0] pc_incr = pc + {14'h0000, fetch, 1'b0};
281
wire [15:0] pc_nxt  = pc_sw_wr               ? pc_sw    :
282 2 olivier.gi
                      (i_state==I_IRQ_FETCH) ? irq_addr :
283 60 olivier.gi
                      (i_state==I_IRQ_DONE)  ? mdb_in   :  pc_incr;
284 2 olivier.gi
 
285
always @(posedge mclk or posedge puc)
286
  if (puc)  pc <= 16'h0000;
287
  else      pc <= pc_nxt;
288
 
289
// Check if ROM has been busy in order to retry ROM access
290 33 olivier.gi
reg pmem_busy;
291 2 olivier.gi
always @(posedge mclk or posedge puc)
292 85 olivier.gi
  if (puc)  pmem_busy <= 1'b0;
293 33 olivier.gi
  else      pmem_busy <= fe_pmem_wait;
294 2 olivier.gi
 
295
// Memory interface
296
wire [15:0] mab      = pc_nxt;
297 106 olivier.gi
wire        mb_en    = fetch | pc_sw_wr | (i_state==I_IRQ_FETCH) | pmem_busy | (dbg_halt_st & ~cpu_halt_cmd);
298 2 olivier.gi
 
299
 
300
//
301
// 3.2) INSTRUCTION REGISTER
302
//--------------------------------
303
 
304
// Instruction register
305
wire [15:0] ir  = mdb_in;
306
 
307
// Detect if source extension word is required
308
wire is_sext = (inst_as[`IDX] | inst_as[`SYMB] | inst_as[`ABS] | inst_as[`IMM]);
309
 
310
// Detect if destination extension word is required
311
wire is_dext = (inst_ad[`IDX] | inst_ad[`SYMB] | inst_ad[`ABS]);
312
 
313
// For the Symbolic addressing mode, add -2 to the extension word in order
314
// to make up for the PC address
315
wire [15:0] ext_incr = ((i_state==I_EXT1)     &  inst_as[`SYMB]) |
316
                       ((i_state==I_EXT2)     &  inst_ad[`SYMB]) |
317
                       ((i_state==I_EXT1)     & ~inst_as[`SYMB] &
318
                       ~(i_state_nxt==I_EXT2) &  inst_ad[`SYMB])   ? 16'hfffe : 16'h0000;
319
 
320
wire [15:0] ext_nxt  = ir + ext_incr;
321
 
322
// Store source extension word
323
reg [15:0] inst_sext;
324
always @(posedge mclk or posedge puc)
325
  if (puc)                                     inst_sext <= 16'h0000;
326
  else if (decode & is_const)                  inst_sext <= sconst_nxt;
327
  else if (decode & inst_type_nxt[`INST_JMP])  inst_sext <= {{5{ir[9]}},ir[9:0],1'b0};
328
  else if ((i_state==I_EXT1) & is_sext)        inst_sext <= ext_nxt;
329
 
330
// Source extension word is ready
331
wire inst_sext_rdy = (i_state==I_EXT1) & is_sext;
332
 
333
 
334
// Store destination extension word
335
reg [15:0] inst_dext;
336
always @(posedge mclk or posedge puc)
337
  if (puc)                               inst_dext <= 16'h0000;
338
  else if ((i_state==I_EXT1) & ~is_sext) inst_dext <= ext_nxt;
339
  else if  (i_state==I_EXT2)             inst_dext <= ext_nxt;
340
 
341
// Destination extension word is ready
342
wire inst_dext_rdy = (((i_state==I_EXT1) & ~is_sext) | (i_state==I_EXT2));
343
 
344
 
345
//=============================================================================
346
// 4)  DECODE INSTRUCTION
347
//=============================================================================
348
 
349
//
350
// 4.1) OPCODE: INSTRUCTION TYPE
351
//----------------------------------------
352
// Instructions type is encoded in a one hot fashion as following:
353
//
354
// 3'b001: Single-operand arithmetic
355
// 3'b010: Conditional jump
356
// 3'b100: Two-operand arithmetic
357
 
358
reg  [2:0] inst_type;
359
assign     inst_type_nxt = {(ir[15:14]!=2'b00),
360
                            (ir[15:13]==3'b001),
361
                            (ir[15:13]==3'b000)} & {3{~irq_detect}};
362
 
363
always @(posedge mclk or posedge puc)
364
  if (puc)                      inst_type <= 3'b000;
365
  else if (decode)              inst_type <= inst_type_nxt;
366
 
367
//
368
// 4.2) OPCODE: SINGLE-OPERAND ARITHMETIC
369
//----------------------------------------
370
// Instructions are encoded in a one hot fashion as following:
371
//
372
// 8'b00000001: RRC
373
// 8'b00000010: SWPB
374
// 8'b00000100: RRA
375
// 8'b00001000: SXT
376
// 8'b00010000: PUSH
377
// 8'b00100000: CALL
378
// 8'b01000000: RETI
379
// 8'b10000000: IRQ
380
 
381
reg   [7:0] inst_so;
382 85 olivier.gi
wire  [7:0] inst_so_nxt = irq_detect ? 8'h80 : (one_hot8(ir[9:7]) & {8{inst_type_nxt[`INST_SO]}});
383 2 olivier.gi
 
384
always @(posedge mclk or posedge puc)
385
  if (puc)         inst_so <= 8'h00;
386
  else if (decode) inst_so <= inst_so_nxt;
387
 
388
//
389
// 4.3) OPCODE: CONDITIONAL JUMP
390
//--------------------------------
391
// Instructions are encoded in a one hot fashion as following:
392
//
393
// 8'b00000001: JNE/JNZ
394
// 8'b00000010: JEQ/JZ
395
// 8'b00000100: JNC/JLO
396
// 8'b00001000: JC/JHS
397
// 8'b00010000: JN
398
// 8'b00100000: JGE
399
// 8'b01000000: JL
400
// 8'b10000000: JMP
401
 
402
reg   [2:0] inst_jmp_bin;
403
always @(posedge mclk or posedge puc)
404
  if (puc)         inst_jmp_bin <= 3'h0;
405
  else if (decode) inst_jmp_bin <= ir[12:10];
406
 
407 85 olivier.gi
wire [7:0] inst_jmp = one_hot8(inst_jmp_bin) & {8{inst_type[`INST_JMP]}};
408 2 olivier.gi
 
409
 
410
//
411
// 4.4) OPCODE: TWO-OPERAND ARITHMETIC
412
//-------------------------------------
413
// Instructions are encoded in a one hot fashion as following:
414
//
415
// 12'b000000000001: MOV
416
// 12'b000000000010: ADD
417
// 12'b000000000100: ADDC
418
// 12'b000000001000: SUBC
419
// 12'b000000010000: SUB
420
// 12'b000000100000: CMP
421
// 12'b000001000000: DADD
422
// 12'b000010000000: BIT
423
// 12'b000100000000: BIC
424
// 12'b001000000000: BIS
425
// 12'b010000000000: XOR
426
// 12'b100000000000: AND
427
 
428 85 olivier.gi
wire [15:0] inst_to_1hot = one_hot16(ir[15:12]) & {16{inst_type_nxt[`INST_TO]}};
429 2 olivier.gi
wire [11:0] inst_to_nxt  = inst_to_1hot[15:4];
430
 
431 105 olivier.gi
reg         inst_mov;
432
always @(posedge mclk or posedge puc)
433
  if (puc)         inst_mov <= 1'b0;
434
  else if (decode) inst_mov <= inst_to_nxt[`MOV];
435 2 olivier.gi
 
436 105 olivier.gi
 
437 2 olivier.gi
//
438
// 4.5) SOURCE AND DESTINATION REGISTERS
439
//---------------------------------------
440
 
441
// Destination register
442
reg [3:0] inst_dest_bin;
443
always @(posedge mclk or posedge puc)
444
  if (puc)         inst_dest_bin <= 4'h0;
445
  else if (decode) inst_dest_bin <= ir[3:0];
446
 
447 85 olivier.gi
wire  [15:0] inst_dest = dbg_halt_st          ? one_hot16(dbg_reg_sel) :
448
                         inst_type[`INST_JMP] ? 16'h0001               :
449 2 olivier.gi
                         inst_so[`IRQ]  |
450
                         inst_so[`PUSH] |
451 85 olivier.gi
                         inst_so[`CALL]       ? 16'h0002               :
452
                                                one_hot16(inst_dest_bin);
453 2 olivier.gi
 
454
 
455
// Source register
456
reg [3:0] inst_src_bin;
457
always @(posedge mclk or posedge puc)
458
  if (puc)         inst_src_bin <= 4'h0;
459
  else if (decode) inst_src_bin <= ir[11:8];
460
 
461 85 olivier.gi
wire  [15:0] inst_src = inst_type[`INST_TO] ? one_hot16(inst_src_bin)  :
462
                        inst_so[`RETI]      ? 16'h0002                 :
463
                        inst_so[`IRQ]       ? 16'h0001                 :
464
                        inst_type[`INST_SO] ? one_hot16(inst_dest_bin) : 16'h0000;
465 2 olivier.gi
 
466
 
467
//
468
// 4.6) SOURCE ADDRESSING MODES
469
//--------------------------------
470
// Source addressing modes are encoded in a one hot fashion as following:
471
//
472
// 13'b0000000000001: Register direct.
473
// 13'b0000000000010: Register indexed.
474
// 13'b0000000000100: Register indirect.
475
// 13'b0000000001000: Register indirect autoincrement.
476
// 13'b0000000010000: Symbolic (operand is in memory at address PC+x).
477
// 13'b0000000100000: Immediate (operand is next word in the instruction stream).
478
// 13'b0000001000000: Absolute (operand is in memory at address x).
479
// 13'b0000010000000: Constant 4.
480
// 13'b0000100000000: Constant 8.
481
// 13'b0001000000000: Constant 0.
482
// 13'b0010000000000: Constant 1.
483
// 13'b0100000000000: Constant 2.
484
// 13'b1000000000000: Constant -1.
485
 
486
reg [12:0] inst_as_nxt;
487
 
488
wire [3:0] src_reg = inst_type_nxt[`INST_SO] ? ir[3:0] : ir[11:8];
489
 
490
always @(src_reg or ir or inst_type_nxt)
491
  begin
492
     if (inst_type_nxt[`INST_JMP])
493
       inst_as_nxt =  13'b0000000000001;
494
     else if (src_reg==4'h3) // Addressing mode using R3
495
       case (ir[5:4])
496
         2'b11  : inst_as_nxt =  13'b1000000000000;
497
         2'b10  : inst_as_nxt =  13'b0100000000000;
498
         2'b01  : inst_as_nxt =  13'b0010000000000;
499
         default: inst_as_nxt =  13'b0001000000000;
500
       endcase
501
     else if (src_reg==4'h2) // Addressing mode using R2
502
       case (ir[5:4])
503
         2'b11  : inst_as_nxt =  13'b0000100000000;
504
         2'b10  : inst_as_nxt =  13'b0000010000000;
505
         2'b01  : inst_as_nxt =  13'b0000001000000;
506
         default: inst_as_nxt =  13'b0000000000001;
507
       endcase
508
     else if (src_reg==4'h0) // Addressing mode using R0
509
       case (ir[5:4])
510
         2'b11  : inst_as_nxt =  13'b0000000100000;
511
         2'b10  : inst_as_nxt =  13'b0000000000100;
512
         2'b01  : inst_as_nxt =  13'b0000000010000;
513
         default: inst_as_nxt =  13'b0000000000001;
514
       endcase
515
     else                    // General Addressing mode
516
       case (ir[5:4])
517
         2'b11  : inst_as_nxt =  13'b0000000001000;
518
         2'b10  : inst_as_nxt =  13'b0000000000100;
519
         2'b01  : inst_as_nxt =  13'b0000000000010;
520
         default: inst_as_nxt =  13'b0000000000001;
521
       endcase
522
  end
523
assign    is_const = |inst_as_nxt[12:7];
524
 
525
reg [7:0] inst_as;
526
always @(posedge mclk or posedge puc)
527
  if (puc)         inst_as <= 8'h00;
528
  else if (decode) inst_as <= {is_const, inst_as_nxt[6:0]};
529
 
530
 
531
// 13'b0000010000000: Constant 4.
532
// 13'b0000100000000: Constant 8.
533
// 13'b0001000000000: Constant 0.
534
// 13'b0010000000000: Constant 1.
535
// 13'b0100000000000: Constant 2.
536
// 13'b1000000000000: Constant -1.
537
always @(inst_as_nxt)
538
  begin
539
     if (inst_as_nxt[7])        sconst_nxt = 16'h0004;
540
     else if (inst_as_nxt[8])   sconst_nxt = 16'h0008;
541
     else if (inst_as_nxt[9])   sconst_nxt = 16'h0000;
542
     else if (inst_as_nxt[10])  sconst_nxt = 16'h0001;
543
     else if (inst_as_nxt[11])  sconst_nxt = 16'h0002;
544
     else if (inst_as_nxt[12])  sconst_nxt = 16'hffff;
545
     else                       sconst_nxt = 16'h0000;
546
  end
547
 
548
 
549
//
550
// 4.7) DESTINATION ADDRESSING MODES
551
//-----------------------------------
552
// Destination addressing modes are encoded in a one hot fashion as following:
553
//
554
// 8'b00000001: Register direct.
555
// 8'b00000010: Register indexed.
556
// 8'b00010000: Symbolic (operand is in memory at address PC+x).
557
// 8'b01000000: Absolute (operand is in memory at address x).
558
 
559
reg  [7:0] inst_ad_nxt;
560
 
561
wire [3:0] dest_reg = ir[3:0];
562
 
563
always @(dest_reg or ir or inst_type_nxt)
564
  begin
565
     if (~inst_type_nxt[`INST_TO])
566
       inst_ad_nxt =  8'b00000000;
567
     else if (dest_reg==4'h2)   // Addressing mode using R2
568
       case (ir[7])
569
         1'b1   : inst_ad_nxt =  8'b01000000;
570
         default: inst_ad_nxt =  8'b00000001;
571
       endcase
572
     else if (dest_reg==4'h0)   // Addressing mode using R0
573
       case (ir[7])
574 106 olivier.gi
         1'b1   : inst_ad_nxt =  8'b00010000;
575 2 olivier.gi
         default: inst_ad_nxt =  8'b00000001;
576
       endcase
577
     else                       // General Addressing mode
578
       case (ir[7])
579 106 olivier.gi
         1'b1   : inst_ad_nxt =  8'b00000010;
580 2 olivier.gi
         default: inst_ad_nxt =  8'b00000001;
581
       endcase
582
  end
583
 
584
reg [7:0] inst_ad;
585
always @(posedge mclk or posedge puc)
586
  if (puc)         inst_ad <= 8'h00;
587
  else if (decode) inst_ad <= inst_ad_nxt;
588
 
589
 
590
//
591
// 4.8) REMAINING INSTRUCTION DECODING
592
//-------------------------------------
593
 
594
// Operation size
595
reg       inst_bw;
596
always @(posedge mclk or posedge puc)
597
  if (puc)         inst_bw     <= 1'b0;
598 106 olivier.gi
  else if (decode) inst_bw     <= ir[6] & ~inst_type_nxt[`INST_JMP] & ~irq_detect & ~cpu_halt_cmd;
599 2 olivier.gi
 
600
// Extended instruction size
601
assign    inst_sz_nxt = {1'b0,  (inst_as_nxt[`IDX] | inst_as_nxt[`SYMB] | inst_as_nxt[`ABS] | inst_as_nxt[`IMM])} +
602
                        {1'b0, ((inst_ad_nxt[`IDX] | inst_ad_nxt[`SYMB] | inst_ad_nxt[`ABS]) & ~inst_type_nxt[`INST_SO])};
603
always @(posedge mclk or posedge puc)
604
  if (puc)         inst_sz     <= 2'b00;
605
  else if (decode) inst_sz     <= inst_sz_nxt;
606
 
607
 
608
//=============================================================================
609
// 5)  EXECUTION-UNIT STATE MACHINE
610
//=============================================================================
611
 
612
// State machine registers
613
reg  [3:0] e_state;
614
 
615
 
616
// State machine control signals
617
//--------------------------------
618
 
619
wire src_acalc_pre =  inst_as_nxt[`IDX]   | inst_as_nxt[`SYMB]    | inst_as_nxt[`ABS];
620
wire src_rd_pre    =  inst_as_nxt[`INDIR] | inst_as_nxt[`INDIR_I] | inst_as_nxt[`IMM]  | inst_so_nxt[`RETI];
621
wire dst_acalc_pre =  inst_ad_nxt[`IDX]   | inst_ad_nxt[`SYMB]    | inst_ad_nxt[`ABS];
622
wire dst_acalc     =  inst_ad[`IDX]       | inst_ad[`SYMB]        | inst_ad[`ABS];
623
wire dst_rd_pre    =  inst_ad_nxt[`IDX]   | inst_so_nxt[`PUSH]    | inst_so_nxt[`CALL] | inst_so_nxt[`RETI];
624
wire dst_rd        =  inst_ad[`IDX]       | inst_so[`PUSH]        | inst_so[`CALL]     | inst_so[`RETI];
625
 
626
wire inst_branch   =  (inst_ad_nxt[`DIR] & (ir[3:0]==4'h0)) | inst_type_nxt[`INST_JMP] | inst_so_nxt[`RETI];
627
 
628
reg exec_jmp;
629
always @(posedge mclk or posedge puc)
630
  if (puc)                       exec_jmp <= 1'b0;
631
  else if (inst_branch & decode) exec_jmp <= 1'b1;
632
  else if (e_state==`E_JUMP)     exec_jmp <= 1'b0;
633
 
634
reg exec_dst_wr;
635
always @(posedge mclk or posedge puc)
636
  if (puc)                     exec_dst_wr <= 1'b0;
637
  else if (e_state==`E_DST_RD) exec_dst_wr <= 1'b1;
638
  else if (e_state==`E_DST_WR) exec_dst_wr <= 1'b0;
639
 
640
reg exec_src_wr;
641
always @(posedge mclk or posedge puc)
642
  if (puc)                                               exec_src_wr <= 1'b0;
643
  else if (inst_type[`INST_SO] & (e_state==`E_SRC_RD))   exec_src_wr <= 1'b1;
644
  else if ((e_state==`E_SRC_WR) || (e_state==`E_DST_WR)) exec_src_wr <= 1'b0;
645
 
646
reg exec_dext_rdy;
647
always @(posedge mclk or posedge puc)
648
  if (puc)                     exec_dext_rdy <= 1'b0;
649
  else if (e_state==`E_DST_RD) exec_dext_rdy <= 1'b0;
650
  else if (inst_dext_rdy)      exec_dext_rdy <= 1'b1;
651
 
652
// Execution first state
653
wire [3:0] e_first_state = ~dbg_halt_st  & inst_so_nxt[`IRQ] ? `E_IRQ_0  :
654 106 olivier.gi
                            cpu_halt_cmd | (i_state==I_IDLE) ? `E_IDLE   :
655 2 olivier.gi
                            cpuoff                           ? `E_IDLE   :
656
                            src_acalc_pre                    ? `E_SRC_AD :
657
                            src_rd_pre                       ? `E_SRC_RD :
658
                            dst_acalc_pre                    ? `E_DST_AD :
659
                            dst_rd_pre                       ? `E_DST_RD : `E_EXEC;
660
 
661
 
662
// State machine
663
//--------------------------------
664
 
665
// States Transitions
666
always @(e_state       or dst_acalc     or dst_rd   or inst_sext_rdy or
667
         inst_dext_rdy or exec_dext_rdy or exec_jmp or exec_dst_wr   or
668
         e_first_state or exec_src_wr)
669
    case(e_state)
670
      `E_IDLE   : e_state_nxt =  e_first_state;
671
      `E_IRQ_0  : e_state_nxt =  `E_IRQ_1;
672
      `E_IRQ_1  : e_state_nxt =  `E_IRQ_2;
673
      `E_IRQ_2  : e_state_nxt =  `E_IRQ_3;
674
      `E_IRQ_3  : e_state_nxt =  `E_IRQ_4;
675
      `E_IRQ_4  : e_state_nxt =  `E_EXEC;
676
 
677
      `E_SRC_AD : e_state_nxt =  inst_sext_rdy     ? `E_SRC_RD : `E_SRC_AD;
678
 
679
      `E_SRC_RD : e_state_nxt =  dst_acalc         ? `E_DST_AD :
680
                                 dst_rd            ? `E_DST_RD : `E_EXEC;
681
 
682
      `E_DST_AD : e_state_nxt =  (inst_dext_rdy |
683
                                 exec_dext_rdy)    ? `E_DST_RD : `E_DST_AD;
684
 
685
      `E_DST_RD : e_state_nxt =  `E_EXEC;
686
 
687
      `E_EXEC   : e_state_nxt =  exec_dst_wr       ? `E_DST_WR :
688
                                exec_jmp           ? `E_JUMP   :
689
                                exec_src_wr        ? `E_SRC_WR : e_first_state;
690
 
691
      `E_JUMP   : e_state_nxt =  e_first_state;
692
      `E_DST_WR : e_state_nxt =  exec_jmp           ? `E_JUMP   : e_first_state;
693
      `E_SRC_WR : e_state_nxt =  e_first_state;
694
      default  : e_state_nxt =  `E_IRQ_0;
695
    endcase
696
 
697
// State machine
698
always @(posedge mclk or posedge puc)
699
  if (puc)     e_state  <= `E_IRQ_1;
700
  else         e_state  <= e_state_nxt;
701
 
702
 
703
// Frontend State machine control signals
704
//----------------------------------------
705
 
706
wire exec_done = exec_jmp        ? (e_state==`E_JUMP)   :
707
                 exec_dst_wr     ? (e_state==`E_DST_WR) :
708
                 exec_src_wr     ? (e_state==`E_SRC_WR) : (e_state==`E_EXEC);
709
 
710
 
711
//=============================================================================
712
// 6)  EXECUTION-UNIT STATE CONTROL
713
//=============================================================================
714
 
715
//
716
// 6.1) ALU CONTROL SIGNALS
717
//-------------------------------------
718
//
719
// 12'b000000000001: Enable ALU source inverter
720
// 12'b000000000010: Enable Incrementer
721
// 12'b000000000100: Enable Incrementer on carry bit
722
// 12'b000000001000: Select Adder
723
// 12'b000000010000: Select AND
724
// 12'b000000100000: Select OR
725
// 12'b000001000000: Select XOR
726
// 12'b000010000000: Select DADD
727
// 12'b000100000000: Update N, Z & C (C=~Z)
728
// 12'b001000000000: Update all status bits
729
// 12'b010000000000: Update status bit for XOR instruction
730
// 12'b100000000000: Don't write to destination
731
 
732
reg  [11:0] inst_alu;
733
 
734
wire        alu_src_inv   = inst_to_nxt[`SUB]  | inst_to_nxt[`SUBC] |
735
                            inst_to_nxt[`CMP]  | inst_to_nxt[`BIC] ;
736
 
737
wire        alu_inc       = inst_to_nxt[`SUB]  | inst_to_nxt[`CMP];
738
 
739
wire        alu_inc_c     = inst_to_nxt[`ADDC] | inst_to_nxt[`DADD] |
740
                            inst_to_nxt[`SUBC];
741
 
742
wire        alu_add       = inst_to_nxt[`ADD]  | inst_to_nxt[`ADDC]       |
743
                            inst_to_nxt[`SUB]  | inst_to_nxt[`SUBC]       |
744
                            inst_to_nxt[`CMP]  | inst_type_nxt[`INST_JMP] |
745
                            inst_so_nxt[`RETI];
746
 
747
 
748
wire        alu_and       = inst_to_nxt[`AND]  | inst_to_nxt[`BIC]  |
749
                            inst_to_nxt[`BIT];
750
 
751
wire        alu_or        = inst_to_nxt[`BIS];
752
 
753
wire        alu_xor       = inst_to_nxt[`XOR];
754
 
755
wire        alu_dadd      = inst_to_nxt[`DADD];
756
 
757
wire        alu_stat_7    = inst_to_nxt[`BIT]  | inst_to_nxt[`AND]  |
758
                            inst_so_nxt[`SXT];
759
 
760
wire        alu_stat_f    = inst_to_nxt[`ADD]  | inst_to_nxt[`ADDC] |
761
                            inst_to_nxt[`SUB]  | inst_to_nxt[`SUBC] |
762
                            inst_to_nxt[`CMP]  | inst_to_nxt[`DADD] |
763
                            inst_to_nxt[`BIT]  | inst_to_nxt[`XOR]  |
764
                            inst_to_nxt[`AND]  |
765
                            inst_so_nxt[`RRC]  | inst_so_nxt[`RRA]  |
766
                            inst_so_nxt[`SXT];
767
 
768
wire        alu_shift     = inst_so_nxt[`RRC]  | inst_so_nxt[`RRA];
769
 
770
wire        exec_no_wr    = inst_to_nxt[`CMP] | inst_to_nxt[`BIT];
771
 
772
always @(posedge mclk or posedge puc)
773
  if (puc)         inst_alu <= 12'h000;
774
  else if (decode) inst_alu <= {exec_no_wr,
775
                                alu_shift,
776
                                alu_stat_f,
777
                                alu_stat_7,
778
                                alu_dadd,
779
                                alu_xor,
780
                                alu_or,
781
                                alu_and,
782
                                alu_add,
783
                                alu_inc_c,
784
                                alu_inc,
785
                                alu_src_inv};
786
 
787
 
788 34 olivier.gi
endmodule // omsp_frontend
789 33 olivier.gi
 
790 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
791
`else
792 33 olivier.gi
`include "openMSP430_undefines.v"
793 103 olivier.gi
`endif

powered by: WebSVN 2.1.0

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