unit Neo6502Math

Neo6502 API library for RP2040 accelerated Math and other computations.

author: bocianu bocianu@gmail.com


Set of procedures to cover Math API functionality.
WARNING!
This library uses 15 bytes on ZP ($F0-$FE)
More about Neo6502:
https://www.olimex.com/Products/Retro-Computers/Neo6502/open-source-hardware
https://www.neo6502.com/

API documentation can be found here:
https://github.com/paulscottrobson/neo6502-firmware/wiki

It's work in progress, so please report any bugs you will find.

Constants:

name:value:description:
N6502MSG_ADDRESS$ff00
MATHAdd0Add
MATHSub1Subtract
MATHMul2Multiply
MATHFDiv3Float Divide
MATHIDiv4Int Divide
MATHMod5Int Modulus
MATHCmp6Compare
MATHPow7Power
MATHDist8Distance (counter-rectangle)
MATHNeg16Unary Negate
MATHFlr17Floor (integer part)
MATHSqr18Square root
MATHSin19Sine
MATHCos20Cosine
MATHTan21Tangent
MATHATan22Arc Tangent
MATHExp23Exponent
MATHLog24Logarithm (e)
MATHAbs25Absolute value
MATHSgn26Sign
MATHFRnd27Random (float)
MATHIRnd28Random (integer)
MATHProcessDecimal32Append BCD encoded decimal digits, convert to float
MATHConvertStringToNumber33String to int/float
MATHConvertNumberToString34int/float to string
MATHSetDegRad35Sets the use of degrees (the default) when non zero, radians when zero.
STACK_SIZE2binary operations stack size
VAR_ADDRESS$F0unary operations variable address (5 bytes)
STACK_ADDRESS$F5binary operations stack address (STACK_SIZE * 5 bytes)

Register Variables:

name:address:type:description:
m_integerVAR_ADDRESS+1integerinteger value returned from unary operations
m_floatVAR_ADDRESS+1floatfloat value returned from unary operations

Interface:

name:description:
SetMathStack

procedure SetMathStack(v:float;i:byte);assembler;overload;register;


Inserts float value to the Math stack at the specified position.
    parameters:
  • v (float) - value to be inserted
  • i (byte) - stack position
SetMathStack

procedure SetMathStack(v:integer;i:byte);assembler;overload;register;


Inserts integer value to the Math stack at the specified position.
    parameters:
  • v (integer) - value to be inserted
  • i (byte) - stack position
GetMathStackFloat

function GetMathStackFloat:float;assembler;


Returns float value from Math stack at position 0
    returns:
  • (float) - value at position ptr represented as an float
GetMathStackInt

function GetMathStackInt:integer;assembler;


Returns integer value from Math stack at position 0
    returns:
  • (integer) - value at position ptr represented as an float
IsFloatOnStack

function IsFloatOnStack(i:byte):boolean;


Returns true if at desired position on the Math Stack float value is found.
    parameters:
  • i (byte) - stack position
  • returns:
  • (boolean) - returns true if float
IsFloatVal

function IsFloatVal:boolean;


Returns true if float value is located at MathVar.
    returns:
  • (boolean) - returns true if float
SetMathVar

procedure SetMathVar(v:integer);overload;assembler;register;


Sets integer value as the MathVar (operation register for unary)
    parameters:
  • v (integer) - value to be inserted
SetMathVar

procedure SetMathVar(v:float);overload;assembler;register;


Sets float value as the MathVar (operation register for unary)
    parameters:
  • v (integer) - value to be inserted
DoMathOnStack

procedure DoMathOnStack(cmd:byte);register;


Perform selected operation on the MathStack
    parameters:
  • cmd (byte) - operation id
DoMathOnVar

procedure DoMathOnVar(cmd:byte);register;


Perform selected operation on the MathVar
    parameters:
  • cmd (byte) - operation id
AddFractionalBCD

function AddFractionalBCD(v0:float;bcd:pointer):float;


Adds fractional part to the float variable
    parameters:
  • v0 (float) - initial number
  • bcd (pointer) - pointer to the array containg BCD nibbles. Must be terminated with $F.
  • returns:
  • (float) - returns float value
NeoIntRandom

function NeoIntRandom(range:integer):integer;


Returns an random integer value in the specified range.
    parameters:
  • range (integer) - upper limit
  • returns:
  • (integer) - random value (0..range-1)
NeoFloatRandom

function NeoFloatRandom():float;


Returns a random floating point value in the range 0..1
    returns:
  • (float) - random float value (0..1)
NeoStr

procedure NeoStr(i:integer;var s:string);overload;


Converts integer value into string variable (size is returned in the first byte)
    parameters:
  • i (integer) - value to be converted
  • s (string) - pointer to the string to be filled with the result.
NeoStr

procedure NeoStr(i:float;var s:string);overload;


Converts float value into string variable (size is returned in the first byte)
    parameters:
  • i (float) - value to be converted
  • s (string) - pointer to the string to be filled with the result.
NeoParseInt

function NeoParseInt(var s:string):integer;


Parses string into integer value.
    parameters:
  • s (string) - string to be converted.
  • returns:
  • (integer) - parsed value
NeoParseFloat

function NeoParseFloat(var s:string):float;


Parses string into float value.
    parameters:
  • s (string) - string to be converted.
  • returns:
  • (float) - parsed value
SetDegreeMode;assembler;inline

procedure SetDegreeMode;assembler;inline;


Sets the use of degrees.
    SetRadianMode;assembler;inline

    procedure SetRadianMode;assembler;inline;


    Sets the use of radians.