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

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [fpga/] [xilinx_diligent_s3board/] [rtl/] [verilog/] [openmsp430/] [omsp_frontend.v] - Blame information for rev 94

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

powered by: WebSVN 2.1.0

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