os-z80/vfs.z80

318 lines
4.5 KiB
Z80 Assembly

.global _start
_start:
call get_free_frame
ld b, c
ld e, l
ld d, h
ld c, 0x9
call set_frame
call get_free_frame
ld a, c
ld (send_page_addr), a
ld (send_page_addr+1), hl
ld b, c
ld e, l
ld d, h
ld c, 0x8
call set_frame
ld hl, 0x9000
ld de, 0x9001
ld bc, 0x1000
ld (hl), 0x0
ldir
call get_free_mailbox
push hl
ld de, 2
call proc_map_set
pop hl
ld (vfs_mailbox_num), hl
call get_free_mailbox
ld (fs_mailbox_num), hl
msg_loop:
call yield
yield_back:
ld hl, (vfs_mailbox_num)
call mb_read
mb_read_done:
ld a, b
cp 1
jp z, msg_loop
msg_read:
push bc
push de
push hl
ld b, c
ld c, 0xB
call set_frame
set_frame_done:
ld hl, (0xB000) ; Load destination mailbox into HL and save it on the stack
push hl
ld hl, 0xB002
; Handling code goes here
ld a, (hl)
inc hl
cp 0x0
jp z, register_fs
cp 0x1
jp z, mount
cp 0x2
jp z, open
operation_done:
pop hl
pop ix
pop de
pop bc
call mb_send
jp msg_loop
register_fs:
ld a, (fs_count)
ld l, a
ld h, 0x0
add hl, hl
add hl, hl
add hl, hl
add hl, hl
ld de, data_fs_offset
add hl, de
ld de, 0x9000
add hl, de
push hl
pop de
ld hl, 0xB003
ld bc, 16
ldir
ld a, (fs_count)
inc a
ld (fs_count), a
jp operation_done
; String pointer 1 in IX, 2 in IY
; Return in A
strcmp:
strcmp_loop:
ld a, (iy)
ld b, a
ld a, (ix)
cp b
jp nz, strcmp_loopend
cp 0
ret z
inc ix
inc iy
jp strcmp_loop
strcmp_loopend:
ld a, (iy)
ld b, a
ld a, (ix)
sub b
ret
mount:
ld a, (mount_count)
ld l, 0x0
ld h, a
ld de, 0x9000 + data_mount_offset
add hl, de
push hl
pop de
push de
ld hl, 0xB003
ld bc, 0xFC
ldir
; FS type at 0xB1FF
ld a, (fs_count)
ld b, a
ld hl, data_fs_offset + 0x9000
ld de, 0x10
mount_loop1:
push hl
pop ix
ld iy, 0xB1FF
push bc
call strcmp
pop bc
cp 0
jp mount_loop1_done
add hl, de
djnz mount_loop1
mount_loop1_done:
; hl -> pointer to fs type info
; top of stack -> pointer to open mount info slot
ld de, 14
add hl, de
pop ix
; hl -> pointer to byte 15 of fs type info
; ix -> pointer to open mount info slot
ld de, 0xFD
add ix, de
; ix -> pointer to offset 0xFD of mount info
; fs info word starting at 0xE -> mount info word starting at 0xFD
ld e, (hl)
inc hl
ld d, (hl)
ld (ix), e
ld (ix+1), d
ld hl, (fs_mailbox_num)
ld (0x8000), hl
ld a, 0x2
ld (0x8002), a
push ix
push de
ld hl, 0xB0FF
ld de, 0x8003
ld bc, 256
ldir
ld a, (send_page_addr)
ld c, a
ld de, (send_page_addr+1)
ld ix, 0x0
pop hl
call mb_send
mount_wait_loop:
call yield
ld hl, (fs_mailbox_num)
call mb_read ; Read a message
ld a, b ; Loop if there is no message
cp 0
jp nz, mount_wait_loop
pop ix
; response byte 1 -> mount info byte at offset 0xFF
ld a, (0x8000)
ld (ix+2), a
ld a, (mount_count)
inc a
ld (mount_count), a
jp operation_done
; String pointer 1 in IX, 2 in IY
; Return in A
vfsstrcmp:
vfsstrcmp_loop:
ld a, (iy)
ld b, a
ld a, (ix)
cp b
jp nz, vfsstrcmp_loopend
cp 0
ret z
inc ix
inc iy
jp vfsstrcmp_loop
vfsstrcmp_loopend:
ld a, (ix)
cp 0
ret z
ld a, (iy)
ld b, a
ld a, (ix)
sub b
ret
open:
; Scan mountpoints for largest prefix matching path
ld hl, data_mount_offset + 0x9000
ld bc, 0
ld de, 0
open_mount_scan:
ld a, (hl)
cp 0
jp z, open_mount_scan_done
push hl
pop ix
ld iy, 0xB003
push bc
call vfsstrcmp
pop bc
cp 0
jp nz, open_no_prefix
push hl
pop de
call strlen
open_no_prefix:
ld a, h
inc a
ld h, a
ld l, 0
jp open_mount_scan
open_mount_scan_done:
; Send open request to FS w/ path-prefix and mount ID
ld hl, 0xB003
add hl, bc
push de
; mountpoint path -> 0x8003-0x8103 (exclusive end)
ld de, 0x8003
ld bc, 0x100
ldir
pop de
; mount info byte 0xFF -> 0x8103
ld e, 0xFF
ld a, (de)
ld (0x8103), a
; imm 0x0 -> 0x8002
ld a, 0x0
ld (0x8002), a
; fs_mailbox_num (16bit) -> 0x8000,0x8001
ld hl, 0x8000
ld bc, (fs_mailbox_num)
ld (hl), c
inc hl
ld (hl), b
; mount info word 0xFD -> hl
ld e, 0xFD
ld a, (de)
ld l, a
inc de
ld a, (de)
ld h, a
push hl
ld a, (send_page_addr)
ld c, a
ld de, (send_page_addr+1)
ld ix, 0x0
call mb_send
open_wait_loop:
call yield
ld hl, (fs_mailbox_num)
call mb_read ; Read a message
ld a, b ; Loop if there is no message
cp 0
jp nz, open_wait_loop
pop hl
ld a, (0x8002)
cp 0
jp nz, mb_override
ld a, (0x8003)
cp 0
jp nz, mb_override
jp no_mb_override
mb_override:
ld hl, (0x8002)
no_mb_override:
ld (0xB002), hl
; word at 0x8000 -> 0xB000
ld a, (0x8000)
ld (0xB000), a
ld a, (0x8001)
ld (0xB001), a
jp operation_done
vfs_mailbox_num: .ds.b 2
fs_mailbox_num: .ds.b 2
send_page_addr: .ds.b 3
fs_count: .ds.b 1
mount_count: .ds.b 1
.equ data_fs_offset, 0x0
.equ data_mount_offset, 0x200
.equ fs_name_offset, 0x0
.equ fs_pid_offset, 0xE
.equ mount_path_offset, 0x0
.equ mount_fs_pid_offset, 0xFD
.equ mount_fs_id_offset, 0xFF