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

Subversion Repositories 8051

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

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

powered by: WebSVN 2.1.0

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