6502.org Forum  Projects  Code  Documents  Tools  Forum
It is currently Sat Sep 21, 2024 2:44 am

All times are UTC




Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next
Author Message
 Post subject: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 3:56 am 
Offline
User avatar

Joined: Mon May 12, 2014 6:18 pm
Posts: 365
I'm using the CA65 assembler that is part of CC65. It seems .org doesn't quite work as I would expect inside macros. Here is an example:

Code:
.macro var name,address
   name=address
   .org address
   .res 1 ;reserve one byte
.endmacro

.org $2000
var foo,$3000
var bar,$3001


Generating a listing file shows that while a byte is reserved at $3001 for bar, the byte for foo is reserved at $2000 instead of $3000. It seems .org inside of a macro doesn't take effect until the end of the macro. I understand .segment is usually used instead of .org, but I am only using the assembler to generate a listing file I need for my program and skipping linking altogether. I do need to put data at exact addresses for memory mapped IO.

Is this normal behavior? Is there another way to do this?


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 4:34 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8510
Location: Southern California
I'm not familiar with CA65, but it does look like the origin command doesn't work inside a macro. I had a problem with my assembler (C32) not carrying out the include command inside a macro, and had to do a workaround when I did my program structure macros.

I/O locations are not normally done as variables, but constants. The following is from one-third of the way down section 18 of my 6502 primer, the page titled, "Program-Writing: Where Do I Start?"

Quote:
Code:
;                           +----------------------+
;                           |   General Equates    |
;                           +----------------------+

ACIA:       EQU  $9000    ; The base address of the 6551 Asynchronous Communications Interface Adapter is $9000.
ACIA_DATA:  EQU  ACIA+0   ; Its data I/O register is at $9000.
ACIA_STAT:  EQU  ACIA+1   ; Its  status  register is at $9001.
ACIA_COMM:  EQU  ACIA+2   ; Its command  register is at $9002.
ACIA_CTRL:  EQU  ACIA+3   ; Its control  register is at $9003.

VIA:        EQU  $A000    ; The base address of the 6522 Versatile Interface Adapter is $A000.
PB:         EQU  VIA      ; Its port B is at that address.
PA:         EQU  VIA+1    ; Its port A is at address $A001.
DDRB:       EQU  VIA+2    ; Its data-direction register for port B is at $A002.
DDRA:       EQU  VIA+3    ; Its data-direction register for port A is at $A003.
T2CL:       EQU  VIA+8    ; Its timer-2 counter's low  byte is at $A008.
T2CH:       EQU  VIA+9    ; Its timer-2 counter's high byte is at $A009.
SR:         EQU  VIA+10   ; The shift register is at $A00A.
ACR:        EQU  VIA+11   ; The auxiliary  control register is at $A00B.
PCR:        EQU  VIA+12   ; The peripheral control register is at $A00C.
IFR:        EQU  VIA+13   ; The interrupt  flag  register is at $A00D.
IER:        EQU  VIA+14   ; The interrupt enable register is at $A00E.

I made it extra wordy for this tutorial, just to explain what may not be obvious yet. Note that even the sign at the top is preceded by semicolons, so the assembler will ignore it and not give you error messages saying it can't understand it. (Wow, I sure prefer the old DOS/ANSI [Edit: that should say IBM437] characters. They let you draw smooth lines, boxes, diagrams, and charts.)

Note that I did not assign anything for addresses $A004 through $A007, which was because I didn't use those registers in the project I copied this from.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 4:41 am 
Offline
User avatar

Joined: Mon May 12, 2014 6:18 pm
Posts: 365
Quote:
I/O locations are not normally done as variables, but constants.

Right. Until now I have just been using symbols like "name=address" in the macro (like the EQUs you showed) but it would be convenient in the program reading the listing if I could have a byte for the peripheral specifically reserved in the listing file.


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 4:46 am 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8510
Location: Southern California
I'm not sure I understand your point. Reserving is for when you want some amount of space, in sequence, and you don't care what its exact location is-- you're just grabbing the next x number of available bytes. In the case of the I/O addresses, they cannot go in sequence if you omit registers you're not going to use, and you do care what the exact locations are, because they're set in hardware, and you can't afford to have the assembler or linker moving them around.

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 2:29 pm 
Offline

Joined: Sun Nov 08, 2009 1:56 am
Posts: 395
Location: Minnesota
Dunno the specific answer you're looking for - and suspect it wouldn't help you anyway, as there is probably a better way to get where you ultimately want to go than this - but one way to understand what's happening is to play around and see what happens. In your case I'd first try eliminating the ".org $2000" line to see what happens.

I'd also try re-arranging the macro to look like this:

Code:
.macro var name,address
   .org address
   name=*
   .res 1 ;reserve one byte
.endmacro


or, now that I've looked at that, like this:

Code:
.macro var name,address
   .org address
name .res 1
.endmacro


...assuming ca65 can accept a label on a directive (a quick glance at the documentation does not indicate that it - or any directive, for that matter - can, but I can't see any reason why it wouldn't).


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 4:31 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8390
Location: Midwestern USA
I may not be fully understanding the nature of the problem (I don't use the CA65 assembler) but why can't your macro just execute code that amounts to the *=*+<size> statement used to reserve space?

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 5:11 pm 
Offline
User avatar

Joined: Mon May 12, 2014 6:18 pm
Posts: 365
So the problem is that I need to mark a byte as reserved so it will show up in the listing file since that is what my simulator program uses. I know this is not what the listing file is intended for but it is a lot easier to parse the listing than to reassemble which labels and symbols match which binary data from a linked binary and debug info. I emailed the author but didn't expect a response since he has stopped maintaining it. He explained that the address in the listing is the address before the command is evaluated. The macro changes the address with .org but it is already too late at that point.

It doesn't look like there is any way to do what I'm trying to do, at least with this assembler.


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 6:04 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8510
Location: Southern California
Quote:
the problem is that I need to mark a byte as reserved so it will show up in the listing file since that is what my simulator program uses. I know this is not what the listing file is intended for

All three assemblers I've used do put equates in the list (.lst) file, both at the line where the equate is made, and at the end where the addresses of all labels (including equates, variables, routines, etc.) are listed in alphabetical order. Does CA65 not do this?

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 6:25 pm 
Offline
User avatar

Joined: Mon May 12, 2014 6:18 pm
Posts: 365
Quote:
All three assemblers I've used do put equates in the list (.lst) file

What file do you mean exactly? Is it a debug file? I'm just using the -l option:
Code:
000000  1  xx             var R0
000001  1  xx             var R1
000002  1  xx             var R2
000003  1  xx             var R3
...
008000  1  A2 FF            LDX #$FF
008002  1  9A               TXS
008003  1  A9 FF            LDA #$FF
008005  1  85 23            STA PS_PTR
008007  1  A9 01            LDA #SWITCH_STACK
008009  1  8D 06 30         STA SWITCH


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 7:03 pm 
Offline
User avatar

Joined: Fri Aug 30, 2002 1:09 am
Posts: 8510
Location: Southern California
The parts of the .lst file (which is produced automatically from two of the assemblers, but with the -L option in the other IIRC) from the example I gave above are shown here in two parts: first the in-lined showing of EQUate assignments (which is rather early in the file), then the list of addresses at the very end of the .lst file showing all the labels, alphabetized. The whole project was very small and the list file is only 263 lines, so the list of labels at the end is short enough that it will be easy to pick out the I/O addresses. (Pardon my not taking the time to fix the tabs that got corrupted in the transfer.)
Code:
;                           +----------------------+
;                           |   General Equates    |
;                           +----------------------+

                               
 00009000         =             ACIA:      EQU   $9000
 00009000         =             ACIA_DATA:   EQU   ACIA+0
 00009001         =             ACIA_STAT:   EQU   ACIA+1
 00009002         =             ACIA_COMM:   EQU   ACIA+2
 00009003         =             ACIA_CTRL:   EQU   ACIA+3
                               
 0000A000         =             VIA:      EQU   $A000
 0000A000         =             PB:      EQU   VIA
 0000A001         =             PA:      EQU   VIA+1
 0000A002         =             DDRB:      EQU   VIA+2
 0000A003         =             DDRA:      EQU   VIA+3
 0000A008         =             T2CL:      EQU   VIA+8
 0000A009         =             T2CH:      EQU   VIA+9
 0000A00A         =             SR:      EQU   VIA+10
 0000A00B         =             ACR:      EQU   VIA+11
 0000A00C         =             PCR:      EQU   VIA+12
 0000A00D         =             IFR:      EQU   VIA+13
 0000A00E         =             IER:      EQU   VIA+14
                               
 00000200         =             BUF_RING:   EQU   $200   ; The receive/transmit buffer will be a ring of 100H bytes starting
                                            ; at address 200H.


Code:
00009000  ACIA               00009002  ACIA_COMM          00009003  ACIA_CTRL         
00009000  ACIA_DATA          00009001  ACIA_STAT          0000A00B  ACR               
0000F067  BUF_LEVELSTAT      00000200  BUF_RING           0000F076  CTS_FALSE         
0000F06D  CTS_TRUE           0000A003  DDRA               0000A002  DDRB               
0000A00E  IER                0000A00D  IFR                0000F0A1  IRQ               
0000F08A  LOOP               0000F07F  MOR_2_SEND?        0000A001  PA                 
0000A000  PB                 0000A00C  PCR                0000F045  RCV_BYT           
0000F084  RST                0000F094  RX_EMPTY           0000F058  SB1               
0000F066  SB2                0000F04D  SEND_BYT           0000F000  SETUP             
0000A00A  SR                 0000F004  SU1                0000F002  SU2               
0000A009  T2CH               0000A008  T2CL               00000000  TEMP_Y             
00000001  TX_BEGUN?          0000FFFA  VECTORS            0000A000  VIA

_________________
http://WilsonMinesCo.com/ lots of 6502 resources
The "second front page" is http://wilsonminesco.com/links.html .
What's an additional VIA among friends, anyhow?


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 7:33 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10938
Location: England
I can't see any way to get ca65 or cl65 to output a symbol table. There's an option to output a map file, which seemed promising, and even a .export directive, but I haven't found a way to get the symbol values into the map file.

Ed


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 7:40 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10938
Location: England
Ah, found something:
Code:
cl65 --target none -Ln source.lbl source.a65


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 7:40 pm 
Offline
User avatar

Joined: Mon May 12, 2014 6:18 pm
Posts: 365
Thanks for your help guys. I'm almost done working on this and then I will post the whole project to see what suggestions you guys have. Maybe after that I will rewrite the program to use a map file or similar debugging info instead of the program listing.


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 8:05 pm 
Offline
User avatar

Joined: Thu May 28, 2009 9:46 pm
Posts: 8390
Location: Midwestern USA
BigEd wrote:
I can't see any way to get ca65 or cl65 to output a symbol table. There's an option to output a map file, which seemed promising, and even a .export directive, but I haven't found a way to get the symbol values into the map file.

Ed

This seems to be a common omission. Many of the 65xx assemblers that I have examined fail to produce a symbol table report, which is handy thing to have when developing a really large program.

_________________
x86?  We ain't got no x86.  We don't NEED no stinking x86!


Top
 Profile  
Reply with quote  
 Post subject: Re: CA65: .org in .macro
PostPosted: Thu Aug 14, 2014 8:14 pm 
Offline
User avatar

Joined: Thu Dec 11, 2008 1:28 pm
Posts: 10938
Location: England
But note my subsequent post!


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 28 posts ]  Go to page 1, 2  Next

All times are UTC


Who is online

Users browsing this forum: No registered users and 25 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: