6502.org
http://forum.6502.org/

[29] Useful Subroutines
http://forum.6502.org/viewtopic.php?f=7&t=287
Page 1 of 1

Author:  Mike Naberezny [ Tue Mar 07, 2000 5:10 am ]
Post subject:  [29.1] Useful Subroutines

Welcome to the programming area!
This is the place to discuss your programming problems, or better yet, share useful or interesting programs and subroutines. I'll start the ball rolling by providing this little routine from Frank Kontros. It's for converting a numerical value $00-$0F to ASCII 0-F:

SED
CMP #$0A
ADC #$30
CLD

Pretty simple. Now it's your turn.

Author:  gregqci [ Wed Mar 15, 2000 10:50 am ]
Post subject:  [29.2] Useful Subroutines

Hello All
I have up loaded a file that contains a keypad routine which takes advantage of the bus holding devices of the WCW65C22.
With this routine, there is no need for any glue logic and it can be modified for any amount of keys up to 64.

Gregqci

Author:  Mike Naberezny [ Thu Mar 30, 2000 3:11 am ]
Post subject:  [29.3] Useful Subroutines

Hi All,

This didn't turn into the mega-thread I expected. :(

Here's one that I use with my LCD display, it is from "6502 Software Design" by Leo J. Scanlon. Its purpose is to convert a byte into a three digit decimal number that gets printed on the display.

[code:1:23a4a2c25a]B2AD: LDX #$00 ;INITIALIZE HUNDREDS COUNTER

C100: CMP #100 ;BINARY VALUE = OR GREATER THAN 100?

BCC OUT1 ;NO. GO PRINT DECIMAL DIGIT

SBC #100 ;YES. SUBTRACT 100

INX ;INCREMENT DECIMAL COUNT

JMP C100 ; AND COMPARE AGAIN

OUT1: JSR PUTOUT ;GO PRINT DECIMAL DIGIT

LDX #00 ;INITIALIZE TENS COUNTER

C10: CMP #10 ;BINARY VALUE = OR GREATER THAN 10?

BCC OUT2 ;NO. GO PRINT DECIMAL DIGIT

SBC #10 ;YES. SUBTRACT 10

INX ;INCREMENT DECIMAL COUNT

JMP C10 ; AND COMPARE AGAIN

OUT2: JSR PUTOUT ;GO PRINT DECIMAL DIGIT

CLC ;CONVERT REMAINDER TO ASCII

ADC #$30 JMP PTROUT ;AND PRINT IT

RTSPUTOUT: PHA ;SAVE REMAINDER ON STACK

TXA ;MOVE DECIMAL COUNT TO ACCUMULATOR,

ADC #$30 ; CONVERT IT TO ASCII,

JSR PTROUT ; AND PRINT IT

PLA ;RETRIEVE REMAINDER

RTS

[/code:1:23a4a2c25a]

To call this routine, load the byte you want printed into the accumulator and JSR B2AD ("Binary to ASCII Decimal"). Theroutine will return three decimal digits in the accumulatorto a routine "PTROUT".

Anyone else have a good subroutine?

Author:  paulrsm [ Tue Apr 04, 2000 9:21 pm ]
Post subject:  [29.4] Useful Subroutines

* Name PrintDecW
* Purpose Print a 16-bit number in decimal
* Input Areg = low byte of num to be printed
* Yreg = high byte of num to be printed
* Xreg = leading char:
* $00 for no leading chars
* (left justified)
* $A0 for leading blank spaces
* (right justified)
* $B0 for leading zeros
* Output Xreg = $FF
* Uses Areg
* Xreg = digit ctr
* Yreg = digit
* ptr0 = num to be printed
* LeadChar = leading char
* LeadZero = leading zeros flag
* pos while finding leading zeros
* neg after end of leading zeros
* Calls MON_COUT
* Reference loosely based on Integer BASIC
* routine by WOZ
* Note handles 0 to 65535 ($0000 to $FFFF)

* variables:
* num (ptr0)
* leading char (LeadChar)
* digit ctr (Xreg)
* digit (Yreg)
* leading zeros flag (LeadZero)

* leading zeros flag := true
* for digit ctr = 4 to 0 step -1
* digit := "0"-1
* while num >= 0
* digit := digit + 1
* num := num - power of tem
* endw
* num := num + power of ten
* print digit or leading char
* endf

PrintDecW
STX LeadChar ;set up leading char

* alternate entry point if leading
* char is already set up

PrintDecW_1
STA ptr0
STY ptr0+1

STZ LeadZero ;init to pos
LDX #4 ;5 bytes to print

*!loop
LDY #"0"-1 ;init digit to print

*! loop
INY
LDA ptr0
SEC
SBC PowerOf10Lo,X
STA ptr0
LDA ptr0+1
SBC PowerOf10Hi,X
STA ptr0+1
*! until <mi>

* compensate for one too many subtractions

LDA ptr0
CLC
ADC PowerOf10Lo,X
STA ptr0
LDA ptr0+1
ADC PowerOf10Hi,X
STA ptr0+1

JSR FrontChar ;print digit

CPX #3 ;just printed thousands?
*! if <eq>
* must preserve Xreg, but can destroy Areg & Yreg
LDY #"," ;print a comma
BIT LeadZero ;are we still printing
; leading zeros?
*! if <pl>
LDY LeadChar ;yes, so print lead char
*! endif
TYA ;Areg := char to print
*! if <ne>
JSR MON_COUT ;print the comma or
; lead char
*! endif
*! endif

DEX
*!until <mi>
RTS

*---------
* Name PrintDecB
* Purpose Print an 8-bit number in decimal
* Input Areg = byte of num to be printed
* Xreg = leading char:
* $00 for no leading chars
* (left justified)
* $A0 for leading blank spaces
* (right justified)
* $B0 for leading zeros
* Note handles 0 to 255 ($00 to $FF)

PrintDecB
STX LeadChar ;set up leading char

PrintDecB_1
STA ptr0
PHY ;save Yreg

STZ LeadZero ;init to pos
LDX #2 ;3 bytes to print

*!loop
LDY #"0"-1 ;init digit to print
LDA ptr0

*! loop
INY ;incr count
SEC ;Areg := Areg - 10^Xreg
SBC PowerOf10Lo,X
*! until <mi>

* correct for one too many subtractions

CLC
ADC PowerOf10Lo,X
STA ptr0

JSR FrontChar ;print digit
DEX
*!until <mi>
PLY ;restore Yreg
RTS

*---------

* if digit ctr <> 0 then
* if digit <> "0" then
* leading zeros flag := false
* else
* if leading zeros flag = true then
* digit := leading char
* endif
* endif
* if digit <> 0 then
* print char in Yreg

FrontChar
TXA ;doing last digit?
*!if <ne>
CPY #"0" ;still doing leading zeros?
*! if <ne>
DEC LeadZero ;no, do not substitute
; leading char
*! else
BIT LeadZero ;substitute leading char?
*! if <pl>
LDY LeadChar ;yes
*! endif
*! endif
*!endif
TYA ;Areg := char to print
*!if <ne>
JSR MON_COUT ;print char
*!endif
RTS

PowerOf10Lo
DB <1
DB <10
DB <100
DB <1000
DB <10000

PowerOf10Hi
DB >1
DB >10
DB >100
DB >1000
DB >10000

Page 1 of 1 All times are UTC
Powered by phpBB® Forum Software © phpBB Group
http://www.phpbb.com/