Saturday, July 8, 2017

Building a 68000 Single Board Computer - RPN Calculator Program

I haven't really done any signficant 68000 programming other than typing in examples from books and other sources and porting the code to my TS2. As a larger programming project, I decided to write a simple calculator program.

It uses Reverse Polish (infix) notation and is loosely inspired by the FORTH programming language. It works with 32-bit signed integers and supports some basic math and bitwise functions and input and output in hex and decimal.

The stack size is programmable at build time and defaults to five. It is written for the VASM cross-assembler. Here is a list of the supported commands:

number  Enter number (32-bit signed), put on top of stack
.                           Display stack
+                          Add two numbers on stack, pop them, push result
-                           Subtract two numbers on stack, pop them, push result
*                          Multiply two numbers on stack, pop them, push result
/                           Divide two numbers on stack, pop them, push result
%                        Divide two numbers on stack, pop them, push remainder
!                          2's complement
~                         1's complement
&                        Bitwise AND
|                          Bitwise inclusive OR
^                         Bitwise exclusive OR
<                        Shift left
>                        Shift right
=                        Pop and compare two values on top of stack. Push 1 if they are same, otherwise 0
h                        Set input and output base to hexadecimal.
n                        Set input and output base to decimal.
q                        Quit to TUTOR monitor.
?                        Help (Show summary of commands)

The next four commands are inspired by the FORTH programming language:

DROP                Removes the number from top of stack
SWAP                Exchanges the top 2 numbers on the stack
DUP                   Duplicates the value on the top of stack
ROT                   Rotates the top 3 numbers on the top of the stack

And here is an example session:

RPN Calculator v1.0
00000000
00000000
00000000
00000000
00000000
? 3
00000000
00000000
00000000
00000000
00000003
? 4
00000000
00000000
00000000
00000003
00000004
? *
00000000
00000000
00000000
00000000
0000000C
? !
00000000
00000000
00000000
00000000
FFFFFFF4
? n
Base set to decimal
0
0
0
0
-12
? 10
0
0
0
-12
10
? *
0
0
0
0
-120
?

The code allowed me to get some hands-on experience with 68000 assembly language programming. In particular I was exposed to:

  • Different size operations (byte, world, longword)
  • Use of data and address registers.
  • Use of indexed addressing modes.
  • Use of arithmetic, logical, and shift operations.
  • Calling TUTOR's TRAP 14 firmware routines.
  • String manipulation.
  • Writing subroutines with well-defined interfaces and saving and restoring of registers.
  • Writing position independent code.

Overall it went quite smoothly and only took a few evenings to implement. I made occasional use of TUTOR's trace function and breakpoints to debug the code. It is just over 1000 lines of commented source code and about 2KB of executable code.

The source code can be found at https://github.com/jefftranter/68000/tree/master/rpncalc

No comments:

Post a Comment