Close
0%
0%

Hardware Calculator From Scratch

DIY calculator based on AVR ATmega328P MCU and written in pure assembly

Public Chat
Similar projects worth following
It is based on a homebrew IEEE 754 binary floating point emulation which has been reinvented from the ground up.

UPD: Now I'm finishing a tutorial that explains in details all the theory behind the calculator.

Overview

The calculator is built upon two custom libraries:

  • float32avr.asm - Software emulation of floating-point arithmetic.
  • lcd1602.asm - Library for interfacing with the LCD1602 based on the HD44780 controller.

The float32avr.asm repository with documentation can be found here.

The lcd1602.asm repository is located here.

Architecture and Operation

A bird's-eye block diagram is provided below:

Input of Numbers

An encoder scans key presses and outputs a 4-bit code ranging from 0x0 to 0xF. An ASCII code table corresponding to the keyboard keys is stored in RAM. The table is arranged so that the lower nibble of the address equals zero. When any key is pressed, its 4-bit code is used to access the table and retrieve its ASCII code.

To prevent the input of invalid numeric strings, a simple finite state machine is implemented to parse key presses. The state diagram is presented below:

The current state is stored in the stack as the address of the label to which the program should jump during the next keyboard interrupt.

Operand Conversion

After the first operand is entered and the operator key is pressed, the decimal numeric string is parsed and converted into a single-precision floating-point format. The operator is stored as the ASCII code of the operator symbol.

Once the second operand is entered, it is also converted to float, the first operand is retrieved, the operator is analyzed, and one of the four subroutines from the floating-point emulation library is called.

The resulting value is pre-normalized to one non-zero decimal digit before the decimal point and converted into a decimal string in exponential format.

Display Output

The lcd1602.asm library implements the simplest mode of interaction with the display controller—an 8-bit bus with synchronous waiting for the busy flag. Additionally, auxiliary subroutines for cursor control and screen clearing are implemented.

Calculation Errors

Unlike commercial calculators, numbers in this calculator are represented in binary floating-point format, not in binary-coded decimal (BCD) format, so the accuracy of the result depends not only on the available precision.

Sources of errors include:

  • Characteristics of the binary floating-point format:
    • Not all decimal numbers can be precisely represented in binary form, even with an infinite precision grid. A classic example is the number 0.1. Simply put, if the original decimal number can be expressed as a sum of powers of two, and the binary precision grid is large enough, then such a decimal number will be represented exactly (assuming the conversion algorithm provides the best approximation). In other cases, the conversion will yield only an approximation of the original decimal number.
    • During calculations, due to the limited precision grid, the result is inevitably rounded (by default to the nearest, with ties going to the even number).
  • Conversion of a decimal string to binary floating-point format: Since the conversion is based on a naive scheme that does not use arbitrary-precision arithmetic but relies on the same single-precision floating-point emulation used for calculations, rounding of intermediate values can occur during conversion, leading to distortions in the original decimal value entered by the user (even if the original decimal number can be exactly represented in binary).
  • Conversion of a floating-point number to a decimal string: Although any binary number in float format can have its exact decimal representation derived, in this calculator, as with the string-to-float conversion, a naive scheme based on single-precision float is used, so rounding of intermediate values can occur during conversion, ultimately distorting the entire value and specific decimal digits.

Schematic

Theory and Documentation

The primary material on the theoretical foundations of the calculator's development and some proofs related to floating-point calculations can be found in this paper. More organized...

Read more »

  • Tutorial proofread and edit progress

    igor.24034012/13/2024 at 18:29 0 comments

    [21/2/2025] 74 pages out of 97 edited: 76.3%

    [19/2/2025] Well, I've finished the integer division and returned to floating-point division. Also, accidentally I've found much more clear answer on some old question (actually, it wasn't the new one and I've answered it long before). So, the most hard part that remains is floating-point division and addition/subtraction and it almost finished. At the end I'll refactor conversion, keyboard handling and so on, but there is almost nothing that needs to be totally edited: just refactor some illustrations, typos and so on.

    [13/2/2025] Still had been rewriting some old reasoning about integer division which will extrapolate on floating-point division. It's hard to return and rethink the same thing again and again, the brain just refuses to catch up.

    [06/2/2025] Who am I kiddin? Let's have this sh*t done)

    [04/2/2025] Taking a break until February 12.

    [02/2/2025] 67 pages out of 96 edited: 69.8% [NOTE: I'm not dead. Just rewriting some old proofs of the sufficiency of expanding the binary grid to the left by one digit when we perform non-restoring integer division with immovable divisor which will automatically correct for floating-point case. The old version was vague and verbose. Moreover, I've found and corrected some errors in text along the way: the idea behind was correct but I wrote total mess in text somehow. It's all about the manual, the code of the calculator works fine.]

    [01/26/2025] 67 pages out of 96 edited: 69.8%

    [01/25/2025] 66 pages out of 96 edited: 68.75%

    [01/24/2025] 65 pages out of 95 edited: 68.4%

    [01/22/2025] 64 pages out of 95 edited: 67.4%

    [01/21/2025] 63 pages out of 95 edited: 66.3%

    [01/20/2025] 62 pages out of 94 edited: 66%

    [01/19/2025] 61 pages out of 94 edited: 64.9%

    [01/18/2025] 60 pages out of 94 edited: 63.8%

    [01/17/2025] 59 pages out of 94 edited: 62.8%

    [01/16/2025] 58 pages out of 94 edited: 61.7%

    [01/15/2025] 57 pages out of 94 edited: 60.6%

    [01/14/2025] 56 pages out of 93 edited: 60.2%

    [01/13/2025] 55 pages out of 94 edited: 58.5%

    [01/12/2025] 54 pages out of 93 edited: 58.1% [NOTE: Finished the basics of binary arithmetic.]

    [01/11/2025] 52 pages out of 92 edited: 56.5%

    [01/10/2025] 50 pages out of 90 edited: 55.6%

    [01/09/2025] 49 pages out of 89 edited: 55.1%

    [01/08/2025] 48 pages out of 89 edited: 53.9%

    [01/07/2025] 45 pages out of 86 edited: 52.3%

    [01/06/2025] 43 pages out of 85 edited: 50.6%

    [01/05/2025] 41 pages out of 84 edited: 48.8%

    [01/04/2025] 39 pages out of 84 edited: 46.4%

    [01/03/2025] 38 pages out of 84 edited: 45.2%

    [01/02/2025] 36 pages out of 84 edited: 42.9%

    NOTE: Wrote proofs (not quite rigorous, but demonstrative enough) for some counter-intuitive (well, at least for me) moments - should move faster from now. [UPD 01/10/2025: Faster... hah].

    [01/01/2025] 33 pages out of 84 edited: 39.3%

    [12/29/2024] 31 pages out of 81 edited: 38.3%

    [12/28/2024] 31 pages out of 82 edited: 37.8%

    [12/27/2024] 30 pages out of 81 edited: 37.0%

    [12/26/2024] 27 pages out of 79 edited: 34.2%

    [12/24/2024] 26 pages out of 78 edited: 33.3%

    [12/23/2024] 23 pages out of 78 edited: 29.5%

    [12/22/2024] 19 pages out of 77 edited: 24.7%

    [12/21/2024] 18 pages out of 77 edited: 23.4%

    [12/20/2024] 17 pages out of 77 edited: 22.1%

    [12/19/2024] 15 pages out of 77 edited: 19.5%

    [12/18/2024] 14 pages out of 76 edited: 18.4%

    [12/17/2024] 13 pages out of 75 edited: 17.3%

    [12/16/2024] 12 pages out of 75 edited: 16%

    [12/15/2024] 9 pages out of 75 edited: 12%

    [12/14/2024] 6 pages out of 74 edited: 8.1%

  • Tutorial writing finished

    igor.24034012/06/2024 at 17:48 0 comments

    Have just finished drawing schematic and wrote a couple of words about hardware. Nothing special, but definitely better than it was.

    Before:

    After:

    So, the tutorial is technically finished.

    Well, it's not quite a tutorial, I'd say it's a description of internal functioning of the calculator and theory that stands behind all numerical magic. Anyway, it can be useful for anyone who will want to understand floating point and implement such a calculator from scratch. I wish I had such a thing in the beginning of this journey.

    Now I'm gonna proofread it and edit, redraw some illustrations, numerate all formulas, etc.

    And at the end I'll translate it in English, like it was with code comments.

    I guess, by the end of this year century everything will be done.

  • Code comments translation completed

    igor.24034011/27/2024 at 16:43 0 comments

    I've just finished translation of all comments in firmware code.

    Now all three repos in English now and accessible for everyone to tinker:

    The whole calculator firmware

    The floating-point emulation library

    The library for LCD interfacing

    The only remaining task now is a small pdf doc with holistic overview of theoretical foundations behind this project. About 40% is already done.

  • Code comments translation

    igor.24034011/26/2024 at 01:17 0 comments

    The library for interfacing with LCD1602 display is now completely translated in English.

    The last thing to translate is the actual firmware for the calculator which will take more time than LCD library but much more less than Float32AVR.

  • Code comments translation

    igor.24034011/25/2024 at 15:05 0 comments

    The floating-point emulation library Float32AVR is now completely translated in English: comments, diagrams, etc.

    Next goal is to translate the library for interfacing with LCD1602 and the actual firmware for the calculator.

View all 5 project logs

Enjoy this project?

Share

Discussions

Does this project spark your interest?

Become a member to follow this project and never miss any updates