Live data from Hacker News

Rust on the MOS 6502: Beyond Fibonacci

gergo.erdi.hu

11–20 of 40 posts

Re: Rust on the MOS 6502: Beyond Fibonacci

#11

That is so cool. I saw some posts about LLVM-MOS a while ago, but at that point I thought it would be just another in a fairly long list of attempts to try and get LLVM to output 6502 instructions. I never expected it to come together this well! Especially considering that the author of the article mentions there were so many issues with LLVM-AVR, you'd expect them to exist in LLVM-MOS as well. Apparently not! I gues…

I haven't looked at this closely, but 6502 really doesn't lend itself to C compilation. Three registers, only one of which works with the ALU, awkward immovable stack, etc. The 65816 is a better target (moveable direct page and stack and some wider registers), but also awkward with its register mode switching.

They actually address some of that on their project page, see: https://llvm-mos.org/wiki/Findings

Re: Rust on the MOS 6502: Beyond Fibonacci

#12

That is so cool. I saw some posts about LLVM-MOS a while ago, but at that point I thought it would be just another in a fairly long list of attempts to try and get LLVM to output 6502 instructions. I never expected it to come together this well! Especially considering that the author of the article mentions there were so many issues with LLVM-AVR, you'd expect them to exist in LLVM-MOS as well. Apparently not! I gues…

I haven't looked at this closely, but 6502 really doesn't lend itself to C compilation. Three registers, only one of which works with the ALU, awkward immovable stack, etc. The 65816 is a better target (moveable direct page and stack and some wider registers), but also awkward with its register mode switching.

I remember being in high school, reading K&R and trying to figure out how I could get a C compiler running on an Apple ][. Never did, but it was a useful intellectual enterprise.

My second (and last) assembly language after 6502 was 370 which replaces the "awkward immovable stack" of the 6502 with no hardware stack at all. Applications are completely responsible for maintaining their own call stack.

Re: Rust on the MOS 6502: Beyond Fibonacci

#13

Earlier quoted context omitted.

I haven't looked at this closely, but 6502 really doesn't lend itself to C compilation. Three registers, only one of which works with the ALU, awkward immovable stack, etc. The 65816 is a better target (moveable direct page and stack and some wider registers), but also awkward with its register mode switching.

From what I understand, LLVM-MOS treats large parts of the zero page as virtual ("imaginary") registers, so you have no shortage of that ( https://llvm-mos.org/wiki/Imaginary_registers ). Then, sufficiently advanced compiler technology improves the stack situation ( https://llvm-mos.org/wiki/C_calling_convention ).

6502 assembly has the distinct advantage of having special page-0 instructions for reading/writing from memory, including, if I recall correctly, the ability to take a 2-byte sequence and treat it as a 16-bit value (or was that in the AppleSoft ROM?)

Re: Rust on the MOS 6502: Beyond Fibonacci

#14
post #13

Earlier quoted context omitted.

From what I understand, LLVM-MOS treats large parts of the zero page as virtual ("imaginary") registers, so you have no shortage of that ( https://llvm-mos.org/wiki/Imaginary_registers ). Then, sufficiently advanced compiler technology improves the stack situation ( https://llvm-mos.org/wiki/C_calling_convention ).

6502 assembly has the distinct advantage of having special page-0 instructions for reading/writing from memory, including, if I recall correctly, the ability to take a 2-byte sequence and treat it as a 16-bit value (or was that in the AppleSoft ROM?)

You can treat 2 bytes (not just in the zero page, though) as indirect jump addresses, yes.

Doing something like "JMP ($2345)" will jump to whatever $2345/$2346 is pointing to.

Re: Rust on the MOS 6502: Beyond Fibonacci

#15
post #13

Earlier quoted context omitted.

6502 assembly has the distinct advantage of having special page-0 instructions for reading/writing from memory, including, if I recall correctly, the ability to take a 2-byte sequence and treat it as a 16-bit value (or was that in the AppleSoft ROM?)

You can treat 2 bytes (not just in the zero page, though) as indirect jump addresses, yes. Doing something like "JMP ($2345)" will jump to whatever $2345/$2346 is pointing to.

It's a little amazing how much 6502 assembler sticks with me 35 years later.

But only a little. I didn't have the money to buy an assembler or the skill to write one so I would write out my programs in long-hand on graph paper and hand-assemble them before entering hex codes manually. While not the most efficient process, it did do a good job of encoding things into long-term memory.

Re: Rust on the MOS 6502: Beyond Fibonacci

#16
post #8

Earlier quoted context omitted.

I haven't looked at this closely, but 6502 really doesn't lend itself to C compilation. Three registers, only one of which works with the ALU, awkward immovable stack, etc. The 65816 is a better target (moveable direct page and stack and some wider registers), but also awkward with its register mode switching.

Not only C, any language that thinks there’s other things than global state. If all your functions are void foo(void) and you don’t use local variables (or your language doesn’t support recursion, in which case all locals can be given a fixed address), targeting 6502 is fine (it also helps if you avoid floating point, use 8-bit variables where possible, etc) Not supporting recursion also means you can statically comp…

The cool thing about LLVM-MOS specifically it that by using the zero page as virtual registers you sort-of get the same output with 'regular' code as opposed to this 'global variables' style of programming.

I recall a tutorial for 'cc65 optimizations'[0] which basically destroys a well-structured C program in order to do all of these optimizations (like making everything global) and it was absolutely terrible, code-wise. Well, the end result was probably fine, but it's just a shame these 'optimizations' were needed.

[0] I think it was this one: https://github.com/ilmenit/CC65-Advanced-Optimizations

Re: Rust on the MOS 6502: Beyond Fibonacci

#17
Er... the article doesn't make it clear, but I guess we're talking about cross-compilation here? So it's not "Rust" (or, as he writes later, LLVM) running on the 6502, just the code generated by the Rust compiler.

Still cool though!

Re: Rust on the MOS 6502: Beyond Fibonacci

#18
post #15

Earlier quoted context omitted.

You can treat 2 bytes (not just in the zero page, though) as indirect jump addresses, yes. Doing something like "JMP ($2345)" will jump to whatever $2345/$2346 is pointing to.

It's a little amazing how much 6502 assembler sticks with me 35 years later. But only a little. I didn't have the money to buy an assembler or the skill to write one so I would write out my programs in long-hand on graph paper and hand-assemble them before entering hex codes manually. While not the most efficient process, it did do a good job of encoding things into long-term memory.

Haha, yes, I can relate. I didn't do any 6502 coding for ~25 years and it mostly just stuck around. Apparently it's like riding a bike.

In the meantime I've forgotten most of the 68000 and z80 instruction sets.

Re: Rust on the MOS 6502: Beyond Fibonacci

#19
post #17

Er... the article doesn't make it clear, but I guess we're talking about cross-compilation here? So it's not "Rust" (or, as he writes later, LLVM) running on the 6502, just the code generated by the Rust compiler. Still cool though!

Don’t most people generally mean the target binary from the compiler and not the compiler itself when someone says “see * running on this architecture”?

I can see for some dynamic languages there being a destination between the two, but for compiled binaries, generally Rust on X, it doesn’t seem important if rustc also runs on X (especially when discussing micro-controllers since one would rarely run a full compiler on the chip itself).

Re: Rust on the MOS 6502: Beyond Fibonacci

#20

That is so cool. I saw some posts about LLVM-MOS a while ago, but at that point I thought it would be just another in a fairly long list of attempts to try and get LLVM to output 6502 instructions. I never expected it to come together this well! Especially considering that the author of the article mentions there were so many issues with LLVM-AVR, you'd expect them to exist in LLVM-MOS as well. Apparently not! I gues…

I haven't looked at this closely, but 6502 really doesn't lend itself to C compilation. Three registers, only one of which works with the ALU, awkward immovable stack, etc. The 65816 is a better target (moveable direct page and stack and some wider registers), but also awkward with its register mode switching.

I wonder if the CSG-65CE02 wasn't an attempt to make C easier for the C6x/c128 line. Unfortunately it never saw the light of day except as a serial controller and isn't available today

https://en.wikipedia.org/wiki/CSG_65CE02

Post reply on HN