Megalextoria
Retro computing and gaming, sci-fi books, tv and movies and other geeky stuff.

Home » Digital Archaeology » Computer Arcana » Commodore » Commodore 8-bit » some ML questions 65C02 to 6502 conversion.
Show: Today's Messages :: Show Polls :: Message Navigator
E-mail to friend 
Switch to threaded view of this topic Create a new topic Submit Reply
some ML questions 65C02 to 6502 conversion. [message #105441] Mon, 07 March 2005 23:45 Go to next message
David Murray is currently offline  David Murray
Messages: 1017
Registered: January 2005
Karma: 0
Senior Member
Can anybody explain to me exactly what the BIT instruction does? THe
description is "test memory with accumulator" but that means about
nothing to me.. I've read about 40 websites on 6502 instruction and
haven't found a better description. Is it like a compare? please
elaborate.

Also, I'm trying to translate some code from 65C02 instruction to work on
the C64's 6502-opcodes. One of the codes is like this:

JMP (ABC, X)

Where ABC is an array of words that have vectors of different
subroutines. Problem is, I'm having great difficulty figuring out a way
to duplicate this with 6502 instructions without re-writing the whole
dang code. It seemed easy at first, but the more I looked at it, I
realized it wasn't.

And one last question.. what does BRA do? It stands for Branch Always..
I suspect it is like a JMP command, only rather than using an address to
jump to, it uses a number of bytes forward or backward.. Probably good
for making code portable. if I'm wrong, somebody tell me.. Can I just
substitute JMP in place of BRA?

Here is an example of the main issue with the JMP command:

tax ; save back to x
lda byte ; load scancode back into A
jmp (kbccmd,x) ; execute scancode routine, return

kbccmd .word kbtrap83 ;
.word kbtcaps ;
.word kbsshift ;
.word kbsshift ;
.word kbsctrl ;
.word kbtnum ;
.word kbBrk ;

<<<<<< SNIP >>>>>>>

Here is what I tried to substitude:

JMPBYTE1 = $02D5
JMPBYTE2 = $02D6
tax ; save back to x
LDA kbccmd,x
STA $02D5
INX
LDA kbccmd,x
STA $02D6
lda byte ; load scancode back into A
jmp (JMPBYTE1)

So.. either that didn't work, or there are other issues.

Any help?
Re: some ML questions 65C02 to 6502 conversion. [message #105442 is a reply to message #105441] Tue, 08 March 2005 00:05 Go to previous messageGo to next message
Jim Brain is currently offline  Jim Brain
Messages: 962
Registered: August 2012
Karma: 0
Senior Member
David Murray wrote:
> Can anybody explain to me exactly what the BIT instruction does?

From 6502.org:

BIT sets the Z flag as though the value in the address tested were ANDed
with the accumulator. The S and V flags are set to match bits 7 and 6
respectively in the value stored at the tested address.
BIT is often used to skip one or two following bytes as in:


CLOSE1 LDX #$10 If entered here, we
.BYTE $2C effectively perform
CLOSE2 LDX #$20 a BIT test on $20A2,
.BYTE $2C another one on $30A2,
CLOSE3 LDX #$30 and end up with the X
CLOSEX LDA #12 register still at $10
STA ICCOM,X upon arrival here.

> Also, I'm trying to translate some code from 65C02 instruction to work on

> the C64's 6502-opcodes. One of the codes is like this:

>

> JMP (ABC, X)

>

> Where ABC is an array of words that have vectors of different

> subroutines. Problem is, I'm having great difficulty figuring out a way

> to duplicate this with 6502 instructions without re-writing the whole

> dang code. It seemed easy at first, but the more I looked at it, I

> realized it wasn't.


Easiest is probably to use self mod code:

lda (ABC,X)
sta ptr
lda (ABC+1,X)
sta ptr+1
jmp (ptr)

> And one last question.. what does BRA do? It stands for Branch Always..

> I suspect it is like a JMP command, only rather than using an address to

> jump to, it uses a number of bytes forward or backward.. Probably good

> for making code portable. if I'm wrong, somebody tell me.. Can I just

> substitute JMP in place of BRA?

You can, but note that bra is 2 bytes, jmp is 3.

Jim


--
Jim Brain, Brain Innovations
brain@jbrain.com http://www.jbrain.com
Dabbling in WWW, Embedded Systems, Old CBM computers, and Good Times!
Re: some ML questions 65C02 to 6502 conversion. [message #105469 is a reply to message #105441] Tue, 08 March 2005 00:05 Go to previous messageGo to next message
dfevans is currently offline  dfevans
Messages: 50
Registered: July 2003
Karma: 0
Member
In article <Xns9612E740AC8F4neverspamnospamcom@151.164.30.42>,
David Murray <spamsucks@stopspam.com> wrote:
> Also, I'm trying to translate some code from 65C02 instruction to work on

> the C64's 6502-opcodes. One of the codes is like this:

>

> JMP (ABC, X)

>

> Where ABC is an array of words that have vectors of different

> subroutines. Problem is, I'm having great difficulty figuring out a way

> to duplicate this with 6502 instructions without re-writing the whole

> dang code. It seemed easy at first, but the more I looked at it, I

> realized it wasn't.

>


How "clever" do you want to be? Two straightforward approaches would
be self-modifying code or futzing the stack followed by an RTS.

> And one last question.. what does BRA do? It stands for Branch Always..

> I suspect it is like a JMP command, only rather than using an address to

> jump to, it uses a number of bytes forward or backward..


Yep. BRA is just a jump that uses relative addressing.

--
David Evans
Research Assistant Professor
School of Computer Science dfevans@bbcr.uwaterloo.ca
University of Waterloo, Canada http://bbcr.uwaterloo.ca/~dfevans/
Re: some ML questions 65C02 to 6502 conversion. [message #105523 is a reply to message #105442] Tue, 08 March 2005 12:51 Go to previous messageGo to next message
schepers is currently offline  schepers
Messages: 249
Registered: March 2005
Karma: 0
Senior Member
In article <poaXd.44934$r55.12436@attbi_s52>,
Jim Brain <brain@jbrain.com> wrote:
> David Murray wrote:

>

>> And one last question.. what does BRA do? It stands for Branch Always..

>> I suspect it is like a JMP command, only rather than using an address to

>> jump to, it uses a number of bytes forward or backward.. Probably good

>> for making code portable. if I'm wrong, somebody tell me.. Can I just

>> substitute JMP in place of BRA?

>

> You can, but note that bra is 2 bytes, jmp is 3.


True, but since he is already adding to the code, a close substitution
wouldn't hurt. Try:

CLC
BCC address

for an unconditional branch like BRA. This keeps the code relative in case
mobility in memory is important.

It's been years since I've actually done any assembly coding. I remember
doing all the things that David is looking at... having a JMP table to
subroutines and I would load the address needed into a pointer loc and JMP
($xxxx) to it... using BIT to mask out opcodes, etc. Ahh, the good old
days!

------------------------------------------------------------ ---------------
Peter Schepers, | Author of : 64COPY, The C64 EMU file converter
Info Systems & Technology | http://ist.uwaterloo.ca/~schepers/personal.html
University of Waterloo, | My opinion is not likely that of the
Waterloo, Ontario, Canada | University, its employees, or anybody
(519) 888-4567 ext 2456 | on this planet. Too bad!
Re: some ML questions 65C02 to 6502 conversion. [message #105525 is a reply to message #105523] Tue, 08 March 2005 13:08 Go to previous messageGo to next message
Anders Carlsson is currently offline  Anders Carlsson
Messages: 776
Registered: July 2003
Karma: 0
Senior Member
schepers@ist.uwaterloo.ca (Peter Schepers) writes:

> CLC

> BCC address


Considering how many instructions alter the carry flag, the overflow
flag may be a better subject of hi-jacking: CLV - BVC address ?

--
Anders Carlsson
Re: some ML questions 65C02 to 6502 conversion. [message #105543 is a reply to message #105525] Wed, 09 March 2005 08:18 Go to previous messageGo to next message
schepers is currently offline  schepers
Messages: 249
Registered: March 2005
Karma: 0
Senior Member
In article <k2gu0nmovrd.fsf@legolas.mdh.se>,
Anders Carlsson <anders.carlsson@mds.mdh.se> wrote:
> schepers@ist.uwaterloo.ca (Peter Schepers) writes:

>

>> CLC

>> BCC address

>

> Considering how many instructions alter the carry flag, the overflow

> flag may be a better subject of hi-jacking: CLV - BVC address ?


Of course, the selection of what BRANCH type to select is up to the user.
It was only a simple example of unconditional branching.

PS.
Re: some ML questions 65C02 to 6502 conversion. [message #105587 is a reply to message #105442] Thu, 10 March 2005 00:16 Go to previous messageGo to next message
Anton Treuenfels is currently offline  Anton Treuenfels
Messages: 105
Registered: December 2011
Karma: 0
Senior Member
"Jim Brain" <brain@jbrain.com> wrote in message
news:poaXd.44934$r55.12436@attbi_s52...

> Easiest is probably to use self mod code:

>

> lda (ABC,X)

> sta ptr

> lda (ABC+1,X)

> sta ptr+1

> jmp (ptr)

>


Self-modifying code is, of course a Sin Against Nature :)

But is this code actually self-modifying? Only if 'ptr' is defined somewhere
as being equal to the address of the JMP instruction plus one, which I don't
see anywhere and isn't necessary anyway - 'ptr' can be any location
(preferrably one outside the program code, though).

'Ptr' would have to be equal to the address of the JMP instruction plus one
only if the JMP instruction was using direct addressing. But as written,
it's using indirect addressing.

But there's still a problem. The 6502 does not have an (Absolute Indirect,X)
addressing mode for any instruction - the 65C02 does, but only for the JMP
instruction.

The 6502 does have a (Zero Page Indirect,X) addressing mode which works with
LDA. If we assume that's what being used here, there are apparently two
overlapping pointers in zero page - (ABC,X) accesses a 16-bit pointer at
ABC+X/ABC+X+1, and (ABC+1,X) accesses one at ABC+X+1/ABC+X+2. Awfully tricky
stuff!

To make the overlapped pointers access two consecutive locations in main
memory you'd have to have previously stored something like:

$01 $02 $02

starting ABC+X, so you'd get $0201 and $0202 as your two 16-bit pointers.

Maybe Absolute,X addressing would be simpler? :)

Anton Treuenfels
Re: some ML questions 65C02 to 6502 conversion. [message #105588 is a reply to message #105587] Thu, 10 March 2005 01:50 Go to previous messageGo to next message
Jim Brain is currently offline  Jim Brain
Messages: 962
Registered: August 2012
Karma: 0
Senior Member
Anton Treuenfels wrote:
> But is this code actually self-modifying? Only if 'ptr' is defined somewhere


It was, but I rewrote it before posting, and then it was not self
modifying, but I forgot to retitle it.

> But there's still a problem. The 6502 does not have an (Absolute Indirect,X)

> addressing mode for any instruction - the 65C02 does, but only for the JMP

> instruction.

I was assuming ABC could be in zpage.


> The 6502 does have a (Zero Page Indirect,X) addressing mode which works with

> LDA. If we assume that's what being used here, there are apparently two

> overlapping pointers in zero page - (ABC,X) accesses a 16-bit pointer at

> ABC+X/ABC+X+1, and (ABC+1,X) accesses one at ABC+X+1/ABC+X+2. Awfully tricky

> stuff!


My understanding is that JMP(ABC,X) gets the address at ABC, adds X, and
then jumps to the addressat that location. I think this instruction is
assumed that X increments by 2.

You are correct that the utility is somewhat in question.

Jim




--
Jim Brain, Brain Innovations
brain@jbrain.com http://www.jbrain.com
Dabbling in WWW, Embedded Systems, Old CBM computers, and Good Times!
Re: some ML questions 65C02 to 6502 conversion. [message #105595 is a reply to message #105588] Thu, 10 March 2005 08:13 Go to previous messageGo to next message
schepers is currently offline  schepers
Messages: 249
Registered: March 2005
Karma: 0
Senior Member
In article <g7SXd.112034$4q6.95227@attbi_s01>,
Jim Brain <brain@jbrain.com> wrote:
> Anton Treuenfels wrote:

>> But is this code actually self-modifying? Only if 'ptr' is defined somewhere

>

> It was, but I rewrote it before posting, and then it was not self

> modifying, but I forgot to retitle it.

>

>> But there's still a problem. The 6502 does not have an (Absolute Indirect,X)

>> addressing mode for any instruction - the 65C02 does, but only for the JMP

>> instruction.

> I was assuming ABC could be in zpage.

>

>

>> The 6502 does have a (Zero Page Indirect,X) addressing mode which works with

>> LDA. If we assume that's what being used here, there are apparently two

>> overlapping pointers in zero page - (ABC,X) accesses a 16-bit pointer at

>> ABC+X/ABC+X+1, and (ABC+1,X) accesses one at ABC+X+1/ABC+X+2. Awfully tricky

>> stuff!

>

> My understanding is that JMP(ABC,X) gets the address at ABC, adds X, and

> then jumps to the addressat that location. I think this instruction is

> assumed that X increments by 2.


Actually that sounds backwards. What you seem to be saying in C would be
(*abc)+x, but it actually appears to be *(abc+x). This implies a JMP
vector table who's reference label address is ABC, indexed by X.

According to one web page describing 65C02 opcodes:

http://coinop.org/kb_dl.aspx/kb/cpu_reference/6502/6502ref.h tml

"Absolute Indexed Indirect Addressing [(Absolute,X)] (Jump Instruction
Only) With absolute indexed indirect addressing the contents of the second
and third instruction bytes are added to the X register. The result of
this addition, points to a memory location containing the low-order eight
bits of the effective address. The next memory location contains the
higher-order eight bits of the effective address."

So it would appear to take the ABC value from the opcode, add X, and it
now has the pointer address to get the JMP value from.

------------------------------------------------------------ ---------------
Peter Schepers, | Author of : 64COPY, The C64 EMU file converter
Info Systems & Technology | http://ist.uwaterloo.ca/~schepers/personal.html
University of Waterloo, | My opinion is not likely that of the
Waterloo, Ontario, Canada | University, its employees, or anybody
(519) 888-4567 ext 2456 | on this planet. Too bad!
Re: some ML questions 65C02 to 6502 conversion. [message #105627 is a reply to message #105595] Thu, 10 March 2005 09:42 Go to previous message
Jim Brain is currently offline  Jim Brain
Messages: 962
Registered: August 2012
Karma: 0
Senior Member
Peter Schepers wrote:
>> My understanding is that JMP(ABC,X) gets the address at ABC, adds X, and

>> then jumps to the addressat that location. I think this instruction is

>> assumed that X increments by 2.

>

>

> Actually that sounds backwards. What you seem to be saying in C would be

> (*abc)+x, but it actually appears to be *(abc+x). This implies a JMP

> vector table who's reference label address is ABC, indexed by X.


My bad. Should not reply late at night. It is *(abc+x). Still,
though, my note is correct JMP(ABC,X) assumes that ABC is 2 byte
aligned, and thus X needs to be aligned as well.

Jim



--
Jim Brain, Brain Innovations
brain@jbrain.com http://www.jbrain.com
Dabbling in WWW, Embedded Systems, Old CBM computers, and Good Times!
  Switch to threaded view of this topic Create a new topic Submit Reply
Previous Topic: Little PET needs EPROM doctor
Next Topic: LCD....
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ] [ PDF ]

Current Time: Fri Apr 19 13:22:19 EDT 2024

Total time taken to generate the page: 0.19605 seconds