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

Subversion Repositories 8051

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

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

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

powered by: WebSVN 2.1.0

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