view hr-sid/sidgen.c @ 57:a64e5ed217c0 default tip

top Makefile: add fr-sid
author Mychaela Falconia <falcon@freecalypso.org>
date Thu, 22 Aug 2024 06:43:31 +0000
parents 5c82f2e56d56
children
line wrap: on
line source

/*
 * This program generates a sequence of GSM-HR codec frames in raw packed
 * format (aka TS 101 318) that aims to reverse-engineer the logic of
 * the SID detector in TI Calypso DSP, and maybe later in historical
 * E1 BTS implementations too.
 */

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>

static const uint8_t perfect_sid[14] = {
	0x11, 0x22, 0x33, 0x44, 0xFF, 0xFF, 0xFF,
	0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF
};

static void
clear_bit(buf, num)
	uint8_t *buf;
	unsigned num;
{
	unsigned nbyte = num / 8;
	unsigned nbit = num % 8;
	unsigned mask = 0x80 >> nbit;

	buf[nbyte] &= ~mask;
}

static void
corrupt_class1_bit(buf, num)
	uint8_t *buf;
	unsigned num;
{
	clear_bit(buf, num + 36);
}

static void
corrupt_class2_bit(buf, num)
	uint8_t *buf;
	unsigned num;
{
	if (num < 8)
		clear_bit(buf, num + 81);
	else
		clear_bit(buf, (num - 8) + 98);
}

main(argc, argv)
	char **argv;
{
	FILE *outf;
	uint8_t class1[14], class2[14];
	unsigned n1, n2;

	if (argc != 2) {
		fprintf(stderr, "usage: %s outfile\n", argv[0]);
		exit(1);
	}
	outf = fopen(argv[1], "w");
	if (!outf) {
		perror(argv[1]);
		exit(1);
	}
	bcopy(perfect_sid, class1, 14);
	for (n1 = 0; n1 < 20; n1++) {
		bcopy(class1, class2, 14);
		for (n2 = 0; n2 < 17; n2++) {
			fwrite(class2, 1, 14, outf);
			corrupt_class2_bit(class2, n2);
		}
		fwrite(class2, 1, 14, outf);
		corrupt_class1_bit(class1, n1);
	}
	fclose(outf);
	exit(0);
}