From 34e38292e12969d2268c0d2834641741023f8177 Mon Sep 17 00:00:00 2001 From: schodet Date: Sat, 6 Aug 2005 11:58:43 +0000 Subject: Ajout d'un générateur de nombres aléatoires. --- n/avr/modules/math/random/Makefile.module | 1 + n/avr/modules/math/random/README | 26 +++++++++ n/avr/modules/math/random/random.h | 33 +++++++++++ n/avr/modules/math/random/test/Makefile | 17 ++++++ n/avr/modules/math/random/test/avrconfig.h | 85 +++++++++++++++++++++++++++ n/avr/modules/math/random/test/test_random.c | 44 ++++++++++++++ n/avr/modules/math/random/tt800.c | 86 ++++++++++++++++++++++++++++ 7 files changed, 292 insertions(+) create mode 100644 n/avr/modules/math/random/Makefile.module create mode 100644 n/avr/modules/math/random/README create mode 100644 n/avr/modules/math/random/random.h create mode 100644 n/avr/modules/math/random/test/Makefile create mode 100644 n/avr/modules/math/random/test/avrconfig.h create mode 100644 n/avr/modules/math/random/test/test_random.c create mode 100644 n/avr/modules/math/random/tt800.c (limited to 'n/avr') diff --git a/n/avr/modules/math/random/Makefile.module b/n/avr/modules/math/random/Makefile.module new file mode 100644 index 0000000..491dd63 --- /dev/null +++ b/n/avr/modules/math/random/Makefile.module @@ -0,0 +1 @@ +math_random_SOURCES = tt800.c diff --git a/n/avr/modules/math/random/README b/n/avr/modules/math/random/README new file mode 100644 index 0000000..1e8e07a --- /dev/null +++ b/n/avr/modules/math/random/README @@ -0,0 +1,26 @@ +avr.math.random - Pseudo random numbers generator. + +Provide RNG for AVR, using the TT800 algorithm. See modules README for more +details about AVR modules. + +The TT800 has his own copyright notice, see tt800.c file. + +Copyright (C) 2005 Nicolas Schodet + +Robot APB Team/Efrei 2006. + Web: http://assos.efrei.fr/robot/ + Email: robot AT efrei DOT fr + +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 2 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, write to the Free Software +Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. diff --git a/n/avr/modules/math/random/random.h b/n/avr/modules/math/random/random.h new file mode 100644 index 0000000..5822cfd --- /dev/null +++ b/n/avr/modules/math/random/random.h @@ -0,0 +1,33 @@ +#ifndef random_h +#define random_h +/* random.h */ +/* avr.math.random - Pseudo random numbers generator. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * Web: http://assos.efrei.fr/robot/ + * Email: robot AT efrei DOT fr + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +/** Generate a 32 bit pseudo random number. Sequences are deterministic and + * same numbers are drawn on each runs as the seed is always the same. */ +uint32_t +random_u32 (void); + +#endif /* random_h */ diff --git a/n/avr/modules/math/random/test/Makefile b/n/avr/modules/math/random/test/Makefile new file mode 100644 index 0000000..354961c --- /dev/null +++ b/n/avr/modules/math/random/test/Makefile @@ -0,0 +1,17 @@ +BASE = ../../../.. +PROGS = test_random +test_random_SOURCES = test_random.c +DOC = +EXTRACTDOC = +MODULES = uart proto math/random +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +AVR_MCU = atmega8 +# -O2 : speed +# -Os : size +OPTIMIZE = -Os + +DEFS = +LIBS = + +include $(BASE)/make/Makefile.gen diff --git a/n/avr/modules/math/random/test/avrconfig.h b/n/avr/modules/math/random/test/avrconfig.h new file mode 100644 index 0000000..fa460aa --- /dev/null +++ b/n/avr/modules/math/random/test/avrconfig.h @@ -0,0 +1,85 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h */ +/* avr.math.random - Pseudo random numbers generator. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * Web: http://assos.efrei.fr/robot/ + * Email: robot AT efrei DOT fr + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ + +/* global */ +/** AVR Frequency : 1000000, 1843200, 2000000, 3686400, 4000000, 7372800, + * 8000000, 11059200, 14745600, 16000000, 18432000, 20000000. */ +#define AC_FREQ 14745600 + +/* uart - UART module. */ +/** Select hardware uart for primary uart: 0, 1 or -1 to disable. */ +#define AC_UART0_PORT 0 +/** Baudrate: 2400, 4800, 9600, 14400, 19200, 28800, 38400, 57600, 76800, + * 115200, 230400, 250000, 500000, 1000000. */ +#define AC_UART0_BAUDRATE 115200 +/** Send mode: + * - POLLING: no interrupts. + * - RING: interrupts, ring buffer. */ +#define AC_UART0_SEND_MODE RING +/** Recv mode, same as send mode. */ +#define AC_UART0_RECV_MODE RING +/** Character size: 5, 6, 7, 8, 9 (only 8 implemented). */ +#define AC_UART0_CHAR_SIZE 8 +/** Parity : ODD, EVEN, NONE. */ +#define AC_UART0_PARITY EVEN +/** Stop bits : 1, 2. */ +#define AC_UART0_STOP_BITS 1 +/** Send buffer size, should be power of 2 for RING mode. */ +#define AC_UART0_SEND_BUFFER_SIZE 32 +/** Recv buffer size, should be power of 2 for RING mode. */ +#define AC_UART0_RECV_BUFFER_SIZE 32 +/** If the send buffer is full when putc: + * - DROP: drop the new byte. + * - WAIT: wait until there is room in the send buffer. */ +#define AC_UART0_SEND_BUFFER_FULL WAIT +/** In HOST compilation: + * - STDIO: use stdin/out. + * - PTS: use pseudo terminal. */ +#define AC_UART0_HOST_DRIVER STDIO +/** Same thing for secondary port. */ +#define AC_UART1_PORT -1 +#define AC_UART1_BAUDRATE 115200 +#define AC_UART1_SEND_MODE RING +#define AC_UART1_RECV_MODE RING +#define AC_UART1_CHAR_SIZE 8 +#define AC_UART1_PARITY EVEN +#define AC_UART1_STOP_BITS 1 +#define AC_UART1_SEND_BUFFER_SIZE 32 +#define AC_UART1_RECV_BUFFER_SIZE 32 +#define AC_UART1_SEND_BUFFER_FULL WAIT + +/* proto - Protocol module. */ +/** Maximum argument size. */ +#define AC_PROTO_ARGS_MAX_SIZE 8 +/** Callback function name. */ +#define AC_PROTO_CALLBACK proto_callback +/** Putchar function name. */ +#define AC_PROTO_PUTC uart0_putc +/** Support for quote parameter. */ +#define AC_PROTO_QUOTE 0 + +#endif /* avrconfig_h */ diff --git a/n/avr/modules/math/random/test/test_random.c b/n/avr/modules/math/random/test/test_random.c new file mode 100644 index 0000000..c4d2b04 --- /dev/null +++ b/n/avr/modules/math/random/test/test_random.c @@ -0,0 +1,44 @@ +/* test_random.c */ +/* avr.math.random - Pseudo random numbers generator. {{{ + * + * Copyright (C) 2005 Nicolas Schodet + * + * Robot APB Team/Efrei 2006. + * Web: http://assos.efrei.fr/robot/ + * Email: robot AT efrei DOT fr + * + * 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 2 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, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + * + * }}} */ +#include "common.h" +#include "modules/math/random/random.h" +#include "modules/uart/uart.h" +#include "modules/proto/proto.h" +#include "io.h" + +void +proto_callback (uint8_t cmd, uint8_t size, uint8_t *args) +{ +} + +int +main (void) +{ + sei (); + uart0_init (); + proto_send0 ('z'); + while (42) + proto_send1d ('r', random_u32 ()); +} diff --git a/n/avr/modules/math/random/tt800.c b/n/avr/modules/math/random/tt800.c new file mode 100644 index 0000000..64675e8 --- /dev/null +++ b/n/avr/modules/math/random/tt800.c @@ -0,0 +1,86 @@ +/* A C-program for TT800 : July 8th 1996 Version + by M. Matsumoto, email: m-mat @ math.sci.hiroshima-u.ac.jp + + Modified for AVR modules by Nicolas Schodet. + + genrand() generate one pseudorandom number with double precision + which is uniformly distributed on [0,1]-interval + for each call. One may choose any initial 25 seeds + except all zeros. + + Copyright (C) 1996, Makoto Matsumoto + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions + are met: + + 1. Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + + 2. Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in the + documentation and/or other materials provided with the distribution. + + 3. The names of its contributors may not be used to endorse or promote + products derived from this software without specific prior written + permission. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR + CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR + PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF + LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING + NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + + See: ACM Transactions on Modelling and Computer Simulation, + Vol. 4, No. 3, 1994, pages 254-266. +*/ +#include "common.h" + +#define N 25 +#define M 7 + +uint32_t +random_u32 (void) +{ + uint32_t y; + static uint8_t k = 0; + static uint32_t x[N]={ /* initial 25 seeds, change as you wish */ + 0x95f24dab, 0x0b685215, 0xe76ccae7, 0xaf3ec239, 0x715fad23, + 0x24a590ad, 0x69e4b5ef, 0xbf456141, 0x96bc1b7b, 0xa7bdf825, + 0xc1de75b7, 0x8858a9c9, 0x2da87693, 0xb657f9dd, 0xffdc8a9f, + 0x8121da71, 0x8b823ecb, 0x885d05f5, 0x4e20cd47, 0x5a9ad5d9, + 0x512c0c03, 0xea857ccd, 0x4cc1d30f, 0x8891a8a1, 0xa6b7aadb + }; + static uint32_t mag01[2]={ + 0x0, 0x8ebfd028 /* this is magic vector `a', don't change */ + }; + if (k==N) { /* generate N words at one time */ + uint8_t kk; + for (kk=0;kk> 1) ^ mag01[x[kk] % 2]; + } + for (; kk> 1) ^ mag01[x[kk] % 2]; + } + k=0; + } + y = x[k]; + y ^= (y << 7) & 0x2b5b2500; /* s and b, magic vectors */ + y ^= (y << 15) & 0xdb8b0000; /* t and c, magic vectors */ + /*y &= 0xffffffff;*/ /* you may delete this line if word size = 32 */ +/* + the following line was added by Makoto Matsumoto in the 1996 version + to improve lower bit's corellation. + Delete this line to o use the code published in 1994. +*/ + y ^= (y >> 16); /* added to the 1994 version */ + k++; + return y; +} -- cgit v1.2.3