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

Subversion Repositories 8051

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

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 123 simont
// Revision 1.10  2003/01/13 14:14:40  simont
50
// replace some modules
51
//
52 82 simont
// Revision 1.9  2002/09/30 17:33:59  simont
53
// prepared header
54
//
55
//
56
 
57
// synopsys translate_off
58
`include "oc8051_timescale.v"
59
// synopsys translate_on
60
 
61
`include "oc8051_defines.v"
62
 
63
 
64
 
65
module oc8051_alu (clk, rst, op_code, rd, src1, src2, src3, srcCy, srcAc, bit_in, des1, des2, des1_r, desCy,
66
                   desAc, desOv);
67
//
68
// op_code      (in)  operation code [oc8051_decoder.alu_op -r]
69
// src1         (in)  first operand [oc8051_alu_src1_sel.des]
70
// src2         (in)  second operand [oc8051_alu_src2_sel.des]
71
// src3         (in)  third operand [oc8051_alu_src3_sel.des]
72
// srcCy        (in)  carry input [oc8051_cy_select.data_out]
73
// srcAc        (in)  auxiliary carry input [oc8051_psw.data_out[6] ]
74
// bit_in       (in)  bit input, used for logic operatins on bits [oc8051_ram_sel.bit_out]
75
// des1         (out)
76
// des1_r       (out)
77
// des2         (out)
78
// 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]
79
// desAc        (out) auxiliary carry output [oc8051_psw.ac_in]
80
// desOv        (out) Overflow output [oc8051_psw.ov_in]
81
//
82
 
83
input srcCy, srcAc, bit_in, clk, rst, rd;
84
input [3:0] op_code;
85
input [7:0] src1, src2, src3;
86
output desCy, desAc, desOv;
87
output [7:0] des1, des2;
88
output [7:0] des1_r;
89
 
90
reg desCy, desAc, desOv;
91
reg [7:0] des1, des2;
92
 
93
reg [7:0] des1_r;
94
 
95
 
96
reg rd_r;
97
//
98
//add
99
//
100
wire [4:0] add1, add2, add3, add4;
101
wire [3:0] add5, add6, add7, add8;
102
wire [1:0] add9, adda, addb, addc;
103
 
104
//
105
//sub
106
//
107
wire [4:0] sub1, sub2, sub3, sub4;
108
wire [3:0] sub5, sub6, sub7, sub8;
109
wire [1:0] sub9, suba, subb, subc;
110
 
111
//
112
//mul
113
//
114
  wire [7:0] mulsrc1, mulsrc2;
115
  wire mulOv;
116
  reg enable_mul;
117
 
118
//
119
//div
120
//
121
wire [7:0] divsrc1,divsrc2;
122
wire divOv;
123
reg enable_div;
124
 
125
//
126
//da
127
//
128
reg da_tmp;
129
//reg [8:0] da1;
130
 
131
oc8051_multiply oc8051_mul1(.clk(clk), .rst(rst), .enable(enable_mul), .src1(src1), .src2(src2), .des1(mulsrc1), .des2(mulsrc2), .desOv(mulOv));
132
oc8051_divide oc8051_div1(.clk(clk), .rst(rst), .enable(enable_div), .src1(src1), .src2(src2), .des1(divsrc1), .des2(divsrc2), .desOv(divOv));
133
 
134
/* Add */
135
assign add1 = {1'b0,src1[3:0]};
136
assign add2 = {1'b0,src2[3:0]};
137
assign add3 = {3'b000,srcCy};
138
assign add4 = add1+add2+add3;
139
 
140
assign add5 = {1'b0,src1[6:4]};
141
assign add6 = {1'b0,src2[6:4]};
142
assign add7 = {1'b0,1'b0,1'b0,add4[4]};
143
assign add8 = add5+add6+add7;
144
 
145
assign add9 = {1'b0,src1[7]};
146
assign adda = {1'b0,src2[7]};
147
assign addb = {1'b0,add8[3]};
148
assign addc = add9+adda+addb;
149
 
150
/* Sub */
151
assign sub1 = {1'b1,src1[3:0]};
152
assign sub2 = {1'b0,src2[3:0]};
153
assign sub3 = {1'b0,1'b0,1'b0,srcCy};
154
assign sub4 = sub1-sub2-sub3;
155
 
156
assign sub5 = {1'b1,src1[6:4]};
157
assign sub6 = {1'b0,src2[6:4]};
158
assign sub7 = {1'b0,1'b0,1'b0, !sub4[4]};
159
assign sub8 = sub5-sub6-sub7;
160
 
161
assign sub9 = {1'b1,src1[7]};
162
assign suba = {1'b0,src2[7]};
163
assign subb = {1'b0,!sub8[3]};
164
assign subc = sub9-suba-subb;
165
 
166
 
167
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)
168
begin
169
 
170
  case (op_code)
171
//operation add
172
    `OC8051_ALU_ADD: begin
173
      des1 = {addc[0],add8[2:0],add4[3:0]};
174
      des2 = src3+ {7'b0, addc[1]};
175
      desCy = addc[1];
176
      desAc = add4[4];
177
      desOv = addc[1] ^ add8[3];
178
 
179
      enable_mul = 1'b0;
180
      enable_div = 1'b0;
181
    end
182
//operation subtract
183
    `OC8051_ALU_SUB: begin
184
      des1 = {subc[0],sub8[2:0],sub4[3:0]};
185
      des2 = 8'h00;
186
      desCy = !subc[1];
187
      desAc = !sub4[4];
188
      desOv = !subc[1] ^ sub8[3];
189
 
190
      enable_mul = 1'b0;
191
      enable_div = 1'b0;
192
    end
193
//operation multiply
194
    `OC8051_ALU_MUL: begin
195
      des1 = mulsrc1;
196
      des2 = mulsrc2;
197
      desOv = mulOv;
198
      desCy = 1'b0;
199
      desAc = 1'bx;
200
      enable_mul = 1'b1;
201
      enable_div = 1'b0;
202
    end
203
//operation divide
204
    `OC8051_ALU_DIV: begin
205
      des1 = divsrc1;
206
      des2 = divsrc2;
207
      desOv = divOv;
208
      desAc = 1'bx;
209
      desCy = 1'b0;
210
      enable_mul = 1'b0;
211
      enable_div = 1'b1;
212
    end
213
//operation decimal adjustment
214
    `OC8051_ALU_DA: begin
215
 
216
      if (srcAc==1'b1 | src1[3:0]>4'b1001) {da_tmp, des1[3:0]} = {1'b0, src1[3:0]}+ 5'b00110;
217
      else {da_tmp, des1[3:0]} = {1'b0, src1[3:0]};
218
 
219
      if (srcCy==1'b1 | src1[7:4]>4'b1001)
220
        {desCy, des1[7:4]} = {srcCy, src1[7:4]}+ 5'b00110 + {4'b0, da_tmp};
221
      else {desCy, des1[7:4]} = {srcCy, src1[7:4]} + {4'b0, da_tmp};
222
 
223
      des2 = 8'h00;
224
      desAc = 1'b0;
225
      desOv = 1'b0;
226
      enable_mul = 1'b0;
227
      enable_div = 1'b0;
228
    end
229
//operation not
230
// bit operation not
231
    `OC8051_ALU_NOT: begin
232
      des1 = ~src1;
233
      des2 = 8'h00;
234
      desCy = !srcCy;
235
      desAc = 1'bx;
236
      desOv = 1'bx;
237
      enable_mul = 1'b0;
238
      enable_div = 1'b0;
239
    end
240
//operation and
241
//bit operation and
242
    `OC8051_ALU_AND: begin
243
      des1 = src1 & src2;
244
      des2 = 8'h00;
245
      desCy = srcCy & bit_in;
246
      desAc = 1'bx;
247
      desOv = 1'bx;
248
      enable_mul = 1'b0;
249
      enable_div = 1'b0;
250
    end
251
//operation xor
252
// bit operation xor
253
    `OC8051_ALU_XOR: begin
254
      des1 = src1 ^ src2;
255
      des2 = 8'h00;
256
      desCy = srcCy ^ bit_in;
257
      desAc = 1'bx;
258
      desOv = 1'bx;
259
      enable_mul = 1'b0;
260
      enable_div = 1'b0;
261
    end
262
//operation or
263
// bit operation or
264
    `OC8051_ALU_OR: begin
265
      des1 = src1 | src2;
266
      des2 = 8'h00;
267
      desCy = srcCy | bit_in;
268
      desAc = 1'bx;
269
      desOv = 1'bx;
270
      enable_mul = 1'b0;
271
      enable_div = 1'b0;
272
    end
273
//operation rotate left
274
// bit operation cy= cy or (not ram)
275
    `OC8051_ALU_RL: begin
276
      des1 = {src1[6:0], src1[7]};
277
      des2 = 8'h00;
278
      desCy = srcCy | !bit_in;
279
      desAc = 1'bx;
280
      desOv = 1'bx;
281
      enable_mul = 1'b0;
282
      enable_div = 1'b0;
283
    end
284
//operation rotate left with carry and swap nibbles
285
    `OC8051_ALU_RLC: begin
286
      des1 = {src1[6:0], srcCy};
287
      des2 = {src1[3:0], src1[7:4]};
288
      desCy = src1[7];
289
      desAc = 1'b0;
290
      desOv = 1'b0;
291
      enable_mul = 1'b0;
292
      enable_div = 1'b0;
293
    end
294
//operation rotate right
295
    `OC8051_ALU_RR: begin
296
      des1 = {src1[0], src1[7:1]};
297
      des2 = 8'h00;
298
      desCy = srcCy & !bit_in;
299
      desAc = 1'b0;
300
      desOv = 1'b0;
301
      enable_mul = 1'b0;
302
      enable_div = 1'b0;
303
    end
304
//operation rotate right with carry
305
    `OC8051_ALU_RRC: begin
306
      des1 = {srcCy, src1[7:1]};
307
      des2 = 8'h00;
308
      desCy = src1[0];
309
      desAc = 1'b0;
310
      desOv = 1'b0;
311
      enable_mul = 1'b0;
312
      enable_div = 1'b0;
313
    end
314
//operation pcs Add
315
    `OC8051_ALU_PCS: begin
316
      if (src1[7]) begin
317 123 simont
        {desCy, des1} = {1'b0, src2} + {1'b0, src1};
318
        des2 = {1'b0, src3} - {8'h0, !desCy};
319 82 simont
      end else {des2, des1} = {src3,src2} + {8'h00, src1};
320
      desCy = 1'b0;
321
      desAc = 1'b0;
322
      desOv = 1'b0;
323
      enable_mul = 1'b0;
324
      enable_div = 1'b0;
325
    end
326
//operation exchange
327
//if carry = 0 exchange low order digit
328
    `OC8051_ALU_XCH: begin
329
      if (srcCy)
330
      begin
331
        des1 = src2;
332
        des2 = src1;
333
      end else begin
334
        des1 = {src1[7:4],src2[3:0]};
335
        des2 = {src2[7:4],src1[3:0]};
336
      end
337
      desCy = 1'b0;
338
      desAc = 1'b0;
339
      desOv = 1'b0;
340
      enable_mul = 1'b0;
341
      enable_div = 1'b0;
342
    end
343
    default: begin
344
      des1 = src1;
345
      des2 = src2;
346
      desCy = srcCy;
347
      desAc = srcAc;
348 123 simont
      desOv = 1'b0;
349 82 simont
      enable_mul = 1'b0;
350
      enable_div = 1'b0;
351
    end
352
  endcase
353
end
354
 
355
always @(posedge clk or posedge rst)
356
  if (rst) begin
357
    des1_r <= #1 8'h0;
358
  end else if (rd_r) begin
359
    des1_r <= #1 des1;
360
  end
361
 
362
always @(posedge clk or posedge rst)
363
  if (rst) begin
364
    rd_r <= #1 8'h0;
365
  end else begin
366
    rd_r <= #1 rd;
367
  end
368
 
369
endmodule

powered by: WebSVN 2.1.0

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