Compiling a Spin program on the PC
ersmith
Posts: 6,224
in Propeller 1
spin2cpp version 3.0.5 is out now, and it adds the ability to put inline C code in Spin classes and to override the C compiler. This makes it possible to run Spin code on the PC, as long as you provide C replacements for any Propeller specific functions. The output type for a PC is "--elf"; that makes sense on Linux (where ELF really is the native format), not so much on Windows, but I was too lazy to add another command line option just for Windows.
Of course, no PASM code will run on the PC, and registers like OUTA and DIRA are not available. So to do any I/O you'll have to provide C versions of the I/O routines, surrounded by an appropriate #ifdef. spin2cpp allows you to put inline C code inside special comments marked with "{++".
For example, a hello world program that runs on both the Propeller and PC is:
Of course, no PASM code will run on the PC, and registers like OUTA and DIRA are not available. So to do any I/O you'll have to provide C versions of the I/O routines, surrounded by an appropriate #ifdef. spin2cpp allows you to put inline C code inside special comments marked with "{++".
For example, a hello world program that runs on both the Propeller and PC is:
''
'' simple hello world that works on PC or Propeller
''
'' to compile for PC:
'' spin2cpp --cc=gcc -DPC --elf -o Hello HelloWorld.spin
'' to compile for Propeller
'' spin2cpp --binary -Os -o hello.binary HelloWorld.spin
'' or
'' spin2cpp --asm --binary -o hello_pasm.binary HelloWorld.spin
''
VAR
byte txpin
byte rxpin
long baud
long txmask
long bitcycles
#ifdef PC
'' we will be using stdio, so force it to
'' be included
{++
#include <stdio.h>
#include <stdlib.h>
}
#endif
''
'' print out hello, world
''
PUB hello
start(31, 30, 0, 115200)
repeat 10
str(string("hello, world!", 13, 10))
exit(0)
''
'' code: largely taken from FullDuplexSerial.spin
''
PUB start(rx_pin, tx_pin, mode, baudrate)
#ifndef PC
baud := baudrate
bitcycles := clkfreq / baudrate
txpin := tx_pin
txmask := (1<<txpin)
rxpin := rx_pin
#endif
return 1
PUB tx(c) | val, waitcycles
#ifdef PC
'' just emit direct C code here
{++
putchar(c);
}
#else
OUTA |= txmask
DIRA |= txmask
val := (c | 256) << 1
waitcycles := CNT
repeat 10
waitcnt(waitcycles += bitcycles)
if (val & 1)
OUTA |= txmask
else
OUTA &= !txmask
val >>= 1
#endif
PUB str(s) | c
REPEAT WHILE ((c := byte[s++]) <> 0)
tx(c)
PUB exit(code)
#ifdef PC
{++
exit(code);
}
#endif
repeat
