Page 1 of 1

ACME memory allocation macro and oversized addressing

Posted: Wed Apr 27, 2022 9:13 pm
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?

Re: ACME memory allocation macro and oversized addressing

Posted: Wed Apr 27, 2022 9:19 pm
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.

Re: ACME memory allocation macro and oversized addressing

Posted: Wed Apr 27, 2022 9:40 pm
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.

Re: ACME memory allocation macro and oversized addressing

Posted: Wed Apr 27, 2022 11:01 pm
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!

Re: ACME memory allocation macro and oversized addressing

Posted: Wed Apr 27, 2022 11:06 pm
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"
    }
}

Re: ACME memory allocation macro and oversized addressing

Posted: Sat May 07, 2022 6:41 pm
by fachat
Does acme not support segments? Incl a zeropage segment that is automatically located in zeropage....

Re: ACME memory allocation macro and oversized addressing

Posted: Sat May 07, 2022 7:01 pm
by akohlbecker
Do you mean the *= directive? How would you use it for this?

Re: ACME memory allocation macro and oversized addressing

Posted: Sun May 08, 2022 7:24 am
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.

Re: ACME memory allocation macro and oversized addressing

Posted: Sun May 08, 2022 7:54 pm
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

Re: ACME memory allocation macro and oversized addressing

Posted: Sun May 08, 2022 7:58 pm
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