From ca09d9bf48908036161bcf46cd9834c276a9d249 Mon Sep 17 00:00:00 2001 From: schodet Date: Tue, 2 Aug 2005 22:09:17 +0000 Subject: Ajout de avr.math.fixed. Ajout de fixed_mul_f824, à tester. Ajout de fixed_div_f824, à tester. --- n/avr/modules/math/fixed/Makefile.module | 1 + n/avr/modules/math/fixed/README | 25 ++++ n/avr/modules/math/fixed/fixed.h | 36 ++++++ n/avr/modules/math/fixed/fixed_div_f824.avr.S | 162 ++++++++++++++++++++++++++ n/avr/modules/math/fixed/fixed_mul_f824.avr.S | 162 ++++++++++++++++++++++++++ n/avr/modules/math/fixed/test/Makefile | 17 +++ n/avr/modules/math/fixed/test/avrconfig.h | 85 ++++++++++++++ n/avr/modules/math/fixed/test/test_fixed.c | 44 +++++++ 8 files changed, 532 insertions(+) create mode 100644 n/avr/modules/math/fixed/Makefile.module create mode 100644 n/avr/modules/math/fixed/README create mode 100644 n/avr/modules/math/fixed/fixed.h create mode 100644 n/avr/modules/math/fixed/fixed_div_f824.avr.S create mode 100644 n/avr/modules/math/fixed/fixed_mul_f824.avr.S create mode 100644 n/avr/modules/math/fixed/test/Makefile create mode 100644 n/avr/modules/math/fixed/test/avrconfig.h create mode 100644 n/avr/modules/math/fixed/test/test_fixed.c (limited to 'n/avr/modules') diff --git a/n/avr/modules/math/fixed/Makefile.module b/n/avr/modules/math/fixed/Makefile.module new file mode 100644 index 0000000..91b409b --- /dev/null +++ b/n/avr/modules/math/fixed/Makefile.module @@ -0,0 +1 @@ +math_fixed_SOURCES = fixed_mul_f824.avr.S fixed_div_f824.avr.S diff --git a/n/avr/modules/math/fixed/README b/n/avr/modules/math/fixed/README new file mode 100644 index 0000000..639d4fc --- /dev/null +++ b/n/avr/modules/math/fixed/README @@ -0,0 +1,25 @@ +avr.math.fixed - Fixed point math module. + +Provide fixed point math function for AVR, and host simulation. See modules +README for more details about AVR modules. + + +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/fixed/fixed.h b/n/avr/modules/math/fixed/fixed.h new file mode 100644 index 0000000..ab20779 --- /dev/null +++ b/n/avr/modules/math/fixed/fixed.h @@ -0,0 +1,36 @@ +#ifndef fixed_h +#define fixed_h +/* fixed.h */ +/* avr.math.fixed - Fixed point math module. {{{ + * + * 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. + * + * }}} */ + +/** Multiply f8.24 by f8.24, return f8.24. */ +#define fixed_mul_f824(a, b) ({ \ + uint16_t dummy; \ + asm ("" : "=r" (dummy)); \ + fixed_mul_f824_asm (dummy, a, b); }) +int32_t +fixed_mul_f824_asm (uint16_t dummy, int32_t a, int32_t b); + +#endif /* fixed_h */ diff --git a/n/avr/modules/math/fixed/fixed_div_f824.avr.S b/n/avr/modules/math/fixed/fixed_div_f824.avr.S new file mode 100644 index 0000000..76992c3 --- /dev/null +++ b/n/avr/modules/math/fixed/fixed_div_f824.avr.S @@ -0,0 +1,162 @@ +; fixed_div_f824.avr.S +; avr.math.fixed - Fixed point math module. {{{ +; +; 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. +; +; }}} + +#define dd3 r25 +#define dd2 r24 +#define dd1 r23 +#define dd0 r22 + +#define dv3 r21 +#define dv2 r20 +#define dv1 r19 +#define dv0 r18 + +; r28-r29 are avoided because they should be saved. +#define rem3 r31 +#define rem2 r30 +#define rem1 r27 +#define rem0 r26 + +#define __tmp_reg__ r0 +#define __zero_reg__ r1 + +#define cnt r16 + + .section .text + .global fixed_div_f824 + .func fixed_div_f824 +fixed_div_f824: + push cnt +; Store sign. + mov __tmp_reg__, dd3 + eor __tmp_reg__, dv3 +; Change sign. + sbrs dd3, 7 + rjmp 1f + com dd3 + com dd2 + com dd1 + neg dd0 + sbci dd1, 0xff + sbci dd2, 0xff + sbci dd3, 0xff +1: sbrs dv3, 7 + rjmp 2f + com dv3 + com dv2 + com dv1 + neg dv0 + sbci dv1, 0xff + sbci dv2, 0xff + sbci dv3, 0xff +; Clear rem. +2: clr rem0 + clr rem1 + movw rem2, rem0 +; First loop, dropped bits. + ldi cnt, 24 +1: ;lsl dd0 ; shift out dd + lsl dd1 ; do not touch dd0 + rol dd2 + rol dd3 + rol rem0 ; shift in rem + rol rem1 ; 24 bits only + rol rem2 + ;rol rem3 + sub rem0, dv0 ; rem -= dv + sbc rem1, dv1 + sbc rem2, dv2 + sbc rem3, dv3 + brcc 2f ; if negative, restore rem + add rem0, dv0 + adc rem1, dv1 + adc rem2, dv2 + adc rem3, dv3 +2: dec cnt ; test for loop + brne 1b +; Second loop, stored bits. + ldi cnt, 8 +1: rol dd0 ; shift out dd, shift in result + rol rem0 ; shift in rem + rol rem1 + rol rem2 + rol rem3 + sub rem0, dv0 ; rem -= dv + sbc rem1, dv1 + sbc rem2, dv2 + sbc rem3, dv3 + brcc 2f ; if negative, restore rem + add rem0, dv0 + adc rem1, dv1 + adc rem2, dv2 + adc rem3, dv3 + clc ; result bit 0 + rjmp 3f +2: sec ; result bit 1 +3: dec cnt ; test for loop + brne 1b +; Last loop, stored bits, dd padding bits. + ldi cnt, 24 +1: rol dd0 ; shift out dd, shift in result + rol dd1 ; 0s come from the first loop + rol dd2 + rol dd3 + rol rem0 ; shift in rem + rol rem1 + rol rem2 + rol rem3 + sub rem0, dv0 ; rem -= dv + sbc rem1, dv1 + sbc rem2, dv2 + sbc rem3, dv3 + brcc 2f ; if negative, restore rem + add rem0, dv0 + adc rem1, dv1 + adc rem2, dv2 + adc rem3, dv3 + clc ; result bit 0 + rjmp 3f +2: sec ; result bit 1 +3: dec cnt ; test for loop + brne 1b +; Store last bit. + rol dd0 ; shift in result + rol dd1 + rol dd2 + rol dd3 +; Restore sign. + sbrs __tmp_reg__, 7 + rjmp 1f + com dd3 + com dd2 + com dd1 + neg dd0 + sbci dd1, 0xff + sbci dd2, 0xff + sbci dd3, 0xff +; Return. +1: pop r16 + ret + .endfunc diff --git a/n/avr/modules/math/fixed/fixed_mul_f824.avr.S b/n/avr/modules/math/fixed/fixed_mul_f824.avr.S new file mode 100644 index 0000000..ab69799 --- /dev/null +++ b/n/avr/modules/math/fixed/fixed_mul_f824.avr.S @@ -0,0 +1,162 @@ +; fixed_mul_f824.avr.S +; avr.math.fixed - Fixed point math module. {{{ +; +; 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. +; +; }}} + +; a: A3.A2 A1 A0 +; b: B3.B2 B1 B0 +; m: M3.M2 M1 M0 +; +; . => fractional dot +; A3xB3. | +; |A3xB2 | +; | .A3xB1 | +; | . A3xB0| +; |A2xB3 | +; | .A2xB2 | +; | . A2xB1| +; | . A2xB0 +; | .A1xB3 | +; | . A1xB2| +; | . A1xB1 +; | . |A1xB0 +; | . A0xB3| +; | . A0xB2 +; | . |A0xB1 +; | . | A0xB0 +; . [- step 1 --] +; [- step 2 --] +; +; The operation is done in two steps. The three lsb multiplications can not be +; avoided. +; All multiplications results are added together. If the result is negative, +; do the sign extension (the sbc instructions). + +#define dummy1 r25 +#define dummy0 r24 + +; mulsu are only allowed in r16-r23. +#define a3 r23 +#define a2 r22 +#define a1 r21 +#define a0 r20 + +#define b3 r19 +#define b2 r18 +#define b1 r17 +#define b0 r16 + +; r23 and r22 are used for a, registers will be moved before return. +#define m3 r25 +#define m2 r24 +#define m1 r27 +#define m0 r26 + +#define m1r r23 +#define m0r r22 + +#define z r30 + + .section .text + .global fixed_mul_f824_asm + .func fixed_mul_f824_asm +fixed_mul_f824_asm: + clr z +; Low dword (>> 8, with 8 guards). + mul a0, b1 + movw m0, r0 + clr m2 + clr m3 + mul a0, b0 + add m0, r1 + adc m1, z + adc m2, z + mul a1, b0 + add m0, r0 + adc m1, r1 + adc m2, z + mul a0, b2 + add m1, r0 + adc m2, r1 + adc m3, z + mul a1, b1 + add m1, r0 + adc m2, r1 + adc m3, z + mul a2, b0 + add m1, r0 + adc m2, r1 + adc m3, z +; Shift. + movw m0, m2 +; Upper word. + mulsu b3, a2 + movw m2, r0 + mulsu b3, a0 + sbc m2, z + sbc m3, z + add m0, r0 + adc m1, r1 + adc m2, z + adc m3, z + mul a1, b2 + add m0, r0 + adc m1, r1 + adc m2, z + adc m3, z + mul a2, b1 + add m0, r0 + adc m1, r1 + adc m2, z + adc m3, z + mulsu a3, b0 + sbc m2, z + sbc m3, z + add m0, r0 + adc m1, r1 + adc m2, z + adc m3, z + mulsu b3, a1 + sbc m3, z + add m1, r0 + adc m2, r1 + adc m3, z + mul a2, b2 + add m1, r0 + adc m2, r1 + adc m3, z + mulsu a3, b1 + sbc m3, z + add m1, r0 + adc m2, r1 + adc m3, z + mulsu a3, b2 + add m2, r0 + adc m3, r1 + muls a3, b3 + add m3, r0 +; Restore r1 and return. + clr r1 + movw m0r, m0 + ret + .endfunc diff --git a/n/avr/modules/math/fixed/test/Makefile b/n/avr/modules/math/fixed/test/Makefile new file mode 100644 index 0000000..a70ca2d --- /dev/null +++ b/n/avr/modules/math/fixed/test/Makefile @@ -0,0 +1,17 @@ +BASE = ../../../.. +PROGS = test_fixed +test_fixed_SOURCES = test_fixed.c +DOC = +EXTRACTDOC = +MODULES = uart proto math/fixed +CONFIGFILE = avrconfig.h +# atmega8, atmega8535, atmega128... +AVR_MCU = atmega8 +# -O2 : speed +# -Os : size +OPTIMIZE = -O2 + +DEFS = +LIBS = + +include $(BASE)/make/Makefile.gen diff --git a/n/avr/modules/math/fixed/test/avrconfig.h b/n/avr/modules/math/fixed/test/avrconfig.h new file mode 100644 index 0000000..12cae01 --- /dev/null +++ b/n/avr/modules/math/fixed/test/avrconfig.h @@ -0,0 +1,85 @@ +#ifndef avrconfig_h +#define avrconfig_h +/* avrconfig.h */ +/* avr.math.fixed - Fixed point math module. {{{ + * + * 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/fixed/test/test_fixed.c b/n/avr/modules/math/fixed/test/test_fixed.c new file mode 100644 index 0000000..d42dda3 --- /dev/null +++ b/n/avr/modules/math/fixed/test/test_fixed.c @@ -0,0 +1,44 @@ +/* test_fixed.c */ +/* avr.math.fixed - Fixed point math module. {{{ + * + * 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/fixed/fixed.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) +{ + volatile int32_t a = 42; + sei (); + uart0_init (); + fixed_mul_f824 (-a, 0); + proto_send0 ('z'); +} -- cgit v1.2.3