genraminit.c 874 Bytes
Newer Older
1 2 3
/*
 * This work is part of the White Rabbit project
 *
4 5
 * Copyright (C) 2017 CERN (www.cern.ch)
 * Author: Grzegorz Daniluk <grzegorz.daniluk@cern.ch>
6 7 8
 *
 * Released according to the GNU GPL, version 2 or any later version.
 */
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
9 10
#include <stdio.h>
#include <stdlib.h>
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
#include <string.h>
#include <arpa/inet.h>

const char *byte_to_binary(int x)
{
	unsigned int z;
	static char b[33];
	b[0] = '\0';

	for (z = 0x80000000; z > 0; z >>= 1)
	{
		strcat(b, ((x & z) == z) ? "1" : "0");
	}

	return b;
}

Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
28

29
int main(int argc, char *argv[])
Tomasz Wlostowski's avatar
Tomasz Wlostowski committed
30
{
31 32 33
	FILE *f = fopen(argv[1], "rb");
	if (!f)
		return -1;
34
	int tmp;
35
	int i = 0;
36
	int ram_size = atoi(argv[2]);
37

38
	ram_size = (ram_size+3)/4;
39

40 41 42 43 44 45 46
	while (fread(&tmp, 1, 4, f)) {
		printf("%s\n", byte_to_binary(htonl(tmp)));
		++i;
	}
	//padding
	for(;i<ram_size; ++i) {
		printf("00000000000000000000000000000000\n");
47
	}
48

49 50
	fclose(f);
	return 0;
51
}