ACME memory allocation macro and oversized addressing

Programming the 6502 microprocessor and its relatives in assembly and other languages.
Post Reply
User avatar
akohlbecker
Posts: 282
Joined: 24 Jul 2021
Contact:

ACME memory allocation macro and oversized addressing

Post by akohlbecker »

Hello all,

I've been manually adding zero page memory locations to my kernel, with a list at the beginning of my source code. However, as I'm splitting things up into more and more library files, it would be nice to be able to group memory allocations with the code relevant to them. So I've created these macros, in ACME:

Code: Select all

!set zero_page_pointer = $0             ; pointer to next available zero page address
!set gp_memory_pointer = $0200          ; pointer to next available general purpose memory address

; alloc_zp: allocate a given amount of bytes on the zero page
;
; use: +alloc_zp foo, 2 ; sets label of foo to the first available zero page address, and reserve 2 bytes
!macro alloc_zp ~.address, .amount {
    !address .address = zero_page_pointer
    !set zero_page_pointer = zero_page_pointer + .amount
}

; alloc_mem: allocate a given amount of bytes in general purpose memory
;
; use: +alloc_mem foo, 2 ; sets label of foo to the first available memory address, and reserve 2 bytes
!macro alloc_mem ~.address, .amount {
    !address .address = gp_memory_pointer
    !set gp_memory_pointer = gp_memory_pointer + .amount
}
Then, the goal is to be able to use this code:

Code: Select all

+alloc_zp ~kb_rptr, 1

lda kb_rptr
Unforunately, while alloc_mem works perfectly, ACME is unable to infer zero-page addressing for the alloc_zp example above, and I get the following warning: "Using oversized addressing mode.". It appears that choosing the addressing mode happens before the macro has executed, and so ACME does not know that kb_rptr is a byte.

I've tried various incarnations of using the "<" operator to truncate the address (does nothing), and using the "+1" postfix (says it is "too late for postfix", pointing to an execution order issue). Changing my final "lda kb_rptr" into "lda+1 kb_rptr" works, but it does not look great and means I have to change all the code, so I would rather avoid that.

Has anyone experimented with something similar and got it to work?
Last edited by akohlbecker on Wed Apr 27, 2022 9:20 pm, edited 1 time in total.
User avatar
akohlbecker
Posts: 282
Joined: 24 Jul 2021
Contact:

Re: ACME memory allocation macro and oversized addressing

Post by akohlbecker »

I have found this similar idea here https://www.lemon64.com/forum/viewtopic ... 788#168788, but even using a variable and defining the label outside of the macro, I still get the same issue of ACME not using zero-page addressing and spitting out this warning.
User avatar
GARTHWILSON
Forum Moderator
Posts: 8774
Joined: 30 Aug 2002
Location: Southern California
Contact:

Re: ACME memory allocation macro and oversized addressing

Post by GARTHWILSON »

I'm not familiar with ACME; but can you AND it with $FF, to force it into a single byte? I've done that with the assemblers I use.
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?
User avatar
akohlbecker
Posts: 282
Joined: 24 Jul 2021
Contact:

Re: ACME memory allocation macro and oversized addressing

Post by akohlbecker »

It looks like that did the trick! Not sure why I didn't think of it as I use it in other places... Thanks, Garth!
User avatar
akohlbecker
Posts: 282
Joined: 24 Jul 2021
Contact:

Re: ACME memory allocation macro and oversized addressing

Post by akohlbecker »

Updated macros, with added overflow check

Code: Select all

!set zero_page_pointer = $0             ; pointer to next available zero page address
!set gp_memory_pointer = $0200          ; pointer to next available general purpose memory address

; alloc_zp: allocate a given amount of bytes on the zero page
;
; use: +alloc_zp ~foo, 2 ; sets label of foo to the first available zero page address, and reserve 2 bytes
!macro alloc_zp ~.address, .amount {
    !address .address = $ff & zero_page_pointer
    !set zero_page_pointer = zero_page_pointer + .amount
    !if zero_page_pointer > $ff {
      !error "out of zero page addresses"
    }
}

; alloc_mem: allocate a given amount of bytes in general purpose memory
;
; use: +alloc_mem ~foo, 2 ; sets label of foo to the first available memory address, and reserve 2 bytes
!macro alloc_mem ~.address, .amount {
    !address .address = gp_memory_pointer
    !set gp_memory_pointer = gp_memory_pointer + .amount
    !if gp_memory_pointer > $7f7f {
      !error "out of memory addresses"
    }
}
fachat
Posts: 1124
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Re: ACME memory allocation macro and oversized addressing

Post by fachat »

Does acme not support segments? Incl a zeropage segment that is automatically located in zeropage....
Author of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/
User avatar
akohlbecker
Posts: 282
Joined: 24 Jul 2021
Contact:

Re: ACME memory allocation macro and oversized addressing

Post by akohlbecker »

Do you mean the *= directive? How would you use it for this?
hmn
Posts: 21
Joined: 07 May 2017

Re: ACME memory allocation macro and oversized addressing

Post by hmn »

I don't think ACME has segment support.

Out of the assemblers I frequently use, 64tass (aka tass64) has "sections", and ca65 (from cc65) has segments, along with a linker.
fachat
Posts: 1124
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Re: ACME memory allocation macro and oversized addressing

Post by fachat »

xa65 has at least basic segment support.

You can use .zero .text .data and .bss to switch between these four segments. Each segment has its own PC. Start address is typically given by command line option, or by the relocating loader
Author of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/
fachat
Posts: 1124
Joined: 05 Jul 2005
Location: near Heidelberg, Germany
Contact:

Re: ACME memory allocation macro and oversized addressing

Post by fachat »

.zero is uninitialized zeropage memory
.text is program code
.data is memory initialized with some data
.bss is uninitialized memory

This way the loader can put each segment into the right area, eg text into executable but read only area, zero into zeropage etc

Only works with a file format that allows that, like o65
Author of the GeckOS multitasking operating system, the usb65 stack, designer of the Micro-PET and many more 6502 content: http://6502.org/users/andre/
Post Reply