Putting The C In C64

Older CPUs and some fairly modern microcontrollers are not made to readily support C compilers. Among those are the 1802, some 8-bit PICs, and the 6502 at the heart of the Commodore 64. That’s not to say you can’t make a C compiler for any of them, but the tricks required to handle the odd word sizes, lack of stack manipulation, or whatever other reason C isn’t a good fit tends to make compiled code bloated and possibly slower. [Dr. Mortal Wombat] took a different approach. The oscar64 compiler takes C source code and compiles it to a virtual machine code or native machine code for cases where performance might be important.

Turns out, the penalty for using native code isn’t as much as predicted, at least in some cases, The performance penalty for using the interpreter, however, can be significant in many common cases. The 6502 has a small stack that is hard to address, and indexing into a user-maintained stack is slow. The word size problem also produces lots of code as you have to break 16-bit operations into multiple 8-bit ones. The compiler aims to be C99-compliant, including floating point, recursion, multiple dimensions for arrays, and pointers to structures.

There are a few things left to hammer out. The linker doesn’t support external libraries, and the floating point code doesn’t understand NaN. On the other hand, many C++ features are available, like namespaces, reference types, templates, and more. The compiler can target several Commodore machines from the C128 to the PET. It also works with some Nintendo and Atari systems and can create various cartridge formats.

If you are writing code for any kind of 6502, it is probably worth checking out. Compiling C for the 6502 is no small feat, but then, so it is targeting PowerPoint. Don’t have a C64? Build one.

Image: [MOS6502], CC-BY-SA 3.0

Self-Hosted Pi Pico Development

Older readers and those with an interest in retrocomputing may remember the days when a computer might well have booted into a BASIC interpreter. It was simultaneously a general purpose device that could run any software it would load, and also a development environment. Not something that can be said for today’s development boards which typically require a host computer on which to write code. Have we lost something along the way? Perhaps an answer to that question can be found in [lurk101]’s self-hosted development environment for the Raspberry Pi Pico.

It presents itself as a shell, with a flash file system, a port of the vi editor, and a C compiler. We might think of vi as being more at home on a UNIX-derived system, but in this case it’s a port of the vi included in BusyBox. Meanwhile the compiler comes from amacc project.

Of course, this still requires a terminal of some type which in practice will mean a host computer. But the feat is nevertheless an interesting one, and we can see that it might not be impossible given the Pico’s surprising versatility to being some of the terminal features onto the chip itself.

It’s worth noting that this isn’t the first attempt we’ve seen to put a command line interface on a development board.

One Instruction To Rule Them All: C Compiler Emits Only MOV

How many instructions do you need to successfully compile C code? Let’s see, you’d need some jump instructions, some arithmetic functions, and — of course — move instructions, right? Turns out you only need the move instruction, which — on x86, at least — is Turing complete.

While the effort is a bit tongue-in-cheek, we have to admit that if you were trying to create your own CPU, this would make for a simple architecture and might have power or complexity advantages, so maybe someone will find a practical use for it after all. If you wanted a C compiler for a simple CPU, this wouldn’t require much to emulate at a byte-code level, either.

Continue reading “One Instruction To Rule Them All: C Compiler Emits Only MOV”

Compiling C To PowerPoint

If you have worked for a large company — or even a small one — it might seem that you spend more time writing PowerPoint charts than programming. [Tom Widenhain’s] video asks the question: Can we compile C into PowerPoint? Watch the video below to find out the answer. Would it surprise you to know that [Tom] wanted to simulate the x86? It surprised us, too, and we had to note the video appeared on April 1. It does look workable, though, other than it is a bit unwieldy.

Instead of a Turing machine, this builds a set of clever logic gates. Unsurprisingly, [Tom] is the guy who put together a Turing machine in Excel. Surprisingly, he isn’t the first one to attempt a C to PPT compiler. The University of Chicago had a similar idea over a year ago, based on [Tom’s] earlier work and executed program using inefficient Turing machines.

Continue reading “Compiling C To PowerPoint”

Compiler Explorer, Explored

It wasn’t long ago that we introduced you to a web site, the Godbolt compiler explorer, that allows the visitor to compile code using a slew of compilers and compare their output. We suspect some number of readers said, “Wow! I can use that!”, while perhaps everyone else said, “Huh?” Well if you were in the second group, you ought to watch [What’s a Creel’s] video below where he walks through using the website. He looks at four different algorithms using four different compilers and it is a good example of how you might use the tool to make decisions about how you write software.

Continue reading “Compiler Explorer, Explored”

The No-CPU Computer Gets A C Compiler

C is the most perfect language and it will run on anything. It will even run on a computer without a CPU.

The computer in question here is the Gigatron, a fully-functional ‘home computer’ the likes of which you would find in the late 70s and early 80s, complete with a VGA output. What makes the Gigatron exceptional is the fact that there is no microprocessor; everything is just a RAM, a ROM, and a bunch of logic chips. There is no ALU chip. Or rather, there is; it’s just that an entire RISC CPU is implemented in basic logic chips and a whole lot of microcode on the ROM. It’s weird, yes, but it is cool. We’ve taken a look at the Gigatron before, and with this computer you get a glimpse of how clever engineers could have been if there were massive memories available in the late 70s.

While the Gigatron can be programmed in BASIC, the limiting factor of this computer is the fact that it remains exceptionally difficult to program. This is what the 8-Bit Guy says, and even though you can write some simple programs, it’s nothing compared to the likes of an Apple II or C64. If only there were a proper IDE, indeed if only there were a C compiler. That’s where [pgavlin] comes in. He has the LCC compiler working on the Gigatron. This is technically a C compiler for a computer without a CPU, or a computer that is entirely CPU. Either way you look at it, this is impressive.

As far as examples and demos go, [pgavlin] has a demo of Conway’s Game of Life working, and a program that will put dots on the screen. It’s not much, and it’s very slow, but check out the video below.

This isn’t a complete implementation of C, as multiplication, division, mod, and arbitrary shifts left or right haven’t been written yet. Floating point support will probably never be completed, and there’s no shame in that. The hardware is limited due to the fact of the fragmented memory map, but this can be improved by upgrading the Gigatron to a 64k memory model.

 

Continue reading “The No-CPU Computer Gets A C Compiler”

Can You Trust Your C Compiler?

If you are writing a hello world program, you probably aren’t too concerned about how the compiler translates your source code to machine code. However, if your code runs on something that people’s lives depend on, you will want to be a bit pickier and use something like the COMPCERT compiler.  It’s a formally verified compiler, meaning there is a mathematical proof that what you write in C will be correctly translated to machine code. The compiler can generate for PowerPC, ARM, RISC-V, and x86, accepting a subset of ISO C 99 with a few extensions. While it doesn’t produce code that runs as fast as gcc, you can be sure the generated code will do what you asked it to do.

Of course, this still provides no assurance that your code will work. It just means that if you write something such as “x=0;” the generated code will set x to zero and will not do anything else. You can apply formal methods to verify your source code and be assured that the compiler doesn’t introduce possible failures. Cases where code like “x=0;” does extra things or incorrect things are very hard to figure out because the source code is correct and an examination of the generated code would be necessary to find the compiler’s code generation bug.

Continue reading “Can You Trust Your C Compiler?”