.org   000h
ljmp   start

.org   003h
ljmp   isr

.org   080h
.db    0x50, 0x41, 0x54, 0x54, 0x45, 0x52, 0x4E, 0x2E, 0x74, 0x78, 0x74	; These bytes are ascii for the file name to be read "Pattern.txt"
;===============================================================

.org   100h
start:
	lcall init
main:
	mov   R0,  #30h
	mov   R1,  #10h
	mainLoop:
		mov   a,  @R0
		mov   P1, a
		
		mov    R2, #00h		   ; Wait a second or two
		mov    R3, #00h
		mov    R4, #02h
		delay:
			djnz   R2, delay
			djnz   R3, delay
			djnz   R4, delay
		
		inc   R0
		djnz  R1, mainLoop
	sjmp  main
	
;===============================================================
	
init: 
    mov   tcon, #01h       ; Enable edge triggered interrupts

	mov   IE,   #81h       ; Set the global interrupt enable flag
	
	mov   dptr, #0xFE0D    ; Set up PSoC EmFile to accept a file name and enable the PSoC external interrupt
	mov   a, #0x81
	clr   00h
	movx  @dptr, a
	
	lcall  hold
	
	mov   R0,   #80h
	mov   R1,   #0Bh
	nameSend:			   ; This sends the name of the file to be read to the SD card - "PATTERN.txt"
		mov   dpl,  R0
		mov   dph,  #00h
		movx  a,    @dptr
		
		mov   dptr, #0xFE0E
		clr   00h
		movx  @dptr, a
		
		lcall  hold
		
		inc    R0
		djnz   R1, nameSend
	
	mov   dptr, #0xFE0D    ; Set up PSoC EmFile to read data
	mov   a, #0x83
	clr   00h
	movx  @dptr, a
	
	lcall  hold
	
	mov   R0,   #30h
	mov   R1,   #10h
	dataRead:			          ; This reads the relevant data from the SD card and stuffs it into the scratch-pad memory starting at 0x30
		mov    dptr, #0xFE0E
		mov    a,    #0xFF
		clr    00h
		movx   @dptr, a
		
		lcall  hold
		
		mov    dptr,  #0xFE0E     ; Reads in the first byte of a typed hex number
		movx   a,     @dptr
		lcall  ascbin
		swap   a
		mov    R2,    a
		
		mov    dptr, #0xFE0E
		mov    a,    #0xFF
		clr    00h
		movx   @dptr, a
		
		lcall  hold
		
		mov    dptr,  #0xFE0E     ; Reads in the second byte of a typed hex number
		movx   a,     @dptr
		lcall  ascbin
		orl    a,     R2
	    
		mov    @R0,   a			  ; Stores the byte into the scratchpad memory
		inc    R0
		
		mov    dptr, #0xFE0E	  ; Ignores the following byte to ignore the commas
		mov    a,    #0xFF
		clr    00h
		movx   @dptr, a
		
		lcall  hold

		djnz   R1,   dataRead

	mov   dptr, #0xFE0D	   ; Closes the file
	mov   a, #0x84
	clr   00h         
	movx  @dptr, a
	
	lcall  hold
	
	ret

;===============================================================
	
hold:
	jnb    00h, hold
	ret

;===============================================================
	
isr:
	mov   dptr, #0xFE0D    ; Clear the PSoC EmFile flag thus clearing the external interrupt
	mov   a, #0x80
	movx  @dptr, a
	
	setb  00h
	
	reti                   ; Return 

;===============================================================
; subroutine ascbin
; this routine takes the ascii character passed to it in the
; acc and converts it to a 4 bit binary number which is returned
; in the acc.  If an error occurs, 0 will be stuffed in the acc.
;===============================================================
ascbin:
   clr   c
   add   a,  #0d0h        ; if chr < 30 then error
   jnc   notnum
   clr   c                ; check if chr is 0-9
   add   a,  #0f6h        ; adjust it
   jc    hextry           ; jmp if chr not 0-9
   add   a,  #0ah         ; if it is then adjust it
   ret
hextry:
   clr   acc.5            ; convert to upper
   clr   c                ; check if chr is a-f
   add   a,  #0f9h        ; adjust it
   jnc   notnum           ; if not a-f then error
   clr   c                ; see if char is 46 or less.
   add   a,  #0fah        ; adjust acc
   jc    notnum           ; if carry then not hex
   anl   a,  #0fh         ; clear unused bits
   ret
notnum:
   mov   a,  #00h
   ret
   