Attention code golfers...

Programming the 6502 microprocessor and its relatives in assembly and other languages.
BillG
Posts: 710
Joined: 12 Mar 2020
Location: North Tejas

Attention code golfers...

Post by BillG »

This is the ready check for a serial printer in a driver for the 6800:

Code: Select all

*
* Check printer ready, return with sign flag set if ready
*
PChk
    psha            ; Save A

    ldaa    ACIA    ; Get status register
    rora            ; Get TDRE bit into MSB
    rora
    rora

    pula            ; Recover A
    rts
How would you write this for the NMOS 6502?

I can see juggling bytes on the stack or using a temporary variable, but no direct translation.
gfoot
Posts: 871
Joined: 09 Jul 2021

Re: Attention code golfers...

Post by gfoot »

Something like this - it puts the result in the carry flag rather than the negative flag:

Code: Select all

    pha
    lda ACIA
    ror
    ror
    pla
    rts
A CMOS 6502 could do this all in one instruction but you asked for MMOS explicitly.
BillG
Posts: 710
Joined: 12 Mar 2020
Location: North Tejas

Re: Attention code golfers...

Post by BillG »

The interface was originally used for a parallel printer. Space was extremely limited, so they just read the port and the ready line was bit 7. I would rather not change the interface as that would make the parallel driver even bigger.

The best I could come up with is:

Code: Select all

PChk
    pha             ; Save A

    lda     ACIA    ; Get status register
    ror     A       ; Get TDRE bit into MSB
    ror     A
    ror     A
    sta     Temp

    pla             ; Recover A
    bit     Temp    ; Set S flag accordingly
    rts
User avatar
BigDumbDinosaur
Posts: 9425
Joined: 28 May 2009
Location: Midwestern USA (JB Pritzker’s dystopia)
Contact:

Re: Attention code golfers...

Post by BigDumbDinosaur »

BillG wrote:
The interface was originally used for a parallel printer. Space was extremely limited, so they just read the port and the ready line was bit 7. I would rather not change the interface as that would make the parallel driver even bigger.

The best I could come up with is:

Code: Select all

PChk
    pha             ; Save A

    lda     ACIA    ; Get status register
    ror     A       ; Get TDRE bit into MSB
    ror     A
    ror     A
    sta     Temp

    pla             ; Recover A
    bit     Temp    ; Set S flag accordingly
    rts

If the ready line is bit 7 what's wrong with BIT ACIA followed by BPL or BMI?
x86?  We ain't got no x86.  We don't NEED no stinking x86!
BillG
Posts: 710
Joined: 12 Mar 2020
Location: North Tejas

Re: Attention code golfers...

Post by BillG »

The Ready line is bit 7 for the parallel port. It is bit 1 for the serial port.
User avatar
Dr Jefyll
Posts: 3525
Joined: 11 Dec 2009
Location: Ontario, Canada
Contact:

Re: Attention code golfers...

Post by Dr Jefyll »

gfoot wrote:
Something like this - it puts the result in the carry flag rather than the negative flag:

Code: Select all

    pha
    lda ACIA
    ror
    ror
    pla
    rts
George's code looks good to me. Yes, you'd have to make a minor change in every place from which this routine is called (to test Carry rather than the Sign). But that seems tolerable if you're replacing all the 6800 code with 6502 code anyway...

-- Jeff
In 1988 my 65C02 got six new registers and 44 new full-speed instructions!
https://laughtonelectronics.com/Arcana/ ... mmary.html
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Attention code golfers...

Post by barrym95838 »

It looks like the 6800 version is nine bytes. If you have space for ten bytes and you have a "junk" zp location, you can just add an ROR zp to George's code:

Code: Select all

    pha
    lda ACIA
    ror
    ror
    pla
    ror temp
    rts
When I was translating code from 6800 to 6502 I would be often annoyed by the need to tsta after a pula, but this is a good counterexample.
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)
teamtempest
Posts: 443
Joined: 08 Nov 2009
Location: Minnesota
Contact:

Re: Attention code golfers...

Post by teamtempest »

Is there any reason you can't trash X or Y? If not, here's another 10-byte attempt:

Code: Select all

    tax
    lda ACIA
    ror
    ror
    ror
    php
    txa
    rti
IamRob
Posts: 357
Joined: 26 Apr 2020

Re: Attention code golfers...

Post by IamRob »

The ROR's can be replaced with LSR's if only the carry flag is pertinent.
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Attention code golfers...

Post by barrym95838 »

teamtempest wrote:

Code: Select all


    php
    txa
    rti
I don't recommend it, unless you only JSR $EAxx ... PC will be one byte off ...
Last edited by barrym95838 on Thu Mar 10, 2022 2:07 am, edited 2 times in total.
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)
User avatar
barrym95838
Posts: 2056
Joined: 30 Jun 2013
Location: Sacramento, CA, USA

Re: Attention code golfers...

Post by barrym95838 »

IamRob wrote:
The ROR's can be replaced with LSR's if only the carry flag is pertinent.
... and it works around that pesky ROR bug!
Got a kilobyte lying fallow in your 65xx's memory map? Sprinkle some VTL02C on it and see how it grows on you!

Mike B. (about me) (learning how to github)
BillG
Posts: 710
Joined: 12 Mar 2020
Location: North Tejas

Re: Attention code golfers...

Post by BillG »

Dr Jefyll wrote:
Yes, you'd have to make a minor change in every place from which this routine is called (to test Carry rather than the Sign). But that seems tolerable if you're replacing all the 6800 code with 6502 code anyway...
teamtempest wrote:
Is there any reason you can't trash X or Y?
One of the goals of the project is to make it as easy as possible to port 6800 code over. Changing this interface would be yet another thing to forget to check.

The serial driver is under no space pressure.

The parallel driver is. The 6800 version uses 55 of the 56 bytes allotted. I've allocated 64 bytes for the 6502 version and barely managed to code golf it to fit.

I posted that subroutine from the serial driver to see whether I overlooked any coding tricks.
BillG
Posts: 710
Joined: 12 Mar 2020
Location: North Tejas

Re: Attention code golfers...

Post by BillG »

barrym95838 wrote:
It looks like the 6800 version is nine bytes. If you have space for ten bytes and you have a "junk" zp location, you can just add an ROR zp to George's code:
I do not have a spare zp location, but there are bytes to spare within the driver, that temporary variable I used, for instance.

I should have known that you would come up with something.
gfoot
Posts: 871
Joined: 09 Jul 2021

Re: Attention code golfers...

Post by gfoot »

BillG wrote:
barrym95838 wrote:
I do not have a spare zp location, but there are bytes to spare within the driver, that temporary variable I used, for instance.
You could even ROR a read-only location, like something in ROM. The write will fail but the negative flag should be set correctly.
BillG
Posts: 710
Joined: 12 Mar 2020
Location: North Tejas

Re: Attention code golfers...

Post by BillG »

gfoot wrote:
BillG wrote:
barrym95838 wrote:
I do not have a spare zp location, but there are bytes to spare within the driver, that temporary variable I used, for instance.
You could even ROR a read-only location, like something in ROM. The write will fail but the negative flag should be set correctly.
Interesting that you can do it with ROM. That will confuse someone trying to reserve engineer the code unless they happen to know that fact.
Post Reply