URL
https://opencores.org/ocsvn/next186_soc_pc/next186_soc_pc/trunk
Subversion Repositories next186_soc_pc
[/] [next186_soc_pc/] [trunk/] [SW/] [bootload_BIOS_SD/] [bootstrap.asm] - Rev 3
Compare with Previous | Blame | View Log
; This file is part of the Next186 SoC PC project; http://opencores.org/project,next186; Filename: bootstrap.asm; Description: Part of the Next186 SoC PC project, bootstrap "ROM" code (RAM initialized with cache); Version 1.0; Creation date: Jun2013; Author: Nicolae Dumitrache; e-mail: ndumitrache@opencores.org; -------------------------------------------------------------------------------------; Copyright (C) 2013 Nicolae Dumitrache; This source file may be used and distributed without; restriction provided that this copyright statement is not; removed from the file and that any derivative work contains; the original copyright notice and the associated disclaimer.; This source file is free software; you can redistribute it; and/or modify it under the terms of the GNU Lesser General; Public License as published by the Free Software Foundation;; either version 2.1 of the License, or (at your option) any; later version.; This source is distributed in the hope that it will be; useful, but WITHOUT ANY WARRANTY; without even the implied; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR; PURPOSE. See the GNU Lesser General Public License for more; details.; You should have received a copy of the GNU Lesser General; Public License along with this source; if not, download it; from http://www.opencores.org/lgpl.shtml; -----------------------------------------------------------------------; Additional Comments:; Assembled with MASM v6.14.8444; No hardware resources are required for the bootstrap ROM, I use only the initial value of the cache memory; BIOS will be read from the last BIOSSIZE sectors of SD Card and placed in DRAM at F000:(-BIOSSIZE*512); SD HC card required.186.model tiny.codeBIOSSIZE EQU 16 ; sectorsBOOTOFFSET EQU 0fc00h ; bootstrap code offset in segment 0f000h; this code is for bootstrap deployment only, it will not be present in ROM (cache);---------------- EXECUTE -----------------org 100h ; this code is loaded at 0f000h:100hexec label nearmov si, beginmov di, BOOTOFFSETmov cx, 256*4/2 ; last 4 cache lines (from total 8)rep movswdb 0eahdw 0, -1 ; CPU reset, execute bootstrap; Loads BIOS (8K = 16 sectors) from last sectors of SD card (if present); If no SD card detected, wait on RS232 115200bps and load program at F000:100h; the following code is placed in the last 1kB of cache (last 4 lines), each with the dirty bit set; the corresponding position in RAM will be F000:BOOTOFFSET; ----------------- RS232 bootstrap - last 256byte cache line ---------------org 200hbegin label far ; this code is placed at F000:BOOTOFFSETclicldmov ax, cs ; cs = 0f000hmov ds, axmov es, axmov ss, axmov sp, BOOTOFFSETxor ax, ax ; map seg0out 80h, axmov al, 0bh ; map text segBout 8bh, axmov al, 0fh ; map ROM segFout 8fh, axmov al, 34hout 43h, alxor al, alout 40h, alout 40h, al ; program PIT for RS232call sdinit_test ax, axjz short RS232mov dx, axshr dx, 6shl ax, 10mov cx, BIOSSIZE ; sectorssub ax, cxsbb dx, 0xor bx, bx ; read BIOSSIZE/2 KB BIOS at 0f000h:0hnextsect:push axpush dxpush cxcall sdread_dec cxpop cxpop dxpop axjnz short RS232 ; cx was not 1add ax, 1adc dx, 0add bx, 512loop nextsectcmp word ptr ds:[0], 'eN'jne short RS232cmp word ptr ds:[2], 'tx'je short BIOSOKRS232:mov dx, 3c0hmov al, 10hout dx, almov al, 8hout dx, al ; set text modemov dx, 3d4hmov al, 0ahout dx, alinc dxmov al, 1 shl 5 ; hide cursorout dx, aldec dxmov al, 0chout dx, alinc dxmov al, 0out dx, aldec dxmov al, 0dhout dx, alinc dxmov al, 0out dx, al ; reset video offsetpush 0b800h ; clear screenpop esxor di, dimov cx, 25*80xor ax, axrep stoswmov dx, 3c8h ; set palette entry 1mov ax, 101hout dx, alinc dxmov al, 2ahout dx, alout dx, alout dx, alxor di, dimov si, booterrmsg + BOOTOFFSET - beginlodsbnextchar:stoswlodsbtest al, aljnz short nextcharmov bh, 8flush:mov al, [bx]dec bhjnz flushmov si, 100hcall srecbmov bh, ahcall srecbmov bl, ahsloop:call srecbmov [si], ahinc sidec bxjnz sloopxor sp, spmov ss, spdb 0eahdw 100h,0f000h ; execute loaded programBIOSOK:mov si, reloc + BOOTOFFSET - beginmov di, bxmov cx, endreloc - relocrep movsb ; relocate code from reloc to endreloc after loaded BIOSmov di, -BIOSSIZE*512xor si, simov cx, BIOSSIZE*512/2jmp bxreloc:rep movswdb 0eahdw 0, -1 ; CPU reset, execute BIOSendreloc:; ---------------- serial receive byte 115200 bps --------------srecb:mov ah, 80hmov dx, 3dahmov cx, -5aeh ; (half start bit)srstb:in al, dxshr al, 2jc srstbin al, 40h ; lo counteradd ch, alin al, 40h ; hi counter, ignorel1:call dlybitin al, dxshr al, 2rcr ah, 1jnc l1dlybit:sub cx, 0a5bh ; (full bit)dly1:in al, 40hcmp al, chin al, 40hjnz dly1ret;--------------------- read/write byte ----------------------sdrb:mov al, 0ffhsdsb: ; in AL=byte, DX = 03dah, out AX=resultmov ah, 1sdsb1:out dx, aladd ax, axjnc sdsb1in ax, dxret;--------------------- write block ----------------------sdwblk: ; in DS:SI=data ptr, DX=03dah, CX=sizelodsbcall sdsbloop sdwblkret;--------------------- read block ----------------------sdrblk: ; in DS:DI=data ptr, DX=03dah, CX=sizecall sdrbmov [di], ahinc diloop sdrblkret;--------------------- write command ----------------------sdcmd8T:call sdrbsdcmd: ; in DS:SI=6 bytes cmd buffer, DX=03dah, out AH = 0ffh on errormov cx, 6call sdwblksdresp:xor si, sisdresp1:call sdrbinc sijz sdcmd1cmp ah, 0ffhje sdresp1sdcmd1:ret;--------------------- read one sector ----------------------sdread_ proc near ; DX:AX sector, DS:BX buffer, returns CX=read sectorspush axmov al, dlpush axmov dl, 51h ; CMD17push dxmov si, spmov dx, 3dahmov ah, 1out dx, ax ; CS oncall sdcmdadd sp, 6or ah, ahjnz sdr1 ; error (cx=0)call sdresp ; wait for 0feh tokencmp ah, 0fehjne sdr1 ; read token error (cx=0)mov ch, 2 ; 512 bytesmov di, bxcall sdrblkcall sdrb ; ignore CRCcall sdrb ; ignore CRCinc cx ; 1 blocksdr1:xor ax, axout dx, axcall sdrb ; 8Tretsdread_ endp;--------------------- init SD ----------------------sdinit_ proc near ; returns AX = num kilosectorsmov dx, 3dahmov cx, 10sdinit1: ; send 80Tcall sdrbloop sdinit1mov ah, 1out dx, ax ; select SDmov si, SD_CMD0 + BOOTOFFSET - begincall sdcmddec ahjnz sdexit ; errormov si, SD_CMD8 + BOOTOFFSET - begincall sdcmd8Tdec ahjnz sdexit ; errormov cl, 4sub sp, cxmov di, spcall sdrblkpop axpop axcmp ah, 0aahjne sdexit ; CMD8 errorrepinit:mov si, SD_CMD55 + BOOTOFFSET - begincall sdcmd8Tcall sdrbmov si, SD_CMD41 + BOOTOFFSET - begincall sdcmddec ahjz repinitmov si, SD_CMD58 + BOOTOFFSET - begincall sdcmd8Tmov cl, 4sub sp, cxmov di, spcall sdrblkpop axtest al, 40h ; test OCR bit 30 (CCS)pop axjz sdexit ; no SDHCmov si, SD_CMD9 + BOOTOFFSET - begin ; get size infocall sdcmd8Tor ah, ahjnz sdexitcall sdresp ; wait for 0feh tokencmp ah, 0fehjne sdexitmov cl, 18 ; 16bytes + 2bytes CRCsub sp, cxmov di, spcall sdrblkmov cx, [di-10]xchg cl, chinc cxmov sp, disdexit:xor ax, ax ; raise CSout dx, axcall sdrbmov ax, cxretsdinit_ endpbooterrmsg db 'BIOS not present on SDCard last 8KB, waiting on RS232 (115200bps, f000:100) ...', 0SD_CMD0 db 40h, 0, 0, 0, 0, 95hSD_CMD8 db 48h, 0, 0, 1, 0aah, 087hSD_CMD9 db 49h, 0, 0, 0, 0, 0ffhSD_CMD41 db 69h, 40h, 0, 0, 0, 0ffhSD_CMD55 db 77h, 0, 0, 0, 0, 0ffhSD_CMD58 db 7ah, 0, 0, 0, 0, 0ffh; ---------------- RESET ------------------org 05f0hstart:db 0eahdw BOOTOFFSET, 0f000hdb 0,0,0,0,0,0,0,0,0,0,0end exec
