Author Topic: Best RETARGETABLE C compiler for FPGA CPU projects?  (Read 6273 times)

0 Members and 1 Guest are viewing this topic.

Offline plainsteveTopic starter

  • Newbie
  • Posts: 2
  • Country: us
Best RETARGETABLE C compiler for FPGA CPU projects?
« on: January 17, 2020, 07:02:24 pm »
One of my bucket list items is to create a CPU (on FPGA) which can play Doom at 320x200(or 240) and then move to Quake 1 at 640x480.  One major setback is a lack of a C compiler which is accessible enough to actually retarget by a person who does not wish to spend his career as a compiler specialist.  I've looked at LLVM on and off over the last 6 years, but their documentation is awful and I don't have the time to chip away at that behemoth.

Can you suggest a decent quality C compiler that is retargetable by someone knowledgeable about compilers but not an expert?

Thanks!
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11393
  • Country: us
    • Personal site
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #1 on: January 17, 2020, 07:08:16 pm »
Realistically any advanced compiler will require a lot of effort to retarget.

TCC seems like your best bet, but I'm not sure it actually supports enough of C to compile Quake.  I bet Doom won't be a problem though.

As an alternative you can create a core for already existing architecture (like RISC-V). This will move the project forward fast and if you still feel like doing it, you can replace the core at a later time when all the peripherals are working.
Alex
 

Offline Berni

  • Super Contributor
  • ***
  • Posts: 4997
  • Country: si
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #2 on: January 17, 2020, 07:55:10 pm »
Yeah if you are using a FPGA then its best to just make the softcore CPU compatible with some sort of existing instruction set. There are a lot of instruction sets out there so im sure you will find one that works for what you are trying to make.

Just make sure to use a instruction set that is reasonably high level language friendly so that the compiler will have a easy time making fast optimized code for it. So you may need to chose a bit more complex of a cpu core or you might have to run your softcore CPU at a fairly high speed to compensate for the slow software.
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9905
  • Country: us
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #3 on: January 17, 2020, 11:29:27 pm »
It would be best to just use a core provided by the FPGA manufacturers like Intel (Altera) NIOS or Xilinx MicroBlaze.

If you really want to do it the hard way with a custom core, consider using LCC.

https://en.wikipedia.org/wiki/LCC_(compiler)

https://www.amazon.com/Retargetable-Compiler-Design-Implementation/dp/0805316701
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4506
  • Country: dk
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #4 on: January 18, 2020, 12:24:18 am »
It would be best to just use a core provided by the FPGA manufacturers like Intel (Altera) NIOS or Xilinx MicroBlaze.

If you really want to do it the hard way with a custom core, consider using LCC.

https://en.wikipedia.org/wiki/LCC_(compiler)

https://www.amazon.com/Retargetable-Compiler-Design-Implementation/dp/0805316701

here's an example of an FPGA cpu core and LCC, http://www.fpgacpu.org/xsoc/index.html


 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14706
  • Country: fr
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #5 on: January 18, 2020, 12:55:08 am »
Realistically any advanced compiler will require a lot of effort to retarget.

TCC seems like your best bet, but I'm not sure it actually supports enough of C to compile Quake.  I bet Doom won't be a problem though.

TCC is probably much easier to write a new target for than LLVM or GCC.
But as to your remark? You must be kidding, or haven't looked at it in a long while. It's fully C99 compliant, and supports a sizable portion of C11 as well (including Generic). I've tried compiling a wide variety of projects with it and found no issue. It's also blazingly fast.

It's become very good actually. Only downside is, of course, that it does very little optimization (which explains why it's so fast and why it's relatively easy to modify). It has ARM and ARM-64, x86 and x86-64,
RISCV-64 and oddly C67, so that's a few targets there, should be good to get you started writing a new one.

As I said above, it does little to almost no code optimization, so keep that in mind if you're going for it, but other than that, it's very good.


 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11393
  • Country: us
    • Personal site
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #6 on: January 18, 2020, 01:01:16 am »
Well, I was not worried about standard C support. But I have no doubt that Quake code contains inline assembly and SIMD intrinsic. I don't know if they can be easily disabled. I have never seen Quake source code.
Alex
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14706
  • Country: fr
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #7 on: January 18, 2020, 01:16:57 am »
Well, I was not worried about standard C support. But I have no doubt that Quake code contains inline assembly and SIMD intrinsic. I don't know if they can be easily disabled. I have never seen Quake source code.

OK, this part
Quote
TCC seems like your best bet, but I'm not sure it actually supports enough of C to compile Quake.
was probably kind of confusing to me. Inline assembly is not C, and so supporting it is not supporting "enough of C". Its support is not standard anyway so if there's any, it could be hard to compile with any other compiler than the one used initially. Besides, the OP is thinking about creating a non-exisiting CPU as far as I got it? So existing inline assembly would be useless whatever the compiler is. Just my 2 cents.

And that said, for the record, TCC does support GCC's extension for inline assembly:
https://bellard.org/tcc/tcc-doc.html#GNU-C-extensions

so porting that to a new target should also be doable strictly focusing on the target (back-end).
« Last Edit: January 18, 2020, 01:22:54 am by SiliconWizard »
 

Offline plainsteveTopic starter

  • Newbie
  • Posts: 2
  • Country: us
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #8 on: January 18, 2020, 02:42:09 am »
SiliconWizard wrote:
Quote
Besides, the OP is thinking about creating a non-exisiting CPU as far as I got it?

Yes.  I want to make a CPU of my own design.

* * *

Thanks for comments.  So far, LCC 4.x, TCC, and something I came across called SDCC seem worthy of investigation.
Open to more suggestions, though.
 

Offline ataradov

  • Super Contributor
  • ***
  • Posts: 11393
  • Country: us
    • Personal site
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #9 on: January 18, 2020, 02:43:21 am »
Don't bother with SDCC. It definitely does not support enough of actual basic C to be usable. It is barely usable on 8-bit MCUs, which it targets.
Alex
 

Offline SiliconWizard

  • Super Contributor
  • ***
  • Posts: 14706
  • Country: fr
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #10 on: January 18, 2020, 03:26:32 pm »
SDCC is 8-bit only AFAIK indeed? I've used it for 8051 targets (Cypress FX1/FX2) and it was fine for that, knowing all the limitations those processors have.

If the OP intends on developing an 8-bit CPU, that would actually be a wise choice (as the others we cited don't support 8-bit targets easily or at all I think?), but otherwise, I agree it would be useless.
 

Online laugensalm

  • Regular Contributor
  • *
  • Posts: 120
  • Country: ch
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #11 on: January 18, 2020, 06:14:54 pm »
Why don't you just pick a RISC-V and add some Bresenham rendering engine that you can drive with your own 'inline' assembly?
That way you might really focus on the fun stuff and base on a well tested compiler. Plus, you'd possibly have some seriously reusable IP in the drawer.
Other than that I found the LCC pretty interesting for wild CPU architecture hacking (actually, on a sidenote, there's an interesting CPU concept 'X32' that seems to understand the LCC bytecode directly). However, at that time the LCC had troubles with designated initializers, IIRC.

When going 'classical register style', I think you can't be much different from a RISC-V, it's kind of a logical attractor architecture. You could of course consider different concepts, like stack machines (e.g. ZPU, there's also various DOOM hacks, like https://turbogrill.blogspot.com/2014/06/doom-on-zpu.html)
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9905
  • Country: us
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #12 on: January 18, 2020, 06:54:47 pm »
A couple of man-weeks to design and implement a CPU in an FPGA but from what I see, man-years to retarget the compiler.  SDCC doesn't seem to be over-friendly in that area and I haven't tried retargeting LCC.  LCC started out as a compiler specifically for retargeting so it may be a great deal easier to modify than SDCC,

That's why I would pick a CPU for which a toolchain already exists.  Designing compilers is not a job for mere mortals.

Many of the text based games were available on the PDP-11/70.  Well, there is the PiDP-11-70 which is a really good emulation and runs several operating systems including BSD 2.11 which is very similar to BSD 4.3.  It is based on a Raspberry pi with the SIMH simulator package simulating the PDP-11/70.  I have two of these machines and I really enjoy writing little code snippets in C, Pascal or FORTRAN.  I have no particular application but I do have a web server on each (yet to have any interesting content).  Berkeley Sockets works well!  Somehow, that seems really important, to me...

https://obsolescence.wixsite.com/obsolescence/pidp-11

There are competing goals:  1) Play the games, 2) invent a CPU and 3) retarget a compiler.

For mere mortals, goal 3 is out of the question which kills goal 2.  It would be far better to pick a CPU (like the Z80) for which tools already exist (CP/M, C, Macro Assembler, FORTRAN, PL/I).  To that end, I recommend the T80 core at OpenCores.org.  It is a cycle accurate Z80 and I used it to implement CP/M (and friends) on a Spartan 2 about 15 years ago.  It was as easy as adding a Compact Flash to the bus, writing the BIOS code, loading the disk image (I forget how I did that, today I would use Linux 'dd') and it was up and running.  A really fast Z80!

Again, I picked a CPU for which tools already exist.  In fact, I used the 22Nice Z80 emulator, running on a PC, to allow use of the Digital Research Macro Assembler to write the BIOS.  Ultimately, I had Kermit running and I transferred the bulk of the files from a CompuPro (real) Z80 which used 8" floppies.  Pretty cool to watch that old dog feed files to something like an FPGA Z80 CPU.

The T80 initially came up because I wanted to recreate PacMan - in fact, PacMan got me started in this adventure with FPGAs.  I have it running on a Nexys 2 board.

https://www.fpgaarcade.com/kb/pacman/

BDS C Compiler (started in 1979) for the 8080/Z80

https://www.bdsoft.com/resources/bdsc.html

Short answer:  I would avoid retargeting a compiler at any cost.  To that end, I would avoid designing a custom CPU.  We're talking about man-years of work (as a guess) to get that C compiler running.  Don't forget, the emitted code has to pass the regression tests - all of them!  But even the tests need to be retargeted.  The compiler becomes the project and that isn't the original goal.

One alternative might be to manually convert the C code to asssembly language.  An assembler for a custom CPU could be written in Python in a couple of days.  Maybe a week if there is any desire to have a listing.  There are examples on the web.  A Macro Assembler would be far better.

I thought a lot of these text based games had been ported to the Z80 (with CP/M?).
« Last Edit: January 18, 2020, 06:57:37 pm by rstofer »
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4118
  • Country: nz
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #13 on: January 18, 2020, 07:41:41 pm »
PDP11, MSP430, M68k/Coldfire, SH4 form a kind of family of ISAs that are quite pleasant to program in assembly language and relatively easy to write a decent compiler for. The PDP11 doesn't really have enough registers (8, with 5 or 6 available), and the others double this in different ways. The last two in the list have the big advantage of being 32 bit. They have nice compact code, though no real advantage over variable-length Thumb2 or RISC-V with the C extension.

They're all pretty easy to implement using a microcode approach, but very difficult without, so they're going to lose out in speed to RISC, which is effectively its own microcode.

I don't know where you'd find a compiler for modern C targeting the PDP11. The others are all supported by gcc and/or llvm.


The 8080/z80 are just ugly. They're extremely difficult to make good code for, either hand-written or with a compiler, and the code tends to be huge, and they also need microcoding to implement. And 16 bit address space. I'd stay away unless you specifically have existing code you want to run -- CP/M, wordstar, old games etc.

6502 is much the same, in a different direction. It's easier to implement, and easier to hand or machine write code for once you wrap your head around zero page being the registers, not the actual registers :-)

6809 and 8086 are a huge usability improvement over their predecessors both for hand-written and compiled code, and code size. But you're just so much better off going for something similar to ARMv2, MIPS, RISC-V for implementation simplicity, speed, and ease of compiling for.

Transputer would be an interesting wildcard. Very easy to implement the basic integer ISA and should be easy to write a compiler for. You can make it 16 bit, 32 bit, 64 bit or whatever you want as the actual ISA just doesn't care!
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9905
  • Country: us
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #14 on: January 18, 2020, 08:39:23 pm »

I don't know where you'd find a compiler for modern C targeting the PDP11. The others are all supported by gcc and/or llvm.


On any of the Unix (BSD) releases for the PDP-11.  BSD 2.11 is my favorite of those that run on the PiDP-11 simply because I understand Unix (a bit) but I know nothing about the other DEC OSes.  They might be learnable but they are not compelling - to me.

BSD 2.11 has everything!  C (along the lines of the ORIGINAL C, as used to create Unix), FORTRAN and Pascal (a Berkeley creation).  No, the C doesn't comply with any of the later standards but is exactly K&R C as given in "The C Programming Language" book (original, pre-ANSI edition).  If it was good enough to create Unix, it will probably handle Doom.

For giggles, attached is a screen shot of a Telnet session into a PiDP-11/70 and a listing of the  /bin directory.  CC shows up, FORTRAN and Pascal are elsewhere.

OK, the PiDP-11 project is based on a Raspberry Pi but there is a parallel project using an FPGA incantation to implement the same setup including all of the OSes.  Most important, to me, is BSD 2.11.

Here is the project:  http://pdp2011.sytse.net/wordpress/

So, it is possible to run BSD 2.11 on an FPGA inside the nifty console of the PiDP-11.  Now we're talking retro!

A lot of games were written for the PDP-11s (and the PDP-8s).
« Last Edit: January 18, 2020, 08:41:58 pm by rstofer »
 

Offline ale500

  • Frequent Contributor
  • **
  • Posts: 415
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #15 on: January 19, 2020, 06:27:39 pm »
There is the bkunix project, the cross-devel directory has all the tools for the pdp-11, assembler, linker, compiler. I compiled them on linux on windows, they work pretty well.

https://github.com/sergev/bkunix/tree/master/cross-devel
 

Offline dsula

  • Newbie
  • Posts: 1
  • Country: us
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #16 on: May 19, 2024, 06:40:15 pm »
Use vbcc at http://www.compilers.de/

By the far the simplest retargetable C compiler out there. I'm no compiler expert but was able to write my own backend for several of my own CPU creations.
 
The following users thanked this post: edavid

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9905
  • Country: us
 

Offline gnuarm

  • Super Contributor
  • ***
  • Posts: 2247
  • Country: pr
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #18 on: May 25, 2024, 01:42:59 pm »
People seem enthralled by C.  I'm not such a big fan.  I much prefer to work in forth.  But then, I'm not trying to port C code. 

A forth oriented CPU is very easy to make in an fpga.  They can be small and if you know how to design in an HDL, they can be very fast.  Your forth CPU can be programmed in forth to "interpret" x86 opcodes as an intermediate language.  Then you can still program it in C.
Rick C.  --  Puerto Rico is not a country... It's part of the USA
  - Get 1,000 miles of free Supercharging
  - Tesla referral code - https://ts.la/richard11209
 

Offline langwadt

  • Super Contributor
  • ***
  • Posts: 4506
  • Country: dk
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #19 on: May 25, 2024, 10:04:43 pm »
People seem enthralled by C.  I'm not such a big fan.  I much prefer to work in forth.  But then, I'm not trying to port C code. 

A forth oriented CPU is very easy to make in an fpga.  They can be small and if you know how to design in an HDL, they can be very fast.  Your forth CPU can be programmed in forth to "interpret" x86 opcodes as an intermediate language.  Then you can still program it in C.

what does x86 opcodes have to do  with c? x86 didn't even exist when c came about
 
The following users thanked this post: SiliconWizard

Offline gnuarm

  • Super Contributor
  • ***
  • Posts: 2247
  • Country: pr
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #20 on: May 26, 2024, 03:57:16 am »
People seem enthralled by C.  I'm not such a big fan.  I much prefer to work in forth.  But then, I'm not trying to port C code. 

A forth oriented CPU is very easy to make in an fpga.  They can be small and if you know how to design in an HDL, they can be very fast.  Your forth CPU can be programmed in forth to "interpret" x86 opcodes as an intermediate language.  Then you can still program it in C.

what does x86 opcodes have to do  with c? x86 didn't even exist when c came about

Existing compilers will compile C code for an x86 processor.  Your forth target can emulate an x86 and run the compiled C code.   I believe the C compilers still support the early x86 processors, like the 8086, 80286, etc.  So, you don't need to worry with the full, modern instruction set. 

Meanwhile, your forth target will run forth to provide an interactive scripting language.
Rick C.  --  Puerto Rico is not a country... It's part of the USA
  - Get 1,000 miles of free Supercharging
  - Tesla referral code - https://ts.la/richard11209
 

Online RoGeorge

  • Super Contributor
  • ***
  • Posts: 6416
  • Country: ro
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #21 on: May 26, 2024, 06:02:59 am »
One of my bucket list items is to create a CPU (on FPGA) which can play Doom at 320x200(or 240) and then move to Quake 1 at 640x480.  One major setback is a lack of a C compiler which is accessible enough to actually retarget by a person who does not wish to spend his career as a compiler specialist.

If I got it right, this is a hardware exercise in designing a CPU + a video out.  I will assume you know that there are already examples of similar FPGA implementations, but you want to design your own.

If so, I would just take the instruction set of an existing CPU that already has a port for existing C compilers, and design the CPU compatible with that instruction set.  :)

Side note, having a C port for the new CPU might not be enough.  Depending on how innovative is the architecture of the new CPU, it might also need an assembler and a linker written from scratch (maybe a dissasembler and a debugger, too), aside from the C port.

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4118
  • Country: nz
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #22 on: May 26, 2024, 06:42:45 am »
People seem enthralled by C.  I'm not such a big fan.  I much prefer to work in forth.  But then, I'm not trying to port C code. 

A forth oriented CPU is very easy to make in an fpga.  They can be small and if you know how to design in an HDL, they can be very fast.  Your forth CPU can be programmed in forth to "interpret" x86 opcodes as an intermediate language.  Then you can still program it in C.

what does x86 opcodes have to do  with c? x86 didn't even exist when c came about

Existing compilers will compile C code for an x86 processor.  Your forth target can emulate an x86 and run the compiled C code.   I believe the C compilers still support the early x86 processors, like the 8086, 80286, etc.  So, you don't need to worry with the full, modern instruction set. 

Meanwhile, your forth target will run forth to provide an interactive scripting language.

Yes you could do that, but there is no reason to think that x86 is the best ISA to use for this purpose. Almost anything else (at least 16 or 32 bit) I can think of would be better.

x86 is just about as annoying to parse / decode in software as it is in hardware. Perhaps even worse, it is EXTREMELY annoying and slow to emulate condition codes on a machine with different condition codes or even different rules for setting them (or none at all). In a JITting emulator that can give a 2:1 slowdown, but in an interpretive one (assuming trivial instruction code, which x86 doesn't have) it can be 5:1 or worse compared to emulating an ISA without condition codes.

 

Offline gnuarm

  • Super Contributor
  • ***
  • Posts: 2247
  • Country: pr
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #23 on: May 26, 2024, 07:58:07 am »
People seem enthralled by C.  I'm not such a big fan.  I much prefer to work in forth.  But then, I'm not trying to port C code. 

A forth oriented CPU is very easy to make in an fpga.  They can be small and if you know how to design in an HDL, they can be very fast.  Your forth CPU can be programmed in forth to "interpret" x86 opcodes as an intermediate language.  Then you can still program it in C.

what does x86 opcodes have to do  with c? x86 didn't even exist when c came about

Existing compilers will compile C code for an x86 processor.  Your forth target can emulate an x86 and run the compiled C code.   I believe the C compilers still support the early x86 processors, like the 8086, 80286, etc.  So, you don't need to worry with the full, modern instruction set. 

Meanwhile, your forth target will run forth to provide an interactive scripting language.

Yes you could do that, but there is no reason to think that x86 is the best ISA to use for this purpose. Almost anything else (at least 16 or 32 bit) I can think of would be better.

x86 is just about as annoying to parse / decode in software as it is in hardware. Perhaps even worse, it is EXTREMELY annoying and slow to emulate condition codes on a machine with different condition codes or even different rules for setting them (or none at all). In a JITting emulator that can give a 2:1 slowdown, but in an interpretive one (assuming trivial instruction code, which x86 doesn't have) it can be 5:1 or worse compared to emulating an ISA without condition codes.

You seem to be swimming against the flow.  If you can't figure out how to deal with this issue, then you should not be working on the problem. 

You have total control over the hardware CPU, and the x86 emulation.  There is no reason to make the problem difficult. 
Rick C.  --  Puerto Rico is not a country... It's part of the USA
  - Get 1,000 miles of free Supercharging
  - Tesla referral code - https://ts.la/richard11209
 

Online brucehoult

  • Super Contributor
  • ***
  • Posts: 4118
  • Country: nz
Re: Best RETARGETABLE C compiler for FPGA CPU projects?
« Reply #24 on: May 26, 2024, 09:05:18 am »
People seem enthralled by C.  I'm not such a big fan.  I much prefer to work in forth.  But then, I'm not trying to port C code. 

A forth oriented CPU is very easy to make in an fpga.  They can be small and if you know how to design in an HDL, they can be very fast.  Your forth CPU can be programmed in forth to "interpret" x86 opcodes as an intermediate language.  Then you can still program it in C.

what does x86 opcodes have to do  with c? x86 didn't even exist when c came about

Existing compilers will compile C code for an x86 processor.  Your forth target can emulate an x86 and run the compiled C code.   I believe the C compilers still support the early x86 processors, like the 8086, 80286, etc.  So, you don't need to worry with the full, modern instruction set. 

Meanwhile, your forth target will run forth to provide an interactive scripting language.

Yes you could do that, but there is no reason to think that x86 is the best ISA to use for this purpose. Almost anything else (at least 16 or 32 bit) I can think of would be better.

x86 is just about as annoying to parse / decode in software as it is in hardware. Perhaps even worse, it is EXTREMELY annoying and slow to emulate condition codes on a machine with different condition codes or even different rules for setting them (or none at all). In a JITting emulator that can give a 2:1 slowdown, but in an interpretive one (assuming trivial instruction code, which x86 doesn't have) it can be 5:1 or worse compared to emulating an ISA without condition codes.

You seem to be swimming against the flow.  If you can't figure out how to deal with this issue, then you should not be working on the problem. 

On the contrary, I have academically published (and frequently cited) work in this area (high performance RISC-V emulation on x86), have commits in QEMU and Spike (Golden Reference RISC-V emulator), and have worked on JITs for Javascript, C# and others.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf