This repository has been archived by the owner on Sep 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkernel.asm
375 lines (275 loc) · 10.1 KB
/
kernel.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
; ==================================================================
; Copyright (C) 2015 - present alOS Developers -- see LICENSE.md
; Copyright (C) 2006 - 2010 MikeOS Developers -- see LICENSE.md
;
; This is loaded from the floppy/CD, by BOOTLOAD.BIN, as KERNEL.BIN.
; First we have the system call vectors, which start at a static point
; for programs to jump to. Following that is the main kernel code and
; then additional system call code is included.
; ==================================================================
BITS 16
%DEFINE ALOS_VER '4.1' ; OS version number
%DEFINE ALOS_API_VER 13 ; API version for programs to check
; This is the location in RAM for kernel disk operations, 24K
; after the point where the kernel has loaded; it's 8K in size,
; because external programs load after it at the 32K point:
disk_buffer equ 24576
; ------------------------------------------------------------------
; OS CALL VECTORS -- Static locations for system call vectors
; Note: these cannot be moved, or it'll break the calls!
; The comments show exact locations of instructions in this section,
; and are used in programs/mikedev.inc so that an external program can
; use a MikeOS system call without having to know its exact position
; in the kernel source code...
os_call_vectors:
jmp os_main ; 0000h -- Called from bootloader
jmp os_print_string ; 0003h
jmp os_move_cursor ; 0006h
jmp os_clear_screen ; 0009h
jmp os_print_horiz_line ; 000Ch
jmp os_print_newline ; 000Fh
jmp os_wait_for_key ; 0012h
jmp os_check_for_key ; 0015h
jmp os_int_to_string ; 0018h
jmp os_speaker_tone ; 001Bh
jmp os_speaker_off ; 001Eh
jmp os_load_file ; 0021h
jmp os_pause ; 0024h
jmp os_fatal_error ; 0027h
jmp os_draw_background ; 002Ah
jmp os_string_length ; 002Dh
jmp os_string_uppercase ; 0030h
jmp os_string_lowercase ; 0033h
jmp os_input_string ; 0036h
jmp os_string_copy ; 0039h
jmp os_dialog_box ; 003Ch
jmp os_string_join ; 003Fh
jmp os_get_file_list ; 0042h
jmp os_string_compare ; 0045h
jmp os_string_chomp ; 0048h
jmp os_string_strip ; 004Bh
jmp os_string_truncate ; 004Eh
jmp os_bcd_to_int ; 0051h
jmp os_get_time_string ; 0055h
jmp os_get_api_version ; 0057h
jmp os_file_selector ; 005Ah
jmp os_get_date_string ; 005Dh
jmp os_send_via_serial ; 0060h
jmp os_get_via_serial ; 0063h
jmp os_find_char_in_string ; 0066h
jmp os_get_cursor_pos ; 0069h
jmp os_print_space ; 006Ch
jmp os_dump_string ; 006Fh
jmp os_print_digit ; 0072h
jmp os_print_1hex ; 0075h
jmp os_print_2hex ; 0078h
jmp os_print_4hex ; 007Bh
jmp os_long_int_to_string ; 007Eh
jmp os_long_int_negate ; 0081h
jmp os_set_time_fmt ; 0084h
jmp os_set_date_fmt ; 0087h
jmp os_show_cursor ; 008Ah
jmp os_hide_cursor ; 008Dh
jmp os_dump_registers ; 0090h
jmp os_string_strincmp ; 0093h
jmp os_write_file ; 0096h
jmp os_file_exists ; 0099h
jmp os_create_file ; 009Ch
jmp os_remove_file ; 009Fh
jmp os_rename_file ; 00A2h
jmp os_get_file_size ; 00A5h
jmp os_input_dialog ; 00A8h
jmp os_list_dialog ; 00ABh
jmp os_string_reverse ; 00AEh
jmp os_string_to_int ; 00B1h
jmp os_draw_block ; 00B4h
jmp os_get_random ; 00B7h
jmp os_string_charchange ; 00BAh
jmp os_serial_port_enable ; 00BDh
jmp os_sint_to_string ; 00C0h
jmp os_string_parse ; 00C3h
jmp os_run_basic ; 00C6h
jmp os_port_byte_out ; 00C9h
jmp os_port_byte_in ; 00CCh
; ------------------------------------------------------------------
; START OF MAIN KERNEL CODE
os_main:
cli ; Clear interrupts
mov ax, 0
mov ss, ax ; Set stack segment and pointer
mov sp, 0FFFFh
sti ; Restore interrupts
cld ; The default direction for string operations
; will be 'up' - incrementing address in RAM
mov ax, 2000h ; Set all segments to match where kernel is loaded
mov ds, ax ; After this, we don't need to bother with
mov es, ax ; segments ever again!
mov fs, ax
mov gs, ax
mov ax, 1003h ; Set text output with certain attributes
mov bx, 0 ; to be bright, and not blinking
int 10h
call os_seed_random ; Seed random number generator
; First up, let's see if there's a file called AUTORUN.BIN and execute
; it if so, before going to the program launcher menu
mov ax, autorun_bin_file_name
call os_file_exists
jc no_autorun_bin ; Skip next three lines if AUTORUN.BIN doesn't exist
mov cx, 32768 ; Otherwise load the program into RAM...
call os_load_file
jmp execute_bin_program ; ...and move on to the executing part
; Perhaps there's an AUTORUN.BAS however?
no_autorun_bin:
mov ax, autorun_bas_file_name
call os_file_exists
jc option_screen ; Skip next five lines if AUTORUN.BAS doesn't exist
mov cx, 32768 ; Otherwise load the program into RAM
call os_load_file
call os_clear_screen
mov ax, 32768
call os_run_basic ; Run the kernel's BASIC interpreter
jmp app_selector ; And go to the app selector menu when BASIC ends
; Now we display a dialog box offering the user a choice of
; a menu-driven program selector, or a command-line interface
option_screen:
mov ax, os_init_msg ; Set up the welcome screen
mov bx, os_version_msg
mov cx, 10011111b ; Colour: white text on light blue
call os_draw_background
mov ax, dialog_string_1 ; Ask if user wants app selector or command-line
mov bx, dialog_string_2
mov cx, dialog_string_3
mov dx, 1 ; We want a two-option dialog box (OK or Cancel)
call os_dialog_box
cmp ax, 1 ; If OK (option 0) chosen, start app selector
jne near app_selector
call os_clear_screen ; Otherwise clean screen and start the CLI
call os_command_line
jmp option_screen ; Offer menu/CLI choice after CLI has exited
; Data for the above code...
os_init_msg db 'Welcome to the alOS', 0
os_version_msg db 'Version ', ALOS_VER, 0
dialog_string_1 db 'Thanks for trying out alOS!', 0
dialog_string_2 db 'Please select an interface: OK for the', 0
dialog_string_3 db 'program menu, Cancel for command line.', 0
app_selector:
mov ax, os_init_msg ; Draw main screen layout
mov bx, os_version_msg
mov cx, 10011111b ; Colour: white text on light blue
call os_draw_background
call os_file_selector ; Get user to select a file, and store
; the resulting string location in AX
; (other registers are undetermined)
jc option_screen ; Return to the CLI/menu choice screen if Esc pressed
mov si, ax ; Did the user try to run 'KERNEL.BIN'?
mov di, kern_file_name
call os_string_compare
jc no_kernel_execute ; Show an error message if so
; Next, we need to check that the program we're attempting to run is
; valid -- in other words, that it has a .BIN extension
push si ; Save filename temporarily
mov bx, si
mov ax, si
call os_string_length
mov si, bx
add si, ax ; SI now points to end of filename...
dec si
dec si
dec si ; ...and now to start of extension!
mov di, bin_ext
mov cx, 3
rep cmpsb ; Are final 3 chars 'BIN'?
jne not_bin_extension ; If not, it might be a '.BAS'
pop si ; Restore filename
mov ax, si
mov cx, 32768 ; Where to load the program file
call os_load_file ; Load filename pointed to by AX
execute_bin_program:
call os_clear_screen ; Clear screen before running
mov ax, 0 ; Clear all registers
mov bx, 0
mov cx, 0
mov dx, 0
mov si, 0
mov di, 0
call 32768 ; Call the external program code,
; loaded at second 32K of segment
; (program must end with 'ret')
call os_clear_screen ; When finished, clear screen
jmp app_selector ; and go back to the program list
no_kernel_execute: ; Warn about trying to executing kernel!
mov ax, kerndlg_string_1
mov bx, kerndlg_string_2
mov cx, kerndlg_string_3
mov dx, 0 ; One button for dialog box
call os_dialog_box
jmp app_selector ; Start over again...
not_bin_extension:
pop si ; We pushed during the .BIN extension check
push si ; Save it again in case of error...
mov bx, si
mov ax, si
call os_string_length
mov si, bx
add si, ax ; SI now points to end of filename...
dec si
dec si
dec si ; ...and now to start of extension!
mov di, bas_ext
mov cx, 3
rep cmpsb ; Are final 3 chars 'BAS'?
jne not_bas_extension ; If not, error out
pop si
mov ax, si
mov cx, 32768 ; Where to load the program file
call os_load_file ; Load filename pointed to by AX
call os_clear_screen ; Clear screen before running
mov ax, 32768
call os_run_basic ; And run our BASIC interpreter on the code!
mov si, basic_finished_msg
call os_print_string
call os_wait_for_key
call os_clear_screen
jmp app_selector ; and go back to the program list
not_bas_extension:
pop si
mov ax, ext_string_1
mov bx, ext_string_2
mov cx, 0
mov dx, 0 ; One button for dialog box
call os_dialog_box
jmp app_selector ; Start over again...
; And now data for the above code...
kern_file_name db 'KERNEL.BIN', 0
autorun_bin_file_name db 'AUTORUN.BIN', 0
autorun_bas_file_name db 'AUTORUN.BAS', 0
bin_ext db 'BIN'
bas_ext db 'BAS'
kerndlg_string_1 db 'Cannot load and execute alOS kernel!', 0
kerndlg_string_2 db 'KERNEL.BIN is the core of alOS, and', 0
kerndlg_string_3 db 'is not a normal program.', 0
ext_string_1 db 'Invalid filename extension! You can', 0
ext_string_2 db 'only execute .BIN or .BAS programs.', 0
basic_finished_msg db '>>> BASIC program finished -- press a key', 0
; ------------------------------------------------------------------
; SYSTEM VARIABLES -- Settings for programs and system calls
; Time and date formatting
fmt_12_24 db 0 ; Non-zero = 24-hr format
fmt_date db 0, '/' ; 0, 1, 2 = M/D/Y, D/M/Y or Y/M/D
; Bit 7 = use name for months
; If bit 7 = 0, second byte = separator character
; ------------------------------------------------------------------
; FEATURES -- Code to pull into the kernel
%INCLUDE "features/cli.asm"
%INCLUDE "features/basic.asm"
%INCLUDE "features/disk.asm"
%INCLUDE "features/keyboard.asm"
%INCLUDE "features/math.asm"
%INCLUDE "features/misc.asm"
%INCLUDE "features/ports.asm"
%INCLUDE "features/screen.asm"
%INCLUDE "features/sound.asm"
%INCLUDE "features/string.asm"
; ==================================================================
; END OF KERNEL
; ==================================================================