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

Subversion Repositories rf6809

[/] [rf6809/] [trunk/] [rtl/] [lib/] [BCDMath.sv] - Blame information for rev 22

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 22 robfinch
`timescale 1ns / 1ps
2
// ============================================================================
3
//        __
4
//   \\__/ o\    (C) 2012-2021  Robert Finch, Waterloo
5
//    \  __ /    All rights reserved.
6
//     \/_//     robfinch@finitron.ca
7
//       ||
8
//
9
//      BCDMath.sv
10
//
11
// BSD 3-Clause License
12
// Redistribution and use in source and binary forms, with or without
13
// modification, are permitted provided that the following conditions are met:
14
//
15
// 1. Redistributions of source code must retain the above copyright notice, this
16
//    list of conditions and the following disclaimer.
17
//
18
// 2. Redistributions in binary form must reproduce the above copyright notice,
19
//    this list of conditions and the following disclaimer in the documentation
20
//    and/or other materials provided with the distribution.
21
//
22
// 3. Neither the name of the copyright holder nor the names of its
23
//    contributors may be used to endorse or promote products derived from
24
//    this software without specific prior written permission.
25
//
26
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
30
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
33
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
34
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36
//
37
// ============================================================================
38
//
39
// Could use the following approach for add/sub but it ends up being larger
40
// than using an adjustment lookup table.
41
 
42
module BCDAddNyb(ci,a,b,o,c);
43
input ci;               // carry input
44
input [3:0] a;
45
input [3:0] b;
46
output [3:0] o;
47
output c;
48
 
49
wire c0;
50
 
51
reg [4:0] hsN0;
52
always_comb
53
begin
54
        hsN0 = a[3:0] + b[3:0] + ci;
55
        if (hsN0 > 5'd9)
56
                hsN0 = hsN0 + 3'd6;
57
end
58
assign o = hsN0[3:0];
59
assign c = hsN0[4];
60
 
61
endmodule
62
 
63
module BCDAdd(ci,a,b,o,c);
64
input ci;               // carry input
65
input [7:0] a;
66
input [7:0] b;
67
output [7:0] o;
68
output c;
69
 
70
wire c0,c1;
71
reg [4:0] hsN0, hsN1;
72
always_comb
73
        hsN0 <= a[3:0] + b[3:0] + ci;
74
always_comb
75
        hsN1 <= a[7:4] + b[7:4] + c0;
76
 
77
BCDAddAdjust u1 (hsN0,o[3:0],c0);
78
BCDAddAdjust u2 (hsN1,o[7:4],c);
79
 
80
endmodule
81
 
82
module BCDAdd4(ci,a,b,o,c,c8);
83
input ci;               // carry input
84
input [15:0] a;
85
input [15:0] b;
86
output [15:0] o;
87
output c;
88
output c8;
89
 
90
wire c0,c1,c2;
91
assign c8 = c1;
92
 
93
reg [4:0] hsN0, hsN1, hsN2, hsN3;
94
always_comb
95
        hsN0 <= a[3:0] + b[3:0] + ci;
96
always_comb
97
        hsN1 <= a[7:4] + b[7:4] + c0;
98
always_comb
99
        hsN2 <= a[11:8] + b[11:8] + c1;
100
always_comb
101
        hsN3 <= a[15:12] + b[15:12] + c2;
102
 
103
BCDAddAdjust u1 (hsN0,o[3:0],c0);
104
BCDAddAdjust u2 (hsN1,o[7:4],c1);
105
BCDAddAdjust u3 (hsN2,o[11:8],c2);
106
BCDAddAdjust u4 (hsN3,o[15:12],c);
107
 
108
endmodule
109
 
110
module BCDAddN(ci,a,b,o,co);
111
parameter N=24;
112
input ci;               // carry input
113
input [N*4-1:0] a;
114
input [N*4-1:0] b;
115
output [N*4-1:0] o;
116
output co;
117
 
118
genvar g;
119
generate begin : gBCDAddN
120
reg [4:0] hsN [0:N-1];
121
wire [N:0] c;
122
 
123
assign c[0] = ci;
124
assign co = c[N];
125
 
126
for (g = 0; g < N; g = g + 1)
127
        always_comb
128
                hsN[g] = a[g*4+3:g*4] + b[g*4+3:g*4] + c[g];
129
 
130
for (g = 0; g < N; g = g + 1)
131
        BCDAddAdjust u1 (hsN[g],o[g*4+3:g*4],c[g+1]);
132
end
133
endgenerate
134
 
135
endmodule
136
 
137
module BCDSub(ci,a,b,o,c);
138
input ci;               // carry input
139
input [7:0] a;
140
input [7:0] b;
141
output [7:0] o;
142
output c;
143
 
144
wire c0,c1;
145
 
146
reg [4:0] hdN0, hdN1;
147
always_comb
148
        hdN0 <= a[3:0] - b[3:0] - ci;
149
always_comb
150
        hdN1 <= a[7:4] - b[7:4] - c0;
151
 
152
BCDSubAdjust u1 (hdN0,o[3:0],c0);
153
BCDSubAdjust u2 (hdN1,o[7:4],c);
154
 
155
endmodule
156
 
157
module BCDSub4(ci,a,b,o,c,c8);
158
input ci;               // carry input
159
input [15:0] a;
160
input [15:0] b;
161
output [15:0] o;
162
output c;
163
output c8;
164
 
165
wire c0,c1,c2;
166
assign c8 = c1;
167
 
168
reg [4:0] hdN0, hdN1, hdN2, hdN3;
169
always_comb
170
        hdN0 <= a[3:0] - b[3:0] - ci;
171
always_comb
172
        hdN1 <= a[7:4] - b[7:4] - c0;
173
always_comb
174
        hdN2 <= a[11:8] - b[11:8] - c1;
175
always_comb
176
        hdN3 <= a[15:12] - b[15:12] - c2;
177
 
178
BCDSubAdjust u1 (hdN0,o[3:0],c0);
179
BCDSubAdjust u2 (hdN1,o[7:4],c1);
180
BCDSubAdjust u3 (hdN2,o[11:8],c2);
181
BCDSubAdjust u4 (hdN3,o[15:12],c);
182
 
183
endmodule
184
 
185
module BCDSubN(ci,a,b,o,co);
186
parameter N=24;
187
input ci;               // carry input
188
input [N*4-1:0] a;
189
input [N*4-1:0] b;
190
output [N*4-1:0] o;
191
output co;
192
 
193
genvar g;
194
generate begin : gBCDSubN
195
reg [4:0] hdN [0:N-1];
196
wire [N:0] c;
197
 
198
assign c[0] = ci;
199
assign co = c[N];
200
 
201
for (g = 0; g < N; g = g + 1)
202
        always_comb
203
                hdN[g] = a[g*4+3:g*4] - b[g*4+3:g*4] - c[g];
204
 
205
for (g = 0; g < N; g = g + 1)
206
        BCDSubAdjust u1 (hdN[g],o[g*4+3:g*4],c[g+1]);
207
end
208
endgenerate
209
 
210
endmodule
211
 
212
module BCDAddAdjust(i,o,c);
213
input [4:0] i;
214
output [3:0] o;
215
reg [3:0] o;
216
output c;
217
reg c;
218
always_comb
219
case(i)
220
5'h0: begin o = 4'h0; c = 1'b0; end
221
5'h1: begin o = 4'h1; c = 1'b0; end
222
5'h2: begin o = 4'h2; c = 1'b0; end
223
5'h3: begin o = 4'h3; c = 1'b0; end
224
5'h4: begin o = 4'h4; c = 1'b0; end
225
5'h5: begin o = 4'h5; c = 1'b0; end
226
5'h6: begin o = 4'h6; c = 1'b0; end
227
5'h7: begin o = 4'h7; c = 1'b0; end
228
5'h8: begin o = 4'h8; c = 1'b0; end
229
5'h9: begin o = 4'h9; c = 1'b0; end
230
5'hA: begin o = 4'h0; c = 1'b1; end
231
5'hB: begin o = 4'h1; c = 1'b1; end
232
5'hC: begin o = 4'h2; c = 1'b1; end
233
5'hD: begin o = 4'h3; c = 1'b1; end
234
5'hE: begin o = 4'h4; c = 1'b1; end
235
5'hF: begin o = 4'h5; c = 1'b1; end
236
5'h10:  begin o = 4'h6; c = 1'b1; end
237
5'h11:  begin o = 4'h7; c = 1'b1; end
238
5'h12:  begin o = 4'h8; c = 1'b1; end
239
5'h13:  begin o = 4'h9; c = 1'b1; end
240
default:        begin o = 4'h9; c = 1'b1; end
241
endcase
242
endmodule
243
 
244
module BCDSubAdjust(i,o,c);
245
input [4:0] i;
246
output [3:0] o;
247
reg [3:0] o;
248
output c;
249
reg c;
250
always_comb
251
case(i)
252
5'h0: begin o = 4'h0; c = 1'b0; end
253
5'h1: begin o = 4'h1; c = 1'b0; end
254
5'h2: begin o = 4'h2; c = 1'b0; end
255
5'h3: begin o = 4'h3; c = 1'b0; end
256
5'h4: begin o = 4'h4; c = 1'b0; end
257
5'h5: begin o = 4'h5; c = 1'b0; end
258
5'h6: begin o = 4'h6; c = 1'b0; end
259
5'h7: begin o = 4'h7; c = 1'b0; end
260
5'h8: begin o = 4'h8; c = 1'b0; end
261
5'h9: begin o = 4'h9; c = 1'b0; end
262
5'h16: begin o = 4'h0; c = 1'b1; end
263
5'h17: begin o = 4'h1; c = 1'b1; end
264
5'h18: begin o = 4'h2; c = 1'b1; end
265
5'h19: begin o = 4'h3; c = 1'b1; end
266
5'h1A: begin o = 4'h4; c = 1'b1; end
267
5'h1B: begin o = 4'h5; c = 1'b1; end
268
5'h1C: begin o = 4'h6; c = 1'b1; end
269
5'h1D: begin o = 4'h7; c = 1'b1; end
270
5'h1E: begin o = 4'h8; c = 1'b1; end
271
5'h1F: begin o = 4'h9; c = 1'b1; end
272
default: begin o = 4'h9; c = 1'b1; end
273
endcase
274
endmodule
275
 
276
// Multiply two BCD digits
277
// Method used is table lookup
278
module BCDMul1(a,b,o);
279
input [3:0] a;
280
input [3:0] b;
281
output [7:0] o;
282
reg [7:0] o;
283
 
284
always_comb
285
case({a,b})
286
8'h00: o = 8'h00;
287
8'h01: o = 8'h00;
288
8'h02: o = 8'h00;
289
8'h03: o = 8'h00;
290
8'h04: o = 8'h00;
291
8'h05: o = 8'h00;
292
8'h06: o = 8'h00;
293
8'h07: o = 8'h00;
294
8'h08: o = 8'h00;
295
8'h09: o = 8'h00;
296
8'h10: o = 8'h00;
297
8'h11: o = 8'h01;
298
8'h12: o = 8'h02;
299
8'h13: o = 8'h03;
300
8'h14: o = 8'h04;
301
8'h15: o = 8'h05;
302
8'h16: o = 8'h06;
303
8'h17: o = 8'h07;
304
8'h18: o = 8'h08;
305
8'h19: o = 8'h09;
306
8'h20: o = 8'h00;
307
8'h21: o = 8'h02;
308
8'h22: o = 8'h04;
309
8'h23: o = 8'h06;
310
8'h24: o = 8'h08;
311
8'h25: o = 8'h10;
312
8'h26: o = 8'h12;
313
8'h27: o = 8'h14;
314
8'h28: o = 8'h16;
315
8'h29: o = 8'h18;
316
8'h30: o = 8'h00;
317
8'h31: o = 8'h03;
318
8'h32: o = 8'h06;
319
8'h33: o = 8'h09;
320
8'h34: o = 8'h12;
321
8'h35: o = 8'h15;
322
8'h36: o = 8'h18;
323
8'h37: o = 8'h21;
324
8'h38: o = 8'h24;
325
8'h39: o = 8'h27;
326
8'h40: o = 8'h00;
327
8'h41: o = 8'h04;
328
8'h42: o = 8'h08;
329
8'h43: o = 8'h12;
330
8'h44: o = 8'h16;
331
8'h45: o = 8'h20;
332
8'h46: o = 8'h24;
333
8'h47: o = 8'h28;
334
8'h48: o = 8'h32;
335
8'h49: o = 8'h36;
336
8'h50: o = 8'h00;
337
8'h51: o = 8'h05;
338
8'h52: o = 8'h10;
339
8'h53: o = 8'h15;
340
8'h54: o = 8'h20;
341
8'h55: o = 8'h25;
342
8'h56: o = 8'h30;
343
8'h57: o = 8'h35;
344
8'h58: o = 8'h40;
345
8'h59: o = 8'h45;
346
8'h60: o = 8'h00;
347
8'h61: o = 8'h06;
348
8'h62: o = 8'h12;
349
8'h63: o = 8'h18;
350
8'h64: o = 8'h24;
351
8'h65: o = 8'h30;
352
8'h66: o = 8'h36;
353
8'h67: o = 8'h42;
354
8'h68: o = 8'h48;
355
8'h69: o = 8'h54;
356
8'h70: o = 8'h00;
357
8'h71: o = 8'h07;
358
8'h72: o = 8'h14;
359
8'h73: o = 8'h21;
360
8'h74: o = 8'h28;
361
8'h75: o = 8'h35;
362
8'h76: o = 8'h42;
363
8'h77: o = 8'h49;
364
8'h78: o = 8'h56;
365
8'h79: o = 8'h63;
366
8'h80: o = 8'h00;
367
8'h81: o = 8'h08;
368
8'h82: o = 8'h16;
369
8'h83: o = 8'h24;
370
8'h84: o = 8'h32;
371
8'h85: o = 8'h40;
372
8'h86: o = 8'h48;
373
8'h87: o = 8'h56;
374
8'h88: o = 8'h64;
375
8'h89: o = 8'h72;
376
8'h90: o = 8'h00;
377
8'h91: o = 8'h09;
378
8'h92: o = 8'h18;
379
8'h93: o = 8'h27;
380
8'h94: o = 8'h36;
381
8'h95: o = 8'h45;
382
8'h96: o = 8'h54;
383
8'h97: o = 8'h63;
384
8'h98: o = 8'h72;
385
8'h99: o = 8'h81;
386
default:        o = 8'h00;
387
endcase
388
endmodule
389
 
390
 
391
// Multiply two pairs of BCD digits
392
// handles from 0x0 to 99x99
393
module BCDMul2(a,b,o);
394
input [7:0] a;
395
input [7:0] b;
396
output [15:0] o;
397
 
398
wire [7:0] p1,p2,p3,p4;
399
wire [15:0] s1;
400
 
401
BCDMul1 u1 (a[3:0],b[3:0],p1);
402
BCDMul1 u2 (a[7:4],b[3:0],p2);
403
BCDMul1 u3 (a[3:0],b[7:4],p3);
404
BCDMul1 u4 (a[7:4],b[7:4],p4);
405
 
406
BCDAdd4 u5 (1'b0,{p4,p1},{4'h0,p2,4'h0},s1);
407
BCDAdd4 u6 (1'b0,s1,{4'h0,p3,4'h0},o);
408
 
409
endmodule
410
 
411
module BCDMul4(a,b,o);
412
input [15:0] a;
413
input [15:0] b;
414
output [31:0] o;
415
 
416
wire [15:0] p1,p2,p3,p4;
417
wire [31:0] s1;
418
 
419
BCDMul2 u1 (a[7:0],b[7:0],p1);
420
BCDMul2 u2 (a[15:8],b[7:0],p2);
421
BCDMul2 u3 (a[7:0],b[15:8],p3);
422
BCDMul2 u4 (a[15:8],b[15:8],p4);
423
 
424
BCDAddN #(.N(8)) u5 (1'b0,{p4,p1},{8'h0,p2,8'h0},s1);
425
BCDAddN #(.N(8)) u6 (1'b0,s1,{8'h0,p3,8'h0},o);
426
 
427
endmodule
428
 
429
module BCDMul8(a,b,o);
430
input [31:0] a;
431
input [31:0] b;
432
output [63:0] o;
433
 
434
wire [31:0] p1,p2,p3,p4;
435
wire [63:0] s1;
436
 
437
BCDMul4 u1 (a[15:0],b[15:0],p1);
438
BCDMul4 u2 (a[31:16],b[15:0],p2);
439
BCDMul4 u3 (a[15:0],b[31:16],p3);
440
BCDMul4 u4 (a[31:16],b[31:16],p4);
441
 
442
BCDAddN #(.N(16)) u5 (1'b0,{p4,p1},{16'h0,p2,16'h0},s1);
443
BCDAddN #(.N(16)) u6 (1'b0,s1,{16'h0,p3,16'h0},o);
444
 
445
endmodule
446
 
447
module BCDMul16(a,b,o);
448
input [63:0] a;
449
input [63:0] b;
450
output [127:0] o;
451
 
452
wire [63:0] p1,p2,p3,p4;
453
wire [127:0] s1;
454
 
455
BCDMul8 u1 (a[31:0],b[31:0],p1);
456
BCDMul8 u2 (a[63:32],b[31:0],p2);
457
BCDMul8 u3 (a[31:0],b[63:32],p3);
458
BCDMul8 u4 (a[63:32],b[63:32],p4);
459
 
460
BCDAddN #(.N(32)) u5 (1'b0,{p4,p1},{32'h0,p2,32'h0},s1);
461
BCDAddN #(.N(32)) u6 (1'b0,s1,{32'h0,p3,32'h0},o);
462
 
463
endmodule
464
 
465
module BCDMul32(a,b,o);
466
input [127:0] a;
467
input [127:0] b;
468
output [255:0] o;
469
 
470
wire [127:0] p1,p2,p3,p4;
471
wire [255:0] s1;
472
 
473
BCDMul16 u1 (a[63:0],b[63:0],p1);
474
BCDMul16 u2 (a[127:64],b[63:0],p2);
475
BCDMul16 u3 (a[63:0],b[127:64],p3);
476
BCDMul16 u4 (a[127:64],b[127:64],p4);
477
 
478
BCDAddN #(.N(64)) u5 (1'b0,{p4,p1},{64'h0,p2,64'h0},s1);
479
BCDAddN #(.N(64)) u6 (1'b0,s1,{64'h0,p3,64'h0},o);
480
 
481
endmodule
482
 
483
module BCDMul_tb();
484
 
485
wire [15:0] o1,o2,o3,o4;
486
 
487
BCDMul2 u1 (8'h00,8'h00,o1);
488
BCDMul2 u2 (8'h99,8'h99,o2);
489
BCDMul2 u3 (8'h25,8'h18,o3);
490
BCDMul2 u4 (8'h37,8'h21,o4);
491
 
492
endmodule
493
 
494
module BinToBCD(i, o);
495
input [7:0] i;
496
output [11:0] o;
497
 
498
reg [11:0] tbl [0:255];
499
 
500
genvar g;
501
generate begin : gTbl
502
reg [3:0] n0 [0:255];
503
reg [3:0] n1 [0:255];
504
reg [3:0] n2 [0:255];
505
 
506
for (g = 0; g < 256; g = g + 1) begin
507
        initial begin
508
                n0[g] = g % 10;
509
                n1[g] = g / 10;
510
                n2[g] = g / 100;
511
                tbl[g] <= {n2[g],n1[g],n0[g]};
512
        end
513
end
514
 
515
assign o = tbl[i];
516
 
517
end
518
endgenerate
519
 
520
endmodule
521
 
522
// Perform a logical shift to the right.
523
module BCDSRLN(ci, i, o, co);
524
parameter N=4;
525
input ci;
526
input [N*4-1:0] i;
527
output reg [N*4-1:0] o;
528
output co;
529
 
530
reg [N:0] c;
531
 
532
genvar g;
533
generate begin :gSRL
534
always @*
535
        c[N] = ci;
536
for (g = N - 1; g >= 0; g = g - 1)
537
always @*
538
        c[g] = i[g*4];
539
for (g = N - 1; g >= 0; g = g - 1)
540
always @*
541
begin
542
        // Because there is a divide by two, the value will range between 0 and 4.
543
        // Adding 5 keeps it within deicmal boundaries of 0 to 9. No carry can be
544
        // generated
545
        if (c[g+1])
546
                o[g*4+3:g*4] = {1'b0,i[g*4+3:g*4+1]} + 4'd5;
547
        else
548
                o[g*4+3:g*4] = {1'b0,i[g*4+3:g*4+1]};
549
end
550
        assign co = c[0];
551
end
552
endgenerate
553
 
554
endmodule

powered by: WebSVN 2.1.0

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