Page 1 of 2
Attention code golfers...
Posted: Wed Mar 09, 2022 8:51 am
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.
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 9:06 am
by gfoot
Something like this - it puts the result in the carry flag rather than the negative flag:
A CMOS 6502 could do this all in one instruction but you asked for MMOS explicitly.
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 9:19 am
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
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 10:10 am
by BigDumbDinosaur
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?
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 10:13 am
by BillG
The Ready line is bit 7 for the parallel port. It is bit 1 for the serial port.
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 12:47 pm
by Dr Jefyll
Something like this - it puts the result in the carry flag rather than the negative flag:
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
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 2:38 pm
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.
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 5:35 pm
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
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 7:17 pm
by IamRob
The ROR's can be replaced with LSR's if only the carry flag is pertinent.
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 8:45 pm
by barrym95838
I don't recommend it, unless you only JSR $EAxx ... PC will be one byte off ...
Re: Attention code golfers...
Posted: Wed Mar 09, 2022 8:46 pm
by barrym95838
The ROR's can be replaced with LSR's if only the carry flag is pertinent.
... and it works around that pesky ROR bug!
Re: Attention code golfers...
Posted: Thu Mar 10, 2022 2:29 am
by BillG
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...
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.
Re: Attention code golfers...
Posted: Thu Mar 10, 2022 2:33 am
by BillG
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.
Re: Attention code golfers...
Posted: Thu Mar 10, 2022 6:22 pm
by gfoot
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.
Re: Attention code golfers...
Posted: Sat Mar 12, 2022 12:18 pm
by BillG
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.