1 |
71 |
zero_gravi |
// #################################################################################################
|
2 |
|
|
// # << NEORV32 - CFS Demo Program >> #
|
3 |
|
|
// # ********************************************************************************************* #
|
4 |
|
|
// # BSD 3-Clause License #
|
5 |
|
|
// # #
|
6 |
|
|
// # Copyright (c) 2022, 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 NEORV32 Processor - https://github.com/stnolting/neorv32 (c) Stephan Nolting #
|
33 |
|
|
// #################################################################################################
|
34 |
|
|
|
35 |
|
|
|
36 |
|
|
/**********************************************************************//**
|
37 |
|
|
* @file demo_cfs/main.c
|
38 |
|
|
* @author Stephan Nolting
|
39 |
|
|
* @brief Simple demo program for the _default_ custom functions subsystem (CFS) module.
|
40 |
|
|
**************************************************************************/
|
41 |
|
|
|
42 |
|
|
#include <neorv32.h>
|
43 |
|
|
|
44 |
|
|
|
45 |
|
|
/**********************************************************************//**
|
46 |
|
|
* @name User configuration
|
47 |
|
|
**************************************************************************/
|
48 |
|
|
/**@{*/
|
49 |
|
|
/** UART BAUD rate */
|
50 |
|
|
#define BAUD_RATE 19200
|
51 |
|
|
/** Number of test cases per CFS function */
|
52 |
|
|
#define TESTCASES 4
|
53 |
|
|
/**@}*/
|
54 |
|
|
|
55 |
|
|
|
56 |
|
|
/**********************************************************************//**
|
57 |
|
|
* @name Prototypes
|
58 |
|
|
**************************************************************************/
|
59 |
|
|
uint32_t xorshift32(void);
|
60 |
|
|
|
61 |
|
|
|
62 |
|
|
/**********************************************************************//**
|
63 |
|
|
* Main function
|
64 |
|
|
*
|
65 |
|
|
* @note This program requires the CFS and UART0.
|
66 |
|
|
*
|
67 |
|
|
* @return 0 if execution was successful
|
68 |
|
|
**************************************************************************/
|
69 |
|
|
int main() {
|
70 |
|
|
|
71 |
|
|
uint32_t i, tmp;
|
72 |
|
|
|
73 |
|
|
|
74 |
|
|
// init UART0 at default baud rate, no parity bits, no HW flow control
|
75 |
|
|
neorv32_uart0_setup(BAUD_RATE, PARITY_NONE, FLOW_CONTROL_NONE);
|
76 |
|
|
|
77 |
|
|
// capture all exceptions and give debug info via UART0
|
78 |
|
|
// this is not required, but keeps us safe
|
79 |
|
|
neorv32_rte_setup();
|
80 |
|
|
|
81 |
|
|
|
82 |
|
|
// check if CFS is implemented at all
|
83 |
|
|
if (neorv32_cfs_available() == 0) {
|
84 |
|
|
neorv32_uart0_printf("Error! No CFS synthesized!\n");
|
85 |
|
|
return 1;
|
86 |
|
|
}
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
// intro
|
90 |
|
|
neorv32_uart0_printf("<<< NEORV32 Custom Functions Subsystem (CFS) Demo Program >>>\n\n");
|
91 |
|
|
|
92 |
|
|
neorv32_uart0_printf("NOTE: This program assumes the _default_ CFS hardware module, which implements\n"
|
93 |
|
|
" simple data conversion functions using four memory-mapped registers.\n\n");
|
94 |
|
|
|
95 |
|
|
neorv32_uart0_printf("Default CFS memory-mapped registers:\n"
|
96 |
|
|
" * NEORV32_CFS.REG[0] (r/w): convert binary to gray code\n"
|
97 |
|
|
" * NEORV32_CFS.REG[1] (r/w): convert gray to binary code\n"
|
98 |
|
|
" * NEORV32_CFS.REG[2] (r/w): bit reversal\n"
|
99 |
|
|
" * NEORV32_CFS.REG[3] (r/w): byte swap\n"
|
100 |
|
|
"The remaining 28 CFS registers are unused and will return 0 when read.\n");
|
101 |
|
|
|
102 |
|
|
|
103 |
|
|
// function examples
|
104 |
|
|
neorv32_uart0_printf("\n--- CFS 'binary to gray' function ---\n");
|
105 |
|
|
for (i=0; i<TESTCASES; i++) {
|
106 |
|
|
tmp = xorshift32(); // get random test data
|
107 |
|
|
NEORV32_CFS.REG[0] = tmp; // write to CFS memory-mapped register 0
|
108 |
|
|
neorv32_uart0_printf("%u: IN = 0x%x, OUT = 0x%x\n", i, tmp, NEORV32_CFS.REG[0]); // read from CFS memory-mapped register 0
|
109 |
|
|
}
|
110 |
|
|
|
111 |
|
|
neorv32_uart0_printf("\n--- CFS 'gray to binary' function ---\n");
|
112 |
|
|
for (i=0; i<TESTCASES; i++) {
|
113 |
|
|
tmp = xorshift32(); // get random test data
|
114 |
|
|
NEORV32_CFS.REG[1] = tmp; // write to CFS memory-mapped register 1
|
115 |
|
|
neorv32_uart0_printf("%u: IN = 0x%x, OUT = 0x%x\n", i, tmp, NEORV32_CFS.REG[1]); // read from CFS memory-mapped register 1
|
116 |
|
|
}
|
117 |
|
|
|
118 |
|
|
neorv32_uart0_printf("\n--- CFS 'bit reversal' function ---\n");
|
119 |
|
|
for (i=0; i<TESTCASES; i++) {
|
120 |
|
|
tmp = xorshift32(); // get random test data
|
121 |
|
|
NEORV32_CFS.REG[2] = tmp; // write to CFS memory-mapped register 2
|
122 |
|
|
neorv32_uart0_printf("%u: IN = 0x%x, OUT = 0x%x\n", i, tmp, NEORV32_CFS.REG[2]); // read from CFS memory-mapped register 2
|
123 |
|
|
}
|
124 |
|
|
|
125 |
|
|
neorv32_uart0_printf("\n--- CFS 'byte swap' function ---\n");
|
126 |
|
|
for (i=0; i<TESTCASES; i++) {
|
127 |
|
|
tmp = xorshift32(); // get random test data
|
128 |
|
|
NEORV32_CFS.REG[3] = tmp; // write to CFS memory-mapped register 3
|
129 |
|
|
neorv32_uart0_printf("%u: IN = 0x%x, OUT = 0x%x\n", i, tmp, NEORV32_CFS.REG[3]); // read from CFS memory-mapped register 3
|
130 |
|
|
}
|
131 |
|
|
|
132 |
|
|
|
133 |
|
|
neorv32_uart0_printf("\nCFS demo program completed.\n");
|
134 |
|
|
|
135 |
|
|
return 0;
|
136 |
|
|
}
|
137 |
|
|
|
138 |
|
|
|
139 |
|
|
/**********************************************************************//**
|
140 |
|
|
* Pseudo-Random Number Generator (to generate deterministic test vectors).
|
141 |
|
|
*
|
142 |
|
|
* @return Random data (32-bit).
|
143 |
|
|
**************************************************************************/
|
144 |
|
|
uint32_t xorshift32(void) {
|
145 |
|
|
|
146 |
|
|
static uint32_t x32 = 314159265;
|
147 |
|
|
|
148 |
|
|
x32 ^= x32 << 13;
|
149 |
|
|
x32 ^= x32 >> 17;
|
150 |
|
|
x32 ^= x32 << 5;
|
151 |
|
|
|
152 |
|
|
return x32;
|
153 |
|
|
}
|