Skip to content

Commit

Permalink
Make lib usable with C++
Browse files Browse the repository at this point in the history
Add 'extern "C"' to function definitions to inhibit name mangling
if used in C++ sources.
  • Loading branch information
jwupf committed Sep 6, 2024
1 parent 1f83cf7 commit eb45771
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 12 deletions.
17 changes: 13 additions & 4 deletions include/mega65/conio.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,11 @@
#define CINPUT_NO_AUTOTRANSLATE 8
#define CINPUT_ACCEPT_ALPHA CINPUT_ACCEPT_NUMERIC | CINPUT_ACCEPT_LETTER

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

/*------------------------------------------------------------------------
Public structs
-----------------------------------------------------------------------*/
Expand Down Expand Up @@ -923,10 +928,10 @@ void fastcall cputnc(unsigned char count, unsigned char c);
void cputhex(unsigned long n, unsigned char prec);

/* \m65libsummary{cputdec}{Output a decimal number at current position}
\m65libsyntax {void cputdec(unsigned long n, unsigned char padding, unsigned char
leadingZ)} \m65libparam {n}{The number to write} \m65libparam
{padding}{The padding space to add before number} \m65libparam {leadingZ}{The
leading zeros to print}
\m65libsyntax {void cputdec(unsigned long n, unsigned char padding,
unsigned char leadingZ)} \m65libparam {n}{The number to write}
\m65libparam {padding}{The padding space to add before number} \m65libparam
{leadingZ}{The leading zeros to print}
*/
/**
* @brief Output a decimal number at current position
Expand Down Expand Up @@ -1197,4 +1202,8 @@ void flushkeybuf(void);
unsigned char cinput(
unsigned char* buffer, unsigned char buflen, unsigned char flags);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif /* __MEGA65_CONIO_H */
9 changes: 9 additions & 0 deletions include/mega65/debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,19 @@
#ifndef __MEGA65_DEBUG_H
#define __MEGA65_DEBUG_H

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

/**
* @brief Write debug message to serial monitor
* @param msg Text message to write
*/
void debug_msg(char* msg);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_DEBUG_H
15 changes: 13 additions & 2 deletions include/mega65/dirent.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,24 @@

#include <stdint.h>

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

/// Open a directory
#ifdef __clang__
__attribute__((leaf))
#endif
unsigned char opendir(void);
unsigned char
opendir(void);

/// Read directory entry
#ifdef __clang__
__attribute__((leaf))
#endif
struct m65_dirent* readdir(unsigned char);
struct m65_dirent*
readdir(unsigned char);

/// Close directory entry
#ifdef __clang__
Expand All @@ -38,4 +45,8 @@ struct m65_dirent {
char d_name[256]; //!< Filename string of entry
};

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_DIRENT_H
9 changes: 9 additions & 0 deletions include/mega65/fcio.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,11 @@
#define COLOUR_GREY3 15 //!< Grey3 color
#define COLOUR_LIGHTGREY 15 //!< Light grey color

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

#ifndef byte
typedef unsigned char byte;
#endif
Expand Down Expand Up @@ -543,4 +548,8 @@ void fc_plotScreenChar(byte x, byte y, byte c, byte color, byte exAttr);
#endif
void fc_plotPetsciiChar(byte x, byte y, byte c, byte color, byte exAttr);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_FCIO_H
21 changes: 17 additions & 4 deletions include/mega65/fileio.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
#include <stdint.h>
#include <stddef.h>

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

#ifdef __clang__
__attribute__((leaf))
#endif
Expand Down Expand Up @@ -42,7 +47,8 @@ void close(uint8_t fd);
#ifdef __clang__
__attribute__((leaf))
#endif
uint8_t open(char* filename);
uint8_t
open(char* filename);

/**
* @brief Read up to 512 bytes from file
Expand All @@ -52,7 +58,8 @@ uint8_t open(char* filename);
#ifdef __clang__
__attribute__((leaf))
#endif
size_t read512(uint8_t* buffer);
size_t
read512(uint8_t* buffer);

/**
* @brief Change working directory
Expand All @@ -63,7 +70,8 @@ size_t read512(uint8_t* buffer);
#ifdef __clang__
__attribute__((leaf))
#endif
uint8_t chdir(char* filename);
uint8_t
chdir(char* filename);

/**
* @brief Change working directory to the root directory
Expand All @@ -72,7 +80,8 @@ uint8_t chdir(char* filename);
#ifdef __clang__
__attribute__((leaf))
#endif
uint8_t chdirroot(void);
uint8_t
chdirroot(void);

/**
* @brief Struct for holding version information of the hypervisor
Expand All @@ -96,4 +105,8 @@ __attribute__((leaf))
#endif
void gethyppoversion(struct hyppo_version* version);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_FILEIO_H
9 changes: 9 additions & 0 deletions include/mega65/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,19 @@

#include <stdint.h>

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

/**
* @brief Sleep for the given number of microseconds
* @param micros Microseconds to sleep
*/
void usleep(uint32_t micros);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_HAL_H
9 changes: 9 additions & 0 deletions include/mega65/math.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ SOFTWARE.
#define MULTINB 0xD774
#define MULTOUT 0xD778

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

/**
* @brief Multiply two 32-bit unsigned integers and return 32-bit value
* @param a First 32-bit value
Expand Down Expand Up @@ -92,4 +97,8 @@ inline uint8_t umul8(const uint8_t a, const uint8_t b)
uint8_t umul8(const uint8_t a, const uint8_t b);
#endif

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_MATH_H
15 changes: 13 additions & 2 deletions include/mega65/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@
#define DMA_XYMOD_ADDR \
0x03; //!< DMA XY MOD (bitmap rectangular) addressing mode (unimplemented)

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

/**
* @brief DMA list structure
*/
Expand Down Expand Up @@ -70,14 +75,16 @@ void mega65_io_enable(void);
#ifdef __clang__
__attribute__((leaf))
#endif
uint8_t lpeek(uint32_t address);
uint8_t
lpeek(uint32_t address);

#ifdef __llvm__
/**
* @brief Inlined version of `lpeek()`
* @param ADDRESS 28-bit address; must be a compile-time constant.
* @returns Value at address
* @warning This function is experimental and may change/be removed in future versions
* @warning This function is experimental and may change/be removed in future
* versions
*/
inline uint8_t lpeek_i(const uint32_t ADDRESS)
{
Expand Down Expand Up @@ -174,4 +181,8 @@ void lfill_skip(
#define PEEK64(X) (*(volatile uint64_t*)(X))
#endif

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_MEMORY_H
9 changes: 9 additions & 0 deletions include/mega65/mouse.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
#ifndef __MEGA65_MOUSE_H
#define __MEGA65_MOUSE_H

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

void mouse_warp_to(unsigned short x, unsigned short y);
void mouse_update_position(unsigned short* mx, unsigned short* my);
void mouse_update_pointer(void);
Expand All @@ -36,4 +41,8 @@ void mouse_bind_to_sprite(unsigned char sprite_num);
void mouse_set_bounding_box(
unsigned short x1, unsigned short y1, unsigned short x2, unsigned short y2);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_MOUSE_H
9 changes: 9 additions & 0 deletions include/mega65/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

#include <stdint.h>

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

/**
* @brief 32-bit random number using MEGA65 hardware entropy
* @param range Maximum value in range
Expand Down Expand Up @@ -54,4 +59,8 @@ uint16_t rand16(uint16_t range);
*/
uint32_t rand32(uint32_t range);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_RANDOM_H
9 changes: 9 additions & 0 deletions include/mega65/sdcard.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

#include <stdint.h>

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

extern uint8_t sector_buffer[512];

void mega65_clear_sector_buffer(void);
Expand All @@ -20,4 +25,8 @@ uint8_t mega65_sdcard_writesector(const uint32_t sector_number);
void mega65_sdcard_erase(
const uint32_t first_sector, const uint32_t last_sector);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_SDCARD_H
9 changes: 9 additions & 0 deletions include/mega65/targets.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@

#include <stdint.h>

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

enum
#ifdef __clang__
: uint8_t
Expand Down Expand Up @@ -38,4 +43,8 @@ enum
*/
uint8_t detect_target(void);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_TARGETS_H
9 changes: 9 additions & 0 deletions include/mega65/tests.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
#define XEMU_CONTROL 0xD6CF
#define XEMU_QUIT 0x42 //!< Command to make Xemu quit

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

/**
* @brief Quits Xemu with given exit code
* @param exit_code Exit code passed to Xemu, e.g. EXIT_SUCCESS or EXIT_FAILURE
Expand Down Expand Up @@ -217,4 +222,8 @@ void unit_test_print(uint8_t x, uint8_t y, uint8_t colour, char* msg);
void unit_test_read_pixel(
short x, short y, uint8_t* red, uint8_t* green, uint8_t* blue);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_TESTS_H
9 changes: 9 additions & 0 deletions include/mega65/time.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@
#ifndef __MEGA65_TIME_H
#define __MEGA65_TIME_H

#ifdef __cplusplus
// Being compiled by a C++ compiler, inhibit name mangling
extern "C" {
#endif

/**
* @brief Real-time clock structure
*/
Expand All @@ -35,4 +40,8 @@ void getrtc(struct m65_tm* tm);
/// Set real-time clock
void setrtc(struct m65_tm* tm);

#ifdef __cplusplus
} // End of extern "C"
#endif

#endif // __MEGA65_TIME_H

0 comments on commit eb45771

Please sign in to comment.