OpenCores
URL https://opencores.org/ocsvn/neo430/neo430/trunk

Subversion Repositories neo430

[/] [neo430/] [trunk/] [neo430/] [sw/] [example/] [muldiv_test/] [main.c] - Blame information for rev 198

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 198 zero_gravi
// #################################################################################################
2
// #  < Test the multiplier/divider unit >                                                         #
3
// # ********************************************************************************************* #
4
// # BSD 3-Clause License                                                                          #
5
// #                                                                                               #
6
// # Copyright (c) 2020, Stephan Nolting. All rights reserved.                                     #
7
// #                                                                                               #
8
// # Redistribution and use in source and binary forms, with or without modification, are          #
9
// # permitted provided that the following conditions are met:                                     #
10
// #                                                                                               #
11
// # 1. Redistributions of source code must retain the above copyright notice, this list of        #
12
// #    conditions and the following disclaimer.                                                   #
13
// #                                                                                               #
14
// # 2. Redistributions in binary form must reproduce the above copyright notice, this list of     #
15
// #    conditions and the following disclaimer in the documentation and/or other materials        #
16
// #    provided with the distribution.                                                            #
17
// #                                                                                               #
18
// # 3. Neither the name of the copyright holder nor the names of its contributors may be used to  #
19
// #    endorse or promote products derived from this software without specific prior written      #
20
// #    permission.                                                                                #
21
// #                                                                                               #
22
// # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS   #
23
// # OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF               #
24
// # MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE    #
25
// # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,     #
26
// # EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE #
27
// # GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED    #
28
// # AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING     #
29
// # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED  #
30
// # OF THE POSSIBILITY OF SUCH DAMAGE.                                                            #
31
// # ********************************************************************************************* #
32
// # The NEO430 Processor - https://github.com/stnolting/neo430                                    #
33
// #################################################################################################
34
 
35
// Libraries
36
#include <stdint.h>
37
#include <neo430.h>
38
 
39
// Configuration
40
#define BAUD_RATE 19200
41
 
42
 
43
/* ------------------------------------------------------------
44
 * INFO Main function
45
 * ------------------------------------------------------------ */
46
int main(void) {
47
 
48
  // setup UART
49
  neo430_uart_setup(BAUD_RATE);
50
 
51
  neo430_printf("Multiplier/Divider Test\n\n");
52
 
53
  // check if multiplier/divider unit was synthesized, exit if not
54
  if (!(SYS_FEATURES & (1<<SYS_MULDIV_EN))) {
55
    neo430_uart_br_print("Error! No multiplier/divider unit synthesized!");
56
    return 1;
57
  }
58
 
59
  uint32_t prod, prod_ref;
60
  int32_t sprod, sprod_ref;
61
  uint16_t i, a, b, rem, quot, rem_ref, quot_ref;
62
  int16_t sa, sb, srem, squot, srem_ref, squot_ref;
63
 
64
  for (i=0; i<0xFFFF; i++) {
65
 
66
    // get "random" operands
67
    a = (uint16_t)neo430_xorshift32();
68
    b = (uint16_t)neo430_xorshift32();
69
 
70
    // multiply test
71
    prod_ref = (uint32_t)a * (uint32_t)b;
72
    prod = neo430_umul32(a, b);
73
    neo430_printf("UNSIGNED %u: %u * %u  =  P: %n  vs ref.  P: %n\n", i, a, b, prod, prod_ref);
74
 
75
    // division test
76
    if (b == 0) // dont divide by zero
77
      b = 1;
78
    quot_ref = a / b;
79
    rem_ref  = a % b;
80
    quot = neo430_umoddiv16(&rem, a, b);
81
    neo430_printf("UNSIGNED %u: %u / %u  =  Q: %u & R: %u  vs ref.  Q: %u & R: %u\n", i, a, b, quot, rem, quot_ref, rem_ref);
82
 
83
 
84
    // get "random" operands
85
    sa = (int16_t)neo430_xorshift32();
86
    sb = (int16_t)neo430_xorshift32();
87
 
88
    // division test - signed
89
    if (sb == 0) // dont divide by zero
90
      sb = 1;
91
    squot_ref = sa / sb;
92
    srem_ref  = sa % sb;
93
    squot = neo430_moddiv16(&srem, sa, sb);
94
    neo430_printf("SIGNED   %u: %i / %i  =  Q: %i & R: %i  vs ref.  Q: %i & R: %i\n", i, sa, sb, squot, srem, squot_ref, srem_ref);
95
 
96
    // multiply test - signed
97
    sprod_ref = (int32_t)sa * (int32_t)sb;
98
    sprod = neo430_mul32(sa, sb);
99
    neo430_printf("SIGNED   %u: %i * %i  =  P: %l  vs ref.  P: %l\n", i, sa, sb, sprod, sprod_ref);
100
 
101
 
102
    // all correct?
103
    if ((rem != rem_ref) || (quot != quot_ref) || (prod != prod_ref) || (srem != srem_ref) || (squot != squot_ref) || (sprod != sprod_ref)) {
104
      neo430_printf("ERROR in one or more of the four previous operations!");
105
      while(1);
106
    }
107
  }
108
 
109
  neo430_printf("Tests passed!\n");
110
  while(1);
111
 
112
  return 0;
113
}
114
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.