# TrueRNG C Implementation

This is a C equivalent of the Python `main.py` script for reading data from TrueRNG devices.

## Overview

The program reads random data from a TrueRNG device via serial port, counts the number of ones and zeros in the data, and saves the raw data to a binary file called `random.bin`.

## Supported Devices

- TrueRNG (PID: 04D8, HID: F5FE)
- TrueRNGpro (PID: 16D0, HID: 0AA0)  
- TrueRNGproV2 (PID: 04D8, HID: EBB5)

## Requirements

- Linux system with termios support
- GCC compiler
- TrueRNG device connected via USB
- Appropriate permissions to access the serial device (may need root or proper udev rules)

## Building

```bash
make
```

Or manually:
```bash
gcc -Wall -Wextra -std=c99 -O2 -o truerng main.c
```

## Usage

```bash
./truerng
```

The program will:
1. Automatically detect connected TrueRNG devices
2. Read 1024 blocks of 1024 bytes each (1MB total)
3. Count ones and zeros in real-time
4. Display read rate in KB/s
5. Save raw data to `random.bin`
6. Display final statistics

## Configuration

You can modify the following constants in [`main.c`](main.c:32):

- `BLOCKSIZE`: Number of bytes to read per block (default: 1024)
- `NUMLOOPS`: Number of blocks to read (default: 1024)

## Permissions

On Linux, you may need to set proper permissions for the serial device:

```bash
sudo chmod 666 /dev/ttyUSB0  # Replace with your device
```

Or add your user to the dialout group:
```bash
sudo usermod -a -G dialout $USER
```

## Differences from Python Version

The C implementation:
- Uses native Linux serial port APIs instead of pyserial
- Implements USB device detection via sysfs
- Uses lookup tables for efficient bit counting
- Provides equivalent functionality with better performance

## Output

Sample output:
```
TrueRNG Counting Ones vs Zeros Example
http://ubld.it
==================================================
TrueRNG Found
Using com port:  /dev/ttyUSB0
Block Size:      1024 Bytes
Number of loops: 1024
Total size:      1048576 Bytes
Writing to:      random.bin
==================================================
1048576 Bytes Read at  45.23 Kbytes/s

Results
=======
Total Ones : 4194234
Total Zeros: 4194314

Total Bits : 8388608

There are 80 more zeros in the Capture!
=======