Page 3 of 4
Re: Reimplementing 6502 syntax in ca65
Posted: Mon Nov 25, 2013 7:02 am
by tepples
A lot of the macros in this pack already make a new local label. So long as the "guessed address size" of an expression like
foo + 3 is reasonable (and constant enough to get used in .if), it should still work.
Code: Select all
.importzp foo
argvalue = foo + 3
.if .addrsize(argvalue) = NONE02_SIZE_ZEROPAGE
.out .sprintf("%d", .addrsize(argvalue))
.endif
As for implementing new ISAs directly in instr.c: There might be little resistance to implementing SPC700, but Z80 and 68000 might meet more. Working in macros also allows more experimentation with the syntax because no waiting for the assembler itself to compile and link.
Re: Reimplementing 6502 syntax in ca65
Posted: Mon Nov 25, 2013 11:25 am
by Movax12
Actually, you are correct, it does guess well enough, I think. I got things working with my build environment.
Updated source:
https://github.com/Movax12/cc65/tree/master/src/ca65
Note: .addrsize() will return a value from 1 to 4 for addressing size (number of bytes needed for the address). It will return 0 for unknown rather than error out. Makes things easier.
I edited the ca65none.s: All labels are now cheap locals. I used
.feature ubiquitous_idents due to the fact that ca65 complains about the use of a forced
:absolute in my code (says not valid for cpu type). Since you have defined all the instructions, it won't matter in this case. (main.c has the fix for ubiquitous_idents).
Please let me know if anything doesn't work.
Re: Reimplementing 6502 syntax in ca65
Posted: Mon Nov 25, 2013 1:40 pm
by thefox
tepples wrote:So long as the "guessed address size" of an expression like foo + 3 is reasonable (and constant enough to get used in .if), it should still work.
As I understand it, if "foo" is an imported zero page variable, ca65 will assume that "foo + 3" fits in zero page as well. If it doesn't, it will give a fatal "Range error" at link time. You may want to duplicate this behavior with an assert. (I assume Movax's .addrsize returns 1 for a symbol constructed from the expression "foo + 3".)
I.e.
Code: Select all
; foo.s
.importzp foo
lda foo + 3
; bar.s
.exportzp foo = 255
; > cl65 -t none foo.s bar.s
ld65.exe: Error: Range error in module `foo.s', line 3
Re: Reimplementing 6502 syntax in ca65
Posted: Mon Nov 25, 2013 11:40 pm
by Movax12
thefox wrote:..ca65 will assume that "foo + 3" fits in zero page as well. If it doesn't, it will give a fatal "Range error" at link time. You may want to duplicate this behavior with an assert.
I tested that scenario. There is no need for an assert, ca65 still complains with "Range error". I suppose since it is out of range for the
.byte statement. What needs to be added, however is an assert for when a forward reference is made:
Code: Select all
.if .addrsize(@argvalue) = 1
.byte $04 | (inst), @argvalue
.else
.byte $0C | (inst)
.word @argvalue
.assert @argvalue >= $100, warning, "zeropage addressing could have been used here"
.endif
Re: Reimplementing 6502 syntax in ca65
Posted: Tue Nov 26, 2013 12:26 am
by blargg
I love how attempts are being made to closely duplicate ca65's behavior with its macros. I guess next you'll need to reimplement its macro package with itself. ca65inception.s.
Re: Reimplementing 6502 syntax in ca65
Posted: Tue Nov 26, 2013 12:33 am
by lidnariq
I never metacircular evaluator I didn't like!
Re: Reimplementing 6502 syntax in ca65
Posted: Tue Nov 26, 2013 1:31 am
by thefox
blargg wrote:I love how attempts are being made to closely duplicate ca65's behavior with its macros. I guess next you'll need to reimplement its macro package with itself. ca65inception.s.
It would also be funny if somebody wrote a compiler that generates code as ca65 macros. Could even be quite useful, because the ca65 macro language isn't always that nice to use.
spcc65
Posted: Tue Nov 26, 2013 8:01 am
by tepples
thefox wrote:It would also be funny if somebody wrote a compiler that generates code as ca65 macros.
If it turns out that the SPC700 is as close to the 6502 as I think it is, the second iteration of this macro pack may turn out to be just that: something you can stick at the top of cc65-generated assembly files to turn cc65 into an SPC700 compiler.
Re: Reimplementing 6502 syntax in ca65
Posted: Tue Nov 26, 2013 12:10 pm
by blargg
Way back Anti-Resonance pointed me to another chip with SPC-700's same CPU core. I had to
re-find it again. It's the
GMS800 series. For example, the
GMS81C3004 (page 17 onward) should be familiar. Note how the absolute addressing mode has a ! prefix (pages 84-89). This might help as other references for the instruction syntax.
Re: spcc65
Posted: Tue Nov 26, 2013 6:26 pm
by thefox
tepples wrote:thefox wrote:It would also be funny if somebody wrote a compiler that generates code as ca65 macros.
If it turns out that the SPC700 is as close to the 6502 as I think it is, the second iteration of this macro pack may turn out to be just that: something you can stick at the top of cc65-generated assembly files to turn cc65 into an SPC700 compiler.
Not sure if you misunderstood me, or I'm misunderstanding you, but what I meant was a compiler that would take stuff in whatever language and output a program as ca65 macros (to be ran at compile time).
Re: spcc65
Posted: Tue Nov 26, 2013 6:43 pm
by Movax12
thefox wrote:take stuff in whatever language and output a program as ca65 macros (to be ran at compile time).
Macros ultimately output data or code. What would be be the benefit of using them as an intermediate step? Example?
Re: Reimplementing 6502 syntax in ca65
Posted: Tue Nov 26, 2013 6:50 pm
by blargg
Write a language parser for X in preferred language, output as ca65 macros, then program in X using ca65 and these macros.
Re: Reimplementing 6502 syntax in ca65
Posted: Tue Nov 26, 2013 7:59 pm
by Movax12
I think I get it. Sounds like a nice way to implement high level functions, but could be difficult, since macros can't properly loop, just simulate looping with recursion.
Re: Reimplementing 6502 syntax in ca65
Posted: Tue Nov 26, 2013 8:09 pm
by tepples
Movax12 wrote:macros can't properly loop, just simulate looping with recursion.
Is this recursion, or is it looping?
Code: Select all
; Language=Scheme
; Multiplies the factorial of x by acc.
(define (factorial* x acc)
(if (zero? x)
acc
(factorial* (- x 1) (* x acc)) ))
; Calculates the factorial of a nonnegative integer.
(define (factorial acc) (factorial* x 1))
If ca65 doesn't correctly optimize macro tail recursion, on the other hand...
Re: Reimplementing 6502 syntax in ca65
Posted: Tue Nov 26, 2013 8:17 pm
by Movax12
I don't know Scheme, but in that case I would guess it is actual looping with a variable stack that allows for recursive algorithms. Macros just keep expanding and executing from top to bottom.