⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 spi.asm

📁 bios开发培训涉及例子。可以给大家参考。请在dos下运行。用masm6.0编译。
💻 ASM
字号:



PRINTSTR    MACRO X
        push    dx
        push    ax
	LEA	DX,X
	MOV	AH,09H
	INT	21H
	pop     ax
	pop     dx
ENDM


.model small
.586
.stack  100
.stack
.data
                SPIBAR          dd      0
                bit2            equ     04h
                bit0            equ     01h
                err1            db      'There is an error!$'
                enters          db      10,13,'$'
                space1          db      ' $'
                data1           db      4096 dup(0)
.code

.startup	
                push    es
                xor     ax,ax
                mov     es,ax
                                
                call    Get_RCRB
                add     eax,3020h
                mov     SPIBAR,eax
                
                mov     edx,0ffff0000h          ;address
                mov     si,offset data1
                call    read_sector
                
                mov     si,offset data1
                mov     cx,100h
show1:          mov     al,[si]
                call    show_al
                printstr        space1
                inc     si
                loop    show1
                
                mov     eax,spibar
                call    Init_SPI_Erase
                call    Set_BIOS_EN
                call    write_status
                call    read_status
                
                mov     ebx,spibar
                add     ebx,08h
                mov     al,es:[ebx]
                call    show_al
                and     al,00011100b
                cmp     al,0
                jne     error1

                call    set_sector_erase_com
                call    set_program_com
                                                  
                jmp     end1                
                


error1:         printstr        err1
                printstr        enters
	
end1:           pop     es
	
.exit



Get_RCRB                proc            ;output: eax

                mov     eax,8000f8f0h
                mov     dx,0cf8h
                out     dx,eax
                mov     dx,0cfch
                in      eax,dx
                and     eax,not bit0               
                ret

Get_RCRB                endp


Init_SPI_Read          proc            ;input:es = 00h, eax; 
                
                mov     ebx,eax
                add     ebx,58h
                mov     dword ptr es:[ebx],0ab0bh  ;opcode menu
                mov     ebx,eax
                add     ebx,56h
                mov     word ptr es:[ebx],02h    ;opcode type
                mov     ebx,eax
                add     ebx,54h          ;prefix opcode configuration register
                mov     word ptr es:[ebx],0006h
                ret
                
Init_SPI_Read          endp

;==========================================
;input: eax: spi address; si: memory start address; edx: sector address

read_sector             proc  
             
                push    edi             
                call    Init_SPI_Read
                and     edx,0fffff000h
                mov     cx,64
store:          
                mov     ebx,eax                         
                or      word ptr es:[ebx],bit2      ;clear cycle done
                
                add     ebx,04h
                mov     dword ptr es:[ebx],edx
                sub     ebx,02h
                mov     word ptr es:[ebx],7f06h
                
                mov     ebx,eax
@@:             mov     ax,word ptr es:[ebx]            ;finish??
                test    ax,bit2
                jz      @B

                push    cx      
                mov     ebx,spibar
                add     ebx,08h
                mov     cx,16
store1:         mov     edi,dword ptr es:[ebx]
                mov     [si],edi
                add     si,4
                add     ebx,4
                loop    store1
                pop     cx
                
                add     edx,64
                loop    store                                          
                pop     edi
                ret
read_sector             endp




Init_SPI_Erase          proc            ;input:es = 00h, eax; 
                
                mov     ebx,eax
                add     ebx,58h
                mov     dword ptr es:[ebx],0a1010506h  ;opcode menu
                add     ebx,04h
                mov     dword ptr es:[ebx],02d7f1h
                mov     ebx,eax
                add     ebx,56h
                mov     word ptr es:[ebx],3d11h    ;opcode type
                mov     ebx,eax
                add     ebx,54h          ;prefix opcode configuration register
                mov     word ptr es:[ebx],0006h
                ret
                
Init_SPI_Erase          endp

Set_BIOS_EN     proc 
        
                mov     eax,8000f8dch
                mov     dx,0cf8h
                out     dx,eax
                mov     dx,0cfch
                in      al,dx
                or      al,01h
                out     dx,al
                ret
        
Set_BIOS_EN     endp     

write_status    proc

                mov     ebx,spibar
                add     ebx,00h         
                or      word ptr es:[ebx],bit2      ;clear cycle done
                add     ebx,08h
                mov     byte ptr es:[ebx],02h
                mov     ebx,spibar
                add     ebx,02h                 ;control configuration
                mov     word ptr es:[ebx],4026h         ;
                
                mov     ebx,spibar
@@:             mov     ax,word ptr es:[ebx]            ;finish??
                test    ax,bit2
                jz      @B
                
                ret
write_status    endp

                
read_status     proc
                
                ;read spi device status
                mov     ebx,spibar
                or      word ptr es:[ebx],bit2      ;clear cycle done
                add     ebx,02h
                mov     word ptr es:[ebx],4016h
                
                mov     ebx,spibar
@@:             mov     ax,word ptr es:[ebx]            ;finish??
                test    ax,bit2
                jz      @B
                ret
read_status     endp

set_sector_erase_com           proc
                
        ;;==========   set erase command
                mov     ebx,spibar
                or      word ptr es:[ebx],bit2      ;clear cycle done
                mov     ebx,spibar
                add     ebx,04h
                mov     dword ptr es:[ebx],0ff0000h
                mov     ebx,spibar
                add     ebx,02h
                mov     word ptr es:[ebx],56h
                
                mov     ebx,spibar
@@:             mov     ax,word ptr es:[ebx]            ;finish??
                test    ax,bit2
                jz      @B
                ret
set_sector_erase_com           endp     

set_program_com         proc                       
        ;;==========  set program command
                mov     ebx,spibar
                add     ebx,04h
                mov     dword ptr es:[ebx],0ff0000h       ;address
                add     ebx,04h
                mov     dword ptr es:[ebx],'cniV'       ;data0
                add     ebx,04h
                mov     dword ptr es:[ebx],'_tne'       ;data1
                add     ebx,04h
                mov     dword ptr es:[ebx],'uW'       ;data2
                               
                mov     ebx,spibar
                add     ebx,02h
                mov     word ptr es:[ebx],4b66h
                
                mov     ebx,spibar
@@:             mov     ax,word ptr es:[ebx]            ;finish??
                test    ax,bit2
                jz      @B  
                ret              
set_program_com         endp    
	
show_al         proc    far     public

                push    ax
                push    dx
                mov     dh,al
                and     al,0f0h
                shr     al,4
                add     al,30h
                cmp     al,3ah
                jb      doing
                add     al,7
doing:          mov     dl,al
                mov     ah,2
                int     21h                                   
	
	        mov     al,dh
                and     al,0fh
                add     al,30h
                cmp     al,3ah
                jb      doing1
                add     al,7
doing1:         mov     dl,al
                mov     ah,2
                int     21h  	
                pop     dx
                pop     ax
                ret
show_al		endp              
                
end
	

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -