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

Subversion Repositories 8051

[/] [8051/] [trunk/] [rtl/] [verilog/] [oc8051_alu_test.v] - Blame information for rev 185

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

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

powered by: WebSVN 2.1.0

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