-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbios.asm
692 lines (567 loc) · 14 KB
/
bios.asm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
cpu 8086
%include "config.inc"
%include "ioports.inc"
org 0
[BITS 16]
image_start:
; BIOS parameter block
; Do not move!
%include "data\biosdata.asm"
align 8
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Entry point
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
start:
; For real 286+ CS register is normalized here
; So address will be 0x000F0000 instead of 0xFFFF0000
cli
cld
; If we have 386EX CPU we may need to program Chip Select Unit
; as soon as possible
%include "drivers\chipselect.asm"
; Setup segments
mov ax, 0x40
mov ds, ax
; Check if this restart is just an exit from protected mode
; and we need to resume execution of user program
mov ax, [pmode_exit_cs]
or ax, [pmode_exit_ip]
jz normal_restart
; Zero values or the system will not restart properly
mov word [pmode_exit_cs], 0
mov word [pmode_exit_ip], 0
; Check NVRAM for a protected mode exit flag
mov al, 0x0F
out 0x70, al
jmp $+2
in al, 0x71
cmp al, 0x5
je pmode_exit
cmp al, 0xA
jne normal_restart
pmode_exit:
; Resume execution in real mode
push word [pmode_exit_cs]
push word [pmode_exit_ip]
retf
normal_restart:
; Send '1' to debug port to notify that BIOS and debug console both working
mov al, '1'
out DEBUG_UART, al
; Setup stack
xor ax, ax
mov ss, ax
mov sp, 0x400
; Set FPGA / emulator video controller to text mode
mov al, 3
out PORT_VMODE, al
; Set interrupt controller (PICs 8259) registers
%include "drivers\pic.asm"
; Send '2' to debug port to notify that PIC initialized
mov al, '2'
out DEBUG_UART, al
; Create an empty interrupt table
xor ax, ax
mov ds, ax
mov es, ax
xor di, di
mov cx, 192
erase_ints2:
mov ax, empty_int
stosw
mov ax, cs
stosw
loop erase_ints2
; Fill used vectors
mov [0x00 * 4], word int00
mov [0x01 * 4], word int01
mov [0x02 * 4], word int02
mov [0x03 * 4], word int03
mov [0x04 * 4], word int04
mov [0x05 * 4], word int05
mov [0x06 * 4], word int06
mov [0x07 * 4], word int07
mov [0x08 * 4], word int08
mov [0x09 * 4], word int09
mov [0x0A * 4], word empty_hw_int
mov [0x0B * 4], word empty_hw_int
mov [0x0C * 4], word int0c
mov [0x0D * 4], word empty_hw_int
mov [0x0E * 4], word empty_hw_int
mov [0x0F * 4], word empty_hw_int
mov [0x10 * 4], word int10
mov [0x11 * 4], word int11
mov [0x12 * 4], word int12
mov [0x13 * 4], word int13
mov [0x14 * 4], word int14
mov [0x15 * 4], word int15
mov [0x16 * 4], word int16
mov [0x17 * 4], word int17
mov [0x19 * 4], word int19
mov [0x1A * 4], word int1a
mov [0x1D * 4], word video_init_table
mov [0x1E * 4], word int1e
mov [0x41 * 4], word int41
; Set CGA glyph table address
mov [0x43 * 4 + 2], word 0xF000
mov [0x43 * 4], word 0xFA6E
; Copy BIOS data
mov ax, 0x0000
mov es, ax
mov di, 0x400
mov ax, cs
mov ds, ax
mov si, bios_data
mov cx, 0x100 + bios_data_end - bios_data + 1
rep movsb
; Display 'R' on the left top corner of the screen
mov ax, 0xB800
mov ds, ax
mov word [0], 0x0F00 + 'R'
; Send '3' to debug port to notify that all going ok
mov al, '3'
out DEBUG_UART, al
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Before this point there were only writes to RAM ;
; Now we should try to read ;
;
; If your RAM controller is not working properly ;
; or add-on ROM chips generate an error ;
; you will see "123" in the debug console and 'R' in the left top ;
; corner of the screen and the system will hang or restart ;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Check if we have additional BIOS chips
%if (USE_ADDON_ROMS == 1)
mov ax, 0x40
mov ds, ax
mov word [bios_temp + 2], 0xC000
mov word [bios_temp], 2
scan_roms:
mov ax, [bios_temp + 2]
mov es, ax
cmp [es:0], word 0xAA55
jnz no_rom
call far [bios_temp]
no_rom:
mov ax, 0x40
mov ds, ax
mov ax, 0x1000
add [bios_temp + 2], ax
cmp word [bios_temp + 2], 0xF000
jnz scan_roms
%endif
; Send '4' to debug port to notify that we are going to use
; stack, interrupts and read from RAM
mov al, '4'
out DEBUG_UART, al
; Set the text mode normal way
mov ax, 3
int 0x10
; Send '5' to debug port to notify that RAM is working properly
mov al, '5'
out DEBUG_UART, al
mov al, 13
out DEBUG_UART, al
mov al, 10
out DEBUG_UART, al
; Display welcome message
mov si, msg_reset
call putsv
; Enable A31..A20 lines
call a20_enable
; Check BIOS parameter block alignment
%if ((check_size - bios_data) != 0xA8)
%error BIOS parameter block data offset detected!
%endif
; Initialize COM-port
; mov ax, 0
; int 0x14
mov al, 13
call putchv
mov al, 10
call putchv
; This is a good place to activate CPU's PLL to run at full speed
; The memory test performed at the next step will help to check
; system stability in a full speed mode
%if (ACTIVATE_PLL == 1)
%include "drivers\pll.asm"
call pll_enable
%endif
%if (RAM_TEST == 1)
; Perform 0 - 640 KB RAM test
; Can be runned several times
mov bp, 17171
begin_memory_test:
push bp
call memory_test
pop bp
add bp, 52371
%if (CONTINUOUS_RAM_TEST == 1)
jmp begin_memory_test
%endif
mov al, 13
call putchv
mov al, 10
call putchv
%endif
; Memory test OK, so now we can enable CPU's cache
%if (ENABLE_CACHE == 1)
%include "drivers\cache.asm"
call cache_enable
%endif
; Timer 1: 15 us / 0x12
; We don't need old-style timer DRAM regeneration so will use default value
mov al, 0x54
out 0x43, al
mov al, 0x12
out 0x41, al
mov al, 0x00
out 0x41, al
mov al, 0x40
out 0x43, al
; Timer 2: 1 ms / 0x4A9
; Can be programmed to 2 or 4 KHz to produce loud beeps
mov al, 0xB6
out 0x43, al
mov al, 0xA9
out 0x42, al
mov al, 0x04
out 0x42, al
mov al, 0x40
out 0x43, al
; Timer 0: 55 ms / 0xFFFF
; System timer connected to IRQ0
mov al, 0x36
out 0x43, al
mov al, 0
out 0x40, al
out 0x40, al
; Ok, lets try to initialize FPGA's USB1 and USB2 for keyboard and mouse
%if (USE_COMPACT_FPGA_USB == 1)
mov si, msg_usb1_init
call putsv
call usb1_init
mov si, msg_ok
call putsv
mov si, msg_usb2_init
call putsv
call usb1_init
mov si, msg_ok
call putsv
%endif
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
mov ax, 0x0000
mov es, ax
%if (RUN_ROM_BASIC == 1)
jmp (0xF600):0
%endif
%if (CLEAR_HMA)
; Need to clear HMA
mov ax, 0xFFFF
mov es, ax
mov di, 0x10
mov cx, 32768 - 8
xor ax, ax
rep stosw
%endif
%if (USE_SPI_SDCARD == 1)
; Try to find and initialize SD-card
call sd_init
%endif
; If we reached this point -> we have disk and can boot now
mov si, msg_bootsector
call putsv
; Load a very first sector from default boot drive
xor ax, ax
mov es, ax
mov bx, 0x7C00
mov ax, 0x201
mov dx, BOOT_DRIVE
mov cx, 1
int 0x13
xor ax, ax
mov es, ax
mov ds, ax
; Display on screen first bytes and boot signature of the received data
; If all ok you will see something like FA33C08E...55AA on screen
; and in the debug console
mov si, 0x7C00
lodsb
call print_hexv
lodsb
call print_hexv
lodsb
call print_hexv
lodsb
call print_hexv
mov al, '.'
call putchv
mov al, '.'
call putchv
mov al, '.'
call putchv
mov si, 0x7DFE
lodsb
call print_hexv
lodsb
call print_hexv
mov si, msg_crlf
call putsv
; Enable interrupts
mov al, 0x20
out 0x20, al
sti
%if (LEAVE_A20_ENABLED != 1)
; Disable A20
call a20_disable
%endif
; Set general register default values
xor ax, ax
xor bx, bx
xor cx, cx
mov dx, BOOT_DRIVE ; Boot drive code should be in DL
xor si, si
xor di, di
xor bp, bp
; Start OS
jmp (0x0000):0x7C00
%include "console.asm"
%if (USE_COMPACT_FPGA_USB == 1)
%include "drivers\compact_fpga_usb.asm"
%include "drivers\compact_fpga_hid.asm"
%endif
; Memory test
; In:
; BP - random value
%if (RAM_TEST == 1)
memory_test:
mov si, msg_testingmemory
call putsv
cli
xor di, di
mov ds, di
mov di, 0x800
mov bx, bp
memory_test_fill:
mov [di], bx
add bx, 531
add di, 2
jnz memory_test_fill
xor di, di
mov ax, ds
add ax, 0x1000
mov ds, ax
cmp ax, 0xA000
jnz memory_test_fill
xor di, di
mov ds, di
mov di, 0x800
mov bx, bp
memory_test_cmp:
cmp bx, [di]
jne memory_test_fail
add bx, 531
add di, 2
jnz memory_test_cmp
mov ax, ds
add ax, 0x1000
mov ds, ax
push bx
push ds
mov si, msg_testingmemory
call putsv
pop ds
push ds
mov ax, ds
xor dx, dx
mov bx, 64
div bx
call print_u16v
mov si, msg_kbok
call putsv
pop ds
pop bx
mov ax, ds
xor di, di
cmp ax, 0xA000
jnz memory_test_cmp
ret
memory_test_fail:
mov al, 'F'
out DEBUG_UART, al
mov si, msg_memorytestfailed
call putsv
cli
jmp $
%endif
; Interrupt return with Z or C flag set/reset
iret_carry_on:
stc
iret_carry:
push bp
mov bp, sp
jnc iret_carry_off1
or word [bp+6], 1
pop bp
iret
iret_carry_off1:
and word [bp+6], 0xFFFE
pop bp
iret
iret_carry_off:
clc
jmp iret_carry
iret_zero:
push bp
mov bp, sp
jnz iret_zero_off1
or word [bp+6], 0x40
pop bp
iret
iret_zero_off1:
and word [bp+6], 0xFFBF
pop bp
iret
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; Interrupts
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
empty_hw_int:
push ax
mov al, 0x20
out 0x20, al
pop ax
iret
empty_int:
iret
%include "isr\traps.asm"
%include "isr\int08_timer.asm"
%include "isr\int09_keyboard.asm"
%include "isr\int0c_comm.asm"
%include "isr\int10_video.asm"
%include "isr\int11.asm"
%include "isr\int12.asm"
%include "isr\int13_disk.asm"
%include "isr\int14_comm.asm"
%include "isr\int15_at.asm"
%include "isr\int16_keyboard.asm"
%include "isr\int17.asm"
%include "isr\int19.asm"
%include "isr\int1a_rtc.asm"
; SPI controller for SD-card or W25Q128 flash ROM
%include "drivers\spi.asm"
; BIOS messages
msg_reset:
db "x86 embedded BIOS R3", 13, 10, "github.com/b-dmitry1/BIOS", 13, 10, 0
msg_usb1_init:
db "USB 1 init", 0
msg_usb2_init:
db "USB 2 init", 0
msg_ok:
db "OK", 13, 10, 0
msg_crlf:
db 13, 10, 0
msg_testingmemory:
db 13, "Testing RAM: ", 0
msg_memorytestfailed:
db 13, 10, "RAM test FAILED", 13, 10, "System halted", 0
msg_kbok:
db " KB OK ", 0
msg_bootsector:
db 13, 10, "Boot sector: ", 0
msg_failed:
db 'failed', 0
msg_sd_init:
db 13, 10, 13, 10, "HDD0: ", 0
msg_sd_init_error:
db "not found", 13, 10, 0
msg_sd_init_mmc:
db "MMC", 13, 10, 0
msg_sd_init_sdv1:
db "SDv1", 13, 10, 0
msg_sd_init_sdv2:
db "SDv2", 13, 10, 0
msg_sd_init_sdv2_block:
db "SD mode: Block", 13, 10, 0
; Tables
int1e:
db 0xDF ; Step rate 2ms, head unload time 240ms
db 0x02 ; Head load time 4 ms, non-DMA mode 0
db 0x25 ; Byte delay until motor turned off
db 0x02 ; 512 bytes per sector
floppy_sectors_per_track:
db 18 ; 18 sectors per track (1.44MB)
db 0x1B ; Gap between sectors for 3.5" floppy
db 0xFF ; Data length (ignored)
db 0x54 ; Gap length when formatting
db 0xF6 ; Format filler byte
db 0x0F ; Head settle time (1 ms)
db 0x08 ; Motor start time in 1/8 seconds
video_static_table:
; https://dos4gw.org/VGA_Static_Functionality_Table
db 0x7f ; bits 0 .. 7 = modes 00h .. 07h supported
db 0xff ; bits 0 .. 7 = modes 08h .. 0fh supported
db 0x0f ; bits 0 .. 3 = modes 10h .. 13h supported
dd 0 ; IBM reserved
db 0x07 ; scan lines suppported: bit 0 = 200, 1 = 350, 2 = 400
db 0x08 ; font blocks available in text mode (4 = EGA, 8 = VGA)
db 0x02 ; maximum active font blocks in text mode (2 = EGA èëè VGA)
db 0xfd ; misc support flags
db 0x08 ; misc capabilities (DCC support)
dw 0x00 ; reserved
db 0x3C ; save pointer function flags
db 0x00 ; reserved
video_init_table:
; https://dos4gw.org/Video_Initialization_Table
abRegs40x25:
db 0x39, 0x28, 0x2d, 0x10, 0x1f, 0x06, 0x19, 0x1c, 0x02, 0x07, 0x66, 0x07, 0x00, 0x00, 0x00, 0x00
abRegs80x25:
db 0x72, 0x50, 0x5a, 0x10, 0x1f, 0x06, 0x19, 0x1c, 0x02, 0x07, 0x66, 0x07, 0x00, 0x00, 0x00, 0x00
abRegsGfx:
db 0x39, 0x28, 0x2d, 0x10, 0x7f, 0x06, 0x64, 0x70, 0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00
abRegsMono:
db 0x72, 0x50, 0x5a, 0x10, 0x1f, 0x06, 0x19, 0x1c, 0x02, 0x07, 0x06, 0x07, 0x00, 0x00, 0x00, 0x00
; wSize40x25
dw 0x3E8
; wSize80x25
dw 0x7d0
; wSizeLoRes
dw 0x3e80
; wSizeHiRes
dw 0x3e80
; abClmCnts (Text columns in each mode)
db 0x28, 0x28, 0x50, 0x50, 0x28, 0x28, 0x50, 0x00
; abModeCodes (port 3d8 values for each mode)
db 0x0c, 0x08, 0x02, 0x09, 0x0a, 0x0e, 0x1a, 0x00
%if (NO_CGA_GLYPHS != 1)
; CGA 8x8 font
; Do not move! Need to be placed in 0x1A6E
times 0x1A6E - $ + image_start db 0x90
%include "data\cgafont.asm"
%endif
int41:
; Hard disk parameter table
; Hard-coded to save space
; 203 * 16 * 63 * 512 = 104767488 bytes = 99.9 MB
hdd_cyls:
dw HDD_CYLINDERS - 1 ; Number of cyls minus 1
hdd_heads:
db HDD_HEADS - 1 ; Number of heads minus 1
dw 0 ; Starting reduced-write current cylinder
dw 0 ; Starting write precompensation cylinder
db 0 ; Maximum ECC data burst length
db 0xC0 ; Disable retries (bit 7), Disable ECC (bit 6)
db 0 ; Standard timeout value
db 0 ; Timeout value for format drive
db 0 ; Timeout value for check drive
dw 0 ; Reserved
hdd_sectors:
db HDD_SECTORS ; Sectors per track
db 0
; Fill unused area with NOPs
times 8192 - 16 - $ + image_start db 0x90
bootentry:
jmp (0F000h):start
db "08/29/87"
db 0x00
db 0xFC ; Machine type (XT)
db 0x55 ; Checksum