@@ -113,4 +113,47 @@ describe('USART', () => {
113113 expect ( cpu . data [ 0xc0 ] ) . toEqual ( 0x40 | 0x20 ) ; // UCSR0A: TXC | UDRE
114114 } ) ;
115115 } ) ;
116+
117+ describe ( 'onLineTransmit' , ( ) => {
118+ it ( 'should call onLineTransmit with the current line buffer after every newline' , ( ) => {
119+ const cpu = new CPU ( new Uint16Array ( 1024 ) ) ;
120+ const usart = new AVRUSART ( cpu , usart0Config , FREQ_16MHZ ) ;
121+ usart . onLineTransmit = jest . fn ( ) ;
122+ cpu . writeData ( 0xc1 , 0x8 ) ; // UCSR0B <- TXEN
123+ cpu . writeData ( 0xc6 , 0x48 ) ; // 'H'
124+ cpu . writeData ( 0xc6 , 0x65 ) ; // 'e'
125+ cpu . writeData ( 0xc6 , 0x6c ) ; // 'l'
126+ cpu . writeData ( 0xc6 , 0x6c ) ; // 'l'
127+ cpu . writeData ( 0xc6 , 0x6f ) ; // 'o'
128+ cpu . writeData ( 0xc6 , 0xa ) ; // '\n'
129+ expect ( usart . onLineTransmit ) . toHaveBeenCalledWith ( 'Hello' ) ;
130+ } ) ;
131+
132+ it ( 'should not call onLineTransmit if no newline was received' , ( ) => {
133+ const cpu = new CPU ( new Uint16Array ( 1024 ) ) ;
134+ const usart = new AVRUSART ( cpu , usart0Config , FREQ_16MHZ ) ;
135+ usart . onLineTransmit = jest . fn ( ) ;
136+ cpu . writeData ( 0xc1 , 0x8 ) ; // UCSR0B <- TXEN
137+ cpu . writeData ( 0xc6 , 0x48 ) ; // 'H'
138+ cpu . writeData ( 0xc6 , 0x69 ) ; // 'i'
139+ expect ( usart . onLineTransmit ) . not . toHaveBeenCalled ( ) ;
140+ } ) ;
141+
142+ it ( 'should clear the line buffer after each call to onLineTransmit' , ( ) => {
143+ const cpu = new CPU ( new Uint16Array ( 1024 ) ) ;
144+ const usart = new AVRUSART ( cpu , usart0Config , FREQ_16MHZ ) ;
145+ usart . onLineTransmit = jest . fn ( ) ;
146+ cpu . writeData ( 0xc1 , 0x8 ) ; // UCSR0B <- TXEN
147+ cpu . writeData ( 0xc6 , 0x48 ) ; // 'H'
148+ cpu . writeData ( 0xc6 , 0x69 ) ; // 'i'
149+ cpu . writeData ( 0xc6 , 0xa ) ; // '\n'
150+ cpu . writeData ( 0xc6 , 0x74 ) ; // 't'
151+ cpu . writeData ( 0xc6 , 0x68 ) ; // 'h'
152+ cpu . writeData ( 0xc6 , 0x65 ) ; // 'e'
153+ cpu . writeData ( 0xc6 , 0x72 ) ; // 'r'
154+ cpu . writeData ( 0xc6 , 0x65 ) ; // 'e'
155+ cpu . writeData ( 0xc6 , 0xa ) ; // '\n'
156+ expect ( usart . onLineTransmit ) . toHaveBeenCalledWith ( 'there' ) ;
157+ } ) ;
158+ } ) ;
116159} ) ;
0 commit comments