2016-07-13 01:42:23 +00:00
//
// D P L L T e s t s . s w i f t
// C l o c k S i g n a l
//
// C r e a t e d b y T h o m a s H a r t e o n 1 2 / 0 7 / 2 0 1 6 .
// C o p y r i g h t © 2 0 1 6 T h o m a s H a r t e . A l l r i g h t s r e s e r v e d .
//
import XCTest
class DPLLTests : XCTestCase {
2016-09-16 02:12:12 +00:00
func testRegularNibblesOnPLL ( _ pll : DigitalPhaseLockedLoopBridge , bitLength : UInt ) {
2016-07-14 11:12:02 +00:00
// c l o c k i n t w o 1 s , a 0 , a n d a 1 , 2 0 0 t i m e s o v e r
for _ in 0 . . < 200 {
2016-09-16 02:12:12 +00:00
pll . run ( forCycles : bitLength / 2 )
2016-07-14 11:12:02 +00:00
pll . addPulse ( )
2016-09-16 02:12:12 +00:00
pll . run ( forCycles : bitLength )
2016-07-14 11:12:02 +00:00
pll . addPulse ( )
2016-09-16 02:12:12 +00:00
pll . run ( forCycles : bitLength * 2 )
2016-07-14 11:12:02 +00:00
pll . addPulse ( )
2016-09-16 02:12:12 +00:00
pll . run ( forCycles : bitLength / 2 )
2016-07-14 11:12:02 +00:00
}
2016-07-27 20:24:24 +00:00
XCTAssert ( ( pll . stream & 0xffffffff ) = = 0xdddddddd , " PLL should have synchronised and clocked repeating 0xd nibbles; got \( String ( pll . stream , radix : 16 , uppercase : false ) ) " )
2016-07-14 11:12:02 +00:00
}
2016-07-13 01:42:23 +00:00
func testPerfectInput ( ) {
2016-07-27 20:24:24 +00:00
let pll = DigitalPhaseLockedLoopBridge ( clocksPerBit : 100 , tolerance : 20 , historyLength : 3 )
2016-09-16 02:12:12 +00:00
testRegularNibblesOnPLL ( pll ! , bitLength : 100 )
2016-07-14 11:12:02 +00:00
}
2016-07-13 01:42:23 +00:00
2016-07-14 11:12:02 +00:00
func testFastButRegular ( ) {
2016-07-27 20:24:24 +00:00
let pll = DigitalPhaseLockedLoopBridge ( clocksPerBit : 100 , tolerance : 20 , historyLength : 3 )
2016-09-16 02:12:12 +00:00
testRegularNibblesOnPLL ( pll ! , bitLength : 90 )
2016-07-13 01:42:23 +00:00
}
2016-07-14 11:12:02 +00:00
func testSlowButRegular ( ) {
2016-07-27 20:24:24 +00:00
let pll = DigitalPhaseLockedLoopBridge ( clocksPerBit : 100 , tolerance : 20 , historyLength : 3 )
2016-09-16 02:12:12 +00:00
testRegularNibblesOnPLL ( pll ! , bitLength : 110 )
2016-07-14 11:12:02 +00:00
}
2016-07-14 23:54:48 +00:00
2016-07-28 15:32:14 +00:00
func testTwentyPercentSinePattern ( ) {
2016-07-14 23:54:48 +00:00
let pll = DigitalPhaseLockedLoopBridge ( clocksPerBit : 100 , tolerance : 20 , historyLength : 3 )
var angle = 0.0
// c l o c k i n t w o 1 s , a 0 , a n d a 1 , 2 0 0 t i m e s o v e r
for _ in 0 . . < 200 {
2016-07-28 15:32:14 +00:00
let bitLength : UInt = UInt ( 100 + 20 * sin ( angle ) )
2016-07-14 23:54:48 +00:00
2016-09-16 02:12:12 +00:00
pll ? . run ( forCycles : bitLength / 2 )
pll ? . addPulse ( )
pll ? . run ( forCycles : ( bitLength * 3 ) / 2 )
2016-07-14 23:54:48 +00:00
angle = angle + 0.1
}
2016-09-16 02:12:12 +00:00
let endOfStream = ( pll ? . stream ) !& 0xffffffff ;
XCTAssert ( endOfStream = = 0xaaaaaaaa || endOfStream = = 0x55555555 , " PLL should have synchronised and clocked repeating 0xa or 0x5 nibbles; got \( String ( pll ? . stream , radix : 16 , uppercase : false ) ) " )
2016-07-14 23:54:48 +00:00
}
2016-07-13 01:42:23 +00:00
}