Reimplementing 6502 syntax in ca65

Discuss technical or other issues relating to programming the Nintendo Entertainment System, Famicom, or compatible systems.

Moderator: Moderators

tepples
Posts: 21750
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Reimplementing 6502 syntax in ca65

Post by tepples » Mon Nov 25, 2013 7:02 am

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.

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: Reimplementing 6502 syntax in ca65

Post by Movax12 » Mon Nov 25, 2013 11:25 am

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.
Attachments
ca65none.s.txt
(11.93 KiB) Downloaded 216 times

User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: Reimplementing 6502 syntax in ca65

Post by thefox » Mon Nov 25, 2013 1:40 pm

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
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: Reimplementing 6502 syntax in ca65

Post by Movax12 » Mon Nov 25, 2013 11:40 pm

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

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: Reimplementing 6502 syntax in ca65

Post by blargg » Tue Nov 26, 2013 12:26 am

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.

lidnariq
Posts: 8780
Joined: Sun Apr 13, 2008 11:12 am
Location: Seattle

Re: Reimplementing 6502 syntax in ca65

Post by lidnariq » Tue Nov 26, 2013 12:33 am

I never metacircular evaluator I didn't like!

User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: Reimplementing 6502 syntax in ca65

Post by thefox » Tue Nov 26, 2013 1:31 am

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.
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi

tepples
Posts: 21750
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

spcc65

Post by tepples » Tue Nov 26, 2013 8:01 am

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.

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: Reimplementing 6502 syntax in ca65

Post by blargg » Tue Nov 26, 2013 12:10 pm

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.

User avatar
thefox
Posts: 3141
Joined: Mon Jan 03, 2005 10:36 am
Location: Tampere, Finland
Contact:

Re: spcc65

Post by thefox » Tue Nov 26, 2013 6:26 pm

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).
Download STREEMERZ for NES from fauxgame.com! — Some other stuff I've done: fo.aspekt.fi

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: spcc65

Post by Movax12 » Tue Nov 26, 2013 6:43 pm

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?

User avatar
blargg
Posts: 3715
Joined: Mon Sep 27, 2004 8:33 am
Location: Central Texas, USA
Contact:

Re: Reimplementing 6502 syntax in ca65

Post by blargg » Tue Nov 26, 2013 6:50 pm

Write a language parser for X in preferred language, output as ca65 macros, then program in X using ca65 and these macros.

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: Reimplementing 6502 syntax in ca65

Post by Movax12 » Tue Nov 26, 2013 7:59 pm

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.

tepples
Posts: 21750
Joined: Sun Sep 19, 2004 11:12 pm
Location: NE Indiana, USA (NTSC)
Contact:

Re: Reimplementing 6502 syntax in ca65

Post by tepples » Tue Nov 26, 2013 8:09 pm

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...

User avatar
Movax12
Posts: 522
Joined: Sun Jan 02, 2011 11:50 am

Re: Reimplementing 6502 syntax in ca65

Post by Movax12 » Tue Nov 26, 2013 8:17 pm

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.

Post Reply