Learning 6502 Assembly on the PAL-1, Need some help

126 views
Skip to first unread message

Kaveen R

unread,
Sep 12, 2022, 9:55:00 AM9/12/22
to PAL 6502 computer
Hey folks, 

To get the most out of my PAL-1, I started to learn 6502 assembly and started playing with some of the ROM routines of the KIM-1!

I started by creating a small program that allows you to select a number on the keypad and press the AD, DA buttons to add or subtract. 


But I've been stuck on this JMP issue, where I cannot JMP to my loop point, as it just yields all zeros on my display (via the SCANDS subroutine) 

    jmp loop

But if I use a BNE to branch back to the loop point it "works" 😅

    inc $fd            ; why do I have to do this?
    bne loop           ; Tell me why :') ????


I'm aware that this might be a tad out of scope for this group.  But my guess is I'm making a stupid mistake here and y'all might catch it...

Thanks!

Jim McClanahan

unread,
Sep 12, 2022, 12:40:08 PM9/12/22
to Kaveen R, PAL 6502 computer
I haven't tinkered with the LEDs or keyboard much, so I'm not familiar with the details. But a couple of thoughts.

The first thing I'd try is adding a CLD at the start--you don't know if you are entering this code with the DECimal flag on or off otherwise. Behavior is going to vary depending on that.

Also, the values are stored in the nibbles of the three bytes displayed--there are six digits stored in 3 bytes. So you will need code to properly shift things through the three bytes 4 bits at a time.

Also, when you read the keyboard, it will  show pressed thousands of times before you let up if you are in a tight loop. You will need to add some code to make sure the key is released before you move on and get the next key stroke.

I've made a pass at some code that will let you load the display with hex digits that move from right to left as you enter new digits in keyp2led.a65 which you can find here:


There is a branch to handle functions, but I didn't do anything with it yet.

Thanks,
Jim W4JBM


--
You received this message because you are subscribed to the Google Groups "PAL 6502 computer" group.
To unsubscribe from this group and stop receiving emails from it, send an email to pal6502+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/pal6502/38ec1720-1e2e-4e3b-b41f-cdc827fa13b8n%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Hans Otten

unread,
Sep 12, 2022, 1:20:00 PM9/12/22
to PAL 6502 computer
Jim already mentioned CLD to make sure yiu are in binary mode. Though your program may work fine in decimal mode!

The other things missing from your sources are the carry set/clear in combination with ADC and SBC.

ADC requires a CLC, SBC a SEC before using the first time in a calculation.

Examples:

  clc 
 lda num1 
 adc num2 
 sta result_low 
 lda result_high 
 adc #$00 
 sta result_high 

sec 
lda num1_low 
sbc num2_low 
sta result_low 
lda num1_high 
sbc num2_high 
sta result_high 

Hans

PS You can try and debug  your program in my KIM-1 Simulator, it emulates SCANDS and GETKEY the way you use it.

Kaveen R

unread,
Sep 12, 2022, 9:12:07 PM9/12/22
to PAL 6502 computer
Thanks Hans and Jim, 

Thanks for the great suggestions, I want to expand this program by adding debouncing and learning how to do arithmetic on the KIM... But I'm still having this darn JMP issue

For simplification, I removed all the function logic, how it just takes a single keypress and stores digit 1&2. But still, I'm having issues


Is it the tool chain? This is my assembly commands for cc65 

ca65 -DKIM1 -g -l kimio.lst kimio.s
ld65 -t none -vm -m kimio.map -o kimio.bin kimio.o
srec_cat kimio.bin -binary -o ../kimio.ptp -MOS_Technologies -CRLF


re: Simulator: I can't find anything for the Mac :( I tried the online one and I can't upload a ptp file for it.

Kaveen

Jim McClanahan

unread,
Sep 12, 2022, 9:42:10 PM9/12/22
to Kaveen R, PAL 6502 computer
That looks like it should run if it is linked right. (That is my pet peeve about ca--I want to give it an address and have it assemble.)

Can you put a couple of zeros at $0200, load the punch file, and make sure they are overwritten?

For quick checks and small projects I will use the online assembler at asm89 dot com. There is an emulator there also, but I haven't used it in years.

My gut tells me the linker/loader is putting your code someplace other than $0200. You could eyeball the punch file also to see what load address it has.

Thanks,
Jim W4JBM 

Jim McClanahan

unread,
Sep 12, 2022, 10:00:48 PM9/12/22
to Kaveen R, PAL 6502 computer
Actually this can't be getting things in the right place. Since you are feeding a binary file to srec, it has no way of knowing where the object code actually belongs.

I would guess it is putting things down at $0000.

There is a workaround I've seen to snag the start address from the symbol file, but I'd have to dig around.

Thanks,
Jim W4JBM 

On Mon, Sep 12, 2022, 9:12 PM Kaveen R <u.k.k....@gmail.com> wrote:

Kaveen R

unread,
Sep 12, 2022, 11:03:35 PM9/12/22
to PAL 6502 computer
Thanks Jim!

That was exactly the issue, I was able to pass the offset to srecord!

ca65 -g -l kimio.lst kimio.s

ld65 -t none -vm -m kimio.map -o kimio.bin kimio.o
srec_cat kimio.bin -binary -offset 0x0200 -o ../kimio.ptp -MOS_Technologies -CRLF

That did the trick, Next time I won't be lazy and inspect the memory of the KIM! 

Cheers,
Kaveen

Hendrik-Jan Megens

unread,
Sep 13, 2022, 3:30:36 AM9/13/22
to PAL 6502 computer
Hi Kaveen,

can't offer much in addition to what Jim and Hans already replied, other than to respond to your concern regarding scope: your question is exactly right for this forum and I would encourage everybody to share similar questions. I'm relatively new to 6502 programming myself and I really enjoy these questions and the answers and discussions that follow! So, thanks!

HappyHacking6502! 
Cheers,
Hendrik-Jan

Hans Otten

unread,
Sep 13, 2022, 3:35:46 AM9/13/22
to PAL 6502 computer
bne loop depends on zero flag, inc does set that.

replace with unconditional jump

Hans Otten

unread,
Sep 13, 2022, 3:44:27 AM9/13/22
to PAL 6502 computer
(having trouble with internet, i ma traveling, sorry)
zero flag, as used in your bne loop, depends on previous instructions. it looks like you think it is still set from getkey,  but you have some other instructions before that that set/reset the flag.
 
the inc $fd increments zero page location $fd, which may contain any value. increment it and it probably will be no zero. every 256 time in your program loop it will be zero and the flag set , and failure occurs.

Do never assume values of flags, look up what instruction sets which flag. 

Hans Otten

unread,
Sep 13, 2022, 3:46:54 AM9/13/22
to PAL 6502 computer
the kim-1 simulator runs on a mac, you need to compile it from source. i do not have a mac, so cannot supply binaries.

GN Liu

unread,
Sep 13, 2022, 4:09:30 AM9/13/22
to PAL 6502 computer
Just like Hendrik-Jan said, we're very welcome to discuss 6502 programming in the group~

Happy Hacking 6502,
Liu

Jeff M. Nay

unread,
Sep 13, 2022, 6:57:59 AM9/13/22
to Kaveen R, PAL 6502 computer

If possible, I would like to see the actual machine language next to the assembly language, to make sure the correct code is being entered and where it is being entered.

 

0200

A9

LDA

00

0201

00

0202

85

STA

F9

0203

F9

0204

85

STA

FA

0205

FA

0206

85

STA

FB

0207

FB

0208

20

JSR

$1F1F

0209

1F

020A

1F

020B

20

JSR

$1F6A

020C

6A

020D

1F

020E

C9

CMP

#15

020F

15

 

This also makes it easier for me to load it, into my KIM-1 manually..

Kaveen R

unread,
Sep 13, 2022, 9:06:16 AM9/13/22
to PAL 6502 computer
Thanks all for the help! You folks have been super welcoming! 

Good to know there is a place to get some help on learning 6502 assembly! In summary, it was a toolchain issue,  I updated my Makefile to set the offset. 
Hans thanks for the adc/sbc examples! I've incorporated them successfully! re:Sim I will try setting up Lazarus and checking it out!

cheers,
Kaveen

Hans Otten

unread,
Sep 13, 2022, 9:07:03 AM9/13/22
to PAL 6502 computer
Current source and papertape looks much better now!

Jim McClanahan

unread,
Sep 13, 2022, 11:07:06 AM9/13/22
to PAL 6502 computer
A lot of people use CA65. I use it, but usually avoid it when I can. To me, a good tool chain will just do what you expect without having to put in manual trickery (or, even worse, my memory).

I had a grep command I'd used at one point that pulled the origin out of an assembly file and into a variable in the environment. I'm not finding it now, but I built it when I was testing code in RAM (at $2000) but ultimately wanted to build it for ROM (up above $A000). I found the idea in someone else's make file. These days I tend to just build shell scripts instead of messing with make anyway.

Actually it makes me think that some kind of tool that could look at a punch file and tell you something like "107 bytes load to $0200". Some of my punch files also add things like a line that sets the processor flags memory location so you don't have to remember to do that before you run some of the older code that doesn't have a CLD at the start.

I came across the code for the Ohio Scientific resident editor/assembler a few weeks back. Getting that running is on my to do list--the best of both worlds: assembly language AND line numbers. :-)

Thanks,
Jim W4JBM


Jim McClanahan

unread,
Sep 14, 2022, 12:58:58 PM9/14/22
to PAL 6502 computer
I made a few quick changes to the code. It made sense to move the "shift left across three bytes" logic into a subroutine that gets called four times instead of repeating the code four times. I thought about putting a loop around it, but that didn't save much and would mean I would have to use the stack or one more byte of scratchpad memory.

Also, I noticed that if you pressed a number, the way things worked the display would go dark until you released the button and then show the updated value. Now the "wait loop" watching for you to release the button you just pressed also updates the display as it loops.

That was the end of Rev 2, but then I started thinking about how to implement a repeat function for a key you hold down. It turned out that a one byte counter does the trick and give you about a 1 second delay between the number repeating. It feels about the right speed to me--not so fast that I'm rushed pressing a key, but not so long that I get impatient.

The code is here:


I've been messing around with the Julia programming language lately, so spending some time in 6502 assembler was a nice distraction. :-)

Thanks,
Jim W4JBM

Message has been deleted

Ryan Roth

unread,
Sep 14, 2022, 1:04:14 PM9/14/22
to PAL 6502 computer

Hans Otten

unread,
Sep 19, 2022, 3:29:31 AM9/19/22
to PAL 6502 computer
Thansk for the Mac compiled version! Will add this to the downloads!

Hans

Hans Otten

unread,
Sep 19, 2022, 7:06:29 AM9/19/22
to PAL 6502 computer
And added to the downloads!

Ryan Roth

unread,
Sep 19, 2022, 10:48:14 AM9/19/22
to PAL 6502 computer
Great, thanks!
Reply all
Reply to author
Forward
0 new messages