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

Subversion Repositories openmsp430

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

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_alu.v
26 2 olivier.gi
// 
27
// *Module Description:
28
//                       openMSP430 ALU
29
//
30
// *Author(s):
31
//              - Olivier Girard,    olgirard@gmail.com
32
//
33
//----------------------------------------------------------------------------
34 17 olivier.gi
// $Rev: 103 $
35
// $LastChangedBy: olivier.girard $
36
// $LastChangedDate: 2011-03-05 15:44:48 +0100 (Sat, 05 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_alu (
44 2 olivier.gi
 
45
// OUTPUTs
46
    alu_out,                       // ALU output value
47
    alu_out_add,                   // ALU adder output value
48
    alu_stat,                      // ALU Status {V,N,Z,C}
49
    alu_stat_wr,                   // ALU Status write {V,N,Z,C}
50
 
51
// INPUTs
52
    dbg_halt_st,                   // Halt/Run status from CPU
53
    exec_cycle,                    // Instruction execution cycle
54
    inst_alu,                      // ALU control signals
55
    inst_bw,                       // Decoded Inst: byte width
56
    inst_jmp,                      // Decoded Inst: Conditional jump
57
    inst_so,                       // Single-operand arithmetic
58
    op_dst,                        // Destination operand
59
    op_src,                        // Source operand
60
    status                         // R2 Status {V,N,Z,C}
61
);
62
 
63
// OUTPUTs
64
//=========
65
output       [15:0] alu_out;       // ALU output value
66
output       [15:0] alu_out_add;   // ALU adder output value
67
output        [3:0] alu_stat;      // ALU Status {V,N,Z,C}
68
output        [3:0] alu_stat_wr;   // ALU Status write {V,N,Z,C}
69
 
70
// INPUTs
71
//=========
72
input               dbg_halt_st;   // Halt/Run status from CPU
73
input               exec_cycle;    // Instruction execution cycle
74
input        [11:0] inst_alu;      // ALU control signals
75
input               inst_bw;       // Decoded Inst: byte width
76
input         [7:0] inst_jmp;      // Decoded Inst: Conditional jump
77
input         [7:0] inst_so;       // Single-operand arithmetic
78
input        [15:0] op_dst;        // Destination operand
79
input        [15:0] op_src;        // Source operand
80
input         [3:0] status;        // R2 Status {V,N,Z,C}
81
 
82
 
83
//=============================================================================
84
// 1)  FUNCTIONS
85
//=============================================================================
86
 
87
function [4:0] bcd_add;
88
 
89
   input [3:0] X;
90
   input [3:0] Y;
91
   input       C;
92
 
93
   reg   [4:0] Z;
94
   begin
95
      Z = {1'b0,X}+{1'b0,Y}+C;
96
      if (Z<10) bcd_add = Z;
97
      else      bcd_add = Z+6;
98
   end
99
 
100
endfunction
101
 
102
 
103
//=============================================================================
104
// 2)  INSTRUCTION FETCH/DECODE CONTROL STATE MACHINE
105
//=============================================================================
106
// SINGLE-OPERAND ARITHMETIC:
107
//-----------------------------------------------------------------------------
108
//   Mnemonic   S-Reg,   Operation                               Status bits
109
//              D-Reg,                                            V  N  Z  C
110
//
111
//   RRC         dst     C->MSB->...LSB->C                        *  *  *  *
112
//   RRA         dst     MSB->MSB->...LSB->C                      0  *  *  *
113
//   SWPB        dst     Swap bytes                               -  -  -  -
114
//   SXT         dst     Bit7->Bit8...Bit15                       0  *  *  *
115
//   PUSH        src     SP-2->SP, src->@SP                       -  -  -  -
116
//   CALL        dst     SP-2->SP, PC+2->@SP, dst->PC             -  -  -  -
117
//   RETI                TOS->SR, SP+2->SP, TOS->PC, SP+2->SP     *  *  *  *
118
//
119
//-----------------------------------------------------------------------------
120
// TWO-OPERAND ARITHMETIC:
121
//-----------------------------------------------------------------------------
122
//   Mnemonic   S-Reg,   Operation                               Status bits
123
//              D-Reg,                                            V  N  Z  C
124
//
125
//   MOV       src,dst    src            -> dst                   -  -  -  -
126
//   ADD       src,dst    src +  dst     -> dst                   *  *  *  *
127
//   ADDC      src,dst    src +  dst + C -> dst                   *  *  *  *
128
//   SUB       src,dst    dst + ~src + 1 -> dst                   *  *  *  *
129
//   SUBC      src,dst    dst + ~src + C -> dst                   *  *  *  *
130
//   CMP       src,dst    dst + ~src + 1                          *  *  *  *
131
//   DADD      src,dst    src +  dst + C -> dst (decimaly)        *  *  *  *
132
//   BIT       src,dst    src &  dst                              0  *  *  *
133
//   BIC       src,dst   ~src &  dst     -> dst                   -  -  -  -
134
//   BIS       src,dst    src |  dst     -> dst                   -  -  -  -
135
//   XOR       src,dst    src ^  dst     -> dst                   *  *  *  *
136
//   AND       src,dst    src &  dst     -> dst                   0  *  *  *
137
//
138
//-----------------------------------------------------------------------------
139
// * the status bit is affected
140
// - the status bit is not affected
141
// 0 the status bit is cleared
142
// 1 the status bit is set
143
//-----------------------------------------------------------------------------
144
 
145
// Invert source for substract and compare instructions.
146
wire        op_src_inv_cmd = exec_cycle & (inst_alu[`ALU_SRC_INV]);
147
wire [15:0] op_src_inv     = {16{op_src_inv_cmd}} ^ op_src;
148
 
149
 
150
// Mask the bit 8 for the Byte instructions for correct flags generation
151
wire        op_bit8_msk     = ~exec_cycle | ~inst_bw;
152 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]};
153
wire [16:0] op_dst_in       = {1'b0, {op_dst[15:8]     & {8{op_bit8_msk}}}, op_dst[7:0]};
154 2 olivier.gi
 
155
// Clear the source operand (= jump offset) for conditional jumps
156
wire        jmp_not_taken  = (inst_jmp[`JL]  & ~(status[3]^status[2])) |
157
                             (inst_jmp[`JGE] &  (status[3]^status[2])) |
158
                             (inst_jmp[`JN]  &  ~status[2])            |
159
                             (inst_jmp[`JC]  &  ~status[0])            |
160
                             (inst_jmp[`JNC] &   status[0])            |
161
                             (inst_jmp[`JEQ] &  ~status[1])            |
162
                             (inst_jmp[`JNE] &   status[1]);
163
wire [16:0] op_src_in_jmp  = op_src_in & {17{~jmp_not_taken}};
164
 
165
// Adder / AND / OR / XOR
166
wire [16:0] alu_add        = op_src_in_jmp + op_dst_in;
167
wire [16:0] alu_and        = op_src_in     & op_dst_in;
168
wire [16:0] alu_or         = op_src_in     | op_dst_in;
169
wire [16:0] alu_xor        = op_src_in     ^ op_dst_in;
170
 
171
 
172
// Incrementer
173
wire        alu_inc         = exec_cycle & ((inst_alu[`ALU_INC_C] & status[0]) |
174
                                             inst_alu[`ALU_INC]);
175
wire [16:0] alu_add_inc    = alu_add + {16'h0000, alu_inc};
176
 
177
 
178
 
179
// Decimal adder (DADD)
180
wire  [4:0] alu_dadd0      = bcd_add(op_src_in[3:0],   op_dst_in[3:0],  status[0]);
181
wire  [4:0] alu_dadd1      = bcd_add(op_src_in[7:4],   op_dst_in[7:4],  alu_dadd0[4]);
182
wire  [4:0] alu_dadd2      = bcd_add(op_src_in[11:8],  op_dst_in[11:8], alu_dadd1[4]);
183
wire  [4:0] alu_dadd3      = bcd_add(op_src_in[15:12], op_dst_in[15:12],alu_dadd2[4]);
184
wire [16:0] alu_dadd       = {alu_dadd3, alu_dadd2[3:0], alu_dadd1[3:0], alu_dadd0[3:0]};
185
 
186
 
187
// Shifter for rotate instructions (RRC & RRA)
188
wire        alu_shift_msb  = inst_so[`RRC] ? status[0]     :
189
                             inst_bw       ? op_src[7]     : op_src[15];
190
wire        alu_shift_7    = inst_bw       ? alu_shift_msb : op_src[8];
191
wire [16:0] alu_shift      = {1'b0, alu_shift_msb, op_src[15:9], alu_shift_7, op_src[7:1]};
192
 
193
 
194
// Swap bytes / Extend Sign
195
wire [16:0] alu_swpb       = {1'b0, op_src[7:0],op_src[15:8]};
196
wire [16:0] alu_sxt        = {1'b0, {8{op_src[7]}},op_src[7:0]};
197
 
198
 
199
// Combine short paths toghether to simplify final ALU mux
200
wire        alu_short_thro = ~(inst_alu[`ALU_AND]   |
201
                               inst_alu[`ALU_OR]    |
202
                               inst_alu[`ALU_XOR]   |
203
                               inst_alu[`ALU_SHIFT] |
204
                               inst_so[`SWPB]       |
205
                               inst_so[`SXT]);
206
 
207
wire [16:0] alu_short      = ({16{inst_alu[`ALU_AND]}}   & alu_and)   |
208
                             ({16{inst_alu[`ALU_OR]}}    & alu_or)    |
209
                             ({16{inst_alu[`ALU_XOR]}}   & alu_xor)   |
210
                             ({16{inst_alu[`ALU_SHIFT]}} & alu_shift) |
211
                             ({16{inst_so[`SWPB]}}       & alu_swpb)  |
212
                             ({16{inst_so[`SXT]}}        & alu_sxt)   |
213
                             ({16{alu_short_thro}}       & op_src_in);
214
 
215
 
216
// ALU output mux
217
wire [16:0] alu_out_nxt    = (inst_so[`IRQ] | dbg_halt_st |
218
                              inst_alu[`ALU_ADD]) ? alu_add_inc :
219
                              inst_alu[`ALU_DADD] ? alu_dadd    : alu_short;
220
 
221
assign      alu_out        =  alu_out_nxt[15:0];
222
assign      alu_out_add    =  alu_add[15:0];
223
 
224
 
225
//-----------------------------------------------------------------------------
226
// STATUS FLAG GENERATION
227
//-----------------------------------------------------------------------------
228
 
229
wire    V_xor       = inst_bw ? (op_src_in[7]  & op_dst_in[7])  :
230
                                (op_src_in[15] & op_dst_in[15]);
231
 
232
wire    V           = inst_bw ? ((~op_src_in[7]  & ~op_dst_in[7]  &  alu_out[7])  |
233
                                 ( op_src_in[7]  &  op_dst_in[7]  & ~alu_out[7])) :
234
                                ((~op_src_in[15] & ~op_dst_in[15] &  alu_out[15]) |
235
                                 ( op_src_in[15] &  op_dst_in[15] & ~alu_out[15]));
236
 
237
wire    N           = inst_bw ?  alu_out[7]       : alu_out[15];
238
wire    Z           = inst_bw ? (alu_out[7:0]==0) : (alu_out==0);
239
wire    C           = inst_bw ?  alu_out[8]       : alu_out_nxt[16];
240
 
241
assign  alu_stat    = inst_alu[`ALU_SHIFT]  ? {1'b0, N,Z,op_src_in[0]} :
242
                      inst_alu[`ALU_STAT_7] ? {1'b0, N,Z,~Z}           :
243
                      inst_alu[`ALU_XOR]    ? {V_xor,N,Z,~Z}           : {V,N,Z,C};
244
 
245
assign  alu_stat_wr = (inst_alu[`ALU_STAT_F] & exec_cycle) ? 4'b1111 : 4'b0000;
246
 
247
 
248 34 olivier.gi
endmodule // omsp_alu
249 2 olivier.gi
 
250 103 olivier.gi
`ifdef OMSP_NO_INCLUDE
251
`else
252 33 olivier.gi
`include "openMSP430_undefines.v"
253 103 olivier.gi
`endif

powered by: WebSVN 2.1.0

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