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

Subversion Repositories 8051

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

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 4 markom
module oc8051_alu (clk, rst, op_code, src1, src2, src3, srcCy, srcAc, bit_in, des1, des2, 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
// des2_r       (out)
70 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]
71
// desAc        (out) auxiliary carry output [oc8051_psw.ac_in]
72
// desOv        (out) Overflow output [oc8051_psw.ov_in]
73
//
74
 
75 4 markom
input srcCy, srcAc, bit_in, clk, rst; input [3:0] op_code; input [7:0] src1, src2, src3;
76 2 simont
output desCy, desAc, desOv;
77
output [7:0] des1, des2;
78 7 markom
output [7:0] des1_r, des2_r;
79 2 simont
 
80 7 markom
reg desCy, desAc, desOv;
81
reg [7:0] des1, des2;
82
 
83
reg [7:0] des1_r, des2_r;
84
 
85 2 simont
//
86
//add
87
//
88 5 markom
wire [4:0] add1, add2, add3, add4;
89
wire [3:0] add5, add6, add7, add8;
90
wire [1:0] add9, adda, addb, addc;
91 2 simont
 
92
//
93
//sub
94
//
95 5 markom
wire [4:0] sub1, sub2, sub3, sub4;
96
wire [3:0] sub5, sub6, sub7, sub8;
97
wire [1:0] sub9, suba, subb, subc;
98 2 simont
 
99
//
100
//mul
101
//
102
  wire [7:0] mulsrc1, mulsrc2;
103
  wire mulOv;
104 4 markom
  reg enable_mul;
105 2 simont
 
106
//
107
//div
108
//
109
wire [7:0] divsrc1,divsrc2;
110
wire divOv;
111 4 markom
reg enable_div;
112 2 simont
 
113
//
114
//da
115
//
116
reg da_tmp;
117
//reg [8:0] da1;
118
 
119 4 markom
oc8051_multiply oc8051_mul1(.clk(clk), .rst(rst), .enable(enable_mul), .src1(src1), .src2(src2), .des1(mulsrc1), .des2(mulsrc2), .desOv(mulOv));
120
oc8051_divide oc8051_div1(.clk(clk), .rst(rst), .enable(enable_div), .src1(src1), .src2(src2), .des1(divsrc1), .des2(divsrc2), .desOv(divOv));
121 2 simont
 
122 5 markom
/* Add */
123
assign add1 = {1'b0,src1[3:0]};
124
assign add2 = {1'b0,src2[3:0]};
125
assign add3 = {3'b000,srcCy};
126
assign add4 = add1+add2+add3;
127
 
128
assign add5 = {1'b0,src1[6:4]};
129
assign add6 = {1'b0,src2[6:4]};
130
assign add7 = {1'b0,1'b0,1'b0,add4[4]};
131
assign add8 = add5+add6+add7;
132
 
133
assign add9 = {1'b0,src1[7]};
134
assign adda = {1'b0,src2[7]};
135
assign addb = {1'b0,add8[3]};
136
assign addc = add9+adda+addb;
137
 
138
/* Sub */
139
assign sub1 = {1'b1,src1[3:0]};
140
assign sub2 = {1'b0,src2[3:0]};
141
assign sub3 = {1'b0,1'b0,1'b0,srcCy};
142
assign sub4 = sub1-sub2-sub3;
143
 
144
assign sub5 = {1'b1,src1[6:4]};
145
assign sub6 = {1'b0,src2[6:4]};
146
assign sub7 = {1'b0,1'b0,1'b0, !sub4[4]};
147
assign sub8 = sub5-sub6-sub7;
148
 
149
assign sub9 = {1'b1,src1[7]};
150
assign suba = {1'b0,src2[7]};
151
assign subb = {1'b0,!sub8[3]};
152
assign subc = sub9-suba-subb;
153
 
154
 
155
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)
156 2 simont
begin
157
 
158
  case (op_code)
159
//operation add
160
    `OC8051_ALU_ADD: begin
161 5 markom
      des1 = {addc[0],add8[2:0],add4[3:0]};
162
      des2 = src3+ {7'b0, addc[1]};
163
      desCy = addc[1];
164
      desAc = add4[4];
165
      desOv = addc[1] ^ add8[3];
166 2 simont
 
167 5 markom
      enable_mul = 1'b0;
168
      enable_div = 1'b0;
169 2 simont
    end
170
//operation subtract
171
    `OC8051_ALU_SUB: begin
172 5 markom
      des1 = {subc[0],sub8[2:0],sub4[3:0]};
173
      des2 = 8'h00;
174
      desCy = !subc[1];
175
      desAc = !sub4[4];
176
      desOv = !subc[1] ^ sub8[3];
177 2 simont
 
178 5 markom
      enable_mul = 1'b0;
179
      enable_div = 1'b0;
180 2 simont
    end
181
//operation multiply
182
    `OC8051_ALU_MUL: begin
183 5 markom
      des1 = mulsrc2;
184
      des2 = mulsrc1;
185
      desOv = mulOv;
186
      desCy = 1'b0;
187
      desAc = 1'bx;
188
      enable_mul = 1'b1;
189
      enable_div = 1'b0;
190 2 simont
    end
191
//operation divide
192
    `OC8051_ALU_DIV: begin
193 5 markom
      des1 = divsrc2;
194
      des2 = divsrc1;
195
      desOv = divOv;
196
      desAc = 1'bx;
197
      desCy = 1'b0;
198
      enable_mul = 1'b0;
199
      enable_div = 1'b1;
200 2 simont
    end
201
//operation decimal adjustment
202
    `OC8051_ALU_DA: begin
203
/*      da1= {1'b0, src1};
204
      if (srcAc==1'b1 | da1[3:0]>4'b1001) da1= da1+ 9'b0_0000_0110;
205
 
206
      da1[8]= da1[8] | srcCy;
207
 
208
      if (da1[8]==1'b1) da1=da1+ 9'b0_0110_0000;
209
      des1=da1[7:0];
210
      des2=8'h00;
211
      desCy=da1[8];*/
212
 
213 5 markom
      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 2 simont
 
216 4 markom
      if (srcCy==1'b1 | src1[7:4]>4'b1001)
217 5 markom
        {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 2 simont
 
220 5 markom
      des2 = 8'h00;
221
      desAc = 1'b0;
222
      desOv = 1'b0;
223
      enable_mul = 1'b0;
224
      enable_div = 1'b0;
225 2 simont
    end
226
//operation not
227
// bit operation not
228
    `OC8051_ALU_NOT: begin
229 5 markom
      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 2 simont
    end
237
//operation and
238
//bit operation and
239
    `OC8051_ALU_AND: begin
240 5 markom
      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 2 simont
    end
248
//operation xor
249
// bit operation xor
250
    `OC8051_ALU_XOR: begin
251 5 markom
      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 2 simont
    end
259
//operation or
260
// bit operation or
261
    `OC8051_ALU_OR: begin
262 5 markom
      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 2 simont
    end
270
//operation rotate left
271
// bit operation cy= cy or (not ram)
272
    `OC8051_ALU_RL: begin
273 5 markom
      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 2 simont
    end
281
//operation rotate left with carry and swap nibbles
282
    `OC8051_ALU_RLC: begin
283 5 markom
      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 2 simont
    end
291
//operation rotate right
292
    `OC8051_ALU_RR: begin
293 5 markom
      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 2 simont
    end
301
//operation rotate right with carry
302
    `OC8051_ALU_RRC: begin
303 5 markom
      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 2 simont
    end
311
//operation pcs Add
312
    `OC8051_ALU_PCS: begin
313
       case (src1[7])
314
        1'b1: begin
315 5 markom
          des1 = src2+src1;
316
          des2 = src3;
317 2 simont
        end
318 5 markom
        default: {des2, des1} = {src3,src2} + {8'h00, src1};
319 2 simont
      endcase
320 5 markom
      desCy = 1'b0;
321
      desAc = 1'b0;
322
      desOv = 1'b0;
323
      enable_mul = 1'b0;
324
      enable_div = 1'b0;
325 2 simont
    end
326
//operation exchange
327
//if carry = 0 exchange low order digit
328
    `OC8051_ALU_XCH: begin
329
      if (srcCy)
330
      begin
331 5 markom
        des1 = src2;
332
        des2 = src1;
333 2 simont
      end else begin
334 5 markom
        des1 = {src1[7:4],src2[3:0]};
335
        des2 = {src2[7:4],src1[3:0]};
336 2 simont
      end
337 5 markom
      desCy = 1'b0;
338
      desAc = 1'b0;
339
      desOv = 1'b0;
340
      enable_mul = 1'b0;
341
      enable_div = 1'b0;
342 2 simont
    end
343
    default: begin
344 5 markom
      des1 = src1;
345
      des2 = src2;
346
      desCy = srcCy;
347
      desAc = srcAc;
348
      desOv = 1'bx;
349
      enable_mul = 1'b0;
350
      enable_div = 1'b0;
351 2 simont
    end
352
  endcase
353
end
354
 
355 7 markom
always @(posedge clk or posedge rst)
356
  if (rst) begin
357
    dst1_r <= #1 8'h0;
358
    dst2_r <= #1 8'h0;
359
  else begin
360
    dst1_r <= #1 dst1;
361
    dst2_r <= #1 dst2;
362
  end
363 2 simont
 
364
endmodule

powered by: WebSVN 2.1.0

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