UART Channels

UART driver added for channels.
This commit is contained in:
Peter Weingartner 2022-04-27 16:21:06 -04:00
parent b53a2d92d4
commit 86e475e0de
15 changed files with 10152 additions and 9666 deletions

Binary file not shown.

View file

@ -25,6 +25,7 @@
#include "dev/uart.h" #include "dev/uart.h"
#include "uart_reg.h" #include "uart_reg.h"
#include "rtc_reg.h" #include "rtc_reg.h"
#include "utilities.h"
#include "vicky_general.h" #include "vicky_general.h"
#include "version.h" #include "version.h"
@ -285,36 +286,6 @@ short cli_exec(short channel, char * command, int argc, const char * argv[]) {
return cmd_run(channel, argc, argv); return cmd_run(channel, argc, argv);
} }
char * strtok_r(char * source, const char * delimiter, char ** saveptr) {
char * x = *saveptr;
char * y;
/* Skip over leading delimiters */
for (x = *saveptr; *x && (*x == delimiter[0]); x++) {
}
/* If we reached the end of the string, return NULL */
if (*x == 0) {
return 0;
}
for (y = x; *y && (*y != delimiter[0]); y++) {
}
/* If we reached the end of the string, return x */
if (*y == 0) {
*saveptr = y;
return x;
}
/* Otherwise, make that position in the source string NULL, and return x */
*y = 0;
*saveptr = y + 1;
return x;
}
/** /**
* Reactivate the CLI's read-eval-print loop after a command has completed * Reactivate the CLI's read-eval-print loop after a command has completed
*/ */

View file

@ -214,40 +214,40 @@ short cli_test_uart(short channel, int argc, const char * argv[]) {
char c, c_out; char c, c_out;
short scan_code; short scan_code;
char buffer[80]; char buffer[80];
short port = 1; short cdev = CDEV_COM1;
short uart = -1;
short uart_index = 0; short uart_index = 0;
unsigned long uart_address = 0; unsigned long uart_address = 0;
if (argc > 1) { if (argc > 1) {
// Get the COM port number // Get the COM port number
port = (short)cli_eval_number(argv[1]); short port = (short)cli_eval_number(argv[1]);
if (port < 1) port = 1; if (port <= 1) cdev = CDEV_COM1;
if (port > 2) port = 2; if (port >= 2) cdev = CDEV_COM2;
} }
uart_index = port - 1; uart_index = cdev - CDEV_COM1;
uart_address = (unsigned long)uart_get_base(uart_index); uart_address = (unsigned long)uart_get_base(uart_index);
sprintf(buffer, "Serial port loopback test of COM%d at 0x%08X...\n", port, uart_address); sprintf(buffer, "Serial port loopback test of COM%d at 0x%08X...\n", cdev - CDEV_COM1 + 1, uart_address);
print(channel, buffer); print(channel, buffer);
uart_init(uart_index); uart = sys_chan_open(cdev, "9600,8,1,NONE", 0);
uart_setbps(uart_index, UART_9600); if (uart >= 0) {
uart_setlcr(uart_index, LCR_DATABITS_8 | LCR_STOPBIT_1 | LCR_PARITY_NONE); sprintf(buffer, "COM%d: 9600, no parity, 1 stop bit, 8 data bits\nPress ESC to finish.\n", cdev - CDEV_COM1 + 1);
sprintf(buffer, "COM%d: 9600, no parity, 1 stop bit, 8 data bits\nPress ESC to finish.\n", port);
print(channel, buffer); print(channel, buffer);
c_out = ' '; c_out = ' ';
do { do {
uart_put(0, c_out++); sys_chan_write_b(uart, c_out++);
if (c_out > '}') { if (c_out > '}') {
c_out = ' '; c_out = ' ';
uart_put(uart_index, '\r'); sys_chan_write_b(uart, '\r');
uart_put(uart_index, '\n'); sys_chan_write_b(uart, '\n');
} }
if (uart_has_bytes(uart_index)) { if (sys_chan_status(uart) & CDEV_STAT_READABLE) {
c = uart_get(uart_index); c = sys_chan_read_b(uart);
if (c != 0) { if (c != 0) {
sys_chan_write_b(channel, c); sys_chan_write_b(channel, c);
} }
@ -255,6 +255,10 @@ short cli_test_uart(short channel, int argc, const char * argv[]) {
scan_code = sys_kbd_scancode(); scan_code = sys_kbd_scancode();
} while (scan_code != 0x01); } while (scan_code != 0x01);
} else {
sprintf(buffer, "Unable to open the serial port: %d\n", uart);
print(channel, buffer);
}
return 0; return 0;
} }

View file

@ -83,8 +83,8 @@ p_channel chan_alloc(short dev) {
TRACE("chan_alloc"); TRACE("chan_alloc");
if ((dev == CDEV_CONSOLE) || (dev == CDEV_EVID) || (dev == CDEV_LPT)) { if ((dev >= CDEV_CONSOLE) && (dev < CDEV_FILE)) {
/* For CONSOLE, EVID, and LPT: the channel is always the same number as the device */ /* For most devices (all but files): the channel is always the same number as the device */
g_channels[dev].number = dev; g_channels[dev].number = dev;
g_channels[dev].dev = dev; g_channels[dev].dev = dev;
return &g_channels[dev]; return &g_channels[dev];

View file

@ -204,7 +204,7 @@ void fdc_delay(int jiffies) {
* ptr = pointer to the byte position to fill * ptr = pointer to the byte position to fill
* *
* Returns: * Returns:
* 0 on success, 1 = the controller has dropped out of execution mode, negative number is an error * 0 on success, negative number is an error
*/ */
short fdc_in(unsigned char *ptr) { short fdc_in(unsigned char *ptr) {
unsigned char msr, data; unsigned char msr, data;
@ -214,11 +214,6 @@ short fdc_in(unsigned char *ptr) {
for (i = 0; i < fdc_timeout; i += step) { for (i = 0; i < fdc_timeout; i += step) {
msr = *FDC_MSR & (FDC_MSR_DIO | FDC_MSR_RQM); msr = *FDC_MSR & (FDC_MSR_DIO | FDC_MSR_RQM);
if ((msr & FDC_MSR_NONDMA) == 0) {
// The controller has left execution mode... go to get the status bytes
return 1;
}
if (msr == (FDC_MSR_DIO | FDC_MSR_RQM)) { if (msr == (FDC_MSR_DIO | FDC_MSR_RQM)) {
data = *FDC_DATA; data = *FDC_DATA;
if (ptr) if (ptr)
@ -239,43 +234,6 @@ short fdc_in(unsigned char *ptr) {
return DEV_TIMEOUT; return DEV_TIMEOUT;
} }
/*
* Get a status/response byte from the floppy drive controller FIFO
*
* Inputs:
* ptr = pointer to the byte position to fill
*
* Returns:
* 0 on success, negative number is an error
*/
short fdc_stat_in(unsigned char *ptr) {
unsigned char msr, data;
short step, i;
step = 1;
for (i = 0; i < fdc_timeout; i += step) {
msr = *FDC_MSR & (FDC_MSR_DIO | FDC_MSR_RQM);
if (msr == (FDC_MSR_DIO | FDC_MSR_RQM)) {
data = *FDC_DATA;
if (ptr)
*ptr = data;
return 0;
}
if (msr == FDC_MSR_RQM) {
log(LOG_ERROR, "fdc_stat_in: ready for output during input");
return ERR_GENERAL;
}
step += step;
fdc_delay(step);
}
log(LOG_ERROR, "fdc_stat_in: timeout");
return DEV_TIMEOUT;
}
/* /*
* Put a byte to the floppy drive controller FIFO * Put a byte to the floppy drive controller FIFO
* *
@ -414,7 +372,7 @@ short fdc_sense_interrupt_status(unsigned char *st0, unsigned char *pcn) {
/* Read the status byte */ /* Read the status byte */
*st0 = *FDC_DATA; *st0 = *FDC_DATA;
log_num(LOG_ERROR, "ST0 = ", *st0); log_num(LOG_INFO, "fdc_sense_interrupt_status: ST0=", *st0);
if (fdc_wait_rqm()) { if (fdc_wait_rqm()) {
/* Timed out waiting to receive data */ /* Timed out waiting to receive data */
@ -612,8 +570,8 @@ short fdc_reset() {
return DEV_TIMEOUT; return DEV_TIMEOUT;
} }
log_num(LOG_INFO, "ST0: ", st0); log_num(LOG_INFO, "fdc_reset ST0: ", st0);
log_num(LOG_INFO, "PCN: ", pcn); log_num(LOG_INFO, "fdc_reset PCN: ", pcn);
if (st0 == 0xC0) { if (st0 == 0xC0) {
break; break;
@ -709,21 +667,12 @@ short fdc_command(p_fdc_trans transaction) {
case FDC_TRANS_READ: case FDC_TRANS_READ:
/* We're reading from the FDC */ /* We're reading from the FDC */
for (i = 0; (i < transaction->data_count) && ((*FDC_MSR & FDC_MSR_NONDMA) == FDC_MSR_NONDMA); i++) {
for (i = 0; i < transaction->data_count; i++) {
/* Wait for the FDC to be ready */
if ((result = fdc_in(&transaction->data[i])) < 0) { if ((result = fdc_in(&transaction->data[i])) < 0) {
log(LOG_ERROR, "fdc_command: timeout getting data"); log(LOG_ERROR, "fdc_command: timeout getting data");
return result; return result;
} }
if (result == 0) {
sys_chan_write_b(0, '.'); sys_chan_write_b(0, '.');
} else {
// We dropped out of execution mode... go get status bytes
log(LOG_ERROR, "Dropped out of execution mode");
break;
}
} }
break; break;
@ -734,7 +683,7 @@ short fdc_command(p_fdc_trans transaction) {
/* Result phase: read the result bytes */ /* Result phase: read the result bytes */
for (i = 0; i < transaction->result_count; i++) { for (i = 0; i < transaction->result_count; i++) {
if ((result = fdc_stat_in(&transaction->results[i])) < 0) { if ((result = fdc_in(&transaction->results[i])) < 0) {
log(LOG_ERROR, "fdc_command: timeout getting results"); log(LOG_ERROR, "fdc_command: timeout getting results");
return result; return result;
} }
@ -943,18 +892,13 @@ short fdc_read(long lba, unsigned char * buffer, short size) {
while (trans.retries > 0) { while (trans.retries > 0) {
result = fdc_command(&trans); /* Issue the transaction */ result = fdc_command(&trans); /* Issue the transaction */
if ((result == 0)) { // && ((trans.results[0] & 0xC0) == 0)) { if ((result == 0) && ((trans.results[0] & 0xC0) == 0)) {
sprintf(message, "fdc_read: success ST0 = %02x ST1 = %02x ST2 = %02x", trans.results[0], trans.results[1], trans.results[2]); sprintf(message, "fdc_read: success? ST0 = %02x ST1 = %02x ST2 = %02x", trans.results[0], trans.results[1], trans.results[2]);
log(LOG_ERROR, message); log(LOG_ERROR, message);
return size; return size;
} else { } else {
if (result != 0) {
sprintf(message, "fdc_read: retry ST0 = %02x ST1 = %02x ST2 = %02x", trans.results[0], trans.results[1], trans.results[2]); sprintf(message, "fdc_read: retry ST0 = %02x ST1 = %02x ST2 = %02x", trans.results[0], trans.results[1], trans.results[2]);
log(LOG_ERROR, message); log(LOG_ERROR, message);
} else {
sprintf(message, "ST0 = %02x ST1 = %02x ST2 = %02x", trans.results[0], trans.results[1], trans.results[2]);
log(LOG_ERROR, message);
}
} }
fdc_init(); fdc_init();
trans.retries--; trans.retries--;

View file

@ -2,9 +2,23 @@
* Definitions of the UART routines * Definitions of the UART routines
*/ */
#include <stdlib.h>
#include <string.h>
#include "errors.h"
#include "log.h" #include "log.h"
#include "uart_reg.h" #include "uart_reg.h"
#include "dev/channel.h"
#include "dev/uart.h" #include "dev/uart.h"
#include "simpleio.h"
#include "utilities.h"
/**
* Return the UART index for the given channel device
*/
short cdev_to_uart(short dev) {
return dev - CDEV_COM1;
}
volatile unsigned char * uart_get_base(short uart) { volatile unsigned char * uart_get_base(short uart) {
if (uart == 0) { if (uart == 0) {
@ -99,6 +113,26 @@ short uart_has_bytes(short uart) {
} }
} }
/**
* Return true (non-zero) if the UART transmit FIFO is not full
*
* @param uart the number of the UART: 0 for COM1, 1 for COM2
* @return non-zero if the FIFO can accept a byte, 0 if it is full
*/
short uart_can_send(short uart) {
volatile unsigned char * uart_base = uart_get_base(uart);
if (uart_base) {
if (uart_base[UART_LSR] & LSR_XMIT_EMPTY) {
return 1;
} else {
return 0;
}
} else {
return 0;
}
}
/* /*
* Send a byte to the UART. Blocks until the FIFO is ready to recieve a byte. * Send a byte to the UART. Blocks until the FIFO is ready to recieve a byte.
* *
@ -146,3 +180,305 @@ unsigned char uart_get(short uart) {
return uart_base[UART_TRHB]; return uart_base[UART_TRHB];
} }
} }
/**
* Return the status of the UART
*
* @param channel the descriptor for the channel
* @return the status of the device
*/
short uart_status(p_channel chan) {
short status = 0;
// Check if there is data to be read
if (uart_has_bytes(cdev_to_uart(chan->dev))) {
status |= CDEV_STAT_READABLE;
}
// Check if the FIFO is full
if (uart_can_send(cdev_to_uart(chan->dev))) {
status |= CDEV_STAT_WRITABLE;
}
// TODO: reflect error bits?
return status;
}
/**
* Open a serial connection... allow specification for bps, data size, etc.
*
* The specification provided to open is a string of four comma separated values:
* 1. Bits-per-second: 300, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200
* 2. Number of data bits: 5, 6, 7, 8
* 3. Stop bits: 1, or 2
* 4. Parity: NONE, ODD, EVEN, MARK, or SPACE
*
* @param chan the channel descriptor to open
* @param spec a string describing the connection to open: <bps>,<data bits>,<stop bits>,<parity Y/N>
* @param mode an unused parameter
* @return 0 on success, any other number is an error
*/
short uart_open(p_channel chan, const char * spec, short mode) {
unsigned short bps = 0, lcr = 0;
char * saveptr;
char spec_copy[80];
char *token = 0;
short i = 0;
short bps_code = 0;
short lcr_code = 0;
/* Do some basic initialization of the UART */
uart_init(cdev_to_uart(chan->dev));
// Make a local copy of the specification so we can tokenize it
strncpy(spec_copy, spec, 80);
// Get the first token
saveptr = spec_copy;
token = strtok_r(spec_copy, ",", &saveptr);
if (token) {
// Parse the bit rate token
i = atoi(token);
switch (i) {
case 300:
bps_code = UART_300;
break;
case 1200:
bps_code = UART_1200;
break;
case 2400:
bps_code = UART_2400;
break;
case 4800:
bps_code = UART_4800;
break;
case 9600:
bps_code = UART_9600;
break;
case 19200:
bps_code = UART_19200;
break;
case 38400:
bps_code = UART_38400;
break;
case 57600:
bps_code = UART_57600;
break;
case 115200:
bps_code = UART_115200;
break;
default:
log_num(LOG_ERROR, "uart_open: Bad BPS: ", i);
return ERR_BAD_ARGUMENT;
}
uart_setbps(cdev_to_uart(chan->dev), bps_code);
// Get the next token
token = strtok_r(spec_copy, ",", &saveptr);
if (token) {
// Parse the data bit count
i = atoi(token);
switch (i) {
case 5:
lcr_code = LCR_DATABITS_5;
break;
case 6:
lcr_code = LCR_DATABITS_6;
break;
case 7:
lcr_code = LCR_DATABITS_7;
break;
case 8:
lcr_code = LCR_DATABITS_8;
break;
default:
log(LOG_ERROR, "uart_open: Bad data word length");
return ERR_BAD_ARGUMENT;
}
// Get the next token
token = strtok_r(spec_copy, ",", &saveptr);
if (token) {
// Parse the stop bit count (1 or 2)
i = atoi(token);
if (i == 1) {
lcr_code |= LCR_STOPBIT_1;
} else if (i == 2) {
lcr_code |= LCR_STOPBIT_2;
} else {
log(LOG_ERROR, "uart_open: Bad stop bits");
return ERR_BAD_ARGUMENT;
}
// Get the next token
token = strtok_r(spec_copy, ",", &saveptr);
if (token) {
// NONE, ODD, EVEN, MARK, or SPACE
if (strcmp(token, "NONE") == 0) {
lcr_code |= LCR_PARITY_NONE;
} else if (strcmp(token, "ODD") == 0) {
lcr_code |= LCR_PARITY_ODD;
} else if (strcmp(token, "EVEN") == 0) {
lcr_code |= LCR_PARITY_EVEN;
} else if (strcmp(token, "MARK") == 0) {
lcr_code |= LCR_PARITY_MARK;
} else if (strcmp(token, "SPACE") == 0) {
lcr_code |= LCR_PARITY_SPACE;
} else {
log(LOG_ERROR, "uart_open: Bad parity");
return ERR_BAD_ARGUMENT;
}
// Set word length, stop bit size, and parity
uart_setlcr(cdev_to_uart(chan->dev), lcr_code);
return 0;
} else {
log(LOG_ERROR, "uart_open: no parity token");
return ERR_BAD_ARGUMENT;
}
} else {
log(LOG_ERROR, "uart_open: no stop bit token");
return ERR_BAD_ARGUMENT;
}
} else {
log(LOG_ERROR, "uart_open: no data length token");
return ERR_BAD_ARGUMENT;
}
} else {
log(LOG_ERROR, "uart_open: no BPS token");
return ERR_BAD_ARGUMENT;
}
return 0;
}
/*
* Read a single char from the UART
*
* @param channel the number of the channel
* @return the value read (if negative, error)
*/
short uart_read_b(p_channel chan) {
return uart_get(cdev_to_uart(chan->dev));
}
/**
* Read bytes from the UART
*
* @param channel the number of the channel
* @param buffer the buffer into which to copy the channel data
* @param size the size of the buffer.
* @return number of bytes read, any negative number is an error code
*/
short uart_read(p_channel chan, char * buffer, short size) {
short i = 0, count = 0;
for (i = 0; i < size; i++) {
buffer[i] = uart_get(cdev_to_uart(chan->dev));
count++;
}
return count;
}
/**
* Read a line from the UART
*
* @param channel the number of the channel
* @param buffer the buffer into which to copy the channel data
* @param size the size of the buffer.
* @returns number of bytes read, any negative number is an error code
*/
short uart_readline(p_channel chan, char * buffer, short size) {
short i = 0, count = 0;
for (i = 0; i < size; i++) {
char c = uart_get(cdev_to_uart(chan->dev));
if ((c == '\n') || (c == '\r')) {
break;
}
buffer[i] = c;
count++;
}
return count;
}
/**
* Write a single unsigned char to the UART
*
* @param channel the number of the channel
* @param b the unsigned char to write
* @return 0 on success, a negative value on error
*/
short uart_write_b(p_channel chan, unsigned char c) {
uart_put(cdev_to_uart(chan->dev), c);
return 0;
}
/**
* Write a bytes to the UART
*
* @param channel the number of the channel
* @param buffer the buffer containing the data to write
* @param size the size of the buffer.
* @return number of bytes written, any negative number is an error code
*/
short uart_write(p_channel chan, const char * buffer, short size) {
int i;
for (i = 0; i < size; i++) {
uart_put(cdev_to_uart(chan->dev), buffer[i]);
}
return 0;
}
/**
* Install the UART driver
*/
short uart_install() {
t_dev_chan dev;
short result = 0;
dev.name = "COM1";
dev.number = CDEV_COM1;
dev.init = 0;
dev.open = uart_open;
dev.close = 0;
dev.read = uart_read;
dev.readline = uart_readline;
dev.read_b = uart_read_b;
dev.write = uart_write;
dev.write_b = uart_write_b;
dev.flush = 0;
dev.seek = 0;
dev.status = uart_status;
dev.ioctrl = 0;
result = cdev_register(&dev);
if (result) {
return result;
}
dev.name = "COM2";
dev.number = CDEV_COM2;
result = cdev_register(&dev);
if (result) {
return result;
}
}

View file

@ -68,4 +68,10 @@ extern void uart_put(short uart, unsigned char b);
* the byte read from the UART * the byte read from the UART
*/ */
extern unsigned char uart_get(short uart); extern unsigned char uart_get(short uart);
/**
* Install the UART driver
*/
extern short uart_install();
#endif #endif

View file

@ -26,19 +26,19 @@
#include "dev/console.h" #include "dev/console.h"
#include "dev/fdc.h" #include "dev/fdc.h"
#include "dev/lpt.h" #include "dev/lpt.h"
#include "dev/txt_screen.h"
#include "dev/txt_a2560k_a.h"
#include "dev/txt_a2560k_b.h"
#include "dev/pata.h" #include "dev/pata.h"
#include "dev/ps2.h" #include "dev/ps2.h"
#include "dev/rtc.h" #include "dev/rtc.h"
#include "dev/sdc.h" #include "dev/sdc.h"
#include "dev/txt_screen.h"
#include "dev/txt_a2560k_a.h"
#include "dev/txt_a2560k_b.h"
#include "dev/uart.h" #include "dev/uart.h"
#include "vicky_general.h"
#include "snd/codec.h" #include "snd/codec.h"
#include "snd/psg.h" #include "snd/psg.h"
#include "snd/sid.h" #include "snd/sid.h"
#include "snd/yamaha.h" #include "snd/yamaha.h"
#include "vicky_general.h"
#include "fatfs/ff.h" #include "fatfs/ff.h"
#include "cli/cli.h" #include "cli/cli.h"
#include "rsrc/font/MSX_CP437_8x8.h" #include "rsrc/font/MSX_CP437_8x8.h"
@ -232,6 +232,12 @@ void initialize() {
} }
#endif #endif
if (res = uart_install()) {
log_num(LOG_ERROR, "FAILED: serial port initialization", res);
} else {
log(LOG_INFO, "Serial ports initialized.");
}
if (res = cli_init()) { if (res = cli_init()) {
log_num(LOG_ERROR, "FAILED: CLI initialization", res); log_num(LOG_ERROR, "FAILED: CLI initialization", res);
} else { } else {

View file

@ -46,5 +46,6 @@
#define FSYS_ERR_INVALID_PARAMETER -36 /* (19) Given parameter is invalid */ #define FSYS_ERR_INVALID_PARAMETER -36 /* (19) Given parameter is invalid */
#define ERR_NOT_SUPPORTED -37 /* Device does not support the file or operation */ #define ERR_NOT_SUPPORTED -37 /* Device does not support the file or operation */
#define ERR_BAD_ARGUMENT -38 /* An invalid argument was provided */
#endif #endif

View file

@ -1,19 +0,0 @@
/**
* @file utilities.h
*
* Define some handy macros and utility functions
*/
#ifndef __UTILITIES_H
#define __UTILITIES_H
/** Return the minimum value of x or y */
#define min(x, y) ((x < y) ? x : y)
/** Return the maximum value of x or y */
#define max(x, y) ((x < y) ? y : x)
/** Return the absolute value of x */
#define abs(x) ((x >= 0) ? x : 0 - x)
#endif

View file

@ -61,7 +61,8 @@ const char * err_messages[] = {
"not enough core", "not enough core",
"too many open files", "too many open files",
"file system invalid parameter", "file system invalid parameter",
"not supported" "not supported",
"bad argument"
}; };
/* /*
@ -70,7 +71,7 @@ const char * err_messages[] = {
const char * err_message(short err_number) { const char * err_message(short err_number) {
short index = 0 - err_number; short index = 0 - err_number;
if (index < 38) { if (index < 39) {
return err_messages[index]; return err_messages[index];
} else { } else {
return "unknown error"; return "unknown error";

19190
src/mapfile

File diff suppressed because it is too large Load diff

45
src/utilities.c Normal file
View file

@ -0,0 +1,45 @@
/**
* @file utilities.c
*
* Define some handy macros and utility functions
*/
#include <utilities.h>
/**
* Re-entrant version of strtok_r, because VBCC does not provide it
*
* @param source the string to tokenize
* @param delimiter the string containing the delimiter
* @param saveptr a pointer to the character pointer to use to save the state of the tokenizer
* @return the pointer to the next token, 0 if none
*/
char * strtok_r(char * source, const char * delimiter, char ** saveptr) {
char * x = *saveptr;
char * y;
/* Skip over leading delimiters */
for (x = *saveptr; *x && (*x == delimiter[0]); x++) {
}
/* If we reached the end of the string, return NULL */
if (*x == 0) {
return 0;
}
for (y = x; *y && (*y != delimiter[0]); y++) {
}
/* If we reached the end of the string, return x */
if (*y == 0) {
*saveptr = y;
return x;
}
/* Otherwise, make that position in the source string NULL, and return x */
*y = 0;
*saveptr = y + 1;
return x;
}

29
src/utilities.h Normal file
View file

@ -0,0 +1,29 @@
/**
* @file utilities.h
*
* Define some handy macros and utility functions
*/
#ifndef __UTILITIES_H
#define __UTILITIES_H
/** Return the minimum value of x or y */
#define min(x, y) ((x < y) ? x : y)
/** Return the maximum value of x or y */
#define max(x, y) ((x < y) ? y : x)
/** Return the absolute value of x */
#define abs(x) ((x >= 0) ? x : 0 - x)
/**
* Re-entrant version of strtok_r, because VBCC does not provide it
*
* @param source the string to tokenize
* @param delimiter the string containing the delimiter
* @param saveptr a pointer to the character pointer to use to save the state of the tokenizer
* @return the pointer to the next token, 0 if none
*/
extern char * strtok_r(char * source, const char * delimiter, char ** saveptr);
#endif

View file

@ -7,6 +7,6 @@
#define VER_MAJOR 0 #define VER_MAJOR 0
#define VER_MINOR 52 #define VER_MINOR 52
#define VER_BUILD 30 #define VER_BUILD 34
#endif #endif