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

Subversion Repositories openmsp430

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

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

powered by: WebSVN 2.1.0

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