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:
parent
cca4e7aea1
commit
bbb55e2946
@ -78,6 +78,8 @@ The following functions are defined:
|
|||||||
post-operators will generate smaller code for less
|
post-operators will generate smaller code for less
|
||||||
than 5 shifts and will always be faster.
|
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
|
Note: This library expects the following function to be defined
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
; C02 library stdlib.h02 assembly language subroutines
|
f; C02 library stdlib.h02 assembly language subroutines
|
||||||
; Requires
|
; Requires
|
||||||
; external zero page locations SRCLO and srchi
|
; external zero page locations SRCLO and srchi
|
||||||
; and external locations RANDOM, RDSEED, TEMP0, TEMP1, and TEMP2.
|
; and external locations RANDOM, RDSEED, TEMP0, TEMP1, and TEMP2.
|
||||||
@ -40,7 +40,7 @@ MINX: RTS
|
|||||||
; Y = Nultiplier
|
; Y = Nultiplier
|
||||||
;Sets: TEMP0 = Multiplicand
|
;Sets: TEMP0 = Multiplicand
|
||||||
; TEMP1 = 0
|
; TEMP1 = 0
|
||||||
; TEMP3 = Product MSB
|
; TEMP2 = Product MSB
|
||||||
;Affects: N,Z,C
|
;Affects: N,Z,C
|
||||||
;Returns: A = Product LSB
|
;Returns: A = Product LSB
|
||||||
; Y = Product MSB
|
; Y = Product MSB
|
||||||
@ -48,15 +48,15 @@ MULT: STA TEMP0 ;Store Multiplicand
|
|||||||
STY TEMP1 ;Store Multiplier
|
STY TEMP1 ;Store Multiplier
|
||||||
;Multiply TEMP0 times TEMP1
|
;Multiply TEMP0 times TEMP1
|
||||||
MULTT: LDA #$00 ;Initialize Accumulator
|
MULTT: LDA #$00 ;Initialize Accumulator
|
||||||
STA TEMP2 ;and Result High Byte
|
|
||||||
BEQ MULTE ;Enter Loop
|
BEQ MULTE ;Enter Loop
|
||||||
MULTA: CLC
|
MULTA: CLC
|
||||||
ADC TEMP0 ;Add Multiplicand
|
ADC TEMP0 ;Add Multiplicand
|
||||||
MULTL: ASL TEMP0 ;Shift Multiplicand Left
|
MULTL: ASL TEMP0 ;Shift Multiplicand Left
|
||||||
ROL TEMP2
|
|
||||||
MULTE: LSR TEMP1 ;Shift Multiplier Right
|
MULTE: LSR TEMP1 ;Shift Multiplier Right
|
||||||
BCS MULTA ;If Bit Shifted Out, Add Multiplicand
|
BCS MULTA ;If Bit Shifted Out, Add Multiplicand
|
||||||
BNE MULTL ;Loop if Any 1 Bits Left
|
BNE MULTL ;Loop if Any 1 Bits Left
|
||||||
|
LDY TEMP2 ;Load Y with MSB
|
||||||
|
TAX ;and Copy LSB to X
|
||||||
RTS
|
RTS
|
||||||
|
|
||||||
;div(m,n) - MULTiply Two Numbers
|
;div(m,n) - MULTiply Two Numbers
|
||||||
@ -81,7 +81,7 @@ DIVL: ROL TEMP0 ;Shift Bit Out of Dividend
|
|||||||
DIVS: DEX ;Decrement Counter
|
DIVS: DEX ;Decrement Counter
|
||||||
BPL DIVL ; and Loop
|
BPL DIVL ; and Loop
|
||||||
ROL TEMP0 ;Shift Result into Dividend
|
ROL TEMP0 ;Shift Result into Dividend
|
||||||
TYA ;Copy Remainder to Y Register
|
TAY ;Copy Remainder to Y Register
|
||||||
LDA TEMP0 ;Load Result into Accumulator
|
LDA TEMP0 ;Load Result into Accumulator
|
||||||
RTS
|
RTS
|
||||||
|
|
||||||
@ -121,6 +121,18 @@ SHIFTR: LSR ;Shift Byte to Right
|
|||||||
BNE SHIFTR ; and Loop if Not 0
|
BNE SHIFTR ; and Loop if Not 0
|
||||||
RTS
|
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
|
;atoc(&s) - ASCII string TO Character
|
||||||
;Args: Y,X = Address of String to Convert
|
;Args: Y,X = Address of String to Convert
|
||||||
;Uses: TEMP0, TEMP1
|
;Uses: TEMP0, TEMP1
|
||||||
@ -159,7 +171,7 @@ ATOCX: LDA TEMP0 ;Load Result
|
|||||||
;Returns: A,Y = Length of String
|
;Returns: A,Y = Length of String
|
||||||
CTOA: JSR SETSRC ;Initialize Source String
|
CTOA: JSR SETSRC ;Initialize Source String
|
||||||
JSR CUBCD ;Convert Accumulator to Unpacked BCD
|
JSR CUBCD ;Convert Accumulator to Unpacked BCD
|
||||||
LDA TEMP2 ;Get High Byte
|
LDA TEMP2 ;Get MSB
|
||||||
BEQ CTOA1 ;If Not Zero
|
BEQ CTOA1 ;If Not Zero
|
||||||
JSR CTOAN ; Convert Low Nybble
|
JSR CTOAN ; Convert Low Nybble
|
||||||
CTOA1: LDA TEMP1 ;Get Low Byte
|
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
|
LDA TEMP1 ;Add BCD Low Byte to Itself
|
||||||
ADC TEMP1 ; Plus Bit Shifted out of Binary
|
ADC TEMP1 ; Plus Bit Shifted out of Binary
|
||||||
STA TEMP1 ; Effectively Multiplying It by 2
|
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
|
ADC TEMP2 ; Plus Bit Shifted out of Low Byte
|
||||||
STA TEMP2 ; Effectively Multiplying It by 2
|
STA TEMP2 ; Effectively Multiplying It by 2
|
||||||
DEX ;Decrement Counter and
|
DEX ;Decrement Counter and
|
||||||
BNE CVBCDL ; Process Next Bit
|
BNE CVBCDL ; Process Next Bit
|
||||||
PLP ;Restore Status Register
|
PLP ;Restore Status Register
|
||||||
RTS
|
RTS
|
||||||
|
|
||||||
|
@ -56,15 +56,20 @@ char rand();
|
|||||||
/* Shift Byte Left *
|
/* Shift Byte Left *
|
||||||
* Args: b - Byte to Shift *
|
* Args: b - Byte to Shift *
|
||||||
* n - Number of Bits to Shift *
|
* n - Number of Bits to Shift *
|
||||||
* Returns: Shifted Byte */
|
* Returns: Shifted Byte */
|
||||||
char shiftl();
|
char shiftl();
|
||||||
|
|
||||||
/* Shift Byte Right *
|
/* Shift Byte Right *
|
||||||
* Args: b - Byte to Shift *
|
* Args: b - Byte to Shift *
|
||||||
* n - Number of Bits to Shift *
|
* n - Number of Bits to Shift *
|
||||||
* Returns: Shifted Byte */
|
* Returns: Shifted Byte */
|
||||||
char shiftr();
|
char shiftr();
|
||||||
|
|
||||||
|
/* Swap Nybbles in Byte *
|
||||||
|
* Args: b - Byte to Swap *
|
||||||
|
* Returns: Swapped Byte */
|
||||||
|
char swap();
|
||||||
|
|
||||||
/* Seed Pseudo-Random Number Generator *
|
/* Seed Pseudo-Random Number Generator *
|
||||||
* Args: n - Seed number */
|
* Args: n - Seed number */
|
||||||
void srand();
|
void srand();
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
************************************************/
|
************************************************/
|
||||||
|
|
||||||
#include <py65.h02>
|
#include <py65.h02>
|
||||||
|
#include <stddef.h02>
|
||||||
#include <stdlib.h02>
|
#include <stdlib.h02>
|
||||||
|
|
||||||
main:
|
main:
|
||||||
@ -35,17 +36,21 @@ anloop:
|
|||||||
newlin();
|
newlin();
|
||||||
|
|
||||||
tstmlt: //Test mult()
|
tstmlt: //Test mult()
|
||||||
char mltplr, mltpnd, prodct, acmltr;
|
char mltplr, mltpnd, acmlsb, acmmsb, acmlst;
|
||||||
|
char prodct, ovrflw;
|
||||||
|
|
||||||
mltplr = 1;
|
mltplr = 1;
|
||||||
mrloop:
|
mrloop:
|
||||||
prbyte(mltplr);
|
prbyte(mltplr);
|
||||||
mltpnd = 1;
|
mltpnd = 1; acmlst = 0;
|
||||||
acmltr = 0;
|
acmlsb = 0; acmmsb = 0;
|
||||||
mdloop:
|
mdloop:
|
||||||
acmltr = acmltr + mltplr;
|
acmlsb = acmlsb + mltplr;
|
||||||
prodct = mult(mltplr,mltpnd);
|
if (acmlsb<acmlst) acmmsb++;
|
||||||
if (prodct <> acmltr) goto merror;
|
acmlst = acmlsb;
|
||||||
|
prodct,ovrflw = mult(mltplr,mltpnd);
|
||||||
|
if (prodct <> acmlsb) goto merror;
|
||||||
|
//if (ovrflw <> acmmsb) goto merror;
|
||||||
mltpnd++;
|
mltpnd++;
|
||||||
if (mltpnd > 0) goto mdloop;
|
if (mltpnd > 0) goto mdloop;
|
||||||
mltplr++;
|
mltplr++;
|
||||||
@ -68,10 +73,10 @@ maxmpd = 1;
|
|||||||
drloop:
|
drloop:
|
||||||
prbyte(mltplr);
|
prbyte(mltplr);
|
||||||
mltpnd = 1;
|
mltpnd = 1;
|
||||||
acmltr = 0;
|
acmlsb = 0;
|
||||||
ddloop:
|
ddloop:
|
||||||
prbyte(mltpnd);
|
prbyte(mltpnd);
|
||||||
acmltr = acmltr + mltplr;
|
acmlsb = acmlsb + mltplr;
|
||||||
prodct = mult(mltplr, mltpnd);
|
prodct = mult(mltplr, mltpnd);
|
||||||
quotnt = div(prodct, mltpnd);
|
quotnt = div(prodct, mltpnd);
|
||||||
if (quotnt <> mltplr) goto derror;
|
if (quotnt <> mltplr) goto derror;
|
||||||
@ -143,6 +148,25 @@ caloop:
|
|||||||
prchr('K');
|
prchr('K');
|
||||||
newlin();
|
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;
|
goto exit;
|
||||||
|
|
||||||
abserr:
|
abserr:
|
||||||
@ -165,9 +189,11 @@ merror:
|
|||||||
prchr('*');
|
prchr('*');
|
||||||
prbyte(mltpnd);
|
prbyte(mltpnd);
|
||||||
prchr('=');
|
prchr('=');
|
||||||
|
//prbyte(ovrflw);
|
||||||
prbyte(prodct);
|
prbyte(prodct);
|
||||||
prchr(',');
|
prchr(',');
|
||||||
prbyte(acmltr);
|
//prbyte(acmmsb);
|
||||||
|
prbyte(acmlsb);
|
||||||
newlin();
|
newlin();
|
||||||
goto exit;
|
goto exit;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user