- Assembly 97.1%
- Linker Script 1.8%
- Makefile 1.1%
|
Amand Tihon
65d980fe1e
Fix potential misaligned store in line storage
When prepending the binary line number in the input buffer, before storing the line in the program memory, do not store the half-word at once, but make two separate byte stores to avoid potential misaligned memory access. |
||
|---|---|---|
| examples | Add Readme and examples. | |
| Makefile | Tweak Makefile | |
| README.md | Add Readme and examples. | |
| rvtb.ld | Update linker script, avoid warning. | |
| rvtb.S | Fix potential misaligned store in line storage | |
Palo Alto Tiny Basic for RISC-V
This is a port to the RISC-V architecture of Li-Chen Wang's Palo Alto Tiny Basic version 3, (PATB) as published in July 1977 by People's Computer Company.
Build and run
The code shared here is meant to be run inside qemu, but porting it to another system should not bee too difficult. The input/output functions that need to be adapted are located near the end.
It should run on any RV32I implementation. It does not need any extension and it does not make misaligned memory accesses.
Build with
make
Run with
make qemu
Press Ctrl+A, X to exit.
ABI and conventions
Warning! This code does not adhere strictly to the standard ABI and calling conventions for RISC-V!
rais used as the return address andspfor the stack.- Registers
t0tot6are used as temporaries - There is no hard rule about callee-saved and caller-saved registers
- Function arguments and return values are not always passed via
a0
Notable differences with the original source code
The comments and overall program structure have been kept as faithful to the original version as possible, but the differences between the 8080 and RV32 architectures have caused a few deviations described below.
Integer size
Since I am porting PATB to a 32 bits architecture, I have decided to change the integer size from 16 bits to 32 bits. This applies to all arithmetic operations and to the variables' size but not to line numbers, which must still fit in 16 bits. It should not cause any issue with old programs because the original code used to treat any overflow as an error that interrupted the program anyway (a "How?" error).
Each numbered line in program memory is aligned on a 2 byte boundary so that the line number can be read in a single memory access.
Abandoned subroutines
Running on a 32 bits architecture has rendered some helper routines useless:
subde, chksgn, chgsgn, chklde, comp, locr were all used to perform
16 bits operations on pairs of 8 bits registers and were not reimplemented.
Standard RISC-V instructions acting on a single register are used instead.
The logic behind testch, used everywhere via the macro testc (not shown or
explained in the original article), has not been reproduced. Instead, ignblk
returns the first non-blank character ready to be compared.
No mixed uses of calls and tails
In the Intel world, the call instruction is responsible for pushing the return
address on the stack before jumping to the function to execute. This function can
then be used either as a plain function (called, return), or as a tail call (jump
into it, return).
In RISC-V, since it is up to the called function to save the value of the ra
register on the stack, this approach is not practical. Such a block of code
can be used either as a plain function (saving ra in its preamble, restoring
it before returning), or as a tail-call or fall-through (restoring ra that
was saved from somewhere else), but not both.
This has forced a few little changes here and there, but more prominently
in parn, that had to become a real function, and in size, that cannot be
used as one.
Random
The random generator I used is converted from an x86 assembly code snippet shared on the VCFed forums: https://forum.vcfed.org/index.php?threads/palo-alto-tiny-basic-download.40493/post-492806
The seed is not randomized and the generator will always output the same sequence upon restart.
Links
A copy of the original article from the PPC's Reference Book of Personal and Home computing can be found on the Internet Archive: https://archive.org/details/Palo_Alto_Tiny_BASIC_Version_3_Li-Chen_Wang_1977/mode/1up
The Tasty Basic project on Github has a some nice additional example programs to play with.