
;*************************************************************
;Name:          Display Card Number
;
;Author:        Dan Kohn
;
;Date:          03/15/2008
;
;
;Function:      To read from a magnetic card reader and 
;               display the card number on the screen
;               
;*************************************************************

#make_COM#


        ORG 100h
          
        jmp main

ready   db 'ready',10,13,'$'


card_bcd:
        db 'X'  ;00000
        db '0'  ;00001
        db '8'  ;00010
        db 'X'  ;00011
        db '4'  ;00100
        db 'X'  ;00101
        db 'X'  ;00110
        db '<'  ;00111 (Control)
        db '2'  ;01000
        db 'X'  ;01001
        db 'X'  ;01010
        db ':'  ;01011 (Control)
        db 'X'  ;01100
        db '6'  ;01101
        db '>'  ;01110 (Control)
        db 'X'  ;01111
        db '1'  ;10000
        db 'X'  ;10001
        db 'X'  ;10010
        db '9'  ;10011
        db 'X'  ;10100
        db '5'  ;10101
        db '='  ;10110 (Field Separator)
        db 'X'  ;10111
        db 'X'  ;11000
        db '3'  ;11001
        db ';'  ;11010 (Start Sentinel)
        db 'X'  ;11011
        db '7'  ;11100
        db 'X'  ;11101
        db 'X'  ;11110
        db '?'  ;11111 (End Sentinel)

main:
	    mov al,0ffh	            ;set Parallel Port D0-D7 high
	    mov dx,0378h	        ;to power card reader
	    out dx,al

    	call time_delay         ;wait for power up (~1 sec)
	
        mov ax, cs              ;print "ready"
        mov ds, ax
        mov dx, offset ready
        mov ah,09h
        int 21h
    

l1:	    call card_read          ;read card until start Sentinel
        cmp card,11010b
	    jne l1
	    
l3:	    mov card,0              ;clear
	    
        mov al,5                ;read next 5 bits on card
l2:     call card_read
        dec al
        jnz l2

        mov bl,00h              ;translate to number and 
        mov bl,card             ;print to screen
        lea si, card_bcd
        mov al,[si+bx]
        mov ah,0eh
        int 10h    
        
        mov al,card             ;end if field separator
        cmp al,10110b
        je l_end 
        
                   	    
	    jmp l3                  ;get next card digit

        
                                    
l_end:  ret                                    

;*************************************************************
;Name:          TIME_DELAY
;
;Author:        Dan Kohn
;
;Date:          10/30/2005
;
;Revised:       10/31/2005
;               PUSHA does not work on Toshiba XT Compatable
;               laptop. Replaced with individual push and pops.
;
;Function:      Wait for aprox 1 seconds then return to the 
;               calling code
;
;On Call:       NONE
;
;On Return:     All Registers Restored

time_delay: push ax         ;put all reg on stack for restore
            push bx
            push cx
            push dx
            
            mov ah,00       ;get system time 
            int 1ah         ;(CX:DX = number of clock 
                            ;ticks since midnigh)
                            ;[18.20648 ticks = 1 sec)
 
            add dx,0018     ;add 18 ticks (~1 sec)
            mov bx,dx       ;store for compare
            
t_loop:     mov ah,00       ;loop until system time > Last 
            int 1ah         ;time + 1 sec
            cmp bx,dx
            jg t_loop
            
            pop dx            ;restore all registers
            pop cx
            pop bx
            pop ax
            RET

;*************************************************************
;Name:          card_read
;
;Author:        Dan Kohn
;
;Date:          03/15/2008
;
;
;Function:      to get one bit from a magnetic card reader
;               
;
;On Call:       NONE
;
;On Return:     'card' contains new value including latest bit. 
;
;NOTE:          'card' must be cleared in mainline program
;               where appropriate.
;
;Hardware:      The card reader is hooked up as follows
;
;               PORT: LPT1 (378h)
;               
;               PIN 5  - PWR
;               PIN 25 - GND
;               PIN 12 - CLK
;               PIN 13 - DATA

new	    db 	0
card    db  0

card_read:


        push dx
        push ax
        
cr_1:   mov dx,0379h    ;wait for falling edge of clock
	    in al,dx	    
	    mov new,al	
	    and al,20h	    
	    jnz cr_1		
	
	    mov al,new	    ;get last input
	    
	    and al,10h	    ;check data bit
	    jz  low
high:	                ;if high, shift in a 0
        clc
        rcl card,1
	    jmp cr_2
low:	stc             ;if low, shift in a 1
        rcl card,1
	    
	
cr_2:	mov dx,0379h    ;wait for rising edge of clock
	    in al,dx
	    and al,20h
	    jz cr_2
	    
	    pop ax
	    pop dx
	    
	    ret


