Skip to content

Commit

Permalink
Neo6502: accelerated sin & cos - fix
Browse files Browse the repository at this point in the history
  • Loading branch information
zbyti committed Apr 9, 2024
1 parent 5d32de3 commit 1246ce8
Show file tree
Hide file tree
Showing 3 changed files with 127 additions and 59 deletions.
8 changes: 8 additions & 0 deletions base/neo/single.asm
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,9 @@ A = FP2MAN0
sta VAR2_TYPE

mva #STACK_ADDRESS NEOMESSAGE_PAR1W
stz NEOMESSAGE_PAR1W+1
mva #STACK_SIZE2 NEOMESSAGE_PAR2W
stz NEOMESSAGE_PAR2W+1
jsr @WaitMessage
mva #MATH_ADD NEOMESSAGE_FUNC
mva #MATH_GROUP NEOMESSAGE_GROUP
Expand All @@ -182,7 +184,9 @@ A = FP2MAN0
sta VAR2_TYPE

mva #STACK_ADDRESS NEOMESSAGE_PAR1W
stz NEOMESSAGE_PAR1W+1
mva #STACK_SIZE2 NEOMESSAGE_PAR2W
stz NEOMESSAGE_PAR2W+1
jsr @WaitMessage
mva #MATH_SUB NEOMESSAGE_FUNC
mva #MATH_GROUP NEOMESSAGE_GROUP
Expand Down Expand Up @@ -426,7 +430,9 @@ EXIT:
sta VAR2_TYPE

mva #STACK_ADDRESS NEOMESSAGE_PAR1W
stz NEOMESSAGE_PAR1W+1
mva #STACK_SIZE2 NEOMESSAGE_PAR2W
stz NEOMESSAGE_PAR2W+1
jsr @WaitMessage
mva #MATH_MUL NEOMESSAGE_FUNC
mva #MATH_GROUP NEOMESSAGE_GROUP
Expand All @@ -441,7 +447,9 @@ EXIT:
sta VAR2_TYPE

mva #STACK_ADDRESS NEOMESSAGE_PAR1W
stz NEOMESSAGE_PAR1W+1
mva #STACK_SIZE2 NEOMESSAGE_PAR2W
stz NEOMESSAGE_PAR2W+1
jsr @WaitMessage
mva #MATH_FDIV NEOMESSAGE_FUNC
mva #MATH_GROUP NEOMESSAGE_GROUP
Expand Down
174 changes: 115 additions & 59 deletions lib/neo6502math.pas
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@ interface

const
N6502MSG_ADDRESS = $ff00;
MATHAdd = 0; // Add
MATHSub = 1; // Subtract
MATHMul = 2; // Multiply
MATHFDiv = 3; // Float Divide
MATHIDiv = 4; // Int Divide
MATHMod = 5; // Int Modulus
MATHCmp = 6; // Compare
MATHAdd = 0; // Add
MATHSub = 1; // Subtract
MATHMul = 2; // Multiply
MATHFDiv = 3; // Float Divide
MATHIDiv = 4; // Int Divide
MATHMod = 5; // Int Modulus
MATHCmp = 6; // Compare

MATHNeg = 16; // Unary Negate
MATHFlr = 17; // Floor (integer part)
Expand All @@ -54,152 +54,152 @@ interface
MATHFRnd = 27; // Random (float)
MATHIRnd = 28; // Random (integer)

MATHProcessDecimal = 32; // Append BCD encoded decimal digits, convert to float
MATHProcessDecimal = 32; // Append BCD encoded decimal digits, convert to float
MATHConvertStringToNumber = 33; // String to int/float
MATHConvertNumberToString = 34; // int/float to string
MATHConvertNumberToString = 34; // int/float to string

STACK_SIZE = 2; // binary operations stack size
STACK_SIZE = 2; // binary operations stack size
VAR_ADDRESS = $F0; // unary operations variable address (5 bytes)
STACK_ADDRESS = $F5; // binary operations stack address (STACK_SIZE * 5 bytes)

var
var
m_integer: integer absolute VAR_ADDRESS+1; // integer value returned from unary operations
m_float: float absolute VAR_ADDRESS+1; // float value returned from unary operations

procedure SetMathStack(v:float;i:byte);assembler;overload;
(*
* @description:
* @description:
* Inserts float value to the Math stack at the specified position.
*
*
* @param: v (float) - value to be inserted
* @param: i (byte) - stack position
*
*
*)
procedure SetMathStack(v:integer;i:byte);assembler;overload;
(*
* @description:
* @description:
* Inserts integer value to the Math stack at the specified position.
*
*
* @param: v (integer) - value to be inserted
* @param: i (byte) - stack position
*
*
*)
function GetMathStackFloat:float;assembler;
(*
* @description:
* Returns float value from Math stack at position 0
*
*
* @returns: (float) - value at position ptr represented as an float
*)
function GetMathStackInt:integer;assembler;
(*
* @description:
* Returns integer value from Math stack at position 0
*
*
* @returns: (integer) - value at position ptr represented as an float
*)
function IsFloatOnStack(i:byte):boolean;
(*
* @description:
* @description:
* Returns true if at desired position on the Math Stack float value is found.
*
*
* @param: i (byte) - stack position
*
*
* @returns: (boolean) - returns true if float
*)
function IsFloatVal:boolean;
(*
* @description:
* @description:
* Returns true if float value is located at MathVar.
*
*
* @returns: (boolean) - returns true if float
*)
procedure SetMathVar(v:integer);overload;assembler;
(*
* @description:
* @description:
* Sets integer value as the MathVar (operation register for unary)
*
*
* @param: v (integer) - value to be inserted
*)
procedure SetMathVar(v:float);overload;assembler;
(*
* @description:
* @description:
* Sets float value as the MathVar (operation register for unary)
*
*
* @param: v (integer) - value to be inserted
*)
procedure DoMathOnStack(cmd:byte);
(*
* @description:
* @description:
* Perform selected operation on the MathStack
*
*
* @param: cmd (byte) - operation id
*)
procedure DoMathOnVar(cmd:byte);
(*
* @description:
* @description:
* Perform selected operation on the MathVar
*
*
* @param: cmd (byte) - operation id
*)
function AddFractionalBCD(v0:float;bcd:pointer):float;
function AddFractionalBCD(v0:float;bcd:pointer):float;
(*
* @description:
* @description:
* Adds fractional part to the float variable
*
*
* @param: v0 (float) - initial number
* @param: bcd (pointer) - pointer to the array containg BCD nibbles. Must be terminated with $F.
*
*
* @returns: (float) - returns float value
*)
function NeoIntRandom(range:integer):integer;
(*
* @description:
* @description:
* Returns an random integer value in the specified range.
*
* @param: range (integer) - upper limit
*
*
* @param: range (integer) - upper limit
*
* @returns: (integer) - random value (0..range-1)
*)
function NeoFloatRandom():float;
(*
* @description:
* @description:
* Returns a random floating point value in the range 0..1
*
*
* @returns: (float) - random float value (0..1)
*)
procedure NeoStr(i:integer;var s:string);overload;
(*
* @description:
* @description:
* Converts integer value into string variable (size is returned in the first byte)
*
*
* @param: i (integer) - value to be converted
* @param: s (string) - pointer to the string to be filled with the result.
*)
procedure NeoStr(i:float;var s:string);overload;
(*
* @description:
* @description:
* Converts float value into string variable (size is returned in the first byte)
*
*
* @param: i (float) - value to be converted
* @param: s (string) - pointer to the string to be filled with the result.
*)
function NeoParseInt(var s:string):integer;
(*
* @description:
* @description:
* Parses string into integer value.
*
*
* @param: s (string) - string to be converted.
*
*
* @returns: (integer) - parsed value
*)
function NeoParseFloat(var s:string):float;
(*
* @description:
* @description:
* Parses string into float value.
*
*
* @param: s (string) - string to be converted.
*
*
* @returns: (float) - parsed value
*)
procedure SetDegreeMode;assembler;inline;
Expand All @@ -212,6 +212,24 @@ procedure SetRadianMode;assembler;inline;
* @description:
* Sets the use of radians.
*)
function NeoSin(x:float):float;
(*
* @description:
* Accelerated sinus.
*
* @param: x (float)
*
* @returns: (float)
*)
function NeoCos(x:float):float;
(*
* @description:
* Accelerated cosinus.
*
* @param: x (float)
*
* @returns: (float)
*)
implementation

procedure SetDegreeMode;assembler;inline;
Expand All @@ -228,6 +246,44 @@ procedure SetRadianMode;assembler;inline;
mva #4 N6502MSG_ADDRESS
end;

function NeoSin(x:float):float;
begin
asm
mva #$40 VAR_ADDRESS
mva x VAR_ADDRESS+1
mva x+1 VAR_ADDRESS+2
mva x+2 VAR_ADDRESS+3
mva x+3 VAR_ADDRESS+4
end;

wordParams[0] := VAR_ADDRESS;
NeoMessage.params[2] := 1;
NeoWaitMessage;
NeoMessage.func := MATHSin;
NeoMessage.group := 4;

result := m_float;
end;

function NeoCos(x:float):float;
begin
asm
mva #$40 VAR_ADDRESS
mva x VAR_ADDRESS+1
mva x+1 VAR_ADDRESS+2
mva x+2 VAR_ADDRESS+3
mva x+3 VAR_ADDRESS+4
end;

wordParams[0] := VAR_ADDRESS;
NeoMessage.params[2] := 1;
NeoWaitMessage;
NeoMessage.func := MATHCos;
NeoMessage.group := 4;

result := m_float;
end;

procedure SetMathStack(v:float;i:byte);assembler;overload;
asm
lda i
Expand Down Expand Up @@ -263,7 +319,7 @@ procedure SetMathStack(v:integer;i:byte);assembler;overload;
end;

function GetMathStackFloat:float;assembler;
//var src:array [0..3] of byte absolute result; // @nodoc
//var src:array [0..3] of byte absolute result; // @nodoc
asm
mva STACK_ADDRESS+2 result
mva STACK_ADDRESS+4 result+1
Expand All @@ -272,7 +328,7 @@ function GetMathStackFloat:float;assembler;
end;

function GetMathStackInt:integer;assembler;
//var src:array [0..3] of byte absolute result; // @nodoc
//var src:array [0..3] of byte absolute result; // @nodoc
asm
mva STACK_ADDRESS+2 result
mva STACK_ADDRESS+4 result+1
Expand All @@ -292,7 +348,7 @@ function IsFloatVal:boolean;

procedure SetMathVar(v:integer);overload;assembler;
asm
mva #$00 VAR_ADDRESS
mva #$00 VAR_ADDRESS
mva v VAR_ADDRESS+1
mva v+1 VAR_ADDRESS+2
mva v+2 VAR_ADDRESS+3
Expand Down Expand Up @@ -337,13 +393,13 @@ function AddFractionalBCD(v0:float;bcd:pointer):float;
function NeoIntRandom(range:integer):integer;
begin
SetMathVar(range);
DoMathOnVar(MATHIRnd);
DoMathOnVar(MATHIRnd);
result := m_integer;
end;

function NeoFloatRandom():float;
begin
DoMathOnVar(MATHFRnd);
DoMathOnVar(MATHFRnd);
result := m_float;
end;

Expand Down Expand Up @@ -375,11 +431,11 @@ function NeoParseFloat(var s:string):float;
b:=$0f;
wordParams[2] := word(@s);
DoMathOnVar(MATHConvertStringToNumber);
if not IsFloatVal then begin
if not IsFloatVal then begin
wordParams[2] := word(@b);
DoMathOnVar(MATHProcessDecimal);
end;
result := m_float;
end;

end.
end.
Loading

0 comments on commit 1246ce8

Please sign in to comment.