summaryrefslogtreecommitdiffstats
path: root/src/VBox/Devices/Network/testcase/tstDevEEPROM.cpp
blob: 148bc7aaba3421890cd718871638d54821e6c599 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
/* $Id: tstDevEEPROM.cpp $ */
/** @file
 * EEPROM 93C46 unit tests.
 */

/*
 * Copyright (C) 2007-2023 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * 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, in version 3 of the
 * License.
 *
 * 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, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */


/*********************************************************************************************************************************
*   Header Files                                                                                                                 *
*********************************************************************************************************************************/
#ifdef USE_CPPUNIT
# include <cppunit/ui/text/TestRunner.h>
# include <cppunit/extensions/HelperMacros.h>
#else
# include "CppUnitEmulation.h"
#endif
#include <VBox/vmm/pdmdev.h>
#include "../DevEEPROM.h"


/*********************************************************************************************************************************
*   Global Variables                                                                                                             *
*********************************************************************************************************************************/
static const uint16_t g_abInitialContent[] =
{
    0x0000, 0x0001, 0x0002, 0x0003, 0x0004, 0x0005, 0x0006, 0x0007,
    0x0008, 0x0009, 0x000a, 0x000b, 0x000c, 0x000d, 0x000e, 0x000f,
    0x0010, 0x0011, 0x0012, 0x0013, 0x0014, 0x0015, 0x0016, 0x0017,
    0x0018, 0x0019, 0x001a, 0x001b, 0x001c, 0x001d, 0x001e, 0x001f,
    0x0020, 0x0021, 0x0022, 0x0023, 0x0024, 0x0025, 0x0026, 0x0027,
    0x0028, 0x0029, 0x002a, 0x002b, 0x002c, 0x002d, 0x002e, 0x002f,
    0x0030, 0x0031, 0x0032, 0x0033, 0x0034, 0x0035, 0x0036, 0x0037,
    0x0038, 0x0039, 0x003a, 0x003b, 0x003c, 0x003d, 0x003e, 0x003f
};


/**
 * Test fixture for 93C46-compatible EEPROM device emulation.
 */
class EEPROMTest
#ifdef USE_CPPUNIT
    : public CppUnit::TestFixture
#endif
{
    CPPUNIT_TEST_SUITE( tstDevEEPROM );

    CPPUNIT_TEST( testRead );
    CPPUNIT_TEST( testSequentialRead );
    CPPUNIT_TEST( testWrite );
    CPPUNIT_TEST( testWriteAll );
    CPPUNIT_TEST( testWriteDisabled );
    CPPUNIT_TEST( testErase );
    CPPUNIT_TEST( testEraseAll );

    CPPUNIT_TEST_SUITE_END();

private:
    enum Wires { DO=8, DI=4, CS=2, SK=0x01 };
    enum OpCodes {
        READ_OPCODE  = 0x6,
        WRITE_OPCODE = 0x5,
        ERASE_OPCODE = 0x7,
        EWDS_OPCODE  = 0x10, // erase/write disable
        WRAL_OPCODE  = 0x11, // write all
        ERAL_OPCODE  = 0x12, // erase all
        EWEN_OPCODE  = 0x13  // erase/write enable
    };
    enum BitWidths {
        READ_OPCODE_BITS  =  3,
        WRITE_OPCODE_BITS =  3,
        ERASE_OPCODE_BITS =  3,
        EWDS_OPCODE_BITS  =  5,
        WRAL_OPCODE_BITS  =  5,
        ERAL_OPCODE_BITS  =  5,
        EWEN_OPCODE_BITS  =  5,
        READ_ADDR_BITS    =  6,
        WRITE_ADDR_BITS   =  6,
        ERASE_ADDR_BITS   =  6,
        EWDS_ADDR_BITS    =  4,
        WRAL_ADDR_BITS    =  4,
        ERAL_ADDR_BITS    =  4,
        EWEN_ADDR_BITS    =  4,
        DATA_BITS         = 16
    };

    EEPROM93C46 *eeprom;

    // Helper methods
    void shiftOutBits(uint16_t data, uint16_t count);
    uint16_t shiftInBits(uint16_t count);
    void getReady();
    void standby();
    void stop();
    uint16_t readAt(uint16_t addr);
    bool writeTo(uint16_t addr, uint16_t value);
    void writeOpAddr(int opCode, int opCodeBits, uint16_t addr, int addrBits);
    void writeData(uint16_t value) { shiftOutBits(value, DATA_BITS); }
    bool waitForCompletion();

public:
    void setUp()
    {
        eeprom = new EEPROM93C46;
        eeprom->init(g_abInitialContent);
    }

    void tearDown()
    {
        delete eeprom;
    }

    void testSize()
    {
        CPPUNIT_ASSERT_EQUAL( sizeof(g_abInitialContent), (size_t)EEPROM93C46::SIZE );
    }

    void testRead()
    {
        getReady();
        for ( uint32_t wordAddr=0; wordAddr < EEPROM93C46::SIZE; wordAddr++ ) {
            shiftOutBits(READ_OPCODE, READ_OPCODE_BITS);
            shiftOutBits(wordAddr, READ_ADDR_BITS);

            CPPUNIT_ASSERT_EQUAL( g_abInitialContent[wordAddr], (uint16_t)wordAddr );
            CPPUNIT_ASSERT_EQUAL( g_abInitialContent[wordAddr], shiftInBits(DATA_BITS) );
            standby();
        }
        stop();
    }

    void testSequentialRead()
    {
        getReady();
        shiftOutBits(READ_OPCODE, READ_OPCODE_BITS);
        shiftOutBits(0, READ_ADDR_BITS);
        for ( int wordAddr=0; wordAddr < EEPROM93C46::SIZE; wordAddr++ ) {
            CPPUNIT_ASSERT_EQUAL( g_abInitialContent[wordAddr], shiftInBits(DATA_BITS) );
        }
        stop();
    }

    void testWrite()
    {
        //unused: int i;
        uint16_t wordAddr;

        getReady();
        // Enable write
        writeOpAddr(EWEN_OPCODE, EWEN_OPCODE_BITS, 0, EWEN_ADDR_BITS);
        standby();

        for ( wordAddr=0; wordAddr < EEPROM93C46::SIZE; wordAddr++ ) {
            //writeOpAddr(WRITE_OPCODE, WRITE_OPCODE_BITS, (uint16_t)wordAddr, WRITE_ADDR_BITS);
            writeTo(wordAddr, 0x3F00 - (wordAddr<<8));
            standby();

            if (!waitForCompletion()) {
                CPPUNIT_FAIL("EEPROM write was not completed");
                stop();
                return;
            }
            standby();
        }

        // Disable write
        writeOpAddr(EWDS_OPCODE, EWDS_OPCODE_BITS, 0, EWDS_ADDR_BITS);

        stop();

        // Now check the result
        getReady();
        writeOpAddr(READ_OPCODE, READ_OPCODE_BITS, 0, READ_ADDR_BITS);
        for ( wordAddr=0; wordAddr < EEPROM93C46::SIZE; wordAddr++ ) {
            CPPUNIT_ASSERT_EQUAL((uint16_t)(0x3F00 - (wordAddr<<8)), shiftInBits(DATA_BITS) );
        }
        stop();
    }

    void testWriteDisabled()
    {
        getReady();

        uint16_t addr = 0;
        uint16_t oldValue = readAt(addr);
        stop();
        getReady();
        if (writeTo(addr, ~oldValue)) {
            // Write appears to be successful -- continue
            CPPUNIT_ASSERT_EQUAL(oldValue, readAt(addr));
        }
        else {
            CPPUNIT_FAIL("EEPROM write was not completed");
        }
        stop();
    }

    void testErase()
    {
        int i;
        uint16_t addr = 0x1F;

        getReady();
        // Enable write
        shiftOutBits(EWEN_OPCODE, EWEN_OPCODE_BITS);
        shiftOutBits(0, EWEN_ADDR_BITS);
        standby();

        if (writeTo(addr, addr)) {
            stop();
            getReady();
            // Write successful -- continue
            CPPUNIT_ASSERT_EQUAL(addr, readAt(addr));
            stop();
            getReady();

            shiftOutBits(ERASE_OPCODE, ERASE_OPCODE_BITS);
            shiftOutBits(addr, ERASE_ADDR_BITS);

            standby();

            for (i = 0; i < 200; i++) {
                if (eeprom->read() & DO)
                    break;
                //usec_delay(50);
            }

            if (i == 200) {
                CPPUNIT_FAIL("EEPROM erase was not completed");
                stop();
                return;
            }

            standby();

            shiftOutBits(EWDS_OPCODE, EWDS_OPCODE_BITS);
            shiftOutBits(0, EWDS_ADDR_BITS);

            stop();
            getReady();
            CPPUNIT_ASSERT_EQUAL((uint16_t)0xFFFF, readAt(addr));
        }
        else {
            CPPUNIT_FAIL("EEPROM write was not completed");
        }
        stop();
    }

    void testWriteAll()
    {
        uint16_t addr;

        getReady();
        // Enable write
        writeOpAddr(EWEN_OPCODE, EWEN_OPCODE_BITS, 0, EWEN_ADDR_BITS);
        standby();
        // Fill all memory
        writeOpAddr(WRAL_OPCODE, WRAL_OPCODE_BITS, 0, WRAL_ADDR_BITS);
        writeData(0xABBA);
        standby();

        if (waitForCompletion()) {
            stop();
            getReady();
            // Write successful -- verify all memory
            for ( addr=0; addr < EEPROM93C46::SIZE; addr++ ) {
                CPPUNIT_ASSERT_EQUAL((uint16_t)0xABBA, readAt(addr));
            }
        }
        else {
            CPPUNIT_FAIL("EEPROM write was not completed");
        }
        stop();
    }

    void testEraseAll()
    {
        //unused: int i;
        uint16_t addr = 0x1F;

        getReady();
        // Enable write
        writeOpAddr(EWEN_OPCODE, EWEN_OPCODE_BITS, 0, EWEN_ADDR_BITS);
        standby();
        // Fill all memory
        writeOpAddr(WRITE_OPCODE, WRITE_OPCODE_BITS, addr, WRITE_ADDR_BITS);
        writeData(0);
        standby();

        if (waitForCompletion()) {
            stop();
            getReady();
            // Write successful -- verify random location
            CPPUNIT_ASSERT_EQUAL((uint16_t)0, readAt(addr));
            stop();
            getReady();

            writeOpAddr(ERAL_OPCODE, ERAL_OPCODE_BITS, addr, ERAL_ADDR_BITS);
            standby();

            if (!waitForCompletion()) {
                CPPUNIT_FAIL("EEPROM erase was not completed");
                stop();
                return;
            }

            standby();

            writeOpAddr(EWDS_OPCODE, EWDS_OPCODE_BITS, 0, EWDS_ADDR_BITS);
            stop();

            getReady();
            for ( addr=0; addr < EEPROM93C46::SIZE; addr++ ) {
                CPPUNIT_ASSERT_EQUAL((uint16_t)0xFFFF, readAt(addr));
            }
        }
        else {
            CPPUNIT_FAIL("EEPROM write was not completed");
        }
        stop();
    }
};

/**
 *  shiftOutBits - Shift data bits our to the EEPROM
 *  @hw: pointer to the EEPROM object
 *  @data: data to send to the EEPROM
 *  @count: number of bits to shift out
 *
 *  We need to shift 'count' bits out to the EEPROM.  So, the value in the
 *  "data" parameter will be shifted out to the EEPROM one bit at a time.
 *  In order to do this, "data" must be broken down into bits.
 **/
void EEPROMTest::shiftOutBits(uint16_t data, uint16_t count) {
    uint32_t wires = eeprom->read();
    uint32_t mask;

    mask = 0x01 << (count - 1);
    wires &= ~DO;

    do {
        wires &= ~DI;

        if (data & mask)
            wires |= DI;

        eeprom->write(wires);

        // Raise clock
        eeprom->write(wires |= SK);
        // Lower clock
        eeprom->write(wires &= ~SK);

        mask >>= 1;
    } while (mask);

    wires &= ~DI;
    eeprom->write(wires);
}

/**
 *  shiftInBits - Shift data bits in from the EEPROM
 *  @count: number of bits to shift in
 *
 *  In order to read a register from the EEPROM, we need to shift 'count' bits
 *  in from the EEPROM.  Bits are "shifted in" by raising the clock input to
 *  the EEPROM (setting the SK bit), and then reading the value of the data out
 *  "DO" bit.  During this "shifting in" process the data in "DI" bit should
 *  always be clear.
 **/
uint16_t EEPROMTest::shiftInBits(uint16_t count)
{
    uint32_t wires;
    uint32_t i;
    uint16_t data;

    wires = eeprom->read();

    wires &= ~(DO | DI);
    data = 0;

    for (i = 0; i < count; i++) {
        data <<= 1;
        // Raise clock
        eeprom->write(wires |= SK);

        wires = eeprom->read();

        wires &= ~DI;
        if (wires & DO)
            data |= 1;

        // Lower clock
        eeprom->write(wires &= ~SK);
    }

    return data;
}

/**
 *  getReady - Prepares EEPROM for read/write
 *
 *  Setups the EEPROM for reading and writing.
 **/
void EEPROMTest::getReady()
{
    unsigned wires = eeprom->read();
    /* Clear SK and DI */
    eeprom->write(wires &= ~(DI | SK));
    /* Set CS */
    eeprom->write(wires | CS);
}

/**
 *  standby - Return EEPROM to standby state
 *
 *  Return the EEPROM to a standby state.
 **/
void EEPROMTest::standby()
{
    unsigned wires = eeprom->read();

    eeprom->write(wires &= ~(CS | SK));

    // Raise clock
    eeprom->write(wires |= SK);

    // Select EEPROM
    eeprom->write(wires |= CS);

    // Lower clock
    eeprom->write(wires &= ~SK);
}

/**
 *  stop - Terminate EEPROM command
 *
 *  Terminates the current command by inverting the EEPROM's chip select pin.
 **/
void EEPROMTest::stop()
{
    unsigned wires = eeprom->read();

    eeprom->write(wires &= ~(CS | DI));
    // Raise clock
    eeprom->write(wires |= SK);
    // Lower clock
    eeprom->write(wires &= ~SK);
}

/**
 *  readAt - Read a word at specified address
 *  @addr: address to read
 *
 *  Returns the value of the word specified in 'addr' parameter.
 **/
uint16_t EEPROMTest::readAt(uint16_t addr)
{
    getReady();
    shiftOutBits(READ_OPCODE, READ_OPCODE_BITS);
    shiftOutBits(addr, READ_ADDR_BITS);

    uint16_t value = shiftInBits(DATA_BITS);
    stop();

    return value;
}

/**
 *  writeTo - Write a word to specified address
 *  @addr: address to write to
 *  @value: value to store
 *
 *  Returns false if write did not complete.
 *
 *  Note: Make sure EEPROM is selected and writable before attempting
 *        to write. Use getReady() and stop() to select/deselect
 *        EEPROM.
 **/
bool EEPROMTest::writeTo(uint16_t addr, uint16_t value)
{
    writeOpAddr(WRITE_OPCODE, WRITE_OPCODE_BITS, addr, WRITE_ADDR_BITS);
    writeData(value);
    standby();
    return waitForCompletion();
}


/**
 *  waitForCompletion - Wait until EEPROM clears the busy bit
 *
 *  Returns false if the EEPROM is still busy.
 */
bool EEPROMTest::waitForCompletion() {
    for (int i = 0; i < 200; i++) {
        if (eeprom->read() & DO) {
            standby();
            return true;
        }
        // Wait 50 usec;
    }

    return false;
}

/**
 *  writeOpAddr - Write an opcode and address
 *  @opCode:     operation code
 *  @opCodeBits: number of bits in opCode
 *  @addr:       address to write to
 *  @addrBits:   number of bits in address
 **/
void EEPROMTest::writeOpAddr(int opCode, int opCodeBits, uint16_t addr, int addrBits)
{
    shiftOutBits(opCode, opCodeBits);
    shiftOutBits(addr, addrBits);
}

int main()
{
#ifdef USE_CPPUNIT
    CppUnit::TextUi::TestRunner runner;
    runner.addTest( EEPROMTest::suite() );
    return runner.run() ? 0 : 1;
#else
    EEPROMTest Test;
    return Test.run();
#endif
}