# HG changeset patch # User Paul Boddie # Date 1541793863 -3600 # Node ID 839de4f15476a3be250f12d9170d192c3b83e3d4 # Parent f40a40d7101fd8e186115b9557aca8553e5e2562# Parent 14c1529d2eb6d67a93f62048a66a20fe44d0b3c3 Merged changes involving absent files. diff -r f40a40d7101f -r 839de4f15476 include/utils.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/include/utils.h Fri Nov 09 21:04:23 2018 +0100 @@ -0,0 +1,31 @@ +/* + * Some simple utility functions. + * + * Copyright (C) 2018 Paul Boddie + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef __UTILS_H__ +#define __UTILS_H__ + +#include + +void wait(uint32_t delay); + +uint8_t *wrap_pointer(uint8_t *ptr, uint8_t *lower, uint8_t *upper); + +int wrap_value(int value, int limit); + +#endif /* __UTILS_H__ */ diff -r f40a40d7101f -r 839de4f15476 lib/utils.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/lib/utils.c Fri Nov 09 21:04:23 2018 +0100 @@ -0,0 +1,58 @@ +/* + * Some simple utility functions. + * + * Copyright (C) 2018 Paul Boddie + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#include "utils.h" + + + +/* A simple busy wait involving a register-based counter. */ + +void wait(uint32_t delay) +{ + uint32_t counter = delay; + + if (!delay) return; + while (counter--) __asm__(""); /* retain loop */ +} + +/* Wrap a pointer within the given limits. */ + +uint8_t *wrap_pointer(uint8_t *ptr, uint8_t *lower, uint8_t *upper) +{ + uint32_t size = upper - lower; + + if (ptr < lower) + return upper - (lower - ptr) % size; + else if (ptr >= upper) + return lower + (ptr - upper) % size; + else + return ptr; +} + +/* Wrap a value within the bounds [0, limit). */ + +int wrap_value(int value, int limit) +{ + if (value < 0) + return limit - (-value % limit); + else if (value >= limit) + return (value - limit) % limit; + else + return value; +}