blob: 2de8f6e6f7508048f1cd5fd32f57b286402cc3c7 (
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
56
57
58
59
60
61
62
63
64
65
|
/* SPDX-License-Identifier: copyleft-next-0.3.1 */
/* Copyright 2023 Kim Kuparinen < kimi.h.kuparinen@gmail.com > */
#include <stdlib.h>
#include <assert.h>
#include <gran/component.h>
#include "traffic_gen.h"
struct traffic_gen {
struct component component;
struct component *stress;
uintptr_t addr;
uintptr_t end;
size_t counter;
};
static stat traffic_gen_clock(struct traffic_gen *tg)
{
if (tg->addr == tg->end)
return DONE;
uint8_t c = 0;
switch (tg->counter) {
case 0:
assert(write_u8(tg->stress, tg->addr, 13) == OK);
tg->counter = 1;
break;
case 1:
c = 0;
assert(read_u8(tg->stress, tg->addr, &c) == OK);
assert(c == 13);
tg->counter = 0;
tg->addr++;
break;
}
return OK;
}
void traffic_gen_destroy(struct traffic_gen *tg)
{
destroy(tg->stress);
free(tg);
}
struct component *create_traffic_gen(struct component *stress, uintptr_t start,
size_t size)
{
struct traffic_gen *new = calloc(1, sizeof(struct traffic_gen));
if (!new)
return NULL;
new->stress = stress;
new->addr = start;
new->end = start + size;
new->counter = 0;
new->component.clock = (clock_callback)traffic_gen_clock;
new->component.destroy = (destroy_callback)traffic_gen_destroy;
return (struct component *)new;
}
|