Blob


1 [bits 16]
2 [cpu 286]
4 %include "macros.inc"
6 ; Floppy Registers
7 SRA equ 0x3f0 ; Status Register A RO
8 SRB equ 0x3f1 ; Status Register B RO
9 DOR equ 0x3f2 ; Digital Output Register RW
10 TDR equ 0x3f3 ; Tape Drive Register RW
11 MSR equ 0x3f4 ; Main Status Register RO
12 DSR equ 0x3f4 ; Datarate Select Register WO
13 DFF equ 0x3f5 ; Data FIFO RW
14 DIR equ 0x3f7 ; Digital Input Register RO
15 CCR equ 0x3f7 ; Configuration Control Register WO
17 DOR_MOTD equ 0x80 ; Drive Motor 3 ON
18 DOR_MOTC equ 0x40 ; Drive Motor 2 ON
19 DOR_MOTB equ 0x20 ; Drive Motor 1 ON
20 DOR_MOTA equ 0x10 ; Drive Motor 0 ON
21 DOR_IRQ equ 0x08 ; Enable IRQs and DMA
22 DOR_NRESET equ 0x04 ; 0=enter reset mode, 1=normal operation
23 DOR_DSEL equ 0x03 ; Select drive number mask
25 MSR_RQM equ 0x80 ; It's OK (or mandatory) to exchange bytes with the FIFO IO port
26 MSR_DIO equ 0x40 ; FIFO expects an IN opcode
27 MSR_NDMA equ 0x20 ; Set while executing PIO mode read/write commands
28 MSR_BSY equ 0x10 ; Command Busy
29 MSR_ACTD equ 0x08 ; Drive 3 is seeking
30 MSR_ACTC equ 0x04 ; Drive 2 is seeking
31 MSR_ACTB equ 0x02 ; Drive 1 is seeking
32 MSR_ACTA equ 0x01 ; Drive 0 is seeking
34 DSR_288M equ 0x03 ; 2.88M floppy (1Mbps)
35 DSR_144M equ 0x00 ; 1.44M floppy (500Kbps)
37 ; Floppy drive commands
38 CMD_READ_TRACK equ 2 ; IRQ6
39 CMD_SPECIFY equ 3 ; set drive parameters
40 CMD_SENSE_DRIVE_STATUS equ 4
41 CMD_WRITE_DATA equ 5 ; write to disk
42 CMD_READ_DATA equ 6 ; read from disk
43 CMD_RECALIBRATE equ 7 ; seek to cylinder 0
44 CMD_SENSE_INTERRUPT equ 8 ; ack IRQ6, get status of last command
45 CMD_WRITE_DELETED_DATA equ 9
46 CMD_READ_ID equ 10 ; IRQ6
47 CMD_READ_DELETED_DATA equ 12
48 CMD_FORMAT_TRACK equ 13
49 CMD_DUMPREG equ 14
50 CMD_SEEK equ 15 ; seek both heads to cylinder X
51 CMD_VERSION equ 16 ; used during initialization, once
52 CMD_SCAN_EQUAL equ 17
53 CMD_PERPENDICULAR_MODE equ 18 ; used during initialization, once, maybe
54 CMD_CONFIGURE equ 19 ; set controller parameters
55 CMD_LOCK equ 20 ; protect controller parameters from reset
56 CMD_VERIFY equ 22
57 CMD_SCAN_LOW_OR_EQUAL equ 25
58 CMD_SCAN_HIGH_OR_EQUAL equ 29
60 CMD_MT equ 0x80 ; multitrack mode
61 CMD_MF equ 0x40 ; "MFM" magnetic encoding mode. Always set for read/write/format/verify
62 CMD_SK equ 0x20 ; Skip mode. Skips "deleted sectors"
65 section .text
66 extern puthexb
67 extern putchar
68 extern puts
69 extern panic
70 global floppy_init
71 floppy_init:
72 lea bp, [fimsg]
73 call puts
75 ; read what types of floppy drives we have
76 outb_slow 0x70, 0x90
77 in al, 0x71
78 mov byte [floppy_type], al
79 call puthexb
81 mov al, 10
82 call putchar
84 lea bp, [vermsg]
85 call puts
87 call version
88 call puthexb
90 mov al, 10
91 call putchar
93 ret
95 wait_rqm:
96 push cx
97 mov cx, 128
99 .again:
100 dec cx
101 jz error
103 mov dx, MSR
104 in al, dx
105 test al, MSR_RQM
106 jz .again
108 pop cx
109 ret
111 version:
112 mov cx, 3
114 .begin:
115 dec cx
116 jz error
118 ; wait for the controller
119 mov dx, MSR
120 in al, dx
121 and al, (MSR_RQM | MSR_DIO)
122 cmp al, MSR_RQM
123 jne .reset
125 ; send version command
126 mov al, CMD_VERSION
127 mov dx, DFF
128 out dx, al
130 ; wait for version byte
131 call wait_rqm
132 test al, MSR_DIO
133 jz error
135 ; read version byte
136 mov dx, DFF
137 in al, dx
138 mov bl, al
140 ; wait for completion
141 call wait_rqm
143 ; verify result
144 mov dx, MSR
145 in al, dx
146 and al, (MSR_RQM | MSR_BSY | MSR_DIO)
147 cmp al, MSR_RQM
148 jne .retry
149 mov al, bl
150 ret
152 .retry:
153 push cx
154 lea bp, [rtmsg]
155 call puts
156 pop cx
157 jmp .begin
159 .reset:
160 push cx
161 call reset
162 pop cx
163 jmp .begin
165 reset:
166 lea bp, [rsmsg]
167 jmp puts
169 error:
170 lea bp, [errstr]
171 jmp panic
173 ; ax LBA
174 ; return:
175 ; - bx cyl
176 ; - cx head
177 ; - dx sector
178 lba_to_cha:
179 xor dx, dx
180 mov cx, 36
181 div cx
182 mov bx, ax ; cyl
183 mov ax, dx
184 xor dx, dx
185 mov cx, 18
186 div cx
187 mov cx, ax ; head
188 inc dx ; sector
189 ret
191 section .rodata
192 errstr:
193 db "An error occured in the floppy driver", 10, 0
194 fimsg:
195 db "Floppy drive type: 0x", 0
196 vermsg:
197 db "Floppy drive version: 0x", 0
198 rsmsg:
199 db "Resetting floppy drive...", 10, 0
200 rtmsg:
201 db "Retrying last operating", 10, 0
203 section .bss
204 floppy_type:
205 resb 1