|
| 1 | +/*********************************************************************************************************************** |
| 2 | + * Copyright (C) 2016 Andrew Zonenberg and contributors * |
| 3 | + * * |
| 4 | + * This program is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General * |
| 5 | + * Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) * |
| 6 | + * any later version. * |
| 7 | + * * |
| 8 | + * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied * |
| 9 | + * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for * |
| 10 | + * more details. * |
| 11 | + * * |
| 12 | + * You should have received a copy of the GNU Lesser General Public License along with this program; if not, you may * |
| 13 | + * find one here: * |
| 14 | + * https://www.gnu.org/licenses/old-licenses/lgpl-2.1.txt * |
| 15 | + * or you may search the http://www.gnu.org website for the version 2.1 license, or you may write to the Free Software * |
| 16 | + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA * |
| 17 | + **********************************************************************************************************************/ |
| 18 | + |
| 19 | +#include <log.h> |
| 20 | +#include <gpdevboard.h> |
| 21 | +#include <unistd.h> |
| 22 | +#include <math.h> |
| 23 | + |
| 24 | +using namespace std; |
| 25 | + |
| 26 | +bool RunTest(hdevice hdev); |
| 27 | + |
| 28 | +int main(int argc, char* argv[]) |
| 29 | +{ |
| 30 | + g_log_sinks.emplace(g_log_sinks.begin(), new STDLogSink(Severity::VERBOSE)); |
| 31 | + |
| 32 | + //expect one arg: the bitstream |
| 33 | + if(argc != 2) |
| 34 | + { |
| 35 | + LogNotice("Usage: [testcase] bitstream.txt\n"); |
| 36 | + return 1; |
| 37 | + } |
| 38 | + |
| 39 | + //Set up the test |
| 40 | + hdevice hdev = MultiBoardTestSetup(argv[1], 25000, 1.8, SilegoPart::SLG46620V); |
| 41 | + if(!hdev) |
| 42 | + { |
| 43 | + LogError("Failed to open board\n"); |
| 44 | + return 1; |
| 45 | + } |
| 46 | + |
| 47 | + //Run the actual test case |
| 48 | + LogNotice("\n"); |
| 49 | + LogNotice("Running application test case\n"); |
| 50 | + if(!RunTest(hdev)) |
| 51 | + { |
| 52 | + SetStatusLED(hdev, 0); |
| 53 | + Reset(hdev); |
| 54 | + return 1; |
| 55 | + } |
| 56 | + |
| 57 | + //Turn off the LED before declaring success |
| 58 | + LogVerbose("\n"); |
| 59 | + LogVerbose("Test complete, resetting board\n"); |
| 60 | + SetStatusLED(hdev, 0); |
| 61 | + ResetAllSiggens(hdev); |
| 62 | + Reset(hdev); |
| 63 | + USBCleanup(hdev); |
| 64 | + return 0; |
| 65 | +} |
| 66 | + |
| 67 | +/** |
| 68 | + @brief The actual application-layer test |
| 69 | + */ |
| 70 | +bool RunTest(hdevice hdev) |
| 71 | +{ |
| 72 | + LogIndenter li; |
| 73 | + |
| 74 | + //Set up the I/O config we need for this test |
| 75 | + IOConfig ioConfig; |
| 76 | + for(size_t i = 2; i <= 20; i++) |
| 77 | + ioConfig.driverConfigs[i] = TP_RESET; |
| 78 | + if(!SetIOConfig(hdev, ioConfig)) |
| 79 | + return false; |
| 80 | + |
| 81 | + //Loop over all values for the 4 inputs and see what we get |
| 82 | + bool ok = true; |
| 83 | + for(int i=0; i<16; i++) |
| 84 | + { |
| 85 | + bool a = (i & 1) ? true : false; |
| 86 | + bool b = (i & 2) ? true : false; |
| 87 | + bool c = (i & 4) ? true : false; |
| 88 | + bool d = (i & 8) ? true : false; |
| 89 | + |
| 90 | + bool nand1_expected = !a; |
| 91 | + bool nand2_expected = !(a & b); |
| 92 | + bool nand3_expected = !(a & b & c); |
| 93 | + bool nand4_expected = !(a & b & c & d); |
| 94 | + |
| 95 | + bool expected[8] = |
| 96 | + { |
| 97 | + nand1_expected, nand2_expected, nand3_expected, nand4_expected, |
| 98 | + nand1_expected, nand2_expected, nand3_expected, nand4_expected |
| 99 | + }; |
| 100 | + |
| 101 | + //Drive the inputs |
| 102 | + LogVerbose("Testing %d %d %d %d\n", a, b, c, d); |
| 103 | + LogVerbose("Expected: %d %d %d %d\n", nand1_expected, nand2_expected, nand3_expected, nand4_expected); |
| 104 | + ioConfig.driverConfigs[2] = a ? TP_VDD : TP_GND; |
| 105 | + ioConfig.driverConfigs[3] = b ? TP_VDD : TP_GND; |
| 106 | + ioConfig.driverConfigs[4] = c ? TP_VDD : TP_GND; |
| 107 | + ioConfig.driverConfigs[5] = d ? TP_VDD : TP_GND; |
| 108 | + if(!SetIOConfig(hdev, ioConfig)) |
| 109 | + return false; |
| 110 | + |
| 111 | + //Read the outputs |
| 112 | + int pin_nums[8] = {6, 7, 8, 9, 12, 13, 14, 15}; |
| 113 | + bool results[8]; |
| 114 | + for(int i=0; i<8; i++) |
| 115 | + { |
| 116 | + double value; |
| 117 | + if(!SingleReadADC(hdev, pin_nums[i], value)) |
| 118 | + return false; |
| 119 | + results[i] = (value > 0.5); |
| 120 | + } |
| 121 | + |
| 122 | + //Sanity check the results |
| 123 | + for(int j=0; j<8; j++) |
| 124 | + { |
| 125 | + if(expected[j] != results[j]) |
| 126 | + { |
| 127 | + LogError("input (%d %d %d %d), output pin %d, got %d instead of %d\n", |
| 128 | + a, b, c, d, |
| 129 | + pin_nums[j], |
| 130 | + results[j], |
| 131 | + expected[j]); |
| 132 | + ok = false; |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + return ok; |
| 138 | +} |
0 commit comments