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

Subversion Repositories openmsp430

[/] [openmsp430/] [trunk/] [core/] [rtl/] [verilog/] [omsp_alu.v] - Blame information for rev 155

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

Line No. Rev Author Line
1 2 olivier.gi
//----------------------------------------------------------------------------
2 117 olivier.gi
// Copyright (C) 2009 , Olivier Girard
3 2 olivier.gi
//
4 117 olivier.gi
// Redistribution and use in source and binary forms, with or without
5
// modification, are permitted provided that the following conditions
6
// are met:
7
//     * Redistributions of source code must retain the above copyright
8
//       notice, this list of conditions and the following disclaimer.
9
//     * Redistributions in binary form must reproduce the above copyright
10
//       notice, this list of conditions and the following disclaimer in the
11
//       documentation and/or other materials provided with the distribution.
12
//     * Neither the name of the authors nor the names of its contributors
13
//       may be used to endorse or promote products derived from this software
14
//       without specific prior written permission.
15 2 olivier.gi
//
16 117 olivier.gi
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
21
// OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26
// THE POSSIBILITY OF SUCH DAMAGE
27 2 olivier.gi
//
28
//----------------------------------------------------------------------------
29
//
30 34 olivier.gi
// *File Name: omsp_alu.v
31 2 olivier.gi
// 
32
// *Module Description:
33
//                       openMSP430 ALU
34
//
35
// *Author(s):
36
//              - Olivier Girard,    olgirard@gmail.com
37
//
38
//----------------------------------------------------------------------------
39 17 olivier.gi
// $Rev: 134 $
40
// $LastChangedBy: olivier.girard $
41
// $LastChangedDate: 2012-03-22 21:31:06 +0100 (Thu, 22 Mar 2012) $
42
//----------------------------------------------------------------------------
43 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
44
`else
45 23 olivier.gi
`include "openMSP430_defines.v"
46 103 olivier.gi
`endif
47 2 olivier.gi
 
48 34 olivier.gi
module  omsp_alu (
49 2 olivier.gi
 
50
// OUTPUTs
51
    alu_out,                       // ALU output value
52
    alu_out_add,                   // ALU adder output value
53
    alu_stat,                      // ALU Status {V,N,Z,C}
54
    alu_stat_wr,                   // ALU Status write {V,N,Z,C}
55
 
56
// INPUTs
57
    dbg_halt_st,                   // Halt/Run status from CPU
58
    exec_cycle,                    // Instruction execution cycle
59
    inst_alu,                      // ALU control signals
60
    inst_bw,                       // Decoded Inst: byte width
61
    inst_jmp,                      // Decoded Inst: Conditional jump
62
    inst_so,                       // Single-operand arithmetic
63
    op_dst,                        // Destination operand
64
    op_src,                        // Source operand
65
    status                         // R2 Status {V,N,Z,C}
66
);
67
 
68
// OUTPUTs
69
//=========
70
output       [15:0] alu_out;       // ALU output value
71
output       [15:0] alu_out_add;   // ALU adder output value
72
output        [3:0] alu_stat;      // ALU Status {V,N,Z,C}
73
output        [3:0] alu_stat_wr;   // ALU Status write {V,N,Z,C}
74
 
75
// INPUTs
76
//=========
77
input               dbg_halt_st;   // Halt/Run status from CPU
78
input               exec_cycle;    // Instruction execution cycle
79
input        [11:0] inst_alu;      // ALU control signals
80
input               inst_bw;       // Decoded Inst: byte width
81
input         [7:0] inst_jmp;      // Decoded Inst: Conditional jump
82
input         [7:0] inst_so;       // Single-operand arithmetic
83
input        [15:0] op_dst;        // Destination operand
84
input        [15:0] op_src;        // Source operand
85
input         [3:0] status;        // R2 Status {V,N,Z,C}
86
 
87
 
88
//=============================================================================
89
// 1)  FUNCTIONS
90
//=============================================================================
91
 
92
function [4:0] bcd_add;
93
 
94
   input [3:0] X;
95
   input [3:0] Y;
96 134 olivier.gi
   input       C_;
97 2 olivier.gi
 
98 134 olivier.gi
   reg   [4:0] Z_;
99 2 olivier.gi
   begin
100 134 olivier.gi
      Z_ = {1'b0,X}+{1'b0,Y}+{4'b0,C_};
101
      if (Z_<5'd10) bcd_add = Z_;
102
      else          bcd_add = Z_+5'd6;
103 2 olivier.gi
   end
104
 
105
endfunction
106
 
107
 
108
//=============================================================================
109
// 2)  INSTRUCTION FETCH/DECODE CONTROL STATE MACHINE
110
//=============================================================================
111
// SINGLE-OPERAND ARITHMETIC:
112
//-----------------------------------------------------------------------------
113
//   Mnemonic   S-Reg,   Operation                               Status bits
114
//              D-Reg,                                            V  N  Z  C
115
//
116
//   RRC         dst     C->MSB->...LSB->C                        *  *  *  *
117
//   RRA         dst     MSB->MSB->...LSB->C                      0  *  *  *
118
//   SWPB        dst     Swap bytes                               -  -  -  -
119
//   SXT         dst     Bit7->Bit8...Bit15                       0  *  *  *
120
//   PUSH        src     SP-2->SP, src->@SP                       -  -  -  -
121
//   CALL        dst     SP-2->SP, PC+2->@SP, dst->PC             -  -  -  -
122
//   RETI                TOS->SR, SP+2->SP, TOS->PC, SP+2->SP     *  *  *  *
123
//
124
//-----------------------------------------------------------------------------
125
// TWO-OPERAND ARITHMETIC:
126
//-----------------------------------------------------------------------------
127
//   Mnemonic   S-Reg,   Operation                               Status bits
128
//              D-Reg,                                            V  N  Z  C
129
//
130
//   MOV       src,dst    src            -> dst                   -  -  -  -
131
//   ADD       src,dst    src +  dst     -> dst                   *  *  *  *
132
//   ADDC      src,dst    src +  dst + C -> dst                   *  *  *  *
133
//   SUB       src,dst    dst + ~src + 1 -> dst                   *  *  *  *
134
//   SUBC      src,dst    dst + ~src + C -> dst                   *  *  *  *
135
//   CMP       src,dst    dst + ~src + 1                          *  *  *  *
136
//   DADD      src,dst    src +  dst + C -> dst (decimaly)        *  *  *  *
137
//   BIT       src,dst    src &  dst                              0  *  *  *
138
//   BIC       src,dst   ~src &  dst     -> dst                   -  -  -  -
139
//   BIS       src,dst    src |  dst     -> dst                   -  -  -  -
140
//   XOR       src,dst    src ^  dst     -> dst                   *  *  *  *
141
//   AND       src,dst    src &  dst     -> dst                   0  *  *  *
142
//
143
//-----------------------------------------------------------------------------
144
// * the status bit is affected
145
// - the status bit is not affected
146
// 0 the status bit is cleared
147
// 1 the status bit is set
148
//-----------------------------------------------------------------------------
149
 
150
// Invert source for substract and compare instructions.
151
wire        op_src_inv_cmd = exec_cycle & (inst_alu[`ALU_SRC_INV]);
152
wire [15:0] op_src_inv     = {16{op_src_inv_cmd}} ^ op_src;
153
 
154
 
155
// Mask the bit 8 for the Byte instructions for correct flags generation
156
wire        op_bit8_msk     = ~exec_cycle | ~inst_bw;
157 101 olivier.gi
wire [16:0] op_src_in       = {1'b0, {op_src_inv[15:8] & {8{op_bit8_msk}}}, op_src_inv[7:0]};
158
wire [16:0] op_dst_in       = {1'b0, {op_dst[15:8]     & {8{op_bit8_msk}}}, op_dst[7:0]};
159 2 olivier.gi
 
160
// Clear the source operand (= jump offset) for conditional jumps
161
wire        jmp_not_taken  = (inst_jmp[`JL]  & ~(status[3]^status[2])) |
162
                             (inst_jmp[`JGE] &  (status[3]^status[2])) |
163
                             (inst_jmp[`JN]  &  ~status[2])            |
164
                             (inst_jmp[`JC]  &  ~status[0])            |
165
                             (inst_jmp[`JNC] &   status[0])            |
166
                             (inst_jmp[`JEQ] &  ~status[1])            |
167
                             (inst_jmp[`JNE] &   status[1]);
168
wire [16:0] op_src_in_jmp  = op_src_in & {17{~jmp_not_taken}};
169
 
170
// Adder / AND / OR / XOR
171
wire [16:0] alu_add        = op_src_in_jmp + op_dst_in;
172
wire [16:0] alu_and        = op_src_in     & op_dst_in;
173
wire [16:0] alu_or         = op_src_in     | op_dst_in;
174
wire [16:0] alu_xor        = op_src_in     ^ op_dst_in;
175
 
176
 
177
// Incrementer
178
wire        alu_inc         = exec_cycle & ((inst_alu[`ALU_INC_C] & status[0]) |
179
                                             inst_alu[`ALU_INC]);
180
wire [16:0] alu_add_inc    = alu_add + {16'h0000, alu_inc};
181
 
182
 
183
 
184
// Decimal adder (DADD)
185
wire  [4:0] alu_dadd0      = bcd_add(op_src_in[3:0],   op_dst_in[3:0],  status[0]);
186
wire  [4:0] alu_dadd1      = bcd_add(op_src_in[7:4],   op_dst_in[7:4],  alu_dadd0[4]);
187
wire  [4:0] alu_dadd2      = bcd_add(op_src_in[11:8],  op_dst_in[11:8], alu_dadd1[4]);
188
wire  [4:0] alu_dadd3      = bcd_add(op_src_in[15:12], op_dst_in[15:12],alu_dadd2[4]);
189
wire [16:0] alu_dadd       = {alu_dadd3, alu_dadd2[3:0], alu_dadd1[3:0], alu_dadd0[3:0]};
190
 
191
 
192
// Shifter for rotate instructions (RRC & RRA)
193
wire        alu_shift_msb  = inst_so[`RRC] ? status[0]     :
194
                             inst_bw       ? op_src[7]     : op_src[15];
195
wire        alu_shift_7    = inst_bw       ? alu_shift_msb : op_src[8];
196
wire [16:0] alu_shift      = {1'b0, alu_shift_msb, op_src[15:9], alu_shift_7, op_src[7:1]};
197
 
198
 
199
// Swap bytes / Extend Sign
200
wire [16:0] alu_swpb       = {1'b0, op_src[7:0],op_src[15:8]};
201
wire [16:0] alu_sxt        = {1'b0, {8{op_src[7]}},op_src[7:0]};
202
 
203
 
204
// Combine short paths toghether to simplify final ALU mux
205
wire        alu_short_thro = ~(inst_alu[`ALU_AND]   |
206
                               inst_alu[`ALU_OR]    |
207
                               inst_alu[`ALU_XOR]   |
208
                               inst_alu[`ALU_SHIFT] |
209
                               inst_so[`SWPB]       |
210
                               inst_so[`SXT]);
211
 
212 111 olivier.gi
wire [16:0] alu_short      = ({17{inst_alu[`ALU_AND]}}   & alu_and)   |
213
                             ({17{inst_alu[`ALU_OR]}}    & alu_or)    |
214
                             ({17{inst_alu[`ALU_XOR]}}   & alu_xor)   |
215
                             ({17{inst_alu[`ALU_SHIFT]}} & alu_shift) |
216
                             ({17{inst_so[`SWPB]}}       & alu_swpb)  |
217
                             ({17{inst_so[`SXT]}}        & alu_sxt)   |
218
                             ({17{alu_short_thro}}       & op_src_in);
219 2 olivier.gi
 
220
 
221
// ALU output mux
222
wire [16:0] alu_out_nxt    = (inst_so[`IRQ] | dbg_halt_st |
223
                              inst_alu[`ALU_ADD]) ? alu_add_inc :
224
                              inst_alu[`ALU_DADD] ? alu_dadd    : alu_short;
225
 
226
assign      alu_out        =  alu_out_nxt[15:0];
227
assign      alu_out_add    =  alu_add[15:0];
228
 
229
 
230
//-----------------------------------------------------------------------------
231
// STATUS FLAG GENERATION
232
//-----------------------------------------------------------------------------
233
 
234
wire    V_xor       = inst_bw ? (op_src_in[7]  & op_dst_in[7])  :
235
                                (op_src_in[15] & op_dst_in[15]);
236
 
237
wire    V           = inst_bw ? ((~op_src_in[7]  & ~op_dst_in[7]  &  alu_out[7])  |
238
                                 ( op_src_in[7]  &  op_dst_in[7]  & ~alu_out[7])) :
239
                                ((~op_src_in[15] & ~op_dst_in[15] &  alu_out[15]) |
240
                                 ( op_src_in[15] &  op_dst_in[15] & ~alu_out[15]));
241
 
242
wire    N           = inst_bw ?  alu_out[7]       : alu_out[15];
243
wire    Z           = inst_bw ? (alu_out[7:0]==0) : (alu_out==0);
244
wire    C           = inst_bw ?  alu_out[8]       : alu_out_nxt[16];
245
 
246
assign  alu_stat    = inst_alu[`ALU_SHIFT]  ? {1'b0, N,Z,op_src_in[0]} :
247
                      inst_alu[`ALU_STAT_7] ? {1'b0, N,Z,~Z}           :
248
                      inst_alu[`ALU_XOR]    ? {V_xor,N,Z,~Z}           : {V,N,Z,C};
249
 
250
assign  alu_stat_wr = (inst_alu[`ALU_STAT_F] & exec_cycle) ? 4'b1111 : 4'b0000;
251
 
252
 
253 34 olivier.gi
endmodule // omsp_alu
254 2 olivier.gi
 
255 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
256
`else
257 33 olivier.gi
`include "openMSP430_undefines.v"
258 103 olivier.gi
`endif

powered by: WebSVN 2.1.0

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