-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbootloader.dasm
290 lines (265 loc) · 4.71 KB
/
bootloader.dasm
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
; KISS bootloader
; looks for a boot sector in every connected floppy and loads it in memory at location 0.
; Prompts for boot choice if there is a possible choice
; Sets A to the floppy the bootsector was loaded from.
; Sets B to an array of device structs containing all of the devices detected
;
; ==================
; Boot sector description
;
; u16[3] reserved
; u16[10] name in boot menu (not compressed, stops at first 0 found or 10)
; u16[498] boot sector specific data
; u16 magic word (0x55AA)
;
; ==================
; Array
;
; u16 array_size
; Device_struct* array
;
; ==================
; Device_struct
;
; u16 ID
; u16 hardware ID A
; u16 hardware ID B
; u16 hardware version
; u16 manufacturer ID A
; u16 manufacturer ID B
;
; =================
#define SECTOR_LOCATION 0xD000
#define FLOPPY_STATE_BUSY 3
#define SECTOR_SIZE 0x200
#define STANDARD_SCREEN 0x7000
#define STANDARD_MEDIA 0x4000
#define STANDARD_KEYBOARD 0x3000
#define COPY_LOCATION 0xF000
#define DEVICES_LOCATION 0xE000
#define SCREEN_LOCATION 0x8000
#define SCREEN_SIZE 0x200
#define COLOR 0xF000
#define KEY_ENTER 0x11
#define KEY_UP 0x80
#define KEY_DOWN 0x81
jsr detect_peripherals
ife [n_media], 0
set PC, error
ife [screen], 0xFFFF
set PC, no_screen_continue
set A, 0
set B, SCREEN_LOCATION
hwi [screen] ;Mapping screen
jsr clear_screen
:no_screen_continue
set I, KISS_welcome
set X, 3
set Y, 0
jsr print
set Y, 2
jsr find_bootsector
ife Z, 0xFFFF
set PC, error_print
set B, DEVICES_LOCATION
set A, Z
set I, copy
set J, COPY_LOCATION
set C, copy_end + 1 - copy
jsr memcpy
set I, memcpy
set J, COPY_LOCATION + copy_end - copy
set C, memcpy_end + 1 - memcpy
jsr memcpy
set PUSH, 0
set PC, COPY_LOCATION
:error_print
set I, error_msg
jsr print
:error
log [keyboard]
log [screen]
log [n_media]
log [media]
sub PC, 1
:copy
set I, SECTOR_LOCATION
set J, 0
set C, SECTOR_SIZE
:copy_end
:find_bootsector
set I, 0
set Z, 0xFFFF
ife I, [n_media]
set PC, POP
:find_bootsector_loop
set A, 2
set X, 0
set PUSH, Y
set Y, SECTOR_LOCATION
hwi [I + media]
set Y, POP
jsr wait
ife [SECTOR_LOCATION + 0x1FF], 0x55AA
jsr sector_found
add I, 1
ifn I, [n_media]
set PC, find_bootsector_loop
jsr select_boot
set PC, POP
:sector_found
set A, bootsectors
:sector_found_loop
ife [A], 0
set PC, sector_found_found
add A, 1
set PC, sector_found_loop
:sector_found_found
set X, A
set [A], [I + media]
set PUSH, I
set I, SECTOR_LOCATION + 0x1F5
set PUSH, X
set X, 2
jsr print
set X, POP
set I, POP
add Y, 1
set PC, POP
:wait
set A, 0
hwi [I + media]
ife B, FLOPPY_STATE_BUSY
set PC, wait
set PC, POP
:select_boot
ife [bootsectors], 0
set PC, POP
ifn [bootsectors], 0
set Z, [bootsectors]
ife [keyboard], 0
set PC, POP
ife [screen], 0xFFFF
set PC, POP
set A, X
sub A, bootsectors - 1
ife A, 1
set PC, POP
add A, 1
set X, 0
set Y, 2
:select_boot_loop
set I, KISS_selector
jsr print
jsr get_key
set I, KISS_no_selector
jsr print
ife C, KEY_ENTER
set PC, POP
ife C, KEY_UP
sub Y, 1
ife C, KEY_DOWN
add Y, 1
ifl Y, 2
set Y, 2
ifg Y, A
set Y, A
set Z, Y
sub Z, 2
set PC, select_boot_loop
:get_key
set PUSH, A
set A, 1
:get_key_loop
hwi [keyboard]
ife C, 0
set PC, get_key_loop
set A, POP
set PC, POP
:add_device
set PUSH, A
set A, [DEVICES_LOCATION]
mul A, 6
add A, 1
add A, DEVICES_LOCATION
set [A], J
set [A + 1], B
set [A + 2], POP
set [A + 3], C
set [A + 4], Y
set [A + 5], X
add [DEVICES_LOCATION], 1
and B, 0xF000
ife B, STANDARD_SCREEN
set [screen], J
ife B, STANDARD_MEDIA
set PC, add_media
ife B, STANDARD_KEYBOARD
set [keyboard], J
set PC, POP
:add_media
ife [n_media], 10
set PC, POP
set A, [n_media]
set [A + media], J
add [n_media], 1
set PC, POP
:detect_peripherals
hwn I
set J, 0
:detect_peripherals_loop
hwq J
jsr add_device
add J, 1
ifn J, I
set PC, detect_peripherals_loop
set PC, POP
; I -> from
; J -> to
; C -> size
:memcpy
ife C, 0
set PC, POP
sti [J], [I]
sub C, 1
sub PC, 5
:memcpy_end
:keyboard .dat 0
:screen .dat 0xFFFF
:n_media .dat 0
:media .dat 0 0 0 0 0 0 0 0 0 0
:bootsectors .dat 0 0 0 0 0 0 0 0 0 0
; I -> char*
; X, Y -> position
:print
ife [screen], 0xFFFF
set PC, POP
set PUSH, J
set PUSH, A
set PUSH, Y
set J, SCREEN_LOCATION
mul Y, 32
add Y, X
add J, Y
:print_loop
set A, [I]
bor A, COLOR
sti [J], A
ifn [I], 0
set PC, print_loop
set Y, POP
set A, POP
set J, POP
set PC, POP
:clear_screen
set A, 0
set I, SCREEN_LOCATION
set J, 0
ife J, SCREEN_SIZE
set PC, POP
sti [I], A
sub PC, 5
:KISS_welcome .dat "==== KISS bootloader ===="
:error_msg .dat "No boot sector found, aborting..."
:KISS_selector .dat ">"
:KISS_no_selector .dat " "