2016-02-29 22:50:29 +01:00
|
|
|
// SYSTEM BUS RADIO
|
|
|
|
// https://github.com/fulldecent/system-bus-radio
|
|
|
|
// Copyright 2016 William Entriken
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <emmintrin.h>
|
2016-03-01 20:27:35 +01:00
|
|
|
#include <inttypes.h>
|
|
|
|
#include <time.h>
|
|
|
|
#ifdef __MACH__
|
2016-02-29 22:50:29 +01:00
|
|
|
#include <mach/mach_traps.h>
|
|
|
|
#include <mach/mach_time.h>
|
2016-03-01 20:27:35 +01:00
|
|
|
#endif
|
2016-02-29 22:50:29 +01:00
|
|
|
#include <math.h>
|
|
|
|
|
2016-03-01 20:27:35 +01:00
|
|
|
#ifndef NSEC_PER_SEC
|
|
|
|
#define NSEC_PER_SEC 1000000000ull
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef __MACH__
|
2016-03-02 18:02:36 +01:00
|
|
|
#define TIME_ABSOLUTE CLOCK_REALTIME
|
2016-03-01 20:27:35 +01:00
|
|
|
typedef struct timespec mach_timespec_t;
|
|
|
|
typedef unsigned int mach_port_t;
|
|
|
|
|
|
|
|
static inline uint64_t mach_absolute_time(void) {
|
|
|
|
mach_timespec_t tp;
|
2016-03-02 18:02:36 +01:00
|
|
|
int res = clock_gettime(CLOCK_REALTIME, &tp);
|
2016-03-01 20:27:35 +01:00
|
|
|
if (res < 0) {
|
|
|
|
perror("clock_gettime");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
uint64_t result = tp.tv_sec * NSEC_PER_SEC;
|
|
|
|
result += tp.tv_nsec;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2016-03-02 18:02:36 +01:00
|
|
|
// non-conformant wrapper just for the purposes of this application
|
|
|
|
static inline void clock_sleep_trap(mach_port_t clock_port, int sleep_type, time_t sec, long nsec, mach_timespec_t *remain) {
|
2016-03-01 20:27:35 +01:00
|
|
|
mach_timespec_t req = { sec, nsec };
|
2016-03-02 18:02:36 +01:00
|
|
|
int res = clock_nanosleep(sleep_type, TIMER_ABSTIME, &req, remain);
|
2016-03-01 20:27:35 +01:00
|
|
|
if (res < 0) {
|
2016-03-02 18:02:36 +01:00
|
|
|
perror("clock_nanosleep");
|
2016-03-01 20:27:35 +01:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // __MACH__
|
|
|
|
|
2016-02-29 22:50:29 +01:00
|
|
|
__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) {
|
2016-03-01 22:48:29 +01:00
|
|
|
printf("Playing / %0.3f seconds / %4.0f Hz\n", time, frequency);
|
2016-03-01 17:38:10 +01:00
|
|
|
uint64_t period = NSEC_PER_SEC / frequency;
|
2016-02-29 22:50:29 +01:00
|
|
|
|
2016-03-01 17:38:10 +01:00
|
|
|
uint64_t start = mach_absolute_time();
|
|
|
|
uint64_t end = start + time * NSEC_PER_SEC;
|
2016-02-29 22:50:29 +01:00
|
|
|
|
2016-03-01 17:38:10 +01:00
|
|
|
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;
|
2016-02-29 22:50:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int main()
|
|
|
|
{
|
2016-03-01 20:27:35 +01:00
|
|
|
#ifdef __MACH__
|
2016-03-01 17:38:10 +01:00
|
|
|
mach_timebase_info_data_t theTimeBaseInfo;
|
|
|
|
mach_timebase_info(&theTimeBaseInfo);
|
|
|
|
puts("TESTING TIME BASE: the following should be 1 / 1");
|
2016-03-01 22:49:14 +01:00
|
|
|
printf(" Mach base: %u / %u nanoseconds\n\n", theTimeBaseInfo.numer, theTimeBaseInfo.denom);
|
2016-03-01 20:27:35 +01:00
|
|
|
#endif
|
2016-02-29 22:50:29 +01:00
|
|
|
|
2016-03-01 17:38:10 +01:00
|
|
|
uint64_t start = mach_absolute_time();
|
|
|
|
uint64_t end = mach_absolute_time();
|
2016-03-01 20:29:27 +01:00
|
|
|
printf("TESTING TIME TO EXECUTE mach_absolute_time()\n Result: %"PRIu64" nanoseconds\n\n", end - start);
|
2016-02-29 22:50:29 +01:00
|
|
|
|
2016-03-01 17:38:10 +01:00
|
|
|
reg_zero = _mm_set_epi32(0, 0, 0, 0);
|
|
|
|
reg_one = _mm_set_epi32(-1, -1, -1, -1);
|
2016-02-29 22:50:29 +01:00
|
|
|
|
2016-03-01 17:38:10 +01:00
|
|
|
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);
|
|
|
|
}
|
2016-02-29 22:50:29 +01:00
|
|
|
}
|