Leanpub Header

Skip to main content

BerryBasiC

The Bare-Metal Basic for RaspberryPi4

BerryBasiC

Another BASIC? Really? ... but what sets BerryBasiC apart from every other contemporary BASIC is where it runs: on bare metal. It is not an application on Linux or Windows, but a single program running on the Raspberry Pi. When the Pi powers on, there is no operating system to load, BerryBasiC takes the role of the operating system, just like on a classic BBC Micro or a Commodore. Curios? Dig In!

Minimum price

$19.00

$29.00

You pay

Author earns

$

Also available for 1 book credit with a Reader Membership

PDF
EPUB
About

About

About the Book

What if turning on your computer dropped you straight into programming? No operating system to load, no endless layers of software, no desktop. Just you, a blinking cursor, and a machine waiting to do exactly what you tell it to do.

That was the magic of the classic home computers of the 1980s. You typed PRINT "HELLO" and it happened. You changed a memory location and the screen changed. Every line of code brought you closer to understanding not just a programming language, but the computer itself.

Modern computers are vastly more powerful, but they have also become vastly more complicated. Between your program and the hardware stand millions of lines of operating system code, virtual machines, drivers, runtimes, and frameworks. They exist for good reasons, but they also hide the machine from the programmer.

BerryBasiC was created to bring that direct connection back.

Running directly on the Raspberry Pi 4, BerryBasiC is not another BASIC interpreter that runs inside Linux or Windows. It is the software that boots when the computer starts. From the moment the Raspberry Pi powers on, you are in complete control of the machine. Graphics, sound, files, GPIO pins, memory, timers, and hardware are all available immediately through a simple, approachable programming language.

But BerryBasiC is far more than a nostalgic recreation of an old BASIC. It combines the immediacy of classic home computers with features programmers expect today: structured programming, procedures and functions, local variables, recursion, modern control structures, exception handling, reusable libraries, PNG/JPEG/BMP graphics, event-driven programming, true-color graphics, sound, and direct access to the Raspberry Pi's hardware.

When performance matters, BerryBasiC lets you step beyond the interpreter by loading small pieces of compiled native code (called seeds) allowing critical routines to execute at full processor speed while the rest of your application remains simple and readable.

Most importantly, BerryBasiC is designed to be transparent. The interpreter itself is small enough to understand, the system is simple enough to explore, and nothing important is hidden behind layers of abstraction. It is a complete computing environment that you can truly own, modify, and learn from.

Whether you want to:

  • learn programming from first principles,
  • teach programming and electronics,
  • build games and graphical applications,
  • control LEDs, motors, sensors, and other hardware,
  • create dedicated Raspberry Pi appliances,
  • or simply rediscover the joy of programming close to the hardware,

BerryBasiC offers an experience that has become surprisingly rare: a computer that belongs entirely to you.

This book is your guide to that journey. It starts with the very first PRINT statement and gradually explores everything BerryBasiC has to offer, from the language itself to graphics, sound, GPIO programming, event-driven applications, native extensions, and the architecture that makes the entire system possible.

If you've ever wanted to understand how computers really work, and not just how to use them, you've come to the right place.

Welcome to BerryBasiC. Welcome back to owning your computer.

Author

About the Author

Contents

Table of Contents

Part I - BerryBasiC introduction

Another BASIC? Really?

  1. What makes BerryBasiC different
  2. What you can do with it
  3. Who it is for
  4. The bargain of direct hardware access
  5. Where to Get it

Acknowledgements

BerryBasiC Quick Intro

  1. Getting Started

Program Structure

  1. Comments

Data Types

  1. Floating point (the default)
  2. Integer variables (%)
  3. Numeric Literals
  4. String variables ($)
  5. Arrays
  6. Collections
  7. Type mismatches
  8. Constants

Assignment

Operators

  1. Arithmetic
  2. Integer division - DIV
  3. Remainder - MOD
  4. Relational
  5. Logical / bitwise - AND OR EOR NOT
  6. Shifts and rotates
  7. String concatenation
  8. Indirection - ? ! $
  9. Precedence

The Beginner’s Reference to Basic Statements

  1. PRINT Statement
  2. INPUT Statement
  3. Branching
  4. Subroutines
  5. FOR Loops
  6. REPEAT … UNTIL
  7. WHILE … ENDWHILE
  8. Loop Control - EXIT and CONTINUE
  9. Error Handling - TRY and CATCH
  10. Procedures
  11. Functions
  12. DATA Processing
  13. Program Control Commands

The Slightly Excessive BerryBasiC Library

  1. String Functions
  2. Mathematical Functions
  3. Keyboard Functions
  4. Mouse
  5. Cursor Functions
  6. Time
  7. Screen Control
  8. VDU

The More Advanced BerryBasiC Constructs

  1. User-Defined Types (Records)
  2. Memory and Indirection
  3. Dynamic Evaluation - EVAL and EXEC
  4. WAIT
  5. DELAY
  6. Events - ON TIMER, ON PIN, ON MOUSE

BGI: The BerryBasiC Graphics Interface

  1. Screen resolution
  2. MODE
  3. GCOL - graphics colour and plot action
  4. PLOT
  5. MOVE
  6. DRAW
  7. CLG
  8. POINT
  9. The Extended Graphics Library

Sound

  1. SOUND
  2. TONE
  3. SOUND OFF

Raw Basic

  1. GPIO (the 40-pin header)
  2. I2C
  3. Notes and limits

Files and Storage

  1. Long file names
  2. Directories
  3. File Handling

Basically Extending the Programs

  1. Modules (IMPORT)
  2. Native Seeds
  3. Beyond seeds: whole native programs
  4. Part II - BerryBasiC Native Code and Toolchain

Why Native

BerryBasiC Executables

  1. Commands live in /sys
  2. The text editor: ed
  3. The tcc compiler

POD Executables

The POD Executable Format

  1. File layout
  2. Fixed header (64 bytes)
  3. Chunks
  4. The capability model
  5. The memory image and entry
  6. Loading, and errors

The Seed Packet (.PKT) Format

  1. Layout
  2. Building, using, and the link algorithm
  3. Capabilities compose

BerryServices - the native services interface

  1. How to Obtain the Table
  2. Capabilities, or Why a Pointer Might be a Stub
  3. The Argument Type
  4. The Service Groups
  5. Practical guidance

Building BerryBasiC from Source

  1. What you need
  2. Get the code and build
  3. Configure the machine - make config
  4. Run it in QEMU - make run
  5. Build the native seeds - make seeds
  6. Put it on a real Raspberry Pi 4 - make sdimage / make flash
  7. Develop on the PC - make host and make test
  8. The build targets at a glance

Under the Hood - Kernel, Drivers and Boot

  1. The shape of the system
  2. How a Raspberry Pi 4 boots (before our code runs)
  3. First instructions - kernel/boot.S
  4. Catching faults - kernel/vectors.S
  5. The kernel in kernel_main
  6. The serial console - kernel/uart.c
  7. Talking to the GPU - the mailbox (kernel/mailbox.c)
  8. The framebuffer (fb_configure)
  9. Virtual memory and caches - kernel/mmu.c
  10. Storage - SD card and FAT (drivers/sd.c, drivers/fat.c)
  11. Input - USB HID keyboard and mouse
  12. Output and Peripherals
  13. The Platform Starts: how BASIC Stays Hardware-agnostic
  14. Where BerryBasiC steps in - the REPL

Hardware Support and Limitations

  1. USB
  2. Storage
  3. Display
  4. Sound
  5. The 40-pin header
  6. Not present at all
  7. QEMU versus real hardware
  8. Lifting a limit

Join the Development

Last Chapter

  1. Appendix

Appendix A: Error Messages

  1. Syntax and expressions
  2. Types and values
  3. Control flow
  4. Capacity (“Out of memory” and the “Too many…” family)
  5. Arrays and data
  6. User-defined types (records)
  7. Collections
  8. Graphics
  9. Hardware buses (GPIO, I2C)
  10. Files and storage
  11. Seeds

Appendix B: Limits

Appendix C: Keyword Quick Reference

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $15 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub