about summary refs log tree commit diff stats
path: root/tools/iso/kernel.soso/random.c
blob: c78501d8e1755e9b215d656f5a6ef97d05c5a594 (plain) (blame)
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
#include "random.h"
#include "devfs.h"
#include "device.h"
#include "common.h"
#include "process.h"
#include "sleep.h"

static BOOL random_open(File *file, uint32 flags);
static int32 random_read(File *file, uint32 size, uint8 *buffer);

void initializeRandom() {
    Device device;
    memset((uint8*)&device, 0, sizeof(Device));
    strcpy(device.name, "random");
    device.deviceType = FT_CharacterDevice;
    device.open = random_open;
    device.read = random_read;

    registerDevice(&device);
}

static BOOL random_open(File *file, uint32 flags) {
    return TRUE;
}

static int32 random_read(File *file, uint32 size, uint8 *buffer) {
    if (size == 0) {
        return 0;
    }

    //printkf("random_read: calling sleep\n");

    //sleep(10000);

    //printkf("random_read: returned from sleep\n");

    uint32 number = rand();

    if (size == 1) {
        *buffer = (uint8)number;
        return 1;
    }
    else if (size == 2 || size == 3) {
        *((uint16*)buffer) = (uint16)number;
        return 2;
    }
    else if (size >= 4) {
        //printkf("random_read: buffer is %x, writing %x to buffer\n", buffer, number);

        *((uint32*)buffer) = number;
        return 4;
    }

    return 0;
}