*
********************************
*                              *
*      -< MATH  DEMO >-        *
*                              *
*       VERSION 00.03.00       *
*                              *
*         20-JAN-2018          *
*                              *
********************************
*                              *
*       NATHAN D. RIGGS        *
*   NATHAN.RIGGS@OUTLOOK.COM   *
*                              *
********************************
*
** ASSEMBLER DIRECTIVES
*
         CYC   AVE
         EXP   ONLY
         TR    ON
         DSK   MATH.DEMO
         OBJ   $BFE0
         ORG   $6000
*
*``````````````````````````````*
*  TOP INCLUDES (HOOKS,MACROS) *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
         PUT   MIN.VARS.REQUIRED
         PUT   MIN.HOOKS.REQUIRED
         USE   MIN.MAC.REQUIRED
         PUT   MIN.HOOKS.MATH
         USE   MIN.MAC.MATH
*
*``````````````````````````````*
*      PROGRAM MAIN BODY       *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
         JSR   $FC58      ; HOME
         _PRNT "INTEGER MATH LIBRARIES AND",8D
         _PRNT "MACROS.                ",8D
         _PRNT "-------------------------",8D8D
         _PRNT "THE RAND8 ROUTINE RETURNS",8D
         _PRNT "A PSEUDO-RANDOM VALUE BETWEEN",8D
         _PRNT "1 AND 255.",8D8D
         JSR   RAND8
         STA   $300
         JSR   RAND8
         STA   $301
         JSR   RAND8
         STA   $302
         JSR   RAND8
         STA   $303
         JSR   RAND8
         STA   $304
         DUMP  #$300;#5
         _WAIT
*
         _PRNT " ",8D8D
         _PRNT "WHILE THE RNDB MACRO RETURNS",8D
         _PRNT "A BYTE VALUE BETWEEN A GIVEN",8D
         _PRNT "LOW AND HIGH VALUE.",8D8D
         RNDB  #$A0;#$AF
         STA   $300
         RNDB  #$A0;#$AF
         STA   $301
         RNDB  #$A0;#$AF
         STA   $302
         RNDB  #$A0;#$AF
         STA   $303
         RNDB  #$A0;#$AF
         STA   $304
         DUMP  #$300;#5
         _WAIT
*
         _PRNT " ",8D8D
         _PRNT "THE MUL8 MACRO MULTIPLIES",8D
         _PRNT "TWO UNSIGNED BYTES AND RETURNS",8D
         _PRNT "A 16-BIT RESULT (WORD).",8D
         _PRNT "NOTE THAT THIS RESULT IS STORED",8D
         _PRNT "WITH THE LOW BYTE FIRST, THEN THE",8D
         _PRNT "HIGH BYTE, AS PER 6502 CONVENTION.",8D8D
         _PRNT ":: MUL8 #10;#10 RETURNS (L,H):",8D
         LDA   #10
         STA   $300
         MUL8  #10;$300
         DUMP  #RETURN;#2
         _WAIT
         _PRNT " ",8D8D
         _PRNT ":: MUL8 #100;#100 RETURNS (L,H):",8D
         MUL8  #100;#100
         DUMP  RETURN;#2
         _PRNT " ",8D8D
         _WAIT
*
         _PRNT "THE DIV8 MACRO DIVIDES A ",8D
         _PRNT "DIVIDEND BY A DIVISOR,"8D
         _PRNT "RETURNING THE QUOTIENT.",8D
         _PRNT "ALL VALUES ARE UNSIGNED.",8D8D
         DIV8  #100;#10
         _PRNT ":: DIV8 #100;#10 RETURNS:",8D8D
         DUMP  #RETURN;#1
         _WAIT
         _PRNT "::DIV8 #200;#33 RETURNS",8D8D
         DIV8  #200;#33
         STY   $300       ; REMAINDER IN .Y
         DUMP  #RETURN;#1
         _PRNT " ",8D8D
         _PRNT "WITH A REMAINDER OF: ",8D
         DUMP  #$300;#1
         _PRNT " ",8D8D
         _WAIT
*
** THE FOLLOWING IS DONE JUST TO SHOW THAT
** THESE MACROS ACCEPT LITERAL VALUES AS WELL
** AS POINTERS
*
         LDA   #$D0       ; LOAD #2000 INTO $300
         STA   $300
         LDA   #$07
         STA   $301
         LDA   #$B8
         STA   $302
         LDA   #$0B
         STA   $303
         ADD16 $300;#3000
         _PRNT " ",8D8D
         _PRNT "THE ADD16 MACRO ADDS TWO",8D
         _PRNT "16-BIT VALUES, SIGNED OR UNSIGNED.",8D8D
         _PRNT "ADD16 #3000;#2000 RETURNS: ",8D8D
         DUMP  #RETURN;RETLEN
         _PRNT " ",8D8D
         _WAIT
*
         _PRNT " ",8D8D
         _PRNT "THE SUB16 MACRO SUBTRACTS ONE",8D
         _PRNT "16-BIT NUMBER FROM ANOTHER.",8D8D
         _PRNT "CALCULATED LEFT TO RIGHT, L-R.",8D
         _PRNT "SIGNED OR UNSIGNED.",8D8D
         _PRNT "SUB16 #5000;#3000 RETURNS: ",8D8D
         SUB16 #5000;#3000 ; 5000 - 3000
         DUMP  #RETURN;RETLEN
         _PRNT " ",8D8D
         _WAIT
*
         _PRNT "THE MUL16 MACRO MULTIPLIES TWO",8D
         _PRNT "16-BIT NUMBERS, SIGNED OR NOT.",8D8D
         _PRNT "MUL16 #500;#500 RETURNS: ",8D8D
         MUL16 #500;#500
         DUMP  #RETURN;RETLEN
         _PRNT " ",8D8D
         _WAIT
*
         _PRNT "AND THE DIV16 MACRO DIVIDES THE",8D
         _PRNT "FIRST PARAMETER BY THE SECOND.",8D8D
         _PRNT "IF THE THIRD PARAMETER IS",8D
         _PRNT "AN S, THEN THE DIVISION IS SIGNED;",8D
         _PRNT "OTHERWISE, IT IS UNSIGNED.",8D8D
         _PRNT "DIV16 #25000;#500;U RETURNS:",8D8D
         DIV16 #25000;#500;U
         DUMP  #RETURN;RETLEN
         _PRNT " ",8D8D
         _WAIT
*
         _PRNT "THE REM16 MACRO LIKEWISE CALCULATES",8D
         _PRNT "THE REMAINDER OF A 16-BIT DIVISION.",8D
         _PRNT "IT ALSO USES THE THIRD PARAMETER TO",8D
         _PRNT "SIGNIFY WHETHER IT IS SIGNED (S).",8D8D
         _PRNT "REM16 #5000;#333;U RETURNS:",8D8D
         REM16 #5000;#333;U
         DUMP  #RETURN;RETLEN
         _PRNT " ",8D8D
         _WAIT
*
         _PRNT "CMP16 COMPARES TWO 16-BIT VALUES",8D
         _PRNT "SETTING FLAGS AS WOULD THE CMP ",8D
         _PRNT "COMMAND IN 6502 ASSEMBLY. WE WON'T",8D
         _PRNT "BE ILLUSTRATING THIS FOR NOW ",8D
         _PRNT "BECAUSE I HATE YOU.",8D8D
         _WAIT
*
         _PRNT " ",8D8D
         _PRNT "AND FINALLY, THE RND16 MACRO",8D
         _PRNT "RETURNS A RANDOM 16-BIT VALUE, FROM",8D
         _PRNT "0 TO 65455.",8D8D
         LUP   5
         _PRNT "RND16 RETURNS:",8D8D
         RND16
         DUMP  #RETURN;RETLEN
         _PRNT " ",8D8D
         _WAIT
         --^
*
********************************
*
* THE FOLLOWING IS CURRENTLY UNDER
* CONSTRUCTION, AND DOES NOT YET
* WORK AS PLANNED. FLOATING POINT
* ROUTINES WILL BE ADDED IN THE NEXT
* MAJOR VERSION RELEASE.
*
********************************
*
         _PRNT " ",8D8D8D8D8D8D8D8D
         _PRNT "FLOATING-POINT CALCULATION",8D
         _PRNT "--------------------------",8D8D
         _PRNT "THIS LIBRARY USES THE APPLESOFT",8D
         _PRNT "ROUTINES FOR FLOATING-POINT",8D
         _PRNT "ARITHMETIC, WRITTEN ORIGINALLY",8D
         _PRNT "BY STEVE WOZNIAK. THESE ARE",8D
         _PRNT "PRESENT IN EVERY APPLE II MODEL",8D
         _PRNT "FROM THE APPLE ][+ AND FORWARD.",8D
         _PRNT " ",8D8D
         _WAIT
         _PRNT "FLOATING-POINT VALUES ARE STORED",8D
         _PRNT "USING SCIENTIFIC NOTATION, WHERE",8D
         _PRNT "THE FIRST BYTE IS THE EXPONENT,",8D
         _PRNT "THE SECOND THROUGH FIFTH BYTES",8D
         _PRNT "ARE THE MANTISSA, AND THE SIXTH",8D
         _PRNT "BYTE THE SIGN.",8D8D
         _WAIT
         _PRNT "EXP   MANTISSA     SIGN",8D
         _PRNT "00    01 02 03 04  05",8D8D8D
         LDA   #$84       ; = $80 + EXPONENT; -1 = $79
         STA   $300
         LDA   #$A0       ; MANTISSA
         STA   $301       ; STORED MOST SIGNIFICANT
         LDA   #00
         STA   $302
         STA   $303       ; LEFT TO RIGHT.
         STA   $304
         LDA   #$FF
         STA   $305       ; FF= NEGATIVE, 0 = POSITIVE
         DUMP  #$300;#6
*
** 25E10^-1 = 2.5
*
         _WAIT
         _PRNT "SO, LET'S PUT 2.5 IN $300 AND",8D
         _PRNT "10 IN $310, THEN ADD THEM.",8D8D
         _PRNT "2.5 = 25E-1 SCIENTIFIC NOTATION",8D8D
         _PRNT "    = 01 37 00 00 00 FF",8D8D
         _PRNT "10 = 1E+1",8D
         _PRNT "   = 01 01 00 00 00 00",8D8D
         _PRNT " ",8D8D8D
         LDA   #$84
         LDA   #$A0
         STA   $310
         STA   $311
         LDA   #0
         STA   $312
         STA   $313
         STA   $314
         STA   $315
         _WAIT
         _PRNT " ",8D8D
         _PRNT "NOW LET'S ADD THEM WITH THE",8D
         _PRNT "FPADD MACRO TO SEE THE RESULT.",8D8D
         _PRNT "FPADD $300;$310 RETURNS:",8D8D
         FPADD $300;$310
         DUMP  #RETURN;RETLEN
*
********************************
*
* END OF CONSTRUCTION
*
********************************
*
         JMP   REENTRY
*
*``````````````````````````````*
*        BOTTOM INCLUDES       *
*,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,*
*
** BOTTOM INCLUDES
*
         PUT   MIN.LIB.REQUIRED
*
** INDIVIDUAL SUBROUTINE INCLUDES
*
** 8-BIT MATH SUBROUTINES
*
         PUT   MIN.SUB.MULT8
         PUT   MIN.SUB.DIVD8
         PUT   MIN.SUB.RAND8
         PUT   MIN.SUB.RANDB
*
** 16-BIT MATH SUBROUTINES
*
         PUT   MIN.SUB.ADDIT16
         PUT   MIN.SUB.SUBT16
         PUT   MIN.SUB.COMP16
         PUT   MIN.SUB.MULT16
         PUT   MIN.SUB.DIVD16
         PUT   MIN.SUB.RAND16
*
