diff --git a/doc/stdlib.txt b/doc/stdlib.txt index 9a51a2c..62b863c 100644 --- a/doc/stdlib.txt +++ b/doc/stdlib.txt @@ -78,6 +78,8 @@ The following functions are defined: post-operators will generate smaller code for less than 5 shifts and will always be faster. + c = swap(b); Returns the byte b with the high and low nybbles + swapped. Note: This library expects the following function to be defined diff --git a/include/stdlib.a02 b/include/stdlib.a02 index 0031d40..54e5ff7 100644 --- a/include/stdlib.a02 +++ b/include/stdlib.a02 @@ -1,4 +1,4 @@ -; C02 library stdlib.h02 assembly language subroutines +f; C02 library stdlib.h02 assembly language subroutines ; Requires ; external zero page locations SRCLO and srchi ; and external locations RANDOM, RDSEED, TEMP0, TEMP1, and TEMP2. @@ -40,7 +40,7 @@ MINX: RTS ; Y = Nultiplier ;Sets: TEMP0 = Multiplicand ; TEMP1 = 0 -; TEMP3 = Product MSB +; TEMP2 = Product MSB ;Affects: N,Z,C ;Returns: A = Product LSB ; Y = Product MSB @@ -48,15 +48,15 @@ MULT: STA TEMP0 ;Store Multiplicand STY TEMP1 ;Store Multiplier ;Multiply TEMP0 times TEMP1 MULTT: LDA #$00 ;Initialize Accumulator - STA TEMP2 ;and Result High Byte BEQ MULTE ;Enter Loop MULTA: CLC ADC TEMP0 ;Add Multiplicand MULTL: ASL TEMP0 ;Shift Multiplicand Left - ROL TEMP2 MULTE: LSR TEMP1 ;Shift Multiplier Right BCS MULTA ;If Bit Shifted Out, Add Multiplicand BNE MULTL ;Loop if Any 1 Bits Left + LDY TEMP2 ;Load Y with MSB + TAX ;and Copy LSB to X RTS ;div(m,n) - MULTiply Two Numbers @@ -81,7 +81,7 @@ DIVL: ROL TEMP0 ;Shift Bit Out of Dividend DIVS: DEX ;Decrement Counter BPL DIVL ; and Loop ROL TEMP0 ;Shift Result into Dividend - TYA ;Copy Remainder to Y Register + TAY ;Copy Remainder to Y Register LDA TEMP0 ;Load Result into Accumulator RTS @@ -121,6 +121,18 @@ SHIFTR: LSR ;Shift Byte to Right BNE SHIFTR ; and Loop if Not 0 RTS +;SWAP Nybbles in Byte +;Args: A = Byte containing Nybbles to Swap +;Affects: C,N,Z +;Returns: A = Byte with Nybbles Swapped +SWAP: ASL ;Code by + ADC #$80 ;David Galloway + ROL ;posted on + ASL ;6502.org by + ADC #$80 ;Garth Wilson + ROL ;Oct 27, 2017 + RTS + ;atoc(&s) - ASCII string TO Character ;Args: Y,X = Address of String to Convert ;Uses: TEMP0, TEMP1 @@ -159,7 +171,7 @@ ATOCX: LDA TEMP0 ;Load Result ;Returns: A,Y = Length of String CTOA: JSR SETSRC ;Initialize Source String JSR CUBCD ;Convert Accumulator to Unpacked BCD - LDA TEMP2 ;Get High Byte + LDA TEMP2 ;Get MSB BEQ CTOA1 ;If Not Zero JSR CTOAN ; Convert Low Nybble CTOA1: LDA TEMP1 ;Get Low Byte @@ -218,10 +230,11 @@ CVBCDL: ASL TEMP0 ;Shift High Bit Into Carry LDA TEMP1 ;Add BCD Low Byte to Itself ADC TEMP1 ; Plus Bit Shifted out of Binary STA TEMP1 ; Effectively Multiplying It by 2 - LDA TEMP2 ;Add BCD High Byte to Itself + LDA TEMP2 ;Add BCD MSB to Itself ADC TEMP2 ; Plus Bit Shifted out of Low Byte STA TEMP2 ; Effectively Multiplying It by 2 DEX ;Decrement Counter and BNE CVBCDL ; Process Next Bit PLP ;Restore Status Register RTS + diff --git a/include/stdlib.h02 b/include/stdlib.h02 index f8abc2a..82f6d76 100644 --- a/include/stdlib.h02 +++ b/include/stdlib.h02 @@ -56,15 +56,20 @@ char rand(); /* Shift Byte Left * * Args: b - Byte to Shift * * n - Number of Bits to Shift * - * Returns: Shifted Byte */ + * Returns: Shifted Byte */ char shiftl(); /* Shift Byte Right * * Args: b - Byte to Shift * * n - Number of Bits to Shift * - * Returns: Shifted Byte */ + * Returns: Shifted Byte */ char shiftr(); +/* Swap Nybbles in Byte * + * Args: b - Byte to Swap * + * Returns: Swapped Byte */ +char swap(); + /* Seed Pseudo-Random Number Generator * * Args: n - Seed number */ void srand(); diff --git a/py65/testsl.c02 b/py65/testsl.c02 index fb2d11a..06dea4b 100644 --- a/py65/testsl.c02 +++ b/py65/testsl.c02 @@ -3,6 +3,7 @@ ************************************************/ #include +#include #include main: @@ -35,17 +36,21 @@ anloop: newlin(); tstmlt: //Test mult() -char mltplr, mltpnd, prodct, acmltr; +char mltplr, mltpnd, acmlsb, acmmsb, acmlst; +char prodct, ovrflw; mltplr = 1; mrloop: prbyte(mltplr); - mltpnd = 1; - acmltr = 0; + mltpnd = 1; acmlst = 0; + acmlsb = 0; acmmsb = 0; mdloop: - acmltr = acmltr + mltplr; - prodct = mult(mltplr,mltpnd); - if (prodct <> acmltr) goto merror; + acmlsb = acmlsb + mltplr; + if (acmlsb acmlsb) goto merror; + //if (ovrflw <> acmmsb) goto merror; mltpnd++; if (mltpnd > 0) goto mdloop; mltplr++; @@ -68,10 +73,10 @@ maxmpd = 1; drloop: prbyte(mltplr); mltpnd = 1; - acmltr = 0; + acmlsb = 0; ddloop: prbyte(mltpnd); - acmltr = acmltr + mltplr; + acmlsb = acmlsb + mltplr; prodct = mult(mltplr, mltpnd); quotnt = div(prodct, mltpnd); if (quotnt <> mltplr) goto derror; @@ -143,6 +148,25 @@ caloop: prchr('K'); newlin(); +doswap: + char i,j,m,n,p,r; + for (i=0;i<16;i++) { + for (j=0;j<16;j++) { + m = shiftl(i,4)+j; prbyte(m); + n = shiftl(j,5)+i; prchr('~'); + p = swap(m); prbyte(p); + prchr(' '); + } + newlin(); + } + prchr('s'); + prchr('w'); + prchr('a'); + prchr('p'); + prchr(' '); + prchr('O'); + prchr('K'); + goto exit; abserr: @@ -165,9 +189,11 @@ merror: prchr('*'); prbyte(mltpnd); prchr('='); + //prbyte(ovrflw); prbyte(prodct); prchr(','); - prbyte(acmltr); + //prbyte(acmmsb); + prbyte(acmlsb); newlin(); goto exit;