1
0
mirror of https://github.com/RevCurtisP/C02.git synced 2024-11-22 01:31:33 +00:00

Added function swap() to module 'stdlib'

This commit is contained in:
Curtis F Kaylor 2018-07-30 19:30:20 -04:00
parent cca4e7aea1
commit bbb55e2946
4 changed files with 64 additions and 18 deletions

View File

@ -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

View File

@ -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

View File

@ -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();

View File

@ -3,6 +3,7 @@
************************************************/
#include <py65.h02>
#include <stddef.h02>
#include <stdlib.h02>
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<acmlst) acmmsb++;
acmlst = acmlsb;
prodct,ovrflw = mult(mltplr,mltpnd);
if (prodct <> 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;