forked from fulldecent/system-bus-radio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
78 lines (69 loc) · 2.53 KB
/
main.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
// SYSTEM BUS RADIO
// https://github.com/fulldecent/system-bus-radio
// Copyright 2016 William Entriken
#include <stdio.h>
#include <emmintrin.h>
#include <mach/mach_traps.h>
#include <mach/mach_time.h>
#include <math.h>
__m128i reg;
__m128i reg_zero;
__m128i reg_one;
mach_port_t clock_port;
mach_timespec_t remain;
static inline void square_am_signal(float time, float frequency) {
printf("Playing / %0.3f seconds / %4.0f Hz\n", time, frequency);
uint64_t period = NSEC_PER_SEC / frequency;
uint64_t start = mach_absolute_time();
uint64_t end = start + time * NSEC_PER_SEC;
while (mach_absolute_time() < end) {
uint64_t mid = start + period / 2;
uint64_t reset = start + period;
while (mach_absolute_time() < mid) {
_mm_stream_si128(®, reg_one);
_mm_stream_si128(®, reg_zero);
}
clock_sleep_trap(clock_port, TIME_ABSOLUTE, reset / NSEC_PER_SEC, reset % NSEC_PER_SEC, &remain);
start = reset;
}
}
int main()
{
mach_timebase_info_data_t theTimeBaseInfo;
mach_timebase_info(&theTimeBaseInfo);
puts("TESTING TIME BASE: the following should be 1 / 1");
printf(" Mach base: %u / %u nanoseconds\n\n", theTimeBaseInfo.numer, theTimeBaseInfo.denom);
uint64_t start = mach_absolute_time();
uint64_t end = mach_absolute_time();
printf("TESTING TIME TO EXECUTE mach_absolute_time()\n Result: %lld nanoseconds\n\n", end - start);
reg_zero = _mm_set_epi32(0, 0, 0, 0);
reg_one = _mm_set_epi32(-1, -1, -1, -1);
while (1) {
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2093);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2673);
square_am_signal(0.790, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2349);
square_am_signal(0.790, 2349);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 3136);
square_am_signal(0.790, 3136);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2093);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2349);
square_am_signal(0.400, 2673);
square_am_signal(0.400, 2349);
square_am_signal(0.790, 2093);
}
}