%include "../defaults_bin.asm" ITERS EQU 8 mov al,TIMER0 | BOTH | MODE2 | BINARY out 0x43,al xor al,al out 0x40,al out 0x40,al cli mov ax,0 mov ds,ax mov ax,cs mov word[0x20],interrupt8 mov [0x22],ax mov ds,ax mov es,ax mov ss,ax mov sp,0 mov si,experimentData nextExperiment: lodsb cmp al,0 je noRefresh mov byte[pitMode],TIMER1 | LSB | MODE2 | BINARY mov [pitCount],al jmp doneRefresh noRefresh: mov byte[pitMode],TIMER1 | LSB | MODE0 | BINARY mov byte[pitCount],1 doneRefresh: push cx mov cx,ITERS+1 ; Number of iterations in primary measurement call doMeasurement push bx mov cx,1 ; Number of iterations in secondary measurement call doMeasurement pop ax ; The primary measurement will have the lower value, since the counter counts down sub ax,bx ; Subtract the secondary value, which will be higher, now AX is negative neg ax ; Negate to get the positive difference. ; Advance to the next experiment lodsw add si,ax lodsw add si,ax outputNewLine jmp nextExperiment repeatLoop1: jmp repeatLoop doMeasurement: push si push cx push cx ; Save number of iterations ; Copy init lodsw ; Number of init bytes mov cx,ax mov di,timerStartEnd call codeCopy ; Copy code lodsw ; Number of code bytes pop cx iterationLoop: push cx push si mov cx,ax call codeCopy pop si pop cx loop iterationLoop ; Copy timer end mov si,timerEndStart mov cx,timerEndEnd-timerEndStart call codeCopy ; Turn off refresh refreshOff pop cx ; Enable IRQ0 mov al,0xfe ; Enable IRQ 0 (timer), disable others out 0x21,al ; Use IRQ0 to go into lockstep with timer 0 mov al,TIMER0 | LSB | MODE2 | BINARY out 0x43,al mov al,0x04 ; Count = 0x0004 which should be after the hlt instruction has out 0x40,al ; taken effect. sti hlt mov al,0xff ; Disble IRQs out 0x21,al ; The actual measurement happens in the the IRQ0 handler which runs here and ; returns the timer value in BX. pop cx pop si ret codeCopy: ; push cx ; push ax ; push di ; outputCharacter 'b' ; pop di ; push di ; mov ax,di ; outputHex ; pop di ; pop ax ; pop cx cmp cx,0 je codeCopyDone codeCopyLoop: cmp di,0 je codeCopyOutOfSpace movsb loop codeCopyLoop codeCopyDone: ret codeCopyOutOfSpace: mov si,outOfSpaceMessage mov cx,outOfSpaceMessageEnd-outOfSpaceMessage outputString complete outOfSpaceMessage: db "Copy out of space - use fewer iterations" outOfSpaceMessageEnd: savedSS: dw 0 savedSP: dw 0 pitMode: db 0 pitCount: db 0 timerEndStart: in al,0x40 mov bl,al in al,0x40 mov bh,al refreshOn mov al,0x20 out 0x20,al mov ax,cs mov ds,ax mov es,ax mov ss,[savedSS] mov sp,[savedSP] ; Don't use IRET here - it'll turn interrupts back on and IRQ0 will be ; triggered a second time. popf retf timerEndEnd: ; This must come last in the program so that the experiment code can be ; copied after it. interrupt8: pushf mov al,[pitMode] out 0x43,al mov al,[pitCount] out 0x41,al xor ax,ax push ax popf mov [savedSS],ss mov [savedSP],sp mov bx,0x8000 mov ds,bx mov bx,0x7000 mov ss,bx mov bx,0x6000 mov es,bx mov dx,ax mov bx,ax mov si,ax mov di,ax mov bp,ax mov sp,ax mov al,TIMER0 | BOTH | MODE2 | BINARY out 0x43,al mov al,0x00 out 0x40,al out 0x40,al mov ax,bp timerStartEnd: testCases: