6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Fri Nov 22, 2024 1:57 am

All times are UTC




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Is this buggy code?
PostPosted: Tue Jul 12, 2022 5:21 pm 
Offline

Joined: Sun Sep 22, 2019 5:52 pm
Posts: 6
I have a SYM-1. It has a feature where it can draw text on an oscilloscope, and I'm trying to get it to work. I'm using the sample code from the manual, and I think it's buggy. But since I'm not experienced in 6502 programming, I was hoping someone could double check me.

I've attached a snapshot of the code below.

I think that the relative branch at line 0032 is wrong - it's going to jump into the middle of the instruction on line 0030. Likewise, the branch at line 0064 is wrong: it should probably be BNE *+5.

Am I right about this?

And a follow up question... in order to use this code, I need to alter some vectors that the SYM-1 monitor uses to display text. The manual explains how to do that manually from the keyboard. But if I wanted to do it programmatically, how would I? On a 16 bit processor, I'd load a register with the address of the code, then store that address in the vector location. E.g.:
LD D0,#TARGET
ST D0,VECTORLOCATION

On the 6502, I imagine I need to load the low byte of the target address, save it in the low byte of the vector, then repeat with the high byte. I'd like to do something like:
LDA #TARGET
STA VECTORLOCATION
LDA #TARGET>>8
STA VECTORLOCATION+1

But the SYM-1's assembler doesn't support shifting in expressions. It only allows addition and subtraction. Is there an elegant way to do this, or do I just need to load hexadecimal numbers, instead of using labels?

Thank you.


Attachments:
oscope.png
oscope.png [ 210.05 KiB | Viewed 751 times ]
Top
 Profile  
Reply with quote  
 Post subject: Re: Is this buggy code?
PostPosted: Tue Jul 12, 2022 6:20 pm 
Offline

Joined: Sat Jan 02, 2016 10:22 am
Posts: 197
The code looks fine to me.

The jump into the middle of the LDX #$EA at line 0030 works because constant $EA is also the opcode for NOP. The actual delay code is
Code:
NOP
DEX
BNE nop


The other the code assembled at line 0064 is D0 02, it jumps over the LDX #$CC at line 0065
With more lables in the assembler code you'd be looking at
Code:
        LSR A
        BCS LIGHT
DARK:   LDX #&EC
        BNE COMMON ; branch always
LIGHT:  LDX #$CC
COMMON: STX PCR3


Top
 Profile  
Reply with quote  
 Post subject: Re: Is this buggy code?
PostPosted: Tue Jul 12, 2022 6:31 pm 
Offline

Joined: Tue Sep 03, 2002 12:58 pm
Posts: 336
rea5245 wrote:
I think that the relative branch at line 0032 is wrong - it's going to jump into the middle of the instruction on line 0030. Likewise, the branch at line 0064 is wrong: it should probably be BNE *+5.


That first branch is jumping into the middle of the instruction, but that's correct. The operand byte is $EA, which is a NOP instruction. By branching to this, they get an extra two cycles per iteration without making the code any longer. That sort of trickery was a lot more common in the early days, when memory was small and expensive.

The second branch just looks correct to me. In "*+4", the * refers to the address of this instruction, which is $0551. So *+4 is $0555, which is the STX instruction. It's loading X with a value, then jumping over the instruction that loads X with a different value.

They've missed an opportunity to save another byte here: if you replace BNE *+4 with .byte $2C, then the following LDX #$CC becomes the operand of a BIT instruction, and won't be executed as an LDX. That was also a common trick in the old days.

Quote:
LDA #TARGET
STA VECTORLOCATION
LDA #TARGET>>8
STA VECTORLOCATION+1


I don't know the SYM-1's assembler, but the usual syntax for this is
Code:
    LDA #>TARGET
    STA VECTORLOCATION
    LDA #<TARGET
    STA VECTORLOCATION+1

The < and > operators give the high and low bytes of their argument respectively.


Top
 Profile  
Reply with quote  
 Post subject: Re: Is this buggy code?
PostPosted: Tue Jul 12, 2022 8:48 pm 
Offline

Joined: Sun Sep 22, 2019 5:52 pm
Posts: 6
Thank you all. It's a good thing I asked. :-)


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC


Who is online

Users browsing this forum: No registered users and 5 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: