summaryrefslogtreecommitdiffstats
path: root/apps/compare_to_value.c
blob: b6df9c03481ee89f7b1879d1f9270f28e36d8a61 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

int main(int argc, char *argv[]) {
    long i, j, size, num;
    size_t count = 0, total = 0;
    int offset = 0, toread = 1, toskip = 0;
    uint32_t value;
    uint32_t *buf;
    
    if ((argc != 4)&&(argc != 7)) {
	printf("Usage: %s <file> <dwords> <value> [offset_dwords read_dwords skip_dwords] \n", argv[0]);
	exit(0);
    }
    
    FILE *f = fopen(argv[1], "r");
    if (!f) {
	printf("Can't open %s\n", argv[1]);
	exit(1);
    }
    
    size = atol(argv[2]);
    if (size <= 0) {
	printf("Can't parse size %s\n", argv[2]);
	exit(1);
    }
    
    if (sscanf(argv[3], "%x", &value) != 1) {
	printf("Can't parse register %s\n", argv[3]);
	exit(1);
    }

    buf = malloc(size * sizeof(uint32_t));
    if (!buf) {
	printf("Can't allocate %lu bytes of memory\n", size * sizeof(uint32_t));
	exit(1);
    }
    
    if (argc == 7) {
	offset = atoi(argv[4]);
	toread = atoi(argv[5]);
	toskip = atoi(argv[6]);
    }
    

    num = fread(buf, 4, size, f);
    if (num != size) {
	printf("Only %lu of %lu dwords in the file\n", num, size);
    }
    fclose(f);
    
    for (i = offset; i < size; i += toskip) {
	for (j = 0; j < toread; j++, i++) {
	    total++;
	    if (buf[i] != value) {
		count++;
	    }
	}
    }
    free(buf);
    
    printf("%lu of %lu is wrong\n", count, total);

}