VirtualBox

source: vbox/trunk/src/VBox/Devices/Network/testcase/tstDevPhy.cpp@ 60441

Last change on this file since 60441 was 56292, checked in by vboxsync, 10 years ago

Devices: Updated (C) year.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.6 KB
Line 
1/* $Id: tstDevPhy.cpp 56292 2015-06-09 14:20:46Z vboxsync $ */
2/** @file
3 * PHY MDIO unit tests.
4 */
5
6/*
7 * Copyright (C) 2007-2015 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18#include <cppunit/ui/text/TestRunner.h>
19#include <cppunit/extensions/HelperMacros.h>
20
21#include "../DevE1000Phy.h"
22
23/**
24 * Test fixture for PHY MDIO/MDC interface emulation.
25 */
26class PhyTest : public CppUnit::TestFixture {
27 CPPUNIT_TEST_SUITE( PhyTest );
28
29 CPPUNIT_TEST(testSize);
30 CPPUNIT_TEST(testReadPID);
31 CPPUNIT_TEST(testReadEPID);
32 CPPUNIT_TEST(testWriteANA);
33
34 CPPUNIT_TEST_SUITE_END();
35
36private:
37 enum Op
38 {
39 WRITE_OP = 0x1,
40 READ_OP = 0x2
41 };
42
43#define PHYADR_VAL (uint16_t)0
44#define ST_VAL (uint16_t)1
45#define TA_VAL (uint16_t)2
46#define PREAMBLE_VAL 0xFFFFFFFF
47
48 enum BitWidths {
49 ST_BITS = 2,
50 OP_BITS = 2,
51 PHYADR_BITS = 5,
52 REGADR_BITS = 5,
53 TA_BITS = 2,
54 DATA_BITS = 16,
55 PREAMBLE_BITS = 32
56 };
57
58 PPHY phy;
59
60 // Helper methods
61 void shiftOutBits(uint32_t data, uint16_t count);
62 uint16_t shiftInBits(uint16_t count);
63 int readAt(uint16_t addr);
64 void writeTo(uint16_t addr, uint32_t value);
65
66public:
67 void setUp()
68 {
69 phy = new PHY;
70 Phy::init(phy, 0, PHY_EPID_M881000);
71 }
72
73 void tearDown()
74 {
75 delete phy;
76 }
77
78 void testSize()
79 {
80 CPPUNIT_ASSERT_EQUAL(32, ST_BITS+OP_BITS+PHYADR_BITS+REGADR_BITS+TA_BITS+DATA_BITS);
81 }
82
83 void testReadPID()
84 {
85 CPPUNIT_ASSERT_EQUAL(0x0141, readAt(2));
86 }
87
88 void testReadEPID()
89 {
90 CPPUNIT_ASSERT_EQUAL(0x0141, readAt(2));
91 CPPUNIT_ASSERT_EQUAL(PHY_EPID_M881000, readAt(3));
92 }
93
94 void testWriteANA()
95 {
96 writeTo(4, 0xBEEF);
97 CPPUNIT_ASSERT_EQUAL(0xBEEF, readAt(4));
98 }
99
100};
101
102/**
103 * shiftOutBits - Shift data bits our to MDIO
104 **/
105void PhyTest::shiftOutBits(uint32_t data, uint16_t count) {
106 uint32_t mask = 0x01 << (count - 1);
107
108 do {
109 Phy::writeMDIO(phy, data & mask);
110 mask >>= 1;
111 } while (mask);
112}
113
114/**
115 * shiftInBits - Shift data bits in from MDIO
116 **/
117uint16_t PhyTest::shiftInBits(uint16_t count)
118{
119 uint16_t data = 0;
120
121 while (count--)
122 {
123 data <<= 1;
124 data |= Phy::readMDIO(phy) ? 1 : 0;
125 }
126
127 return data;
128}
129
130int PhyTest::readAt(uint16_t addr)
131{
132 shiftOutBits(PREAMBLE_VAL, PREAMBLE_BITS);
133
134 shiftOutBits(ST_VAL, ST_BITS);
135 shiftOutBits(READ_OP, OP_BITS);
136 shiftOutBits(PHYADR_VAL, PHYADR_BITS);
137 shiftOutBits(addr, REGADR_BITS);
138
139 CPPUNIT_ASSERT_EQUAL((uint16_t)0, shiftInBits(1));
140 uint16_t u16Data = shiftInBits(DATA_BITS);
141 shiftInBits(1);
142 return u16Data;
143}
144
145void PhyTest::writeTo(uint16_t addr, uint32_t value)
146{
147 shiftOutBits(PREAMBLE_VAL, PREAMBLE_BITS);
148
149 shiftOutBits(ST_VAL, ST_BITS);
150 shiftOutBits(WRITE_OP, OP_BITS);
151 shiftOutBits(PHYADR_VAL, PHYADR_BITS);
152 shiftOutBits(addr, REGADR_BITS);
153 shiftOutBits(TA_VAL, TA_BITS);
154 shiftOutBits(value, DATA_BITS);
155}
156
157// Create text test runner and run all tests.
158int main( int argc, char **argv)
159{
160 CppUnit::TextUi::TestRunner runner;
161 runner.addTest( PhyTest::suite() );
162 return runner.run() ? 0 : 1;
163}
164
Note: See TracBrowser for help on using the repository browser.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette