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

Subversion Repositories 8051

[/] [8051/] [trunk/] [rtl/] [verilog/] [oc8051_alu.v] - Blame information for rev 132

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

Line No. Rev Author Line
1 82 simont
//////////////////////////////////////////////////////////////////////
2
////                                                              ////
3
//// alu for 8051 Core                                            ////
4
////                                                              ////
5
//// This file is part of the 8051 cores project                  ////
6
//// http://www.opencores.org/cores/8051/                         ////
7
////                                                              ////
8
//// Description                                                  ////
9
//// Implementation of aritmetic unit  according to               ////
10
//// 8051 IP core specification document. Uses divide.v and       ////
11
//// multiply.v                                                   ////
12
////                                                              ////
13
//// To Do:                                                       ////
14
////  pc signed add                                               ////
15
////                                                              ////
16
//// Author(s):                                                   ////
17
//// - Simon Teran, simont@opencores.org                          ////
18
////                                                              ////
19
//////////////////////////////////////////////////////////////////////
20
////                                                              ////
21
//// Copyright (C) 2001 Authors and OPENCORES.ORG                 ////
22
////                                                              ////
23
//// This source file may be used and distributed without         ////
24
//// restriction provided that this copyright statement is not    ////
25
//// removed from the file and that any derivative work contains  ////
26
//// the original copyright notice and the associated disclaimer. ////
27
////                                                              ////
28
//// This source file is free software; you can redistribute it   ////
29
//// and/or modify it under the terms of the GNU Lesser General   ////
30
//// Public License as published by the Free Software Foundation; ////
31
//// either version 2.1 of the License, or (at your option) any   ////
32
//// later version.                                               ////
33
////                                                              ////
34
//// This source is distributed in the hope that it will be       ////
35
//// useful, but WITHOUT ANY WARRANTY; without even the implied   ////
36
//// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      ////
37
//// PURPOSE. See the GNU Lesser General Public License for more  ////
38
//// details.                                                     ////
39
////                                                              ////
40
//// You should have received a copy of the GNU Lesser General    ////
41
//// Public License along with this source; if not, download it   ////
42
//// from http://www.opencores.org/lgpl.shtml                     ////
43
////                                                              ////
44
//////////////////////////////////////////////////////////////////////
45
//
46
// CVS Revision History
47
//
48
// $Log: not supported by cvs2svn $
49 132 simont
// Revision 1.11  2003/04/14 14:29:42  simont
50
// fiz bug iv pcs operation.
51
//
52 123 simont
// Revision 1.10  2003/01/13 14:14:40  simont
53
// replace some modules
54
//
55 82 simont
// Revision 1.9  2002/09/30 17:33:59  simont
56
// prepared header
57
//
58
//
59
 
60
// synopsys translate_off
61
`include "oc8051_timescale.v"
62
// synopsys translate_on
63
 
64
`include "oc8051_defines.v"
65
 
66
 
67
 
68 132 simont
module oc8051_alu (clk, rst, op_code, src1, src2, src3, srcCy, srcAc, bit_in, des1, des2, desCy,
69 82 simont
                   desAc, desOv);
70
//
71
// op_code      (in)  operation code [oc8051_decoder.alu_op -r]
72
// src1         (in)  first operand [oc8051_alu_src1_sel.des]
73
// src2         (in)  second operand [oc8051_alu_src2_sel.des]
74
// src3         (in)  third operand [oc8051_alu_src3_sel.des]
75
// srcCy        (in)  carry input [oc8051_cy_select.data_out]
76
// srcAc        (in)  auxiliary carry input [oc8051_psw.data_out[6] ]
77
// bit_in       (in)  bit input, used for logic operatins on bits [oc8051_ram_sel.bit_out]
78
// des1         (out)
79
// des2         (out)
80
// desCy        (out) carry output [oc8051_ram_top.bit_data_in, oc8051_acc.bit_in, oc8051_b_register.bit_in, oc8051_psw.cy_in, oc8051_ports.bit_in]
81
// desAc        (out) auxiliary carry output [oc8051_psw.ac_in]
82
// desOv        (out) Overflow output [oc8051_psw.ov_in]
83
//
84
 
85 132 simont
input srcCy, srcAc, bit_in, clk, rst;
86 82 simont
input [3:0] op_code;
87
input [7:0] src1, src2, src3;
88
output desCy, desAc, desOv;
89
output [7:0] des1, des2;
90
 
91
reg desCy, desAc, desOv;
92
reg [7:0] des1, des2;
93
 
94
 
95
//
96
//add
97
//
98
wire [4:0] add1, add2, add3, add4;
99
wire [3:0] add5, add6, add7, add8;
100
wire [1:0] add9, adda, addb, addc;
101
 
102
//
103
//sub
104
//
105
wire [4:0] sub1, sub2, sub3, sub4;
106
wire [3:0] sub5, sub6, sub7, sub8;
107
wire [1:0] sub9, suba, subb, subc;
108
 
109
//
110
//mul
111
//
112
  wire [7:0] mulsrc1, mulsrc2;
113
  wire mulOv;
114
  reg enable_mul;
115
 
116
//
117
//div
118
//
119
wire [7:0] divsrc1,divsrc2;
120
wire divOv;
121
reg enable_div;
122
 
123
//
124
//da
125
//
126
reg da_tmp;
127
//reg [8:0] da1;
128
 
129
oc8051_multiply oc8051_mul1(.clk(clk), .rst(rst), .enable(enable_mul), .src1(src1), .src2(src2), .des1(mulsrc1), .des2(mulsrc2), .desOv(mulOv));
130
oc8051_divide oc8051_div1(.clk(clk), .rst(rst), .enable(enable_div), .src1(src1), .src2(src2), .des1(divsrc1), .des2(divsrc2), .desOv(divOv));
131
 
132
/* Add */
133
assign add1 = {1'b0,src1[3:0]};
134
assign add2 = {1'b0,src2[3:0]};
135
assign add3 = {3'b000,srcCy};
136
assign add4 = add1+add2+add3;
137
 
138
assign add5 = {1'b0,src1[6:4]};
139
assign add6 = {1'b0,src2[6:4]};
140
assign add7 = {1'b0,1'b0,1'b0,add4[4]};
141
assign add8 = add5+add6+add7;
142
 
143
assign add9 = {1'b0,src1[7]};
144
assign adda = {1'b0,src2[7]};
145
assign addb = {1'b0,add8[3]};
146
assign addc = add9+adda+addb;
147
 
148
/* Sub */
149
assign sub1 = {1'b1,src1[3:0]};
150
assign sub2 = {1'b0,src2[3:0]};
151
assign sub3 = {1'b0,1'b0,1'b0,srcCy};
152
assign sub4 = sub1-sub2-sub3;
153
 
154
assign sub5 = {1'b1,src1[6:4]};
155
assign sub6 = {1'b0,src2[6:4]};
156
assign sub7 = {1'b0,1'b0,1'b0, !sub4[4]};
157
assign sub8 = sub5-sub6-sub7;
158
 
159
assign sub9 = {1'b1,src1[7]};
160
assign suba = {1'b0,src2[7]};
161
assign subb = {1'b0,!sub8[3]};
162
assign subc = sub9-suba-subb;
163
 
164
 
165
always @(op_code or src1 or src2 or srcCy or srcAc or bit_in or src3 or mulsrc1 or mulsrc2 or mulOv or divsrc1 or divsrc2 or divOv or addc or add8 or add4 or sub4 or sub8 or subc or da_tmp)
166
begin
167
 
168
  case (op_code)
169
//operation add
170
    `OC8051_ALU_ADD: begin
171
      des1 = {addc[0],add8[2:0],add4[3:0]};
172
      des2 = src3+ {7'b0, addc[1]};
173
      desCy = addc[1];
174
      desAc = add4[4];
175
      desOv = addc[1] ^ add8[3];
176
 
177
      enable_mul = 1'b0;
178
      enable_div = 1'b0;
179
    end
180
//operation subtract
181
    `OC8051_ALU_SUB: begin
182
      des1 = {subc[0],sub8[2:0],sub4[3:0]};
183
      des2 = 8'h00;
184
      desCy = !subc[1];
185
      desAc = !sub4[4];
186
      desOv = !subc[1] ^ sub8[3];
187
 
188
      enable_mul = 1'b0;
189
      enable_div = 1'b0;
190
    end
191
//operation multiply
192
    `OC8051_ALU_MUL: begin
193
      des1 = mulsrc1;
194
      des2 = mulsrc2;
195
      desOv = mulOv;
196
      desCy = 1'b0;
197
      desAc = 1'bx;
198
      enable_mul = 1'b1;
199
      enable_div = 1'b0;
200
    end
201
//operation divide
202
    `OC8051_ALU_DIV: begin
203
      des1 = divsrc1;
204
      des2 = divsrc2;
205
      desOv = divOv;
206
      desAc = 1'bx;
207
      desCy = 1'b0;
208
      enable_mul = 1'b0;
209
      enable_div = 1'b1;
210
    end
211
//operation decimal adjustment
212
    `OC8051_ALU_DA: begin
213
 
214
      if (srcAc==1'b1 | src1[3:0]>4'b1001) {da_tmp, des1[3:0]} = {1'b0, src1[3:0]}+ 5'b00110;
215
      else {da_tmp, des1[3:0]} = {1'b0, src1[3:0]};
216
 
217
      if (srcCy==1'b1 | src1[7:4]>4'b1001)
218
        {desCy, des1[7:4]} = {srcCy, src1[7:4]}+ 5'b00110 + {4'b0, da_tmp};
219
      else {desCy, des1[7:4]} = {srcCy, src1[7:4]} + {4'b0, da_tmp};
220
 
221
      des2 = 8'h00;
222
      desAc = 1'b0;
223
      desOv = 1'b0;
224
      enable_mul = 1'b0;
225
      enable_div = 1'b0;
226
    end
227
//operation not
228
// bit operation not
229
    `OC8051_ALU_NOT: begin
230
      des1 = ~src1;
231
      des2 = 8'h00;
232
      desCy = !srcCy;
233
      desAc = 1'bx;
234
      desOv = 1'bx;
235
      enable_mul = 1'b0;
236
      enable_div = 1'b0;
237
    end
238
//operation and
239
//bit operation and
240
    `OC8051_ALU_AND: begin
241
      des1 = src1 & src2;
242
      des2 = 8'h00;
243
      desCy = srcCy & bit_in;
244
      desAc = 1'bx;
245
      desOv = 1'bx;
246
      enable_mul = 1'b0;
247
      enable_div = 1'b0;
248
    end
249
//operation xor
250
// bit operation xor
251
    `OC8051_ALU_XOR: begin
252
      des1 = src1 ^ src2;
253
      des2 = 8'h00;
254
      desCy = srcCy ^ bit_in;
255
      desAc = 1'bx;
256
      desOv = 1'bx;
257
      enable_mul = 1'b0;
258
      enable_div = 1'b0;
259
    end
260
//operation or
261
// bit operation or
262
    `OC8051_ALU_OR: begin
263
      des1 = src1 | src2;
264
      des2 = 8'h00;
265
      desCy = srcCy | bit_in;
266
      desAc = 1'bx;
267
      desOv = 1'bx;
268
      enable_mul = 1'b0;
269
      enable_div = 1'b0;
270
    end
271
//operation rotate left
272
// bit operation cy= cy or (not ram)
273
    `OC8051_ALU_RL: begin
274
      des1 = {src1[6:0], src1[7]};
275
      des2 = 8'h00;
276
      desCy = srcCy | !bit_in;
277
      desAc = 1'bx;
278
      desOv = 1'bx;
279
      enable_mul = 1'b0;
280
      enable_div = 1'b0;
281
    end
282
//operation rotate left with carry and swap nibbles
283
    `OC8051_ALU_RLC: begin
284
      des1 = {src1[6:0], srcCy};
285
      des2 = {src1[3:0], src1[7:4]};
286
      desCy = src1[7];
287
      desAc = 1'b0;
288
      desOv = 1'b0;
289
      enable_mul = 1'b0;
290
      enable_div = 1'b0;
291
    end
292
//operation rotate right
293
    `OC8051_ALU_RR: begin
294
      des1 = {src1[0], src1[7:1]};
295
      des2 = 8'h00;
296
      desCy = srcCy & !bit_in;
297
      desAc = 1'b0;
298
      desOv = 1'b0;
299
      enable_mul = 1'b0;
300
      enable_div = 1'b0;
301
    end
302
//operation rotate right with carry
303
    `OC8051_ALU_RRC: begin
304
      des1 = {srcCy, src1[7:1]};
305
      des2 = 8'h00;
306
      desCy = src1[0];
307
      desAc = 1'b0;
308
      desOv = 1'b0;
309
      enable_mul = 1'b0;
310
      enable_div = 1'b0;
311
    end
312
//operation pcs Add
313 132 simont
/*    `OC8051_ALU_PCS: begin
314 82 simont
      if (src1[7]) begin
315 123 simont
        {desCy, des1} = {1'b0, src2} + {1'b0, src1};
316
        des2 = {1'b0, src3} - {8'h0, !desCy};
317 82 simont
      end else {des2, des1} = {src3,src2} + {8'h00, src1};
318
      desCy = 1'b0;
319
      desAc = 1'b0;
320
      desOv = 1'b0;
321
      enable_mul = 1'b0;
322
      enable_div = 1'b0;
323 132 simont
    end*/
324 82 simont
//operation exchange
325
//if carry = 0 exchange low order digit
326
    `OC8051_ALU_XCH: begin
327
      if (srcCy)
328
      begin
329
        des1 = src2;
330
        des2 = src1;
331
      end else begin
332
        des1 = {src1[7:4],src2[3:0]};
333
        des2 = {src2[7:4],src1[3:0]};
334
      end
335
      desCy = 1'b0;
336
      desAc = 1'b0;
337
      desOv = 1'b0;
338
      enable_mul = 1'b0;
339
      enable_div = 1'b0;
340
    end
341
    default: begin
342
      des1 = src1;
343
      des2 = src2;
344
      desCy = srcCy;
345
      desAc = srcAc;
346 123 simont
      desOv = 1'b0;
347 82 simont
      enable_mul = 1'b0;
348
      enable_div = 1'b0;
349
    end
350
  endcase
351
end
352
 
353
endmodule

powered by: WebSVN 2.1.0

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