Skip to content

Common API definitions

Andrey Eliseev edited this page Oct 3, 2026 · 25 revisions

Built-in macros and definitions.

__CDM__ macro

#define __CDM__

Defined automatically when compiling for the cdm-cocas or the cdm target.

__COCAS__ macro

#define __COCAS__

Defined automatically when compiling for the cdm-cocas target.

__CDM_EXT_E__ macro

#define __CDM_EXT_E__

Defined automatically when the "E" (operating system) extension is enabled.

__CDM_EXT_M__ macro

#define __CDM_EXT_M__

Defined automatically when the "M" (integer multiplication and division) extension is enabled.

__VON_NEUMANN__ macro

#define __VON_NEUMANN__

Defined automatically when compiling for the von Neumann memory architecture (with -mmem-model=vonNeumann).

__HARVARD__ macro

#define __HARVARD__

Defined automatically when compiling for the Harvard memory architecture (with -mmem-model=harvard).

__progmem macro

#define __progmem __attribute__((__address_space__(1)))

__progmem const char example_constant[] = "Hello, world!";

void foo(__progmem const char *progmem_ptr);

Sets the address space of a global variable or a pointer to 1 (program memory).

  • global variables marked with __progmem will be placed in program memory and accessed with lcw/lcb/lcsb commands
  • pointers marked with __progmem will use lcw/lcb/lcsb commands when dereferenced

Important

Must be used with const, because program memory is read-only.

Exception handler functions

#include <cdm/ivt.h>

ISR void _exception_handler(context_t *ctx);
ISR void _ex_unaligned_sp(context_t *ctx);        // 0xDED1
ISR void _ex_unaligned_pc(context_t *ctx);        // 0xDED2
ISR void _ex_invalid_inst(context_t *ctx);        // 0xDED3
ISR void _ex_double_fault(context_t *ctx);        // 0xDED4
ISR void _ex_priv_violation(context_t *ctx);      // 0xDED5
ISR void _ex_reserved_6(context_t *ctx);          // 0xDED6
ISR void _ex_syscall(context_t *ctx);             // 0xDED7
ISR void _ex_reserved_8(context_t *ctx);          // 0xDED8
ISR void _ex_reserved_9(context_t *ctx);          // 0xDED9
ISR void _ex_reserved_a(context_t *ctx);          // 0xDEDA
ISR void _ex_reserved_b(context_t *ctx);          // 0xDEDB
ISR void _ex_reserved_c(context_t *ctx);          // 0xDEDC
ISR void _ex_reserved_d(context_t *ctx);          // 0xDEDD
ISR void _ex_reserved_e(context_t *ctx);          // 0xDEDE
ISR void _ex_reserved_f(context_t *ctx);          // 0xDEDF

Note

Only supported on the cdm target.

These functions are called when a hardware exception is triggered.

Default behavior: 0xDEDY is loaded into r0 (where Y is the exception number) and the processor is halted.

The functions can be re-defined to override this behavior:

  • _exception_handler - exception handler used in absence of a specific exception handler (any of the below)
  • _ex_unaligned_sp - unaligned stack pointer register (SP)
  • _ex_unaligned_pc - unaligned program counter register (PC)
  • _ex_invalid_inst - invalid instruction
  • _ex_double_fault - double fault
  • _ex_priv_violation - privilege violation (cdm16e only)
  • _ex_syscall - system call (cdm16e only)

Note

Before returning from these functions, you should modify the saved program counter. If it is left unchanged, the processor will try to re-execute the instruction that triggered the exception, re-entering the exception handler.

_interrupt_handler() function

#include <cdm/ivt.h>

ISR void _interrupt_handler(context_t *ctx);

Note

Only supported on the cdm target.

This function is called when handling an interrupt without a specific handler attached to it.

Default behavior: 0xDED0 is loaded into r0, then the processor halts. The function can be re-defined to override this behavior.

main() function

int main(void);

The main entry point to the application. The return value is left in r0 when the processor halts.

Clone this wiki locally