-
Notifications
You must be signed in to change notification settings - Fork 2
/
bios_22f_IV.s
413 lines (375 loc) · 9.13 KB
/
bios_22f_IV.s
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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; CONSTANTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
io_00_serial_baud_rate: EQU 0x00
io_1c_system_bits: EQU 0x1c
system_bit_motors_neg: EQU 6
system_bit_bank: EQU 7
reset: EQU 0x0000
iobyte: EQU 0x0003
user_drive: EQU 0x0004
bdos_ep: EQU 0x0005
cpm_boot: EQU 0xe400
bdos_entrypoint: EQU 0xec06
rom_stack: EQU 0xfc00
disk_DMA_address: EQU 0xfc14 ; 2 bytes
ram_e407: EQU 0xe407
; See CP/M 2.2 System alteration guide appendix G
rw_type_directory_write: EQU 1
; Info to reload CP/M from disk
; Like on the ROM, it only works with double density disks
logical_sector_size: EQU 128
double_density_sectors_per_track: EQU 40
double_density_sectors_for_directory: EQU 16
boot_sectors: EQU 44
; On the actual CP/M 2.2 disks this number is higher
; - 48 sectors on CPM 2.2f
; - 55 sectors on CPM 2.2 SP or DE
ROM_INITDSK: EQU 0x03
ROM_HOME: EQU 0x0c
ROM_SELDSK: EQU 0x0f
ROM_SETTRK: EQU 0x12
ROM_SETSEC: EQU 0x15
ROM_SETDMA: EQU 0x18
ROM_READ: EQU 0x1b
ROM_WRITE: EQU 0x1e
ROM_SECTRAN: EQU 0x21
ROM_KBDSTAT: EQU 0x2a
ROM_KBDIN: EQU 0x2d
ROM_SIOSTI: EQU 0x33
ROM_SIOIN: EQU 0x36
ROM_SIOOUT: EQU 0x39
ROM_LISTST: EQU 0x3c
ROM_LIST: EQU 0x3f
ROM_SERSTO: EQU 0x42
ROM_VIDOUT: EQU 0x45
; IOBYTE Mappings:
;
; Bits Bits 6,7 Bits 4,5 Bits 2,3 Bits 0,1
; Device LIST PUNCH READER CONSOLE
;
; Value
; 00 TTY: TTY: TTY: TTY:
; 01 CRT: PTP: PTR: CRT:
; 10 LPT: UP1: UR1: BAT:
; 11 UL1: UP2: UR2: UC1:
iobyte_console_mask: EQU 0x03
iobyte_list_mask: EQU 0xc0
iobyte_list_CRT: EQU 0x40
iobyte_list_PRT: EQU 0x80
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; BIOS ENTRY POINTS
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ORG 0fa00h
JP BOOT
EP_WBOOT:
JP WBOOT
JP CONST
JP CONIN
JP CONOUT
JP LIST
JP PUNCH
JP READER
JP HOME
JP SELDSK
JP SETTRK
JP SETSEC
JP SETDMA
JP READ
JP WRITE
JP LISTST
JP SECTRAN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; BIOS CONFIGURATION
;
; Using CONFIG.COM
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
iobyte_default:
DB 81h ; Console to CRT and List to parallel port
bios_config:
DB 00h
key_maps:
arrow_key_map: ; Mapping for the arrow keys
DB 0Bh, 0Ah, 08h, 0Ch
keypad_map: ; Mapping for the keypad
DB "0123"
DB "4567"
DB "89-,"
DB "\r."
baud_rate_default:
DB 05h
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
; BOOT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
BOOT:
CALL INITDSK
; User 0, drive 0
XOR A
LD (user_drive), A
; Reset iobyte
LD A, (iobyte_default)
LD (iobyte), A
; Reset serial baud rate
LD A, (baud_rate_default)
OUT (io_00_serial_baud_rate), A
CALL WRITE_STRING_INLINE
DB 1Ah,"\r\nKAYPRO IV 64k CP/M vers 2.2\r\n",0
BOOT_SILENT:
LD A,0xc3 ; JP opcode
; Set the WBOOT jump at address 0
LD HL, EP_WBOOT
LD (reset), A
LD (reset+1), HL
; Set BDOS entry point at address 5
LD HL, bdos_entrypoint
LD (bdos_ep), A
LD (bdos_ep+1), HL
; Continue boot to CP/M
LD A, (user_drive)
LD C, A
JP cpm_boot
WBOOT:
CALL INITDSK
CALL WRITE_STRING_INLINE
DB "\r\nWarm Boot\r\n",0
WBOOT_SILENT:
; Reset the stack
LD SP, 0x100
; Select drive 0, track 0
LD C, 0x0
CALL SELDSK
LD BC, 0x0
CALL SETTRK
; Set DMA address to where CP/M must be is
LD HL, cpm_boot
LD (disk_DMA_address), HL
; Read 44 sectors, start on sector 1
LD BC, (boot_sectors * 256) + 1
WBOOT_LOOP:
PUSH BC
CALL SETSEC
CALL READ
POP BC
; Read error?
OR A
; Yes, restart at track 0, sector 1
JR NZ, WBOOT_SILENT
; No, increase DMA by 128 bytes for the next sector
LD HL, (disk_DMA_address)
LD DE, logical_sector_size
ADD HL, DE
LD (disk_DMA_address), HL
; Store 0 in e407. Why?
XOR A
LD (ram_e407), A
; Are we done?
DEC B
; Yes, boot CP/M
JP Z, BOOT_SILENT
; No, next sector
INC C
; Are we past the last sector of track 0
LD A, double_density_sectors_per_track
CP C
; No, read the next sector
JP NZ, WBOOT_LOOP
; Yes, track 0 completed. Continue with track 1
; Skip the 16 sectors used for the directory
LD C, double_density_sectors_for_directory
PUSH BC
LD C, 0x1
CALL SETTRK
POP BC
; Read the next sector
JR WBOOT_LOOP
CONST:
; Make sure the disk is off once every 256 calls
LD HL, CONST_COUNTER
INC (HL)
CALL Z, DISKOFF
; What is the console assigned device?
LD A, (iobyte)
AND iobyte_console_mask
LD L, ROM_SIOSTI
; It's the serial port, query the serial port
JP Z, ROM_JUMP
; It's the CRT, query the keyboard
LD L, ROM_KBDSTAT
JP ROM_JUMP
CONIN:
CALL DISKOFF
; What is the console assigned device?
LD A, (iobyte)
AND iobyte_console_mask
LD L, ROM_SIOIN
; It's the TTY, query the serial port
JP Z, ROM_JUMP
; It's the CRT, query the keyboard
LD L, ROM_KBDIN
CALL ROM_JUMP
; Process the keyboard output
OR A
; If < 0x80, return it
RET P
; Let's check the config to map the arrow keys and the numeric keypad
; The ROM returns 80-83 for the arrows and 84-91 for the keypad
; Apply the mapping
AND 0x1f
LD HL, key_maps
LD C, A
LD B, 0x0
ADD HL, BC
LD A, (HL)
RET
DISKOFF:
IN A, (io_1c_system_bits)
SET system_bit_motors_neg, A
OUT (io_1c_system_bits), A
RET
CONOUT:
; What is the console assigned device?
LD A, (iobyte)
AND iobyte_console_mask
LD L, ROM_SIOOUT
; It's the TTY, write to the serial port
JP Z, ROM_JUMP
; It's the CRT, write to video memory
LD L, ROM_VIDOUT
JP ROM_JUMP
READER:
; Only the TTY is supported
LD L, ROM_SIOIN
JP ROM_JUMP
PUNCH:
; Only the TTY is supported
LD L, ROM_SIOOUT
JP ROM_JUMP
LIST:
; What is the list assigned device?
LD A,(iobyte)
AND iobyte_list_mask
; It's the TTY, write to the serial port
LD L, ROM_SIOOUT
JP Z, ROM_JUMP
LD L, ROM_LIST
CP iobyte_list_PRT
; It's the PRT, write to the parallel port
JP Z, ROM_JUMP
LD L, ROM_VIDOUT
CP iobyte_list_CRT
; It's the CRT, write to video memory
JP Z, ROM_JUMP
LD L, ROM_SIOOUT
; It's the UL1, write to the serial port
JP ROM_JUMP
LISTST:
; What is the list assigned device?
LD A,(iobyte)
AND iobyte_list_mask
; It's the TTY, query the serial port
LD L, ROM_SERSTO
JP Z, ROM_JUMP
LD L, ROM_LISTST
CP iobyte_list_PRT
; It's the PRT, write to the parallel port
JP Z, ROM_JUMP
; It's the CRT or UL1, return always 0
XOR A
RET
INITDSK:
LD L, ROM_INITDSK
JR ROM_JUMP
HOME:
LD L, ROM_HOME
JR ROM_JUMP
SELDSK:
LD L, ROM_SELDSK
JR ROM_JUMP
SETTRK:
LD L, ROM_SETTRK
JR ROM_JUMP
SETSEC:
LD L, ROM_SETSEC
JR ROM_JUMP
SETDMA:
LD L, ROM_SETDMA
JR ROM_JUMP
READ:
; Reset the CONST counter
XOR A
LD (CONST_COUNTER), A
; Read
LD L, ROM_READ
JR ROM_JUMP
WRITE:
; Reset the CONST counter
XOR A
LD (CONST_COUNTER), A
; Write
LD L,ROM_WRITE
; Is the bios_config 0?
LD A,(bios_config)
OR A
; Yes, write normally
JR Z, ROM_JUMP
; No, force the write type
LD C, rw_type_directory_write
JR ROM_JUMP
SECTRAN:
LD L, ROM_SECTRAN
JR ROM_JUMP
ROM_JUMP:
EXX
; Activate the ROM bank
IN A, (io_1c_system_bits)
SET system_bit_bank, A
OUT (io_1c_system_bits), A
; Switch to the ROM stack
LD (STACK_SAVE), SP
LD SP, rom_stack
; Push the ROM cleanup code to RET there
LD DE, ROM_JUMP_CLEANUP
PUSH DE
EXX
; Jump to the ROM address in L, page 0
LD H,0x0
JP (HL)
ROM_JUMP_CLEANUP:
EX AF, AF' ;'
; Restore the stack
LD SP, (STACK_SAVE)
; Deactivate the ROM bank
IN A, (io_1c_system_bits)
RES system_bit_bank, A
OUT (io_1c_system_bits), A
EX AF, AF' ; '
; Return to the caller
RET
WRITE_STRING_INLINE:
; Retrieve the char at the return address in the stack
EX (SP), HL
LD A, (HL)
; Increase the return address
INC HL
EX (SP), HL
; Is the char a null?
OR A
; Yes, return
RET Z
; No, write it
LD C, A
CALL CONOUT
; Process the next char
JR WRITE_STRING_INLINE
CONST_COUNTER:
DB $04
STACK_SAVE:
DW $EF35
FILLER:
DS 14, $00
DB $8E, $04, $8E, $04
DB $03, $04, $88, $9C
DB $C5, $04, $53, $FF
DB $14, $05, $0F, $04
DB $13, $06
DW ROM_JUMP_CLEANUP ; not used