| Crates.io | kz80_bc |
| lib.rs | kz80_bc |
| version | 0.1.0 |
| created_at | 2025-12-29 01:15:04.155887+00 |
| updated_at | 2025-12-29 01:15:04.155887+00 |
| description | Arbitrary-precision decimal arithmetic language and calculator for Z80 |
| homepage | |
| repository | https://github.com/ajokela/kz80_bc |
| max_upload_size | |
| id | 2009734 |
| size | 235,873 |
An arbitrary-precision decimal calculator and programming language for the Z80 processor. Compiles bc-style programs to native Z80 machine code using BCD (Binary Coded Decimal) arithmetic for exact decimal calculations.
bc calculator| Operation | Syntax | Example |
|---|---|---|
| Addition | a + b |
1 + 2 |
| Subtraction | a - b |
5 - 3 |
| Multiplication | a * b |
6 * 7 |
| Division | a / b |
22 / 7 |
| Parentheses | (expr) |
(1 + 2) * 3 |
| Assignment | var = expr |
x = 42 |
| Comparison | <, >, <=, >=, ==, != |
x > 0 |
/* If statement */
if (x > 0) {
x = x - 1
}
/* While loop */
while (n > 0) {
n = n - 1
}
/* For loop */
for (i = 0; i < 10; i = i + 1) {
sum = sum + i
}
/* Define a function */
define factorial(n) {
if (n <= 1) return 1
return n * factorial(n - 1)
}
/* Call it */
factorial(10)
/* Set decimal places for division results */
scale = 10
/* Now division produces 10 decimal places */
1 / 3 /* outputs: .3333333333 */
/* Scale also affects decimal multiplication */
scale = 2
2.5 * 2 /* outputs: 5.0 */
Requires Rust 1.70 or later.
cargo build --release
The binary will be at target/release/bc80.
bc80 program.bc --rom output.bin
bc80 --repl calculator.bin
bc80 program.bc --tokens # Show lexer tokens
bc80 program.bc --ast # Show parsed AST
bc80 program.bc --bytecode # Show compiled bytecode
The generated ROM images are designed for Z80 systems with:
Works with the RetroShield Z80 on Arduino Mega. Use the included emulator for testing:
../emulator/retroshield output.bin
The REPL (Read-Eval-Print Loop) provides an interactive calculator experience, similar to the traditional Unix bc command.
bc80 --repl calculator.bin
This creates a standalone ~2KB ROM that runs an interactive calculator.
With the emulator:
../emulator/retroshield calculator.bin
Or load calculator.bin onto your RetroShield Z80 hardware.
bc80 REPL v1.0
> 2+3
5
> 10*5
50
> scale=2
2
> 7/2
3.50
> 1.5+2.5
4.0
> 100-250
-150
> (1+2)*(3+4)
21
>
| Feature | Example | Description |
|---|---|---|
| Arithmetic | 2+3*4 |
Full operator precedence |
| Decimals | 3.14159 |
Enter decimal numbers directly |
| Negative results | 5-10 |
Displays -5 |
| Scale setting | scale=5 |
Set decimal places (echoes the value) |
| Parentheses | (1+2)*3 |
Group expressions |
The REPL is a lightweight implementation optimized for the Z80's limited resources:
define)if, while, for)For programs requiring variables, functions, or control structures, write a .bc file and compile it with --rom instead.
/* simple.bc */
1 + 2
3 * 4
10 - 5
a = 42
a
scale = 10
1 / 3
/* factorial.bc */
define f(n) {
if (n <= 1) return 1
return n * f(n - 1)
}
f(5) /* 120 */
f(10) /* 3628800 */
f(20) /* 2432902008176640000 */
/* pi.bc */
scale = 50
define pi(n) {
auto i, s, t
s = 0
t = 1
for (i = 1; i <= n; i += 2) {
s = s + t / i
t = -t
}
return 4 * s
}
pi(1000)
┌─────────────┐ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐
│ Lexer │────▶│ Parser │────▶│ Compiler │────▶│ Z80 Codegen │
│ (lexer.rs) │ │ (parser.rs) │ │(compiler.rs)│ │ (z80.rs) │
└─────────────┘ └─────────────┘ └─────────────┘ └─────────────┘
│ │ │ │
▼ ▼ ▼ ▼
Tokens AST Bytecode Z80 ROM
Numbers are stored in a 28-byte structure:
| Offset | Size | Description |
|---|---|---|
| 0 | 1 | Sign (0x00 = positive, 0x80 = negative) |
| 1 | 1 | Length (always 50 digits) |
| 2 | 1 | Scale (decimal places) |
| 3-27 | 25 | Packed BCD digits (2 digits per byte) |
Run the comprehensive math test suite:
bash tests/math_tests.sh
Tests cover:
BSD 3-Clause License. See LICENSE for details.
Alex Jokela