OpenCores
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] - Blame information for rev 3

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 3 ndumitrach
; This file is part of the Next186 SoC PC project
2
; http://opencores.org/project,next186
3
 
4
; Filename: bootstrap.asm
5
; Description: Part of the Next186 SoC PC project, bootstrap "ROM" code (RAM initialized with cache)
6
; Version 1.0
7
; Creation date: Jun2013
8
 
9
; Author: Nicolae Dumitrache
10
; e-mail: ndumitrache@opencores.org
11
 
12
; -------------------------------------------------------------------------------------
13
 
14
; Copyright (C) 2013 Nicolae Dumitrache
15
 
16
; This source file may be used and distributed without
17
; restriction provided that this copyright statement is not
18
; removed from the file and that any derivative work contains
19
; the original copyright notice and the associated disclaimer.
20
 
21
; This source file is free software; you can redistribute it
22
; and/or modify it under the terms of the GNU Lesser General
23
; Public License as published by the Free Software Foundation;
24
; either version 2.1 of the License, or (at your option) any
25
; later version.
26
 
27
; This source is distributed in the hope that it will be
28
; useful, but WITHOUT ANY WARRANTY; without even the implied
29
; warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
30
; PURPOSE. See the GNU Lesser General Public License for more
31
; details.
32
 
33
; You should have received a copy of the GNU Lesser General
34
; Public License along with this source; if not, download it
35
; from http://www.opencores.org/lgpl.shtml
36
 
37
; -----------------------------------------------------------------------
38
 
39
; Additional Comments:
40
; Assembled with MASM v6.14.8444
41
; No hardware resources are required for the bootstrap ROM, I use only the initial value of the cache memory
42
; BIOS will be read from the last BIOSSIZE sectors of SD Card and placed in DRAM at F000:(-BIOSSIZE*512)
43
; SD HC card required
44
 
45
 
46
 
47
.186
48
.model tiny
49
.code
50
 
51
 
52
BIOSSIZE    EQU     16      ; sectors
53
BOOTOFFSET  EQU     0fc00h  ; bootstrap code offset in segment 0f000h
54
 
55
; this code is for bootstrap deployment only, it will not be present in ROM (cache)
56
;---------------- EXECUTE -----------------
57
                org             100h        ; this code is loaded at 0f000h:100h
58
exec    label near
59
 
60
                mov             si, begin
61
                mov             di, BOOTOFFSET
62
                mov             cx, 256*4/2 ; last 4 cache lines (from total 8)
63
                rep             movsw
64
                db              0eah
65
                dw              0, -1       ; CPU reset, execute bootstrap
66
 
67
 
68
; Loads BIOS (8K = 16 sectors) from last sectors of SD card (if present)
69
; If no SD card detected, wait on RS232 115200bps and load program at F000:100h
70
; the following code is placed in the last 1kB of cache (last 4 lines), each with the dirty bit set
71
; the corresponding position in RAM will be F000:BOOTOFFSET
72
; ----------------- RS232 bootstrap - last 256byte cache line ---------------
73
        org     200h
74
begin label far               ; this code is placed at F000:BOOTOFFSET
75
 
76
                cli
77
                cld
78
                mov             ax, cs        ; cs = 0f000h
79
                mov             ds, ax
80
                mov             es, ax
81
                mov             ss, ax
82
                mov             sp, BOOTOFFSET
83
                xor             ax, ax        ; map seg0
84
                out             80h, ax
85
                mov             al, 0bh       ; map text segB
86
                out             8bh, ax
87
                mov             al, 0fh       ; map ROM segF
88
                out             8fh, ax
89
                mov             al, 34h
90
                out             43h, al
91
                xor             al, al
92
                out             40h, al
93
                out             40h, al       ; program PIT for RS232
94
 
95
                call            sdinit_
96
                test            ax, ax
97
                jz              short RS232
98
                mov             dx, ax
99
                shr             dx, 6
100
                shl             ax, 10
101
                mov             cx, BIOSSIZE       ;  sectors
102
                sub             ax, cx
103
                sbb             dx, 0
104
                xor             bx, bx       ; read BIOSSIZE/2 KB BIOS at 0f000h:0h
105
nextsect:
106
                push            ax
107
                push            dx
108
                push            cx
109
                call            sdread_
110
                dec             cx
111
                pop             cx
112
                pop             dx
113
                pop             ax
114
                jnz             short RS232  ; cx was not 1
115
                add             ax, 1
116
                adc             dx, 0
117
                add             bx, 512
118
                loop            nextsect
119
                cmp             word ptr ds:[0], 'eN'
120
                jne             short RS232
121
                cmp             word ptr ds:[2], 'tx'
122
                je              short   BIOSOK
123
 
124
RS232:
125
                mov             dx, 3c0h
126
                mov             al, 10h
127
                out             dx, al
128
                mov             al, 8h
129
                out             dx, al      ; set text mode
130
                mov             dx, 3d4h
131
                mov             al, 0ah
132
                out             dx, al
133
                inc             dx
134
                mov             al, 1 shl 5 ; hide cursor
135
                out             dx, al
136
                dec             dx
137
                mov             al, 0ch
138
                out             dx, al
139
                inc             dx
140
                mov             al, 0
141
                out             dx, al
142
                dec             dx
143
                mov             al, 0dh
144
                out             dx, al
145
                inc             dx
146
                mov             al, 0
147
                out             dx, al      ; reset video offset
148
 
149
                push            0b800h      ; clear screen
150
                pop             es
151
                xor             di, di
152
                mov             cx, 25*80
153
                xor             ax, ax
154
                rep             stosw
155
 
156
                mov             dx, 3c8h    ; set palette entry 1
157
                mov             ax, 101h
158
                out             dx, al
159
                inc             dx
160
                mov             al, 2ah
161
                out             dx, al
162
                out             dx, al
163
                out             dx, al
164
 
165
                xor             di, di
166
                mov             si, booterrmsg + BOOTOFFSET - begin
167
                lodsb
168
nextchar:
169
                stosw
170
                lodsb
171
                test            al, al
172
                jnz             short nextchar
173
 
174
                mov             bh, 8
175
flush:
176
                mov             al, [bx]
177
                dec             bh
178
                jnz             flush
179
 
180
                mov             si, 100h
181
                call            srecb
182
                mov             bh, ah
183
                call            srecb
184
                mov             bl, ah
185
 
186
sloop:
187
                call            srecb
188
                mov             [si], ah
189
                inc             si
190
                dec             bx
191
                jnz             sloop
192
                xor             sp, sp
193
                mov             ss, sp
194
                db              0eah
195
                dw              100h,0f000h ; execute loaded program
196
 
197
BIOSOK:
198
                mov             si, reloc + BOOTOFFSET - begin
199
                mov             di, bx
200
                mov             cx, endreloc - reloc
201
                rep             movsb       ; relocate code from reloc to endreloc after loaded BIOS
202
                mov             di, -BIOSSIZE*512
203
                xor             si, si
204
                mov             cx, BIOSSIZE*512/2
205
                jmp             bx
206
reloc:
207
                rep             movsw
208
                db              0eah
209
                dw              0, -1       ; CPU reset, execute BIOS
210
endreloc:
211
 
212
 
213
; ----------------  serial receive byte 115200 bps --------------
214
srecb:
215
                mov             ah, 80h
216
                mov             dx, 3dah
217
                mov             cx, -5aeh ; (half start bit)
218
srstb:
219
                in              al, dx
220
                shr             al, 2
221
                jc              srstb
222
 
223
                in              al, 40h ; lo counter
224
                add             ch, al
225
                in              al, 40h ; hi counter, ignore
226
l1:
227
                call            dlybit
228
                in              al, dx
229
                shr             al, 2
230
                rcr             ah, 1
231
                jnc             l1
232
dlybit:
233
                sub             cx, 0a5bh  ;  (full bit)
234
dly1:
235
                in              al, 40h
236
                cmp             al, ch
237
                in              al, 40h
238
                jnz             dly1
239
                ret
240
 
241
;---------------------  read/write byte ----------------------
242
sdrb:
243
                mov             al, 0ffh
244
sdsb:               ; in AL=byte, DX = 03dah, out AX=result
245
                mov             ah, 1
246
sdsb1:
247
                out             dx, al
248
                add             ax, ax
249
                jnc             sdsb1
250
                in              ax, dx
251
                ret
252
 
253
;---------------------  write block ----------------------
254
sdwblk:              ; in DS:SI=data ptr, DX=03dah, CX=size
255
                lodsb
256
                call            sdsb
257
                loop            sdwblk
258
                ret
259
 
260
;---------------------  read block ----------------------
261
sdrblk:              ; in DS:DI=data ptr, DX=03dah, CX=size
262
                call            sdrb
263
                mov             [di], ah
264
                inc             di
265
                loop            sdrblk
266
                ret
267
 
268
;---------------------  write command ----------------------
269
sdcmd8T:
270
                call    sdrb
271
sdcmd:              ; in DS:SI=6 bytes cmd buffer, DX=03dah, out AH = 0ffh on error
272
                mov             cx, 6
273
                call            sdwblk
274
sdresp:
275
                xor             si, si
276
sdresp1:
277
                call            sdrb
278
                inc             si
279
                jz              sdcmd1
280
                cmp             ah, 0ffh
281
                je              sdresp1
282
sdcmd1:
283
                ret
284
 
285
;---------------------  read one sector ----------------------
286
sdread_ proc near   ; DX:AX sector, DS:BX buffer, returns CX=read sectors
287
                push            ax
288
                mov             al, dl
289
                push            ax
290
                mov             dl, 51h     ; CMD17
291
                push            dx
292
                mov             si, sp
293
 
294
                mov             dx, 3dah
295
                mov             ah, 1
296
                out             dx, ax      ; CS on
297
                call            sdcmd
298
                add             sp, 6
299
                or              ah, ah
300
                jnz             sdr1        ; error (cx=0)
301
                call            sdresp      ; wait for 0feh token
302
                cmp             ah, 0feh
303
                jne             sdr1        ; read token error (cx=0)
304
                mov             ch, 2       ; 512 bytes
305
                mov             di, bx
306
                call            sdrblk
307
                call            sdrb        ; ignore CRC
308
                call            sdrb        ; ignore CRC
309
                inc             cx          ; 1 block
310
 sdr1:
311
                xor             ax, ax
312
                out             dx, ax
313
                call            sdrb        ; 8T
314
                ret
315
sdread_ endp
316
 
317
;---------------------  init SD ----------------------
318
sdinit_ proc near       ; returns AX = num kilosectors
319
                mov             dx, 3dah
320
                mov             cx, 10
321
sdinit1:                   ; send 80T
322
                call            sdrb
323
                loop            sdinit1
324
 
325
                mov             ah, 1
326
                out             dx, ax       ; select SD
327
 
328
                mov             si, SD_CMD0 + BOOTOFFSET - begin
329
                call            sdcmd
330
                dec             ah
331
                jnz             sdexit      ; error
332
 
333
                mov             si, SD_CMD8 + BOOTOFFSET - begin
334
                call            sdcmd8T
335
                dec             ah
336
                jnz             sdexit      ; error
337
                mov             cl, 4
338
                sub             sp, cx
339
                mov             di, sp
340
                call            sdrblk
341
                pop             ax
342
                pop             ax
343
                cmp             ah, 0aah
344
                jne             sdexit      ; CMD8 error
345
repinit:
346
                mov             si, SD_CMD55 + BOOTOFFSET - begin
347
                call            sdcmd8T
348
                call            sdrb
349
                mov             si, SD_CMD41 + BOOTOFFSET - begin
350
                call            sdcmd
351
                dec             ah
352
                jz              repinit
353
 
354
                mov             si, SD_CMD58 + BOOTOFFSET - begin
355
                call            sdcmd8T
356
                mov             cl, 4
357
                sub             sp, cx
358
                mov             di, sp
359
                call            sdrblk
360
                pop             ax
361
                test            al, 40h     ; test OCR bit 30 (CCS)
362
                pop             ax
363
                jz              sdexit      ; no SDHC
364
 
365
                mov             si, SD_CMD9 + BOOTOFFSET - begin ; get size info
366
                call            sdcmd8T
367
                or              ah, ah
368
                jnz             sdexit
369
                call            sdresp      ; wait for 0feh token
370
                cmp             ah, 0feh
371
                jne             sdexit
372
                mov             cl, 18      ; 16bytes + 2bytes CRC
373
                sub             sp, cx
374
                mov             di, sp
375
                call            sdrblk
376
                mov             cx, [di-10]
377
                xchg            cl, ch
378
                inc             cx
379
                mov             sp, di
380
sdexit:
381
                xor             ax, ax      ; raise CS
382
                out             dx, ax
383
                call    sdrb
384
                mov             ax, cx
385
                ret
386
sdinit_ endp
387
 
388
 
389
booterrmsg  db  'BIOS not present on SDCard last 8KB, waiting on RS232 (115200bps, f000:100) ...', 0
390
SD_CMD0         db              40h, 0, 0, 0, 0, 95h
391
SD_CMD8         db              48h, 0, 0, 1, 0aah, 087h
392
SD_CMD9         db              49h, 0, 0, 0, 0, 0ffh
393
SD_CMD41        db              69h, 40h, 0, 0, 0, 0ffh
394
SD_CMD55        db              77h, 0, 0, 0, 0, 0ffh
395
SD_CMD58        db              7ah, 0, 0, 0, 0, 0ffh
396
 
397
 
398
; ---------------- RESET ------------------
399
                org 05f0h
400
start:
401
                db              0eah
402
                dw              BOOTOFFSET, 0f000h
403
                db              0,0,0,0,0,0,0,0,0,0,0
404
 
405
end exec

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.