G1.6.1 BCD Sums by Hand

EasyImplementIcarus · browser samples / server checksReview the topic

Add packed BCD numbers digit by digit. Add 6 where a digit sum is above 9, and keep every decimal carry.

Hardware context: The coin adder of a parking meter and the bridge between an excess-3 calculator and a BCD display. Add packed BCD numbers by hand with the +6 correction and every decimal carry.

A parking meter adds up coins in packed BCD, so its display needs no conversion. The test team needs a list of sums worked out by hand before they check the adder.

This is an Implement problem with no logic to write. Work out each answer and write it into the answer module. Write every sum as packed BCD in hex: the sum 125 is written 12'h125. The first three constants are worked examples.

The check compares every constant with the right value and names the first one that is wrong.

Signals in the Timeline

SignalWidthRole
question8input of each check
Full specification
ConstantWhat to write
SUM_25_34The packed BCD sum of 25 and 34 (5 + 4 is 9: no correction).
RAW_8_5The 4-bit binary sum of the digits 8 and 5, before any correction.
FIX_8_5The corrected BCD sum of the digits 8 and 5, carry digit included.
SUM_29_47The packed BCD sum of 29 and 47, carry digit included.
SUM_19_19The packed BCD sum of 19 and 19, carry digit included.
SUM_45_54The packed BCD sum of 45 and 54, carry digit included.
SUM_47_56The packed BCD sum of 47 and 56, carry digit included.
SUM_99_99The packed BCD sum of 99 and 99, carry digit included.
SUM_9_1The packed BCD sum of 9 and 1, carry digit included.
SUM_76_48The packed BCD sum of 76 and 48, carry digit included.
RAW_7_9The binary sum of the digits 7 and 9, before any correction.
BCD_OF_3AThe packed BCD form of the binary byte 3a.
BINARY_OF_59The binary value, in hex, of the packed BCD byte 59.
AIKEN_68The two digits of 68 in the 2421 code, as one byte.
AIKEN_GAPThe smallest 4-bit pattern that is not a 2421 code word.
XS3_NINES_274The excess-3 code of 999 - 274 (three digits), found by inverting the excess-3 code of 274.
How a sum is written
SUM_25_34 = 12'h059;   // the digits 0, 5 and 9, one per nibble

RAW_8_5 and RAW_7_9 are plain binary sums, written in hex. The last three constants use the 2421 and excess-3 codes of the lesson.

Keep the module name and the constant names.

Examples

Example 1

Worked Example: No Correction
Input
the packed BCD sum of 25 and 34 (5 + 4 is 9: no correction)
Output
sum_25_34 = 0x59

5 + 4 is 9 and 2 + 3 is 5. Neither digit sum is above 9, so the binary sum is already BCD: 059. This constant is already filled in.

Example 2

Worked Example: The Raw Sum
Input
the 4-bit binary sum of the digits 8 and 5, before any correction
Output
raw_8_5 = 0xd

8 + 5 is 13, which is d in hex. That pattern is not a decimal digit. This constant is already filled in.

Example 3

Worked Example: The Correction
Input
the corrected BCD sum of the digits 8 and 5, carry digit included
Output
fix_8_5 = 0x13

13 + 6 is 19, which is 1 0011 in binary: a carry of 1 and the digit 3. The BCD sum is 13. This constant is already filled in.

Constraints and Contract

  • Keep the module name answer and the names of the constants.
  • Every sum fits in three BCD digits.

Hints

Hint 1

Add the ones digits first, in binary. If the sum is above 9, add 6. The low four bits are the ones digit and the bit above is the carry.

Hint 2

Add the tens digits and that carry, and correct again in the same way. A last carry becomes the hundreds digit.

Hint 3

For BCD_OF_3A, first find the value of the byte in decimal. Then write each decimal digit as one nibble. BINARY_OF_59 goes the other way.

What the grader checks: The check reads sixteen constants and compares each with a value from a digit-by-digit decimal model. It cannot see how a value was found.

16 checks on Icarus · browser samples / server checks. Run the examples first, then check the full regression. Full checks use the server judge. Downloads contain public samples only. A passing result establishes the tested cases, not a proof for all possible inputs.

See all 2 steps and the final capstone

Report a problem with this page

Your code SystemVerilogSaved locally
Ln 1, Col 1Tab indents · Ctrl/⌘ + Enter runs samples
Not run yet

Worked Example: No Correction

Input
the packed BCD sum of 25 and 34 (5 + 4 is 9: no correction)
Expected output
sum_25_34 = 0x59

5 + 4 is 9 and 2 + 3 is 5. Neither digit sum is above 9, so the binary sum is already BCD: 059. This constant is already filled in.

Run samples executes all 3 public checks. Check all tests runs 16 checks on the server.

Icarus · browser samples / server checks