C from scratch?
Rick Murray (539) 13423 posts |
What is required in order to run some code written in C from a non-C environment? I am guessing that… R10 (SL) size of stack? Existing code seems to LSR#20 then LSL#20 this. The static data – aren’t these the first two words on the stack? What happens if you are not using CLib? The example in the PRM isn’t so helpful as this assumes that you will be invoking the entire CLib instead of simply calling some code written in C (that will be using custom library to avoid issues with setting up CLib’s jump tables). A lot of stuff is set up in Stub$$Data, I’m just wondering what happens if one doesn’t link to Stubs (or ANSILib that probably fakes similar behaviour). Should the __rt_stkovf_split_small function be provided? Why? A while back it was mentioned about using C code in the HAL. Got me wondering what would be necessary to start running C code at such a low level, before the OS (and CLib itself) are ‘available’. |
Colin Ferris (399) 1751 posts |
Have a look on Adrian’s site http://sendiri.co.uk/ [update] is there a contact email address for Adrian -: A copy of the stubs would be nice. (anyone done a port of RPCEmu for Win with the latest version of the ‘Allegro’ Library?) |
Jeffrey Lee (213) 6046 posts |
Stack limit. LSR#20, LSL#20 is used with module code, under the assumption that the privileged mode stacks will always start at a megabyte boundary (boo!). However there’ll also be an extra offset added on; this provides a bit of a safety buffer for if functions do go over the limit (IIRC leaf functions which only use small amounts of stack aren’t required to perform stack checks), and it also ensures there’s stack space available for the stack extension routine and for the relocation offsets (although stack extension, and in turn stack limit checking, should be disabled for module code since any extension stack isn’t likely to be megabyte-aligned anymore) If you’re not building module code then you can specify a “noswst” APCS variant in order to free up R10 for general use.
This shouldn’t be necessary if you specify a “nofp” APCS variant.
“Inter-procedure register” or somesuch. It’s a general-purpose callee-save register that may get used for special purposes during function entry. R13 (SP) should be a valid stack pointer Yep.
They’re only used by module code. The compiler generates references to them no matter what library you’re using (but you can use some magic to control where the relocation offsets are stored).
You’ll have to just try linking the binary and see which functions are missing. Stack splitting routines are easy to solve (disable stack limit checking), but you may need to implement integer division routines. Possibly the compiler will also generate references to memcpy and memset when large structures are being copied or zero-initialised.
https://www.riscosopen.org/wiki/documentation/show/Using%20C%20in%20assembler%20components If you want some pre-OS, pre-MMU examples then you could take a look at the IOMD HAL (which uses it for calculating an optimal RAM configuration), or the old HALTester utility/fake kernel I wrote to allow for some basic testing of the OMAP3 HAL before it was linked up to the kernel. There’s also the zlib inflate stub that gets appended to compressed ROMs, although that’s only run once the MMU is enabled, and a few of the HALs have C code that runs once the MMU is enabled. |