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

Subversion Repositories turbo8051

[/] [turbo8051/] [trunk/] [rtl/] [8051/] [oc8051_alu_test.v] - Blame information for rev 76

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

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