view ffstools/caltools/fc-rftab2c.c @ 465:003e48f8ebe1

rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel I generally don't use NULL and use plain 0 instead, based on a "NULL considered harmful" discussion on the classiccmp mailing list many aeons ago (I couldn't find it, and I reason that it must have been 2005 or earlier), but a recent complaint by a packager sent me searching, and I found this: https://ewontfix.com/11/ While I don't give a @#$% about "modern" systems and code-nazi tools, I realized that passing a plain 0 as a pointer sentinel in execl is wrong because it will break on systems where pointers are longer than the plain int type. Again, I don't give a @#$% about the abomination of x86_64 and the like, but if anyone ever manages to port my code to something like a PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0 as a function argument where a pointer is expected most definitely won't work: if the most natural stack slot and SP alignment unit is 16 bits, fitting an int, with longs and pointers taking up two such slots, then the call stack will be totally wrong with a plain 0 passed for a pointer. Casting the 0 to (char *) ought to be the most kosher solution for the most retro systems possible.
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Feb 2019 00:00:19 +0000
parents f4a32c1025a2
children 5bcf12be0834
line wrap: on
line source

/*
 * This utility reads an RF parameter table of one of the supported types
 * in FreeCalypso ASCII format (it has to be one of the tables that go
 * into the T_RF_BAND structure) and converts it into a C code snippet
 * suitable for insertion into the firmware source in the L1 RF "customization"
 * code where compiled-in default RF parameter tables are defined.
 *
 * This tool is primarily intended for use with tx-ramps tables and maybe
 * tx-levels, but it also supports tx-calchan, tx-caltemp, rx-agc-params,
 * rx-calchan and rx-caltemp tables.
 *
 * This program is based on the calextract tool from 2014 (freecalypso-reveng
 * repository) and the generated C code snippets feature the same style,
 * indentation and comments.
 */

#include <sys/types.h>
#include <stdio.h>
#include <stdint.h>
#include <endian.h>
#include <stdlib.h>

u_char binbuf[512];

static unsigned
get_u16(bin)
	u_char *bin;
{
	return le16toh(*(uint16_t *)bin);
}

static int
get_s16(bin)
	u_char *bin;
{
	int i;

	i = le16toh(*(uint16_t *)bin);
	if (i >= 32768)
		i -= 65536;
	return(i);
}

void
do_rx_cal_params()
{
	u_char *bp = binbuf;
	int i;

	puts("    { /* T_RX_CAL_PARAMS */");
	for (i = 0; i < 4; i++) {
		printf("%10u,\n", get_u16(bp));
		bp += 2;
	}
	puts("    },");
}

void
do_rx_agc_bands()
{
	u_char *bp = binbuf;
	int i, s;
	unsigned u;

	puts("    { /* T_RF_AGC_BANDs */");
	for (i = 0; i < 10; i++) {
		u = get_u16(bp);
		bp += 2;
		s = get_s16(bp);
		bp += 2;
		printf("      {%5u,%6d},\n", u, s);
	}
	puts("    },");
}

void
do_rx_temp_comp()
{
	u_char *bp = binbuf;
	int i, s1, s2;

	puts("    { /* Rx temperature compensation */");
	for (i = 0; i < 11; i++) {
		s1 = get_s16(bp);
		bp += 2;
		s2 = get_s16(bp);
		bp += 2;
		printf("      {%6d,%6d},\n", s1, s2);
	}
	puts("    },");
}

void
do_tx_levels()
{
	u_char *bp = binbuf;
	unsigned i, u, b1, b2;

	puts("    { /* levels */");
	for (i = 0; i < 32; i++) {
		u = get_u16(bp);
		bp += 2;
		b1 = *bp++;
		b2 = *bp++;
		printf("      {%5u,%3u,%3u}, /* %u */\n", u, b1, b2, i);
	}
	puts("    },");
}

void
do_tx_calchan()
{
	u_char *bp = binbuf;
	int i, j, s;
	unsigned u;

	puts("    { /* channel calibration tables */");
	for (i = 0; i < 4; i++) {
		printf("      { /* calibration table %d */\n", i);
		for (j = 0; j < 8; j++) {
			u = get_u16(bp);
			bp += 2;
			s = get_s16(bp);
			bp += 2;
			printf("\t{%5u,%6d},\n", u, s);
		}
		puts("      },");
	}
	puts("    },");
}

static void
do_ramp_16bytes(bin)
	u_char *bin;
{
	u_char *bp = bin;
	int i, b;

	putchar('\t');
	putchar('{');
	for (i = 0; i < 16; i++) {
		b = *bp++;
		printf("%3d%c", b, i == 15 ? '}' : ',');
	}
	putchar(',');
	putchar('\n');
}

void
do_tx_ramps()
{
	u_char *bp = binbuf;
	int i;

	puts("    { /* ramps */");
	for (i = 0; i < 16; i++) {
		printf("      { /* profile %d */\n", i);
		puts("\t/* ramp-up */");
		do_ramp_16bytes(bp);
		bp += 16;
		puts("\t/* ramp-down */");
		do_ramp_16bytes(bp);
		bp += 16;
		puts("      },");
	}
	puts("    },");
}

void
do_tx_temp_comp()
{
	u_char *bp = binbuf;
	int i, j, s[4];

	puts("    { /* Tx temperature compensation */");
	for (i = 0; i < 5; i++) {
		for (j = 0; j < 4; j++) {
			s[j] = get_s16(bp);
			bp += 2;
		}
		printf("      {%6d,%6d,%6d,%6d},\n", s[0], s[1], s[2], s[3]);
	}
	puts("    },");
}

static struct map {
	char	*format;
	void	(*func)();
} map_table[] = {
	{"tx-ramps",		do_tx_ramps},
	{"tx-levels",		do_tx_levels},
	{"tx-calchan",		do_tx_calchan},
	{"tx-caltemp",		do_tx_temp_comp},
	{"rx-calchan",		do_rx_agc_bands},
	{"rx-caltemp",		do_rx_temp_comp},
	{"rx-agc-params",	do_rx_cal_params},
	{0,			0}
};

main(argc, argv)
	char **argv;
{
	char *format;
	struct map *map;

	if (argc < 2 || argc > 3) {
		fprintf(stderr, "usage: %s ascii-rftab-file [C-output-file]\n",
			argv[0]);
		exit(1);
	}
	if (read_rf_table_ext(argv[1], binbuf, 1, &format, 0))
		exit(1);
	for (map = map_table; map->format; map++)
		if (!strcmp(map->format, format))
			break;
	if (!map->func) {
		printf("error: %s tables are not supported\n", format);
		exit(1);
	}
	if (argc >= 3 && !freopen(argv[2], "w", stdout)) {
		perror(argv[2]);
		exit(1);
	}
	map->func();
	exit(0);
}